1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2011, 2014 by Delphix. All rights reserved. 25 * Copyright 2012 Milan Jurik. All rights reserved. 26 * Copyright (c) 2012, Joyent, Inc. All rights reserved. 27 * Copyright (c) 2013 Steven Hartland. All rights reserved. 28 * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 29 */ 30 31 #include <assert.h> 32 #include <ctype.h> 33 #include <errno.h> 34 #include <libgen.h> 35 #include <libintl.h> 36 #include <libuutil.h> 37 #include <libnvpair.h> 38 #include <locale.h> 39 #include <stddef.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <strings.h> 43 #include <unistd.h> 44 #include <fcntl.h> 45 #include <zone.h> 46 #include <grp.h> 47 #include <pwd.h> 48 #include <signal.h> 49 #include <sys/list.h> 50 #include <sys/mkdev.h> 51 #include <sys/mntent.h> 52 #include <sys/mnttab.h> 53 #include <sys/mount.h> 54 #include <sys/stat.h> 55 #include <sys/fs/zfs.h> 56 #include <sys/types.h> 57 #include <time.h> 58 59 #include <libzfs.h> 60 #include <libzfs_core.h> 61 #include <zfs_prop.h> 62 #include <zfs_deleg.h> 63 #include <libuutil.h> 64 #include <aclutils.h> 65 #include <directory.h> 66 67 #include "zfs_iter.h" 68 #include "zfs_util.h" 69 #include "zfs_comutil.h" 70 71 libzfs_handle_t *g_zfs; 72 73 static FILE *mnttab_file; 74 static char history_str[HIS_MAX_RECORD_LEN]; 75 static boolean_t log_history = B_TRUE; 76 77 static int zfs_do_clone(int argc, char **argv); 78 static int zfs_do_create(int argc, char **argv); 79 static int zfs_do_destroy(int argc, char **argv); 80 static int zfs_do_get(int argc, char **argv); 81 static int zfs_do_inherit(int argc, char **argv); 82 static int zfs_do_list(int argc, char **argv); 83 static int zfs_do_mount(int argc, char **argv); 84 static int zfs_do_rename(int argc, char **argv); 85 static int zfs_do_rollback(int argc, char **argv); 86 static int zfs_do_set(int argc, char **argv); 87 static int zfs_do_upgrade(int argc, char **argv); 88 static int zfs_do_snapshot(int argc, char **argv); 89 static int zfs_do_unmount(int argc, char **argv); 90 static int zfs_do_share(int argc, char **argv); 91 static int zfs_do_unshare(int argc, char **argv); 92 static int zfs_do_send(int argc, char **argv); 93 static int zfs_do_receive(int argc, char **argv); 94 static int zfs_do_promote(int argc, char **argv); 95 static int zfs_do_userspace(int argc, char **argv); 96 static int zfs_do_allow(int argc, char **argv); 97 static int zfs_do_unallow(int argc, char **argv); 98 static int zfs_do_hold(int argc, char **argv); 99 static int zfs_do_holds(int argc, char **argv); 100 static int zfs_do_release(int argc, char **argv); 101 static int zfs_do_diff(int argc, char **argv); 102 static int zfs_do_bookmark(int argc, char **argv); 103 104 /* 105 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds. 106 */ 107 108 #ifdef DEBUG 109 const char * 110 _umem_debug_init(void) 111 { 112 return ("default,verbose"); /* $UMEM_DEBUG setting */ 113 } 114 115 const char * 116 _umem_logging_init(void) 117 { 118 return ("fail,contents"); /* $UMEM_LOGGING setting */ 119 } 120 #endif 121 122 typedef enum { 123 HELP_CLONE, 124 HELP_CREATE, 125 HELP_DESTROY, 126 HELP_GET, 127 HELP_INHERIT, 128 HELP_UPGRADE, 129 HELP_LIST, 130 HELP_MOUNT, 131 HELP_PROMOTE, 132 HELP_RECEIVE, 133 HELP_RENAME, 134 HELP_ROLLBACK, 135 HELP_SEND, 136 HELP_SET, 137 HELP_SHARE, 138 HELP_SNAPSHOT, 139 HELP_UNMOUNT, 140 HELP_UNSHARE, 141 HELP_ALLOW, 142 HELP_UNALLOW, 143 HELP_USERSPACE, 144 HELP_GROUPSPACE, 145 HELP_HOLD, 146 HELP_HOLDS, 147 HELP_RELEASE, 148 HELP_DIFF, 149 HELP_BOOKMARK, 150 } zfs_help_t; 151 152 typedef struct zfs_command { 153 const char *name; 154 int (*func)(int argc, char **argv); 155 zfs_help_t usage; 156 } zfs_command_t; 157 158 /* 159 * Master command table. Each ZFS command has a name, associated function, and 160 * usage message. The usage messages need to be internationalized, so we have 161 * to have a function to return the usage message based on a command index. 162 * 163 * These commands are organized according to how they are displayed in the usage 164 * message. An empty command (one with a NULL name) indicates an empty line in 165 * the generic usage message. 166 */ 167 static zfs_command_t command_table[] = { 168 { "create", zfs_do_create, HELP_CREATE }, 169 { "destroy", zfs_do_destroy, HELP_DESTROY }, 170 { NULL }, 171 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT }, 172 { "rollback", zfs_do_rollback, HELP_ROLLBACK }, 173 { "clone", zfs_do_clone, HELP_CLONE }, 174 { "promote", zfs_do_promote, HELP_PROMOTE }, 175 { "rename", zfs_do_rename, HELP_RENAME }, 176 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK }, 177 { NULL }, 178 { "list", zfs_do_list, HELP_LIST }, 179 { NULL }, 180 { "set", zfs_do_set, HELP_SET }, 181 { "get", zfs_do_get, HELP_GET }, 182 { "inherit", zfs_do_inherit, HELP_INHERIT }, 183 { "upgrade", zfs_do_upgrade, HELP_UPGRADE }, 184 { "userspace", zfs_do_userspace, HELP_USERSPACE }, 185 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE }, 186 { NULL }, 187 { "mount", zfs_do_mount, HELP_MOUNT }, 188 { "unmount", zfs_do_unmount, HELP_UNMOUNT }, 189 { "share", zfs_do_share, HELP_SHARE }, 190 { "unshare", zfs_do_unshare, HELP_UNSHARE }, 191 { NULL }, 192 { "send", zfs_do_send, HELP_SEND }, 193 { "receive", zfs_do_receive, HELP_RECEIVE }, 194 { NULL }, 195 { "allow", zfs_do_allow, HELP_ALLOW }, 196 { NULL }, 197 { "unallow", zfs_do_unallow, HELP_UNALLOW }, 198 { NULL }, 199 { "hold", zfs_do_hold, HELP_HOLD }, 200 { "holds", zfs_do_holds, HELP_HOLDS }, 201 { "release", zfs_do_release, HELP_RELEASE }, 202 { "diff", zfs_do_diff, HELP_DIFF }, 203 }; 204 205 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0])) 206 207 zfs_command_t *current_command; 208 209 static const char * 210 get_usage(zfs_help_t idx) 211 { 212 switch (idx) { 213 case HELP_CLONE: 214 return (gettext("\tclone [-p] [-o property=value] ... " 215 "<snapshot> <filesystem|volume>\n")); 216 case HELP_CREATE: 217 return (gettext("\tcreate [-p] [-o property=value] ... " 218 "<filesystem>\n" 219 "\tcreate [-ps] [-b blocksize] [-o property=value] ... " 220 "-V <size> <volume>\n")); 221 case HELP_DESTROY: 222 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n" 223 "\tdestroy [-dnpRrv] " 224 "<filesystem|volume>@<snap>[%<snap>][,...]\n" 225 "\tdestroy <filesystem|volume>#<bookmark>\n")); 226 case HELP_GET: 227 return (gettext("\tget [-rHp] [-d max] " 228 "[-o \"all\" | field[,...]]\n" 229 "\t [-t type[,...]] [-s source[,...]]\n" 230 "\t <\"all\" | property[,...]> " 231 "[filesystem|volume|snapshot] ...\n")); 232 case HELP_INHERIT: 233 return (gettext("\tinherit [-rS] <property> " 234 "<filesystem|volume|snapshot> ...\n")); 235 case HELP_UPGRADE: 236 return (gettext("\tupgrade [-v]\n" 237 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n")); 238 case HELP_LIST: 239 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] " 240 "[-s property]...\n\t [-S property]... [-t type[,...]] " 241 "[filesystem|volume|snapshot] ...\n")); 242 case HELP_MOUNT: 243 return (gettext("\tmount\n" 244 "\tmount [-vO] [-o opts] <-a | filesystem>\n")); 245 case HELP_PROMOTE: 246 return (gettext("\tpromote <clone-filesystem>\n")); 247 case HELP_RECEIVE: 248 return (gettext("\treceive [-vnFu] <filesystem|volume|" 249 "snapshot>\n" 250 "\treceive [-vnFu] [-d | -e] <filesystem>\n")); 251 case HELP_RENAME: 252 return (gettext("\trename [-f] <filesystem|volume|snapshot> " 253 "<filesystem|volume|snapshot>\n" 254 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n" 255 "\trename -r <snapshot> <snapshot>\n")); 256 case HELP_ROLLBACK: 257 return (gettext("\trollback [-rRf] <snapshot>\n")); 258 case HELP_SEND: 259 return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] " 260 "<snapshot>\n" 261 "\tsend [-Le] [-i snapshot|bookmark] " 262 "<filesystem|volume|snapshot>\n")); 263 case HELP_SET: 264 return (gettext("\tset <property=value> " 265 "<filesystem|volume|snapshot> ...\n")); 266 case HELP_SHARE: 267 return (gettext("\tshare <-a | filesystem>\n")); 268 case HELP_SNAPSHOT: 269 return (gettext("\tsnapshot [-r] [-o property=value] ... " 270 "<filesystem|volume>@<snap> ...\n")); 271 case HELP_UNMOUNT: 272 return (gettext("\tunmount [-f] " 273 "<-a | filesystem|mountpoint>\n")); 274 case HELP_UNSHARE: 275 return (gettext("\tunshare " 276 "<-a | filesystem|mountpoint>\n")); 277 case HELP_ALLOW: 278 return (gettext("\tallow <filesystem|volume>\n" 279 "\tallow [-ldug] " 280 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n" 281 "\t <filesystem|volume>\n" 282 "\tallow [-ld] -e <perm|@setname>[,...] " 283 "<filesystem|volume>\n" 284 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n" 285 "\tallow -s @setname <perm|@setname>[,...] " 286 "<filesystem|volume>\n")); 287 case HELP_UNALLOW: 288 return (gettext("\tunallow [-rldug] " 289 "<\"everyone\"|user|group>[,...]\n" 290 "\t [<perm|@setname>[,...]] <filesystem|volume>\n" 291 "\tunallow [-rld] -e [<perm|@setname>[,...]] " 292 "<filesystem|volume>\n" 293 "\tunallow [-r] -c [<perm|@setname>[,...]] " 294 "<filesystem|volume>\n" 295 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] " 296 "<filesystem|volume>\n")); 297 case HELP_USERSPACE: 298 return (gettext("\tuserspace [-Hinp] [-o field[,...]] " 299 "[-s field] ...\n" 300 "\t [-S field] ... [-t type[,...]] " 301 "<filesystem|snapshot>\n")); 302 case HELP_GROUPSPACE: 303 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] " 304 "[-s field] ...\n" 305 "\t [-S field] ... [-t type[,...]] " 306 "<filesystem|snapshot>\n")); 307 case HELP_HOLD: 308 return (gettext("\thold [-r] <tag> <snapshot> ...\n")); 309 case HELP_HOLDS: 310 return (gettext("\tholds [-r] <snapshot> ...\n")); 311 case HELP_RELEASE: 312 return (gettext("\trelease [-r] <tag> <snapshot> ...\n")); 313 case HELP_DIFF: 314 return (gettext("\tdiff [-FHt] <snapshot> " 315 "[snapshot|filesystem]\n")); 316 case HELP_BOOKMARK: 317 return (gettext("\tbookmark <snapshot> <bookmark>\n")); 318 } 319 320 abort(); 321 /* NOTREACHED */ 322 } 323 324 void 325 nomem(void) 326 { 327 (void) fprintf(stderr, gettext("internal error: out of memory\n")); 328 exit(1); 329 } 330 331 /* 332 * Utility function to guarantee malloc() success. 333 */ 334 335 void * 336 safe_malloc(size_t size) 337 { 338 void *data; 339 340 if ((data = calloc(1, size)) == NULL) 341 nomem(); 342 343 return (data); 344 } 345 346 static char * 347 safe_strdup(char *str) 348 { 349 char *dupstr = strdup(str); 350 351 if (dupstr == NULL) 352 nomem(); 353 354 return (dupstr); 355 } 356 357 /* 358 * Callback routine that will print out information for each of 359 * the properties. 360 */ 361 static int 362 usage_prop_cb(int prop, void *cb) 363 { 364 FILE *fp = cb; 365 366 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop)); 367 368 if (zfs_prop_readonly(prop)) 369 (void) fprintf(fp, " NO "); 370 else 371 (void) fprintf(fp, "YES "); 372 373 if (zfs_prop_inheritable(prop)) 374 (void) fprintf(fp, " YES "); 375 else 376 (void) fprintf(fp, " NO "); 377 378 if (zfs_prop_values(prop) == NULL) 379 (void) fprintf(fp, "-\n"); 380 else 381 (void) fprintf(fp, "%s\n", zfs_prop_values(prop)); 382 383 return (ZPROP_CONT); 384 } 385 386 /* 387 * Display usage message. If we're inside a command, display only the usage for 388 * that command. Otherwise, iterate over the entire command table and display 389 * a complete usage message. 390 */ 391 static void 392 usage(boolean_t requested) 393 { 394 int i; 395 boolean_t show_properties = B_FALSE; 396 FILE *fp = requested ? stdout : stderr; 397 398 if (current_command == NULL) { 399 400 (void) fprintf(fp, gettext("usage: zfs command args ...\n")); 401 (void) fprintf(fp, 402 gettext("where 'command' is one of the following:\n\n")); 403 404 for (i = 0; i < NCOMMAND; i++) { 405 if (command_table[i].name == NULL) 406 (void) fprintf(fp, "\n"); 407 else 408 (void) fprintf(fp, "%s", 409 get_usage(command_table[i].usage)); 410 } 411 412 (void) fprintf(fp, gettext("\nEach dataset is of the form: " 413 "pool/[dataset/]*dataset[@name]\n")); 414 } else { 415 (void) fprintf(fp, gettext("usage:\n")); 416 (void) fprintf(fp, "%s", get_usage(current_command->usage)); 417 } 418 419 if (current_command != NULL && 420 (strcmp(current_command->name, "set") == 0 || 421 strcmp(current_command->name, "get") == 0 || 422 strcmp(current_command->name, "inherit") == 0 || 423 strcmp(current_command->name, "list") == 0)) 424 show_properties = B_TRUE; 425 426 if (show_properties) { 427 (void) fprintf(fp, 428 gettext("\nThe following properties are supported:\n")); 429 430 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n", 431 "PROPERTY", "EDIT", "INHERIT", "VALUES"); 432 433 /* Iterate over all properties */ 434 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE, 435 ZFS_TYPE_DATASET); 436 437 (void) fprintf(fp, "\t%-15s ", "userused@..."); 438 (void) fprintf(fp, " NO NO <size>\n"); 439 (void) fprintf(fp, "\t%-15s ", "groupused@..."); 440 (void) fprintf(fp, " NO NO <size>\n"); 441 (void) fprintf(fp, "\t%-15s ", "userquota@..."); 442 (void) fprintf(fp, "YES NO <size> | none\n"); 443 (void) fprintf(fp, "\t%-15s ", "groupquota@..."); 444 (void) fprintf(fp, "YES NO <size> | none\n"); 445 (void) fprintf(fp, "\t%-15s ", "written@<snap>"); 446 (void) fprintf(fp, " NO NO <size>\n"); 447 448 (void) fprintf(fp, gettext("\nSizes are specified in bytes " 449 "with standard units such as K, M, G, etc.\n")); 450 (void) fprintf(fp, gettext("\nUser-defined properties can " 451 "be specified by using a name containing a colon (:).\n")); 452 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ " 453 "properties must be appended with\n" 454 "a user or group specifier of one of these forms:\n" 455 " POSIX name (eg: \"matt\")\n" 456 " POSIX id (eg: \"126829\")\n" 457 " SMB name@domain (eg: \"matt@sun\")\n" 458 " SMB SID (eg: \"S-1-234-567-89\")\n")); 459 } else { 460 (void) fprintf(fp, 461 gettext("\nFor the property list, run: %s\n"), 462 "zfs set|get"); 463 (void) fprintf(fp, 464 gettext("\nFor the delegated permission list, run: %s\n"), 465 "zfs allow|unallow"); 466 } 467 468 /* 469 * See comments at end of main(). 470 */ 471 if (getenv("ZFS_ABORT") != NULL) { 472 (void) printf("dumping core by request\n"); 473 abort(); 474 } 475 476 exit(requested ? 0 : 2); 477 } 478 479 static int 480 parseprop(nvlist_t *props) 481 { 482 char *propname = optarg; 483 char *propval, *strval; 484 485 if ((propval = strchr(propname, '=')) == NULL) { 486 (void) fprintf(stderr, gettext("missing " 487 "'=' for -o option\n")); 488 return (-1); 489 } 490 *propval = '\0'; 491 propval++; 492 if (nvlist_lookup_string(props, propname, &strval) == 0) { 493 (void) fprintf(stderr, gettext("property '%s' " 494 "specified multiple times\n"), propname); 495 return (-1); 496 } 497 if (nvlist_add_string(props, propname, propval) != 0) 498 nomem(); 499 return (0); 500 } 501 502 static int 503 parse_depth(char *opt, int *flags) 504 { 505 char *tmp; 506 int depth; 507 508 depth = (int)strtol(opt, &tmp, 0); 509 if (*tmp) { 510 (void) fprintf(stderr, 511 gettext("%s is not an integer\n"), optarg); 512 usage(B_FALSE); 513 } 514 if (depth < 0) { 515 (void) fprintf(stderr, 516 gettext("Depth can not be negative.\n")); 517 usage(B_FALSE); 518 } 519 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE); 520 return (depth); 521 } 522 523 #define PROGRESS_DELAY 2 /* seconds */ 524 525 static char *pt_reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"; 526 static time_t pt_begin; 527 static char *pt_header = NULL; 528 static boolean_t pt_shown; 529 530 static void 531 start_progress_timer(void) 532 { 533 pt_begin = time(NULL) + PROGRESS_DELAY; 534 pt_shown = B_FALSE; 535 } 536 537 static void 538 set_progress_header(char *header) 539 { 540 assert(pt_header == NULL); 541 pt_header = safe_strdup(header); 542 if (pt_shown) { 543 (void) printf("%s: ", header); 544 (void) fflush(stdout); 545 } 546 } 547 548 static void 549 update_progress(char *update) 550 { 551 if (!pt_shown && time(NULL) > pt_begin) { 552 int len = strlen(update); 553 554 (void) printf("%s: %s%*.*s", pt_header, update, len, len, 555 pt_reverse); 556 (void) fflush(stdout); 557 pt_shown = B_TRUE; 558 } else if (pt_shown) { 559 int len = strlen(update); 560 561 (void) printf("%s%*.*s", update, len, len, pt_reverse); 562 (void) fflush(stdout); 563 } 564 } 565 566 static void 567 finish_progress(char *done) 568 { 569 if (pt_shown) { 570 (void) printf("%s\n", done); 571 (void) fflush(stdout); 572 } 573 free(pt_header); 574 pt_header = NULL; 575 } 576 577 /* 578 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol> 579 * 580 * Given an existing dataset, create a writable copy whose initial contents 581 * are the same as the source. The newly created dataset maintains a 582 * dependency on the original; the original cannot be destroyed so long as 583 * the clone exists. 584 * 585 * The '-p' flag creates all the non-existing ancestors of the target first. 586 */ 587 static int 588 zfs_do_clone(int argc, char **argv) 589 { 590 zfs_handle_t *zhp = NULL; 591 boolean_t parents = B_FALSE; 592 nvlist_t *props; 593 int ret = 0; 594 int c; 595 596 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 597 nomem(); 598 599 /* check options */ 600 while ((c = getopt(argc, argv, "o:p")) != -1) { 601 switch (c) { 602 case 'o': 603 if (parseprop(props)) 604 return (1); 605 break; 606 case 'p': 607 parents = B_TRUE; 608 break; 609 case '?': 610 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 611 optopt); 612 goto usage; 613 } 614 } 615 616 argc -= optind; 617 argv += optind; 618 619 /* check number of arguments */ 620 if (argc < 1) { 621 (void) fprintf(stderr, gettext("missing source dataset " 622 "argument\n")); 623 goto usage; 624 } 625 if (argc < 2) { 626 (void) fprintf(stderr, gettext("missing target dataset " 627 "argument\n")); 628 goto usage; 629 } 630 if (argc > 2) { 631 (void) fprintf(stderr, gettext("too many arguments\n")); 632 goto usage; 633 } 634 635 /* open the source dataset */ 636 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 637 return (1); 638 639 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM | 640 ZFS_TYPE_VOLUME)) { 641 /* 642 * Now create the ancestors of the target dataset. If the 643 * target already exists and '-p' option was used we should not 644 * complain. 645 */ 646 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | 647 ZFS_TYPE_VOLUME)) 648 return (0); 649 if (zfs_create_ancestors(g_zfs, argv[1]) != 0) 650 return (1); 651 } 652 653 /* pass to libzfs */ 654 ret = zfs_clone(zhp, argv[1], props); 655 656 /* create the mountpoint if necessary */ 657 if (ret == 0) { 658 zfs_handle_t *clone; 659 660 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET); 661 if (clone != NULL) { 662 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME) 663 if ((ret = zfs_mount(clone, NULL, 0)) == 0) 664 ret = zfs_share(clone); 665 zfs_close(clone); 666 } 667 } 668 669 zfs_close(zhp); 670 nvlist_free(props); 671 672 return (!!ret); 673 674 usage: 675 if (zhp) 676 zfs_close(zhp); 677 nvlist_free(props); 678 usage(B_FALSE); 679 return (-1); 680 } 681 682 /* 683 * zfs create [-p] [-o prop=value] ... fs 684 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size 685 * 686 * Create a new dataset. This command can be used to create filesystems 687 * and volumes. Snapshot creation is handled by 'zfs snapshot'. 688 * For volumes, the user must specify a size to be used. 689 * 690 * The '-s' flag applies only to volumes, and indicates that we should not try 691 * to set the reservation for this volume. By default we set a reservation 692 * equal to the size for any volume. For pools with SPA_VERSION >= 693 * SPA_VERSION_REFRESERVATION, we set a refreservation instead. 694 * 695 * The '-p' flag creates all the non-existing ancestors of the target first. 696 */ 697 static int 698 zfs_do_create(int argc, char **argv) 699 { 700 zfs_type_t type = ZFS_TYPE_FILESYSTEM; 701 zfs_handle_t *zhp = NULL; 702 uint64_t volsize; 703 int c; 704 boolean_t noreserve = B_FALSE; 705 boolean_t bflag = B_FALSE; 706 boolean_t parents = B_FALSE; 707 int ret = 1; 708 nvlist_t *props; 709 uint64_t intval; 710 int canmount = ZFS_CANMOUNT_OFF; 711 712 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 713 nomem(); 714 715 /* check options */ 716 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) { 717 switch (c) { 718 case 'V': 719 type = ZFS_TYPE_VOLUME; 720 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) { 721 (void) fprintf(stderr, gettext("bad volume " 722 "size '%s': %s\n"), optarg, 723 libzfs_error_description(g_zfs)); 724 goto error; 725 } 726 727 if (nvlist_add_uint64(props, 728 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0) 729 nomem(); 730 volsize = intval; 731 break; 732 case 'p': 733 parents = B_TRUE; 734 break; 735 case 'b': 736 bflag = B_TRUE; 737 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) { 738 (void) fprintf(stderr, gettext("bad volume " 739 "block size '%s': %s\n"), optarg, 740 libzfs_error_description(g_zfs)); 741 goto error; 742 } 743 744 if (nvlist_add_uint64(props, 745 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 746 intval) != 0) 747 nomem(); 748 break; 749 case 'o': 750 if (parseprop(props)) 751 goto error; 752 break; 753 case 's': 754 noreserve = B_TRUE; 755 break; 756 case ':': 757 (void) fprintf(stderr, gettext("missing size " 758 "argument\n")); 759 goto badusage; 760 case '?': 761 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 762 optopt); 763 goto badusage; 764 } 765 } 766 767 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) { 768 (void) fprintf(stderr, gettext("'-s' and '-b' can only be " 769 "used when creating a volume\n")); 770 goto badusage; 771 } 772 773 argc -= optind; 774 argv += optind; 775 776 /* check number of arguments */ 777 if (argc == 0) { 778 (void) fprintf(stderr, gettext("missing %s argument\n"), 779 zfs_type_to_name(type)); 780 goto badusage; 781 } 782 if (argc > 1) { 783 (void) fprintf(stderr, gettext("too many arguments\n")); 784 goto badusage; 785 } 786 787 if (type == ZFS_TYPE_VOLUME && !noreserve) { 788 zpool_handle_t *zpool_handle; 789 nvlist_t *real_props; 790 uint64_t spa_version; 791 char *p; 792 zfs_prop_t resv_prop; 793 char *strval; 794 char msg[1024]; 795 796 if (p = strchr(argv[0], '/')) 797 *p = '\0'; 798 zpool_handle = zpool_open(g_zfs, argv[0]); 799 if (p != NULL) 800 *p = '/'; 801 if (zpool_handle == NULL) 802 goto error; 803 spa_version = zpool_get_prop_int(zpool_handle, 804 ZPOOL_PROP_VERSION, NULL); 805 zpool_close(zpool_handle); 806 if (spa_version >= SPA_VERSION_REFRESERVATION) 807 resv_prop = ZFS_PROP_REFRESERVATION; 808 else 809 resv_prop = ZFS_PROP_RESERVATION; 810 811 (void) snprintf(msg, sizeof (msg), 812 gettext("cannot create '%s'"), argv[0]); 813 if (props && (real_props = zfs_valid_proplist(g_zfs, type, 814 props, 0, NULL, msg)) == NULL) 815 goto error; 816 817 volsize = zvol_volsize_to_reservation(volsize, real_props); 818 nvlist_free(real_props); 819 820 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop), 821 &strval) != 0) { 822 if (nvlist_add_uint64(props, 823 zfs_prop_to_name(resv_prop), volsize) != 0) { 824 nvlist_free(props); 825 nomem(); 826 } 827 } 828 } 829 830 if (parents && zfs_name_valid(argv[0], type)) { 831 /* 832 * Now create the ancestors of target dataset. If the target 833 * already exists and '-p' option was used we should not 834 * complain. 835 */ 836 if (zfs_dataset_exists(g_zfs, argv[0], type)) { 837 ret = 0; 838 goto error; 839 } 840 if (zfs_create_ancestors(g_zfs, argv[0]) != 0) 841 goto error; 842 } 843 844 /* pass to libzfs */ 845 if (zfs_create(g_zfs, argv[0], type, props) != 0) 846 goto error; 847 848 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL) 849 goto error; 850 851 ret = 0; 852 /* 853 * if the user doesn't want the dataset automatically mounted, 854 * then skip the mount/share step 855 */ 856 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type)) 857 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT); 858 859 /* 860 * Mount and/or share the new filesystem as appropriate. We provide a 861 * verbose error message to let the user know that their filesystem was 862 * in fact created, even if we failed to mount or share it. 863 */ 864 if (canmount == ZFS_CANMOUNT_ON) { 865 if (zfs_mount(zhp, NULL, 0) != 0) { 866 (void) fprintf(stderr, gettext("filesystem " 867 "successfully created, but not mounted\n")); 868 ret = 1; 869 } else if (zfs_share(zhp) != 0) { 870 (void) fprintf(stderr, gettext("filesystem " 871 "successfully created, but not shared\n")); 872 ret = 1; 873 } 874 } 875 876 error: 877 if (zhp) 878 zfs_close(zhp); 879 nvlist_free(props); 880 return (ret); 881 badusage: 882 nvlist_free(props); 883 usage(B_FALSE); 884 return (2); 885 } 886 887 /* 888 * zfs destroy [-rRf] <fs, vol> 889 * zfs destroy [-rRd] <snap> 890 * 891 * -r Recursively destroy all children 892 * -R Recursively destroy all dependents, including clones 893 * -f Force unmounting of any dependents 894 * -d If we can't destroy now, mark for deferred destruction 895 * 896 * Destroys the given dataset. By default, it will unmount any filesystems, 897 * and refuse to destroy a dataset that has any dependents. A dependent can 898 * either be a child, or a clone of a child. 899 */ 900 typedef struct destroy_cbdata { 901 boolean_t cb_first; 902 boolean_t cb_force; 903 boolean_t cb_recurse; 904 boolean_t cb_error; 905 boolean_t cb_doclones; 906 zfs_handle_t *cb_target; 907 boolean_t cb_defer_destroy; 908 boolean_t cb_verbose; 909 boolean_t cb_parsable; 910 boolean_t cb_dryrun; 911 nvlist_t *cb_nvl; 912 nvlist_t *cb_batchedsnaps; 913 914 /* first snap in contiguous run */ 915 char *cb_firstsnap; 916 /* previous snap in contiguous run */ 917 char *cb_prevsnap; 918 int64_t cb_snapused; 919 char *cb_snapspec; 920 char *cb_bookmark; 921 } destroy_cbdata_t; 922 923 /* 924 * Check for any dependents based on the '-r' or '-R' flags. 925 */ 926 static int 927 destroy_check_dependent(zfs_handle_t *zhp, void *data) 928 { 929 destroy_cbdata_t *cbp = data; 930 const char *tname = zfs_get_name(cbp->cb_target); 931 const char *name = zfs_get_name(zhp); 932 933 if (strncmp(tname, name, strlen(tname)) == 0 && 934 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) { 935 /* 936 * This is a direct descendant, not a clone somewhere else in 937 * the hierarchy. 938 */ 939 if (cbp->cb_recurse) 940 goto out; 941 942 if (cbp->cb_first) { 943 (void) fprintf(stderr, gettext("cannot destroy '%s': " 944 "%s has children\n"), 945 zfs_get_name(cbp->cb_target), 946 zfs_type_to_name(zfs_get_type(cbp->cb_target))); 947 (void) fprintf(stderr, gettext("use '-r' to destroy " 948 "the following datasets:\n")); 949 cbp->cb_first = B_FALSE; 950 cbp->cb_error = B_TRUE; 951 } 952 953 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 954 } else { 955 /* 956 * This is a clone. We only want to report this if the '-r' 957 * wasn't specified, or the target is a snapshot. 958 */ 959 if (!cbp->cb_recurse && 960 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT) 961 goto out; 962 963 if (cbp->cb_first) { 964 (void) fprintf(stderr, gettext("cannot destroy '%s': " 965 "%s has dependent clones\n"), 966 zfs_get_name(cbp->cb_target), 967 zfs_type_to_name(zfs_get_type(cbp->cb_target))); 968 (void) fprintf(stderr, gettext("use '-R' to destroy " 969 "the following datasets:\n")); 970 cbp->cb_first = B_FALSE; 971 cbp->cb_error = B_TRUE; 972 cbp->cb_dryrun = B_TRUE; 973 } 974 975 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 976 } 977 978 out: 979 zfs_close(zhp); 980 return (0); 981 } 982 983 static int 984 destroy_callback(zfs_handle_t *zhp, void *data) 985 { 986 destroy_cbdata_t *cb = data; 987 const char *name = zfs_get_name(zhp); 988 989 if (cb->cb_verbose) { 990 if (cb->cb_parsable) { 991 (void) printf("destroy\t%s\n", name); 992 } else if (cb->cb_dryrun) { 993 (void) printf(gettext("would destroy %s\n"), 994 name); 995 } else { 996 (void) printf(gettext("will destroy %s\n"), 997 name); 998 } 999 } 1000 1001 /* 1002 * Ignore pools (which we've already flagged as an error before getting 1003 * here). 1004 */ 1005 if (strchr(zfs_get_name(zhp), '/') == NULL && 1006 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 1007 zfs_close(zhp); 1008 return (0); 1009 } 1010 if (cb->cb_dryrun) { 1011 zfs_close(zhp); 1012 return (0); 1013 } 1014 1015 /* 1016 * We batch up all contiguous snapshots (even of different 1017 * filesystems) and destroy them with one ioctl. We can't 1018 * simply do all snap deletions and then all fs deletions, 1019 * because we must delete a clone before its origin. 1020 */ 1021 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) { 1022 fnvlist_add_boolean(cb->cb_batchedsnaps, name); 1023 } else { 1024 int error = zfs_destroy_snaps_nvl(g_zfs, 1025 cb->cb_batchedsnaps, B_FALSE); 1026 fnvlist_free(cb->cb_batchedsnaps); 1027 cb->cb_batchedsnaps = fnvlist_alloc(); 1028 1029 if (error != 0 || 1030 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 || 1031 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) { 1032 zfs_close(zhp); 1033 return (-1); 1034 } 1035 } 1036 1037 zfs_close(zhp); 1038 return (0); 1039 } 1040 1041 static int 1042 destroy_print_cb(zfs_handle_t *zhp, void *arg) 1043 { 1044 destroy_cbdata_t *cb = arg; 1045 const char *name = zfs_get_name(zhp); 1046 int err = 0; 1047 1048 if (nvlist_exists(cb->cb_nvl, name)) { 1049 if (cb->cb_firstsnap == NULL) 1050 cb->cb_firstsnap = strdup(name); 1051 if (cb->cb_prevsnap != NULL) 1052 free(cb->cb_prevsnap); 1053 /* this snap continues the current range */ 1054 cb->cb_prevsnap = strdup(name); 1055 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL) 1056 nomem(); 1057 if (cb->cb_verbose) { 1058 if (cb->cb_parsable) { 1059 (void) printf("destroy\t%s\n", name); 1060 } else if (cb->cb_dryrun) { 1061 (void) printf(gettext("would destroy %s\n"), 1062 name); 1063 } else { 1064 (void) printf(gettext("will destroy %s\n"), 1065 name); 1066 } 1067 } 1068 } else if (cb->cb_firstsnap != NULL) { 1069 /* end of this range */ 1070 uint64_t used = 0; 1071 err = lzc_snaprange_space(cb->cb_firstsnap, 1072 cb->cb_prevsnap, &used); 1073 cb->cb_snapused += used; 1074 free(cb->cb_firstsnap); 1075 cb->cb_firstsnap = NULL; 1076 free(cb->cb_prevsnap); 1077 cb->cb_prevsnap = NULL; 1078 } 1079 zfs_close(zhp); 1080 return (err); 1081 } 1082 1083 static int 1084 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb) 1085 { 1086 int err = 0; 1087 assert(cb->cb_firstsnap == NULL); 1088 assert(cb->cb_prevsnap == NULL); 1089 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb); 1090 if (cb->cb_firstsnap != NULL) { 1091 uint64_t used = 0; 1092 if (err == 0) { 1093 err = lzc_snaprange_space(cb->cb_firstsnap, 1094 cb->cb_prevsnap, &used); 1095 } 1096 cb->cb_snapused += used; 1097 free(cb->cb_firstsnap); 1098 cb->cb_firstsnap = NULL; 1099 free(cb->cb_prevsnap); 1100 cb->cb_prevsnap = NULL; 1101 } 1102 return (err); 1103 } 1104 1105 static int 1106 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg) 1107 { 1108 destroy_cbdata_t *cb = arg; 1109 int err = 0; 1110 1111 /* Check for clones. */ 1112 if (!cb->cb_doclones && !cb->cb_defer_destroy) { 1113 cb->cb_target = zhp; 1114 cb->cb_first = B_TRUE; 1115 err = zfs_iter_dependents(zhp, B_TRUE, 1116 destroy_check_dependent, cb); 1117 } 1118 1119 if (err == 0) { 1120 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp))) 1121 nomem(); 1122 } 1123 zfs_close(zhp); 1124 return (err); 1125 } 1126 1127 static int 1128 gather_snapshots(zfs_handle_t *zhp, void *arg) 1129 { 1130 destroy_cbdata_t *cb = arg; 1131 int err = 0; 1132 1133 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb); 1134 if (err == ENOENT) 1135 err = 0; 1136 if (err != 0) 1137 goto out; 1138 1139 if (cb->cb_verbose) { 1140 err = destroy_print_snapshots(zhp, cb); 1141 if (err != 0) 1142 goto out; 1143 } 1144 1145 if (cb->cb_recurse) 1146 err = zfs_iter_filesystems(zhp, gather_snapshots, cb); 1147 1148 out: 1149 zfs_close(zhp); 1150 return (err); 1151 } 1152 1153 static int 1154 destroy_clones(destroy_cbdata_t *cb) 1155 { 1156 nvpair_t *pair; 1157 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL); 1158 pair != NULL; 1159 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) { 1160 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair), 1161 ZFS_TYPE_SNAPSHOT); 1162 if (zhp != NULL) { 1163 boolean_t defer = cb->cb_defer_destroy; 1164 int err = 0; 1165 1166 /* 1167 * We can't defer destroy non-snapshots, so set it to 1168 * false while destroying the clones. 1169 */ 1170 cb->cb_defer_destroy = B_FALSE; 1171 err = zfs_iter_dependents(zhp, B_FALSE, 1172 destroy_callback, cb); 1173 cb->cb_defer_destroy = defer; 1174 zfs_close(zhp); 1175 if (err != 0) 1176 return (err); 1177 } 1178 } 1179 return (0); 1180 } 1181 1182 static int 1183 zfs_do_destroy(int argc, char **argv) 1184 { 1185 destroy_cbdata_t cb = { 0 }; 1186 int rv = 0; 1187 int err = 0; 1188 int c; 1189 zfs_handle_t *zhp = NULL; 1190 char *at, *pound; 1191 zfs_type_t type = ZFS_TYPE_DATASET; 1192 1193 /* check options */ 1194 while ((c = getopt(argc, argv, "vpndfrR")) != -1) { 1195 switch (c) { 1196 case 'v': 1197 cb.cb_verbose = B_TRUE; 1198 break; 1199 case 'p': 1200 cb.cb_verbose = B_TRUE; 1201 cb.cb_parsable = B_TRUE; 1202 break; 1203 case 'n': 1204 cb.cb_dryrun = B_TRUE; 1205 break; 1206 case 'd': 1207 cb.cb_defer_destroy = B_TRUE; 1208 type = ZFS_TYPE_SNAPSHOT; 1209 break; 1210 case 'f': 1211 cb.cb_force = B_TRUE; 1212 break; 1213 case 'r': 1214 cb.cb_recurse = B_TRUE; 1215 break; 1216 case 'R': 1217 cb.cb_recurse = B_TRUE; 1218 cb.cb_doclones = B_TRUE; 1219 break; 1220 case '?': 1221 default: 1222 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1223 optopt); 1224 usage(B_FALSE); 1225 } 1226 } 1227 1228 argc -= optind; 1229 argv += optind; 1230 1231 /* check number of arguments */ 1232 if (argc == 0) { 1233 (void) fprintf(stderr, gettext("missing dataset argument\n")); 1234 usage(B_FALSE); 1235 } 1236 if (argc > 1) { 1237 (void) fprintf(stderr, gettext("too many arguments\n")); 1238 usage(B_FALSE); 1239 } 1240 1241 at = strchr(argv[0], '@'); 1242 pound = strchr(argv[0], '#'); 1243 if (at != NULL) { 1244 1245 /* Build the list of snaps to destroy in cb_nvl. */ 1246 cb.cb_nvl = fnvlist_alloc(); 1247 1248 *at = '\0'; 1249 zhp = zfs_open(g_zfs, argv[0], 1250 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 1251 if (zhp == NULL) 1252 return (1); 1253 1254 cb.cb_snapspec = at + 1; 1255 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 || 1256 cb.cb_error) { 1257 rv = 1; 1258 goto out; 1259 } 1260 1261 if (nvlist_empty(cb.cb_nvl)) { 1262 (void) fprintf(stderr, gettext("could not find any " 1263 "snapshots to destroy; check snapshot names.\n")); 1264 rv = 1; 1265 goto out; 1266 } 1267 1268 if (cb.cb_verbose) { 1269 char buf[16]; 1270 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf)); 1271 if (cb.cb_parsable) { 1272 (void) printf("reclaim\t%llu\n", 1273 cb.cb_snapused); 1274 } else if (cb.cb_dryrun) { 1275 (void) printf(gettext("would reclaim %s\n"), 1276 buf); 1277 } else { 1278 (void) printf(gettext("will reclaim %s\n"), 1279 buf); 1280 } 1281 } 1282 1283 if (!cb.cb_dryrun) { 1284 if (cb.cb_doclones) { 1285 cb.cb_batchedsnaps = fnvlist_alloc(); 1286 err = destroy_clones(&cb); 1287 if (err == 0) { 1288 err = zfs_destroy_snaps_nvl(g_zfs, 1289 cb.cb_batchedsnaps, B_FALSE); 1290 } 1291 if (err != 0) { 1292 rv = 1; 1293 goto out; 1294 } 1295 } 1296 if (err == 0) { 1297 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl, 1298 cb.cb_defer_destroy); 1299 } 1300 } 1301 1302 if (err != 0) 1303 rv = 1; 1304 } else if (pound != NULL) { 1305 int err; 1306 nvlist_t *nvl; 1307 1308 if (cb.cb_dryrun) { 1309 (void) fprintf(stderr, 1310 "dryrun is not supported with bookmark\n"); 1311 return (-1); 1312 } 1313 1314 if (cb.cb_defer_destroy) { 1315 (void) fprintf(stderr, 1316 "defer destroy is not supported with bookmark\n"); 1317 return (-1); 1318 } 1319 1320 if (cb.cb_recurse) { 1321 (void) fprintf(stderr, 1322 "recursive is not supported with bookmark\n"); 1323 return (-1); 1324 } 1325 1326 if (!zfs_bookmark_exists(argv[0])) { 1327 (void) fprintf(stderr, gettext("bookmark '%s' " 1328 "does not exist.\n"), argv[0]); 1329 return (1); 1330 } 1331 1332 nvl = fnvlist_alloc(); 1333 fnvlist_add_boolean(nvl, argv[0]); 1334 1335 err = lzc_destroy_bookmarks(nvl, NULL); 1336 if (err != 0) { 1337 (void) zfs_standard_error(g_zfs, err, 1338 "cannot destroy bookmark"); 1339 } 1340 1341 nvlist_free(cb.cb_nvl); 1342 1343 return (err); 1344 } else { 1345 /* Open the given dataset */ 1346 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL) 1347 return (1); 1348 1349 cb.cb_target = zhp; 1350 1351 /* 1352 * Perform an explicit check for pools before going any further. 1353 */ 1354 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL && 1355 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 1356 (void) fprintf(stderr, gettext("cannot destroy '%s': " 1357 "operation does not apply to pools\n"), 1358 zfs_get_name(zhp)); 1359 (void) fprintf(stderr, gettext("use 'zfs destroy -r " 1360 "%s' to destroy all datasets in the pool\n"), 1361 zfs_get_name(zhp)); 1362 (void) fprintf(stderr, gettext("use 'zpool destroy %s' " 1363 "to destroy the pool itself\n"), zfs_get_name(zhp)); 1364 rv = 1; 1365 goto out; 1366 } 1367 1368 /* 1369 * Check for any dependents and/or clones. 1370 */ 1371 cb.cb_first = B_TRUE; 1372 if (!cb.cb_doclones && 1373 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent, 1374 &cb) != 0) { 1375 rv = 1; 1376 goto out; 1377 } 1378 1379 if (cb.cb_error) { 1380 rv = 1; 1381 goto out; 1382 } 1383 1384 cb.cb_batchedsnaps = fnvlist_alloc(); 1385 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback, 1386 &cb) != 0) { 1387 rv = 1; 1388 goto out; 1389 } 1390 1391 /* 1392 * Do the real thing. The callback will close the 1393 * handle regardless of whether it succeeds or not. 1394 */ 1395 err = destroy_callback(zhp, &cb); 1396 zhp = NULL; 1397 if (err == 0) { 1398 err = zfs_destroy_snaps_nvl(g_zfs, 1399 cb.cb_batchedsnaps, cb.cb_defer_destroy); 1400 } 1401 if (err != 0) 1402 rv = 1; 1403 } 1404 1405 out: 1406 fnvlist_free(cb.cb_batchedsnaps); 1407 fnvlist_free(cb.cb_nvl); 1408 if (zhp != NULL) 1409 zfs_close(zhp); 1410 return (rv); 1411 } 1412 1413 static boolean_t 1414 is_recvd_column(zprop_get_cbdata_t *cbp) 1415 { 1416 int i; 1417 zfs_get_column_t col; 1418 1419 for (i = 0; i < ZFS_GET_NCOLS && 1420 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++) 1421 if (col == GET_COL_RECVD) 1422 return (B_TRUE); 1423 return (B_FALSE); 1424 } 1425 1426 /* 1427 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...] 1428 * < all | property[,property]... > < fs | snap | vol > ... 1429 * 1430 * -r recurse over any child datasets 1431 * -H scripted mode. Headers are stripped, and fields are separated 1432 * by tabs instead of spaces. 1433 * -o Set of fields to display. One of "name,property,value, 1434 * received,source". Default is "name,property,value,source". 1435 * "all" is an alias for all five. 1436 * -s Set of sources to allow. One of 1437 * "local,default,inherited,received,temporary,none". Default is 1438 * all six. 1439 * -p Display values in parsable (literal) format. 1440 * 1441 * Prints properties for the given datasets. The user can control which 1442 * columns to display as well as which property types to allow. 1443 */ 1444 1445 /* 1446 * Invoked to display the properties for a single dataset. 1447 */ 1448 static int 1449 get_callback(zfs_handle_t *zhp, void *data) 1450 { 1451 char buf[ZFS_MAXPROPLEN]; 1452 char rbuf[ZFS_MAXPROPLEN]; 1453 zprop_source_t sourcetype; 1454 char source[ZFS_MAXNAMELEN]; 1455 zprop_get_cbdata_t *cbp = data; 1456 nvlist_t *user_props = zfs_get_user_props(zhp); 1457 zprop_list_t *pl = cbp->cb_proplist; 1458 nvlist_t *propval; 1459 char *strval; 1460 char *sourceval; 1461 boolean_t received = is_recvd_column(cbp); 1462 1463 for (; pl != NULL; pl = pl->pl_next) { 1464 char *recvdval = NULL; 1465 /* 1466 * Skip the special fake placeholder. This will also skip over 1467 * the name property when 'all' is specified. 1468 */ 1469 if (pl->pl_prop == ZFS_PROP_NAME && 1470 pl == cbp->cb_proplist) 1471 continue; 1472 1473 if (pl->pl_prop != ZPROP_INVAL) { 1474 if (zfs_prop_get(zhp, pl->pl_prop, buf, 1475 sizeof (buf), &sourcetype, source, 1476 sizeof (source), 1477 cbp->cb_literal) != 0) { 1478 if (pl->pl_all) 1479 continue; 1480 if (!zfs_prop_valid_for_type(pl->pl_prop, 1481 ZFS_TYPE_DATASET)) { 1482 (void) fprintf(stderr, 1483 gettext("No such property '%s'\n"), 1484 zfs_prop_to_name(pl->pl_prop)); 1485 continue; 1486 } 1487 sourcetype = ZPROP_SRC_NONE; 1488 (void) strlcpy(buf, "-", sizeof (buf)); 1489 } 1490 1491 if (received && (zfs_prop_get_recvd(zhp, 1492 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf), 1493 cbp->cb_literal) == 0)) 1494 recvdval = rbuf; 1495 1496 zprop_print_one_property(zfs_get_name(zhp), cbp, 1497 zfs_prop_to_name(pl->pl_prop), 1498 buf, sourcetype, source, recvdval); 1499 } else if (zfs_prop_userquota(pl->pl_user_prop)) { 1500 sourcetype = ZPROP_SRC_LOCAL; 1501 1502 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop, 1503 buf, sizeof (buf), cbp->cb_literal) != 0) { 1504 sourcetype = ZPROP_SRC_NONE; 1505 (void) strlcpy(buf, "-", sizeof (buf)); 1506 } 1507 1508 zprop_print_one_property(zfs_get_name(zhp), cbp, 1509 pl->pl_user_prop, buf, sourcetype, source, NULL); 1510 } else if (zfs_prop_written(pl->pl_user_prop)) { 1511 sourcetype = ZPROP_SRC_LOCAL; 1512 1513 if (zfs_prop_get_written(zhp, pl->pl_user_prop, 1514 buf, sizeof (buf), cbp->cb_literal) != 0) { 1515 sourcetype = ZPROP_SRC_NONE; 1516 (void) strlcpy(buf, "-", sizeof (buf)); 1517 } 1518 1519 zprop_print_one_property(zfs_get_name(zhp), cbp, 1520 pl->pl_user_prop, buf, sourcetype, source, NULL); 1521 } else { 1522 if (nvlist_lookup_nvlist(user_props, 1523 pl->pl_user_prop, &propval) != 0) { 1524 if (pl->pl_all) 1525 continue; 1526 sourcetype = ZPROP_SRC_NONE; 1527 strval = "-"; 1528 } else { 1529 verify(nvlist_lookup_string(propval, 1530 ZPROP_VALUE, &strval) == 0); 1531 verify(nvlist_lookup_string(propval, 1532 ZPROP_SOURCE, &sourceval) == 0); 1533 1534 if (strcmp(sourceval, 1535 zfs_get_name(zhp)) == 0) { 1536 sourcetype = ZPROP_SRC_LOCAL; 1537 } else if (strcmp(sourceval, 1538 ZPROP_SOURCE_VAL_RECVD) == 0) { 1539 sourcetype = ZPROP_SRC_RECEIVED; 1540 } else { 1541 sourcetype = ZPROP_SRC_INHERITED; 1542 (void) strlcpy(source, 1543 sourceval, sizeof (source)); 1544 } 1545 } 1546 1547 if (received && (zfs_prop_get_recvd(zhp, 1548 pl->pl_user_prop, rbuf, sizeof (rbuf), 1549 cbp->cb_literal) == 0)) 1550 recvdval = rbuf; 1551 1552 zprop_print_one_property(zfs_get_name(zhp), cbp, 1553 pl->pl_user_prop, strval, sourcetype, 1554 source, recvdval); 1555 } 1556 } 1557 1558 return (0); 1559 } 1560 1561 static int 1562 zfs_do_get(int argc, char **argv) 1563 { 1564 zprop_get_cbdata_t cb = { 0 }; 1565 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS; 1566 int types = ZFS_TYPE_DATASET; 1567 char *value, *fields; 1568 int ret = 0; 1569 int limit = 0; 1570 zprop_list_t fake_name = { 0 }; 1571 1572 /* 1573 * Set up default columns and sources. 1574 */ 1575 cb.cb_sources = ZPROP_SRC_ALL; 1576 cb.cb_columns[0] = GET_COL_NAME; 1577 cb.cb_columns[1] = GET_COL_PROPERTY; 1578 cb.cb_columns[2] = GET_COL_VALUE; 1579 cb.cb_columns[3] = GET_COL_SOURCE; 1580 cb.cb_type = ZFS_TYPE_DATASET; 1581 1582 /* check options */ 1583 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) { 1584 switch (c) { 1585 case 'p': 1586 cb.cb_literal = B_TRUE; 1587 break; 1588 case 'd': 1589 limit = parse_depth(optarg, &flags); 1590 break; 1591 case 'r': 1592 flags |= ZFS_ITER_RECURSE; 1593 break; 1594 case 'H': 1595 cb.cb_scripted = B_TRUE; 1596 break; 1597 case ':': 1598 (void) fprintf(stderr, gettext("missing argument for " 1599 "'%c' option\n"), optopt); 1600 usage(B_FALSE); 1601 break; 1602 case 'o': 1603 /* 1604 * Process the set of columns to display. We zero out 1605 * the structure to give us a blank slate. 1606 */ 1607 bzero(&cb.cb_columns, sizeof (cb.cb_columns)); 1608 i = 0; 1609 while (*optarg != '\0') { 1610 static char *col_subopts[] = 1611 { "name", "property", "value", "received", 1612 "source", "all", NULL }; 1613 1614 if (i == ZFS_GET_NCOLS) { 1615 (void) fprintf(stderr, gettext("too " 1616 "many fields given to -o " 1617 "option\n")); 1618 usage(B_FALSE); 1619 } 1620 1621 switch (getsubopt(&optarg, col_subopts, 1622 &value)) { 1623 case 0: 1624 cb.cb_columns[i++] = GET_COL_NAME; 1625 break; 1626 case 1: 1627 cb.cb_columns[i++] = GET_COL_PROPERTY; 1628 break; 1629 case 2: 1630 cb.cb_columns[i++] = GET_COL_VALUE; 1631 break; 1632 case 3: 1633 cb.cb_columns[i++] = GET_COL_RECVD; 1634 flags |= ZFS_ITER_RECVD_PROPS; 1635 break; 1636 case 4: 1637 cb.cb_columns[i++] = GET_COL_SOURCE; 1638 break; 1639 case 5: 1640 if (i > 0) { 1641 (void) fprintf(stderr, 1642 gettext("\"all\" conflicts " 1643 "with specific fields " 1644 "given to -o option\n")); 1645 usage(B_FALSE); 1646 } 1647 cb.cb_columns[0] = GET_COL_NAME; 1648 cb.cb_columns[1] = GET_COL_PROPERTY; 1649 cb.cb_columns[2] = GET_COL_VALUE; 1650 cb.cb_columns[3] = GET_COL_RECVD; 1651 cb.cb_columns[4] = GET_COL_SOURCE; 1652 flags |= ZFS_ITER_RECVD_PROPS; 1653 i = ZFS_GET_NCOLS; 1654 break; 1655 default: 1656 (void) fprintf(stderr, 1657 gettext("invalid column name " 1658 "'%s'\n"), value); 1659 usage(B_FALSE); 1660 } 1661 } 1662 break; 1663 1664 case 's': 1665 cb.cb_sources = 0; 1666 while (*optarg != '\0') { 1667 static char *source_subopts[] = { 1668 "local", "default", "inherited", 1669 "received", "temporary", "none", 1670 NULL }; 1671 1672 switch (getsubopt(&optarg, source_subopts, 1673 &value)) { 1674 case 0: 1675 cb.cb_sources |= ZPROP_SRC_LOCAL; 1676 break; 1677 case 1: 1678 cb.cb_sources |= ZPROP_SRC_DEFAULT; 1679 break; 1680 case 2: 1681 cb.cb_sources |= ZPROP_SRC_INHERITED; 1682 break; 1683 case 3: 1684 cb.cb_sources |= ZPROP_SRC_RECEIVED; 1685 break; 1686 case 4: 1687 cb.cb_sources |= ZPROP_SRC_TEMPORARY; 1688 break; 1689 case 5: 1690 cb.cb_sources |= ZPROP_SRC_NONE; 1691 break; 1692 default: 1693 (void) fprintf(stderr, 1694 gettext("invalid source " 1695 "'%s'\n"), value); 1696 usage(B_FALSE); 1697 } 1698 } 1699 break; 1700 1701 case 't': 1702 types = 0; 1703 flags &= ~ZFS_ITER_PROP_LISTSNAPS; 1704 while (*optarg != '\0') { 1705 static char *type_subopts[] = { "filesystem", 1706 "volume", "snapshot", "bookmark", 1707 "all", NULL }; 1708 1709 switch (getsubopt(&optarg, type_subopts, 1710 &value)) { 1711 case 0: 1712 types |= ZFS_TYPE_FILESYSTEM; 1713 break; 1714 case 1: 1715 types |= ZFS_TYPE_VOLUME; 1716 break; 1717 case 2: 1718 types |= ZFS_TYPE_SNAPSHOT; 1719 break; 1720 case 3: 1721 types |= ZFS_TYPE_BOOKMARK; 1722 break; 1723 case 4: 1724 types = ZFS_TYPE_DATASET | 1725 ZFS_TYPE_BOOKMARK; 1726 break; 1727 1728 default: 1729 (void) fprintf(stderr, 1730 gettext("invalid type '%s'\n"), 1731 value); 1732 usage(B_FALSE); 1733 } 1734 } 1735 break; 1736 1737 case '?': 1738 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1739 optopt); 1740 usage(B_FALSE); 1741 } 1742 } 1743 1744 argc -= optind; 1745 argv += optind; 1746 1747 if (argc < 1) { 1748 (void) fprintf(stderr, gettext("missing property " 1749 "argument\n")); 1750 usage(B_FALSE); 1751 } 1752 1753 fields = argv[0]; 1754 1755 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET) 1756 != 0) 1757 usage(B_FALSE); 1758 1759 argc--; 1760 argv++; 1761 1762 /* 1763 * As part of zfs_expand_proplist(), we keep track of the maximum column 1764 * width for each property. For the 'NAME' (and 'SOURCE') columns, we 1765 * need to know the maximum name length. However, the user likely did 1766 * not specify 'name' as one of the properties to fetch, so we need to 1767 * make sure we always include at least this property for 1768 * print_get_headers() to work properly. 1769 */ 1770 if (cb.cb_proplist != NULL) { 1771 fake_name.pl_prop = ZFS_PROP_NAME; 1772 fake_name.pl_width = strlen(gettext("NAME")); 1773 fake_name.pl_next = cb.cb_proplist; 1774 cb.cb_proplist = &fake_name; 1775 } 1776 1777 cb.cb_first = B_TRUE; 1778 1779 /* run for each object */ 1780 ret = zfs_for_each(argc, argv, flags, types, NULL, 1781 &cb.cb_proplist, limit, get_callback, &cb); 1782 1783 if (cb.cb_proplist == &fake_name) 1784 zprop_free_list(fake_name.pl_next); 1785 else 1786 zprop_free_list(cb.cb_proplist); 1787 1788 return (ret); 1789 } 1790 1791 /* 1792 * inherit [-rS] <property> <fs|vol> ... 1793 * 1794 * -r Recurse over all children 1795 * -S Revert to received value, if any 1796 * 1797 * For each dataset specified on the command line, inherit the given property 1798 * from its parent. Inheriting a property at the pool level will cause it to 1799 * use the default value. The '-r' flag will recurse over all children, and is 1800 * useful for setting a property on a hierarchy-wide basis, regardless of any 1801 * local modifications for each dataset. 1802 */ 1803 1804 typedef struct inherit_cbdata { 1805 const char *cb_propname; 1806 boolean_t cb_received; 1807 } inherit_cbdata_t; 1808 1809 static int 1810 inherit_recurse_cb(zfs_handle_t *zhp, void *data) 1811 { 1812 inherit_cbdata_t *cb = data; 1813 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname); 1814 1815 /* 1816 * If we're doing it recursively, then ignore properties that 1817 * are not valid for this type of dataset. 1818 */ 1819 if (prop != ZPROP_INVAL && 1820 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp))) 1821 return (0); 1822 1823 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0); 1824 } 1825 1826 static int 1827 inherit_cb(zfs_handle_t *zhp, void *data) 1828 { 1829 inherit_cbdata_t *cb = data; 1830 1831 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0); 1832 } 1833 1834 static int 1835 zfs_do_inherit(int argc, char **argv) 1836 { 1837 int c; 1838 zfs_prop_t prop; 1839 inherit_cbdata_t cb = { 0 }; 1840 char *propname; 1841 int ret = 0; 1842 int flags = 0; 1843 boolean_t received = B_FALSE; 1844 1845 /* check options */ 1846 while ((c = getopt(argc, argv, "rS")) != -1) { 1847 switch (c) { 1848 case 'r': 1849 flags |= ZFS_ITER_RECURSE; 1850 break; 1851 case 'S': 1852 received = B_TRUE; 1853 break; 1854 case '?': 1855 default: 1856 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1857 optopt); 1858 usage(B_FALSE); 1859 } 1860 } 1861 1862 argc -= optind; 1863 argv += optind; 1864 1865 /* check number of arguments */ 1866 if (argc < 1) { 1867 (void) fprintf(stderr, gettext("missing property argument\n")); 1868 usage(B_FALSE); 1869 } 1870 if (argc < 2) { 1871 (void) fprintf(stderr, gettext("missing dataset argument\n")); 1872 usage(B_FALSE); 1873 } 1874 1875 propname = argv[0]; 1876 argc--; 1877 argv++; 1878 1879 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) { 1880 if (zfs_prop_readonly(prop)) { 1881 (void) fprintf(stderr, gettext( 1882 "%s property is read-only\n"), 1883 propname); 1884 return (1); 1885 } 1886 if (!zfs_prop_inheritable(prop) && !received) { 1887 (void) fprintf(stderr, gettext("'%s' property cannot " 1888 "be inherited\n"), propname); 1889 if (prop == ZFS_PROP_QUOTA || 1890 prop == ZFS_PROP_RESERVATION || 1891 prop == ZFS_PROP_REFQUOTA || 1892 prop == ZFS_PROP_REFRESERVATION) 1893 (void) fprintf(stderr, gettext("use 'zfs set " 1894 "%s=none' to clear\n"), propname); 1895 return (1); 1896 } 1897 if (received && (prop == ZFS_PROP_VOLSIZE || 1898 prop == ZFS_PROP_VERSION)) { 1899 (void) fprintf(stderr, gettext("'%s' property cannot " 1900 "be reverted to a received value\n"), propname); 1901 return (1); 1902 } 1903 } else if (!zfs_prop_user(propname)) { 1904 (void) fprintf(stderr, gettext("invalid property '%s'\n"), 1905 propname); 1906 usage(B_FALSE); 1907 } 1908 1909 cb.cb_propname = propname; 1910 cb.cb_received = received; 1911 1912 if (flags & ZFS_ITER_RECURSE) { 1913 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, 1914 NULL, NULL, 0, inherit_recurse_cb, &cb); 1915 } else { 1916 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, 1917 NULL, NULL, 0, inherit_cb, &cb); 1918 } 1919 1920 return (ret); 1921 } 1922 1923 typedef struct upgrade_cbdata { 1924 uint64_t cb_numupgraded; 1925 uint64_t cb_numsamegraded; 1926 uint64_t cb_numfailed; 1927 uint64_t cb_version; 1928 boolean_t cb_newer; 1929 boolean_t cb_foundone; 1930 char cb_lastfs[ZFS_MAXNAMELEN]; 1931 } upgrade_cbdata_t; 1932 1933 static int 1934 same_pool(zfs_handle_t *zhp, const char *name) 1935 { 1936 int len1 = strcspn(name, "/@"); 1937 const char *zhname = zfs_get_name(zhp); 1938 int len2 = strcspn(zhname, "/@"); 1939 1940 if (len1 != len2) 1941 return (B_FALSE); 1942 return (strncmp(name, zhname, len1) == 0); 1943 } 1944 1945 static int 1946 upgrade_list_callback(zfs_handle_t *zhp, void *data) 1947 { 1948 upgrade_cbdata_t *cb = data; 1949 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 1950 1951 /* list if it's old/new */ 1952 if ((!cb->cb_newer && version < ZPL_VERSION) || 1953 (cb->cb_newer && version > ZPL_VERSION)) { 1954 char *str; 1955 if (cb->cb_newer) { 1956 str = gettext("The following filesystems are " 1957 "formatted using a newer software version and\n" 1958 "cannot be accessed on the current system.\n\n"); 1959 } else { 1960 str = gettext("The following filesystems are " 1961 "out of date, and can be upgraded. After being\n" 1962 "upgraded, these filesystems (and any 'zfs send' " 1963 "streams generated from\n" 1964 "subsequent snapshots) will no longer be " 1965 "accessible by older software versions.\n\n"); 1966 } 1967 1968 if (!cb->cb_foundone) { 1969 (void) puts(str); 1970 (void) printf(gettext("VER FILESYSTEM\n")); 1971 (void) printf(gettext("--- ------------\n")); 1972 cb->cb_foundone = B_TRUE; 1973 } 1974 1975 (void) printf("%2u %s\n", version, zfs_get_name(zhp)); 1976 } 1977 1978 return (0); 1979 } 1980 1981 static int 1982 upgrade_set_callback(zfs_handle_t *zhp, void *data) 1983 { 1984 upgrade_cbdata_t *cb = data; 1985 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 1986 int needed_spa_version; 1987 int spa_version; 1988 1989 if (zfs_spa_version(zhp, &spa_version) < 0) 1990 return (-1); 1991 1992 needed_spa_version = zfs_spa_version_map(cb->cb_version); 1993 1994 if (needed_spa_version < 0) 1995 return (-1); 1996 1997 if (spa_version < needed_spa_version) { 1998 /* can't upgrade */ 1999 (void) printf(gettext("%s: can not be " 2000 "upgraded; the pool version needs to first " 2001 "be upgraded\nto version %d\n\n"), 2002 zfs_get_name(zhp), needed_spa_version); 2003 cb->cb_numfailed++; 2004 return (0); 2005 } 2006 2007 /* upgrade */ 2008 if (version < cb->cb_version) { 2009 char verstr[16]; 2010 (void) snprintf(verstr, sizeof (verstr), 2011 "%llu", cb->cb_version); 2012 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) { 2013 /* 2014 * If they did "zfs upgrade -a", then we could 2015 * be doing ioctls to different pools. We need 2016 * to log this history once to each pool, and bypass 2017 * the normal history logging that happens in main(). 2018 */ 2019 (void) zpool_log_history(g_zfs, history_str); 2020 log_history = B_FALSE; 2021 } 2022 if (zfs_prop_set(zhp, "version", verstr) == 0) 2023 cb->cb_numupgraded++; 2024 else 2025 cb->cb_numfailed++; 2026 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp)); 2027 } else if (version > cb->cb_version) { 2028 /* can't downgrade */ 2029 (void) printf(gettext("%s: can not be downgraded; " 2030 "it is already at version %u\n"), 2031 zfs_get_name(zhp), version); 2032 cb->cb_numfailed++; 2033 } else { 2034 cb->cb_numsamegraded++; 2035 } 2036 return (0); 2037 } 2038 2039 /* 2040 * zfs upgrade 2041 * zfs upgrade -v 2042 * zfs upgrade [-r] [-V <version>] <-a | filesystem> 2043 */ 2044 static int 2045 zfs_do_upgrade(int argc, char **argv) 2046 { 2047 boolean_t all = B_FALSE; 2048 boolean_t showversions = B_FALSE; 2049 int ret = 0; 2050 upgrade_cbdata_t cb = { 0 }; 2051 char c; 2052 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS; 2053 2054 /* check options */ 2055 while ((c = getopt(argc, argv, "rvV:a")) != -1) { 2056 switch (c) { 2057 case 'r': 2058 flags |= ZFS_ITER_RECURSE; 2059 break; 2060 case 'v': 2061 showversions = B_TRUE; 2062 break; 2063 case 'V': 2064 if (zfs_prop_string_to_index(ZFS_PROP_VERSION, 2065 optarg, &cb.cb_version) != 0) { 2066 (void) fprintf(stderr, 2067 gettext("invalid version %s\n"), optarg); 2068 usage(B_FALSE); 2069 } 2070 break; 2071 case 'a': 2072 all = B_TRUE; 2073 break; 2074 case '?': 2075 default: 2076 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2077 optopt); 2078 usage(B_FALSE); 2079 } 2080 } 2081 2082 argc -= optind; 2083 argv += optind; 2084 2085 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version)) 2086 usage(B_FALSE); 2087 if (showversions && (flags & ZFS_ITER_RECURSE || all || 2088 cb.cb_version || argc)) 2089 usage(B_FALSE); 2090 if ((all || argc) && (showversions)) 2091 usage(B_FALSE); 2092 if (all && argc) 2093 usage(B_FALSE); 2094 2095 if (showversions) { 2096 /* Show info on available versions. */ 2097 (void) printf(gettext("The following filesystem versions are " 2098 "supported:\n\n")); 2099 (void) printf(gettext("VER DESCRIPTION\n")); 2100 (void) printf("--- -----------------------------------------" 2101 "---------------\n"); 2102 (void) printf(gettext(" 1 Initial ZFS filesystem version\n")); 2103 (void) printf(gettext(" 2 Enhanced directory entries\n")); 2104 (void) printf(gettext(" 3 Case insensitive and filesystem " 2105 "user identifier (FUID)\n")); 2106 (void) printf(gettext(" 4 userquota, groupquota " 2107 "properties\n")); 2108 (void) printf(gettext(" 5 System attributes\n")); 2109 (void) printf(gettext("\nFor more information on a particular " 2110 "version, including supported releases,\n")); 2111 (void) printf("see the ZFS Administration Guide.\n\n"); 2112 ret = 0; 2113 } else if (argc || all) { 2114 /* Upgrade filesystems */ 2115 if (cb.cb_version == 0) 2116 cb.cb_version = ZPL_VERSION; 2117 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM, 2118 NULL, NULL, 0, upgrade_set_callback, &cb); 2119 (void) printf(gettext("%llu filesystems upgraded\n"), 2120 cb.cb_numupgraded); 2121 if (cb.cb_numsamegraded) { 2122 (void) printf(gettext("%llu filesystems already at " 2123 "this version\n"), 2124 cb.cb_numsamegraded); 2125 } 2126 if (cb.cb_numfailed != 0) 2127 ret = 1; 2128 } else { 2129 /* List old-version filesytems */ 2130 boolean_t found; 2131 (void) printf(gettext("This system is currently running " 2132 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION); 2133 2134 flags |= ZFS_ITER_RECURSE; 2135 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM, 2136 NULL, NULL, 0, upgrade_list_callback, &cb); 2137 2138 found = cb.cb_foundone; 2139 cb.cb_foundone = B_FALSE; 2140 cb.cb_newer = B_TRUE; 2141 2142 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM, 2143 NULL, NULL, 0, upgrade_list_callback, &cb); 2144 2145 if (!cb.cb_foundone && !found) { 2146 (void) printf(gettext("All filesystems are " 2147 "formatted with the current version.\n")); 2148 } 2149 } 2150 2151 return (ret); 2152 } 2153 2154 /* 2155 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...] 2156 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot 2157 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...] 2158 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot 2159 * 2160 * -H Scripted mode; elide headers and separate columns by tabs. 2161 * -i Translate SID to POSIX ID. 2162 * -n Print numeric ID instead of user/group name. 2163 * -o Control which fields to display. 2164 * -p Use exact (parsable) numeric output. 2165 * -s Specify sort columns, descending order. 2166 * -S Specify sort columns, ascending order. 2167 * -t Control which object types to display. 2168 * 2169 * Displays space consumed by, and quotas on, each user in the specified 2170 * filesystem or snapshot. 2171 */ 2172 2173 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */ 2174 enum us_field_types { 2175 USFIELD_TYPE, 2176 USFIELD_NAME, 2177 USFIELD_USED, 2178 USFIELD_QUOTA 2179 }; 2180 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" }; 2181 static char *us_field_names[] = { "type", "name", "used", "quota" }; 2182 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *)) 2183 2184 #define USTYPE_PSX_GRP (1 << 0) 2185 #define USTYPE_PSX_USR (1 << 1) 2186 #define USTYPE_SMB_GRP (1 << 2) 2187 #define USTYPE_SMB_USR (1 << 3) 2188 #define USTYPE_ALL \ 2189 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR) 2190 2191 static int us_type_bits[] = { 2192 USTYPE_PSX_GRP, 2193 USTYPE_PSX_USR, 2194 USTYPE_SMB_GRP, 2195 USTYPE_SMB_USR, 2196 USTYPE_ALL 2197 }; 2198 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup", 2199 "smbuser", "all" }; 2200 2201 typedef struct us_node { 2202 nvlist_t *usn_nvl; 2203 uu_avl_node_t usn_avlnode; 2204 uu_list_node_t usn_listnode; 2205 } us_node_t; 2206 2207 typedef struct us_cbdata { 2208 nvlist_t **cb_nvlp; 2209 uu_avl_pool_t *cb_avl_pool; 2210 uu_avl_t *cb_avl; 2211 boolean_t cb_numname; 2212 boolean_t cb_nicenum; 2213 boolean_t cb_sid2posix; 2214 zfs_userquota_prop_t cb_prop; 2215 zfs_sort_column_t *cb_sortcol; 2216 size_t cb_width[USFIELD_LAST]; 2217 } us_cbdata_t; 2218 2219 static boolean_t us_populated = B_FALSE; 2220 2221 typedef struct { 2222 zfs_sort_column_t *si_sortcol; 2223 boolean_t si_numname; 2224 } us_sort_info_t; 2225 2226 static int 2227 us_field_index(char *field) 2228 { 2229 int i; 2230 2231 for (i = 0; i < USFIELD_LAST; i++) { 2232 if (strcmp(field, us_field_names[i]) == 0) 2233 return (i); 2234 } 2235 2236 return (-1); 2237 } 2238 2239 static int 2240 us_compare(const void *larg, const void *rarg, void *unused) 2241 { 2242 const us_node_t *l = larg; 2243 const us_node_t *r = rarg; 2244 us_sort_info_t *si = (us_sort_info_t *)unused; 2245 zfs_sort_column_t *sortcol = si->si_sortcol; 2246 boolean_t numname = si->si_numname; 2247 nvlist_t *lnvl = l->usn_nvl; 2248 nvlist_t *rnvl = r->usn_nvl; 2249 int rc = 0; 2250 boolean_t lvb, rvb; 2251 2252 for (; sortcol != NULL; sortcol = sortcol->sc_next) { 2253 char *lvstr = ""; 2254 char *rvstr = ""; 2255 uint32_t lv32 = 0; 2256 uint32_t rv32 = 0; 2257 uint64_t lv64 = 0; 2258 uint64_t rv64 = 0; 2259 zfs_prop_t prop = sortcol->sc_prop; 2260 const char *propname = NULL; 2261 boolean_t reverse = sortcol->sc_reverse; 2262 2263 switch (prop) { 2264 case ZFS_PROP_TYPE: 2265 propname = "type"; 2266 (void) nvlist_lookup_uint32(lnvl, propname, &lv32); 2267 (void) nvlist_lookup_uint32(rnvl, propname, &rv32); 2268 if (rv32 != lv32) 2269 rc = (rv32 < lv32) ? 1 : -1; 2270 break; 2271 case ZFS_PROP_NAME: 2272 propname = "name"; 2273 if (numname) { 2274 (void) nvlist_lookup_uint64(lnvl, propname, 2275 &lv64); 2276 (void) nvlist_lookup_uint64(rnvl, propname, 2277 &rv64); 2278 if (rv64 != lv64) 2279 rc = (rv64 < lv64) ? 1 : -1; 2280 } else { 2281 (void) nvlist_lookup_string(lnvl, propname, 2282 &lvstr); 2283 (void) nvlist_lookup_string(rnvl, propname, 2284 &rvstr); 2285 rc = strcmp(lvstr, rvstr); 2286 } 2287 break; 2288 case ZFS_PROP_USED: 2289 case ZFS_PROP_QUOTA: 2290 if (!us_populated) 2291 break; 2292 if (prop == ZFS_PROP_USED) 2293 propname = "used"; 2294 else 2295 propname = "quota"; 2296 (void) nvlist_lookup_uint64(lnvl, propname, &lv64); 2297 (void) nvlist_lookup_uint64(rnvl, propname, &rv64); 2298 if (rv64 != lv64) 2299 rc = (rv64 < lv64) ? 1 : -1; 2300 break; 2301 } 2302 2303 if (rc != 0) { 2304 if (rc < 0) 2305 return (reverse ? 1 : -1); 2306 else 2307 return (reverse ? -1 : 1); 2308 } 2309 } 2310 2311 /* 2312 * If entries still seem to be the same, check if they are of the same 2313 * type (smbentity is added only if we are doing SID to POSIX ID 2314 * translation where we can have duplicate type/name combinations). 2315 */ 2316 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 && 2317 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 && 2318 lvb != rvb) 2319 return (lvb < rvb ? -1 : 1); 2320 2321 return (0); 2322 } 2323 2324 static inline const char * 2325 us_type2str(unsigned field_type) 2326 { 2327 switch (field_type) { 2328 case USTYPE_PSX_USR: 2329 return ("POSIX User"); 2330 case USTYPE_PSX_GRP: 2331 return ("POSIX Group"); 2332 case USTYPE_SMB_USR: 2333 return ("SMB User"); 2334 case USTYPE_SMB_GRP: 2335 return ("SMB Group"); 2336 default: 2337 return ("Undefined"); 2338 } 2339 } 2340 2341 static int 2342 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space) 2343 { 2344 us_cbdata_t *cb = (us_cbdata_t *)arg; 2345 zfs_userquota_prop_t prop = cb->cb_prop; 2346 char *name = NULL; 2347 char *propname; 2348 char sizebuf[32]; 2349 us_node_t *node; 2350 uu_avl_pool_t *avl_pool = cb->cb_avl_pool; 2351 uu_avl_t *avl = cb->cb_avl; 2352 uu_avl_index_t idx; 2353 nvlist_t *props; 2354 us_node_t *n; 2355 zfs_sort_column_t *sortcol = cb->cb_sortcol; 2356 unsigned type; 2357 const char *typestr; 2358 size_t namelen; 2359 size_t typelen; 2360 size_t sizelen; 2361 int typeidx, nameidx, sizeidx; 2362 us_sort_info_t sortinfo = { sortcol, cb->cb_numname }; 2363 boolean_t smbentity = B_FALSE; 2364 2365 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 2366 nomem(); 2367 node = safe_malloc(sizeof (us_node_t)); 2368 uu_avl_node_init(node, &node->usn_avlnode, avl_pool); 2369 node->usn_nvl = props; 2370 2371 if (domain != NULL && domain[0] != '\0') { 2372 /* SMB */ 2373 char sid[ZFS_MAXNAMELEN + 32]; 2374 uid_t id; 2375 uint64_t classes; 2376 int err; 2377 directory_error_t e; 2378 2379 smbentity = B_TRUE; 2380 2381 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid); 2382 2383 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) { 2384 type = USTYPE_SMB_GRP; 2385 err = sid_to_id(sid, B_FALSE, &id); 2386 } else { 2387 type = USTYPE_SMB_USR; 2388 err = sid_to_id(sid, B_TRUE, &id); 2389 } 2390 2391 if (err == 0) { 2392 rid = id; 2393 if (!cb->cb_sid2posix) { 2394 e = directory_name_from_sid(NULL, sid, &name, 2395 &classes); 2396 if (e != NULL) 2397 directory_error_free(e); 2398 if (name == NULL) 2399 name = sid; 2400 } 2401 } 2402 } 2403 2404 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') { 2405 /* POSIX or -i */ 2406 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) { 2407 type = USTYPE_PSX_GRP; 2408 if (!cb->cb_numname) { 2409 struct group *g; 2410 2411 if ((g = getgrgid(rid)) != NULL) 2412 name = g->gr_name; 2413 } 2414 } else { 2415 type = USTYPE_PSX_USR; 2416 if (!cb->cb_numname) { 2417 struct passwd *p; 2418 2419 if ((p = getpwuid(rid)) != NULL) 2420 name = p->pw_name; 2421 } 2422 } 2423 } 2424 2425 /* 2426 * Make sure that the type/name combination is unique when doing 2427 * SID to POSIX ID translation (hence changing the type from SMB to 2428 * POSIX). 2429 */ 2430 if (cb->cb_sid2posix && 2431 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0) 2432 nomem(); 2433 2434 /* Calculate/update width of TYPE field */ 2435 typestr = us_type2str(type); 2436 typelen = strlen(gettext(typestr)); 2437 typeidx = us_field_index("type"); 2438 if (typelen > cb->cb_width[typeidx]) 2439 cb->cb_width[typeidx] = typelen; 2440 if (nvlist_add_uint32(props, "type", type) != 0) 2441 nomem(); 2442 2443 /* Calculate/update width of NAME field */ 2444 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) { 2445 if (nvlist_add_uint64(props, "name", rid) != 0) 2446 nomem(); 2447 namelen = snprintf(NULL, 0, "%u", rid); 2448 } else { 2449 if (nvlist_add_string(props, "name", name) != 0) 2450 nomem(); 2451 namelen = strlen(name); 2452 } 2453 nameidx = us_field_index("name"); 2454 if (namelen > cb->cb_width[nameidx]) 2455 cb->cb_width[nameidx] = namelen; 2456 2457 /* 2458 * Check if this type/name combination is in the list and update it; 2459 * otherwise add new node to the list. 2460 */ 2461 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) { 2462 uu_avl_insert(avl, node, idx); 2463 } else { 2464 nvlist_free(props); 2465 free(node); 2466 node = n; 2467 props = node->usn_nvl; 2468 } 2469 2470 /* Calculate/update width of USED/QUOTA fields */ 2471 if (cb->cb_nicenum) 2472 zfs_nicenum(space, sizebuf, sizeof (sizebuf)); 2473 else 2474 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space); 2475 sizelen = strlen(sizebuf); 2476 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) { 2477 propname = "used"; 2478 if (!nvlist_exists(props, "quota")) 2479 (void) nvlist_add_uint64(props, "quota", 0); 2480 } else { 2481 propname = "quota"; 2482 if (!nvlist_exists(props, "used")) 2483 (void) nvlist_add_uint64(props, "used", 0); 2484 } 2485 sizeidx = us_field_index(propname); 2486 if (sizelen > cb->cb_width[sizeidx]) 2487 cb->cb_width[sizeidx] = sizelen; 2488 2489 if (nvlist_add_uint64(props, propname, space) != 0) 2490 nomem(); 2491 2492 return (0); 2493 } 2494 2495 static void 2496 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types, 2497 size_t *width, us_node_t *node) 2498 { 2499 nvlist_t *nvl = node->usn_nvl; 2500 char valstr[ZFS_MAXNAMELEN]; 2501 boolean_t first = B_TRUE; 2502 int cfield = 0; 2503 int field; 2504 uint32_t ustype; 2505 2506 /* Check type */ 2507 (void) nvlist_lookup_uint32(nvl, "type", &ustype); 2508 if (!(ustype & types)) 2509 return; 2510 2511 while ((field = fields[cfield]) != USFIELD_LAST) { 2512 nvpair_t *nvp = NULL; 2513 data_type_t type; 2514 uint32_t val32; 2515 uint64_t val64; 2516 char *strval = NULL; 2517 2518 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 2519 if (strcmp(nvpair_name(nvp), 2520 us_field_names[field]) == 0) 2521 break; 2522 } 2523 2524 type = nvpair_type(nvp); 2525 switch (type) { 2526 case DATA_TYPE_UINT32: 2527 (void) nvpair_value_uint32(nvp, &val32); 2528 break; 2529 case DATA_TYPE_UINT64: 2530 (void) nvpair_value_uint64(nvp, &val64); 2531 break; 2532 case DATA_TYPE_STRING: 2533 (void) nvpair_value_string(nvp, &strval); 2534 break; 2535 default: 2536 (void) fprintf(stderr, "invalid data type\n"); 2537 } 2538 2539 switch (field) { 2540 case USFIELD_TYPE: 2541 strval = (char *)us_type2str(val32); 2542 break; 2543 case USFIELD_NAME: 2544 if (type == DATA_TYPE_UINT64) { 2545 (void) sprintf(valstr, "%llu", val64); 2546 strval = valstr; 2547 } 2548 break; 2549 case USFIELD_USED: 2550 case USFIELD_QUOTA: 2551 if (type == DATA_TYPE_UINT64) { 2552 if (parsable) { 2553 (void) sprintf(valstr, "%llu", val64); 2554 } else { 2555 zfs_nicenum(val64, valstr, 2556 sizeof (valstr)); 2557 } 2558 if (field == USFIELD_QUOTA && 2559 strcmp(valstr, "0") == 0) 2560 strval = "none"; 2561 else 2562 strval = valstr; 2563 } 2564 break; 2565 } 2566 2567 if (!first) { 2568 if (scripted) 2569 (void) printf("\t"); 2570 else 2571 (void) printf(" "); 2572 } 2573 if (scripted) 2574 (void) printf("%s", strval); 2575 else if (field == USFIELD_TYPE || field == USFIELD_NAME) 2576 (void) printf("%-*s", width[field], strval); 2577 else 2578 (void) printf("%*s", width[field], strval); 2579 2580 first = B_FALSE; 2581 cfield++; 2582 } 2583 2584 (void) printf("\n"); 2585 } 2586 2587 static void 2588 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types, 2589 size_t *width, boolean_t rmnode, uu_avl_t *avl) 2590 { 2591 us_node_t *node; 2592 const char *col; 2593 int cfield = 0; 2594 int field; 2595 2596 if (!scripted) { 2597 boolean_t first = B_TRUE; 2598 2599 while ((field = fields[cfield]) != USFIELD_LAST) { 2600 col = gettext(us_field_hdr[field]); 2601 if (field == USFIELD_TYPE || field == USFIELD_NAME) { 2602 (void) printf(first ? "%-*s" : " %-*s", 2603 width[field], col); 2604 } else { 2605 (void) printf(first ? "%*s" : " %*s", 2606 width[field], col); 2607 } 2608 first = B_FALSE; 2609 cfield++; 2610 } 2611 (void) printf("\n"); 2612 } 2613 2614 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) { 2615 print_us_node(scripted, parsable, fields, types, width, node); 2616 if (rmnode) 2617 nvlist_free(node->usn_nvl); 2618 } 2619 } 2620 2621 static int 2622 zfs_do_userspace(int argc, char **argv) 2623 { 2624 zfs_handle_t *zhp; 2625 zfs_userquota_prop_t p; 2626 uu_avl_pool_t *avl_pool; 2627 uu_avl_t *avl_tree; 2628 uu_avl_walk_t *walk; 2629 char *delim; 2630 char deffields[] = "type,name,used,quota"; 2631 char *ofield = NULL; 2632 char *tfield = NULL; 2633 int cfield = 0; 2634 int fields[256]; 2635 int i; 2636 boolean_t scripted = B_FALSE; 2637 boolean_t prtnum = B_FALSE; 2638 boolean_t parsable = B_FALSE; 2639 boolean_t sid2posix = B_FALSE; 2640 int ret = 0; 2641 int c; 2642 zfs_sort_column_t *sortcol = NULL; 2643 int types = USTYPE_PSX_USR | USTYPE_SMB_USR; 2644 us_cbdata_t cb; 2645 us_node_t *node; 2646 us_node_t *rmnode; 2647 uu_list_pool_t *listpool; 2648 uu_list_t *list; 2649 uu_avl_index_t idx = 0; 2650 uu_list_index_t idx2 = 0; 2651 2652 if (argc < 2) 2653 usage(B_FALSE); 2654 2655 if (strcmp(argv[0], "groupspace") == 0) 2656 /* Toggle default group types */ 2657 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP; 2658 2659 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) { 2660 switch (c) { 2661 case 'n': 2662 prtnum = B_TRUE; 2663 break; 2664 case 'H': 2665 scripted = B_TRUE; 2666 break; 2667 case 'p': 2668 parsable = B_TRUE; 2669 break; 2670 case 'o': 2671 ofield = optarg; 2672 break; 2673 case 's': 2674 case 'S': 2675 if (zfs_add_sort_column(&sortcol, optarg, 2676 c == 's' ? B_FALSE : B_TRUE) != 0) { 2677 (void) fprintf(stderr, 2678 gettext("invalid field '%s'\n"), optarg); 2679 usage(B_FALSE); 2680 } 2681 break; 2682 case 't': 2683 tfield = optarg; 2684 break; 2685 case 'i': 2686 sid2posix = B_TRUE; 2687 break; 2688 case ':': 2689 (void) fprintf(stderr, gettext("missing argument for " 2690 "'%c' option\n"), optopt); 2691 usage(B_FALSE); 2692 break; 2693 case '?': 2694 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2695 optopt); 2696 usage(B_FALSE); 2697 } 2698 } 2699 2700 argc -= optind; 2701 argv += optind; 2702 2703 if (argc < 1) { 2704 (void) fprintf(stderr, gettext("missing dataset name\n")); 2705 usage(B_FALSE); 2706 } 2707 if (argc > 1) { 2708 (void) fprintf(stderr, gettext("too many arguments\n")); 2709 usage(B_FALSE); 2710 } 2711 2712 /* Use default output fields if not specified using -o */ 2713 if (ofield == NULL) 2714 ofield = deffields; 2715 do { 2716 if ((delim = strchr(ofield, ',')) != NULL) 2717 *delim = '\0'; 2718 if ((fields[cfield++] = us_field_index(ofield)) == -1) { 2719 (void) fprintf(stderr, gettext("invalid type '%s' " 2720 "for -o option\n"), ofield); 2721 return (-1); 2722 } 2723 if (delim != NULL) 2724 ofield = delim + 1; 2725 } while (delim != NULL); 2726 fields[cfield] = USFIELD_LAST; 2727 2728 /* Override output types (-t option) */ 2729 if (tfield != NULL) { 2730 types = 0; 2731 2732 do { 2733 boolean_t found = B_FALSE; 2734 2735 if ((delim = strchr(tfield, ',')) != NULL) 2736 *delim = '\0'; 2737 for (i = 0; i < sizeof (us_type_bits) / sizeof (int); 2738 i++) { 2739 if (strcmp(tfield, us_type_names[i]) == 0) { 2740 found = B_TRUE; 2741 types |= us_type_bits[i]; 2742 break; 2743 } 2744 } 2745 if (!found) { 2746 (void) fprintf(stderr, gettext("invalid type " 2747 "'%s' for -t option\n"), tfield); 2748 return (-1); 2749 } 2750 if (delim != NULL) 2751 tfield = delim + 1; 2752 } while (delim != NULL); 2753 } 2754 2755 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL) 2756 return (1); 2757 2758 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t), 2759 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL) 2760 nomem(); 2761 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL) 2762 nomem(); 2763 2764 /* Always add default sorting columns */ 2765 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE); 2766 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE); 2767 2768 cb.cb_sortcol = sortcol; 2769 cb.cb_numname = prtnum; 2770 cb.cb_nicenum = !parsable; 2771 cb.cb_avl_pool = avl_pool; 2772 cb.cb_avl = avl_tree; 2773 cb.cb_sid2posix = sid2posix; 2774 2775 for (i = 0; i < USFIELD_LAST; i++) 2776 cb.cb_width[i] = strlen(gettext(us_field_hdr[i])); 2777 2778 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) { 2779 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) && 2780 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) || 2781 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) && 2782 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP)))) 2783 continue; 2784 cb.cb_prop = p; 2785 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0) 2786 return (ret); 2787 } 2788 2789 /* Sort the list */ 2790 if ((node = uu_avl_first(avl_tree)) == NULL) 2791 return (0); 2792 2793 us_populated = B_TRUE; 2794 2795 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t), 2796 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT); 2797 list = uu_list_create(listpool, NULL, UU_DEFAULT); 2798 uu_list_node_init(node, &node->usn_listnode, listpool); 2799 2800 while (node != NULL) { 2801 rmnode = node; 2802 node = uu_avl_next(avl_tree, node); 2803 uu_avl_remove(avl_tree, rmnode); 2804 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL) 2805 uu_list_insert(list, rmnode, idx2); 2806 } 2807 2808 for (node = uu_list_first(list); node != NULL; 2809 node = uu_list_next(list, node)) { 2810 us_sort_info_t sortinfo = { sortcol, cb.cb_numname }; 2811 2812 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL) 2813 uu_avl_insert(avl_tree, node, idx); 2814 } 2815 2816 uu_list_destroy(list); 2817 uu_list_pool_destroy(listpool); 2818 2819 /* Print and free node nvlist memory */ 2820 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE, 2821 cb.cb_avl); 2822 2823 zfs_free_sort_columns(sortcol); 2824 2825 /* Clean up the AVL tree */ 2826 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL) 2827 nomem(); 2828 2829 while ((node = uu_avl_walk_next(walk)) != NULL) { 2830 uu_avl_remove(cb.cb_avl, node); 2831 free(node); 2832 } 2833 2834 uu_avl_walk_end(walk); 2835 uu_avl_destroy(avl_tree); 2836 uu_avl_pool_destroy(avl_pool); 2837 2838 return (ret); 2839 } 2840 2841 /* 2842 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ... 2843 * [-t type[,...]] [filesystem|volume|snapshot] ... 2844 * 2845 * -H Scripted mode; elide headers and separate columns by tabs. 2846 * -p Display values in parsable (literal) format. 2847 * -r Recurse over all children. 2848 * -d Limit recursion by depth. 2849 * -o Control which fields to display. 2850 * -s Specify sort columns, descending order. 2851 * -S Specify sort columns, ascending order. 2852 * -t Control which object types to display. 2853 * 2854 * When given no arguments, list all filesystems in the system. 2855 * Otherwise, list the specified datasets, optionally recursing down them if 2856 * '-r' is specified. 2857 */ 2858 typedef struct list_cbdata { 2859 boolean_t cb_first; 2860 boolean_t cb_literal; 2861 boolean_t cb_scripted; 2862 zprop_list_t *cb_proplist; 2863 } list_cbdata_t; 2864 2865 /* 2866 * Given a list of columns to display, output appropriate headers for each one. 2867 */ 2868 static void 2869 print_header(list_cbdata_t *cb) 2870 { 2871 zprop_list_t *pl = cb->cb_proplist; 2872 char headerbuf[ZFS_MAXPROPLEN]; 2873 const char *header; 2874 int i; 2875 boolean_t first = B_TRUE; 2876 boolean_t right_justify; 2877 2878 for (; pl != NULL; pl = pl->pl_next) { 2879 if (!first) { 2880 (void) printf(" "); 2881 } else { 2882 first = B_FALSE; 2883 } 2884 2885 right_justify = B_FALSE; 2886 if (pl->pl_prop != ZPROP_INVAL) { 2887 header = zfs_prop_column_name(pl->pl_prop); 2888 right_justify = zfs_prop_align_right(pl->pl_prop); 2889 } else { 2890 for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 2891 headerbuf[i] = toupper(pl->pl_user_prop[i]); 2892 headerbuf[i] = '\0'; 2893 header = headerbuf; 2894 } 2895 2896 if (pl->pl_next == NULL && !right_justify) 2897 (void) printf("%s", header); 2898 else if (right_justify) 2899 (void) printf("%*s", pl->pl_width, header); 2900 else 2901 (void) printf("%-*s", pl->pl_width, header); 2902 } 2903 2904 (void) printf("\n"); 2905 } 2906 2907 /* 2908 * Given a dataset and a list of fields, print out all the properties according 2909 * to the described layout. 2910 */ 2911 static void 2912 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb) 2913 { 2914 zprop_list_t *pl = cb->cb_proplist; 2915 boolean_t first = B_TRUE; 2916 char property[ZFS_MAXPROPLEN]; 2917 nvlist_t *userprops = zfs_get_user_props(zhp); 2918 nvlist_t *propval; 2919 char *propstr; 2920 boolean_t right_justify; 2921 2922 for (; pl != NULL; pl = pl->pl_next) { 2923 if (!first) { 2924 if (cb->cb_scripted) 2925 (void) printf("\t"); 2926 else 2927 (void) printf(" "); 2928 } else { 2929 first = B_FALSE; 2930 } 2931 2932 if (pl->pl_prop != ZPROP_INVAL) { 2933 if (zfs_prop_get(zhp, pl->pl_prop, property, 2934 sizeof (property), NULL, NULL, 0, 2935 cb->cb_literal) != 0) 2936 propstr = "-"; 2937 else 2938 propstr = property; 2939 right_justify = zfs_prop_align_right(pl->pl_prop); 2940 } else if (zfs_prop_userquota(pl->pl_user_prop)) { 2941 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop, 2942 property, sizeof (property), cb->cb_literal) != 0) 2943 propstr = "-"; 2944 else 2945 propstr = property; 2946 right_justify = B_TRUE; 2947 } else if (zfs_prop_written(pl->pl_user_prop)) { 2948 if (zfs_prop_get_written(zhp, pl->pl_user_prop, 2949 property, sizeof (property), cb->cb_literal) != 0) 2950 propstr = "-"; 2951 else 2952 propstr = property; 2953 right_justify = B_TRUE; 2954 } else { 2955 if (nvlist_lookup_nvlist(userprops, 2956 pl->pl_user_prop, &propval) != 0) 2957 propstr = "-"; 2958 else 2959 verify(nvlist_lookup_string(propval, 2960 ZPROP_VALUE, &propstr) == 0); 2961 right_justify = B_FALSE; 2962 } 2963 2964 /* 2965 * If this is being called in scripted mode, or if this is the 2966 * last column and it is left-justified, don't include a width 2967 * format specifier. 2968 */ 2969 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify)) 2970 (void) printf("%s", propstr); 2971 else if (right_justify) 2972 (void) printf("%*s", pl->pl_width, propstr); 2973 else 2974 (void) printf("%-*s", pl->pl_width, propstr); 2975 } 2976 2977 (void) printf("\n"); 2978 } 2979 2980 /* 2981 * Generic callback function to list a dataset or snapshot. 2982 */ 2983 static int 2984 list_callback(zfs_handle_t *zhp, void *data) 2985 { 2986 list_cbdata_t *cbp = data; 2987 2988 if (cbp->cb_first) { 2989 if (!cbp->cb_scripted) 2990 print_header(cbp); 2991 cbp->cb_first = B_FALSE; 2992 } 2993 2994 print_dataset(zhp, cbp); 2995 2996 return (0); 2997 } 2998 2999 static int 3000 zfs_do_list(int argc, char **argv) 3001 { 3002 int c; 3003 static char default_fields[] = 3004 "name,used,available,referenced,mountpoint"; 3005 int types = ZFS_TYPE_DATASET; 3006 boolean_t types_specified = B_FALSE; 3007 char *fields = NULL; 3008 list_cbdata_t cb = { 0 }; 3009 char *value; 3010 int limit = 0; 3011 int ret = 0; 3012 zfs_sort_column_t *sortcol = NULL; 3013 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS; 3014 3015 /* check options */ 3016 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) { 3017 switch (c) { 3018 case 'o': 3019 fields = optarg; 3020 break; 3021 case 'p': 3022 cb.cb_literal = B_TRUE; 3023 flags |= ZFS_ITER_LITERAL_PROPS; 3024 break; 3025 case 'd': 3026 limit = parse_depth(optarg, &flags); 3027 break; 3028 case 'r': 3029 flags |= ZFS_ITER_RECURSE; 3030 break; 3031 case 'H': 3032 cb.cb_scripted = B_TRUE; 3033 break; 3034 case 's': 3035 if (zfs_add_sort_column(&sortcol, optarg, 3036 B_FALSE) != 0) { 3037 (void) fprintf(stderr, 3038 gettext("invalid property '%s'\n"), optarg); 3039 usage(B_FALSE); 3040 } 3041 break; 3042 case 'S': 3043 if (zfs_add_sort_column(&sortcol, optarg, 3044 B_TRUE) != 0) { 3045 (void) fprintf(stderr, 3046 gettext("invalid property '%s'\n"), optarg); 3047 usage(B_FALSE); 3048 } 3049 break; 3050 case 't': 3051 types = 0; 3052 types_specified = B_TRUE; 3053 flags &= ~ZFS_ITER_PROP_LISTSNAPS; 3054 while (*optarg != '\0') { 3055 static char *type_subopts[] = { "filesystem", 3056 "volume", "snapshot", "snap", "bookmark", 3057 "all", NULL }; 3058 3059 switch (getsubopt(&optarg, type_subopts, 3060 &value)) { 3061 case 0: 3062 types |= ZFS_TYPE_FILESYSTEM; 3063 break; 3064 case 1: 3065 types |= ZFS_TYPE_VOLUME; 3066 break; 3067 case 2: 3068 case 3: 3069 types |= ZFS_TYPE_SNAPSHOT; 3070 break; 3071 case 4: 3072 types |= ZFS_TYPE_BOOKMARK; 3073 break; 3074 case 5: 3075 types = ZFS_TYPE_DATASET | 3076 ZFS_TYPE_BOOKMARK; 3077 break; 3078 default: 3079 (void) fprintf(stderr, 3080 gettext("invalid type '%s'\n"), 3081 value); 3082 usage(B_FALSE); 3083 } 3084 } 3085 break; 3086 case ':': 3087 (void) fprintf(stderr, gettext("missing argument for " 3088 "'%c' option\n"), optopt); 3089 usage(B_FALSE); 3090 break; 3091 case '?': 3092 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3093 optopt); 3094 usage(B_FALSE); 3095 } 3096 } 3097 3098 argc -= optind; 3099 argv += optind; 3100 3101 if (fields == NULL) 3102 fields = default_fields; 3103 3104 /* 3105 * If "-o space" and no types were specified, don't display snapshots. 3106 */ 3107 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE) 3108 types &= ~ZFS_TYPE_SNAPSHOT; 3109 3110 /* 3111 * If the user specifies '-o all', the zprop_get_list() doesn't 3112 * normally include the name of the dataset. For 'zfs list', we always 3113 * want this property to be first. 3114 */ 3115 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET) 3116 != 0) 3117 usage(B_FALSE); 3118 3119 cb.cb_first = B_TRUE; 3120 3121 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist, 3122 limit, list_callback, &cb); 3123 3124 zprop_free_list(cb.cb_proplist); 3125 zfs_free_sort_columns(sortcol); 3126 3127 if (ret == 0 && cb.cb_first && !cb.cb_scripted) 3128 (void) printf(gettext("no datasets available\n")); 3129 3130 return (ret); 3131 } 3132 3133 /* 3134 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol> 3135 * zfs rename [-f] -p <fs | vol> <fs | vol> 3136 * zfs rename -r <snap> <snap> 3137 * 3138 * Renames the given dataset to another of the same type. 3139 * 3140 * The '-p' flag creates all the non-existing ancestors of the target first. 3141 */ 3142 /* ARGSUSED */ 3143 static int 3144 zfs_do_rename(int argc, char **argv) 3145 { 3146 zfs_handle_t *zhp; 3147 int c; 3148 int ret = 0; 3149 boolean_t recurse = B_FALSE; 3150 boolean_t parents = B_FALSE; 3151 boolean_t force_unmount = B_FALSE; 3152 3153 /* check options */ 3154 while ((c = getopt(argc, argv, "prf")) != -1) { 3155 switch (c) { 3156 case 'p': 3157 parents = B_TRUE; 3158 break; 3159 case 'r': 3160 recurse = B_TRUE; 3161 break; 3162 case 'f': 3163 force_unmount = B_TRUE; 3164 break; 3165 case '?': 3166 default: 3167 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3168 optopt); 3169 usage(B_FALSE); 3170 } 3171 } 3172 3173 argc -= optind; 3174 argv += optind; 3175 3176 /* check number of arguments */ 3177 if (argc < 1) { 3178 (void) fprintf(stderr, gettext("missing source dataset " 3179 "argument\n")); 3180 usage(B_FALSE); 3181 } 3182 if (argc < 2) { 3183 (void) fprintf(stderr, gettext("missing target dataset " 3184 "argument\n")); 3185 usage(B_FALSE); 3186 } 3187 if (argc > 2) { 3188 (void) fprintf(stderr, gettext("too many arguments\n")); 3189 usage(B_FALSE); 3190 } 3191 3192 if (recurse && parents) { 3193 (void) fprintf(stderr, gettext("-p and -r options are mutually " 3194 "exclusive\n")); 3195 usage(B_FALSE); 3196 } 3197 3198 if (recurse && strchr(argv[0], '@') == 0) { 3199 (void) fprintf(stderr, gettext("source dataset for recursive " 3200 "rename must be a snapshot\n")); 3201 usage(B_FALSE); 3202 } 3203 3204 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM | 3205 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL) 3206 return (1); 3207 3208 /* If we were asked and the name looks good, try to create ancestors. */ 3209 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) && 3210 zfs_create_ancestors(g_zfs, argv[1]) != 0) { 3211 zfs_close(zhp); 3212 return (1); 3213 } 3214 3215 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0); 3216 3217 zfs_close(zhp); 3218 return (ret); 3219 } 3220 3221 /* 3222 * zfs promote <fs> 3223 * 3224 * Promotes the given clone fs to be the parent 3225 */ 3226 /* ARGSUSED */ 3227 static int 3228 zfs_do_promote(int argc, char **argv) 3229 { 3230 zfs_handle_t *zhp; 3231 int ret = 0; 3232 3233 /* check options */ 3234 if (argc > 1 && argv[1][0] == '-') { 3235 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3236 argv[1][1]); 3237 usage(B_FALSE); 3238 } 3239 3240 /* check number of arguments */ 3241 if (argc < 2) { 3242 (void) fprintf(stderr, gettext("missing clone filesystem" 3243 " argument\n")); 3244 usage(B_FALSE); 3245 } 3246 if (argc > 2) { 3247 (void) fprintf(stderr, gettext("too many arguments\n")); 3248 usage(B_FALSE); 3249 } 3250 3251 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3252 if (zhp == NULL) 3253 return (1); 3254 3255 ret = (zfs_promote(zhp) != 0); 3256 3257 3258 zfs_close(zhp); 3259 return (ret); 3260 } 3261 3262 /* 3263 * zfs rollback [-rRf] <snapshot> 3264 * 3265 * -r Delete any intervening snapshots before doing rollback 3266 * -R Delete any snapshots and their clones 3267 * -f ignored for backwards compatability 3268 * 3269 * Given a filesystem, rollback to a specific snapshot, discarding any changes 3270 * since then and making it the active dataset. If more recent snapshots exist, 3271 * the command will complain unless the '-r' flag is given. 3272 */ 3273 typedef struct rollback_cbdata { 3274 uint64_t cb_create; 3275 boolean_t cb_first; 3276 int cb_doclones; 3277 char *cb_target; 3278 int cb_error; 3279 boolean_t cb_recurse; 3280 } rollback_cbdata_t; 3281 3282 static int 3283 rollback_check_dependent(zfs_handle_t *zhp, void *data) 3284 { 3285 rollback_cbdata_t *cbp = data; 3286 3287 if (cbp->cb_first && cbp->cb_recurse) { 3288 (void) fprintf(stderr, gettext("cannot rollback to " 3289 "'%s': clones of previous snapshots exist\n"), 3290 cbp->cb_target); 3291 (void) fprintf(stderr, gettext("use '-R' to " 3292 "force deletion of the following clones and " 3293 "dependents:\n")); 3294 cbp->cb_first = 0; 3295 cbp->cb_error = 1; 3296 } 3297 3298 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 3299 3300 zfs_close(zhp); 3301 return (0); 3302 } 3303 3304 /* 3305 * Report any snapshots more recent than the one specified. Used when '-r' is 3306 * not specified. We reuse this same callback for the snapshot dependents - if 3307 * 'cb_dependent' is set, then this is a dependent and we should report it 3308 * without checking the transaction group. 3309 */ 3310 static int 3311 rollback_check(zfs_handle_t *zhp, void *data) 3312 { 3313 rollback_cbdata_t *cbp = data; 3314 3315 if (cbp->cb_doclones) { 3316 zfs_close(zhp); 3317 return (0); 3318 } 3319 3320 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { 3321 if (cbp->cb_first && !cbp->cb_recurse) { 3322 (void) fprintf(stderr, gettext("cannot " 3323 "rollback to '%s': more recent snapshots " 3324 "or bookmarks exist\n"), 3325 cbp->cb_target); 3326 (void) fprintf(stderr, gettext("use '-r' to " 3327 "force deletion of the following " 3328 "snapshots and bookmarks:\n")); 3329 cbp->cb_first = 0; 3330 cbp->cb_error = 1; 3331 } 3332 3333 if (cbp->cb_recurse) { 3334 if (zfs_iter_dependents(zhp, B_TRUE, 3335 rollback_check_dependent, cbp) != 0) { 3336 zfs_close(zhp); 3337 return (-1); 3338 } 3339 } else { 3340 (void) fprintf(stderr, "%s\n", 3341 zfs_get_name(zhp)); 3342 } 3343 } 3344 zfs_close(zhp); 3345 return (0); 3346 } 3347 3348 static int 3349 zfs_do_rollback(int argc, char **argv) 3350 { 3351 int ret = 0; 3352 int c; 3353 boolean_t force = B_FALSE; 3354 rollback_cbdata_t cb = { 0 }; 3355 zfs_handle_t *zhp, *snap; 3356 char parentname[ZFS_MAXNAMELEN]; 3357 char *delim; 3358 3359 /* check options */ 3360 while ((c = getopt(argc, argv, "rRf")) != -1) { 3361 switch (c) { 3362 case 'r': 3363 cb.cb_recurse = 1; 3364 break; 3365 case 'R': 3366 cb.cb_recurse = 1; 3367 cb.cb_doclones = 1; 3368 break; 3369 case 'f': 3370 force = B_TRUE; 3371 break; 3372 case '?': 3373 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3374 optopt); 3375 usage(B_FALSE); 3376 } 3377 } 3378 3379 argc -= optind; 3380 argv += optind; 3381 3382 /* check number of arguments */ 3383 if (argc < 1) { 3384 (void) fprintf(stderr, gettext("missing dataset argument\n")); 3385 usage(B_FALSE); 3386 } 3387 if (argc > 1) { 3388 (void) fprintf(stderr, gettext("too many arguments\n")); 3389 usage(B_FALSE); 3390 } 3391 3392 /* open the snapshot */ 3393 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 3394 return (1); 3395 3396 /* open the parent dataset */ 3397 (void) strlcpy(parentname, argv[0], sizeof (parentname)); 3398 verify((delim = strrchr(parentname, '@')) != NULL); 3399 *delim = '\0'; 3400 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) { 3401 zfs_close(snap); 3402 return (1); 3403 } 3404 3405 /* 3406 * Check for more recent snapshots and/or clones based on the presence 3407 * of '-r' and '-R'. 3408 */ 3409 cb.cb_target = argv[0]; 3410 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 3411 cb.cb_first = B_TRUE; 3412 cb.cb_error = 0; 3413 if ((ret = zfs_iter_snapshots(zhp, rollback_check, &cb)) != 0) 3414 goto out; 3415 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0) 3416 goto out; 3417 3418 if ((ret = cb.cb_error) != 0) 3419 goto out; 3420 3421 /* 3422 * Rollback parent to the given snapshot. 3423 */ 3424 ret = zfs_rollback(zhp, snap, force); 3425 3426 out: 3427 zfs_close(snap); 3428 zfs_close(zhp); 3429 3430 if (ret == 0) 3431 return (0); 3432 else 3433 return (1); 3434 } 3435 3436 /* 3437 * zfs set property=value { fs | snap | vol } ... 3438 * 3439 * Sets the given property for all datasets specified on the command line. 3440 */ 3441 typedef struct set_cbdata { 3442 char *cb_propname; 3443 char *cb_value; 3444 } set_cbdata_t; 3445 3446 static int 3447 set_callback(zfs_handle_t *zhp, void *data) 3448 { 3449 set_cbdata_t *cbp = data; 3450 3451 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) { 3452 switch (libzfs_errno(g_zfs)) { 3453 case EZFS_MOUNTFAILED: 3454 (void) fprintf(stderr, gettext("property may be set " 3455 "but unable to remount filesystem\n")); 3456 break; 3457 case EZFS_SHARENFSFAILED: 3458 (void) fprintf(stderr, gettext("property may be set " 3459 "but unable to reshare filesystem\n")); 3460 break; 3461 } 3462 return (1); 3463 } 3464 return (0); 3465 } 3466 3467 static int 3468 zfs_do_set(int argc, char **argv) 3469 { 3470 set_cbdata_t cb; 3471 int ret = 0; 3472 3473 /* check for options */ 3474 if (argc > 1 && argv[1][0] == '-') { 3475 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3476 argv[1][1]); 3477 usage(B_FALSE); 3478 } 3479 3480 /* check number of arguments */ 3481 if (argc < 2) { 3482 (void) fprintf(stderr, gettext("missing property=value " 3483 "argument\n")); 3484 usage(B_FALSE); 3485 } 3486 if (argc < 3) { 3487 (void) fprintf(stderr, gettext("missing dataset name\n")); 3488 usage(B_FALSE); 3489 } 3490 3491 /* validate property=value argument */ 3492 cb.cb_propname = argv[1]; 3493 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) || 3494 (cb.cb_value[1] == '\0')) { 3495 (void) fprintf(stderr, gettext("missing value in " 3496 "property=value argument\n")); 3497 usage(B_FALSE); 3498 } 3499 3500 *cb.cb_value = '\0'; 3501 cb.cb_value++; 3502 3503 if (*cb.cb_propname == '\0') { 3504 (void) fprintf(stderr, 3505 gettext("missing property in property=value argument\n")); 3506 usage(B_FALSE); 3507 } 3508 3509 ret = zfs_for_each(argc - 2, argv + 2, NULL, 3510 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb); 3511 3512 return (ret); 3513 } 3514 3515 typedef struct snap_cbdata { 3516 nvlist_t *sd_nvl; 3517 boolean_t sd_recursive; 3518 const char *sd_snapname; 3519 } snap_cbdata_t; 3520 3521 static int 3522 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 3523 { 3524 snap_cbdata_t *sd = arg; 3525 char *name; 3526 int rv = 0; 3527 int error; 3528 3529 if (sd->sd_recursive && 3530 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) { 3531 zfs_close(zhp); 3532 return (0); 3533 } 3534 3535 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname); 3536 if (error == -1) 3537 nomem(); 3538 fnvlist_add_boolean(sd->sd_nvl, name); 3539 free(name); 3540 3541 if (sd->sd_recursive) 3542 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); 3543 zfs_close(zhp); 3544 return (rv); 3545 } 3546 3547 /* 3548 * zfs snapshot [-r] [-o prop=value] ... <fs@snap> 3549 * 3550 * Creates a snapshot with the given name. While functionally equivalent to 3551 * 'zfs create', it is a separate command to differentiate intent. 3552 */ 3553 static int 3554 zfs_do_snapshot(int argc, char **argv) 3555 { 3556 int ret = 0; 3557 char c; 3558 nvlist_t *props; 3559 snap_cbdata_t sd = { 0 }; 3560 boolean_t multiple_snaps = B_FALSE; 3561 3562 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 3563 nomem(); 3564 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0) 3565 nomem(); 3566 3567 /* check options */ 3568 while ((c = getopt(argc, argv, "ro:")) != -1) { 3569 switch (c) { 3570 case 'o': 3571 if (parseprop(props)) 3572 return (1); 3573 break; 3574 case 'r': 3575 sd.sd_recursive = B_TRUE; 3576 multiple_snaps = B_TRUE; 3577 break; 3578 case '?': 3579 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3580 optopt); 3581 goto usage; 3582 } 3583 } 3584 3585 argc -= optind; 3586 argv += optind; 3587 3588 /* check number of arguments */ 3589 if (argc < 1) { 3590 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 3591 goto usage; 3592 } 3593 3594 if (argc > 1) 3595 multiple_snaps = B_TRUE; 3596 for (; argc > 0; argc--, argv++) { 3597 char *atp; 3598 zfs_handle_t *zhp; 3599 3600 atp = strchr(argv[0], '@'); 3601 if (atp == NULL) 3602 goto usage; 3603 *atp = '\0'; 3604 sd.sd_snapname = atp + 1; 3605 zhp = zfs_open(g_zfs, argv[0], 3606 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3607 if (zhp == NULL) 3608 goto usage; 3609 if (zfs_snapshot_cb(zhp, &sd) != 0) 3610 goto usage; 3611 } 3612 3613 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props); 3614 nvlist_free(sd.sd_nvl); 3615 nvlist_free(props); 3616 if (ret != 0 && multiple_snaps) 3617 (void) fprintf(stderr, gettext("no snapshots were created\n")); 3618 return (ret != 0); 3619 3620 usage: 3621 nvlist_free(sd.sd_nvl); 3622 nvlist_free(props); 3623 usage(B_FALSE); 3624 return (-1); 3625 } 3626 3627 /* 3628 * Send a backup stream to stdout. 3629 */ 3630 static int 3631 zfs_do_send(int argc, char **argv) 3632 { 3633 char *fromname = NULL; 3634 char *toname = NULL; 3635 char *cp; 3636 zfs_handle_t *zhp; 3637 sendflags_t flags = { 0 }; 3638 int c, err; 3639 nvlist_t *dbgnv = NULL; 3640 boolean_t extraverbose = B_FALSE; 3641 3642 /* check options */ 3643 while ((c = getopt(argc, argv, ":i:I:RDpvnPLe")) != -1) { 3644 switch (c) { 3645 case 'i': 3646 if (fromname) 3647 usage(B_FALSE); 3648 fromname = optarg; 3649 break; 3650 case 'I': 3651 if (fromname) 3652 usage(B_FALSE); 3653 fromname = optarg; 3654 flags.doall = B_TRUE; 3655 break; 3656 case 'R': 3657 flags.replicate = B_TRUE; 3658 break; 3659 case 'p': 3660 flags.props = B_TRUE; 3661 break; 3662 case 'P': 3663 flags.parsable = B_TRUE; 3664 flags.verbose = B_TRUE; 3665 break; 3666 case 'v': 3667 if (flags.verbose) 3668 extraverbose = B_TRUE; 3669 flags.verbose = B_TRUE; 3670 flags.progress = B_TRUE; 3671 break; 3672 case 'D': 3673 flags.dedup = B_TRUE; 3674 break; 3675 case 'n': 3676 flags.dryrun = B_TRUE; 3677 break; 3678 case 'L': 3679 flags.largeblock = B_TRUE; 3680 break; 3681 case 'e': 3682 flags.embed_data = B_TRUE; 3683 break; 3684 case ':': 3685 (void) fprintf(stderr, gettext("missing argument for " 3686 "'%c' option\n"), optopt); 3687 usage(B_FALSE); 3688 break; 3689 case '?': 3690 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3691 optopt); 3692 usage(B_FALSE); 3693 } 3694 } 3695 3696 argc -= optind; 3697 argv += optind; 3698 3699 /* check number of arguments */ 3700 if (argc < 1) { 3701 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 3702 usage(B_FALSE); 3703 } 3704 if (argc > 1) { 3705 (void) fprintf(stderr, gettext("too many arguments\n")); 3706 usage(B_FALSE); 3707 } 3708 3709 if (!flags.dryrun && isatty(STDOUT_FILENO)) { 3710 (void) fprintf(stderr, 3711 gettext("Error: Stream can not be written to a terminal.\n" 3712 "You must redirect standard output.\n")); 3713 return (1); 3714 } 3715 3716 /* 3717 * Special case sending a filesystem, or from a bookmark. 3718 */ 3719 if (strchr(argv[0], '@') == NULL || 3720 (fromname && strchr(fromname, '#') != NULL)) { 3721 char frombuf[ZFS_MAXNAMELEN]; 3722 enum lzc_send_flags lzc_flags = 0; 3723 3724 if (flags.replicate || flags.doall || flags.props || 3725 flags.dedup || flags.dryrun || flags.verbose || 3726 flags.progress) { 3727 (void) fprintf(stderr, 3728 gettext("Error: " 3729 "Unsupported flag with filesystem or bookmark.\n")); 3730 return (1); 3731 } 3732 3733 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET); 3734 if (zhp == NULL) 3735 return (1); 3736 3737 if (flags.largeblock) 3738 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK; 3739 if (flags.embed_data) 3740 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA; 3741 3742 if (fromname != NULL && 3743 (fromname[0] == '#' || fromname[0] == '@')) { 3744 /* 3745 * Incremental source name begins with # or @. 3746 * Default to same fs as target. 3747 */ 3748 (void) strncpy(frombuf, argv[0], sizeof (frombuf)); 3749 cp = strchr(frombuf, '@'); 3750 if (cp != NULL) 3751 *cp = '\0'; 3752 (void) strlcat(frombuf, fromname, sizeof (frombuf)); 3753 fromname = frombuf; 3754 } 3755 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags); 3756 zfs_close(zhp); 3757 return (err != 0); 3758 } 3759 3760 cp = strchr(argv[0], '@'); 3761 *cp = '\0'; 3762 toname = cp + 1; 3763 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3764 if (zhp == NULL) 3765 return (1); 3766 3767 /* 3768 * If they specified the full path to the snapshot, chop off 3769 * everything except the short name of the snapshot, but special 3770 * case if they specify the origin. 3771 */ 3772 if (fromname && (cp = strchr(fromname, '@')) != NULL) { 3773 char origin[ZFS_MAXNAMELEN]; 3774 zprop_source_t src; 3775 3776 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN, 3777 origin, sizeof (origin), &src, NULL, 0, B_FALSE); 3778 3779 if (strcmp(origin, fromname) == 0) { 3780 fromname = NULL; 3781 flags.fromorigin = B_TRUE; 3782 } else { 3783 *cp = '\0'; 3784 if (cp != fromname && strcmp(argv[0], fromname)) { 3785 (void) fprintf(stderr, 3786 gettext("incremental source must be " 3787 "in same filesystem\n")); 3788 usage(B_FALSE); 3789 } 3790 fromname = cp + 1; 3791 if (strchr(fromname, '@') || strchr(fromname, '/')) { 3792 (void) fprintf(stderr, 3793 gettext("invalid incremental source\n")); 3794 usage(B_FALSE); 3795 } 3796 } 3797 } 3798 3799 if (flags.replicate && fromname == NULL) 3800 flags.doall = B_TRUE; 3801 3802 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0, 3803 extraverbose ? &dbgnv : NULL); 3804 3805 if (extraverbose && dbgnv != NULL) { 3806 /* 3807 * dump_nvlist prints to stdout, but that's been 3808 * redirected to a file. Make it print to stderr 3809 * instead. 3810 */ 3811 (void) dup2(STDERR_FILENO, STDOUT_FILENO); 3812 dump_nvlist(dbgnv, 0); 3813 nvlist_free(dbgnv); 3814 } 3815 zfs_close(zhp); 3816 3817 return (err != 0); 3818 } 3819 3820 /* 3821 * zfs receive [-vnFu] [-d | -e] <fs@snap> 3822 * 3823 * Restore a backup stream from stdin. 3824 */ 3825 static int 3826 zfs_do_receive(int argc, char **argv) 3827 { 3828 int c, err; 3829 recvflags_t flags = { 0 }; 3830 3831 /* check options */ 3832 while ((c = getopt(argc, argv, ":denuvF")) != -1) { 3833 switch (c) { 3834 case 'd': 3835 flags.isprefix = B_TRUE; 3836 break; 3837 case 'e': 3838 flags.isprefix = B_TRUE; 3839 flags.istail = B_TRUE; 3840 break; 3841 case 'n': 3842 flags.dryrun = B_TRUE; 3843 break; 3844 case 'u': 3845 flags.nomount = B_TRUE; 3846 break; 3847 case 'v': 3848 flags.verbose = B_TRUE; 3849 break; 3850 case 'F': 3851 flags.force = B_TRUE; 3852 break; 3853 case ':': 3854 (void) fprintf(stderr, gettext("missing argument for " 3855 "'%c' option\n"), optopt); 3856 usage(B_FALSE); 3857 break; 3858 case '?': 3859 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3860 optopt); 3861 usage(B_FALSE); 3862 } 3863 } 3864 3865 argc -= optind; 3866 argv += optind; 3867 3868 /* check number of arguments */ 3869 if (argc < 1) { 3870 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 3871 usage(B_FALSE); 3872 } 3873 if (argc > 1) { 3874 (void) fprintf(stderr, gettext("too many arguments\n")); 3875 usage(B_FALSE); 3876 } 3877 3878 if (isatty(STDIN_FILENO)) { 3879 (void) fprintf(stderr, 3880 gettext("Error: Backup stream can not be read " 3881 "from a terminal.\n" 3882 "You must redirect standard input.\n")); 3883 return (1); 3884 } 3885 3886 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL); 3887 3888 return (err != 0); 3889 } 3890 3891 /* 3892 * allow/unallow stuff 3893 */ 3894 /* copied from zfs/sys/dsl_deleg.h */ 3895 #define ZFS_DELEG_PERM_CREATE "create" 3896 #define ZFS_DELEG_PERM_DESTROY "destroy" 3897 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot" 3898 #define ZFS_DELEG_PERM_ROLLBACK "rollback" 3899 #define ZFS_DELEG_PERM_CLONE "clone" 3900 #define ZFS_DELEG_PERM_PROMOTE "promote" 3901 #define ZFS_DELEG_PERM_RENAME "rename" 3902 #define ZFS_DELEG_PERM_MOUNT "mount" 3903 #define ZFS_DELEG_PERM_SHARE "share" 3904 #define ZFS_DELEG_PERM_SEND "send" 3905 #define ZFS_DELEG_PERM_RECEIVE "receive" 3906 #define ZFS_DELEG_PERM_ALLOW "allow" 3907 #define ZFS_DELEG_PERM_USERPROP "userprop" 3908 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */ 3909 #define ZFS_DELEG_PERM_USERQUOTA "userquota" 3910 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota" 3911 #define ZFS_DELEG_PERM_USERUSED "userused" 3912 #define ZFS_DELEG_PERM_GROUPUSED "groupused" 3913 #define ZFS_DELEG_PERM_HOLD "hold" 3914 #define ZFS_DELEG_PERM_RELEASE "release" 3915 #define ZFS_DELEG_PERM_DIFF "diff" 3916 #define ZFS_DELEG_PERM_BOOKMARK "bookmark" 3917 3918 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE 3919 3920 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = { 3921 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW }, 3922 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE }, 3923 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE }, 3924 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY }, 3925 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF}, 3926 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD }, 3927 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT }, 3928 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE }, 3929 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE }, 3930 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE }, 3931 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME }, 3932 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK }, 3933 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND }, 3934 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE }, 3935 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT }, 3936 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK }, 3937 3938 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA }, 3939 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED }, 3940 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP }, 3941 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA }, 3942 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED }, 3943 { NULL, ZFS_DELEG_NOTE_NONE } 3944 }; 3945 3946 /* permission structure */ 3947 typedef struct deleg_perm { 3948 zfs_deleg_who_type_t dp_who_type; 3949 const char *dp_name; 3950 boolean_t dp_local; 3951 boolean_t dp_descend; 3952 } deleg_perm_t; 3953 3954 /* */ 3955 typedef struct deleg_perm_node { 3956 deleg_perm_t dpn_perm; 3957 3958 uu_avl_node_t dpn_avl_node; 3959 } deleg_perm_node_t; 3960 3961 typedef struct fs_perm fs_perm_t; 3962 3963 /* permissions set */ 3964 typedef struct who_perm { 3965 zfs_deleg_who_type_t who_type; 3966 const char *who_name; /* id */ 3967 char who_ug_name[256]; /* user/group name */ 3968 fs_perm_t *who_fsperm; /* uplink */ 3969 3970 uu_avl_t *who_deleg_perm_avl; /* permissions */ 3971 } who_perm_t; 3972 3973 /* */ 3974 typedef struct who_perm_node { 3975 who_perm_t who_perm; 3976 uu_avl_node_t who_avl_node; 3977 } who_perm_node_t; 3978 3979 typedef struct fs_perm_set fs_perm_set_t; 3980 /* fs permissions */ 3981 struct fs_perm { 3982 const char *fsp_name; 3983 3984 uu_avl_t *fsp_sc_avl; /* sets,create */ 3985 uu_avl_t *fsp_uge_avl; /* user,group,everyone */ 3986 3987 fs_perm_set_t *fsp_set; /* uplink */ 3988 }; 3989 3990 /* */ 3991 typedef struct fs_perm_node { 3992 fs_perm_t fspn_fsperm; 3993 uu_avl_t *fspn_avl; 3994 3995 uu_list_node_t fspn_list_node; 3996 } fs_perm_node_t; 3997 3998 /* top level structure */ 3999 struct fs_perm_set { 4000 uu_list_pool_t *fsps_list_pool; 4001 uu_list_t *fsps_list; /* list of fs_perms */ 4002 4003 uu_avl_pool_t *fsps_named_set_avl_pool; 4004 uu_avl_pool_t *fsps_who_perm_avl_pool; 4005 uu_avl_pool_t *fsps_deleg_perm_avl_pool; 4006 }; 4007 4008 static inline const char * 4009 deleg_perm_type(zfs_deleg_note_t note) 4010 { 4011 /* subcommands */ 4012 switch (note) { 4013 /* SUBCOMMANDS */ 4014 /* OTHER */ 4015 case ZFS_DELEG_NOTE_GROUPQUOTA: 4016 case ZFS_DELEG_NOTE_GROUPUSED: 4017 case ZFS_DELEG_NOTE_USERPROP: 4018 case ZFS_DELEG_NOTE_USERQUOTA: 4019 case ZFS_DELEG_NOTE_USERUSED: 4020 /* other */ 4021 return (gettext("other")); 4022 default: 4023 return (gettext("subcommand")); 4024 } 4025 } 4026 4027 static int inline 4028 who_type2weight(zfs_deleg_who_type_t who_type) 4029 { 4030 int res; 4031 switch (who_type) { 4032 case ZFS_DELEG_NAMED_SET_SETS: 4033 case ZFS_DELEG_NAMED_SET: 4034 res = 0; 4035 break; 4036 case ZFS_DELEG_CREATE_SETS: 4037 case ZFS_DELEG_CREATE: 4038 res = 1; 4039 break; 4040 case ZFS_DELEG_USER_SETS: 4041 case ZFS_DELEG_USER: 4042 res = 2; 4043 break; 4044 case ZFS_DELEG_GROUP_SETS: 4045 case ZFS_DELEG_GROUP: 4046 res = 3; 4047 break; 4048 case ZFS_DELEG_EVERYONE_SETS: 4049 case ZFS_DELEG_EVERYONE: 4050 res = 4; 4051 break; 4052 default: 4053 res = -1; 4054 } 4055 4056 return (res); 4057 } 4058 4059 /* ARGSUSED */ 4060 static int 4061 who_perm_compare(const void *larg, const void *rarg, void *unused) 4062 { 4063 const who_perm_node_t *l = larg; 4064 const who_perm_node_t *r = rarg; 4065 zfs_deleg_who_type_t ltype = l->who_perm.who_type; 4066 zfs_deleg_who_type_t rtype = r->who_perm.who_type; 4067 int lweight = who_type2weight(ltype); 4068 int rweight = who_type2weight(rtype); 4069 int res = lweight - rweight; 4070 if (res == 0) 4071 res = strncmp(l->who_perm.who_name, r->who_perm.who_name, 4072 ZFS_MAX_DELEG_NAME-1); 4073 4074 if (res == 0) 4075 return (0); 4076 if (res > 0) 4077 return (1); 4078 else 4079 return (-1); 4080 } 4081 4082 /* ARGSUSED */ 4083 static int 4084 deleg_perm_compare(const void *larg, const void *rarg, void *unused) 4085 { 4086 const deleg_perm_node_t *l = larg; 4087 const deleg_perm_node_t *r = rarg; 4088 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name, 4089 ZFS_MAX_DELEG_NAME-1); 4090 4091 if (res == 0) 4092 return (0); 4093 4094 if (res > 0) 4095 return (1); 4096 else 4097 return (-1); 4098 } 4099 4100 static inline void 4101 fs_perm_set_init(fs_perm_set_t *fspset) 4102 { 4103 bzero(fspset, sizeof (fs_perm_set_t)); 4104 4105 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool", 4106 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node), 4107 NULL, UU_DEFAULT)) == NULL) 4108 nomem(); 4109 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL, 4110 UU_DEFAULT)) == NULL) 4111 nomem(); 4112 4113 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create( 4114 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof( 4115 who_perm_node_t, who_avl_node), who_perm_compare, 4116 UU_DEFAULT)) == NULL) 4117 nomem(); 4118 4119 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create( 4120 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof( 4121 who_perm_node_t, who_avl_node), who_perm_compare, 4122 UU_DEFAULT)) == NULL) 4123 nomem(); 4124 4125 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create( 4126 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof( 4127 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT)) 4128 == NULL) 4129 nomem(); 4130 } 4131 4132 static inline void fs_perm_fini(fs_perm_t *); 4133 static inline void who_perm_fini(who_perm_t *); 4134 4135 static inline void 4136 fs_perm_set_fini(fs_perm_set_t *fspset) 4137 { 4138 fs_perm_node_t *node = uu_list_first(fspset->fsps_list); 4139 4140 while (node != NULL) { 4141 fs_perm_node_t *next_node = 4142 uu_list_next(fspset->fsps_list, node); 4143 fs_perm_t *fsperm = &node->fspn_fsperm; 4144 fs_perm_fini(fsperm); 4145 uu_list_remove(fspset->fsps_list, node); 4146 free(node); 4147 node = next_node; 4148 } 4149 4150 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool); 4151 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool); 4152 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool); 4153 } 4154 4155 static inline void 4156 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type, 4157 const char *name) 4158 { 4159 deleg_perm->dp_who_type = type; 4160 deleg_perm->dp_name = name; 4161 } 4162 4163 static inline void 4164 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm, 4165 zfs_deleg_who_type_t type, const char *name) 4166 { 4167 uu_avl_pool_t *pool; 4168 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool; 4169 4170 bzero(who_perm, sizeof (who_perm_t)); 4171 4172 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL, 4173 UU_DEFAULT)) == NULL) 4174 nomem(); 4175 4176 who_perm->who_type = type; 4177 who_perm->who_name = name; 4178 who_perm->who_fsperm = fsperm; 4179 } 4180 4181 static inline void 4182 who_perm_fini(who_perm_t *who_perm) 4183 { 4184 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl); 4185 4186 while (node != NULL) { 4187 deleg_perm_node_t *next_node = 4188 uu_avl_next(who_perm->who_deleg_perm_avl, node); 4189 4190 uu_avl_remove(who_perm->who_deleg_perm_avl, node); 4191 free(node); 4192 node = next_node; 4193 } 4194 4195 uu_avl_destroy(who_perm->who_deleg_perm_avl); 4196 } 4197 4198 static inline void 4199 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname) 4200 { 4201 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool; 4202 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool; 4203 4204 bzero(fsperm, sizeof (fs_perm_t)); 4205 4206 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT)) 4207 == NULL) 4208 nomem(); 4209 4210 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT)) 4211 == NULL) 4212 nomem(); 4213 4214 fsperm->fsp_set = fspset; 4215 fsperm->fsp_name = fsname; 4216 } 4217 4218 static inline void 4219 fs_perm_fini(fs_perm_t *fsperm) 4220 { 4221 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl); 4222 while (node != NULL) { 4223 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl, 4224 node); 4225 who_perm_t *who_perm = &node->who_perm; 4226 who_perm_fini(who_perm); 4227 uu_avl_remove(fsperm->fsp_sc_avl, node); 4228 free(node); 4229 node = next_node; 4230 } 4231 4232 node = uu_avl_first(fsperm->fsp_uge_avl); 4233 while (node != NULL) { 4234 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl, 4235 node); 4236 who_perm_t *who_perm = &node->who_perm; 4237 who_perm_fini(who_perm); 4238 uu_avl_remove(fsperm->fsp_uge_avl, node); 4239 free(node); 4240 node = next_node; 4241 } 4242 4243 uu_avl_destroy(fsperm->fsp_sc_avl); 4244 uu_avl_destroy(fsperm->fsp_uge_avl); 4245 } 4246 4247 static void inline 4248 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node, 4249 zfs_deleg_who_type_t who_type, const char *name, char locality) 4250 { 4251 uu_avl_index_t idx = 0; 4252 4253 deleg_perm_node_t *found_node = NULL; 4254 deleg_perm_t *deleg_perm = &node->dpn_perm; 4255 4256 deleg_perm_init(deleg_perm, who_type, name); 4257 4258 if ((found_node = uu_avl_find(avl, node, NULL, &idx)) 4259 == NULL) 4260 uu_avl_insert(avl, node, idx); 4261 else { 4262 node = found_node; 4263 deleg_perm = &node->dpn_perm; 4264 } 4265 4266 4267 switch (locality) { 4268 case ZFS_DELEG_LOCAL: 4269 deleg_perm->dp_local = B_TRUE; 4270 break; 4271 case ZFS_DELEG_DESCENDENT: 4272 deleg_perm->dp_descend = B_TRUE; 4273 break; 4274 case ZFS_DELEG_NA: 4275 break; 4276 default: 4277 assert(B_FALSE); /* invalid locality */ 4278 } 4279 } 4280 4281 static inline int 4282 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality) 4283 { 4284 nvpair_t *nvp = NULL; 4285 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set; 4286 uu_avl_t *avl = who_perm->who_deleg_perm_avl; 4287 zfs_deleg_who_type_t who_type = who_perm->who_type; 4288 4289 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4290 const char *name = nvpair_name(nvp); 4291 data_type_t type = nvpair_type(nvp); 4292 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool; 4293 deleg_perm_node_t *node = 4294 safe_malloc(sizeof (deleg_perm_node_t)); 4295 4296 assert(type == DATA_TYPE_BOOLEAN); 4297 4298 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool); 4299 set_deleg_perm_node(avl, node, who_type, name, locality); 4300 } 4301 4302 return (0); 4303 } 4304 4305 static inline int 4306 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl) 4307 { 4308 nvpair_t *nvp = NULL; 4309 fs_perm_set_t *fspset = fsperm->fsp_set; 4310 4311 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4312 nvlist_t *nvl2 = NULL; 4313 const char *name = nvpair_name(nvp); 4314 uu_avl_t *avl = NULL; 4315 uu_avl_pool_t *avl_pool; 4316 zfs_deleg_who_type_t perm_type = name[0]; 4317 char perm_locality = name[1]; 4318 const char *perm_name = name + 3; 4319 boolean_t is_set = B_TRUE; 4320 who_perm_t *who_perm = NULL; 4321 4322 assert('$' == name[2]); 4323 4324 if (nvpair_value_nvlist(nvp, &nvl2) != 0) 4325 return (-1); 4326 4327 switch (perm_type) { 4328 case ZFS_DELEG_CREATE: 4329 case ZFS_DELEG_CREATE_SETS: 4330 case ZFS_DELEG_NAMED_SET: 4331 case ZFS_DELEG_NAMED_SET_SETS: 4332 avl_pool = fspset->fsps_named_set_avl_pool; 4333 avl = fsperm->fsp_sc_avl; 4334 break; 4335 case ZFS_DELEG_USER: 4336 case ZFS_DELEG_USER_SETS: 4337 case ZFS_DELEG_GROUP: 4338 case ZFS_DELEG_GROUP_SETS: 4339 case ZFS_DELEG_EVERYONE: 4340 case ZFS_DELEG_EVERYONE_SETS: 4341 avl_pool = fspset->fsps_who_perm_avl_pool; 4342 avl = fsperm->fsp_uge_avl; 4343 break; 4344 } 4345 4346 if (is_set) { 4347 who_perm_node_t *found_node = NULL; 4348 who_perm_node_t *node = safe_malloc( 4349 sizeof (who_perm_node_t)); 4350 who_perm = &node->who_perm; 4351 uu_avl_index_t idx = 0; 4352 4353 uu_avl_node_init(node, &node->who_avl_node, avl_pool); 4354 who_perm_init(who_perm, fsperm, perm_type, perm_name); 4355 4356 if ((found_node = uu_avl_find(avl, node, NULL, &idx)) 4357 == NULL) { 4358 if (avl == fsperm->fsp_uge_avl) { 4359 uid_t rid = 0; 4360 struct passwd *p = NULL; 4361 struct group *g = NULL; 4362 const char *nice_name = NULL; 4363 4364 switch (perm_type) { 4365 case ZFS_DELEG_USER_SETS: 4366 case ZFS_DELEG_USER: 4367 rid = atoi(perm_name); 4368 p = getpwuid(rid); 4369 if (p) 4370 nice_name = p->pw_name; 4371 break; 4372 case ZFS_DELEG_GROUP_SETS: 4373 case ZFS_DELEG_GROUP: 4374 rid = atoi(perm_name); 4375 g = getgrgid(rid); 4376 if (g) 4377 nice_name = g->gr_name; 4378 break; 4379 } 4380 4381 if (nice_name != NULL) 4382 (void) strlcpy( 4383 node->who_perm.who_ug_name, 4384 nice_name, 256); 4385 } 4386 4387 uu_avl_insert(avl, node, idx); 4388 } else { 4389 node = found_node; 4390 who_perm = &node->who_perm; 4391 } 4392 } 4393 4394 (void) parse_who_perm(who_perm, nvl2, perm_locality); 4395 } 4396 4397 return (0); 4398 } 4399 4400 static inline int 4401 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl) 4402 { 4403 nvpair_t *nvp = NULL; 4404 uu_avl_index_t idx = 0; 4405 4406 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4407 nvlist_t *nvl2 = NULL; 4408 const char *fsname = nvpair_name(nvp); 4409 data_type_t type = nvpair_type(nvp); 4410 fs_perm_t *fsperm = NULL; 4411 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t)); 4412 if (node == NULL) 4413 nomem(); 4414 4415 fsperm = &node->fspn_fsperm; 4416 4417 assert(DATA_TYPE_NVLIST == type); 4418 4419 uu_list_node_init(node, &node->fspn_list_node, 4420 fspset->fsps_list_pool); 4421 4422 idx = uu_list_numnodes(fspset->fsps_list); 4423 fs_perm_init(fsperm, fspset, fsname); 4424 4425 if (nvpair_value_nvlist(nvp, &nvl2) != 0) 4426 return (-1); 4427 4428 (void) parse_fs_perm(fsperm, nvl2); 4429 4430 uu_list_insert(fspset->fsps_list, node, idx); 4431 } 4432 4433 return (0); 4434 } 4435 4436 static inline const char * 4437 deleg_perm_comment(zfs_deleg_note_t note) 4438 { 4439 const char *str = ""; 4440 4441 /* subcommands */ 4442 switch (note) { 4443 /* SUBCOMMANDS */ 4444 case ZFS_DELEG_NOTE_ALLOW: 4445 str = gettext("Must also have the permission that is being" 4446 "\n\t\t\t\tallowed"); 4447 break; 4448 case ZFS_DELEG_NOTE_CLONE: 4449 str = gettext("Must also have the 'create' ability and 'mount'" 4450 "\n\t\t\t\tability in the origin file system"); 4451 break; 4452 case ZFS_DELEG_NOTE_CREATE: 4453 str = gettext("Must also have the 'mount' ability"); 4454 break; 4455 case ZFS_DELEG_NOTE_DESTROY: 4456 str = gettext("Must also have the 'mount' ability"); 4457 break; 4458 case ZFS_DELEG_NOTE_DIFF: 4459 str = gettext("Allows lookup of paths within a dataset;" 4460 "\n\t\t\t\tgiven an object number. Ordinary users need this" 4461 "\n\t\t\t\tin order to use zfs diff"); 4462 break; 4463 case ZFS_DELEG_NOTE_HOLD: 4464 str = gettext("Allows adding a user hold to a snapshot"); 4465 break; 4466 case ZFS_DELEG_NOTE_MOUNT: 4467 str = gettext("Allows mount/umount of ZFS datasets"); 4468 break; 4469 case ZFS_DELEG_NOTE_PROMOTE: 4470 str = gettext("Must also have the 'mount'\n\t\t\t\tand" 4471 " 'promote' ability in the origin file system"); 4472 break; 4473 case ZFS_DELEG_NOTE_RECEIVE: 4474 str = gettext("Must also have the 'mount' and 'create'" 4475 " ability"); 4476 break; 4477 case ZFS_DELEG_NOTE_RELEASE: 4478 str = gettext("Allows releasing a user hold which\n\t\t\t\t" 4479 "might destroy the snapshot"); 4480 break; 4481 case ZFS_DELEG_NOTE_RENAME: 4482 str = gettext("Must also have the 'mount' and 'create'" 4483 "\n\t\t\t\tability in the new parent"); 4484 break; 4485 case ZFS_DELEG_NOTE_ROLLBACK: 4486 str = gettext(""); 4487 break; 4488 case ZFS_DELEG_NOTE_SEND: 4489 str = gettext(""); 4490 break; 4491 case ZFS_DELEG_NOTE_SHARE: 4492 str = gettext("Allows sharing file systems over NFS or SMB" 4493 "\n\t\t\t\tprotocols"); 4494 break; 4495 case ZFS_DELEG_NOTE_SNAPSHOT: 4496 str = gettext(""); 4497 break; 4498 /* 4499 * case ZFS_DELEG_NOTE_VSCAN: 4500 * str = gettext(""); 4501 * break; 4502 */ 4503 /* OTHER */ 4504 case ZFS_DELEG_NOTE_GROUPQUOTA: 4505 str = gettext("Allows accessing any groupquota@... property"); 4506 break; 4507 case ZFS_DELEG_NOTE_GROUPUSED: 4508 str = gettext("Allows reading any groupused@... property"); 4509 break; 4510 case ZFS_DELEG_NOTE_USERPROP: 4511 str = gettext("Allows changing any user property"); 4512 break; 4513 case ZFS_DELEG_NOTE_USERQUOTA: 4514 str = gettext("Allows accessing any userquota@... property"); 4515 break; 4516 case ZFS_DELEG_NOTE_USERUSED: 4517 str = gettext("Allows reading any userused@... property"); 4518 break; 4519 /* other */ 4520 default: 4521 str = ""; 4522 } 4523 4524 return (str); 4525 } 4526 4527 struct allow_opts { 4528 boolean_t local; 4529 boolean_t descend; 4530 boolean_t user; 4531 boolean_t group; 4532 boolean_t everyone; 4533 boolean_t create; 4534 boolean_t set; 4535 boolean_t recursive; /* unallow only */ 4536 boolean_t prt_usage; 4537 4538 boolean_t prt_perms; 4539 char *who; 4540 char *perms; 4541 const char *dataset; 4542 }; 4543 4544 static inline int 4545 prop_cmp(const void *a, const void *b) 4546 { 4547 const char *str1 = *(const char **)a; 4548 const char *str2 = *(const char **)b; 4549 return (strcmp(str1, str2)); 4550 } 4551 4552 static void 4553 allow_usage(boolean_t un, boolean_t requested, const char *msg) 4554 { 4555 const char *opt_desc[] = { 4556 "-h", gettext("show this help message and exit"), 4557 "-l", gettext("set permission locally"), 4558 "-d", gettext("set permission for descents"), 4559 "-u", gettext("set permission for user"), 4560 "-g", gettext("set permission for group"), 4561 "-e", gettext("set permission for everyone"), 4562 "-c", gettext("set create time permission"), 4563 "-s", gettext("define permission set"), 4564 /* unallow only */ 4565 "-r", gettext("remove permissions recursively"), 4566 }; 4567 size_t unallow_size = sizeof (opt_desc) / sizeof (char *); 4568 size_t allow_size = unallow_size - 2; 4569 const char *props[ZFS_NUM_PROPS]; 4570 int i; 4571 size_t count = 0; 4572 FILE *fp = requested ? stdout : stderr; 4573 zprop_desc_t *pdtbl = zfs_prop_get_table(); 4574 const char *fmt = gettext("%-16s %-14s\t%s\n"); 4575 4576 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW : 4577 HELP_ALLOW)); 4578 (void) fprintf(fp, gettext("Options:\n")); 4579 for (int i = 0; i < (un ? unallow_size : allow_size); i++) { 4580 const char *opt = opt_desc[i++]; 4581 const char *optdsc = opt_desc[i]; 4582 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc); 4583 } 4584 4585 (void) fprintf(fp, gettext("\nThe following permissions are " 4586 "supported:\n\n")); 4587 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"), 4588 gettext("NOTES")); 4589 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) { 4590 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm; 4591 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note; 4592 const char *perm_type = deleg_perm_type(perm_note); 4593 const char *perm_comment = deleg_perm_comment(perm_note); 4594 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment); 4595 } 4596 4597 for (i = 0; i < ZFS_NUM_PROPS; i++) { 4598 zprop_desc_t *pd = &pdtbl[i]; 4599 if (pd->pd_visible != B_TRUE) 4600 continue; 4601 4602 if (pd->pd_attr == PROP_READONLY) 4603 continue; 4604 4605 props[count++] = pd->pd_name; 4606 } 4607 props[count] = NULL; 4608 4609 qsort(props, count, sizeof (char *), prop_cmp); 4610 4611 for (i = 0; i < count; i++) 4612 (void) fprintf(fp, fmt, props[i], gettext("property"), ""); 4613 4614 if (msg != NULL) 4615 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg); 4616 4617 exit(requested ? 0 : 2); 4618 } 4619 4620 static inline const char * 4621 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc, 4622 char **permsp) 4623 { 4624 if (un && argc == expected_argc - 1) 4625 *permsp = NULL; 4626 else if (argc == expected_argc) 4627 *permsp = argv[argc - 2]; 4628 else 4629 allow_usage(un, B_FALSE, 4630 gettext("wrong number of parameters\n")); 4631 4632 return (argv[argc - 1]); 4633 } 4634 4635 static void 4636 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts) 4637 { 4638 int uge_sum = opts->user + opts->group + opts->everyone; 4639 int csuge_sum = opts->create + opts->set + uge_sum; 4640 int ldcsuge_sum = csuge_sum + opts->local + opts->descend; 4641 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum; 4642 4643 if (uge_sum > 1) 4644 allow_usage(un, B_FALSE, 4645 gettext("-u, -g, and -e are mutually exclusive\n")); 4646 4647 if (opts->prt_usage) 4648 if (argc == 0 && all_sum == 0) 4649 allow_usage(un, B_TRUE, NULL); 4650 else 4651 usage(B_FALSE); 4652 4653 if (opts->set) { 4654 if (csuge_sum > 1) 4655 allow_usage(un, B_FALSE, 4656 gettext("invalid options combined with -s\n")); 4657 4658 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms); 4659 if (argv[0][0] != '@') 4660 allow_usage(un, B_FALSE, 4661 gettext("invalid set name: missing '@' prefix\n")); 4662 opts->who = argv[0]; 4663 } else if (opts->create) { 4664 if (ldcsuge_sum > 1) 4665 allow_usage(un, B_FALSE, 4666 gettext("invalid options combined with -c\n")); 4667 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4668 } else if (opts->everyone) { 4669 if (csuge_sum > 1) 4670 allow_usage(un, B_FALSE, 4671 gettext("invalid options combined with -e\n")); 4672 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4673 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone") 4674 == 0) { 4675 opts->everyone = B_TRUE; 4676 argc--; 4677 argv++; 4678 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4679 } else if (argc == 1 && !un) { 4680 opts->prt_perms = B_TRUE; 4681 opts->dataset = argv[argc-1]; 4682 } else { 4683 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms); 4684 opts->who = argv[0]; 4685 } 4686 4687 if (!opts->local && !opts->descend) { 4688 opts->local = B_TRUE; 4689 opts->descend = B_TRUE; 4690 } 4691 } 4692 4693 static void 4694 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend, 4695 const char *who, char *perms, nvlist_t *top_nvl) 4696 { 4697 int i; 4698 char ld[2] = { '\0', '\0' }; 4699 char who_buf[ZFS_MAXNAMELEN+32]; 4700 char base_type; 4701 char set_type; 4702 nvlist_t *base_nvl = NULL; 4703 nvlist_t *set_nvl = NULL; 4704 nvlist_t *nvl; 4705 4706 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0) 4707 nomem(); 4708 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0) 4709 nomem(); 4710 4711 switch (type) { 4712 case ZFS_DELEG_NAMED_SET_SETS: 4713 case ZFS_DELEG_NAMED_SET: 4714 set_type = ZFS_DELEG_NAMED_SET_SETS; 4715 base_type = ZFS_DELEG_NAMED_SET; 4716 ld[0] = ZFS_DELEG_NA; 4717 break; 4718 case ZFS_DELEG_CREATE_SETS: 4719 case ZFS_DELEG_CREATE: 4720 set_type = ZFS_DELEG_CREATE_SETS; 4721 base_type = ZFS_DELEG_CREATE; 4722 ld[0] = ZFS_DELEG_NA; 4723 break; 4724 case ZFS_DELEG_USER_SETS: 4725 case ZFS_DELEG_USER: 4726 set_type = ZFS_DELEG_USER_SETS; 4727 base_type = ZFS_DELEG_USER; 4728 if (local) 4729 ld[0] = ZFS_DELEG_LOCAL; 4730 if (descend) 4731 ld[1] = ZFS_DELEG_DESCENDENT; 4732 break; 4733 case ZFS_DELEG_GROUP_SETS: 4734 case ZFS_DELEG_GROUP: 4735 set_type = ZFS_DELEG_GROUP_SETS; 4736 base_type = ZFS_DELEG_GROUP; 4737 if (local) 4738 ld[0] = ZFS_DELEG_LOCAL; 4739 if (descend) 4740 ld[1] = ZFS_DELEG_DESCENDENT; 4741 break; 4742 case ZFS_DELEG_EVERYONE_SETS: 4743 case ZFS_DELEG_EVERYONE: 4744 set_type = ZFS_DELEG_EVERYONE_SETS; 4745 base_type = ZFS_DELEG_EVERYONE; 4746 if (local) 4747 ld[0] = ZFS_DELEG_LOCAL; 4748 if (descend) 4749 ld[1] = ZFS_DELEG_DESCENDENT; 4750 } 4751 4752 if (perms != NULL) { 4753 char *curr = perms; 4754 char *end = curr + strlen(perms); 4755 4756 while (curr < end) { 4757 char *delim = strchr(curr, ','); 4758 if (delim == NULL) 4759 delim = end; 4760 else 4761 *delim = '\0'; 4762 4763 if (curr[0] == '@') 4764 nvl = set_nvl; 4765 else 4766 nvl = base_nvl; 4767 4768 (void) nvlist_add_boolean(nvl, curr); 4769 if (delim != end) 4770 *delim = ','; 4771 curr = delim + 1; 4772 } 4773 4774 for (i = 0; i < 2; i++) { 4775 char locality = ld[i]; 4776 if (locality == 0) 4777 continue; 4778 4779 if (!nvlist_empty(base_nvl)) { 4780 if (who != NULL) 4781 (void) snprintf(who_buf, 4782 sizeof (who_buf), "%c%c$%s", 4783 base_type, locality, who); 4784 else 4785 (void) snprintf(who_buf, 4786 sizeof (who_buf), "%c%c$", 4787 base_type, locality); 4788 4789 (void) nvlist_add_nvlist(top_nvl, who_buf, 4790 base_nvl); 4791 } 4792 4793 4794 if (!nvlist_empty(set_nvl)) { 4795 if (who != NULL) 4796 (void) snprintf(who_buf, 4797 sizeof (who_buf), "%c%c$%s", 4798 set_type, locality, who); 4799 else 4800 (void) snprintf(who_buf, 4801 sizeof (who_buf), "%c%c$", 4802 set_type, locality); 4803 4804 (void) nvlist_add_nvlist(top_nvl, who_buf, 4805 set_nvl); 4806 } 4807 } 4808 } else { 4809 for (i = 0; i < 2; i++) { 4810 char locality = ld[i]; 4811 if (locality == 0) 4812 continue; 4813 4814 if (who != NULL) 4815 (void) snprintf(who_buf, sizeof (who_buf), 4816 "%c%c$%s", base_type, locality, who); 4817 else 4818 (void) snprintf(who_buf, sizeof (who_buf), 4819 "%c%c$", base_type, locality); 4820 (void) nvlist_add_boolean(top_nvl, who_buf); 4821 4822 if (who != NULL) 4823 (void) snprintf(who_buf, sizeof (who_buf), 4824 "%c%c$%s", set_type, locality, who); 4825 else 4826 (void) snprintf(who_buf, sizeof (who_buf), 4827 "%c%c$", set_type, locality); 4828 (void) nvlist_add_boolean(top_nvl, who_buf); 4829 } 4830 } 4831 } 4832 4833 static int 4834 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp) 4835 { 4836 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0) 4837 nomem(); 4838 4839 if (opts->set) { 4840 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local, 4841 opts->descend, opts->who, opts->perms, *nvlp); 4842 } else if (opts->create) { 4843 store_allow_perm(ZFS_DELEG_CREATE, opts->local, 4844 opts->descend, NULL, opts->perms, *nvlp); 4845 } else if (opts->everyone) { 4846 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local, 4847 opts->descend, NULL, opts->perms, *nvlp); 4848 } else { 4849 char *curr = opts->who; 4850 char *end = curr + strlen(curr); 4851 4852 while (curr < end) { 4853 const char *who; 4854 zfs_deleg_who_type_t who_type; 4855 char *endch; 4856 char *delim = strchr(curr, ','); 4857 char errbuf[256]; 4858 char id[64]; 4859 struct passwd *p = NULL; 4860 struct group *g = NULL; 4861 4862 uid_t rid; 4863 if (delim == NULL) 4864 delim = end; 4865 else 4866 *delim = '\0'; 4867 4868 rid = (uid_t)strtol(curr, &endch, 0); 4869 if (opts->user) { 4870 who_type = ZFS_DELEG_USER; 4871 if (*endch != '\0') 4872 p = getpwnam(curr); 4873 else 4874 p = getpwuid(rid); 4875 4876 if (p != NULL) 4877 rid = p->pw_uid; 4878 else { 4879 (void) snprintf(errbuf, 256, gettext( 4880 "invalid user %s"), curr); 4881 allow_usage(un, B_TRUE, errbuf); 4882 } 4883 } else if (opts->group) { 4884 who_type = ZFS_DELEG_GROUP; 4885 if (*endch != '\0') 4886 g = getgrnam(curr); 4887 else 4888 g = getgrgid(rid); 4889 4890 if (g != NULL) 4891 rid = g->gr_gid; 4892 else { 4893 (void) snprintf(errbuf, 256, gettext( 4894 "invalid group %s"), curr); 4895 allow_usage(un, B_TRUE, errbuf); 4896 } 4897 } else { 4898 if (*endch != '\0') { 4899 p = getpwnam(curr); 4900 } else { 4901 p = getpwuid(rid); 4902 } 4903 4904 if (p == NULL) 4905 if (*endch != '\0') { 4906 g = getgrnam(curr); 4907 } else { 4908 g = getgrgid(rid); 4909 } 4910 4911 if (p != NULL) { 4912 who_type = ZFS_DELEG_USER; 4913 rid = p->pw_uid; 4914 } else if (g != NULL) { 4915 who_type = ZFS_DELEG_GROUP; 4916 rid = g->gr_gid; 4917 } else { 4918 (void) snprintf(errbuf, 256, gettext( 4919 "invalid user/group %s"), curr); 4920 allow_usage(un, B_TRUE, errbuf); 4921 } 4922 } 4923 4924 (void) sprintf(id, "%u", rid); 4925 who = id; 4926 4927 store_allow_perm(who_type, opts->local, 4928 opts->descend, who, opts->perms, *nvlp); 4929 curr = delim + 1; 4930 } 4931 } 4932 4933 return (0); 4934 } 4935 4936 static void 4937 print_set_creat_perms(uu_avl_t *who_avl) 4938 { 4939 const char *sc_title[] = { 4940 gettext("Permission sets:\n"), 4941 gettext("Create time permissions:\n"), 4942 NULL 4943 }; 4944 const char **title_ptr = sc_title; 4945 who_perm_node_t *who_node = NULL; 4946 int prev_weight = -1; 4947 4948 for (who_node = uu_avl_first(who_avl); who_node != NULL; 4949 who_node = uu_avl_next(who_avl, who_node)) { 4950 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl; 4951 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type; 4952 const char *who_name = who_node->who_perm.who_name; 4953 int weight = who_type2weight(who_type); 4954 boolean_t first = B_TRUE; 4955 deleg_perm_node_t *deleg_node; 4956 4957 if (prev_weight != weight) { 4958 (void) printf(*title_ptr++); 4959 prev_weight = weight; 4960 } 4961 4962 if (who_name == NULL || strnlen(who_name, 1) == 0) 4963 (void) printf("\t"); 4964 else 4965 (void) printf("\t%s ", who_name); 4966 4967 for (deleg_node = uu_avl_first(avl); deleg_node != NULL; 4968 deleg_node = uu_avl_next(avl, deleg_node)) { 4969 if (first) { 4970 (void) printf("%s", 4971 deleg_node->dpn_perm.dp_name); 4972 first = B_FALSE; 4973 } else 4974 (void) printf(",%s", 4975 deleg_node->dpn_perm.dp_name); 4976 } 4977 4978 (void) printf("\n"); 4979 } 4980 } 4981 4982 static void inline 4983 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend, 4984 const char *title) 4985 { 4986 who_perm_node_t *who_node = NULL; 4987 boolean_t prt_title = B_TRUE; 4988 uu_avl_walk_t *walk; 4989 4990 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL) 4991 nomem(); 4992 4993 while ((who_node = uu_avl_walk_next(walk)) != NULL) { 4994 const char *who_name = who_node->who_perm.who_name; 4995 const char *nice_who_name = who_node->who_perm.who_ug_name; 4996 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl; 4997 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type; 4998 char delim = ' '; 4999 deleg_perm_node_t *deleg_node; 5000 boolean_t prt_who = B_TRUE; 5001 5002 for (deleg_node = uu_avl_first(avl); 5003 deleg_node != NULL; 5004 deleg_node = uu_avl_next(avl, deleg_node)) { 5005 if (local != deleg_node->dpn_perm.dp_local || 5006 descend != deleg_node->dpn_perm.dp_descend) 5007 continue; 5008 5009 if (prt_who) { 5010 const char *who = NULL; 5011 if (prt_title) { 5012 prt_title = B_FALSE; 5013 (void) printf(title); 5014 } 5015 5016 switch (who_type) { 5017 case ZFS_DELEG_USER_SETS: 5018 case ZFS_DELEG_USER: 5019 who = gettext("user"); 5020 if (nice_who_name) 5021 who_name = nice_who_name; 5022 break; 5023 case ZFS_DELEG_GROUP_SETS: 5024 case ZFS_DELEG_GROUP: 5025 who = gettext("group"); 5026 if (nice_who_name) 5027 who_name = nice_who_name; 5028 break; 5029 case ZFS_DELEG_EVERYONE_SETS: 5030 case ZFS_DELEG_EVERYONE: 5031 who = gettext("everyone"); 5032 who_name = NULL; 5033 } 5034 5035 prt_who = B_FALSE; 5036 if (who_name == NULL) 5037 (void) printf("\t%s", who); 5038 else 5039 (void) printf("\t%s %s", who, who_name); 5040 } 5041 5042 (void) printf("%c%s", delim, 5043 deleg_node->dpn_perm.dp_name); 5044 delim = ','; 5045 } 5046 5047 if (!prt_who) 5048 (void) printf("\n"); 5049 } 5050 5051 uu_avl_walk_end(walk); 5052 } 5053 5054 static void 5055 print_fs_perms(fs_perm_set_t *fspset) 5056 { 5057 fs_perm_node_t *node = NULL; 5058 char buf[ZFS_MAXNAMELEN+32]; 5059 const char *dsname = buf; 5060 5061 for (node = uu_list_first(fspset->fsps_list); node != NULL; 5062 node = uu_list_next(fspset->fsps_list, node)) { 5063 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl; 5064 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl; 5065 int left = 0; 5066 5067 (void) snprintf(buf, ZFS_MAXNAMELEN+32, 5068 gettext("---- Permissions on %s "), 5069 node->fspn_fsperm.fsp_name); 5070 (void) printf(dsname); 5071 left = 70 - strlen(buf); 5072 while (left-- > 0) 5073 (void) printf("-"); 5074 (void) printf("\n"); 5075 5076 print_set_creat_perms(sc_avl); 5077 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE, 5078 gettext("Local permissions:\n")); 5079 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE, 5080 gettext("Descendent permissions:\n")); 5081 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE, 5082 gettext("Local+Descendent permissions:\n")); 5083 } 5084 } 5085 5086 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL }; 5087 5088 struct deleg_perms { 5089 boolean_t un; 5090 nvlist_t *nvl; 5091 }; 5092 5093 static int 5094 set_deleg_perms(zfs_handle_t *zhp, void *data) 5095 { 5096 struct deleg_perms *perms = (struct deleg_perms *)data; 5097 zfs_type_t zfs_type = zfs_get_type(zhp); 5098 5099 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME) 5100 return (0); 5101 5102 return (zfs_set_fsacl(zhp, perms->un, perms->nvl)); 5103 } 5104 5105 static int 5106 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un) 5107 { 5108 zfs_handle_t *zhp; 5109 nvlist_t *perm_nvl = NULL; 5110 nvlist_t *update_perm_nvl = NULL; 5111 int error = 1; 5112 int c; 5113 struct allow_opts opts = { 0 }; 5114 5115 const char *optstr = un ? "ldugecsrh" : "ldugecsh"; 5116 5117 /* check opts */ 5118 while ((c = getopt(argc, argv, optstr)) != -1) { 5119 switch (c) { 5120 case 'l': 5121 opts.local = B_TRUE; 5122 break; 5123 case 'd': 5124 opts.descend = B_TRUE; 5125 break; 5126 case 'u': 5127 opts.user = B_TRUE; 5128 break; 5129 case 'g': 5130 opts.group = B_TRUE; 5131 break; 5132 case 'e': 5133 opts.everyone = B_TRUE; 5134 break; 5135 case 's': 5136 opts.set = B_TRUE; 5137 break; 5138 case 'c': 5139 opts.create = B_TRUE; 5140 break; 5141 case 'r': 5142 opts.recursive = B_TRUE; 5143 break; 5144 case ':': 5145 (void) fprintf(stderr, gettext("missing argument for " 5146 "'%c' option\n"), optopt); 5147 usage(B_FALSE); 5148 break; 5149 case 'h': 5150 opts.prt_usage = B_TRUE; 5151 break; 5152 case '?': 5153 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5154 optopt); 5155 usage(B_FALSE); 5156 } 5157 } 5158 5159 argc -= optind; 5160 argv += optind; 5161 5162 /* check arguments */ 5163 parse_allow_args(argc, argv, un, &opts); 5164 5165 /* try to open the dataset */ 5166 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM | 5167 ZFS_TYPE_VOLUME)) == NULL) { 5168 (void) fprintf(stderr, "Failed to open dataset: %s\n", 5169 opts.dataset); 5170 return (-1); 5171 } 5172 5173 if (zfs_get_fsacl(zhp, &perm_nvl) != 0) 5174 goto cleanup2; 5175 5176 fs_perm_set_init(&fs_perm_set); 5177 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) { 5178 (void) fprintf(stderr, "Failed to parse fsacl permissions\n"); 5179 goto cleanup1; 5180 } 5181 5182 if (opts.prt_perms) 5183 print_fs_perms(&fs_perm_set); 5184 else { 5185 (void) construct_fsacl_list(un, &opts, &update_perm_nvl); 5186 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0) 5187 goto cleanup0; 5188 5189 if (un && opts.recursive) { 5190 struct deleg_perms data = { un, update_perm_nvl }; 5191 if (zfs_iter_filesystems(zhp, set_deleg_perms, 5192 &data) != 0) 5193 goto cleanup0; 5194 } 5195 } 5196 5197 error = 0; 5198 5199 cleanup0: 5200 nvlist_free(perm_nvl); 5201 if (update_perm_nvl != NULL) 5202 nvlist_free(update_perm_nvl); 5203 cleanup1: 5204 fs_perm_set_fini(&fs_perm_set); 5205 cleanup2: 5206 zfs_close(zhp); 5207 5208 return (error); 5209 } 5210 5211 static int 5212 zfs_do_allow(int argc, char **argv) 5213 { 5214 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE)); 5215 } 5216 5217 static int 5218 zfs_do_unallow(int argc, char **argv) 5219 { 5220 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE)); 5221 } 5222 5223 static int 5224 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding) 5225 { 5226 int errors = 0; 5227 int i; 5228 const char *tag; 5229 boolean_t recursive = B_FALSE; 5230 const char *opts = holding ? "rt" : "r"; 5231 int c; 5232 5233 /* check options */ 5234 while ((c = getopt(argc, argv, opts)) != -1) { 5235 switch (c) { 5236 case 'r': 5237 recursive = B_TRUE; 5238 break; 5239 case '?': 5240 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5241 optopt); 5242 usage(B_FALSE); 5243 } 5244 } 5245 5246 argc -= optind; 5247 argv += optind; 5248 5249 /* check number of arguments */ 5250 if (argc < 2) 5251 usage(B_FALSE); 5252 5253 tag = argv[0]; 5254 --argc; 5255 ++argv; 5256 5257 if (holding && tag[0] == '.') { 5258 /* tags starting with '.' are reserved for libzfs */ 5259 (void) fprintf(stderr, gettext("tag may not start with '.'\n")); 5260 usage(B_FALSE); 5261 } 5262 5263 for (i = 0; i < argc; ++i) { 5264 zfs_handle_t *zhp; 5265 char parent[ZFS_MAXNAMELEN]; 5266 const char *delim; 5267 char *path = argv[i]; 5268 5269 delim = strchr(path, '@'); 5270 if (delim == NULL) { 5271 (void) fprintf(stderr, 5272 gettext("'%s' is not a snapshot\n"), path); 5273 ++errors; 5274 continue; 5275 } 5276 (void) strncpy(parent, path, delim - path); 5277 parent[delim - path] = '\0'; 5278 5279 zhp = zfs_open(g_zfs, parent, 5280 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 5281 if (zhp == NULL) { 5282 ++errors; 5283 continue; 5284 } 5285 if (holding) { 5286 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0) 5287 ++errors; 5288 } else { 5289 if (zfs_release(zhp, delim+1, tag, recursive) != 0) 5290 ++errors; 5291 } 5292 zfs_close(zhp); 5293 } 5294 5295 return (errors != 0); 5296 } 5297 5298 /* 5299 * zfs hold [-r] [-t] <tag> <snap> ... 5300 * 5301 * -r Recursively hold 5302 * 5303 * Apply a user-hold with the given tag to the list of snapshots. 5304 */ 5305 static int 5306 zfs_do_hold(int argc, char **argv) 5307 { 5308 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE)); 5309 } 5310 5311 /* 5312 * zfs release [-r] <tag> <snap> ... 5313 * 5314 * -r Recursively release 5315 * 5316 * Release a user-hold with the given tag from the list of snapshots. 5317 */ 5318 static int 5319 zfs_do_release(int argc, char **argv) 5320 { 5321 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE)); 5322 } 5323 5324 typedef struct holds_cbdata { 5325 boolean_t cb_recursive; 5326 const char *cb_snapname; 5327 nvlist_t **cb_nvlp; 5328 size_t cb_max_namelen; 5329 size_t cb_max_taglen; 5330 } holds_cbdata_t; 5331 5332 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y" 5333 #define DATETIME_BUF_LEN (32) 5334 /* 5335 * 5336 */ 5337 static void 5338 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl) 5339 { 5340 int i; 5341 nvpair_t *nvp = NULL; 5342 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" }; 5343 const char *col; 5344 5345 if (!scripted) { 5346 for (i = 0; i < 3; i++) { 5347 col = gettext(hdr_cols[i]); 5348 if (i < 2) 5349 (void) printf("%-*s ", i ? tagwidth : nwidth, 5350 col); 5351 else 5352 (void) printf("%s\n", col); 5353 } 5354 } 5355 5356 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 5357 char *zname = nvpair_name(nvp); 5358 nvlist_t *nvl2; 5359 nvpair_t *nvp2 = NULL; 5360 (void) nvpair_value_nvlist(nvp, &nvl2); 5361 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) { 5362 char tsbuf[DATETIME_BUF_LEN]; 5363 char *tagname = nvpair_name(nvp2); 5364 uint64_t val = 0; 5365 time_t time; 5366 struct tm t; 5367 char sep = scripted ? '\t' : ' '; 5368 size_t sepnum = scripted ? 1 : 2; 5369 5370 (void) nvpair_value_uint64(nvp2, &val); 5371 time = (time_t)val; 5372 (void) localtime_r(&time, &t); 5373 (void) strftime(tsbuf, DATETIME_BUF_LEN, 5374 gettext(STRFTIME_FMT_STR), &t); 5375 5376 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname, 5377 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf); 5378 } 5379 } 5380 } 5381 5382 /* 5383 * Generic callback function to list a dataset or snapshot. 5384 */ 5385 static int 5386 holds_callback(zfs_handle_t *zhp, void *data) 5387 { 5388 holds_cbdata_t *cbp = data; 5389 nvlist_t *top_nvl = *cbp->cb_nvlp; 5390 nvlist_t *nvl = NULL; 5391 nvpair_t *nvp = NULL; 5392 const char *zname = zfs_get_name(zhp); 5393 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN); 5394 5395 if (cbp->cb_recursive) { 5396 const char *snapname; 5397 char *delim = strchr(zname, '@'); 5398 if (delim == NULL) 5399 return (0); 5400 5401 snapname = delim + 1; 5402 if (strcmp(cbp->cb_snapname, snapname)) 5403 return (0); 5404 } 5405 5406 if (zfs_get_holds(zhp, &nvl) != 0) 5407 return (-1); 5408 5409 if (znamelen > cbp->cb_max_namelen) 5410 cbp->cb_max_namelen = znamelen; 5411 5412 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 5413 const char *tag = nvpair_name(nvp); 5414 size_t taglen = strnlen(tag, MAXNAMELEN); 5415 if (taglen > cbp->cb_max_taglen) 5416 cbp->cb_max_taglen = taglen; 5417 } 5418 5419 return (nvlist_add_nvlist(top_nvl, zname, nvl)); 5420 } 5421 5422 /* 5423 * zfs holds [-r] <snap> ... 5424 * 5425 * -r Recursively hold 5426 */ 5427 static int 5428 zfs_do_holds(int argc, char **argv) 5429 { 5430 int errors = 0; 5431 int c; 5432 int i; 5433 boolean_t scripted = B_FALSE; 5434 boolean_t recursive = B_FALSE; 5435 const char *opts = "rH"; 5436 nvlist_t *nvl; 5437 5438 int types = ZFS_TYPE_SNAPSHOT; 5439 holds_cbdata_t cb = { 0 }; 5440 5441 int limit = 0; 5442 int ret = 0; 5443 int flags = 0; 5444 5445 /* check options */ 5446 while ((c = getopt(argc, argv, opts)) != -1) { 5447 switch (c) { 5448 case 'r': 5449 recursive = B_TRUE; 5450 break; 5451 case 'H': 5452 scripted = B_TRUE; 5453 break; 5454 case '?': 5455 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5456 optopt); 5457 usage(B_FALSE); 5458 } 5459 } 5460 5461 if (recursive) { 5462 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME; 5463 flags |= ZFS_ITER_RECURSE; 5464 } 5465 5466 argc -= optind; 5467 argv += optind; 5468 5469 /* check number of arguments */ 5470 if (argc < 1) 5471 usage(B_FALSE); 5472 5473 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 5474 nomem(); 5475 5476 for (i = 0; i < argc; ++i) { 5477 char *snapshot = argv[i]; 5478 const char *delim; 5479 const char *snapname; 5480 5481 delim = strchr(snapshot, '@'); 5482 if (delim == NULL) { 5483 (void) fprintf(stderr, 5484 gettext("'%s' is not a snapshot\n"), snapshot); 5485 ++errors; 5486 continue; 5487 } 5488 snapname = delim + 1; 5489 if (recursive) 5490 snapshot[delim - snapshot] = '\0'; 5491 5492 cb.cb_recursive = recursive; 5493 cb.cb_snapname = snapname; 5494 cb.cb_nvlp = &nvl; 5495 5496 /* 5497 * 1. collect holds data, set format options 5498 */ 5499 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit, 5500 holds_callback, &cb); 5501 if (ret != 0) 5502 ++errors; 5503 } 5504 5505 /* 5506 * 2. print holds data 5507 */ 5508 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl); 5509 5510 if (nvlist_empty(nvl)) 5511 (void) printf(gettext("no datasets available\n")); 5512 5513 nvlist_free(nvl); 5514 5515 return (0 != errors); 5516 } 5517 5518 #define CHECK_SPINNER 30 5519 #define SPINNER_TIME 3 /* seconds */ 5520 #define MOUNT_TIME 5 /* seconds */ 5521 5522 static int 5523 get_one_dataset(zfs_handle_t *zhp, void *data) 5524 { 5525 static char *spin[] = { "-", "\\", "|", "/" }; 5526 static int spinval = 0; 5527 static int spincheck = 0; 5528 static time_t last_spin_time = (time_t)0; 5529 get_all_cb_t *cbp = data; 5530 zfs_type_t type = zfs_get_type(zhp); 5531 5532 if (cbp->cb_verbose) { 5533 if (--spincheck < 0) { 5534 time_t now = time(NULL); 5535 if (last_spin_time + SPINNER_TIME < now) { 5536 update_progress(spin[spinval++ % 4]); 5537 last_spin_time = now; 5538 } 5539 spincheck = CHECK_SPINNER; 5540 } 5541 } 5542 5543 /* 5544 * Interate over any nested datasets. 5545 */ 5546 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) { 5547 zfs_close(zhp); 5548 return (1); 5549 } 5550 5551 /* 5552 * Skip any datasets whose type does not match. 5553 */ 5554 if ((type & ZFS_TYPE_FILESYSTEM) == 0) { 5555 zfs_close(zhp); 5556 return (0); 5557 } 5558 libzfs_add_handle(cbp, zhp); 5559 assert(cbp->cb_used <= cbp->cb_alloc); 5560 5561 return (0); 5562 } 5563 5564 static void 5565 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose) 5566 { 5567 get_all_cb_t cb = { 0 }; 5568 cb.cb_verbose = verbose; 5569 cb.cb_getone = get_one_dataset; 5570 5571 if (verbose) 5572 set_progress_header(gettext("Reading ZFS config")); 5573 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb); 5574 5575 *dslist = cb.cb_handles; 5576 *count = cb.cb_used; 5577 5578 if (verbose) 5579 finish_progress(gettext("done.")); 5580 } 5581 5582 /* 5583 * Generic callback for sharing or mounting filesystems. Because the code is so 5584 * similar, we have a common function with an extra parameter to determine which 5585 * mode we are using. 5586 */ 5587 #define OP_SHARE 0x1 5588 #define OP_MOUNT 0x2 5589 5590 /* 5591 * Share or mount a dataset. 5592 */ 5593 static int 5594 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol, 5595 boolean_t explicit, const char *options) 5596 { 5597 char mountpoint[ZFS_MAXPROPLEN]; 5598 char shareopts[ZFS_MAXPROPLEN]; 5599 char smbshareopts[ZFS_MAXPROPLEN]; 5600 const char *cmdname = op == OP_SHARE ? "share" : "mount"; 5601 struct mnttab mnt; 5602 uint64_t zoned, canmount; 5603 boolean_t shared_nfs, shared_smb; 5604 5605 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM); 5606 5607 /* 5608 * Check to make sure we can mount/share this dataset. If we 5609 * are in the global zone and the filesystem is exported to a 5610 * local zone, or if we are in a local zone and the 5611 * filesystem is not exported, then it is an error. 5612 */ 5613 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 5614 5615 if (zoned && getzoneid() == GLOBAL_ZONEID) { 5616 if (!explicit) 5617 return (0); 5618 5619 (void) fprintf(stderr, gettext("cannot %s '%s': " 5620 "dataset is exported to a local zone\n"), cmdname, 5621 zfs_get_name(zhp)); 5622 return (1); 5623 5624 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) { 5625 if (!explicit) 5626 return (0); 5627 5628 (void) fprintf(stderr, gettext("cannot %s '%s': " 5629 "permission denied\n"), cmdname, 5630 zfs_get_name(zhp)); 5631 return (1); 5632 } 5633 5634 /* 5635 * Ignore any filesystems which don't apply to us. This 5636 * includes those with a legacy mountpoint, or those with 5637 * legacy share options. 5638 */ 5639 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 5640 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0); 5641 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, 5642 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0); 5643 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts, 5644 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0); 5645 5646 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 && 5647 strcmp(smbshareopts, "off") == 0) { 5648 if (!explicit) 5649 return (0); 5650 5651 (void) fprintf(stderr, gettext("cannot share '%s': " 5652 "legacy share\n"), zfs_get_name(zhp)); 5653 (void) fprintf(stderr, gettext("use share(1M) to " 5654 "share this filesystem, or set " 5655 "sharenfs property on\n")); 5656 return (1); 5657 } 5658 5659 /* 5660 * We cannot share or mount legacy filesystems. If the 5661 * shareopts is non-legacy but the mountpoint is legacy, we 5662 * treat it as a legacy share. 5663 */ 5664 if (strcmp(mountpoint, "legacy") == 0) { 5665 if (!explicit) 5666 return (0); 5667 5668 (void) fprintf(stderr, gettext("cannot %s '%s': " 5669 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp)); 5670 (void) fprintf(stderr, gettext("use %s(1M) to " 5671 "%s this filesystem\n"), cmdname, cmdname); 5672 return (1); 5673 } 5674 5675 if (strcmp(mountpoint, "none") == 0) { 5676 if (!explicit) 5677 return (0); 5678 5679 (void) fprintf(stderr, gettext("cannot %s '%s': no " 5680 "mountpoint set\n"), cmdname, zfs_get_name(zhp)); 5681 return (1); 5682 } 5683 5684 /* 5685 * canmount explicit outcome 5686 * on no pass through 5687 * on yes pass through 5688 * off no return 0 5689 * off yes display error, return 1 5690 * noauto no return 0 5691 * noauto yes pass through 5692 */ 5693 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT); 5694 if (canmount == ZFS_CANMOUNT_OFF) { 5695 if (!explicit) 5696 return (0); 5697 5698 (void) fprintf(stderr, gettext("cannot %s '%s': " 5699 "'canmount' property is set to 'off'\n"), cmdname, 5700 zfs_get_name(zhp)); 5701 return (1); 5702 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) { 5703 return (0); 5704 } 5705 5706 /* 5707 * At this point, we have verified that the mountpoint and/or 5708 * shareopts are appropriate for auto management. If the 5709 * filesystem is already mounted or shared, return (failing 5710 * for explicit requests); otherwise mount or share the 5711 * filesystem. 5712 */ 5713 switch (op) { 5714 case OP_SHARE: 5715 5716 shared_nfs = zfs_is_shared_nfs(zhp, NULL); 5717 shared_smb = zfs_is_shared_smb(zhp, NULL); 5718 5719 if (shared_nfs && shared_smb || 5720 (shared_nfs && strcmp(shareopts, "on") == 0 && 5721 strcmp(smbshareopts, "off") == 0) || 5722 (shared_smb && strcmp(smbshareopts, "on") == 0 && 5723 strcmp(shareopts, "off") == 0)) { 5724 if (!explicit) 5725 return (0); 5726 5727 (void) fprintf(stderr, gettext("cannot share " 5728 "'%s': filesystem already shared\n"), 5729 zfs_get_name(zhp)); 5730 return (1); 5731 } 5732 5733 if (!zfs_is_mounted(zhp, NULL) && 5734 zfs_mount(zhp, NULL, 0) != 0) 5735 return (1); 5736 5737 if (protocol == NULL) { 5738 if (zfs_shareall(zhp) != 0) 5739 return (1); 5740 } else if (strcmp(protocol, "nfs") == 0) { 5741 if (zfs_share_nfs(zhp)) 5742 return (1); 5743 } else if (strcmp(protocol, "smb") == 0) { 5744 if (zfs_share_smb(zhp)) 5745 return (1); 5746 } else { 5747 (void) fprintf(stderr, gettext("cannot share " 5748 "'%s': invalid share type '%s' " 5749 "specified\n"), 5750 zfs_get_name(zhp), protocol); 5751 return (1); 5752 } 5753 5754 break; 5755 5756 case OP_MOUNT: 5757 if (options == NULL) 5758 mnt.mnt_mntopts = ""; 5759 else 5760 mnt.mnt_mntopts = (char *)options; 5761 5762 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) && 5763 zfs_is_mounted(zhp, NULL)) { 5764 if (!explicit) 5765 return (0); 5766 5767 (void) fprintf(stderr, gettext("cannot mount " 5768 "'%s': filesystem already mounted\n"), 5769 zfs_get_name(zhp)); 5770 return (1); 5771 } 5772 5773 if (zfs_mount(zhp, options, flags) != 0) 5774 return (1); 5775 break; 5776 } 5777 5778 return (0); 5779 } 5780 5781 /* 5782 * Reports progress in the form "(current/total)". Not thread-safe. 5783 */ 5784 static void 5785 report_mount_progress(int current, int total) 5786 { 5787 static time_t last_progress_time = 0; 5788 time_t now = time(NULL); 5789 char info[32]; 5790 5791 /* report 1..n instead of 0..n-1 */ 5792 ++current; 5793 5794 /* display header if we're here for the first time */ 5795 if (current == 1) { 5796 set_progress_header(gettext("Mounting ZFS filesystems")); 5797 } else if (current != total && last_progress_time + MOUNT_TIME >= now) { 5798 /* too soon to report again */ 5799 return; 5800 } 5801 5802 last_progress_time = now; 5803 5804 (void) sprintf(info, "(%d/%d)", current, total); 5805 5806 if (current == total) 5807 finish_progress(info); 5808 else 5809 update_progress(info); 5810 } 5811 5812 static void 5813 append_options(char *mntopts, char *newopts) 5814 { 5815 int len = strlen(mntopts); 5816 5817 /* original length plus new string to append plus 1 for the comma */ 5818 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) { 5819 (void) fprintf(stderr, gettext("the opts argument for " 5820 "'%c' option is too long (more than %d chars)\n"), 5821 "-o", MNT_LINE_MAX); 5822 usage(B_FALSE); 5823 } 5824 5825 if (*mntopts) 5826 mntopts[len++] = ','; 5827 5828 (void) strcpy(&mntopts[len], newopts); 5829 } 5830 5831 static int 5832 share_mount(int op, int argc, char **argv) 5833 { 5834 int do_all = 0; 5835 boolean_t verbose = B_FALSE; 5836 int c, ret = 0; 5837 char *options = NULL; 5838 int flags = 0; 5839 5840 /* check options */ 5841 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a")) 5842 != -1) { 5843 switch (c) { 5844 case 'a': 5845 do_all = 1; 5846 break; 5847 case 'v': 5848 verbose = B_TRUE; 5849 break; 5850 case 'o': 5851 if (*optarg == '\0') { 5852 (void) fprintf(stderr, gettext("empty mount " 5853 "options (-o) specified\n")); 5854 usage(B_FALSE); 5855 } 5856 5857 if (options == NULL) 5858 options = safe_malloc(MNT_LINE_MAX + 1); 5859 5860 /* option validation is done later */ 5861 append_options(options, optarg); 5862 break; 5863 5864 case 'O': 5865 flags |= MS_OVERLAY; 5866 break; 5867 case ':': 5868 (void) fprintf(stderr, gettext("missing argument for " 5869 "'%c' option\n"), optopt); 5870 usage(B_FALSE); 5871 break; 5872 case '?': 5873 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5874 optopt); 5875 usage(B_FALSE); 5876 } 5877 } 5878 5879 argc -= optind; 5880 argv += optind; 5881 5882 /* check number of arguments */ 5883 if (do_all) { 5884 zfs_handle_t **dslist = NULL; 5885 size_t i, count = 0; 5886 char *protocol = NULL; 5887 5888 if (op == OP_SHARE && argc > 0) { 5889 if (strcmp(argv[0], "nfs") != 0 && 5890 strcmp(argv[0], "smb") != 0) { 5891 (void) fprintf(stderr, gettext("share type " 5892 "must be 'nfs' or 'smb'\n")); 5893 usage(B_FALSE); 5894 } 5895 protocol = argv[0]; 5896 argc--; 5897 argv++; 5898 } 5899 5900 if (argc != 0) { 5901 (void) fprintf(stderr, gettext("too many arguments\n")); 5902 usage(B_FALSE); 5903 } 5904 5905 start_progress_timer(); 5906 get_all_datasets(&dslist, &count, verbose); 5907 5908 if (count == 0) 5909 return (0); 5910 5911 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp); 5912 5913 for (i = 0; i < count; i++) { 5914 if (verbose) 5915 report_mount_progress(i, count); 5916 5917 if (share_mount_one(dslist[i], op, flags, protocol, 5918 B_FALSE, options) != 0) 5919 ret = 1; 5920 zfs_close(dslist[i]); 5921 } 5922 5923 free(dslist); 5924 } else if (argc == 0) { 5925 struct mnttab entry; 5926 5927 if ((op == OP_SHARE) || (options != NULL)) { 5928 (void) fprintf(stderr, gettext("missing filesystem " 5929 "argument (specify -a for all)\n")); 5930 usage(B_FALSE); 5931 } 5932 5933 /* 5934 * When mount is given no arguments, go through /etc/mnttab and 5935 * display any active ZFS mounts. We hide any snapshots, since 5936 * they are controlled automatically. 5937 */ 5938 rewind(mnttab_file); 5939 while (getmntent(mnttab_file, &entry) == 0) { 5940 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 || 5941 strchr(entry.mnt_special, '@') != NULL) 5942 continue; 5943 5944 (void) printf("%-30s %s\n", entry.mnt_special, 5945 entry.mnt_mountp); 5946 } 5947 5948 } else { 5949 zfs_handle_t *zhp; 5950 5951 if (argc > 1) { 5952 (void) fprintf(stderr, 5953 gettext("too many arguments\n")); 5954 usage(B_FALSE); 5955 } 5956 5957 if ((zhp = zfs_open(g_zfs, argv[0], 5958 ZFS_TYPE_FILESYSTEM)) == NULL) { 5959 ret = 1; 5960 } else { 5961 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE, 5962 options); 5963 zfs_close(zhp); 5964 } 5965 } 5966 5967 return (ret); 5968 } 5969 5970 /* 5971 * zfs mount -a [nfs] 5972 * zfs mount filesystem 5973 * 5974 * Mount all filesystems, or mount the given filesystem. 5975 */ 5976 static int 5977 zfs_do_mount(int argc, char **argv) 5978 { 5979 return (share_mount(OP_MOUNT, argc, argv)); 5980 } 5981 5982 /* 5983 * zfs share -a [nfs | smb] 5984 * zfs share filesystem 5985 * 5986 * Share all filesystems, or share the given filesystem. 5987 */ 5988 static int 5989 zfs_do_share(int argc, char **argv) 5990 { 5991 return (share_mount(OP_SHARE, argc, argv)); 5992 } 5993 5994 typedef struct unshare_unmount_node { 5995 zfs_handle_t *un_zhp; 5996 char *un_mountp; 5997 uu_avl_node_t un_avlnode; 5998 } unshare_unmount_node_t; 5999 6000 /* ARGSUSED */ 6001 static int 6002 unshare_unmount_compare(const void *larg, const void *rarg, void *unused) 6003 { 6004 const unshare_unmount_node_t *l = larg; 6005 const unshare_unmount_node_t *r = rarg; 6006 6007 return (strcmp(l->un_mountp, r->un_mountp)); 6008 } 6009 6010 /* 6011 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an 6012 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem, 6013 * and unmount it appropriately. 6014 */ 6015 static int 6016 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual) 6017 { 6018 zfs_handle_t *zhp; 6019 int ret = 0; 6020 struct stat64 statbuf; 6021 struct extmnttab entry; 6022 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount"; 6023 ino_t path_inode; 6024 6025 /* 6026 * Search for the path in /etc/mnttab. Rather than looking for the 6027 * specific path, which can be fooled by non-standard paths (i.e. ".." 6028 * or "//"), we stat() the path and search for the corresponding 6029 * (major,minor) device pair. 6030 */ 6031 if (stat64(path, &statbuf) != 0) { 6032 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 6033 cmdname, path, strerror(errno)); 6034 return (1); 6035 } 6036 path_inode = statbuf.st_ino; 6037 6038 /* 6039 * Search for the given (major,minor) pair in the mount table. 6040 */ 6041 rewind(mnttab_file); 6042 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) { 6043 if (entry.mnt_major == major(statbuf.st_dev) && 6044 entry.mnt_minor == minor(statbuf.st_dev)) 6045 break; 6046 } 6047 if (ret != 0) { 6048 if (op == OP_SHARE) { 6049 (void) fprintf(stderr, gettext("cannot %s '%s': not " 6050 "currently mounted\n"), cmdname, path); 6051 return (1); 6052 } 6053 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"), 6054 path); 6055 if ((ret = umount2(path, flags)) != 0) 6056 (void) fprintf(stderr, gettext("%s: %s\n"), path, 6057 strerror(errno)); 6058 return (ret != 0); 6059 } 6060 6061 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 6062 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS " 6063 "filesystem\n"), cmdname, path); 6064 return (1); 6065 } 6066 6067 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 6068 ZFS_TYPE_FILESYSTEM)) == NULL) 6069 return (1); 6070 6071 ret = 1; 6072 if (stat64(entry.mnt_mountp, &statbuf) != 0) { 6073 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 6074 cmdname, path, strerror(errno)); 6075 goto out; 6076 } else if (statbuf.st_ino != path_inode) { 6077 (void) fprintf(stderr, gettext("cannot " 6078 "%s '%s': not a mountpoint\n"), cmdname, path); 6079 goto out; 6080 } 6081 6082 if (op == OP_SHARE) { 6083 char nfs_mnt_prop[ZFS_MAXPROPLEN]; 6084 char smbshare_prop[ZFS_MAXPROPLEN]; 6085 6086 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop, 6087 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0); 6088 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop, 6089 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0); 6090 6091 if (strcmp(nfs_mnt_prop, "off") == 0 && 6092 strcmp(smbshare_prop, "off") == 0) { 6093 (void) fprintf(stderr, gettext("cannot unshare " 6094 "'%s': legacy share\n"), path); 6095 (void) fprintf(stderr, gettext("use " 6096 "unshare(1M) to unshare this filesystem\n")); 6097 } else if (!zfs_is_shared(zhp)) { 6098 (void) fprintf(stderr, gettext("cannot unshare '%s': " 6099 "not currently shared\n"), path); 6100 } else { 6101 ret = zfs_unshareall_bypath(zhp, path); 6102 } 6103 } else { 6104 char mtpt_prop[ZFS_MAXPROPLEN]; 6105 6106 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop, 6107 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0); 6108 6109 if (is_manual) { 6110 ret = zfs_unmount(zhp, NULL, flags); 6111 } else if (strcmp(mtpt_prop, "legacy") == 0) { 6112 (void) fprintf(stderr, gettext("cannot unmount " 6113 "'%s': legacy mountpoint\n"), 6114 zfs_get_name(zhp)); 6115 (void) fprintf(stderr, gettext("use umount(1M) " 6116 "to unmount this filesystem\n")); 6117 } else { 6118 ret = zfs_unmountall(zhp, flags); 6119 } 6120 } 6121 6122 out: 6123 zfs_close(zhp); 6124 6125 return (ret != 0); 6126 } 6127 6128 /* 6129 * Generic callback for unsharing or unmounting a filesystem. 6130 */ 6131 static int 6132 unshare_unmount(int op, int argc, char **argv) 6133 { 6134 int do_all = 0; 6135 int flags = 0; 6136 int ret = 0; 6137 int c; 6138 zfs_handle_t *zhp; 6139 char nfs_mnt_prop[ZFS_MAXPROPLEN]; 6140 char sharesmb[ZFS_MAXPROPLEN]; 6141 6142 /* check options */ 6143 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) { 6144 switch (c) { 6145 case 'a': 6146 do_all = 1; 6147 break; 6148 case 'f': 6149 flags = MS_FORCE; 6150 break; 6151 case '?': 6152 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6153 optopt); 6154 usage(B_FALSE); 6155 } 6156 } 6157 6158 argc -= optind; 6159 argv += optind; 6160 6161 if (do_all) { 6162 /* 6163 * We could make use of zfs_for_each() to walk all datasets in 6164 * the system, but this would be very inefficient, especially 6165 * since we would have to linearly search /etc/mnttab for each 6166 * one. Instead, do one pass through /etc/mnttab looking for 6167 * zfs entries and call zfs_unmount() for each one. 6168 * 6169 * Things get a little tricky if the administrator has created 6170 * mountpoints beneath other ZFS filesystems. In this case, we 6171 * have to unmount the deepest filesystems first. To accomplish 6172 * this, we place all the mountpoints in an AVL tree sorted by 6173 * the special type (dataset name), and walk the result in 6174 * reverse to make sure to get any snapshots first. 6175 */ 6176 struct mnttab entry; 6177 uu_avl_pool_t *pool; 6178 uu_avl_t *tree; 6179 unshare_unmount_node_t *node; 6180 uu_avl_index_t idx; 6181 uu_avl_walk_t *walk; 6182 6183 if (argc != 0) { 6184 (void) fprintf(stderr, gettext("too many arguments\n")); 6185 usage(B_FALSE); 6186 } 6187 6188 if (((pool = uu_avl_pool_create("unmount_pool", 6189 sizeof (unshare_unmount_node_t), 6190 offsetof(unshare_unmount_node_t, un_avlnode), 6191 unshare_unmount_compare, UU_DEFAULT)) == NULL) || 6192 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL)) 6193 nomem(); 6194 6195 rewind(mnttab_file); 6196 while (getmntent(mnttab_file, &entry) == 0) { 6197 6198 /* ignore non-ZFS entries */ 6199 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 6200 continue; 6201 6202 /* ignore snapshots */ 6203 if (strchr(entry.mnt_special, '@') != NULL) 6204 continue; 6205 6206 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 6207 ZFS_TYPE_FILESYSTEM)) == NULL) { 6208 ret = 1; 6209 continue; 6210 } 6211 6212 switch (op) { 6213 case OP_SHARE: 6214 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 6215 nfs_mnt_prop, 6216 sizeof (nfs_mnt_prop), 6217 NULL, NULL, 0, B_FALSE) == 0); 6218 if (strcmp(nfs_mnt_prop, "off") != 0) 6219 break; 6220 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 6221 nfs_mnt_prop, 6222 sizeof (nfs_mnt_prop), 6223 NULL, NULL, 0, B_FALSE) == 0); 6224 if (strcmp(nfs_mnt_prop, "off") == 0) 6225 continue; 6226 break; 6227 case OP_MOUNT: 6228 /* Ignore legacy mounts */ 6229 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, 6230 nfs_mnt_prop, 6231 sizeof (nfs_mnt_prop), 6232 NULL, NULL, 0, B_FALSE) == 0); 6233 if (strcmp(nfs_mnt_prop, "legacy") == 0) 6234 continue; 6235 /* Ignore canmount=noauto mounts */ 6236 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == 6237 ZFS_CANMOUNT_NOAUTO) 6238 continue; 6239 default: 6240 break; 6241 } 6242 6243 node = safe_malloc(sizeof (unshare_unmount_node_t)); 6244 node->un_zhp = zhp; 6245 node->un_mountp = safe_strdup(entry.mnt_mountp); 6246 6247 uu_avl_node_init(node, &node->un_avlnode, pool); 6248 6249 if (uu_avl_find(tree, node, NULL, &idx) == NULL) { 6250 uu_avl_insert(tree, node, idx); 6251 } else { 6252 zfs_close(node->un_zhp); 6253 free(node->un_mountp); 6254 free(node); 6255 } 6256 } 6257 6258 /* 6259 * Walk the AVL tree in reverse, unmounting each filesystem and 6260 * removing it from the AVL tree in the process. 6261 */ 6262 if ((walk = uu_avl_walk_start(tree, 6263 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) 6264 nomem(); 6265 6266 while ((node = uu_avl_walk_next(walk)) != NULL) { 6267 uu_avl_remove(tree, node); 6268 6269 switch (op) { 6270 case OP_SHARE: 6271 if (zfs_unshareall_bypath(node->un_zhp, 6272 node->un_mountp) != 0) 6273 ret = 1; 6274 break; 6275 6276 case OP_MOUNT: 6277 if (zfs_unmount(node->un_zhp, 6278 node->un_mountp, flags) != 0) 6279 ret = 1; 6280 break; 6281 } 6282 6283 zfs_close(node->un_zhp); 6284 free(node->un_mountp); 6285 free(node); 6286 } 6287 6288 uu_avl_walk_end(walk); 6289 uu_avl_destroy(tree); 6290 uu_avl_pool_destroy(pool); 6291 6292 } else { 6293 if (argc != 1) { 6294 if (argc == 0) 6295 (void) fprintf(stderr, 6296 gettext("missing filesystem argument\n")); 6297 else 6298 (void) fprintf(stderr, 6299 gettext("too many arguments\n")); 6300 usage(B_FALSE); 6301 } 6302 6303 /* 6304 * We have an argument, but it may be a full path or a ZFS 6305 * filesystem. Pass full paths off to unmount_path() (shared by 6306 * manual_unmount), otherwise open the filesystem and pass to 6307 * zfs_unmount(). 6308 */ 6309 if (argv[0][0] == '/') 6310 return (unshare_unmount_path(op, argv[0], 6311 flags, B_FALSE)); 6312 6313 if ((zhp = zfs_open(g_zfs, argv[0], 6314 ZFS_TYPE_FILESYSTEM)) == NULL) 6315 return (1); 6316 6317 verify(zfs_prop_get(zhp, op == OP_SHARE ? 6318 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, 6319 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL, 6320 NULL, 0, B_FALSE) == 0); 6321 6322 switch (op) { 6323 case OP_SHARE: 6324 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 6325 nfs_mnt_prop, 6326 sizeof (nfs_mnt_prop), 6327 NULL, NULL, 0, B_FALSE) == 0); 6328 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 6329 sharesmb, sizeof (sharesmb), NULL, NULL, 6330 0, B_FALSE) == 0); 6331 6332 if (strcmp(nfs_mnt_prop, "off") == 0 && 6333 strcmp(sharesmb, "off") == 0) { 6334 (void) fprintf(stderr, gettext("cannot " 6335 "unshare '%s': legacy share\n"), 6336 zfs_get_name(zhp)); 6337 (void) fprintf(stderr, gettext("use " 6338 "unshare(1M) to unshare this " 6339 "filesystem\n")); 6340 ret = 1; 6341 } else if (!zfs_is_shared(zhp)) { 6342 (void) fprintf(stderr, gettext("cannot " 6343 "unshare '%s': not currently " 6344 "shared\n"), zfs_get_name(zhp)); 6345 ret = 1; 6346 } else if (zfs_unshareall(zhp) != 0) { 6347 ret = 1; 6348 } 6349 break; 6350 6351 case OP_MOUNT: 6352 if (strcmp(nfs_mnt_prop, "legacy") == 0) { 6353 (void) fprintf(stderr, gettext("cannot " 6354 "unmount '%s': legacy " 6355 "mountpoint\n"), zfs_get_name(zhp)); 6356 (void) fprintf(stderr, gettext("use " 6357 "umount(1M) to unmount this " 6358 "filesystem\n")); 6359 ret = 1; 6360 } else if (!zfs_is_mounted(zhp, NULL)) { 6361 (void) fprintf(stderr, gettext("cannot " 6362 "unmount '%s': not currently " 6363 "mounted\n"), 6364 zfs_get_name(zhp)); 6365 ret = 1; 6366 } else if (zfs_unmountall(zhp, flags) != 0) { 6367 ret = 1; 6368 } 6369 break; 6370 } 6371 6372 zfs_close(zhp); 6373 } 6374 6375 return (ret); 6376 } 6377 6378 /* 6379 * zfs unmount -a 6380 * zfs unmount filesystem 6381 * 6382 * Unmount all filesystems, or a specific ZFS filesystem. 6383 */ 6384 static int 6385 zfs_do_unmount(int argc, char **argv) 6386 { 6387 return (unshare_unmount(OP_MOUNT, argc, argv)); 6388 } 6389 6390 /* 6391 * zfs unshare -a 6392 * zfs unshare filesystem 6393 * 6394 * Unshare all filesystems, or a specific ZFS filesystem. 6395 */ 6396 static int 6397 zfs_do_unshare(int argc, char **argv) 6398 { 6399 return (unshare_unmount(OP_SHARE, argc, argv)); 6400 } 6401 6402 /* 6403 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is 6404 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'. 6405 */ 6406 static int 6407 manual_mount(int argc, char **argv) 6408 { 6409 zfs_handle_t *zhp; 6410 char mountpoint[ZFS_MAXPROPLEN]; 6411 char mntopts[MNT_LINE_MAX] = { '\0' }; 6412 int ret = 0; 6413 int c; 6414 int flags = 0; 6415 char *dataset, *path; 6416 6417 /* check options */ 6418 while ((c = getopt(argc, argv, ":mo:O")) != -1) { 6419 switch (c) { 6420 case 'o': 6421 (void) strlcpy(mntopts, optarg, sizeof (mntopts)); 6422 break; 6423 case 'O': 6424 flags |= MS_OVERLAY; 6425 break; 6426 case 'm': 6427 flags |= MS_NOMNTTAB; 6428 break; 6429 case ':': 6430 (void) fprintf(stderr, gettext("missing argument for " 6431 "'%c' option\n"), optopt); 6432 usage(B_FALSE); 6433 break; 6434 case '?': 6435 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6436 optopt); 6437 (void) fprintf(stderr, gettext("usage: mount [-o opts] " 6438 "<path>\n")); 6439 return (2); 6440 } 6441 } 6442 6443 argc -= optind; 6444 argv += optind; 6445 6446 /* check that we only have two arguments */ 6447 if (argc != 2) { 6448 if (argc == 0) 6449 (void) fprintf(stderr, gettext("missing dataset " 6450 "argument\n")); 6451 else if (argc == 1) 6452 (void) fprintf(stderr, 6453 gettext("missing mountpoint argument\n")); 6454 else 6455 (void) fprintf(stderr, gettext("too many arguments\n")); 6456 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n"); 6457 return (2); 6458 } 6459 6460 dataset = argv[0]; 6461 path = argv[1]; 6462 6463 /* try to open the dataset */ 6464 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) 6465 return (1); 6466 6467 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 6468 sizeof (mountpoint), NULL, NULL, 0, B_FALSE); 6469 6470 /* check for legacy mountpoint and complain appropriately */ 6471 ret = 0; 6472 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) { 6473 if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS, 6474 NULL, 0, mntopts, sizeof (mntopts)) != 0) { 6475 (void) fprintf(stderr, gettext("mount failed: %s\n"), 6476 strerror(errno)); 6477 ret = 1; 6478 } 6479 } else { 6480 (void) fprintf(stderr, gettext("filesystem '%s' cannot be " 6481 "mounted using 'mount -F zfs'\n"), dataset); 6482 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' " 6483 "instead.\n"), path); 6484 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' " 6485 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n")); 6486 (void) fprintf(stderr, gettext("See zfs(1M) for more " 6487 "information.\n")); 6488 ret = 1; 6489 } 6490 6491 return (ret); 6492 } 6493 6494 /* 6495 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow 6496 * unmounts of non-legacy filesystems, as this is the dominant administrative 6497 * interface. 6498 */ 6499 static int 6500 manual_unmount(int argc, char **argv) 6501 { 6502 int flags = 0; 6503 int c; 6504 6505 /* check options */ 6506 while ((c = getopt(argc, argv, "f")) != -1) { 6507 switch (c) { 6508 case 'f': 6509 flags = MS_FORCE; 6510 break; 6511 case '?': 6512 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6513 optopt); 6514 (void) fprintf(stderr, gettext("usage: unmount [-f] " 6515 "<path>\n")); 6516 return (2); 6517 } 6518 } 6519 6520 argc -= optind; 6521 argv += optind; 6522 6523 /* check arguments */ 6524 if (argc != 1) { 6525 if (argc == 0) 6526 (void) fprintf(stderr, gettext("missing path " 6527 "argument\n")); 6528 else 6529 (void) fprintf(stderr, gettext("too many arguments\n")); 6530 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n")); 6531 return (2); 6532 } 6533 6534 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE)); 6535 } 6536 6537 static int 6538 find_command_idx(char *command, int *idx) 6539 { 6540 int i; 6541 6542 for (i = 0; i < NCOMMAND; i++) { 6543 if (command_table[i].name == NULL) 6544 continue; 6545 6546 if (strcmp(command, command_table[i].name) == 0) { 6547 *idx = i; 6548 return (0); 6549 } 6550 } 6551 return (1); 6552 } 6553 6554 static int 6555 zfs_do_diff(int argc, char **argv) 6556 { 6557 zfs_handle_t *zhp; 6558 int flags = 0; 6559 char *tosnap = NULL; 6560 char *fromsnap = NULL; 6561 char *atp, *copy; 6562 int err = 0; 6563 int c; 6564 6565 while ((c = getopt(argc, argv, "FHt")) != -1) { 6566 switch (c) { 6567 case 'F': 6568 flags |= ZFS_DIFF_CLASSIFY; 6569 break; 6570 case 'H': 6571 flags |= ZFS_DIFF_PARSEABLE; 6572 break; 6573 case 't': 6574 flags |= ZFS_DIFF_TIMESTAMP; 6575 break; 6576 default: 6577 (void) fprintf(stderr, 6578 gettext("invalid option '%c'\n"), optopt); 6579 usage(B_FALSE); 6580 } 6581 } 6582 6583 argc -= optind; 6584 argv += optind; 6585 6586 if (argc < 1) { 6587 (void) fprintf(stderr, 6588 gettext("must provide at least one snapshot name\n")); 6589 usage(B_FALSE); 6590 } 6591 6592 if (argc > 2) { 6593 (void) fprintf(stderr, gettext("too many arguments\n")); 6594 usage(B_FALSE); 6595 } 6596 6597 fromsnap = argv[0]; 6598 tosnap = (argc == 2) ? argv[1] : NULL; 6599 6600 copy = NULL; 6601 if (*fromsnap != '@') 6602 copy = strdup(fromsnap); 6603 else if (tosnap) 6604 copy = strdup(tosnap); 6605 if (copy == NULL) 6606 usage(B_FALSE); 6607 6608 if (atp = strchr(copy, '@')) 6609 *atp = '\0'; 6610 6611 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) 6612 return (1); 6613 6614 free(copy); 6615 6616 /* 6617 * Ignore SIGPIPE so that the library can give us 6618 * information on any failure 6619 */ 6620 (void) sigignore(SIGPIPE); 6621 6622 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags); 6623 6624 zfs_close(zhp); 6625 6626 return (err != 0); 6627 } 6628 6629 /* 6630 * zfs bookmark <fs@snap> <fs#bmark> 6631 * 6632 * Creates a bookmark with the given name from the given snapshot. 6633 */ 6634 static int 6635 zfs_do_bookmark(int argc, char **argv) 6636 { 6637 char snapname[ZFS_MAXNAMELEN]; 6638 zfs_handle_t *zhp; 6639 nvlist_t *nvl; 6640 int ret = 0; 6641 int c; 6642 6643 /* check options */ 6644 while ((c = getopt(argc, argv, "")) != -1) { 6645 switch (c) { 6646 case '?': 6647 (void) fprintf(stderr, 6648 gettext("invalid option '%c'\n"), optopt); 6649 goto usage; 6650 } 6651 } 6652 6653 argc -= optind; 6654 argv += optind; 6655 6656 /* check number of arguments */ 6657 if (argc < 1) { 6658 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 6659 goto usage; 6660 } 6661 if (argc < 2) { 6662 (void) fprintf(stderr, gettext("missing bookmark argument\n")); 6663 goto usage; 6664 } 6665 6666 if (strchr(argv[1], '#') == NULL) { 6667 (void) fprintf(stderr, 6668 gettext("invalid bookmark name '%s' -- " 6669 "must contain a '#'\n"), argv[1]); 6670 goto usage; 6671 } 6672 6673 if (argv[0][0] == '@') { 6674 /* 6675 * Snapshot name begins with @. 6676 * Default to same fs as bookmark. 6677 */ 6678 (void) strncpy(snapname, argv[1], sizeof (snapname)); 6679 *strchr(snapname, '#') = '\0'; 6680 (void) strlcat(snapname, argv[0], sizeof (snapname)); 6681 } else { 6682 (void) strncpy(snapname, argv[0], sizeof (snapname)); 6683 } 6684 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT); 6685 if (zhp == NULL) 6686 goto usage; 6687 zfs_close(zhp); 6688 6689 6690 nvl = fnvlist_alloc(); 6691 fnvlist_add_string(nvl, argv[1], snapname); 6692 ret = lzc_bookmark(nvl, NULL); 6693 fnvlist_free(nvl); 6694 6695 if (ret != 0) { 6696 const char *err_msg; 6697 char errbuf[1024]; 6698 6699 (void) snprintf(errbuf, sizeof (errbuf), 6700 dgettext(TEXT_DOMAIN, 6701 "cannot create bookmark '%s'"), argv[1]); 6702 6703 switch (ret) { 6704 case EXDEV: 6705 err_msg = "bookmark is in a different pool"; 6706 break; 6707 case EEXIST: 6708 err_msg = "bookmark exists"; 6709 break; 6710 case EINVAL: 6711 err_msg = "invalid argument"; 6712 break; 6713 case ENOTSUP: 6714 err_msg = "bookmark feature not enabled"; 6715 break; 6716 case ENOSPC: 6717 err_msg = "out of space"; 6718 break; 6719 default: 6720 err_msg = "unknown error"; 6721 break; 6722 } 6723 (void) fprintf(stderr, "%s: %s\n", errbuf, 6724 dgettext(TEXT_DOMAIN, err_msg)); 6725 } 6726 6727 return (ret != 0); 6728 6729 usage: 6730 usage(B_FALSE); 6731 return (-1); 6732 } 6733 6734 int 6735 main(int argc, char **argv) 6736 { 6737 int ret = 0; 6738 int i; 6739 char *progname; 6740 char *cmdname; 6741 6742 (void) setlocale(LC_ALL, ""); 6743 (void) textdomain(TEXT_DOMAIN); 6744 6745 opterr = 0; 6746 6747 if ((g_zfs = libzfs_init()) == NULL) { 6748 (void) fprintf(stderr, gettext("internal error: failed to " 6749 "initialize ZFS library\n")); 6750 return (1); 6751 } 6752 6753 zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 6754 6755 libzfs_print_on_error(g_zfs, B_TRUE); 6756 6757 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) { 6758 (void) fprintf(stderr, gettext("internal error: unable to " 6759 "open %s\n"), MNTTAB); 6760 return (1); 6761 } 6762 6763 /* 6764 * This command also doubles as the /etc/fs mount and unmount program. 6765 * Determine if we should take this behavior based on argv[0]. 6766 */ 6767 progname = basename(argv[0]); 6768 if (strcmp(progname, "mount") == 0) { 6769 ret = manual_mount(argc, argv); 6770 } else if (strcmp(progname, "umount") == 0) { 6771 ret = manual_unmount(argc, argv); 6772 } else { 6773 /* 6774 * Make sure the user has specified some command. 6775 */ 6776 if (argc < 2) { 6777 (void) fprintf(stderr, gettext("missing command\n")); 6778 usage(B_FALSE); 6779 } 6780 6781 cmdname = argv[1]; 6782 6783 /* 6784 * The 'umount' command is an alias for 'unmount' 6785 */ 6786 if (strcmp(cmdname, "umount") == 0) 6787 cmdname = "unmount"; 6788 6789 /* 6790 * The 'recv' command is an alias for 'receive' 6791 */ 6792 if (strcmp(cmdname, "recv") == 0) 6793 cmdname = "receive"; 6794 6795 /* 6796 * The 'snap' command is an alias for 'snapshot' 6797 */ 6798 if (strcmp(cmdname, "snap") == 0) 6799 cmdname = "snapshot"; 6800 6801 /* 6802 * Special case '-?' 6803 */ 6804 if (strcmp(cmdname, "-?") == 0) 6805 usage(B_TRUE); 6806 6807 /* 6808 * Run the appropriate command. 6809 */ 6810 libzfs_mnttab_cache(g_zfs, B_TRUE); 6811 if (find_command_idx(cmdname, &i) == 0) { 6812 current_command = &command_table[i]; 6813 ret = command_table[i].func(argc - 1, argv + 1); 6814 } else if (strchr(cmdname, '=') != NULL) { 6815 verify(find_command_idx("set", &i) == 0); 6816 current_command = &command_table[i]; 6817 ret = command_table[i].func(argc, argv); 6818 } else { 6819 (void) fprintf(stderr, gettext("unrecognized " 6820 "command '%s'\n"), cmdname); 6821 usage(B_FALSE); 6822 } 6823 libzfs_mnttab_cache(g_zfs, B_FALSE); 6824 } 6825 6826 (void) fclose(mnttab_file); 6827 6828 if (ret == 0 && log_history) 6829 (void) zpool_log_history(g_zfs, history_str); 6830 6831 libzfs_fini(g_zfs); 6832 6833 /* 6834 * The 'ZFS_ABORT' environment variable causes us to dump core on exit 6835 * for the purposes of running ::findleaks. 6836 */ 6837 if (getenv("ZFS_ABORT") != NULL) { 6838 (void) printf("dumping core by request\n"); 6839 abort(); 6840 } 6841 6842 return (ret); 6843 } 6844