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 /* 3773 * If a parameter was not passed, optopt contains the 3774 * value that would normally lead us into the 3775 * appropriate case statement. If it's > 256, then this 3776 * must be a longopt and we should look at argv to get 3777 * the string. Otherwise it's just the character, so we 3778 * should use it directly. 3779 */ 3780 if (optopt <= UINT8_MAX) { 3781 (void) fprintf(stderr, 3782 gettext("missing argument for '%c' " 3783 "option\n"), optopt); 3784 } else { 3785 (void) fprintf(stderr, 3786 gettext("missing argument for '%s' " 3787 "option\n"), argv[optind - 1]); 3788 } 3789 usage(B_FALSE); 3790 break; 3791 case '?': 3792 /*FALLTHROUGH*/ 3793 default: 3794 /* 3795 * If an invalid flag was passed, optopt contains the 3796 * character if it was a short flag, or 0 if it was a 3797 * longopt. 3798 */ 3799 if (optopt != 0) { 3800 (void) fprintf(stderr, 3801 gettext("invalid option '%c'\n"), optopt); 3802 } else { 3803 (void) fprintf(stderr, 3804 gettext("invalid option '%s'\n"), 3805 argv[optind - 1]); 3806 3807 } 3808 usage(B_FALSE); 3809 } 3810 } 3811 3812 argc -= optind; 3813 argv += optind; 3814 3815 if (resume_token != NULL) { 3816 if (fromname != NULL || flags.replicate || flags.props || 3817 flags.dedup) { 3818 (void) fprintf(stderr, 3819 gettext("invalid flags combined with -t\n")); 3820 usage(B_FALSE); 3821 } 3822 if (argc != 0) { 3823 (void) fprintf(stderr, gettext("no additional " 3824 "arguments are permitted with -t\n")); 3825 usage(B_FALSE); 3826 } 3827 } else { 3828 if (argc < 1) { 3829 (void) fprintf(stderr, 3830 gettext("missing snapshot argument\n")); 3831 usage(B_FALSE); 3832 } 3833 if (argc > 1) { 3834 (void) fprintf(stderr, gettext("too many arguments\n")); 3835 usage(B_FALSE); 3836 } 3837 } 3838 3839 if (!flags.dryrun && isatty(STDOUT_FILENO)) { 3840 (void) fprintf(stderr, 3841 gettext("Error: Stream can not be written to a terminal.\n" 3842 "You must redirect standard output.\n")); 3843 return (1); 3844 } 3845 3846 if (resume_token != NULL) { 3847 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO, 3848 resume_token)); 3849 } 3850 3851 /* 3852 * Special case sending a filesystem, or from a bookmark. 3853 */ 3854 if (strchr(argv[0], '@') == NULL || 3855 (fromname && strchr(fromname, '#') != NULL)) { 3856 char frombuf[ZFS_MAX_DATASET_NAME_LEN]; 3857 enum lzc_send_flags lzc_flags = 0; 3858 3859 if (flags.replicate || flags.doall || flags.props || 3860 flags.dedup || flags.dryrun || flags.verbose || 3861 flags.progress) { 3862 (void) fprintf(stderr, 3863 gettext("Error: " 3864 "Unsupported flag with filesystem or bookmark.\n")); 3865 return (1); 3866 } 3867 3868 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET); 3869 if (zhp == NULL) 3870 return (1); 3871 3872 if (flags.largeblock) 3873 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK; 3874 if (flags.embed_data) 3875 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA; 3876 if (flags.compress) 3877 lzc_flags |= LZC_SEND_FLAG_COMPRESS; 3878 3879 if (fromname != NULL && 3880 (fromname[0] == '#' || fromname[0] == '@')) { 3881 /* 3882 * Incremental source name begins with # or @. 3883 * Default to same fs as target. 3884 */ 3885 (void) strncpy(frombuf, argv[0], sizeof (frombuf)); 3886 cp = strchr(frombuf, '@'); 3887 if (cp != NULL) 3888 *cp = '\0'; 3889 (void) strlcat(frombuf, fromname, sizeof (frombuf)); 3890 fromname = frombuf; 3891 } 3892 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags); 3893 zfs_close(zhp); 3894 return (err != 0); 3895 } 3896 3897 cp = strchr(argv[0], '@'); 3898 *cp = '\0'; 3899 toname = cp + 1; 3900 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3901 if (zhp == NULL) 3902 return (1); 3903 3904 /* 3905 * If they specified the full path to the snapshot, chop off 3906 * everything except the short name of the snapshot, but special 3907 * case if they specify the origin. 3908 */ 3909 if (fromname && (cp = strchr(fromname, '@')) != NULL) { 3910 char origin[ZFS_MAX_DATASET_NAME_LEN]; 3911 zprop_source_t src; 3912 3913 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN, 3914 origin, sizeof (origin), &src, NULL, 0, B_FALSE); 3915 3916 if (strcmp(origin, fromname) == 0) { 3917 fromname = NULL; 3918 flags.fromorigin = B_TRUE; 3919 } else { 3920 *cp = '\0'; 3921 if (cp != fromname && strcmp(argv[0], fromname)) { 3922 (void) fprintf(stderr, 3923 gettext("incremental source must be " 3924 "in same filesystem\n")); 3925 usage(B_FALSE); 3926 } 3927 fromname = cp + 1; 3928 if (strchr(fromname, '@') || strchr(fromname, '/')) { 3929 (void) fprintf(stderr, 3930 gettext("invalid incremental source\n")); 3931 usage(B_FALSE); 3932 } 3933 } 3934 } 3935 3936 if (flags.replicate && fromname == NULL) 3937 flags.doall = B_TRUE; 3938 3939 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0, 3940 extraverbose ? &dbgnv : NULL); 3941 3942 if (extraverbose && dbgnv != NULL) { 3943 /* 3944 * dump_nvlist prints to stdout, but that's been 3945 * redirected to a file. Make it print to stderr 3946 * instead. 3947 */ 3948 (void) dup2(STDERR_FILENO, STDOUT_FILENO); 3949 dump_nvlist(dbgnv, 0); 3950 nvlist_free(dbgnv); 3951 } 3952 zfs_close(zhp); 3953 3954 return (err != 0); 3955 } 3956 3957 /* 3958 * Restore a backup stream from stdin. 3959 */ 3960 static int 3961 zfs_do_receive(int argc, char **argv) 3962 { 3963 int c, err = 0; 3964 recvflags_t flags = { 0 }; 3965 boolean_t abort_resumable = B_FALSE; 3966 3967 nvlist_t *props; 3968 nvpair_t *nvp = NULL; 3969 3970 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 3971 nomem(); 3972 3973 /* check options */ 3974 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) { 3975 switch (c) { 3976 case 'o': 3977 if (parseprop(props, optarg) != 0) 3978 return (1); 3979 break; 3980 case 'd': 3981 flags.isprefix = B_TRUE; 3982 break; 3983 case 'e': 3984 flags.isprefix = B_TRUE; 3985 flags.istail = B_TRUE; 3986 break; 3987 case 'n': 3988 flags.dryrun = B_TRUE; 3989 break; 3990 case 'u': 3991 flags.nomount = B_TRUE; 3992 break; 3993 case 'v': 3994 flags.verbose = B_TRUE; 3995 break; 3996 case 's': 3997 flags.resumable = B_TRUE; 3998 break; 3999 case 'F': 4000 flags.force = B_TRUE; 4001 break; 4002 case 'A': 4003 abort_resumable = B_TRUE; 4004 break; 4005 case ':': 4006 (void) fprintf(stderr, gettext("missing argument for " 4007 "'%c' option\n"), optopt); 4008 usage(B_FALSE); 4009 break; 4010 case '?': 4011 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4012 optopt); 4013 usage(B_FALSE); 4014 } 4015 } 4016 4017 argc -= optind; 4018 argv += optind; 4019 4020 /* check number of arguments */ 4021 if (argc < 1) { 4022 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 4023 usage(B_FALSE); 4024 } 4025 if (argc > 1) { 4026 (void) fprintf(stderr, gettext("too many arguments\n")); 4027 usage(B_FALSE); 4028 } 4029 4030 while ((nvp = nvlist_next_nvpair(props, nvp))) { 4031 if (strcmp(nvpair_name(nvp), "origin") != 0) { 4032 (void) fprintf(stderr, gettext("invalid option")); 4033 usage(B_FALSE); 4034 } 4035 } 4036 4037 if (abort_resumable) { 4038 if (flags.isprefix || flags.istail || flags.dryrun || 4039 flags.resumable || flags.nomount) { 4040 (void) fprintf(stderr, gettext("invalid option")); 4041 usage(B_FALSE); 4042 } 4043 4044 char namebuf[ZFS_MAX_DATASET_NAME_LEN]; 4045 (void) snprintf(namebuf, sizeof (namebuf), 4046 "%s/%%recv", argv[0]); 4047 4048 if (zfs_dataset_exists(g_zfs, namebuf, 4049 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) { 4050 zfs_handle_t *zhp = zfs_open(g_zfs, 4051 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 4052 if (zhp == NULL) 4053 return (1); 4054 err = zfs_destroy(zhp, B_FALSE); 4055 } else { 4056 zfs_handle_t *zhp = zfs_open(g_zfs, 4057 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 4058 if (zhp == NULL) 4059 usage(B_FALSE); 4060 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) || 4061 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 4062 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) { 4063 (void) fprintf(stderr, 4064 gettext("'%s' does not have any " 4065 "resumable receive state to abort\n"), 4066 argv[0]); 4067 return (1); 4068 } 4069 err = zfs_destroy(zhp, B_FALSE); 4070 } 4071 4072 return (err != 0); 4073 } 4074 4075 if (isatty(STDIN_FILENO)) { 4076 (void) fprintf(stderr, 4077 gettext("Error: Backup stream can not be read " 4078 "from a terminal.\n" 4079 "You must redirect standard input.\n")); 4080 return (1); 4081 } 4082 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL); 4083 4084 return (err != 0); 4085 } 4086 4087 /* 4088 * allow/unallow stuff 4089 */ 4090 /* copied from zfs/sys/dsl_deleg.h */ 4091 #define ZFS_DELEG_PERM_CREATE "create" 4092 #define ZFS_DELEG_PERM_DESTROY "destroy" 4093 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot" 4094 #define ZFS_DELEG_PERM_ROLLBACK "rollback" 4095 #define ZFS_DELEG_PERM_CLONE "clone" 4096 #define ZFS_DELEG_PERM_PROMOTE "promote" 4097 #define ZFS_DELEG_PERM_RENAME "rename" 4098 #define ZFS_DELEG_PERM_MOUNT "mount" 4099 #define ZFS_DELEG_PERM_SHARE "share" 4100 #define ZFS_DELEG_PERM_SEND "send" 4101 #define ZFS_DELEG_PERM_RECEIVE "receive" 4102 #define ZFS_DELEG_PERM_ALLOW "allow" 4103 #define ZFS_DELEG_PERM_USERPROP "userprop" 4104 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */ 4105 #define ZFS_DELEG_PERM_USERQUOTA "userquota" 4106 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota" 4107 #define ZFS_DELEG_PERM_USERUSED "userused" 4108 #define ZFS_DELEG_PERM_GROUPUSED "groupused" 4109 #define ZFS_DELEG_PERM_HOLD "hold" 4110 #define ZFS_DELEG_PERM_RELEASE "release" 4111 #define ZFS_DELEG_PERM_DIFF "diff" 4112 #define ZFS_DELEG_PERM_BOOKMARK "bookmark" 4113 4114 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE 4115 4116 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = { 4117 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW }, 4118 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE }, 4119 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE }, 4120 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY }, 4121 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF}, 4122 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD }, 4123 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT }, 4124 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE }, 4125 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE }, 4126 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE }, 4127 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME }, 4128 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK }, 4129 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND }, 4130 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE }, 4131 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT }, 4132 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK }, 4133 4134 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA }, 4135 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED }, 4136 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP }, 4137 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA }, 4138 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED }, 4139 { NULL, ZFS_DELEG_NOTE_NONE } 4140 }; 4141 4142 /* permission structure */ 4143 typedef struct deleg_perm { 4144 zfs_deleg_who_type_t dp_who_type; 4145 const char *dp_name; 4146 boolean_t dp_local; 4147 boolean_t dp_descend; 4148 } deleg_perm_t; 4149 4150 /* */ 4151 typedef struct deleg_perm_node { 4152 deleg_perm_t dpn_perm; 4153 4154 uu_avl_node_t dpn_avl_node; 4155 } deleg_perm_node_t; 4156 4157 typedef struct fs_perm fs_perm_t; 4158 4159 /* permissions set */ 4160 typedef struct who_perm { 4161 zfs_deleg_who_type_t who_type; 4162 const char *who_name; /* id */ 4163 char who_ug_name[256]; /* user/group name */ 4164 fs_perm_t *who_fsperm; /* uplink */ 4165 4166 uu_avl_t *who_deleg_perm_avl; /* permissions */ 4167 } who_perm_t; 4168 4169 /* */ 4170 typedef struct who_perm_node { 4171 who_perm_t who_perm; 4172 uu_avl_node_t who_avl_node; 4173 } who_perm_node_t; 4174 4175 typedef struct fs_perm_set fs_perm_set_t; 4176 /* fs permissions */ 4177 struct fs_perm { 4178 const char *fsp_name; 4179 4180 uu_avl_t *fsp_sc_avl; /* sets,create */ 4181 uu_avl_t *fsp_uge_avl; /* user,group,everyone */ 4182 4183 fs_perm_set_t *fsp_set; /* uplink */ 4184 }; 4185 4186 /* */ 4187 typedef struct fs_perm_node { 4188 fs_perm_t fspn_fsperm; 4189 uu_avl_t *fspn_avl; 4190 4191 uu_list_node_t fspn_list_node; 4192 } fs_perm_node_t; 4193 4194 /* top level structure */ 4195 struct fs_perm_set { 4196 uu_list_pool_t *fsps_list_pool; 4197 uu_list_t *fsps_list; /* list of fs_perms */ 4198 4199 uu_avl_pool_t *fsps_named_set_avl_pool; 4200 uu_avl_pool_t *fsps_who_perm_avl_pool; 4201 uu_avl_pool_t *fsps_deleg_perm_avl_pool; 4202 }; 4203 4204 static inline const char * 4205 deleg_perm_type(zfs_deleg_note_t note) 4206 { 4207 /* subcommands */ 4208 switch (note) { 4209 /* SUBCOMMANDS */ 4210 /* OTHER */ 4211 case ZFS_DELEG_NOTE_GROUPQUOTA: 4212 case ZFS_DELEG_NOTE_GROUPUSED: 4213 case ZFS_DELEG_NOTE_USERPROP: 4214 case ZFS_DELEG_NOTE_USERQUOTA: 4215 case ZFS_DELEG_NOTE_USERUSED: 4216 /* other */ 4217 return (gettext("other")); 4218 default: 4219 return (gettext("subcommand")); 4220 } 4221 } 4222 4223 static int 4224 who_type2weight(zfs_deleg_who_type_t who_type) 4225 { 4226 int res; 4227 switch (who_type) { 4228 case ZFS_DELEG_NAMED_SET_SETS: 4229 case ZFS_DELEG_NAMED_SET: 4230 res = 0; 4231 break; 4232 case ZFS_DELEG_CREATE_SETS: 4233 case ZFS_DELEG_CREATE: 4234 res = 1; 4235 break; 4236 case ZFS_DELEG_USER_SETS: 4237 case ZFS_DELEG_USER: 4238 res = 2; 4239 break; 4240 case ZFS_DELEG_GROUP_SETS: 4241 case ZFS_DELEG_GROUP: 4242 res = 3; 4243 break; 4244 case ZFS_DELEG_EVERYONE_SETS: 4245 case ZFS_DELEG_EVERYONE: 4246 res = 4; 4247 break; 4248 default: 4249 res = -1; 4250 } 4251 4252 return (res); 4253 } 4254 4255 /* ARGSUSED */ 4256 static int 4257 who_perm_compare(const void *larg, const void *rarg, void *unused) 4258 { 4259 const who_perm_node_t *l = larg; 4260 const who_perm_node_t *r = rarg; 4261 zfs_deleg_who_type_t ltype = l->who_perm.who_type; 4262 zfs_deleg_who_type_t rtype = r->who_perm.who_type; 4263 int lweight = who_type2weight(ltype); 4264 int rweight = who_type2weight(rtype); 4265 int res = lweight - rweight; 4266 if (res == 0) 4267 res = strncmp(l->who_perm.who_name, r->who_perm.who_name, 4268 ZFS_MAX_DELEG_NAME-1); 4269 4270 if (res == 0) 4271 return (0); 4272 if (res > 0) 4273 return (1); 4274 else 4275 return (-1); 4276 } 4277 4278 /* ARGSUSED */ 4279 static int 4280 deleg_perm_compare(const void *larg, const void *rarg, void *unused) 4281 { 4282 const deleg_perm_node_t *l = larg; 4283 const deleg_perm_node_t *r = rarg; 4284 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name, 4285 ZFS_MAX_DELEG_NAME-1); 4286 4287 if (res == 0) 4288 return (0); 4289 4290 if (res > 0) 4291 return (1); 4292 else 4293 return (-1); 4294 } 4295 4296 static inline void 4297 fs_perm_set_init(fs_perm_set_t *fspset) 4298 { 4299 bzero(fspset, sizeof (fs_perm_set_t)); 4300 4301 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool", 4302 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node), 4303 NULL, UU_DEFAULT)) == NULL) 4304 nomem(); 4305 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL, 4306 UU_DEFAULT)) == NULL) 4307 nomem(); 4308 4309 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create( 4310 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof( 4311 who_perm_node_t, who_avl_node), who_perm_compare, 4312 UU_DEFAULT)) == NULL) 4313 nomem(); 4314 4315 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create( 4316 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof( 4317 who_perm_node_t, who_avl_node), who_perm_compare, 4318 UU_DEFAULT)) == NULL) 4319 nomem(); 4320 4321 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create( 4322 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof( 4323 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT)) 4324 == NULL) 4325 nomem(); 4326 } 4327 4328 static inline void fs_perm_fini(fs_perm_t *); 4329 static inline void who_perm_fini(who_perm_t *); 4330 4331 static inline void 4332 fs_perm_set_fini(fs_perm_set_t *fspset) 4333 { 4334 fs_perm_node_t *node = uu_list_first(fspset->fsps_list); 4335 4336 while (node != NULL) { 4337 fs_perm_node_t *next_node = 4338 uu_list_next(fspset->fsps_list, node); 4339 fs_perm_t *fsperm = &node->fspn_fsperm; 4340 fs_perm_fini(fsperm); 4341 uu_list_remove(fspset->fsps_list, node); 4342 free(node); 4343 node = next_node; 4344 } 4345 4346 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool); 4347 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool); 4348 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool); 4349 } 4350 4351 static inline void 4352 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type, 4353 const char *name) 4354 { 4355 deleg_perm->dp_who_type = type; 4356 deleg_perm->dp_name = name; 4357 } 4358 4359 static inline void 4360 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm, 4361 zfs_deleg_who_type_t type, const char *name) 4362 { 4363 uu_avl_pool_t *pool; 4364 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool; 4365 4366 bzero(who_perm, sizeof (who_perm_t)); 4367 4368 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL, 4369 UU_DEFAULT)) == NULL) 4370 nomem(); 4371 4372 who_perm->who_type = type; 4373 who_perm->who_name = name; 4374 who_perm->who_fsperm = fsperm; 4375 } 4376 4377 static inline void 4378 who_perm_fini(who_perm_t *who_perm) 4379 { 4380 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl); 4381 4382 while (node != NULL) { 4383 deleg_perm_node_t *next_node = 4384 uu_avl_next(who_perm->who_deleg_perm_avl, node); 4385 4386 uu_avl_remove(who_perm->who_deleg_perm_avl, node); 4387 free(node); 4388 node = next_node; 4389 } 4390 4391 uu_avl_destroy(who_perm->who_deleg_perm_avl); 4392 } 4393 4394 static inline void 4395 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname) 4396 { 4397 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool; 4398 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool; 4399 4400 bzero(fsperm, sizeof (fs_perm_t)); 4401 4402 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT)) 4403 == NULL) 4404 nomem(); 4405 4406 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT)) 4407 == NULL) 4408 nomem(); 4409 4410 fsperm->fsp_set = fspset; 4411 fsperm->fsp_name = fsname; 4412 } 4413 4414 static inline void 4415 fs_perm_fini(fs_perm_t *fsperm) 4416 { 4417 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl); 4418 while (node != NULL) { 4419 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl, 4420 node); 4421 who_perm_t *who_perm = &node->who_perm; 4422 who_perm_fini(who_perm); 4423 uu_avl_remove(fsperm->fsp_sc_avl, node); 4424 free(node); 4425 node = next_node; 4426 } 4427 4428 node = uu_avl_first(fsperm->fsp_uge_avl); 4429 while (node != NULL) { 4430 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl, 4431 node); 4432 who_perm_t *who_perm = &node->who_perm; 4433 who_perm_fini(who_perm); 4434 uu_avl_remove(fsperm->fsp_uge_avl, node); 4435 free(node); 4436 node = next_node; 4437 } 4438 4439 uu_avl_destroy(fsperm->fsp_sc_avl); 4440 uu_avl_destroy(fsperm->fsp_uge_avl); 4441 } 4442 4443 static void 4444 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node, 4445 zfs_deleg_who_type_t who_type, const char *name, char locality) 4446 { 4447 uu_avl_index_t idx = 0; 4448 4449 deleg_perm_node_t *found_node = NULL; 4450 deleg_perm_t *deleg_perm = &node->dpn_perm; 4451 4452 deleg_perm_init(deleg_perm, who_type, name); 4453 4454 if ((found_node = uu_avl_find(avl, node, NULL, &idx)) 4455 == NULL) 4456 uu_avl_insert(avl, node, idx); 4457 else { 4458 node = found_node; 4459 deleg_perm = &node->dpn_perm; 4460 } 4461 4462 4463 switch (locality) { 4464 case ZFS_DELEG_LOCAL: 4465 deleg_perm->dp_local = B_TRUE; 4466 break; 4467 case ZFS_DELEG_DESCENDENT: 4468 deleg_perm->dp_descend = B_TRUE; 4469 break; 4470 case ZFS_DELEG_NA: 4471 break; 4472 default: 4473 assert(B_FALSE); /* invalid locality */ 4474 } 4475 } 4476 4477 static inline int 4478 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality) 4479 { 4480 nvpair_t *nvp = NULL; 4481 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set; 4482 uu_avl_t *avl = who_perm->who_deleg_perm_avl; 4483 zfs_deleg_who_type_t who_type = who_perm->who_type; 4484 4485 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4486 const char *name = nvpair_name(nvp); 4487 data_type_t type = nvpair_type(nvp); 4488 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool; 4489 deleg_perm_node_t *node = 4490 safe_malloc(sizeof (deleg_perm_node_t)); 4491 4492 assert(type == DATA_TYPE_BOOLEAN); 4493 4494 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool); 4495 set_deleg_perm_node(avl, node, who_type, name, locality); 4496 } 4497 4498 return (0); 4499 } 4500 4501 static inline int 4502 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl) 4503 { 4504 nvpair_t *nvp = NULL; 4505 fs_perm_set_t *fspset = fsperm->fsp_set; 4506 4507 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4508 nvlist_t *nvl2 = NULL; 4509 const char *name = nvpair_name(nvp); 4510 uu_avl_t *avl = NULL; 4511 uu_avl_pool_t *avl_pool = NULL; 4512 zfs_deleg_who_type_t perm_type = name[0]; 4513 char perm_locality = name[1]; 4514 const char *perm_name = name + 3; 4515 boolean_t is_set = B_TRUE; 4516 who_perm_t *who_perm = NULL; 4517 4518 assert('$' == name[2]); 4519 4520 if (nvpair_value_nvlist(nvp, &nvl2) != 0) 4521 return (-1); 4522 4523 switch (perm_type) { 4524 case ZFS_DELEG_CREATE: 4525 case ZFS_DELEG_CREATE_SETS: 4526 case ZFS_DELEG_NAMED_SET: 4527 case ZFS_DELEG_NAMED_SET_SETS: 4528 avl_pool = fspset->fsps_named_set_avl_pool; 4529 avl = fsperm->fsp_sc_avl; 4530 break; 4531 case ZFS_DELEG_USER: 4532 case ZFS_DELEG_USER_SETS: 4533 case ZFS_DELEG_GROUP: 4534 case ZFS_DELEG_GROUP_SETS: 4535 case ZFS_DELEG_EVERYONE: 4536 case ZFS_DELEG_EVERYONE_SETS: 4537 avl_pool = fspset->fsps_who_perm_avl_pool; 4538 avl = fsperm->fsp_uge_avl; 4539 break; 4540 4541 default: 4542 assert(!"unhandled zfs_deleg_who_type_t"); 4543 } 4544 4545 if (is_set) { 4546 who_perm_node_t *found_node = NULL; 4547 who_perm_node_t *node = safe_malloc( 4548 sizeof (who_perm_node_t)); 4549 who_perm = &node->who_perm; 4550 uu_avl_index_t idx = 0; 4551 4552 uu_avl_node_init(node, &node->who_avl_node, avl_pool); 4553 who_perm_init(who_perm, fsperm, perm_type, perm_name); 4554 4555 if ((found_node = uu_avl_find(avl, node, NULL, &idx)) 4556 == NULL) { 4557 if (avl == fsperm->fsp_uge_avl) { 4558 uid_t rid = 0; 4559 struct passwd *p = NULL; 4560 struct group *g = NULL; 4561 const char *nice_name = NULL; 4562 4563 switch (perm_type) { 4564 case ZFS_DELEG_USER_SETS: 4565 case ZFS_DELEG_USER: 4566 rid = atoi(perm_name); 4567 p = getpwuid(rid); 4568 if (p) 4569 nice_name = p->pw_name; 4570 break; 4571 case ZFS_DELEG_GROUP_SETS: 4572 case ZFS_DELEG_GROUP: 4573 rid = atoi(perm_name); 4574 g = getgrgid(rid); 4575 if (g) 4576 nice_name = g->gr_name; 4577 break; 4578 4579 default: 4580 break; 4581 } 4582 4583 if (nice_name != NULL) 4584 (void) strlcpy( 4585 node->who_perm.who_ug_name, 4586 nice_name, 256); 4587 } 4588 4589 uu_avl_insert(avl, node, idx); 4590 } else { 4591 node = found_node; 4592 who_perm = &node->who_perm; 4593 } 4594 } 4595 4596 (void) parse_who_perm(who_perm, nvl2, perm_locality); 4597 } 4598 4599 return (0); 4600 } 4601 4602 static inline int 4603 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl) 4604 { 4605 nvpair_t *nvp = NULL; 4606 uu_avl_index_t idx = 0; 4607 4608 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4609 nvlist_t *nvl2 = NULL; 4610 const char *fsname = nvpair_name(nvp); 4611 data_type_t type = nvpair_type(nvp); 4612 fs_perm_t *fsperm = NULL; 4613 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t)); 4614 if (node == NULL) 4615 nomem(); 4616 4617 fsperm = &node->fspn_fsperm; 4618 4619 assert(DATA_TYPE_NVLIST == type); 4620 4621 uu_list_node_init(node, &node->fspn_list_node, 4622 fspset->fsps_list_pool); 4623 4624 idx = uu_list_numnodes(fspset->fsps_list); 4625 fs_perm_init(fsperm, fspset, fsname); 4626 4627 if (nvpair_value_nvlist(nvp, &nvl2) != 0) 4628 return (-1); 4629 4630 (void) parse_fs_perm(fsperm, nvl2); 4631 4632 uu_list_insert(fspset->fsps_list, node, idx); 4633 } 4634 4635 return (0); 4636 } 4637 4638 static inline const char * 4639 deleg_perm_comment(zfs_deleg_note_t note) 4640 { 4641 const char *str = ""; 4642 4643 /* subcommands */ 4644 switch (note) { 4645 /* SUBCOMMANDS */ 4646 case ZFS_DELEG_NOTE_ALLOW: 4647 str = gettext("Must also have the permission that is being" 4648 "\n\t\t\t\tallowed"); 4649 break; 4650 case ZFS_DELEG_NOTE_CLONE: 4651 str = gettext("Must also have the 'create' ability and 'mount'" 4652 "\n\t\t\t\tability in the origin file system"); 4653 break; 4654 case ZFS_DELEG_NOTE_CREATE: 4655 str = gettext("Must also have the 'mount' ability"); 4656 break; 4657 case ZFS_DELEG_NOTE_DESTROY: 4658 str = gettext("Must also have the 'mount' ability"); 4659 break; 4660 case ZFS_DELEG_NOTE_DIFF: 4661 str = gettext("Allows lookup of paths within a dataset;" 4662 "\n\t\t\t\tgiven an object number. Ordinary users need this" 4663 "\n\t\t\t\tin order to use zfs diff"); 4664 break; 4665 case ZFS_DELEG_NOTE_HOLD: 4666 str = gettext("Allows adding a user hold to a snapshot"); 4667 break; 4668 case ZFS_DELEG_NOTE_MOUNT: 4669 str = gettext("Allows mount/umount of ZFS datasets"); 4670 break; 4671 case ZFS_DELEG_NOTE_PROMOTE: 4672 str = gettext("Must also have the 'mount'\n\t\t\t\tand" 4673 " 'promote' ability in the origin file system"); 4674 break; 4675 case ZFS_DELEG_NOTE_RECEIVE: 4676 str = gettext("Must also have the 'mount' and 'create'" 4677 " ability"); 4678 break; 4679 case ZFS_DELEG_NOTE_RELEASE: 4680 str = gettext("Allows releasing a user hold which\n\t\t\t\t" 4681 "might destroy the snapshot"); 4682 break; 4683 case ZFS_DELEG_NOTE_RENAME: 4684 str = gettext("Must also have the 'mount' and 'create'" 4685 "\n\t\t\t\tability in the new parent"); 4686 break; 4687 case ZFS_DELEG_NOTE_ROLLBACK: 4688 str = gettext(""); 4689 break; 4690 case ZFS_DELEG_NOTE_SEND: 4691 str = gettext(""); 4692 break; 4693 case ZFS_DELEG_NOTE_SHARE: 4694 str = gettext("Allows sharing file systems over NFS or SMB" 4695 "\n\t\t\t\tprotocols"); 4696 break; 4697 case ZFS_DELEG_NOTE_SNAPSHOT: 4698 str = gettext(""); 4699 break; 4700 /* 4701 * case ZFS_DELEG_NOTE_VSCAN: 4702 * str = gettext(""); 4703 * break; 4704 */ 4705 /* OTHER */ 4706 case ZFS_DELEG_NOTE_GROUPQUOTA: 4707 str = gettext("Allows accessing any groupquota@... property"); 4708 break; 4709 case ZFS_DELEG_NOTE_GROUPUSED: 4710 str = gettext("Allows reading any groupused@... property"); 4711 break; 4712 case ZFS_DELEG_NOTE_USERPROP: 4713 str = gettext("Allows changing any user property"); 4714 break; 4715 case ZFS_DELEG_NOTE_USERQUOTA: 4716 str = gettext("Allows accessing any userquota@... property"); 4717 break; 4718 case ZFS_DELEG_NOTE_USERUSED: 4719 str = gettext("Allows reading any userused@... property"); 4720 break; 4721 /* other */ 4722 default: 4723 str = ""; 4724 } 4725 4726 return (str); 4727 } 4728 4729 struct allow_opts { 4730 boolean_t local; 4731 boolean_t descend; 4732 boolean_t user; 4733 boolean_t group; 4734 boolean_t everyone; 4735 boolean_t create; 4736 boolean_t set; 4737 boolean_t recursive; /* unallow only */ 4738 boolean_t prt_usage; 4739 4740 boolean_t prt_perms; 4741 char *who; 4742 char *perms; 4743 const char *dataset; 4744 }; 4745 4746 static inline int 4747 prop_cmp(const void *a, const void *b) 4748 { 4749 const char *str1 = *(const char **)a; 4750 const char *str2 = *(const char **)b; 4751 return (strcmp(str1, str2)); 4752 } 4753 4754 static void 4755 allow_usage(boolean_t un, boolean_t requested, const char *msg) 4756 { 4757 const char *opt_desc[] = { 4758 "-h", gettext("show this help message and exit"), 4759 "-l", gettext("set permission locally"), 4760 "-d", gettext("set permission for descents"), 4761 "-u", gettext("set permission for user"), 4762 "-g", gettext("set permission for group"), 4763 "-e", gettext("set permission for everyone"), 4764 "-c", gettext("set create time permission"), 4765 "-s", gettext("define permission set"), 4766 /* unallow only */ 4767 "-r", gettext("remove permissions recursively"), 4768 }; 4769 size_t unallow_size = sizeof (opt_desc) / sizeof (char *); 4770 size_t allow_size = unallow_size - 2; 4771 const char *props[ZFS_NUM_PROPS]; 4772 int i; 4773 size_t count = 0; 4774 FILE *fp = requested ? stdout : stderr; 4775 zprop_desc_t *pdtbl = zfs_prop_get_table(); 4776 const char *fmt = gettext("%-16s %-14s\t%s\n"); 4777 4778 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW : 4779 HELP_ALLOW)); 4780 (void) fprintf(fp, gettext("Options:\n")); 4781 for (int i = 0; i < (un ? unallow_size : allow_size); i++) { 4782 const char *opt = opt_desc[i++]; 4783 const char *optdsc = opt_desc[i]; 4784 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc); 4785 } 4786 4787 (void) fprintf(fp, gettext("\nThe following permissions are " 4788 "supported:\n\n")); 4789 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"), 4790 gettext("NOTES")); 4791 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) { 4792 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm; 4793 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note; 4794 const char *perm_type = deleg_perm_type(perm_note); 4795 const char *perm_comment = deleg_perm_comment(perm_note); 4796 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment); 4797 } 4798 4799 for (i = 0; i < ZFS_NUM_PROPS; i++) { 4800 zprop_desc_t *pd = &pdtbl[i]; 4801 if (pd->pd_visible != B_TRUE) 4802 continue; 4803 4804 if (pd->pd_attr == PROP_READONLY) 4805 continue; 4806 4807 props[count++] = pd->pd_name; 4808 } 4809 props[count] = NULL; 4810 4811 qsort(props, count, sizeof (char *), prop_cmp); 4812 4813 for (i = 0; i < count; i++) 4814 (void) fprintf(fp, fmt, props[i], gettext("property"), ""); 4815 4816 if (msg != NULL) 4817 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg); 4818 4819 exit(requested ? 0 : 2); 4820 } 4821 4822 static inline const char * 4823 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc, 4824 char **permsp) 4825 { 4826 if (un && argc == expected_argc - 1) 4827 *permsp = NULL; 4828 else if (argc == expected_argc) 4829 *permsp = argv[argc - 2]; 4830 else 4831 allow_usage(un, B_FALSE, 4832 gettext("wrong number of parameters\n")); 4833 4834 return (argv[argc - 1]); 4835 } 4836 4837 static void 4838 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts) 4839 { 4840 int uge_sum = opts->user + opts->group + opts->everyone; 4841 int csuge_sum = opts->create + opts->set + uge_sum; 4842 int ldcsuge_sum = csuge_sum + opts->local + opts->descend; 4843 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum; 4844 4845 if (uge_sum > 1) 4846 allow_usage(un, B_FALSE, 4847 gettext("-u, -g, and -e are mutually exclusive\n")); 4848 4849 if (opts->prt_usage) { 4850 if (argc == 0 && all_sum == 0) 4851 allow_usage(un, B_TRUE, NULL); 4852 else 4853 usage(B_FALSE); 4854 } 4855 4856 if (opts->set) { 4857 if (csuge_sum > 1) 4858 allow_usage(un, B_FALSE, 4859 gettext("invalid options combined with -s\n")); 4860 4861 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms); 4862 if (argv[0][0] != '@') 4863 allow_usage(un, B_FALSE, 4864 gettext("invalid set name: missing '@' prefix\n")); 4865 opts->who = argv[0]; 4866 } else if (opts->create) { 4867 if (ldcsuge_sum > 1) 4868 allow_usage(un, B_FALSE, 4869 gettext("invalid options combined with -c\n")); 4870 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4871 } else if (opts->everyone) { 4872 if (csuge_sum > 1) 4873 allow_usage(un, B_FALSE, 4874 gettext("invalid options combined with -e\n")); 4875 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4876 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone") 4877 == 0) { 4878 opts->everyone = B_TRUE; 4879 argc--; 4880 argv++; 4881 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4882 } else if (argc == 1 && !un) { 4883 opts->prt_perms = B_TRUE; 4884 opts->dataset = argv[argc-1]; 4885 } else { 4886 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms); 4887 opts->who = argv[0]; 4888 } 4889 4890 if (!opts->local && !opts->descend) { 4891 opts->local = B_TRUE; 4892 opts->descend = B_TRUE; 4893 } 4894 } 4895 4896 static void 4897 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend, 4898 const char *who, char *perms, nvlist_t *top_nvl) 4899 { 4900 int i; 4901 char ld[2] = { '\0', '\0' }; 4902 char who_buf[MAXNAMELEN + 32]; 4903 char base_type = '\0'; 4904 char set_type = '\0'; 4905 nvlist_t *base_nvl = NULL; 4906 nvlist_t *set_nvl = NULL; 4907 nvlist_t *nvl; 4908 4909 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0) 4910 nomem(); 4911 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0) 4912 nomem(); 4913 4914 switch (type) { 4915 case ZFS_DELEG_NAMED_SET_SETS: 4916 case ZFS_DELEG_NAMED_SET: 4917 set_type = ZFS_DELEG_NAMED_SET_SETS; 4918 base_type = ZFS_DELEG_NAMED_SET; 4919 ld[0] = ZFS_DELEG_NA; 4920 break; 4921 case ZFS_DELEG_CREATE_SETS: 4922 case ZFS_DELEG_CREATE: 4923 set_type = ZFS_DELEG_CREATE_SETS; 4924 base_type = ZFS_DELEG_CREATE; 4925 ld[0] = ZFS_DELEG_NA; 4926 break; 4927 case ZFS_DELEG_USER_SETS: 4928 case ZFS_DELEG_USER: 4929 set_type = ZFS_DELEG_USER_SETS; 4930 base_type = ZFS_DELEG_USER; 4931 if (local) 4932 ld[0] = ZFS_DELEG_LOCAL; 4933 if (descend) 4934 ld[1] = ZFS_DELEG_DESCENDENT; 4935 break; 4936 case ZFS_DELEG_GROUP_SETS: 4937 case ZFS_DELEG_GROUP: 4938 set_type = ZFS_DELEG_GROUP_SETS; 4939 base_type = ZFS_DELEG_GROUP; 4940 if (local) 4941 ld[0] = ZFS_DELEG_LOCAL; 4942 if (descend) 4943 ld[1] = ZFS_DELEG_DESCENDENT; 4944 break; 4945 case ZFS_DELEG_EVERYONE_SETS: 4946 case ZFS_DELEG_EVERYONE: 4947 set_type = ZFS_DELEG_EVERYONE_SETS; 4948 base_type = ZFS_DELEG_EVERYONE; 4949 if (local) 4950 ld[0] = ZFS_DELEG_LOCAL; 4951 if (descend) 4952 ld[1] = ZFS_DELEG_DESCENDENT; 4953 break; 4954 4955 default: 4956 assert(set_type != '\0' && base_type != '\0'); 4957 } 4958 4959 if (perms != NULL) { 4960 char *curr = perms; 4961 char *end = curr + strlen(perms); 4962 4963 while (curr < end) { 4964 char *delim = strchr(curr, ','); 4965 if (delim == NULL) 4966 delim = end; 4967 else 4968 *delim = '\0'; 4969 4970 if (curr[0] == '@') 4971 nvl = set_nvl; 4972 else 4973 nvl = base_nvl; 4974 4975 (void) nvlist_add_boolean(nvl, curr); 4976 if (delim != end) 4977 *delim = ','; 4978 curr = delim + 1; 4979 } 4980 4981 for (i = 0; i < 2; i++) { 4982 char locality = ld[i]; 4983 if (locality == 0) 4984 continue; 4985 4986 if (!nvlist_empty(base_nvl)) { 4987 if (who != NULL) 4988 (void) snprintf(who_buf, 4989 sizeof (who_buf), "%c%c$%s", 4990 base_type, locality, who); 4991 else 4992 (void) snprintf(who_buf, 4993 sizeof (who_buf), "%c%c$", 4994 base_type, locality); 4995 4996 (void) nvlist_add_nvlist(top_nvl, who_buf, 4997 base_nvl); 4998 } 4999 5000 5001 if (!nvlist_empty(set_nvl)) { 5002 if (who != NULL) 5003 (void) snprintf(who_buf, 5004 sizeof (who_buf), "%c%c$%s", 5005 set_type, locality, who); 5006 else 5007 (void) snprintf(who_buf, 5008 sizeof (who_buf), "%c%c$", 5009 set_type, locality); 5010 5011 (void) nvlist_add_nvlist(top_nvl, who_buf, 5012 set_nvl); 5013 } 5014 } 5015 } else { 5016 for (i = 0; i < 2; i++) { 5017 char locality = ld[i]; 5018 if (locality == 0) 5019 continue; 5020 5021 if (who != NULL) 5022 (void) snprintf(who_buf, sizeof (who_buf), 5023 "%c%c$%s", base_type, locality, who); 5024 else 5025 (void) snprintf(who_buf, sizeof (who_buf), 5026 "%c%c$", base_type, locality); 5027 (void) nvlist_add_boolean(top_nvl, who_buf); 5028 5029 if (who != NULL) 5030 (void) snprintf(who_buf, sizeof (who_buf), 5031 "%c%c$%s", set_type, locality, who); 5032 else 5033 (void) snprintf(who_buf, sizeof (who_buf), 5034 "%c%c$", set_type, locality); 5035 (void) nvlist_add_boolean(top_nvl, who_buf); 5036 } 5037 } 5038 } 5039 5040 static int 5041 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp) 5042 { 5043 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0) 5044 nomem(); 5045 5046 if (opts->set) { 5047 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local, 5048 opts->descend, opts->who, opts->perms, *nvlp); 5049 } else if (opts->create) { 5050 store_allow_perm(ZFS_DELEG_CREATE, opts->local, 5051 opts->descend, NULL, opts->perms, *nvlp); 5052 } else if (opts->everyone) { 5053 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local, 5054 opts->descend, NULL, opts->perms, *nvlp); 5055 } else { 5056 char *curr = opts->who; 5057 char *end = curr + strlen(curr); 5058 5059 while (curr < end) { 5060 const char *who; 5061 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN; 5062 char *endch; 5063 char *delim = strchr(curr, ','); 5064 char errbuf[256]; 5065 char id[64]; 5066 struct passwd *p = NULL; 5067 struct group *g = NULL; 5068 5069 uid_t rid; 5070 if (delim == NULL) 5071 delim = end; 5072 else 5073 *delim = '\0'; 5074 5075 rid = (uid_t)strtol(curr, &endch, 0); 5076 if (opts->user) { 5077 who_type = ZFS_DELEG_USER; 5078 if (*endch != '\0') 5079 p = getpwnam(curr); 5080 else 5081 p = getpwuid(rid); 5082 5083 if (p != NULL) 5084 rid = p->pw_uid; 5085 else { 5086 (void) snprintf(errbuf, 256, gettext( 5087 "invalid user %s"), curr); 5088 allow_usage(un, B_TRUE, errbuf); 5089 } 5090 } else if (opts->group) { 5091 who_type = ZFS_DELEG_GROUP; 5092 if (*endch != '\0') 5093 g = getgrnam(curr); 5094 else 5095 g = getgrgid(rid); 5096 5097 if (g != NULL) 5098 rid = g->gr_gid; 5099 else { 5100 (void) snprintf(errbuf, 256, gettext( 5101 "invalid group %s"), curr); 5102 allow_usage(un, B_TRUE, errbuf); 5103 } 5104 } else { 5105 if (*endch != '\0') { 5106 p = getpwnam(curr); 5107 } else { 5108 p = getpwuid(rid); 5109 } 5110 5111 if (p == NULL) { 5112 if (*endch != '\0') { 5113 g = getgrnam(curr); 5114 } else { 5115 g = getgrgid(rid); 5116 } 5117 } 5118 5119 if (p != NULL) { 5120 who_type = ZFS_DELEG_USER; 5121 rid = p->pw_uid; 5122 } else if (g != NULL) { 5123 who_type = ZFS_DELEG_GROUP; 5124 rid = g->gr_gid; 5125 } else { 5126 (void) snprintf(errbuf, 256, gettext( 5127 "invalid user/group %s"), curr); 5128 allow_usage(un, B_TRUE, errbuf); 5129 } 5130 } 5131 5132 (void) sprintf(id, "%u", rid); 5133 who = id; 5134 5135 store_allow_perm(who_type, opts->local, 5136 opts->descend, who, opts->perms, *nvlp); 5137 curr = delim + 1; 5138 } 5139 } 5140 5141 return (0); 5142 } 5143 5144 static void 5145 print_set_creat_perms(uu_avl_t *who_avl) 5146 { 5147 const char *sc_title[] = { 5148 gettext("Permission sets:\n"), 5149 gettext("Create time permissions:\n"), 5150 NULL 5151 }; 5152 const char **title_ptr = sc_title; 5153 who_perm_node_t *who_node = NULL; 5154 int prev_weight = -1; 5155 5156 for (who_node = uu_avl_first(who_avl); who_node != NULL; 5157 who_node = uu_avl_next(who_avl, who_node)) { 5158 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl; 5159 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type; 5160 const char *who_name = who_node->who_perm.who_name; 5161 int weight = who_type2weight(who_type); 5162 boolean_t first = B_TRUE; 5163 deleg_perm_node_t *deleg_node; 5164 5165 if (prev_weight != weight) { 5166 (void) printf(*title_ptr++); 5167 prev_weight = weight; 5168 } 5169 5170 if (who_name == NULL || strnlen(who_name, 1) == 0) 5171 (void) printf("\t"); 5172 else 5173 (void) printf("\t%s ", who_name); 5174 5175 for (deleg_node = uu_avl_first(avl); deleg_node != NULL; 5176 deleg_node = uu_avl_next(avl, deleg_node)) { 5177 if (first) { 5178 (void) printf("%s", 5179 deleg_node->dpn_perm.dp_name); 5180 first = B_FALSE; 5181 } else 5182 (void) printf(",%s", 5183 deleg_node->dpn_perm.dp_name); 5184 } 5185 5186 (void) printf("\n"); 5187 } 5188 } 5189 5190 static void 5191 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend, 5192 const char *title) 5193 { 5194 who_perm_node_t *who_node = NULL; 5195 boolean_t prt_title = B_TRUE; 5196 uu_avl_walk_t *walk; 5197 5198 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL) 5199 nomem(); 5200 5201 while ((who_node = uu_avl_walk_next(walk)) != NULL) { 5202 const char *who_name = who_node->who_perm.who_name; 5203 const char *nice_who_name = who_node->who_perm.who_ug_name; 5204 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl; 5205 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type; 5206 char delim = ' '; 5207 deleg_perm_node_t *deleg_node; 5208 boolean_t prt_who = B_TRUE; 5209 5210 for (deleg_node = uu_avl_first(avl); 5211 deleg_node != NULL; 5212 deleg_node = uu_avl_next(avl, deleg_node)) { 5213 if (local != deleg_node->dpn_perm.dp_local || 5214 descend != deleg_node->dpn_perm.dp_descend) 5215 continue; 5216 5217 if (prt_who) { 5218 const char *who = NULL; 5219 if (prt_title) { 5220 prt_title = B_FALSE; 5221 (void) printf(title); 5222 } 5223 5224 switch (who_type) { 5225 case ZFS_DELEG_USER_SETS: 5226 case ZFS_DELEG_USER: 5227 who = gettext("user"); 5228 if (nice_who_name) 5229 who_name = nice_who_name; 5230 break; 5231 case ZFS_DELEG_GROUP_SETS: 5232 case ZFS_DELEG_GROUP: 5233 who = gettext("group"); 5234 if (nice_who_name) 5235 who_name = nice_who_name; 5236 break; 5237 case ZFS_DELEG_EVERYONE_SETS: 5238 case ZFS_DELEG_EVERYONE: 5239 who = gettext("everyone"); 5240 who_name = NULL; 5241 break; 5242 5243 default: 5244 assert(who != NULL); 5245 } 5246 5247 prt_who = B_FALSE; 5248 if (who_name == NULL) 5249 (void) printf("\t%s", who); 5250 else 5251 (void) printf("\t%s %s", who, who_name); 5252 } 5253 5254 (void) printf("%c%s", delim, 5255 deleg_node->dpn_perm.dp_name); 5256 delim = ','; 5257 } 5258 5259 if (!prt_who) 5260 (void) printf("\n"); 5261 } 5262 5263 uu_avl_walk_end(walk); 5264 } 5265 5266 static void 5267 print_fs_perms(fs_perm_set_t *fspset) 5268 { 5269 fs_perm_node_t *node = NULL; 5270 char buf[MAXNAMELEN + 32]; 5271 const char *dsname = buf; 5272 5273 for (node = uu_list_first(fspset->fsps_list); node != NULL; 5274 node = uu_list_next(fspset->fsps_list, node)) { 5275 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl; 5276 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl; 5277 int left = 0; 5278 5279 (void) snprintf(buf, sizeof (buf), 5280 gettext("---- Permissions on %s "), 5281 node->fspn_fsperm.fsp_name); 5282 (void) printf(dsname); 5283 left = 70 - strlen(buf); 5284 while (left-- > 0) 5285 (void) printf("-"); 5286 (void) printf("\n"); 5287 5288 print_set_creat_perms(sc_avl); 5289 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE, 5290 gettext("Local permissions:\n")); 5291 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE, 5292 gettext("Descendent permissions:\n")); 5293 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE, 5294 gettext("Local+Descendent permissions:\n")); 5295 } 5296 } 5297 5298 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL }; 5299 5300 struct deleg_perms { 5301 boolean_t un; 5302 nvlist_t *nvl; 5303 }; 5304 5305 static int 5306 set_deleg_perms(zfs_handle_t *zhp, void *data) 5307 { 5308 struct deleg_perms *perms = (struct deleg_perms *)data; 5309 zfs_type_t zfs_type = zfs_get_type(zhp); 5310 5311 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME) 5312 return (0); 5313 5314 return (zfs_set_fsacl(zhp, perms->un, perms->nvl)); 5315 } 5316 5317 static int 5318 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un) 5319 { 5320 zfs_handle_t *zhp; 5321 nvlist_t *perm_nvl = NULL; 5322 nvlist_t *update_perm_nvl = NULL; 5323 int error = 1; 5324 int c; 5325 struct allow_opts opts = { 0 }; 5326 5327 const char *optstr = un ? "ldugecsrh" : "ldugecsh"; 5328 5329 /* check opts */ 5330 while ((c = getopt(argc, argv, optstr)) != -1) { 5331 switch (c) { 5332 case 'l': 5333 opts.local = B_TRUE; 5334 break; 5335 case 'd': 5336 opts.descend = B_TRUE; 5337 break; 5338 case 'u': 5339 opts.user = B_TRUE; 5340 break; 5341 case 'g': 5342 opts.group = B_TRUE; 5343 break; 5344 case 'e': 5345 opts.everyone = B_TRUE; 5346 break; 5347 case 's': 5348 opts.set = B_TRUE; 5349 break; 5350 case 'c': 5351 opts.create = B_TRUE; 5352 break; 5353 case 'r': 5354 opts.recursive = B_TRUE; 5355 break; 5356 case ':': 5357 (void) fprintf(stderr, gettext("missing argument for " 5358 "'%c' option\n"), optopt); 5359 usage(B_FALSE); 5360 break; 5361 case 'h': 5362 opts.prt_usage = B_TRUE; 5363 break; 5364 case '?': 5365 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5366 optopt); 5367 usage(B_FALSE); 5368 } 5369 } 5370 5371 argc -= optind; 5372 argv += optind; 5373 5374 /* check arguments */ 5375 parse_allow_args(argc, argv, un, &opts); 5376 5377 /* try to open the dataset */ 5378 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM | 5379 ZFS_TYPE_VOLUME)) == NULL) { 5380 (void) fprintf(stderr, "Failed to open dataset: %s\n", 5381 opts.dataset); 5382 return (-1); 5383 } 5384 5385 if (zfs_get_fsacl(zhp, &perm_nvl) != 0) 5386 goto cleanup2; 5387 5388 fs_perm_set_init(&fs_perm_set); 5389 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) { 5390 (void) fprintf(stderr, "Failed to parse fsacl permissions\n"); 5391 goto cleanup1; 5392 } 5393 5394 if (opts.prt_perms) 5395 print_fs_perms(&fs_perm_set); 5396 else { 5397 (void) construct_fsacl_list(un, &opts, &update_perm_nvl); 5398 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0) 5399 goto cleanup0; 5400 5401 if (un && opts.recursive) { 5402 struct deleg_perms data = { un, update_perm_nvl }; 5403 if (zfs_iter_filesystems(zhp, set_deleg_perms, 5404 &data) != 0) 5405 goto cleanup0; 5406 } 5407 } 5408 5409 error = 0; 5410 5411 cleanup0: 5412 nvlist_free(perm_nvl); 5413 nvlist_free(update_perm_nvl); 5414 cleanup1: 5415 fs_perm_set_fini(&fs_perm_set); 5416 cleanup2: 5417 zfs_close(zhp); 5418 5419 return (error); 5420 } 5421 5422 static int 5423 zfs_do_allow(int argc, char **argv) 5424 { 5425 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE)); 5426 } 5427 5428 static int 5429 zfs_do_unallow(int argc, char **argv) 5430 { 5431 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE)); 5432 } 5433 5434 static int 5435 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding) 5436 { 5437 int errors = 0; 5438 int i; 5439 const char *tag; 5440 boolean_t recursive = B_FALSE; 5441 const char *opts = holding ? "rt" : "r"; 5442 int c; 5443 5444 /* check options */ 5445 while ((c = getopt(argc, argv, opts)) != -1) { 5446 switch (c) { 5447 case 'r': 5448 recursive = B_TRUE; 5449 break; 5450 case '?': 5451 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5452 optopt); 5453 usage(B_FALSE); 5454 } 5455 } 5456 5457 argc -= optind; 5458 argv += optind; 5459 5460 /* check number of arguments */ 5461 if (argc < 2) 5462 usage(B_FALSE); 5463 5464 tag = argv[0]; 5465 --argc; 5466 ++argv; 5467 5468 if (holding && tag[0] == '.') { 5469 /* tags starting with '.' are reserved for libzfs */ 5470 (void) fprintf(stderr, gettext("tag may not start with '.'\n")); 5471 usage(B_FALSE); 5472 } 5473 5474 for (i = 0; i < argc; ++i) { 5475 zfs_handle_t *zhp; 5476 char parent[ZFS_MAX_DATASET_NAME_LEN]; 5477 const char *delim; 5478 char *path = argv[i]; 5479 5480 delim = strchr(path, '@'); 5481 if (delim == NULL) { 5482 (void) fprintf(stderr, 5483 gettext("'%s' is not a snapshot\n"), path); 5484 ++errors; 5485 continue; 5486 } 5487 (void) strncpy(parent, path, delim - path); 5488 parent[delim - path] = '\0'; 5489 5490 zhp = zfs_open(g_zfs, parent, 5491 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 5492 if (zhp == NULL) { 5493 ++errors; 5494 continue; 5495 } 5496 if (holding) { 5497 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0) 5498 ++errors; 5499 } else { 5500 if (zfs_release(zhp, delim+1, tag, recursive) != 0) 5501 ++errors; 5502 } 5503 zfs_close(zhp); 5504 } 5505 5506 return (errors != 0); 5507 } 5508 5509 /* 5510 * zfs hold [-r] [-t] <tag> <snap> ... 5511 * 5512 * -r Recursively hold 5513 * 5514 * Apply a user-hold with the given tag to the list of snapshots. 5515 */ 5516 static int 5517 zfs_do_hold(int argc, char **argv) 5518 { 5519 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE)); 5520 } 5521 5522 /* 5523 * zfs release [-r] <tag> <snap> ... 5524 * 5525 * -r Recursively release 5526 * 5527 * Release a user-hold with the given tag from the list of snapshots. 5528 */ 5529 static int 5530 zfs_do_release(int argc, char **argv) 5531 { 5532 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE)); 5533 } 5534 5535 typedef struct holds_cbdata { 5536 boolean_t cb_recursive; 5537 const char *cb_snapname; 5538 nvlist_t **cb_nvlp; 5539 size_t cb_max_namelen; 5540 size_t cb_max_taglen; 5541 } holds_cbdata_t; 5542 5543 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y" 5544 #define DATETIME_BUF_LEN (32) 5545 /* 5546 * 5547 */ 5548 static void 5549 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl) 5550 { 5551 int i; 5552 nvpair_t *nvp = NULL; 5553 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" }; 5554 const char *col; 5555 5556 if (!scripted) { 5557 for (i = 0; i < 3; i++) { 5558 col = gettext(hdr_cols[i]); 5559 if (i < 2) 5560 (void) printf("%-*s ", i ? tagwidth : nwidth, 5561 col); 5562 else 5563 (void) printf("%s\n", col); 5564 } 5565 } 5566 5567 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 5568 char *zname = nvpair_name(nvp); 5569 nvlist_t *nvl2; 5570 nvpair_t *nvp2 = NULL; 5571 (void) nvpair_value_nvlist(nvp, &nvl2); 5572 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) { 5573 char tsbuf[DATETIME_BUF_LEN]; 5574 char *tagname = nvpair_name(nvp2); 5575 uint64_t val = 0; 5576 time_t time; 5577 struct tm t; 5578 char sep = scripted ? '\t' : ' '; 5579 size_t sepnum = scripted ? 1 : 2; 5580 5581 (void) nvpair_value_uint64(nvp2, &val); 5582 time = (time_t)val; 5583 (void) localtime_r(&time, &t); 5584 (void) strftime(tsbuf, DATETIME_BUF_LEN, 5585 gettext(STRFTIME_FMT_STR), &t); 5586 5587 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname, 5588 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf); 5589 } 5590 } 5591 } 5592 5593 /* 5594 * Generic callback function to list a dataset or snapshot. 5595 */ 5596 static int 5597 holds_callback(zfs_handle_t *zhp, void *data) 5598 { 5599 holds_cbdata_t *cbp = data; 5600 nvlist_t *top_nvl = *cbp->cb_nvlp; 5601 nvlist_t *nvl = NULL; 5602 nvpair_t *nvp = NULL; 5603 const char *zname = zfs_get_name(zhp); 5604 size_t znamelen = strlen(zname); 5605 5606 if (cbp->cb_recursive) { 5607 const char *snapname; 5608 char *delim = strchr(zname, '@'); 5609 if (delim == NULL) 5610 return (0); 5611 5612 snapname = delim + 1; 5613 if (strcmp(cbp->cb_snapname, snapname)) 5614 return (0); 5615 } 5616 5617 if (zfs_get_holds(zhp, &nvl) != 0) 5618 return (-1); 5619 5620 if (znamelen > cbp->cb_max_namelen) 5621 cbp->cb_max_namelen = znamelen; 5622 5623 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 5624 const char *tag = nvpair_name(nvp); 5625 size_t taglen = strlen(tag); 5626 if (taglen > cbp->cb_max_taglen) 5627 cbp->cb_max_taglen = taglen; 5628 } 5629 5630 return (nvlist_add_nvlist(top_nvl, zname, nvl)); 5631 } 5632 5633 /* 5634 * zfs holds [-r] <snap> ... 5635 * 5636 * -r Recursively hold 5637 */ 5638 static int 5639 zfs_do_holds(int argc, char **argv) 5640 { 5641 int errors = 0; 5642 int c; 5643 int i; 5644 boolean_t scripted = B_FALSE; 5645 boolean_t recursive = B_FALSE; 5646 const char *opts = "rH"; 5647 nvlist_t *nvl; 5648 5649 int types = ZFS_TYPE_SNAPSHOT; 5650 holds_cbdata_t cb = { 0 }; 5651 5652 int limit = 0; 5653 int ret = 0; 5654 int flags = 0; 5655 5656 /* check options */ 5657 while ((c = getopt(argc, argv, opts)) != -1) { 5658 switch (c) { 5659 case 'r': 5660 recursive = B_TRUE; 5661 break; 5662 case 'H': 5663 scripted = B_TRUE; 5664 break; 5665 case '?': 5666 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5667 optopt); 5668 usage(B_FALSE); 5669 } 5670 } 5671 5672 if (recursive) { 5673 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME; 5674 flags |= ZFS_ITER_RECURSE; 5675 } 5676 5677 argc -= optind; 5678 argv += optind; 5679 5680 /* check number of arguments */ 5681 if (argc < 1) 5682 usage(B_FALSE); 5683 5684 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 5685 nomem(); 5686 5687 for (i = 0; i < argc; ++i) { 5688 char *snapshot = argv[i]; 5689 const char *delim; 5690 const char *snapname; 5691 5692 delim = strchr(snapshot, '@'); 5693 if (delim == NULL) { 5694 (void) fprintf(stderr, 5695 gettext("'%s' is not a snapshot\n"), snapshot); 5696 ++errors; 5697 continue; 5698 } 5699 snapname = delim + 1; 5700 if (recursive) 5701 snapshot[delim - snapshot] = '\0'; 5702 5703 cb.cb_recursive = recursive; 5704 cb.cb_snapname = snapname; 5705 cb.cb_nvlp = &nvl; 5706 5707 /* 5708 * 1. collect holds data, set format options 5709 */ 5710 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit, 5711 holds_callback, &cb); 5712 if (ret != 0) 5713 ++errors; 5714 } 5715 5716 /* 5717 * 2. print holds data 5718 */ 5719 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl); 5720 5721 if (nvlist_empty(nvl)) 5722 (void) printf(gettext("no datasets available\n")); 5723 5724 nvlist_free(nvl); 5725 5726 return (0 != errors); 5727 } 5728 5729 #define CHECK_SPINNER 30 5730 #define SPINNER_TIME 3 /* seconds */ 5731 #define MOUNT_TIME 5 /* seconds */ 5732 5733 static int 5734 get_one_dataset(zfs_handle_t *zhp, void *data) 5735 { 5736 static char *spin[] = { "-", "\\", "|", "/" }; 5737 static int spinval = 0; 5738 static int spincheck = 0; 5739 static time_t last_spin_time = (time_t)0; 5740 get_all_cb_t *cbp = data; 5741 zfs_type_t type = zfs_get_type(zhp); 5742 5743 if (cbp->cb_verbose) { 5744 if (--spincheck < 0) { 5745 time_t now = time(NULL); 5746 if (last_spin_time + SPINNER_TIME < now) { 5747 update_progress(spin[spinval++ % 4]); 5748 last_spin_time = now; 5749 } 5750 spincheck = CHECK_SPINNER; 5751 } 5752 } 5753 5754 /* 5755 * Interate over any nested datasets. 5756 */ 5757 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) { 5758 zfs_close(zhp); 5759 return (1); 5760 } 5761 5762 /* 5763 * Skip any datasets whose type does not match. 5764 */ 5765 if ((type & ZFS_TYPE_FILESYSTEM) == 0) { 5766 zfs_close(zhp); 5767 return (0); 5768 } 5769 libzfs_add_handle(cbp, zhp); 5770 assert(cbp->cb_used <= cbp->cb_alloc); 5771 5772 return (0); 5773 } 5774 5775 static void 5776 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose) 5777 { 5778 get_all_cb_t cb = { 0 }; 5779 cb.cb_verbose = verbose; 5780 cb.cb_getone = get_one_dataset; 5781 5782 if (verbose) 5783 set_progress_header(gettext("Reading ZFS config")); 5784 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb); 5785 5786 *dslist = cb.cb_handles; 5787 *count = cb.cb_used; 5788 5789 if (verbose) 5790 finish_progress(gettext("done.")); 5791 } 5792 5793 /* 5794 * Generic callback for sharing or mounting filesystems. Because the code is so 5795 * similar, we have a common function with an extra parameter to determine which 5796 * mode we are using. 5797 */ 5798 #define OP_SHARE 0x1 5799 #define OP_MOUNT 0x2 5800 5801 /* 5802 * Share or mount a dataset. 5803 */ 5804 static int 5805 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol, 5806 boolean_t explicit, const char *options) 5807 { 5808 char mountpoint[ZFS_MAXPROPLEN]; 5809 char shareopts[ZFS_MAXPROPLEN]; 5810 char smbshareopts[ZFS_MAXPROPLEN]; 5811 const char *cmdname = op == OP_SHARE ? "share" : "mount"; 5812 struct mnttab mnt; 5813 uint64_t zoned, canmount; 5814 boolean_t shared_nfs, shared_smb; 5815 5816 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM); 5817 5818 /* 5819 * Check to make sure we can mount/share this dataset. If we 5820 * are in the global zone and the filesystem is exported to a 5821 * local zone, or if we are in a local zone and the 5822 * filesystem is not exported, then it is an error. 5823 */ 5824 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 5825 5826 if (zoned && getzoneid() == GLOBAL_ZONEID) { 5827 if (!explicit) 5828 return (0); 5829 5830 (void) fprintf(stderr, gettext("cannot %s '%s': " 5831 "dataset is exported to a local zone\n"), cmdname, 5832 zfs_get_name(zhp)); 5833 return (1); 5834 5835 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) { 5836 if (!explicit) 5837 return (0); 5838 5839 (void) fprintf(stderr, gettext("cannot %s '%s': " 5840 "permission denied\n"), cmdname, 5841 zfs_get_name(zhp)); 5842 return (1); 5843 } 5844 5845 /* 5846 * Ignore any filesystems which don't apply to us. This 5847 * includes those with a legacy mountpoint, or those with 5848 * legacy share options. 5849 */ 5850 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 5851 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0); 5852 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, 5853 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0); 5854 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts, 5855 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0); 5856 5857 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 && 5858 strcmp(smbshareopts, "off") == 0) { 5859 if (!explicit) 5860 return (0); 5861 5862 (void) fprintf(stderr, gettext("cannot share '%s': " 5863 "legacy share\n"), zfs_get_name(zhp)); 5864 (void) fprintf(stderr, gettext("use share(1M) to " 5865 "share this filesystem, or set " 5866 "sharenfs property on\n")); 5867 return (1); 5868 } 5869 5870 /* 5871 * We cannot share or mount legacy filesystems. If the 5872 * shareopts is non-legacy but the mountpoint is legacy, we 5873 * treat it as a legacy share. 5874 */ 5875 if (strcmp(mountpoint, "legacy") == 0) { 5876 if (!explicit) 5877 return (0); 5878 5879 (void) fprintf(stderr, gettext("cannot %s '%s': " 5880 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp)); 5881 (void) fprintf(stderr, gettext("use %s(1M) to " 5882 "%s this filesystem\n"), cmdname, cmdname); 5883 return (1); 5884 } 5885 5886 if (strcmp(mountpoint, "none") == 0) { 5887 if (!explicit) 5888 return (0); 5889 5890 (void) fprintf(stderr, gettext("cannot %s '%s': no " 5891 "mountpoint set\n"), cmdname, zfs_get_name(zhp)); 5892 return (1); 5893 } 5894 5895 /* 5896 * canmount explicit outcome 5897 * on no pass through 5898 * on yes pass through 5899 * off no return 0 5900 * off yes display error, return 1 5901 * noauto no return 0 5902 * noauto yes pass through 5903 */ 5904 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT); 5905 if (canmount == ZFS_CANMOUNT_OFF) { 5906 if (!explicit) 5907 return (0); 5908 5909 (void) fprintf(stderr, gettext("cannot %s '%s': " 5910 "'canmount' property is set to 'off'\n"), cmdname, 5911 zfs_get_name(zhp)); 5912 return (1); 5913 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) { 5914 return (0); 5915 } 5916 5917 /* 5918 * If this filesystem is inconsistent and has a receive resume 5919 * token, we can not mount it. 5920 */ 5921 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) && 5922 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 5923 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) { 5924 if (!explicit) 5925 return (0); 5926 5927 (void) fprintf(stderr, gettext("cannot %s '%s': " 5928 "Contains partially-completed state from " 5929 "\"zfs receive -r\", which can be resumed with " 5930 "\"zfs send -t\"\n"), 5931 cmdname, zfs_get_name(zhp)); 5932 return (1); 5933 } 5934 5935 /* 5936 * At this point, we have verified that the mountpoint and/or 5937 * shareopts are appropriate for auto management. If the 5938 * filesystem is already mounted or shared, return (failing 5939 * for explicit requests); otherwise mount or share the 5940 * filesystem. 5941 */ 5942 switch (op) { 5943 case OP_SHARE: 5944 5945 shared_nfs = zfs_is_shared_nfs(zhp, NULL); 5946 shared_smb = zfs_is_shared_smb(zhp, NULL); 5947 5948 if ((shared_nfs && shared_smb) || 5949 (shared_nfs && strcmp(shareopts, "on") == 0 && 5950 strcmp(smbshareopts, "off") == 0) || 5951 (shared_smb && strcmp(smbshareopts, "on") == 0 && 5952 strcmp(shareopts, "off") == 0)) { 5953 if (!explicit) 5954 return (0); 5955 5956 (void) fprintf(stderr, gettext("cannot share " 5957 "'%s': filesystem already shared\n"), 5958 zfs_get_name(zhp)); 5959 return (1); 5960 } 5961 5962 if (!zfs_is_mounted(zhp, NULL) && 5963 zfs_mount(zhp, NULL, 0) != 0) 5964 return (1); 5965 5966 if (protocol == NULL) { 5967 if (zfs_shareall(zhp) != 0) 5968 return (1); 5969 } else if (strcmp(protocol, "nfs") == 0) { 5970 if (zfs_share_nfs(zhp)) 5971 return (1); 5972 } else if (strcmp(protocol, "smb") == 0) { 5973 if (zfs_share_smb(zhp)) 5974 return (1); 5975 } else { 5976 (void) fprintf(stderr, gettext("cannot share " 5977 "'%s': invalid share type '%s' " 5978 "specified\n"), 5979 zfs_get_name(zhp), protocol); 5980 return (1); 5981 } 5982 5983 break; 5984 5985 case OP_MOUNT: 5986 if (options == NULL) 5987 mnt.mnt_mntopts = ""; 5988 else 5989 mnt.mnt_mntopts = (char *)options; 5990 5991 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) && 5992 zfs_is_mounted(zhp, NULL)) { 5993 if (!explicit) 5994 return (0); 5995 5996 (void) fprintf(stderr, gettext("cannot mount " 5997 "'%s': filesystem already mounted\n"), 5998 zfs_get_name(zhp)); 5999 return (1); 6000 } 6001 6002 if (zfs_mount(zhp, options, flags) != 0) 6003 return (1); 6004 break; 6005 } 6006 6007 return (0); 6008 } 6009 6010 /* 6011 * Reports progress in the form "(current/total)". Not thread-safe. 6012 */ 6013 static void 6014 report_mount_progress(int current, int total) 6015 { 6016 static time_t last_progress_time = 0; 6017 time_t now = time(NULL); 6018 char info[32]; 6019 6020 /* report 1..n instead of 0..n-1 */ 6021 ++current; 6022 6023 /* display header if we're here for the first time */ 6024 if (current == 1) { 6025 set_progress_header(gettext("Mounting ZFS filesystems")); 6026 } else if (current != total && last_progress_time + MOUNT_TIME >= now) { 6027 /* too soon to report again */ 6028 return; 6029 } 6030 6031 last_progress_time = now; 6032 6033 (void) sprintf(info, "(%d/%d)", current, total); 6034 6035 if (current == total) 6036 finish_progress(info); 6037 else 6038 update_progress(info); 6039 } 6040 6041 static void 6042 append_options(char *mntopts, char *newopts) 6043 { 6044 int len = strlen(mntopts); 6045 6046 /* original length plus new string to append plus 1 for the comma */ 6047 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) { 6048 (void) fprintf(stderr, gettext("the opts argument for " 6049 "'%c' option is too long (more than %d chars)\n"), 6050 "-o", MNT_LINE_MAX); 6051 usage(B_FALSE); 6052 } 6053 6054 if (*mntopts) 6055 mntopts[len++] = ','; 6056 6057 (void) strcpy(&mntopts[len], newopts); 6058 } 6059 6060 static int 6061 share_mount(int op, int argc, char **argv) 6062 { 6063 int do_all = 0; 6064 boolean_t verbose = B_FALSE; 6065 int c, ret = 0; 6066 char *options = NULL; 6067 int flags = 0; 6068 6069 /* check options */ 6070 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a")) 6071 != -1) { 6072 switch (c) { 6073 case 'a': 6074 do_all = 1; 6075 break; 6076 case 'v': 6077 verbose = B_TRUE; 6078 break; 6079 case 'o': 6080 if (*optarg == '\0') { 6081 (void) fprintf(stderr, gettext("empty mount " 6082 "options (-o) specified\n")); 6083 usage(B_FALSE); 6084 } 6085 6086 if (options == NULL) 6087 options = safe_malloc(MNT_LINE_MAX + 1); 6088 6089 /* option validation is done later */ 6090 append_options(options, optarg); 6091 break; 6092 6093 case 'O': 6094 flags |= MS_OVERLAY; 6095 break; 6096 case ':': 6097 (void) fprintf(stderr, gettext("missing argument for " 6098 "'%c' option\n"), optopt); 6099 usage(B_FALSE); 6100 break; 6101 case '?': 6102 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6103 optopt); 6104 usage(B_FALSE); 6105 } 6106 } 6107 6108 argc -= optind; 6109 argv += optind; 6110 6111 /* check number of arguments */ 6112 if (do_all) { 6113 zfs_handle_t **dslist = NULL; 6114 size_t i, count = 0; 6115 char *protocol = NULL; 6116 6117 if (op == OP_SHARE && argc > 0) { 6118 if (strcmp(argv[0], "nfs") != 0 && 6119 strcmp(argv[0], "smb") != 0) { 6120 (void) fprintf(stderr, gettext("share type " 6121 "must be 'nfs' or 'smb'\n")); 6122 usage(B_FALSE); 6123 } 6124 protocol = argv[0]; 6125 argc--; 6126 argv++; 6127 } 6128 6129 if (argc != 0) { 6130 (void) fprintf(stderr, gettext("too many arguments\n")); 6131 usage(B_FALSE); 6132 } 6133 6134 start_progress_timer(); 6135 get_all_datasets(&dslist, &count, verbose); 6136 6137 if (count == 0) 6138 return (0); 6139 6140 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp); 6141 6142 for (i = 0; i < count; i++) { 6143 if (verbose) 6144 report_mount_progress(i, count); 6145 6146 if (share_mount_one(dslist[i], op, flags, protocol, 6147 B_FALSE, options) != 0) 6148 ret = 1; 6149 zfs_close(dslist[i]); 6150 } 6151 6152 free(dslist); 6153 } else if (argc == 0) { 6154 struct mnttab entry; 6155 6156 if ((op == OP_SHARE) || (options != NULL)) { 6157 (void) fprintf(stderr, gettext("missing filesystem " 6158 "argument (specify -a for all)\n")); 6159 usage(B_FALSE); 6160 } 6161 6162 /* 6163 * When mount is given no arguments, go through /etc/mnttab and 6164 * display any active ZFS mounts. We hide any snapshots, since 6165 * they are controlled automatically. 6166 */ 6167 rewind(mnttab_file); 6168 while (getmntent(mnttab_file, &entry) == 0) { 6169 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 || 6170 strchr(entry.mnt_special, '@') != NULL) 6171 continue; 6172 6173 (void) printf("%-30s %s\n", entry.mnt_special, 6174 entry.mnt_mountp); 6175 } 6176 6177 } else { 6178 zfs_handle_t *zhp; 6179 6180 if (argc > 1) { 6181 (void) fprintf(stderr, 6182 gettext("too many arguments\n")); 6183 usage(B_FALSE); 6184 } 6185 6186 if ((zhp = zfs_open(g_zfs, argv[0], 6187 ZFS_TYPE_FILESYSTEM)) == NULL) { 6188 ret = 1; 6189 } else { 6190 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE, 6191 options); 6192 zfs_close(zhp); 6193 } 6194 } 6195 6196 return (ret); 6197 } 6198 6199 /* 6200 * zfs mount -a [nfs] 6201 * zfs mount filesystem 6202 * 6203 * Mount all filesystems, or mount the given filesystem. 6204 */ 6205 static int 6206 zfs_do_mount(int argc, char **argv) 6207 { 6208 return (share_mount(OP_MOUNT, argc, argv)); 6209 } 6210 6211 /* 6212 * zfs share -a [nfs | smb] 6213 * zfs share filesystem 6214 * 6215 * Share all filesystems, or share the given filesystem. 6216 */ 6217 static int 6218 zfs_do_share(int argc, char **argv) 6219 { 6220 return (share_mount(OP_SHARE, argc, argv)); 6221 } 6222 6223 typedef struct unshare_unmount_node { 6224 zfs_handle_t *un_zhp; 6225 char *un_mountp; 6226 uu_avl_node_t un_avlnode; 6227 } unshare_unmount_node_t; 6228 6229 /* ARGSUSED */ 6230 static int 6231 unshare_unmount_compare(const void *larg, const void *rarg, void *unused) 6232 { 6233 const unshare_unmount_node_t *l = larg; 6234 const unshare_unmount_node_t *r = rarg; 6235 6236 return (strcmp(l->un_mountp, r->un_mountp)); 6237 } 6238 6239 /* 6240 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an 6241 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem, 6242 * and unmount it appropriately. 6243 */ 6244 static int 6245 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual) 6246 { 6247 zfs_handle_t *zhp; 6248 int ret = 0; 6249 struct stat64 statbuf; 6250 struct extmnttab entry; 6251 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount"; 6252 ino_t path_inode; 6253 6254 /* 6255 * Search for the path in /etc/mnttab. Rather than looking for the 6256 * specific path, which can be fooled by non-standard paths (i.e. ".." 6257 * or "//"), we stat() the path and search for the corresponding 6258 * (major,minor) device pair. 6259 */ 6260 if (stat64(path, &statbuf) != 0) { 6261 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 6262 cmdname, path, strerror(errno)); 6263 return (1); 6264 } 6265 path_inode = statbuf.st_ino; 6266 6267 /* 6268 * Search for the given (major,minor) pair in the mount table. 6269 */ 6270 rewind(mnttab_file); 6271 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) { 6272 if (entry.mnt_major == major(statbuf.st_dev) && 6273 entry.mnt_minor == minor(statbuf.st_dev)) 6274 break; 6275 } 6276 if (ret != 0) { 6277 if (op == OP_SHARE) { 6278 (void) fprintf(stderr, gettext("cannot %s '%s': not " 6279 "currently mounted\n"), cmdname, path); 6280 return (1); 6281 } 6282 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"), 6283 path); 6284 if ((ret = umount2(path, flags)) != 0) 6285 (void) fprintf(stderr, gettext("%s: %s\n"), path, 6286 strerror(errno)); 6287 return (ret != 0); 6288 } 6289 6290 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) { 6291 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS " 6292 "filesystem\n"), cmdname, path); 6293 return (1); 6294 } 6295 6296 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 6297 ZFS_TYPE_FILESYSTEM)) == NULL) 6298 return (1); 6299 6300 ret = 1; 6301 if (stat64(entry.mnt_mountp, &statbuf) != 0) { 6302 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"), 6303 cmdname, path, strerror(errno)); 6304 goto out; 6305 } else if (statbuf.st_ino != path_inode) { 6306 (void) fprintf(stderr, gettext("cannot " 6307 "%s '%s': not a mountpoint\n"), cmdname, path); 6308 goto out; 6309 } 6310 6311 if (op == OP_SHARE) { 6312 char nfs_mnt_prop[ZFS_MAXPROPLEN]; 6313 char smbshare_prop[ZFS_MAXPROPLEN]; 6314 6315 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop, 6316 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0); 6317 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop, 6318 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0); 6319 6320 if (strcmp(nfs_mnt_prop, "off") == 0 && 6321 strcmp(smbshare_prop, "off") == 0) { 6322 (void) fprintf(stderr, gettext("cannot unshare " 6323 "'%s': legacy share\n"), path); 6324 (void) fprintf(stderr, gettext("use " 6325 "unshare(1M) to unshare this filesystem\n")); 6326 } else if (!zfs_is_shared(zhp)) { 6327 (void) fprintf(stderr, gettext("cannot unshare '%s': " 6328 "not currently shared\n"), path); 6329 } else { 6330 ret = zfs_unshareall_bypath(zhp, path); 6331 } 6332 } else { 6333 char mtpt_prop[ZFS_MAXPROPLEN]; 6334 6335 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop, 6336 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0); 6337 6338 if (is_manual) { 6339 ret = zfs_unmount(zhp, NULL, flags); 6340 } else if (strcmp(mtpt_prop, "legacy") == 0) { 6341 (void) fprintf(stderr, gettext("cannot unmount " 6342 "'%s': legacy mountpoint\n"), 6343 zfs_get_name(zhp)); 6344 (void) fprintf(stderr, gettext("use umount(1M) " 6345 "to unmount this filesystem\n")); 6346 } else { 6347 ret = zfs_unmountall(zhp, flags); 6348 } 6349 } 6350 6351 out: 6352 zfs_close(zhp); 6353 6354 return (ret != 0); 6355 } 6356 6357 /* 6358 * Generic callback for unsharing or unmounting a filesystem. 6359 */ 6360 static int 6361 unshare_unmount(int op, int argc, char **argv) 6362 { 6363 int do_all = 0; 6364 int flags = 0; 6365 int ret = 0; 6366 int c; 6367 zfs_handle_t *zhp; 6368 char nfs_mnt_prop[ZFS_MAXPROPLEN]; 6369 char sharesmb[ZFS_MAXPROPLEN]; 6370 6371 /* check options */ 6372 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) { 6373 switch (c) { 6374 case 'a': 6375 do_all = 1; 6376 break; 6377 case 'f': 6378 flags = MS_FORCE; 6379 break; 6380 case '?': 6381 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6382 optopt); 6383 usage(B_FALSE); 6384 } 6385 } 6386 6387 argc -= optind; 6388 argv += optind; 6389 6390 if (do_all) { 6391 /* 6392 * We could make use of zfs_for_each() to walk all datasets in 6393 * the system, but this would be very inefficient, especially 6394 * since we would have to linearly search /etc/mnttab for each 6395 * one. Instead, do one pass through /etc/mnttab looking for 6396 * zfs entries and call zfs_unmount() for each one. 6397 * 6398 * Things get a little tricky if the administrator has created 6399 * mountpoints beneath other ZFS filesystems. In this case, we 6400 * have to unmount the deepest filesystems first. To accomplish 6401 * this, we place all the mountpoints in an AVL tree sorted by 6402 * the special type (dataset name), and walk the result in 6403 * reverse to make sure to get any snapshots first. 6404 */ 6405 struct mnttab entry; 6406 uu_avl_pool_t *pool; 6407 uu_avl_t *tree = NULL; 6408 unshare_unmount_node_t *node; 6409 uu_avl_index_t idx; 6410 uu_avl_walk_t *walk; 6411 6412 if (argc != 0) { 6413 (void) fprintf(stderr, gettext("too many arguments\n")); 6414 usage(B_FALSE); 6415 } 6416 6417 if (((pool = uu_avl_pool_create("unmount_pool", 6418 sizeof (unshare_unmount_node_t), 6419 offsetof(unshare_unmount_node_t, un_avlnode), 6420 unshare_unmount_compare, UU_DEFAULT)) == NULL) || 6421 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL)) 6422 nomem(); 6423 6424 rewind(mnttab_file); 6425 while (getmntent(mnttab_file, &entry) == 0) { 6426 6427 /* ignore non-ZFS entries */ 6428 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 6429 continue; 6430 6431 /* ignore snapshots */ 6432 if (strchr(entry.mnt_special, '@') != NULL) 6433 continue; 6434 6435 if ((zhp = zfs_open(g_zfs, entry.mnt_special, 6436 ZFS_TYPE_FILESYSTEM)) == NULL) { 6437 ret = 1; 6438 continue; 6439 } 6440 6441 /* 6442 * Ignore datasets that are excluded/restricted by 6443 * parent pool name. 6444 */ 6445 if (zpool_skip_pool(zfs_get_pool_name(zhp))) { 6446 zfs_close(zhp); 6447 continue; 6448 } 6449 6450 switch (op) { 6451 case OP_SHARE: 6452 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 6453 nfs_mnt_prop, 6454 sizeof (nfs_mnt_prop), 6455 NULL, NULL, 0, B_FALSE) == 0); 6456 if (strcmp(nfs_mnt_prop, "off") != 0) 6457 break; 6458 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 6459 nfs_mnt_prop, 6460 sizeof (nfs_mnt_prop), 6461 NULL, NULL, 0, B_FALSE) == 0); 6462 if (strcmp(nfs_mnt_prop, "off") == 0) 6463 continue; 6464 break; 6465 case OP_MOUNT: 6466 /* Ignore legacy mounts */ 6467 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, 6468 nfs_mnt_prop, 6469 sizeof (nfs_mnt_prop), 6470 NULL, NULL, 0, B_FALSE) == 0); 6471 if (strcmp(nfs_mnt_prop, "legacy") == 0) 6472 continue; 6473 /* Ignore canmount=noauto mounts */ 6474 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == 6475 ZFS_CANMOUNT_NOAUTO) 6476 continue; 6477 default: 6478 break; 6479 } 6480 6481 node = safe_malloc(sizeof (unshare_unmount_node_t)); 6482 node->un_zhp = zhp; 6483 node->un_mountp = safe_strdup(entry.mnt_mountp); 6484 6485 uu_avl_node_init(node, &node->un_avlnode, pool); 6486 6487 if (uu_avl_find(tree, node, NULL, &idx) == NULL) { 6488 uu_avl_insert(tree, node, idx); 6489 } else { 6490 zfs_close(node->un_zhp); 6491 free(node->un_mountp); 6492 free(node); 6493 } 6494 } 6495 6496 /* 6497 * Walk the AVL tree in reverse, unmounting each filesystem and 6498 * removing it from the AVL tree in the process. 6499 */ 6500 if ((walk = uu_avl_walk_start(tree, 6501 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL) 6502 nomem(); 6503 6504 while ((node = uu_avl_walk_next(walk)) != NULL) { 6505 uu_avl_remove(tree, node); 6506 6507 switch (op) { 6508 case OP_SHARE: 6509 if (zfs_unshareall_bypath(node->un_zhp, 6510 node->un_mountp) != 0) 6511 ret = 1; 6512 break; 6513 6514 case OP_MOUNT: 6515 if (zfs_unmount(node->un_zhp, 6516 node->un_mountp, flags) != 0) 6517 ret = 1; 6518 break; 6519 } 6520 6521 zfs_close(node->un_zhp); 6522 free(node->un_mountp); 6523 free(node); 6524 } 6525 6526 uu_avl_walk_end(walk); 6527 uu_avl_destroy(tree); 6528 uu_avl_pool_destroy(pool); 6529 6530 } else { 6531 if (argc != 1) { 6532 if (argc == 0) 6533 (void) fprintf(stderr, 6534 gettext("missing filesystem argument\n")); 6535 else 6536 (void) fprintf(stderr, 6537 gettext("too many arguments\n")); 6538 usage(B_FALSE); 6539 } 6540 6541 /* 6542 * We have an argument, but it may be a full path or a ZFS 6543 * filesystem. Pass full paths off to unmount_path() (shared by 6544 * manual_unmount), otherwise open the filesystem and pass to 6545 * zfs_unmount(). 6546 */ 6547 if (argv[0][0] == '/') 6548 return (unshare_unmount_path(op, argv[0], 6549 flags, B_FALSE)); 6550 6551 if ((zhp = zfs_open(g_zfs, argv[0], 6552 ZFS_TYPE_FILESYSTEM)) == NULL) 6553 return (1); 6554 6555 verify(zfs_prop_get(zhp, op == OP_SHARE ? 6556 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT, 6557 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL, 6558 NULL, 0, B_FALSE) == 0); 6559 6560 switch (op) { 6561 case OP_SHARE: 6562 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, 6563 nfs_mnt_prop, 6564 sizeof (nfs_mnt_prop), 6565 NULL, NULL, 0, B_FALSE) == 0); 6566 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, 6567 sharesmb, sizeof (sharesmb), NULL, NULL, 6568 0, B_FALSE) == 0); 6569 6570 if (strcmp(nfs_mnt_prop, "off") == 0 && 6571 strcmp(sharesmb, "off") == 0) { 6572 (void) fprintf(stderr, gettext("cannot " 6573 "unshare '%s': legacy share\n"), 6574 zfs_get_name(zhp)); 6575 (void) fprintf(stderr, gettext("use " 6576 "unshare(1M) to unshare this " 6577 "filesystem\n")); 6578 ret = 1; 6579 } else if (!zfs_is_shared(zhp)) { 6580 (void) fprintf(stderr, gettext("cannot " 6581 "unshare '%s': not currently " 6582 "shared\n"), zfs_get_name(zhp)); 6583 ret = 1; 6584 } else if (zfs_unshareall(zhp) != 0) { 6585 ret = 1; 6586 } 6587 break; 6588 6589 case OP_MOUNT: 6590 if (strcmp(nfs_mnt_prop, "legacy") == 0) { 6591 (void) fprintf(stderr, gettext("cannot " 6592 "unmount '%s': legacy " 6593 "mountpoint\n"), zfs_get_name(zhp)); 6594 (void) fprintf(stderr, gettext("use " 6595 "umount(1M) to unmount this " 6596 "filesystem\n")); 6597 ret = 1; 6598 } else if (!zfs_is_mounted(zhp, NULL)) { 6599 (void) fprintf(stderr, gettext("cannot " 6600 "unmount '%s': not currently " 6601 "mounted\n"), 6602 zfs_get_name(zhp)); 6603 ret = 1; 6604 } else if (zfs_unmountall(zhp, flags) != 0) { 6605 ret = 1; 6606 } 6607 break; 6608 } 6609 6610 zfs_close(zhp); 6611 } 6612 6613 return (ret); 6614 } 6615 6616 /* 6617 * zfs unmount -a 6618 * zfs unmount filesystem 6619 * 6620 * Unmount all filesystems, or a specific ZFS filesystem. 6621 */ 6622 static int 6623 zfs_do_unmount(int argc, char **argv) 6624 { 6625 return (unshare_unmount(OP_MOUNT, argc, argv)); 6626 } 6627 6628 /* 6629 * zfs unshare -a 6630 * zfs unshare filesystem 6631 * 6632 * Unshare all filesystems, or a specific ZFS filesystem. 6633 */ 6634 static int 6635 zfs_do_unshare(int argc, char **argv) 6636 { 6637 return (unshare_unmount(OP_SHARE, argc, argv)); 6638 } 6639 6640 /* 6641 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is 6642 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'. 6643 */ 6644 static int 6645 manual_mount(int argc, char **argv) 6646 { 6647 zfs_handle_t *zhp; 6648 char mountpoint[ZFS_MAXPROPLEN]; 6649 char mntopts[MNT_LINE_MAX] = { '\0' }; 6650 int ret = 0; 6651 int c; 6652 int flags = 0; 6653 char *dataset, *path; 6654 6655 /* check options */ 6656 while ((c = getopt(argc, argv, ":mo:O")) != -1) { 6657 switch (c) { 6658 case 'o': 6659 (void) strlcpy(mntopts, optarg, sizeof (mntopts)); 6660 break; 6661 case 'O': 6662 flags |= MS_OVERLAY; 6663 break; 6664 case 'm': 6665 flags |= MS_NOMNTTAB; 6666 break; 6667 case ':': 6668 (void) fprintf(stderr, gettext("missing argument for " 6669 "'%c' option\n"), optopt); 6670 usage(B_FALSE); 6671 break; 6672 case '?': 6673 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6674 optopt); 6675 (void) fprintf(stderr, gettext("usage: mount [-o opts] " 6676 "<path>\n")); 6677 return (2); 6678 } 6679 } 6680 6681 argc -= optind; 6682 argv += optind; 6683 6684 /* check that we only have two arguments */ 6685 if (argc != 2) { 6686 if (argc == 0) 6687 (void) fprintf(stderr, gettext("missing dataset " 6688 "argument\n")); 6689 else if (argc == 1) 6690 (void) fprintf(stderr, 6691 gettext("missing mountpoint argument\n")); 6692 else 6693 (void) fprintf(stderr, gettext("too many arguments\n")); 6694 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n"); 6695 return (2); 6696 } 6697 6698 dataset = argv[0]; 6699 path = argv[1]; 6700 6701 /* try to open the dataset */ 6702 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) 6703 return (1); 6704 6705 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint, 6706 sizeof (mountpoint), NULL, NULL, 0, B_FALSE); 6707 6708 /* check for legacy mountpoint and complain appropriately */ 6709 ret = 0; 6710 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) { 6711 if (mount(dataset, path, MS_OPTIONSTR | flags, MNTTYPE_ZFS, 6712 NULL, 0, mntopts, sizeof (mntopts)) != 0) { 6713 (void) fprintf(stderr, gettext("mount failed: %s\n"), 6714 strerror(errno)); 6715 ret = 1; 6716 } 6717 } else { 6718 (void) fprintf(stderr, gettext("filesystem '%s' cannot be " 6719 "mounted using 'mount -F zfs'\n"), dataset); 6720 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' " 6721 "instead.\n"), path); 6722 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' " 6723 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n")); 6724 (void) fprintf(stderr, gettext("See zfs(1M) for more " 6725 "information.\n")); 6726 ret = 1; 6727 } 6728 6729 return (ret); 6730 } 6731 6732 /* 6733 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow 6734 * unmounts of non-legacy filesystems, as this is the dominant administrative 6735 * interface. 6736 */ 6737 static int 6738 manual_unmount(int argc, char **argv) 6739 { 6740 int flags = 0; 6741 int c; 6742 6743 /* check options */ 6744 while ((c = getopt(argc, argv, "f")) != -1) { 6745 switch (c) { 6746 case 'f': 6747 flags = MS_FORCE; 6748 break; 6749 case '?': 6750 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 6751 optopt); 6752 (void) fprintf(stderr, gettext("usage: unmount [-f] " 6753 "<path>\n")); 6754 return (2); 6755 } 6756 } 6757 6758 argc -= optind; 6759 argv += optind; 6760 6761 /* check arguments */ 6762 if (argc != 1) { 6763 if (argc == 0) 6764 (void) fprintf(stderr, gettext("missing path " 6765 "argument\n")); 6766 else 6767 (void) fprintf(stderr, gettext("too many arguments\n")); 6768 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n")); 6769 return (2); 6770 } 6771 6772 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE)); 6773 } 6774 6775 static int 6776 find_command_idx(char *command, int *idx) 6777 { 6778 int i; 6779 6780 for (i = 0; i < NCOMMAND; i++) { 6781 if (command_table[i].name == NULL) 6782 continue; 6783 6784 if (strcmp(command, command_table[i].name) == 0) { 6785 *idx = i; 6786 return (0); 6787 } 6788 } 6789 return (1); 6790 } 6791 6792 static int 6793 zfs_do_diff(int argc, char **argv) 6794 { 6795 zfs_handle_t *zhp; 6796 int flags = 0; 6797 char *tosnap = NULL; 6798 char *fromsnap = NULL; 6799 char *atp, *copy; 6800 int err = 0; 6801 int c; 6802 6803 while ((c = getopt(argc, argv, "FHt")) != -1) { 6804 switch (c) { 6805 case 'F': 6806 flags |= ZFS_DIFF_CLASSIFY; 6807 break; 6808 case 'H': 6809 flags |= ZFS_DIFF_PARSEABLE; 6810 break; 6811 case 't': 6812 flags |= ZFS_DIFF_TIMESTAMP; 6813 break; 6814 default: 6815 (void) fprintf(stderr, 6816 gettext("invalid option '%c'\n"), optopt); 6817 usage(B_FALSE); 6818 } 6819 } 6820 6821 argc -= optind; 6822 argv += optind; 6823 6824 if (argc < 1) { 6825 (void) fprintf(stderr, 6826 gettext("must provide at least one snapshot name\n")); 6827 usage(B_FALSE); 6828 } 6829 6830 if (argc > 2) { 6831 (void) fprintf(stderr, gettext("too many arguments\n")); 6832 usage(B_FALSE); 6833 } 6834 6835 fromsnap = argv[0]; 6836 tosnap = (argc == 2) ? argv[1] : NULL; 6837 6838 copy = NULL; 6839 if (*fromsnap != '@') 6840 copy = strdup(fromsnap); 6841 else if (tosnap) 6842 copy = strdup(tosnap); 6843 if (copy == NULL) 6844 usage(B_FALSE); 6845 6846 if ((atp = strchr(copy, '@')) != NULL) 6847 *atp = '\0'; 6848 6849 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) 6850 return (1); 6851 6852 free(copy); 6853 6854 /* 6855 * Ignore SIGPIPE so that the library can give us 6856 * information on any failure 6857 */ 6858 (void) sigignore(SIGPIPE); 6859 6860 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags); 6861 6862 zfs_close(zhp); 6863 6864 return (err != 0); 6865 } 6866 6867 /* 6868 * zfs bookmark <fs@snap> <fs#bmark> 6869 * 6870 * Creates a bookmark with the given name from the given snapshot. 6871 */ 6872 static int 6873 zfs_do_bookmark(int argc, char **argv) 6874 { 6875 char snapname[ZFS_MAX_DATASET_NAME_LEN]; 6876 zfs_handle_t *zhp; 6877 nvlist_t *nvl; 6878 int ret = 0; 6879 int c; 6880 6881 /* check options */ 6882 while ((c = getopt(argc, argv, "")) != -1) { 6883 switch (c) { 6884 case '?': 6885 (void) fprintf(stderr, 6886 gettext("invalid option '%c'\n"), optopt); 6887 goto usage; 6888 } 6889 } 6890 6891 argc -= optind; 6892 argv += optind; 6893 6894 /* check number of arguments */ 6895 if (argc < 1) { 6896 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 6897 goto usage; 6898 } 6899 if (argc < 2) { 6900 (void) fprintf(stderr, gettext("missing bookmark argument\n")); 6901 goto usage; 6902 } 6903 6904 if (strchr(argv[1], '#') == NULL) { 6905 (void) fprintf(stderr, 6906 gettext("invalid bookmark name '%s' -- " 6907 "must contain a '#'\n"), argv[1]); 6908 goto usage; 6909 } 6910 6911 if (argv[0][0] == '@') { 6912 /* 6913 * Snapshot name begins with @. 6914 * Default to same fs as bookmark. 6915 */ 6916 (void) strncpy(snapname, argv[1], sizeof (snapname)); 6917 *strchr(snapname, '#') = '\0'; 6918 (void) strlcat(snapname, argv[0], sizeof (snapname)); 6919 } else { 6920 (void) strncpy(snapname, argv[0], sizeof (snapname)); 6921 } 6922 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT); 6923 if (zhp == NULL) 6924 goto usage; 6925 zfs_close(zhp); 6926 6927 6928 nvl = fnvlist_alloc(); 6929 fnvlist_add_string(nvl, argv[1], snapname); 6930 ret = lzc_bookmark(nvl, NULL); 6931 fnvlist_free(nvl); 6932 6933 if (ret != 0) { 6934 const char *err_msg; 6935 char errbuf[1024]; 6936 6937 (void) snprintf(errbuf, sizeof (errbuf), 6938 dgettext(TEXT_DOMAIN, 6939 "cannot create bookmark '%s'"), argv[1]); 6940 6941 switch (ret) { 6942 case EXDEV: 6943 err_msg = "bookmark is in a different pool"; 6944 break; 6945 case EEXIST: 6946 err_msg = "bookmark exists"; 6947 break; 6948 case EINVAL: 6949 err_msg = "invalid argument"; 6950 break; 6951 case ENOTSUP: 6952 err_msg = "bookmark feature not enabled"; 6953 break; 6954 case ENOSPC: 6955 err_msg = "out of space"; 6956 break; 6957 default: 6958 err_msg = "unknown error"; 6959 break; 6960 } 6961 (void) fprintf(stderr, "%s: %s\n", errbuf, 6962 dgettext(TEXT_DOMAIN, err_msg)); 6963 } 6964 6965 return (ret != 0); 6966 6967 usage: 6968 usage(B_FALSE); 6969 return (-1); 6970 } 6971 6972 int 6973 main(int argc, char **argv) 6974 { 6975 int ret = 0; 6976 int i; 6977 char *progname; 6978 char *cmdname; 6979 6980 (void) setlocale(LC_ALL, ""); 6981 (void) textdomain(TEXT_DOMAIN); 6982 6983 opterr = 0; 6984 6985 if ((g_zfs = libzfs_init()) == NULL) { 6986 (void) fprintf(stderr, gettext("internal error: failed to " 6987 "initialize ZFS library\n")); 6988 return (1); 6989 } 6990 6991 zfs_save_arguments(argc, argv, history_str, sizeof (history_str)); 6992 6993 libzfs_print_on_error(g_zfs, B_TRUE); 6994 6995 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) { 6996 (void) fprintf(stderr, gettext("internal error: unable to " 6997 "open %s\n"), MNTTAB); 6998 return (1); 6999 } 7000 7001 /* 7002 * This command also doubles as the /etc/fs mount and unmount program. 7003 * Determine if we should take this behavior based on argv[0]. 7004 */ 7005 progname = basename(argv[0]); 7006 if (strcmp(progname, "mount") == 0) { 7007 ret = manual_mount(argc, argv); 7008 } else if (strcmp(progname, "umount") == 0) { 7009 ret = manual_unmount(argc, argv); 7010 } else { 7011 /* 7012 * Make sure the user has specified some command. 7013 */ 7014 if (argc < 2) { 7015 (void) fprintf(stderr, gettext("missing command\n")); 7016 usage(B_FALSE); 7017 } 7018 7019 cmdname = argv[1]; 7020 7021 /* 7022 * The 'umount' command is an alias for 'unmount' 7023 */ 7024 if (strcmp(cmdname, "umount") == 0) 7025 cmdname = "unmount"; 7026 7027 /* 7028 * The 'recv' command is an alias for 'receive' 7029 */ 7030 if (strcmp(cmdname, "recv") == 0) 7031 cmdname = "receive"; 7032 7033 /* 7034 * The 'snap' command is an alias for 'snapshot' 7035 */ 7036 if (strcmp(cmdname, "snap") == 0) 7037 cmdname = "snapshot"; 7038 7039 /* 7040 * Special case '-?' 7041 */ 7042 if (strcmp(cmdname, "-?") == 0) 7043 usage(B_TRUE); 7044 7045 /* 7046 * Run the appropriate command. 7047 */ 7048 libzfs_mnttab_cache(g_zfs, B_TRUE); 7049 if (find_command_idx(cmdname, &i) == 0) { 7050 current_command = &command_table[i]; 7051 ret = command_table[i].func(argc - 1, argv + 1); 7052 } else if (strchr(cmdname, '=') != NULL) { 7053 verify(find_command_idx("set", &i) == 0); 7054 current_command = &command_table[i]; 7055 ret = command_table[i].func(argc, argv); 7056 } else { 7057 (void) fprintf(stderr, gettext("unrecognized " 7058 "command '%s'\n"), cmdname); 7059 usage(B_FALSE); 7060 } 7061 libzfs_mnttab_cache(g_zfs, B_FALSE); 7062 } 7063 7064 (void) fclose(mnttab_file); 7065 7066 if (ret == 0 && log_history) 7067 (void) zpool_log_history(g_zfs, history_str); 7068 7069 libzfs_fini(g_zfs); 7070 7071 /* 7072 * The 'ZFS_ABORT' environment variable causes us to dump core on exit 7073 * for the purposes of running ::findleaks. 7074 */ 7075 if (getenv("ZFS_ABORT") != NULL) { 7076 (void) printf("dumping core by request\n"); 7077 abort(); 7078 } 7079 7080 return (ret); 7081 } 7082