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 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <assert.h> 30 #include <ctype.h> 31 #include <errno.h> 32 #include <libgen.h> 33 #include <libintl.h> 34 #include <libuutil.h> 35 #include <libnvpair.h> 36 #include <locale.h> 37 #include <stddef.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <strings.h> 41 #include <unistd.h> 42 #include <fcntl.h> 43 #include <zone.h> 44 #include <sys/mkdev.h> 45 #include <sys/mntent.h> 46 #include <sys/mnttab.h> 47 #include <sys/mount.h> 48 #include <sys/stat.h> 49 #include <sys/avl.h> 50 51 #include <libzfs.h> 52 #include <libuutil.h> 53 54 #include "zfs_iter.h" 55 #include "zfs_util.h" 56 57 libzfs_handle_t *g_zfs; 58 59 static FILE *mnttab_file; 60 61 static int zfs_do_clone(int argc, char **argv); 62 static int zfs_do_create(int argc, char **argv); 63 static int zfs_do_destroy(int argc, char **argv); 64 static int zfs_do_get(int argc, char **argv); 65 static int zfs_do_inherit(int argc, char **argv); 66 static int zfs_do_list(int argc, char **argv); 67 static int zfs_do_mount(int argc, char **argv); 68 static int zfs_do_rename(int argc, char **argv); 69 static int zfs_do_rollback(int argc, char **argv); 70 static int zfs_do_set(int argc, char **argv); 71 static int zfs_do_snapshot(int argc, char **argv); 72 static int zfs_do_unmount(int argc, char **argv); 73 static int zfs_do_share(int argc, char **argv); 74 static int zfs_do_unshare(int argc, char **argv); 75 static int zfs_do_send(int argc, char **argv); 76 static int zfs_do_receive(int argc, char **argv); 77 static int zfs_do_promote(int argc, char **argv); 78 static int zfs_do_allow(int argc, char **argv); 79 static int zfs_do_unallow(int argc, char **argv); 80 81 /* 82 * These libumem hooks provide a reasonable set of defaults for the allocator's 83 * debugging facilities. 84 */ 85 const char * 86 _umem_debug_init(void) 87 { 88 return ("default,verbose"); /* $UMEM_DEBUG setting */ 89 } 90 91 const char * 92 _umem_logging_init(void) 93 { 94 return ("fail,contents"); /* $UMEM_LOGGING setting */ 95 } 96 97 typedef enum { 98 HELP_CLONE, 99 HELP_CREATE, 100 HELP_DESTROY, 101 HELP_GET, 102 HELP_INHERIT, 103 HELP_LIST, 104 HELP_MOUNT, 105 HELP_PROMOTE, 106 HELP_RECEIVE, 107 HELP_RENAME, 108 HELP_ROLLBACK, 109 HELP_SEND, 110 HELP_SET, 111 HELP_SHARE, 112 HELP_SNAPSHOT, 113 HELP_UNMOUNT, 114 HELP_UNSHARE, 115 HELP_ALLOW, 116 HELP_UNALLOW 117 } zfs_help_t; 118 119 typedef struct zfs_command { 120 const char *name; 121 int (*func)(int argc, char **argv); 122 zfs_help_t usage; 123 } zfs_command_t; 124 125 /* 126 * Master command table. Each ZFS command has a name, associated function, and 127 * usage message. The usage messages need to be internationalized, so we have 128 * to have a function to return the usage message based on a command index. 129 * 130 * These commands are organized according to how they are displayed in the usage 131 * message. An empty command (one with a NULL name) indicates an empty line in 132 * the generic usage message. 133 */ 134 static zfs_command_t command_table[] = { 135 { "create", zfs_do_create, HELP_CREATE }, 136 { "destroy", zfs_do_destroy, HELP_DESTROY }, 137 { NULL }, 138 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT }, 139 { "rollback", zfs_do_rollback, HELP_ROLLBACK }, 140 { "clone", zfs_do_clone, HELP_CLONE }, 141 { "promote", zfs_do_promote, HELP_PROMOTE }, 142 { "rename", zfs_do_rename, HELP_RENAME }, 143 { NULL }, 144 { "list", zfs_do_list, HELP_LIST }, 145 { NULL }, 146 { "set", zfs_do_set, HELP_SET }, 147 { "get", zfs_do_get, HELP_GET }, 148 { "inherit", zfs_do_inherit, HELP_INHERIT }, 149 { NULL }, 150 { "mount", zfs_do_mount, HELP_MOUNT }, 151 { NULL }, 152 { "unmount", zfs_do_unmount, HELP_UNMOUNT }, 153 { NULL }, 154 { "share", zfs_do_share, HELP_SHARE }, 155 { NULL }, 156 { "unshare", zfs_do_unshare, HELP_UNSHARE }, 157 { NULL }, 158 { "send", zfs_do_send, HELP_SEND }, 159 { "receive", zfs_do_receive, HELP_RECEIVE }, 160 { NULL }, 161 { "allow", zfs_do_allow, HELP_ALLOW }, 162 { NULL }, 163 { "unallow", zfs_do_unallow, HELP_UNALLOW }, 164 }; 165 166 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0])) 167 168 zfs_command_t *current_command; 169 170 static const char * 171 get_usage(zfs_help_t idx) 172 { 173 switch (idx) { 174 case HELP_CLONE: 175 return (gettext("\tclone [-p] <snapshot> " 176 "<filesystem|volume>\n")); 177 case HELP_CREATE: 178 return (gettext("\tcreate [-p] [[-o property=value] ... ] " 179 "<filesystem>\n" 180 "\tcreate [-ps] [-b blocksize] [[-o property=value] " 181 "...]\n" 182 "\t -V <size> <volume>\n")); 183 case HELP_DESTROY: 184 return (gettext("\tdestroy [-rRf] " 185 "<filesystem|volume|snapshot>\n")); 186 case HELP_GET: 187 return (gettext("\tget [-rHp] [-o field[,field]...] " 188 "[-s source[,source]...]\n" 189 "\t <all | property[,property]...> " 190 "[filesystem|volume|snapshot] ...\n")); 191 case HELP_INHERIT: 192 return (gettext("\tinherit [-r] <property> " 193 "<filesystem|volume> ...\n")); 194 case HELP_LIST: 195 return (gettext("\tlist [-rH] [-o property[,property]...] " 196 "[-t type[,type]...]\n" 197 "\t [-s property [-s property]...]" 198 " [-S property [-S property]...]\n" 199 "\t [filesystem|volume|snapshot] ...\n")); 200 case HELP_MOUNT: 201 return (gettext("\tmount\n" 202 "\tmount [-o opts] [-O] -a\n" 203 "\tmount [-o opts] [-O] <filesystem>\n")); 204 case HELP_PROMOTE: 205 return (gettext("\tpromote <clone filesystem>\n")); 206 case HELP_RECEIVE: 207 return (gettext("\treceive [-vnF] <filesystem|volume|" 208 "snapshot>\n" 209 "\treceive [-vnF] -d <filesystem>\n")); 210 case HELP_RENAME: 211 return (gettext("\trename <filesystem|volume|snapshot> " 212 "<filesystem|volume|snapshot>\n" 213 "\trename -p <filesystem|volume> <filesystem|volume>\n" 214 "\trename -r <snapshot> <snapshot>")); 215 case HELP_ROLLBACK: 216 return (gettext("\trollback [-rRf] <snapshot>\n")); 217 case HELP_SEND: 218 return (gettext("\tsend [-i <snapshot>] <snapshot>\n")); 219 case HELP_SET: 220 return (gettext("\tset <property=value> " 221 "<filesystem|volume> ...\n")); 222 case HELP_SHARE: 223 return (gettext("\tshare -a\n" 224 "\tshare <filesystem>\n")); 225 case HELP_SNAPSHOT: 226 return (gettext("\tsnapshot [-r] " 227 "<filesystem@name|volume@name>\n")); 228 case HELP_UNMOUNT: 229 return (gettext("\tunmount [-f] -a\n" 230 "\tunmount [-f] <filesystem|mountpoint>\n")); 231 case HELP_UNSHARE: 232 return (gettext("\tunshare [-f] -a\n" 233 "\tunshare [-f] <filesystem|mountpoint>\n")); 234 case HELP_ALLOW: 235 return (gettext("\tallow [-l][-d] <everyone|user|group>[," 236 "<everyone|user|group>...]\n\t " 237 "<perm>|@<setname>[,<perm>|@<setname>...]\n\t" 238 " <filesystem|volume\n" 239 "\tallow [-l] [-d] -u <user> " 240 "<perm>|@<setname>[,<perm>|@<setname>...]\n\t" 241 " <filesystem|volume>\n" 242 "\tallow [-l] [-d] -g <group> " 243 "<perm>|@<setname>[,<perm>|@<setname>...]\n\t" 244 " <filesystem|volume>\n" 245 "\tallow [-l] [-d] -e " 246 "<perm>|@<setname>[,<perm>|@<setname>...]\n\t" 247 " <filesystem|volume>\n" 248 "\tallow -c " 249 "<perm>|@<setname>[,<perm>|@<setname>...]\n\t" 250 " <filesystem|volume>\n" 251 "\tallow -s @setname " 252 "<perm>|@<setname>[,<perm>|@<setname>...]\n\t" 253 " <filesystem|volume>\n")); 254 255 case HELP_UNALLOW: 256 return (gettext("\tunallow [-r][-l][-d] <everyone|user|group>[," 257 "<everyone|user|group>...] \n\t " 258 "[<perm>|@<setname>[,<perm>|@<setname>...]]\n\t" 259 " <filesystem|volume>\n" 260 "\tunallow [-r][-l][-d] -u user " 261 "[<perm>|@<setname>[,<perm>|@<setname>...]]\n\t" 262 " <filesystem|volume>\n" 263 "\tunallow [-r][-l][-d] -g group " 264 "[<perm>|@<setname>[,<perm>|@<setname>...]]\n\t" 265 " <filesystem|volume>\n" 266 "\tunallow [-r][-l][-d] -e " 267 "[<perm>|@<setname>[,<perm>|@<setname>...]]\n\t" 268 " <filesystem|volume>\n" 269 "\tunallow [-r] -c " 270 "[<perm>|@<setname>[,<perm>|@<setname>...]]\n\t" 271 " <filesystem|volume>\n" 272 "\tunallow [-r] -s @setname " 273 "[<perm>|@<setname>[,<perm>|@<setname>...]]\n\t" 274 " <filesystem|volume> \n\t")); 275 } 276 277 abort(); 278 /* NOTREACHED */ 279 } 280 281 /* 282 * Utility function to guarantee malloc() success. 283 */ 284 void * 285 safe_malloc(size_t size) 286 { 287 void *data; 288 289 if ((data = calloc(1, size)) == NULL) { 290 (void) fprintf(stderr, "internal error: out of memory\n"); 291 exit(1); 292 } 293 294 return (data); 295 } 296 297 /* 298 * Callback routinue that will print out information for each of the 299 * the properties. 300 */ 301 static zfs_prop_t 302 usage_prop_cb(zfs_prop_t prop, void *cb) 303 { 304 FILE *fp = cb; 305 306 (void) fprintf(fp, "\t%-13s ", zfs_prop_to_name(prop)); 307 308 if (zfs_prop_readonly(prop)) 309 (void) fprintf(fp, " NO "); 310 else 311 (void) fprintf(fp, " YES "); 312 313 if (zfs_prop_inheritable(prop)) 314 (void) fprintf(fp, " YES "); 315 else 316 (void) fprintf(fp, " NO "); 317 318 if (zfs_prop_values(prop) == NULL) 319 (void) fprintf(fp, "-\n"); 320 else 321 (void) fprintf(fp, "%s\n", zfs_prop_values(prop)); 322 323 return (ZFS_PROP_CONT); 324 } 325 326 /* 327 * Display usage message. If we're inside a command, display only the usage for 328 * that command. Otherwise, iterate over the entire command table and display 329 * a complete usage message. 330 */ 331 static void 332 usage(boolean_t requested) 333 { 334 int i; 335 boolean_t show_properties = B_FALSE; 336 FILE *fp = requested ? stdout : stderr; 337 338 if (current_command == NULL) { 339 340 (void) fprintf(fp, gettext("usage: zfs command args ...\n")); 341 (void) fprintf(fp, 342 gettext("where 'command' is one of the following:\n\n")); 343 344 for (i = 0; i < NCOMMAND; i++) { 345 if (command_table[i].name == NULL) 346 (void) fprintf(fp, "\n"); 347 else 348 (void) fprintf(fp, "%s", 349 get_usage(command_table[i].usage)); 350 } 351 352 (void) fprintf(fp, gettext("\nEach dataset is of the form: " 353 "pool/[dataset/]*dataset[@name]\n")); 354 } else { 355 (void) fprintf(fp, gettext("usage:\n")); 356 (void) fprintf(fp, "%s", get_usage(current_command->usage)); 357 } 358 359 if (current_command != NULL && 360 (strcmp(current_command->name, "set") == 0 || 361 strcmp(current_command->name, "get") == 0 || 362 strcmp(current_command->name, "inherit") == 0 || 363 strcmp(current_command->name, "list") == 0)) 364 show_properties = B_TRUE; 365 366 if (show_properties) { 367 368 (void) fprintf(fp, 369 gettext("\nThe following properties are supported:\n")); 370 371 (void) fprintf(fp, "\n\t%-13s %s %s %s\n\n", 372 "PROPERTY", "EDIT", "INHERIT", "VALUES"); 373 374 /* Iterate over all properties */ 375 (void) zfs_prop_iter(usage_prop_cb, fp, B_FALSE); 376 377 (void) fprintf(fp, gettext("\nSizes are specified in bytes " 378 "with standard units such as K, M, G, etc.\n")); 379 (void) fprintf(fp, gettext("\n\nUser-defined properties can " 380 "be specified by using a name containing a colon (:).\n")); 381 } else { 382 /* 383 * TRANSLATION NOTE: 384 * "zfs set|get" must not be localised this is the 385 * command name and arguments. 386 */ 387 (void) fprintf(fp, 388 gettext("\nFor the property list, run: zfs set|get\n")); 389 } 390 391 /* 392 * See comments at end of main(). 393 */ 394 if (getenv("ZFS_ABORT") != NULL) { 395 (void) printf("dumping core by request\n"); 396 abort(); 397 } 398 399 exit(requested ? 0 : 2); 400 } 401 402 /* 403 * zfs clone [-p] <snap> <fs | vol> 404 * 405 * Given an existing dataset, create a writable copy whose initial contents 406 * are the same as the source. The newly created dataset maintains a 407 * dependency on the original; the original cannot be destroyed so long as 408 * the clone exists. 409 * 410 * The '-p' flag creates all the non-existing ancestors of the target first. 411 */ 412 static int 413 zfs_do_clone(int argc, char **argv) 414 { 415 zfs_handle_t *zhp; 416 boolean_t parents = B_FALSE; 417 int ret; 418 int c; 419 420 /* check options */ 421 while ((c = getopt(argc, argv, "p")) != -1) { 422 switch (c) { 423 case 'p': 424 parents = B_TRUE; 425 break; 426 case '?': 427 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 428 optopt); 429 usage(B_FALSE); 430 } 431 } 432 433 argc -= optind; 434 argv += optind; 435 436 /* check number of arguments */ 437 if (argc < 1) { 438 (void) fprintf(stderr, gettext("missing source dataset " 439 "argument\n")); 440 usage(B_FALSE); 441 } 442 if (argc < 2) { 443 (void) fprintf(stderr, gettext("missing target dataset " 444 "argument\n")); 445 usage(B_FALSE); 446 } 447 if (argc > 2) { 448 (void) fprintf(stderr, gettext("too many arguments\n")); 449 usage(B_FALSE); 450 } 451 452 /* open the source dataset */ 453 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 454 return (1); 455 456 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM | 457 ZFS_TYPE_VOLUME)) { 458 /* 459 * Now create the ancestors of the target dataset. If the 460 * target already exists and '-p' option was used we should not 461 * complain. 462 */ 463 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | 464 ZFS_TYPE_VOLUME)) 465 return (0); 466 if (zfs_create_ancestors(g_zfs, argv[1]) != 0) 467 return (1); 468 } 469 470 /* pass to libzfs */ 471 ret = zfs_clone(zhp, argv[1], NULL); 472 473 /* create the mountpoint if necessary */ 474 if (ret == 0) { 475 zfs_handle_t *clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_ANY); 476 if (clone != NULL) { 477 if ((ret = zfs_mount(clone, NULL, 0)) == 0) 478 ret = zfs_share(clone); 479 zfs_close(clone); 480 } 481 } 482 483 zfs_close(zhp); 484 485 return (ret == 0 ? 0 : 1); 486 } 487 488 /* 489 * zfs create [-p] [-o prop=value] ... fs 490 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size 491 * 492 * Create a new dataset. This command can be used to create filesystems 493 * and volumes. Snapshot creation is handled by 'zfs snapshot'. 494 * For volumes, the user must specify a size to be used. 495 * 496 * The '-s' flag applies only to volumes, and indicates that we should not try 497 * to set the reservation for this volume. By default we set a reservation 498 * equal to the size for any volume. 499 * 500 * The '-p' flag creates all the non-existing ancestors of the target first. 501 */ 502 static int 503 zfs_do_create(int argc, char **argv) 504 { 505 zfs_type_t type = ZFS_TYPE_FILESYSTEM; 506 zfs_handle_t *zhp = NULL; 507 uint64_t volsize; 508 int c; 509 boolean_t noreserve = B_FALSE; 510 boolean_t parents = B_FALSE; 511 int ret = 1; 512 nvlist_t *props = NULL; 513 uint64_t intval; 514 char *propname; 515 char *propval = NULL; 516 char *strval; 517 518 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) { 519 (void) fprintf(stderr, gettext("internal error: " 520 "out of memory\n")); 521 return (1); 522 } 523 524 /* check options */ 525 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) { 526 switch (c) { 527 case 'V': 528 type = ZFS_TYPE_VOLUME; 529 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) { 530 (void) fprintf(stderr, gettext("bad volume " 531 "size '%s': %s\n"), optarg, 532 libzfs_error_description(g_zfs)); 533 goto error; 534 } 535 536 if (nvlist_add_uint64(props, 537 zfs_prop_to_name(ZFS_PROP_VOLSIZE), 538 intval) != 0) { 539 (void) fprintf(stderr, gettext("internal " 540 "error: out of memory\n")); 541 goto error; 542 } 543 volsize = intval; 544 break; 545 case 'p': 546 parents = B_TRUE; 547 break; 548 case 'b': 549 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) { 550 (void) fprintf(stderr, gettext("bad volume " 551 "block size '%s': %s\n"), optarg, 552 libzfs_error_description(g_zfs)); 553 goto error; 554 } 555 556 if (nvlist_add_uint64(props, 557 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 558 intval) != 0) { 559 (void) fprintf(stderr, gettext("internal " 560 "error: out of memory\n")); 561 goto error; 562 } 563 break; 564 case 'o': 565 propname = optarg; 566 if ((propval = strchr(propname, '=')) == NULL) { 567 (void) fprintf(stderr, gettext("missing " 568 "'=' for -o option\n")); 569 goto error; 570 } 571 *propval = '\0'; 572 propval++; 573 if (nvlist_lookup_string(props, propname, 574 &strval) == 0) { 575 (void) fprintf(stderr, gettext("property '%s' " 576 "specified multiple times\n"), propname); 577 goto error; 578 } 579 if (nvlist_add_string(props, propname, propval) != 0) { 580 (void) fprintf(stderr, gettext("internal " 581 "error: out of memory\n")); 582 goto error; 583 } 584 break; 585 case 's': 586 noreserve = B_TRUE; 587 break; 588 case ':': 589 (void) fprintf(stderr, gettext("missing size " 590 "argument\n")); 591 goto badusage; 592 break; 593 case '?': 594 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 595 optopt); 596 goto badusage; 597 } 598 } 599 600 if (noreserve && type != ZFS_TYPE_VOLUME) { 601 (void) fprintf(stderr, gettext("'-s' can only be used when " 602 "creating a volume\n")); 603 goto badusage; 604 } 605 606 argc -= optind; 607 argv += optind; 608 609 /* check number of arguments */ 610 if (argc == 0) { 611 (void) fprintf(stderr, gettext("missing %s argument\n"), 612 zfs_type_to_name(type)); 613 goto badusage; 614 } 615 if (argc > 1) { 616 (void) fprintf(stderr, gettext("too many arguments\n")); 617 goto badusage; 618 } 619 620 if (type == ZFS_TYPE_VOLUME && !noreserve && 621 nvlist_lookup_string(props, zfs_prop_to_name(ZFS_PROP_RESERVATION), 622 &strval) != 0) { 623 if (nvlist_add_uint64(props, 624 zfs_prop_to_name(ZFS_PROP_RESERVATION), 625 volsize) != 0) { 626 (void) fprintf(stderr, gettext("internal " 627 "error: out of memory\n")); 628 nvlist_free(props); 629 return (1); 630 } 631 } 632 633 if (parents && zfs_name_valid(argv[0], type)) { 634 /* 635 * Now create the ancestors of target dataset. If the target 636 * already exists and '-p' option was used we should not 637 * complain. 638 */ 639 if (zfs_dataset_exists(g_zfs, argv[0], type)) { 640 ret = 0; 641 goto error; 642 } 643 if (zfs_create_ancestors(g_zfs, argv[0]) != 0) 644 goto error; 645 } 646 647 /* pass to libzfs */ 648 if (zfs_create(g_zfs, argv[0], type, props) != 0) 649 goto error; 650 651 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_ANY)) == NULL) 652 goto error; 653 654 /* 655 * Mount and/or share the new filesystem as appropriate. We provide a 656 * verbose error message to let the user know that their filesystem was 657 * in fact created, even if we failed to mount or share it. 658 */ 659 if (zfs_mount(zhp, NULL, 0) != 0) { 660 (void) fprintf(stderr, gettext("filesystem successfully " 661 "created, but not mounted\n")); 662 ret = 1; 663 } else if (zfs_share(zhp) != 0) { 664 (void) fprintf(stderr, gettext("filesystem successfully " 665 "created, but not shared\n")); 666 ret = 1; 667 } else { 668 ret = 0; 669 } 670 671 error: 672 if (zhp) 673 zfs_close(zhp); 674 nvlist_free(props); 675 return (ret); 676 badusage: 677 nvlist_free(props); 678 usage(B_FALSE); 679 return (2); 680 } 681 682 /* 683 * zfs destroy [-rf] <fs, snap, vol> 684 * 685 * -r Recursively destroy all children 686 * -R Recursively destroy all dependents, including clones 687 * -f Force unmounting of any dependents 688 * 689 * Destroys the given dataset. By default, it will unmount any filesystems, 690 * and refuse to destroy a dataset that has any dependents. A dependent can 691 * either be a child, or a clone of a child. 692 */ 693 typedef struct destroy_cbdata { 694 boolean_t cb_first; 695 int cb_force; 696 int cb_recurse; 697 int cb_error; 698 int cb_needforce; 699 int cb_doclones; 700 boolean_t cb_closezhp; 701 zfs_handle_t *cb_target; 702 char *cb_snapname; 703 } destroy_cbdata_t; 704 705 /* 706 * Check for any dependents based on the '-r' or '-R' flags. 707 */ 708 static int 709 destroy_check_dependent(zfs_handle_t *zhp, void *data) 710 { 711 destroy_cbdata_t *cbp = data; 712 const char *tname = zfs_get_name(cbp->cb_target); 713 const char *name = zfs_get_name(zhp); 714 715 if (strncmp(tname, name, strlen(tname)) == 0 && 716 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) { 717 /* 718 * This is a direct descendant, not a clone somewhere else in 719 * the hierarchy. 720 */ 721 if (cbp->cb_recurse) 722 goto out; 723 724 if (cbp->cb_first) { 725 (void) fprintf(stderr, gettext("cannot destroy '%s': " 726 "%s has children\n"), 727 zfs_get_name(cbp->cb_target), 728 zfs_type_to_name(zfs_get_type(cbp->cb_target))); 729 (void) fprintf(stderr, gettext("use '-r' to destroy " 730 "the following datasets:\n")); 731 cbp->cb_first = B_FALSE; 732 cbp->cb_error = 1; 733 } 734 735 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 736 } else { 737 /* 738 * This is a clone. We only want to report this if the '-r' 739 * wasn't specified, or the target is a snapshot. 740 */ 741 if (!cbp->cb_recurse && 742 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT) 743 goto out; 744 745 if (cbp->cb_first) { 746 (void) fprintf(stderr, gettext("cannot destroy '%s': " 747 "%s has dependent clones\n"), 748 zfs_get_name(cbp->cb_target), 749 zfs_type_to_name(zfs_get_type(cbp->cb_target))); 750 (void) fprintf(stderr, gettext("use '-R' to destroy " 751 "the following datasets:\n")); 752 cbp->cb_first = B_FALSE; 753 cbp->cb_error = 1; 754 } 755 756 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 757 } 758 759 out: 760 zfs_close(zhp); 761 return (0); 762 } 763 764 static int 765 destroy_callback(zfs_handle_t *zhp, void *data) 766 { 767 destroy_cbdata_t *cbp = data; 768 769 /* 770 * Ignore pools (which we've already flagged as an error before getting 771 * here. 772 */ 773 if (strchr(zfs_get_name(zhp), '/') == NULL && 774 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 775 zfs_close(zhp); 776 return (0); 777 } 778 779 /* 780 * Bail out on the first error. 781 */ 782 if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 || 783 zfs_destroy(zhp) != 0) { 784 zfs_close(zhp); 785 return (-1); 786 } 787 788 zfs_close(zhp); 789 return (0); 790 } 791 792 static int 793 destroy_snap_clones(zfs_handle_t *zhp, void *arg) 794 { 795 destroy_cbdata_t *cbp = arg; 796 char thissnap[MAXPATHLEN]; 797 zfs_handle_t *szhp; 798 boolean_t closezhp = cbp->cb_closezhp; 799 int rv; 800 801 (void) snprintf(thissnap, sizeof (thissnap), 802 "%s@%s", zfs_get_name(zhp), cbp->cb_snapname); 803 804 libzfs_print_on_error(g_zfs, B_FALSE); 805 szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT); 806 libzfs_print_on_error(g_zfs, B_TRUE); 807 if (szhp) { 808 /* 809 * Destroy any clones of this snapshot 810 */ 811 if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback, 812 cbp) != 0) { 813 zfs_close(szhp); 814 if (closezhp) 815 zfs_close(zhp); 816 return (-1); 817 } 818 zfs_close(szhp); 819 } 820 821 cbp->cb_closezhp = B_TRUE; 822 rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg); 823 if (closezhp) 824 zfs_close(zhp); 825 return (rv); 826 } 827 828 static int 829 zfs_do_destroy(int argc, char **argv) 830 { 831 destroy_cbdata_t cb = { 0 }; 832 int c; 833 zfs_handle_t *zhp; 834 char *cp; 835 836 /* check options */ 837 while ((c = getopt(argc, argv, "frR")) != -1) { 838 switch (c) { 839 case 'f': 840 cb.cb_force = 1; 841 break; 842 case 'r': 843 cb.cb_recurse = 1; 844 break; 845 case 'R': 846 cb.cb_recurse = 1; 847 cb.cb_doclones = 1; 848 break; 849 case '?': 850 default: 851 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 852 optopt); 853 usage(B_FALSE); 854 } 855 } 856 857 argc -= optind; 858 argv += optind; 859 860 /* check number of arguments */ 861 if (argc == 0) { 862 (void) fprintf(stderr, gettext("missing path argument\n")); 863 usage(B_FALSE); 864 } 865 if (argc > 1) { 866 (void) fprintf(stderr, gettext("too many arguments\n")); 867 usage(B_FALSE); 868 } 869 870 /* 871 * If we are doing recursive destroy of a snapshot, then the 872 * named snapshot may not exist. Go straight to libzfs. 873 */ 874 if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) { 875 int ret; 876 877 *cp = '\0'; 878 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_ANY)) == NULL) 879 return (1); 880 *cp = '@'; 881 cp++; 882 883 if (cb.cb_doclones) { 884 cb.cb_snapname = cp; 885 if (destroy_snap_clones(zhp, &cb) != 0) { 886 zfs_close(zhp); 887 return (1); 888 } 889 } 890 891 ret = zfs_destroy_snaps(zhp, cp); 892 zfs_close(zhp); 893 if (ret) { 894 (void) fprintf(stderr, 895 gettext("no snapshots destroyed\n")); 896 } 897 return (ret != 0); 898 } 899 900 901 /* Open the given dataset */ 902 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_ANY)) == NULL) 903 return (1); 904 905 cb.cb_target = zhp; 906 907 /* 908 * Perform an explicit check for pools before going any further. 909 */ 910 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL && 911 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 912 (void) fprintf(stderr, gettext("cannot destroy '%s': " 913 "operation does not apply to pools\n"), 914 zfs_get_name(zhp)); 915 (void) fprintf(stderr, gettext("use 'zfs destroy -r " 916 "%s' to destroy all datasets in the pool\n"), 917 zfs_get_name(zhp)); 918 (void) fprintf(stderr, gettext("use 'zpool destroy %s' " 919 "to destroy the pool itself\n"), zfs_get_name(zhp)); 920 zfs_close(zhp); 921 return (1); 922 } 923 924 /* 925 * Check for any dependents and/or clones. 926 */ 927 cb.cb_first = B_TRUE; 928 if (!cb.cb_doclones && 929 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent, 930 &cb) != 0) { 931 zfs_close(zhp); 932 return (1); 933 } 934 935 if (cb.cb_error || 936 zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0) { 937 zfs_close(zhp); 938 return (1); 939 } 940 941 /* 942 * Do the real thing. The callback will close the handle regardless of 943 * whether it succeeds or not. 944 */ 945 946 if (destroy_callback(zhp, &cb) != 0) 947 return (1); 948 949 950 return (0); 951 } 952 953 /* 954 * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...] 955 * < all | property[,property]... > < fs | snap | vol > ... 956 * 957 * -r recurse over any child datasets 958 * -H scripted mode. Headers are stripped, and fields are separated 959 * by tabs instead of spaces. 960 * -o Set of fields to display. One of "name,property,value,source". 961 * Default is all four. 962 * -s Set of sources to allow. One of 963 * "local,default,inherited,temporary,none". Default is all 964 * five. 965 * -p Display values in parsable (literal) format. 966 * 967 * Prints properties for the given datasets. The user can control which 968 * columns to display as well as which property types to allow. 969 */ 970 971 /* 972 * Invoked to display the properties for a single dataset. 973 */ 974 static int 975 get_callback(zfs_handle_t *zhp, void *data) 976 { 977 char buf[ZFS_MAXPROPLEN]; 978 zfs_source_t sourcetype; 979 char source[ZFS_MAXNAMELEN]; 980 libzfs_get_cbdata_t *cbp = data; 981 nvlist_t *userprop = zfs_get_user_props(zhp); 982 zfs_proplist_t *pl = cbp->cb_proplist; 983 nvlist_t *propval; 984 char *strval; 985 char *sourceval; 986 987 for (; pl != NULL; pl = pl->pl_next) { 988 /* 989 * Skip the special fake placeholder. This will also skip over 990 * the name property when 'all' is specified. 991 */ 992 if (pl->pl_prop == ZFS_PROP_NAME && 993 pl == cbp->cb_proplist) 994 continue; 995 996 if (pl->pl_prop != ZFS_PROP_INVAL) { 997 if (zfs_prop_get(zhp, pl->pl_prop, buf, 998 sizeof (buf), &sourcetype, source, 999 sizeof (source), 1000 cbp->cb_literal) != 0) { 1001 if (pl->pl_all) 1002 continue; 1003 if (!zfs_prop_valid_for_type(pl->pl_prop, 1004 ZFS_TYPE_ANY)) { 1005 (void) fprintf(stderr, 1006 gettext("No such property '%s'\n"), 1007 zfs_prop_to_name(pl->pl_prop)); 1008 continue; 1009 } 1010 sourcetype = ZFS_SRC_NONE; 1011 (void) strlcpy(buf, "-", sizeof (buf)); 1012 } 1013 1014 libzfs_print_one_property(zfs_get_name(zhp), cbp, 1015 zfs_prop_to_name(pl->pl_prop), 1016 buf, sourcetype, source); 1017 } else { 1018 if (nvlist_lookup_nvlist(userprop, 1019 pl->pl_user_prop, &propval) != 0) { 1020 if (pl->pl_all) 1021 continue; 1022 sourcetype = ZFS_SRC_NONE; 1023 strval = "-"; 1024 } else { 1025 verify(nvlist_lookup_string(propval, 1026 ZFS_PROP_VALUE, &strval) == 0); 1027 verify(nvlist_lookup_string(propval, 1028 ZFS_PROP_SOURCE, &sourceval) == 0); 1029 1030 if (strcmp(sourceval, 1031 zfs_get_name(zhp)) == 0) { 1032 sourcetype = ZFS_SRC_LOCAL; 1033 } else { 1034 sourcetype = ZFS_SRC_INHERITED; 1035 (void) strlcpy(source, 1036 sourceval, sizeof (source)); 1037 } 1038 } 1039 1040 libzfs_print_one_property(zfs_get_name(zhp), cbp, 1041 pl->pl_user_prop, strval, sourcetype, 1042 source); 1043 } 1044 } 1045 1046 return (0); 1047 } 1048 1049 static int 1050 zfs_do_get(int argc, char **argv) 1051 { 1052 libzfs_get_cbdata_t cb = { 0 }; 1053 boolean_t recurse = B_FALSE; 1054 int i, c; 1055 char *value, *fields; 1056 int ret; 1057 zfs_proplist_t fake_name = { 0 }; 1058 1059 /* 1060 * Set up default columns and sources. 1061 */ 1062 cb.cb_sources = ZFS_SRC_ALL; 1063 cb.cb_columns[0] = GET_COL_NAME; 1064 cb.cb_columns[1] = GET_COL_PROPERTY; 1065 cb.cb_columns[2] = GET_COL_VALUE; 1066 cb.cb_columns[3] = GET_COL_SOURCE; 1067 1068 /* check options */ 1069 while ((c = getopt(argc, argv, ":o:s:rHp")) != -1) { 1070 switch (c) { 1071 case 'p': 1072 cb.cb_literal = B_TRUE; 1073 break; 1074 case 'r': 1075 recurse = B_TRUE; 1076 break; 1077 case 'H': 1078 cb.cb_scripted = B_TRUE; 1079 break; 1080 case ':': 1081 (void) fprintf(stderr, gettext("missing argument for " 1082 "'%c' option\n"), optopt); 1083 usage(B_FALSE); 1084 break; 1085 case 'o': 1086 /* 1087 * Process the set of columns to display. We zero out 1088 * the structure to give us a blank slate. 1089 */ 1090 bzero(&cb.cb_columns, sizeof (cb.cb_columns)); 1091 i = 0; 1092 while (*optarg != '\0') { 1093 static char *col_subopts[] = 1094 { "name", "property", "value", "source", 1095 NULL }; 1096 1097 if (i == 4) { 1098 (void) fprintf(stderr, gettext("too " 1099 "many fields given to -o " 1100 "option\n")); 1101 usage(B_FALSE); 1102 } 1103 1104 switch (getsubopt(&optarg, col_subopts, 1105 &value)) { 1106 case 0: 1107 cb.cb_columns[i++] = GET_COL_NAME; 1108 break; 1109 case 1: 1110 cb.cb_columns[i++] = GET_COL_PROPERTY; 1111 break; 1112 case 2: 1113 cb.cb_columns[i++] = GET_COL_VALUE; 1114 break; 1115 case 3: 1116 cb.cb_columns[i++] = GET_COL_SOURCE; 1117 break; 1118 default: 1119 (void) fprintf(stderr, 1120 gettext("invalid column name " 1121 "'%s'\n"), value); 1122 usage(B_FALSE); 1123 } 1124 } 1125 break; 1126 1127 case 's': 1128 cb.cb_sources = 0; 1129 while (*optarg != '\0') { 1130 static char *source_subopts[] = { 1131 "local", "default", "inherited", 1132 "temporary", "none", NULL }; 1133 1134 switch (getsubopt(&optarg, source_subopts, 1135 &value)) { 1136 case 0: 1137 cb.cb_sources |= ZFS_SRC_LOCAL; 1138 break; 1139 case 1: 1140 cb.cb_sources |= ZFS_SRC_DEFAULT; 1141 break; 1142 case 2: 1143 cb.cb_sources |= ZFS_SRC_INHERITED; 1144 break; 1145 case 3: 1146 cb.cb_sources |= ZFS_SRC_TEMPORARY; 1147 break; 1148 case 4: 1149 cb.cb_sources |= ZFS_SRC_NONE; 1150 break; 1151 default: 1152 (void) fprintf(stderr, 1153 gettext("invalid source " 1154 "'%s'\n"), value); 1155 usage(B_FALSE); 1156 } 1157 } 1158 break; 1159 1160 case '?': 1161 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1162 optopt); 1163 usage(B_FALSE); 1164 } 1165 } 1166 1167 argc -= optind; 1168 argv += optind; 1169 1170 if (argc < 1) { 1171 (void) fprintf(stderr, gettext("missing property " 1172 "argument\n")); 1173 usage(B_FALSE); 1174 } 1175 1176 fields = argv[0]; 1177 1178 if (zfs_get_proplist(g_zfs, fields, &cb.cb_proplist) != 0) 1179 usage(B_FALSE); 1180 1181 argc--; 1182 argv++; 1183 1184 /* 1185 * As part of zfs_expand_proplist(), we keep track of the maximum column 1186 * width for each property. For the 'NAME' (and 'SOURCE') columns, we 1187 * need to know the maximum name length. However, the user likely did 1188 * not specify 'name' as one of the properties to fetch, so we need to 1189 * make sure we always include at least this property for 1190 * print_get_headers() to work properly. 1191 */ 1192 if (cb.cb_proplist != NULL) { 1193 fake_name.pl_prop = ZFS_PROP_NAME; 1194 fake_name.pl_width = strlen(gettext("NAME")); 1195 fake_name.pl_next = cb.cb_proplist; 1196 cb.cb_proplist = &fake_name; 1197 } 1198 1199 cb.cb_first = B_TRUE; 1200 1201 /* run for each object */ 1202 ret = zfs_for_each(argc, argv, recurse, ZFS_TYPE_ANY, NULL, 1203 &cb.cb_proplist, get_callback, &cb, B_FALSE); 1204 1205 if (cb.cb_proplist == &fake_name) 1206 zfs_free_proplist(fake_name.pl_next); 1207 else 1208 zfs_free_proplist(cb.cb_proplist); 1209 1210 return (ret); 1211 } 1212 1213 /* 1214 * inherit [-r] <property> <fs|vol> ... 1215 * 1216 * -r Recurse over all children 1217 * 1218 * For each dataset specified on the command line, inherit the given property 1219 * from its parent. Inheriting a property at the pool level will cause it to 1220 * use the default value. The '-r' flag will recurse over all children, and is 1221 * useful for setting a property on a hierarchy-wide basis, regardless of any 1222 * local modifications for each dataset. 1223 */ 1224 1225 static int 1226 inherit_callback(zfs_handle_t *zhp, void *data) 1227 { 1228 char *propname = data; 1229 int ret; 1230 1231 ret = zfs_prop_inherit(zhp, propname); 1232 return (ret != 0); 1233 } 1234 1235 static int 1236 zfs_do_inherit(int argc, char **argv) 1237 { 1238 boolean_t recurse = B_FALSE; 1239 int c; 1240 zfs_prop_t prop; 1241 char *propname; 1242 int ret; 1243 1244 /* check options */ 1245 while ((c = getopt(argc, argv, "r")) != -1) { 1246 switch (c) { 1247 case 'r': 1248 recurse = B_TRUE; 1249 break; 1250 case '?': 1251 default: 1252 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1253 optopt); 1254 usage(B_FALSE); 1255 } 1256 } 1257 1258 argc -= optind; 1259 argv += optind; 1260 1261 /* check number of arguments */ 1262 if (argc < 1) { 1263 (void) fprintf(stderr, gettext("missing property argument\n")); 1264 usage(B_FALSE); 1265 } 1266 if (argc < 2) { 1267 (void) fprintf(stderr, gettext("missing dataset argument\n")); 1268 usage(B_FALSE); 1269 } 1270 1271 propname = argv[0]; 1272 argc--; 1273 argv++; 1274 1275 if ((prop = zfs_name_to_prop(propname)) != ZFS_PROP_INVAL) { 1276 if (zfs_prop_readonly(prop)) { 1277 (void) fprintf(stderr, gettext( 1278 "%s property is read-only\n"), 1279 propname); 1280 return (1); 1281 } 1282 if (!zfs_prop_inheritable(prop)) { 1283 (void) fprintf(stderr, gettext("'%s' property cannot " 1284 "be inherited\n"), propname); 1285 if (prop == ZFS_PROP_QUOTA || 1286 prop == ZFS_PROP_RESERVATION) 1287 (void) fprintf(stderr, gettext("use 'zfs set " 1288 "%s=none' to clear\n"), propname); 1289 return (1); 1290 } 1291 } else if (!zfs_prop_user(propname)) { 1292 (void) fprintf(stderr, gettext("invalid property '%s'\n"), 1293 propname); 1294 usage(B_FALSE); 1295 } 1296 1297 ret = zfs_for_each(argc, argv, recurse, 1298 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL, 1299 inherit_callback, propname, B_FALSE); 1300 1301 return (ret); 1302 } 1303 1304 /* 1305 * list [-rH] [-o property[,property]...] [-t type[,type]...] 1306 * [-s property [-s property]...] [-S property [-S property]...] 1307 * <dataset> ... 1308 * 1309 * -r Recurse over all children 1310 * -H Scripted mode; elide headers and separate colums by tabs 1311 * -o Control which fields to display. 1312 * -t Control which object types to display. 1313 * -s Specify sort columns, descending order. 1314 * -S Specify sort columns, ascending order. 1315 * 1316 * When given no arguments, lists all filesystems in the system. 1317 * Otherwise, list the specified datasets, optionally recursing down them if 1318 * '-r' is specified. 1319 */ 1320 typedef struct list_cbdata { 1321 boolean_t cb_first; 1322 boolean_t cb_scripted; 1323 zfs_proplist_t *cb_proplist; 1324 } list_cbdata_t; 1325 1326 /* 1327 * Given a list of columns to display, output appropriate headers for each one. 1328 */ 1329 static void 1330 print_header(zfs_proplist_t *pl) 1331 { 1332 char headerbuf[ZFS_MAXPROPLEN]; 1333 const char *header; 1334 int i; 1335 boolean_t first = B_TRUE; 1336 boolean_t right_justify; 1337 1338 for (; pl != NULL; pl = pl->pl_next) { 1339 if (!first) { 1340 (void) printf(" "); 1341 } else { 1342 first = B_FALSE; 1343 } 1344 1345 right_justify = B_FALSE; 1346 if (pl->pl_prop != ZFS_PROP_INVAL) { 1347 header = zfs_prop_column_name(pl->pl_prop); 1348 right_justify = zfs_prop_align_right(pl->pl_prop); 1349 } else { 1350 for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 1351 headerbuf[i] = toupper(pl->pl_user_prop[i]); 1352 headerbuf[i] = '\0'; 1353 header = headerbuf; 1354 } 1355 1356 if (pl->pl_next == NULL && !right_justify) 1357 (void) printf("%s", header); 1358 else if (right_justify) 1359 (void) printf("%*s", pl->pl_width, header); 1360 else 1361 (void) printf("%-*s", pl->pl_width, header); 1362 } 1363 1364 (void) printf("\n"); 1365 } 1366 1367 /* 1368 * Given a dataset and a list of fields, print out all the properties according 1369 * to the described layout. 1370 */ 1371 static void 1372 print_dataset(zfs_handle_t *zhp, zfs_proplist_t *pl, int scripted) 1373 { 1374 boolean_t first = B_TRUE; 1375 char property[ZFS_MAXPROPLEN]; 1376 nvlist_t *userprops = zfs_get_user_props(zhp); 1377 nvlist_t *propval; 1378 char *propstr; 1379 boolean_t right_justify; 1380 int width; 1381 1382 for (; pl != NULL; pl = pl->pl_next) { 1383 if (!first) { 1384 if (scripted) 1385 (void) printf("\t"); 1386 else 1387 (void) printf(" "); 1388 } else { 1389 first = B_FALSE; 1390 } 1391 1392 right_justify = B_FALSE; 1393 if (pl->pl_prop != ZFS_PROP_INVAL) { 1394 if (zfs_prop_get(zhp, pl->pl_prop, property, 1395 sizeof (property), NULL, NULL, 0, B_FALSE) != 0) 1396 propstr = "-"; 1397 else 1398 propstr = property; 1399 1400 right_justify = zfs_prop_align_right(pl->pl_prop); 1401 } else { 1402 if (nvlist_lookup_nvlist(userprops, 1403 pl->pl_user_prop, &propval) != 0) 1404 propstr = "-"; 1405 else 1406 verify(nvlist_lookup_string(propval, 1407 ZFS_PROP_VALUE, &propstr) == 0); 1408 } 1409 1410 width = pl->pl_width; 1411 1412 /* 1413 * If this is being called in scripted mode, or if this is the 1414 * last column and it is left-justified, don't include a width 1415 * format specifier. 1416 */ 1417 if (scripted || (pl->pl_next == NULL && !right_justify)) 1418 (void) printf("%s", propstr); 1419 else if (right_justify) 1420 (void) printf("%*s", width, propstr); 1421 else 1422 (void) printf("%-*s", width, propstr); 1423 } 1424 1425 (void) printf("\n"); 1426 } 1427 1428 /* 1429 * Generic callback function to list a dataset or snapshot. 1430 */ 1431 static int 1432 list_callback(zfs_handle_t *zhp, void *data) 1433 { 1434 list_cbdata_t *cbp = data; 1435 1436 if (cbp->cb_first) { 1437 if (!cbp->cb_scripted) 1438 print_header(cbp->cb_proplist); 1439 cbp->cb_first = B_FALSE; 1440 } 1441 1442 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted); 1443 1444 return (0); 1445 } 1446 1447 static int 1448 zfs_do_list(int argc, char **argv) 1449 { 1450 int c; 1451 boolean_t recurse = B_FALSE; 1452 boolean_t scripted = B_FALSE; 1453 static char default_fields[] = 1454 "name,used,available,referenced,mountpoint"; 1455 int types = ZFS_TYPE_ANY; 1456 char *fields = NULL; 1457 char *basic_fields = default_fields; 1458 list_cbdata_t cb = { 0 }; 1459 char *value; 1460 int ret; 1461 char *type_subopts[] = { "filesystem", "volume", "snapshot", NULL }; 1462 zfs_sort_column_t *sortcol = NULL; 1463 1464 /* check options */ 1465 while ((c = getopt(argc, argv, ":o:rt:Hs:S:")) != -1) { 1466 switch (c) { 1467 case 'o': 1468 fields = optarg; 1469 break; 1470 case 'r': 1471 recurse = B_TRUE; 1472 break; 1473 case 'H': 1474 scripted = B_TRUE; 1475 break; 1476 case 's': 1477 if (zfs_add_sort_column(&sortcol, optarg, 1478 B_FALSE) != 0) { 1479 (void) fprintf(stderr, 1480 gettext("invalid property '%s'\n"), optarg); 1481 usage(B_FALSE); 1482 } 1483 break; 1484 case 'S': 1485 if (zfs_add_sort_column(&sortcol, optarg, 1486 B_TRUE) != 0) { 1487 (void) fprintf(stderr, 1488 gettext("invalid property '%s'\n"), optarg); 1489 usage(B_FALSE); 1490 } 1491 break; 1492 case 't': 1493 types = 0; 1494 while (*optarg != '\0') { 1495 switch (getsubopt(&optarg, type_subopts, 1496 &value)) { 1497 case 0: 1498 types |= ZFS_TYPE_FILESYSTEM; 1499 break; 1500 case 1: 1501 types |= ZFS_TYPE_VOLUME; 1502 break; 1503 case 2: 1504 types |= ZFS_TYPE_SNAPSHOT; 1505 break; 1506 default: 1507 (void) fprintf(stderr, 1508 gettext("invalid type '%s'\n"), 1509 value); 1510 usage(B_FALSE); 1511 } 1512 } 1513 break; 1514 case ':': 1515 (void) fprintf(stderr, gettext("missing argument for " 1516 "'%c' option\n"), optopt); 1517 usage(B_FALSE); 1518 break; 1519 case '?': 1520 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1521 optopt); 1522 usage(B_FALSE); 1523 } 1524 } 1525 1526 argc -= optind; 1527 argv += optind; 1528 1529 if (fields == NULL) 1530 fields = basic_fields; 1531 1532 /* 1533 * If the user specifies '-o all', the zfs_get_proplist() doesn't 1534 * normally include the name of the dataset. For 'zfs list', we always 1535 * want this property to be first. 1536 */ 1537 if (zfs_get_proplist(g_zfs, fields, &cb.cb_proplist) != 0) 1538 usage(B_FALSE); 1539 1540 cb.cb_scripted = scripted; 1541 cb.cb_first = B_TRUE; 1542 1543 ret = zfs_for_each(argc, argv, recurse, types, sortcol, &cb.cb_proplist, 1544 list_callback, &cb, B_TRUE); 1545 1546 zfs_free_proplist(cb.cb_proplist); 1547 zfs_free_sort_columns(sortcol); 1548 1549 if (ret == 0 && cb.cb_first && !cb.cb_scripted) 1550 (void) printf(gettext("no datasets available\n")); 1551 1552 return (ret); 1553 } 1554 1555 /* 1556 * zfs rename <fs | snap | vol> <fs | snap | vol> 1557 * zfs rename -p <fs | vol> <fs | vol> 1558 * zfs rename -r <snap> <snap> 1559 * 1560 * Renames the given dataset to another of the same type. 1561 * 1562 * The '-p' flag creates all the non-existing ancestors of the target first. 1563 */ 1564 /* ARGSUSED */ 1565 static int 1566 zfs_do_rename(int argc, char **argv) 1567 { 1568 zfs_handle_t *zhp; 1569 int c; 1570 int ret; 1571 boolean_t recurse = B_FALSE; 1572 boolean_t parents = B_FALSE; 1573 1574 /* check options */ 1575 while ((c = getopt(argc, argv, "pr")) != -1) { 1576 switch (c) { 1577 case 'p': 1578 parents = B_TRUE; 1579 break; 1580 case 'r': 1581 recurse = B_TRUE; 1582 break; 1583 case '?': 1584 default: 1585 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1586 optopt); 1587 usage(B_FALSE); 1588 } 1589 } 1590 1591 argc -= optind; 1592 argv += optind; 1593 1594 /* check number of arguments */ 1595 if (argc < 1) { 1596 (void) fprintf(stderr, gettext("missing source dataset " 1597 "argument\n")); 1598 usage(B_FALSE); 1599 } 1600 if (argc < 2) { 1601 (void) fprintf(stderr, gettext("missing target dataset " 1602 "argument\n")); 1603 usage(B_FALSE); 1604 } 1605 if (argc > 2) { 1606 (void) fprintf(stderr, gettext("too many arguments\n")); 1607 usage(B_FALSE); 1608 } 1609 1610 if (recurse && parents) { 1611 (void) fprintf(stderr, gettext("-p and -r options are mutually " 1612 "exclusive\n")); 1613 usage(B_FALSE); 1614 } 1615 1616 if (recurse && strchr(argv[0], '@') == 0) { 1617 (void) fprintf(stderr, gettext("source dataset for recursive " 1618 "rename must be a snapshot\n")); 1619 usage(B_FALSE); 1620 } 1621 1622 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM | 1623 ZFS_TYPE_VOLUME : ZFS_TYPE_ANY)) == NULL) 1624 return (1); 1625 1626 /* If we were asked and the name looks good, try to create ancestors. */ 1627 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) && 1628 zfs_create_ancestors(g_zfs, argv[1]) != 0) { 1629 zfs_close(zhp); 1630 return (1); 1631 } 1632 1633 ret = (zfs_rename(zhp, argv[1], recurse) != 0); 1634 1635 zfs_close(zhp); 1636 return (ret); 1637 } 1638 1639 /* 1640 * zfs promote <fs> 1641 * 1642 * Promotes the given clone fs to be the parent 1643 */ 1644 /* ARGSUSED */ 1645 static int 1646 zfs_do_promote(int argc, char **argv) 1647 { 1648 zfs_handle_t *zhp; 1649 int ret; 1650 1651 /* check options */ 1652 if (argc > 1 && argv[1][0] == '-') { 1653 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1654 argv[1][1]); 1655 usage(B_FALSE); 1656 } 1657 1658 /* check number of arguments */ 1659 if (argc < 2) { 1660 (void) fprintf(stderr, gettext("missing clone filesystem" 1661 " argument\n")); 1662 usage(B_FALSE); 1663 } 1664 if (argc > 2) { 1665 (void) fprintf(stderr, gettext("too many arguments\n")); 1666 usage(B_FALSE); 1667 } 1668 1669 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 1670 if (zhp == NULL) 1671 return (1); 1672 1673 ret = (zfs_promote(zhp) != 0); 1674 1675 1676 zfs_close(zhp); 1677 return (ret); 1678 } 1679 1680 /* 1681 * zfs rollback [-rfR] <snapshot> 1682 * 1683 * -r Delete any intervening snapshots before doing rollback 1684 * -R Delete any snapshots and their clones 1685 * -f Force unmount filesystems, even if they are in use. 1686 * 1687 * Given a filesystem, rollback to a specific snapshot, discarding any changes 1688 * since then and making it the active dataset. If more recent snapshots exist, 1689 * the command will complain unless the '-r' flag is given. 1690 */ 1691 typedef struct rollback_cbdata { 1692 uint64_t cb_create; 1693 boolean_t cb_first; 1694 int cb_doclones; 1695 char *cb_target; 1696 int cb_error; 1697 boolean_t cb_recurse; 1698 boolean_t cb_dependent; 1699 } rollback_cbdata_t; 1700 1701 /* 1702 * Report any snapshots more recent than the one specified. Used when '-r' is 1703 * not specified. We reuse this same callback for the snapshot dependents - if 1704 * 'cb_dependent' is set, then this is a dependent and we should report it 1705 * without checking the transaction group. 1706 */ 1707 static int 1708 rollback_check(zfs_handle_t *zhp, void *data) 1709 { 1710 rollback_cbdata_t *cbp = data; 1711 1712 if (cbp->cb_doclones) { 1713 zfs_close(zhp); 1714 return (0); 1715 } 1716 1717 if (!cbp->cb_dependent) { 1718 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 && 1719 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 1720 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 1721 cbp->cb_create) { 1722 1723 if (cbp->cb_first && !cbp->cb_recurse) { 1724 (void) fprintf(stderr, gettext("cannot " 1725 "rollback to '%s': more recent snapshots " 1726 "exist\n"), 1727 cbp->cb_target); 1728 (void) fprintf(stderr, gettext("use '-r' to " 1729 "force deletion of the following " 1730 "snapshots:\n")); 1731 cbp->cb_first = 0; 1732 cbp->cb_error = 1; 1733 } 1734 1735 if (cbp->cb_recurse) { 1736 cbp->cb_dependent = B_TRUE; 1737 if (zfs_iter_dependents(zhp, B_TRUE, 1738 rollback_check, cbp) != 0) { 1739 zfs_close(zhp); 1740 return (-1); 1741 } 1742 cbp->cb_dependent = B_FALSE; 1743 } else { 1744 (void) fprintf(stderr, "%s\n", 1745 zfs_get_name(zhp)); 1746 } 1747 } 1748 } else { 1749 if (cbp->cb_first && cbp->cb_recurse) { 1750 (void) fprintf(stderr, gettext("cannot rollback to " 1751 "'%s': clones of previous snapshots exist\n"), 1752 cbp->cb_target); 1753 (void) fprintf(stderr, gettext("use '-R' to " 1754 "force deletion of the following clones and " 1755 "dependents:\n")); 1756 cbp->cb_first = 0; 1757 cbp->cb_error = 1; 1758 } 1759 1760 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 1761 } 1762 1763 zfs_close(zhp); 1764 return (0); 1765 } 1766 1767 static int 1768 zfs_do_rollback(int argc, char **argv) 1769 { 1770 int ret; 1771 int c; 1772 rollback_cbdata_t cb = { 0 }; 1773 zfs_handle_t *zhp, *snap; 1774 char parentname[ZFS_MAXNAMELEN]; 1775 char *delim; 1776 int force = 0; 1777 1778 /* check options */ 1779 while ((c = getopt(argc, argv, "rfR")) != -1) { 1780 switch (c) { 1781 case 'f': 1782 force = 1; 1783 break; 1784 case 'r': 1785 cb.cb_recurse = 1; 1786 break; 1787 case 'R': 1788 cb.cb_recurse = 1; 1789 cb.cb_doclones = 1; 1790 break; 1791 case '?': 1792 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1793 optopt); 1794 usage(B_FALSE); 1795 } 1796 } 1797 1798 argc -= optind; 1799 argv += optind; 1800 1801 /* check number of arguments */ 1802 if (argc < 1) { 1803 (void) fprintf(stderr, gettext("missing dataset argument\n")); 1804 usage(B_FALSE); 1805 } 1806 if (argc > 1) { 1807 (void) fprintf(stderr, gettext("too many arguments\n")); 1808 usage(B_FALSE); 1809 } 1810 1811 /* open the snapshot */ 1812 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 1813 return (1); 1814 1815 /* open the parent dataset */ 1816 (void) strlcpy(parentname, argv[0], sizeof (parentname)); 1817 verify((delim = strrchr(parentname, '@')) != NULL); 1818 *delim = '\0'; 1819 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_ANY)) == NULL) { 1820 zfs_close(snap); 1821 return (1); 1822 } 1823 1824 /* 1825 * Check for more recent snapshots and/or clones based on the presence 1826 * of '-r' and '-R'. 1827 */ 1828 cb.cb_target = argv[0]; 1829 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 1830 cb.cb_first = B_TRUE; 1831 cb.cb_error = 0; 1832 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0) 1833 goto out; 1834 1835 if ((ret = cb.cb_error) != 0) 1836 goto out; 1837 1838 /* 1839 * Rollback parent to the given snapshot. 1840 */ 1841 ret = zfs_rollback(zhp, snap, force); 1842 1843 out: 1844 zfs_close(snap); 1845 zfs_close(zhp); 1846 1847 if (ret == 0) 1848 return (0); 1849 else 1850 return (1); 1851 } 1852 1853 /* 1854 * zfs set property=value { fs | snap | vol } ... 1855 * 1856 * Sets the given property for all datasets specified on the command line. 1857 */ 1858 typedef struct set_cbdata { 1859 char *cb_propname; 1860 char *cb_value; 1861 } set_cbdata_t; 1862 1863 static int 1864 set_callback(zfs_handle_t *zhp, void *data) 1865 { 1866 set_cbdata_t *cbp = data; 1867 1868 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) { 1869 switch (libzfs_errno(g_zfs)) { 1870 case EZFS_MOUNTFAILED: 1871 (void) fprintf(stderr, gettext("property may be set " 1872 "but unable to remount filesystem\n")); 1873 break; 1874 case EZFS_SHARENFSFAILED: 1875 (void) fprintf(stderr, gettext("property may be set " 1876 "but unable to reshare filesystem\n")); 1877 break; 1878 } 1879 return (1); 1880 } 1881 return (0); 1882 } 1883 1884 static int 1885 zfs_do_set(int argc, char **argv) 1886 { 1887 set_cbdata_t cb; 1888 int ret; 1889 1890 /* check for options */ 1891 if (argc > 1 && argv[1][0] == '-') { 1892 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1893 argv[1][1]); 1894 usage(B_FALSE); 1895 } 1896 1897 /* check number of arguments */ 1898 if (argc < 2) { 1899 (void) fprintf(stderr, gettext("missing property=value " 1900 "argument\n")); 1901 usage(B_FALSE); 1902 } 1903 if (argc < 3) { 1904 (void) fprintf(stderr, gettext("missing dataset name\n")); 1905 usage(B_FALSE); 1906 } 1907 1908 /* validate property=value argument */ 1909 cb.cb_propname = argv[1]; 1910 if ((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) { 1911 (void) fprintf(stderr, gettext("missing value in " 1912 "property=value argument\n")); 1913 usage(B_FALSE); 1914 } 1915 1916 *cb.cb_value = '\0'; 1917 cb.cb_value++; 1918 1919 if (*cb.cb_propname == '\0') { 1920 (void) fprintf(stderr, 1921 gettext("missing property in property=value argument\n")); 1922 usage(B_FALSE); 1923 } 1924 1925 1926 ret = zfs_for_each(argc - 2, argv + 2, B_FALSE, 1927 ZFS_TYPE_ANY, NULL, NULL, set_callback, &cb, B_FALSE); 1928 1929 return (ret); 1930 } 1931 1932 /* 1933 * zfs snapshot [-r] <fs@snap> 1934 * 1935 * Creates a snapshot with the given name. While functionally equivalent to 1936 * 'zfs create', it is a separate command to diffferentiate intent. 1937 */ 1938 static int 1939 zfs_do_snapshot(int argc, char **argv) 1940 { 1941 boolean_t recursive = B_FALSE; 1942 int ret; 1943 char c; 1944 1945 /* check options */ 1946 while ((c = getopt(argc, argv, ":r")) != -1) { 1947 switch (c) { 1948 case 'r': 1949 recursive = B_TRUE; 1950 break; 1951 case '?': 1952 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1953 optopt); 1954 usage(B_FALSE); 1955 } 1956 } 1957 1958 argc -= optind; 1959 argv += optind; 1960 1961 /* check number of arguments */ 1962 if (argc < 1) { 1963 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 1964 usage(B_FALSE); 1965 } 1966 if (argc > 1) { 1967 (void) fprintf(stderr, gettext("too many arguments\n")); 1968 usage(B_FALSE); 1969 } 1970 1971 ret = zfs_snapshot(g_zfs, argv[0], recursive); 1972 if (ret && recursive) 1973 (void) fprintf(stderr, gettext("no snapshots were created\n")); 1974 return (ret != 0); 1975 } 1976 1977 /* 1978 * zfs send [-i <@snap>] <fs@snap> 1979 * 1980 * Send a backup stream to stdout. 1981 */ 1982 static int 1983 zfs_do_send(int argc, char **argv) 1984 { 1985 char *fromname = NULL; 1986 char *cp; 1987 zfs_handle_t *zhp; 1988 int c, err; 1989 1990 /* check options */ 1991 while ((c = getopt(argc, argv, ":i:")) != -1) { 1992 switch (c) { 1993 case 'i': 1994 if (fromname) 1995 usage(B_FALSE); 1996 fromname = optarg; 1997 break; 1998 case ':': 1999 (void) fprintf(stderr, gettext("missing argument for " 2000 "'%c' option\n"), optopt); 2001 usage(B_FALSE); 2002 break; 2003 case '?': 2004 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2005 optopt); 2006 usage(B_FALSE); 2007 } 2008 } 2009 2010 argc -= optind; 2011 argv += optind; 2012 2013 /* check number of arguments */ 2014 if (argc < 1) { 2015 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 2016 usage(B_FALSE); 2017 } 2018 if (argc > 1) { 2019 (void) fprintf(stderr, gettext("too many arguments\n")); 2020 usage(B_FALSE); 2021 } 2022 2023 if (isatty(STDOUT_FILENO)) { 2024 (void) fprintf(stderr, 2025 gettext("Error: Stream can not be written to a terminal.\n" 2026 "You must redirect standard output.\n")); 2027 return (1); 2028 } 2029 2030 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 2031 return (1); 2032 2033 /* 2034 * If they specified the full path to the snapshot, chop off 2035 * everything except the short name of the snapshot. 2036 */ 2037 if (fromname && (cp = strchr(fromname, '@')) != NULL) { 2038 if (cp != fromname && 2039 strncmp(argv[0], fromname, cp - fromname + 1)) { 2040 (void) fprintf(stderr, 2041 gettext("incremental source must be " 2042 "in same filesystem\n")); 2043 usage(B_FALSE); 2044 } 2045 fromname = cp + 1; 2046 if (strchr(fromname, '@') || strchr(fromname, '/')) { 2047 (void) fprintf(stderr, 2048 gettext("invalid incremental source\n")); 2049 usage(B_FALSE); 2050 } 2051 } 2052 2053 err = zfs_send(zhp, fromname, STDOUT_FILENO); 2054 zfs_close(zhp); 2055 2056 return (err != 0); 2057 } 2058 2059 /* 2060 * zfs receive <fs@snap> 2061 * 2062 * Restore a backup stream from stdin. 2063 */ 2064 static int 2065 zfs_do_receive(int argc, char **argv) 2066 { 2067 int c, err; 2068 boolean_t isprefix = B_FALSE; 2069 boolean_t dryrun = B_FALSE; 2070 boolean_t verbose = B_FALSE; 2071 boolean_t force = B_FALSE; 2072 2073 /* check options */ 2074 while ((c = getopt(argc, argv, ":dnvF")) != -1) { 2075 switch (c) { 2076 case 'd': 2077 isprefix = B_TRUE; 2078 break; 2079 case 'n': 2080 dryrun = B_TRUE; 2081 break; 2082 case 'v': 2083 verbose = B_TRUE; 2084 break; 2085 case 'F': 2086 force = B_TRUE; 2087 break; 2088 case ':': 2089 (void) fprintf(stderr, gettext("missing argument for " 2090 "'%c' option\n"), optopt); 2091 usage(B_FALSE); 2092 break; 2093 case '?': 2094 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2095 optopt); 2096 usage(B_FALSE); 2097 } 2098 } 2099 2100 argc -= optind; 2101 argv += optind; 2102 2103 /* check number of arguments */ 2104 if (argc < 1) { 2105 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 2106 usage(B_FALSE); 2107 } 2108 if (argc > 1) { 2109 (void) fprintf(stderr, gettext("too many arguments\n")); 2110 usage(B_FALSE); 2111 } 2112 2113 if (isatty(STDIN_FILENO)) { 2114 (void) fprintf(stderr, 2115 gettext("Error: Backup stream can not be read " 2116 "from a terminal.\n" 2117 "You must redirect standard input.\n")); 2118 return (1); 2119 } 2120 2121 err = zfs_receive(g_zfs, argv[0], isprefix, verbose, dryrun, force, 2122 STDIN_FILENO); 2123 2124 return (err != 0); 2125 } 2126 2127 typedef struct allow_cb { 2128 int a_permcnt; 2129 size_t a_treeoffset; 2130 } allow_cb_t; 2131 2132 static void 2133 zfs_print_perms(avl_tree_t *tree) 2134 { 2135 zfs_perm_node_t *permnode; 2136 2137 permnode = avl_first(tree); 2138 while (permnode != NULL) { 2139 (void) printf("%s", permnode->z_pname); 2140 permnode = AVL_NEXT(tree, permnode); 2141 if (permnode) 2142 (void) printf(","); 2143 else 2144 (void) printf("\n"); 2145 } 2146 } 2147 2148 /* 2149 * Iterate over user/groups/everyone/... and the call perm_iter 2150 * function to print actual permission when tree has >0 nodes. 2151 */ 2152 static void 2153 zfs_iter_perms(avl_tree_t *tree, const char *banner, allow_cb_t *cb) 2154 { 2155 zfs_allow_node_t *item; 2156 avl_tree_t *ptree; 2157 2158 item = avl_first(tree); 2159 while (item) { 2160 ptree = (void *)((char *)item + cb->a_treeoffset); 2161 if (avl_numnodes(ptree)) { 2162 if (cb->a_permcnt++ == 0) 2163 (void) printf("%s\n", banner); 2164 (void) printf("\t%s", item->z_key); 2165 /* 2166 * Avoid an extra space being printed 2167 * for "everyone" which is keyed with a null 2168 * string 2169 */ 2170 if (item->z_key[0] != '\0') 2171 (void) printf(" "); 2172 zfs_print_perms(ptree); 2173 } 2174 item = AVL_NEXT(tree, item); 2175 } 2176 } 2177 2178 #define LINES "-------------------------------------------------------------\n" 2179 static int 2180 zfs_print_allows(char *ds) 2181 { 2182 zfs_allow_t *curperms, *perms; 2183 zfs_handle_t *zhp; 2184 allow_cb_t allowcb = { 0 }; 2185 char banner[MAXPATHLEN]; 2186 2187 if (ds[0] == '-') 2188 usage(B_FALSE); 2189 2190 if (strrchr(ds, '@')) { 2191 (void) fprintf(stderr, gettext("Snapshots don't have 'allow'" 2192 " permissions\n")); 2193 return (1); 2194 } 2195 if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_ANY)) == NULL) 2196 return (1); 2197 2198 if (zfs_perm_get(zhp, &perms)) { 2199 (void) fprintf(stderr, 2200 gettext("Failed to retrieve 'allows' on %s\n"), ds); 2201 zfs_close(zhp); 2202 return (1); 2203 } 2204 2205 zfs_close(zhp); 2206 2207 if (perms != NULL) 2208 (void) printf("%s", LINES); 2209 for (curperms = perms; curperms; curperms = curperms->z_next) { 2210 2211 (void) snprintf(banner, sizeof (banner), 2212 "Permission sets on (%s)", curperms->z_setpoint); 2213 allowcb.a_treeoffset = 2214 offsetof(zfs_allow_node_t, z_localdescend); 2215 allowcb.a_permcnt = 0; 2216 zfs_iter_perms(&curperms->z_sets, banner, &allowcb); 2217 2218 (void) snprintf(banner, sizeof (banner), 2219 "Create time permissions on (%s)", curperms->z_setpoint); 2220 allowcb.a_treeoffset = 2221 offsetof(zfs_allow_node_t, z_localdescend); 2222 allowcb.a_permcnt = 0; 2223 zfs_iter_perms(&curperms->z_crperms, banner, &allowcb); 2224 2225 2226 (void) snprintf(banner, sizeof (banner), 2227 "Local permissions on (%s)", curperms->z_setpoint); 2228 allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_local); 2229 allowcb.a_permcnt = 0; 2230 zfs_iter_perms(&curperms->z_user, banner, &allowcb); 2231 zfs_iter_perms(&curperms->z_group, banner, &allowcb); 2232 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb); 2233 2234 (void) snprintf(banner, sizeof (banner), 2235 "Descendent permissions on (%s)", curperms->z_setpoint); 2236 allowcb.a_treeoffset = offsetof(zfs_allow_node_t, z_descend); 2237 allowcb.a_permcnt = 0; 2238 zfs_iter_perms(&curperms->z_user, banner, &allowcb); 2239 zfs_iter_perms(&curperms->z_group, banner, &allowcb); 2240 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb); 2241 2242 (void) snprintf(banner, sizeof (banner), 2243 "Local+Descendent permissions on (%s)", 2244 curperms->z_setpoint); 2245 allowcb.a_treeoffset = 2246 offsetof(zfs_allow_node_t, z_localdescend); 2247 allowcb.a_permcnt = 0; 2248 zfs_iter_perms(&curperms->z_user, banner, &allowcb); 2249 zfs_iter_perms(&curperms->z_group, banner, &allowcb); 2250 zfs_iter_perms(&curperms->z_everyone, banner, &allowcb); 2251 2252 (void) printf("%s", LINES); 2253 } 2254 zfs_free_allows(perms); 2255 return (0); 2256 } 2257 2258 #define ALLOWOPTIONS "ldcsu:g:e" 2259 #define UNALLOWOPTIONS "ldcsu:g:er" 2260 2261 /* 2262 * Validate options, and build necessary datastructure to display/remove/add 2263 * permissions. 2264 * Returns 0 - If permissions should be added/removed 2265 * Returns 1 - If permissions should be displayed. 2266 * Returns -1 - on failure 2267 */ 2268 int 2269 parse_allow_args(int *argc, char **argv[], boolean_t unallow, 2270 char **ds, int *recurse, nvlist_t **zperms) 2271 { 2272 int c; 2273 char *options = unallow ? UNALLOWOPTIONS : ALLOWOPTIONS; 2274 zfs_deleg_inherit_t deleg_type = ZFS_DELEG_NONE; 2275 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN; 2276 char *who; 2277 char *perms = NULL; 2278 zfs_handle_t *zhp; 2279 2280 while ((c = getopt(*argc, *argv, options)) != -1) { 2281 switch (c) { 2282 case 'l': 2283 if (who_type == ZFS_DELEG_CREATE || 2284 who_type == ZFS_DELEG_NAMED_SET) 2285 usage(B_FALSE); 2286 2287 deleg_type |= ZFS_DELEG_PERM_LOCAL; 2288 break; 2289 case 'd': 2290 if (who_type == ZFS_DELEG_CREATE || 2291 who_type == ZFS_DELEG_NAMED_SET) 2292 usage(B_FALSE); 2293 2294 deleg_type |= ZFS_DELEG_PERM_DESCENDENT; 2295 break; 2296 case 'r': 2297 *recurse = B_TRUE; 2298 break; 2299 case 'c': 2300 if (who_type != ZFS_DELEG_WHO_UNKNOWN) 2301 usage(B_FALSE); 2302 if (deleg_type) 2303 usage(B_FALSE); 2304 who_type = ZFS_DELEG_CREATE; 2305 break; 2306 case 's': 2307 if (who_type != ZFS_DELEG_WHO_UNKNOWN) 2308 usage(B_FALSE); 2309 if (deleg_type) 2310 usage(B_FALSE); 2311 who_type = ZFS_DELEG_NAMED_SET; 2312 break; 2313 case 'u': 2314 if (who_type != ZFS_DELEG_WHO_UNKNOWN) 2315 usage(B_FALSE); 2316 who_type = ZFS_DELEG_USER; 2317 who = optarg; 2318 break; 2319 case 'g': 2320 if (who_type != ZFS_DELEG_WHO_UNKNOWN) 2321 usage(B_FALSE); 2322 who_type = ZFS_DELEG_GROUP; 2323 who = optarg; 2324 break; 2325 case 'e': 2326 if (who_type != ZFS_DELEG_WHO_UNKNOWN) 2327 usage(B_FALSE); 2328 who_type = ZFS_DELEG_EVERYONE; 2329 break; 2330 default: 2331 usage(B_FALSE); 2332 break; 2333 } 2334 } 2335 2336 if (deleg_type == 0) 2337 deleg_type = ZFS_DELEG_PERM_LOCALDESCENDENT; 2338 2339 *argc -= optind; 2340 *argv += optind; 2341 2342 if (unallow == B_FALSE && *argc == 1) { 2343 /* 2344 * Only print permissions if no options were processed 2345 */ 2346 if (optind == 1) 2347 return (1); 2348 else 2349 usage(B_FALSE); 2350 } 2351 2352 /* 2353 * initialize variables for zfs_build_perms based on number 2354 * of arguments. 2355 * 3 arguments ==> zfs [un]allow joe perm,perm,perm <dataset> or 2356 * zfs [un]allow -s @set1 perm,perm <dataset> 2357 * 2 arguments ==> zfs [un]allow -c perm,perm <dataset> or 2358 * zfs [un]allow -u|-g <name> perm <dataset> or 2359 * zfs [un]allow -e perm,perm <dataset> 2360 * zfs unallow joe <dataset> 2361 * zfs unallow -s @set1 <dataset> 2362 * 1 argument ==> zfs [un]allow -e <dataset> or 2363 * zfs [un]allow -c <dataset> 2364 */ 2365 2366 switch (*argc) { 2367 case 3: 2368 perms = (*argv)[1]; 2369 who = (*argv)[0]; 2370 *ds = (*argv)[2]; 2371 2372 /* 2373 * advance argc/argv for do_allow cases. 2374 * for do_allow case make sure who have a know who type 2375 * and its not a permission set. 2376 */ 2377 if (unallow == B_TRUE) { 2378 *argc -= 2; 2379 *argv += 2; 2380 } else if (who_type != ZFS_DELEG_WHO_UNKNOWN && 2381 who_type != ZFS_DELEG_NAMED_SET) 2382 usage(B_FALSE); 2383 break; 2384 2385 case 2: 2386 if (unallow == B_TRUE && (who_type == ZFS_DELEG_EVERYONE || 2387 who_type == ZFS_DELEG_CREATE || who != NULL)) { 2388 perms = (*argv)[0]; 2389 *ds = (*argv)[1]; 2390 } else { 2391 if (unallow == B_FALSE && 2392 (who_type == ZFS_DELEG_WHO_UNKNOWN || 2393 who_type == ZFS_DELEG_NAMED_SET)) 2394 usage(B_FALSE); 2395 else if (who_type == ZFS_DELEG_WHO_UNKNOWN || 2396 who_type == ZFS_DELEG_NAMED_SET) 2397 who = (*argv)[0]; 2398 else if (who_type != ZFS_DELEG_NAMED_SET) 2399 perms = (*argv)[0]; 2400 *ds = (*argv)[1]; 2401 } 2402 if (unallow == B_TRUE) { 2403 (*argc)--; 2404 (*argv)++; 2405 } 2406 break; 2407 2408 case 1: 2409 if (unallow == B_FALSE) 2410 usage(B_FALSE); 2411 if (who == NULL && who_type != ZFS_DELEG_CREATE && 2412 who_type != ZFS_DELEG_EVERYONE) 2413 usage(B_FALSE); 2414 *ds = (*argv)[0]; 2415 break; 2416 2417 default: 2418 usage(B_FALSE); 2419 } 2420 2421 if (strrchr(*ds, '@')) { 2422 (void) fprintf(stderr, 2423 gettext("Can't set or remove 'allow' permissions " 2424 "on snapshots.\n")); 2425 return (-1); 2426 } 2427 2428 if ((zhp = zfs_open(g_zfs, *ds, ZFS_TYPE_ANY)) == NULL) 2429 return (-1); 2430 2431 if ((zfs_build_perms(zhp, who, perms, 2432 who_type, deleg_type, zperms)) != 0) { 2433 zfs_close(zhp); 2434 return (-1); 2435 } 2436 zfs_close(zhp); 2437 return (0); 2438 } 2439 2440 static int 2441 zfs_do_allow(int argc, char **argv) 2442 { 2443 char *ds; 2444 nvlist_t *zperms = NULL; 2445 zfs_handle_t *zhp; 2446 int unused; 2447 int ret; 2448 2449 if ((ret = parse_allow_args(&argc, &argv, B_FALSE, &ds, 2450 &unused, &zperms)) == -1) 2451 return (1); 2452 2453 if (ret == 1) 2454 return (zfs_print_allows(argv[0])); 2455 2456 if ((zhp = zfs_open(g_zfs, ds, ZFS_TYPE_ANY)) == NULL) 2457 return (1); 2458 2459 if (zfs_perm_set(zhp, zperms)) { 2460 zfs_close(zhp); 2461 nvlist_free(zperms); 2462 return (1); 2463 } 2464 nvlist_free(zperms); 2465 zfs_close(zhp); 2466 2467 return (0); 2468 } 2469 2470 static int 2471 unallow_callback(zfs_handle_t *zhp, void *data) 2472 { 2473 nvlist_t *nvp = (nvlist_t *)data; 2474 int error; 2475 2476 error = zfs_perm_remove(zhp, nvp); 2477 if (error) { 2478 (void) fprintf(stderr, gettext("Failed to remove permissions " 2479 "on %s\n"), zfs_get_name(zhp)); 2480 } 2481 return (error); 2482 } 2483 2484 static int 2485 zfs_do_unallow(int argc, char **argv) 2486 { 2487 int recurse = B_FALSE; 2488 char *ds; 2489 int error; 2490 nvlist_t *zperms = NULL; 2491 2492 if (parse_allow_args(&argc, &argv, B_TRUE, 2493 &ds, &recurse, &zperms) == -1) 2494 return (1); 2495 2496 error = zfs_for_each(argc, argv, recurse, 2497 ZFS_TYPE_FILESYSTEM|ZFS_TYPE_VOLUME, NULL, 2498 NULL, unallow_callback, (void *)zperms, B_FALSE); 2499 2500 if (zperms) 2501 nvlist_free(zperms); 2502 2503 return (error); 2504 } 2505 2506 typedef struct get_all_cbdata { 2507 zfs_handle_t **cb_handles; 2508 size_t cb_alloc; 2509 size_t cb_used; 2510 uint_t cb_types; 2511 } get_all_cbdata_t; 2512 2513 static int 2514 get_one_dataset(zfs_handle_t *zhp, void *data) 2515 { 2516 get_all_cbdata_t *cbp = data; 2517 zfs_type_t type = zfs_get_type(zhp); 2518 2519 /* 2520 * Interate over any nested datasets. 2521 */ 2522 if (type == ZFS_TYPE_FILESYSTEM && 2523 zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) { 2524 zfs_close(zhp); 2525 return (1); 2526 } 2527 2528 /* 2529 * Skip any datasets whose type does not match. 2530 */ 2531 if ((type & cbp->cb_types) == 0) { 2532 zfs_close(zhp); 2533 return (0); 2534 } 2535 2536 if (cbp->cb_alloc == cbp->cb_used) { 2537 zfs_handle_t **handles; 2538 2539 if (cbp->cb_alloc == 0) 2540 cbp->cb_alloc = 64; 2541 else 2542 cbp->cb_alloc *= 2; 2543 2544 handles = safe_malloc(cbp->cb_alloc * sizeof (void *)); 2545 2546 if (cbp->cb_handles) { 2547 bcopy(cbp->cb_handles, handles, 2548 cbp->cb_used * sizeof (void *)); 2549 free(cbp->cb_handles); 2550 } 2551 2552 cbp->cb_handles = handles; 2553 } 2554 2555 cbp->cb_handles[cbp->cb_used++] = zhp; 2556 2557 return (0); 2558 } 2559 2560 static void 2561 get_all_datasets(uint_t types, zfs_handle_t ***dslist, size_t *count) 2562 { 2563 get_all_cbdata_t cb = { 0 }; 2564 cb.cb_types = types; 2565 2566 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb); 2567 2568 *dslist = cb.cb_handles; 2569 *count = cb.cb_used; 2570 } 2571 2572 static int 2573 dataset_cmp(const void *a, const void *b) 2574 { 2575 zfs_handle_t **za = (zfs_handle_t **)a; 2576 zfs_handle_t **zb = (zfs_handle_t **)b; 2577 char mounta[MAXPATHLEN]; 2578 char mountb[MAXPATHLEN]; 2579 boolean_t gota, gotb; 2580 2581 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0) 2582 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 2583 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 2584 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0) 2585 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 2586 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 2587 2588 if (gota && gotb) 2589 return (strcmp(mounta, mountb)); 2590 2591 if (gota) 2592 return (-1); 2593 if (gotb) 2594 return (1); 2595 2596 return (strcmp(zfs_get_name(a), zfs_get_name(b))); 2597 } 2598 2599 /* 2600 * Generic callback for sharing or mounting filesystems. Because the code is so 2601 * similar, we have a common function with an extra parameter to determine which 2602 * mode we are using. 2603 */ 2604 #define OP_SHARE 0x1 2605 #define OP_MOUNT 0x2 2606 2607 /* 2608 * Share or mount a dataset. 2609 */ 2610 static int 2611 share_mount_one(zfs_handle_t *zhp, int op, int flags, boolean_t explicit, 2612 const char *options) 2613 { 2614 char mountpoint[ZFS_MAXPROPLEN]; 2615 char shareopts[ZFS_MAXPROPLEN]; 2616 const char *cmdname = op == OP_SHARE ? "share" : "mount"; 2617 struct mnttab mnt; 2618 uint64_t zoned, canmount; 2619 zfs_type_t type = zfs_get_type(zhp); 2620 2621 assert(type & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)); 2622 2623 if (type == ZFS_TYPE_FILESYSTEM) { 2624 /* 2625 * Check to make sure we can mount/share this dataset. If we 2626 * are in the global zone and the filesystem is exported to a 2627 * local zone, or if we are in a local zone and the 2628 * filesystem is not exported, then it is an error. 2629 */ 2630 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2631 2632 if (zoned && getzoneid() == GLOBAL_ZONEID) { 2633 if (!explicit) 2634 return (0); 2635 2636 (void) fprintf(stderr, gettext("cannot %s '%s': " 2637 "dataset is exported to a local zone\n"), cmdname, 2638 zfs_get_name(zhp)); 2639 return (1); 2640 2641 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) { 2642 if (!explicit) 2643 return (0); 2644 2645 (void) fprintf(stderr, gettext("cannot %s '%s': " 2646 "permission denied\n"), cmdname, 2647 zfs_get_name(zhp)); 2648 return (1); 2649 } 2650 2651 /* 2652 * Ignore any filesystems which don't apply to us. This 2653 * includes those with a legacy mountpoint, or those with 2654 * legacy share options. 2655 */ 2656 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 2657 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0); 2658 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, 2659 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0); 2660 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT); 2661 2662 if (op == OP_SHARE && strcmp(shareopts, "off") == 0) { 2663 if (!explicit) 2664 return (0); 2665 2666 (void) fprintf(stderr, gettext("cannot share '%s': " 2667 "legacy share\n"), zfs_get_name(zhp)); 2668 (void) fprintf(stderr, gettext("use share(1M) to " 2669 "share this filesystem\n")); 2670 return (1); 2671 } 2672 2673 /* 2674 * We cannot share or mount legacy filesystems. If the 2675 * shareopts is non-legacy but the mountpoint is legacy, we 2676 * treat it as a legacy share. 2677 */ 2678 if (strcmp(mountpoint, "legacy") == 0) { 2679 if (!explicit) 2680 return (0); 2681 2682 (void) fprintf(stderr, gettext("cannot %s '%s': " 2683 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp)); 2684 (void) fprintf(stderr, gettext("use %s to " 2685 "%s this filesystem\n"), op == OP_SHARE ? 2686 "share(1M)" : "mount(1M)", cmdname); 2687 return (1); 2688 } 2689 2690 if (strcmp(mountpoint, "none") == 0) { 2691 if (!explicit) 2692 return (0); 2693 2694 (void) fprintf(stderr, gettext("cannot %s '%s': no " 2695 "mountpoint set\n"), cmdname, zfs_get_name(zhp)); 2696 return (1); 2697 } 2698 2699 if (!canmount) { 2700 if (!explicit) 2701 return (0); 2702 2703 (void) fprintf(stderr, gettext("cannot %s '%s': " 2704 "'canmount' property is set to 'off'\n"), cmdname, 2705 zfs_get_name(zhp)); 2706 return (1); 2707 } 2708 2709 /* 2710 * At this point, we have verified that the mountpoint and/or 2711 * shareopts are appropriate for auto management. If the 2712 * filesystem is already mounted or shared, return (failing 2713 * for explicit requests); otherwise mount or share the 2714 * filesystem. 2715 */ 2716 switch (op) { 2717 case OP_SHARE: 2718 if (zfs_is_shared_nfs(zhp, NULL)) { 2719 if (!explicit) 2720 return (0); 2721 2722 (void) fprintf(stderr, gettext("cannot share " 2723 "'%s': filesystem already shared\n"), 2724 zfs_get_name(zhp)); 2725 return (1); 2726 } 2727 2728 if (!zfs_is_mounted(zhp, NULL) && 2729 zfs_mount(zhp, NULL, 0) != 0) 2730 return (1); 2731 2732 if (zfs_share_nfs(zhp) != 0) 2733 return (1); 2734 break; 2735 2736 case OP_MOUNT: 2737 if (options == NULL) 2738 mnt.mnt_mntopts = ""; 2739 else 2740 mnt.mnt_mntopts = (char *)options; 2741 2742 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) && 2743 zfs_is_mounted(zhp, NULL)) { 2744 if (!explicit) 2745 return (0); 2746 2747 (void) fprintf(stderr, gettext("cannot mount " 2748 "'%s': filesystem already mounted\n"), 2749 zfs_get_name(zhp)); 2750 return (1); 2751 } 2752 2753 if (zfs_mount(zhp, options, flags) != 0) 2754 return (1); 2755 break; 2756 } 2757 } else { 2758 assert(op == OP_SHARE); 2759 2760 /* 2761 * Ignore any volumes that aren't shared. 2762 */ 2763 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts, 2764 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0); 2765 2766 if (strcmp(shareopts, "off") == 0) { 2767 if (!explicit) 2768 return (0); 2769 2770 (void) fprintf(stderr, gettext("cannot share '%s': " 2771 "'shareiscsi' property not set\n"), 2772 zfs_get_name(zhp)); 2773 (void) fprintf(stderr, gettext("set 'shareiscsi' " 2774 "property or use iscsitadm(1M) to share this " 2775 "volume\n")); 2776 return (1); 2777 } 2778 2779 if (zfs_is_shared_iscsi(zhp)) { 2780 if (!explicit) 2781 return (0); 2782 2783 (void) fprintf(stderr, gettext("cannot share " 2784 "'%s': volume already shared\n"), 2785 zfs_get_name(zhp)); 2786 return (1); 2787 } 2788 2789 if (zfs_share_iscsi(zhp) != 0) 2790 return (1); 2791 } 2792 2793 return (0); 2794 } 2795 2796 static int 2797 share_mount(int op, int argc, char **argv) 2798 { 2799 int do_all = 0; 2800 int c, ret = 0; 2801 const char *options = NULL; 2802 int types, flags = 0; 2803 2804 /* check options */ 2805 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":ao:O" : "a")) 2806 != -1) { 2807 switch (c) { 2808 case 'a': 2809 do_all = 1; 2810 break; 2811 case 'o': 2812 options = optarg; 2813 break; 2814 case 'O': 2815 flags |= MS_OVERLAY; 2816 break; 2817 case ':': 2818 (void) fprintf(stderr, gettext("missing argument for " 2819 "'%c' option\n"), optopt); 2820 usage(B_FALSE); 2821 break; 2822 case '?': 2823 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2824 optopt); 2825 usage(B_FALSE); 2826 } 2827 } 2828 2829 argc -= optind; 2830 argv += optind; 2831 2832 /* check number of arguments */ 2833 if (do_all) { 2834 zfs_handle_t **dslist = NULL; 2835 size_t i, count = 0; 2836 2837 if (op == OP_MOUNT) { 2838 types = ZFS_TYPE_FILESYSTEM; 2839 } else if (argc > 0) { 2840 if (strcmp(argv[0], "nfs") == 0) { 2841 types = ZFS_TYPE_FILESYSTEM; 2842 } else if (strcmp(argv[0], "iscsi") == 0) { 2843 types = ZFS_TYPE_VOLUME; 2844 } else { 2845 (void) fprintf(stderr, gettext("share type " 2846 "must be 'nfs' or 'iscsi'\n")); 2847 usage(B_FALSE); 2848 } 2849 2850 argc--; 2851 argv++; 2852 } else { 2853 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME; 2854 } 2855 2856 if (argc != 0) { 2857 (void) fprintf(stderr, gettext("too many arguments\n")); 2858 usage(B_FALSE); 2859 } 2860 2861 get_all_datasets(types, &dslist, &count); 2862 2863 if (count == 0) 2864 return (0); 2865 2866 qsort(dslist, count, sizeof (void *), dataset_cmp); 2867 2868 for (i = 0; i < count; i++) { 2869 if (share_mount_one(dslist[i], op, flags, B_FALSE, 2870 options) != 0) 2871 ret = 1; 2872 zfs_close(dslist[i]); 2873 } 2874 2875 free(dslist); 2876 } else if (argc == 0) { 2877 struct mnttab entry; 2878 2879 if (op == OP_SHARE) { 2880 (void) fprintf(stderr, gettext("missing filesystem " 2881 "argument\n")); 2882 usage(B_FALSE); 2883 } 2884 2885 /* 2886 * When mount is given no arguments, go through /etc/mnttab and 2887 * display any active ZFS mounts. We hide any snapshots, since 2888 * they are controlled automatically. 2889 */ 2890 rewind(mnttab_file); 2891 while (getmntent(mnttab_file, &entry) == 0) { 2892 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 || 2893 strchr(entry.mnt_special, '@') != NULL) 2894 continue; 2895 2896 (void) printf("%-30s %s\n", entry.mnt_special, 2897 entry.mnt_mountp); 2898 } 2899 2900 } else { 2901 zfs_handle_t *zhp; 2902 2903 types = ZFS_TYPE_FILESYSTEM; 2904 if (op == OP_SHARE) 2905 types |= ZFS_TYPE_VOLUME; 2906 2907 if (argc > 1) { 2908 (void) fprintf(stderr, 2909 gettext("too many arguments\n")); 2910 usage(B_FALSE); 2911 } 2912 2913 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) { 2914 ret = 1; 2915 } else { 2916 ret = share_mount_one(zhp, op, flags, B_TRUE, 2917 options); 2918 zfs_close(zhp); 2919 } 2920 } 2921 2922 return (ret); 2923 } 2924 2925 /* 2926 * zfs mount -a [nfs | iscsi] 2927 * zfs mount filesystem 2928 * 2929 * Mount all filesystems, or mount the given filesystem. 2930 */ 2931 static int 2932 zfs_do_mount(int argc, char **argv) 2933 { 2934 return (share_mount(OP_MOUNT, argc, argv)); 2935 } 2936 2937 /* 2938 * zfs share -a [nfs | iscsi] 2939 * zfs share filesystem 2940 * 2941 * Share all filesystems, or share the given filesystem. 2942 */ 2943 static int 2944 zfs_do_share(int argc, char **argv) 2945 { 2946 return (share_mount(OP_SHARE, argc, argv)); 2947 } 2948 2949 typedef struct unshare_unmount_node { 2950 zfs_handle_t *un_zhp; 2951 char *un_mountp; 2952 uu_avl_node_t un_avlnode; 2953 } unshare_unmount_node_t; 2954 2955 /* ARGSUSED */ 2956 static int 2957 unshare_unmount_compare(const void *larg, const void *rarg, void *unused) 2958 { 2959 const unshare_unmount_node_t *l = larg; 2960 const unshare_unmount_node_t *r = rarg; 2961 2962 return (strcmp(l->un_mountp, r->un_mountp)); 2963 } 2964 2965 /* 2966 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an 2967 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem, 2968 * and unmount it appropriately. 2969 */ 2970 static int 2971 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual) 2972 { 2973 zfs_handle_t *zhp; 2974 int ret; 2975 struct stat64 statbuf; 2976 struct extmnttab entry; 2977 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount"; 2978 char property[ZFS_MAXPROPLEN]; 2979 2980 /* 2981 * Search for the path in /etc/mnttab. Rather than looking for the 2982 * specific path, which can be fooled by non-standard paths (i.e. ".." 2983 * or "//"), we stat() the path and search for the corresponding 2984 * (major,minor) device pair. 2985 */ 2986 if (stat64(path, &statbuf) != 0) { 2987 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 2988 cmdname, path, strerror(errno)); 2989 return (1); 2990 } 2991 2992 /* 2993 * Search for the given (major,minor) pair in the mount table. 2994 */ 2995 rewind(mnttab_file); 2996 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) { 2997 if (entry.mnt_major == major(statbuf.st_dev) && 2998 entry.mnt_minor == minor(statbuf.st_dev)) 2999 break; 3000 } 3001 if (ret != 0) { 3002 (void) fprintf(stderr, gettext("cannot %s '%s': not " 3003 "currently mounted\n"), cmdname, path); 3004 return (1); 3005 } 3006 3007 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 3008 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS " 3009 "filesystem\n"), cmdname, path); 3010 return (1); 3011 } 3012 3013 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 3014 ZFS_TYPE_FILESYSTEM)) == NULL) 3015 return (1); 3016 3017 verify(zfs_prop_get(zhp, op == OP_SHARE ? 3018 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, property, 3019 sizeof (property), NULL, NULL, 0, B_FALSE) == 0); 3020 3021 if (op == OP_SHARE) { 3022 if (strcmp(property, "off") == 0) { 3023 (void) fprintf(stderr, gettext("cannot unshare " 3024 "'%s': legacy share\n"), path); 3025 (void) fprintf(stderr, gettext("use " 3026 "unshare(1M) to unshare this filesystem\n")); 3027 ret = 1; 3028 } else if (!zfs_is_shared_nfs(zhp, NULL)) { 3029 (void) fprintf(stderr, gettext("cannot unshare '%s': " 3030 "not currently shared\n"), path); 3031 ret = 1; 3032 } else { 3033 ret = zfs_unshareall_nfs(zhp); 3034 } 3035 } else { 3036 if (is_manual) { 3037 ret = zfs_unmount(zhp, NULL, flags); 3038 } else if (strcmp(property, "legacy") == 0) { 3039 (void) fprintf(stderr, gettext("cannot unmount " 3040 "'%s': legacy mountpoint\n"), 3041 zfs_get_name(zhp)); 3042 (void) fprintf(stderr, gettext("use umount(1M) " 3043 "to unmount this filesystem\n")); 3044 ret = 1; 3045 } else { 3046 ret = zfs_unmountall(zhp, flags); 3047 } 3048 } 3049 3050 zfs_close(zhp); 3051 3052 return (ret != 0); 3053 } 3054 3055 /* 3056 * Generic callback for unsharing or unmounting a filesystem. 3057 */ 3058 static int 3059 unshare_unmount(int op, int argc, char **argv) 3060 { 3061 int do_all = 0; 3062 int flags = 0; 3063 int ret = 0; 3064 int types, c; 3065 zfs_handle_t *zhp; 3066 char property[ZFS_MAXPROPLEN]; 3067 3068 /* check options */ 3069 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) { 3070 switch (c) { 3071 case 'a': 3072 do_all = 1; 3073 break; 3074 case 'f': 3075 flags = MS_FORCE; 3076 break; 3077 case '?': 3078 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3079 optopt); 3080 usage(B_FALSE); 3081 } 3082 } 3083 3084 argc -= optind; 3085 argv += optind; 3086 3087 if (do_all) { 3088 /* 3089 * We could make use of zfs_for_each() to walk all datasets in 3090 * the system, but this would be very inefficient, especially 3091 * since we would have to linearly search /etc/mnttab for each 3092 * one. Instead, do one pass through /etc/mnttab looking for 3093 * zfs entries and call zfs_unmount() for each one. 3094 * 3095 * Things get a little tricky if the administrator has created 3096 * mountpoints beneath other ZFS filesystems. In this case, we 3097 * have to unmount the deepest filesystems first. To accomplish 3098 * this, we place all the mountpoints in an AVL tree sorted by 3099 * the special type (dataset name), and walk the result in 3100 * reverse to make sure to get any snapshots first. 3101 */ 3102 struct mnttab entry; 3103 uu_avl_pool_t *pool; 3104 uu_avl_t *tree; 3105 unshare_unmount_node_t *node; 3106 uu_avl_index_t idx; 3107 uu_avl_walk_t *walk; 3108 3109 if (argc != 0) { 3110 (void) fprintf(stderr, gettext("too many arguments\n")); 3111 usage(B_FALSE); 3112 } 3113 3114 if ((pool = uu_avl_pool_create("unmount_pool", 3115 sizeof (unshare_unmount_node_t), 3116 offsetof(unshare_unmount_node_t, un_avlnode), 3117 unshare_unmount_compare, 3118 UU_DEFAULT)) == NULL) { 3119 (void) fprintf(stderr, gettext("internal error: " 3120 "out of memory\n")); 3121 exit(1); 3122 } 3123 3124 if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) { 3125 (void) fprintf(stderr, gettext("internal error: " 3126 "out of memory\n")); 3127 exit(1); 3128 } 3129 3130 rewind(mnttab_file); 3131 while (getmntent(mnttab_file, &entry) == 0) { 3132 3133 /* ignore non-ZFS entries */ 3134 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 3135 continue; 3136 3137 /* ignore snapshots */ 3138 if (strchr(entry.mnt_special, '@') != NULL) 3139 continue; 3140 3141 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 3142 ZFS_TYPE_FILESYSTEM)) == NULL) { 3143 ret = 1; 3144 continue; 3145 } 3146 3147 verify(zfs_prop_get(zhp, op == OP_SHARE ? 3148 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, 3149 property, sizeof (property), NULL, NULL, 3150 0, B_FALSE) == 0); 3151 3152 /* Ignore legacy mounts and shares */ 3153 if ((op == OP_SHARE && 3154 strcmp(property, "off") == 0) || 3155 (op == OP_MOUNT && 3156 strcmp(property, "legacy") == 0)) { 3157 zfs_close(zhp); 3158 continue; 3159 } 3160 3161 node = safe_malloc(sizeof (unshare_unmount_node_t)); 3162 node->un_zhp = zhp; 3163 3164 if ((node->un_mountp = strdup(entry.mnt_mountp)) == 3165 NULL) { 3166 (void) fprintf(stderr, gettext("internal error:" 3167 " out of memory\n")); 3168 exit(1); 3169 } 3170 3171 uu_avl_node_init(node, &node->un_avlnode, pool); 3172 3173 if (uu_avl_find(tree, node, NULL, &idx) == NULL) { 3174 uu_avl_insert(tree, node, idx); 3175 } else { 3176 zfs_close(node->un_zhp); 3177 free(node->un_mountp); 3178 free(node); 3179 } 3180 } 3181 3182 /* 3183 * Walk the AVL tree in reverse, unmounting each filesystem and 3184 * removing it from the AVL tree in the process. 3185 */ 3186 if ((walk = uu_avl_walk_start(tree, 3187 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) { 3188 (void) fprintf(stderr, 3189 gettext("internal error: out of memory")); 3190 exit(1); 3191 } 3192 3193 while ((node = uu_avl_walk_next(walk)) != NULL) { 3194 uu_avl_remove(tree, node); 3195 3196 switch (op) { 3197 case OP_SHARE: 3198 if (zfs_unshare_nfs(node->un_zhp, 3199 node->un_mountp) != 0) 3200 ret = 1; 3201 break; 3202 3203 case OP_MOUNT: 3204 if (zfs_unmount(node->un_zhp, 3205 node->un_mountp, flags) != 0) 3206 ret = 1; 3207 break; 3208 } 3209 3210 zfs_close(node->un_zhp); 3211 free(node->un_mountp); 3212 free(node); 3213 } 3214 3215 uu_avl_walk_end(walk); 3216 uu_avl_destroy(tree); 3217 uu_avl_pool_destroy(pool); 3218 3219 if (op == OP_SHARE) { 3220 /* 3221 * Finally, unshare any volumes shared via iSCSI. 3222 */ 3223 zfs_handle_t **dslist = NULL; 3224 size_t i, count = 0; 3225 3226 get_all_datasets(ZFS_TYPE_VOLUME, &dslist, &count); 3227 3228 if (count != 0) { 3229 qsort(dslist, count, sizeof (void *), 3230 dataset_cmp); 3231 3232 for (i = 0; i < count; i++) { 3233 if (zfs_unshare_iscsi(dslist[i]) != 0) 3234 ret = 1; 3235 zfs_close(dslist[i]); 3236 } 3237 3238 free(dslist); 3239 } 3240 } 3241 } else { 3242 if (argc != 1) { 3243 if (argc == 0) 3244 (void) fprintf(stderr, 3245 gettext("missing filesystem argument\n")); 3246 else 3247 (void) fprintf(stderr, 3248 gettext("too many arguments\n")); 3249 usage(B_FALSE); 3250 } 3251 3252 /* 3253 * We have an argument, but it may be a full path or a ZFS 3254 * filesystem. Pass full paths off to unmount_path() (shared by 3255 * manual_unmount), otherwise open the filesystem and pass to 3256 * zfs_unmount(). 3257 */ 3258 if (argv[0][0] == '/') 3259 return (unshare_unmount_path(op, argv[0], 3260 flags, B_FALSE)); 3261 3262 types = ZFS_TYPE_FILESYSTEM; 3263 if (op == OP_SHARE) 3264 types |= ZFS_TYPE_VOLUME; 3265 3266 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) 3267 return (1); 3268 3269 if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 3270 verify(zfs_prop_get(zhp, op == OP_SHARE ? 3271 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, property, 3272 sizeof (property), NULL, NULL, 0, B_FALSE) == 0); 3273 3274 switch (op) { 3275 case OP_SHARE: 3276 if (strcmp(property, "off") == 0) { 3277 (void) fprintf(stderr, gettext("cannot " 3278 "unshare '%s': legacy share\n"), 3279 zfs_get_name(zhp)); 3280 (void) fprintf(stderr, gettext("use " 3281 "unshare(1M) to unshare this " 3282 "filesystem\n")); 3283 ret = 1; 3284 } else if (!zfs_is_shared_nfs(zhp, NULL)) { 3285 (void) fprintf(stderr, gettext("cannot " 3286 "unshare '%s': not currently " 3287 "shared\n"), zfs_get_name(zhp)); 3288 ret = 1; 3289 } else if (zfs_unshareall_nfs(zhp) != 0) { 3290 ret = 1; 3291 } 3292 break; 3293 3294 case OP_MOUNT: 3295 if (strcmp(property, "legacy") == 0) { 3296 (void) fprintf(stderr, gettext("cannot " 3297 "unmount '%s': legacy " 3298 "mountpoint\n"), zfs_get_name(zhp)); 3299 (void) fprintf(stderr, gettext("use " 3300 "umount(1M) to unmount this " 3301 "filesystem\n")); 3302 ret = 1; 3303 } else if (!zfs_is_mounted(zhp, NULL)) { 3304 (void) fprintf(stderr, gettext("cannot " 3305 "unmount '%s': not currently " 3306 "mounted\n"), 3307 zfs_get_name(zhp)); 3308 ret = 1; 3309 } else if (zfs_unmountall(zhp, flags) != 0) { 3310 ret = 1; 3311 } 3312 break; 3313 } 3314 } else { 3315 assert(op == OP_SHARE); 3316 3317 verify(zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, property, 3318 sizeof (property), NULL, NULL, 0, B_FALSE) == 0); 3319 3320 if (strcmp(property, "off") == 0) { 3321 (void) fprintf(stderr, gettext("cannot unshare " 3322 "'%s': 'shareiscsi' property not set\n"), 3323 zfs_get_name(zhp)); 3324 (void) fprintf(stderr, gettext("set " 3325 "'shareiscsi' property or use " 3326 "iscsitadm(1M) to share this volume\n")); 3327 ret = 1; 3328 } else if (!zfs_is_shared_iscsi(zhp)) { 3329 (void) fprintf(stderr, gettext("cannot " 3330 "unshare '%s': not currently shared\n"), 3331 zfs_get_name(zhp)); 3332 ret = 1; 3333 } else if (zfs_unshare_iscsi(zhp) != 0) { 3334 ret = 1; 3335 } 3336 } 3337 3338 zfs_close(zhp); 3339 } 3340 3341 return (ret); 3342 } 3343 3344 /* 3345 * zfs unmount -a 3346 * zfs unmount filesystem 3347 * 3348 * Unmount all filesystems, or a specific ZFS filesystem. 3349 */ 3350 static int 3351 zfs_do_unmount(int argc, char **argv) 3352 { 3353 return (unshare_unmount(OP_MOUNT, argc, argv)); 3354 } 3355 3356 /* 3357 * zfs unshare -a 3358 * zfs unshare filesystem 3359 * 3360 * Unshare all filesystems, or a specific ZFS filesystem. 3361 */ 3362 static int 3363 zfs_do_unshare(int argc, char **argv) 3364 { 3365 return (unshare_unmount(OP_SHARE, argc, argv)); 3366 } 3367 3368 /* 3369 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is 3370 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'. 3371 */ 3372 static int 3373 manual_mount(int argc, char **argv) 3374 { 3375 zfs_handle_t *zhp; 3376 char mountpoint[ZFS_MAXPROPLEN]; 3377 char mntopts[MNT_LINE_MAX] = { '\0' }; 3378 int ret; 3379 int c; 3380 int flags = 0; 3381 char *dataset, *path; 3382 3383 /* check options */ 3384 while ((c = getopt(argc, argv, ":mo:O")) != -1) { 3385 switch (c) { 3386 case 'o': 3387 (void) strlcpy(mntopts, optarg, sizeof (mntopts)); 3388 break; 3389 case 'O': 3390 flags |= MS_OVERLAY; 3391 break; 3392 case 'm': 3393 flags |= MS_NOMNTTAB; 3394 break; 3395 case ':': 3396 (void) fprintf(stderr, gettext("missing argument for " 3397 "'%c' option\n"), optopt); 3398 usage(B_FALSE); 3399 break; 3400 case '?': 3401 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3402 optopt); 3403 (void) fprintf(stderr, gettext("usage: mount [-o opts] " 3404 "<path>\n")); 3405 return (2); 3406 } 3407 } 3408 3409 argc -= optind; 3410 argv += optind; 3411 3412 /* check that we only have two arguments */ 3413 if (argc != 2) { 3414 if (argc == 0) 3415 (void) fprintf(stderr, gettext("missing dataset " 3416 "argument\n")); 3417 else if (argc == 1) 3418 (void) fprintf(stderr, 3419 gettext("missing mountpoint argument\n")); 3420 else 3421 (void) fprintf(stderr, gettext("too many arguments\n")); 3422 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n"); 3423 return (2); 3424 } 3425 3426 dataset = argv[0]; 3427 path = argv[1]; 3428 3429 /* try to open the dataset */ 3430 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) 3431 return (1); 3432 3433 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 3434 sizeof (mountpoint), NULL, NULL, 0, B_FALSE); 3435 3436 /* check for legacy mountpoint and complain appropriately */ 3437 ret = 0; 3438 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) { 3439 if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS, 3440 NULL, 0, mntopts, sizeof (mntopts)) != 0) { 3441 (void) fprintf(stderr, gettext("mount failed: %s\n"), 3442 strerror(errno)); 3443 ret = 1; 3444 } 3445 } else { 3446 (void) fprintf(stderr, gettext("filesystem '%s' cannot be " 3447 "mounted using 'mount -F zfs'\n"), dataset); 3448 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' " 3449 "instead.\n"), path); 3450 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' " 3451 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n")); 3452 (void) fprintf(stderr, gettext("See zfs(1M) for more " 3453 "information.\n")); 3454 ret = 1; 3455 } 3456 3457 return (ret); 3458 } 3459 3460 /* 3461 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow 3462 * unmounts of non-legacy filesystems, as this is the dominant administrative 3463 * interface. 3464 */ 3465 static int 3466 manual_unmount(int argc, char **argv) 3467 { 3468 int flags = 0; 3469 int c; 3470 3471 /* check options */ 3472 while ((c = getopt(argc, argv, "f")) != -1) { 3473 switch (c) { 3474 case 'f': 3475 flags = MS_FORCE; 3476 break; 3477 case '?': 3478 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3479 optopt); 3480 (void) fprintf(stderr, gettext("usage: unmount [-f] " 3481 "<path>\n")); 3482 return (2); 3483 } 3484 } 3485 3486 argc -= optind; 3487 argv += optind; 3488 3489 /* check arguments */ 3490 if (argc != 1) { 3491 if (argc == 0) 3492 (void) fprintf(stderr, gettext("missing path " 3493 "argument\n")); 3494 else 3495 (void) fprintf(stderr, gettext("too many arguments\n")); 3496 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n")); 3497 return (2); 3498 } 3499 3500 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE)); 3501 } 3502 3503 static int 3504 volcheck(zpool_handle_t *zhp, void *data) 3505 { 3506 boolean_t isinit = *((boolean_t *)data); 3507 3508 if (isinit) 3509 return (zpool_create_zvol_links(zhp)); 3510 else 3511 return (zpool_remove_zvol_links(zhp)); 3512 } 3513 3514 /* 3515 * Iterate over all pools in the system and either create or destroy /dev/zvol 3516 * links, depending on the value of 'isinit'. 3517 */ 3518 static int 3519 do_volcheck(boolean_t isinit) 3520 { 3521 return (zpool_iter(g_zfs, volcheck, &isinit) ? 1 : 0); 3522 } 3523 3524 static int 3525 find_command_idx(char *command, int *idx) 3526 { 3527 int i; 3528 3529 for (i = 0; i < NCOMMAND; i++) { 3530 if (command_table[i].name == NULL) 3531 continue; 3532 3533 if (strcmp(command, command_table[i].name) == 0) { 3534 *idx = i; 3535 return (0); 3536 } 3537 } 3538 return (1); 3539 } 3540 3541 zfs_prop_t 3542 propset_cb(zfs_prop_t prop, void *data) 3543 { 3544 char *cmdname = (char *)data; 3545 3546 if (strcmp(cmdname, zfs_prop_to_name(prop)) == 0) 3547 return (prop); 3548 3549 return (ZFS_PROP_CONT); 3550 } 3551 3552 int 3553 main(int argc, char **argv) 3554 { 3555 int ret; 3556 int i; 3557 char *progname; 3558 char *cmdname; 3559 char *str; 3560 boolean_t found = B_FALSE; 3561 3562 (void) setlocale(LC_ALL, ""); 3563 (void) textdomain(TEXT_DOMAIN); 3564 3565 opterr = 0; 3566 3567 if ((g_zfs = libzfs_init()) == NULL) { 3568 (void) fprintf(stderr, gettext("internal error: failed to " 3569 "initialize ZFS library\n")); 3570 return (1); 3571 } 3572 3573 zpool_stage_history(g_zfs, argc, argv, B_TRUE, B_FALSE); 3574 3575 libzfs_print_on_error(g_zfs, B_TRUE); 3576 3577 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) { 3578 (void) fprintf(stderr, gettext("internal error: unable to " 3579 "open %s\n"), MNTTAB); 3580 return (1); 3581 } 3582 3583 /* 3584 * This command also doubles as the /etc/fs mount and unmount program. 3585 * Determine if we should take this behavior based on argv[0]. 3586 */ 3587 progname = basename(argv[0]); 3588 if (strcmp(progname, "mount") == 0) { 3589 ret = manual_mount(argc, argv); 3590 } else if (strcmp(progname, "umount") == 0) { 3591 ret = manual_unmount(argc, argv); 3592 } else { 3593 /* 3594 * Make sure the user has specified some command. 3595 */ 3596 if (argc < 2) { 3597 (void) fprintf(stderr, gettext("missing command\n")); 3598 usage(B_FALSE); 3599 } 3600 3601 cmdname = argv[1]; 3602 3603 /* 3604 * The 'umount' command is an alias for 'unmount' 3605 */ 3606 if (strcmp(cmdname, "umount") == 0) 3607 cmdname = "unmount"; 3608 3609 /* 3610 * The 'recv' command is an alias for 'receive' 3611 */ 3612 if (strcmp(cmdname, "recv") == 0) 3613 cmdname = "receive"; 3614 3615 /* 3616 * Special case '-?' 3617 */ 3618 if (strcmp(cmdname, "-?") == 0) 3619 usage(B_TRUE); 3620 3621 /* 3622 * 'volinit' and 'volfini' do not appear in the usage message, 3623 * so we have to special case them here. 3624 */ 3625 if (strcmp(cmdname, "volinit") == 0) 3626 return (do_volcheck(B_TRUE)); 3627 else if (strcmp(cmdname, "volfini") == 0) 3628 return (do_volcheck(B_FALSE)); 3629 3630 /* 3631 * Run the appropriate command. 3632 */ 3633 if (find_command_idx(cmdname, &i) == 0) { 3634 current_command = &command_table[i]; 3635 ret = command_table[i].func(argc - 1, argv + 1); 3636 found = B_TRUE; 3637 } 3638 3639 /* 3640 * Check and see if they are doing property=value 3641 */ 3642 if (found == B_FALSE && 3643 ((str = strchr(cmdname, '=')) != NULL)) { 3644 *str = '\0'; 3645 if (zfs_prop_iter(propset_cb, cmdname, 3646 B_FALSE) != ZFS_PROP_INVAL) 3647 found = B_TRUE; 3648 3649 if (found == B_FALSE && zfs_prop_user(cmdname)) 3650 found = B_TRUE; 3651 3652 if (found == B_TRUE && 3653 find_command_idx("set", &i) == 0) { 3654 *str = '='; 3655 current_command = &command_table[i]; 3656 ret = command_table[i].func(argc, argv); 3657 } else { 3658 (void) fprintf(stderr, 3659 gettext("invalid property '%s'\n"), 3660 cmdname); 3661 found = B_TRUE; 3662 ret = 1; 3663 } 3664 3665 } 3666 3667 if (found == B_FALSE) { 3668 (void) fprintf(stderr, gettext("unrecognized " 3669 "command '%s'\n"), cmdname); 3670 usage(B_FALSE); 3671 } 3672 } 3673 3674 (void) fclose(mnttab_file); 3675 3676 libzfs_fini(g_zfs); 3677 3678 /* 3679 * The 'ZFS_ABORT' environment variable causes us to dump core on exit 3680 * for the purposes of running ::findleaks. 3681 */ 3682 if (getenv("ZFS_ABORT") != NULL) { 3683 (void) printf("dumping core by request\n"); 3684 abort(); 3685 } 3686 3687 return (ret); 3688 } 3689