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