zfs_main.c revision 295844
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 */ 33 34#include <assert.h> 35#include <ctype.h> 36#include <errno.h> 37#include <libgen.h> 38#include <libintl.h> 39#include <libuutil.h> 40#include <libnvpair.h> 41#include <locale.h> 42#include <stddef.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <strings.h> 46#include <unistd.h> 47#include <fcntl.h> 48#include <zone.h> 49#include <grp.h> 50#include <pwd.h> 51#include <signal.h> 52#include <sys/list.h> 53#include <sys/mntent.h> 54#include <sys/mnttab.h> 55#include <sys/mount.h> 56#include <sys/stat.h> 57#include <sys/fs/zfs.h> 58#include <sys/types.h> 59#include <time.h> 60#include <err.h> 61#include <jail.h> 62 63#include <libzfs.h> 64#include <libzfs_core.h> 65#include <zfs_prop.h> 66#include <zfs_deleg.h> 67#include <libuutil.h> 68#ifdef illumos 69#include <aclutils.h> 70#include <directory.h> 71#include <idmap.h> 72#endif 73 74#include "zfs_iter.h" 75#include "zfs_util.h" 76#include "zfs_comutil.h" 77 78libzfs_handle_t *g_zfs; 79 80static FILE *mnttab_file; 81static char history_str[HIS_MAX_RECORD_LEN]; 82static boolean_t log_history = B_TRUE; 83 84static int zfs_do_clone(int argc, char **argv); 85static int zfs_do_create(int argc, char **argv); 86static int zfs_do_destroy(int argc, char **argv); 87static int zfs_do_get(int argc, char **argv); 88static int zfs_do_inherit(int argc, char **argv); 89static int zfs_do_list(int argc, char **argv); 90static int zfs_do_mount(int argc, char **argv); 91static int zfs_do_rename(int argc, char **argv); 92static int zfs_do_rollback(int argc, char **argv); 93static int zfs_do_set(int argc, char **argv); 94static int zfs_do_upgrade(int argc, char **argv); 95static int zfs_do_snapshot(int argc, char **argv); 96static int zfs_do_unmount(int argc, char **argv); 97static int zfs_do_share(int argc, char **argv); 98static int zfs_do_unshare(int argc, char **argv); 99static int zfs_do_send(int argc, char **argv); 100static int zfs_do_receive(int argc, char **argv); 101static int zfs_do_promote(int argc, char **argv); 102static int zfs_do_userspace(int argc, char **argv); 103static int zfs_do_allow(int argc, char **argv); 104static int zfs_do_unallow(int argc, char **argv); 105static int zfs_do_hold(int argc, char **argv); 106static int zfs_do_holds(int argc, char **argv); 107static int zfs_do_release(int argc, char **argv); 108static int zfs_do_diff(int argc, char **argv); 109static int zfs_do_jail(int argc, char **argv); 110static int zfs_do_unjail(int argc, char **argv); 111static int zfs_do_bookmark(int argc, char **argv); 112 113/* 114 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds. 115 */ 116 117#ifdef DEBUG 118const char * 119_umem_debug_init(void) 120{ 121 return ("default,verbose"); /* $UMEM_DEBUG setting */ 122} 123 124const char * 125_umem_logging_init(void) 126{ 127 return ("fail,contents"); /* $UMEM_LOGGING setting */ 128} 129#endif 130 131typedef enum { 132 HELP_CLONE, 133 HELP_CREATE, 134 HELP_DESTROY, 135 HELP_GET, 136 HELP_INHERIT, 137 HELP_UPGRADE, 138 HELP_JAIL, 139 HELP_UNJAIL, 140 HELP_LIST, 141 HELP_MOUNT, 142 HELP_PROMOTE, 143 HELP_RECEIVE, 144 HELP_RENAME, 145 HELP_ROLLBACK, 146 HELP_SEND, 147 HELP_SET, 148 HELP_SHARE, 149 HELP_SNAPSHOT, 150 HELP_UNMOUNT, 151 HELP_UNSHARE, 152 HELP_ALLOW, 153 HELP_UNALLOW, 154 HELP_USERSPACE, 155 HELP_GROUPSPACE, 156 HELP_HOLD, 157 HELP_HOLDS, 158 HELP_RELEASE, 159 HELP_DIFF, 160 HELP_BOOKMARK, 161} zfs_help_t; 162 163typedef struct zfs_command { 164 const char *name; 165 int (*func)(int argc, char **argv); 166 zfs_help_t usage; 167} zfs_command_t; 168 169/* 170 * Master command table. Each ZFS command has a name, associated function, and 171 * usage message. The usage messages need to be internationalized, so we have 172 * to have a function to return the usage message based on a command index. 173 * 174 * These commands are organized according to how they are displayed in the usage 175 * message. An empty command (one with a NULL name) indicates an empty line in 176 * the generic usage message. 177 */ 178static zfs_command_t command_table[] = { 179 { "create", zfs_do_create, HELP_CREATE }, 180 { "destroy", zfs_do_destroy, HELP_DESTROY }, 181 { NULL }, 182 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT }, 183 { "rollback", zfs_do_rollback, HELP_ROLLBACK }, 184 { "clone", zfs_do_clone, HELP_CLONE }, 185 { "promote", zfs_do_promote, HELP_PROMOTE }, 186 { "rename", zfs_do_rename, HELP_RENAME }, 187 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK }, 188 { NULL }, 189 { "list", zfs_do_list, HELP_LIST }, 190 { NULL }, 191 { "set", zfs_do_set, HELP_SET }, 192 { "get", zfs_do_get, HELP_GET }, 193 { "inherit", zfs_do_inherit, HELP_INHERIT }, 194 { "upgrade", zfs_do_upgrade, HELP_UPGRADE }, 195 { "userspace", zfs_do_userspace, HELP_USERSPACE }, 196 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE }, 197 { NULL }, 198 { "mount", zfs_do_mount, HELP_MOUNT }, 199 { "unmount", zfs_do_unmount, HELP_UNMOUNT }, 200 { "share", zfs_do_share, HELP_SHARE }, 201 { "unshare", zfs_do_unshare, HELP_UNSHARE }, 202 { NULL }, 203 { "send", zfs_do_send, HELP_SEND }, 204 { "receive", zfs_do_receive, HELP_RECEIVE }, 205 { NULL }, 206 { "allow", zfs_do_allow, HELP_ALLOW }, 207 { NULL }, 208 { "unallow", zfs_do_unallow, HELP_UNALLOW }, 209 { NULL }, 210 { "hold", zfs_do_hold, HELP_HOLD }, 211 { "holds", zfs_do_holds, HELP_HOLDS }, 212 { "release", zfs_do_release, HELP_RELEASE }, 213 { "diff", zfs_do_diff, HELP_DIFF }, 214 { NULL }, 215 { "jail", zfs_do_jail, HELP_JAIL }, 216 { "unjail", zfs_do_unjail, HELP_UNJAIL }, 217}; 218 219#define NCOMMAND (sizeof (command_table) / sizeof (command_table[0])) 220 221zfs_command_t *current_command; 222 223static const char * 224get_usage(zfs_help_t idx) 225{ 226 switch (idx) { 227 case HELP_CLONE: 228 return (gettext("\tclone [-p] [-o property=value] ... " 229 "<snapshot> <filesystem|volume>\n")); 230 case HELP_CREATE: 231 return (gettext("\tcreate [-pu] [-o property=value] ... " 232 "<filesystem>\n" 233 "\tcreate [-ps] [-b blocksize] [-o property=value] ... " 234 "-V <size> <volume>\n")); 235 case HELP_DESTROY: 236 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n" 237 "\tdestroy [-dnpRrv] " 238 "<filesystem|volume>@<snap>[%<snap>][,...]\n" 239 "\tdestroy <filesystem|volume>#<bookmark>\n")); 240 case HELP_GET: 241 return (gettext("\tget [-rHp] [-d max] " 242 "[-o \"all\" | field[,...]]\n" 243 "\t [-t type[,...]] [-s source[,...]]\n" 244 "\t <\"all\" | property[,...]> " 245 "[filesystem|volume|snapshot] ...\n")); 246 case HELP_INHERIT: 247 return (gettext("\tinherit [-rS] <property> " 248 "<filesystem|volume|snapshot> ...\n")); 249 case HELP_UPGRADE: 250 return (gettext("\tupgrade [-v]\n" 251 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n")); 252 case HELP_JAIL: 253 return (gettext("\tjail <jailid|jailname> <filesystem>\n")); 254 case HELP_UNJAIL: 255 return (gettext("\tunjail <jailid|jailname> <filesystem>\n")); 256 case HELP_LIST: 257 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] " 258 "[-s property]...\n\t [-S property]... [-t type[,...]] " 259 "[filesystem|volume|snapshot] ...\n")); 260 case HELP_MOUNT: 261 return (gettext("\tmount\n" 262 "\tmount [-vO] [-o opts] <-a | filesystem>\n")); 263 case HELP_PROMOTE: 264 return (gettext("\tpromote <clone-filesystem>\n")); 265 case HELP_RECEIVE: 266 return (gettext("\treceive|recv [-vnsFu] <filesystem|volume|" 267 "snapshot>\n" 268 "\treceive|recv [-vnsFu] [-o origin=<snapshot>] [-d | -e] " 269 "<filesystem>\n" 270 "\treceive|recv -A <filesystem|volume>\n")); 271 case HELP_RENAME: 272 return (gettext("\trename [-f] <filesystem|volume|snapshot> " 273 "<filesystem|volume|snapshot>\n" 274 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n" 275 "\trename -r <snapshot> <snapshot>\n" 276 "\trename -u [-p] <filesystem> <filesystem>")); 277 case HELP_ROLLBACK: 278 return (gettext("\trollback [-rRf] <snapshot>\n")); 279 case HELP_SEND: 280 return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] " 281 "<snapshot>\n" 282 "\tsend [-Le] [-i snapshot|bookmark] " 283 "<filesystem|volume|snapshot>\n" 284 "\tsend [-nvPe] -t <receive_resume_token>\n")); 285 case HELP_SET: 286 return (gettext("\tset <property=value> ... " 287 "<filesystem|volume|snapshot> ...\n")); 288 case HELP_SHARE: 289 return (gettext("\tshare <-a | filesystem>\n")); 290 case HELP_SNAPSHOT: 291 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... " 292 "<filesystem|volume>@<snap> ...\n")); 293 case HELP_UNMOUNT: 294 return (gettext("\tunmount|umount [-f] " 295 "<-a | filesystem|mountpoint>\n")); 296 case HELP_UNSHARE: 297 return (gettext("\tunshare " 298 "<-a | filesystem|mountpoint>\n")); 299 case HELP_ALLOW: 300 return (gettext("\tallow <filesystem|volume>\n" 301 "\tallow [-ldug] " 302 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n" 303 "\t <filesystem|volume>\n" 304 "\tallow [-ld] -e <perm|@setname>[,...] " 305 "<filesystem|volume>\n" 306 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n" 307 "\tallow -s @setname <perm|@setname>[,...] " 308 "<filesystem|volume>\n")); 309 case HELP_UNALLOW: 310 return (gettext("\tunallow [-rldug] " 311 "<\"everyone\"|user|group>[,...]\n" 312 "\t [<perm|@setname>[,...]] <filesystem|volume>\n" 313 "\tunallow [-rld] -e [<perm|@setname>[,...]] " 314 "<filesystem|volume>\n" 315 "\tunallow [-r] -c [<perm|@setname>[,...]] " 316 "<filesystem|volume>\n" 317 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] " 318 "<filesystem|volume>\n")); 319 case HELP_USERSPACE: 320 return (gettext("\tuserspace [-Hinp] [-o field[,...]] " 321 "[-s field] ...\n" 322 "\t [-S field] ... [-t type[,...]] " 323 "<filesystem|snapshot>\n")); 324 case HELP_GROUPSPACE: 325 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] " 326 "[-s field] ...\n" 327 "\t [-S field] ... [-t type[,...]] " 328 "<filesystem|snapshot>\n")); 329 case HELP_HOLD: 330 return (gettext("\thold [-r] <tag> <snapshot> ...\n")); 331 case HELP_HOLDS: 332 return (gettext("\tholds [-Hp] [-r|-d depth] " 333 "<filesystem|volume|snapshot> ...\n")); 334 case HELP_RELEASE: 335 return (gettext("\trelease [-r] <tag> <snapshot> ...\n")); 336 case HELP_DIFF: 337 return (gettext("\tdiff [-FHt] <snapshot> " 338 "[snapshot|filesystem]\n")); 339 case HELP_BOOKMARK: 340 return (gettext("\tbookmark <snapshot> <bookmark>\n")); 341 } 342 343 abort(); 344 /* NOTREACHED */ 345} 346 347void 348nomem(void) 349{ 350 (void) fprintf(stderr, gettext("internal error: out of memory\n")); 351 exit(1); 352} 353 354/* 355 * Utility function to guarantee malloc() success. 356 */ 357 358void * 359safe_malloc(size_t size) 360{ 361 void *data; 362 363 if ((data = calloc(1, size)) == NULL) 364 nomem(); 365 366 return (data); 367} 368 369static char * 370safe_strdup(char *str) 371{ 372 char *dupstr = strdup(str); 373 374 if (dupstr == NULL) 375 nomem(); 376 377 return (dupstr); 378} 379 380/* 381 * Callback routine that will print out information for each of 382 * the properties. 383 */ 384static int 385usage_prop_cb(int prop, void *cb) 386{ 387 FILE *fp = cb; 388 389 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop)); 390 391 if (zfs_prop_readonly(prop)) 392 (void) fprintf(fp, " NO "); 393 else 394 (void) fprintf(fp, "YES "); 395 396 if (zfs_prop_inheritable(prop)) 397 (void) fprintf(fp, " YES "); 398 else 399 (void) fprintf(fp, " NO "); 400 401 if (zfs_prop_values(prop) == NULL) 402 (void) fprintf(fp, "-\n"); 403 else 404 (void) fprintf(fp, "%s\n", zfs_prop_values(prop)); 405 406 return (ZPROP_CONT); 407} 408 409/* 410 * Display usage message. If we're inside a command, display only the usage for 411 * that command. Otherwise, iterate over the entire command table and display 412 * a complete usage message. 413 */ 414static void 415usage(boolean_t requested) 416{ 417 int i; 418 boolean_t show_properties = B_FALSE; 419 FILE *fp = requested ? stdout : stderr; 420 421 if (current_command == NULL) { 422 423 (void) fprintf(fp, gettext("usage: zfs command args ...\n")); 424 (void) fprintf(fp, 425 gettext("where 'command' is one of the following:\n\n")); 426 427 for (i = 0; i < NCOMMAND; i++) { 428 if (command_table[i].name == NULL) 429 (void) fprintf(fp, "\n"); 430 else 431 (void) fprintf(fp, "%s", 432 get_usage(command_table[i].usage)); 433 } 434 435 (void) fprintf(fp, gettext("\nEach dataset is of the form: " 436 "pool/[dataset/]*dataset[@name]\n")); 437 } else { 438 (void) fprintf(fp, gettext("usage:\n")); 439 (void) fprintf(fp, "%s", get_usage(current_command->usage)); 440 } 441 442 if (current_command != NULL && 443 (strcmp(current_command->name, "set") == 0 || 444 strcmp(current_command->name, "get") == 0 || 445 strcmp(current_command->name, "inherit") == 0 || 446 strcmp(current_command->name, "list") == 0)) 447 show_properties = B_TRUE; 448 449 if (show_properties) { 450 (void) fprintf(fp, 451 gettext("\nThe following properties are supported:\n")); 452 453 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n", 454 "PROPERTY", "EDIT", "INHERIT", "VALUES"); 455 456 /* Iterate over all properties */ 457 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE, 458 ZFS_TYPE_DATASET); 459 460 (void) fprintf(fp, "\t%-15s ", "userused@..."); 461 (void) fprintf(fp, " NO NO <size>\n"); 462 (void) fprintf(fp, "\t%-15s ", "groupused@..."); 463 (void) fprintf(fp, " NO NO <size>\n"); 464 (void) fprintf(fp, "\t%-15s ", "userquota@..."); 465 (void) fprintf(fp, "YES NO <size> | none\n"); 466 (void) fprintf(fp, "\t%-15s ", "groupquota@..."); 467 (void) fprintf(fp, "YES NO <size> | none\n"); 468 (void) fprintf(fp, "\t%-15s ", "written@<snap>"); 469 (void) fprintf(fp, " NO NO <size>\n"); 470 471 (void) fprintf(fp, gettext("\nSizes are specified in bytes " 472 "with standard units such as K, M, G, etc.\n")); 473 (void) fprintf(fp, gettext("\nUser-defined properties can " 474 "be specified by using a name containing a colon (:).\n")); 475 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ " 476 "properties must be appended with\n" 477 "a user or group specifier of one of these forms:\n" 478 " POSIX name (eg: \"matt\")\n" 479 " POSIX id (eg: \"126829\")\n" 480 " SMB name@domain (eg: \"matt@sun\")\n" 481 " SMB SID (eg: \"S-1-234-567-89\")\n")); 482 } else { 483 (void) fprintf(fp, 484 gettext("\nFor the property list, run: %s\n"), 485 "zfs set|get"); 486 (void) fprintf(fp, 487 gettext("\nFor the delegated permission list, run: %s\n"), 488 "zfs allow|unallow"); 489 } 490 491 /* 492 * See comments at end of main(). 493 */ 494 if (getenv("ZFS_ABORT") != NULL) { 495 (void) printf("dumping core by request\n"); 496 abort(); 497 } 498 499 exit(requested ? 0 : 2); 500} 501 502/* 503 * Take a property=value argument string and add it to the given nvlist. 504 * Modifies the argument inplace. 505 */ 506static int 507parseprop(nvlist_t *props, char *propname) 508{ 509 char *propval, *strval; 510 511 if ((propval = strchr(propname, '=')) == NULL) { 512 (void) fprintf(stderr, gettext("missing " 513 "'=' for property=value argument\n")); 514 return (-1); 515 } 516 *propval = '\0'; 517 propval++; 518 if (nvlist_lookup_string(props, propname, &strval) == 0) { 519 (void) fprintf(stderr, gettext("property '%s' " 520 "specified multiple times\n"), propname); 521 return (-1); 522 } 523 if (nvlist_add_string(props, propname, propval) != 0) 524 nomem(); 525 return (0); 526} 527 528static int 529parse_depth(char *opt, int *flags) 530{ 531 char *tmp; 532 int depth; 533 534 depth = (int)strtol(opt, &tmp, 0); 535 if (*tmp) { 536 (void) fprintf(stderr, 537 gettext("%s is not an integer\n"), opt); 538 usage(B_FALSE); 539 } 540 if (depth < 0) { 541 (void) fprintf(stderr, 542 gettext("Depth can not be negative.\n")); 543 usage(B_FALSE); 544 } 545 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE); 546 return (depth); 547} 548 549#define PROGRESS_DELAY 2 /* seconds */ 550 551static 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"; 552static time_t pt_begin; 553static char *pt_header = NULL; 554static boolean_t pt_shown; 555 556static void 557start_progress_timer(void) 558{ 559 pt_begin = time(NULL) + PROGRESS_DELAY; 560 pt_shown = B_FALSE; 561} 562 563static void 564set_progress_header(char *header) 565{ 566 assert(pt_header == NULL); 567 pt_header = safe_strdup(header); 568 if (pt_shown) { 569 (void) printf("%s: ", header); 570 (void) fflush(stdout); 571 } 572} 573 574static void 575update_progress(char *update) 576{ 577 if (!pt_shown && time(NULL) > pt_begin) { 578 int len = strlen(update); 579 580 (void) printf("%s: %s%*.*s", pt_header, update, len, len, 581 pt_reverse); 582 (void) fflush(stdout); 583 pt_shown = B_TRUE; 584 } else if (pt_shown) { 585 int len = strlen(update); 586 587 (void) printf("%s%*.*s", update, len, len, pt_reverse); 588 (void) fflush(stdout); 589 } 590} 591 592static void 593finish_progress(char *done) 594{ 595 if (pt_shown) { 596 (void) printf("%s\n", done); 597 (void) fflush(stdout); 598 } 599 free(pt_header); 600 pt_header = NULL; 601} 602 603/* 604 * Check if the dataset is mountable and should be automatically mounted. 605 */ 606static boolean_t 607should_auto_mount(zfs_handle_t *zhp) 608{ 609 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp))) 610 return (B_FALSE); 611 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON); 612} 613 614/* 615 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol> 616 * 617 * Given an existing dataset, create a writable copy whose initial contents 618 * are the same as the source. The newly created dataset maintains a 619 * dependency on the original; the original cannot be destroyed so long as 620 * the clone exists. 621 * 622 * The '-p' flag creates all the non-existing ancestors of the target first. 623 */ 624static int 625zfs_do_clone(int argc, char **argv) 626{ 627 zfs_handle_t *zhp = NULL; 628 boolean_t parents = B_FALSE; 629 nvlist_t *props; 630 int ret = 0; 631 int c; 632 633 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 634 nomem(); 635 636 /* check options */ 637 while ((c = getopt(argc, argv, "o:p")) != -1) { 638 switch (c) { 639 case 'o': 640 if (parseprop(props, optarg) != 0) 641 return (1); 642 break; 643 case 'p': 644 parents = B_TRUE; 645 break; 646 case '?': 647 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 648 optopt); 649 goto usage; 650 } 651 } 652 653 argc -= optind; 654 argv += optind; 655 656 /* check number of arguments */ 657 if (argc < 1) { 658 (void) fprintf(stderr, gettext("missing source dataset " 659 "argument\n")); 660 goto usage; 661 } 662 if (argc < 2) { 663 (void) fprintf(stderr, gettext("missing target dataset " 664 "argument\n")); 665 goto usage; 666 } 667 if (argc > 2) { 668 (void) fprintf(stderr, gettext("too many arguments\n")); 669 goto usage; 670 } 671 672 /* open the source dataset */ 673 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 674 return (1); 675 676 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM | 677 ZFS_TYPE_VOLUME)) { 678 /* 679 * Now create the ancestors of the target dataset. If the 680 * target already exists and '-p' option was used we should not 681 * complain. 682 */ 683 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | 684 ZFS_TYPE_VOLUME)) 685 return (0); 686 if (zfs_create_ancestors(g_zfs, argv[1]) != 0) 687 return (1); 688 } 689 690 /* pass to libzfs */ 691 ret = zfs_clone(zhp, argv[1], props); 692 693 /* create the mountpoint if necessary */ 694 if (ret == 0) { 695 zfs_handle_t *clone; 696 697 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET); 698 if (clone != NULL) { 699 /* 700 * If the user doesn't want the dataset 701 * automatically mounted, then skip the mount/share 702 * step. 703 */ 704 if (should_auto_mount(clone)) { 705 if ((ret = zfs_mount(clone, NULL, 0)) != 0) { 706 (void) fprintf(stderr, gettext("clone " 707 "successfully created, " 708 "but not mounted\n")); 709 } else if ((ret = zfs_share(clone)) != 0) { 710 (void) fprintf(stderr, gettext("clone " 711 "successfully created, " 712 "but not shared\n")); 713 } 714 } 715 zfs_close(clone); 716 } 717 } 718 719 zfs_close(zhp); 720 nvlist_free(props); 721 722 return (!!ret); 723 724usage: 725 if (zhp) 726 zfs_close(zhp); 727 nvlist_free(props); 728 usage(B_FALSE); 729 return (-1); 730} 731 732/* 733 * zfs create [-pu] [-o prop=value] ... fs 734 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size 735 * 736 * Create a new dataset. This command can be used to create filesystems 737 * and volumes. Snapshot creation is handled by 'zfs snapshot'. 738 * For volumes, the user must specify a size to be used. 739 * 740 * The '-s' flag applies only to volumes, and indicates that we should not try 741 * to set the reservation for this volume. By default we set a reservation 742 * equal to the size for any volume. For pools with SPA_VERSION >= 743 * SPA_VERSION_REFRESERVATION, we set a refreservation instead. 744 * 745 * The '-p' flag creates all the non-existing ancestors of the target first. 746 * 747 * The '-u' flag prevents mounting of newly created file system. 748 */ 749static int 750zfs_do_create(int argc, char **argv) 751{ 752 zfs_type_t type = ZFS_TYPE_FILESYSTEM; 753 zfs_handle_t *zhp = NULL; 754 uint64_t volsize; 755 int c; 756 boolean_t noreserve = B_FALSE; 757 boolean_t bflag = B_FALSE; 758 boolean_t parents = B_FALSE; 759 boolean_t nomount = B_FALSE; 760 int ret = 1; 761 nvlist_t *props; 762 uint64_t intval; 763 764 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 765 nomem(); 766 767 /* check options */ 768 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) { 769 switch (c) { 770 case 'V': 771 type = ZFS_TYPE_VOLUME; 772 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) { 773 (void) fprintf(stderr, gettext("bad volume " 774 "size '%s': %s\n"), optarg, 775 libzfs_error_description(g_zfs)); 776 goto error; 777 } 778 779 if (nvlist_add_uint64(props, 780 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0) 781 nomem(); 782 volsize = intval; 783 break; 784 case 'p': 785 parents = B_TRUE; 786 break; 787 case 'b': 788 bflag = B_TRUE; 789 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) { 790 (void) fprintf(stderr, gettext("bad volume " 791 "block size '%s': %s\n"), optarg, 792 libzfs_error_description(g_zfs)); 793 goto error; 794 } 795 796 if (nvlist_add_uint64(props, 797 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 798 intval) != 0) 799 nomem(); 800 break; 801 case 'o': 802 if (parseprop(props, optarg) != 0) 803 goto error; 804 break; 805 case 's': 806 noreserve = B_TRUE; 807 break; 808 case 'u': 809 nomount = B_TRUE; 810 break; 811 case ':': 812 (void) fprintf(stderr, gettext("missing size " 813 "argument\n")); 814 goto badusage; 815 case '?': 816 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 817 optopt); 818 goto badusage; 819 } 820 } 821 822 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) { 823 (void) fprintf(stderr, gettext("'-s' and '-b' can only be " 824 "used when creating a volume\n")); 825 goto badusage; 826 } 827 if (nomount && type != ZFS_TYPE_FILESYSTEM) { 828 (void) fprintf(stderr, gettext("'-u' can only be " 829 "used when creating a file system\n")); 830 goto badusage; 831 } 832 833 argc -= optind; 834 argv += optind; 835 836 /* check number of arguments */ 837 if (argc == 0) { 838 (void) fprintf(stderr, gettext("missing %s argument\n"), 839 zfs_type_to_name(type)); 840 goto badusage; 841 } 842 if (argc > 1) { 843 (void) fprintf(stderr, gettext("too many arguments\n")); 844 goto badusage; 845 } 846 847 if (type == ZFS_TYPE_VOLUME && !noreserve) { 848 zpool_handle_t *zpool_handle; 849 nvlist_t *real_props; 850 uint64_t spa_version; 851 char *p; 852 zfs_prop_t resv_prop; 853 char *strval; 854 char msg[1024]; 855 856 if (p = strchr(argv[0], '/')) 857 *p = '\0'; 858 zpool_handle = zpool_open(g_zfs, argv[0]); 859 if (p != NULL) 860 *p = '/'; 861 if (zpool_handle == NULL) 862 goto error; 863 spa_version = zpool_get_prop_int(zpool_handle, 864 ZPOOL_PROP_VERSION, NULL); 865 if (spa_version >= SPA_VERSION_REFRESERVATION) 866 resv_prop = ZFS_PROP_REFRESERVATION; 867 else 868 resv_prop = ZFS_PROP_RESERVATION; 869 870 (void) snprintf(msg, sizeof (msg), 871 gettext("cannot create '%s'"), argv[0]); 872 if (props && (real_props = zfs_valid_proplist(g_zfs, type, 873 props, 0, NULL, zpool_handle, msg)) == NULL) { 874 zpool_close(zpool_handle); 875 goto error; 876 } 877 zpool_close(zpool_handle); 878 879 volsize = zvol_volsize_to_reservation(volsize, real_props); 880 nvlist_free(real_props); 881 882 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop), 883 &strval) != 0) { 884 if (nvlist_add_uint64(props, 885 zfs_prop_to_name(resv_prop), volsize) != 0) { 886 nvlist_free(props); 887 nomem(); 888 } 889 } 890 } 891 892 if (parents && zfs_name_valid(argv[0], type)) { 893 /* 894 * Now create the ancestors of target dataset. If the target 895 * already exists and '-p' option was used we should not 896 * complain. 897 */ 898 if (zfs_dataset_exists(g_zfs, argv[0], type)) { 899 ret = 0; 900 goto error; 901 } 902 if (zfs_create_ancestors(g_zfs, argv[0]) != 0) 903 goto error; 904 } 905 906 /* pass to libzfs */ 907 if (zfs_create(g_zfs, argv[0], type, props) != 0) 908 goto error; 909 910 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL) 911 goto error; 912 913 ret = 0; 914 915 /* 916 * Mount and/or share the new filesystem as appropriate. We provide a 917 * verbose error message to let the user know that their filesystem was 918 * in fact created, even if we failed to mount or share it. 919 * If the user doesn't want the dataset automatically mounted, 920 * then skip the mount/share step altogether. 921 */ 922 if (!nomount && should_auto_mount(zhp)) { 923 if (zfs_mount(zhp, NULL, 0) != 0) { 924 (void) fprintf(stderr, gettext("filesystem " 925 "successfully created, but not mounted\n")); 926 ret = 1; 927 } else if (zfs_share(zhp) != 0) { 928 (void) fprintf(stderr, gettext("filesystem " 929 "successfully created, but not shared\n")); 930 ret = 1; 931 } 932 } 933 934error: 935 if (zhp) 936 zfs_close(zhp); 937 nvlist_free(props); 938 return (ret); 939badusage: 940 nvlist_free(props); 941 usage(B_FALSE); 942 return (2); 943} 944 945/* 946 * zfs destroy [-rRf] <fs, vol> 947 * zfs destroy [-rRd] <snap> 948 * 949 * -r Recursively destroy all children 950 * -R Recursively destroy all dependents, including clones 951 * -f Force unmounting of any dependents 952 * -d If we can't destroy now, mark for deferred destruction 953 * 954 * Destroys the given dataset. By default, it will unmount any filesystems, 955 * and refuse to destroy a dataset that has any dependents. A dependent can 956 * either be a child, or a clone of a child. 957 */ 958typedef struct destroy_cbdata { 959 boolean_t cb_first; 960 boolean_t cb_force; 961 boolean_t cb_recurse; 962 boolean_t cb_error; 963 boolean_t cb_doclones; 964 zfs_handle_t *cb_target; 965 boolean_t cb_defer_destroy; 966 boolean_t cb_verbose; 967 boolean_t cb_parsable; 968 boolean_t cb_dryrun; 969 nvlist_t *cb_nvl; 970 nvlist_t *cb_batchedsnaps; 971 972 /* first snap in contiguous run */ 973 char *cb_firstsnap; 974 /* previous snap in contiguous run */ 975 char *cb_prevsnap; 976 int64_t cb_snapused; 977 char *cb_snapspec; 978 char *cb_bookmark; 979} destroy_cbdata_t; 980 981/* 982 * Check for any dependents based on the '-r' or '-R' flags. 983 */ 984static int 985destroy_check_dependent(zfs_handle_t *zhp, void *data) 986{ 987 destroy_cbdata_t *cbp = data; 988 const char *tname = zfs_get_name(cbp->cb_target); 989 const char *name = zfs_get_name(zhp); 990 991 if (strncmp(tname, name, strlen(tname)) == 0 && 992 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) { 993 /* 994 * This is a direct descendant, not a clone somewhere else in 995 * the hierarchy. 996 */ 997 if (cbp->cb_recurse) 998 goto out; 999 1000 if (cbp->cb_first) { 1001 (void) fprintf(stderr, gettext("cannot destroy '%s': " 1002 "%s has children\n"), 1003 zfs_get_name(cbp->cb_target), 1004 zfs_type_to_name(zfs_get_type(cbp->cb_target))); 1005 (void) fprintf(stderr, gettext("use '-r' to destroy " 1006 "the following datasets:\n")); 1007 cbp->cb_first = B_FALSE; 1008 cbp->cb_error = B_TRUE; 1009 } 1010 1011 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 1012 } else { 1013 /* 1014 * This is a clone. We only want to report this if the '-r' 1015 * wasn't specified, or the target is a snapshot. 1016 */ 1017 if (!cbp->cb_recurse && 1018 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT) 1019 goto out; 1020 1021 if (cbp->cb_first) { 1022 (void) fprintf(stderr, gettext("cannot destroy '%s': " 1023 "%s has dependent clones\n"), 1024 zfs_get_name(cbp->cb_target), 1025 zfs_type_to_name(zfs_get_type(cbp->cb_target))); 1026 (void) fprintf(stderr, gettext("use '-R' to destroy " 1027 "the following datasets:\n")); 1028 cbp->cb_first = B_FALSE; 1029 cbp->cb_error = B_TRUE; 1030 cbp->cb_dryrun = B_TRUE; 1031 } 1032 1033 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 1034 } 1035 1036out: 1037 zfs_close(zhp); 1038 return (0); 1039} 1040 1041static int 1042destroy_callback(zfs_handle_t *zhp, void *data) 1043{ 1044 destroy_cbdata_t *cb = data; 1045 const char *name = zfs_get_name(zhp); 1046 1047 if (cb->cb_verbose) { 1048 if (cb->cb_parsable) { 1049 (void) printf("destroy\t%s\n", name); 1050 } else if (cb->cb_dryrun) { 1051 (void) printf(gettext("would destroy %s\n"), 1052 name); 1053 } else { 1054 (void) printf(gettext("will destroy %s\n"), 1055 name); 1056 } 1057 } 1058 1059 /* 1060 * Ignore pools (which we've already flagged as an error before getting 1061 * here). 1062 */ 1063 if (strchr(zfs_get_name(zhp), '/') == NULL && 1064 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 1065 zfs_close(zhp); 1066 return (0); 1067 } 1068 if (cb->cb_dryrun) { 1069 zfs_close(zhp); 1070 return (0); 1071 } 1072 1073 /* 1074 * We batch up all contiguous snapshots (even of different 1075 * filesystems) and destroy them with one ioctl. We can't 1076 * simply do all snap deletions and then all fs deletions, 1077 * because we must delete a clone before its origin. 1078 */ 1079 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) { 1080 fnvlist_add_boolean(cb->cb_batchedsnaps, name); 1081 } else { 1082 int error = zfs_destroy_snaps_nvl(g_zfs, 1083 cb->cb_batchedsnaps, B_FALSE); 1084 fnvlist_free(cb->cb_batchedsnaps); 1085 cb->cb_batchedsnaps = fnvlist_alloc(); 1086 1087 if (error != 0 || 1088 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 || 1089 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) { 1090 zfs_close(zhp); 1091 return (-1); 1092 } 1093 } 1094 1095 zfs_close(zhp); 1096 return (0); 1097} 1098 1099static int 1100destroy_print_cb(zfs_handle_t *zhp, void *arg) 1101{ 1102 destroy_cbdata_t *cb = arg; 1103 const char *name = zfs_get_name(zhp); 1104 int err = 0; 1105 1106 if (nvlist_exists(cb->cb_nvl, name)) { 1107 if (cb->cb_firstsnap == NULL) 1108 cb->cb_firstsnap = strdup(name); 1109 if (cb->cb_prevsnap != NULL) 1110 free(cb->cb_prevsnap); 1111 /* this snap continues the current range */ 1112 cb->cb_prevsnap = strdup(name); 1113 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL) 1114 nomem(); 1115 if (cb->cb_verbose) { 1116 if (cb->cb_parsable) { 1117 (void) printf("destroy\t%s\n", name); 1118 } else if (cb->cb_dryrun) { 1119 (void) printf(gettext("would destroy %s\n"), 1120 name); 1121 } else { 1122 (void) printf(gettext("will destroy %s\n"), 1123 name); 1124 } 1125 } 1126 } else if (cb->cb_firstsnap != NULL) { 1127 /* end of this range */ 1128 uint64_t used = 0; 1129 err = lzc_snaprange_space(cb->cb_firstsnap, 1130 cb->cb_prevsnap, &used); 1131 cb->cb_snapused += used; 1132 free(cb->cb_firstsnap); 1133 cb->cb_firstsnap = NULL; 1134 free(cb->cb_prevsnap); 1135 cb->cb_prevsnap = NULL; 1136 } 1137 zfs_close(zhp); 1138 return (err); 1139} 1140 1141static int 1142destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb) 1143{ 1144 int err = 0; 1145 assert(cb->cb_firstsnap == NULL); 1146 assert(cb->cb_prevsnap == NULL); 1147 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb); 1148 if (cb->cb_firstsnap != NULL) { 1149 uint64_t used = 0; 1150 if (err == 0) { 1151 err = lzc_snaprange_space(cb->cb_firstsnap, 1152 cb->cb_prevsnap, &used); 1153 } 1154 cb->cb_snapused += used; 1155 free(cb->cb_firstsnap); 1156 cb->cb_firstsnap = NULL; 1157 free(cb->cb_prevsnap); 1158 cb->cb_prevsnap = NULL; 1159 } 1160 return (err); 1161} 1162 1163static int 1164snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg) 1165{ 1166 destroy_cbdata_t *cb = arg; 1167 int err = 0; 1168 1169 /* Check for clones. */ 1170 if (!cb->cb_doclones && !cb->cb_defer_destroy) { 1171 cb->cb_target = zhp; 1172 cb->cb_first = B_TRUE; 1173 err = zfs_iter_dependents(zhp, B_TRUE, 1174 destroy_check_dependent, cb); 1175 } 1176 1177 if (err == 0) { 1178 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp))) 1179 nomem(); 1180 } 1181 zfs_close(zhp); 1182 return (err); 1183} 1184 1185static int 1186gather_snapshots(zfs_handle_t *zhp, void *arg) 1187{ 1188 destroy_cbdata_t *cb = arg; 1189 int err = 0; 1190 1191 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb); 1192 if (err == ENOENT) 1193 err = 0; 1194 if (err != 0) 1195 goto out; 1196 1197 if (cb->cb_verbose) { 1198 err = destroy_print_snapshots(zhp, cb); 1199 if (err != 0) 1200 goto out; 1201 } 1202 1203 if (cb->cb_recurse) 1204 err = zfs_iter_filesystems(zhp, gather_snapshots, cb); 1205 1206out: 1207 zfs_close(zhp); 1208 return (err); 1209} 1210 1211static int 1212destroy_clones(destroy_cbdata_t *cb) 1213{ 1214 nvpair_t *pair; 1215 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL); 1216 pair != NULL; 1217 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) { 1218 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair), 1219 ZFS_TYPE_SNAPSHOT); 1220 if (zhp != NULL) { 1221 boolean_t defer = cb->cb_defer_destroy; 1222 int err = 0; 1223 1224 /* 1225 * We can't defer destroy non-snapshots, so set it to 1226 * false while destroying the clones. 1227 */ 1228 cb->cb_defer_destroy = B_FALSE; 1229 err = zfs_iter_dependents(zhp, B_FALSE, 1230 destroy_callback, cb); 1231 cb->cb_defer_destroy = defer; 1232 zfs_close(zhp); 1233 if (err != 0) 1234 return (err); 1235 } 1236 } 1237 return (0); 1238} 1239 1240static int 1241zfs_do_destroy(int argc, char **argv) 1242{ 1243 destroy_cbdata_t cb = { 0 }; 1244 int rv = 0; 1245 int err = 0; 1246 int c; 1247 zfs_handle_t *zhp = NULL; 1248 char *at, *pound; 1249 zfs_type_t type = ZFS_TYPE_DATASET; 1250 1251 /* check options */ 1252 while ((c = getopt(argc, argv, "vpndfrR")) != -1) { 1253 switch (c) { 1254 case 'v': 1255 cb.cb_verbose = B_TRUE; 1256 break; 1257 case 'p': 1258 cb.cb_verbose = B_TRUE; 1259 cb.cb_parsable = B_TRUE; 1260 break; 1261 case 'n': 1262 cb.cb_dryrun = B_TRUE; 1263 break; 1264 case 'd': 1265 cb.cb_defer_destroy = B_TRUE; 1266 type = ZFS_TYPE_SNAPSHOT; 1267 break; 1268 case 'f': 1269 cb.cb_force = B_TRUE; 1270 break; 1271 case 'r': 1272 cb.cb_recurse = B_TRUE; 1273 break; 1274 case 'R': 1275 cb.cb_recurse = B_TRUE; 1276 cb.cb_doclones = B_TRUE; 1277 break; 1278 case '?': 1279 default: 1280 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1281 optopt); 1282 usage(B_FALSE); 1283 } 1284 } 1285 1286 argc -= optind; 1287 argv += optind; 1288 1289 /* check number of arguments */ 1290 if (argc == 0) { 1291 (void) fprintf(stderr, gettext("missing dataset argument\n")); 1292 usage(B_FALSE); 1293 } 1294 if (argc > 1) { 1295 (void) fprintf(stderr, gettext("too many arguments\n")); 1296 usage(B_FALSE); 1297 } 1298 1299 at = strchr(argv[0], '@'); 1300 pound = strchr(argv[0], '#'); 1301 if (at != NULL) { 1302 1303 /* Build the list of snaps to destroy in cb_nvl. */ 1304 cb.cb_nvl = fnvlist_alloc(); 1305 1306 *at = '\0'; 1307 zhp = zfs_open(g_zfs, argv[0], 1308 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 1309 if (zhp == NULL) 1310 return (1); 1311 1312 cb.cb_snapspec = at + 1; 1313 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 || 1314 cb.cb_error) { 1315 rv = 1; 1316 goto out; 1317 } 1318 1319 if (nvlist_empty(cb.cb_nvl)) { 1320 (void) fprintf(stderr, gettext("could not find any " 1321 "snapshots to destroy; check snapshot names.\n")); 1322 rv = 1; 1323 goto out; 1324 } 1325 1326 if (cb.cb_verbose) { 1327 char buf[16]; 1328 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf)); 1329 if (cb.cb_parsable) { 1330 (void) printf("reclaim\t%llu\n", 1331 cb.cb_snapused); 1332 } else if (cb.cb_dryrun) { 1333 (void) printf(gettext("would reclaim %s\n"), 1334 buf); 1335 } else { 1336 (void) printf(gettext("will reclaim %s\n"), 1337 buf); 1338 } 1339 } 1340 1341 if (!cb.cb_dryrun) { 1342 if (cb.cb_doclones) { 1343 cb.cb_batchedsnaps = fnvlist_alloc(); 1344 err = destroy_clones(&cb); 1345 if (err == 0) { 1346 err = zfs_destroy_snaps_nvl(g_zfs, 1347 cb.cb_batchedsnaps, B_FALSE); 1348 } 1349 if (err != 0) { 1350 rv = 1; 1351 goto out; 1352 } 1353 } 1354 if (err == 0) { 1355 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl, 1356 cb.cb_defer_destroy); 1357 } 1358 } 1359 1360 if (err != 0) 1361 rv = 1; 1362 } else if (pound != NULL) { 1363 int err; 1364 nvlist_t *nvl; 1365 1366 if (cb.cb_dryrun) { 1367 (void) fprintf(stderr, 1368 "dryrun is not supported with bookmark\n"); 1369 return (-1); 1370 } 1371 1372 if (cb.cb_defer_destroy) { 1373 (void) fprintf(stderr, 1374 "defer destroy is not supported with bookmark\n"); 1375 return (-1); 1376 } 1377 1378 if (cb.cb_recurse) { 1379 (void) fprintf(stderr, 1380 "recursive is not supported with bookmark\n"); 1381 return (-1); 1382 } 1383 1384 if (!zfs_bookmark_exists(argv[0])) { 1385 (void) fprintf(stderr, gettext("bookmark '%s' " 1386 "does not exist.\n"), argv[0]); 1387 return (1); 1388 } 1389 1390 nvl = fnvlist_alloc(); 1391 fnvlist_add_boolean(nvl, argv[0]); 1392 1393 err = lzc_destroy_bookmarks(nvl, NULL); 1394 if (err != 0) { 1395 (void) zfs_standard_error(g_zfs, err, 1396 "cannot destroy bookmark"); 1397 } 1398 1399 nvlist_free(cb.cb_nvl); 1400 1401 return (err); 1402 } else { 1403 /* Open the given dataset */ 1404 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL) 1405 return (1); 1406 1407 cb.cb_target = zhp; 1408 1409 /* 1410 * Perform an explicit check for pools before going any further. 1411 */ 1412 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL && 1413 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 1414 (void) fprintf(stderr, gettext("cannot destroy '%s': " 1415 "operation does not apply to pools\n"), 1416 zfs_get_name(zhp)); 1417 (void) fprintf(stderr, gettext("use 'zfs destroy -r " 1418 "%s' to destroy all datasets in the pool\n"), 1419 zfs_get_name(zhp)); 1420 (void) fprintf(stderr, gettext("use 'zpool destroy %s' " 1421 "to destroy the pool itself\n"), zfs_get_name(zhp)); 1422 rv = 1; 1423 goto out; 1424 } 1425 1426 /* 1427 * Check for any dependents and/or clones. 1428 */ 1429 cb.cb_first = B_TRUE; 1430 if (!cb.cb_doclones && 1431 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent, 1432 &cb) != 0) { 1433 rv = 1; 1434 goto out; 1435 } 1436 1437 if (cb.cb_error) { 1438 rv = 1; 1439 goto out; 1440 } 1441 1442 cb.cb_batchedsnaps = fnvlist_alloc(); 1443 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback, 1444 &cb) != 0) { 1445 rv = 1; 1446 goto out; 1447 } 1448 1449 /* 1450 * Do the real thing. The callback will close the 1451 * handle regardless of whether it succeeds or not. 1452 */ 1453 err = destroy_callback(zhp, &cb); 1454 zhp = NULL; 1455 if (err == 0) { 1456 err = zfs_destroy_snaps_nvl(g_zfs, 1457 cb.cb_batchedsnaps, cb.cb_defer_destroy); 1458 } 1459 if (err != 0) 1460 rv = 1; 1461 } 1462 1463out: 1464 fnvlist_free(cb.cb_batchedsnaps); 1465 fnvlist_free(cb.cb_nvl); 1466 if (zhp != NULL) 1467 zfs_close(zhp); 1468 return (rv); 1469} 1470 1471static boolean_t 1472is_recvd_column(zprop_get_cbdata_t *cbp) 1473{ 1474 int i; 1475 zfs_get_column_t col; 1476 1477 for (i = 0; i < ZFS_GET_NCOLS && 1478 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++) 1479 if (col == GET_COL_RECVD) 1480 return (B_TRUE); 1481 return (B_FALSE); 1482} 1483 1484/* 1485 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...] 1486 * < all | property[,property]... > < fs | snap | vol > ... 1487 * 1488 * -r recurse over any child datasets 1489 * -H scripted mode. Headers are stripped, and fields are separated 1490 * by tabs instead of spaces. 1491 * -o Set of fields to display. One of "name,property,value, 1492 * received,source". Default is "name,property,value,source". 1493 * "all" is an alias for all five. 1494 * -s Set of sources to allow. One of 1495 * "local,default,inherited,received,temporary,none". Default is 1496 * all six. 1497 * -p Display values in parsable (literal) format. 1498 * 1499 * Prints properties for the given datasets. The user can control which 1500 * columns to display as well as which property types to allow. 1501 */ 1502 1503/* 1504 * Invoked to display the properties for a single dataset. 1505 */ 1506static int 1507get_callback(zfs_handle_t *zhp, void *data) 1508{ 1509 char buf[ZFS_MAXPROPLEN]; 1510 char rbuf[ZFS_MAXPROPLEN]; 1511 zprop_source_t sourcetype; 1512 char source[ZFS_MAXNAMELEN]; 1513 zprop_get_cbdata_t *cbp = data; 1514 nvlist_t *user_props = zfs_get_user_props(zhp); 1515 zprop_list_t *pl = cbp->cb_proplist; 1516 nvlist_t *propval; 1517 char *strval; 1518 char *sourceval; 1519 boolean_t received = is_recvd_column(cbp); 1520 1521 for (; pl != NULL; pl = pl->pl_next) { 1522 char *recvdval = NULL; 1523 /* 1524 * Skip the special fake placeholder. This will also skip over 1525 * the name property when 'all' is specified. 1526 */ 1527 if (pl->pl_prop == ZFS_PROP_NAME && 1528 pl == cbp->cb_proplist) 1529 continue; 1530 1531 if (pl->pl_prop != ZPROP_INVAL) { 1532 if (zfs_prop_get(zhp, pl->pl_prop, buf, 1533 sizeof (buf), &sourcetype, source, 1534 sizeof (source), 1535 cbp->cb_literal) != 0) { 1536 if (pl->pl_all) 1537 continue; 1538 if (!zfs_prop_valid_for_type(pl->pl_prop, 1539 ZFS_TYPE_DATASET)) { 1540 (void) fprintf(stderr, 1541 gettext("No such property '%s'\n"), 1542 zfs_prop_to_name(pl->pl_prop)); 1543 continue; 1544 } 1545 sourcetype = ZPROP_SRC_NONE; 1546 (void) strlcpy(buf, "-", sizeof (buf)); 1547 } 1548 1549 if (received && (zfs_prop_get_recvd(zhp, 1550 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf), 1551 cbp->cb_literal) == 0)) 1552 recvdval = rbuf; 1553 1554 zprop_print_one_property(zfs_get_name(zhp), cbp, 1555 zfs_prop_to_name(pl->pl_prop), 1556 buf, sourcetype, source, recvdval); 1557 } else if (zfs_prop_userquota(pl->pl_user_prop)) { 1558 sourcetype = ZPROP_SRC_LOCAL; 1559 1560 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop, 1561 buf, sizeof (buf), cbp->cb_literal) != 0) { 1562 sourcetype = ZPROP_SRC_NONE; 1563 (void) strlcpy(buf, "-", sizeof (buf)); 1564 } 1565 1566 zprop_print_one_property(zfs_get_name(zhp), cbp, 1567 pl->pl_user_prop, buf, sourcetype, source, NULL); 1568 } else if (zfs_prop_written(pl->pl_user_prop)) { 1569 sourcetype = ZPROP_SRC_LOCAL; 1570 1571 if (zfs_prop_get_written(zhp, pl->pl_user_prop, 1572 buf, sizeof (buf), cbp->cb_literal) != 0) { 1573 sourcetype = ZPROP_SRC_NONE; 1574 (void) strlcpy(buf, "-", sizeof (buf)); 1575 } 1576 1577 zprop_print_one_property(zfs_get_name(zhp), cbp, 1578 pl->pl_user_prop, buf, sourcetype, source, NULL); 1579 } else { 1580 if (nvlist_lookup_nvlist(user_props, 1581 pl->pl_user_prop, &propval) != 0) { 1582 if (pl->pl_all) 1583 continue; 1584 sourcetype = ZPROP_SRC_NONE; 1585 strval = "-"; 1586 } else { 1587 verify(nvlist_lookup_string(propval, 1588 ZPROP_VALUE, &strval) == 0); 1589 verify(nvlist_lookup_string(propval, 1590 ZPROP_SOURCE, &sourceval) == 0); 1591 1592 if (strcmp(sourceval, 1593 zfs_get_name(zhp)) == 0) { 1594 sourcetype = ZPROP_SRC_LOCAL; 1595 } else if (strcmp(sourceval, 1596 ZPROP_SOURCE_VAL_RECVD) == 0) { 1597 sourcetype = ZPROP_SRC_RECEIVED; 1598 } else { 1599 sourcetype = ZPROP_SRC_INHERITED; 1600 (void) strlcpy(source, 1601 sourceval, sizeof (source)); 1602 } 1603 } 1604 1605 if (received && (zfs_prop_get_recvd(zhp, 1606 pl->pl_user_prop, rbuf, sizeof (rbuf), 1607 cbp->cb_literal) == 0)) 1608 recvdval = rbuf; 1609 1610 zprop_print_one_property(zfs_get_name(zhp), cbp, 1611 pl->pl_user_prop, strval, sourcetype, 1612 source, recvdval); 1613 } 1614 } 1615 1616 return (0); 1617} 1618 1619static int 1620zfs_do_get(int argc, char **argv) 1621{ 1622 zprop_get_cbdata_t cb = { 0 }; 1623 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS; 1624 int types = ZFS_TYPE_DATASET; 1625 char *value, *fields; 1626 int ret = 0; 1627 int limit = 0; 1628 zprop_list_t fake_name = { 0 }; 1629 1630 /* 1631 * Set up default columns and sources. 1632 */ 1633 cb.cb_sources = ZPROP_SRC_ALL; 1634 cb.cb_columns[0] = GET_COL_NAME; 1635 cb.cb_columns[1] = GET_COL_PROPERTY; 1636 cb.cb_columns[2] = GET_COL_VALUE; 1637 cb.cb_columns[3] = GET_COL_SOURCE; 1638 cb.cb_type = ZFS_TYPE_DATASET; 1639 1640 /* check options */ 1641 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) { 1642 switch (c) { 1643 case 'p': 1644 cb.cb_literal = B_TRUE; 1645 break; 1646 case 'd': 1647 limit = parse_depth(optarg, &flags); 1648 break; 1649 case 'r': 1650 flags |= ZFS_ITER_RECURSE; 1651 break; 1652 case 'H': 1653 cb.cb_scripted = B_TRUE; 1654 break; 1655 case ':': 1656 (void) fprintf(stderr, gettext("missing argument for " 1657 "'%c' option\n"), optopt); 1658 usage(B_FALSE); 1659 break; 1660 case 'o': 1661 /* 1662 * Process the set of columns to display. We zero out 1663 * the structure to give us a blank slate. 1664 */ 1665 bzero(&cb.cb_columns, sizeof (cb.cb_columns)); 1666 i = 0; 1667 while (*optarg != '\0') { 1668 static char *col_subopts[] = 1669 { "name", "property", "value", "received", 1670 "source", "all", NULL }; 1671 1672 if (i == ZFS_GET_NCOLS) { 1673 (void) fprintf(stderr, gettext("too " 1674 "many fields given to -o " 1675 "option\n")); 1676 usage(B_FALSE); 1677 } 1678 1679 switch (getsubopt(&optarg, col_subopts, 1680 &value)) { 1681 case 0: 1682 cb.cb_columns[i++] = GET_COL_NAME; 1683 break; 1684 case 1: 1685 cb.cb_columns[i++] = GET_COL_PROPERTY; 1686 break; 1687 case 2: 1688 cb.cb_columns[i++] = GET_COL_VALUE; 1689 break; 1690 case 3: 1691 cb.cb_columns[i++] = GET_COL_RECVD; 1692 flags |= ZFS_ITER_RECVD_PROPS; 1693 break; 1694 case 4: 1695 cb.cb_columns[i++] = GET_COL_SOURCE; 1696 break; 1697 case 5: 1698 if (i > 0) { 1699 (void) fprintf(stderr, 1700 gettext("\"all\" conflicts " 1701 "with specific fields " 1702 "given to -o option\n")); 1703 usage(B_FALSE); 1704 } 1705 cb.cb_columns[0] = GET_COL_NAME; 1706 cb.cb_columns[1] = GET_COL_PROPERTY; 1707 cb.cb_columns[2] = GET_COL_VALUE; 1708 cb.cb_columns[3] = GET_COL_RECVD; 1709 cb.cb_columns[4] = GET_COL_SOURCE; 1710 flags |= ZFS_ITER_RECVD_PROPS; 1711 i = ZFS_GET_NCOLS; 1712 break; 1713 default: 1714 (void) fprintf(stderr, 1715 gettext("invalid column name " 1716 "'%s'\n"), suboptarg); 1717 usage(B_FALSE); 1718 } 1719 } 1720 break; 1721 1722 case 's': 1723 cb.cb_sources = 0; 1724 while (*optarg != '\0') { 1725 static char *source_subopts[] = { 1726 "local", "default", "inherited", 1727 "received", "temporary", "none", 1728 NULL }; 1729 1730 switch (getsubopt(&optarg, source_subopts, 1731 &value)) { 1732 case 0: 1733 cb.cb_sources |= ZPROP_SRC_LOCAL; 1734 break; 1735 case 1: 1736 cb.cb_sources |= ZPROP_SRC_DEFAULT; 1737 break; 1738 case 2: 1739 cb.cb_sources |= ZPROP_SRC_INHERITED; 1740 break; 1741 case 3: 1742 cb.cb_sources |= ZPROP_SRC_RECEIVED; 1743 break; 1744 case 4: 1745 cb.cb_sources |= ZPROP_SRC_TEMPORARY; 1746 break; 1747 case 5: 1748 cb.cb_sources |= ZPROP_SRC_NONE; 1749 break; 1750 default: 1751 (void) fprintf(stderr, 1752 gettext("invalid source " 1753 "'%s'\n"), suboptarg); 1754 usage(B_FALSE); 1755 } 1756 } 1757 break; 1758 1759 case 't': 1760 types = 0; 1761 flags &= ~ZFS_ITER_PROP_LISTSNAPS; 1762 while (*optarg != '\0') { 1763 static char *type_subopts[] = { "filesystem", 1764 "volume", "snapshot", "bookmark", 1765 "all", NULL }; 1766 1767 switch (getsubopt(&optarg, type_subopts, 1768 &value)) { 1769 case 0: 1770 types |= ZFS_TYPE_FILESYSTEM; 1771 break; 1772 case 1: 1773 types |= ZFS_TYPE_VOLUME; 1774 break; 1775 case 2: 1776 types |= ZFS_TYPE_SNAPSHOT; 1777 break; 1778 case 3: 1779 types |= ZFS_TYPE_BOOKMARK; 1780 break; 1781 case 4: 1782 types = ZFS_TYPE_DATASET | 1783 ZFS_TYPE_BOOKMARK; 1784 break; 1785 1786 default: 1787 (void) fprintf(stderr, 1788 gettext("invalid type '%s'\n"), 1789 suboptarg); 1790 usage(B_FALSE); 1791 } 1792 } 1793 break; 1794 1795 case '?': 1796 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1797 optopt); 1798 usage(B_FALSE); 1799 } 1800 } 1801 1802 argc -= optind; 1803 argv += optind; 1804 1805 if (argc < 1) { 1806 (void) fprintf(stderr, gettext("missing property " 1807 "argument\n")); 1808 usage(B_FALSE); 1809 } 1810 1811 fields = argv[0]; 1812 1813 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET) 1814 != 0) 1815 usage(B_FALSE); 1816 1817 argc--; 1818 argv++; 1819 1820 /* 1821 * As part of zfs_expand_proplist(), we keep track of the maximum column 1822 * width for each property. For the 'NAME' (and 'SOURCE') columns, we 1823 * need to know the maximum name length. However, the user likely did 1824 * not specify 'name' as one of the properties to fetch, so we need to 1825 * make sure we always include at least this property for 1826 * print_get_headers() to work properly. 1827 */ 1828 if (cb.cb_proplist != NULL) { 1829 fake_name.pl_prop = ZFS_PROP_NAME; 1830 fake_name.pl_width = strlen(gettext("NAME")); 1831 fake_name.pl_next = cb.cb_proplist; 1832 cb.cb_proplist = &fake_name; 1833 } 1834 1835 cb.cb_first = B_TRUE; 1836 1837 /* run for each object */ 1838 ret = zfs_for_each(argc, argv, flags, types, NULL, 1839 &cb.cb_proplist, limit, get_callback, &cb); 1840 1841 if (cb.cb_proplist == &fake_name) 1842 zprop_free_list(fake_name.pl_next); 1843 else 1844 zprop_free_list(cb.cb_proplist); 1845 1846 return (ret); 1847} 1848 1849/* 1850 * inherit [-rS] <property> <fs|vol> ... 1851 * 1852 * -r Recurse over all children 1853 * -S Revert to received value, if any 1854 * 1855 * For each dataset specified on the command line, inherit the given property 1856 * from its parent. Inheriting a property at the pool level will cause it to 1857 * use the default value. The '-r' flag will recurse over all children, and is 1858 * useful for setting a property on a hierarchy-wide basis, regardless of any 1859 * local modifications for each dataset. 1860 */ 1861 1862typedef struct inherit_cbdata { 1863 const char *cb_propname; 1864 boolean_t cb_received; 1865} inherit_cbdata_t; 1866 1867static int 1868inherit_recurse_cb(zfs_handle_t *zhp, void *data) 1869{ 1870 inherit_cbdata_t *cb = data; 1871 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname); 1872 1873 /* 1874 * If we're doing it recursively, then ignore properties that 1875 * are not valid for this type of dataset. 1876 */ 1877 if (prop != ZPROP_INVAL && 1878 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp))) 1879 return (0); 1880 1881 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0); 1882} 1883 1884static int 1885inherit_cb(zfs_handle_t *zhp, void *data) 1886{ 1887 inherit_cbdata_t *cb = data; 1888 1889 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0); 1890} 1891 1892static int 1893zfs_do_inherit(int argc, char **argv) 1894{ 1895 int c; 1896 zfs_prop_t prop; 1897 inherit_cbdata_t cb = { 0 }; 1898 char *propname; 1899 int ret = 0; 1900 int flags = 0; 1901 boolean_t received = B_FALSE; 1902 1903 /* check options */ 1904 while ((c = getopt(argc, argv, "rS")) != -1) { 1905 switch (c) { 1906 case 'r': 1907 flags |= ZFS_ITER_RECURSE; 1908 break; 1909 case 'S': 1910 received = B_TRUE; 1911 break; 1912 case '?': 1913 default: 1914 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 1915 optopt); 1916 usage(B_FALSE); 1917 } 1918 } 1919 1920 argc -= optind; 1921 argv += optind; 1922 1923 /* check number of arguments */ 1924 if (argc < 1) { 1925 (void) fprintf(stderr, gettext("missing property argument\n")); 1926 usage(B_FALSE); 1927 } 1928 if (argc < 2) { 1929 (void) fprintf(stderr, gettext("missing dataset argument\n")); 1930 usage(B_FALSE); 1931 } 1932 1933 propname = argv[0]; 1934 argc--; 1935 argv++; 1936 1937 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) { 1938 if (zfs_prop_readonly(prop)) { 1939 (void) fprintf(stderr, gettext( 1940 "%s property is read-only\n"), 1941 propname); 1942 return (1); 1943 } 1944 if (!zfs_prop_inheritable(prop) && !received) { 1945 (void) fprintf(stderr, gettext("'%s' property cannot " 1946 "be inherited\n"), propname); 1947 if (prop == ZFS_PROP_QUOTA || 1948 prop == ZFS_PROP_RESERVATION || 1949 prop == ZFS_PROP_REFQUOTA || 1950 prop == ZFS_PROP_REFRESERVATION) { 1951 (void) fprintf(stderr, gettext("use 'zfs set " 1952 "%s=none' to clear\n"), propname); 1953 (void) fprintf(stderr, gettext("use 'zfs " 1954 "inherit -S %s' to revert to received " 1955 "value\n"), propname); 1956 } 1957 return (1); 1958 } 1959 if (received && (prop == ZFS_PROP_VOLSIZE || 1960 prop == ZFS_PROP_VERSION)) { 1961 (void) fprintf(stderr, gettext("'%s' property cannot " 1962 "be reverted to a received value\n"), propname); 1963 return (1); 1964 } 1965 } else if (!zfs_prop_user(propname)) { 1966 (void) fprintf(stderr, gettext("invalid property '%s'\n"), 1967 propname); 1968 usage(B_FALSE); 1969 } 1970 1971 cb.cb_propname = propname; 1972 cb.cb_received = received; 1973 1974 if (flags & ZFS_ITER_RECURSE) { 1975 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, 1976 NULL, NULL, 0, inherit_recurse_cb, &cb); 1977 } else { 1978 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, 1979 NULL, NULL, 0, inherit_cb, &cb); 1980 } 1981 1982 return (ret); 1983} 1984 1985typedef struct upgrade_cbdata { 1986 uint64_t cb_numupgraded; 1987 uint64_t cb_numsamegraded; 1988 uint64_t cb_numfailed; 1989 uint64_t cb_version; 1990 boolean_t cb_newer; 1991 boolean_t cb_foundone; 1992 char cb_lastfs[ZFS_MAXNAMELEN]; 1993} upgrade_cbdata_t; 1994 1995static int 1996same_pool(zfs_handle_t *zhp, const char *name) 1997{ 1998 int len1 = strcspn(name, "/@"); 1999 const char *zhname = zfs_get_name(zhp); 2000 int len2 = strcspn(zhname, "/@"); 2001 2002 if (len1 != len2) 2003 return (B_FALSE); 2004 return (strncmp(name, zhname, len1) == 0); 2005} 2006 2007static int 2008upgrade_list_callback(zfs_handle_t *zhp, void *data) 2009{ 2010 upgrade_cbdata_t *cb = data; 2011 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 2012 2013 /* list if it's old/new */ 2014 if ((!cb->cb_newer && version < ZPL_VERSION) || 2015 (cb->cb_newer && version > ZPL_VERSION)) { 2016 char *str; 2017 if (cb->cb_newer) { 2018 str = gettext("The following filesystems are " 2019 "formatted using a newer software version and\n" 2020 "cannot be accessed on the current system.\n\n"); 2021 } else { 2022 str = gettext("The following filesystems are " 2023 "out of date, and can be upgraded. After being\n" 2024 "upgraded, these filesystems (and any 'zfs send' " 2025 "streams generated from\n" 2026 "subsequent snapshots) will no longer be " 2027 "accessible by older software versions.\n\n"); 2028 } 2029 2030 if (!cb->cb_foundone) { 2031 (void) puts(str); 2032 (void) printf(gettext("VER FILESYSTEM\n")); 2033 (void) printf(gettext("--- ------------\n")); 2034 cb->cb_foundone = B_TRUE; 2035 } 2036 2037 (void) printf("%2u %s\n", version, zfs_get_name(zhp)); 2038 } 2039 2040 return (0); 2041} 2042 2043static int 2044upgrade_set_callback(zfs_handle_t *zhp, void *data) 2045{ 2046 upgrade_cbdata_t *cb = data; 2047 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 2048 int needed_spa_version; 2049 int spa_version; 2050 2051 if (zfs_spa_version(zhp, &spa_version) < 0) 2052 return (-1); 2053 2054 needed_spa_version = zfs_spa_version_map(cb->cb_version); 2055 2056 if (needed_spa_version < 0) 2057 return (-1); 2058 2059 if (spa_version < needed_spa_version) { 2060 /* can't upgrade */ 2061 (void) printf(gettext("%s: can not be " 2062 "upgraded; the pool version needs to first " 2063 "be upgraded\nto version %d\n\n"), 2064 zfs_get_name(zhp), needed_spa_version); 2065 cb->cb_numfailed++; 2066 return (0); 2067 } 2068 2069 /* upgrade */ 2070 if (version < cb->cb_version) { 2071 char verstr[16]; 2072 (void) snprintf(verstr, sizeof (verstr), 2073 "%llu", cb->cb_version); 2074 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) { 2075 /* 2076 * If they did "zfs upgrade -a", then we could 2077 * be doing ioctls to different pools. We need 2078 * to log this history once to each pool, and bypass 2079 * the normal history logging that happens in main(). 2080 */ 2081 (void) zpool_log_history(g_zfs, history_str); 2082 log_history = B_FALSE; 2083 } 2084 if (zfs_prop_set(zhp, "version", verstr) == 0) 2085 cb->cb_numupgraded++; 2086 else 2087 cb->cb_numfailed++; 2088 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp)); 2089 } else if (version > cb->cb_version) { 2090 /* can't downgrade */ 2091 (void) printf(gettext("%s: can not be downgraded; " 2092 "it is already at version %u\n"), 2093 zfs_get_name(zhp), version); 2094 cb->cb_numfailed++; 2095 } else { 2096 cb->cb_numsamegraded++; 2097 } 2098 return (0); 2099} 2100 2101/* 2102 * zfs upgrade 2103 * zfs upgrade -v 2104 * zfs upgrade [-r] [-V <version>] <-a | filesystem> 2105 */ 2106static int 2107zfs_do_upgrade(int argc, char **argv) 2108{ 2109 boolean_t all = B_FALSE; 2110 boolean_t showversions = B_FALSE; 2111 int ret = 0; 2112 upgrade_cbdata_t cb = { 0 }; 2113 int c; 2114 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS; 2115 2116 /* check options */ 2117 while ((c = getopt(argc, argv, "rvV:a")) != -1) { 2118 switch (c) { 2119 case 'r': 2120 flags |= ZFS_ITER_RECURSE; 2121 break; 2122 case 'v': 2123 showversions = B_TRUE; 2124 break; 2125 case 'V': 2126 if (zfs_prop_string_to_index(ZFS_PROP_VERSION, 2127 optarg, &cb.cb_version) != 0) { 2128 (void) fprintf(stderr, 2129 gettext("invalid version %s\n"), optarg); 2130 usage(B_FALSE); 2131 } 2132 break; 2133 case 'a': 2134 all = B_TRUE; 2135 break; 2136 case '?': 2137 default: 2138 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2139 optopt); 2140 usage(B_FALSE); 2141 } 2142 } 2143 2144 argc -= optind; 2145 argv += optind; 2146 2147 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version)) 2148 usage(B_FALSE); 2149 if (showversions && (flags & ZFS_ITER_RECURSE || all || 2150 cb.cb_version || argc)) 2151 usage(B_FALSE); 2152 if ((all || argc) && (showversions)) 2153 usage(B_FALSE); 2154 if (all && argc) 2155 usage(B_FALSE); 2156 2157 if (showversions) { 2158 /* Show info on available versions. */ 2159 (void) printf(gettext("The following filesystem versions are " 2160 "supported:\n\n")); 2161 (void) printf(gettext("VER DESCRIPTION\n")); 2162 (void) printf("--- -----------------------------------------" 2163 "---------------\n"); 2164 (void) printf(gettext(" 1 Initial ZFS filesystem version\n")); 2165 (void) printf(gettext(" 2 Enhanced directory entries\n")); 2166 (void) printf(gettext(" 3 Case insensitive and filesystem " 2167 "user identifier (FUID)\n")); 2168 (void) printf(gettext(" 4 userquota, groupquota " 2169 "properties\n")); 2170 (void) printf(gettext(" 5 System attributes\n")); 2171 (void) printf(gettext("\nFor more information on a particular " 2172 "version, including supported releases,\n")); 2173 (void) printf("see the ZFS Administration Guide.\n\n"); 2174 ret = 0; 2175 } else if (argc || all) { 2176 /* Upgrade filesystems */ 2177 if (cb.cb_version == 0) 2178 cb.cb_version = ZPL_VERSION; 2179 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM, 2180 NULL, NULL, 0, upgrade_set_callback, &cb); 2181 (void) printf(gettext("%llu filesystems upgraded\n"), 2182 cb.cb_numupgraded); 2183 if (cb.cb_numsamegraded) { 2184 (void) printf(gettext("%llu filesystems already at " 2185 "this version\n"), 2186 cb.cb_numsamegraded); 2187 } 2188 if (cb.cb_numfailed != 0) 2189 ret = 1; 2190 } else { 2191 /* List old-version filesytems */ 2192 boolean_t found; 2193 (void) printf(gettext("This system is currently running " 2194 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION); 2195 2196 flags |= ZFS_ITER_RECURSE; 2197 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM, 2198 NULL, NULL, 0, upgrade_list_callback, &cb); 2199 2200 found = cb.cb_foundone; 2201 cb.cb_foundone = B_FALSE; 2202 cb.cb_newer = B_TRUE; 2203 2204 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM, 2205 NULL, NULL, 0, upgrade_list_callback, &cb); 2206 2207 if (!cb.cb_foundone && !found) { 2208 (void) printf(gettext("All filesystems are " 2209 "formatted with the current version.\n")); 2210 } 2211 } 2212 2213 return (ret); 2214} 2215 2216/* 2217 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...] 2218 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot 2219 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...] 2220 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot 2221 * 2222 * -H Scripted mode; elide headers and separate columns by tabs. 2223 * -i Translate SID to POSIX ID. 2224 * -n Print numeric ID instead of user/group name. 2225 * -o Control which fields to display. 2226 * -p Use exact (parsable) numeric output. 2227 * -s Specify sort columns, descending order. 2228 * -S Specify sort columns, ascending order. 2229 * -t Control which object types to display. 2230 * 2231 * Displays space consumed by, and quotas on, each user in the specified 2232 * filesystem or snapshot. 2233 */ 2234 2235/* us_field_types, us_field_hdr and us_field_names should be kept in sync */ 2236enum us_field_types { 2237 USFIELD_TYPE, 2238 USFIELD_NAME, 2239 USFIELD_USED, 2240 USFIELD_QUOTA 2241}; 2242static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" }; 2243static char *us_field_names[] = { "type", "name", "used", "quota" }; 2244#define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *)) 2245 2246#define USTYPE_PSX_GRP (1 << 0) 2247#define USTYPE_PSX_USR (1 << 1) 2248#define USTYPE_SMB_GRP (1 << 2) 2249#define USTYPE_SMB_USR (1 << 3) 2250#define USTYPE_ALL \ 2251 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR) 2252 2253static int us_type_bits[] = { 2254 USTYPE_PSX_GRP, 2255 USTYPE_PSX_USR, 2256 USTYPE_SMB_GRP, 2257 USTYPE_SMB_USR, 2258 USTYPE_ALL 2259}; 2260static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup", 2261 "smbuser", "all" }; 2262 2263typedef struct us_node { 2264 nvlist_t *usn_nvl; 2265 uu_avl_node_t usn_avlnode; 2266 uu_list_node_t usn_listnode; 2267} us_node_t; 2268 2269typedef struct us_cbdata { 2270 nvlist_t **cb_nvlp; 2271 uu_avl_pool_t *cb_avl_pool; 2272 uu_avl_t *cb_avl; 2273 boolean_t cb_numname; 2274 boolean_t cb_nicenum; 2275 boolean_t cb_sid2posix; 2276 zfs_userquota_prop_t cb_prop; 2277 zfs_sort_column_t *cb_sortcol; 2278 size_t cb_width[USFIELD_LAST]; 2279} us_cbdata_t; 2280 2281static boolean_t us_populated = B_FALSE; 2282 2283typedef struct { 2284 zfs_sort_column_t *si_sortcol; 2285 boolean_t si_numname; 2286} us_sort_info_t; 2287 2288static int 2289us_field_index(char *field) 2290{ 2291 int i; 2292 2293 for (i = 0; i < USFIELD_LAST; i++) { 2294 if (strcmp(field, us_field_names[i]) == 0) 2295 return (i); 2296 } 2297 2298 return (-1); 2299} 2300 2301static int 2302us_compare(const void *larg, const void *rarg, void *unused) 2303{ 2304 const us_node_t *l = larg; 2305 const us_node_t *r = rarg; 2306 us_sort_info_t *si = (us_sort_info_t *)unused; 2307 zfs_sort_column_t *sortcol = si->si_sortcol; 2308 boolean_t numname = si->si_numname; 2309 nvlist_t *lnvl = l->usn_nvl; 2310 nvlist_t *rnvl = r->usn_nvl; 2311 int rc = 0; 2312 boolean_t lvb, rvb; 2313 2314 for (; sortcol != NULL; sortcol = sortcol->sc_next) { 2315 char *lvstr = ""; 2316 char *rvstr = ""; 2317 uint32_t lv32 = 0; 2318 uint32_t rv32 = 0; 2319 uint64_t lv64 = 0; 2320 uint64_t rv64 = 0; 2321 zfs_prop_t prop = sortcol->sc_prop; 2322 const char *propname = NULL; 2323 boolean_t reverse = sortcol->sc_reverse; 2324 2325 switch (prop) { 2326 case ZFS_PROP_TYPE: 2327 propname = "type"; 2328 (void) nvlist_lookup_uint32(lnvl, propname, &lv32); 2329 (void) nvlist_lookup_uint32(rnvl, propname, &rv32); 2330 if (rv32 != lv32) 2331 rc = (rv32 < lv32) ? 1 : -1; 2332 break; 2333 case ZFS_PROP_NAME: 2334 propname = "name"; 2335 if (numname) { 2336 (void) nvlist_lookup_uint64(lnvl, propname, 2337 &lv64); 2338 (void) nvlist_lookup_uint64(rnvl, propname, 2339 &rv64); 2340 if (rv64 != lv64) 2341 rc = (rv64 < lv64) ? 1 : -1; 2342 } else { 2343 (void) nvlist_lookup_string(lnvl, propname, 2344 &lvstr); 2345 (void) nvlist_lookup_string(rnvl, propname, 2346 &rvstr); 2347 rc = strcmp(lvstr, rvstr); 2348 } 2349 break; 2350 case ZFS_PROP_USED: 2351 case ZFS_PROP_QUOTA: 2352 if (!us_populated) 2353 break; 2354 if (prop == ZFS_PROP_USED) 2355 propname = "used"; 2356 else 2357 propname = "quota"; 2358 (void) nvlist_lookup_uint64(lnvl, propname, &lv64); 2359 (void) nvlist_lookup_uint64(rnvl, propname, &rv64); 2360 if (rv64 != lv64) 2361 rc = (rv64 < lv64) ? 1 : -1; 2362 break; 2363 } 2364 2365 if (rc != 0) { 2366 if (rc < 0) 2367 return (reverse ? 1 : -1); 2368 else 2369 return (reverse ? -1 : 1); 2370 } 2371 } 2372 2373 /* 2374 * If entries still seem to be the same, check if they are of the same 2375 * type (smbentity is added only if we are doing SID to POSIX ID 2376 * translation where we can have duplicate type/name combinations). 2377 */ 2378 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 && 2379 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 && 2380 lvb != rvb) 2381 return (lvb < rvb ? -1 : 1); 2382 2383 return (0); 2384} 2385 2386static inline const char * 2387us_type2str(unsigned field_type) 2388{ 2389 switch (field_type) { 2390 case USTYPE_PSX_USR: 2391 return ("POSIX User"); 2392 case USTYPE_PSX_GRP: 2393 return ("POSIX Group"); 2394 case USTYPE_SMB_USR: 2395 return ("SMB User"); 2396 case USTYPE_SMB_GRP: 2397 return ("SMB Group"); 2398 default: 2399 return ("Undefined"); 2400 } 2401} 2402 2403static int 2404userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space) 2405{ 2406 us_cbdata_t *cb = (us_cbdata_t *)arg; 2407 zfs_userquota_prop_t prop = cb->cb_prop; 2408 char *name = NULL; 2409 char *propname; 2410 char sizebuf[32]; 2411 us_node_t *node; 2412 uu_avl_pool_t *avl_pool = cb->cb_avl_pool; 2413 uu_avl_t *avl = cb->cb_avl; 2414 uu_avl_index_t idx; 2415 nvlist_t *props; 2416 us_node_t *n; 2417 zfs_sort_column_t *sortcol = cb->cb_sortcol; 2418 unsigned type; 2419 const char *typestr; 2420 size_t namelen; 2421 size_t typelen; 2422 size_t sizelen; 2423 int typeidx, nameidx, sizeidx; 2424 us_sort_info_t sortinfo = { sortcol, cb->cb_numname }; 2425 boolean_t smbentity = B_FALSE; 2426 2427 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 2428 nomem(); 2429 node = safe_malloc(sizeof (us_node_t)); 2430 uu_avl_node_init(node, &node->usn_avlnode, avl_pool); 2431 node->usn_nvl = props; 2432 2433 if (domain != NULL && domain[0] != '\0') { 2434 /* SMB */ 2435 char sid[ZFS_MAXNAMELEN + 32]; 2436 uid_t id; 2437#ifdef illumos 2438 int err; 2439 int flag = IDMAP_REQ_FLG_USE_CACHE; 2440#endif 2441 2442 smbentity = B_TRUE; 2443 2444 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid); 2445 2446 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) { 2447 type = USTYPE_SMB_GRP; 2448#ifdef illumos 2449 err = sid_to_id(sid, B_FALSE, &id); 2450#endif 2451 } else { 2452 type = USTYPE_SMB_USR; 2453#ifdef illumos 2454 err = sid_to_id(sid, B_TRUE, &id); 2455#endif 2456 } 2457 2458#ifdef illumos 2459 if (err == 0) { 2460 rid = id; 2461 if (!cb->cb_sid2posix) { 2462 if (type == USTYPE_SMB_USR) { 2463 (void) idmap_getwinnamebyuid(rid, flag, 2464 &name, NULL); 2465 } else { 2466 (void) idmap_getwinnamebygid(rid, flag, 2467 &name, NULL); 2468 } 2469 if (name == NULL) 2470 name = sid; 2471 } 2472 } 2473#endif 2474 } 2475 2476 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') { 2477 /* POSIX or -i */ 2478 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) { 2479 type = USTYPE_PSX_GRP; 2480 if (!cb->cb_numname) { 2481 struct group *g; 2482 2483 if ((g = getgrgid(rid)) != NULL) 2484 name = g->gr_name; 2485 } 2486 } else { 2487 type = USTYPE_PSX_USR; 2488 if (!cb->cb_numname) { 2489 struct passwd *p; 2490 2491 if ((p = getpwuid(rid)) != NULL) 2492 name = p->pw_name; 2493 } 2494 } 2495 } 2496 2497 /* 2498 * Make sure that the type/name combination is unique when doing 2499 * SID to POSIX ID translation (hence changing the type from SMB to 2500 * POSIX). 2501 */ 2502 if (cb->cb_sid2posix && 2503 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0) 2504 nomem(); 2505 2506 /* Calculate/update width of TYPE field */ 2507 typestr = us_type2str(type); 2508 typelen = strlen(gettext(typestr)); 2509 typeidx = us_field_index("type"); 2510 if (typelen > cb->cb_width[typeidx]) 2511 cb->cb_width[typeidx] = typelen; 2512 if (nvlist_add_uint32(props, "type", type) != 0) 2513 nomem(); 2514 2515 /* Calculate/update width of NAME field */ 2516 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) { 2517 if (nvlist_add_uint64(props, "name", rid) != 0) 2518 nomem(); 2519 namelen = snprintf(NULL, 0, "%u", rid); 2520 } else { 2521 if (nvlist_add_string(props, "name", name) != 0) 2522 nomem(); 2523 namelen = strlen(name); 2524 } 2525 nameidx = us_field_index("name"); 2526 if (namelen > cb->cb_width[nameidx]) 2527 cb->cb_width[nameidx] = namelen; 2528 2529 /* 2530 * Check if this type/name combination is in the list and update it; 2531 * otherwise add new node to the list. 2532 */ 2533 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) { 2534 uu_avl_insert(avl, node, idx); 2535 } else { 2536 nvlist_free(props); 2537 free(node); 2538 node = n; 2539 props = node->usn_nvl; 2540 } 2541 2542 /* Calculate/update width of USED/QUOTA fields */ 2543 if (cb->cb_nicenum) 2544 zfs_nicenum(space, sizebuf, sizeof (sizebuf)); 2545 else 2546 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space); 2547 sizelen = strlen(sizebuf); 2548 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) { 2549 propname = "used"; 2550 if (!nvlist_exists(props, "quota")) 2551 (void) nvlist_add_uint64(props, "quota", 0); 2552 } else { 2553 propname = "quota"; 2554 if (!nvlist_exists(props, "used")) 2555 (void) nvlist_add_uint64(props, "used", 0); 2556 } 2557 sizeidx = us_field_index(propname); 2558 if (sizelen > cb->cb_width[sizeidx]) 2559 cb->cb_width[sizeidx] = sizelen; 2560 2561 if (nvlist_add_uint64(props, propname, space) != 0) 2562 nomem(); 2563 2564 return (0); 2565} 2566 2567static void 2568print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types, 2569 size_t *width, us_node_t *node) 2570{ 2571 nvlist_t *nvl = node->usn_nvl; 2572 char valstr[ZFS_MAXNAMELEN]; 2573 boolean_t first = B_TRUE; 2574 int cfield = 0; 2575 int field; 2576 uint32_t ustype; 2577 2578 /* Check type */ 2579 (void) nvlist_lookup_uint32(nvl, "type", &ustype); 2580 if (!(ustype & types)) 2581 return; 2582 2583 while ((field = fields[cfield]) != USFIELD_LAST) { 2584 nvpair_t *nvp = NULL; 2585 data_type_t type; 2586 uint32_t val32; 2587 uint64_t val64; 2588 char *strval = NULL; 2589 2590 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 2591 if (strcmp(nvpair_name(nvp), 2592 us_field_names[field]) == 0) 2593 break; 2594 } 2595 2596 type = nvpair_type(nvp); 2597 switch (type) { 2598 case DATA_TYPE_UINT32: 2599 (void) nvpair_value_uint32(nvp, &val32); 2600 break; 2601 case DATA_TYPE_UINT64: 2602 (void) nvpair_value_uint64(nvp, &val64); 2603 break; 2604 case DATA_TYPE_STRING: 2605 (void) nvpair_value_string(nvp, &strval); 2606 break; 2607 default: 2608 (void) fprintf(stderr, "invalid data type\n"); 2609 } 2610 2611 switch (field) { 2612 case USFIELD_TYPE: 2613 strval = (char *)us_type2str(val32); 2614 break; 2615 case USFIELD_NAME: 2616 if (type == DATA_TYPE_UINT64) { 2617 (void) sprintf(valstr, "%llu", val64); 2618 strval = valstr; 2619 } 2620 break; 2621 case USFIELD_USED: 2622 case USFIELD_QUOTA: 2623 if (type == DATA_TYPE_UINT64) { 2624 if (parsable) { 2625 (void) sprintf(valstr, "%llu", val64); 2626 } else { 2627 zfs_nicenum(val64, valstr, 2628 sizeof (valstr)); 2629 } 2630 if (field == USFIELD_QUOTA && 2631 strcmp(valstr, "0") == 0) 2632 strval = "none"; 2633 else 2634 strval = valstr; 2635 } 2636 break; 2637 } 2638 2639 if (!first) { 2640 if (scripted) 2641 (void) printf("\t"); 2642 else 2643 (void) printf(" "); 2644 } 2645 if (scripted) 2646 (void) printf("%s", strval); 2647 else if (field == USFIELD_TYPE || field == USFIELD_NAME) 2648 (void) printf("%-*s", width[field], strval); 2649 else 2650 (void) printf("%*s", width[field], strval); 2651 2652 first = B_FALSE; 2653 cfield++; 2654 } 2655 2656 (void) printf("\n"); 2657} 2658 2659static void 2660print_us(boolean_t scripted, boolean_t parsable, int *fields, int types, 2661 size_t *width, boolean_t rmnode, uu_avl_t *avl) 2662{ 2663 us_node_t *node; 2664 const char *col; 2665 int cfield = 0; 2666 int field; 2667 2668 if (!scripted) { 2669 boolean_t first = B_TRUE; 2670 2671 while ((field = fields[cfield]) != USFIELD_LAST) { 2672 col = gettext(us_field_hdr[field]); 2673 if (field == USFIELD_TYPE || field == USFIELD_NAME) { 2674 (void) printf(first ? "%-*s" : " %-*s", 2675 width[field], col); 2676 } else { 2677 (void) printf(first ? "%*s" : " %*s", 2678 width[field], col); 2679 } 2680 first = B_FALSE; 2681 cfield++; 2682 } 2683 (void) printf("\n"); 2684 } 2685 2686 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) { 2687 print_us_node(scripted, parsable, fields, types, width, node); 2688 if (rmnode) 2689 nvlist_free(node->usn_nvl); 2690 } 2691} 2692 2693static int 2694zfs_do_userspace(int argc, char **argv) 2695{ 2696 zfs_handle_t *zhp; 2697 zfs_userquota_prop_t p; 2698 2699 uu_avl_pool_t *avl_pool; 2700 uu_avl_t *avl_tree; 2701 uu_avl_walk_t *walk; 2702 char *delim; 2703 char deffields[] = "type,name,used,quota"; 2704 char *ofield = NULL; 2705 char *tfield = NULL; 2706 int cfield = 0; 2707 int fields[256]; 2708 int i; 2709 boolean_t scripted = B_FALSE; 2710 boolean_t prtnum = B_FALSE; 2711 boolean_t parsable = B_FALSE; 2712 boolean_t sid2posix = B_FALSE; 2713 int ret = 0; 2714 int c; 2715 zfs_sort_column_t *sortcol = NULL; 2716 int types = USTYPE_PSX_USR | USTYPE_SMB_USR; 2717 us_cbdata_t cb; 2718 us_node_t *node; 2719 us_node_t *rmnode; 2720 uu_list_pool_t *listpool; 2721 uu_list_t *list; 2722 uu_avl_index_t idx = 0; 2723 uu_list_index_t idx2 = 0; 2724 2725 if (argc < 2) 2726 usage(B_FALSE); 2727 2728 if (strcmp(argv[0], "groupspace") == 0) 2729 /* Toggle default group types */ 2730 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP; 2731 2732 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) { 2733 switch (c) { 2734 case 'n': 2735 prtnum = B_TRUE; 2736 break; 2737 case 'H': 2738 scripted = B_TRUE; 2739 break; 2740 case 'p': 2741 parsable = B_TRUE; 2742 break; 2743 case 'o': 2744 ofield = optarg; 2745 break; 2746 case 's': 2747 case 'S': 2748 if (zfs_add_sort_column(&sortcol, optarg, 2749 c == 's' ? B_FALSE : B_TRUE) != 0) { 2750 (void) fprintf(stderr, 2751 gettext("invalid field '%s'\n"), optarg); 2752 usage(B_FALSE); 2753 } 2754 break; 2755 case 't': 2756 tfield = optarg; 2757 break; 2758 case 'i': 2759 sid2posix = B_TRUE; 2760 break; 2761 case ':': 2762 (void) fprintf(stderr, gettext("missing argument for " 2763 "'%c' option\n"), optopt); 2764 usage(B_FALSE); 2765 break; 2766 case '?': 2767 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 2768 optopt); 2769 usage(B_FALSE); 2770 } 2771 } 2772 2773 argc -= optind; 2774 argv += optind; 2775 2776 if (argc < 1) { 2777 (void) fprintf(stderr, gettext("missing dataset name\n")); 2778 usage(B_FALSE); 2779 } 2780 if (argc > 1) { 2781 (void) fprintf(stderr, gettext("too many arguments\n")); 2782 usage(B_FALSE); 2783 } 2784 2785 /* Use default output fields if not specified using -o */ 2786 if (ofield == NULL) 2787 ofield = deffields; 2788 do { 2789 if ((delim = strchr(ofield, ',')) != NULL) 2790 *delim = '\0'; 2791 if ((fields[cfield++] = us_field_index(ofield)) == -1) { 2792 (void) fprintf(stderr, gettext("invalid type '%s' " 2793 "for -o option\n"), ofield); 2794 return (-1); 2795 } 2796 if (delim != NULL) 2797 ofield = delim + 1; 2798 } while (delim != NULL); 2799 fields[cfield] = USFIELD_LAST; 2800 2801 /* Override output types (-t option) */ 2802 if (tfield != NULL) { 2803 types = 0; 2804 2805 do { 2806 boolean_t found = B_FALSE; 2807 2808 if ((delim = strchr(tfield, ',')) != NULL) 2809 *delim = '\0'; 2810 for (i = 0; i < sizeof (us_type_bits) / sizeof (int); 2811 i++) { 2812 if (strcmp(tfield, us_type_names[i]) == 0) { 2813 found = B_TRUE; 2814 types |= us_type_bits[i]; 2815 break; 2816 } 2817 } 2818 if (!found) { 2819 (void) fprintf(stderr, gettext("invalid type " 2820 "'%s' for -t option\n"), tfield); 2821 return (-1); 2822 } 2823 if (delim != NULL) 2824 tfield = delim + 1; 2825 } while (delim != NULL); 2826 } 2827 2828 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL) 2829 return (1); 2830 2831 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t), 2832 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL) 2833 nomem(); 2834 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL) 2835 nomem(); 2836 2837 /* Always add default sorting columns */ 2838 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE); 2839 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE); 2840 2841 cb.cb_sortcol = sortcol; 2842 cb.cb_numname = prtnum; 2843 cb.cb_nicenum = !parsable; 2844 cb.cb_avl_pool = avl_pool; 2845 cb.cb_avl = avl_tree; 2846 cb.cb_sid2posix = sid2posix; 2847 2848 for (i = 0; i < USFIELD_LAST; i++) 2849 cb.cb_width[i] = strlen(gettext(us_field_hdr[i])); 2850 2851 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) { 2852 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) && 2853 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) || 2854 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) && 2855 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP)))) 2856 continue; 2857 cb.cb_prop = p; 2858 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0) 2859 return (ret); 2860 } 2861 2862 /* Sort the list */ 2863 if ((node = uu_avl_first(avl_tree)) == NULL) 2864 return (0); 2865 2866 us_populated = B_TRUE; 2867 2868 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t), 2869 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT); 2870 list = uu_list_create(listpool, NULL, UU_DEFAULT); 2871 uu_list_node_init(node, &node->usn_listnode, listpool); 2872 2873 while (node != NULL) { 2874 rmnode = node; 2875 node = uu_avl_next(avl_tree, node); 2876 uu_avl_remove(avl_tree, rmnode); 2877 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL) 2878 uu_list_insert(list, rmnode, idx2); 2879 } 2880 2881 for (node = uu_list_first(list); node != NULL; 2882 node = uu_list_next(list, node)) { 2883 us_sort_info_t sortinfo = { sortcol, cb.cb_numname }; 2884 2885 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL) 2886 uu_avl_insert(avl_tree, node, idx); 2887 } 2888 2889 uu_list_destroy(list); 2890 uu_list_pool_destroy(listpool); 2891 2892 /* Print and free node nvlist memory */ 2893 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE, 2894 cb.cb_avl); 2895 2896 zfs_free_sort_columns(sortcol); 2897 2898 /* Clean up the AVL tree */ 2899 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL) 2900 nomem(); 2901 2902 while ((node = uu_avl_walk_next(walk)) != NULL) { 2903 uu_avl_remove(cb.cb_avl, node); 2904 free(node); 2905 } 2906 2907 uu_avl_walk_end(walk); 2908 uu_avl_destroy(avl_tree); 2909 uu_avl_pool_destroy(avl_pool); 2910 2911 return (ret); 2912} 2913 2914/* 2915 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ... 2916 * [-t type[,...]] [filesystem|volume|snapshot] ... 2917 * 2918 * -H Scripted mode; elide headers and separate columns by tabs. 2919 * -p Display values in parsable (literal) format. 2920 * -r Recurse over all children. 2921 * -d Limit recursion by depth. 2922 * -o Control which fields to display. 2923 * -s Specify sort columns, descending order. 2924 * -S Specify sort columns, ascending order. 2925 * -t Control which object types to display. 2926 * 2927 * When given no arguments, list all filesystems in the system. 2928 * Otherwise, list the specified datasets, optionally recursing down them if 2929 * '-r' is specified. 2930 */ 2931typedef struct list_cbdata { 2932 boolean_t cb_first; 2933 boolean_t cb_literal; 2934 boolean_t cb_scripted; 2935 zprop_list_t *cb_proplist; 2936} list_cbdata_t; 2937 2938/* 2939 * Given a list of columns to display, output appropriate headers for each one. 2940 */ 2941static void 2942print_header(list_cbdata_t *cb) 2943{ 2944 zprop_list_t *pl = cb->cb_proplist; 2945 char headerbuf[ZFS_MAXPROPLEN]; 2946 const char *header; 2947 int i; 2948 boolean_t first = B_TRUE; 2949 boolean_t right_justify; 2950 2951 for (; pl != NULL; pl = pl->pl_next) { 2952 if (!first) { 2953 (void) printf(" "); 2954 } else { 2955 first = B_FALSE; 2956 } 2957 2958 right_justify = B_FALSE; 2959 if (pl->pl_prop != ZPROP_INVAL) { 2960 header = zfs_prop_column_name(pl->pl_prop); 2961 right_justify = zfs_prop_align_right(pl->pl_prop); 2962 } else { 2963 for (i = 0; pl->pl_user_prop[i] != '\0'; i++) 2964 headerbuf[i] = toupper(pl->pl_user_prop[i]); 2965 headerbuf[i] = '\0'; 2966 header = headerbuf; 2967 } 2968 2969 if (pl->pl_next == NULL && !right_justify) 2970 (void) printf("%s", header); 2971 else if (right_justify) 2972 (void) printf("%*s", pl->pl_width, header); 2973 else 2974 (void) printf("%-*s", pl->pl_width, header); 2975 } 2976 2977 (void) printf("\n"); 2978} 2979 2980/* 2981 * Given a dataset and a list of fields, print out all the properties according 2982 * to the described layout. 2983 */ 2984static void 2985print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb) 2986{ 2987 zprop_list_t *pl = cb->cb_proplist; 2988 boolean_t first = B_TRUE; 2989 char property[ZFS_MAXPROPLEN]; 2990 nvlist_t *userprops = zfs_get_user_props(zhp); 2991 nvlist_t *propval; 2992 char *propstr; 2993 boolean_t right_justify; 2994 2995 for (; pl != NULL; pl = pl->pl_next) { 2996 if (!first) { 2997 if (cb->cb_scripted) 2998 (void) printf("\t"); 2999 else 3000 (void) printf(" "); 3001 } else { 3002 first = B_FALSE; 3003 } 3004 3005 if (pl->pl_prop == ZFS_PROP_NAME) { 3006 (void) strlcpy(property, zfs_get_name(zhp), 3007 sizeof(property)); 3008 propstr = property; 3009 right_justify = zfs_prop_align_right(pl->pl_prop); 3010 } else if (pl->pl_prop != ZPROP_INVAL) { 3011 if (zfs_prop_get(zhp, pl->pl_prop, property, 3012 sizeof (property), NULL, NULL, 0, 3013 cb->cb_literal) != 0) 3014 propstr = "-"; 3015 else 3016 propstr = property; 3017 right_justify = zfs_prop_align_right(pl->pl_prop); 3018 } else if (zfs_prop_userquota(pl->pl_user_prop)) { 3019 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop, 3020 property, sizeof (property), cb->cb_literal) != 0) 3021 propstr = "-"; 3022 else 3023 propstr = property; 3024 right_justify = B_TRUE; 3025 } else if (zfs_prop_written(pl->pl_user_prop)) { 3026 if (zfs_prop_get_written(zhp, pl->pl_user_prop, 3027 property, sizeof (property), cb->cb_literal) != 0) 3028 propstr = "-"; 3029 else 3030 propstr = property; 3031 right_justify = B_TRUE; 3032 } else { 3033 if (nvlist_lookup_nvlist(userprops, 3034 pl->pl_user_prop, &propval) != 0) 3035 propstr = "-"; 3036 else 3037 verify(nvlist_lookup_string(propval, 3038 ZPROP_VALUE, &propstr) == 0); 3039 right_justify = B_FALSE; 3040 } 3041 3042 /* 3043 * If this is being called in scripted mode, or if this is the 3044 * last column and it is left-justified, don't include a width 3045 * format specifier. 3046 */ 3047 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify)) 3048 (void) printf("%s", propstr); 3049 else if (right_justify) 3050 (void) printf("%*s", pl->pl_width, propstr); 3051 else 3052 (void) printf("%-*s", pl->pl_width, propstr); 3053 } 3054 3055 (void) printf("\n"); 3056} 3057 3058/* 3059 * Generic callback function to list a dataset or snapshot. 3060 */ 3061static int 3062list_callback(zfs_handle_t *zhp, void *data) 3063{ 3064 list_cbdata_t *cbp = data; 3065 3066 if (cbp->cb_first) { 3067 if (!cbp->cb_scripted) 3068 print_header(cbp); 3069 cbp->cb_first = B_FALSE; 3070 } 3071 3072 print_dataset(zhp, cbp); 3073 3074 return (0); 3075} 3076 3077static int 3078zfs_do_list(int argc, char **argv) 3079{ 3080 int c; 3081 static char default_fields[] = 3082 "name,used,available,referenced,mountpoint"; 3083 int types = ZFS_TYPE_DATASET; 3084 boolean_t types_specified = B_FALSE; 3085 char *fields = NULL; 3086 list_cbdata_t cb = { 0 }; 3087 char *value; 3088 int limit = 0; 3089 int ret = 0; 3090 zfs_sort_column_t *sortcol = NULL; 3091 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS; 3092 3093 /* check options */ 3094 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) { 3095 switch (c) { 3096 case 'o': 3097 fields = optarg; 3098 break; 3099 case 'p': 3100 cb.cb_literal = B_TRUE; 3101 flags |= ZFS_ITER_LITERAL_PROPS; 3102 break; 3103 case 'd': 3104 limit = parse_depth(optarg, &flags); 3105 break; 3106 case 'r': 3107 flags |= ZFS_ITER_RECURSE; 3108 break; 3109 case 'H': 3110 cb.cb_scripted = B_TRUE; 3111 break; 3112 case 's': 3113 if (zfs_add_sort_column(&sortcol, optarg, 3114 B_FALSE) != 0) { 3115 (void) fprintf(stderr, 3116 gettext("invalid property '%s'\n"), optarg); 3117 usage(B_FALSE); 3118 } 3119 break; 3120 case 'S': 3121 if (zfs_add_sort_column(&sortcol, optarg, 3122 B_TRUE) != 0) { 3123 (void) fprintf(stderr, 3124 gettext("invalid property '%s'\n"), optarg); 3125 usage(B_FALSE); 3126 } 3127 break; 3128 case 't': 3129 types = 0; 3130 types_specified = B_TRUE; 3131 flags &= ~ZFS_ITER_PROP_LISTSNAPS; 3132 while (*optarg != '\0') { 3133 static char *type_subopts[] = { "filesystem", 3134 "volume", "snapshot", "snap", "bookmark", 3135 "all", NULL }; 3136 3137 switch (getsubopt(&optarg, type_subopts, 3138 &value)) { 3139 case 0: 3140 types |= ZFS_TYPE_FILESYSTEM; 3141 break; 3142 case 1: 3143 types |= ZFS_TYPE_VOLUME; 3144 break; 3145 case 2: 3146 case 3: 3147 types |= ZFS_TYPE_SNAPSHOT; 3148 break; 3149 case 4: 3150 types |= ZFS_TYPE_BOOKMARK; 3151 break; 3152 case 5: 3153 types = ZFS_TYPE_DATASET | 3154 ZFS_TYPE_BOOKMARK; 3155 break; 3156 default: 3157 (void) fprintf(stderr, 3158 gettext("invalid type '%s'\n"), 3159 suboptarg); 3160 usage(B_FALSE); 3161 } 3162 } 3163 break; 3164 case ':': 3165 (void) fprintf(stderr, gettext("missing argument for " 3166 "'%c' option\n"), optopt); 3167 usage(B_FALSE); 3168 break; 3169 case '?': 3170 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3171 optopt); 3172 usage(B_FALSE); 3173 } 3174 } 3175 3176 argc -= optind; 3177 argv += optind; 3178 3179 if (fields == NULL) 3180 fields = default_fields; 3181 3182 /* 3183 * If we are only going to list snapshot names and sort by name, 3184 * then we can use faster version. 3185 */ 3186 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol)) 3187 flags |= ZFS_ITER_SIMPLE; 3188 3189 /* 3190 * If "-o space" and no types were specified, don't display snapshots. 3191 */ 3192 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE) 3193 types &= ~ZFS_TYPE_SNAPSHOT; 3194 3195 /* 3196 * If the user specifies '-o all', the zprop_get_list() doesn't 3197 * normally include the name of the dataset. For 'zfs list', we always 3198 * want this property to be first. 3199 */ 3200 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET) 3201 != 0) 3202 usage(B_FALSE); 3203 3204 cb.cb_first = B_TRUE; 3205 3206 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist, 3207 limit, list_callback, &cb); 3208 3209 zprop_free_list(cb.cb_proplist); 3210 zfs_free_sort_columns(sortcol); 3211 3212 if (ret == 0 && cb.cb_first && !cb.cb_scripted) 3213 (void) printf(gettext("no datasets available\n")); 3214 3215 return (ret); 3216} 3217 3218/* 3219 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol> 3220 * zfs rename [-f] -p <fs | vol> <fs | vol> 3221 * zfs rename -r <snap> <snap> 3222 * zfs rename -u [-p] <fs> <fs> 3223 * 3224 * Renames the given dataset to another of the same type. 3225 * 3226 * The '-p' flag creates all the non-existing ancestors of the target first. 3227 */ 3228/* ARGSUSED */ 3229static int 3230zfs_do_rename(int argc, char **argv) 3231{ 3232 zfs_handle_t *zhp; 3233 renameflags_t flags = { 0 }; 3234 int c; 3235 int ret = 0; 3236 int types; 3237 boolean_t parents = B_FALSE; 3238 char *snapshot = NULL; 3239 3240 /* check options */ 3241 while ((c = getopt(argc, argv, "fpru")) != -1) { 3242 switch (c) { 3243 case 'p': 3244 parents = B_TRUE; 3245 break; 3246 case 'r': 3247 flags.recurse = B_TRUE; 3248 break; 3249 case 'u': 3250 flags.nounmount = B_TRUE; 3251 break; 3252 case 'f': 3253 flags.forceunmount = B_TRUE; 3254 break; 3255 case '?': 3256 default: 3257 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3258 optopt); 3259 usage(B_FALSE); 3260 } 3261 } 3262 3263 argc -= optind; 3264 argv += optind; 3265 3266 /* check number of arguments */ 3267 if (argc < 1) { 3268 (void) fprintf(stderr, gettext("missing source dataset " 3269 "argument\n")); 3270 usage(B_FALSE); 3271 } 3272 if (argc < 2) { 3273 (void) fprintf(stderr, gettext("missing target dataset " 3274 "argument\n")); 3275 usage(B_FALSE); 3276 } 3277 if (argc > 2) { 3278 (void) fprintf(stderr, gettext("too many arguments\n")); 3279 usage(B_FALSE); 3280 } 3281 3282 if (flags.recurse && parents) { 3283 (void) fprintf(stderr, gettext("-p and -r options are mutually " 3284 "exclusive\n")); 3285 usage(B_FALSE); 3286 } 3287 3288 if (flags.recurse && strchr(argv[0], '@') == 0) { 3289 (void) fprintf(stderr, gettext("source dataset for recursive " 3290 "rename must be a snapshot\n")); 3291 usage(B_FALSE); 3292 } 3293 3294 if (flags.nounmount && parents) { 3295 (void) fprintf(stderr, gettext("-u and -p options are mutually " 3296 "exclusive\n")); 3297 usage(B_FALSE); 3298 } 3299 3300 if (flags.nounmount) 3301 types = ZFS_TYPE_FILESYSTEM; 3302 else if (parents) 3303 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME; 3304 else 3305 types = ZFS_TYPE_DATASET; 3306 3307 if (flags.recurse) { 3308 /* 3309 * When we do recursive rename we are fine when the given 3310 * snapshot for the given dataset doesn't exist - it can 3311 * still exists below. 3312 */ 3313 3314 snapshot = strchr(argv[0], '@'); 3315 assert(snapshot != NULL); 3316 *snapshot = '\0'; 3317 snapshot++; 3318 } 3319 3320 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL) 3321 return (1); 3322 3323 /* If we were asked and the name looks good, try to create ancestors. */ 3324 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) && 3325 zfs_create_ancestors(g_zfs, argv[1]) != 0) { 3326 zfs_close(zhp); 3327 return (1); 3328 } 3329 3330 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0); 3331 3332 zfs_close(zhp); 3333 return (ret); 3334} 3335 3336/* 3337 * zfs promote <fs> 3338 * 3339 * Promotes the given clone fs to be the parent 3340 */ 3341/* ARGSUSED */ 3342static int 3343zfs_do_promote(int argc, char **argv) 3344{ 3345 zfs_handle_t *zhp; 3346 int ret = 0; 3347 3348 /* check options */ 3349 if (argc > 1 && argv[1][0] == '-') { 3350 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3351 argv[1][1]); 3352 usage(B_FALSE); 3353 } 3354 3355 /* check number of arguments */ 3356 if (argc < 2) { 3357 (void) fprintf(stderr, gettext("missing clone filesystem" 3358 " argument\n")); 3359 usage(B_FALSE); 3360 } 3361 if (argc > 2) { 3362 (void) fprintf(stderr, gettext("too many arguments\n")); 3363 usage(B_FALSE); 3364 } 3365 3366 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3367 if (zhp == NULL) 3368 return (1); 3369 3370 ret = (zfs_promote(zhp) != 0); 3371 3372 3373 zfs_close(zhp); 3374 return (ret); 3375} 3376 3377/* 3378 * zfs rollback [-rRf] <snapshot> 3379 * 3380 * -r Delete any intervening snapshots before doing rollback 3381 * -R Delete any snapshots and their clones 3382 * -f ignored for backwards compatability 3383 * 3384 * Given a filesystem, rollback to a specific snapshot, discarding any changes 3385 * since then and making it the active dataset. If more recent snapshots exist, 3386 * the command will complain unless the '-r' flag is given. 3387 */ 3388typedef struct rollback_cbdata { 3389 uint64_t cb_create; 3390 boolean_t cb_first; 3391 int cb_doclones; 3392 char *cb_target; 3393 int cb_error; 3394 boolean_t cb_recurse; 3395} rollback_cbdata_t; 3396 3397static int 3398rollback_check_dependent(zfs_handle_t *zhp, void *data) 3399{ 3400 rollback_cbdata_t *cbp = data; 3401 3402 if (cbp->cb_first && cbp->cb_recurse) { 3403 (void) fprintf(stderr, gettext("cannot rollback to " 3404 "'%s': clones of previous snapshots exist\n"), 3405 cbp->cb_target); 3406 (void) fprintf(stderr, gettext("use '-R' to " 3407 "force deletion of the following clones and " 3408 "dependents:\n")); 3409 cbp->cb_first = 0; 3410 cbp->cb_error = 1; 3411 } 3412 3413 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp)); 3414 3415 zfs_close(zhp); 3416 return (0); 3417} 3418 3419/* 3420 * Report any snapshots more recent than the one specified. Used when '-r' is 3421 * not specified. We reuse this same callback for the snapshot dependents - if 3422 * 'cb_dependent' is set, then this is a dependent and we should report it 3423 * without checking the transaction group. 3424 */ 3425static int 3426rollback_check(zfs_handle_t *zhp, void *data) 3427{ 3428 rollback_cbdata_t *cbp = data; 3429 3430 if (cbp->cb_doclones) { 3431 zfs_close(zhp); 3432 return (0); 3433 } 3434 3435 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { 3436 if (cbp->cb_first && !cbp->cb_recurse) { 3437 (void) fprintf(stderr, gettext("cannot " 3438 "rollback to '%s': more recent snapshots " 3439 "or bookmarks exist\n"), 3440 cbp->cb_target); 3441 (void) fprintf(stderr, gettext("use '-r' to " 3442 "force deletion of the following " 3443 "snapshots and bookmarks:\n")); 3444 cbp->cb_first = 0; 3445 cbp->cb_error = 1; 3446 } 3447 3448 if (cbp->cb_recurse) { 3449 if (zfs_iter_dependents(zhp, B_TRUE, 3450 rollback_check_dependent, cbp) != 0) { 3451 zfs_close(zhp); 3452 return (-1); 3453 } 3454 } else { 3455 (void) fprintf(stderr, "%s\n", 3456 zfs_get_name(zhp)); 3457 } 3458 } 3459 zfs_close(zhp); 3460 return (0); 3461} 3462 3463static int 3464zfs_do_rollback(int argc, char **argv) 3465{ 3466 int ret = 0; 3467 int c; 3468 boolean_t force = B_FALSE; 3469 rollback_cbdata_t cb = { 0 }; 3470 zfs_handle_t *zhp, *snap; 3471 char parentname[ZFS_MAXNAMELEN]; 3472 char *delim; 3473 3474 /* check options */ 3475 while ((c = getopt(argc, argv, "rRf")) != -1) { 3476 switch (c) { 3477 case 'r': 3478 cb.cb_recurse = 1; 3479 break; 3480 case 'R': 3481 cb.cb_recurse = 1; 3482 cb.cb_doclones = 1; 3483 break; 3484 case 'f': 3485 force = B_TRUE; 3486 break; 3487 case '?': 3488 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3489 optopt); 3490 usage(B_FALSE); 3491 } 3492 } 3493 3494 argc -= optind; 3495 argv += optind; 3496 3497 /* check number of arguments */ 3498 if (argc < 1) { 3499 (void) fprintf(stderr, gettext("missing dataset argument\n")); 3500 usage(B_FALSE); 3501 } 3502 if (argc > 1) { 3503 (void) fprintf(stderr, gettext("too many arguments\n")); 3504 usage(B_FALSE); 3505 } 3506 3507 /* open the snapshot */ 3508 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) 3509 return (1); 3510 3511 /* open the parent dataset */ 3512 (void) strlcpy(parentname, argv[0], sizeof (parentname)); 3513 verify((delim = strrchr(parentname, '@')) != NULL); 3514 *delim = '\0'; 3515 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) { 3516 zfs_close(snap); 3517 return (1); 3518 } 3519 3520 /* 3521 * Check for more recent snapshots and/or clones based on the presence 3522 * of '-r' and '-R'. 3523 */ 3524 cb.cb_target = argv[0]; 3525 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 3526 cb.cb_first = B_TRUE; 3527 cb.cb_error = 0; 3528 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0) 3529 goto out; 3530 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0) 3531 goto out; 3532 3533 if ((ret = cb.cb_error) != 0) 3534 goto out; 3535 3536 /* 3537 * Rollback parent to the given snapshot. 3538 */ 3539 ret = zfs_rollback(zhp, snap, force); 3540 3541out: 3542 zfs_close(snap); 3543 zfs_close(zhp); 3544 3545 if (ret == 0) 3546 return (0); 3547 else 3548 return (1); 3549} 3550 3551/* 3552 * zfs set property=value ... { fs | snap | vol } ... 3553 * 3554 * Sets the given properties for all datasets specified on the command line. 3555 */ 3556 3557static int 3558set_callback(zfs_handle_t *zhp, void *data) 3559{ 3560 nvlist_t *props = data; 3561 3562 if (zfs_prop_set_list(zhp, props) != 0) { 3563 switch (libzfs_errno(g_zfs)) { 3564 case EZFS_MOUNTFAILED: 3565 (void) fprintf(stderr, gettext("property may be set " 3566 "but unable to remount filesystem\n")); 3567 break; 3568 case EZFS_SHARENFSFAILED: 3569 (void) fprintf(stderr, gettext("property may be set " 3570 "but unable to reshare filesystem\n")); 3571 break; 3572 } 3573 return (1); 3574 } 3575 return (0); 3576} 3577 3578static int 3579zfs_do_set(int argc, char **argv) 3580{ 3581 nvlist_t *props = NULL; 3582 int ds_start = -1; /* argv idx of first dataset arg */ 3583 int ret = 0; 3584 3585 /* check for options */ 3586 if (argc > 1 && argv[1][0] == '-') { 3587 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3588 argv[1][1]); 3589 usage(B_FALSE); 3590 } 3591 3592 /* check number of arguments */ 3593 if (argc < 2) { 3594 (void) fprintf(stderr, gettext("missing arguments\n")); 3595 usage(B_FALSE); 3596 } 3597 if (argc < 3) { 3598 if (strchr(argv[1], '=') == NULL) { 3599 (void) fprintf(stderr, gettext("missing property=value " 3600 "argument(s)\n")); 3601 } else { 3602 (void) fprintf(stderr, gettext("missing dataset " 3603 "name(s)\n")); 3604 } 3605 usage(B_FALSE); 3606 } 3607 3608 /* validate argument order: prop=val args followed by dataset args */ 3609 for (int i = 1; i < argc; i++) { 3610 if (strchr(argv[i], '=') != NULL) { 3611 if (ds_start > 0) { 3612 /* out-of-order prop=val argument */ 3613 (void) fprintf(stderr, gettext("invalid " 3614 "argument order\n"), i); 3615 usage(B_FALSE); 3616 } 3617 } else if (ds_start < 0) { 3618 ds_start = i; 3619 } 3620 } 3621 if (ds_start < 0) { 3622 (void) fprintf(stderr, gettext("missing dataset name(s)\n")); 3623 usage(B_FALSE); 3624 } 3625 3626 /* Populate a list of property settings */ 3627 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 3628 nomem(); 3629 for (int i = 1; i < ds_start; i++) { 3630 if ((ret = parseprop(props, argv[i])) != 0) 3631 goto error; 3632 } 3633 3634 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0, 3635 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props); 3636 3637error: 3638 nvlist_free(props); 3639 return (ret); 3640} 3641 3642typedef struct snap_cbdata { 3643 nvlist_t *sd_nvl; 3644 boolean_t sd_recursive; 3645 const char *sd_snapname; 3646} snap_cbdata_t; 3647 3648static int 3649zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 3650{ 3651 snap_cbdata_t *sd = arg; 3652 char *name; 3653 int rv = 0; 3654 int error; 3655 3656 if (sd->sd_recursive && 3657 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) { 3658 zfs_close(zhp); 3659 return (0); 3660 } 3661 3662 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname); 3663 if (error == -1) 3664 nomem(); 3665 fnvlist_add_boolean(sd->sd_nvl, name); 3666 free(name); 3667 3668 if (sd->sd_recursive) 3669 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); 3670 zfs_close(zhp); 3671 return (rv); 3672} 3673 3674/* 3675 * zfs snapshot [-r] [-o prop=value] ... <fs@snap> 3676 * 3677 * Creates a snapshot with the given name. While functionally equivalent to 3678 * 'zfs create', it is a separate command to differentiate intent. 3679 */ 3680static int 3681zfs_do_snapshot(int argc, char **argv) 3682{ 3683 int ret = 0; 3684 int c; 3685 nvlist_t *props; 3686 snap_cbdata_t sd = { 0 }; 3687 boolean_t multiple_snaps = B_FALSE; 3688 3689 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 3690 nomem(); 3691 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0) 3692 nomem(); 3693 3694 /* check options */ 3695 while ((c = getopt(argc, argv, "ro:")) != -1) { 3696 switch (c) { 3697 case 'o': 3698 if (parseprop(props, optarg) != 0) 3699 return (1); 3700 break; 3701 case 'r': 3702 sd.sd_recursive = B_TRUE; 3703 multiple_snaps = B_TRUE; 3704 break; 3705 case '?': 3706 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3707 optopt); 3708 goto usage; 3709 } 3710 } 3711 3712 argc -= optind; 3713 argv += optind; 3714 3715 /* check number of arguments */ 3716 if (argc < 1) { 3717 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 3718 goto usage; 3719 } 3720 3721 if (argc > 1) 3722 multiple_snaps = B_TRUE; 3723 for (; argc > 0; argc--, argv++) { 3724 char *atp; 3725 zfs_handle_t *zhp; 3726 3727 atp = strchr(argv[0], '@'); 3728 if (atp == NULL) 3729 goto usage; 3730 *atp = '\0'; 3731 sd.sd_snapname = atp + 1; 3732 zhp = zfs_open(g_zfs, argv[0], 3733 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3734 if (zhp == NULL) 3735 goto usage; 3736 if (zfs_snapshot_cb(zhp, &sd) != 0) 3737 goto usage; 3738 } 3739 3740 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props); 3741 nvlist_free(sd.sd_nvl); 3742 nvlist_free(props); 3743 if (ret != 0 && multiple_snaps) 3744 (void) fprintf(stderr, gettext("no snapshots were created\n")); 3745 return (ret != 0); 3746 3747usage: 3748 nvlist_free(sd.sd_nvl); 3749 nvlist_free(props); 3750 usage(B_FALSE); 3751 return (-1); 3752} 3753 3754/* 3755 * Send a backup stream to stdout. 3756 */ 3757static int 3758zfs_do_send(int argc, char **argv) 3759{ 3760 char *fromname = NULL; 3761 char *toname = NULL; 3762 char *resume_token = NULL; 3763 char *cp; 3764 zfs_handle_t *zhp; 3765 sendflags_t flags = { 0 }; 3766 int c, err; 3767 nvlist_t *dbgnv = NULL; 3768 boolean_t extraverbose = B_FALSE; 3769 3770 /* check options */ 3771 while ((c = getopt(argc, argv, ":i:I:RDpvnPLet:")) != -1) { 3772 switch (c) { 3773 case 'i': 3774 if (fromname) 3775 usage(B_FALSE); 3776 fromname = optarg; 3777 break; 3778 case 'I': 3779 if (fromname) 3780 usage(B_FALSE); 3781 fromname = optarg; 3782 flags.doall = B_TRUE; 3783 break; 3784 case 'R': 3785 flags.replicate = B_TRUE; 3786 break; 3787 case 'p': 3788 flags.props = B_TRUE; 3789 break; 3790 case 'P': 3791 flags.parsable = B_TRUE; 3792 flags.verbose = B_TRUE; 3793 break; 3794 case 'v': 3795 if (flags.verbose) 3796 extraverbose = B_TRUE; 3797 flags.verbose = B_TRUE; 3798 flags.progress = B_TRUE; 3799 break; 3800 case 'D': 3801 flags.dedup = B_TRUE; 3802 break; 3803 case 'n': 3804 flags.dryrun = B_TRUE; 3805 break; 3806 case 'L': 3807 flags.largeblock = B_TRUE; 3808 break; 3809 case 'e': 3810 flags.embed_data = B_TRUE; 3811 break; 3812 case 't': 3813 resume_token = optarg; 3814 break; 3815 case ':': 3816 (void) fprintf(stderr, gettext("missing argument for " 3817 "'%c' option\n"), optopt); 3818 usage(B_FALSE); 3819 break; 3820 case '?': 3821 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 3822 optopt); 3823 usage(B_FALSE); 3824 } 3825 } 3826 3827 argc -= optind; 3828 argv += optind; 3829 3830 if (resume_token != NULL) { 3831 if (fromname != NULL || flags.replicate || flags.props || 3832 flags.dedup) { 3833 (void) fprintf(stderr, 3834 gettext("invalid flags combined with -t\n")); 3835 usage(B_FALSE); 3836 } 3837 if (argc != 0) { 3838 (void) fprintf(stderr, gettext("no additional " 3839 "arguments are permitted with -t\n")); 3840 usage(B_FALSE); 3841 } 3842 } else { 3843 if (argc < 1) { 3844 (void) fprintf(stderr, 3845 gettext("missing snapshot argument\n")); 3846 usage(B_FALSE); 3847 } 3848 if (argc > 1) { 3849 (void) fprintf(stderr, gettext("too many arguments\n")); 3850 usage(B_FALSE); 3851 } 3852 } 3853 3854 if (!flags.dryrun && isatty(STDOUT_FILENO)) { 3855 (void) fprintf(stderr, 3856 gettext("Error: Stream can not be written to a terminal.\n" 3857 "You must redirect standard output.\n")); 3858 return (1); 3859 } 3860 3861 if (resume_token != NULL) { 3862 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO, 3863 resume_token)); 3864 } 3865 3866 /* 3867 * Special case sending a filesystem, or from a bookmark. 3868 */ 3869 if (strchr(argv[0], '@') == NULL || 3870 (fromname && strchr(fromname, '#') != NULL)) { 3871 char frombuf[ZFS_MAXNAMELEN]; 3872 enum lzc_send_flags lzc_flags = 0; 3873 3874 if (flags.replicate || flags.doall || flags.props || 3875 flags.dedup || flags.dryrun || flags.verbose || 3876 flags.progress) { 3877 (void) fprintf(stderr, 3878 gettext("Error: " 3879 "Unsupported flag with filesystem or bookmark.\n")); 3880 return (1); 3881 } 3882 3883 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET); 3884 if (zhp == NULL) 3885 return (1); 3886 3887 if (flags.largeblock) 3888 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK; 3889 if (flags.embed_data) 3890 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA; 3891 3892 if (fromname != NULL && 3893 (fromname[0] == '#' || fromname[0] == '@')) { 3894 /* 3895 * Incremental source name begins with # or @. 3896 * Default to same fs as target. 3897 */ 3898 (void) strncpy(frombuf, argv[0], sizeof (frombuf)); 3899 cp = strchr(frombuf, '@'); 3900 if (cp != NULL) 3901 *cp = '\0'; 3902 (void) strlcat(frombuf, fromname, sizeof (frombuf)); 3903 fromname = frombuf; 3904 } 3905 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags); 3906 zfs_close(zhp); 3907 return (err != 0); 3908 } 3909 3910 cp = strchr(argv[0], '@'); 3911 *cp = '\0'; 3912 toname = cp + 1; 3913 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 3914 if (zhp == NULL) 3915 return (1); 3916 3917 /* 3918 * If they specified the full path to the snapshot, chop off 3919 * everything except the short name of the snapshot, but special 3920 * case if they specify the origin. 3921 */ 3922 if (fromname && (cp = strchr(fromname, '@')) != NULL) { 3923 char origin[ZFS_MAXNAMELEN]; 3924 zprop_source_t src; 3925 3926 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN, 3927 origin, sizeof (origin), &src, NULL, 0, B_FALSE); 3928 3929 if (strcmp(origin, fromname) == 0) { 3930 fromname = NULL; 3931 flags.fromorigin = B_TRUE; 3932 } else { 3933 *cp = '\0'; 3934 if (cp != fromname && strcmp(argv[0], fromname)) { 3935 (void) fprintf(stderr, 3936 gettext("incremental source must be " 3937 "in same filesystem\n")); 3938 usage(B_FALSE); 3939 } 3940 fromname = cp + 1; 3941 if (strchr(fromname, '@') || strchr(fromname, '/')) { 3942 (void) fprintf(stderr, 3943 gettext("invalid incremental source\n")); 3944 usage(B_FALSE); 3945 } 3946 } 3947 } 3948 3949 if (flags.replicate && fromname == NULL) 3950 flags.doall = B_TRUE; 3951 3952 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0, 3953 extraverbose ? &dbgnv : NULL); 3954 3955 if (extraverbose && dbgnv != NULL) { 3956 /* 3957 * dump_nvlist prints to stdout, but that's been 3958 * redirected to a file. Make it print to stderr 3959 * instead. 3960 */ 3961 (void) dup2(STDERR_FILENO, STDOUT_FILENO); 3962 dump_nvlist(dbgnv, 0); 3963 nvlist_free(dbgnv); 3964 } 3965 zfs_close(zhp); 3966 3967 return (err != 0); 3968} 3969 3970/* 3971 * Restore a backup stream from stdin. 3972 */ 3973static int 3974zfs_do_receive(int argc, char **argv) 3975{ 3976 int c, err; 3977 recvflags_t flags = { 0 }; 3978 boolean_t abort_resumable = B_FALSE; 3979 3980 nvlist_t *props; 3981 nvpair_t *nvp = NULL; 3982 3983 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) 3984 nomem(); 3985 3986 /* check options */ 3987 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) { 3988 switch (c) { 3989 case 'o': 3990 if (parseprop(props, optarg) != 0) 3991 return (1); 3992 break; 3993 case 'd': 3994 flags.isprefix = B_TRUE; 3995 break; 3996 case 'e': 3997 flags.isprefix = B_TRUE; 3998 flags.istail = B_TRUE; 3999 break; 4000 case 'n': 4001 flags.dryrun = B_TRUE; 4002 break; 4003 case 'u': 4004 flags.nomount = B_TRUE; 4005 break; 4006 case 'v': 4007 flags.verbose = B_TRUE; 4008 break; 4009 case 's': 4010 flags.resumable = B_TRUE; 4011 break; 4012 case 'F': 4013 flags.force = B_TRUE; 4014 break; 4015 case 'A': 4016 abort_resumable = B_TRUE; 4017 break; 4018 case ':': 4019 (void) fprintf(stderr, gettext("missing argument for " 4020 "'%c' option\n"), optopt); 4021 usage(B_FALSE); 4022 break; 4023 case '?': 4024 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 4025 optopt); 4026 usage(B_FALSE); 4027 } 4028 } 4029 4030 argc -= optind; 4031 argv += optind; 4032 4033 /* check number of arguments */ 4034 if (argc < 1) { 4035 (void) fprintf(stderr, gettext("missing snapshot argument\n")); 4036 usage(B_FALSE); 4037 } 4038 if (argc > 1) { 4039 (void) fprintf(stderr, gettext("too many arguments\n")); 4040 usage(B_FALSE); 4041 } 4042 4043 while ((nvp = nvlist_next_nvpair(props, nvp))) { 4044 if (strcmp(nvpair_name(nvp), "origin") != 0) { 4045 (void) fprintf(stderr, gettext("invalid option")); 4046 usage(B_FALSE); 4047 } 4048 } 4049 4050 if (abort_resumable) { 4051 if (flags.isprefix || flags.istail || flags.dryrun || 4052 flags.resumable || flags.nomount) { 4053 (void) fprintf(stderr, gettext("invalid option")); 4054 usage(B_FALSE); 4055 } 4056 4057 char namebuf[ZFS_MAXNAMELEN]; 4058 (void) snprintf(namebuf, sizeof (namebuf), 4059 "%s/%%recv", argv[0]); 4060 4061 if (zfs_dataset_exists(g_zfs, namebuf, 4062 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) { 4063 zfs_handle_t *zhp = zfs_open(g_zfs, 4064 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 4065 if (zhp == NULL) 4066 return (1); 4067 err = zfs_destroy(zhp, B_FALSE); 4068 } else { 4069 zfs_handle_t *zhp = zfs_open(g_zfs, 4070 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); 4071 if (zhp == NULL) 4072 usage(B_FALSE); 4073 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) || 4074 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, 4075 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) { 4076 (void) fprintf(stderr, 4077 gettext("'%s' does not have any " 4078 "resumable receive state to abort\n"), 4079 argv[0]); 4080 return (1); 4081 } 4082 err = zfs_destroy(zhp, B_FALSE); 4083 } 4084 4085 return (err != 0); 4086 } 4087 4088 if (isatty(STDIN_FILENO)) { 4089 (void) fprintf(stderr, 4090 gettext("Error: Backup stream can not be read " 4091 "from a terminal.\n" 4092 "You must redirect standard input.\n")); 4093 return (1); 4094 } 4095 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL); 4096 4097 return (err != 0); 4098} 4099 4100/* 4101 * allow/unallow stuff 4102 */ 4103/* copied from zfs/sys/dsl_deleg.h */ 4104#define ZFS_DELEG_PERM_CREATE "create" 4105#define ZFS_DELEG_PERM_DESTROY "destroy" 4106#define ZFS_DELEG_PERM_SNAPSHOT "snapshot" 4107#define ZFS_DELEG_PERM_ROLLBACK "rollback" 4108#define ZFS_DELEG_PERM_CLONE "clone" 4109#define ZFS_DELEG_PERM_PROMOTE "promote" 4110#define ZFS_DELEG_PERM_RENAME "rename" 4111#define ZFS_DELEG_PERM_MOUNT "mount" 4112#define ZFS_DELEG_PERM_SHARE "share" 4113#define ZFS_DELEG_PERM_SEND "send" 4114#define ZFS_DELEG_PERM_RECEIVE "receive" 4115#define ZFS_DELEG_PERM_ALLOW "allow" 4116#define ZFS_DELEG_PERM_USERPROP "userprop" 4117#define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */ 4118#define ZFS_DELEG_PERM_USERQUOTA "userquota" 4119#define ZFS_DELEG_PERM_GROUPQUOTA "groupquota" 4120#define ZFS_DELEG_PERM_USERUSED "userused" 4121#define ZFS_DELEG_PERM_GROUPUSED "groupused" 4122#define ZFS_DELEG_PERM_HOLD "hold" 4123#define ZFS_DELEG_PERM_RELEASE "release" 4124#define ZFS_DELEG_PERM_DIFF "diff" 4125#define ZFS_DELEG_PERM_BOOKMARK "bookmark" 4126 4127#define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE 4128 4129static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = { 4130 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW }, 4131 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE }, 4132 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE }, 4133 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY }, 4134 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF}, 4135 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD }, 4136 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT }, 4137 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE }, 4138 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE }, 4139 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE }, 4140 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME }, 4141 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK }, 4142 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND }, 4143 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE }, 4144 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT }, 4145 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK }, 4146 4147 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA }, 4148 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED }, 4149 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP }, 4150 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA }, 4151 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED }, 4152 { NULL, ZFS_DELEG_NOTE_NONE } 4153}; 4154 4155/* permission structure */ 4156typedef struct deleg_perm { 4157 zfs_deleg_who_type_t dp_who_type; 4158 const char *dp_name; 4159 boolean_t dp_local; 4160 boolean_t dp_descend; 4161} deleg_perm_t; 4162 4163/* */ 4164typedef struct deleg_perm_node { 4165 deleg_perm_t dpn_perm; 4166 4167 uu_avl_node_t dpn_avl_node; 4168} deleg_perm_node_t; 4169 4170typedef struct fs_perm fs_perm_t; 4171 4172/* permissions set */ 4173typedef struct who_perm { 4174 zfs_deleg_who_type_t who_type; 4175 const char *who_name; /* id */ 4176 char who_ug_name[256]; /* user/group name */ 4177 fs_perm_t *who_fsperm; /* uplink */ 4178 4179 uu_avl_t *who_deleg_perm_avl; /* permissions */ 4180} who_perm_t; 4181 4182/* */ 4183typedef struct who_perm_node { 4184 who_perm_t who_perm; 4185 uu_avl_node_t who_avl_node; 4186} who_perm_node_t; 4187 4188typedef struct fs_perm_set fs_perm_set_t; 4189/* fs permissions */ 4190struct fs_perm { 4191 const char *fsp_name; 4192 4193 uu_avl_t *fsp_sc_avl; /* sets,create */ 4194 uu_avl_t *fsp_uge_avl; /* user,group,everyone */ 4195 4196 fs_perm_set_t *fsp_set; /* uplink */ 4197}; 4198 4199/* */ 4200typedef struct fs_perm_node { 4201 fs_perm_t fspn_fsperm; 4202 uu_avl_t *fspn_avl; 4203 4204 uu_list_node_t fspn_list_node; 4205} fs_perm_node_t; 4206 4207/* top level structure */ 4208struct fs_perm_set { 4209 uu_list_pool_t *fsps_list_pool; 4210 uu_list_t *fsps_list; /* list of fs_perms */ 4211 4212 uu_avl_pool_t *fsps_named_set_avl_pool; 4213 uu_avl_pool_t *fsps_who_perm_avl_pool; 4214 uu_avl_pool_t *fsps_deleg_perm_avl_pool; 4215}; 4216 4217static inline const char * 4218deleg_perm_type(zfs_deleg_note_t note) 4219{ 4220 /* subcommands */ 4221 switch (note) { 4222 /* SUBCOMMANDS */ 4223 /* OTHER */ 4224 case ZFS_DELEG_NOTE_GROUPQUOTA: 4225 case ZFS_DELEG_NOTE_GROUPUSED: 4226 case ZFS_DELEG_NOTE_USERPROP: 4227 case ZFS_DELEG_NOTE_USERQUOTA: 4228 case ZFS_DELEG_NOTE_USERUSED: 4229 /* other */ 4230 return (gettext("other")); 4231 default: 4232 return (gettext("subcommand")); 4233 } 4234} 4235 4236static int inline 4237who_type2weight(zfs_deleg_who_type_t who_type) 4238{ 4239 int res; 4240 switch (who_type) { 4241 case ZFS_DELEG_NAMED_SET_SETS: 4242 case ZFS_DELEG_NAMED_SET: 4243 res = 0; 4244 break; 4245 case ZFS_DELEG_CREATE_SETS: 4246 case ZFS_DELEG_CREATE: 4247 res = 1; 4248 break; 4249 case ZFS_DELEG_USER_SETS: 4250 case ZFS_DELEG_USER: 4251 res = 2; 4252 break; 4253 case ZFS_DELEG_GROUP_SETS: 4254 case ZFS_DELEG_GROUP: 4255 res = 3; 4256 break; 4257 case ZFS_DELEG_EVERYONE_SETS: 4258 case ZFS_DELEG_EVERYONE: 4259 res = 4; 4260 break; 4261 default: 4262 res = -1; 4263 } 4264 4265 return (res); 4266} 4267 4268/* ARGSUSED */ 4269static int 4270who_perm_compare(const void *larg, const void *rarg, void *unused) 4271{ 4272 const who_perm_node_t *l = larg; 4273 const who_perm_node_t *r = rarg; 4274 zfs_deleg_who_type_t ltype = l->who_perm.who_type; 4275 zfs_deleg_who_type_t rtype = r->who_perm.who_type; 4276 int lweight = who_type2weight(ltype); 4277 int rweight = who_type2weight(rtype); 4278 int res = lweight - rweight; 4279 if (res == 0) 4280 res = strncmp(l->who_perm.who_name, r->who_perm.who_name, 4281 ZFS_MAX_DELEG_NAME-1); 4282 4283 if (res == 0) 4284 return (0); 4285 if (res > 0) 4286 return (1); 4287 else 4288 return (-1); 4289} 4290 4291/* ARGSUSED */ 4292static int 4293deleg_perm_compare(const void *larg, const void *rarg, void *unused) 4294{ 4295 const deleg_perm_node_t *l = larg; 4296 const deleg_perm_node_t *r = rarg; 4297 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name, 4298 ZFS_MAX_DELEG_NAME-1); 4299 4300 if (res == 0) 4301 return (0); 4302 4303 if (res > 0) 4304 return (1); 4305 else 4306 return (-1); 4307} 4308 4309static inline void 4310fs_perm_set_init(fs_perm_set_t *fspset) 4311{ 4312 bzero(fspset, sizeof (fs_perm_set_t)); 4313 4314 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool", 4315 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node), 4316 NULL, UU_DEFAULT)) == NULL) 4317 nomem(); 4318 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL, 4319 UU_DEFAULT)) == NULL) 4320 nomem(); 4321 4322 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create( 4323 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof( 4324 who_perm_node_t, who_avl_node), who_perm_compare, 4325 UU_DEFAULT)) == NULL) 4326 nomem(); 4327 4328 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create( 4329 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof( 4330 who_perm_node_t, who_avl_node), who_perm_compare, 4331 UU_DEFAULT)) == NULL) 4332 nomem(); 4333 4334 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create( 4335 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof( 4336 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT)) 4337 == NULL) 4338 nomem(); 4339} 4340 4341static inline void fs_perm_fini(fs_perm_t *); 4342static inline void who_perm_fini(who_perm_t *); 4343 4344static inline void 4345fs_perm_set_fini(fs_perm_set_t *fspset) 4346{ 4347 fs_perm_node_t *node = uu_list_first(fspset->fsps_list); 4348 4349 while (node != NULL) { 4350 fs_perm_node_t *next_node = 4351 uu_list_next(fspset->fsps_list, node); 4352 fs_perm_t *fsperm = &node->fspn_fsperm; 4353 fs_perm_fini(fsperm); 4354 uu_list_remove(fspset->fsps_list, node); 4355 free(node); 4356 node = next_node; 4357 } 4358 4359 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool); 4360 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool); 4361 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool); 4362} 4363 4364static inline void 4365deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type, 4366 const char *name) 4367{ 4368 deleg_perm->dp_who_type = type; 4369 deleg_perm->dp_name = name; 4370} 4371 4372static inline void 4373who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm, 4374 zfs_deleg_who_type_t type, const char *name) 4375{ 4376 uu_avl_pool_t *pool; 4377 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool; 4378 4379 bzero(who_perm, sizeof (who_perm_t)); 4380 4381 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL, 4382 UU_DEFAULT)) == NULL) 4383 nomem(); 4384 4385 who_perm->who_type = type; 4386 who_perm->who_name = name; 4387 who_perm->who_fsperm = fsperm; 4388} 4389 4390static inline void 4391who_perm_fini(who_perm_t *who_perm) 4392{ 4393 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl); 4394 4395 while (node != NULL) { 4396 deleg_perm_node_t *next_node = 4397 uu_avl_next(who_perm->who_deleg_perm_avl, node); 4398 4399 uu_avl_remove(who_perm->who_deleg_perm_avl, node); 4400 free(node); 4401 node = next_node; 4402 } 4403 4404 uu_avl_destroy(who_perm->who_deleg_perm_avl); 4405} 4406 4407static inline void 4408fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname) 4409{ 4410 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool; 4411 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool; 4412 4413 bzero(fsperm, sizeof (fs_perm_t)); 4414 4415 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT)) 4416 == NULL) 4417 nomem(); 4418 4419 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT)) 4420 == NULL) 4421 nomem(); 4422 4423 fsperm->fsp_set = fspset; 4424 fsperm->fsp_name = fsname; 4425} 4426 4427static inline void 4428fs_perm_fini(fs_perm_t *fsperm) 4429{ 4430 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl); 4431 while (node != NULL) { 4432 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl, 4433 node); 4434 who_perm_t *who_perm = &node->who_perm; 4435 who_perm_fini(who_perm); 4436 uu_avl_remove(fsperm->fsp_sc_avl, node); 4437 free(node); 4438 node = next_node; 4439 } 4440 4441 node = uu_avl_first(fsperm->fsp_uge_avl); 4442 while (node != NULL) { 4443 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl, 4444 node); 4445 who_perm_t *who_perm = &node->who_perm; 4446 who_perm_fini(who_perm); 4447 uu_avl_remove(fsperm->fsp_uge_avl, node); 4448 free(node); 4449 node = next_node; 4450 } 4451 4452 uu_avl_destroy(fsperm->fsp_sc_avl); 4453 uu_avl_destroy(fsperm->fsp_uge_avl); 4454} 4455 4456static void inline 4457set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node, 4458 zfs_deleg_who_type_t who_type, const char *name, char locality) 4459{ 4460 uu_avl_index_t idx = 0; 4461 4462 deleg_perm_node_t *found_node = NULL; 4463 deleg_perm_t *deleg_perm = &node->dpn_perm; 4464 4465 deleg_perm_init(deleg_perm, who_type, name); 4466 4467 if ((found_node = uu_avl_find(avl, node, NULL, &idx)) 4468 == NULL) 4469 uu_avl_insert(avl, node, idx); 4470 else { 4471 node = found_node; 4472 deleg_perm = &node->dpn_perm; 4473 } 4474 4475 4476 switch (locality) { 4477 case ZFS_DELEG_LOCAL: 4478 deleg_perm->dp_local = B_TRUE; 4479 break; 4480 case ZFS_DELEG_DESCENDENT: 4481 deleg_perm->dp_descend = B_TRUE; 4482 break; 4483 case ZFS_DELEG_NA: 4484 break; 4485 default: 4486 assert(B_FALSE); /* invalid locality */ 4487 } 4488} 4489 4490static inline int 4491parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality) 4492{ 4493 nvpair_t *nvp = NULL; 4494 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set; 4495 uu_avl_t *avl = who_perm->who_deleg_perm_avl; 4496 zfs_deleg_who_type_t who_type = who_perm->who_type; 4497 4498 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4499 const char *name = nvpair_name(nvp); 4500 data_type_t type = nvpair_type(nvp); 4501 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool; 4502 deleg_perm_node_t *node = 4503 safe_malloc(sizeof (deleg_perm_node_t)); 4504 4505 assert(type == DATA_TYPE_BOOLEAN); 4506 4507 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool); 4508 set_deleg_perm_node(avl, node, who_type, name, locality); 4509 } 4510 4511 return (0); 4512} 4513 4514static inline int 4515parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl) 4516{ 4517 nvpair_t *nvp = NULL; 4518 fs_perm_set_t *fspset = fsperm->fsp_set; 4519 4520 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4521 nvlist_t *nvl2 = NULL; 4522 const char *name = nvpair_name(nvp); 4523 uu_avl_t *avl = NULL; 4524 uu_avl_pool_t *avl_pool; 4525 zfs_deleg_who_type_t perm_type = name[0]; 4526 char perm_locality = name[1]; 4527 const char *perm_name = name + 3; 4528 boolean_t is_set = B_TRUE; 4529 who_perm_t *who_perm = NULL; 4530 4531 assert('$' == name[2]); 4532 4533 if (nvpair_value_nvlist(nvp, &nvl2) != 0) 4534 return (-1); 4535 4536 switch (perm_type) { 4537 case ZFS_DELEG_CREATE: 4538 case ZFS_DELEG_CREATE_SETS: 4539 case ZFS_DELEG_NAMED_SET: 4540 case ZFS_DELEG_NAMED_SET_SETS: 4541 avl_pool = fspset->fsps_named_set_avl_pool; 4542 avl = fsperm->fsp_sc_avl; 4543 break; 4544 case ZFS_DELEG_USER: 4545 case ZFS_DELEG_USER_SETS: 4546 case ZFS_DELEG_GROUP: 4547 case ZFS_DELEG_GROUP_SETS: 4548 case ZFS_DELEG_EVERYONE: 4549 case ZFS_DELEG_EVERYONE_SETS: 4550 avl_pool = fspset->fsps_who_perm_avl_pool; 4551 avl = fsperm->fsp_uge_avl; 4552 break; 4553 } 4554 4555 if (is_set) { 4556 who_perm_node_t *found_node = NULL; 4557 who_perm_node_t *node = safe_malloc( 4558 sizeof (who_perm_node_t)); 4559 who_perm = &node->who_perm; 4560 uu_avl_index_t idx = 0; 4561 4562 uu_avl_node_init(node, &node->who_avl_node, avl_pool); 4563 who_perm_init(who_perm, fsperm, perm_type, perm_name); 4564 4565 if ((found_node = uu_avl_find(avl, node, NULL, &idx)) 4566 == NULL) { 4567 if (avl == fsperm->fsp_uge_avl) { 4568 uid_t rid = 0; 4569 struct passwd *p = NULL; 4570 struct group *g = NULL; 4571 const char *nice_name = NULL; 4572 4573 switch (perm_type) { 4574 case ZFS_DELEG_USER_SETS: 4575 case ZFS_DELEG_USER: 4576 rid = atoi(perm_name); 4577 p = getpwuid(rid); 4578 if (p) 4579 nice_name = p->pw_name; 4580 break; 4581 case ZFS_DELEG_GROUP_SETS: 4582 case ZFS_DELEG_GROUP: 4583 rid = atoi(perm_name); 4584 g = getgrgid(rid); 4585 if (g) 4586 nice_name = g->gr_name; 4587 break; 4588 } 4589 4590 if (nice_name != NULL) 4591 (void) strlcpy( 4592 node->who_perm.who_ug_name, 4593 nice_name, 256); 4594 } 4595 4596 uu_avl_insert(avl, node, idx); 4597 } else { 4598 node = found_node; 4599 who_perm = &node->who_perm; 4600 } 4601 } 4602 4603 (void) parse_who_perm(who_perm, nvl2, perm_locality); 4604 } 4605 4606 return (0); 4607} 4608 4609static inline int 4610parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl) 4611{ 4612 nvpair_t *nvp = NULL; 4613 uu_avl_index_t idx = 0; 4614 4615 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) { 4616 nvlist_t *nvl2 = NULL; 4617 const char *fsname = nvpair_name(nvp); 4618 data_type_t type = nvpair_type(nvp); 4619 fs_perm_t *fsperm = NULL; 4620 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t)); 4621 if (node == NULL) 4622 nomem(); 4623 4624 fsperm = &node->fspn_fsperm; 4625 4626 assert(DATA_TYPE_NVLIST == type); 4627 4628 uu_list_node_init(node, &node->fspn_list_node, 4629 fspset->fsps_list_pool); 4630 4631 idx = uu_list_numnodes(fspset->fsps_list); 4632 fs_perm_init(fsperm, fspset, fsname); 4633 4634 if (nvpair_value_nvlist(nvp, &nvl2) != 0) 4635 return (-1); 4636 4637 (void) parse_fs_perm(fsperm, nvl2); 4638 4639 uu_list_insert(fspset->fsps_list, node, idx); 4640 } 4641 4642 return (0); 4643} 4644 4645static inline const char * 4646deleg_perm_comment(zfs_deleg_note_t note) 4647{ 4648 const char *str = ""; 4649 4650 /* subcommands */ 4651 switch (note) { 4652 /* SUBCOMMANDS */ 4653 case ZFS_DELEG_NOTE_ALLOW: 4654 str = gettext("Must also have the permission that is being" 4655 "\n\t\t\t\tallowed"); 4656 break; 4657 case ZFS_DELEG_NOTE_CLONE: 4658 str = gettext("Must also have the 'create' ability and 'mount'" 4659 "\n\t\t\t\tability in the origin file system"); 4660 break; 4661 case ZFS_DELEG_NOTE_CREATE: 4662 str = gettext("Must also have the 'mount' ability"); 4663 break; 4664 case ZFS_DELEG_NOTE_DESTROY: 4665 str = gettext("Must also have the 'mount' ability"); 4666 break; 4667 case ZFS_DELEG_NOTE_DIFF: 4668 str = gettext("Allows lookup of paths within a dataset;" 4669 "\n\t\t\t\tgiven an object number. Ordinary users need this" 4670 "\n\t\t\t\tin order to use zfs diff"); 4671 break; 4672 case ZFS_DELEG_NOTE_HOLD: 4673 str = gettext("Allows adding a user hold to a snapshot"); 4674 break; 4675 case ZFS_DELEG_NOTE_MOUNT: 4676 str = gettext("Allows mount/umount of ZFS datasets"); 4677 break; 4678 case ZFS_DELEG_NOTE_PROMOTE: 4679 str = gettext("Must also have the 'mount'\n\t\t\t\tand" 4680 " 'promote' ability in the origin file system"); 4681 break; 4682 case ZFS_DELEG_NOTE_RECEIVE: 4683 str = gettext("Must also have the 'mount' and 'create'" 4684 " ability"); 4685 break; 4686 case ZFS_DELEG_NOTE_RELEASE: 4687 str = gettext("Allows releasing a user hold which\n\t\t\t\t" 4688 "might destroy the snapshot"); 4689 break; 4690 case ZFS_DELEG_NOTE_RENAME: 4691 str = gettext("Must also have the 'mount' and 'create'" 4692 "\n\t\t\t\tability in the new parent"); 4693 break; 4694 case ZFS_DELEG_NOTE_ROLLBACK: 4695 str = gettext(""); 4696 break; 4697 case ZFS_DELEG_NOTE_SEND: 4698 str = gettext(""); 4699 break; 4700 case ZFS_DELEG_NOTE_SHARE: 4701 str = gettext("Allows sharing file systems over NFS or SMB" 4702 "\n\t\t\t\tprotocols"); 4703 break; 4704 case ZFS_DELEG_NOTE_SNAPSHOT: 4705 str = gettext(""); 4706 break; 4707/* 4708 * case ZFS_DELEG_NOTE_VSCAN: 4709 * str = gettext(""); 4710 * break; 4711 */ 4712 /* OTHER */ 4713 case ZFS_DELEG_NOTE_GROUPQUOTA: 4714 str = gettext("Allows accessing any groupquota@... property"); 4715 break; 4716 case ZFS_DELEG_NOTE_GROUPUSED: 4717 str = gettext("Allows reading any groupused@... property"); 4718 break; 4719 case ZFS_DELEG_NOTE_USERPROP: 4720 str = gettext("Allows changing any user property"); 4721 break; 4722 case ZFS_DELEG_NOTE_USERQUOTA: 4723 str = gettext("Allows accessing any userquota@... property"); 4724 break; 4725 case ZFS_DELEG_NOTE_USERUSED: 4726 str = gettext("Allows reading any userused@... property"); 4727 break; 4728 /* other */ 4729 default: 4730 str = ""; 4731 } 4732 4733 return (str); 4734} 4735 4736struct allow_opts { 4737 boolean_t local; 4738 boolean_t descend; 4739 boolean_t user; 4740 boolean_t group; 4741 boolean_t everyone; 4742 boolean_t create; 4743 boolean_t set; 4744 boolean_t recursive; /* unallow only */ 4745 boolean_t prt_usage; 4746 4747 boolean_t prt_perms; 4748 char *who; 4749 char *perms; 4750 const char *dataset; 4751}; 4752 4753static inline int 4754prop_cmp(const void *a, const void *b) 4755{ 4756 const char *str1 = *(const char **)a; 4757 const char *str2 = *(const char **)b; 4758 return (strcmp(str1, str2)); 4759} 4760 4761static void 4762allow_usage(boolean_t un, boolean_t requested, const char *msg) 4763{ 4764 const char *opt_desc[] = { 4765 "-h", gettext("show this help message and exit"), 4766 "-l", gettext("set permission locally"), 4767 "-d", gettext("set permission for descents"), 4768 "-u", gettext("set permission for user"), 4769 "-g", gettext("set permission for group"), 4770 "-e", gettext("set permission for everyone"), 4771 "-c", gettext("set create time permission"), 4772 "-s", gettext("define permission set"), 4773 /* unallow only */ 4774 "-r", gettext("remove permissions recursively"), 4775 }; 4776 size_t unallow_size = sizeof (opt_desc) / sizeof (char *); 4777 size_t allow_size = unallow_size - 2; 4778 const char *props[ZFS_NUM_PROPS]; 4779 int i; 4780 size_t count = 0; 4781 FILE *fp = requested ? stdout : stderr; 4782 zprop_desc_t *pdtbl = zfs_prop_get_table(); 4783 const char *fmt = gettext("%-16s %-14s\t%s\n"); 4784 4785 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW : 4786 HELP_ALLOW)); 4787 (void) fprintf(fp, gettext("Options:\n")); 4788 for (i = 0; i < (un ? unallow_size : allow_size); i++) { 4789 const char *opt = opt_desc[i++]; 4790 const char *optdsc = opt_desc[i]; 4791 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc); 4792 } 4793 4794 (void) fprintf(fp, gettext("\nThe following permissions are " 4795 "supported:\n\n")); 4796 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"), 4797 gettext("NOTES")); 4798 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) { 4799 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm; 4800 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note; 4801 const char *perm_type = deleg_perm_type(perm_note); 4802 const char *perm_comment = deleg_perm_comment(perm_note); 4803 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment); 4804 } 4805 4806 for (i = 0; i < ZFS_NUM_PROPS; i++) { 4807 zprop_desc_t *pd = &pdtbl[i]; 4808 if (pd->pd_visible != B_TRUE) 4809 continue; 4810 4811 if (pd->pd_attr == PROP_READONLY) 4812 continue; 4813 4814 props[count++] = pd->pd_name; 4815 } 4816 props[count] = NULL; 4817 4818 qsort(props, count, sizeof (char *), prop_cmp); 4819 4820 for (i = 0; i < count; i++) 4821 (void) fprintf(fp, fmt, props[i], gettext("property"), ""); 4822 4823 if (msg != NULL) 4824 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg); 4825 4826 exit(requested ? 0 : 2); 4827} 4828 4829static inline const char * 4830munge_args(int argc, char **argv, boolean_t un, size_t expected_argc, 4831 char **permsp) 4832{ 4833 if (un && argc == expected_argc - 1) 4834 *permsp = NULL; 4835 else if (argc == expected_argc) 4836 *permsp = argv[argc - 2]; 4837 else 4838 allow_usage(un, B_FALSE, 4839 gettext("wrong number of parameters\n")); 4840 4841 return (argv[argc - 1]); 4842} 4843 4844static void 4845parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts) 4846{ 4847 int uge_sum = opts->user + opts->group + opts->everyone; 4848 int csuge_sum = opts->create + opts->set + uge_sum; 4849 int ldcsuge_sum = csuge_sum + opts->local + opts->descend; 4850 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum; 4851 4852 if (uge_sum > 1) 4853 allow_usage(un, B_FALSE, 4854 gettext("-u, -g, and -e are mutually exclusive\n")); 4855 4856 if (opts->prt_usage) 4857 if (argc == 0 && all_sum == 0) 4858 allow_usage(un, B_TRUE, NULL); 4859 else 4860 usage(B_FALSE); 4861 4862 if (opts->set) { 4863 if (csuge_sum > 1) 4864 allow_usage(un, B_FALSE, 4865 gettext("invalid options combined with -s\n")); 4866 4867 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms); 4868 if (argv[0][0] != '@') 4869 allow_usage(un, B_FALSE, 4870 gettext("invalid set name: missing '@' prefix\n")); 4871 opts->who = argv[0]; 4872 } else if (opts->create) { 4873 if (ldcsuge_sum > 1) 4874 allow_usage(un, B_FALSE, 4875 gettext("invalid options combined with -c\n")); 4876 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4877 } else if (opts->everyone) { 4878 if (csuge_sum > 1) 4879 allow_usage(un, B_FALSE, 4880 gettext("invalid options combined with -e\n")); 4881 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4882 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone") 4883 == 0) { 4884 opts->everyone = B_TRUE; 4885 argc--; 4886 argv++; 4887 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms); 4888 } else if (argc == 1 && !un) { 4889 opts->prt_perms = B_TRUE; 4890 opts->dataset = argv[argc-1]; 4891 } else { 4892 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms); 4893 opts->who = argv[0]; 4894 } 4895 4896 if (!opts->local && !opts->descend) { 4897 opts->local = B_TRUE; 4898 opts->descend = B_TRUE; 4899 } 4900} 4901 4902static void 4903store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend, 4904 const char *who, char *perms, nvlist_t *top_nvl) 4905{ 4906 int i; 4907 char ld[2] = { '\0', '\0' }; 4908 char who_buf[ZFS_MAXNAMELEN+32]; 4909 char base_type; 4910 char set_type; 4911 nvlist_t *base_nvl = NULL; 4912 nvlist_t *set_nvl = NULL; 4913 nvlist_t *nvl; 4914 4915 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0) 4916 nomem(); 4917 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0) 4918 nomem(); 4919 4920 switch (type) { 4921 case ZFS_DELEG_NAMED_SET_SETS: 4922 case ZFS_DELEG_NAMED_SET: 4923 set_type = ZFS_DELEG_NAMED_SET_SETS; 4924 base_type = ZFS_DELEG_NAMED_SET; 4925 ld[0] = ZFS_DELEG_NA; 4926 break; 4927 case ZFS_DELEG_CREATE_SETS: 4928 case ZFS_DELEG_CREATE: 4929 set_type = ZFS_DELEG_CREATE_SETS; 4930 base_type = ZFS_DELEG_CREATE; 4931 ld[0] = ZFS_DELEG_NA; 4932 break; 4933 case ZFS_DELEG_USER_SETS: 4934 case ZFS_DELEG_USER: 4935 set_type = ZFS_DELEG_USER_SETS; 4936 base_type = ZFS_DELEG_USER; 4937 if (local) 4938 ld[0] = ZFS_DELEG_LOCAL; 4939 if (descend) 4940 ld[1] = ZFS_DELEG_DESCENDENT; 4941 break; 4942 case ZFS_DELEG_GROUP_SETS: 4943 case ZFS_DELEG_GROUP: 4944 set_type = ZFS_DELEG_GROUP_SETS; 4945 base_type = ZFS_DELEG_GROUP; 4946 if (local) 4947 ld[0] = ZFS_DELEG_LOCAL; 4948 if (descend) 4949 ld[1] = ZFS_DELEG_DESCENDENT; 4950 break; 4951 case ZFS_DELEG_EVERYONE_SETS: 4952 case ZFS_DELEG_EVERYONE: 4953 set_type = ZFS_DELEG_EVERYONE_SETS; 4954 base_type = ZFS_DELEG_EVERYONE; 4955 if (local) 4956 ld[0] = ZFS_DELEG_LOCAL; 4957 if (descend) 4958 ld[1] = ZFS_DELEG_DESCENDENT; 4959 } 4960 4961 if (perms != NULL) { 4962 char *curr = perms; 4963 char *end = curr + strlen(perms); 4964 4965 while (curr < end) { 4966 char *delim = strchr(curr, ','); 4967 if (delim == NULL) 4968 delim = end; 4969 else 4970 *delim = '\0'; 4971 4972 if (curr[0] == '@') 4973 nvl = set_nvl; 4974 else 4975 nvl = base_nvl; 4976 4977 (void) nvlist_add_boolean(nvl, curr); 4978 if (delim != end) 4979 *delim = ','; 4980 curr = delim + 1; 4981 } 4982 4983 for (i = 0; i < 2; i++) { 4984 char locality = ld[i]; 4985 if (locality == 0) 4986 continue; 4987 4988 if (!nvlist_empty(base_nvl)) { 4989 if (who != NULL) 4990 (void) snprintf(who_buf, 4991 sizeof (who_buf), "%c%c$%s", 4992 base_type, locality, who); 4993 else 4994 (void) snprintf(who_buf, 4995 sizeof (who_buf), "%c%c$", 4996 base_type, locality); 4997 4998 (void) nvlist_add_nvlist(top_nvl, who_buf, 4999 base_nvl); 5000 } 5001 5002 5003 if (!nvlist_empty(set_nvl)) { 5004 if (who != NULL) 5005 (void) snprintf(who_buf, 5006 sizeof (who_buf), "%c%c$%s", 5007 set_type, locality, who); 5008 else 5009 (void) snprintf(who_buf, 5010 sizeof (who_buf), "%c%c$", 5011 set_type, locality); 5012 5013 (void) nvlist_add_nvlist(top_nvl, who_buf, 5014 set_nvl); 5015 } 5016 } 5017 } else { 5018 for (i = 0; i < 2; i++) { 5019 char locality = ld[i]; 5020 if (locality == 0) 5021 continue; 5022 5023 if (who != NULL) 5024 (void) snprintf(who_buf, sizeof (who_buf), 5025 "%c%c$%s", base_type, locality, who); 5026 else 5027 (void) snprintf(who_buf, sizeof (who_buf), 5028 "%c%c$", base_type, locality); 5029 (void) nvlist_add_boolean(top_nvl, who_buf); 5030 5031 if (who != NULL) 5032 (void) snprintf(who_buf, sizeof (who_buf), 5033 "%c%c$%s", set_type, locality, who); 5034 else 5035 (void) snprintf(who_buf, sizeof (who_buf), 5036 "%c%c$", set_type, locality); 5037 (void) nvlist_add_boolean(top_nvl, who_buf); 5038 } 5039 } 5040} 5041 5042static int 5043construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp) 5044{ 5045 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0) 5046 nomem(); 5047 5048 if (opts->set) { 5049 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local, 5050 opts->descend, opts->who, opts->perms, *nvlp); 5051 } else if (opts->create) { 5052 store_allow_perm(ZFS_DELEG_CREATE, opts->local, 5053 opts->descend, NULL, opts->perms, *nvlp); 5054 } else if (opts->everyone) { 5055 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local, 5056 opts->descend, NULL, opts->perms, *nvlp); 5057 } else { 5058 char *curr = opts->who; 5059 char *end = curr + strlen(curr); 5060 5061 while (curr < end) { 5062 const char *who; 5063 zfs_deleg_who_type_t who_type; 5064 char *endch; 5065 char *delim = strchr(curr, ','); 5066 char errbuf[256]; 5067 char id[64]; 5068 struct passwd *p = NULL; 5069 struct group *g = NULL; 5070 5071 uid_t rid; 5072 if (delim == NULL) 5073 delim = end; 5074 else 5075 *delim = '\0'; 5076 5077 rid = (uid_t)strtol(curr, &endch, 0); 5078 if (opts->user) { 5079 who_type = ZFS_DELEG_USER; 5080 if (*endch != '\0') 5081 p = getpwnam(curr); 5082 else 5083 p = getpwuid(rid); 5084 5085 if (p != NULL) 5086 rid = p->pw_uid; 5087 else { 5088 (void) snprintf(errbuf, 256, gettext( 5089 "invalid user %s"), curr); 5090 allow_usage(un, B_TRUE, errbuf); 5091 } 5092 } else if (opts->group) { 5093 who_type = ZFS_DELEG_GROUP; 5094 if (*endch != '\0') 5095 g = getgrnam(curr); 5096 else 5097 g = getgrgid(rid); 5098 5099 if (g != NULL) 5100 rid = g->gr_gid; 5101 else { 5102 (void) snprintf(errbuf, 256, gettext( 5103 "invalid group %s"), curr); 5104 allow_usage(un, B_TRUE, errbuf); 5105 } 5106 } else { 5107 if (*endch != '\0') { 5108 p = getpwnam(curr); 5109 } else { 5110 p = getpwuid(rid); 5111 } 5112 5113 if (p == NULL) 5114 if (*endch != '\0') { 5115 g = getgrnam(curr); 5116 } else { 5117 g = getgrgid(rid); 5118 } 5119 5120 if (p != NULL) { 5121 who_type = ZFS_DELEG_USER; 5122 rid = p->pw_uid; 5123 } else if (g != NULL) { 5124 who_type = ZFS_DELEG_GROUP; 5125 rid = g->gr_gid; 5126 } else { 5127 (void) snprintf(errbuf, 256, gettext( 5128 "invalid user/group %s"), curr); 5129 allow_usage(un, B_TRUE, errbuf); 5130 } 5131 } 5132 5133 (void) sprintf(id, "%u", rid); 5134 who = id; 5135 5136 store_allow_perm(who_type, opts->local, 5137 opts->descend, who, opts->perms, *nvlp); 5138 curr = delim + 1; 5139 } 5140 } 5141 5142 return (0); 5143} 5144 5145static void 5146print_set_creat_perms(uu_avl_t *who_avl) 5147{ 5148 const char *sc_title[] = { 5149 gettext("Permission sets:\n"), 5150 gettext("Create time permissions:\n"), 5151 NULL 5152 }; 5153 const char **title_ptr = sc_title; 5154 who_perm_node_t *who_node = NULL; 5155 int prev_weight = -1; 5156 5157 for (who_node = uu_avl_first(who_avl); who_node != NULL; 5158 who_node = uu_avl_next(who_avl, who_node)) { 5159 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl; 5160 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type; 5161 const char *who_name = who_node->who_perm.who_name; 5162 int weight = who_type2weight(who_type); 5163 boolean_t first = B_TRUE; 5164 deleg_perm_node_t *deleg_node; 5165 5166 if (prev_weight != weight) { 5167 (void) printf(*title_ptr++); 5168 prev_weight = weight; 5169 } 5170 5171 if (who_name == NULL || strnlen(who_name, 1) == 0) 5172 (void) printf("\t"); 5173 else 5174 (void) printf("\t%s ", who_name); 5175 5176 for (deleg_node = uu_avl_first(avl); deleg_node != NULL; 5177 deleg_node = uu_avl_next(avl, deleg_node)) { 5178 if (first) { 5179 (void) printf("%s", 5180 deleg_node->dpn_perm.dp_name); 5181 first = B_FALSE; 5182 } else 5183 (void) printf(",%s", 5184 deleg_node->dpn_perm.dp_name); 5185 } 5186 5187 (void) printf("\n"); 5188 } 5189} 5190 5191static void inline 5192print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend, 5193 const char *title) 5194{ 5195 who_perm_node_t *who_node = NULL; 5196 boolean_t prt_title = B_TRUE; 5197 uu_avl_walk_t *walk; 5198 5199 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL) 5200 nomem(); 5201 5202 while ((who_node = uu_avl_walk_next(walk)) != NULL) { 5203 const char *who_name = who_node->who_perm.who_name; 5204 const char *nice_who_name = who_node->who_perm.who_ug_name; 5205 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl; 5206 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type; 5207 char delim = ' '; 5208 deleg_perm_node_t *deleg_node; 5209 boolean_t prt_who = B_TRUE; 5210 5211 for (deleg_node = uu_avl_first(avl); 5212 deleg_node != NULL; 5213 deleg_node = uu_avl_next(avl, deleg_node)) { 5214 if (local != deleg_node->dpn_perm.dp_local || 5215 descend != deleg_node->dpn_perm.dp_descend) 5216 continue; 5217 5218 if (prt_who) { 5219 const char *who = NULL; 5220 if (prt_title) { 5221 prt_title = B_FALSE; 5222 (void) printf(title); 5223 } 5224 5225 switch (who_type) { 5226 case ZFS_DELEG_USER_SETS: 5227 case ZFS_DELEG_USER: 5228 who = gettext("user"); 5229 if (nice_who_name) 5230 who_name = nice_who_name; 5231 break; 5232 case ZFS_DELEG_GROUP_SETS: 5233 case ZFS_DELEG_GROUP: 5234 who = gettext("group"); 5235 if (nice_who_name) 5236 who_name = nice_who_name; 5237 break; 5238 case ZFS_DELEG_EVERYONE_SETS: 5239 case ZFS_DELEG_EVERYONE: 5240 who = gettext("everyone"); 5241 who_name = NULL; 5242 } 5243 5244 prt_who = B_FALSE; 5245 if (who_name == NULL) 5246 (void) printf("\t%s", who); 5247 else 5248 (void) printf("\t%s %s", who, who_name); 5249 } 5250 5251 (void) printf("%c%s", delim, 5252 deleg_node->dpn_perm.dp_name); 5253 delim = ','; 5254 } 5255 5256 if (!prt_who) 5257 (void) printf("\n"); 5258 } 5259 5260 uu_avl_walk_end(walk); 5261} 5262 5263static void 5264print_fs_perms(fs_perm_set_t *fspset) 5265{ 5266 fs_perm_node_t *node = NULL; 5267 char buf[ZFS_MAXNAMELEN+32]; 5268 const char *dsname = buf; 5269 5270 for (node = uu_list_first(fspset->fsps_list); node != NULL; 5271 node = uu_list_next(fspset->fsps_list, node)) { 5272 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl; 5273 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl; 5274 int left = 0; 5275 5276 (void) snprintf(buf, ZFS_MAXNAMELEN+32, 5277 gettext("---- Permissions on %s "), 5278 node->fspn_fsperm.fsp_name); 5279 (void) printf(dsname); 5280 left = 70 - strlen(buf); 5281 while (left-- > 0) 5282 (void) printf("-"); 5283 (void) printf("\n"); 5284 5285 print_set_creat_perms(sc_avl); 5286 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE, 5287 gettext("Local permissions:\n")); 5288 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE, 5289 gettext("Descendent permissions:\n")); 5290 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE, 5291 gettext("Local+Descendent permissions:\n")); 5292 } 5293} 5294 5295static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL }; 5296 5297struct deleg_perms { 5298 boolean_t un; 5299 nvlist_t *nvl; 5300}; 5301 5302static int 5303set_deleg_perms(zfs_handle_t *zhp, void *data) 5304{ 5305 struct deleg_perms *perms = (struct deleg_perms *)data; 5306 zfs_type_t zfs_type = zfs_get_type(zhp); 5307 5308 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME) 5309 return (0); 5310 5311 return (zfs_set_fsacl(zhp, perms->un, perms->nvl)); 5312} 5313 5314static int 5315zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un) 5316{ 5317 zfs_handle_t *zhp; 5318 nvlist_t *perm_nvl = NULL; 5319 nvlist_t *update_perm_nvl = NULL; 5320 int error = 1; 5321 int c; 5322 struct allow_opts opts = { 0 }; 5323 5324 const char *optstr = un ? "ldugecsrh" : "ldugecsh"; 5325 5326 /* check opts */ 5327 while ((c = getopt(argc, argv, optstr)) != -1) { 5328 switch (c) { 5329 case 'l': 5330 opts.local = B_TRUE; 5331 break; 5332 case 'd': 5333 opts.descend = B_TRUE; 5334 break; 5335 case 'u': 5336 opts.user = B_TRUE; 5337 break; 5338 case 'g': 5339 opts.group = B_TRUE; 5340 break; 5341 case 'e': 5342 opts.everyone = B_TRUE; 5343 break; 5344 case 's': 5345 opts.set = B_TRUE; 5346 break; 5347 case 'c': 5348 opts.create = B_TRUE; 5349 break; 5350 case 'r': 5351 opts.recursive = B_TRUE; 5352 break; 5353 case ':': 5354 (void) fprintf(stderr, gettext("missing argument for " 5355 "'%c' option\n"), optopt); 5356 usage(B_FALSE); 5357 break; 5358 case 'h': 5359 opts.prt_usage = B_TRUE; 5360 break; 5361 case '?': 5362 (void) fprintf(stderr, gettext("invalid option '%c'\n"), 5363 optopt); 5364 usage(B_FALSE); 5365 } 5366 } 5367 5368 argc -= optind; 5369 argv += optind; 5370 5371 /* check arguments */ 5372 parse_allow_args(argc, argv, un, &opts); 5373 5374 /* try to open the dataset */ 5375 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM | 5376 ZFS_TYPE_VOLUME)) == NULL) { 5377 (void) fprintf(stderr, "Failed to open dataset: %s\n", 5378 opts.dataset); 5379 return (-1); 5380 } 5381 5382 if (zfs_get_fsacl(zhp, &perm_nvl) != 0) 5383 goto cleanup2; 5384 5385 fs_perm_set_init(&fs_perm_set); 5386 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) { 5387 (void) fprintf(stderr, "Failed to parse fsacl permissions\n"); 5388 goto cleanup1; 5389 } 5390 5391 if (opts.prt_perms) 5392 print_fs_perms(&fs_perm_set); 5393 else { 5394 (void) construct_fsacl_list(un, &opts, &update_perm_nvl); 5395 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0) 5396 goto cleanup0; 5397 5398 if (un && opts.recursive) { 5399 struct deleg_perms data = { un, update_perm_nvl }; 5400 if (zfs_iter_filesystems(zhp, set_deleg_perms, 5401 &data) != 0) 5402 goto cleanup0; 5403 } 5404 } 5405 5406 error = 0; 5407 5408cleanup0: 5409 nvlist_free(perm_nvl); 5410 if (update_perm_nvl != NULL) 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