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