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