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