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