zfs_main.c revision 290759
1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22/* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 25 * Copyright 2012 Milan Jurik. All rights reserved. 26 * Copyright (c) 2012, Joyent, Inc. All rights reserved. 27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>. 28 * All rights reserved. 29 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved. 30 * Copyright (c) 2013 Steven Hartland. All rights reserved. 31 * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 32 */ 33 34#include <assert.h> 35#include <ctype.h> 36#include <errno.h> 37#include <libgen.h> 38#include <libintl.h> 39#include <libuutil.h> 40#include <libnvpair.h> 41#include <locale.h> 42#include <stddef.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <strings.h> 46#include <unistd.h> 47#include <fcntl.h> 48#include <zone.h> 49#include <grp.h> 50#include <pwd.h> 51#include <signal.h> 52#include <sys/list.h> 53#include <sys/mntent.h> 54#include <sys/mnttab.h> 55#include <sys/mount.h> 56#include <sys/stat.h> 57#include <sys/fs/zfs.h> 58#include <sys/types.h> 59#include <time.h> 60#include <err.h> 61#include <jail.h> 62 63#include <libzfs.h> 64#include <libzfs_core.h> 65#include <zfs_prop.h> 66#include <zfs_deleg.h> 67#include <libuutil.h> 68#ifdef sun 69#include <aclutils.h> 70#include <directory.h> 71#include <idmap.h> 72#endif 73 74#include "zfs_iter.h" 75#include "zfs_util.h" 76#include "zfs_comutil.h" 77 78libzfs_handle_t *g_zfs; 79 80static FILE *mnttab_file; 81static char history_str[HIS_MAX_RECORD_LEN]; 82static boolean_t log_history = B_TRUE; 83 84static int zfs_do_clone(int argc, char **argv); 85static int zfs_do_create(int argc, char **argv); 86static int zfs_do_destroy(int argc, char **argv); 87static int zfs_do_get(int argc, char **argv); 88static int zfs_do_inherit(int argc, char **argv); 89static int zfs_do_list(int argc, char **argv); 90static int zfs_do_mount(int argc, char **argv); 91static int zfs_do_rename(int argc, char **argv); 92static int zfs_do_rollback(int argc, char **argv); 93static int zfs_do_set(int argc, char **argv); 94static int zfs_do_upgrade(int argc, char **argv); 95static int zfs_do_snapshot(int argc, char **argv); 96static int zfs_do_unmount(int argc, char **argv); 97static int zfs_do_share(int argc, char **argv); 98static int zfs_do_unshare(int argc, char **argv); 99static int zfs_do_send(int argc, char **argv); 100static int zfs_do_receive(int argc, char **argv); 101static int zfs_do_promote(int argc, char **argv); 102static int zfs_do_userspace(int argc, char **argv); 103static int zfs_do_allow(int argc, char **argv); 104static int zfs_do_unallow(int argc, char **argv); 105static int zfs_do_hold(int argc, char **argv); 106static int zfs_do_holds(int argc, char **argv); 107static int zfs_do_release(int argc, char **argv); 108static int zfs_do_diff(int argc, char **argv); 109static int zfs_do_jail(int argc, char **argv); 110static int zfs_do_unjail(int argc, char **argv); 111static int zfs_do_bookmark(int argc, char **argv); 112 113/* 114 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds. 115 */ 116 117#ifdef DEBUG 118const char * 119_umem_debug_init(void) 120{ 121 return ("default,verbose"); /* $UMEM_DEBUG setting */ 122} 123 124const char * 125_umem_logging_init(void) 126{ 127 return ("fail,contents"); /* $UMEM_LOGGING setting */ 128} 129#endif 130 131typedef enum { 132 HELP_CLONE, 133 HELP_CREATE, 134 HELP_DESTROY, 135 HELP_GET, 136 HELP_INHERIT, 137 HELP_UPGRADE, 138 HELP_JAIL, 139 HELP_UNJAIL, 140 HELP_LIST, 141 HELP_MOUNT, 142 HELP_PROMOTE, 143 HELP_RECEIVE, 144 HELP_RENAME, 145 HELP_ROLLBACK, 146 HELP_SEND, 147 HELP_SET, 148 HELP_SHARE, 149 HELP_SNAPSHOT, 150 HELP_UNMOUNT, 151 HELP_UNSHARE, 152 HELP_ALLOW, 153 HELP_UNALLOW, 154 HELP_USERSPACE, 155 HELP_GROUPSPACE, 156 HELP_HOLD, 157 HELP_HOLDS, 158 HELP_RELEASE, 159 HELP_DIFF, 160 HELP_BOOKMARK, 161} zfs_help_t; 162 163typedef struct zfs_command { 164 const char *name; 165 int (*func)(int argc, char **argv); 166 zfs_help_t usage; 167} zfs_command_t; 168 169/* 170 * Master command table. Each ZFS command has a name, associated function, and 171 * usage message. The usage messages need to be internationalized, so we have 172 * to have a function to return the usage message based on a command index. 173 * 174 * These commands are organized according to how they are displayed in the usage 175 * message. An empty command (one with a NULL name) indicates an empty line in 176 * the generic usage message. 177 */ 178static zfs_command_t command_table[] = { 179 { "create", zfs_do_create, HELP_CREATE }, 180 { "destroy", zfs_do_destroy, HELP_DESTROY }, 181 { NULL }, 182 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT }, 183 { "rollback", zfs_do_rollback, HELP_ROLLBACK }, 184 { "clone", zfs_do_clone, HELP_CLONE }, 185 { "promote", zfs_do_promote, HELP_PROMOTE }, 186 { "rename", zfs_do_rename, HELP_RENAME }, 187 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK }, 188 { NULL }, 189 { "list", zfs_do_list, HELP_LIST }, 190 { NULL }, 191 { "set", zfs_do_set, HELP_SET }, 192 { "get", zfs_do_get, HELP_GET }, 193 { "inherit", zfs_do_inherit, HELP_INHERIT }, 194 { "upgrade", zfs_do_upgrade, HELP_UPGRADE }, 195 { "userspace", zfs_do_userspace, HELP_USERSPACE }, 196 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE }, 197 { NULL }, 198 { "mount", zfs_do_mount, HELP_MOUNT }, 199 { "unmount", zfs_do_unmount, HELP_UNMOUNT }, 200 { "share", zfs_do_share, HELP_SHARE }, 201 { "unshare", zfs_do_unshare, HELP_UNSHARE }, 202 { NULL }, 203 { "send", zfs_do_send, HELP_SEND }, 204 { "receive", zfs_do_receive, HELP_RECEIVE }, 205 { NULL }, 206 { "allow", zfs_do_allow, HELP_ALLOW }, 207 { NULL }, 208 { "unallow", zfs_do_unallow, HELP_UNALLOW }, 209 { NULL }, 210 { "hold", zfs_do_hold, HELP_HOLD }, 211 { "holds", zfs_do_holds, HELP_HOLDS }, 212 { "release", zfs_do_release, HELP_RELEASE }, 213 { "diff", zfs_do_diff, HELP_DIFF }, 214 { NULL }, 215 { "jail", zfs_do_jail, HELP_JAIL }, 216 { "unjail", zfs_do_unjail, HELP_UNJAIL }, 217}; 218 219#define NCOMMAND (sizeof (command_table) / sizeof (command_table[0])) 220 221zfs_command_t *current_command; 222 223static const char * 224get_usage(zfs_help_t idx) 225{ 226 switch (idx) { 227 case HELP_CLONE: 228 return (gettext("\tclone [-p] [-o property=value] ... " 229 "<snapshot> <filesystem|volume>\n")); 230 case HELP_CREATE: 231 return (gettext("\tcreate [-pu] [-o property=value] ... " 232 "<filesystem>\n" 233 "\tcreate [-ps] [-b blocksize] [-o property=value] ... " 234 "-V <size> <volume>\n")); 235 case HELP_DESTROY: 236 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n" 237 "\tdestroy [-dnpRrv] " 238 "<filesystem|volume>@<snap>[%<snap>][,...]\n" 239 "\tdestroy <filesystem|volume>#<bookmark>\n")); 240 case HELP_GET: 241 return (gettext("\tget [-rHp] [-d max] " 242 "[-o \"all\" | field[,...]]\n" 243 "\t [-t type[,...]] [-s source[,...]]\n" 244 "\t <\"all\" | property[,...]> " 245 "[filesystem|volume|snapshot] ...\n")); 246 case HELP_INHERIT: 247 return (gettext("\tinherit [-rS] <property> " 248 "<filesystem|volume|snapshot> ...\n")); 249 case HELP_UPGRADE: 250 return (gettext("\tupgrade [-v]\n" 251 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n")); 252 case HELP_JAIL: 253 return (gettext("\tjail <jailid|jailname> <filesystem>\n")); 254 case HELP_UNJAIL: 255 return (gettext("\tunjail <jailid|jailname> <filesystem>\n")); 256 case HELP_LIST: 257 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] " 258 "[-s property]...\n\t [-S property]... [-t type[,...]] " 259 "[filesystem|volume|snapshot] ...\n")); 260 case HELP_MOUNT: 261 return (gettext("\tmount\n" 262 "\tmount [-vO] [-o opts] <-a | filesystem>\n")); 263 case HELP_PROMOTE: 264 return (gettext("\tpromote <clone-filesystem>\n")); 265 case HELP_RECEIVE: 266 return (gettext("\treceive|recv [-vnsFu] <filesystem|volume|" 267 "snapshot>\n" 268 "\treceive|recv [-vnsFu] [-o origin=<snapshot>] [-d | -e] " 269 "<filesystem>\n" 270 "\treceive|recv -A <filesystem|volume>\n")); 271 case HELP_RENAME: 272 return (gettext("\trename [-f] <filesystem|volume|snapshot> " 273 "<filesystem|volume|snapshot>\n" 274 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n" 275 "\trename -r <snapshot> <snapshot>\n" 276 "\trename -u [-p] <filesystem> <filesystem>")); 277 case HELP_ROLLBACK: 278 return (gettext("\trollback [-rRf] <snapshot>\n")); 279 case HELP_SEND: 280 return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] " 281 "<snapshot>\n" 282 "\tsend [-Le] [-i snapshot|bookmark] " 283 "<filesystem|volume|snapshot>\n" 284 "\tsend [-nvPe] -t <receive_resume_token>\n")); 285 case HELP_SET: 286 return (gettext("\tset <property=value> ... " 287 "<filesystem|volume|snapshot> ...\n")); 288 case HELP_SHARE: 289 return (gettext("\tshare <-a | filesystem>\n")); 290 case HELP_SNAPSHOT: 291 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... " 292 "<filesystem|volume>@<snap> ...\n")); 293 case HELP_UNMOUNT: 294 return (gettext("\tunmount|umount [-f] " 295 "<-a | filesystem|mountpoint>\n")); 296 case HELP_UNSHARE: 297 return (gettext("\tunshare " 298 "<-a | filesystem|mountpoint>\n")); 299 case HELP_ALLOW: 300 return (gettext("\tallow <filesystem|volume>\n" 301 "\tallow [-ldug] " 302 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n" 303 "\t <filesystem|volume>\n" 304 "\tallow [-ld] -e <perm|@setname>[,...] " 305 "<filesystem|volume>\n" 306 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n" 307 "\tallow -s @setname <perm|@setname>[,...] " 308 "<filesystem|volume>\n")); 309 case HELP_UNALLOW: 310 return (gettext("\tunallow [-rldug] " 311 "<\"everyone\"|user|group>[,...]\n" 312 "\t [<perm|@setname>[,...]] <filesystem|volume>\n" 313 "\tunallow [-rld] -e [<perm|@setname>[,...]] " 314 "<filesystem|volume>\n" 315 "\tunallow [-r] -c [<perm|@setname>[,...]] " 316 "<filesystem|volume>\n" 317 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] " 318 "<filesystem|volume>\n")); 319 case HELP_USERSPACE: 320 return (gettext("\tuserspace [-Hinp] [-o field[,...]] " 321 "[-s field] ...\n" 322 "\t [-S field] ... [-t type[,...]] " 323 "<filesystem|snapshot>\n")); 324 case HELP_GROUPSPACE: 325 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] " 326 "[-s field] ...\n" 327 "\t [-S field] ... [-t type[,...]] " 328 "<filesystem|snapshot>\n")); 329 case HELP_HOLD: 330 return (gettext("\thold [-r] <tag> <snapshot> ...\n")); 331 case HELP_HOLDS: 332 return (gettext("\tholds [-r] <snapshot> ...\n")); 333 case HELP_RELEASE: 334 return (gettext("\trelease [-r] <tag> <snapshot> ...\n")); 335 case HELP_DIFF: 336 return (gettext("\tdiff [-FHt] <snapshot> " 337 "[snapshot|filesystem]\n")); 338 case HELP_BOOKMARK: 339 return (gettext("\tbookmark <snapshot> <bookmark>\n")); 340 } 341 342 abort(); 343 /* NOTREACHED */ 344} 345 346void 347nomem(void) 348{ 349 (void) fprintf(stderr, gettext("internal error: out of memory\n")); 350 exit(1); 351} 352 353/* 354 * Utility function to guarantee malloc() success. 355 */ 356 357void * 358safe_malloc(size_t size) 359{ 360 void *data; 361 362 if ((data = calloc(1, size)) == NULL) 363 nomem(); 364 365 return (data); 366} 367 368static char * 369safe_strdup(char *str) 370{ 371 char *dupstr = strdup(str); 372 373 if (dupstr == NULL) 374 nomem(); 375 376 return (dupstr); 377} 378 379/* 380 * Callback routine that will print out information for each of 381 * the properties. 382 */ 383static int 384usage_prop_cb(int prop, void *cb) 385{ 386 FILE *fp = cb; 387 388 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop)); 389 390 if (zfs_prop_readonly(prop)) 391 (void) fprintf(fp, " NO "); 392 else 393 (void) fprintf(fp, "YES "); 394 395 if (zfs_prop_inheritable(prop)) 396 (void) fprintf(fp, " YES "); 397 else 398 (void) fprintf(fp, " NO "); 399 400 if (zfs_prop_values(prop) == NULL) 401 (void) fprintf(fp, "-\n"); 402 else 403 (void) fprintf(fp, "%s\n", zfs_prop_values(prop)); 404 405 return (ZPROP_CONT); 406} 407 408/* 409 * Display usage message. If we're inside a command, display only the usage for 410 * that command. Otherwise, iterate over the entire command table and display 411 * a complete usage message. 412 */ 413static void 414usage(boolean_t requested) 415{ 416 int i; 417 boolean_t show_properties = B_FALSE; 418 FILE *fp = requested ? stdout : stderr; 419 420 if (current_command == NULL) { 421 422 (void) fprintf(fp, gettext("usage: zfs command args ...\n")); 423 (void) fprintf(fp, 424 gettext("where 'command' is one of the following:\n\n")); 425 426 for (i = 0; i < NCOMMAND; i++) { 427 if (command_table[i].name == NULL) 428 (void) fprintf(fp, "\n"); 429 else 430 (void) fprintf(fp, "%s", 431 get_usage(command_table[i].usage)); 432 } 433 434 (void) fprintf(fp, gettext("\nEach dataset is of the form: " 435 "pool/[dataset/]*dataset[@name]\n")); 436 } else { 437 (void) fprintf(fp, gettext("usage:\n")); 438 (void) fprintf(fp, "%s", get_usage(current_command->usage)); 439 } 440 441 if (current_command != NULL && 442 (strcmp(current_command->name, "set") == 0 || 443 strcmp(current_command->name, "get") == 0 || 444 strcmp(current_command->name, "inherit") == 0 || 445 strcmp(current_command->name, "list") == 0)) 446 show_properties = B_TRUE; 447 448 if (show_properties) { 449 (void) fprintf(fp, 450 gettext("\nThe following properties are supported:\n")); 451 452 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n", 453 "PROPERTY", "EDIT", "INHERIT", "VALUES"); 454 455 /* Iterate over all properties */ 456 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE, 457 ZFS_TYPE_DATASET); 458 459 (void) fprintf(fp, "\t%-15s ", "userused@..."); 460 (void) fprintf(fp, " NO NO <size>\n"); 461 (void) fprintf(fp, "\t%-15s ", "groupused@..."); 462 (void) fprintf(fp, " NO NO <size>\n"); 463 (void) fprintf(fp, "\t%-15s ", "userquota@..."); 464 (void) fprintf(fp, "YES NO <size> | none\n"); 465 (void) fprintf(fp, "\t%-15s ", "groupquota@..."); 466 (void) fprintf(fp, "YES NO <size> | none\n"); 467 (void) fprintf(fp, "\t%-15s ", "written@<snap>"); 468 (void) fprintf(fp, " NO NO <size>\n"); 469 470 (void) fprintf(fp, gettext("\nSizes are specified in bytes " 471 "with standard units such as K, M, G, etc.\n")); 472 (void) fprintf(fp, gettext("\nUser-defined properties can " 473 "be specified by using a name containing a colon (:).\n")); 474 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ " 475 "properties must be appended with\n" 476 "a user or group specifier of one of these forms:\n" 477 " POSIX name (eg: \"matt\")\n" 478 " POSIX id (eg: \"126829\")\n" 479 " SMB name@domain (eg: \"matt@sun\")\n" 480 " SMB SID (eg: \"S-1-234-567-89\")\n")); 481 } else { 482 (void) fprintf(fp, 483 gettext("\nFor the property list, run: %s\n"), 484 "zfs set|get"); 485 (void) fprintf(fp, 486 gettext("\nFor the delegated permission list, run: %s\n"), 487 "zfs allow|unallow"); 488 } 489 490 /* 491 * See comments at end of main(). 492 */ 493 if (getenv("ZFS_ABORT") != NULL) { 494 (void) printf("dumping core by request\n"); 495 abort(); 496 } 497 498 exit(requested ? 0 : 2); 499} 500 501/* 502 * Take a property=value argument string and add it to the given nvlist. 503 * Modifies the argument inplace. 504 */ 505static int 506parseprop(nvlist_t *props, char *propname) 507{ 508 char *propval, *strval; 509 510 if ((propval = strchr(propname, '=')) == NULL) { 511 (void) fprintf(stderr, gettext("missing " 512 "'=' for property=value argument\n")); 513 return (-1); 514 } 515 *propval = '\0'; 516 propval++; 517 if (nvlist_lookup_string(props, propname, &strval) == 0) { 518 (void) fprintf(stderr, gettext("property '%s' " 519 "specified multiple times\n"), propname); 520 return (-1); 521 } 522 if (nvlist_add_string(props, propname, propval) != 0) 523 nomem(); 524 return (0); 525} 526 527static int 528parse_depth(char *opt, int *flags) 529{ 530 char *tmp; 531 int depth; 532 533 depth = (int)strtol(opt, &tmp, 0); 534 if (*tmp) { 535 (void) fprintf(stderr, 536 gettext("%s is not an integer\n"), opt); 537 usage(B_FALSE); 538 } 539 if (depth < 0) { 540 (void) fprintf(stderr, 541 gettext("Depth can not be negative.\n")); 542 usage(B_FALSE); 543 } 544 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE); 545 return (depth); 546} 547 548#define PROGRESS_DELAY 2 /* seconds */ 549 550static char *pt_reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"; 551static time_t pt_begin; 552static char *pt_header = NULL; 553static boolean_t pt_shown; 554 555static void 556start_progress_timer(void) 557{ 558 pt_begin = time(NULL) + PROGRESS_DELAY; 559 pt_shown = B_FALSE; 560} 561 562static void 563set_progress_header(char *header) 564{ 565 assert(pt_header == NULL); 566 pt_header = safe_strdup(header); 567 if (pt_shown) { 568 (void) printf("%s: ", header); 569 (void) fflush(stdout); 570 } 571} 572 573static void 574update_progress(char *update) 575{ 576 if (!pt_shown && time(NULL) > pt_begin) { 577 int len = strlen(update); 578 579 (void) printf("%s: %s%*.*s", pt_header, update, len, len, 580 pt_reverse); 581 (void) fflush(stdout); 582 pt_shown = B_TRUE; 583 } else if (pt_shown) { 584 int len = strlen(update); 585 586 (void) printf("%s%*.*s", update, len, len, pt_reverse); 587 (void) fflush(stdout); 588 } 589} 590 591static void 592finish_progress(char *done) 593{ 594 if (pt_shown) { 595 (void) printf("%s\n", done); 596 (void) fflush(stdout); 597 } 598 free(pt_header); 599 pt_header = NULL; 600} 601 602/* 603 * Check if the dataset is mountable and should be automatically mounted. 604 */ 605static boolean_t 606should_auto_mount(zfs_handle_t *zhp) 607{ 608 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp))) 609 return (B_FALSE); 610 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON); 611} 612 613/* 614 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol> 615 * 616 * Given an existing dataset, create a writable copy whose initial contents 617 * are the same as the source. The newly created dataset maintains a 618 * dependency on the original; the original cannot be destroyed so long as 619 * the clone exists. 620 * 621 * The '-p' flag creates all the non-existing ancestors of the target first. 622 */ 623static int 624zfs_do_clone(int argc, char **argv) 625{ 626 zfs_handle_t *zhp = NULL; 627 boolean_t parents = B_FALSE; 628 nvlist_t *props; 629 int ret = 0; 630 int c; 631 632 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 633 nomem(); 634 635 /* check options */ 636 while ((c = getopt(argc, argv, "o:p")) != -1) { 637 switch (c) { 638 case 'o': 639 if (parseprop(props, optarg) != 0) 640 return (1); 641 break; 642 case 'p': 643 parents = B_TRUE; 644 break; 645 case '?': 646 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 647 optopt); 648 goto usage; 649 } 650 } 651 652 argc -= optind; 653 argv += optind; 654 655 /* check number of arguments */ 656 if (argc < 1) { 657 (void) fprintf(stderr, gettext("missing source dataset " 658 "argument\n")); 659 goto usage; 660 } 661 if (argc < 2) { 662 (void) fprintf(stderr, gettext("missing target dataset " 663 "argument\n")); 664 goto usage; 665 } 666 if (argc > 2) { 667 (void) fprintf(stderr, gettext("too many arguments\n")); 668 goto usage; 669 } 670 671 /* open the source dataset */ 672 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 673 return (1); 674 675 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM | 676 ZFS_TYPE_VOLUME)) { 677 /* 678 * Now create the ancestors of the target dataset. If the 679 * target already exists and '-p' option was used we should not 680 * complain. 681 */ 682 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | 683 ZFS_TYPE_VOLUME)) 684 return (0); 685 if (zfs_create_ancestors(g_zfs, argv[1]) != 0) 686 return (1); 687 } 688 689 /* pass to libzfs */ 690 ret = zfs_clone(zhp, argv[1], props); 691 692 /* create the mountpoint if necessary */ 693 if (ret == 0) { 694 zfs_handle_t *clone; 695 696 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET); 697 if (clone != NULL) { 698 /* 699 * If the user doesn't want the dataset 700 * automatically mounted, then skip the mount/share 701 * step. 702 */ 703 if (should_auto_mount(clone)) { 704 if ((ret = zfs_mount(clone, NULL, 0)) != 0) { 705 (void) fprintf(stderr, gettext("clone " 706 "successfully created, " 707 "but not mounted\n")); 708 } else if ((ret = zfs_share(clone)) != 0) { 709 (void) fprintf(stderr, gettext("clone " 710 "successfully created, " 711 "but not shared\n")); 712 } 713 } 714 zfs_close(clone); 715 } 716 } 717 718 zfs_close(zhp); 719 nvlist_free(props); 720 721 return (!!ret); 722 723usage: 724 if (zhp) 725 zfs_close(zhp); 726 nvlist_free(props); 727 usage(B_FALSE); 728 return (-1); 729} 730 731/* 732 * zfs create [-pu] [-o prop=value] ... fs 733 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size 734 * 735 * Create a new dataset. This command can be used to create filesystems 736 * and volumes. Snapshot creation is handled by 'zfs snapshot'. 737 * For volumes, the user must specify a size to be used. 738 * 739 * The '-s' flag applies only to volumes, and indicates that we should not try 740 * to set the reservation for this volume. By default we set a reservation 741 * equal to the size for any volume. For pools with SPA_VERSION >= 742 * SPA_VERSION_REFRESERVATION, we set a refreservation instead. 743 * 744 * The '-p' flag creates all the non-existing ancestors of the target first. 745 * 746 * The '-u' flag prevents mounting of newly created file system. 747 */ 748static int 749zfs_do_create(int argc, char **argv) 750{ 751 zfs_type_t type = ZFS_TYPE_FILESYSTEM; 752 zfs_handle_t *zhp = NULL; 753 uint64_t volsize; 754 int c; 755 boolean_t noreserve = B_FALSE; 756 boolean_t bflag = B_FALSE; 757 boolean_t parents = B_FALSE; 758 boolean_t nomount = B_FALSE; 759 int ret = 1; 760 nvlist_t *props; 761 uint64_t intval; 762 763 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 764 nomem(); 765 766 /* check options */ 767 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) { 768 switch (c) { 769 case 'V': 770 type = ZFS_TYPE_VOLUME; 771 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) { 772 (void) fprintf(stderr, gettext("bad volume " 773 "size '%s': %s\n"), optarg, 774 libzfs_error_description(g_zfs)); 775 goto error; 776 } 777 778 if (nvlist_add_uint64(props, 779 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0) 780 nomem(); 781 volsize = intval; 782 break; 783 case 'p': 784 parents = B_TRUE; 785 break; 786 case 'b': 787 bflag = B_TRUE; 788 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) { 789 (void) fprintf(stderr, gettext("bad volume " 790 "block size '%s': %s\n"), optarg, 791 libzfs_error_description(g_zfs)); 792 goto error; 793 } 794 795 if (nvlist_add_uint64(props, 796 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 797 intval) != 0) 798 nomem(); 799 break; 800 case 'o': 801 if (parseprop(props, optarg) != 0) 802 goto error; 803 break; 804 case 's': 805 noreserve = B_TRUE; 806 break; 807 case 'u': 808 nomount = B_TRUE; 809 break; 810 case ':': 811 (void) fprintf(stderr, gettext("missing size " 812 "argument\n")); 813 goto badusage; 814 case '?': 815 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 816 optopt); 817 goto badusage; 818 } 819 } 820 821 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) { 822 (void) fprintf(stderr, gettext("'-s' and '-b' can only be " 823 "used when creating a volume\n")); 824 goto badusage; 825 } 826 if (nomount && type != ZFS_TYPE_FILESYSTEM) { 827 (void) fprintf(stderr, gettext("'-u' can only be " 828 "used when creating a file system\n")); 829 goto badusage; 830 } 831 832 argc -= optind; 833 argv += optind; 834 835 /* check number of arguments */ 836 if (argc == 0) { 837 (void) fprintf(stderr, gettext("missing %s argument\n"), 838 zfs_type_to_name(type)); 839 goto badusage; 840 } 841 if (argc > 1) { 842 (void) fprintf(stderr, gettext("too many arguments\n")); 843 goto badusage; 844 } 845 846 if (type == ZFS_TYPE_VOLUME && !noreserve) { 847 zpool_handle_t *zpool_handle; 848 nvlist_t *real_props; 849 uint64_t spa_version; 850 char *p; 851 zfs_prop_t resv_prop; 852 char *strval; 853 char msg[1024]; 854 855 if (p = strchr(argv[0], '/')) 856 *p = '\0'; 857 zpool_handle = zpool_open(g_zfs, argv[0]); 858 if (p != NULL) 859 *p = '/'; 860 if (zpool_handle == NULL) 861 goto error; 862 spa_version = zpool_get_prop_int(zpool_handle, 863 ZPOOL_PROP_VERSION, NULL); 864 zpool_close(zpool_handle); 865 if (spa_version >= SPA_VERSION_REFRESERVATION) 866 resv_prop = ZFS_PROP_REFRESERVATION; 867 else 868 resv_prop = ZFS_PROP_RESERVATION; 869 870 (void) snprintf(msg, sizeof (msg), 871 gettext("cannot create '%s'"), argv[0]); 872 if (props && (real_props = zfs_valid_proplist(g_zfs, type, 873 props, 0, NULL, msg)) == NULL) 874 goto error; 875 876 volsize = zvol_volsize_to_reservation(volsize, real_props); 877 nvlist_free(real_props); 878 879 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop), 880 &strval) != 0) { 881 if (nvlist_add_uint64(props, 882 zfs_prop_to_name(resv_prop), volsize) != 0) { 883 nvlist_free(props); 884 nomem(); 885 } 886 } 887 } 888 889 if (parents && zfs_name_valid(argv[0], type)) { 890 /* 891 * Now create the ancestors of target dataset. If the target 892 * already exists and '-p' option was used we should not 893 * complain. 894 */ 895 if (zfs_dataset_exists(g_zfs, argv[0], type)) { 896 ret = 0; 897 goto error; 898 } 899 if (zfs_create_ancestors(g_zfs, argv[0]) != 0) 900 goto error; 901 } 902 903 /* pass to libzfs */ 904 if (zfs_create(g_zfs, argv[0], type, props) != 0) 905 goto error; 906 907 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL) 908 goto error; 909 910 ret = 0; 911 912 /* 913 * Mount and/or share the new filesystem as appropriate. We provide a 914 * verbose error message to let the user know that their filesystem was 915 * in fact created, even if we failed to mount or share it. 916 * If the user doesn't want the dataset automatically mounted, 917 * then skip the mount/share step altogether. 918 */ 919 if (!nomount && should_auto_mount(zhp)) { 920 if (zfs_mount(zhp, NULL, 0) != 0) { 921 (void) fprintf(stderr, gettext("filesystem " 922 "successfully created, but not mounted\n")); 923 ret = 1; 924 } else if (zfs_share(zhp) != 0) { 925 (void) fprintf(stderr, gettext("filesystem " 926 "successfully created, but not shared\n")); 927 ret = 1; 928 } 929 } 930 931error: 932 if (zhp) 933 zfs_close(zhp); 934 nvlist_free(props); 935 return (ret); 936badusage: 937 nvlist_free(props); 938 usage(B_FALSE); 939 return (2); 940} 941 942/* 943 * zfs destroy [-rRf] <fs, vol> 944 * zfs destroy [-rRd] <snap> 945 * 946 * -r Recursively destroy all children 947 * -R Recursively destroy all dependents, including clones 948 * -f Force unmounting of any dependents 949 * -d If we can't destroy now, mark for deferred destruction 950 * 951 * Destroys the given dataset. By default, it will unmount any filesystems, 952 * and refuse to destroy a dataset that has any dependents. A dependent can 953 * either be a child, or a clone of a child. 954 */ 955typedef struct destroy_cbdata { 956 boolean_t cb_first; 957 boolean_t cb_force; 958 boolean_t cb_recurse; 959 boolean_t cb_error; 960 boolean_t cb_doclones; 961 zfs_handle_t *cb_target; 962 boolean_t cb_defer_destroy; 963 boolean_t cb_verbose; 964 boolean_t cb_parsable; 965 boolean_t cb_dryrun; 966 nvlist_t *cb_nvl; 967 nvlist_t *cb_batchedsnaps; 968 969 /* first snap in contiguous run */ 970 char *cb_firstsnap; 971 /* previous snap in contiguous run */ 972 char *cb_prevsnap; 973 int64_t cb_snapused; 974 char *cb_snapspec; 975 char *cb_bookmark; 976} destroy_cbdata_t; 977 978/* 979 * Check for any dependents based on the '-r' or '-R' flags. 980 */ 981static int 982destroy_check_dependent(zfs_handle_t *zhp, void *data) 983{ 984 destroy_cbdata_t *cbp = data; 985 const char *tname = zfs_get_name(cbp->cb_target); 986 const char *name = zfs_get_name(zhp); 987 988 if (strncmp(tname, name, strlen(tname)) == 0 && 989 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) { 990 /* 991 * This is a direct descendant, not a clone somewhere else in 992 * the hierarchy. 993 */ 994 if (cbp->cb_recurse) 995 goto out; 996 997 if (cbp->cb_first) { 998 (void) fprintf(stderr, gettext("cannot destroy '%s': " 999 "%s has children\n"), 1000 zfs_get_name(cbp->cb_target), 1001 zfs_type_to_name(zfs_get_type(cbp->cb_target))); 1002 (void) fprintf(stderr, gettext("use '-r' to destroy " 1003 "the following datasets:\n")); 1004 cbp->cb_first = B_FALSE; 1005 cbp->cb_error = B_TRUE; 1006 } 1007 1008 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 1009 } else { 1010 /* 1011 * This is a clone. We only want to report this if the '-r' 1012 * wasn't specified, or the target is a snapshot. 1013 */ 1014 if (!cbp->cb_recurse && 1015 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT) 1016 goto out; 1017 1018 if (cbp->cb_first) { 1019 (void) fprintf(stderr, gettext("cannot destroy '%s': " 1020 "%s has dependent clones\n"), 1021 zfs_get_name(cbp->cb_target), 1022 zfs_type_to_name(zfs_get_type(cbp->cb_target))); 1023 (void) fprintf(stderr, gettext("use '-R' to destroy " 1024 "the following datasets:\n")); 1025 cbp->cb_first = B_FALSE; 1026 cbp->cb_error = B_TRUE; 1027 cbp->cb_dryrun = B_TRUE; 1028 } 1029 1030 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 1031 } 1032 1033out: 1034 zfs_close(zhp); 1035 return (0); 1036} 1037 1038static int 1039destroy_callback(zfs_handle_t *zhp, void *data) 1040{ 1041 destroy_cbdata_t *cb = data; 1042 const char *name = zfs_get_name(zhp); 1043 1044 if (cb->cb_verbose) { 1045 if (cb->cb_parsable) { 1046 (void) printf("destroy\t%s\n", name); 1047 } else if (cb->cb_dryrun) { 1048 (void) printf(gettext("would destroy %s\n"), 1049 name); 1050 } else { 1051 (void) printf(gettext("will destroy %s\n"), 1052 name); 1053 } 1054 } 1055 1056 /* 1057 * Ignore pools (which we've already flagged as an error before getting 1058 * here). 1059 */ 1060 if (strchr(zfs_get_name(zhp), '/') == NULL && 1061 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 1062 zfs_close(zhp); 1063 return (0); 1064 } 1065 if (cb->cb_dryrun) { 1066 zfs_close(zhp); 1067 return (0); 1068 } 1069 1070 /* 1071 * We batch up all contiguous snapshots (even of different 1072 * filesystems) and destroy them with one ioctl. We can't 1073 * simply do all snap deletions and then all fs deletions, 1074 * because we must delete a clone before its origin. 1075 */ 1076 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) { 1077 fnvlist_add_boolean(cb->cb_batchedsnaps, name); 1078 } else { 1079 int error = zfs_destroy_snaps_nvl(g_zfs, 1080 cb->cb_batchedsnaps, B_FALSE); 1081 fnvlist_free(cb->cb_batchedsnaps); 1082 cb->cb_batchedsnaps = fnvlist_alloc(); 1083 1084 if (error != 0 || 1085 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 || 1086 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) { 1087 zfs_close(zhp); 1088 return (-1); 1089 } 1090 } 1091 1092 zfs_close(zhp); 1093 return (0); 1094} 1095 1096static int 1097destroy_print_cb(zfs_handle_t *zhp, void *arg) 1098{ 1099 destroy_cbdata_t *cb = arg; 1100 const char *name = zfs_get_name(zhp); 1101 int err = 0; 1102 1103 if (nvlist_exists(cb->cb_nvl, name)) { 1104 if (cb->cb_firstsnap == NULL) 1105 cb->cb_firstsnap = strdup(name); 1106 if (cb->cb_prevsnap != NULL) 1107 free(cb->cb_prevsnap); 1108 /* this snap continues the current range */ 1109 cb->cb_prevsnap = strdup(name); 1110 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL) 1111 nomem(); 1112 if (cb->cb_verbose) { 1113 if (cb->cb_parsable) { 1114 (void) printf("destroy\t%s\n", name); 1115 } else if (cb->cb_dryrun) { 1116 (void) printf(gettext("would destroy %s\n"), 1117 name); 1118 } else { 1119 (void) printf(gettext("will destroy %s\n"), 1120 name); 1121 } 1122 } 1123 } else if (cb->cb_firstsnap != NULL) { 1124 /* end of this range */ 1125 uint64_t used = 0; 1126 err = lzc_snaprange_space(cb->cb_firstsnap, 1127 cb->cb_prevsnap, &used); 1128 cb->cb_snapused += used; 1129 free(cb->cb_firstsnap); 1130 cb->cb_firstsnap = NULL; 1131 free(cb->cb_prevsnap); 1132 cb->cb_prevsnap = NULL; 1133 } 1134 zfs_close(zhp); 1135 return (err); 1136} 1137 1138static int 1139destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb) 1140{ 1141 int err = 0; 1142 assert(cb->cb_firstsnap == NULL); 1143 assert(cb->cb_prevsnap == NULL); 1144 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb); 1145 if (cb->cb_firstsnap != NULL) { 1146 uint64_t used = 0; 1147 if (err == 0) { 1148 err = lzc_snaprange_space(cb->cb_firstsnap, 1149 cb->cb_prevsnap, &used); 1150 } 1151 cb->cb_snapused += used; 1152 free(cb->cb_firstsnap); 1153 cb->cb_firstsnap = NULL; 1154 free(cb->cb_prevsnap); 1155 cb->cb_prevsnap = NULL; 1156 } 1157 return (err); 1158} 1159 1160static int 1161snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg) 1162{ 1163 destroy_cbdata_t *cb = arg; 1164 int err = 0; 1165 1166 /* Check for clones. */ 1167 if (!cb->cb_doclones && !cb->cb_defer_destroy) { 1168 cb->cb_target = zhp; 1169 cb->cb_first = B_TRUE; 1170 err = zfs_iter_dependents(zhp, B_TRUE, 1171 destroy_check_dependent, cb); 1172 } 1173 1174 if (err == 0) { 1175 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp))) 1176 nomem(); 1177 } 1178 zfs_close(zhp); 1179 return (err); 1180} 1181 1182static int 1183gather_snapshots(zfs_handle_t *zhp, void *arg) 1184{ 1185 destroy_cbdata_t *cb = arg; 1186 int err = 0; 1187 1188 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb); 1189 if (err == ENOENT) 1190 err = 0; 1191 if (err != 0) 1192 goto out; 1193 1194 if (cb->cb_verbose) { 1195 err = destroy_print_snapshots(zhp, cb); 1196 if (err != 0) 1197 goto out; 1198 } 1199 1200 if (cb->cb_recurse) 1201 err = zfs_iter_filesystems(zhp, gather_snapshots, cb); 1202 1203out: 1204 zfs_close(zhp); 1205 return (err); 1206} 1207 1208static int 1209destroy_clones(destroy_cbdata_t *cb) 1210{ 1211 nvpair_t *pair; 1212 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL); 1213 pair != NULL; 1214 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) { 1215 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair), 1216 ZFS_TYPE_SNAPSHOT); 1217 if (zhp != NULL) { 1218 boolean_t defer = cb->cb_defer_destroy; 1219 int err = 0; 1220 1221 /* 1222 * We can't defer destroy non-snapshots, so set it to 1223 * false while destroying the clones. 1224 */ 1225 cb->cb_defer_destroy = B_FALSE; 1226 err = zfs_iter_dependents(zhp, B_FALSE, 1227 destroy_callback, cb); 1228 cb->cb_defer_destroy = defer; 1229 zfs_close(zhp); 1230 if (err != 0) 1231 return (err); 1232 } 1233 } 1234 return (0); 1235} 1236 1237static int 1238zfs_do_destroy(int argc, char **argv) 1239{ 1240 destroy_cbdata_t cb = { 0 }; 1241 int rv = 0; 1242 int err = 0; 1243 int c; 1244 zfs_handle_t *zhp = NULL; 1245 char *at, *pound; 1246 zfs_type_t type = ZFS_TYPE_DATASET; 1247 1248 /* check options */ 1249 while ((c = getopt(argc, argv, "vpndfrR")) != -1) { 1250 switch (c) { 1251 case 'v': 1252 cb.cb_verbose = B_TRUE; 1253 break; 1254 case 'p': 1255 cb.cb_verbose = B_TRUE; 1256 cb.cb_parsable = B_TRUE; 1257 break; 1258 case 'n': 1259 cb.cb_dryrun = B_TRUE; 1260 break; 1261 case 'd': 1262 cb.cb_defer_destroy = B_TRUE; 1263 type = ZFS_TYPE_SNAPSHOT; 1264 break; 1265 case 'f': 1266 cb.cb_force = B_TRUE; 1267 break; 1268 case 'r': 1269 cb.cb_recurse = B_TRUE; 1270 break; 1271 case 'R': 1272 cb.cb_recurse = B_TRUE; 1273 cb.cb_doclones = B_TRUE; 1274 break; 1275 case '?': 1276 default: 1277 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1278 optopt); 1279 usage(B_FALSE); 1280 } 1281 } 1282 1283 argc -= optind; 1284 argv += optind; 1285 1286 /* check number of arguments */ 1287 if (argc == 0) { 1288 (void) fprintf(stderr, gettext("missing dataset argument\n")); 1289 usage(B_FALSE); 1290 } 1291 if (argc > 1) { 1292 (void) fprintf(stderr, gettext("too many arguments\n")); 1293 usage(B_FALSE); 1294 } 1295 1296 at = strchr(argv[0], '@'); 1297 pound = strchr(argv[0], '#'); 1298 if (at != NULL) { 1299 1300 /* Build the list of snaps to destroy in cb_nvl. */ 1301 cb.cb_nvl = fnvlist_alloc(); 1302 1303 *at = '\0'; 1304 zhp = zfs_open(g_zfs, argv[0], 1305 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 1306 if (zhp == NULL) 1307 return (1); 1308 1309 cb.cb_snapspec = at + 1; 1310 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 || 1311 cb.cb_error) { 1312 rv = 1; 1313 goto out; 1314 } 1315 1316 if (nvlist_empty(cb.cb_nvl)) { 1317 (void) fprintf(stderr, gettext("could not find any " 1318 "snapshots to destroy; check snapshot names.\n")); 1319 rv = 1; 1320 goto out; 1321 } 1322 1323 if (cb.cb_verbose) { 1324 char buf[16]; 1325 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf)); 1326 if (cb.cb_parsable) { 1327 (void) printf("reclaim\t%llu\n", 1328 cb.cb_snapused); 1329 } else if (cb.cb_dryrun) { 1330 (void) printf(gettext("would reclaim %s\n"), 1331 buf); 1332 } else { 1333 (void) printf(gettext("will reclaim %s\n"), 1334 buf); 1335 } 1336 } 1337 1338 if (!cb.cb_dryrun) { 1339 if (cb.cb_doclones) { 1340 cb.cb_batchedsnaps = fnvlist_alloc(); 1341 err = destroy_clones(&cb); 1342 if (err == 0) { 1343 err = zfs_destroy_snaps_nvl(g_zfs, 1344 cb.cb_batchedsnaps, B_FALSE); 1345 } 1346 if (err != 0) { 1347 rv = 1; 1348 goto out; 1349 } 1350 } 1351 if (err == 0) { 1352 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl, 1353 cb.cb_defer_destroy); 1354 } 1355 } 1356 1357 if (err != 0) 1358 rv = 1; 1359 } else if (pound != NULL) { 1360 int err; 1361 nvlist_t *nvl; 1362 1363 if (cb.cb_dryrun) { 1364 (void) fprintf(stderr, 1365 "dryrun is not supported with bookmark\n"); 1366 return (-1); 1367 } 1368 1369 if (cb.cb_defer_destroy) { 1370 (void) fprintf(stderr, 1371 "defer destroy is not supported with bookmark\n"); 1372 return (-1); 1373 } 1374 1375 if (cb.cb_recurse) { 1376 (void) fprintf(stderr, 1377 "recursive is not supported with bookmark\n"); 1378 return (-1); 1379 } 1380 1381 if (!zfs_bookmark_exists(argv[0])) { 1382 (void) fprintf(stderr, gettext("bookmark '%s' " 1383 "does not exist.\n"), argv[0]); 1384 return (1); 1385 } 1386 1387 nvl = fnvlist_alloc(); 1388 fnvlist_add_boolean(nvl, argv[0]); 1389 1390 err = lzc_destroy_bookmarks(nvl, NULL); 1391 if (err != 0) { 1392 (void) zfs_standard_error(g_zfs, err, 1393 "cannot destroy bookmark"); 1394 } 1395 1396 nvlist_free(cb.cb_nvl); 1397 1398 return (err); 1399 } else { 1400 /* Open the given dataset */ 1401 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL) 1402 return (1); 1403 1404 cb.cb_target = zhp; 1405 1406 /* 1407 * Perform an explicit check for pools before going any further. 1408 */ 1409 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL && 1410 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 1411 (void) fprintf(stderr, gettext("cannot destroy '%s': " 1412 "operation does not apply to pools\n"), 1413 zfs_get_name(zhp)); 1414 (void) fprintf(stderr, gettext("use 'zfs destroy -r " 1415 "%s' to destroy all datasets in the pool\n"), 1416 zfs_get_name(zhp)); 1417 (void) fprintf(stderr, gettext("use 'zpool destroy %s' " 1418 "to destroy the pool itself\n"), zfs_get_name(zhp)); 1419 rv = 1; 1420 goto out; 1421 } 1422 1423 /* 1424 * Check for any dependents and/or clones. 1425 */ 1426 cb.cb_first = B_TRUE; 1427 if (!cb.cb_doclones && 1428 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent, 1429 &cb) != 0) { 1430 rv = 1; 1431 goto out; 1432 } 1433 1434 if (cb.cb_error) { 1435 rv = 1; 1436 goto out; 1437 } 1438 1439 cb.cb_batchedsnaps = fnvlist_alloc(); 1440 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback, 1441 &cb) != 0) { 1442 rv = 1; 1443 goto out; 1444 } 1445 1446 /* 1447 * Do the real thing. The callback will close the 1448 * handle regardless of whether it succeeds or not. 1449 */ 1450 err = destroy_callback(zhp, &cb); 1451 zhp = NULL; 1452 if (err == 0) { 1453 err = zfs_destroy_snaps_nvl(g_zfs, 1454 cb.cb_batchedsnaps, cb.cb_defer_destroy); 1455 } 1456 if (err != 0) 1457 rv = 1; 1458 } 1459 1460out: 1461 fnvlist_free(cb.cb_batchedsnaps); 1462 fnvlist_free(cb.cb_nvl); 1463 if (zhp != NULL) 1464 zfs_close(zhp); 1465 return (rv); 1466} 1467 1468static boolean_t 1469is_recvd_column(zprop_get_cbdata_t *cbp) 1470{ 1471 int i; 1472 zfs_get_column_t col; 1473 1474 for (i = 0; i < ZFS_GET_NCOLS && 1475 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++) 1476 if (col == GET_COL_RECVD) 1477 return (B_TRUE); 1478 return (B_FALSE); 1479} 1480 1481/* 1482 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...] 1483 * < all | property[,property]... > < fs | snap | vol > ... 1484 * 1485 * -r recurse over any child datasets 1486 * -H scripted mode. Headers are stripped, and fields are separated 1487 * by tabs instead of spaces. 1488 * -o Set of fields to display. One of "name,property,value, 1489 * received,source". Default is "name,property,value,source". 1490 * "all" is an alias for all five. 1491 * -s Set of sources to allow. One of 1492 * "local,default,inherited,received,temporary,none". Default is 1493 * all six. 1494 * -p Display values in parsable (literal) format. 1495 * 1496 * Prints properties for the given datasets. The user can control which 1497 * columns to display as well as which property types to allow. 1498 */ 1499 1500/* 1501 * Invoked to display the properties for a single dataset. 1502 */ 1503static int 1504get_callback(zfs_handle_t *zhp, void *data) 1505{ 1506 char buf[ZFS_MAXPROPLEN]; 1507 char rbuf[ZFS_MAXPROPLEN]; 1508 zprop_source_t sourcetype; 1509 char source[ZFS_MAXNAMELEN]; 1510 zprop_get_cbdata_t *cbp = data; 1511 nvlist_t *user_props = zfs_get_user_props(zhp); 1512 zprop_list_t *pl = cbp->cb_proplist; 1513 nvlist_t *propval; 1514 char *strval; 1515 char *sourceval; 1516 boolean_t received = is_recvd_column(cbp); 1517 1518 for (; pl != NULL; pl = pl->pl_next) { 1519 char *recvdval = NULL; 1520 /* 1521 * Skip the special fake placeholder. This will also skip over 1522 * the name property when 'all' is specified. 1523 */ 1524 if (pl->pl_prop == ZFS_PROP_NAME && 1525 pl == cbp->cb_proplist) 1526 continue; 1527 1528 if (pl->pl_prop != ZPROP_INVAL) { 1529 if (zfs_prop_get(zhp, pl->pl_prop, buf, 1530 sizeof (buf), &sourcetype, source, 1531 sizeof (source), 1532 cbp->cb_literal) != 0) { 1533 if (pl->pl_all) 1534 continue; 1535 if (!zfs_prop_valid_for_type(pl->pl_prop, 1536 ZFS_TYPE_DATASET)) { 1537 (void) fprintf(stderr, 1538 gettext("No such property '%s'\n"), 1539 zfs_prop_to_name(pl->pl_prop)); 1540 continue; 1541 } 1542 sourcetype = ZPROP_SRC_NONE; 1543 (void) strlcpy(buf, "-", sizeof (buf)); 1544 } 1545 1546 if (received && (zfs_prop_get_recvd(zhp, 1547 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf), 1548 cbp->cb_literal) == 0)) 1549 recvdval = rbuf; 1550 1551 zprop_print_one_property(zfs_get_name(zhp), cbp, 1552 zfs_prop_to_name(pl->pl_prop), 1553 buf, sourcetype, source, recvdval); 1554 } else if (zfs_prop_userquota(pl->pl_user_prop)) { 1555 sourcetype = ZPROP_SRC_LOCAL; 1556 1557 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop, 1558 buf, sizeof (buf), cbp->cb_literal) != 0) { 1559 sourcetype = ZPROP_SRC_NONE; 1560 (void) strlcpy(buf, "-", sizeof (buf)); 1561 } 1562 1563 zprop_print_one_property(zfs_get_name(zhp), cbp, 1564 pl->pl_user_prop, buf, sourcetype, source, NULL); 1565 } else if (zfs_prop_written(pl->pl_user_prop)) { 1566 sourcetype = ZPROP_SRC_LOCAL; 1567 1568 if (zfs_prop_get_written(zhp, pl->pl_user_prop, 1569 buf, sizeof (buf), cbp->cb_literal) != 0) { 1570 sourcetype = ZPROP_SRC_NONE; 1571 (void) strlcpy(buf, "-", sizeof (buf)); 1572 } 1573 1574 zprop_print_one_property(zfs_get_name(zhp), cbp, 1575 pl->pl_user_prop, buf, sourcetype, source, NULL); 1576 } else { 1577 if (nvlist_lookup_nvlist(user_props, 1578 pl->pl_user_prop, &propval) != 0) { 1579 if (pl->pl_all) 1580 continue; 1581 sourcetype = ZPROP_SRC_NONE; 1582 strval = "-"; 1583 } else { 1584 verify(nvlist_lookup_string(propval, 1585 ZPROP_VALUE, &strval) == 0); 1586 verify(nvlist_lookup_string(propval, 1587 ZPROP_SOURCE, &sourceval) == 0); 1588 1589 if (strcmp(sourceval, 1590 zfs_get_name(zhp)) == 0) { 1591 sourcetype = ZPROP_SRC_LOCAL; 1592 } else if (strcmp(sourceval, 1593 ZPROP_SOURCE_VAL_RECVD) == 0) { 1594 sourcetype = ZPROP_SRC_RECEIVED; 1595 } else { 1596 sourcetype = ZPROP_SRC_INHERITED; 1597 (void) strlcpy(source, 1598 sourceval, sizeof (source)); 1599 } 1600 } 1601 1602 if (received && (zfs_prop_get_recvd(zhp, 1603 pl->pl_user_prop, rbuf, sizeof (rbuf), 1604 cbp->cb_literal) == 0)) 1605 recvdval = rbuf; 1606 1607 zprop_print_one_property(zfs_get_name(zhp), cbp, 1608 pl->pl_user_prop, strval, sourcetype, 1609 source, recvdval); 1610 } 1611 } 1612 1613 return (0); 1614} 1615 1616static int 1617zfs_do_get(int argc, char **argv) 1618{ 1619 zprop_get_cbdata_t cb = { 0 }; 1620 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS; 1621 int types = ZFS_TYPE_DATASET; 1622 char *value, *fields; 1623 int ret = 0; 1624 int limit = 0; 1625 zprop_list_t fake_name = { 0 }; 1626 1627 /* 1628 * Set up default columns and sources. 1629 */ 1630 cb.cb_sources = ZPROP_SRC_ALL; 1631 cb.cb_columns[0] = GET_COL_NAME; 1632 cb.cb_columns[1] = GET_COL_PROPERTY; 1633 cb.cb_columns[2] = GET_COL_VALUE; 1634 cb.cb_columns[3] = GET_COL_SOURCE; 1635 cb.cb_type = ZFS_TYPE_DATASET; 1636 1637 /* check options */ 1638 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) { 1639 switch (c) { 1640 case 'p': 1641 cb.cb_literal = B_TRUE; 1642 break; 1643 case 'd': 1644 limit = parse_depth(optarg, &flags); 1645 break; 1646 case 'r': 1647 flags |= ZFS_ITER_RECURSE; 1648 break; 1649 case 'H': 1650 cb.cb_scripted = B_TRUE; 1651 break; 1652 case ':': 1653 (void) fprintf(stderr, gettext("missing argument for " 1654 "'%c' option\n"), optopt); 1655 usage(B_FALSE); 1656 break; 1657 case 'o': 1658 /* 1659 * Process the set of columns to display. We zero out 1660 * the structure to give us a blank slate. 1661 */ 1662 bzero(&cb.cb_columns, sizeof (cb.cb_columns)); 1663 i = 0; 1664 while (*optarg != '\0') { 1665 static char *col_subopts[] = 1666 { "name", "property", "value", "received", 1667 "source", "all", NULL }; 1668 1669 if (i == ZFS_GET_NCOLS) { 1670 (void) fprintf(stderr, gettext("too " 1671 "many fields given to -o " 1672 "option\n")); 1673 usage(B_FALSE); 1674 } 1675 1676 switch (getsubopt(&optarg, col_subopts, 1677 &value)) { 1678 case 0: 1679 cb.cb_columns[i++] = GET_COL_NAME; 1680 break; 1681 case 1: 1682 cb.cb_columns[i++] = GET_COL_PROPERTY; 1683 break; 1684 case 2: 1685 cb.cb_columns[i++] = GET_COL_VALUE; 1686 break; 1687 case 3: 1688 cb.cb_columns[i++] = GET_COL_RECVD; 1689 flags |= ZFS_ITER_RECVD_PROPS; 1690 break; 1691 case 4: 1692 cb.cb_columns[i++] = GET_COL_SOURCE; 1693 break; 1694 case 5: 1695 if (i > 0) { 1696 (void) fprintf(stderr, 1697 gettext("\"all\" conflicts " 1698 "with specific fields " 1699 "given to -o option\n")); 1700 usage(B_FALSE); 1701 } 1702 cb.cb_columns[0] = GET_COL_NAME; 1703 cb.cb_columns[1] = GET_COL_PROPERTY; 1704 cb.cb_columns[2] = GET_COL_VALUE; 1705 cb.cb_columns[3] = GET_COL_RECVD; 1706 cb.cb_columns[4] = GET_COL_SOURCE; 1707 flags |= ZFS_ITER_RECVD_PROPS; 1708 i = ZFS_GET_NCOLS; 1709 break; 1710 default: 1711 (void) fprintf(stderr, 1712 gettext("invalid column name " 1713 "'%s'\n"), value); 1714 usage(B_FALSE); 1715 } 1716 } 1717 break; 1718 1719 case 's': 1720 cb.cb_sources = 0; 1721 while (*optarg != '\0') { 1722 static char *source_subopts[] = { 1723 "local", "default", "inherited", 1724 "received", "temporary", "none", 1725 NULL }; 1726 1727 switch (getsubopt(&optarg, source_subopts, 1728 &value)) { 1729 case 0: 1730 cb.cb_sources |= ZPROP_SRC_LOCAL; 1731 break; 1732 case 1: 1733 cb.cb_sources |= ZPROP_SRC_DEFAULT; 1734 break; 1735 case 2: 1736 cb.cb_sources |= ZPROP_SRC_INHERITED; 1737 break; 1738 case 3: 1739 cb.cb_sources |= ZPROP_SRC_RECEIVED; 1740 break; 1741 case 4: 1742 cb.cb_sources |= ZPROP_SRC_TEMPORARY; 1743 break; 1744 case 5: 1745 cb.cb_sources |= ZPROP_SRC_NONE; 1746 break; 1747 default: 1748 (void) fprintf(stderr, 1749 gettext("invalid source " 1750 "'%s'\n"), value); 1751 usage(B_FALSE); 1752 } 1753 } 1754 break; 1755 1756 case 't': 1757 types = 0; 1758 flags &= ~ZFS_ITER_PROP_LISTSNAPS; 1759 while (*optarg != '\0') { 1760 static char *type_subopts[] = { "filesystem", 1761 "volume", "snapshot", "bookmark", 1762 "all", NULL }; 1763 1764 switch (getsubopt(&optarg, type_subopts, 1765 &value)) { 1766 case 0: 1767 types |= ZFS_TYPE_FILESYSTEM; 1768 break; 1769 case 1: 1770 types |= ZFS_TYPE_VOLUME; 1771 break; 1772 case 2: 1773 types |= ZFS_TYPE_SNAPSHOT; 1774 break; 1775 case 3: 1776 types |= ZFS_TYPE_BOOKMARK; 1777 break; 1778 case 4: 1779 types = ZFS_TYPE_DATASET | 1780 ZFS_TYPE_BOOKMARK; 1781 break; 1782 1783 default: 1784 (void) fprintf(stderr, 1785 gettext("invalid type '%s'\n"), 1786 value); 1787 usage(B_FALSE); 1788 } 1789 } 1790 break; 1791 1792 case '?': 1793 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1794 optopt); 1795 usage(B_FALSE); 1796 } 1797 } 1798 1799 argc -= optind; 1800 argv += optind; 1801 1802 if (argc < 1) { 1803 (void) fprintf(stderr, gettext("missing property " 1804 "argument\n")); 1805 usage(B_FALSE); 1806 } 1807 1808 fields = argv[0]; 1809 1810 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET) 1811 != 0) 1812 usage(B_FALSE); 1813 1814 argc--; 1815 argv++; 1816 1817 /* 1818 * As part of zfs_expand_proplist(), we keep track of the maximum column 1819 * width for each property. For the 'NAME' (and 'SOURCE') columns, we 1820 * need to know the maximum name length. However, the user likely did 1821 * not specify 'name' as one of the properties to fetch, so we need to 1822 * make sure we always include at least this property for 1823 * print_get_headers() to work properly. 1824 */ 1825 if (cb.cb_proplist != NULL) { 1826 fake_name.pl_prop = ZFS_PROP_NAME; 1827 fake_name.pl_width = strlen(gettext("NAME")); 1828 fake_name.pl_next = cb.cb_proplist; 1829 cb.cb_proplist = &fake_name; 1830 } 1831 1832 cb.cb_first = B_TRUE; 1833 1834 /* run for each object */ 1835 ret = zfs_for_each(argc, argv, flags, types, NULL, 1836 &cb.cb_proplist, limit, get_callback, &cb); 1837 1838 if (cb.cb_proplist == &fake_name) 1839 zprop_free_list(fake_name.pl_next); 1840 else 1841 zprop_free_list(cb.cb_proplist); 1842 1843 return (ret); 1844} 1845 1846/* 1847 * inherit [-rS] <property> <fs|vol> ... 1848 * 1849 * -r Recurse over all children 1850 * -S Revert to received value, if any 1851 * 1852 * For each dataset specified on the command line, inherit the given property 1853 * from its parent. Inheriting a property at the pool level will cause it to 1854 * use the default value. The '-r' flag will recurse over all children, and is 1855 * useful for setting a property on a hierarchy-wide basis, regardless of any 1856 * local modifications for each dataset. 1857 */ 1858 1859typedef struct inherit_cbdata { 1860 const char *cb_propname; 1861 boolean_t cb_received; 1862} inherit_cbdata_t; 1863 1864static int 1865inherit_recurse_cb(zfs_handle_t *zhp, void *data) 1866{ 1867 inherit_cbdata_t *cb = data; 1868 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname); 1869 1870 /* 1871 * If we're doing it recursively, then ignore properties that 1872 * are not valid for this type of dataset. 1873 */ 1874 if (prop != ZPROP_INVAL && 1875 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp))) 1876 return (0); 1877 1878 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0); 1879} 1880 1881static int 1882inherit_cb(zfs_handle_t *zhp, void *data) 1883{ 1884 inherit_cbdata_t *cb = data; 1885 1886 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0); 1887} 1888 1889static int 1890zfs_do_inherit(int argc, char **argv) 1891{ 1892 int c; 1893 zfs_prop_t prop; 1894 inherit_cbdata_t cb = { 0 }; 1895 char *propname; 1896 int ret = 0; 1897 int flags = 0; 1898 boolean_t received = B_FALSE; 1899 1900 /* check options */ 1901 while ((c = getopt(argc, argv, "rS")) != -1) { 1902 switch (c) { 1903 case 'r': 1904 flags |= ZFS_ITER_RECURSE; 1905 break; 1906 case 'S': 1907 received = B_TRUE; 1908 break; 1909 case '?': 1910 default: 1911 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1912 optopt); 1913 usage(B_FALSE); 1914 } 1915 } 1916 1917 argc -= optind; 1918 argv += optind; 1919 1920 /* check number of arguments */ 1921 if (argc < 1) { 1922 (void) fprintf(stderr, gettext("missing property argument\n")); 1923 usage(B_FALSE); 1924 } 1925 if (argc < 2) { 1926 (void) fprintf(stderr, gettext("missing dataset argument\n")); 1927 usage(B_FALSE); 1928 } 1929 1930 propname = argv[0]; 1931 argc--; 1932 argv++; 1933 1934 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) { 1935 if (zfs_prop_readonly(prop)) { 1936 (void) fprintf(stderr, gettext( 1937 "%s property is read-only\n"), 1938 propname); 1939 return (1); 1940 } 1941 if (!zfs_prop_inheritable(prop) && !received) { 1942 (void) fprintf(stderr, gettext("'%s' property cannot " 1943 "be inherited\n"), propname); 1944 if (prop == ZFS_PROP_QUOTA || 1945 prop == ZFS_PROP_RESERVATION || 1946 prop == ZFS_PROP_REFQUOTA || 1947 prop == ZFS_PROP_REFRESERVATION) { 1948 (void) fprintf(stderr, gettext("use 'zfs set " 1949 "%s=none' to clear\n"), propname); 1950 (void) fprintf(stderr, gettext("use 'zfs " 1951 "inherit -S %s' to revert to received " 1952 "value\n"), propname); 1953 } 1954 return (1); 1955 } 1956 if (received && (prop == ZFS_PROP_VOLSIZE || 1957 prop == ZFS_PROP_VERSION)) { 1958 (void) fprintf(stderr, gettext("'%s' property cannot " 1959 "be reverted to a received value\n"), propname); 1960 return (1); 1961 } 1962 } else if (!zfs_prop_user(propname)) { 1963 (void) fprintf(stderr, gettext("invalid property '%s'\n"), 1964 propname); 1965 usage(B_FALSE); 1966 } 1967 1968 cb.cb_propname = propname; 1969 cb.cb_received = received; 1970 1971 if (flags & ZFS_ITER_RECURSE) { 1972 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, 1973 NULL, NULL, 0, inherit_recurse_cb, &cb); 1974 } else { 1975 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, 1976 NULL, NULL, 0, inherit_cb, &cb); 1977 } 1978 1979 return (ret); 1980} 1981 1982typedef struct upgrade_cbdata { 1983 uint64_t cb_numupgraded; 1984 uint64_t cb_numsamegraded; 1985 uint64_t cb_numfailed; 1986 uint64_t cb_version; 1987 boolean_t cb_newer; 1988 boolean_t cb_foundone; 1989 char cb_lastfs[ZFS_MAXNAMELEN]; 1990} upgrade_cbdata_t; 1991 1992static int 1993same_pool(zfs_handle_t *zhp, const char *name) 1994{ 1995 int len1 = strcspn(name, "/@"); 1996 const char *zhname = zfs_get_name(zhp); 1997 int len2 = strcspn(zhname, "/@"); 1998 1999 if (len1 != len2) 2000 return (B_FALSE); 2001 return (strncmp(name, zhname, len1) == 0); 2002} 2003 2004static int 2005upgrade_list_callback(zfs_handle_t *zhp, void *data) 2006{ 2007 upgrade_cbdata_t *cb = data; 2008 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 2009 2010 /* list if it's old/new */ 2011 if ((!cb->cb_newer && version < ZPL_VERSION) || 2012 (cb->cb_newer && version > ZPL_VERSION)) { 2013 char *str; 2014 if (cb->cb_newer) { 2015 str = gettext("The following filesystems are " 2016 "formatted using a newer software version and\n" 2017 "cannot be accessed on the current system.\n\n"); 2018 } else { 2019 str = gettext("The following filesystems are " 2020 "out of date, and can be upgraded. After being\n" 2021 "upgraded, these filesystems (and any 'zfs send' " 2022 "streams generated from\n" 2023 "subsequent snapshots) will no longer be " 2024 "accessible by older software versions.\n\n"); 2025 } 2026 2027 if (!cb->cb_foundone) { 2028 (void) puts(str); 2029 (void) printf(gettext("VER FILESYSTEM\n")); 2030 (void) printf(gettext("--- ------------\n")); 2031 cb->cb_foundone = B_TRUE; 2032 } 2033 2034 (void) printf("%2u %s\n", version, zfs_get_name(zhp)); 2035 } 2036 2037 return (0); 2038} 2039 2040static int 2041upgrade_set_callback(zfs_handle_t *zhp, void *data) 2042{ 2043 upgrade_cbdata_t *cb = data; 2044 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 2045 int needed_spa_version; 2046 int spa_version; 2047 2048 if (zfs_spa_version(zhp, &spa_version) < 0) 2049 return (-1); 2050 2051 needed_spa_version = zfs_spa_version_map(cb->cb_version); 2052 2053 if (needed_spa_version < 0) 2054 return (-1); 2055 2056 if (spa_version < needed_spa_version) { 2057 /* can't upgrade */ 2058 (void) printf(gettext("%s: can not be " 2059 "upgraded; the pool version needs to first " 2060 "be upgraded\nto version %d\n\n"), 2061 zfs_get_name(zhp), needed_spa_version); 2062 cb->cb_numfailed++; 2063 return (0); 2064 } 2065 2066 /* upgrade */ 2067 if (version < cb->cb_version) { 2068 char verstr[16]; 2069 (void) snprintf(verstr, sizeof (verstr), 2070 "%llu", cb->cb_version); 2071 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) { 2072 /* 2073 * If they did "zfs upgrade -a", then we could 2074 * be doing ioctls to different pools. We need 2075 * to log this history once to each pool, and bypass 2076 * the normal history logging that happens in main(). 2077 */ 2078 (void) zpool_log_history(g_zfs, history_str); 2079 log_history = B_FALSE; 2080 } 2081 if (zfs_prop_set(zhp, "version", verstr) == 0) 2082 cb->cb_numupgraded++; 2083 else 2084 cb->cb_numfailed++; 2085 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp)); 2086 } else if (version > cb->cb_version) { 2087 /* can't downgrade */ 2088 (void) printf(gettext("%s: can not be downgraded; " 2089 "it is already at version %u\n"), 2090 zfs_get_name(zhp), version); 2091 cb->cb_numfailed++; 2092 } else { 2093 cb->cb_numsamegraded++; 2094 } 2095 return (0); 2096} 2097 2098/* 2099 * zfs upgrade 2100 * zfs upgrade -v 2101 * zfs upgrade [-r] [-V <version>] <-a | filesystem> 2102 */ 2103static int 2104zfs_do_upgrade(int argc, char **argv) 2105{ 2106 boolean_t all = B_FALSE; 2107 boolean_t showversions = B_FALSE; 2108 int ret = 0; 2109 upgrade_cbdata_t cb = { 0 }; 2110 int c; 2111 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS; 2112 2113 /* check options */ 2114 while ((c = getopt(argc, argv, "rvV:a")) != -1) { 2115 switch (c) { 2116 case 'r': 2117 flags |= ZFS_ITER_RECURSE; 2118 break; 2119 case 'v': 2120 showversions = B_TRUE; 2121 break; 2122 case 'V': 2123 if (zfs_prop_string_to_index(ZFS_PROP_VERSION, 2124 optarg, &cb.cb_version) != 0) { 2125 (void) fprintf(stderr, 2126 gettext("invalid version %s\n"), optarg); 2127 usage(B_FALSE); 2128 } 2129 break; 2130 case 'a': 2131 all = B_TRUE; 2132 break; 2133 case '?': 2134 default: 2135 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2136 optopt); 2137 usage(B_FALSE); 2138 } 2139 } 2140 2141 argc -= optind; 2142 argv += optind; 2143 2144 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version)) 2145 usage(B_FALSE); 2146 if (showversions && (flags & ZFS_ITER_RECURSE || all || 2147 cb.cb_version || argc)) 2148 usage(B_FALSE); 2149 if ((all || argc) && (showversions)) 2150 usage(B_FALSE); 2151 if (all && argc) 2152 usage(B_FALSE); 2153 2154 if (showversions) { 2155 /* Show info on available versions. */ 2156 (void) printf(gettext("The following filesystem versions are " 2157 "supported:\n\n")); 2158 (void) printf(gettext("VER DESCRIPTION\n")); 2159 (void) printf("--- -----------------------------------------" 2160 "---------------\n"); 2161 (void) printf(gettext(" 1 Initial ZFS filesystem version\n")); 2162 (void) printf(gettext(" 2 Enhanced directory entries\n")); 2163 (void) printf(gettext(" 3 Case insensitive and filesystem " 2164 "user identifier (FUID)\n")); 2165 (void) printf(gettext(" 4 userquota, groupquota " 2166 "properties\n")); 2167 (void) printf(gettext(" 5 System attributes\n")); 2168 (void) printf(gettext("\nFor more information on a particular " 2169 "version, including supported releases,\n")); 2170 (void) printf("see the ZFS Administration Guide.\n\n"); 2171 ret = 0; 2172 } else if (argc || all) { 2173 /* Upgrade filesystems */ 2174 if (cb.cb_version == 0) 2175 cb.cb_version = ZPL_VERSION; 2176 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM, 2177 NULL, NULL, 0, upgrade_set_callback, &cb); 2178 (void) printf(gettext("%llu filesystems upgraded\n"), 2179 cb.cb_numupgraded); 2180 if (cb.cb_numsamegraded) { 2181 (void) printf(gettext("%llu filesystems already at " 2182 "this version\n"), 2183 cb.cb_numsamegraded); 2184 } 2185 if (cb.cb_numfailed != 0) 2186 ret = 1; 2187 } else { 2188 /* List old-version filesytems */ 2189 boolean_t found; 2190 (void) printf(gettext("This system is currently running " 2191 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION); 2192 2193 flags |= ZFS_ITER_RECURSE; 2194 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM, 2195 NULL, NULL, 0, upgrade_list_callback, &cb); 2196 2197 found = cb.cb_foundone; 2198 cb.cb_foundone = B_FALSE; 2199 cb.cb_newer = B_TRUE; 2200 2201 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM, 2202 NULL, NULL, 0, upgrade_list_callback, &cb); 2203 2204 if (!cb.cb_foundone && !found) { 2205 (void) printf(gettext("All filesystems are " 2206 "formatted with the current version.\n")); 2207 } 2208 } 2209 2210 return (ret); 2211} 2212 2213/* 2214 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...] 2215 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot 2216 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...] 2217 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot 2218 * 2219 * -H Scripted mode; elide headers and separate columns by tabs. 2220 * -i Translate SID to POSIX ID. 2221 * -n Print numeric ID instead of user/group name. 2222 * -o Control which fields to display. 2223 * -p Use exact (parsable) numeric output. 2224 * -s Specify sort columns, descending order. 2225 * -S Specify sort columns, ascending order. 2226 * -t Control which object types to display. 2227 * 2228 * Displays space consumed by, and quotas on, each user in the specified 2229 * filesystem or snapshot. 2230 */ 2231 2232/* us_field_types, us_field_hdr and us_field_names should be kept in sync */ 2233enum us_field_types { 2234 USFIELD_TYPE, 2235 USFIELD_NAME, 2236 USFIELD_USED, 2237 USFIELD_QUOTA 2238}; 2239static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" }; 2240static char *us_field_names[] = { "type", "name", "used", "quota" }; 2241#define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *)) 2242 2243#define USTYPE_PSX_GRP (1 << 0) 2244#define USTYPE_PSX_USR (1 << 1) 2245#define USTYPE_SMB_GRP (1 << 2) 2246#define USTYPE_SMB_USR (1 << 3) 2247#define USTYPE_ALL \ 2248 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR) 2249 2250static int us_type_bits[] = { 2251 USTYPE_PSX_GRP, 2252 USTYPE_PSX_USR, 2253 USTYPE_SMB_GRP, 2254 USTYPE_SMB_USR, 2255 USTYPE_ALL 2256}; 2257static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup", 2258 "smbuser", "all" }; 2259 2260typedef struct us_node { 2261 nvlist_t *usn_nvl; 2262 uu_avl_node_t usn_avlnode; 2263 uu_list_node_t usn_listnode; 2264} us_node_t; 2265 2266typedef struct us_cbdata { 2267 nvlist_t **cb_nvlp; 2268 uu_avl_pool_t *cb_avl_pool; 2269 uu_avl_t *cb_avl; 2270 boolean_t cb_numname; 2271 boolean_t cb_nicenum; 2272 boolean_t cb_sid2posix; 2273 zfs_userquota_prop_t cb_prop; 2274 zfs_sort_column_t *cb_sortcol; 2275 size_t cb_width[USFIELD_LAST]; 2276} us_cbdata_t; 2277 2278static boolean_t us_populated = B_FALSE; 2279 2280typedef struct { 2281 zfs_sort_column_t *si_sortcol; 2282 boolean_t si_numname; 2283} us_sort_info_t; 2284 2285static int 2286us_field_index(char *field) 2287{ 2288 int i; 2289 2290 for (i = 0; i < USFIELD_LAST; i++) { 2291 if (strcmp(field, us_field_names[i]) == 0) 2292 return (i); 2293 } 2294 2295 return (-1); 2296} 2297 2298static int 2299us_compare(const void *larg, const void *rarg, void *unused) 2300{ 2301 const us_node_t *l = larg; 2302 const us_node_t *r = rarg; 2303 us_sort_info_t *si = (us_sort_info_t *)unused; 2304 zfs_sort_column_t *sortcol = si->si_sortcol; 2305 boolean_t numname = si->si_numname; 2306 nvlist_t *lnvl = l->usn_nvl; 2307 nvlist_t *rnvl = r->usn_nvl; 2308 int rc = 0; 2309 boolean_t lvb, rvb; 2310 2311 for (; sortcol != NULL; sortcol = sortcol->sc_next) { 2312 char *lvstr = ""; 2313 char *rvstr = ""; 2314 uint32_t lv32 = 0; 2315 uint32_t rv32 = 0; 2316 uint64_t lv64 = 0; 2317 uint64_t rv64 = 0; 2318 zfs_prop_t prop = sortcol->sc_prop; 2319 const char *propname = NULL; 2320 boolean_t reverse = sortcol->sc_reverse; 2321 2322 switch (prop) { 2323 case ZFS_PROP_TYPE: 2324 propname = "type"; 2325 (void) nvlist_lookup_uint32(lnvl, propname, &lv32); 2326 (void) nvlist_lookup_uint32(rnvl, propname, &rv32); 2327 if (rv32 != lv32) 2328 rc = (rv32 < lv32) ? 1 : -1; 2329 break; 2330 case ZFS_PROP_NAME: 2331 propname = "name"; 2332 if (numname) { 2333 (void) nvlist_lookup_uint64(lnvl, propname, 2334 &lv64); 2335 (void) nvlist_lookup_uint64(rnvl, propname, 2336 &rv64); 2337 if (rv64 != lv64) 2338 rc = (rv64 < lv64) ? 1 : -1; 2339 } else { 2340 (void) nvlist_lookup_string(lnvl, propname, 2341 &lvstr); 2342 (void) nvlist_lookup_string(rnvl, propname, 2343 &rvstr); 2344 rc = strcmp(lvstr, rvstr); 2345 } 2346 break; 2347 case ZFS_PROP_USED: 2348 case ZFS_PROP_QUOTA: 2349 if (!us_populated) 2350 break; 2351 if (prop == ZFS_PROP_USED) 2352 propname = "used"; 2353 else 2354 propname = "quota"; 2355 (void) nvlist_lookup_uint64(lnvl, propname, &lv64); 2356 (void) nvlist_lookup_uint64(rnvl, propname, &rv64); 2357 if (rv64 != lv64) 2358 rc = (rv64 < lv64) ? 1 : -1; 2359 break; 2360 } 2361 2362 if (rc != 0) { 2363 if (rc < 0) 2364 return (reverse ? 1 : -1); 2365 else 2366 return (reverse ? -1 : 1); 2367 } 2368 } 2369 2370 /* 2371 * If entries still seem to be the same, check if they are of the same 2372 * type (smbentity is added only if we are doing SID to POSIX ID 2373 * translation where we can have duplicate type/name combinations). 2374 */ 2375 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 && 2376 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 && 2377 lvb != rvb) 2378 return (lvb < rvb ? -1 : 1); 2379 2380 return (0); 2381} 2382 2383static inline const char * 2384us_type2str(unsigned field_type) 2385{ 2386 switch (field_type) { 2387 case USTYPE_PSX_USR: 2388 return ("POSIX User"); 2389 case USTYPE_PSX_GRP: 2390 return ("POSIX Group"); 2391 case USTYPE_SMB_USR: 2392 return ("SMB User"); 2393 case USTYPE_SMB_GRP: 2394 return ("SMB Group"); 2395 default: 2396 return ("Undefined"); 2397 } 2398} 2399 2400static int 2401userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space) 2402{ 2403 us_cbdata_t *cb = (us_cbdata_t *)arg; 2404 zfs_userquota_prop_t prop = cb->cb_prop; 2405 char *name = NULL; 2406 char *propname; 2407 char sizebuf[32]; 2408 us_node_t *node; 2409 uu_avl_pool_t *avl_pool = cb->cb_avl_pool; 2410 uu_avl_t *avl = cb->cb_avl; 2411 uu_avl_index_t idx; 2412 nvlist_t *props; 2413 us_node_t *n; 2414 zfs_sort_column_t *sortcol = cb->cb_sortcol; 2415 unsigned type; 2416 const char *typestr; 2417 size_t namelen; 2418 size_t typelen; 2419 size_t sizelen; 2420 int typeidx, nameidx, sizeidx; 2421 us_sort_info_t sortinfo = { sortcol, cb->cb_numname }; 2422 boolean_t smbentity = B_FALSE; 2423 2424 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 2425 nomem(); 2426 node = safe_malloc(sizeof (us_node_t)); 2427 uu_avl_node_init(node, &node->usn_avlnode, avl_pool); 2428 node->usn_nvl = props; 2429 2430 if (domain != NULL && domain[0] != '\0') { 2431 /* SMB */ 2432 char sid[ZFS_MAXNAMELEN + 32]; 2433 uid_t id; 2434#ifdef sun 2435 int err; 2436 int flag = IDMAP_REQ_FLG_USE_CACHE; 2437#endif 2438 2439 smbentity = B_TRUE; 2440 2441 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid); 2442 2443 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) { 2444 type = USTYPE_SMB_GRP; 2445#ifdef sun 2446 err = sid_to_id(sid, B_FALSE, &id); 2447#endif 2448 } else { 2449 type = USTYPE_SMB_USR; 2450#ifdef sun 2451 err = sid_to_id(sid, B_TRUE, &id); 2452#endif 2453 } 2454 2455#ifdef sun 2456 if (err == 0) { 2457 rid = id; 2458 if (!cb->cb_sid2posix) { 2459 if (type == USTYPE_SMB_USR) { 2460 (void) idmap_getwinnamebyuid(rid, flag, 2461 &name, NULL); 2462 } else { 2463 (void) idmap_getwinnamebygid(rid, flag, 2464 &name, NULL); 2465 } 2466 if (name == NULL) 2467 name = sid; 2468 } 2469 } 2470#endif 2471 } 2472 2473 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') { 2474 /* POSIX or -i */ 2475 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) { 2476 type = USTYPE_PSX_GRP; 2477 if (!cb->cb_numname) { 2478 struct group *g; 2479 2480 if ((g = getgrgid(rid)) != NULL) 2481 name = g->gr_name; 2482 } 2483 } else { 2484 type = USTYPE_PSX_USR; 2485 if (!cb->cb_numname) { 2486 struct passwd *p; 2487 2488 if ((p = getpwuid(rid)) != NULL) 2489 name = p->pw_name; 2490 } 2491 } 2492 } 2493 2494 /* 2495 * Make sure that the type/name combination is unique when doing 2496 * SID to POSIX ID translation (hence changing the type from SMB to 2497 * POSIX). 2498 */ 2499 if (cb->cb_sid2posix && 2500 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0) 2501 nomem(); 2502 2503 /* Calculate/update width of TYPE field */ 2504 typestr = us_type2str(type); 2505 typelen = strlen(gettext(typestr)); 2506 typeidx = us_field_index("type"); 2507 if (typelen > cb->cb_width[typeidx]) 2508 cb->cb_width[typeidx] = typelen; 2509 if (nvlist_add_uint32(props, "type", type) != 0) 2510 nomem(); 2511 2512 /* Calculate/update width of NAME field */ 2513 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) { 2514 if (nvlist_add_uint64(props, "name", rid) != 0) 2515 nomem(); 2516 namelen = snprintf(NULL, 0, "%u", rid); 2517 } else { 2518 if (nvlist_add_string(props, "name", name) != 0) 2519 nomem(); 2520 namelen = strlen(name); 2521 } 2522 nameidx = us_field_index("name"); 2523 if (namelen > cb->cb_width[nameidx]) 2524 cb->cb_width[nameidx] = namelen; 2525 2526 /* 2527 * Check if this type/name combination is in the list and update it; 2528 * otherwise add new node to the list. 2529 */ 2530 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) { 2531 uu_avl_insert(avl, node, idx); 2532 } else { 2533 nvlist_free(props); 2534 free(node); 2535 node = n; 2536 props = node->usn_nvl; 2537 } 2538 2539 /* Calculate/update width of USED/QUOTA fields */ 2540 if (cb->cb_nicenum) 2541 zfs_nicenum(space, sizebuf, sizeof (sizebuf)); 2542 else 2543 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space); 2544 sizelen = strlen(sizebuf); 2545 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) { 2546 propname = "used"; 2547 if (!nvlist_exists(props, "quota")) 2548 (void) nvlist_add_uint64(props, "quota", 0); 2549 } else { 2550 propname = "quota"; 2551 if (!nvlist_exists(props, "used")) 2552 (void) nvlist_add_uint64(props, "used", 0); 2553 } 2554 sizeidx = us_field_index(propname); 2555 if (sizelen > cb->cb_width[sizeidx]) 2556 cb->cb_width[sizeidx] = sizelen; 2557 2558 if (nvlist_add_uint64(props, propname, space) != 0) 2559 nomem(); 2560 2561 return (0); 2562} 2563 2564static void 2565print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types, 2566 size_t *width, us_node_t *node) 2567{ 2568 nvlist_t *nvl = node->usn_nvl; 2569 char valstr[ZFS_MAXNAMELEN]; 2570 boolean_t first = B_TRUE; 2571 int cfield = 0; 2572 int field; 2573 uint32_t ustype; 2574 2575 /* Check type */ 2576 (void) nvlist_lookup_uint32(nvl, "type", &ustype); 2577 if (!(ustype & types)) 2578 return; 2579 2580 while ((field = fields[cfield]) != USFIELD_LAST) { 2581 nvpair_t *nvp = NULL; 2582 data_type_t type; 2583 uint32_t val32; 2584 uint64_t val64; 2585 char *strval = NULL; 2586 2587 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 2588 if (strcmp(nvpair_name(nvp), 2589 us_field_names[field]) == 0) 2590 break; 2591 } 2592 2593 type = nvpair_type(nvp); 2594 switch (type) { 2595 case DATA_TYPE_UINT32: 2596 (void) nvpair_value_uint32(nvp, &val32); 2597 break; 2598 case DATA_TYPE_UINT64: 2599 (void) nvpair_value_uint64(nvp, &val64); 2600 break; 2601 case DATA_TYPE_STRING: 2602 (void) nvpair_value_string(nvp, &strval); 2603 break; 2604 default: 2605 (void) fprintf(stderr, "invalid data type\n"); 2606 } 2607 2608 switch (field) { 2609 case USFIELD_TYPE: 2610 strval = (char *)us_type2str(val32); 2611 break; 2612 case USFIELD_NAME: 2613 if (type == DATA_TYPE_UINT64) { 2614 (void) sprintf(valstr, "%llu", val64); 2615 strval = valstr; 2616 } 2617 break; 2618 case USFIELD_USED: 2619 case USFIELD_QUOTA: 2620 if (type == DATA_TYPE_UINT64) { 2621 if (parsable) { 2622 (void) sprintf(valstr, "%llu", val64); 2623 } else { 2624 zfs_nicenum(val64, valstr, 2625 sizeof (valstr)); 2626 } 2627 if (field == USFIELD_QUOTA && 2628 strcmp(valstr, "0") == 0) 2629 strval = "none"; 2630 else 2631 strval = valstr; 2632 } 2633 break; 2634 } 2635 2636 if (!first) { 2637 if (scripted) 2638 (void) printf("\t"); 2639 else 2640 (void) printf(" "); 2641 } 2642 if (scripted) 2643 (void) printf("%s", strval); 2644 else if (field == USFIELD_TYPE || field == USFIELD_NAME) 2645 (void) printf("%-*s", width[field], strval); 2646 else 2647 (void) printf("%*s", width[field], strval); 2648 2649 first = B_FALSE; 2650 cfield++; 2651 } 2652 2653 (void) printf("\n"); 2654} 2655 2656static void 2657print_us(boolean_t scripted, boolean_t parsable, int *fields, int types, 2658 size_t *width, boolean_t rmnode, uu_avl_t *avl) 2659{ 2660 us_node_t *node; 2661 const char *col; 2662 int cfield = 0; 2663 int field; 2664 2665 if (!scripted) { 2666 boolean_t first = B_TRUE; 2667 2668 while ((field = fields[cfield]) != USFIELD_LAST) { 2669 col = gettext(us_field_hdr[field]); 2670 if (field == USFIELD_TYPE || field == USFIELD_NAME) { 2671 (void) printf(first ? "%-*s" : " %-*s", 2672 width[field], col); 2673 } else { 2674 (void) printf(first ? "%*s" : " %*s", 2675 width[field], col); 2676 } 2677 first = B_FALSE; 2678 cfield++; 2679 } 2680 (void) printf("\n"); 2681 } 2682 2683 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) { 2684 print_us_node(scripted, parsable, fields, types, width, node); 2685 if (rmnode) 2686 nvlist_free(node->usn_nvl); 2687 } 2688} 2689 2690static int 2691zfs_do_userspace(int argc, char **argv) 2692{ 2693 zfs_handle_t *zhp; 2694 zfs_userquota_prop_t p; 2695 2696 uu_avl_pool_t *avl_pool; 2697 uu_avl_t *avl_tree; 2698 uu_avl_walk_t *walk; 2699 char *delim; 2700 char deffields[] = "type,name,used,quota"; 2701 char *ofield = NULL; 2702 char *tfield = NULL; 2703 int cfield = 0; 2704 int fields[256]; 2705 int i; 2706 boolean_t scripted = B_FALSE; 2707 boolean_t prtnum = B_FALSE; 2708 boolean_t parsable = B_FALSE; 2709 boolean_t sid2posix = B_FALSE; 2710 int ret = 0; 2711 int c; 2712 zfs_sort_column_t *sortcol = NULL; 2713 int types = USTYPE_PSX_USR | USTYPE_SMB_USR; 2714 us_cbdata_t cb; 2715 us_node_t *node; 2716 us_node_t *rmnode; 2717 uu_list_pool_t *listpool; 2718 uu_list_t *list; 2719 uu_avl_index_t idx = 0; 2720 uu_list_index_t idx2 = 0; 2721 2722 if (argc < 2) 2723 usage(B_FALSE); 2724 2725 if (strcmp(argv[0], "groupspace") == 0) 2726 /* Toggle default group types */ 2727 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP; 2728 2729 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) { 2730 switch (c) { 2731 case 'n': 2732 prtnum = B_TRUE; 2733 break; 2734 case 'H': 2735 scripted = B_TRUE; 2736 break; 2737 case 'p': 2738 parsable = B_TRUE; 2739 break; 2740 case 'o': 2741 ofield = optarg; 2742 break; 2743 case 's': 2744 case 'S': 2745 if (zfs_add_sort_column(&sortcol, optarg, 2746 c == 's' ? B_FALSE : B_TRUE) != 0) { 2747 (void) fprintf(stderr, 2748 gettext("invalid field '%s'\n"), optarg); 2749 usage(B_FALSE); 2750 } 2751 break; 2752 case 't': 2753 tfield = optarg; 2754 break; 2755 case 'i': 2756 sid2posix = B_TRUE; 2757 break; 2758 case ':': 2759 (void) fprintf(stderr, gettext("missing argument for " 2760 "'%c' option\n"), optopt); 2761 usage(B_FALSE); 2762 break; 2763 case '?': 2764 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2765 optopt); 2766 usage(B_FALSE); 2767 } 2768 } 2769 2770 argc -= optind; 2771 argv += optind; 2772 2773 if (argc < 1) { 2774 (void) fprintf(stderr, gettext("missing dataset name\n")); 2775 usage(B_FALSE); 2776 } 2777 if (argc > 1) { 2778 (void) fprintf(stderr, gettext("too many arguments\n")); 2779 usage(B_FALSE); 2780 } 2781 2782 /* Use default output fields if not specified using -o */ 2783 if (ofield == NULL) 2784 ofield = deffields; 2785 do { 2786 if ((delim = strchr(ofield, ',')) != NULL) 2787 *delim = '\0'; 2788 if ((fields[cfield++] = us_field_index(ofield)) == -1) { 2789 (void) fprintf(stderr, gettext("invalid type '%s' " 2790 "for -o option\n"), ofield); 2791 return (-1); 2792 } 2793 if (delim != NULL) 2794 ofield = delim + 1; 2795 } while (delim != NULL); 2796 fields[cfield] = USFIELD_LAST; 2797 2798 /* Override output types (-t option) */ 2799 if (tfield != NULL) { 2800 types = 0; 2801 2802 do { 2803 boolean_t found = B_FALSE; 2804 2805 if ((delim = strchr(tfield, ',')) != NULL) 2806 *delim = '\0'; 2807 for (i = 0; i < sizeof (us_type_bits) / sizeof (int); 2808 i++) { 2809 if (strcmp(tfield, us_type_names[i]) == 0) { 2810 found = B_TRUE; 2811 types |= us_type_bits[i]; 2812 break; 2813 } 2814 } 2815 if (!found) { 2816 (void) fprintf(stderr, gettext("invalid type " 2817 "'%s' for -t option\n"), tfield); 2818 return (-1); 2819 } 2820 if (delim != NULL) 2821 tfield = delim + 1; 2822 } while (delim != NULL); 2823 } 2824 2825 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL) 2826 return (1); 2827 2828 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t), 2829 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL) 2830 nomem(); 2831 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL) 2832 nomem(); 2833 2834 /* Always add default sorting columns */ 2835 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE); 2836 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE); 2837 2838 cb.cb_sortcol = sortcol; 2839 cb.cb_numname = prtnum; 2840 cb.cb_nicenum = !parsable; 2841 cb.cb_avl_pool = avl_pool; 2842 cb.cb_avl = avl_tree; 2843 cb.cb_sid2posix = sid2posix; 2844 2845 for (i = 0; i < USFIELD_LAST; i++) 2846 cb.cb_width[i] = strlen(gettext(us_field_hdr[i])); 2847 2848 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) { 2849 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) && 2850 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) || 2851 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) && 2852 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP)))) 2853 continue; 2854 cb.cb_prop = p; 2855 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0) 2856 return (ret); 2857 } 2858 2859 /* Sort the list */ 2860 if ((node = uu_avl_first(avl_tree)) == NULL) 2861 return (0); 2862 2863 us_populated = B_TRUE; 2864 2865 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t), 2866 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT); 2867 list = uu_list_create(listpool, NULL, UU_DEFAULT); 2868 uu_list_node_init(node, &node->usn_listnode, listpool); 2869 2870 while (node != NULL) { 2871 rmnode = node; 2872 node = uu_avl_next(avl_tree, node); 2873 uu_avl_remove(avl_tree, rmnode); 2874 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL) 2875 uu_list_insert(list, rmnode, idx2); 2876 } 2877 2878 for (node = uu_list_first(list); node != NULL; 2879 node = uu_list_next(list, node)) { 2880 us_sort_info_t sortinfo = { sortcol, cb.cb_numname }; 2881 2882 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL) 2883 uu_avl_insert(avl_tree, node, idx); 2884 } 2885 2886 uu_list_destroy(list); 2887 uu_list_pool_destroy(listpool); 2888 2889 /* Print and free node nvlist memory */ 2890 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE, 2891 cb.cb_avl); 2892 2893 zfs_free_sort_columns(sortcol); 2894 2895 /* Clean up the AVL tree */ 2896 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL) 2897 nomem(); 2898 2899 while ((node = uu_avl_walk_next(walk)) != NULL) { 2900 uu_avl_remove(cb.cb_avl, node); 2901 free(node); 2902 } 2903 2904 uu_avl_walk_end(walk); 2905 uu_avl_destroy(avl_tree); 2906 uu_avl_pool_destroy(avl_pool); 2907 2908 return (ret); 2909} 2910 2911/* 2912 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ... 2913 * [-t type[,...]] [filesystem|volume|snapshot] ... 2914 * 2915 * -H Scripted mode; elide headers and separate columns by tabs. 2916 * -p Display values in parsable (literal) format. 2917 * -r Recurse over all children. 2918 * -d Limit recursion by depth. 2919 * -o Control which fields to display. 2920 * -s Specify sort columns, descending order. 2921 * -S Specify sort columns, ascending order. 2922 * -t Control which object types to display. 2923 * 2924 * When given no arguments, list all filesystems in the system. 2925 * Otherwise, list the specified datasets, optionally recursing down them if 2926 * '-r' is specified. 2927 */ 2928typedef struct list_cbdata { 2929 boolean_t cb_first; 2930 boolean_t cb_literal; 2931 boolean_t cb_scripted; 2932 zprop_list_t *cb_proplist; 2933} list_cbdata_t; 2934 2935/* 2936 * Given a list of columns to display, output appropriate headers for each one. 2937 */ 2938static void 2939print_header(list_cbdata_t *cb) 2940{ 2941 zprop_list_t *pl = cb->cb_proplist; 2942 char headerbuf[ZFS_MAXPROPLEN]; 2943 const char *header; 2944 int i; 2945 boolean_t first = B_TRUE; 2946 boolean_t right_justify; 2947 2948 for (; pl != NULL; pl = pl->pl_next) { 2949 if (!first) { 2950 (void) printf(" "); 2951 } else { 2952 first = B_FALSE; 2953 } 2954 2955 right_justify = B_FALSE; 2956 if (pl->pl_prop != ZPROP_INVAL) { 2957 header = zfs_prop_column_name(pl->pl_prop); 2958 right_justify = zfs_prop_align_right(pl->pl_prop); 2959 } else { 2960 for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 2961 headerbuf[i] = toupper(pl->pl_user_prop[i]); 2962 headerbuf[i] = '\0'; 2963 header = headerbuf; 2964 } 2965 2966 if (pl->pl_next == NULL && !right_justify) 2967 (void) printf("%s", header); 2968 else if (right_justify) 2969 (void) printf("%*s", pl->pl_width, header); 2970 else 2971 (void) printf("%-*s", pl->pl_width, header); 2972 } 2973 2974 (void) printf("\n"); 2975} 2976 2977/* 2978 * Given a dataset and a list of fields, print out all the properties according 2979 * to the described layout. 2980 */ 2981static void 2982print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb) 2983{ 2984 zprop_list_t *pl = cb->cb_proplist; 2985 boolean_t first = B_TRUE; 2986 char property[ZFS_MAXPROPLEN]; 2987 nvlist_t *userprops = zfs_get_user_props(zhp); 2988 nvlist_t *propval; 2989 char *propstr; 2990 boolean_t right_justify; 2991 2992 for (; pl != NULL; pl = pl->pl_next) { 2993 if (!first) { 2994 if (cb->cb_scripted) 2995 (void) printf("\t"); 2996 else 2997 (void) printf(" "); 2998 } else { 2999 first = B_FALSE; 3000 } 3001 3002 if (pl->pl_prop == ZFS_PROP_NAME) { 3003 (void) strlcpy(property, zfs_get_name(zhp), 3004 sizeof(property)); 3005 propstr = property; 3006 right_justify = zfs_prop_align_right(pl->pl_prop); 3007 } else if (pl->pl_prop != ZPROP_INVAL) { 3008 if (zfs_prop_get(zhp, pl->pl_prop, property, 3009 sizeof (property), NULL, NULL, 0, 3010 cb->cb_literal) != 0) 3011 propstr = "-"; 3012 else 3013 propstr = property; 3014 right_justify = zfs_prop_align_right(pl->pl_prop); 3015 } else if (zfs_prop_userquota(pl->pl_user_prop)) { 3016 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop, 3017 property, sizeof (property), cb->cb_literal) != 0) 3018 propstr = "-"; 3019 else 3020 propstr = property; 3021 right_justify = B_TRUE; 3022 } else if (zfs_prop_written(pl->pl_user_prop)) { 3023 if (zfs_prop_get_written(zhp, pl->pl_user_prop, 3024 property, sizeof (property), cb->cb_literal) != 0) 3025 propstr = "-"; 3026 else 3027 propstr = property; 3028 right_justify = B_TRUE; 3029 } else { 3030 if (nvlist_lookup_nvlist(userprops, 3031 pl->pl_user_prop, &propval) != 0) 3032 propstr = "-"; 3033 else 3034 verify(nvlist_lookup_string(propval, 3035 ZPROP_VALUE, &propstr) == 0); 3036 right_justify = B_FALSE; 3037 } 3038 3039 /* 3040 * If this is being called in scripted mode, or if this is the 3041 * last column and it is left-justified, don't include a width 3042 * format specifier. 3043 */ 3044 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify)) 3045 (void) printf("%s", propstr); 3046 else if (right_justify) 3047 (void) printf("%*s", pl->pl_width, propstr); 3048 else 3049 (void) printf("%-*s", pl->pl_width, propstr); 3050 } 3051 3052 (void) printf("\n"); 3053} 3054 3055/* 3056 * Generic callback function to list a dataset or snapshot. 3057 */ 3058static int 3059list_callback(zfs_handle_t *zhp, void *data) 3060{ 3061 list_cbdata_t *cbp = data; 3062 3063 if (cbp->cb_first) { 3064 if (!cbp->cb_scripted) 3065 print_header(cbp); 3066 cbp->cb_first = B_FALSE; 3067 } 3068 3069 print_dataset(zhp, cbp); 3070 3071 return (0); 3072} 3073 3074static int 3075zfs_do_list(int argc, char **argv) 3076{ 3077 int c; 3078 static char default_fields[] = 3079 "name,used,available,referenced,mountpoint"; 3080 int types = ZFS_TYPE_DATASET; 3081 boolean_t types_specified = B_FALSE; 3082 char *fields = NULL; 3083 list_cbdata_t cb = { 0 }; 3084 char *value; 3085 int limit = 0; 3086 int ret = 0; 3087 zfs_sort_column_t *sortcol = NULL; 3088 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS; 3089 3090 /* check options */ 3091 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) { 3092 switch (c) { 3093 case 'o': 3094 fields = optarg; 3095 break; 3096 case 'p': 3097 cb.cb_literal = B_TRUE; 3098 flags |= ZFS_ITER_LITERAL_PROPS; 3099 break; 3100 case 'd': 3101 limit = parse_depth(optarg, &flags); 3102 break; 3103 case 'r': 3104 flags |= ZFS_ITER_RECURSE; 3105 break; 3106 case 'H': 3107 cb.cb_scripted = B_TRUE; 3108 break; 3109 case 's': 3110 if (zfs_add_sort_column(&sortcol, optarg, 3111 B_FALSE) != 0) { 3112 (void) fprintf(stderr, 3113 gettext("invalid property '%s'\n"), optarg); 3114 usage(B_FALSE); 3115 } 3116 break; 3117 case 'S': 3118 if (zfs_add_sort_column(&sortcol, optarg, 3119 B_TRUE) != 0) { 3120 (void) fprintf(stderr, 3121 gettext("invalid property '%s'\n"), optarg); 3122 usage(B_FALSE); 3123 } 3124 break; 3125 case 't': 3126 types = 0; 3127 types_specified = B_TRUE; 3128 flags &= ~ZFS_ITER_PROP_LISTSNAPS; 3129 while (*optarg != '\0') { 3130 static char *type_subopts[] = { "filesystem", 3131 "volume", "snapshot", "snap", "bookmark", 3132 "all", NULL }; 3133 3134 switch (getsubopt(&optarg, type_subopts, 3135 &value)) { 3136 case 0: 3137 types |= ZFS_TYPE_FILESYSTEM; 3138 break; 3139 case 1: 3140 types |= ZFS_TYPE_VOLUME; 3141 break; 3142 case 2: 3143 case 3: 3144 types |= ZFS_TYPE_SNAPSHOT; 3145 break; 3146 case 4: 3147 types |= ZFS_TYPE_BOOKMARK; 3148 break; 3149 case 5: 3150 types = ZFS_TYPE_DATASET | 3151 ZFS_TYPE_BOOKMARK; 3152 break; 3153 default: 3154 (void) fprintf(stderr, 3155 gettext("invalid type '%s'\n"), 3156 value); 3157 usage(B_FALSE); 3158 } 3159 } 3160 break; 3161 case ':': 3162 (void) fprintf(stderr, gettext("missing argument for " 3163 "'%c' option\n"), optopt); 3164 usage(B_FALSE); 3165 break; 3166 case '?': 3167 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3168 optopt); 3169 usage(B_FALSE); 3170 } 3171 } 3172 3173 argc -= optind; 3174 argv += optind; 3175 3176 if (fields == NULL) 3177 fields = default_fields; 3178 3179 /* 3180 * If we are only going to list snapshot names and sort by name, 3181 * then we can use faster version. 3182 */ 3183 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol)) 3184 flags |= ZFS_ITER_SIMPLE; 3185 3186 /* 3187 * If "-o space" and no types were specified, don't display snapshots. 3188 */ 3189 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE) 3190 types &= ~ZFS_TYPE_SNAPSHOT; 3191 3192 /* 3193 * If the user specifies '-o all', the zprop_get_list() doesn't 3194 * normally include the name of the dataset. For 'zfs list', we always 3195 * want this property to be first. 3196 */ 3197 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET) 3198 != 0) 3199 usage(B_FALSE); 3200 3201 cb.cb_first = B_TRUE; 3202 3203 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist, 3204 limit, list_callback, &cb); 3205 3206 zprop_free_list(cb.cb_proplist); 3207 zfs_free_sort_columns(sortcol); 3208 3209 if (ret == 0 && cb.cb_first && !cb.cb_scripted) 3210 (void) printf(gettext("no datasets available\n")); 3211 3212 return (ret); 3213} 3214 3215/* 3216 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol> 3217 * zfs rename [-f] -p <fs | vol> <fs | vol> 3218 * zfs rename -r <snap> <snap> 3219 * zfs rename -u [-p] <fs> <fs> 3220 * 3221 * Renames the given dataset to another of the same type. 3222 * 3223 * The '-p' flag creates all the non-existing ancestors of the target first. 3224 */ 3225/* ARGSUSED */ 3226static int 3227zfs_do_rename(int argc, char **argv) 3228{ 3229 zfs_handle_t *zhp; 3230 renameflags_t flags = { 0 }; 3231 int c; 3232 int ret = 0; 3233 int types; 3234 boolean_t parents = B_FALSE; 3235 char *snapshot = NULL; 3236 3237 /* check options */ 3238 while ((c = getopt(argc, argv, "fpru")) != -1) { 3239 switch (c) { 3240 case 'p': 3241 parents = B_TRUE; 3242 break; 3243 case 'r': 3244 flags.recurse = B_TRUE; 3245 break; 3246 case 'u': 3247 flags.nounmount = B_TRUE; 3248 break; 3249 case 'f': 3250 flags.forceunmount = B_TRUE; 3251 break; 3252 case '?': 3253 default: 3254 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3255 optopt); 3256 usage(B_FALSE); 3257 } 3258 } 3259 3260 argc -= optind; 3261 argv += optind; 3262 3263 /* check number of arguments */ 3264 if (argc < 1) { 3265 (void) fprintf(stderr, gettext("missing source dataset " 3266 "argument\n")); 3267 usage(B_FALSE); 3268 } 3269 if (argc < 2) { 3270 (void) fprintf(stderr, gettext("missing target dataset " 3271 "argument\n")); 3272 usage(B_FALSE); 3273 } 3274 if (argc > 2) { 3275 (void) fprintf(stderr, gettext("too many arguments\n")); 3276 usage(B_FALSE); 3277 } 3278 3279 if (flags.recurse && parents) { 3280 (void) fprintf(stderr, gettext("-p and -r options are mutually " 3281 "exclusive\n")); 3282 usage(B_FALSE); 3283 } 3284 3285 if (flags.recurse && strchr(argv[0], '@') == 0) { 3286 (void) fprintf(stderr, gettext("source dataset for recursive " 3287 "rename must be a snapshot\n")); 3288 usage(B_FALSE); 3289 } 3290 3291 if (flags.nounmount && parents) { 3292 (void) fprintf(stderr, gettext("-u and -p options are mutually " 3293 "exclusive\n")); 3294 usage(B_FALSE); 3295 } 3296 3297 if (flags.nounmount) 3298 types = ZFS_TYPE_FILESYSTEM; 3299 else if (parents) 3300 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME; 3301 else 3302 types = ZFS_TYPE_DATASET; 3303 3304 if (flags.recurse) { 3305 /* 3306 * When we do recursive rename we are fine when the given 3307 * snapshot for the given dataset doesn't exist - it can 3308 * still exists below. 3309 */ 3310 3311 snapshot = strchr(argv[0], '@'); 3312 assert(snapshot != NULL); 3313 *snapshot = '\0'; 3314 snapshot++; 3315 } 3316 3317 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) 3318 return (1); 3319 3320 /* If we were asked and the name looks good, try to create ancestors. */ 3321 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) && 3322 zfs_create_ancestors(g_zfs, argv[1]) != 0) { 3323 zfs_close(zhp); 3324 return (1); 3325 } 3326 3327 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0); 3328 3329 zfs_close(zhp); 3330 return (ret); 3331} 3332 3333/* 3334 * zfs promote <fs> 3335 * 3336 * Promotes the given clone fs to be the parent 3337 */ 3338/* ARGSUSED */ 3339static int 3340zfs_do_promote(int argc, char **argv) 3341{ 3342 zfs_handle_t *zhp; 3343 int ret = 0; 3344 3345 /* check options */ 3346 if (argc > 1 && argv[1][0] == '-') { 3347 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3348 argv[1][1]); 3349 usage(B_FALSE); 3350 } 3351 3352 /* check number of arguments */ 3353 if (argc < 2) { 3354 (void) fprintf(stderr, gettext("missing clone filesystem" 3355 " argument\n")); 3356 usage(B_FALSE); 3357 } 3358 if (argc > 2) { 3359 (void) fprintf(stderr, gettext("too many arguments\n")); 3360 usage(B_FALSE); 3361 } 3362 3363 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3364 if (zhp == NULL) 3365 return (1); 3366 3367 ret = (zfs_promote(zhp) != 0); 3368 3369 3370 zfs_close(zhp); 3371 return (ret); 3372} 3373 3374/* 3375 * zfs rollback [-rRf] <snapshot> 3376 * 3377 * -r Delete any intervening snapshots before doing rollback 3378 * -R Delete any snapshots and their clones 3379 * -f ignored for backwards compatability 3380 * 3381 * Given a filesystem, rollback to a specific snapshot, discarding any changes 3382 * since then and making it the active dataset. If more recent snapshots exist, 3383 * the command will complain unless the '-r' flag is given. 3384 */ 3385typedef struct rollback_cbdata { 3386 uint64_t cb_create; 3387 boolean_t cb_first; 3388 int cb_doclones; 3389 char *cb_target; 3390 int cb_error; 3391 boolean_t cb_recurse; 3392} rollback_cbdata_t; 3393 3394static int 3395rollback_check_dependent(zfs_handle_t *zhp, void *data) 3396{ 3397 rollback_cbdata_t *cbp = data; 3398 3399 if (cbp->cb_first && cbp->cb_recurse) { 3400 (void) fprintf(stderr, gettext("cannot rollback to " 3401 "'%s': clones of previous snapshots exist\n"), 3402 cbp->cb_target); 3403 (void) fprintf(stderr, gettext("use '-R' to " 3404 "force deletion of the following clones and " 3405 "dependents:\n")); 3406 cbp->cb_first = 0; 3407 cbp->cb_error = 1; 3408 } 3409 3410 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 3411 3412 zfs_close(zhp); 3413 return (0); 3414} 3415 3416/* 3417 * Report any snapshots more recent than the one specified. Used when '-r' is 3418 * not specified. We reuse this same callback for the snapshot dependents - if 3419 * 'cb_dependent' is set, then this is a dependent and we should report it 3420 * without checking the transaction group. 3421 */ 3422static int 3423rollback_check(zfs_handle_t *zhp, void *data) 3424{ 3425 rollback_cbdata_t *cbp = data; 3426 3427 if (cbp->cb_doclones) { 3428 zfs_close(zhp); 3429 return (0); 3430 } 3431 3432 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { 3433 if (cbp->cb_first && !cbp->cb_recurse) { 3434 (void) fprintf(stderr, gettext("cannot " 3435 "rollback to '%s': more recent snapshots " 3436 "or bookmarks exist\n"), 3437 cbp->cb_target); 3438 (void) fprintf(stderr, gettext("use '-r' to " 3439 "force deletion of the following " 3440 "snapshots and bookmarks:\n")); 3441 cbp->cb_first = 0; 3442 cbp->cb_error = 1; 3443 } 3444 3445 if (cbp->cb_recurse) { 3446 if (zfs_iter_dependents(zhp, B_TRUE, 3447 rollback_check_dependent, cbp) != 0) { 3448 zfs_close(zhp); 3449 return (-1); 3450 } 3451 } else { 3452 (void) fprintf(stderr, "%s\n", 3453 zfs_get_name(zhp)); 3454 } 3455 } 3456 zfs_close(zhp); 3457 return (0); 3458} 3459 3460static int 3461zfs_do_rollback(int argc, char **argv) 3462{ 3463 int ret = 0; 3464 int c; 3465 boolean_t force = B_FALSE; 3466 rollback_cbdata_t cb = { 0 }; 3467 zfs_handle_t *zhp, *snap; 3468 char parentname[ZFS_MAXNAMELEN]; 3469 char *delim; 3470 3471 /* check options */ 3472 while ((c = getopt(argc, argv, "rRf")) != -1) { 3473 switch (c) { 3474 case 'r': 3475 cb.cb_recurse = 1; 3476 break; 3477 case 'R': 3478 cb.cb_recurse = 1; 3479 cb.cb_doclones = 1; 3480 break; 3481 case 'f': 3482 force = B_TRUE; 3483 break; 3484 case '?': 3485 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3486 optopt); 3487 usage(B_FALSE); 3488 } 3489 } 3490 3491 argc -= optind; 3492 argv += optind; 3493 3494 /* check number of arguments */ 3495 if (argc < 1) { 3496 (void) fprintf(stderr, gettext("missing dataset argument\n")); 3497 usage(B_FALSE); 3498 } 3499 if (argc > 1) { 3500 (void) fprintf(stderr, gettext("too many arguments\n")); 3501 usage(B_FALSE); 3502 } 3503 3504 /* open the snapshot */ 3505 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 3506 return (1); 3507 3508 /* open the parent dataset */ 3509 (void) strlcpy(parentname, argv[0], sizeof (parentname)); 3510 verify((delim = strrchr(parentname, '@')) != NULL); 3511 *delim = '\0'; 3512 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) { 3513 zfs_close(snap); 3514 return (1); 3515 } 3516 3517 /* 3518 * Check for more recent snapshots and/or clones based on the presence 3519 * of '-r' and '-R'. 3520 */ 3521 cb.cb_target = argv[0]; 3522 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 3523 cb.cb_first = B_TRUE; 3524 cb.cb_error = 0; 3525 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0) 3526 goto out; 3527 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0) 3528 goto out; 3529 3530 if ((ret = cb.cb_error) != 0) 3531 goto out; 3532 3533 /* 3534 * Rollback parent to the given snapshot. 3535 */ 3536 ret = zfs_rollback(zhp, snap, force); 3537 3538out: 3539 zfs_close(snap); 3540 zfs_close(zhp); 3541 3542 if (ret == 0) 3543 return (0); 3544 else 3545 return (1); 3546} 3547 3548/* 3549 * zfs set property=value ... { fs | snap | vol } ... 3550 * 3551 * Sets the given properties for all datasets specified on the command line. 3552 */ 3553 3554static int 3555set_callback(zfs_handle_t *zhp, void *data) 3556{ 3557 nvlist_t *props = data; 3558 3559 if (zfs_prop_set_list(zhp, props) != 0) { 3560 switch (libzfs_errno(g_zfs)) { 3561 case EZFS_MOUNTFAILED: 3562 (void) fprintf(stderr, gettext("property may be set " 3563 "but unable to remount filesystem\n")); 3564 break; 3565 case EZFS_SHARENFSFAILED: 3566 (void) fprintf(stderr, gettext("property may be set " 3567 "but unable to reshare filesystem\n")); 3568 break; 3569 } 3570 return (1); 3571 } 3572 return (0); 3573} 3574 3575static int 3576zfs_do_set(int argc, char **argv) 3577{ 3578 nvlist_t *props = NULL; 3579 int ds_start = -1; /* argv idx of first dataset arg */ 3580 int ret = 0; 3581 3582 /* check for options */ 3583 if (argc > 1 && argv[1][0] == '-') { 3584 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3585 argv[1][1]); 3586 usage(B_FALSE); 3587 } 3588 3589 /* check number of arguments */ 3590 if (argc < 2) { 3591 (void) fprintf(stderr, gettext("missing arguments\n")); 3592 usage(B_FALSE); 3593 } 3594 if (argc < 3) { 3595 if (strchr(argv[1], '=') == NULL) { 3596 (void) fprintf(stderr, gettext("missing property=value " 3597 "argument(s)\n")); 3598 } else { 3599 (void) fprintf(stderr, gettext("missing dataset " 3600 "name(s)\n")); 3601 } 3602 usage(B_FALSE); 3603 } 3604 3605 /* validate argument order: prop=val args followed by dataset args */ 3606 for (int i = 1; i < argc; i++) { 3607 if (strchr(argv[i], '=') != NULL) { 3608 if (ds_start > 0) { 3609 /* out-of-order prop=val argument */ 3610 (void) fprintf(stderr, gettext("invalid " 3611 "argument order\n"), i); 3612 usage(B_FALSE); 3613 } 3614 } else if (ds_start < 0) { 3615 ds_start = i; 3616 } 3617 } 3618 if (ds_start < 0) { 3619 (void) fprintf(stderr, gettext("missing dataset name(s)\n")); 3620 usage(B_FALSE); 3621 } 3622 3623 /* Populate a list of property settings */ 3624 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 3625 nomem(); 3626 for (int i = 1; i < ds_start; i++) { 3627 if ((ret = parseprop(props, argv[i])) != 0) 3628 goto error; 3629 } 3630 3631 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0, 3632 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props); 3633 3634error: 3635 nvlist_free(props); 3636 return (ret); 3637} 3638 3639typedef struct snap_cbdata { 3640 nvlist_t *sd_nvl; 3641 boolean_t sd_recursive; 3642 const char *sd_snapname; 3643} snap_cbdata_t; 3644 3645static int 3646zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 3647{ 3648 snap_cbdata_t *sd = arg; 3649 char *name; 3650 int rv = 0; 3651 int error; 3652 3653 if (sd->sd_recursive && 3654 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) { 3655 zfs_close(zhp); 3656 return (0); 3657 } 3658 3659 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname); 3660 if (error == -1) 3661 nomem(); 3662 fnvlist_add_boolean(sd->sd_nvl, name); 3663 free(name); 3664 3665 if (sd->sd_recursive) 3666 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); 3667 zfs_close(zhp); 3668 return (rv); 3669} 3670 3671/* 3672 * zfs snapshot [-r] [-o prop=value] ... <fs@snap> 3673 * 3674 * Creates a snapshot with the given name. While functionally equivalent to 3675 * 'zfs create', it is a separate command to differentiate intent. 3676 */ 3677static int 3678zfs_do_snapshot(int argc, char **argv) 3679{ 3680 int ret = 0; 3681 int c; 3682 nvlist_t *props; 3683 snap_cbdata_t sd = { 0 }; 3684 boolean_t multiple_snaps = B_FALSE; 3685 3686 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 3687 nomem(); 3688 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0) 3689 nomem(); 3690 3691 /* check options */ 3692 while ((c = getopt(argc, argv, "ro:")) != -1) { 3693 switch (c) { 3694 case 'o': 3695 if (parseprop(props, optarg) != 0) 3696 return (1); 3697 break; 3698 case 'r': 3699 sd.sd_recursive = B_TRUE; 3700 multiple_snaps = B_TRUE; 3701 break; 3702 case '?': 3703 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3704 optopt); 3705 goto usage; 3706 } 3707 } 3708 3709 argc -= optind; 3710 argv += optind; 3711 3712 /* check number of arguments */ 3713 if (argc < 1) { 3714 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 3715 goto usage; 3716 } 3717 3718 if (argc > 1) 3719 multiple_snaps = B_TRUE; 3720 for (; argc > 0; argc--, argv++) { 3721 char *atp; 3722 zfs_handle_t *zhp; 3723 3724 atp = strchr(argv[0], '@'); 3725 if (atp == NULL) 3726 goto usage; 3727 *atp = '\0'; 3728 sd.sd_snapname = atp + 1; 3729 zhp = zfs_open(g_zfs, argv[0], 3730 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3731 if (zhp == NULL) 3732 goto usage; 3733 if (zfs_snapshot_cb(zhp, &sd) != 0) 3734 goto usage; 3735 } 3736 3737 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props); 3738 nvlist_free(sd.sd_nvl); 3739 nvlist_free(props); 3740 if (ret != 0 && multiple_snaps) 3741 (void) fprintf(stderr, gettext("no snapshots were created\n")); 3742 return (ret != 0); 3743 3744usage: 3745 nvlist_free(sd.sd_nvl); 3746 nvlist_free(props); 3747 usage(B_FALSE); 3748 return (-1); 3749} 3750 3751/* 3752 * Send a backup stream to stdout. 3753 */ 3754static int 3755zfs_do_send(int argc, char **argv) 3756{ 3757 char *fromname = NULL; 3758 char *toname = NULL; 3759 char *resume_token = NULL; 3760 char *cp; 3761 zfs_handle_t *zhp; 3762 sendflags_t flags = { 0 }; 3763 int c, err; 3764 nvlist_t *dbgnv = NULL; 3765 boolean_t extraverbose = B_FALSE; 3766 3767 /* check options */ 3768 while ((c = getopt(argc, argv, ":i:I:RDpvnPLet:")) != -1) { 3769 switch (c) { 3770 case 'i': 3771 if (fromname) 3772 usage(B_FALSE); 3773 fromname = optarg; 3774 break; 3775 case 'I': 3776 if (fromname) 3777 usage(B_FALSE); 3778 fromname = optarg; 3779 flags.doall = B_TRUE; 3780 break; 3781 case 'R': 3782 flags.replicate = B_TRUE; 3783 break; 3784 case 'p': 3785 flags.props = B_TRUE; 3786 break; 3787 case 'P': 3788 flags.parsable = B_TRUE; 3789 flags.verbose = B_TRUE; 3790 break; 3791 case 'v': 3792 if (flags.verbose) 3793 extraverbose = B_TRUE; 3794 flags.verbose = B_TRUE; 3795 flags.progress = B_TRUE; 3796 break; 3797 case 'D': 3798 flags.dedup = B_TRUE; 3799 break; 3800 case 'n': 3801 flags.dryrun = B_TRUE; 3802 break; 3803 case 'L': 3804 flags.largeblock = B_TRUE; 3805 break; 3806 case 'e': 3807 flags.embed_data = B_TRUE; 3808 break; 3809 case 't': 3810 resume_token = optarg; 3811 break; 3812 case ':': 3813 (void) fprintf(stderr, gettext("missing argument for " 3814 "'%c' option\n"), optopt); 3815 usage(B_FALSE); 3816 break; 3817 case '?': 3818 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3819 optopt); 3820 usage(B_FALSE); 3821 } 3822 } 3823 3824 argc -= optind; 3825 argv += optind; 3826 3827 if (resume_token != NULL) { 3828 if (fromname != NULL || flags.replicate || flags.props || 3829 flags.dedup) { 3830 (void) fprintf(stderr, 3831 gettext("invalid flags combined with -t\n")); 3832 usage(B_FALSE); 3833 } 3834 if (argc != 0) { 3835 (void) fprintf(stderr, gettext("no additional " 3836 "arguments are permitted with -t\n")); 3837 usage(B_FALSE); 3838 } 3839 } else { 3840 if (argc < 1) { 3841 (void) fprintf(stderr, 3842 gettext("missing snapshot argument\n")); 3843 usage(B_FALSE); 3844 } 3845 if (argc > 1) { 3846 (void) fprintf(stderr, gettext("too many arguments\n")); 3847 usage(B_FALSE); 3848 } 3849 } 3850 3851 if (!flags.dryrun && isatty(STDOUT_FILENO)) { 3852 (void) fprintf(stderr, 3853 gettext("Error: Stream can not be written to a terminal.\n" 3854 "You must redirect standard output.\n")); 3855 return (1); 3856 } 3857 3858 if (resume_token != NULL) { 3859 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO, 3860 resume_token)); 3861 } 3862 3863 /* 3864 * Special case sending a filesystem, or from a bookmark. 3865 */ 3866 if (strchr(argv[0], '@') == NULL || 3867 (fromname && strchr(fromname, '#') != NULL)) { 3868 char frombuf[ZFS_MAXNAMELEN]; 3869 enum lzc_send_flags lzc_flags = 0; 3870 3871 if (flags.replicate || flags.doall || flags.props || 3872 flags.dedup || flags.dryrun || flags.verbose || 3873 flags.progress) { 3874 (void) fprintf(stderr, 3875 gettext("Error: " 3876 "Unsupported flag with filesystem or bookmark.\n")); 3877 return (1); 3878 } 3879 3880 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET); 3881 if (zhp == NULL) 3882 return (1); 3883 3884 if (flags.largeblock) 3885 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK; 3886 if (flags.embed_data) 3887 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA; 3888 3889 if (fromname != NULL && 3890 (fromname[0] == '#' || fromname[0] == '@')) { 3891 /* 3892 * Incremental source name begins with # or @. 3893 * Default to same fs as target. 3894 */ 3895 (void) strncpy(frombuf, argv[0], sizeof (frombuf)); 3896 cp = strchr(frombuf, '@'); 3897 if (cp != NULL) 3898 *cp = '\0'; 3899 (void) strlcat(frombuf, fromname, sizeof (frombuf)); 3900 fromname = frombuf; 3901 } 3902 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags); 3903 zfs_close(zhp); 3904 return (err != 0); 3905 } 3906 3907 cp = strchr(argv[0], '@'); 3908 *cp = '\0'; 3909 toname = cp + 1; 3910 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3911 if (zhp == NULL) 3912 return (1); 3913 3914 /* 3915 * If they specified the full path to the snapshot, chop off 3916 * everything except the short name of the snapshot, but special 3917 * case if they specify the origin. 3918 */ 3919 if (fromname && (cp = strchr(fromname, '@')) != NULL) { 3920 char origin[ZFS_MAXNAMELEN]; 3921 zprop_source_t src; 3922 3923 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN, 3924 origin, sizeof (origin), &src, NULL, 0, B_FALSE); 3925 3926 if (strcmp(origin, fromname) == 0) { 3927 fromname = NULL; 3928 flags.fromorigin = B_TRUE; 3929 } else { 3930 *cp = '\0'; 3931 if (cp != fromname && strcmp(argv[0], fromname)) { 3932 (void) fprintf(stderr, 3933 gettext("incremental source must be " 3934 "in same filesystem\n")); 3935 usage(B_FALSE); 3936 } 3937 fromname = cp + 1; 3938 if (strchr(fromname, '@') || strchr(fromname, '/')) { 3939 (void) fprintf(stderr, 3940 gettext("invalid incremental source\n")); 3941 usage(B_FALSE); 3942 } 3943 } 3944 } 3945 3946 if (flags.replicate && fromname == NULL) 3947 flags.doall = B_TRUE; 3948 3949 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0, 3950 extraverbose ? &dbgnv : NULL); 3951 3952 if (extraverbose && dbgnv != NULL) { 3953 /* 3954 * dump_nvlist prints to stdout, but that's been 3955 * redirected to a file. Make it print to stderr 3956 * instead. 3957 */ 3958 (void) dup2(STDERR_FILENO, STDOUT_FILENO); 3959 dump_nvlist(dbgnv, 0); 3960 nvlist_free(dbgnv); 3961 } 3962 zfs_close(zhp); 3963 3964 return (err != 0); 3965} 3966 3967/* 3968 * Restore a backup stream from stdin. 3969 */ 3970static int 3971zfs_do_receive(int argc, char **argv) 3972{ 3973 int c, err; 3974 recvflags_t flags = { 0 }; 3975 boolean_t abort_resumable = B_FALSE; 3976 3977 nvlist_t *props; 3978 nvpair_t *nvp = NULL; 3979 3980 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 3981 nomem(); 3982 3983 /* check options */ 3984 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) { 3985 switch (c) { 3986 case 'o': 3987 if (parseprop(props, optarg) != 0) 3988 return (1); 3989 break; 3990 case 'd': 3991 flags.isprefix = B_TRUE; 3992 break; 3993 case 'e': 3994 flags.isprefix = B_TRUE; 3995 flags.istail = B_TRUE; 3996 break; 3997 case 'n': 3998 flags.dryrun = B_TRUE; 3999 break; 4000 case 'u': 4001 flags.nomount = B_TRUE; 4002 break; 4003 case 'v': 4004 flags.verbose = B_TRUE; 4005 break; 4006 case 's': 4007 flags.resumable = B_TRUE; 4008 break; 4009 case 'F': 4010 flags.force = B_TRUE; 4011 break; 4012 case 'A': 4013 abort_resumable = B_TRUE; 4014 break; 4015 case ':': 4016 (void) fprintf(stderr, gettext("missing argument for " 4017 "'%c' option\n"), optopt); 4018 usage(B_FALSE); 4019 break; 4020 case '?': 4021 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4022 optopt); 4023 usage(B_FALSE); 4024 } 4025 } 4026 4027 argc -= optind; 4028 argv += optind; 4029 4030 /* check number of arguments */ 4031 if (argc < 1) { 4032 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 4033 usage(B_FALSE); 4034 } 4035 if (argc > 1) { 4036 (void) fprintf(stderr, gettext("too many arguments\n")); 4037 usage(B_FALSE); 4038 } 4039 4040 while ((nvp = nvlist_next_nvpair(props, nvp))) { 4041 if (strcmp(nvpair_name(nvp), "origin") != 0) { 4042 (void) fprintf(stderr, gettext("invalid option")); 4043 usage(B_FALSE); 4044 } 4045 } 4046 4047 if (abort_resumable) { 4048 if (flags.isprefix || flags.istail || flags.dryrun || 4049 flags.resumable || flags.nomount) { 4050 (void) fprintf(stderr, gettext("invalid option")); 4051 usage(B_FALSE); 4052 } 4053 4054 char namebuf[ZFS_MAXNAMELEN]; 4055 (void) snprintf(namebuf, sizeof (namebuf), 4056 "%s/%%recv", argv[0]); 4057 4058 if (zfs_dataset_exists(g_zfs, namebuf, 4059 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) { 4060 zfs_handle_t *zhp = zfs_open(g_zfs, 4061 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 4062 if (zhp == NULL) 4063 return (1); 4064 err = zfs_destroy(zhp, B_FALSE); 4065 } else { 4066 zfs_handle_t *zhp = zfs_open(g_zfs, 4067 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 4068 if (zhp == NULL) 4069 usage(B_FALSE); 4070 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) || 4071 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 4072 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) { 4073 (void) fprintf(stderr, 4074 gettext("'%s' does not have any " 4075 "resumable receive state to abort\n"), 4076 argv[0]); 4077 return (1); 4078 } 4079 err = zfs_destroy(zhp, B_FALSE); 4080 } 4081 4082 return (err != 0); 4083 } 4084 4085 if (isatty(STDIN_FILENO)) { 4086 (void) fprintf(stderr, 4087 gettext("Error: Backup stream can not be read " 4088 "from a terminal.\n" 4089 "You must redirect standard input.\n")); 4090 return (1); 4091 } 4092 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL); 4093 4094 return (err != 0); 4095} 4096 4097/* 4098 * allow/unallow stuff 4099 */ 4100/* copied from zfs/sys/dsl_deleg.h */ 4101#define ZFS_DELEG_PERM_CREATE "create" 4102#define ZFS_DELEG_PERM_DESTROY "destroy" 4103#define ZFS_DELEG_PERM_SNAPSHOT "snapshot" 4104#define ZFS_DELEG_PERM_ROLLBACK "rollback" 4105#define ZFS_DELEG_PERM_CLONE "clone" 4106#define ZFS_DELEG_PERM_PROMOTE "promote" 4107#define ZFS_DELEG_PERM_RENAME "rename" 4108#define ZFS_DELEG_PERM_MOUNT "mount" 4109#define ZFS_DELEG_PERM_SHARE "share" 4110#define ZFS_DELEG_PERM_SEND "send" 4111#define ZFS_DELEG_PERM_RECEIVE "receive" 4112#define ZFS_DELEG_PERM_ALLOW "allow" 4113#define ZFS_DELEG_PERM_USERPROP "userprop" 4114#define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */ 4115#define ZFS_DELEG_PERM_USERQUOTA "userquota" 4116#define ZFS_DELEG_PERM_GROUPQUOTA "groupquota" 4117#define ZFS_DELEG_PERM_USERUSED "userused" 4118#define ZFS_DELEG_PERM_GROUPUSED "groupused" 4119#define ZFS_DELEG_PERM_HOLD "hold" 4120#define ZFS_DELEG_PERM_RELEASE "release" 4121#define ZFS_DELEG_PERM_DIFF "diff" 4122#define ZFS_DELEG_PERM_BOOKMARK "bookmark" 4123 4124#define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE 4125 4126static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = { 4127 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW }, 4128 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE }, 4129 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE }, 4130 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY }, 4131 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF}, 4132 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD }, 4133 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT }, 4134 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE }, 4135 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE }, 4136 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE }, 4137 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME }, 4138 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK }, 4139 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND }, 4140 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE }, 4141 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT }, 4142 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK }, 4143 4144 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA }, 4145 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED }, 4146 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP }, 4147 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA }, 4148 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED }, 4149 { NULL, ZFS_DELEG_NOTE_NONE } 4150}; 4151 4152/* permission structure */ 4153typedef struct deleg_perm { 4154 zfs_deleg_who_type_t dp_who_type; 4155 const char *dp_name; 4156 boolean_t dp_local; 4157 boolean_t dp_descend; 4158} deleg_perm_t; 4159 4160/* */ 4161typedef struct deleg_perm_node { 4162 deleg_perm_t dpn_perm; 4163 4164 uu_avl_node_t dpn_avl_node; 4165} deleg_perm_node_t; 4166 4167typedef struct fs_perm fs_perm_t; 4168 4169/* permissions set */ 4170typedef struct who_perm { 4171 zfs_deleg_who_type_t who_type; 4172 const char *who_name; /* id */ 4173 char who_ug_name[256]; /* user/group name */ 4174 fs_perm_t *who_fsperm; /* uplink */ 4175 4176 uu_avl_t *who_deleg_perm_avl; /* permissions */ 4177} who_perm_t; 4178 4179/* */ 4180typedef struct who_perm_node { 4181 who_perm_t who_perm; 4182 uu_avl_node_t who_avl_node; 4183} who_perm_node_t; 4184 4185typedef struct fs_perm_set fs_perm_set_t; 4186/* fs permissions */ 4187struct fs_perm { 4188 const char *fsp_name; 4189 4190 uu_avl_t *fsp_sc_avl; /* sets,create */ 4191 uu_avl_t *fsp_uge_avl; /* user,group,everyone */ 4192 4193 fs_perm_set_t *fsp_set; /* uplink */ 4194}; 4195 4196/* */ 4197typedef struct fs_perm_node { 4198 fs_perm_t fspn_fsperm; 4199 uu_avl_t *fspn_avl; 4200 4201 uu_list_node_t fspn_list_node; 4202} fs_perm_node_t; 4203 4204/* top level structure */ 4205struct fs_perm_set { 4206 uu_list_pool_t *fsps_list_pool; 4207 uu_list_t *fsps_list; /* list of fs_perms */ 4208 4209 uu_avl_pool_t *fsps_named_set_avl_pool; 4210 uu_avl_pool_t *fsps_who_perm_avl_pool; 4211 uu_avl_pool_t *fsps_deleg_perm_avl_pool; 4212}; 4213 4214static inline const char * 4215deleg_perm_type(zfs_deleg_note_t note) 4216{ 4217 /* subcommands */ 4218 switch (note) { 4219 /* SUBCOMMANDS */ 4220 /* OTHER */ 4221 case ZFS_DELEG_NOTE_GROUPQUOTA: 4222 case ZFS_DELEG_NOTE_GROUPUSED: 4223 case ZFS_DELEG_NOTE_USERPROP: 4224 case ZFS_DELEG_NOTE_USERQUOTA: 4225 case ZFS_DELEG_NOTE_USERUSED: 4226 /* other */ 4227 return (gettext("other")); 4228 default: 4229 return (gettext("subcommand")); 4230 } 4231} 4232 4233static int inline 4234who_type2weight(zfs_deleg_who_type_t who_type) 4235{ 4236 int res; 4237 switch (who_type) { 4238 case ZFS_DELEG_NAMED_SET_SETS: 4239 case ZFS_DELEG_NAMED_SET: 4240 res = 0; 4241 break; 4242 case ZFS_DELEG_CREATE_SETS: 4243 case ZFS_DELEG_CREATE: 4244 res = 1; 4245 break; 4246 case ZFS_DELEG_USER_SETS: 4247 case ZFS_DELEG_USER: 4248 res = 2; 4249 break; 4250 case ZFS_DELEG_GROUP_SETS: 4251 case ZFS_DELEG_GROUP: 4252 res = 3; 4253 break; 4254 case ZFS_DELEG_EVERYONE_SETS: 4255 case ZFS_DELEG_EVERYONE: 4256 res = 4; 4257 break; 4258 default: 4259 res = -1; 4260 } 4261 4262 return (res); 4263} 4264 4265/* ARGSUSED */ 4266static int 4267who_perm_compare(const void *larg, const void *rarg, void *unused) 4268{ 4269 const who_perm_node_t *l = larg; 4270 const who_perm_node_t *r = rarg; 4271 zfs_deleg_who_type_t ltype = l->who_perm.who_type; 4272 zfs_deleg_who_type_t rtype = r->who_perm.who_type; 4273 int lweight = who_type2weight(ltype); 4274 int rweight = who_type2weight(rtype); 4275 int res = lweight - rweight; 4276 if (res == 0) 4277 res = strncmp(l->who_perm.who_name, r->who_perm.who_name, 4278 ZFS_MAX_DELEG_NAME-1); 4279 4280 if (res == 0) 4281 return (0); 4282 if (res > 0) 4283 return (1); 4284 else 4285 return (-1); 4286} 4287 4288/* ARGSUSED */ 4289static int 4290deleg_perm_compare(const void *larg, const void *rarg, void *unused) 4291{ 4292 const deleg_perm_node_t *l = larg; 4293 const deleg_perm_node_t *r = rarg; 4294 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name, 4295 ZFS_MAX_DELEG_NAME-1); 4296 4297 if (res == 0) 4298 return (0); 4299 4300 if (res > 0) 4301 return (1); 4302 else 4303 return (-1); 4304} 4305 4306static inline void 4307fs_perm_set_init(fs_perm_set_t *fspset) 4308{ 4309 bzero(fspset, sizeof (fs_perm_set_t)); 4310 4311 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool", 4312 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node), 4313 NULL, UU_DEFAULT)) == NULL) 4314 nomem(); 4315 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL, 4316 UU_DEFAULT)) == NULL) 4317 nomem(); 4318 4319 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create( 4320 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof( 4321 who_perm_node_t, who_avl_node), who_perm_compare, 4322 UU_DEFAULT)) == NULL) 4323 nomem(); 4324 4325 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create( 4326 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof( 4327 who_perm_node_t, who_avl_node), who_perm_compare, 4328 UU_DEFAULT)) == NULL) 4329 nomem(); 4330 4331 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create( 4332 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof( 4333 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT)) 4334 == NULL) 4335 nomem(); 4336} 4337 4338static inline void fs_perm_fini(fs_perm_t *); 4339static inline void who_perm_fini(who_perm_t *); 4340 4341static inline void 4342fs_perm_set_fini(fs_perm_set_t *fspset) 4343{ 4344 fs_perm_node_t *node = uu_list_first(fspset->fsps_list); 4345 4346 while (node != NULL) { 4347 fs_perm_node_t *next_node = 4348 uu_list_next(fspset->fsps_list, node); 4349 fs_perm_t *fsperm = &node->fspn_fsperm; 4350 fs_perm_fini(fsperm); 4351 uu_list_remove(fspset->fsps_list, node); 4352 free(node); 4353 node = next_node; 4354 } 4355 4356 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool); 4357 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool); 4358 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool); 4359} 4360 4361static inline void 4362deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type, 4363 const char *name) 4364{ 4365 deleg_perm->dp_who_type = type; 4366 deleg_perm->dp_name = name; 4367} 4368 4369static inline void 4370who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm, 4371 zfs_deleg_who_type_t type, const char *name) 4372{ 4373 uu_avl_pool_t *pool; 4374 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool; 4375 4376 bzero(who_perm, sizeof (who_perm_t)); 4377 4378 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL, 4379 UU_DEFAULT)) == NULL) 4380 nomem(); 4381 4382 who_perm->who_type = type; 4383 who_perm->who_name = name; 4384 who_perm->who_fsperm = fsperm; 4385} 4386 4387static inline void 4388who_perm_fini(who_perm_t *who_perm) 4389{ 4390 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl); 4391 4392 while (node != NULL) { 4393 deleg_perm_node_t *next_node = 4394 uu_avl_next(who_perm->who_deleg_perm_avl, node); 4395 4396 uu_avl_remove(who_perm->who_deleg_perm_avl, node); 4397 free(node); 4398 node = next_node; 4399 } 4400 4401 uu_avl_destroy(who_perm->who_deleg_perm_avl); 4402} 4403 4404static inline void 4405fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname) 4406{ 4407 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool; 4408 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool; 4409 4410 bzero(fsperm, sizeof (fs_perm_t)); 4411 4412 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT)) 4413 == NULL) 4414 nomem(); 4415 4416 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT)) 4417 == NULL) 4418 nomem(); 4419 4420 fsperm->fsp_set = fspset; 4421 fsperm->fsp_name = fsname; 4422} 4423 4424static inline void 4425fs_perm_fini(fs_perm_t *fsperm) 4426{ 4427 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl); 4428 while (node != NULL) { 4429 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl, 4430 node); 4431 who_perm_t *who_perm = &node->who_perm; 4432 who_perm_fini(who_perm); 4433 uu_avl_remove(fsperm->fsp_sc_avl, node); 4434 free(node); 4435 node = next_node; 4436 } 4437 4438 node = uu_avl_first(fsperm->fsp_uge_avl); 4439 while (node != NULL) { 4440 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl, 4441 node); 4442 who_perm_t *who_perm = &node->who_perm; 4443 who_perm_fini(who_perm); 4444 uu_avl_remove(fsperm->fsp_uge_avl, node); 4445 free(node); 4446 node = next_node; 4447 } 4448 4449 uu_avl_destroy(fsperm->fsp_sc_avl); 4450 uu_avl_destroy(fsperm->fsp_uge_avl); 4451} 4452 4453static void inline 4454set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node, 4455 zfs_deleg_who_type_t who_type, const char *name, char locality) 4456{ 4457 uu_avl_index_t idx = 0; 4458 4459 deleg_perm_node_t *found_node = NULL; 4460 deleg_perm_t *deleg_perm = &node->dpn_perm; 4461 4462 deleg_perm_init(deleg_perm, who_type, name); 4463 4464 if ((found_node = uu_avl_find(avl, node, NULL, &idx)) 4465 == NULL) 4466 uu_avl_insert(avl, node, idx); 4467 else { 4468 node = found_node; 4469 deleg_perm = &node->dpn_perm; 4470 } 4471 4472 4473 switch (locality) { 4474 case ZFS_DELEG_LOCAL: 4475 deleg_perm->dp_local = B_TRUE; 4476 break; 4477 case ZFS_DELEG_DESCENDENT: 4478 deleg_perm->dp_descend = B_TRUE; 4479 break; 4480 case ZFS_DELEG_NA: 4481 break; 4482 default: 4483 assert(B_FALSE); /* invalid locality */ 4484 } 4485} 4486 4487static inline int 4488parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality) 4489{ 4490 nvpair_t *nvp = NULL; 4491 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set; 4492 uu_avl_t *avl = who_perm->who_deleg_perm_avl; 4493 zfs_deleg_who_type_t who_type = who_perm->who_type; 4494 4495 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4496 const char *name = nvpair_name(nvp); 4497 data_type_t type = nvpair_type(nvp); 4498 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool; 4499 deleg_perm_node_t *node = 4500 safe_malloc(sizeof (deleg_perm_node_t)); 4501 4502 assert(type == DATA_TYPE_BOOLEAN); 4503 4504 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool); 4505 set_deleg_perm_node(avl, node, who_type, name, locality); 4506 } 4507 4508 return (0); 4509} 4510 4511static inline int 4512parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl) 4513{ 4514 nvpair_t *nvp = NULL; 4515 fs_perm_set_t *fspset = fsperm->fsp_set; 4516 4517 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4518 nvlist_t *nvl2 = NULL; 4519 const char *name = nvpair_name(nvp); 4520 uu_avl_t *avl = NULL; 4521 uu_avl_pool_t *avl_pool; 4522 zfs_deleg_who_type_t perm_type = name[0]; 4523 char perm_locality = name[1]; 4524 const char *perm_name = name + 3; 4525 boolean_t is_set = B_TRUE; 4526 who_perm_t *who_perm = NULL; 4527 4528 assert('$' == name[2]); 4529 4530 if (nvpair_value_nvlist(nvp, &nvl2) != 0) 4531 return (-1); 4532 4533 switch (perm_type) { 4534 case ZFS_DELEG_CREATE: 4535 case ZFS_DELEG_CREATE_SETS: 4536 case ZFS_DELEG_NAMED_SET: 4537 case ZFS_DELEG_NAMED_SET_SETS: 4538 avl_pool = fspset->fsps_named_set_avl_pool; 4539 avl = fsperm->fsp_sc_avl; 4540 break; 4541 case ZFS_DELEG_USER: 4542 case ZFS_DELEG_USER_SETS: 4543 case ZFS_DELEG_GROUP: 4544 case ZFS_DELEG_GROUP_SETS: 4545 case ZFS_DELEG_EVERYONE: 4546 case ZFS_DELEG_EVERYONE_SETS: 4547 avl_pool = fspset->fsps_who_perm_avl_pool; 4548 avl = fsperm->fsp_uge_avl; 4549 break; 4550 } 4551 4552 if (is_set) { 4553 who_perm_node_t *found_node = NULL; 4554 who_perm_node_t *node = safe_malloc( 4555 sizeof (who_perm_node_t)); 4556 who_perm = &node->who_perm; 4557 uu_avl_index_t idx = 0; 4558 4559 uu_avl_node_init(node, &node->who_avl_node, avl_pool); 4560 who_perm_init(who_perm, fsperm, perm_type, perm_name); 4561 4562 if ((found_node = uu_avl_find(avl, node, NULL, &idx)) 4563 == NULL) { 4564 if (avl == fsperm->fsp_uge_avl) { 4565 uid_t rid = 0; 4566 struct passwd *p = NULL; 4567 struct group *g = NULL; 4568 const char *nice_name = NULL; 4569 4570 switch (perm_type) { 4571 case ZFS_DELEG_USER_SETS: 4572 case ZFS_DELEG_USER: 4573 rid = atoi(perm_name); 4574 p = getpwuid(rid); 4575 if (p) 4576 nice_name = p->pw_name; 4577 break; 4578 case ZFS_DELEG_GROUP_SETS: 4579 case ZFS_DELEG_GROUP: 4580 rid = atoi(perm_name); 4581 g = getgrgid(rid); 4582 if (g) 4583 nice_name = g->gr_name; 4584 break; 4585 } 4586 4587 if (nice_name != NULL) 4588 (void) strlcpy( 4589 node->who_perm.who_ug_name, 4590 nice_name, 256); 4591 } 4592 4593 uu_avl_insert(avl, node, idx); 4594 } else { 4595 node = found_node; 4596 who_perm = &node->who_perm; 4597 } 4598 } 4599 4600 (void) parse_who_perm(who_perm, nvl2, perm_locality); 4601 } 4602 4603 return (0); 4604} 4605 4606static inline int 4607parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl) 4608{ 4609 nvpair_t *nvp = NULL; 4610 uu_avl_index_t idx = 0; 4611 4612 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4613 nvlist_t *nvl2 = NULL; 4614 const char *fsname = nvpair_name(nvp); 4615 data_type_t type = nvpair_type(nvp); 4616 fs_perm_t *fsperm = NULL; 4617 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t)); 4618 if (node == NULL) 4619 nomem(); 4620 4621 fsperm = &node->fspn_fsperm; 4622 4623 assert(DATA_TYPE_NVLIST == type); 4624 4625 uu_list_node_init(node, &node->fspn_list_node, 4626 fspset->fsps_list_pool); 4627 4628 idx = uu_list_numnodes(fspset->fsps_list); 4629 fs_perm_init(fsperm, fspset, fsname); 4630 4631 if (nvpair_value_nvlist(nvp, &nvl2) != 0) 4632 return (-1); 4633 4634 (void) parse_fs_perm(fsperm, nvl2); 4635 4636 uu_list_insert(fspset->fsps_list, node, idx); 4637 } 4638 4639 return (0); 4640} 4641 4642static inline const char * 4643deleg_perm_comment(zfs_deleg_note_t note) 4644{ 4645 const char *str = ""; 4646 4647 /* subcommands */ 4648 switch (note) { 4649 /* SUBCOMMANDS */ 4650 case ZFS_DELEG_NOTE_ALLOW: 4651 str = gettext("Must also have the permission that is being" 4652 "\n\t\t\t\tallowed"); 4653 break; 4654 case ZFS_DELEG_NOTE_CLONE: 4655 str = gettext("Must also have the 'create' ability and 'mount'" 4656 "\n\t\t\t\tability in the origin file system"); 4657 break; 4658 case ZFS_DELEG_NOTE_CREATE: 4659 str = gettext("Must also have the 'mount' ability"); 4660 break; 4661 case ZFS_DELEG_NOTE_DESTROY: 4662 str = gettext("Must also have the 'mount' ability"); 4663 break; 4664 case ZFS_DELEG_NOTE_DIFF: 4665 str = gettext("Allows lookup of paths within a dataset;" 4666 "\n\t\t\t\tgiven an object number. Ordinary users need this" 4667 "\n\t\t\t\tin order to use zfs diff"); 4668 break; 4669 case ZFS_DELEG_NOTE_HOLD: 4670 str = gettext("Allows adding a user hold to a snapshot"); 4671 break; 4672 case ZFS_DELEG_NOTE_MOUNT: 4673 str = gettext("Allows mount/umount of ZFS datasets"); 4674 break; 4675 case ZFS_DELEG_NOTE_PROMOTE: 4676 str = gettext("Must also have the 'mount'\n\t\t\t\tand" 4677 " 'promote' ability in the origin file system"); 4678 break; 4679 case ZFS_DELEG_NOTE_RECEIVE: 4680 str = gettext("Must also have the 'mount' and 'create'" 4681 " ability"); 4682 break; 4683 case ZFS_DELEG_NOTE_RELEASE: 4684 str = gettext("Allows releasing a user hold which\n\t\t\t\t" 4685 "might destroy the snapshot"); 4686 break; 4687 case ZFS_DELEG_NOTE_RENAME: 4688 str = gettext("Must also have the 'mount' and 'create'" 4689 "\n\t\t\t\tability in the new parent"); 4690 break; 4691 case ZFS_DELEG_NOTE_ROLLBACK: 4692 str = gettext(""); 4693 break; 4694 case ZFS_DELEG_NOTE_SEND: 4695 str = gettext(""); 4696 break; 4697 case ZFS_DELEG_NOTE_SHARE: 4698 str = gettext("Allows sharing file systems over NFS or SMB" 4699 "\n\t\t\t\tprotocols"); 4700 break; 4701 case ZFS_DELEG_NOTE_SNAPSHOT: 4702 str = gettext(""); 4703 break; 4704/* 4705 * case ZFS_DELEG_NOTE_VSCAN: 4706 * str = gettext(""); 4707 * break; 4708 */ 4709 /* OTHER */ 4710 case ZFS_DELEG_NOTE_GROUPQUOTA: 4711 str = gettext("Allows accessing any groupquota@... property"); 4712 break; 4713 case ZFS_DELEG_NOTE_GROUPUSED: 4714 str = gettext("Allows reading any groupused@... property"); 4715 break; 4716 case ZFS_DELEG_NOTE_USERPROP: 4717 str = gettext("Allows changing any user property"); 4718 break; 4719 case ZFS_DELEG_NOTE_USERQUOTA: 4720 str = gettext("Allows accessing any userquota@... property"); 4721 break; 4722 case ZFS_DELEG_NOTE_USERUSED: 4723 str = gettext("Allows reading any userused@... property"); 4724 break; 4725 /* other */ 4726 default: 4727 str = ""; 4728 } 4729 4730 return (str); 4731} 4732 4733struct allow_opts { 4734 boolean_t local; 4735 boolean_t descend; 4736 boolean_t user; 4737 boolean_t group; 4738 boolean_t everyone; 4739 boolean_t create; 4740 boolean_t set; 4741 boolean_t recursive; /* unallow only */ 4742 boolean_t prt_usage; 4743 4744 boolean_t prt_perms; 4745 char *who; 4746 char *perms; 4747 const char *dataset; 4748}; 4749 4750static inline int 4751prop_cmp(const void *a, const void *b) 4752{ 4753 const char *str1 = *(const char **)a; 4754 const char *str2 = *(const char **)b; 4755 return (strcmp(str1, str2)); 4756} 4757 4758static void 4759allow_usage(boolean_t un, boolean_t requested, const char *msg) 4760{ 4761 const char *opt_desc[] = { 4762 "-h", gettext("show this help message and exit"), 4763 "-l", gettext("set permission locally"), 4764 "-d", gettext("set permission for descents"), 4765 "-u", gettext("set permission for user"), 4766 "-g", gettext("set permission for group"), 4767 "-e", gettext("set permission for everyone"), 4768 "-c", gettext("set create time permission"), 4769 "-s", gettext("define permission set"), 4770 /* unallow only */ 4771 "-r", gettext("remove permissions recursively"), 4772 }; 4773 size_t unallow_size = sizeof (opt_desc) / sizeof (char *); 4774 size_t allow_size = unallow_size - 2; 4775 const char *props[ZFS_NUM_PROPS]; 4776 int i; 4777 size_t count = 0; 4778 FILE *fp = requested ? stdout : stderr; 4779 zprop_desc_t *pdtbl = zfs_prop_get_table(); 4780 const char *fmt = gettext("%-16s %-14s\t%s\n"); 4781 4782 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW : 4783 HELP_ALLOW)); 4784 (void) fprintf(fp, gettext("Options:\n")); 4785 for (i = 0; i < (un ? unallow_size : allow_size); i++) { 4786 const char *opt = opt_desc[i++]; 4787 const char *optdsc = opt_desc[i]; 4788 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc); 4789 } 4790 4791 (void) fprintf(fp, gettext("\nThe following permissions are " 4792 "supported:\n\n")); 4793 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"), 4794 gettext("NOTES")); 4795 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) { 4796 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm; 4797 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note; 4798 const char *perm_type = deleg_perm_type(perm_note); 4799 const char *perm_comment = deleg_perm_comment(perm_note); 4800 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment); 4801 } 4802 4803 for (i = 0; i < ZFS_NUM_PROPS; i++) { 4804 zprop_desc_t *pd = &pdtbl[i]; 4805 if (pd->pd_visible != B_TRUE) 4806 continue; 4807 4808 if (pd->pd_attr == PROP_READONLY) 4809 continue; 4810 4811 props[count++] = pd->pd_name; 4812 } 4813 props[count] = NULL; 4814 4815 qsort(props, count, sizeof (char *), prop_cmp); 4816 4817 for (i = 0; i < count; i++) 4818 (void) fprintf(fp, fmt, props[i], gettext("property"), ""); 4819 4820 if (msg != NULL) 4821 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg); 4822 4823 exit(requested ? 0 : 2); 4824} 4825 4826static inline const char * 4827munge_args(int argc, char **argv, boolean_t un, size_t expected_argc, 4828 char **permsp) 4829{ 4830 if (un && argc == expected_argc - 1) 4831 *permsp = NULL; 4832 else if (argc == expected_argc) 4833 *permsp = argv[argc - 2]; 4834 else 4835 allow_usage(un, B_FALSE, 4836 gettext("wrong number of parameters\n")); 4837 4838 return (argv[argc - 1]); 4839} 4840 4841static void 4842parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts) 4843{ 4844 int uge_sum = opts->user + opts->group + opts->everyone; 4845 int csuge_sum = opts->create + opts->set + uge_sum; 4846 int ldcsuge_sum = csuge_sum + opts->local + opts->descend; 4847 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum; 4848 4849 if (uge_sum > 1) 4850 allow_usage(un, B_FALSE, 4851 gettext("-u, -g, and -e are mutually exclusive\n")); 4852 4853 if (opts->prt_usage) 4854 if (argc == 0 && all_sum == 0) 4855 allow_usage(un, B_TRUE, NULL); 4856 else 4857 usage(B_FALSE); 4858 4859 if (opts->set) { 4860 if (csuge_sum > 1) 4861 allow_usage(un, B_FALSE, 4862 gettext("invalid options combined with -s\n")); 4863 4864 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms); 4865 if (argv[0][0] != '@') 4866 allow_usage(un, B_FALSE, 4867 gettext("invalid set name: missing '@' prefix\n")); 4868 opts->who = argv[0]; 4869 } else if (opts->create) { 4870 if (ldcsuge_sum > 1) 4871 allow_usage(un, B_FALSE, 4872 gettext("invalid options combined with -c\n")); 4873 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4874 } else if (opts->everyone) { 4875 if (csuge_sum > 1) 4876 allow_usage(un, B_FALSE, 4877 gettext("invalid options combined with -e\n")); 4878 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4879 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone") 4880 == 0) { 4881 opts->everyone = B_TRUE; 4882 argc--; 4883 argv++; 4884 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4885 } else if (argc == 1 && !un) { 4886 opts->prt_perms = B_TRUE; 4887 opts->dataset = argv[argc-1]; 4888 } else { 4889 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms); 4890 opts->who = argv[0]; 4891 } 4892 4893 if (!opts->local && !opts->descend) { 4894 opts->local = B_TRUE; 4895 opts->descend = B_TRUE; 4896 } 4897} 4898 4899static void 4900store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend, 4901 const char *who, char *perms, nvlist_t *top_nvl) 4902{ 4903 int i; 4904 char ld[2] = { '\0', '\0' }; 4905 char who_buf[ZFS_MAXNAMELEN+32]; 4906 char base_type; 4907 char set_type; 4908 nvlist_t *base_nvl = NULL; 4909 nvlist_t *set_nvl = NULL; 4910 nvlist_t *nvl; 4911 4912 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0) 4913 nomem(); 4914 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0) 4915 nomem(); 4916 4917 switch (type) { 4918 case ZFS_DELEG_NAMED_SET_SETS: 4919 case ZFS_DELEG_NAMED_SET: 4920 set_type = ZFS_DELEG_NAMED_SET_SETS; 4921 base_type = ZFS_DELEG_NAMED_SET; 4922 ld[0] = ZFS_DELEG_NA; 4923 break; 4924 case ZFS_DELEG_CREATE_SETS: 4925 case ZFS_DELEG_CREATE: 4926 set_type = ZFS_DELEG_CREATE_SETS; 4927 base_type = ZFS_DELEG_CREATE; 4928 ld[0] = ZFS_DELEG_NA; 4929 break; 4930 case ZFS_DELEG_USER_SETS: 4931 case ZFS_DELEG_USER: 4932 set_type = ZFS_DELEG_USER_SETS; 4933 base_type = ZFS_DELEG_USER; 4934 if (local) 4935 ld[0] = ZFS_DELEG_LOCAL; 4936 if (descend) 4937 ld[1] = ZFS_DELEG_DESCENDENT; 4938 break; 4939 case ZFS_DELEG_GROUP_SETS: 4940 case ZFS_DELEG_GROUP: 4941 set_type = ZFS_DELEG_GROUP_SETS; 4942 base_type = ZFS_DELEG_GROUP; 4943 if (local) 4944 ld[0] = ZFS_DELEG_LOCAL; 4945 if (descend) 4946 ld[1] = ZFS_DELEG_DESCENDENT; 4947 break; 4948 case ZFS_DELEG_EVERYONE_SETS: 4949 case ZFS_DELEG_EVERYONE: 4950 set_type = ZFS_DELEG_EVERYONE_SETS; 4951 base_type = ZFS_DELEG_EVERYONE; 4952 if (local) 4953 ld[0] = ZFS_DELEG_LOCAL; 4954 if (descend) 4955 ld[1] = ZFS_DELEG_DESCENDENT; 4956 } 4957 4958 if (perms != NULL) { 4959 char *curr = perms; 4960 char *end = curr + strlen(perms); 4961 4962 while (curr < end) { 4963 char *delim = strchr(curr, ','); 4964 if (delim == NULL) 4965 delim = end; 4966 else 4967 *delim = '\0'; 4968 4969 if (curr[0] == '@') 4970 nvl = set_nvl; 4971 else 4972 nvl = base_nvl; 4973 4974 (void) nvlist_add_boolean(nvl, curr); 4975 if (delim != end) 4976 *delim = ','; 4977 curr = delim + 1; 4978 } 4979 4980 for (i = 0; i < 2; i++) { 4981 char locality = ld[i]; 4982 if (locality == 0) 4983 continue; 4984 4985 if (!nvlist_empty(base_nvl)) { 4986 if (who != NULL) 4987 (void) snprintf(who_buf, 4988 sizeof (who_buf), "%c%c$%s", 4989 base_type, locality, who); 4990 else 4991 (void) snprintf(who_buf, 4992 sizeof (who_buf), "%c%c$", 4993 base_type, locality); 4994 4995 (void) nvlist_add_nvlist(top_nvl, who_buf, 4996 base_nvl); 4997 } 4998 4999 5000 if (!nvlist_empty(set_nvl)) { 5001 if (who != NULL) 5002 (void) snprintf(who_buf, 5003 sizeof (who_buf), "%c%c$%s", 5004 set_type, locality, who); 5005 else 5006 (void) snprintf(who_buf, 5007 sizeof (who_buf), "%c%c$", 5008 set_type, locality); 5009 5010 (void) nvlist_add_nvlist(top_nvl, who_buf, 5011 set_nvl); 5012 } 5013 } 5014 } else { 5015 for (i = 0; i < 2; i++) { 5016 char locality = ld[i]; 5017 if (locality == 0) 5018 continue; 5019 5020 if (who != NULL) 5021 (void) snprintf(who_buf, sizeof (who_buf), 5022 "%c%c$%s", base_type, locality, who); 5023 else 5024 (void) snprintf(who_buf, sizeof (who_buf), 5025 "%c%c$", base_type, locality); 5026 (void) nvlist_add_boolean(top_nvl, who_buf); 5027 5028 if (who != NULL) 5029 (void) snprintf(who_buf, sizeof (who_buf), 5030 "%c%c$%s", set_type, locality, who); 5031 else 5032 (void) snprintf(who_buf, sizeof (who_buf), 5033 "%c%c$", set_type, locality); 5034 (void) nvlist_add_boolean(top_nvl, who_buf); 5035 } 5036 } 5037} 5038 5039static int 5040construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp) 5041{ 5042 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0) 5043 nomem(); 5044 5045 if (opts->set) { 5046 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local, 5047 opts->descend, opts->who, opts->perms, *nvlp); 5048 } else if (opts->create) { 5049 store_allow_perm(ZFS_DELEG_CREATE, opts->local, 5050 opts->descend, NULL, opts->perms, *nvlp); 5051 } else if (opts->everyone) { 5052 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local, 5053 opts->descend, NULL, opts->perms, *nvlp); 5054 } else { 5055 char *curr = opts->who; 5056 char *end = curr + strlen(curr); 5057 5058 while (curr < end) { 5059 const char *who; 5060 zfs_deleg_who_type_t who_type; 5061 char *endch; 5062 char *delim = strchr(curr, ','); 5063 char errbuf[256]; 5064 char id[64]; 5065 struct passwd *p = NULL; 5066 struct group *g = NULL; 5067 5068 uid_t rid; 5069 if (delim == NULL) 5070 delim = end; 5071 else 5072 *delim = '\0'; 5073 5074 rid = (uid_t)strtol(curr, &endch, 0); 5075 if (opts->user) { 5076 who_type = ZFS_DELEG_USER; 5077 if (*endch != '\0') 5078 p = getpwnam(curr); 5079 else 5080 p = getpwuid(rid); 5081 5082 if (p != NULL) 5083 rid = p->pw_uid; 5084 else { 5085 (void) snprintf(errbuf, 256, gettext( 5086 "invalid user %s"), curr); 5087 allow_usage(un, B_TRUE, errbuf); 5088 } 5089 } else if (opts->group) { 5090 who_type = ZFS_DELEG_GROUP; 5091 if (*endch != '\0') 5092 g = getgrnam(curr); 5093 else 5094 g = getgrgid(rid); 5095 5096 if (g != NULL) 5097 rid = g->gr_gid; 5098 else { 5099 (void) snprintf(errbuf, 256, gettext( 5100 "invalid group %s"), curr); 5101 allow_usage(un, B_TRUE, errbuf); 5102 } 5103 } else { 5104 if (*endch != '\0') { 5105 p = getpwnam(curr); 5106 } else { 5107 p = getpwuid(rid); 5108 } 5109 5110 if (p == NULL) 5111 if (*endch != '\0') { 5112 g = getgrnam(curr); 5113 } else { 5114 g = getgrgid(rid); 5115 } 5116 5117 if (p != NULL) { 5118 who_type = ZFS_DELEG_USER; 5119 rid = p->pw_uid; 5120 } else if (g != NULL) { 5121 who_type = ZFS_DELEG_GROUP; 5122 rid = g->gr_gid; 5123 } else { 5124 (void) snprintf(errbuf, 256, gettext( 5125 "invalid user/group %s"), curr); 5126 allow_usage(un, B_TRUE, errbuf); 5127 } 5128 } 5129 5130 (void) sprintf(id, "%u", rid); 5131 who = id; 5132 5133 store_allow_perm(who_type, opts->local, 5134 opts->descend, who, opts->perms, *nvlp); 5135 curr = delim + 1; 5136 } 5137 } 5138 5139 return (0); 5140} 5141 5142static void 5143print_set_creat_perms(uu_avl_t *who_avl) 5144{ 5145 const char *sc_title[] = { 5146 gettext("Permission sets:\n"), 5147 gettext("Create time permissions:\n"), 5148 NULL 5149 }; 5150 const char **title_ptr = sc_title; 5151 who_perm_node_t *who_node = NULL; 5152 int prev_weight = -1; 5153 5154 for (who_node = uu_avl_first(who_avl); who_node != NULL; 5155 who_node = uu_avl_next(who_avl, who_node)) { 5156 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl; 5157 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type; 5158 const char *who_name = who_node->who_perm.who_name; 5159 int weight = who_type2weight(who_type); 5160 boolean_t first = B_TRUE; 5161 deleg_perm_node_t *deleg_node; 5162 5163 if (prev_weight != weight) { 5164 (void) printf(*title_ptr++); 5165 prev_weight = weight; 5166 } 5167 5168 if (who_name == NULL || strnlen(who_name, 1) == 0) 5169 (void) printf("\t"); 5170 else 5171 (void) printf("\t%s ", who_name); 5172 5173 for (deleg_node = uu_avl_first(avl); deleg_node != NULL; 5174 deleg_node = uu_avl_next(avl, deleg_node)) { 5175 if (first) { 5176 (void) printf("%s", 5177 deleg_node->dpn_perm.dp_name); 5178 first = B_FALSE; 5179 } else 5180 (void) printf(",%s", 5181 deleg_node->dpn_perm.dp_name); 5182 } 5183 5184 (void) printf("\n"); 5185 } 5186} 5187 5188static void inline 5189print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend, 5190 const char *title) 5191{ 5192 who_perm_node_t *who_node = NULL; 5193 boolean_t prt_title = B_TRUE; 5194 uu_avl_walk_t *walk; 5195 5196 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL) 5197 nomem(); 5198 5199 while ((who_node = uu_avl_walk_next(walk)) != NULL) { 5200 const char *who_name = who_node->who_perm.who_name; 5201 const char *nice_who_name = who_node->who_perm.who_ug_name; 5202 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl; 5203 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type; 5204 char delim = ' '; 5205 deleg_perm_node_t *deleg_node; 5206 boolean_t prt_who = B_TRUE; 5207 5208 for (deleg_node = uu_avl_first(avl); 5209 deleg_node != NULL; 5210 deleg_node = uu_avl_next(avl, deleg_node)) { 5211 if (local != deleg_node->dpn_perm.dp_local || 5212 descend != deleg_node->dpn_perm.dp_descend) 5213 continue; 5214 5215 if (prt_who) { 5216 const char *who = NULL; 5217 if (prt_title) { 5218 prt_title = B_FALSE; 5219 (void) printf(title); 5220 } 5221 5222 switch (who_type) { 5223 case ZFS_DELEG_USER_SETS: 5224 case ZFS_DELEG_USER: 5225 who = gettext("user"); 5226 if (nice_who_name) 5227 who_name = nice_who_name; 5228 break; 5229 case ZFS_DELEG_GROUP_SETS: 5230 case ZFS_DELEG_GROUP: 5231 who = gettext("group"); 5232 if (nice_who_name) 5233 who_name = nice_who_name; 5234 break; 5235 case ZFS_DELEG_EVERYONE_SETS: 5236 case ZFS_DELEG_EVERYONE: 5237 who = gettext("everyone"); 5238 who_name = NULL; 5239 } 5240 5241 prt_who = B_FALSE; 5242 if (who_name == NULL) 5243 (void) printf("\t%s", who); 5244 else 5245 (void) printf("\t%s %s", who, who_name); 5246 } 5247 5248 (void) printf("%c%s", delim, 5249 deleg_node->dpn_perm.dp_name); 5250 delim = ','; 5251 } 5252 5253 if (!prt_who) 5254 (void) printf("\n"); 5255 } 5256 5257 uu_avl_walk_end(walk); 5258} 5259 5260static void 5261print_fs_perms(fs_perm_set_t *fspset) 5262{ 5263 fs_perm_node_t *node = NULL; 5264 char buf[ZFS_MAXNAMELEN+32]; 5265 const char *dsname = buf; 5266 5267 for (node = uu_list_first(fspset->fsps_list); node != NULL; 5268 node = uu_list_next(fspset->fsps_list, node)) { 5269 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl; 5270 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl; 5271 int left = 0; 5272 5273 (void) snprintf(buf, ZFS_MAXNAMELEN+32, 5274 gettext("---- Permissions on %s "), 5275 node->fspn_fsperm.fsp_name); 5276 (void) printf(dsname); 5277 left = 70 - strlen(buf); 5278 while (left-- > 0) 5279 (void) printf("-"); 5280 (void) printf("\n"); 5281 5282 print_set_creat_perms(sc_avl); 5283 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE, 5284 gettext("Local permissions:\n")); 5285 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE, 5286 gettext("Descendent permissions:\n")); 5287 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE, 5288 gettext("Local+Descendent permissions:\n")); 5289 } 5290} 5291 5292static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL }; 5293 5294struct deleg_perms { 5295 boolean_t un; 5296 nvlist_t *nvl; 5297}; 5298 5299static int 5300set_deleg_perms(zfs_handle_t *zhp, void *data) 5301{ 5302 struct deleg_perms *perms = (struct deleg_perms *)data; 5303 zfs_type_t zfs_type = zfs_get_type(zhp); 5304 5305 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME) 5306 return (0); 5307 5308 return (zfs_set_fsacl(zhp, perms->un, perms->nvl)); 5309} 5310 5311static int 5312zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un) 5313{ 5314 zfs_handle_t *zhp; 5315 nvlist_t *perm_nvl = NULL; 5316 nvlist_t *update_perm_nvl = NULL; 5317 int error = 1; 5318 int c; 5319 struct allow_opts opts = { 0 }; 5320 5321 const char *optstr = un ? "ldugecsrh" : "ldugecsh"; 5322 5323 /* check opts */ 5324 while ((c = getopt(argc, argv, optstr)) != -1) { 5325 switch (c) { 5326 case 'l': 5327 opts.local = B_TRUE; 5328 break; 5329 case 'd': 5330 opts.descend = B_TRUE; 5331 break; 5332 case 'u': 5333 opts.user = B_TRUE; 5334 break; 5335 case 'g': 5336 opts.group = B_TRUE; 5337 break; 5338 case 'e': 5339 opts.everyone = B_TRUE; 5340 break; 5341 case 's': 5342 opts.set = B_TRUE; 5343 break; 5344 case 'c': 5345 opts.create = B_TRUE; 5346 break; 5347 case 'r': 5348 opts.recursive = B_TRUE; 5349 break; 5350 case ':': 5351 (void) fprintf(stderr, gettext("missing argument for " 5352 "'%c' option\n"), optopt); 5353 usage(B_FALSE); 5354 break; 5355 case 'h': 5356 opts.prt_usage = B_TRUE; 5357 break; 5358 case '?': 5359 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5360 optopt); 5361 usage(B_FALSE); 5362 } 5363 } 5364 5365 argc -= optind; 5366 argv += optind; 5367 5368 /* check arguments */ 5369 parse_allow_args(argc, argv, un, &opts); 5370 5371 /* try to open the dataset */ 5372 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM | 5373 ZFS_TYPE_VOLUME)) == NULL) { 5374 (void) fprintf(stderr, "Failed to open dataset: %s\n", 5375 opts.dataset); 5376 return (-1); 5377 } 5378 5379 if (zfs_get_fsacl(zhp, &perm_nvl) != 0) 5380 goto cleanup2; 5381 5382 fs_perm_set_init(&fs_perm_set); 5383 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) { 5384 (void) fprintf(stderr, "Failed to parse fsacl permissions\n"); 5385 goto cleanup1; 5386 } 5387 5388 if (opts.prt_perms) 5389 print_fs_perms(&fs_perm_set); 5390 else { 5391 (void) construct_fsacl_list(un, &opts, &update_perm_nvl); 5392 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0) 5393 goto cleanup0; 5394 5395 if (un && opts.recursive) { 5396 struct deleg_perms data = { un, update_perm_nvl }; 5397 if (zfs_iter_filesystems(zhp, set_deleg_perms, 5398 &data) != 0) 5399 goto cleanup0; 5400 } 5401 } 5402 5403 error = 0; 5404 5405cleanup0: 5406 nvlist_free(perm_nvl); 5407 if (update_perm_nvl != NULL) 5408 nvlist_free(update_perm_nvl); 5409cleanup1: 5410 fs_perm_set_fini(&fs_perm_set); 5411cleanup2: 5412 zfs_close(zhp); 5413 5414 return (error); 5415} 5416 5417static int 5418zfs_do_allow(int argc, char **argv) 5419{ 5420 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE)); 5421} 5422 5423static int 5424zfs_do_unallow(int argc, char **argv) 5425{ 5426 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE)); 5427} 5428 5429static int 5430zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding) 5431{ 5432 int errors = 0; 5433 int i; 5434 const char *tag; 5435 boolean_t recursive = B_FALSE; 5436 const char *opts = holding ? "rt" : "r"; 5437 int c; 5438 5439 /* check options */ 5440 while ((c = getopt(argc, argv, opts)) != -1) { 5441 switch (c) { 5442 case 'r': 5443 recursive = B_TRUE; 5444 break; 5445 case '?': 5446 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5447 optopt); 5448 usage(B_FALSE); 5449 } 5450 } 5451 5452 argc -= optind; 5453 argv += optind; 5454 5455 /* check number of arguments */ 5456 if (argc < 2) 5457 usage(B_FALSE); 5458 5459 tag = argv[0]; 5460 --argc; 5461 ++argv; 5462 5463 if (holding && tag[0] == '.') { 5464 /* tags starting with '.' are reserved for libzfs */ 5465 (void) fprintf(stderr, gettext("tag may not start with '.'\n")); 5466 usage(B_FALSE); 5467 } 5468 5469 for (i = 0; i < argc; ++i) { 5470 zfs_handle_t *zhp; 5471 char parent[ZFS_MAXNAMELEN]; 5472 const char *delim; 5473 char *path = argv[i]; 5474 5475 delim = strchr(path, '@'); 5476 if (delim == NULL) { 5477 (void) fprintf(stderr, 5478 gettext("'%s' is not a snapshot\n"), path); 5479 ++errors; 5480 continue; 5481 } 5482 (void) strncpy(parent, path, delim - path); 5483 parent[delim - path] = '\0'; 5484 5485 zhp = zfs_open(g_zfs, parent, 5486 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 5487 if (zhp == NULL) { 5488 ++errors; 5489 continue; 5490 } 5491 if (holding) { 5492 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0) 5493 ++errors; 5494 } else { 5495 if (zfs_release(zhp, delim+1, tag, recursive) != 0) 5496 ++errors; 5497 } 5498 zfs_close(zhp); 5499 } 5500 5501 return (errors != 0); 5502} 5503 5504/* 5505 * zfs hold [-r] [-t] <tag> <snap> ... 5506 * 5507 * -r Recursively hold 5508 * 5509 * Apply a user-hold with the given tag to the list of snapshots. 5510 */ 5511static int 5512zfs_do_hold(int argc, char **argv) 5513{ 5514 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE)); 5515} 5516 5517/* 5518 * zfs release [-r] <tag> <snap> ... 5519 * 5520 * -r Recursively release 5521 * 5522 * Release a user-hold with the given tag from the list of snapshots. 5523 */ 5524static int 5525zfs_do_release(int argc, char **argv) 5526{ 5527 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE)); 5528} 5529 5530typedef struct holds_cbdata { 5531 boolean_t cb_recursive; 5532 const char *cb_snapname; 5533 nvlist_t **cb_nvlp; 5534 size_t cb_max_namelen; 5535 size_t cb_max_taglen; 5536} holds_cbdata_t; 5537 5538#define STRFTIME_FMT_STR "%a %b %e %k:%M %Y" 5539#define DATETIME_BUF_LEN (32) 5540/* 5541 * 5542 */ 5543static void 5544print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl) 5545{ 5546 int i; 5547 nvpair_t *nvp = NULL; 5548 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" }; 5549 const char *col; 5550 5551 if (!scripted) { 5552 for (i = 0; i < 3; i++) { 5553 col = gettext(hdr_cols[i]); 5554 if (i < 2) 5555 (void) printf("%-*s ", i ? tagwidth : nwidth, 5556 col); 5557 else 5558 (void) printf("%s\n", col); 5559 } 5560 } 5561 5562 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 5563 char *zname = nvpair_name(nvp); 5564 nvlist_t *nvl2; 5565 nvpair_t *nvp2 = NULL; 5566 (void) nvpair_value_nvlist(nvp, &nvl2); 5567 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) { 5568 char tsbuf[DATETIME_BUF_LEN]; 5569 char *tagname = nvpair_name(nvp2); 5570 uint64_t val = 0; 5571 time_t time; 5572 struct tm t; 5573 char sep = scripted ? '\t' : ' '; 5574 size_t sepnum = scripted ? 1 : 2; 5575 5576 (void) nvpair_value_uint64(nvp2, &val); 5577 time = (time_t)val; 5578 (void) localtime_r(&time, &t); 5579 (void) strftime(tsbuf, DATETIME_BUF_LEN, 5580 gettext(STRFTIME_FMT_STR), &t); 5581 5582 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname, 5583 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf); 5584 } 5585 } 5586} 5587 5588/* 5589 * Generic callback function to list a dataset or snapshot. 5590 */ 5591static int 5592holds_callback(zfs_handle_t *zhp, void *data) 5593{ 5594 holds_cbdata_t *cbp = data; 5595 nvlist_t *top_nvl = *cbp->cb_nvlp; 5596 nvlist_t *nvl = NULL; 5597 nvpair_t *nvp = NULL; 5598 const char *zname = zfs_get_name(zhp); 5599 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN); 5600 5601 if (cbp->cb_recursive) { 5602 const char *snapname; 5603 char *delim = strchr(zname, '@'); 5604 if (delim == NULL) 5605 return (0); 5606 5607 snapname = delim + 1; 5608 if (strcmp(cbp->cb_snapname, snapname)) 5609 return (0); 5610 } 5611 5612 if (zfs_get_holds(zhp, &nvl) != 0) 5613 return (-1); 5614 5615 if (znamelen > cbp->cb_max_namelen) 5616 cbp->cb_max_namelen = znamelen; 5617 5618 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 5619 const char *tag = nvpair_name(nvp); 5620 size_t taglen = strnlen(tag, MAXNAMELEN); 5621 if (taglen > cbp->cb_max_taglen) 5622 cbp->cb_max_taglen = taglen; 5623 } 5624 5625 return (nvlist_add_nvlist(top_nvl, zname, nvl)); 5626} 5627 5628/* 5629 * zfs holds [-r] <snap> ... 5630 * 5631 * -r Recursively hold 5632 */ 5633static int 5634zfs_do_holds(int argc, char **argv) 5635{ 5636 int errors = 0; 5637 int c; 5638 int i; 5639 boolean_t scripted = B_FALSE; 5640 boolean_t recursive = B_FALSE; 5641 const char *opts = "rH"; 5642 nvlist_t *nvl; 5643 5644 int types = ZFS_TYPE_SNAPSHOT; 5645 holds_cbdata_t cb = { 0 }; 5646 5647 int limit = 0; 5648 int ret = 0; 5649 int flags = 0; 5650 5651 /* check options */ 5652 while ((c = getopt(argc, argv, opts)) != -1) { 5653 switch (c) { 5654 case 'r': 5655 recursive = B_TRUE; 5656 break; 5657 case 'H': 5658 scripted = B_TRUE; 5659 break; 5660 case '?': 5661 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5662 optopt); 5663 usage(B_FALSE); 5664 } 5665 } 5666 5667 if (recursive) { 5668 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME; 5669 flags |= ZFS_ITER_RECURSE; 5670 } 5671 5672 argc -= optind; 5673 argv += optind; 5674 5675 /* check number of arguments */ 5676 if (argc < 1) 5677 usage(B_FALSE); 5678 5679 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 5680 nomem(); 5681 5682 for (i = 0; i < argc; ++i) { 5683 char *snapshot = argv[i]; 5684 const char *delim; 5685 const char *snapname; 5686 5687 delim = strchr(snapshot, '@'); 5688 if (delim == NULL) { 5689 (void) fprintf(stderr, 5690 gettext("'%s' is not a snapshot\n"), snapshot); 5691 ++errors; 5692 continue; 5693 } 5694 snapname = delim + 1; 5695 if (recursive) 5696 snapshot[delim - snapshot] = '\0'; 5697 5698 cb.cb_recursive = recursive; 5699 cb.cb_snapname = snapname; 5700 cb.cb_nvlp = &nvl; 5701 5702 /* 5703 * 1. collect holds data, set format options 5704 */ 5705 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit, 5706 holds_callback, &cb); 5707 if (ret != 0) 5708 ++errors; 5709 } 5710 5711 /* 5712 * 2. print holds data 5713 */ 5714 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl); 5715 5716 if (nvlist_empty(nvl)) 5717 (void) printf(gettext("no datasets available\n")); 5718 5719 nvlist_free(nvl); 5720 5721 return (0 != errors); 5722} 5723 5724#define CHECK_SPINNER 30 5725#define SPINNER_TIME 3 /* seconds */ 5726#define MOUNT_TIME 5 /* seconds */ 5727 5728static int 5729get_one_dataset(zfs_handle_t *zhp, void *data) 5730{ 5731 static char *spin[] = { "-", "\\", "|", "/" }; 5732 static int spinval = 0; 5733 static int spincheck = 0; 5734 static time_t last_spin_time = (time_t)0; 5735 get_all_cb_t *cbp = data; 5736 zfs_type_t type = zfs_get_type(zhp); 5737 5738 if (cbp->cb_verbose) { 5739 if (--spincheck < 0) { 5740 time_t now = time(NULL); 5741 if (last_spin_time + SPINNER_TIME < now) { 5742 update_progress(spin[spinval++ % 4]); 5743 last_spin_time = now; 5744 } 5745 spincheck = CHECK_SPINNER; 5746 } 5747 } 5748 5749 /* 5750 * Interate over any nested datasets. 5751 */ 5752 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) { 5753 zfs_close(zhp); 5754 return (1); 5755 } 5756 5757 /* 5758 * Skip any datasets whose type does not match. 5759 */ 5760 if ((type & ZFS_TYPE_FILESYSTEM) == 0) { 5761 zfs_close(zhp); 5762 return (0); 5763 } 5764 libzfs_add_handle(cbp, zhp); 5765 assert(cbp->cb_used <= cbp->cb_alloc); 5766 5767 return (0); 5768} 5769 5770static void 5771get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose) 5772{ 5773 get_all_cb_t cb = { 0 }; 5774 cb.cb_verbose = verbose; 5775 cb.cb_getone = get_one_dataset; 5776 5777 if (verbose) 5778 set_progress_header(gettext("Reading ZFS config")); 5779 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb); 5780 5781 *dslist = cb.cb_handles; 5782 *count = cb.cb_used; 5783 5784 if (verbose) 5785 finish_progress(gettext("done.")); 5786} 5787 5788/* 5789 * Generic callback for sharing or mounting filesystems. Because the code is so 5790 * similar, we have a common function with an extra parameter to determine which 5791 * mode we are using. 5792 */ 5793#define OP_SHARE 0x1 5794#define OP_MOUNT 0x2 5795 5796/* 5797 * Share or mount a dataset. 5798 */ 5799static int 5800share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol, 5801 boolean_t explicit, const char *options) 5802{ 5803 char mountpoint[ZFS_MAXPROPLEN]; 5804 char shareopts[ZFS_MAXPROPLEN]; 5805 char smbshareopts[ZFS_MAXPROPLEN]; 5806 const char *cmdname = op == OP_SHARE ? "share" : "mount"; 5807 struct mnttab mnt; 5808 uint64_t zoned, canmount; 5809 boolean_t shared_nfs, shared_smb; 5810 5811 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM); 5812 5813 /* 5814 * Check to make sure we can mount/share this dataset. If we 5815 * are in the global zone and the filesystem is exported to a 5816 * local zone, or if we are in a local zone and the 5817 * filesystem is not exported, then it is an error. 5818 */ 5819 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 5820 5821 if (zoned && getzoneid() == GLOBAL_ZONEID) { 5822 if (!explicit) 5823 return (0); 5824 5825 (void) fprintf(stderr, gettext("cannot %s '%s': " 5826 "dataset is exported to a local zone\n"), cmdname, 5827 zfs_get_name(zhp)); 5828 return (1); 5829 5830 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) { 5831 if (!explicit) 5832 return (0); 5833 5834 (void) fprintf(stderr, gettext("cannot %s '%s': " 5835 "permission denied\n"), cmdname, 5836 zfs_get_name(zhp)); 5837 return (1); 5838 } 5839 5840 /* 5841 * Ignore any filesystems which don't apply to us. This 5842 * includes those with a legacy mountpoint, or those with 5843 * legacy share options. 5844 */ 5845 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 5846 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0); 5847 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, 5848 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0); 5849 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts, 5850 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0); 5851 5852 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 && 5853 strcmp(smbshareopts, "off") == 0) { 5854 if (!explicit) 5855 return (0); 5856 5857 (void) fprintf(stderr, gettext("cannot share '%s': " 5858 "legacy share\n"), zfs_get_name(zhp)); 5859 (void) fprintf(stderr, gettext("to " 5860 "share this filesystem set " 5861 "sharenfs property on\n")); 5862 return (1); 5863 } 5864 5865 /* 5866 * We cannot share or mount legacy filesystems. If the 5867 * shareopts is non-legacy but the mountpoint is legacy, we 5868 * treat it as a legacy share. 5869 */ 5870 if (strcmp(mountpoint, "legacy") == 0) { 5871 if (!explicit) 5872 return (0); 5873 5874 (void) fprintf(stderr, gettext("cannot %s '%s': " 5875 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp)); 5876 (void) fprintf(stderr, gettext("use %s(8) to " 5877 "%s this filesystem\n"), cmdname, cmdname); 5878 return (1); 5879 } 5880 5881 if (strcmp(mountpoint, "none") == 0) { 5882 if (!explicit) 5883 return (0); 5884 5885 (void) fprintf(stderr, gettext("cannot %s '%s': no " 5886 "mountpoint set\n"), cmdname, zfs_get_name(zhp)); 5887 return (1); 5888 } 5889 5890 /* 5891 * canmount explicit outcome 5892 * on no pass through 5893 * on yes pass through 5894 * off no return 0 5895 * off yes display error, return 1 5896 * noauto no return 0 5897 * noauto yes pass through 5898 */ 5899 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT); 5900 if (canmount == ZFS_CANMOUNT_OFF) { 5901 if (!explicit) 5902 return (0); 5903 5904 (void) fprintf(stderr, gettext("cannot %s '%s': " 5905 "'canmount' property is set to 'off'\n"), cmdname, 5906 zfs_get_name(zhp)); 5907 return (1); 5908 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) { 5909 return (0); 5910 } 5911 5912 /* 5913 * If this filesystem is inconsistent and has a receive resume 5914 * token, we can not mount it. 5915 */ 5916 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) && 5917 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 5918 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) { 5919 if (!explicit) 5920 return (0); 5921 5922 (void) fprintf(stderr, gettext("cannot %s '%s': " 5923 "Contains partially-completed state from " 5924 "\"zfs receive -r\", which can be resumed with " 5925 "\"zfs send -t\"\n"), 5926 cmdname, zfs_get_name(zhp)); 5927 return (1); 5928 } 5929 5930 /* 5931 * At this point, we have verified that the mountpoint and/or 5932 * shareopts are appropriate for auto management. If the 5933 * filesystem is already mounted or shared, return (failing 5934 * for explicit requests); otherwise mount or share the 5935 * filesystem. 5936 */ 5937 switch (op) { 5938 case OP_SHARE: 5939 5940 shared_nfs = zfs_is_shared_nfs(zhp, NULL); 5941 shared_smb = zfs_is_shared_smb(zhp, NULL); 5942 5943 if (shared_nfs && shared_smb || 5944 (shared_nfs && strcmp(shareopts, "on") == 0 && 5945 strcmp(smbshareopts, "off") == 0) || 5946 (shared_smb && strcmp(smbshareopts, "on") == 0 && 5947 strcmp(shareopts, "off") == 0)) { 5948 if (!explicit) 5949 return (0); 5950 5951 (void) fprintf(stderr, gettext("cannot share " 5952 "'%s': filesystem already shared\n"), 5953 zfs_get_name(zhp)); 5954 return (1); 5955 } 5956 5957 if (!zfs_is_mounted(zhp, NULL) && 5958 zfs_mount(zhp, NULL, 0) != 0) 5959 return (1); 5960 5961 if (protocol == NULL) { 5962 if (zfs_shareall(zhp) != 0) 5963 return (1); 5964 } else if (strcmp(protocol, "nfs") == 0) { 5965 if (zfs_share_nfs(zhp)) 5966 return (1); 5967 } else if (strcmp(protocol, "smb") == 0) { 5968 if (zfs_share_smb(zhp)) 5969 return (1); 5970 } else { 5971 (void) fprintf(stderr, gettext("cannot share " 5972 "'%s': invalid share type '%s' " 5973 "specified\n"), 5974 zfs_get_name(zhp), protocol); 5975 return (1); 5976 } 5977 5978 break; 5979 5980 case OP_MOUNT: 5981 if (options == NULL) 5982 mnt.mnt_mntopts = ""; 5983 else 5984 mnt.mnt_mntopts = (char *)options; 5985 5986 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) && 5987 zfs_is_mounted(zhp, NULL)) { 5988 if (!explicit) 5989 return (0); 5990 5991 (void) fprintf(stderr, gettext("cannot mount " 5992 "'%s': filesystem already mounted\n"), 5993 zfs_get_name(zhp)); 5994 return (1); 5995 } 5996 5997 if (zfs_mount(zhp, options, flags) != 0) 5998 return (1); 5999 break; 6000 } 6001 6002 return (0); 6003} 6004 6005/* 6006 * Reports progress in the form "(current/total)". Not thread-safe. 6007 */ 6008static void 6009report_mount_progress(int current, int total) 6010{ 6011 static time_t last_progress_time = 0; 6012 time_t now = time(NULL); 6013 char info[32]; 6014 6015 /* report 1..n instead of 0..n-1 */ 6016 ++current; 6017 6018 /* display header if we're here for the first time */ 6019 if (current == 1) { 6020 set_progress_header(gettext("Mounting ZFS filesystems")); 6021 } else if (current != total && last_progress_time + MOUNT_TIME >= now) { 6022 /* too soon to report again */ 6023 return; 6024 } 6025 6026 last_progress_time = now; 6027 6028 (void) sprintf(info, "(%d/%d)", current, total); 6029 6030 if (current == total) 6031 finish_progress(info); 6032 else 6033 update_progress(info); 6034} 6035 6036static void 6037append_options(char *mntopts, char *newopts) 6038{ 6039 int len = strlen(mntopts); 6040 6041 /* original length plus new string to append plus 1 for the comma */ 6042 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) { 6043 (void) fprintf(stderr, gettext("the opts argument for " 6044 "'%c' option is too long (more than %d chars)\n"), 6045 "-o", MNT_LINE_MAX); 6046 usage(B_FALSE); 6047 } 6048 6049 if (*mntopts) 6050 mntopts[len++] = ','; 6051 6052 (void) strcpy(&mntopts[len], newopts); 6053} 6054 6055static int 6056share_mount(int op, int argc, char **argv) 6057{ 6058 int do_all = 0; 6059 boolean_t verbose = B_FALSE; 6060 int c, ret = 0; 6061 char *options = NULL; 6062 int flags = 0; 6063 6064 /* check options */ 6065 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a")) 6066 != -1) { 6067 switch (c) { 6068 case 'a': 6069 do_all = 1; 6070 break; 6071 case 'v': 6072 verbose = B_TRUE; 6073 break; 6074 case 'o': 6075 if (*optarg == '\0') { 6076 (void) fprintf(stderr, gettext("empty mount " 6077 "options (-o) specified\n")); 6078 usage(B_FALSE); 6079 } 6080 6081 if (options == NULL) 6082 options = safe_malloc(MNT_LINE_MAX + 1); 6083 6084 /* option validation is done later */ 6085 append_options(options, optarg); 6086 break; 6087 6088 case 'O': 6089 warnx("no overlay mounts support on FreeBSD, ignoring"); 6090 break; 6091 case ':': 6092 (void) fprintf(stderr, gettext("missing argument for " 6093 "'%c' option\n"), optopt); 6094 usage(B_FALSE); 6095 break; 6096 case '?': 6097 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6098 optopt); 6099 usage(B_FALSE); 6100 } 6101 } 6102 6103 argc -= optind; 6104 argv += optind; 6105 6106 /* check number of arguments */ 6107 if (do_all) { 6108 zfs_handle_t **dslist = NULL; 6109 size_t i, count = 0; 6110 char *protocol = NULL; 6111 6112 if (op == OP_SHARE && argc > 0) { 6113 if (strcmp(argv[0], "nfs") != 0 && 6114 strcmp(argv[0], "smb") != 0) { 6115 (void) fprintf(stderr, gettext("share type " 6116 "must be 'nfs' or 'smb'\n")); 6117 usage(B_FALSE); 6118 } 6119 protocol = argv[0]; 6120 argc--; 6121 argv++; 6122 } 6123 6124 if (argc != 0) { 6125 (void) fprintf(stderr, gettext("too many arguments\n")); 6126 usage(B_FALSE); 6127 } 6128 6129 start_progress_timer(); 6130 get_all_datasets(&dslist, &count, verbose); 6131 6132 if (count == 0) 6133 return (0); 6134 6135 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp); 6136 6137 for (i = 0; i < count; i++) { 6138 if (verbose) 6139 report_mount_progress(i, count); 6140 6141 if (share_mount_one(dslist[i], op, flags, protocol, 6142 B_FALSE, options) != 0) 6143 ret = 1; 6144 zfs_close(dslist[i]); 6145 } 6146 6147 free(dslist); 6148 } else if (argc == 0) { 6149 struct mnttab entry; 6150 6151 if ((op == OP_SHARE) || (options != NULL)) { 6152 (void) fprintf(stderr, gettext("missing filesystem " 6153 "argument (specify -a for all)\n")); 6154 usage(B_FALSE); 6155 } 6156 6157 /* 6158 * When mount is given no arguments, go through /etc/mnttab and 6159 * display any active ZFS mounts. We hide any snapshots, since 6160 * they are controlled automatically. 6161 */ 6162 rewind(mnttab_file); 6163 while (getmntent(mnttab_file, &entry) == 0) { 6164 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 || 6165 strchr(entry.mnt_special, '@') != NULL) 6166 continue; 6167 6168 (void) printf("%-30s %s\n", entry.mnt_special, 6169 entry.mnt_mountp); 6170 } 6171 6172 } else { 6173 zfs_handle_t *zhp; 6174 6175 if (argc > 1) { 6176 (void) fprintf(stderr, 6177 gettext("too many arguments\n")); 6178 usage(B_FALSE); 6179 } 6180 6181 if ((zhp = zfs_open(g_zfs, argv[0], 6182 ZFS_TYPE_FILESYSTEM)) == NULL) { 6183 ret = 1; 6184 } else { 6185 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE, 6186 options); 6187 zfs_close(zhp); 6188 } 6189 } 6190 6191 return (ret); 6192} 6193 6194/* 6195 * zfs mount -a [nfs] 6196 * zfs mount filesystem 6197 * 6198 * Mount all filesystems, or mount the given filesystem. 6199 */ 6200static int 6201zfs_do_mount(int argc, char **argv) 6202{ 6203 return (share_mount(OP_MOUNT, argc, argv)); 6204} 6205 6206/* 6207 * zfs share -a [nfs | smb] 6208 * zfs share filesystem 6209 * 6210 * Share all filesystems, or share the given filesystem. 6211 */ 6212static int 6213zfs_do_share(int argc, char **argv) 6214{ 6215 return (share_mount(OP_SHARE, argc, argv)); 6216} 6217 6218typedef struct unshare_unmount_node { 6219 zfs_handle_t *un_zhp; 6220 char *un_mountp; 6221 uu_avl_node_t un_avlnode; 6222} unshare_unmount_node_t; 6223 6224/* ARGSUSED */ 6225static int 6226unshare_unmount_compare(const void *larg, const void *rarg, void *unused) 6227{ 6228 const unshare_unmount_node_t *l = larg; 6229 const unshare_unmount_node_t *r = rarg; 6230 6231 return (strcmp(l->un_mountp, r->un_mountp)); 6232} 6233 6234/* 6235 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an 6236 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem, 6237 * and unmount it appropriately. 6238 */ 6239static int 6240unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual) 6241{ 6242 zfs_handle_t *zhp; 6243 int ret = 0; 6244 struct stat64 statbuf; 6245 struct extmnttab entry; 6246 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount"; 6247 ino_t path_inode; 6248 6249 /* 6250 * Search for the path in /etc/mnttab. Rather than looking for the 6251 * specific path, which can be fooled by non-standard paths (i.e. ".." 6252 * or "//"), we stat() the path and search for the corresponding 6253 * (major,minor) device pair. 6254 */ 6255 if (stat64(path, &statbuf) != 0) { 6256 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 6257 cmdname, path, strerror(errno)); 6258 return (1); 6259 } 6260 path_inode = statbuf.st_ino; 6261 6262 /* 6263 * Search for the given (major,minor) pair in the mount table. 6264 */ 6265#ifdef sun 6266 rewind(mnttab_file); 6267 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) { 6268 if (entry.mnt_major == major(statbuf.st_dev) && 6269 entry.mnt_minor == minor(statbuf.st_dev)) 6270 break; 6271 } 6272#else 6273 { 6274 struct statfs sfs; 6275 6276 if (statfs(path, &sfs) != 0) { 6277 (void) fprintf(stderr, "%s: %s\n", path, 6278 strerror(errno)); 6279 ret = -1; 6280 } 6281 statfs2mnttab(&sfs, &entry); 6282 } 6283#endif 6284 if (ret != 0) { 6285 if (op == OP_SHARE) { 6286 (void) fprintf(stderr, gettext("cannot %s '%s': not " 6287 "currently mounted\n"), cmdname, path); 6288 return (1); 6289 } 6290 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"), 6291 path); 6292 if ((ret = umount2(path, flags)) != 0) 6293 (void) fprintf(stderr, gettext("%s: %s\n"), path, 6294 strerror(errno)); 6295 return (ret != 0); 6296 } 6297 6298 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 6299 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS " 6300 "filesystem\n"), cmdname, path); 6301 return (1); 6302 } 6303 6304 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 6305 ZFS_TYPE_FILESYSTEM)) == NULL) 6306 return (1); 6307 6308 ret = 1; 6309 if (stat64(entry.mnt_mountp, &statbuf) != 0) { 6310 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 6311 cmdname, path, strerror(errno)); 6312 goto out; 6313 } else if (statbuf.st_ino != path_inode) { 6314 (void) fprintf(stderr, gettext("cannot " 6315 "%s '%s': not a mountpoint\n"), cmdname, path); 6316 goto out; 6317 } 6318 6319 if (op == OP_SHARE) { 6320 char nfs_mnt_prop[ZFS_MAXPROPLEN]; 6321 char smbshare_prop[ZFS_MAXPROPLEN]; 6322 6323 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop, 6324 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0); 6325 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop, 6326 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0); 6327 6328 if (strcmp(nfs_mnt_prop, "off") == 0 && 6329 strcmp(smbshare_prop, "off") == 0) { 6330 (void) fprintf(stderr, gettext("cannot unshare " 6331 "'%s': legacy share\n"), path); 6332#ifdef illumos 6333 (void) fprintf(stderr, gettext("use " 6334 "unshare(1M) to unshare this filesystem\n")); 6335#endif 6336 } else if (!zfs_is_shared(zhp)) { 6337 (void) fprintf(stderr, gettext("cannot unshare '%s': " 6338 "not currently shared\n"), path); 6339 } else { 6340 ret = zfs_unshareall_bypath(zhp, path); 6341 } 6342 } else { 6343 char mtpt_prop[ZFS_MAXPROPLEN]; 6344 6345 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop, 6346 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0); 6347 6348 if (is_manual) { 6349 ret = zfs_unmount(zhp, NULL, flags); 6350 } else if (strcmp(mtpt_prop, "legacy") == 0) { 6351 (void) fprintf(stderr, gettext("cannot unmount " 6352 "'%s': legacy mountpoint\n"), 6353 zfs_get_name(zhp)); 6354 (void) fprintf(stderr, gettext("use umount(8) " 6355 "to unmount this filesystem\n")); 6356 } else { 6357 ret = zfs_unmountall(zhp, flags); 6358 } 6359 } 6360 6361out: 6362 zfs_close(zhp); 6363 6364 return (ret != 0); 6365} 6366 6367/* 6368 * Generic callback for unsharing or unmounting a filesystem. 6369 */ 6370static int 6371unshare_unmount(int op, int argc, char **argv) 6372{ 6373 int do_all = 0; 6374 int flags = 0; 6375 int ret = 0; 6376 int c; 6377 zfs_handle_t *zhp; 6378 char nfs_mnt_prop[ZFS_MAXPROPLEN]; 6379 char sharesmb[ZFS_MAXPROPLEN]; 6380 6381 /* check options */ 6382 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) { 6383 switch (c) { 6384 case 'a': 6385 do_all = 1; 6386 break; 6387 case 'f': 6388 flags = MS_FORCE; 6389 break; 6390 case '?': 6391 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6392 optopt); 6393 usage(B_FALSE); 6394 } 6395 } 6396 6397 argc -= optind; 6398 argv += optind; 6399 6400 if (do_all) { 6401 /* 6402 * We could make use of zfs_for_each() to walk all datasets in 6403 * the system, but this would be very inefficient, especially 6404 * since we would have to linearly search /etc/mnttab for each 6405 * one. Instead, do one pass through /etc/mnttab looking for 6406 * zfs entries and call zfs_unmount() for each one. 6407 * 6408 * Things get a little tricky if the administrator has created 6409 * mountpoints beneath other ZFS filesystems. In this case, we 6410 * have to unmount the deepest filesystems first. To accomplish 6411 * this, we place all the mountpoints in an AVL tree sorted by 6412 * the special type (dataset name), and walk the result in 6413 * reverse to make sure to get any snapshots first. 6414 */ 6415 struct mnttab entry; 6416 uu_avl_pool_t *pool; 6417 uu_avl_t *tree; 6418 unshare_unmount_node_t *node; 6419 uu_avl_index_t idx; 6420 uu_avl_walk_t *walk; 6421 6422 if (argc != 0) { 6423 (void) fprintf(stderr, gettext("too many arguments\n")); 6424 usage(B_FALSE); 6425 } 6426 6427 if (((pool = uu_avl_pool_create("unmount_pool", 6428 sizeof (unshare_unmount_node_t), 6429 offsetof(unshare_unmount_node_t, un_avlnode), 6430 unshare_unmount_compare, UU_DEFAULT)) == NULL) || 6431 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL)) 6432 nomem(); 6433 6434 rewind(mnttab_file); 6435 while (getmntent(mnttab_file, &entry) == 0) { 6436 6437 /* ignore non-ZFS entries */ 6438 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 6439 continue; 6440 6441 /* ignore snapshots */ 6442 if (strchr(entry.mnt_special, '@') != NULL) 6443 continue; 6444 6445 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 6446 ZFS_TYPE_FILESYSTEM)) == NULL) { 6447 ret = 1; 6448 continue; 6449 } 6450 6451 switch (op) { 6452 case OP_SHARE: 6453 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 6454 nfs_mnt_prop, 6455 sizeof (nfs_mnt_prop), 6456 NULL, NULL, 0, B_FALSE) == 0); 6457 if (strcmp(nfs_mnt_prop, "off") != 0) 6458 break; 6459 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 6460 nfs_mnt_prop, 6461 sizeof (nfs_mnt_prop), 6462 NULL, NULL, 0, B_FALSE) == 0); 6463 if (strcmp(nfs_mnt_prop, "off") == 0) 6464 continue; 6465 break; 6466 case OP_MOUNT: 6467 /* Ignore legacy mounts */ 6468 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, 6469 nfs_mnt_prop, 6470 sizeof (nfs_mnt_prop), 6471 NULL, NULL, 0, B_FALSE) == 0); 6472 if (strcmp(nfs_mnt_prop, "legacy") == 0) 6473 continue; 6474 /* Ignore canmount=noauto mounts */ 6475 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == 6476 ZFS_CANMOUNT_NOAUTO) 6477 continue; 6478 default: 6479 break; 6480 } 6481 6482 node = safe_malloc(sizeof (unshare_unmount_node_t)); 6483 node->un_zhp = zhp; 6484 node->un_mountp = safe_strdup(entry.mnt_mountp); 6485 6486 uu_avl_node_init(node, &node->un_avlnode, pool); 6487 6488 if (uu_avl_find(tree, node, NULL, &idx) == NULL) { 6489 uu_avl_insert(tree, node, idx); 6490 } else { 6491 zfs_close(node->un_zhp); 6492 free(node->un_mountp); 6493 free(node); 6494 } 6495 } 6496 6497 /* 6498 * Walk the AVL tree in reverse, unmounting each filesystem and 6499 * removing it from the AVL tree in the process. 6500 */ 6501 if ((walk = uu_avl_walk_start(tree, 6502 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) 6503 nomem(); 6504 6505 while ((node = uu_avl_walk_next(walk)) != NULL) { 6506 uu_avl_remove(tree, node); 6507 6508 switch (op) { 6509 case OP_SHARE: 6510 if (zfs_unshareall_bypath(node->un_zhp, 6511 node->un_mountp) != 0) 6512 ret = 1; 6513 break; 6514 6515 case OP_MOUNT: 6516 if (zfs_unmount(node->un_zhp, 6517 node->un_mountp, flags) != 0) 6518 ret = 1; 6519 break; 6520 } 6521 6522 zfs_close(node->un_zhp); 6523 free(node->un_mountp); 6524 free(node); 6525 } 6526 6527 uu_avl_walk_end(walk); 6528 uu_avl_destroy(tree); 6529 uu_avl_pool_destroy(pool); 6530 6531 } else { 6532 if (argc != 1) { 6533 if (argc == 0) 6534 (void) fprintf(stderr, 6535 gettext("missing filesystem argument\n")); 6536 else 6537 (void) fprintf(stderr, 6538 gettext("too many arguments\n")); 6539 usage(B_FALSE); 6540 } 6541 6542 /* 6543 * We have an argument, but it may be a full path or a ZFS 6544 * filesystem. Pass full paths off to unmount_path() (shared by 6545 * manual_unmount), otherwise open the filesystem and pass to 6546 * zfs_unmount(). 6547 */ 6548 if (argv[0][0] == '/') 6549 return (unshare_unmount_path(op, argv[0], 6550 flags, B_FALSE)); 6551 6552 if ((zhp = zfs_open(g_zfs, argv[0], 6553 ZFS_TYPE_FILESYSTEM)) == NULL) 6554 return (1); 6555 6556 verify(zfs_prop_get(zhp, op == OP_SHARE ? 6557 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, 6558 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL, 6559 NULL, 0, B_FALSE) == 0); 6560 6561 switch (op) { 6562 case OP_SHARE: 6563 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 6564 nfs_mnt_prop, 6565 sizeof (nfs_mnt_prop), 6566 NULL, NULL, 0, B_FALSE) == 0); 6567 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 6568 sharesmb, sizeof (sharesmb), NULL, NULL, 6569 0, B_FALSE) == 0); 6570 6571 if (strcmp(nfs_mnt_prop, "off") == 0 && 6572 strcmp(sharesmb, "off") == 0) { 6573 (void) fprintf(stderr, gettext("cannot " 6574 "unshare '%s': legacy share\n"), 6575 zfs_get_name(zhp)); 6576#ifdef illumos 6577 (void) fprintf(stderr, gettext("use " 6578 "unshare(1M) to unshare this " 6579 "filesystem\n")); 6580#endif 6581 ret = 1; 6582 } else if (!zfs_is_shared(zhp)) { 6583 (void) fprintf(stderr, gettext("cannot " 6584 "unshare '%s': not currently " 6585 "shared\n"), zfs_get_name(zhp)); 6586 ret = 1; 6587 } else if (zfs_unshareall(zhp) != 0) { 6588 ret = 1; 6589 } 6590 break; 6591 6592 case OP_MOUNT: 6593 if (strcmp(nfs_mnt_prop, "legacy") == 0) { 6594 (void) fprintf(stderr, gettext("cannot " 6595 "unmount '%s': legacy " 6596 "mountpoint\n"), zfs_get_name(zhp)); 6597 (void) fprintf(stderr, gettext("use " 6598 "umount(8) to unmount this " 6599 "filesystem\n")); 6600 ret = 1; 6601 } else if (!zfs_is_mounted(zhp, NULL)) { 6602 (void) fprintf(stderr, gettext("cannot " 6603 "unmount '%s': not currently " 6604 "mounted\n"), 6605 zfs_get_name(zhp)); 6606 ret = 1; 6607 } else if (zfs_unmountall(zhp, flags) != 0) { 6608 ret = 1; 6609 } 6610 break; 6611 } 6612 6613 zfs_close(zhp); 6614 } 6615 6616 return (ret); 6617} 6618 6619/* 6620 * zfs unmount -a 6621 * zfs unmount filesystem 6622 * 6623 * Unmount all filesystems, or a specific ZFS filesystem. 6624 */ 6625static int 6626zfs_do_unmount(int argc, char **argv) 6627{ 6628 return (unshare_unmount(OP_MOUNT, argc, argv)); 6629} 6630 6631/* 6632 * zfs unshare -a 6633 * zfs unshare filesystem 6634 * 6635 * Unshare all filesystems, or a specific ZFS filesystem. 6636 */ 6637static int 6638zfs_do_unshare(int argc, char **argv) 6639{ 6640 return (unshare_unmount(OP_SHARE, argc, argv)); 6641} 6642 6643/* 6644 * Attach/detach the given dataset to/from the given jail 6645 */ 6646/* ARGSUSED */ 6647static int 6648do_jail(int argc, char **argv, int attach) 6649{ 6650 zfs_handle_t *zhp; 6651 int jailid, ret; 6652 6653 /* check number of arguments */ 6654 if (argc < 3) { 6655 (void) fprintf(stderr, gettext("missing argument(s)\n")); 6656 usage(B_FALSE); 6657 } 6658 if (argc > 3) { 6659 (void) fprintf(stderr, gettext("too many arguments\n")); 6660 usage(B_FALSE); 6661 } 6662 6663 jailid = jail_getid(argv[1]); 6664 if (jailid < 0) { 6665 (void) fprintf(stderr, gettext("invalid jail id or name\n")); 6666 usage(B_FALSE); 6667 } 6668 6669 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM); 6670 if (zhp == NULL) 6671 return (1); 6672 6673 ret = (zfs_jail(zhp, jailid, attach) != 0); 6674 6675 zfs_close(zhp); 6676 return (ret); 6677} 6678 6679/* 6680 * zfs jail jailid filesystem 6681 * 6682 * Attach the given dataset to the given jail 6683 */ 6684/* ARGSUSED */ 6685static int 6686zfs_do_jail(int argc, char **argv) 6687{ 6688 6689 return (do_jail(argc, argv, 1)); 6690} 6691 6692/* 6693 * zfs unjail jailid filesystem 6694 * 6695 * Detach the given dataset from the given jail 6696 */ 6697/* ARGSUSED */ 6698static int 6699zfs_do_unjail(int argc, char **argv) 6700{ 6701 6702 return (do_jail(argc, argv, 0)); 6703} 6704 6705/* 6706 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is 6707 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'. 6708 */ 6709static int 6710manual_mount(int argc, char **argv) 6711{ 6712 zfs_handle_t *zhp; 6713 char mountpoint[ZFS_MAXPROPLEN]; 6714 char mntopts[MNT_LINE_MAX] = { '\0' }; 6715 int ret = 0; 6716 int c; 6717 int flags = 0; 6718 char *dataset, *path; 6719 6720 /* check options */ 6721 while ((c = getopt(argc, argv, ":mo:O")) != -1) { 6722 switch (c) { 6723 case 'o': 6724 (void) strlcpy(mntopts, optarg, sizeof (mntopts)); 6725 break; 6726 case 'O': 6727 flags |= MS_OVERLAY; 6728 break; 6729 case 'm': 6730 flags |= MS_NOMNTTAB; 6731 break; 6732 case ':': 6733 (void) fprintf(stderr, gettext("missing argument for " 6734 "'%c' option\n"), optopt); 6735 usage(B_FALSE); 6736 break; 6737 case '?': 6738 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6739 optopt); 6740 (void) fprintf(stderr, gettext("usage: mount [-o opts] " 6741 "<path>\n")); 6742 return (2); 6743 } 6744 } 6745 6746 argc -= optind; 6747 argv += optind; 6748 6749 /* check that we only have two arguments */ 6750 if (argc != 2) { 6751 if (argc == 0) 6752 (void) fprintf(stderr, gettext("missing dataset " 6753 "argument\n")); 6754 else if (argc == 1) 6755 (void) fprintf(stderr, 6756 gettext("missing mountpoint argument\n")); 6757 else 6758 (void) fprintf(stderr, gettext("too many arguments\n")); 6759 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n"); 6760 return (2); 6761 } 6762 6763 dataset = argv[0]; 6764 path = argv[1]; 6765 6766 /* try to open the dataset */ 6767 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) 6768 return (1); 6769 6770 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 6771 sizeof (mountpoint), NULL, NULL, 0, B_FALSE); 6772 6773 /* check for legacy mountpoint and complain appropriately */ 6774 ret = 0; 6775 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) { 6776 if (zmount(dataset, path, flags, MNTTYPE_ZFS, 6777 NULL, 0, mntopts, sizeof (mntopts)) != 0) { 6778 (void) fprintf(stderr, gettext("mount failed: %s\n"), 6779 strerror(errno)); 6780 ret = 1; 6781 } 6782 } else { 6783 (void) fprintf(stderr, gettext("filesystem '%s' cannot be " 6784 "mounted using 'mount -t zfs'\n"), dataset); 6785 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' " 6786 "instead.\n"), path); 6787 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' " 6788 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n")); 6789 (void) fprintf(stderr, gettext("See zfs(8) for more " 6790 "information.\n")); 6791 ret = 1; 6792 } 6793 6794 return (ret); 6795} 6796 6797/* 6798 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow 6799 * unmounts of non-legacy filesystems, as this is the dominant administrative 6800 * interface. 6801 */ 6802static int 6803manual_unmount(int argc, char **argv) 6804{ 6805 int flags = 0; 6806 int c; 6807 6808 /* check options */ 6809 while ((c = getopt(argc, argv, "f")) != -1) { 6810 switch (c) { 6811 case 'f': 6812 flags = MS_FORCE; 6813 break; 6814 case '?': 6815 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6816 optopt); 6817 (void) fprintf(stderr, gettext("usage: unmount [-f] " 6818 "<path>\n")); 6819 return (2); 6820 } 6821 } 6822 6823 argc -= optind; 6824 argv += optind; 6825 6826 /* check arguments */ 6827 if (argc != 1) { 6828 if (argc == 0) 6829 (void) fprintf(stderr, gettext("missing path " 6830 "argument\n")); 6831 else 6832 (void) fprintf(stderr, gettext("too many arguments\n")); 6833 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n")); 6834 return (2); 6835 } 6836 6837 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE)); 6838} 6839 6840static int 6841find_command_idx(char *command, int *idx) 6842{ 6843 int i; 6844 6845 for (i = 0; i < NCOMMAND; i++) { 6846 if (command_table[i].name == NULL) 6847 continue; 6848 6849 if (strcmp(command, command_table[i].name) == 0) { 6850 *idx = i; 6851 return (0); 6852 } 6853 } 6854 return (1); 6855} 6856 6857static int 6858zfs_do_diff(int argc, char **argv) 6859{ 6860 zfs_handle_t *zhp; 6861 int flags = 0; 6862 char *tosnap = NULL; 6863 char *fromsnap = NULL; 6864 char *atp, *copy; 6865 int err = 0; 6866 int c; 6867 6868 while ((c = getopt(argc, argv, "FHt")) != -1) { 6869 switch (c) { 6870 case 'F': 6871 flags |= ZFS_DIFF_CLASSIFY; 6872 break; 6873 case 'H': 6874 flags |= ZFS_DIFF_PARSEABLE; 6875 break; 6876 case 't': 6877 flags |= ZFS_DIFF_TIMESTAMP; 6878 break; 6879 default: 6880 (void) fprintf(stderr, 6881 gettext("invalid option '%c'\n"), optopt); 6882 usage(B_FALSE); 6883 } 6884 } 6885 6886 argc -= optind; 6887 argv += optind; 6888 6889 if (argc < 1) { 6890 (void) fprintf(stderr, 6891 gettext("must provide at least one snapshot name\n")); 6892 usage(B_FALSE); 6893 } 6894 6895 if (argc > 2) { 6896 (void) fprintf(stderr, gettext("too many arguments\n")); 6897 usage(B_FALSE); 6898 } 6899 6900 fromsnap = argv[0]; 6901 tosnap = (argc == 2) ? argv[1] : NULL; 6902 6903 copy = NULL; 6904 if (*fromsnap != '@') 6905 copy = strdup(fromsnap); 6906 else if (tosnap) 6907 copy = strdup(tosnap); 6908 if (copy == NULL) 6909 usage(B_FALSE); 6910 6911 if (atp = strchr(copy, '@')) 6912 *atp = '\0'; 6913 6914 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) 6915 return (1); 6916 6917 free(copy); 6918 6919 /* 6920 * Ignore SIGPIPE so that the library can give us 6921 * information on any failure 6922 */ 6923 (void) sigignore(SIGPIPE); 6924 6925 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags); 6926 6927 zfs_close(zhp); 6928 6929 return (err != 0); 6930} 6931 6932/* 6933 * zfs bookmark <fs@snap> <fs#bmark> 6934 * 6935 * Creates a bookmark with the given name from the given snapshot. 6936 */ 6937static int 6938zfs_do_bookmark(int argc, char **argv) 6939{ 6940 char snapname[ZFS_MAXNAMELEN]; 6941 zfs_handle_t *zhp; 6942 nvlist_t *nvl; 6943 int ret = 0; 6944 int c; 6945 6946 /* check options */ 6947 while ((c = getopt(argc, argv, "")) != -1) { 6948 switch (c) { 6949 case '?': 6950 (void) fprintf(stderr, 6951 gettext("invalid option '%c'\n"), optopt); 6952 goto usage; 6953 } 6954 } 6955 6956 argc -= optind; 6957 argv += optind; 6958 6959 /* check number of arguments */ 6960 if (argc < 1) { 6961 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 6962 goto usage; 6963 } 6964 if (argc < 2) { 6965 (void) fprintf(stderr, gettext("missing bookmark argument\n")); 6966 goto usage; 6967 } 6968 6969 if (strchr(argv[1], '#') == NULL) { 6970 (void) fprintf(stderr, 6971 gettext("invalid bookmark name '%s' -- " 6972 "must contain a '#'\n"), argv[1]); 6973 goto usage; 6974 } 6975 6976 if (argv[0][0] == '@') { 6977 /* 6978 * Snapshot name begins with @. 6979 * Default to same fs as bookmark. 6980 */ 6981 (void) strncpy(snapname, argv[1], sizeof (snapname)); 6982 *strchr(snapname, '#') = '\0'; 6983 (void) strlcat(snapname, argv[0], sizeof (snapname)); 6984 } else { 6985 (void) strncpy(snapname, argv[0], sizeof (snapname)); 6986 } 6987 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT); 6988 if (zhp == NULL) 6989 goto usage; 6990 zfs_close(zhp); 6991 6992 6993 nvl = fnvlist_alloc(); 6994 fnvlist_add_string(nvl, argv[1], snapname); 6995 ret = lzc_bookmark(nvl, NULL); 6996 fnvlist_free(nvl); 6997 6998 if (ret != 0) { 6999 const char *err_msg; 7000 char errbuf[1024]; 7001 7002 (void) snprintf(errbuf, sizeof (errbuf), 7003 dgettext(TEXT_DOMAIN, 7004 "cannot create bookmark '%s'"), argv[1]); 7005 7006 switch (ret) { 7007 case EXDEV: 7008 err_msg = "bookmark is in a different pool"; 7009 break; 7010 case EEXIST: 7011 err_msg = "bookmark exists"; 7012 break; 7013 case EINVAL: 7014 err_msg = "invalid argument"; 7015 break; 7016 case ENOTSUP: 7017 err_msg = "bookmark feature not enabled"; 7018 break; 7019 case ENOSPC: 7020 err_msg = "out of space"; 7021 break; 7022 default: 7023 err_msg = "unknown error"; 7024 break; 7025 } 7026 (void) fprintf(stderr, "%s: %s\n", errbuf, 7027 dgettext(TEXT_DOMAIN, err_msg)); 7028 } 7029 7030 return (ret != 0); 7031 7032usage: 7033 usage(B_FALSE); 7034 return (-1); 7035} 7036 7037int 7038main(int argc, char **argv) 7039{ 7040 int ret = 0; 7041 int i; 7042 char *progname; 7043 char *cmdname; 7044 7045 (void) setlocale(LC_ALL, ""); 7046 (void) textdomain(TEXT_DOMAIN); 7047 7048 opterr = 0; 7049 7050 if ((g_zfs = libzfs_init()) == NULL) { 7051 (void) fprintf(stderr, gettext("internal error: failed to " 7052 "initialize ZFS library\n")); 7053 return (1); 7054 } 7055 7056 zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 7057 7058 libzfs_print_on_error(g_zfs, B_TRUE); 7059 7060 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) { 7061 (void) fprintf(stderr, gettext("internal error: unable to " 7062 "open %s\n"), MNTTAB); 7063 return (1); 7064 } 7065 7066 /* 7067 * This command also doubles as the /etc/fs mount and unmount program. 7068 * Determine if we should take this behavior based on argv[0]. 7069 */ 7070 progname = basename(argv[0]); 7071 if (strcmp(progname, "mount") == 0) { 7072 ret = manual_mount(argc, argv); 7073 } else if (strcmp(progname, "umount") == 0) { 7074 ret = manual_unmount(argc, argv); 7075 } else { 7076 /* 7077 * Make sure the user has specified some command. 7078 */ 7079 if (argc < 2) { 7080 (void) fprintf(stderr, gettext("missing command\n")); 7081 usage(B_FALSE); 7082 } 7083 7084 cmdname = argv[1]; 7085 7086 /* 7087 * The 'umount' command is an alias for 'unmount' 7088 */ 7089 if (strcmp(cmdname, "umount") == 0) 7090 cmdname = "unmount"; 7091 7092 /* 7093 * The 'recv' command is an alias for 'receive' 7094 */ 7095 if (strcmp(cmdname, "recv") == 0) 7096 cmdname = "receive"; 7097 7098 /* 7099 * The 'snap' command is an alias for 'snapshot' 7100 */ 7101 if (strcmp(cmdname, "snap") == 0) 7102 cmdname = "snapshot"; 7103 7104 /* 7105 * Special case '-?' 7106 */ 7107 if (strcmp(cmdname, "-?") == 0) 7108 usage(B_TRUE); 7109 7110 /* 7111 * Run the appropriate command. 7112 */ 7113 libzfs_mnttab_cache(g_zfs, B_TRUE); 7114 if (find_command_idx(cmdname, &i) == 0) { 7115 current_command = &command_table[i]; 7116 ret = command_table[i].func(argc - 1, argv + 1); 7117 } else if (strchr(cmdname, '=') != NULL) { 7118 verify(find_command_idx("set", &i) == 0); 7119 current_command = &command_table[i]; 7120 ret = command_table[i].func(argc, argv); 7121 } else { 7122 (void) fprintf(stderr, gettext("unrecognized " 7123 "command '%s'\n"), cmdname); 7124 usage(B_FALSE); 7125 } 7126 libzfs_mnttab_cache(g_zfs, B_FALSE); 7127 } 7128 7129 (void) fclose(mnttab_file); 7130 7131 if (ret == 0 && log_history) 7132 (void) zpool_log_history(g_zfs, history_str); 7133 7134 libzfs_fini(g_zfs); 7135 7136 /* 7137 * The 'ZFS_ABORT' environment variable causes us to dump core on exit 7138 * for the purposes of running ::findleaks. 7139 */ 7140 if (getenv("ZFS_ABORT") != NULL) { 7141 (void) printf("dumping core by request\n"); 7142 abort(); 7143 } 7144 7145 return (ret); 7146} 7147