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 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <assert.h> 29 #include <errno.h> 30 #include <libgen.h> 31 #include <libintl.h> 32 #include <libuutil.h> 33 #include <locale.h> 34 #include <stddef.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <strings.h> 38 #include <unistd.h> 39 #include <fcntl.h> 40 #include <zone.h> 41 #include <sys/mkdev.h> 42 #include <sys/mntent.h> 43 #include <sys/mnttab.h> 44 #include <sys/mount.h> 45 #include <sys/stat.h> 46 47 #include <libzfs.h> 48 49 #include "zfs_iter.h" 50 #include "zfs_util.h" 51 52 libzfs_handle_t *g_zfs; 53 54 static FILE *mnttab_file; 55 56 static int zfs_do_clone(int argc, char **argv); 57 static int zfs_do_create(int argc, char **argv); 58 static int zfs_do_destroy(int argc, char **argv); 59 static int zfs_do_get(int argc, char **argv); 60 static int zfs_do_inherit(int argc, char **argv); 61 static int zfs_do_list(int argc, char **argv); 62 static int zfs_do_mount(int argc, char **argv); 63 static int zfs_do_rename(int argc, char **argv); 64 static int zfs_do_rollback(int argc, char **argv); 65 static int zfs_do_set(int argc, char **argv); 66 static int zfs_do_snapshot(int argc, char **argv); 67 static int zfs_do_unmount(int argc, char **argv); 68 static int zfs_do_share(int argc, char **argv); 69 static int zfs_do_unshare(int argc, char **argv); 70 static int zfs_do_send(int argc, char **argv); 71 static int zfs_do_receive(int argc, char **argv); 72 static int zfs_do_promote(int argc, char **argv); 73 74 /* 75 * These libumem hooks provide a reasonable set of defaults for the allocator's 76 * debugging facilities. 77 */ 78 const char * 79 _umem_debug_init() 80 { 81 return ("default,verbose"); /* $UMEM_DEBUG setting */ 82 } 83 84 const char * 85 _umem_logging_init(void) 86 { 87 return ("fail,contents"); /* $UMEM_LOGGING setting */ 88 } 89 90 typedef enum { 91 HELP_CLONE, 92 HELP_CREATE, 93 HELP_DESTROY, 94 HELP_GET, 95 HELP_INHERIT, 96 HELP_LIST, 97 HELP_MOUNT, 98 HELP_PROMOTE, 99 HELP_RECEIVE, 100 HELP_RENAME, 101 HELP_ROLLBACK, 102 HELP_SEND, 103 HELP_SET, 104 HELP_SHARE, 105 HELP_SNAPSHOT, 106 HELP_UNMOUNT, 107 HELP_UNSHARE 108 } zfs_help_t; 109 110 typedef struct zfs_command { 111 const char *name; 112 int (*func)(int argc, char **argv); 113 zfs_help_t usage; 114 } zfs_command_t; 115 116 /* 117 * Master command table. Each ZFS command has a name, associated function, and 118 * usage message. The usage messages need to be internationalized, so we have 119 * to have a function to return the usage message based on a command index. 120 * 121 * These commands are organized according to how they are displayed in the usage 122 * message. An empty command (one with a NULL name) indicates an empty line in 123 * the generic usage message. 124 */ 125 static zfs_command_t command_table[] = { 126 { "create", zfs_do_create, HELP_CREATE }, 127 { "destroy", zfs_do_destroy, HELP_DESTROY }, 128 { NULL }, 129 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT }, 130 { "rollback", zfs_do_rollback, HELP_ROLLBACK }, 131 { "clone", zfs_do_clone, HELP_CLONE }, 132 { "promote", zfs_do_promote, HELP_PROMOTE }, 133 { "rename", zfs_do_rename, HELP_RENAME }, 134 { NULL }, 135 { "list", zfs_do_list, HELP_LIST }, 136 { NULL }, 137 { "set", zfs_do_set, HELP_SET }, 138 { "get", zfs_do_get, HELP_GET }, 139 { "inherit", zfs_do_inherit, HELP_INHERIT }, 140 { NULL }, 141 { "mount", zfs_do_mount, HELP_MOUNT }, 142 { NULL }, 143 { "unmount", zfs_do_unmount, HELP_UNMOUNT }, 144 { NULL }, 145 { "share", zfs_do_share, HELP_SHARE }, 146 { NULL }, 147 { "unshare", zfs_do_unshare, HELP_UNSHARE }, 148 { NULL }, 149 { "send", zfs_do_send, HELP_SEND }, 150 { "receive", zfs_do_receive, HELP_RECEIVE }, 151 }; 152 153 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0])) 154 155 zfs_command_t *current_command; 156 157 static const char * 158 get_usage(zfs_help_t idx) 159 { 160 switch (idx) { 161 case HELP_CLONE: 162 return (gettext("\tclone <snapshot> <filesystem|volume>\n")); 163 case HELP_CREATE: 164 return (gettext("\tcreate <filesystem>\n" 165 "\tcreate [-s] [-b blocksize] -V <size> <volume>\n")); 166 case HELP_DESTROY: 167 return (gettext("\tdestroy [-rRf] " 168 "<filesystem|volume|snapshot>\n")); 169 case HELP_GET: 170 return (gettext("\tget [-rHp] [-o field[,field]...] " 171 "[-s source[,source]...]\n" 172 "\t <all | property[,property]...> " 173 "<filesystem|volume|snapshot> ...\n")); 174 case HELP_INHERIT: 175 return (gettext("\tinherit [-r] <property> " 176 "<filesystem|volume> ...\n")); 177 case HELP_LIST: 178 return (gettext("\tlist [-rH] [-o property[,property]...] " 179 "[-t type[,type]...]\n" 180 "\t [-s property [-s property]...]" 181 " [-S property [-S property]...]\n" 182 "\t [filesystem|volume|snapshot] ...\n")); 183 case HELP_MOUNT: 184 return (gettext("\tmount\n" 185 "\tmount [-o opts] [-O] -a\n" 186 "\tmount [-o opts] [-O] <filesystem>\n")); 187 case HELP_PROMOTE: 188 return (gettext("\tpromote <clone filesystem>\n")); 189 case HELP_RECEIVE: 190 return (gettext("\treceive [-vn] <filesystem|volume|snapshot>\n" 191 "\treceive [-vn] -d <filesystem>\n")); 192 case HELP_RENAME: 193 return (gettext("\trename <filesystem|volume|snapshot> " 194 "<filesystem|volume|snapshot>\n")); 195 case HELP_ROLLBACK: 196 return (gettext("\trollback [-rRf] <snapshot>\n")); 197 case HELP_SEND: 198 return (gettext("\tsend [-i <snapshot>] <snapshot>\n")); 199 case HELP_SET: 200 return (gettext("\tset <property=value> " 201 "<filesystem|volume> ...\n")); 202 case HELP_SHARE: 203 return (gettext("\tshare -a\n" 204 "\tshare <filesystem>\n")); 205 case HELP_SNAPSHOT: 206 return (gettext("\tsnapshot [-r] " 207 "<filesystem@name|volume@name>\n")); 208 case HELP_UNMOUNT: 209 return (gettext("\tunmount [-f] -a\n" 210 "\tunmount [-f] <filesystem|mountpoint>\n")); 211 case HELP_UNSHARE: 212 return (gettext("\tunshare [-f] -a\n" 213 "\tunshare [-f] <filesystem|mountpoint>\n")); 214 } 215 216 abort(); 217 /* NOTREACHED */ 218 } 219 220 /* 221 * Utility function to guarantee malloc() success. 222 */ 223 void * 224 safe_malloc(size_t size) 225 { 226 void *data; 227 228 if ((data = calloc(1, size)) == NULL) { 229 (void) fprintf(stderr, "internal error: out of memory\n"); 230 exit(1); 231 } 232 233 return (data); 234 } 235 236 /* 237 * Display usage message. If we're inside a command, display only the usage for 238 * that command. Otherwise, iterate over the entire command table and display 239 * a complete usage message. 240 */ 241 static void 242 usage(boolean_t requested) 243 { 244 int i; 245 boolean_t show_properties = B_FALSE; 246 FILE *fp = requested ? stdout : stderr; 247 248 if (current_command == NULL) { 249 250 (void) fprintf(fp, gettext("usage: zfs command args ...\n")); 251 (void) fprintf(fp, 252 gettext("where 'command' is one of the following:\n\n")); 253 254 for (i = 0; i < NCOMMAND; i++) { 255 if (command_table[i].name == NULL) 256 (void) fprintf(fp, "\n"); 257 else 258 (void) fprintf(fp, "%s", 259 get_usage(command_table[i].usage)); 260 } 261 262 (void) fprintf(fp, gettext("\nEach dataset is of the form: " 263 "pool/[dataset/]*dataset[@name]\n")); 264 } else { 265 (void) fprintf(fp, gettext("usage:\n")); 266 (void) fprintf(fp, "%s", get_usage(current_command->usage)); 267 } 268 269 if (current_command != NULL && 270 (strcmp(current_command->name, "set") == 0 || 271 strcmp(current_command->name, "get") == 0 || 272 strcmp(current_command->name, "inherit") == 0 || 273 strcmp(current_command->name, "list") == 0)) 274 show_properties = B_TRUE; 275 276 if (show_properties) { 277 278 (void) fprintf(fp, 279 gettext("\nThe following properties are supported:\n")); 280 281 (void) fprintf(fp, "\n\t%-13s %s %s %s\n\n", 282 "PROPERTY", "EDIT", "INHERIT", "VALUES"); 283 284 for (i = 0; i < ZFS_NPROP_VISIBLE; i++) { 285 (void) fprintf(fp, "\t%-13s ", zfs_prop_to_name(i)); 286 287 if (zfs_prop_readonly(i)) 288 (void) fprintf(fp, " NO "); 289 else 290 (void) fprintf(fp, " YES "); 291 292 if (zfs_prop_inheritable(i)) 293 (void) fprintf(fp, " YES "); 294 else 295 (void) fprintf(fp, " NO "); 296 297 if (zfs_prop_values(i) == NULL) 298 (void) fprintf(fp, "-\n"); 299 else 300 (void) fprintf(fp, "%s\n", zfs_prop_values(i)); 301 } 302 (void) fprintf(fp, gettext("\nSizes are specified in bytes " 303 "with standard units such as K, M, G, etc.\n")); 304 } else { 305 /* 306 * TRANSLATION NOTE: 307 * "zfs set|get" must not be localised this is the 308 * command name and arguments. 309 */ 310 (void) fprintf(fp, 311 gettext("\nFor the property list, run: zfs set|get\n")); 312 } 313 314 exit(requested ? 0 : 2); 315 } 316 317 /* 318 * zfs clone <fs, snap, vol> fs 319 * 320 * Given an existing dataset, create a writable copy whose initial contents 321 * are the same as the source. The newly created dataset maintains a 322 * dependency on the original; the original cannot be destroyed so long as 323 * the clone exists. 324 */ 325 static int 326 zfs_do_clone(int argc, char **argv) 327 { 328 zfs_handle_t *zhp; 329 int ret; 330 331 /* check options */ 332 if (argc > 1 && argv[1][0] == '-') { 333 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 334 argv[1][1]); 335 usage(B_FALSE); 336 } 337 338 /* check number of arguments */ 339 if (argc < 2) { 340 (void) fprintf(stderr, gettext("missing source dataset " 341 "argument\n")); 342 usage(B_FALSE); 343 } 344 if (argc < 3) { 345 (void) fprintf(stderr, gettext("missing target dataset " 346 "argument\n")); 347 usage(B_FALSE); 348 } 349 if (argc > 3) { 350 (void) fprintf(stderr, gettext("too many arguments\n")); 351 usage(B_FALSE); 352 } 353 354 /* open the source dataset */ 355 if ((zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_SNAPSHOT)) == NULL) 356 return (1); 357 358 /* pass to libzfs */ 359 ret = zfs_clone(zhp, argv[2]); 360 361 /* create the mountpoint if necessary */ 362 if (ret == 0) { 363 zfs_handle_t *clone = zfs_open(g_zfs, argv[2], ZFS_TYPE_ANY); 364 if (clone != NULL) { 365 if ((ret = zfs_mount(clone, NULL, 0)) == 0) 366 ret = zfs_share(clone); 367 zfs_close(clone); 368 } 369 } 370 371 zfs_close(zhp); 372 373 return (ret == 0 ? 0 : 1); 374 } 375 376 /* 377 * zfs create fs 378 * zfs create [-s] [-b blocksize] -V vol size 379 * 380 * Create a new dataset. This command can be used to create filesystems 381 * and volumes. Snapshot creation is handled by 'zfs snapshot'. 382 * For volumes, the user must specify a size to be used. 383 * 384 * The '-s' flag applies only to volumes, and indicates that we should not try 385 * to set the reservation for this volume. By default we set a reservation 386 * equal to the size for any volume. 387 */ 388 static int 389 zfs_do_create(int argc, char **argv) 390 { 391 zfs_type_t type = ZFS_TYPE_FILESYSTEM; 392 zfs_handle_t *zhp; 393 char *size = NULL; 394 char *blocksize = NULL; 395 int c; 396 boolean_t noreserve = B_FALSE; 397 int ret; 398 399 /* check options */ 400 while ((c = getopt(argc, argv, ":V:b:s")) != -1) { 401 switch (c) { 402 case 'V': 403 type = ZFS_TYPE_VOLUME; 404 size = optarg; 405 break; 406 case 'b': 407 blocksize = optarg; 408 break; 409 case 's': 410 noreserve = B_TRUE; 411 break; 412 case ':': 413 (void) fprintf(stderr, gettext("missing size " 414 "argument\n")); 415 usage(B_FALSE); 416 break; 417 case '?': 418 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 419 optopt); 420 usage(B_FALSE); 421 } 422 } 423 424 if (noreserve && type != ZFS_TYPE_VOLUME) { 425 (void) fprintf(stderr, gettext("'-s' can only be used when " 426 "creating a volume\n")); 427 usage(B_FALSE); 428 } 429 430 argc -= optind; 431 argv += optind; 432 433 /* check number of arguments */ 434 if (argc == 0) { 435 (void) fprintf(stderr, gettext("missing %s argument\n"), 436 zfs_type_to_name(type)); 437 usage(B_FALSE); 438 } 439 if (argc > 1) { 440 (void) fprintf(stderr, gettext("too many arguments\n")); 441 usage(B_FALSE); 442 } 443 444 /* pass to libzfs */ 445 if (zfs_create(g_zfs, argv[0], type, size, blocksize) != 0) 446 return (1); 447 448 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_ANY)) == NULL) 449 return (1); 450 451 /* 452 * Volume handling. By default, we try to create a reservation of equal 453 * size for the volume. If we can't do this, then destroy the dataset 454 * and report an error. 455 */ 456 if (type == ZFS_TYPE_VOLUME && !noreserve) { 457 if (zfs_prop_set(zhp, ZFS_PROP_RESERVATION, size) != 0) { 458 (void) fprintf(stderr, gettext("use '-s' to create a " 459 "volume without a matching reservation\n")); 460 (void) zfs_destroy(zhp); 461 return (1); 462 } 463 } 464 465 /* 466 * Mount and/or share the new filesystem as appropriate. We provide a 467 * verbose error message to let the user know that their filesystem was 468 * in fact created, even if we failed to mount or share it. 469 */ 470 if (zfs_mount(zhp, NULL, 0) != 0) { 471 (void) fprintf(stderr, gettext("filesystem successfully " 472 "created, but not mounted\n")); 473 ret = 1; 474 } else if (zfs_share(zhp) != 0) { 475 (void) fprintf(stderr, gettext("filesystem successfully " 476 "created, but not shared\n")); 477 ret = 1; 478 } else { 479 ret = 0; 480 } 481 482 zfs_close(zhp); 483 return (ret); 484 } 485 486 /* 487 * zfs destroy [-rf] <fs, snap, vol> 488 * 489 * -r Recursively destroy all children 490 * -R Recursively destroy all dependents, including clones 491 * -f Force unmounting of any dependents 492 * 493 * Destroys the given dataset. By default, it will unmount any filesystems, 494 * and refuse to destroy a dataset that has any dependents. A dependent can 495 * either be a child, or a clone of a child. 496 */ 497 typedef struct destroy_cbdata { 498 boolean_t cb_first; 499 int cb_force; 500 int cb_recurse; 501 int cb_error; 502 int cb_needforce; 503 int cb_doclones; 504 zfs_handle_t *cb_target; 505 char *cb_snapname; 506 } destroy_cbdata_t; 507 508 /* 509 * Check for any dependents based on the '-r' or '-R' flags. 510 */ 511 static int 512 destroy_check_dependent(zfs_handle_t *zhp, void *data) 513 { 514 destroy_cbdata_t *cbp = data; 515 const char *tname = zfs_get_name(cbp->cb_target); 516 const char *name = zfs_get_name(zhp); 517 518 if (strncmp(tname, name, strlen(tname)) == 0 && 519 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) { 520 /* 521 * This is a direct descendant, not a clone somewhere else in 522 * the hierarchy. 523 */ 524 if (cbp->cb_recurse) 525 goto out; 526 527 if (cbp->cb_first) { 528 (void) fprintf(stderr, gettext("cannot destroy '%s': " 529 "%s has children\n"), 530 zfs_get_name(cbp->cb_target), 531 zfs_type_to_name(zfs_get_type(cbp->cb_target))); 532 (void) fprintf(stderr, gettext("use '-r' to destroy " 533 "the following datasets:\n")); 534 cbp->cb_first = B_FALSE; 535 cbp->cb_error = 1; 536 } 537 538 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 539 } else { 540 /* 541 * This is a clone. We only want to report this if the '-r' 542 * wasn't specified, or the target is a snapshot. 543 */ 544 if (!cbp->cb_recurse && 545 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT) 546 goto out; 547 548 if (cbp->cb_first) { 549 (void) fprintf(stderr, gettext("cannot destroy '%s': " 550 "%s has dependent clones\n"), 551 zfs_get_name(cbp->cb_target), 552 zfs_type_to_name(zfs_get_type(cbp->cb_target))); 553 (void) fprintf(stderr, gettext("use '-R' to destroy " 554 "the following datasets:\n")); 555 cbp->cb_first = B_FALSE; 556 cbp->cb_error = 1; 557 } 558 559 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 560 } 561 562 out: 563 zfs_close(zhp); 564 return (0); 565 } 566 567 static int 568 destroy_callback(zfs_handle_t *zhp, void *data) 569 { 570 destroy_cbdata_t *cbp = data; 571 572 /* 573 * Ignore pools (which we've already flagged as an error before getting 574 * here. 575 */ 576 if (strchr(zfs_get_name(zhp), '/') == NULL && 577 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 578 zfs_close(zhp); 579 return (0); 580 } 581 582 /* 583 * Bail out on the first error. 584 */ 585 if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 || 586 zfs_destroy(zhp) != 0) { 587 zfs_close(zhp); 588 return (-1); 589 } 590 591 zfs_close(zhp); 592 return (0); 593 } 594 595 static int 596 destroy_snap_clones(zfs_handle_t *zhp, void *arg) 597 { 598 destroy_cbdata_t *cbp = arg; 599 char thissnap[MAXPATHLEN]; 600 zfs_handle_t *szhp; 601 602 (void) snprintf(thissnap, sizeof (thissnap), 603 "%s@%s", zfs_get_name(zhp), cbp->cb_snapname); 604 605 libzfs_print_on_error(g_zfs, B_FALSE); 606 szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT); 607 libzfs_print_on_error(g_zfs, B_TRUE); 608 if (szhp) { 609 /* 610 * Destroy any clones of this snapshot 611 */ 612 if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback, 613 cbp) != 0) { 614 zfs_close(szhp); 615 return (-1); 616 } 617 zfs_close(szhp); 618 } 619 620 return (zfs_iter_filesystems(zhp, destroy_snap_clones, arg)); 621 } 622 623 static int 624 zfs_do_destroy(int argc, char **argv) 625 { 626 destroy_cbdata_t cb = { 0 }; 627 int c; 628 zfs_handle_t *zhp; 629 char *cp; 630 631 /* check options */ 632 while ((c = getopt(argc, argv, "frR")) != -1) { 633 switch (c) { 634 case 'f': 635 cb.cb_force = 1; 636 break; 637 case 'r': 638 cb.cb_recurse = 1; 639 break; 640 case 'R': 641 cb.cb_recurse = 1; 642 cb.cb_doclones = 1; 643 break; 644 case '?': 645 default: 646 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 647 optopt); 648 usage(B_FALSE); 649 } 650 } 651 652 argc -= optind; 653 argv += optind; 654 655 /* check number of arguments */ 656 if (argc == 0) { 657 (void) fprintf(stderr, gettext("missing path argument\n")); 658 usage(B_FALSE); 659 } 660 if (argc > 1) { 661 (void) fprintf(stderr, gettext("too many arguments\n")); 662 usage(B_FALSE); 663 } 664 665 /* 666 * If we are doing recursive destroy of a snapshot, then the 667 * named snapshot may not exist. Go straight to libzfs. 668 */ 669 if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) { 670 int ret; 671 672 *cp = '\0'; 673 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_ANY)) == NULL) 674 return (1); 675 *cp = '@'; 676 cp++; 677 678 if (cb.cb_doclones) { 679 cb.cb_snapname = cp; 680 if (destroy_snap_clones(zhp, &cb) != 0) { 681 zfs_close(zhp); 682 return (1); 683 } 684 } 685 686 ret = zfs_destroy_snaps(zhp, cp); 687 zfs_close(zhp); 688 if (ret) { 689 (void) fprintf(stderr, 690 gettext("no snapshots destroyed\n")); 691 } 692 return (ret != 0); 693 } 694 695 696 /* Open the given dataset */ 697 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_ANY)) == NULL) 698 return (1); 699 700 cb.cb_target = zhp; 701 702 /* 703 * Perform an explicit check for pools before going any further. 704 */ 705 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL && 706 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 707 (void) fprintf(stderr, gettext("cannot destroy '%s': " 708 "operation does not apply to pools\n"), 709 zfs_get_name(zhp)); 710 (void) fprintf(stderr, gettext("use 'zfs destroy -r " 711 "%s' to destroy all datasets in the pool\n"), 712 zfs_get_name(zhp)); 713 (void) fprintf(stderr, gettext("use 'zpool destroy %s' " 714 "to destroy the pool itself\n"), zfs_get_name(zhp)); 715 zfs_close(zhp); 716 return (1); 717 } 718 719 /* 720 * Check for any dependents and/or clones. 721 */ 722 cb.cb_first = B_TRUE; 723 if (!cb.cb_doclones && 724 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent, 725 &cb) != 0) { 726 zfs_close(zhp); 727 return (1); 728 } 729 730 731 if (cb.cb_error || 732 zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0) { 733 zfs_close(zhp); 734 return (1); 735 } 736 737 /* 738 * Do the real thing. The callback will close the handle regardless of 739 * whether it succeeds or not. 740 */ 741 if (destroy_callback(zhp, &cb) != 0) 742 return (1); 743 744 return (0); 745 } 746 747 /* 748 * zfs get [-rHp] [-o field[,field]...] [-s source[,source]...] 749 * < all | property[,property]... > < fs | snap | vol > ... 750 * 751 * -r recurse over any child datasets 752 * -H scripted mode. Headers are stripped, and fields are separated 753 * by tabs instead of spaces. 754 * -o Set of fields to display. One of "name,property,value,source". 755 * Default is all four. 756 * -s Set of sources to allow. One of 757 * "local,default,inherited,temporary,none". Default is all 758 * five. 759 * -p Display values in parsable (literal) format. 760 * 761 * Prints properties for the given datasets. The user can control which 762 * columns to display as well as which property types to allow. 763 */ 764 typedef struct get_cbdata { 765 int cb_sources; 766 int cb_columns[4]; 767 int cb_nprop; 768 boolean_t cb_scripted; 769 boolean_t cb_literal; 770 boolean_t cb_isall; 771 zfs_prop_t cb_prop[ZFS_NPROP_ALL]; 772 } get_cbdata_t; 773 774 #define GET_COL_NAME 1 775 #define GET_COL_PROPERTY 2 776 #define GET_COL_VALUE 3 777 #define GET_COL_SOURCE 4 778 779 /* 780 * Display a single line of output, according to the settings in the callback 781 * structure. 782 */ 783 static void 784 print_one_property(zfs_handle_t *zhp, get_cbdata_t *cbp, zfs_prop_t prop, 785 const char *value, zfs_source_t sourcetype, const char *source) 786 { 787 int i; 788 int width; 789 const char *str; 790 char buf[128]; 791 792 /* 793 * Ignore those source types that the user has chosen to ignore. 794 */ 795 if ((sourcetype & cbp->cb_sources) == 0) 796 return; 797 798 for (i = 0; i < 4; i++) { 799 switch (cbp->cb_columns[i]) { 800 case GET_COL_NAME: 801 width = 15; 802 str = zfs_get_name(zhp); 803 break; 804 805 case GET_COL_PROPERTY: 806 width = 13; 807 str = zfs_prop_to_name(prop); 808 break; 809 810 case GET_COL_VALUE: 811 width = 25; 812 str = value; 813 break; 814 815 case GET_COL_SOURCE: 816 width = 15; 817 switch (sourcetype) { 818 case ZFS_SRC_NONE: 819 str = "-"; 820 break; 821 822 case ZFS_SRC_DEFAULT: 823 str = "default"; 824 break; 825 826 case ZFS_SRC_LOCAL: 827 str = "local"; 828 break; 829 830 case ZFS_SRC_TEMPORARY: 831 str = "temporary"; 832 break; 833 834 case ZFS_SRC_INHERITED: 835 (void) snprintf(buf, sizeof (buf), 836 "inherited from %s", source); 837 str = buf; 838 break; 839 } 840 break; 841 842 default: 843 continue; 844 } 845 846 if (cbp->cb_columns[i + 1] == 0) 847 (void) printf("%s", str); 848 else if (cbp->cb_scripted) 849 (void) printf("%s\t", str); 850 else 851 (void) printf("%-*s ", width, str); 852 853 } 854 855 (void) printf("\n"); 856 } 857 858 /* 859 * Invoked to display the properties for a single dataset. 860 */ 861 static int 862 get_callback(zfs_handle_t *zhp, void *data) 863 { 864 char buf[ZFS_MAXPROPLEN]; 865 zfs_source_t sourcetype; 866 char source[ZFS_MAXNAMELEN]; 867 get_cbdata_t *cbp = data; 868 int i; 869 870 for (i = 0; i < cbp->cb_nprop; i++) { 871 if (zfs_prop_get(zhp, cbp->cb_prop[i], buf, 872 sizeof (buf), &sourcetype, source, sizeof (source), 873 cbp->cb_literal) != 0) { 874 if (cbp->cb_isall) 875 continue; 876 (void) strlcpy(buf, "-", sizeof (buf)); 877 sourcetype = ZFS_SRC_NONE; 878 } 879 880 print_one_property(zhp, cbp, cbp->cb_prop[i], 881 buf, sourcetype, source); 882 } 883 884 return (0); 885 } 886 887 static int 888 zfs_do_get(int argc, char **argv) 889 { 890 get_cbdata_t cb = { 0 }; 891 boolean_t recurse = B_FALSE; 892 int c; 893 char *value, *fields, *badopt; 894 int i; 895 int ret; 896 897 /* 898 * Set up default columns and sources. 899 */ 900 cb.cb_sources = ZFS_SRC_ALL; 901 cb.cb_columns[0] = GET_COL_NAME; 902 cb.cb_columns[1] = GET_COL_PROPERTY; 903 cb.cb_columns[2] = GET_COL_VALUE; 904 cb.cb_columns[3] = GET_COL_SOURCE; 905 906 /* check options */ 907 while ((c = getopt(argc, argv, ":o:s:rHp")) != -1) { 908 switch (c) { 909 case 'p': 910 cb.cb_literal = B_TRUE; 911 break; 912 case 'r': 913 recurse = B_TRUE; 914 break; 915 case 'H': 916 cb.cb_scripted = B_TRUE; 917 break; 918 case ':': 919 (void) fprintf(stderr, gettext("missing argument for " 920 "'%c' option\n"), optopt); 921 usage(B_FALSE); 922 break; 923 case 'o': 924 /* 925 * Process the set of columns to display. We zero out 926 * the structure to give us a blank slate. 927 */ 928 bzero(&cb.cb_columns, sizeof (cb.cb_columns)); 929 i = 0; 930 while (*optarg != '\0') { 931 static char *col_subopts[] = 932 { "name", "property", "value", "source", 933 NULL }; 934 935 if (i == 4) { 936 (void) fprintf(stderr, gettext("too " 937 "many fields given to -o " 938 "option\n")); 939 usage(B_FALSE); 940 } 941 942 switch (getsubopt(&optarg, col_subopts, 943 &value)) { 944 case 0: 945 cb.cb_columns[i++] = GET_COL_NAME; 946 break; 947 case 1: 948 cb.cb_columns[i++] = GET_COL_PROPERTY; 949 break; 950 case 2: 951 cb.cb_columns[i++] = GET_COL_VALUE; 952 break; 953 case 3: 954 cb.cb_columns[i++] = GET_COL_SOURCE; 955 break; 956 default: 957 (void) fprintf(stderr, 958 gettext("invalid column name " 959 "'%s'\n"), value); 960 usage(B_FALSE); 961 } 962 } 963 break; 964 965 case 's': 966 cb.cb_sources = 0; 967 while (*optarg != '\0') { 968 static char *source_subopts[] = { 969 "local", "default", "inherited", 970 "temporary", "none", NULL }; 971 972 switch (getsubopt(&optarg, source_subopts, 973 &value)) { 974 case 0: 975 cb.cb_sources |= ZFS_SRC_LOCAL; 976 break; 977 case 1: 978 cb.cb_sources |= ZFS_SRC_DEFAULT; 979 break; 980 case 2: 981 cb.cb_sources |= ZFS_SRC_INHERITED; 982 break; 983 case 3: 984 cb.cb_sources |= ZFS_SRC_TEMPORARY; 985 break; 986 case 4: 987 cb.cb_sources |= ZFS_SRC_NONE; 988 break; 989 default: 990 (void) fprintf(stderr, 991 gettext("invalid source " 992 "'%s'\n"), value); 993 usage(B_FALSE); 994 } 995 } 996 break; 997 998 case '?': 999 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1000 optopt); 1001 usage(B_FALSE); 1002 } 1003 } 1004 1005 argc -= optind; 1006 argv += optind; 1007 1008 if (argc < 1) { 1009 (void) fprintf(stderr, gettext("missing property " 1010 "argument\n")); 1011 usage(B_FALSE); 1012 } 1013 1014 fields = argv[0]; 1015 1016 /* 1017 * If the user specifies 'all', the behavior of 'zfs get' is slightly 1018 * different, because we don't show properties which don't apply to the 1019 * given dataset. 1020 */ 1021 if (strcmp(fields, "all") == 0) 1022 cb.cb_isall = B_TRUE; 1023 1024 if ((ret = zfs_get_proplist(fields, cb.cb_prop, ZFS_NPROP_ALL, 1025 &cb.cb_nprop, &badopt)) != 0) { 1026 if (ret == EINVAL) 1027 (void) fprintf(stderr, gettext("invalid property " 1028 "'%s'\n"), badopt); 1029 else 1030 (void) fprintf(stderr, gettext("too many properties " 1031 "specified\n")); 1032 usage(B_FALSE); 1033 } 1034 1035 argc--; 1036 argv++; 1037 1038 /* check for at least one dataset name */ 1039 if (argc < 1) { 1040 (void) fprintf(stderr, gettext("missing dataset argument\n")); 1041 usage(B_FALSE); 1042 } 1043 1044 /* 1045 * Print out any headers 1046 */ 1047 if (!cb.cb_scripted) { 1048 int i; 1049 for (i = 0; i < 4; i++) { 1050 switch (cb.cb_columns[i]) { 1051 case GET_COL_NAME: 1052 (void) printf("%-15s ", "NAME"); 1053 break; 1054 case GET_COL_PROPERTY: 1055 (void) printf("%-13s ", "PROPERTY"); 1056 break; 1057 case GET_COL_VALUE: 1058 (void) printf("%-25s ", "VALUE"); 1059 break; 1060 case GET_COL_SOURCE: 1061 (void) printf("%s", "SOURCE"); 1062 break; 1063 } 1064 } 1065 (void) printf("\n"); 1066 } 1067 1068 /* run for each object */ 1069 return (zfs_for_each(argc, argv, recurse, ZFS_TYPE_ANY, NULL, 1070 get_callback, &cb)); 1071 1072 } 1073 1074 /* 1075 * inherit [-r] <property> <fs|vol> ... 1076 * 1077 * -r Recurse over all children 1078 * 1079 * For each dataset specified on the command line, inherit the given property 1080 * from its parent. Inheriting a property at the pool level will cause it to 1081 * use the default value. The '-r' flag will recurse over all children, and is 1082 * useful for setting a property on a hierarchy-wide basis, regardless of any 1083 * local modifications for each dataset. 1084 */ 1085 static int 1086 inherit_callback(zfs_handle_t *zhp, void *data) 1087 { 1088 zfs_prop_t prop = (zfs_prop_t)data; 1089 1090 return (zfs_prop_inherit(zhp, prop) != 0); 1091 } 1092 1093 static int 1094 zfs_do_inherit(int argc, char **argv) 1095 { 1096 boolean_t recurse = B_FALSE; 1097 int c; 1098 zfs_prop_t prop; 1099 char *propname; 1100 1101 /* check options */ 1102 while ((c = getopt(argc, argv, "r")) != -1) { 1103 switch (c) { 1104 case 'r': 1105 recurse = B_TRUE; 1106 break; 1107 case '?': 1108 default: 1109 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1110 optopt); 1111 usage(B_FALSE); 1112 } 1113 } 1114 1115 argc -= optind; 1116 argv += optind; 1117 1118 /* check number of arguments */ 1119 if (argc < 1) { 1120 (void) fprintf(stderr, gettext("missing property argument\n")); 1121 usage(B_FALSE); 1122 } 1123 if (argc < 2) { 1124 (void) fprintf(stderr, gettext("missing dataset argument\n")); 1125 usage(B_FALSE); 1126 } 1127 1128 propname = argv[0]; 1129 1130 /* 1131 * Get and validate the property before iterating over the datasets. We 1132 * do this now so as to avoid printing out an error message for each and 1133 * every dataset. 1134 */ 1135 if ((prop = zfs_name_to_prop(propname)) == ZFS_PROP_INVAL) { 1136 (void) fprintf(stderr, gettext("invalid property '%s'\n"), 1137 propname); 1138 usage(B_FALSE); 1139 } 1140 if (zfs_prop_readonly(prop)) { 1141 (void) fprintf(stderr, gettext("%s property is read-only\n"), 1142 propname); 1143 return (1); 1144 } 1145 if (!zfs_prop_inheritable(prop)) { 1146 (void) fprintf(stderr, gettext("%s property cannot be " 1147 "inherited\n"), propname); 1148 (void) fprintf(stderr, gettext("use 'zfs set %s=none' to " 1149 "clear\n"), propname); 1150 return (1); 1151 } 1152 1153 return (zfs_for_each(argc - 1, argv + 1, recurse, 1154 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, 1155 inherit_callback, (void *)prop)); 1156 } 1157 1158 /* 1159 * list [-rH] [-o property[,property]...] [-t type[,type]...] 1160 * [-s property [-s property]...] [-S property [-S property]...] 1161 * <dataset> ... 1162 * 1163 * -r Recurse over all children 1164 * -H Scripted mode; elide headers and separate colums by tabs 1165 * -o Control which fields to display. 1166 * -t Control which object types to display. 1167 * -s Specify sort columns, descending order. 1168 * -S Specify sort columns, ascending order. 1169 * 1170 * When given no arguments, lists all filesystems in the system. 1171 * Otherwise, list the specified datasets, optionally recursing down them if 1172 * '-r' is specified. 1173 */ 1174 typedef struct list_cbdata { 1175 boolean_t cb_first; 1176 boolean_t cb_scripted; 1177 zfs_prop_t cb_fields[ZFS_NPROP_ALL]; 1178 int cb_fieldcount; 1179 } list_cbdata_t; 1180 1181 /* 1182 * Given a list of columns to display, output appropriate headers for each one. 1183 */ 1184 static void 1185 print_header(zfs_prop_t *fields, size_t count) 1186 { 1187 int i; 1188 1189 for (i = 0; i < count; i++) { 1190 if (i != 0) 1191 (void) printf(" "); 1192 if (i == count - 1) 1193 (void) printf("%s", zfs_prop_column_name(fields[i])); 1194 else /* LINTED - format specifier */ 1195 (void) printf(zfs_prop_column_format(fields[i]), 1196 zfs_prop_column_name(fields[i])); 1197 } 1198 1199 (void) printf("\n"); 1200 } 1201 1202 /* 1203 * Given a dataset and a list of fields, print out all the properties according 1204 * to the described layout. 1205 */ 1206 static void 1207 print_dataset(zfs_handle_t *zhp, zfs_prop_t *fields, size_t count, int scripted) 1208 { 1209 int i; 1210 char property[ZFS_MAXPROPLEN]; 1211 1212 for (i = 0; i < count; i++) { 1213 if (i != 0) { 1214 if (scripted) 1215 (void) printf("\t"); 1216 else 1217 (void) printf(" "); 1218 } 1219 1220 if (zfs_prop_get(zhp, fields[i], property, 1221 sizeof (property), NULL, NULL, 0, B_FALSE) != 0) 1222 (void) strlcpy(property, "-", sizeof (property)); 1223 1224 /* 1225 * If this is being called in scripted mode, or if this is the 1226 * last column and it is left-justified, don't include a width 1227 * format specifier. 1228 */ 1229 if (scripted || (i == count - 1 && 1230 strchr(zfs_prop_column_format(fields[i]), '-') != NULL)) 1231 (void) printf("%s", property); 1232 else /* LINTED - format specifier */ 1233 (void) printf(zfs_prop_column_format(fields[i]), 1234 property); 1235 } 1236 1237 (void) printf("\n"); 1238 } 1239 1240 /* 1241 * Generic callback function to list a dataset or snapshot. 1242 */ 1243 static int 1244 list_callback(zfs_handle_t *zhp, void *data) 1245 { 1246 list_cbdata_t *cbp = data; 1247 1248 if (cbp->cb_first) { 1249 if (!cbp->cb_scripted) 1250 print_header(cbp->cb_fields, cbp->cb_fieldcount); 1251 cbp->cb_first = B_FALSE; 1252 } 1253 1254 print_dataset(zhp, cbp->cb_fields, cbp->cb_fieldcount, 1255 cbp->cb_scripted); 1256 1257 return (0); 1258 } 1259 1260 static int 1261 zfs_do_list(int argc, char **argv) 1262 { 1263 int c; 1264 boolean_t recurse = B_FALSE; 1265 boolean_t scripted = B_FALSE; 1266 static char default_fields[] = 1267 "name,used,available,referenced,mountpoint"; 1268 int types = ZFS_TYPE_ANY; 1269 char *fields = NULL; 1270 char *basic_fields = default_fields; 1271 list_cbdata_t cb = { 0 }; 1272 char *value; 1273 int ret; 1274 char *type_subopts[] = { "filesystem", "volume", "snapshot", NULL }; 1275 char *badopt; 1276 int alloffset; 1277 zfs_sort_column_t *sortcol = NULL; 1278 1279 /* check options */ 1280 while ((c = getopt(argc, argv, ":o:rt:Hs:S:")) != -1) { 1281 zfs_prop_t prop; 1282 1283 switch (c) { 1284 case 'o': 1285 fields = optarg; 1286 break; 1287 case 'r': 1288 recurse = B_TRUE; 1289 break; 1290 case 'H': 1291 scripted = B_TRUE; 1292 break; 1293 case 's': 1294 if ((prop = zfs_name_to_prop(optarg)) == 1295 ZFS_PROP_INVAL) { 1296 (void) fprintf(stderr, 1297 gettext("invalid property '%s'\n"), optarg); 1298 usage(B_FALSE); 1299 } 1300 zfs_add_sort_column(&sortcol, prop, B_FALSE); 1301 break; 1302 case 'S': 1303 if ((prop = zfs_name_to_prop(optarg)) == 1304 ZFS_PROP_INVAL) { 1305 (void) fprintf(stderr, 1306 gettext("invalid property '%s'\n"), optarg); 1307 usage(B_FALSE); 1308 } 1309 zfs_add_sort_column(&sortcol, prop, B_TRUE); 1310 break; 1311 case 't': 1312 types = 0; 1313 while (*optarg != '\0') { 1314 switch (getsubopt(&optarg, type_subopts, 1315 &value)) { 1316 case 0: 1317 types |= ZFS_TYPE_FILESYSTEM; 1318 break; 1319 case 1: 1320 types |= ZFS_TYPE_VOLUME; 1321 break; 1322 case 2: 1323 types |= ZFS_TYPE_SNAPSHOT; 1324 break; 1325 default: 1326 (void) fprintf(stderr, 1327 gettext("invalid type '%s'\n"), 1328 value); 1329 usage(B_FALSE); 1330 } 1331 } 1332 break; 1333 case ':': 1334 (void) fprintf(stderr, gettext("missing argument for " 1335 "'%c' option\n"), optopt); 1336 usage(B_FALSE); 1337 break; 1338 case '?': 1339 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1340 optopt); 1341 usage(B_FALSE); 1342 } 1343 } 1344 1345 argc -= optind; 1346 argv += optind; 1347 1348 if (fields == NULL) 1349 fields = basic_fields; 1350 1351 /* 1352 * If the user specifies '-o all', the zfs_get_proplist() doesn't 1353 * normally include the name of the dataset. For 'zfs list', we always 1354 * want this property to be first. 1355 */ 1356 if (strcmp(fields, "all") == 0) { 1357 cb.cb_fields[0] = ZFS_PROP_NAME; 1358 alloffset = 1; 1359 } else { 1360 alloffset = 0; 1361 } 1362 1363 if ((ret = zfs_get_proplist(fields, cb.cb_fields + alloffset, 1364 ZFS_NPROP_ALL - alloffset, &cb.cb_fieldcount, &badopt)) != 0) { 1365 if (ret == EINVAL) 1366 (void) fprintf(stderr, gettext("invalid property " 1367 "'%s'\n"), badopt); 1368 else 1369 (void) fprintf(stderr, gettext("too many properties " 1370 "specified\n")); 1371 usage(B_FALSE); 1372 } 1373 1374 cb.cb_fieldcount += alloffset; 1375 cb.cb_scripted = scripted; 1376 cb.cb_first = B_TRUE; 1377 1378 ret = zfs_for_each(argc, argv, recurse, types, sortcol, 1379 list_callback, &cb); 1380 1381 zfs_free_sort_columns(sortcol); 1382 1383 if (ret == 0 && cb.cb_first) 1384 (void) printf(gettext("no datasets available\n")); 1385 1386 return (ret); 1387 } 1388 1389 /* 1390 * zfs rename <fs | snap | vol> <fs | snap | vol> 1391 * 1392 * Renames the given dataset to another of the same type. 1393 */ 1394 /* ARGSUSED */ 1395 static int 1396 zfs_do_rename(int argc, char **argv) 1397 { 1398 zfs_handle_t *zhp; 1399 int ret; 1400 1401 /* check options */ 1402 if (argc > 1 && argv[1][0] == '-') { 1403 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1404 argv[1][1]); 1405 usage(B_FALSE); 1406 } 1407 1408 /* check number of arguments */ 1409 if (argc < 2) { 1410 (void) fprintf(stderr, gettext("missing source dataset " 1411 "argument\n")); 1412 usage(B_FALSE); 1413 } 1414 if (argc < 3) { 1415 (void) fprintf(stderr, gettext("missing target dataset " 1416 "argument\n")); 1417 usage(B_FALSE); 1418 } 1419 if (argc > 3) { 1420 (void) fprintf(stderr, gettext("too many arguments\n")); 1421 usage(B_FALSE); 1422 } 1423 1424 if ((zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_ANY)) == NULL) 1425 return (1); 1426 1427 ret = (zfs_rename(zhp, argv[2]) != 0); 1428 1429 zfs_close(zhp); 1430 return (ret); 1431 } 1432 1433 /* 1434 * zfs promote <fs> 1435 * 1436 * Promotes the given clone fs to be the parent 1437 */ 1438 /* ARGSUSED */ 1439 static int 1440 zfs_do_promote(int argc, char **argv) 1441 { 1442 zfs_handle_t *zhp; 1443 int ret; 1444 1445 /* check options */ 1446 if (argc > 1 && argv[1][0] == '-') { 1447 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1448 argv[1][1]); 1449 usage(B_FALSE); 1450 } 1451 1452 /* check number of arguments */ 1453 if (argc < 2) { 1454 (void) fprintf(stderr, gettext("missing clone filesystem" 1455 " argument\n")); 1456 usage(B_FALSE); 1457 } 1458 if (argc > 2) { 1459 (void) fprintf(stderr, gettext("too many arguments\n")); 1460 usage(B_FALSE); 1461 } 1462 1463 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 1464 if (zhp == NULL) 1465 return (1); 1466 1467 ret = (zfs_promote(zhp) != 0); 1468 1469 zfs_close(zhp); 1470 return (ret); 1471 } 1472 1473 /* 1474 * zfs rollback [-rfR] <snapshot> 1475 * 1476 * -r Delete any intervening snapshots before doing rollback 1477 * -R Delete any snapshots and their clones 1478 * -f Force unmount filesystems, even if they are in use. 1479 * 1480 * Given a filesystem, rollback to a specific snapshot, discarding any changes 1481 * since then and making it the active dataset. If more recent snapshots exist, 1482 * the command will complain unless the '-r' flag is given. 1483 */ 1484 typedef struct rollback_cbdata { 1485 uint64_t cb_create; 1486 boolean_t cb_first; 1487 int cb_doclones; 1488 char *cb_target; 1489 int cb_error; 1490 boolean_t cb_recurse; 1491 boolean_t cb_dependent; 1492 } rollback_cbdata_t; 1493 1494 /* 1495 * Report any snapshots more recent than the one specified. Used when '-r' is 1496 * not specified. We reuse this same callback for the snapshot dependents - if 1497 * 'cb_dependent' is set, then this is a dependent and we should report it 1498 * without checking the transaction group. 1499 */ 1500 static int 1501 rollback_check(zfs_handle_t *zhp, void *data) 1502 { 1503 rollback_cbdata_t *cbp = data; 1504 1505 if (cbp->cb_doclones) { 1506 zfs_close(zhp); 1507 return (0); 1508 } 1509 1510 if (!cbp->cb_dependent) { 1511 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 && 1512 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 1513 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 1514 cbp->cb_create) { 1515 1516 if (cbp->cb_first && !cbp->cb_recurse) { 1517 (void) fprintf(stderr, gettext("cannot " 1518 "rollback to '%s': more recent snapshots " 1519 "exist\n"), 1520 cbp->cb_target); 1521 (void) fprintf(stderr, gettext("use '-r' to " 1522 "force deletion of the following " 1523 "snapshots:\n")); 1524 cbp->cb_first = 0; 1525 cbp->cb_error = 1; 1526 } 1527 1528 if (cbp->cb_recurse) { 1529 cbp->cb_dependent = B_TRUE; 1530 if (zfs_iter_dependents(zhp, B_TRUE, 1531 rollback_check, cbp) != 0) { 1532 zfs_close(zhp); 1533 return (-1); 1534 } 1535 cbp->cb_dependent = B_FALSE; 1536 } else { 1537 (void) fprintf(stderr, "%s\n", 1538 zfs_get_name(zhp)); 1539 } 1540 } 1541 } else { 1542 if (cbp->cb_first && cbp->cb_recurse) { 1543 (void) fprintf(stderr, gettext("cannot rollback to " 1544 "'%s': clones of previous snapshots exist\n"), 1545 cbp->cb_target); 1546 (void) fprintf(stderr, gettext("use '-R' to " 1547 "force deletion of the following clones and " 1548 "dependents:\n")); 1549 cbp->cb_first = 0; 1550 cbp->cb_error = 1; 1551 } 1552 1553 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 1554 } 1555 1556 zfs_close(zhp); 1557 return (0); 1558 } 1559 1560 static int 1561 zfs_do_rollback(int argc, char **argv) 1562 { 1563 int ret; 1564 int c; 1565 rollback_cbdata_t cb = { 0 }; 1566 zfs_handle_t *zhp, *snap; 1567 char parentname[ZFS_MAXNAMELEN]; 1568 char *delim; 1569 int force = 0; 1570 1571 /* check options */ 1572 while ((c = getopt(argc, argv, "rfR")) != -1) { 1573 switch (c) { 1574 case 'f': 1575 force = 1; 1576 break; 1577 case 'r': 1578 cb.cb_recurse = 1; 1579 break; 1580 case 'R': 1581 cb.cb_recurse = 1; 1582 cb.cb_doclones = 1; 1583 break; 1584 case '?': 1585 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1586 optopt); 1587 usage(B_FALSE); 1588 } 1589 } 1590 1591 argc -= optind; 1592 argv += optind; 1593 1594 /* check number of arguments */ 1595 if (argc < 1) { 1596 (void) fprintf(stderr, gettext("missing dataset argument\n")); 1597 usage(B_FALSE); 1598 } 1599 if (argc > 1) { 1600 (void) fprintf(stderr, gettext("too many arguments\n")); 1601 usage(B_FALSE); 1602 } 1603 1604 /* open the snapshot */ 1605 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 1606 return (1); 1607 1608 /* open the parent dataset */ 1609 (void) strlcpy(parentname, argv[0], sizeof (parentname)); 1610 verify((delim = strrchr(parentname, '@')) != NULL); 1611 *delim = '\0'; 1612 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_ANY)) == NULL) { 1613 zfs_close(snap); 1614 return (1); 1615 } 1616 1617 /* 1618 * Check for more recent snapshots and/or clones based on the presence 1619 * of '-r' and '-R'. 1620 */ 1621 cb.cb_target = argv[0]; 1622 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 1623 cb.cb_first = B_TRUE; 1624 cb.cb_error = 0; 1625 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0) 1626 goto out; 1627 1628 if ((ret = cb.cb_error) != 0) 1629 goto out; 1630 1631 /* 1632 * Rollback parent to the given snapshot. 1633 */ 1634 ret = zfs_rollback(zhp, snap, force); 1635 1636 out: 1637 zfs_close(snap); 1638 zfs_close(zhp); 1639 1640 if (ret == 0) 1641 return (0); 1642 else 1643 return (1); 1644 } 1645 1646 /* 1647 * zfs set property=value { fs | snap | vol } ... 1648 * 1649 * Sets the given property for all datasets specified on the command line. 1650 */ 1651 typedef struct set_cbdata { 1652 char *cb_propname; 1653 char *cb_value; 1654 zfs_prop_t cb_prop; 1655 } set_cbdata_t; 1656 1657 static int 1658 set_callback(zfs_handle_t *zhp, void *data) 1659 { 1660 set_cbdata_t *cbp = data; 1661 int ret = 1; 1662 1663 /* don't allow setting of properties for snapshots */ 1664 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) { 1665 (void) fprintf(stderr, gettext("cannot set %s property for " 1666 "'%s': snapshot properties cannot be modified\n"), 1667 cbp->cb_propname, zfs_get_name(zhp)); 1668 return (1); 1669 } 1670 1671 /* 1672 * If we're changing the volsize, make sure the value is appropriate, 1673 * and set the reservation if this is a non-sparse volume. 1674 */ 1675 if (cbp->cb_prop == ZFS_PROP_VOLSIZE && 1676 zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 1677 uint64_t volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 1678 uint64_t avail = zfs_prop_get_int(zhp, ZFS_PROP_AVAILABLE); 1679 uint64_t reservation = zfs_prop_get_int(zhp, 1680 ZFS_PROP_RESERVATION); 1681 uint64_t blocksize = zfs_prop_get_int(zhp, 1682 ZFS_PROP_VOLBLOCKSIZE); 1683 uint64_t value; 1684 1685 verify(zfs_nicestrtonum(cbp->cb_value, &value) == 0); 1686 1687 if (value % blocksize != 0) { 1688 char buf[64]; 1689 1690 zfs_nicenum(blocksize, buf, sizeof (buf)); 1691 (void) fprintf(stderr, gettext("cannot set %s for " 1692 "'%s': must be a multiple of volume block size " 1693 "(%s)\n"), cbp->cb_propname, zfs_get_name(zhp), 1694 buf); 1695 return (1); 1696 } 1697 1698 if (value == 0) { 1699 (void) fprintf(stderr, gettext("cannot set %s for " 1700 "'%s': cannot be zero\n"), cbp->cb_propname, 1701 zfs_get_name(zhp)); 1702 return (1); 1703 } 1704 1705 if (volsize == reservation) { 1706 if (value > volsize && (value - volsize) > avail) { 1707 (void) fprintf(stderr, gettext("cannot set " 1708 "%s property for '%s': volume size exceeds " 1709 "amount of available space\n"), 1710 cbp->cb_propname, zfs_get_name(zhp)); 1711 return (1); 1712 } 1713 1714 if (zfs_prop_set(zhp, ZFS_PROP_RESERVATION, 1715 cbp->cb_value) != 0) { 1716 (void) fprintf(stderr, gettext("volsize and " 1717 "reservation must remain equal\n")); 1718 return (1); 1719 } 1720 } 1721 } 1722 1723 /* 1724 * Do not allow the reservation to be set above the volume size. We do 1725 * this here instead of inside libzfs because libzfs violates this rule 1726 * internally. 1727 */ 1728 if (cbp->cb_prop == ZFS_PROP_RESERVATION && 1729 zfs_get_type(zhp) == ZFS_TYPE_VOLUME) { 1730 uint64_t value; 1731 uint64_t volsize; 1732 1733 volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 1734 if (strcmp(cbp->cb_value, "none") == 0) 1735 value = 0; 1736 else 1737 verify(zfs_nicestrtonum(cbp->cb_value, &value) == 0); 1738 1739 if (value > volsize) { 1740 (void) fprintf(stderr, gettext("cannot set %s " 1741 "for '%s': size is greater than current " 1742 "volume size\n"), cbp->cb_propname, 1743 zfs_get_name(zhp)); 1744 return (-1); 1745 } 1746 } 1747 1748 if (zfs_prop_set(zhp, cbp->cb_prop, cbp->cb_value) != 0) { 1749 switch (libzfs_errno(g_zfs)) { 1750 case EZFS_MOUNTFAILED: 1751 (void) fprintf(stderr, gettext("property may be set " 1752 "but unable to remount filesystem\n")); 1753 break; 1754 case EZFS_SHAREFAILED: 1755 (void) fprintf(stderr, gettext("property may be set " 1756 "but unable to reshare filesystem\n")); 1757 break; 1758 } 1759 return (1); 1760 } 1761 ret = 0; 1762 error: 1763 return (ret); 1764 } 1765 1766 static int 1767 zfs_do_set(int argc, char **argv) 1768 { 1769 set_cbdata_t cb; 1770 1771 /* check for options */ 1772 if (argc > 1 && argv[1][0] == '-') { 1773 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1774 argv[1][1]); 1775 usage(B_FALSE); 1776 } 1777 1778 /* check number of arguments */ 1779 if (argc < 2) { 1780 (void) fprintf(stderr, gettext("missing property=value " 1781 "argument\n")); 1782 usage(B_FALSE); 1783 } 1784 if (argc < 3) { 1785 (void) fprintf(stderr, gettext("missing dataset name\n")); 1786 usage(B_FALSE); 1787 } 1788 1789 /* validate property=value argument */ 1790 cb.cb_propname = argv[1]; 1791 if ((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) { 1792 (void) fprintf(stderr, gettext("missing value in " 1793 "property=value argument\n")); 1794 usage(B_FALSE); 1795 } 1796 1797 *cb.cb_value = '\0'; 1798 cb.cb_value++; 1799 1800 if (*cb.cb_propname == '\0') { 1801 (void) fprintf(stderr, 1802 gettext("missing property in property=value argument\n")); 1803 usage(B_FALSE); 1804 } 1805 if (*cb.cb_value == '\0') { 1806 (void) fprintf(stderr, 1807 gettext("missing value in property=value argument\n")); 1808 usage(B_FALSE); 1809 } 1810 1811 /* get the property type */ 1812 if ((cb.cb_prop = zfs_name_to_prop(cb.cb_propname)) == 1813 ZFS_PROP_INVAL) { 1814 (void) fprintf(stderr, 1815 gettext("invalid property '%s'\n"), cb.cb_propname); 1816 usage(B_FALSE); 1817 } 1818 1819 /* 1820 * Validate that the value is appropriate for this property. We do this 1821 * once now so we don't generate multiple errors each time we try to 1822 * apply it to a dataset. 1823 */ 1824 if (zfs_prop_validate(g_zfs, cb.cb_prop, cb.cb_value, NULL) != 0) 1825 return (1); 1826 1827 return (zfs_for_each(argc - 2, argv + 2, B_FALSE, 1828 ZFS_TYPE_ANY, NULL, set_callback, &cb)); 1829 } 1830 1831 /* 1832 * zfs snapshot [-r] <fs@snap> 1833 * 1834 * Creates a snapshot with the given name. While functionally equivalent to 1835 * 'zfs create', it is a separate command to diffferentiate intent. 1836 */ 1837 static int 1838 zfs_do_snapshot(int argc, char **argv) 1839 { 1840 int recursive = B_FALSE; 1841 int ret; 1842 char c; 1843 1844 /* check options */ 1845 while ((c = getopt(argc, argv, ":r")) != -1) { 1846 switch (c) { 1847 case 'r': 1848 recursive = B_TRUE; 1849 break; 1850 case '?': 1851 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1852 optopt); 1853 usage(B_FALSE); 1854 } 1855 } 1856 1857 argc -= optind; 1858 argv += optind; 1859 1860 /* check number of arguments */ 1861 if (argc < 1) { 1862 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 1863 usage(B_FALSE); 1864 } 1865 if (argc > 1) { 1866 (void) fprintf(stderr, gettext("too many arguments\n")); 1867 usage(B_FALSE); 1868 } 1869 1870 ret = zfs_snapshot(g_zfs, argv[0], recursive); 1871 if (ret && recursive) 1872 (void) fprintf(stderr, gettext("no snapshots were created\n")); 1873 return (ret != 0); 1874 1875 } 1876 1877 /* 1878 * zfs send [-i <fs@snap>] <fs@snap> 1879 * 1880 * Send a backup stream to stdout. 1881 */ 1882 static int 1883 zfs_do_send(int argc, char **argv) 1884 { 1885 char *fromname = NULL; 1886 zfs_handle_t *zhp_from = NULL, *zhp_to; 1887 int c, err; 1888 1889 /* check options */ 1890 while ((c = getopt(argc, argv, ":i:")) != -1) { 1891 switch (c) { 1892 case 'i': 1893 fromname = optarg; 1894 break; 1895 case ':': 1896 (void) fprintf(stderr, gettext("missing argument for " 1897 "'%c' option\n"), optopt); 1898 usage(B_FALSE); 1899 break; 1900 case '?': 1901 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1902 optopt); 1903 usage(B_FALSE); 1904 } 1905 } 1906 1907 argc -= optind; 1908 argv += optind; 1909 1910 /* check number of arguments */ 1911 if (argc < 1) { 1912 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 1913 usage(B_FALSE); 1914 } 1915 if (argc > 1) { 1916 (void) fprintf(stderr, gettext("too many arguments\n")); 1917 usage(B_FALSE); 1918 } 1919 1920 if (isatty(STDOUT_FILENO)) { 1921 (void) fprintf(stderr, 1922 gettext("Error: Stream can not be written " 1923 "to a terminal.\n" 1924 "You must redirect standard output.\n")); 1925 return (1); 1926 } 1927 1928 if (fromname) { 1929 if ((zhp_from = zfs_open(g_zfs, fromname, 1930 ZFS_TYPE_SNAPSHOT)) == NULL) 1931 return (1); 1932 } 1933 if ((zhp_to = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 1934 return (1); 1935 1936 err = zfs_send(zhp_to, zhp_from); 1937 1938 if (zhp_from) 1939 zfs_close(zhp_from); 1940 zfs_close(zhp_to); 1941 1942 return (err != 0); 1943 } 1944 1945 /* 1946 * zfs receive <fs@snap> 1947 * 1948 * Restore a backup stream from stdin. 1949 */ 1950 static int 1951 zfs_do_receive(int argc, char **argv) 1952 { 1953 int c, err; 1954 boolean_t isprefix = B_FALSE; 1955 boolean_t dryrun = B_FALSE; 1956 boolean_t verbose = B_FALSE; 1957 1958 /* check options */ 1959 while ((c = getopt(argc, argv, ":dnv")) != -1) { 1960 switch (c) { 1961 case 'd': 1962 isprefix = B_TRUE; 1963 break; 1964 case 'n': 1965 dryrun = B_TRUE; 1966 break; 1967 case 'v': 1968 verbose = B_TRUE; 1969 break; 1970 case ':': 1971 (void) fprintf(stderr, gettext("missing argument for " 1972 "'%c' option\n"), optopt); 1973 usage(B_FALSE); 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 snapshot 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 if (isatty(STDIN_FILENO)) { 1996 (void) fprintf(stderr, 1997 gettext("Error: Backup stream can not be read " 1998 "from a terminal.\n" 1999 "You must redirect standard input.\n")); 2000 return (1); 2001 } 2002 2003 err = zfs_receive(g_zfs, argv[0], isprefix, verbose, dryrun); 2004 return (err != 0); 2005 } 2006 2007 typedef struct get_all_cbdata { 2008 zfs_handle_t **cb_handles; 2009 size_t cb_alloc; 2010 size_t cb_used; 2011 } get_all_cbdata_t; 2012 2013 static int 2014 get_one_filesystem(zfs_handle_t *zhp, void *data) 2015 { 2016 get_all_cbdata_t *cbp = data; 2017 2018 /* 2019 * Skip any zvols 2020 */ 2021 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 2022 zfs_close(zhp); 2023 return (0); 2024 } 2025 2026 if (cbp->cb_alloc == cbp->cb_used) { 2027 zfs_handle_t **handles; 2028 2029 if (cbp->cb_alloc == 0) 2030 cbp->cb_alloc = 64; 2031 else 2032 cbp->cb_alloc *= 2; 2033 2034 handles = safe_malloc(cbp->cb_alloc * sizeof (void *)); 2035 2036 if (cbp->cb_handles) { 2037 bcopy(cbp->cb_handles, handles, 2038 cbp->cb_used * sizeof (void *)); 2039 free(cbp->cb_handles); 2040 } 2041 2042 cbp->cb_handles = handles; 2043 } 2044 2045 cbp->cb_handles[cbp->cb_used++] = zhp; 2046 2047 return (zfs_iter_filesystems(zhp, get_one_filesystem, data)); 2048 } 2049 2050 static void 2051 get_all_filesystems(zfs_handle_t ***fslist, size_t *count) 2052 { 2053 get_all_cbdata_t cb = { 0 }; 2054 2055 (void) zfs_iter_root(g_zfs, get_one_filesystem, &cb); 2056 2057 *fslist = cb.cb_handles; 2058 *count = cb.cb_used; 2059 } 2060 2061 static int 2062 mountpoint_compare(const void *a, const void *b) 2063 { 2064 zfs_handle_t **za = (zfs_handle_t **)a; 2065 zfs_handle_t **zb = (zfs_handle_t **)b; 2066 char mounta[MAXPATHLEN]; 2067 char mountb[MAXPATHLEN]; 2068 2069 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 2070 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 2071 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 2072 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 2073 2074 return (strcmp(mounta, mountb)); 2075 } 2076 2077 /* 2078 * Generic callback for sharing or mounting filesystems. Because the code is so 2079 * similar, we have a common function with an extra parameter to determine which 2080 * mode we are using. 2081 */ 2082 #define OP_SHARE 0x1 2083 #define OP_MOUNT 0x2 2084 2085 typedef struct share_mount_cbdata { 2086 int cb_type; 2087 int cb_explicit; 2088 int cb_flags; 2089 const char *cb_options; 2090 } share_mount_cbdata_t; 2091 2092 /* 2093 * Share or mount the filesystem. 2094 */ 2095 static int 2096 share_mount_callback(zfs_handle_t *zhp, void *data) 2097 { 2098 char mountpoint[ZFS_MAXPROPLEN]; 2099 char shareopts[ZFS_MAXPROPLEN]; 2100 share_mount_cbdata_t *cbp = data; 2101 const char *cmdname = cbp->cb_type == OP_SHARE ? "share" : "mount"; 2102 struct mnttab mnt; 2103 uint64_t zoned; 2104 2105 if (cbp->cb_options == NULL) 2106 mnt.mnt_mntopts = ""; 2107 else 2108 mnt.mnt_mntopts = (char *)cbp->cb_options; 2109 2110 /* 2111 * Check to make sure we can mount/share this dataset. If we are in the 2112 * global zone and the filesystem is exported to a local zone, or if we 2113 * are in a local zone and the filesystem is not exported, then it is an 2114 * error. 2115 */ 2116 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2117 2118 if (zoned && getzoneid() == GLOBAL_ZONEID) { 2119 if (!cbp->cb_explicit) 2120 return (0); 2121 2122 (void) fprintf(stderr, gettext("cannot %s '%s': dataset is " 2123 "exported to a local zone\n"), cmdname, zfs_get_name(zhp)); 2124 return (1); 2125 2126 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) { 2127 if (!cbp->cb_explicit) 2128 return (0); 2129 2130 (void) fprintf(stderr, gettext("cannot %s '%s': permission " 2131 "denied\n"), cmdname, zfs_get_name(zhp)); 2132 return (1); 2133 } 2134 2135 /* 2136 * Inore any filesystems which don't apply to us. This includes those 2137 * with a legacy mountpoint, or those with legacy share options. 2138 */ 2139 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 2140 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0); 2141 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, 2142 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0); 2143 2144 if (cbp->cb_type == OP_SHARE) { 2145 if (strcmp(shareopts, "off") == 0) { 2146 if (!cbp->cb_explicit) 2147 return (0); 2148 2149 (void) fprintf(stderr, gettext("cannot share '%s': " 2150 "legacy share\n"), zfs_get_name(zhp)); 2151 (void) fprintf(stderr, gettext("use share(1M) to " 2152 "share this filesystem\n")); 2153 return (1); 2154 } 2155 } 2156 2157 /* 2158 * We cannot share or mount legacy filesystems. If the shareopts is 2159 * non-legacy but the mountpoint is legacy, we treat it as a legacy 2160 * share. 2161 */ 2162 if (strcmp(mountpoint, "legacy") == 0) { 2163 if (!cbp->cb_explicit) 2164 return (0); 2165 2166 (void) fprintf(stderr, gettext("cannot %s '%s': " 2167 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp)); 2168 (void) fprintf(stderr, gettext("use %s to " 2169 "%s this filesystem\n"), cbp->cb_type == OP_SHARE ? 2170 "share(1M)" : "mount(1M)", cmdname); 2171 return (1); 2172 } 2173 2174 if (strcmp(mountpoint, "none") == 0) { 2175 if (!cbp->cb_explicit) 2176 return (0); 2177 2178 (void) fprintf(stderr, gettext("cannot %s '%s': no " 2179 "mountpoint set\n"), cmdname, zfs_get_name(zhp)); 2180 return (1); 2181 } 2182 2183 /* 2184 * At this point, we have verified that the mountpoint and/or shareopts 2185 * are appropriate for auto management. Determine if the filesystem is 2186 * currently mounted or shared, and abort if this is an explicit 2187 * request. 2188 */ 2189 switch (cbp->cb_type) { 2190 case OP_SHARE: 2191 if (zfs_is_shared(zhp, NULL)) { 2192 if (cbp->cb_explicit) { 2193 (void) fprintf(stderr, gettext("cannot share " 2194 "'%s': filesystem already shared\n"), 2195 zfs_get_name(zhp)); 2196 return (1); 2197 } else { 2198 return (0); 2199 } 2200 } 2201 break; 2202 2203 case OP_MOUNT: 2204 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) && 2205 zfs_is_mounted(zhp, NULL)) { 2206 if (cbp->cb_explicit) { 2207 (void) fprintf(stderr, gettext("cannot mount " 2208 "'%s': filesystem already mounted\n"), 2209 zfs_get_name(zhp)); 2210 return (1); 2211 } else { 2212 return (0); 2213 } 2214 } 2215 break; 2216 } 2217 2218 /* 2219 * Mount and optionally share the filesystem. 2220 */ 2221 switch (cbp->cb_type) { 2222 case OP_SHARE: 2223 { 2224 if (!zfs_is_mounted(zhp, NULL) && 2225 zfs_mount(zhp, NULL, 0) != 0) 2226 return (1); 2227 2228 if (zfs_share(zhp) != 0) 2229 return (1); 2230 } 2231 break; 2232 2233 case OP_MOUNT: 2234 if (zfs_mount(zhp, cbp->cb_options, cbp->cb_flags) != 0) 2235 return (1); 2236 break; 2237 } 2238 2239 return (0); 2240 } 2241 2242 static int 2243 share_or_mount(int type, int argc, char **argv) 2244 { 2245 int do_all = 0; 2246 int c, ret = 0; 2247 share_mount_cbdata_t cb = { 0 }; 2248 2249 cb.cb_type = type; 2250 2251 /* check options */ 2252 while ((c = getopt(argc, argv, type == OP_MOUNT ? ":ao:O" : "a")) 2253 != -1) { 2254 switch (c) { 2255 case 'a': 2256 do_all = 1; 2257 break; 2258 case 'o': 2259 cb.cb_options = optarg; 2260 break; 2261 case 'O': 2262 cb.cb_flags |= MS_OVERLAY; 2263 break; 2264 case ':': 2265 (void) fprintf(stderr, gettext("missing argument for " 2266 "'%c' option\n"), optopt); 2267 usage(B_FALSE); 2268 break; 2269 case '?': 2270 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2271 optopt); 2272 usage(B_FALSE); 2273 } 2274 } 2275 2276 argc -= optind; 2277 argv += optind; 2278 2279 /* check number of arguments */ 2280 if (do_all) { 2281 zfs_handle_t **fslist = NULL; 2282 size_t i, count = 0; 2283 2284 if (argc != 0) { 2285 (void) fprintf(stderr, gettext("too many arguments\n")); 2286 usage(B_FALSE); 2287 } 2288 2289 get_all_filesystems(&fslist, &count); 2290 2291 if (count == 0) 2292 return (0); 2293 2294 qsort(fslist, count, sizeof (void *), mountpoint_compare); 2295 2296 for (i = 0; i < count; i++) { 2297 if (share_mount_callback(fslist[i], &cb) != 0) 2298 ret = 1; 2299 } 2300 2301 for (i = 0; i < count; i++) 2302 zfs_close(fslist[i]); 2303 2304 free(fslist); 2305 } else if (argc == 0) { 2306 struct mnttab entry; 2307 2308 if (type == OP_SHARE) { 2309 (void) fprintf(stderr, gettext("missing filesystem " 2310 "argument\n")); 2311 usage(B_FALSE); 2312 } 2313 2314 /* 2315 * When mount is given no arguments, go through /etc/mnttab and 2316 * display any active ZFS mounts. We hide any snapshots, since 2317 * they are controlled automatically. 2318 */ 2319 rewind(mnttab_file); 2320 while (getmntent(mnttab_file, &entry) == 0) { 2321 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 || 2322 strchr(entry.mnt_special, '@') != NULL) 2323 continue; 2324 2325 (void) printf("%-30s %s\n", entry.mnt_special, 2326 entry.mnt_mountp); 2327 } 2328 2329 } else { 2330 zfs_handle_t *zhp; 2331 2332 if (argc > 1) { 2333 (void) fprintf(stderr, 2334 gettext("too many arguments\n")); 2335 usage(B_FALSE); 2336 } 2337 2338 if ((zhp = zfs_open(g_zfs, argv[0], 2339 ZFS_TYPE_FILESYSTEM)) == NULL) 2340 ret = 1; 2341 else { 2342 cb.cb_explicit = B_TRUE; 2343 ret = share_mount_callback(zhp, &cb); 2344 zfs_close(zhp); 2345 } 2346 } 2347 2348 return (ret); 2349 } 2350 2351 /* 2352 * zfs mount -a 2353 * zfs mount filesystem 2354 * 2355 * Mount all filesystems, or mount the given filesystem. 2356 */ 2357 static int 2358 zfs_do_mount(int argc, char **argv) 2359 { 2360 return (share_or_mount(OP_MOUNT, argc, argv)); 2361 } 2362 2363 /* 2364 * zfs share -a 2365 * zfs share filesystem 2366 * 2367 * Share all filesystems, or share the given filesystem. 2368 */ 2369 static int 2370 zfs_do_share(int argc, char **argv) 2371 { 2372 return (share_or_mount(OP_SHARE, argc, argv)); 2373 } 2374 2375 typedef struct unshare_unmount_node { 2376 zfs_handle_t *un_zhp; 2377 char *un_mountp; 2378 uu_avl_node_t un_avlnode; 2379 } unshare_unmount_node_t; 2380 2381 /* ARGSUSED */ 2382 static int 2383 unshare_unmount_compare(const void *larg, const void *rarg, void *unused) 2384 { 2385 const unshare_unmount_node_t *l = larg; 2386 const unshare_unmount_node_t *r = rarg; 2387 2388 return (strcmp(l->un_mountp, r->un_mountp)); 2389 } 2390 2391 /* 2392 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an 2393 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem, 2394 * and unmount it appropriately. 2395 */ 2396 static int 2397 unshare_unmount_path(int type, char *path, int flags, boolean_t is_manual) 2398 { 2399 zfs_handle_t *zhp; 2400 int ret; 2401 struct stat64 statbuf; 2402 struct extmnttab entry; 2403 const char *cmdname = (type == OP_SHARE) ? "unshare" : "unmount"; 2404 char property[ZFS_MAXPROPLEN]; 2405 2406 /* 2407 * Search for the path in /etc/mnttab. Rather than looking for the 2408 * specific path, which can be fooled by non-standard paths (i.e. ".." 2409 * or "//"), we stat() the path and search for the corresponding 2410 * (major,minor) device pair. 2411 */ 2412 if (stat64(path, &statbuf) != 0) { 2413 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 2414 cmdname, path, strerror(errno)); 2415 return (1); 2416 } 2417 2418 /* 2419 * Search for the given (major,minor) pair in the mount table. 2420 */ 2421 rewind(mnttab_file); 2422 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) { 2423 if (entry.mnt_major == major(statbuf.st_dev) && 2424 entry.mnt_minor == minor(statbuf.st_dev)) 2425 break; 2426 } 2427 if (ret != 0) { 2428 (void) fprintf(stderr, gettext("cannot %s '%s': not " 2429 "currently mounted\n"), cmdname, path); 2430 return (1); 2431 } 2432 2433 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 2434 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS " 2435 "filesystem\n"), cmdname, path); 2436 return (1); 2437 } 2438 2439 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 2440 ZFS_TYPE_FILESYSTEM)) == NULL) 2441 return (1); 2442 2443 verify(zfs_prop_get(zhp, type == OP_SHARE ? 2444 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, property, 2445 sizeof (property), NULL, NULL, 0, B_FALSE) == 0); 2446 2447 if (type == OP_SHARE) { 2448 if (strcmp(property, "off") == 0) { 2449 (void) fprintf(stderr, gettext("cannot unshare " 2450 "'%s': legacy share\n"), path); 2451 (void) fprintf(stderr, gettext("use " 2452 "unshare(1M) to unshare this filesystem\n")); 2453 ret = 1; 2454 } else if (!zfs_is_shared(zhp, NULL)) { 2455 (void) fprintf(stderr, gettext("cannot unshare '%s': " 2456 "not currently shared\n"), path); 2457 ret = 1; 2458 } else { 2459 ret = zfs_unshareall(zhp); 2460 } 2461 } else { 2462 if (is_manual) { 2463 ret = zfs_unmount(zhp, NULL, flags); 2464 } else if (strcmp(property, "legacy") == 0) { 2465 (void) fprintf(stderr, gettext("cannot unmount " 2466 "'%s': legacy mountpoint\n"), 2467 zfs_get_name(zhp)); 2468 (void) fprintf(stderr, gettext("use umount(1M) " 2469 "to unmount this filesystem\n")); 2470 ret = 1; 2471 } else { 2472 ret = zfs_unmountall(zhp, flags); 2473 } 2474 } 2475 2476 zfs_close(zhp); 2477 2478 return (ret != 0); 2479 } 2480 2481 /* 2482 * Generic callback for unsharing or unmounting a filesystem. 2483 */ 2484 static int 2485 unshare_unmount(int type, int argc, char **argv) 2486 { 2487 int do_all = 0; 2488 int flags = 0; 2489 int ret = 0; 2490 int c; 2491 zfs_handle_t *zhp; 2492 char property[ZFS_MAXPROPLEN]; 2493 2494 /* check options */ 2495 while ((c = getopt(argc, argv, type == OP_SHARE ? "a" : "af")) != -1) { 2496 switch (c) { 2497 case 'a': 2498 do_all = 1; 2499 break; 2500 case 'f': 2501 flags = MS_FORCE; 2502 break; 2503 case '?': 2504 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2505 optopt); 2506 usage(B_FALSE); 2507 } 2508 } 2509 2510 argc -= optind; 2511 argv += optind; 2512 2513 /* ensure correct number of arguments */ 2514 if (do_all) { 2515 if (argc != 0) { 2516 (void) fprintf(stderr, gettext("too many arguments\n")); 2517 usage(B_FALSE); 2518 } 2519 } else if (argc != 1) { 2520 if (argc == 0) 2521 (void) fprintf(stderr, 2522 gettext("missing filesystem argument\n")); 2523 else 2524 (void) fprintf(stderr, 2525 gettext("too many arguments\n")); 2526 usage(B_FALSE); 2527 } 2528 2529 if (do_all) { 2530 /* 2531 * We could make use of zfs_for_each() to walk all datasets in 2532 * the system, but this would be very inefficient, especially 2533 * since we would have to linearly search /etc/mnttab for each 2534 * one. Instead, do one pass through /etc/mnttab looking for 2535 * zfs entries and call zfs_unmount() for each one. 2536 * 2537 * Things get a little tricky if the administrator has created 2538 * mountpoints beneath other ZFS filesystems. In this case, we 2539 * have to unmount the deepest filesystems first. To accomplish 2540 * this, we place all the mountpoints in an AVL tree sorted by 2541 * the special type (dataset name), and walk the result in 2542 * reverse to make sure to get any snapshots first. 2543 */ 2544 struct mnttab entry; 2545 uu_avl_pool_t *pool; 2546 uu_avl_t *tree; 2547 unshare_unmount_node_t *node; 2548 uu_avl_index_t idx; 2549 uu_avl_walk_t *walk; 2550 2551 if ((pool = uu_avl_pool_create("unmount_pool", 2552 sizeof (unshare_unmount_node_t), 2553 offsetof(unshare_unmount_node_t, un_avlnode), 2554 unshare_unmount_compare, 2555 UU_DEFAULT)) == NULL) { 2556 (void) fprintf(stderr, gettext("internal error: " 2557 "out of memory\n")); 2558 exit(1); 2559 } 2560 2561 if ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL) { 2562 (void) fprintf(stderr, gettext("internal error: " 2563 "out of memory\n")); 2564 exit(1); 2565 } 2566 2567 rewind(mnttab_file); 2568 while (getmntent(mnttab_file, &entry) == 0) { 2569 2570 /* ignore non-ZFS entries */ 2571 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 2572 continue; 2573 2574 /* ignore snapshots */ 2575 if (strchr(entry.mnt_special, '@') != NULL) 2576 continue; 2577 2578 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 2579 ZFS_TYPE_FILESYSTEM)) == NULL) { 2580 ret = 1; 2581 continue; 2582 } 2583 2584 verify(zfs_prop_get(zhp, type == OP_SHARE ? 2585 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, 2586 property, sizeof (property), NULL, NULL, 2587 0, B_FALSE) == 0); 2588 2589 /* Ignore legacy mounts and shares */ 2590 if ((type == OP_SHARE && 2591 strcmp(property, "off") == 0) || 2592 (type == OP_MOUNT && 2593 strcmp(property, "legacy") == 0)) { 2594 zfs_close(zhp); 2595 continue; 2596 } 2597 2598 node = safe_malloc(sizeof (unshare_unmount_node_t)); 2599 node->un_zhp = zhp; 2600 2601 if ((node->un_mountp = strdup(entry.mnt_mountp)) == 2602 NULL) { 2603 (void) fprintf(stderr, gettext("internal error:" 2604 " out of memory\n")); 2605 exit(1); 2606 } 2607 2608 uu_avl_node_init(node, &node->un_avlnode, pool); 2609 2610 if (uu_avl_find(tree, node, NULL, &idx) == NULL) { 2611 uu_avl_insert(tree, node, idx); 2612 } else { 2613 zfs_close(node->un_zhp); 2614 free(node->un_mountp); 2615 free(node); 2616 } 2617 } 2618 2619 /* 2620 * Walk the AVL tree in reverse, unmounting each filesystem and 2621 * removing it from the AVL tree in the process. 2622 */ 2623 if ((walk = uu_avl_walk_start(tree, 2624 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) { 2625 (void) fprintf(stderr, 2626 gettext("internal error: out of memory")); 2627 exit(1); 2628 } 2629 2630 while ((node = uu_avl_walk_next(walk)) != NULL) { 2631 uu_avl_remove(tree, node); 2632 2633 switch (type) { 2634 case OP_SHARE: 2635 if (zfs_unshare(node->un_zhp, 2636 node->un_mountp) != 0) 2637 ret = 1; 2638 break; 2639 2640 case OP_MOUNT: 2641 if (zfs_unmount(node->un_zhp, 2642 node->un_mountp, flags) != 0) 2643 ret = 1; 2644 break; 2645 } 2646 2647 zfs_close(node->un_zhp); 2648 free(node->un_mountp); 2649 free(node); 2650 } 2651 2652 uu_avl_walk_end(walk); 2653 uu_avl_destroy(tree); 2654 uu_avl_pool_destroy(pool); 2655 } else { 2656 /* 2657 * We have an argument, but it may be a full path or a ZFS 2658 * filesystem. Pass full paths off to unmount_path() (shared by 2659 * manual_unmount), otherwise open the filesystem and pass to 2660 * zfs_unmount(). 2661 */ 2662 if (argv[0][0] == '/') 2663 return (unshare_unmount_path(type, argv[0], 2664 flags, B_FALSE)); 2665 2666 if ((zhp = zfs_open(g_zfs, argv[0], 2667 ZFS_TYPE_FILESYSTEM)) == NULL) 2668 return (1); 2669 2670 verify(zfs_prop_get(zhp, type == OP_SHARE ? 2671 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, property, 2672 sizeof (property), NULL, NULL, 0, B_FALSE) == 0); 2673 2674 switch (type) { 2675 case OP_SHARE: 2676 if (strcmp(property, "off") == 0) { 2677 (void) fprintf(stderr, gettext("cannot unshare " 2678 "'%s': legacy share\n"), zfs_get_name(zhp)); 2679 (void) fprintf(stderr, gettext("use unshare(1M)" 2680 " to unshare this filesystem\n")); 2681 ret = 1; 2682 } else if (!zfs_is_shared(zhp, NULL)) { 2683 (void) fprintf(stderr, gettext("cannot unshare " 2684 "'%s': not currently shared\n"), 2685 zfs_get_name(zhp)); 2686 ret = 1; 2687 } else if (zfs_unshareall(zhp) != 0) { 2688 ret = 1; 2689 } 2690 break; 2691 2692 case OP_MOUNT: 2693 if (strcmp(property, "legacy") == 0) { 2694 (void) fprintf(stderr, gettext("cannot unmount " 2695 "'%s': legacy mountpoint\n"), 2696 zfs_get_name(zhp)); 2697 (void) fprintf(stderr, gettext("use umount(1M) " 2698 "to unmount this filesystem\n")); 2699 ret = 1; 2700 } else if (!zfs_is_mounted(zhp, NULL)) { 2701 (void) fprintf(stderr, gettext("cannot unmount " 2702 "'%s': not currently mounted\n"), 2703 zfs_get_name(zhp)); 2704 ret = 1; 2705 } else if (zfs_unmountall(zhp, flags) != 0) { 2706 ret = 1; 2707 } 2708 } 2709 2710 zfs_close(zhp); 2711 } 2712 2713 return (ret); 2714 } 2715 2716 /* 2717 * zfs unmount -a 2718 * zfs unmount filesystem 2719 * 2720 * Unmount all filesystems, or a specific ZFS filesystem. 2721 */ 2722 static int 2723 zfs_do_unmount(int argc, char **argv) 2724 { 2725 return (unshare_unmount(OP_MOUNT, argc, argv)); 2726 } 2727 2728 /* 2729 * zfs unshare -a 2730 * zfs unshare filesystem 2731 * 2732 * Unshare all filesystems, or a specific ZFS filesystem. 2733 */ 2734 static int 2735 zfs_do_unshare(int argc, char **argv) 2736 { 2737 return (unshare_unmount(OP_SHARE, argc, argv)); 2738 } 2739 2740 /* 2741 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is 2742 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'. 2743 */ 2744 static int 2745 manual_mount(int argc, char **argv) 2746 { 2747 zfs_handle_t *zhp; 2748 char mountpoint[ZFS_MAXPROPLEN]; 2749 char mntopts[MNT_LINE_MAX] = { '\0' }; 2750 int ret; 2751 int c; 2752 int flags = 0; 2753 char *dataset, *path; 2754 2755 /* check options */ 2756 while ((c = getopt(argc, argv, ":mo:O")) != -1) { 2757 switch (c) { 2758 case 'o': 2759 (void) strlcpy(mntopts, optarg, sizeof (mntopts)); 2760 break; 2761 case 'O': 2762 flags |= MS_OVERLAY; 2763 break; 2764 case 'm': 2765 flags |= MS_NOMNTTAB; 2766 break; 2767 case ':': 2768 (void) fprintf(stderr, gettext("missing argument for " 2769 "'%c' option\n"), optopt); 2770 usage(B_FALSE); 2771 break; 2772 case '?': 2773 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2774 optopt); 2775 (void) fprintf(stderr, gettext("usage: mount [-o opts] " 2776 "<path>\n")); 2777 return (2); 2778 } 2779 } 2780 2781 argc -= optind; 2782 argv += optind; 2783 2784 /* check that we only have two arguments */ 2785 if (argc != 2) { 2786 if (argc == 0) 2787 (void) fprintf(stderr, gettext("missing dataset " 2788 "argument\n")); 2789 else if (argc == 1) 2790 (void) fprintf(stderr, 2791 gettext("missing mountpoint argument\n")); 2792 else 2793 (void) fprintf(stderr, gettext("too many arguments\n")); 2794 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n"); 2795 return (2); 2796 } 2797 2798 dataset = argv[0]; 2799 path = argv[1]; 2800 2801 /* try to open the dataset */ 2802 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) 2803 return (1); 2804 2805 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 2806 sizeof (mountpoint), NULL, NULL, 0, B_FALSE); 2807 2808 /* check for legacy mountpoint and complain appropriately */ 2809 ret = 0; 2810 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) { 2811 if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS, 2812 NULL, 0, mntopts, sizeof (mntopts)) != 0) { 2813 (void) fprintf(stderr, gettext("mount failed: %s\n"), 2814 strerror(errno)); 2815 ret = 1; 2816 } 2817 } else { 2818 (void) fprintf(stderr, gettext("filesystem '%s' cannot be " 2819 "mounted using 'mount -F zfs'\n"), dataset); 2820 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' " 2821 "instead.\n"), path); 2822 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' " 2823 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n")); 2824 (void) fprintf(stderr, gettext("See zfs(1M) for more " 2825 "information.\n")); 2826 ret = 1; 2827 } 2828 2829 return (ret); 2830 } 2831 2832 /* 2833 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow 2834 * unmounts of non-legacy filesystems, as this is the dominant administrative 2835 * interface. 2836 */ 2837 static int 2838 manual_unmount(int argc, char **argv) 2839 { 2840 int flags = 0; 2841 int c; 2842 2843 /* check options */ 2844 while ((c = getopt(argc, argv, "f")) != -1) { 2845 switch (c) { 2846 case 'f': 2847 flags = MS_FORCE; 2848 break; 2849 case '?': 2850 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2851 optopt); 2852 (void) fprintf(stderr, gettext("usage: unmount [-f] " 2853 "<path>\n")); 2854 return (2); 2855 } 2856 } 2857 2858 argc -= optind; 2859 argv += optind; 2860 2861 /* check arguments */ 2862 if (argc != 1) { 2863 if (argc == 0) 2864 (void) fprintf(stderr, gettext("missing path " 2865 "argument\n")); 2866 else 2867 (void) fprintf(stderr, gettext("too many arguments\n")); 2868 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n")); 2869 return (2); 2870 } 2871 2872 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE)); 2873 } 2874 2875 static int 2876 volcheck(zpool_handle_t *zhp, void *data) 2877 { 2878 int isinit = (int)data; 2879 2880 if (isinit) 2881 return (zpool_create_zvol_links(zhp)); 2882 else 2883 return (zpool_remove_zvol_links(zhp)); 2884 } 2885 2886 /* 2887 * Iterate over all pools in the system and either create or destroy /dev/zvol 2888 * links, depending on the value of 'isinit'. 2889 */ 2890 static int 2891 do_volcheck(boolean_t isinit) 2892 { 2893 return (zpool_iter(g_zfs, volcheck, (void *)isinit) ? 1 : 0); 2894 } 2895 2896 int 2897 main(int argc, char **argv) 2898 { 2899 int ret; 2900 int i; 2901 char *progname; 2902 char *cmdname; 2903 2904 (void) setlocale(LC_ALL, ""); 2905 (void) textdomain(TEXT_DOMAIN); 2906 2907 opterr = 0; 2908 2909 if ((g_zfs = libzfs_init()) == NULL) { 2910 (void) fprintf(stderr, gettext("internal error: failed to " 2911 "initialize ZFS library\n")); 2912 return (1); 2913 } 2914 2915 libzfs_print_on_error(g_zfs, B_TRUE); 2916 2917 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) { 2918 (void) fprintf(stderr, gettext("internal error: unable to " 2919 "open %s\n"), MNTTAB); 2920 return (1); 2921 } 2922 2923 /* 2924 * This command also doubles as the /etc/fs mount and unmount program. 2925 * Determine if we should take this behavior based on argv[0]. 2926 */ 2927 progname = basename(argv[0]); 2928 if (strcmp(progname, "mount") == 0) { 2929 ret = manual_mount(argc, argv); 2930 } else if (strcmp(progname, "umount") == 0) { 2931 ret = manual_unmount(argc, argv); 2932 } else { 2933 /* 2934 * Make sure the user has specified some command. 2935 */ 2936 if (argc < 2) { 2937 (void) fprintf(stderr, gettext("missing command\n")); 2938 usage(B_FALSE); 2939 } 2940 2941 cmdname = argv[1]; 2942 2943 /* 2944 * The 'umount' command is an alias for 'unmount' 2945 */ 2946 if (strcmp(cmdname, "umount") == 0) 2947 cmdname = "unmount"; 2948 2949 /* 2950 * The 'recv' command is an alias for 'receive' 2951 */ 2952 if (strcmp(cmdname, "recv") == 0) 2953 cmdname = "receive"; 2954 2955 /* 2956 * Special case '-?' 2957 */ 2958 if (strcmp(cmdname, "-?") == 0) 2959 usage(B_TRUE); 2960 2961 /* 2962 * 'volinit' and 'volfini' do not appear in the usage message, 2963 * so we have to special case them here. 2964 */ 2965 if (strcmp(cmdname, "volinit") == 0) 2966 return (do_volcheck(B_TRUE)); 2967 else if (strcmp(cmdname, "volfini") == 0) 2968 return (do_volcheck(B_FALSE)); 2969 2970 /* 2971 * Run the appropriate command. 2972 */ 2973 for (i = 0; i < NCOMMAND; i++) { 2974 if (command_table[i].name == NULL) 2975 continue; 2976 2977 if (strcmp(cmdname, command_table[i].name) == 0) { 2978 current_command = &command_table[i]; 2979 ret = command_table[i].func(argc - 1, argv + 1); 2980 break; 2981 } 2982 } 2983 2984 if (i == NCOMMAND) { 2985 (void) fprintf(stderr, gettext("unrecognized " 2986 "command '%s'\n"), cmdname); 2987 usage(B_FALSE); 2988 } 2989 } 2990 2991 (void) fclose(mnttab_file); 2992 2993 libzfs_fini(g_zfs); 2994 2995 /* 2996 * The 'ZFS_ABORT' environment variable causes us to dump core on exit 2997 * for the purposes of running ::findleaks. 2998 */ 2999 if (getenv("ZFS_ABORT") != NULL) { 3000 (void) printf("dumping core by request\n"); 3001 abort(); 3002 } 3003 3004 return (ret); 3005 } 3006