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