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