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