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