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