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