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