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