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