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