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