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