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