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.
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.
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]
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) 2011 by Delphix. All rights reserved.
34 #include <libnvpair.h>
47 #include <sys/mkdev.h>
48 #include <sys/mntent.h>
49 #include <sys/mnttab.h>
50 #include <sys/mount.h>
52 #include <sys/fs/zfs.h>
53 #include <sys/types.h>
58 #include <zfs_deleg.h>
62 #include <directory.h>
63 #endif /* HAVE_IDMAP */
67 #include "zfs_comutil.h"
69 libzfs_handle_t *g_zfs;
71 static FILE *mnttab_file;
72 static char history_str[HIS_MAX_RECORD_LEN];
74 static int zfs_do_clone(int argc, char **argv);
75 static int zfs_do_create(int argc, char **argv);
76 static int zfs_do_destroy(int argc, char **argv);
77 static int zfs_do_get(int argc, char **argv);
78 static int zfs_do_inherit(int argc, char **argv);
79 static int zfs_do_list(int argc, char **argv);
80 static int zfs_do_mount(int argc, char **argv);
81 static int zfs_do_rename(int argc, char **argv);
82 static int zfs_do_rollback(int argc, char **argv);
83 static int zfs_do_set(int argc, char **argv);
84 static int zfs_do_upgrade(int argc, char **argv);
85 static int zfs_do_snapshot(int argc, char **argv);
86 static int zfs_do_unmount(int argc, char **argv);
87 static int zfs_do_share(int argc, char **argv);
88 static int zfs_do_unshare(int argc, char **argv);
89 static int zfs_do_send(int argc, char **argv);
90 static int zfs_do_receive(int argc, char **argv);
91 static int zfs_do_promote(int argc, char **argv);
92 static int zfs_do_userspace(int argc, char **argv);
93 static int zfs_do_allow(int argc, char **argv);
94 static int zfs_do_unallow(int argc, char **argv);
95 static int zfs_do_hold(int argc, char **argv);
96 static int zfs_do_holds(int argc, char **argv);
97 static int zfs_do_release(int argc, char **argv);
98 static int zfs_do_diff(int argc, char **argv);
101 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
106 _umem_debug_init(void)
108 return ("default,verbose"); /* $UMEM_DEBUG setting */
112 _umem_logging_init(void)
114 return ("fail,contents"); /* $UMEM_LOGGING setting */
147 typedef struct zfs_command {
149 int (*func)(int argc, char **argv);
154 * Master command table. Each ZFS command has a name, associated function, and
155 * usage message. The usage messages need to be internationalized, so we have
156 * to have a function to return the usage message based on a command index.
158 * These commands are organized according to how they are displayed in the usage
159 * message. An empty command (one with a NULL name) indicates an empty line in
160 * the generic usage message.
162 static zfs_command_t command_table[] = {
163 { "create", zfs_do_create, HELP_CREATE },
164 { "destroy", zfs_do_destroy, HELP_DESTROY },
166 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
167 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
168 { "clone", zfs_do_clone, HELP_CLONE },
169 { "promote", zfs_do_promote, HELP_PROMOTE },
170 { "rename", zfs_do_rename, HELP_RENAME },
172 { "list", zfs_do_list, HELP_LIST },
174 { "set", zfs_do_set, HELP_SET },
175 { "get", zfs_do_get, HELP_GET },
176 { "inherit", zfs_do_inherit, HELP_INHERIT },
177 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
178 { "userspace", zfs_do_userspace, HELP_USERSPACE },
179 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
181 { "mount", zfs_do_mount, HELP_MOUNT },
182 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
183 { "share", zfs_do_share, HELP_SHARE },
184 { "unshare", zfs_do_unshare, HELP_UNSHARE },
186 { "send", zfs_do_send, HELP_SEND },
187 { "receive", zfs_do_receive, HELP_RECEIVE },
189 { "allow", zfs_do_allow, HELP_ALLOW },
191 { "unallow", zfs_do_unallow, HELP_UNALLOW },
193 { "hold", zfs_do_hold, HELP_HOLD },
194 { "holds", zfs_do_holds, HELP_HOLDS },
195 { "release", zfs_do_release, HELP_RELEASE },
196 { "diff", zfs_do_diff, HELP_DIFF },
199 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
201 zfs_command_t *current_command;
204 get_usage(zfs_help_t idx)
208 return (gettext("\tclone [-p] [-o property=value] ... "
209 "<snapshot> <filesystem|volume>\n"));
211 return (gettext("\tcreate [-p] [-o property=value] ... "
213 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
214 "-V <size> <volume>\n"));
216 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
217 "\tdestroy [-dnpRrv] "
218 "<filesystem|volume>@<snap>[%<snap>][,...]\n"));
220 return (gettext("\tget [-rHp] [-d max] "
221 "[-o \"all\" | field[,...]] [-s source[,...]]\n"
222 "\t <\"all\" | property[,...]> "
223 "[filesystem|volume|snapshot] ...\n"));
225 return (gettext("\tinherit [-rS] <property> "
226 "<filesystem|volume|snapshot> ...\n"));
228 return (gettext("\tupgrade [-v]\n"
229 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
231 return (gettext("\tlist [-rH][-d max] "
232 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
233 "\t [-S property] ... "
234 "[filesystem|volume|snapshot|snap] ...\n"));
236 return (gettext("\tmount\n"
237 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
239 return (gettext("\tpromote <clone-filesystem>\n"));
241 return (gettext("\treceive [-vnFu] <filesystem|volume|"
243 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
245 return (gettext("\trename <filesystem|volume|snapshot> "
246 "<filesystem|volume|snapshot>\n"
247 "\trename -p <filesystem|volume> <filesystem|volume>\n"
248 "\trename -r <snapshot> <snapshot>"));
250 return (gettext("\trollback [-rRf] <snapshot>\n"));
252 return (gettext("\tsend [-DnPpRrv] [-[iI] snapshot] "
255 return (gettext("\tset <property=value> "
256 "<filesystem|volume|snapshot> ...\n"));
258 return (gettext("\tshare <-a | filesystem>\n"));
260 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
261 "<filesystem@snapname|volume@snapname>\n"));
263 return (gettext("\tunmount [-f] "
264 "<-a | filesystem|mountpoint>\n"));
266 return (gettext("\tunshare "
267 "<-a | filesystem|mountpoint>\n"));
269 return (gettext("\tallow <filesystem|volume>\n"
271 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
272 "\t <filesystem|volume>\n"
273 "\tallow [-ld] -e <perm|@setname>[,...] "
274 "<filesystem|volume>\n"
275 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
276 "\tallow -s @setname <perm|@setname>[,...] "
277 "<filesystem|volume>\n"));
279 return (gettext("\tunallow [-rldug] "
280 "<\"everyone\"|user|group>[,...]\n"
281 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
282 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
283 "<filesystem|volume>\n"
284 "\tunallow [-r] -c [<perm|@setname>[,...]] "
285 "<filesystem|volume>\n"
286 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
287 "<filesystem|volume>\n"));
289 return (gettext("\tuserspace [-hniHp] [-o field[,...]] "
290 "[-sS field] ... [-t type[,...]]\n"
291 "\t <filesystem|snapshot>\n"));
292 case HELP_GROUPSPACE:
293 return (gettext("\tgroupspace [-hniHpU] [-o field[,...]] "
294 "[-sS field] ... [-t type[,...]]\n"
295 "\t <filesystem|snapshot>\n"));
297 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
299 return (gettext("\tholds [-r] <snapshot> ...\n"));
301 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
303 return (gettext("\tdiff [-FHt] <snapshot> "
304 "[snapshot|filesystem]\n"));
314 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
319 * Utility function to guarantee malloc() success.
323 safe_malloc(size_t size)
327 if ((data = calloc(1, size)) == NULL)
334 safe_strdup(char *str)
336 char *dupstr = strdup(str);
345 * Callback routine that will print out information for each of
349 usage_prop_cb(int prop, void *cb)
353 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
355 if (zfs_prop_readonly(prop))
356 (void) fprintf(fp, " NO ");
358 (void) fprintf(fp, "YES ");
360 if (zfs_prop_inheritable(prop))
361 (void) fprintf(fp, " YES ");
363 (void) fprintf(fp, " NO ");
365 if (zfs_prop_values(prop) == NULL)
366 (void) fprintf(fp, "-\n");
368 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
374 * Display usage message. If we're inside a command, display only the usage for
375 * that command. Otherwise, iterate over the entire command table and display
376 * a complete usage message.
379 usage(boolean_t requested)
382 boolean_t show_properties = B_FALSE;
383 FILE *fp = requested ? stdout : stderr;
385 if (current_command == NULL) {
387 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
389 gettext("where 'command' is one of the following:\n\n"));
391 for (i = 0; i < NCOMMAND; i++) {
392 if (command_table[i].name == NULL)
393 (void) fprintf(fp, "\n");
395 (void) fprintf(fp, "%s",
396 get_usage(command_table[i].usage));
399 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
400 "pool/[dataset/]*dataset[@name]\n"));
402 (void) fprintf(fp, gettext("usage:\n"));
403 (void) fprintf(fp, "%s", get_usage(current_command->usage));
406 if (current_command != NULL &&
407 (strcmp(current_command->name, "set") == 0 ||
408 strcmp(current_command->name, "get") == 0 ||
409 strcmp(current_command->name, "inherit") == 0 ||
410 strcmp(current_command->name, "list") == 0))
411 show_properties = B_TRUE;
413 if (show_properties) {
415 gettext("\nThe following properties are supported:\n"));
417 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
418 "PROPERTY", "EDIT", "INHERIT", "VALUES");
420 /* Iterate over all properties */
421 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
424 (void) fprintf(fp, "\t%-15s ", "userused@...");
425 (void) fprintf(fp, " NO NO <size>\n");
426 (void) fprintf(fp, "\t%-15s ", "groupused@...");
427 (void) fprintf(fp, " NO NO <size>\n");
428 (void) fprintf(fp, "\t%-15s ", "userquota@...");
429 (void) fprintf(fp, "YES NO <size> | none\n");
430 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
431 (void) fprintf(fp, "YES NO <size> | none\n");
432 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
433 (void) fprintf(fp, " NO NO <size>\n");
435 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
436 "with standard units such as K, M, G, etc.\n"));
437 (void) fprintf(fp, gettext("\nUser-defined properties can "
438 "be specified by using a name containing a colon (:).\n"));
439 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
440 "properties must be appended with\n"
441 "a user or group specifier of one of these forms:\n"
442 " POSIX name (eg: \"matt\")\n"
443 " POSIX id (eg: \"126829\")\n"
444 " SMB name@domain (eg: \"matt@sun\")\n"
445 " SMB SID (eg: \"S-1-234-567-89\")\n"));
448 gettext("\nFor the property list, run: %s\n"),
451 gettext("\nFor the delegated permission list, run: %s\n"),
452 "zfs allow|unallow");
456 * See comments at end of main().
458 if (getenv("ZFS_ABORT") != NULL) {
459 (void) printf("dumping core by request\n");
463 exit(requested ? 0 : 2);
467 parseprop(nvlist_t *props)
469 char *propname = optarg;
470 char *propval, *strval;
472 if ((propval = strchr(propname, '=')) == NULL) {
473 (void) fprintf(stderr, gettext("missing "
474 "'=' for -o option\n"));
479 if (nvlist_lookup_string(props, propname, &strval) == 0) {
480 (void) fprintf(stderr, gettext("property '%s' "
481 "specified multiple times\n"), propname);
484 if (nvlist_add_string(props, propname, propval) != 0)
490 parse_depth(char *opt, int *flags)
495 depth = (int)strtol(opt, &tmp, 0);
497 (void) fprintf(stderr,
498 gettext("%s is not an integer\n"), optarg);
502 (void) fprintf(stderr,
503 gettext("Depth can not be negative.\n"));
506 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
510 #define PROGRESS_DELAY 2 /* seconds */
512 static 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";
513 static time_t pt_begin;
514 static char *pt_header = NULL;
515 static boolean_t pt_shown;
518 start_progress_timer(void)
520 pt_begin = time(NULL) + PROGRESS_DELAY;
525 set_progress_header(char *header)
527 assert(pt_header == NULL);
528 pt_header = safe_strdup(header);
530 (void) printf("%s: ", header);
531 (void) fflush(stdout);
536 update_progress(char *update)
538 if (!pt_shown && time(NULL) > pt_begin) {
539 int len = strlen(update);
541 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
543 (void) fflush(stdout);
545 } else if (pt_shown) {
546 int len = strlen(update);
548 (void) printf("%s%*.*s", update, len, len, pt_reverse);
549 (void) fflush(stdout);
554 finish_progress(char *done)
557 (void) printf("%s\n", done);
558 (void) fflush(stdout);
565 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
567 * Given an existing dataset, create a writable copy whose initial contents
568 * are the same as the source. The newly created dataset maintains a
569 * dependency on the original; the original cannot be destroyed so long as
572 * The '-p' flag creates all the non-existing ancestors of the target first.
575 zfs_do_clone(int argc, char **argv)
577 zfs_handle_t *zhp = NULL;
578 boolean_t parents = B_FALSE;
583 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
587 while ((c = getopt(argc, argv, "o:p")) != -1) {
590 if (parseprop(props))
597 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
606 /* check number of arguments */
608 (void) fprintf(stderr, gettext("missing source dataset "
613 (void) fprintf(stderr, gettext("missing target dataset "
618 (void) fprintf(stderr, gettext("too many arguments\n"));
622 /* open the source dataset */
623 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
626 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
629 * Now create the ancestors of the target dataset. If the
630 * target already exists and '-p' option was used we should not
633 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
636 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
641 ret = zfs_clone(zhp, argv[1], props);
643 /* create the mountpoint if necessary */
647 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
649 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
650 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
651 ret = zfs_share(clone);
670 * zfs create [-p] [-o prop=value] ... fs
671 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
673 * Create a new dataset. This command can be used to create filesystems
674 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
675 * For volumes, the user must specify a size to be used.
677 * The '-s' flag applies only to volumes, and indicates that we should not try
678 * to set the reservation for this volume. By default we set a reservation
679 * equal to the size for any volume. For pools with SPA_VERSION >=
680 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
682 * The '-p' flag creates all the non-existing ancestors of the target first.
685 zfs_do_create(int argc, char **argv)
687 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
688 zfs_handle_t *zhp = NULL;
689 uint64_t volsize = 0;
691 boolean_t noreserve = B_FALSE;
692 boolean_t bflag = B_FALSE;
693 boolean_t parents = B_FALSE;
697 int canmount = ZFS_CANMOUNT_OFF;
699 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
703 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
706 type = ZFS_TYPE_VOLUME;
707 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
708 (void) fprintf(stderr, gettext("bad volume "
709 "size '%s': %s\n"), optarg,
710 libzfs_error_description(g_zfs));
714 if (nvlist_add_uint64(props,
715 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
724 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
725 (void) fprintf(stderr, gettext("bad volume "
726 "block size '%s': %s\n"), optarg,
727 libzfs_error_description(g_zfs));
731 if (nvlist_add_uint64(props,
732 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
737 if (parseprop(props))
744 (void) fprintf(stderr, gettext("missing size "
749 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
755 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
756 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
757 "used when creating a volume\n"));
764 /* check number of arguments */
766 (void) fprintf(stderr, gettext("missing %s argument\n"),
767 zfs_type_to_name(type));
771 (void) fprintf(stderr, gettext("too many arguments\n"));
775 if (type == ZFS_TYPE_VOLUME && !noreserve) {
776 zpool_handle_t *zpool_handle;
777 uint64_t spa_version;
779 zfs_prop_t resv_prop;
782 if ((p = strchr(argv[0], '/')))
784 zpool_handle = zpool_open(g_zfs, argv[0]);
787 if (zpool_handle == NULL)
789 spa_version = zpool_get_prop_int(zpool_handle,
790 ZPOOL_PROP_VERSION, NULL);
791 zpool_close(zpool_handle);
792 if (spa_version >= SPA_VERSION_REFRESERVATION)
793 resv_prop = ZFS_PROP_REFRESERVATION;
795 resv_prop = ZFS_PROP_RESERVATION;
796 volsize = zvol_volsize_to_reservation(volsize, props);
798 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
800 if (nvlist_add_uint64(props,
801 zfs_prop_to_name(resv_prop), volsize) != 0) {
808 if (parents && zfs_name_valid(argv[0], type)) {
810 * Now create the ancestors of target dataset. If the target
811 * already exists and '-p' option was used we should not
814 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
818 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
823 if (zfs_create(g_zfs, argv[0], type, props) != 0)
826 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
831 * if the user doesn't want the dataset automatically mounted,
832 * then skip the mount/share step
834 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
835 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
838 * Mount and/or share the new filesystem as appropriate. We provide a
839 * verbose error message to let the user know that their filesystem was
840 * in fact created, even if we failed to mount or share it.
842 if (canmount == ZFS_CANMOUNT_ON) {
843 if (zfs_mount(zhp, NULL, 0) != 0) {
844 (void) fprintf(stderr, gettext("filesystem "
845 "successfully created, but not mounted\n"));
847 } else if (zfs_share(zhp) != 0) {
848 (void) fprintf(stderr, gettext("filesystem "
849 "successfully created, but not shared\n"));
866 * zfs destroy [-rRf] <fs, vol>
867 * zfs destroy [-rRd] <snap>
869 * -r Recursively destroy all children
870 * -R Recursively destroy all dependents, including clones
871 * -f Force unmounting of any dependents
872 * -d If we can't destroy now, mark for deferred destruction
874 * Destroys the given dataset. By default, it will unmount any filesystems,
875 * and refuse to destroy a dataset that has any dependents. A dependent can
876 * either be a child, or a clone of a child.
878 typedef struct destroy_cbdata {
881 boolean_t cb_recurse;
883 boolean_t cb_doclones;
884 zfs_handle_t *cb_target;
885 boolean_t cb_defer_destroy;
886 boolean_t cb_verbose;
887 boolean_t cb_parsable;
891 /* first snap in contiguous run */
892 zfs_handle_t *cb_firstsnap;
893 /* previous snap in contiguous run */
894 zfs_handle_t *cb_prevsnap;
900 * Check for any dependents based on the '-r' or '-R' flags.
903 destroy_check_dependent(zfs_handle_t *zhp, void *data)
905 destroy_cbdata_t *cbp = data;
906 const char *tname = zfs_get_name(cbp->cb_target);
907 const char *name = zfs_get_name(zhp);
909 if (strncmp(tname, name, strlen(tname)) == 0 &&
910 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
912 * This is a direct descendant, not a clone somewhere else in
919 (void) fprintf(stderr, gettext("cannot destroy '%s': "
920 "%s has children\n"),
921 zfs_get_name(cbp->cb_target),
922 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
923 (void) fprintf(stderr, gettext("use '-r' to destroy "
924 "the following datasets:\n"));
925 cbp->cb_first = B_FALSE;
926 cbp->cb_error = B_TRUE;
929 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
932 * This is a clone. We only want to report this if the '-r'
933 * wasn't specified, or the target is a snapshot.
935 if (!cbp->cb_recurse &&
936 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
940 (void) fprintf(stderr, gettext("cannot destroy '%s': "
941 "%s has dependent clones\n"),
942 zfs_get_name(cbp->cb_target),
943 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
944 (void) fprintf(stderr, gettext("use '-R' to destroy "
945 "the following datasets:\n"));
946 cbp->cb_first = B_FALSE;
947 cbp->cb_error = B_TRUE;
948 cbp->cb_dryrun = B_TRUE;
951 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
960 destroy_callback(zfs_handle_t *zhp, void *data)
962 destroy_cbdata_t *cb = data;
963 const char *name = zfs_get_name(zhp);
965 if (cb->cb_verbose) {
966 if (cb->cb_parsable) {
967 (void) printf("destroy\t%s\n", name);
968 } else if (cb->cb_dryrun) {
969 (void) printf(gettext("would destroy %s\n"),
972 (void) printf(gettext("will destroy %s\n"),
978 * Ignore pools (which we've already flagged as an error before getting
981 if (strchr(zfs_get_name(zhp), '/') == NULL &&
982 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
987 if (!cb->cb_dryrun) {
988 if (zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
989 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1000 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1002 destroy_cbdata_t *cb = arg;
1003 const char *name = zfs_get_name(zhp);
1006 if (nvlist_exists(cb->cb_nvl, name)) {
1007 if (cb->cb_firstsnap == NULL)
1008 cb->cb_firstsnap = zfs_handle_dup(zhp);
1009 if (cb->cb_prevsnap != NULL)
1010 zfs_close(cb->cb_prevsnap);
1011 /* this snap continues the current range */
1012 cb->cb_prevsnap = zfs_handle_dup(zhp);
1013 if (cb->cb_verbose) {
1014 if (cb->cb_parsable) {
1015 (void) printf("destroy\t%s\n", name);
1016 } else if (cb->cb_dryrun) {
1017 (void) printf(gettext("would destroy %s\n"),
1020 (void) printf(gettext("will destroy %s\n"),
1024 } else if (cb->cb_firstsnap != NULL) {
1025 /* end of this range */
1027 err = zfs_get_snapused_int(cb->cb_firstsnap,
1028 cb->cb_prevsnap, &used);
1029 cb->cb_snapused += used;
1030 zfs_close(cb->cb_firstsnap);
1031 cb->cb_firstsnap = NULL;
1032 zfs_close(cb->cb_prevsnap);
1033 cb->cb_prevsnap = NULL;
1040 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1043 assert(cb->cb_firstsnap == NULL);
1044 assert(cb->cb_prevsnap == NULL);
1045 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1046 if (cb->cb_firstsnap != NULL) {
1049 err = zfs_get_snapused_int(cb->cb_firstsnap,
1050 cb->cb_prevsnap, &used);
1052 cb->cb_snapused += used;
1053 zfs_close(cb->cb_firstsnap);
1054 cb->cb_firstsnap = NULL;
1055 zfs_close(cb->cb_prevsnap);
1056 cb->cb_prevsnap = NULL;
1062 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1064 destroy_cbdata_t *cb = arg;
1067 /* Check for clones. */
1068 if (!cb->cb_doclones) {
1069 cb->cb_target = zhp;
1070 cb->cb_first = B_TRUE;
1071 err = zfs_iter_dependents(zhp, B_TRUE,
1072 destroy_check_dependent, cb);
1076 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1084 gather_snapshots(zfs_handle_t *zhp, void *arg)
1086 destroy_cbdata_t *cb = arg;
1089 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1095 if (cb->cb_verbose) {
1096 err = destroy_print_snapshots(zhp, cb);
1102 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1110 destroy_clones(destroy_cbdata_t *cb)
1113 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1115 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1116 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1119 boolean_t defer = cb->cb_defer_destroy;
1123 * We can't defer destroy non-snapshots, so set it to
1124 * false while destroying the clones.
1126 cb->cb_defer_destroy = B_FALSE;
1127 err = zfs_iter_dependents(zhp, B_FALSE,
1128 destroy_callback, cb);
1129 cb->cb_defer_destroy = defer;
1139 zfs_do_destroy(int argc, char **argv)
1141 destroy_cbdata_t cb = { 0 };
1145 zfs_type_t type = ZFS_TYPE_DATASET;
1148 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1151 cb.cb_verbose = B_TRUE;
1154 cb.cb_verbose = B_TRUE;
1155 cb.cb_parsable = B_TRUE;
1158 cb.cb_dryrun = B_TRUE;
1161 cb.cb_defer_destroy = B_TRUE;
1162 type = ZFS_TYPE_SNAPSHOT;
1165 cb.cb_force = B_TRUE;
1168 cb.cb_recurse = B_TRUE;
1171 cb.cb_recurse = B_TRUE;
1172 cb.cb_doclones = B_TRUE;
1176 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1185 /* check number of arguments */
1187 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1191 (void) fprintf(stderr, gettext("too many arguments\n"));
1195 at = strchr(argv[0], '@');
1199 /* Build the list of snaps to destroy in cb_nvl. */
1200 if (nvlist_alloc(&cb.cb_nvl, NV_UNIQUE_NAME, 0) != 0)
1204 zhp = zfs_open(g_zfs, argv[0],
1205 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1209 cb.cb_snapspec = at + 1;
1210 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1213 nvlist_free(cb.cb_nvl);
1217 if (nvlist_empty(cb.cb_nvl)) {
1218 (void) fprintf(stderr, gettext("could not find any "
1219 "snapshots to destroy; check snapshot names.\n"));
1221 nvlist_free(cb.cb_nvl);
1225 if (cb.cb_verbose) {
1227 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1228 if (cb.cb_parsable) {
1229 (void) printf("reclaim\t%llu\n",
1230 (u_longlong_t)cb.cb_snapused);
1231 } else if (cb.cb_dryrun) {
1232 (void) printf(gettext("would reclaim %s\n"),
1235 (void) printf(gettext("will reclaim %s\n"),
1240 if (!cb.cb_dryrun) {
1242 err = destroy_clones(&cb);
1244 err = zfs_destroy_snaps_nvl(zhp, cb.cb_nvl,
1245 cb.cb_defer_destroy);
1250 nvlist_free(cb.cb_nvl);
1254 /* Open the given dataset */
1255 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1261 * Perform an explicit check for pools before going any further.
1263 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1264 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1265 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1266 "operation does not apply to pools\n"),
1268 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1269 "%s' to destroy all datasets in the pool\n"),
1271 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1272 "to destroy the pool itself\n"), zfs_get_name(zhp));
1278 * Check for any dependents and/or clones.
1280 cb.cb_first = B_TRUE;
1281 if (!cb.cb_doclones &&
1282 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1293 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1300 * Do the real thing. The callback will close the
1301 * handle regardless of whether it succeeds or not.
1303 if (destroy_callback(zhp, &cb) != 0)
1311 is_recvd_column(zprop_get_cbdata_t *cbp)
1314 zfs_get_column_t col;
1316 for (i = 0; i < ZFS_GET_NCOLS &&
1317 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1318 if (col == GET_COL_RECVD)
1324 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1325 * < all | property[,property]... > < fs | snap | vol > ...
1327 * -r recurse over any child datasets
1328 * -H scripted mode. Headers are stripped, and fields are separated
1329 * by tabs instead of spaces.
1330 * -o Set of fields to display. One of "name,property,value,
1331 * received,source". Default is "name,property,value,source".
1332 * "all" is an alias for all five.
1333 * -s Set of sources to allow. One of
1334 * "local,default,inherited,received,temporary,none". Default is
1336 * -p Display values in parsable (literal) format.
1338 * Prints properties for the given datasets. The user can control which
1339 * columns to display as well as which property types to allow.
1343 * Invoked to display the properties for a single dataset.
1346 get_callback(zfs_handle_t *zhp, void *data)
1348 char buf[ZFS_MAXPROPLEN];
1349 char rbuf[ZFS_MAXPROPLEN];
1350 zprop_source_t sourcetype;
1351 char source[ZFS_MAXNAMELEN];
1352 zprop_get_cbdata_t *cbp = data;
1353 nvlist_t *user_props = zfs_get_user_props(zhp);
1354 zprop_list_t *pl = cbp->cb_proplist;
1358 boolean_t received = is_recvd_column(cbp);
1360 for (; pl != NULL; pl = pl->pl_next) {
1361 char *recvdval = NULL;
1363 * Skip the special fake placeholder. This will also skip over
1364 * the name property when 'all' is specified.
1366 if (pl->pl_prop == ZFS_PROP_NAME &&
1367 pl == cbp->cb_proplist)
1370 if (pl->pl_prop != ZPROP_INVAL) {
1371 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1372 sizeof (buf), &sourcetype, source,
1374 cbp->cb_literal) != 0) {
1377 if (!zfs_prop_valid_for_type(pl->pl_prop,
1378 ZFS_TYPE_DATASET)) {
1379 (void) fprintf(stderr,
1380 gettext("No such property '%s'\n"),
1381 zfs_prop_to_name(pl->pl_prop));
1384 sourcetype = ZPROP_SRC_NONE;
1385 (void) strlcpy(buf, "-", sizeof (buf));
1388 if (received && (zfs_prop_get_recvd(zhp,
1389 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1390 cbp->cb_literal) == 0))
1393 zprop_print_one_property(zfs_get_name(zhp), cbp,
1394 zfs_prop_to_name(pl->pl_prop),
1395 buf, sourcetype, source, recvdval);
1396 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1397 sourcetype = ZPROP_SRC_LOCAL;
1399 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1400 buf, sizeof (buf), cbp->cb_literal) != 0) {
1401 sourcetype = ZPROP_SRC_NONE;
1402 (void) strlcpy(buf, "-", sizeof (buf));
1405 zprop_print_one_property(zfs_get_name(zhp), cbp,
1406 pl->pl_user_prop, buf, sourcetype, source, NULL);
1407 } else if (zfs_prop_written(pl->pl_user_prop)) {
1408 sourcetype = ZPROP_SRC_LOCAL;
1410 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1411 buf, sizeof (buf), cbp->cb_literal) != 0) {
1412 sourcetype = ZPROP_SRC_NONE;
1413 (void) strlcpy(buf, "-", sizeof (buf));
1416 zprop_print_one_property(zfs_get_name(zhp), cbp,
1417 pl->pl_user_prop, buf, sourcetype, source, NULL);
1419 if (nvlist_lookup_nvlist(user_props,
1420 pl->pl_user_prop, &propval) != 0) {
1423 sourcetype = ZPROP_SRC_NONE;
1426 verify(nvlist_lookup_string(propval,
1427 ZPROP_VALUE, &strval) == 0);
1428 verify(nvlist_lookup_string(propval,
1429 ZPROP_SOURCE, &sourceval) == 0);
1431 if (strcmp(sourceval,
1432 zfs_get_name(zhp)) == 0) {
1433 sourcetype = ZPROP_SRC_LOCAL;
1434 } else if (strcmp(sourceval,
1435 ZPROP_SOURCE_VAL_RECVD) == 0) {
1436 sourcetype = ZPROP_SRC_RECEIVED;
1438 sourcetype = ZPROP_SRC_INHERITED;
1439 (void) strlcpy(source,
1440 sourceval, sizeof (source));
1444 if (received && (zfs_prop_get_recvd(zhp,
1445 pl->pl_user_prop, rbuf, sizeof (rbuf),
1446 cbp->cb_literal) == 0))
1449 zprop_print_one_property(zfs_get_name(zhp), cbp,
1450 pl->pl_user_prop, strval, sourcetype,
1459 zfs_do_get(int argc, char **argv)
1461 zprop_get_cbdata_t cb = { 0 };
1462 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1463 char *value, *fields;
1466 zprop_list_t fake_name = { 0 };
1469 * Set up default columns and sources.
1471 cb.cb_sources = ZPROP_SRC_ALL;
1472 cb.cb_columns[0] = GET_COL_NAME;
1473 cb.cb_columns[1] = GET_COL_PROPERTY;
1474 cb.cb_columns[2] = GET_COL_VALUE;
1475 cb.cb_columns[3] = GET_COL_SOURCE;
1476 cb.cb_type = ZFS_TYPE_DATASET;
1479 while ((c = getopt(argc, argv, ":d:o:s:rHp")) != -1) {
1482 cb.cb_literal = B_TRUE;
1485 limit = parse_depth(optarg, &flags);
1488 flags |= ZFS_ITER_RECURSE;
1491 cb.cb_scripted = B_TRUE;
1494 (void) fprintf(stderr, gettext("missing argument for "
1495 "'%c' option\n"), optopt);
1500 * Process the set of columns to display. We zero out
1501 * the structure to give us a blank slate.
1503 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1505 while (*optarg != '\0') {
1506 static char *col_subopts[] =
1507 { "name", "property", "value", "received",
1508 "source", "all", NULL };
1510 if (i == ZFS_GET_NCOLS) {
1511 (void) fprintf(stderr, gettext("too "
1512 "many fields given to -o "
1517 switch (getsubopt(&optarg, col_subopts,
1520 cb.cb_columns[i++] = GET_COL_NAME;
1523 cb.cb_columns[i++] = GET_COL_PROPERTY;
1526 cb.cb_columns[i++] = GET_COL_VALUE;
1529 cb.cb_columns[i++] = GET_COL_RECVD;
1530 flags |= ZFS_ITER_RECVD_PROPS;
1533 cb.cb_columns[i++] = GET_COL_SOURCE;
1537 (void) fprintf(stderr,
1538 gettext("\"all\" conflicts "
1539 "with specific fields "
1540 "given to -o option\n"));
1543 cb.cb_columns[0] = GET_COL_NAME;
1544 cb.cb_columns[1] = GET_COL_PROPERTY;
1545 cb.cb_columns[2] = GET_COL_VALUE;
1546 cb.cb_columns[3] = GET_COL_RECVD;
1547 cb.cb_columns[4] = GET_COL_SOURCE;
1548 flags |= ZFS_ITER_RECVD_PROPS;
1552 (void) fprintf(stderr,
1553 gettext("invalid column name "
1562 while (*optarg != '\0') {
1563 static char *source_subopts[] = {
1564 "local", "default", "inherited",
1565 "received", "temporary", "none",
1568 switch (getsubopt(&optarg, source_subopts,
1571 cb.cb_sources |= ZPROP_SRC_LOCAL;
1574 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1577 cb.cb_sources |= ZPROP_SRC_INHERITED;
1580 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1583 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1586 cb.cb_sources |= ZPROP_SRC_NONE;
1589 (void) fprintf(stderr,
1590 gettext("invalid source "
1598 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1608 (void) fprintf(stderr, gettext("missing property "
1615 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1623 * As part of zfs_expand_proplist(), we keep track of the maximum column
1624 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1625 * need to know the maximum name length. However, the user likely did
1626 * not specify 'name' as one of the properties to fetch, so we need to
1627 * make sure we always include at least this property for
1628 * print_get_headers() to work properly.
1630 if (cb.cb_proplist != NULL) {
1631 fake_name.pl_prop = ZFS_PROP_NAME;
1632 fake_name.pl_width = strlen(gettext("NAME"));
1633 fake_name.pl_next = cb.cb_proplist;
1634 cb.cb_proplist = &fake_name;
1637 cb.cb_first = B_TRUE;
1639 /* run for each object */
1640 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL,
1641 &cb.cb_proplist, limit, get_callback, &cb);
1643 if (cb.cb_proplist == &fake_name)
1644 zprop_free_list(fake_name.pl_next);
1646 zprop_free_list(cb.cb_proplist);
1652 * inherit [-rS] <property> <fs|vol> ...
1654 * -r Recurse over all children
1655 * -S Revert to received value, if any
1657 * For each dataset specified on the command line, inherit the given property
1658 * from its parent. Inheriting a property at the pool level will cause it to
1659 * use the default value. The '-r' flag will recurse over all children, and is
1660 * useful for setting a property on a hierarchy-wide basis, regardless of any
1661 * local modifications for each dataset.
1664 typedef struct inherit_cbdata {
1665 const char *cb_propname;
1666 boolean_t cb_received;
1670 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1672 inherit_cbdata_t *cb = data;
1673 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1676 * If we're doing it recursively, then ignore properties that
1677 * are not valid for this type of dataset.
1679 if (prop != ZPROP_INVAL &&
1680 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1683 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1687 inherit_cb(zfs_handle_t *zhp, void *data)
1689 inherit_cbdata_t *cb = data;
1691 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1695 zfs_do_inherit(int argc, char **argv)
1699 inherit_cbdata_t cb = { 0 };
1703 boolean_t received = B_FALSE;
1706 while ((c = getopt(argc, argv, "rS")) != -1) {
1709 flags |= ZFS_ITER_RECURSE;
1716 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1725 /* check number of arguments */
1727 (void) fprintf(stderr, gettext("missing property argument\n"));
1731 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1739 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1740 if (zfs_prop_readonly(prop)) {
1741 (void) fprintf(stderr, gettext(
1742 "%s property is read-only\n"),
1746 if (!zfs_prop_inheritable(prop) && !received) {
1747 (void) fprintf(stderr, gettext("'%s' property cannot "
1748 "be inherited\n"), propname);
1749 if (prop == ZFS_PROP_QUOTA ||
1750 prop == ZFS_PROP_RESERVATION ||
1751 prop == ZFS_PROP_REFQUOTA ||
1752 prop == ZFS_PROP_REFRESERVATION)
1753 (void) fprintf(stderr, gettext("use 'zfs set "
1754 "%s=none' to clear\n"), propname);
1757 if (received && (prop == ZFS_PROP_VOLSIZE ||
1758 prop == ZFS_PROP_VERSION)) {
1759 (void) fprintf(stderr, gettext("'%s' property cannot "
1760 "be reverted to a received value\n"), propname);
1763 } else if (!zfs_prop_user(propname)) {
1764 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1769 cb.cb_propname = propname;
1770 cb.cb_received = received;
1772 if (flags & ZFS_ITER_RECURSE) {
1773 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1774 NULL, NULL, 0, inherit_recurse_cb, &cb);
1776 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1777 NULL, NULL, 0, inherit_cb, &cb);
1783 typedef struct upgrade_cbdata {
1784 uint64_t cb_numupgraded;
1785 uint64_t cb_numsamegraded;
1786 uint64_t cb_numfailed;
1787 uint64_t cb_version;
1789 boolean_t cb_foundone;
1790 char cb_lastfs[ZFS_MAXNAMELEN];
1794 same_pool(zfs_handle_t *zhp, const char *name)
1796 int len1 = strcspn(name, "/@");
1797 const char *zhname = zfs_get_name(zhp);
1798 int len2 = strcspn(zhname, "/@");
1802 return (strncmp(name, zhname, len1) == 0);
1806 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1808 upgrade_cbdata_t *cb = data;
1809 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1811 /* list if it's old/new */
1812 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1813 (cb->cb_newer && version > ZPL_VERSION)) {
1816 str = gettext("The following filesystems are "
1817 "formatted using a newer software version and\n"
1818 "cannot be accessed on the current system.\n\n");
1820 str = gettext("The following filesystems are "
1821 "out of date, and can be upgraded. After being\n"
1822 "upgraded, these filesystems (and any 'zfs send' "
1823 "streams generated from\n"
1824 "subsequent snapshots) will no longer be "
1825 "accessible by older software versions.\n\n");
1828 if (!cb->cb_foundone) {
1830 (void) printf(gettext("VER FILESYSTEM\n"));
1831 (void) printf(gettext("--- ------------\n"));
1832 cb->cb_foundone = B_TRUE;
1835 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1842 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1844 upgrade_cbdata_t *cb = data;
1845 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1846 int needed_spa_version;
1849 if (zfs_spa_version(zhp, &spa_version) < 0)
1852 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1854 if (needed_spa_version < 0)
1857 if (spa_version < needed_spa_version) {
1859 (void) printf(gettext("%s: can not be "
1860 "upgraded; the pool version needs to first "
1861 "be upgraded\nto version %d\n\n"),
1862 zfs_get_name(zhp), needed_spa_version);
1868 if (version < cb->cb_version) {
1870 (void) snprintf(verstr, sizeof (verstr),
1871 "%llu", (u_longlong_t)cb->cb_version);
1872 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1874 * If they did "zfs upgrade -a", then we could
1875 * be doing ioctls to different pools. We need
1876 * to log this history once to each pool.
1878 verify(zpool_stage_history(g_zfs, history_str) == 0);
1880 if (zfs_prop_set(zhp, "version", verstr) == 0)
1881 cb->cb_numupgraded++;
1884 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1885 } else if (version > cb->cb_version) {
1886 /* can't downgrade */
1887 (void) printf(gettext("%s: can not be downgraded; "
1888 "it is already at version %u\n"),
1889 zfs_get_name(zhp), version);
1892 cb->cb_numsamegraded++;
1900 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1903 zfs_do_upgrade(int argc, char **argv)
1905 boolean_t all = B_FALSE;
1906 boolean_t showversions = B_FALSE;
1908 upgrade_cbdata_t cb = { 0 };
1910 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1913 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1916 flags |= ZFS_ITER_RECURSE;
1919 showversions = B_TRUE;
1922 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1923 optarg, &cb.cb_version) != 0) {
1924 (void) fprintf(stderr,
1925 gettext("invalid version %s\n"), optarg);
1934 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1943 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1945 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1946 cb.cb_version || argc))
1948 if ((all || argc) && (showversions))
1954 /* Show info on available versions. */
1955 (void) printf(gettext("The following filesystem versions are "
1957 (void) printf(gettext("VER DESCRIPTION\n"));
1958 (void) printf("--- -----------------------------------------"
1959 "---------------\n");
1960 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1961 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1962 (void) printf(gettext(" 3 Case insensitive and filesystem "
1963 "user identifier (FUID)\n"));
1964 (void) printf(gettext(" 4 userquota, groupquota "
1966 (void) printf(gettext(" 5 System attributes\n"));
1967 (void) printf(gettext("\nFor more information on a particular "
1968 "version, including supported releases,\n"));
1969 (void) printf("see the ZFS Administration Guide.\n\n");
1971 } else if (argc || all) {
1972 /* Upgrade filesystems */
1973 if (cb.cb_version == 0)
1974 cb.cb_version = ZPL_VERSION;
1975 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
1976 NULL, NULL, 0, upgrade_set_callback, &cb);
1977 (void) printf(gettext("%llu filesystems upgraded\n"),
1978 (u_longlong_t)cb.cb_numupgraded);
1979 if (cb.cb_numsamegraded) {
1980 (void) printf(gettext("%llu filesystems already at "
1982 (u_longlong_t)cb.cb_numsamegraded);
1984 if (cb.cb_numfailed != 0)
1987 /* List old-version filesytems */
1989 (void) printf(gettext("This system is currently running "
1990 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1992 flags |= ZFS_ITER_RECURSE;
1993 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1994 NULL, NULL, 0, upgrade_list_callback, &cb);
1996 found = cb.cb_foundone;
1997 cb.cb_foundone = B_FALSE;
1998 cb.cb_newer = B_TRUE;
2000 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2001 NULL, NULL, 0, upgrade_list_callback, &cb);
2003 if (!cb.cb_foundone && !found) {
2004 (void) printf(gettext("All filesystems are "
2005 "formatted with the current version.\n"));
2012 #define USTYPE_USR_BIT (0)
2013 #define USTYPE_GRP_BIT (1)
2014 #define USTYPE_PSX_BIT (2)
2015 #define USTYPE_SMB_BIT (3)
2017 #define USTYPE_USR (1 << USTYPE_USR_BIT)
2018 #define USTYPE_GRP (1 << USTYPE_GRP_BIT)
2020 #define USTYPE_PSX (1 << USTYPE_PSX_BIT)
2021 #define USTYPE_SMB (1 << USTYPE_SMB_BIT)
2023 #define USTYPE_PSX_USR (USTYPE_PSX | USTYPE_USR)
2024 #define USTYPE_SMB_USR (USTYPE_SMB | USTYPE_USR)
2025 #define USTYPE_PSX_GRP (USTYPE_PSX | USTYPE_GRP)
2026 #define USTYPE_SMB_GRP (USTYPE_SMB | USTYPE_GRP)
2027 #define USTYPE_ALL (USTYPE_PSX_USR | USTYPE_SMB_USR \
2028 | USTYPE_PSX_GRP | USTYPE_SMB_GRP)
2031 #define USPROP_USED_BIT (0)
2032 #define USPROP_QUOTA_BIT (1)
2034 #define USPROP_USED (1 << USPROP_USED_BIT)
2035 #define USPROP_QUOTA (1 << USPROP_QUOTA_BIT)
2037 typedef struct us_node {
2039 uu_avl_node_t usn_avlnode;
2040 uu_list_node_t usn_listnode;
2043 typedef struct us_cbdata {
2045 uu_avl_pool_t *cb_avl_pool;
2047 boolean_t cb_numname;
2048 boolean_t cb_nicenum;
2049 boolean_t cb_sid2posix;
2050 zfs_userquota_prop_t cb_prop;
2051 zfs_sort_column_t *cb_sortcol;
2052 size_t cb_max_typelen;
2053 size_t cb_max_namelen;
2054 size_t cb_max_usedlen;
2055 size_t cb_max_quotalen;
2059 zfs_sort_column_t *si_sortcol;
2060 boolean_t si_num_name;
2061 boolean_t si_parsable;
2065 us_compare(const void *larg, const void *rarg, void *unused)
2067 const us_node_t *l = larg;
2068 const us_node_t *r = rarg;
2070 us_sort_info_t *si = (us_sort_info_t *)unused;
2071 zfs_sort_column_t *sortcol = si->si_sortcol;
2072 boolean_t num_name = si->si_num_name;
2073 nvlist_t *lnvl = l->usn_nvl;
2074 nvlist_t *rnvl = r->usn_nvl;
2076 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2083 zfs_prop_t prop = sortcol->sc_prop;
2084 const char *propname = NULL;
2085 boolean_t reverse = sortcol->sc_reverse;
2090 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2091 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2093 rc = (rv32 > lv32) ? 1 : -1;
2098 (void) nvlist_lookup_uint32(lnvl, propname,
2100 (void) nvlist_lookup_uint32(rnvl, propname,
2103 rc = (rv32 > lv32) ? 1 : -1;
2105 (void) nvlist_lookup_string(lnvl, propname,
2107 (void) nvlist_lookup_string(rnvl, propname,
2109 rc = strcmp(lvstr, rvstr);
2114 case ZFS_PROP_QUOTA:
2115 if (ZFS_PROP_USED == prop)
2119 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2120 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2122 rc = (rv64 > lv64) ? 1 : -1;
2129 return (reverse ? 1 : -1);
2131 return (reverse ? -1 : 1);
2138 static inline const char *
2139 us_type2str(unsigned field_type)
2141 switch (field_type) {
2142 case USTYPE_PSX_USR:
2143 return ("POSIX User");
2144 case USTYPE_PSX_GRP:
2145 return ("POSIX Group");
2146 case USTYPE_SMB_USR:
2147 return ("SMB User");
2148 case USTYPE_SMB_GRP:
2149 return ("SMB Group");
2151 return ("Undefined");
2159 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2161 us_cbdata_t *cb = (us_cbdata_t *)arg;
2162 zfs_userquota_prop_t prop = cb->cb_prop;
2168 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2169 uu_avl_t *avl = cb->cb_avl;
2173 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2175 const char *typestr;
2179 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2181 if (domain == NULL || domain[0] == '\0') {
2183 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2184 type = USTYPE_PSX_GRP;
2185 struct group *g = getgrgid(rid);
2189 type = USTYPE_PSX_USR;
2190 struct passwd *p = getpwuid(rid);
2196 char sid[ZFS_MAXNAMELEN+32];
2200 directory_error_t e;
2202 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2204 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2205 type = USTYPE_SMB_GRP;
2206 err = sid_to_id(sid, B_FALSE, &id);
2208 type = USTYPE_SMB_USR;
2209 err = sid_to_id(sid, B_TRUE, &id);
2215 e = directory_name_from_sid(NULL, sid, &name, &classes);
2217 directory_error_free(e);
2226 #endif /* HAVE_IDMAP */
2230 * if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA)
2236 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED)
2241 (void) snprintf(namebuf, sizeof (namebuf), "%u", rid);
2246 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2248 (void) sprintf(sizebuf, "%llu", (u_longlong_t)space);
2250 node = safe_malloc(sizeof (us_node_t));
2251 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2253 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
2258 if (nvlist_add_uint32(props, "type", type) != 0)
2261 if (cb->cb_numname) {
2262 if (nvlist_add_uint32(props, "name", rid) != 0)
2264 namelen = strlen(namebuf);
2266 if (nvlist_add_string(props, "name", name) != 0)
2268 namelen = strlen(name);
2271 typestr = us_type2str(type);
2272 typelen = strlen(gettext(typestr));
2273 if (typelen > cb->cb_max_typelen)
2274 cb->cb_max_typelen = typelen;
2276 if (namelen > cb->cb_max_namelen)
2277 cb->cb_max_namelen = namelen;
2279 sizelen = strlen(sizebuf);
2280 if (0 == strcmp(propname, "used")) {
2281 if (sizelen > cb->cb_max_usedlen)
2282 cb->cb_max_usedlen = sizelen;
2284 if (sizelen > cb->cb_max_quotalen)
2285 cb->cb_max_quotalen = sizelen;
2288 node->usn_nvl = props;
2290 n = uu_avl_find(avl, node, &sortinfo, &idx);
2292 uu_avl_insert(avl, node, idx);
2297 props = node->usn_nvl;
2300 if (nvlist_add_uint64(props, propname, space) != 0)
2306 static inline boolean_t
2307 usprop_check(zfs_userquota_prop_t p, unsigned types, unsigned props)
2313 case ZFS_PROP_USERUSED:
2317 case ZFS_PROP_USERQUOTA:
2319 prop = USPROP_QUOTA;
2321 case ZFS_PROP_GROUPUSED:
2325 case ZFS_PROP_GROUPQUOTA:
2327 prop = USPROP_QUOTA;
2333 return (type & types && prop & props);
2336 #define USFIELD_TYPE (1 << 0)
2337 #define USFIELD_NAME (1 << 1)
2338 #define USFIELD_USED (1 << 2)
2339 #define USFIELD_QUOTA (1 << 3)
2340 #define USFIELD_ALL (USFIELD_TYPE | USFIELD_NAME | USFIELD_USED | USFIELD_QUOTA)
2343 parsefields(unsigned *fieldsp, char **names, unsigned *bits, size_t len)
2345 char *field = optarg;
2350 boolean_t found = B_FALSE;
2351 delim = strchr(field, ',');
2355 for (i = 0; i < len; i++)
2356 if (0 == strcmp(field, names[i])) {
2358 *fieldsp |= bits[i];
2363 (void) fprintf(stderr, gettext("invalid type '%s'"
2364 "for -t option\n"), field);
2375 static char *type_names[] = { "posixuser", "smbuser", "posixgroup", "smbgroup",
2377 static unsigned type_bits[] = {
2385 static char *us_field_names[] = { "type", "name", "used", "quota" };
2386 static unsigned us_field_bits[] = {
2394 print_us_node(boolean_t scripted, boolean_t parseable, unsigned fields,
2395 size_t type_width, size_t name_width, size_t used_width,
2396 size_t quota_width, us_node_t *node)
2398 nvlist_t *nvl = node->usn_nvl;
2399 nvpair_t *nvp = NULL;
2400 char valstr[ZFS_MAXNAMELEN];
2401 boolean_t first = B_TRUE;
2402 boolean_t quota_found = B_FALSE;
2404 if (fields & USFIELD_QUOTA && !nvlist_exists(nvl, "quota"))
2405 if (nvlist_add_string(nvl, "quota", "none") != 0)
2408 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2409 char *pname = nvpair_name(nvp);
2410 data_type_t type = nvpair_type(nvp);
2413 char *strval = NULL;
2417 for (i = 0; i < 4; i++) {
2418 if (0 == strcmp(pname, us_field_names[i])) {
2419 field = us_field_bits[i];
2424 if (!(field & fields))
2428 case DATA_TYPE_UINT32:
2429 (void) nvpair_value_uint32(nvp, &val32);
2431 case DATA_TYPE_UINT64:
2432 (void) nvpair_value_uint64(nvp, &val64);
2434 case DATA_TYPE_STRING:
2435 (void) nvpair_value_string(nvp, &strval);
2438 (void) fprintf(stderr, "Invalid data type\n");
2443 (void) printf("\t");
2450 strval = (char *)us_type2str(val32);
2454 if (type == DATA_TYPE_UINT64) {
2455 (void) sprintf(valstr, "%llu",
2456 (u_longlong_t) val64);
2463 if (type == DATA_TYPE_UINT64) {
2464 (void) nvpair_value_uint64(nvp, &val64);
2466 (void) sprintf(valstr, "%llu",
2467 (u_longlong_t) val64);
2469 zfs_nicenum(val64, valstr,
2474 if (field == USFIELD_USED)
2477 quota_found = B_FALSE;
2478 width = quota_width;
2484 if (field == USFIELD_QUOTA && !quota_found)
2485 (void) printf("%*s", width, strval);
2487 if (type == DATA_TYPE_STRING)
2488 (void) printf("%-*s", width, strval);
2490 (void) printf("%*s", width, strval);
2497 (void) printf("\n");
2501 print_us(boolean_t scripted, boolean_t parsable, unsigned fields,
2502 unsigned type_width, unsigned name_width, unsigned used_width,
2503 unsigned quota_width, boolean_t rmnode, uu_avl_t *avl)
2505 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2509 int width[4] = { type_width, name_width, used_width, quota_width };
2512 boolean_t first = B_TRUE;
2513 for (i = 0; i < 4; i++) {
2514 unsigned field = us_field_bits[i];
2515 if (!(field & fields))
2518 col = gettext(us_field_hdr[i]);
2519 if (field == USFIELD_TYPE || field == USFIELD_NAME)
2520 (void) printf(first?"%-*s":" %-*s", width[i],
2523 (void) printf(first?"%*s":" %*s", width[i],
2527 (void) printf("\n");
2530 for (node = uu_avl_first(avl); node != NULL;
2531 node = uu_avl_next(avl, node)) {
2532 print_us_node(scripted, parsable, fields, type_width,
2533 name_width, used_width, used_width, node);
2535 nvlist_free(node->usn_nvl);
2540 zfs_do_userspace(int argc, char **argv)
2543 zfs_userquota_prop_t p;
2544 uu_avl_pool_t *avl_pool;
2546 uu_avl_walk_t *walk;
2549 boolean_t scripted = B_FALSE;
2550 boolean_t prtnum = B_FALSE;
2551 boolean_t parseable = B_FALSE;
2552 boolean_t sid2posix = B_FALSE;
2555 zfs_sort_column_t *default_sortcol = NULL;
2556 zfs_sort_column_t *sortcol = NULL;
2557 unsigned types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2558 unsigned fields = 0;
2559 unsigned props = USPROP_USED | USPROP_QUOTA;
2562 boolean_t resort_avl = B_FALSE;
2568 if (0 == strcmp(cmd, "groupspace"))
2569 /* toggle default group types */
2570 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2573 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2585 if (parsefields(&fields, us_field_names, us_field_bits,
2590 if (zfs_add_sort_column(&sortcol, optarg,
2592 (void) fprintf(stderr,
2593 gettext("invalid property '%s'\n"), optarg);
2598 if (zfs_add_sort_column(&sortcol, optarg,
2600 (void) fprintf(stderr,
2601 gettext("invalid property '%s'\n"), optarg);
2606 if (parsefields(&types, type_names, type_bits, 5))
2613 (void) fprintf(stderr, gettext("missing argument for "
2614 "'%c' option\n"), optopt);
2618 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2627 /* ok, now we have sorted by default colums (type,name) avl tree */
2629 zfs_sort_column_t *sc;
2630 for (sc = sortcol; sc; sc = sc->sc_next) {
2631 if (sc->sc_prop == ZFS_PROP_QUOTA) {
2632 resort_avl = B_TRUE;
2639 fields = USFIELD_ALL;
2641 if ((zhp = zfs_open(g_zfs, argv[argc-1], ZFS_TYPE_DATASET)) == NULL)
2644 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2645 offsetof(us_node_t, usn_avlnode),
2646 us_compare, UU_DEFAULT)) == NULL)
2648 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2651 if (sortcol && !resort_avl)
2652 cb.cb_sortcol = sortcol;
2654 (void) zfs_add_sort_column(&default_sortcol, "type", B_FALSE);
2655 (void) zfs_add_sort_column(&default_sortcol, "name", B_FALSE);
2656 cb.cb_sortcol = default_sortcol;
2658 cb.cb_numname = prtnum;
2659 cb.cb_nicenum = !parseable;
2660 cb.cb_avl_pool = avl_pool;
2661 cb.cb_avl = avl_tree;
2662 cb.cb_sid2posix = sid2posix;
2663 cb.cb_max_typelen = strlen(gettext("TYPE"));
2664 cb.cb_max_namelen = strlen(gettext("NAME"));
2665 cb.cb_max_usedlen = strlen(gettext("USED"));
2666 cb.cb_max_quotalen = strlen(gettext("QUOTA"));
2668 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2669 if (!usprop_check(p, types, props))
2673 error = zfs_userspace(zhp, p, userspace_cb, &cb);
2682 uu_list_pool_t *listpool;
2684 uu_avl_index_t idx = 0;
2685 uu_list_index_t idx2 = 0;
2686 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2687 offsetof(us_node_t, usn_listnode), NULL,
2689 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2691 node = uu_avl_first(avl_tree);
2692 uu_list_node_init(node, &node->usn_listnode, listpool);
2693 while (node != NULL) {
2695 node = uu_avl_next(avl_tree, node);
2696 uu_avl_remove(avl_tree, rmnode);
2697 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL) {
2698 uu_list_insert(list, rmnode, idx2);
2702 for (node = uu_list_first(list); node != NULL;
2703 node = uu_list_next(list, node)) {
2704 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2705 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) ==
2707 uu_avl_insert(avl_tree, node, idx);
2710 uu_list_destroy(list);
2713 /* print & free node`s nvlist memory */
2714 print_us(scripted, parseable, fields, cb.cb_max_typelen,
2715 cb.cb_max_namelen, cb.cb_max_usedlen,
2716 cb.cb_max_quotalen, B_TRUE, cb.cb_avl);
2719 zfs_free_sort_columns(sortcol);
2720 zfs_free_sort_columns(default_sortcol);
2723 * Finally, clean up the AVL tree.
2725 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2728 while ((node = uu_avl_walk_next(walk)) != NULL) {
2729 uu_avl_remove(cb.cb_avl, node);
2733 uu_avl_walk_end(walk);
2734 uu_avl_destroy(avl_tree);
2735 uu_avl_pool_destroy(avl_pool);
2741 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2742 * [-s property [-s property]...] [-S property [-S property]...]
2745 * -r Recurse over all children
2746 * -d Limit recursion by depth.
2747 * -H Scripted mode; elide headers and separate columns by tabs
2748 * -o Control which fields to display.
2749 * -t Control which object types to display.
2750 * -s Specify sort columns, descending order.
2751 * -S Specify sort columns, ascending order.
2753 * When given no arguments, lists all filesystems in the system.
2754 * Otherwise, list the specified datasets, optionally recursing down them if
2755 * '-r' is specified.
2757 typedef struct list_cbdata {
2759 boolean_t cb_scripted;
2760 zprop_list_t *cb_proplist;
2764 * Given a list of columns to display, output appropriate headers for each one.
2767 print_header(zprop_list_t *pl)
2769 char headerbuf[ZFS_MAXPROPLEN];
2772 boolean_t first = B_TRUE;
2773 boolean_t right_justify;
2775 for (; pl != NULL; pl = pl->pl_next) {
2782 right_justify = B_FALSE;
2783 if (pl->pl_prop != ZPROP_INVAL) {
2784 header = zfs_prop_column_name(pl->pl_prop);
2785 right_justify = zfs_prop_align_right(pl->pl_prop);
2787 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2788 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2789 headerbuf[i] = '\0';
2793 if (pl->pl_next == NULL && !right_justify)
2794 (void) printf("%s", header);
2795 else if (right_justify)
2796 (void) printf("%*s", (int)pl->pl_width, header);
2798 (void) printf("%-*s", (int)pl->pl_width, header);
2801 (void) printf("\n");
2805 * Given a dataset and a list of fields, print out all the properties according
2806 * to the described layout.
2809 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
2811 boolean_t first = B_TRUE;
2812 char property[ZFS_MAXPROPLEN];
2813 nvlist_t *userprops = zfs_get_user_props(zhp);
2816 boolean_t right_justify;
2819 for (; pl != NULL; pl = pl->pl_next) {
2822 (void) printf("\t");
2829 if (pl->pl_prop == ZFS_PROP_NAME) {
2830 (void) strlcpy(property, zfs_get_name(zhp),
2833 right_justify = zfs_prop_align_right(pl->pl_prop);
2834 } else if (pl->pl_prop != ZPROP_INVAL) {
2835 if (zfs_prop_get(zhp, pl->pl_prop, property,
2836 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2841 right_justify = zfs_prop_align_right(pl->pl_prop);
2842 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2843 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2844 property, sizeof (property), B_FALSE) != 0)
2848 right_justify = B_TRUE;
2849 } else if (zfs_prop_written(pl->pl_user_prop)) {
2850 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2851 property, sizeof (property), B_FALSE) != 0)
2855 right_justify = B_TRUE;
2857 if (nvlist_lookup_nvlist(userprops,
2858 pl->pl_user_prop, &propval) != 0)
2861 verify(nvlist_lookup_string(propval,
2862 ZPROP_VALUE, &propstr) == 0);
2863 right_justify = B_FALSE;
2866 width = pl->pl_width;
2869 * If this is being called in scripted mode, or if this is the
2870 * last column and it is left-justified, don't include a width
2873 if (scripted || (pl->pl_next == NULL && !right_justify))
2874 (void) printf("%s", propstr);
2875 else if (right_justify)
2876 (void) printf("%*s", width, propstr);
2878 (void) printf("%-*s", width, propstr);
2881 (void) printf("\n");
2885 * Generic callback function to list a dataset or snapshot.
2888 list_callback(zfs_handle_t *zhp, void *data)
2890 list_cbdata_t *cbp = data;
2892 if (cbp->cb_first) {
2893 if (!cbp->cb_scripted)
2894 print_header(cbp->cb_proplist);
2895 cbp->cb_first = B_FALSE;
2898 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2904 zfs_do_list(int argc, char **argv)
2907 boolean_t scripted = B_FALSE;
2908 static char default_fields[] =
2909 "name,used,available,referenced,mountpoint";
2910 int types = ZFS_TYPE_DATASET;
2911 boolean_t types_specified = B_FALSE;
2912 char *fields = NULL;
2913 list_cbdata_t cb = { 0 };
2917 zfs_sort_column_t *sortcol = NULL;
2918 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2921 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2927 limit = parse_depth(optarg, &flags);
2930 flags |= ZFS_ITER_RECURSE;
2936 if (zfs_add_sort_column(&sortcol, optarg,
2938 (void) fprintf(stderr,
2939 gettext("invalid property '%s'\n"), optarg);
2944 if (zfs_add_sort_column(&sortcol, optarg,
2946 (void) fprintf(stderr,
2947 gettext("invalid property '%s'\n"), optarg);
2953 types_specified = B_TRUE;
2954 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2955 while (*optarg != '\0') {
2956 static char *type_subopts[] = { "filesystem",
2957 "volume", "snapshot", "snap", "all", NULL };
2959 switch (getsubopt(&optarg, type_subopts,
2962 types |= ZFS_TYPE_FILESYSTEM;
2965 types |= ZFS_TYPE_VOLUME;
2969 types |= ZFS_TYPE_SNAPSHOT;
2972 types = ZFS_TYPE_DATASET;
2976 (void) fprintf(stderr,
2977 gettext("invalid type '%s'\n"),
2984 (void) fprintf(stderr, gettext("missing argument for "
2985 "'%c' option\n"), optopt);
2989 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2999 fields = default_fields;
3002 * If we are only going to list snapshot names and sort by name,
3003 * then we can use faster version.
3005 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3006 flags |= ZFS_ITER_SIMPLE;
3009 * If "-o space" and no types were specified, don't display snapshots.
3011 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3012 types &= ~ZFS_TYPE_SNAPSHOT;
3015 * If the user specifies '-o all', the zprop_get_list() doesn't
3016 * normally include the name of the dataset. For 'zfs list', we always
3017 * want this property to be first.
3019 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3023 cb.cb_scripted = scripted;
3024 cb.cb_first = B_TRUE;
3026 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3027 limit, list_callback, &cb);
3029 zprop_free_list(cb.cb_proplist);
3030 zfs_free_sort_columns(sortcol);
3032 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3033 (void) fprintf(stderr, gettext("no datasets available\n"));
3039 * zfs rename <fs | snap | vol> <fs | snap | vol>
3040 * zfs rename -p <fs | vol> <fs | vol>
3041 * zfs rename -r <snap> <snap>
3043 * Renames the given dataset to another of the same type.
3045 * The '-p' flag creates all the non-existing ancestors of the target first.
3049 zfs_do_rename(int argc, char **argv)
3054 boolean_t recurse = B_FALSE;
3055 boolean_t parents = B_FALSE;
3058 while ((c = getopt(argc, argv, "pr")) != -1) {
3068 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3077 /* check number of arguments */
3079 (void) fprintf(stderr, gettext("missing source dataset "
3084 (void) fprintf(stderr, gettext("missing target dataset "
3089 (void) fprintf(stderr, gettext("too many arguments\n"));
3093 if (recurse && parents) {
3094 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3099 if (recurse && strchr(argv[0], '@') == 0) {
3100 (void) fprintf(stderr, gettext("source dataset for recursive "
3101 "rename must be a snapshot\n"));
3105 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3106 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3109 /* If we were asked and the name looks good, try to create ancestors. */
3110 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3111 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3116 ret = (zfs_rename(zhp, argv[1], recurse) != 0);
3125 * Promotes the given clone fs to be the parent
3129 zfs_do_promote(int argc, char **argv)
3135 if (argc > 1 && argv[1][0] == '-') {
3136 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3141 /* check number of arguments */
3143 (void) fprintf(stderr, gettext("missing clone filesystem"
3148 (void) fprintf(stderr, gettext("too many arguments\n"));
3152 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3156 ret = (zfs_promote(zhp) != 0);
3164 * zfs rollback [-rRf] <snapshot>
3166 * -r Delete any intervening snapshots before doing rollback
3167 * -R Delete any snapshots and their clones
3168 * -f ignored for backwards compatability
3170 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3171 * since then and making it the active dataset. If more recent snapshots exist,
3172 * the command will complain unless the '-r' flag is given.
3174 typedef struct rollback_cbdata {
3180 boolean_t cb_recurse;
3181 boolean_t cb_dependent;
3182 } rollback_cbdata_t;
3185 * Report any snapshots more recent than the one specified. Used when '-r' is
3186 * not specified. We reuse this same callback for the snapshot dependents - if
3187 * 'cb_dependent' is set, then this is a dependent and we should report it
3188 * without checking the transaction group.
3191 rollback_check(zfs_handle_t *zhp, void *data)
3193 rollback_cbdata_t *cbp = data;
3195 if (cbp->cb_doclones) {
3200 if (!cbp->cb_dependent) {
3201 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
3202 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3203 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3206 if (cbp->cb_first && !cbp->cb_recurse) {
3207 (void) fprintf(stderr, gettext("cannot "
3208 "rollback to '%s': more recent snapshots "
3211 (void) fprintf(stderr, gettext("use '-r' to "
3212 "force deletion of the following "
3218 if (cbp->cb_recurse) {
3219 cbp->cb_dependent = B_TRUE;
3220 if (zfs_iter_dependents(zhp, B_TRUE,
3221 rollback_check, cbp) != 0) {
3225 cbp->cb_dependent = B_FALSE;
3227 (void) fprintf(stderr, "%s\n",
3232 if (cbp->cb_first && cbp->cb_recurse) {
3233 (void) fprintf(stderr, gettext("cannot rollback to "
3234 "'%s': clones of previous snapshots exist\n"),
3236 (void) fprintf(stderr, gettext("use '-R' to "
3237 "force deletion of the following clones and "
3243 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3251 zfs_do_rollback(int argc, char **argv)
3255 boolean_t force = B_FALSE;
3256 rollback_cbdata_t cb = { 0 };
3257 zfs_handle_t *zhp, *snap;
3258 char parentname[ZFS_MAXNAMELEN];
3262 while ((c = getopt(argc, argv, "rRf")) != -1) {
3275 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3284 /* check number of arguments */
3286 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3290 (void) fprintf(stderr, gettext("too many arguments\n"));
3294 /* open the snapshot */
3295 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3298 /* open the parent dataset */
3299 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3300 verify((delim = strrchr(parentname, '@')) != NULL);
3302 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3308 * Check for more recent snapshots and/or clones based on the presence
3311 cb.cb_target = argv[0];
3312 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3313 cb.cb_first = B_TRUE;
3315 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
3318 if ((ret = cb.cb_error) != 0)
3322 * Rollback parent to the given snapshot.
3324 ret = zfs_rollback(zhp, snap, force);
3337 * zfs set property=value { fs | snap | vol } ...
3339 * Sets the given property for all datasets specified on the command line.
3341 typedef struct set_cbdata {
3347 set_callback(zfs_handle_t *zhp, void *data)
3349 set_cbdata_t *cbp = data;
3351 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3352 switch (libzfs_errno(g_zfs)) {
3353 case EZFS_MOUNTFAILED:
3354 (void) fprintf(stderr, gettext("property may be set "
3355 "but unable to remount filesystem\n"));
3357 case EZFS_SHARENFSFAILED:
3358 (void) fprintf(stderr, gettext("property may be set "
3359 "but unable to reshare filesystem\n"));
3368 zfs_do_set(int argc, char **argv)
3373 /* check for options */
3374 if (argc > 1 && argv[1][0] == '-') {
3375 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3380 /* check number of arguments */
3382 (void) fprintf(stderr, gettext("missing property=value "
3387 (void) fprintf(stderr, gettext("missing dataset name\n"));
3391 /* validate property=value argument */
3392 cb.cb_propname = argv[1];
3393 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3394 (cb.cb_value[1] == '\0')) {
3395 (void) fprintf(stderr, gettext("missing value in "
3396 "property=value argument\n"));
3400 *cb.cb_value = '\0';
3403 if (*cb.cb_propname == '\0') {
3404 (void) fprintf(stderr,
3405 gettext("missing property in property=value argument\n"));
3409 ret = zfs_for_each(argc - 2, argv + 2, 0,
3410 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3416 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3418 * Creates a snapshot with the given name. While functionally equivalent to
3419 * 'zfs create', it is a separate command to differentiate intent.
3422 zfs_do_snapshot(int argc, char **argv)
3424 boolean_t recursive = B_FALSE;
3429 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3433 while ((c = getopt(argc, argv, "ro:")) != -1) {
3436 if (parseprop(props))
3443 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3452 /* check number of arguments */
3454 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3458 (void) fprintf(stderr, gettext("too many arguments\n"));
3462 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
3464 if (ret && recursive)
3465 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3475 * Send a backup stream to stdout.
3478 zfs_do_send(int argc, char **argv)
3480 char *fromname = NULL;
3481 char *toname = NULL;
3484 sendflags_t flags = { 0 };
3486 nvlist_t *dbgnv = NULL;
3487 boolean_t extraverbose = B_FALSE;
3490 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3501 flags.doall = B_TRUE;
3504 flags.replicate = B_TRUE;
3507 flags.props = B_TRUE;
3510 flags.parsable = B_TRUE;
3511 flags.verbose = B_TRUE;
3515 extraverbose = B_TRUE;
3516 flags.verbose = B_TRUE;
3519 flags.dedup = B_TRUE;
3522 flags.dryrun = B_TRUE;
3525 (void) fprintf(stderr, gettext("missing argument for "
3526 "'%c' option\n"), optopt);
3530 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3539 /* check number of arguments */
3541 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3545 (void) fprintf(stderr, gettext("too many arguments\n"));
3549 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3550 (void) fprintf(stderr,
3551 gettext("Error: Stream can not be written to a terminal.\n"
3552 "You must redirect standard output.\n"));
3556 cp = strchr(argv[0], '@');
3558 (void) fprintf(stderr,
3559 gettext("argument must be a snapshot\n"));
3564 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3569 * If they specified the full path to the snapshot, chop off
3570 * everything except the short name of the snapshot, but special
3571 * case if they specify the origin.
3573 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3574 char origin[ZFS_MAXNAMELEN];
3577 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3578 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3580 if (strcmp(origin, fromname) == 0) {
3582 flags.fromorigin = B_TRUE;
3585 if (cp != fromname && strcmp(argv[0], fromname)) {
3586 (void) fprintf(stderr,
3587 gettext("incremental source must be "
3588 "in same filesystem\n"));
3592 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3593 (void) fprintf(stderr,
3594 gettext("invalid incremental source\n"));
3600 if (flags.replicate && fromname == NULL)
3601 flags.doall = B_TRUE;
3603 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3604 extraverbose ? &dbgnv : NULL);
3606 if (extraverbose && dbgnv != NULL) {
3608 * dump_nvlist prints to stdout, but that's been
3609 * redirected to a file. Make it print to stderr
3612 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3613 dump_nvlist(dbgnv, 0);
3622 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3624 * Restore a backup stream from stdin.
3627 zfs_do_receive(int argc, char **argv)
3630 recvflags_t flags = { 0 };
3633 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3636 flags.isprefix = B_TRUE;
3639 flags.isprefix = B_TRUE;
3640 flags.istail = B_TRUE;
3643 flags.dryrun = B_TRUE;
3646 flags.nomount = B_TRUE;
3649 flags.verbose = B_TRUE;
3652 flags.force = B_TRUE;
3655 (void) fprintf(stderr, gettext("missing argument for "
3656 "'%c' option\n"), optopt);
3660 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3669 /* check number of arguments */
3671 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3675 (void) fprintf(stderr, gettext("too many arguments\n"));
3679 if (isatty(STDIN_FILENO)) {
3680 (void) fprintf(stderr,
3681 gettext("Error: Backup stream can not be read "
3682 "from a terminal.\n"
3683 "You must redirect standard input.\n"));
3687 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3693 * allow/unallow stuff
3695 /* copied from zfs/sys/dsl_deleg.h */
3696 #define ZFS_DELEG_PERM_CREATE "create"
3697 #define ZFS_DELEG_PERM_DESTROY "destroy"
3698 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3699 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3700 #define ZFS_DELEG_PERM_CLONE "clone"
3701 #define ZFS_DELEG_PERM_PROMOTE "promote"
3702 #define ZFS_DELEG_PERM_RENAME "rename"
3703 #define ZFS_DELEG_PERM_MOUNT "mount"
3704 #define ZFS_DELEG_PERM_SHARE "share"
3705 #define ZFS_DELEG_PERM_SEND "send"
3706 #define ZFS_DELEG_PERM_RECEIVE "receive"
3707 #define ZFS_DELEG_PERM_ALLOW "allow"
3708 #define ZFS_DELEG_PERM_USERPROP "userprop"
3709 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3710 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3711 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3712 #define ZFS_DELEG_PERM_USERUSED "userused"
3713 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3714 #define ZFS_DELEG_PERM_HOLD "hold"
3715 #define ZFS_DELEG_PERM_RELEASE "release"
3716 #define ZFS_DELEG_PERM_DIFF "diff"
3718 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3720 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3721 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3722 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3723 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3724 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3725 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3726 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3727 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3728 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3729 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3730 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3731 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3732 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3733 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3734 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3735 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3737 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3738 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3739 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3740 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3741 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3742 { NULL, ZFS_DELEG_NOTE_NONE }
3745 /* permission structure */
3746 typedef struct deleg_perm {
3747 zfs_deleg_who_type_t dp_who_type;
3748 const char *dp_name;
3750 boolean_t dp_descend;
3754 typedef struct deleg_perm_node {
3755 deleg_perm_t dpn_perm;
3757 uu_avl_node_t dpn_avl_node;
3758 } deleg_perm_node_t;
3760 typedef struct fs_perm fs_perm_t;
3762 /* permissions set */
3763 typedef struct who_perm {
3764 zfs_deleg_who_type_t who_type;
3765 const char *who_name; /* id */
3766 char who_ug_name[256]; /* user/group name */
3767 fs_perm_t *who_fsperm; /* uplink */
3769 uu_avl_t *who_deleg_perm_avl; /* permissions */
3773 typedef struct who_perm_node {
3774 who_perm_t who_perm;
3775 uu_avl_node_t who_avl_node;
3778 typedef struct fs_perm_set fs_perm_set_t;
3779 /* fs permissions */
3781 const char *fsp_name;
3783 uu_avl_t *fsp_sc_avl; /* sets,create */
3784 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3786 fs_perm_set_t *fsp_set; /* uplink */
3790 typedef struct fs_perm_node {
3791 fs_perm_t fspn_fsperm;
3794 uu_list_node_t fspn_list_node;
3797 /* top level structure */
3798 struct fs_perm_set {
3799 uu_list_pool_t *fsps_list_pool;
3800 uu_list_t *fsps_list; /* list of fs_perms */
3802 uu_avl_pool_t *fsps_named_set_avl_pool;
3803 uu_avl_pool_t *fsps_who_perm_avl_pool;
3804 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
3807 static inline const char *
3808 deleg_perm_type(zfs_deleg_note_t note)
3814 case ZFS_DELEG_NOTE_GROUPQUOTA:
3815 case ZFS_DELEG_NOTE_GROUPUSED:
3816 case ZFS_DELEG_NOTE_USERPROP:
3817 case ZFS_DELEG_NOTE_USERQUOTA:
3818 case ZFS_DELEG_NOTE_USERUSED:
3820 return (gettext("other"));
3822 return (gettext("subcommand"));
3827 who_type2weight(zfs_deleg_who_type_t who_type)
3831 case ZFS_DELEG_NAMED_SET_SETS:
3832 case ZFS_DELEG_NAMED_SET:
3835 case ZFS_DELEG_CREATE_SETS:
3836 case ZFS_DELEG_CREATE:
3839 case ZFS_DELEG_USER_SETS:
3840 case ZFS_DELEG_USER:
3843 case ZFS_DELEG_GROUP_SETS:
3844 case ZFS_DELEG_GROUP:
3847 case ZFS_DELEG_EVERYONE_SETS:
3848 case ZFS_DELEG_EVERYONE:
3860 who_perm_compare(const void *larg, const void *rarg, void *unused)
3862 const who_perm_node_t *l = larg;
3863 const who_perm_node_t *r = rarg;
3864 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
3865 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
3866 int lweight = who_type2weight(ltype);
3867 int rweight = who_type2weight(rtype);
3868 int res = lweight - rweight;
3870 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
3871 ZFS_MAX_DELEG_NAME-1);
3883 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
3885 const deleg_perm_node_t *l = larg;
3886 const deleg_perm_node_t *r = rarg;
3887 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
3888 ZFS_MAX_DELEG_NAME-1);
3900 fs_perm_set_init(fs_perm_set_t *fspset)
3902 bzero(fspset, sizeof (fs_perm_set_t));
3904 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
3905 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
3906 NULL, UU_DEFAULT)) == NULL)
3908 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
3909 UU_DEFAULT)) == NULL)
3912 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
3913 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
3914 who_perm_node_t, who_avl_node), who_perm_compare,
3915 UU_DEFAULT)) == NULL)
3918 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
3919 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
3920 who_perm_node_t, who_avl_node), who_perm_compare,
3921 UU_DEFAULT)) == NULL)
3924 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
3925 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
3926 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
3931 static inline void fs_perm_fini(fs_perm_t *);
3932 static inline void who_perm_fini(who_perm_t *);
3935 fs_perm_set_fini(fs_perm_set_t *fspset)
3937 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
3939 while (node != NULL) {
3940 fs_perm_node_t *next_node =
3941 uu_list_next(fspset->fsps_list, node);
3942 fs_perm_t *fsperm = &node->fspn_fsperm;
3943 fs_perm_fini(fsperm);
3944 uu_list_remove(fspset->fsps_list, node);
3949 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
3950 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
3951 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
3955 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
3958 deleg_perm->dp_who_type = type;
3959 deleg_perm->dp_name = name;
3963 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
3964 zfs_deleg_who_type_t type, const char *name)
3966 uu_avl_pool_t *pool;
3967 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
3969 bzero(who_perm, sizeof (who_perm_t));
3971 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
3972 UU_DEFAULT)) == NULL)
3975 who_perm->who_type = type;
3976 who_perm->who_name = name;
3977 who_perm->who_fsperm = fsperm;
3981 who_perm_fini(who_perm_t *who_perm)
3983 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
3985 while (node != NULL) {
3986 deleg_perm_node_t *next_node =
3987 uu_avl_next(who_perm->who_deleg_perm_avl, node);
3989 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
3994 uu_avl_destroy(who_perm->who_deleg_perm_avl);
3998 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4000 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4001 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4003 bzero(fsperm, sizeof (fs_perm_t));
4005 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4009 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4013 fsperm->fsp_set = fspset;
4014 fsperm->fsp_name = fsname;
4018 fs_perm_fini(fs_perm_t *fsperm)
4020 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4021 while (node != NULL) {
4022 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4024 who_perm_t *who_perm = &node->who_perm;
4025 who_perm_fini(who_perm);
4026 uu_avl_remove(fsperm->fsp_sc_avl, node);
4031 node = uu_avl_first(fsperm->fsp_uge_avl);
4032 while (node != NULL) {
4033 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4035 who_perm_t *who_perm = &node->who_perm;
4036 who_perm_fini(who_perm);
4037 uu_avl_remove(fsperm->fsp_uge_avl, node);
4042 uu_avl_destroy(fsperm->fsp_sc_avl);
4043 uu_avl_destroy(fsperm->fsp_uge_avl);
4047 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4048 zfs_deleg_who_type_t who_type, const char *name, char locality)
4050 uu_avl_index_t idx = 0;
4052 deleg_perm_node_t *found_node = NULL;
4053 deleg_perm_t *deleg_perm = &node->dpn_perm;
4055 deleg_perm_init(deleg_perm, who_type, name);
4057 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4059 uu_avl_insert(avl, node, idx);
4062 deleg_perm = &node->dpn_perm;
4067 case ZFS_DELEG_LOCAL:
4068 deleg_perm->dp_local = B_TRUE;
4070 case ZFS_DELEG_DESCENDENT:
4071 deleg_perm->dp_descend = B_TRUE;
4076 assert(B_FALSE); /* invalid locality */
4081 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4083 nvpair_t *nvp = NULL;
4084 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4085 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4086 zfs_deleg_who_type_t who_type = who_perm->who_type;
4088 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4089 const char *name = nvpair_name(nvp);
4090 data_type_t type = nvpair_type(nvp);
4091 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4092 deleg_perm_node_t *node =
4093 safe_malloc(sizeof (deleg_perm_node_t));
4095 VERIFY(type == DATA_TYPE_BOOLEAN);
4097 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4098 set_deleg_perm_node(avl, node, who_type, name, locality);
4105 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4107 nvpair_t *nvp = NULL;
4108 fs_perm_set_t *fspset = fsperm->fsp_set;
4110 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4111 nvlist_t *nvl2 = NULL;
4112 const char *name = nvpair_name(nvp);
4113 uu_avl_t *avl = NULL;
4114 uu_avl_pool_t *avl_pool = NULL;
4115 zfs_deleg_who_type_t perm_type = name[0];
4116 char perm_locality = name[1];
4117 const char *perm_name = name + 3;
4118 boolean_t is_set = B_TRUE;
4119 who_perm_t *who_perm = NULL;
4121 assert('$' == name[2]);
4123 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4126 switch (perm_type) {
4127 case ZFS_DELEG_CREATE:
4128 case ZFS_DELEG_CREATE_SETS:
4129 case ZFS_DELEG_NAMED_SET:
4130 case ZFS_DELEG_NAMED_SET_SETS:
4131 avl_pool = fspset->fsps_named_set_avl_pool;
4132 avl = fsperm->fsp_sc_avl;
4134 case ZFS_DELEG_USER:
4135 case ZFS_DELEG_USER_SETS:
4136 case ZFS_DELEG_GROUP:
4137 case ZFS_DELEG_GROUP_SETS:
4138 case ZFS_DELEG_EVERYONE:
4139 case ZFS_DELEG_EVERYONE_SETS:
4140 avl_pool = fspset->fsps_who_perm_avl_pool;
4141 avl = fsperm->fsp_uge_avl;
4148 who_perm_node_t *found_node = NULL;
4149 who_perm_node_t *node = safe_malloc(
4150 sizeof (who_perm_node_t));
4151 who_perm = &node->who_perm;
4152 uu_avl_index_t idx = 0;
4154 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4155 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4157 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4159 if (avl == fsperm->fsp_uge_avl) {
4161 struct passwd *p = NULL;
4162 struct group *g = NULL;
4163 const char *nice_name = NULL;
4165 switch (perm_type) {
4166 case ZFS_DELEG_USER_SETS:
4167 case ZFS_DELEG_USER:
4168 rid = atoi(perm_name);
4171 nice_name = p->pw_name;
4173 case ZFS_DELEG_GROUP_SETS:
4174 case ZFS_DELEG_GROUP:
4175 rid = atoi(perm_name);
4178 nice_name = g->gr_name;
4184 if (nice_name != NULL)
4186 node->who_perm.who_ug_name,
4190 uu_avl_insert(avl, node, idx);
4193 who_perm = &node->who_perm;
4197 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4204 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4206 nvpair_t *nvp = NULL;
4207 uu_avl_index_t idx = 0;
4209 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4210 nvlist_t *nvl2 = NULL;
4211 const char *fsname = nvpair_name(nvp);
4212 data_type_t type = nvpair_type(nvp);
4213 fs_perm_t *fsperm = NULL;
4214 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4218 fsperm = &node->fspn_fsperm;
4220 VERIFY(DATA_TYPE_NVLIST == type);
4222 uu_list_node_init(node, &node->fspn_list_node,
4223 fspset->fsps_list_pool);
4225 idx = uu_list_numnodes(fspset->fsps_list);
4226 fs_perm_init(fsperm, fspset, fsname);
4228 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4231 (void) parse_fs_perm(fsperm, nvl2);
4233 uu_list_insert(fspset->fsps_list, node, idx);
4239 static inline const char *
4240 deleg_perm_comment(zfs_deleg_note_t note)
4242 const char *str = "";
4247 case ZFS_DELEG_NOTE_ALLOW:
4248 str = gettext("Must also have the permission that is being"
4249 "\n\t\t\t\tallowed");
4251 case ZFS_DELEG_NOTE_CLONE:
4252 str = gettext("Must also have the 'create' ability and 'mount'"
4253 "\n\t\t\t\tability in the origin file system");
4255 case ZFS_DELEG_NOTE_CREATE:
4256 str = gettext("Must also have the 'mount' ability");
4258 case ZFS_DELEG_NOTE_DESTROY:
4259 str = gettext("Must also have the 'mount' ability");
4261 case ZFS_DELEG_NOTE_DIFF:
4262 str = gettext("Allows lookup of paths within a dataset;"
4263 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4264 "\n\t\t\t\tin order to use zfs diff");
4266 case ZFS_DELEG_NOTE_HOLD:
4267 str = gettext("Allows adding a user hold to a snapshot");
4269 case ZFS_DELEG_NOTE_MOUNT:
4270 str = gettext("Allows mount/umount of ZFS datasets");
4272 case ZFS_DELEG_NOTE_PROMOTE:
4273 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4274 " 'promote' ability in the origin file system");
4276 case ZFS_DELEG_NOTE_RECEIVE:
4277 str = gettext("Must also have the 'mount' and 'create'"
4280 case ZFS_DELEG_NOTE_RELEASE:
4281 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4282 "might destroy the snapshot");
4284 case ZFS_DELEG_NOTE_RENAME:
4285 str = gettext("Must also have the 'mount' and 'create'"
4286 "\n\t\t\t\tability in the new parent");
4288 case ZFS_DELEG_NOTE_ROLLBACK:
4291 case ZFS_DELEG_NOTE_SEND:
4294 case ZFS_DELEG_NOTE_SHARE:
4295 str = gettext("Allows sharing file systems over NFS or SMB"
4296 "\n\t\t\t\tprotocols");
4298 case ZFS_DELEG_NOTE_SNAPSHOT:
4302 * case ZFS_DELEG_NOTE_VSCAN:
4303 * str = gettext("");
4307 case ZFS_DELEG_NOTE_GROUPQUOTA:
4308 str = gettext("Allows accessing any groupquota@... property");
4310 case ZFS_DELEG_NOTE_GROUPUSED:
4311 str = gettext("Allows reading any groupused@... property");
4313 case ZFS_DELEG_NOTE_USERPROP:
4314 str = gettext("Allows changing any user property");
4316 case ZFS_DELEG_NOTE_USERQUOTA:
4317 str = gettext("Allows accessing any userquota@... property");
4319 case ZFS_DELEG_NOTE_USERUSED:
4320 str = gettext("Allows reading any userused@... property");
4338 boolean_t recursive; /* unallow only */
4339 boolean_t prt_usage;
4341 boolean_t prt_perms;
4344 const char *dataset;
4348 prop_cmp(const void *a, const void *b)
4350 const char *str1 = *(const char **)a;
4351 const char *str2 = *(const char **)b;
4352 return (strcmp(str1, str2));
4356 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4358 const char *opt_desc[] = {
4359 "-h", gettext("show this help message and exit"),
4360 "-l", gettext("set permission locally"),
4361 "-d", gettext("set permission for descents"),
4362 "-u", gettext("set permission for user"),
4363 "-g", gettext("set permission for group"),
4364 "-e", gettext("set permission for everyone"),
4365 "-c", gettext("set create time permission"),
4366 "-s", gettext("define permission set"),
4368 "-r", gettext("remove permissions recursively"),
4370 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4371 size_t allow_size = unallow_size - 2;
4372 const char *props[ZFS_NUM_PROPS];
4375 FILE *fp = requested ? stdout : stderr;
4376 zprop_desc_t *pdtbl = zfs_prop_get_table();
4377 const char *fmt = gettext("%-16s %-14s\t%s\n");
4379 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4381 (void) fprintf(fp, gettext("Options:\n"));
4382 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4383 const char *opt = opt_desc[i++];
4384 const char *optdsc = opt_desc[i];
4385 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4388 (void) fprintf(fp, gettext("\nThe following permissions are "
4390 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4392 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4393 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4394 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4395 const char *perm_type = deleg_perm_type(perm_note);
4396 const char *perm_comment = deleg_perm_comment(perm_note);
4397 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4400 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4401 zprop_desc_t *pd = &pdtbl[i];
4402 if (pd->pd_visible != B_TRUE)
4405 if (pd->pd_attr == PROP_READONLY)
4408 props[count++] = pd->pd_name;
4410 props[count] = NULL;
4412 qsort(props, count, sizeof (char *), prop_cmp);
4414 for (i = 0; i < count; i++)
4415 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4418 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4420 exit(requested ? 0 : 2);
4423 static inline const char *
4424 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4427 if (un && argc == expected_argc - 1)
4429 else if (argc == expected_argc)
4430 *permsp = argv[argc - 2];
4432 allow_usage(un, B_FALSE,
4433 gettext("wrong number of parameters\n"));
4435 return (argv[argc - 1]);
4439 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4441 int uge_sum = opts->user + opts->group + opts->everyone;
4442 int csuge_sum = opts->create + opts->set + uge_sum;
4443 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4444 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4447 allow_usage(un, B_FALSE,
4448 gettext("-u, -g, and -e are mutually exclusive\n"));
4450 if (opts->prt_usage) {
4451 if (argc == 0 && all_sum == 0)
4452 allow_usage(un, B_TRUE, NULL);
4459 allow_usage(un, B_FALSE,
4460 gettext("invalid options combined with -s\n"));
4462 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4463 if (argv[0][0] != '@')
4464 allow_usage(un, B_FALSE,
4465 gettext("invalid set name: missing '@' prefix\n"));
4466 opts->who = argv[0];
4467 } else if (opts->create) {
4468 if (ldcsuge_sum > 1)
4469 allow_usage(un, B_FALSE,
4470 gettext("invalid options combined with -c\n"));
4471 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4472 } else if (opts->everyone) {
4474 allow_usage(un, B_FALSE,
4475 gettext("invalid options combined with -e\n"));
4476 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4477 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4479 opts->everyone = B_TRUE;
4482 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4483 } else if (argc == 1 && !un) {
4484 opts->prt_perms = B_TRUE;
4485 opts->dataset = argv[argc-1];
4487 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4488 opts->who = argv[0];
4491 if (!opts->local && !opts->descend) {
4492 opts->local = B_TRUE;
4493 opts->descend = B_TRUE;
4498 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4499 const char *who, char *perms, nvlist_t *top_nvl)
4502 char ld[2] = { '\0', '\0' };
4503 char who_buf[ZFS_MAXNAMELEN+32];
4504 char base_type = ZFS_DELEG_WHO_UNKNOWN;
4505 char set_type = ZFS_DELEG_WHO_UNKNOWN;
4506 nvlist_t *base_nvl = NULL;
4507 nvlist_t *set_nvl = NULL;
4510 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4512 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4516 case ZFS_DELEG_NAMED_SET_SETS:
4517 case ZFS_DELEG_NAMED_SET:
4518 set_type = ZFS_DELEG_NAMED_SET_SETS;
4519 base_type = ZFS_DELEG_NAMED_SET;
4520 ld[0] = ZFS_DELEG_NA;
4522 case ZFS_DELEG_CREATE_SETS:
4523 case ZFS_DELEG_CREATE:
4524 set_type = ZFS_DELEG_CREATE_SETS;
4525 base_type = ZFS_DELEG_CREATE;
4526 ld[0] = ZFS_DELEG_NA;
4528 case ZFS_DELEG_USER_SETS:
4529 case ZFS_DELEG_USER:
4530 set_type = ZFS_DELEG_USER_SETS;
4531 base_type = ZFS_DELEG_USER;
4533 ld[0] = ZFS_DELEG_LOCAL;
4535 ld[1] = ZFS_DELEG_DESCENDENT;
4537 case ZFS_DELEG_GROUP_SETS:
4538 case ZFS_DELEG_GROUP:
4539 set_type = ZFS_DELEG_GROUP_SETS;
4540 base_type = ZFS_DELEG_GROUP;
4542 ld[0] = ZFS_DELEG_LOCAL;
4544 ld[1] = ZFS_DELEG_DESCENDENT;
4546 case ZFS_DELEG_EVERYONE_SETS:
4547 case ZFS_DELEG_EVERYONE:
4548 set_type = ZFS_DELEG_EVERYONE_SETS;
4549 base_type = ZFS_DELEG_EVERYONE;
4551 ld[0] = ZFS_DELEG_LOCAL;
4553 ld[1] = ZFS_DELEG_DESCENDENT;
4558 if (perms != NULL) {
4560 char *end = curr + strlen(perms);
4562 while (curr < end) {
4563 char *delim = strchr(curr, ',');
4574 (void) nvlist_add_boolean(nvl, curr);
4580 for (i = 0; i < 2; i++) {
4581 char locality = ld[i];
4585 if (!nvlist_empty(base_nvl)) {
4587 (void) snprintf(who_buf,
4588 sizeof (who_buf), "%c%c$%s",
4589 base_type, locality, who);
4591 (void) snprintf(who_buf,
4592 sizeof (who_buf), "%c%c$",
4593 base_type, locality);
4595 (void) nvlist_add_nvlist(top_nvl, who_buf,
4600 if (!nvlist_empty(set_nvl)) {
4602 (void) snprintf(who_buf,
4603 sizeof (who_buf), "%c%c$%s",
4604 set_type, locality, who);
4606 (void) snprintf(who_buf,
4607 sizeof (who_buf), "%c%c$",
4608 set_type, locality);
4610 (void) nvlist_add_nvlist(top_nvl, who_buf,
4615 for (i = 0; i < 2; i++) {
4616 char locality = ld[i];
4621 (void) snprintf(who_buf, sizeof (who_buf),
4622 "%c%c$%s", base_type, locality, who);
4624 (void) snprintf(who_buf, sizeof (who_buf),
4625 "%c%c$", base_type, locality);
4626 (void) nvlist_add_boolean(top_nvl, who_buf);
4629 (void) snprintf(who_buf, sizeof (who_buf),
4630 "%c%c$%s", set_type, locality, who);
4632 (void) snprintf(who_buf, sizeof (who_buf),
4633 "%c%c$", set_type, locality);
4634 (void) nvlist_add_boolean(top_nvl, who_buf);
4640 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4642 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4646 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4647 opts->descend, opts->who, opts->perms, *nvlp);
4648 } else if (opts->create) {
4649 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4650 opts->descend, NULL, opts->perms, *nvlp);
4651 } else if (opts->everyone) {
4652 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4653 opts->descend, NULL, opts->perms, *nvlp);
4655 char *curr = opts->who;
4656 char *end = curr + strlen(curr);
4658 while (curr < end) {
4660 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
4662 char *delim = strchr(curr, ',');
4665 struct passwd *p = NULL;
4666 struct group *g = NULL;
4674 rid = (uid_t)strtol(curr, &endch, 0);
4676 who_type = ZFS_DELEG_USER;
4685 (void) snprintf(errbuf, 256, gettext(
4686 "invalid user %s"), curr);
4687 allow_usage(un, B_TRUE, errbuf);
4689 } else if (opts->group) {
4690 who_type = ZFS_DELEG_GROUP;
4699 (void) snprintf(errbuf, 256, gettext(
4700 "invalid group %s"), curr);
4701 allow_usage(un, B_TRUE, errbuf);
4704 if (*endch != '\0') {
4711 if (*endch != '\0') {
4719 who_type = ZFS_DELEG_USER;
4721 } else if (g != NULL) {
4722 who_type = ZFS_DELEG_GROUP;
4725 (void) snprintf(errbuf, 256, gettext(
4726 "invalid user/group %s"), curr);
4727 allow_usage(un, B_TRUE, errbuf);
4731 (void) sprintf(id, "%u", rid);
4734 store_allow_perm(who_type, opts->local,
4735 opts->descend, who, opts->perms, *nvlp);
4744 print_set_creat_perms(uu_avl_t *who_avl)
4746 const char *sc_title[] = {
4747 gettext("Permission sets:\n"),
4748 gettext("Create time permissions:\n"),
4751 const char **title_ptr = sc_title;
4752 who_perm_node_t *who_node = NULL;
4753 int prev_weight = -1;
4755 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4756 who_node = uu_avl_next(who_avl, who_node)) {
4757 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4758 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4759 const char *who_name = who_node->who_perm.who_name;
4760 int weight = who_type2weight(who_type);
4761 boolean_t first = B_TRUE;
4762 deleg_perm_node_t *deleg_node;
4764 if (prev_weight != weight) {
4765 (void) printf("%s", *title_ptr++);
4766 prev_weight = weight;
4769 if (who_name == NULL || strnlen(who_name, 1) == 0)
4770 (void) printf("\t");
4772 (void) printf("\t%s ", who_name);
4774 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4775 deleg_node = uu_avl_next(avl, deleg_node)) {
4778 deleg_node->dpn_perm.dp_name);
4781 (void) printf(",%s",
4782 deleg_node->dpn_perm.dp_name);
4785 (void) printf("\n");
4790 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4793 who_perm_node_t *who_node = NULL;
4794 boolean_t prt_title = B_TRUE;
4795 uu_avl_walk_t *walk;
4797 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4800 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4801 const char *who_name = who_node->who_perm.who_name;
4802 const char *nice_who_name = who_node->who_perm.who_ug_name;
4803 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4804 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4806 deleg_perm_node_t *deleg_node;
4807 boolean_t prt_who = B_TRUE;
4809 for (deleg_node = uu_avl_first(avl);
4811 deleg_node = uu_avl_next(avl, deleg_node)) {
4812 if (local != deleg_node->dpn_perm.dp_local ||
4813 descend != deleg_node->dpn_perm.dp_descend)
4817 const char *who = NULL;
4819 prt_title = B_FALSE;
4820 (void) printf("%s", title);
4824 case ZFS_DELEG_USER_SETS:
4825 case ZFS_DELEG_USER:
4826 who = gettext("user");
4828 who_name = nice_who_name;
4830 case ZFS_DELEG_GROUP_SETS:
4831 case ZFS_DELEG_GROUP:
4832 who = gettext("group");
4834 who_name = nice_who_name;
4836 case ZFS_DELEG_EVERYONE_SETS:
4837 case ZFS_DELEG_EVERYONE:
4838 who = gettext("everyone");
4845 if (who_name == NULL)
4846 (void) printf("\t%s", who);
4848 (void) printf("\t%s %s", who, who_name);
4851 (void) printf("%c%s", delim,
4852 deleg_node->dpn_perm.dp_name);
4857 (void) printf("\n");
4860 uu_avl_walk_end(walk);
4864 print_fs_perms(fs_perm_set_t *fspset)
4866 fs_perm_node_t *node = NULL;
4867 char buf[ZFS_MAXNAMELEN+32];
4868 const char *dsname = buf;
4870 for (node = uu_list_first(fspset->fsps_list); node != NULL;
4871 node = uu_list_next(fspset->fsps_list, node)) {
4872 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
4873 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
4876 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
4877 gettext("---- Permissions on %s "),
4878 node->fspn_fsperm.fsp_name);
4879 (void) printf("%s", dsname);
4880 left = 70 - strlen(buf);
4883 (void) printf("\n");
4885 print_set_creat_perms(sc_avl);
4886 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
4887 gettext("Local permissions:\n"));
4888 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
4889 gettext("Descendent permissions:\n"));
4890 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
4891 gettext("Local+Descendent permissions:\n"));
4895 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
4897 struct deleg_perms {
4903 set_deleg_perms(zfs_handle_t *zhp, void *data)
4905 struct deleg_perms *perms = (struct deleg_perms *)data;
4906 zfs_type_t zfs_type = zfs_get_type(zhp);
4908 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
4911 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
4915 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
4918 nvlist_t *perm_nvl = NULL;
4919 nvlist_t *update_perm_nvl = NULL;
4922 struct allow_opts opts = { 0 };
4924 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
4927 while ((c = getopt(argc, argv, optstr)) != -1) {
4930 opts.local = B_TRUE;
4933 opts.descend = B_TRUE;
4939 opts.group = B_TRUE;
4942 opts.everyone = B_TRUE;
4948 opts.create = B_TRUE;
4951 opts.recursive = B_TRUE;
4954 (void) fprintf(stderr, gettext("missing argument for "
4955 "'%c' option\n"), optopt);
4959 opts.prt_usage = B_TRUE;
4962 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4971 /* check arguments */
4972 parse_allow_args(argc, argv, un, &opts);
4974 /* try to open the dataset */
4975 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
4976 ZFS_TYPE_VOLUME)) == NULL) {
4977 (void) fprintf(stderr, "Failed to open dataset: %s\n",
4982 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
4985 fs_perm_set_init(&fs_perm_set);
4986 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
4987 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
4992 print_fs_perms(&fs_perm_set);
4994 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
4995 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
4998 if (un && opts.recursive) {
4999 struct deleg_perms data = { un, update_perm_nvl };
5000 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5009 nvlist_free(perm_nvl);
5010 if (update_perm_nvl != NULL)
5011 nvlist_free(update_perm_nvl);
5013 fs_perm_set_fini(&fs_perm_set);
5021 * zfs allow [-r] [-t] <tag> <snap> ...
5023 * -r Recursively hold
5024 * -t Temporary hold (hidden option)
5026 * Apply a user-hold with the given tag to the list of snapshots.
5029 zfs_do_allow(int argc, char **argv)
5031 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5035 * zfs unallow [-r] [-t] <tag> <snap> ...
5037 * -r Recursively hold
5038 * -t Temporary hold (hidden option)
5040 * Apply a user-hold with the given tag to the list of snapshots.
5043 zfs_do_unallow(int argc, char **argv)
5045 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5049 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5054 boolean_t recursive = B_FALSE;
5055 boolean_t temphold = B_FALSE;
5056 const char *opts = holding ? "rt" : "r";
5060 while ((c = getopt(argc, argv, opts)) != -1) {
5069 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5078 /* check number of arguments */
5086 if (holding && tag[0] == '.') {
5087 /* tags starting with '.' are reserved for libzfs */
5088 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5092 for (i = 0; i < argc; ++i) {
5094 char parent[ZFS_MAXNAMELEN];
5096 char *path = argv[i];
5098 delim = strchr(path, '@');
5099 if (delim == NULL) {
5100 (void) fprintf(stderr,
5101 gettext("'%s' is not a snapshot\n"), path);
5105 (void) strncpy(parent, path, delim - path);
5106 parent[delim - path] = '\0';
5108 zhp = zfs_open(g_zfs, parent,
5109 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5115 if (zfs_hold(zhp, delim+1, tag, recursive,
5116 temphold, B_FALSE, -1, 0, 0) != 0)
5119 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5125 return (errors != 0);
5129 * zfs hold [-r] [-t] <tag> <snap> ...
5131 * -r Recursively hold
5132 * -t Temporary hold (hidden option)
5134 * Apply a user-hold with the given tag to the list of snapshots.
5137 zfs_do_hold(int argc, char **argv)
5139 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5143 * zfs release [-r] <tag> <snap> ...
5145 * -r Recursively release
5147 * Release a user-hold with the given tag from the list of snapshots.
5150 zfs_do_release(int argc, char **argv)
5152 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5155 typedef struct holds_cbdata {
5156 boolean_t cb_recursive;
5157 const char *cb_snapname;
5159 size_t cb_max_namelen;
5160 size_t cb_max_taglen;
5163 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5164 #define DATETIME_BUF_LEN (32)
5169 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
5172 nvpair_t *nvp = NULL;
5173 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5177 for (i = 0; i < 3; i++) {
5178 col = gettext(hdr_cols[i]);
5180 (void) printf("%-*s ", i ? tagwidth : nwidth,
5183 (void) printf("%s\n", col);
5187 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5188 char *zname = nvpair_name(nvp);
5190 nvpair_t *nvp2 = NULL;
5191 (void) nvpair_value_nvlist(nvp, &nvl2);
5192 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5193 char tsbuf[DATETIME_BUF_LEN];
5194 char *tagname = nvpair_name(nvp2);
5198 char sep = scripted ? '\t' : ' ';
5199 int sepnum = scripted ? 1 : 2;
5201 (void) nvpair_value_uint64(nvp2, &val);
5203 (void) localtime_r(&time, &t);
5204 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5205 gettext(STRFTIME_FMT_STR), &t);
5207 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5208 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5214 * Generic callback function to list a dataset or snapshot.
5217 holds_callback(zfs_handle_t *zhp, void *data)
5219 holds_cbdata_t *cbp = data;
5220 nvlist_t *top_nvl = *cbp->cb_nvlp;
5221 nvlist_t *nvl = NULL;
5222 nvpair_t *nvp = NULL;
5223 const char *zname = zfs_get_name(zhp);
5224 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5226 if (cbp->cb_recursive) {
5227 const char *snapname;
5228 char *delim = strchr(zname, '@');
5232 snapname = delim + 1;
5233 if (strcmp(cbp->cb_snapname, snapname))
5237 if (zfs_get_holds(zhp, &nvl) != 0)
5240 if (znamelen > cbp->cb_max_namelen)
5241 cbp->cb_max_namelen = znamelen;
5243 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5244 const char *tag = nvpair_name(nvp);
5245 size_t taglen = strnlen(tag, MAXNAMELEN);
5246 if (taglen > cbp->cb_max_taglen)
5247 cbp->cb_max_taglen = taglen;
5250 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5254 * zfs holds [-r] <snap> ...
5256 * -r Recursively hold
5259 zfs_do_holds(int argc, char **argv)
5264 boolean_t scripted = B_FALSE;
5265 boolean_t recursive = B_FALSE;
5266 const char *opts = "rH";
5269 int types = ZFS_TYPE_SNAPSHOT;
5270 holds_cbdata_t cb = { 0 };
5277 while ((c = getopt(argc, argv, opts)) != -1) {
5286 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5293 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5294 flags |= ZFS_ITER_RECURSE;
5300 /* check number of arguments */
5304 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5307 for (i = 0; i < argc; ++i) {
5308 char *snapshot = argv[i];
5310 const char *snapname;
5312 delim = strchr(snapshot, '@');
5313 if (delim == NULL) {
5314 (void) fprintf(stderr,
5315 gettext("'%s' is not a snapshot\n"), snapshot);
5319 snapname = delim + 1;
5321 snapshot[delim - snapshot] = '\0';
5323 cb.cb_recursive = recursive;
5324 cb.cb_snapname = snapname;
5328 * 1. collect holds data, set format options
5330 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5331 holds_callback, &cb);
5337 * 2. print holds data
5339 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5341 if (nvlist_empty(nvl))
5342 (void) fprintf(stderr, gettext("no datasets available\n"));
5346 return (0 != errors);
5349 #define CHECK_SPINNER 30
5350 #define SPINNER_TIME 3 /* seconds */
5351 #define MOUNT_TIME 5 /* seconds */
5354 get_one_dataset(zfs_handle_t *zhp, void *data)
5356 static char *spin[] = { "-", "\\", "|", "/" };
5357 static int spinval = 0;
5358 static int spincheck = 0;
5359 static time_t last_spin_time = (time_t)0;
5360 get_all_cb_t *cbp = data;
5361 zfs_type_t type = zfs_get_type(zhp);
5363 if (cbp->cb_verbose) {
5364 if (--spincheck < 0) {
5365 time_t now = time(NULL);
5366 if (last_spin_time + SPINNER_TIME < now) {
5367 update_progress(spin[spinval++ % 4]);
5368 last_spin_time = now;
5370 spincheck = CHECK_SPINNER;
5375 * Interate over any nested datasets.
5377 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5383 * Skip any datasets whose type does not match.
5385 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5389 libzfs_add_handle(cbp, zhp);
5390 assert(cbp->cb_used <= cbp->cb_alloc);
5396 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5398 get_all_cb_t cb = { 0 };
5399 cb.cb_verbose = verbose;
5400 cb.cb_getone = get_one_dataset;
5403 set_progress_header(gettext("Reading ZFS config"));
5404 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5406 *dslist = cb.cb_handles;
5407 *count = cb.cb_used;
5410 finish_progress(gettext("done."));
5414 * Generic callback for sharing or mounting filesystems. Because the code is so
5415 * similar, we have a common function with an extra parameter to determine which
5416 * mode we are using.
5418 #define OP_SHARE 0x1
5419 #define OP_MOUNT 0x2
5422 * Share or mount a dataset.
5425 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5426 boolean_t explicit, const char *options)
5428 char mountpoint[ZFS_MAXPROPLEN];
5429 char shareopts[ZFS_MAXPROPLEN];
5430 char smbshareopts[ZFS_MAXPROPLEN];
5431 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5433 uint64_t zoned, canmount;
5434 boolean_t shared_nfs, shared_smb;
5436 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5439 * Check to make sure we can mount/share this dataset. If we
5440 * are in the global zone and the filesystem is exported to a
5441 * local zone, or if we are in a local zone and the
5442 * filesystem is not exported, then it is an error.
5444 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5446 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5450 (void) fprintf(stderr, gettext("cannot %s '%s': "
5451 "dataset is exported to a local zone\n"), cmdname,
5455 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5459 (void) fprintf(stderr, gettext("cannot %s '%s': "
5460 "permission denied\n"), cmdname,
5466 * Ignore any filesystems which don't apply to us. This
5467 * includes those with a legacy mountpoint, or those with
5468 * legacy share options.
5470 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5471 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5472 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5473 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5474 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5475 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5477 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5478 strcmp(smbshareopts, "off") == 0) {
5482 (void) fprintf(stderr, gettext("cannot share '%s': "
5483 "legacy share\n"), zfs_get_name(zhp));
5484 (void) fprintf(stderr, gettext("use share(1M) to "
5485 "share this filesystem, or set "
5486 "sharenfs property on\n"));
5491 * We cannot share or mount legacy filesystems. If the
5492 * shareopts is non-legacy but the mountpoint is legacy, we
5493 * treat it as a legacy share.
5495 if (strcmp(mountpoint, "legacy") == 0) {
5499 (void) fprintf(stderr, gettext("cannot %s '%s': "
5500 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5501 (void) fprintf(stderr, gettext("use %s(1M) to "
5502 "%s this filesystem\n"), cmdname, cmdname);
5506 if (strcmp(mountpoint, "none") == 0) {
5510 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5511 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5516 * canmount explicit outcome
5517 * on no pass through
5518 * on yes pass through
5520 * off yes display error, return 1
5521 * noauto no return 0
5522 * noauto yes pass through
5524 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5525 if (canmount == ZFS_CANMOUNT_OFF) {
5529 (void) fprintf(stderr, gettext("cannot %s '%s': "
5530 "'canmount' property is set to 'off'\n"), cmdname,
5533 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5538 * At this point, we have verified that the mountpoint and/or
5539 * shareopts are appropriate for auto management. If the
5540 * filesystem is already mounted or shared, return (failing
5541 * for explicit requests); otherwise mount or share the
5547 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5548 shared_smb = zfs_is_shared_smb(zhp, NULL);
5550 if ((shared_nfs && shared_smb) ||
5551 ((shared_nfs && strcmp(shareopts, "on") == 0) &&
5552 (strcmp(smbshareopts, "off") == 0)) ||
5553 ((shared_smb && strcmp(smbshareopts, "on") == 0) &&
5554 (strcmp(shareopts, "off") == 0))) {
5558 (void) fprintf(stderr, gettext("cannot share "
5559 "'%s': filesystem already shared\n"),
5564 if (!zfs_is_mounted(zhp, NULL) &&
5565 zfs_mount(zhp, NULL, 0) != 0)
5568 if (protocol == NULL) {
5569 if (zfs_shareall(zhp) != 0)
5571 } else if (strcmp(protocol, "nfs") == 0) {
5572 if (zfs_share_nfs(zhp))
5574 } else if (strcmp(protocol, "smb") == 0) {
5575 if (zfs_share_smb(zhp))
5578 (void) fprintf(stderr, gettext("cannot share "
5579 "'%s': invalid share type '%s' "
5581 zfs_get_name(zhp), protocol);
5588 if (options == NULL)
5589 mnt.mnt_mntopts = "";
5591 mnt.mnt_mntopts = (char *)options;
5593 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5594 zfs_is_mounted(zhp, NULL)) {
5598 (void) fprintf(stderr, gettext("cannot mount "
5599 "'%s': filesystem already mounted\n"),
5604 if (zfs_mount(zhp, options, flags) != 0)
5613 * Reports progress in the form "(current/total)". Not thread-safe.
5616 report_mount_progress(int current, int total)
5618 static time_t last_progress_time = 0;
5619 time_t now = time(NULL);
5622 /* report 1..n instead of 0..n-1 */
5625 /* display header if we're here for the first time */
5627 set_progress_header(gettext("Mounting ZFS filesystems"));
5628 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5629 /* too soon to report again */
5633 last_progress_time = now;
5635 (void) sprintf(info, "(%d/%d)", current, total);
5637 if (current == total)
5638 finish_progress(info);
5640 update_progress(info);
5644 append_options(char *mntopts, char *newopts)
5646 int len = strlen(mntopts);
5648 /* original length plus new string to append plus 1 for the comma */
5649 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5650 (void) fprintf(stderr, gettext("the opts argument for "
5651 "'%s' option is too long (more than %d chars)\n"),
5652 "-o", MNT_LINE_MAX);
5657 mntopts[len++] = ',';
5659 (void) strcpy(&mntopts[len], newopts);
5663 share_mount(int op, int argc, char **argv)
5666 boolean_t verbose = B_FALSE;
5668 char *options = NULL;
5672 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5682 if (*optarg == '\0') {
5683 (void) fprintf(stderr, gettext("empty mount "
5684 "options (-o) specified\n"));
5688 if (options == NULL)
5689 options = safe_malloc(MNT_LINE_MAX + 1);
5691 /* option validation is done later */
5692 append_options(options, optarg);
5695 flags |= MS_OVERLAY;
5698 (void) fprintf(stderr, gettext("missing argument for "
5699 "'%c' option\n"), optopt);
5703 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5712 /* check number of arguments */
5714 zfs_handle_t **dslist = NULL;
5715 size_t i, count = 0;
5716 char *protocol = NULL;
5718 if (op == OP_SHARE && argc > 0) {
5719 if (strcmp(argv[0], "nfs") != 0 &&
5720 strcmp(argv[0], "smb") != 0) {
5721 (void) fprintf(stderr, gettext("share type "
5722 "must be 'nfs' or 'smb'\n"));
5731 (void) fprintf(stderr, gettext("too many arguments\n"));
5735 start_progress_timer();
5736 get_all_datasets(&dslist, &count, verbose);
5741 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5743 for (i = 0; i < count; i++) {
5745 report_mount_progress(i, count);
5747 if (share_mount_one(dslist[i], op, flags, protocol,
5748 B_FALSE, options) != 0)
5750 zfs_close(dslist[i]);
5754 } else if (argc == 0) {
5755 struct mnttab entry;
5757 if ((op == OP_SHARE) || (options != NULL)) {
5758 (void) fprintf(stderr, gettext("missing filesystem "
5759 "argument (specify -a for all)\n"));
5764 * When mount is given no arguments, go through /etc/mtab and
5765 * display any active ZFS mounts. We hide any snapshots, since
5766 * they are controlled automatically.
5768 rewind(mnttab_file);
5769 while (getmntent(mnttab_file, &entry) == 0) {
5770 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5771 strchr(entry.mnt_special, '@') != NULL)
5774 (void) printf("%-30s %s\n", entry.mnt_special,
5782 (void) fprintf(stderr,
5783 gettext("too many arguments\n"));
5787 if ((zhp = zfs_open(g_zfs, argv[0],
5788 ZFS_TYPE_FILESYSTEM)) == NULL) {
5791 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5801 * zfs mount -a [nfs]
5802 * zfs mount filesystem
5804 * Mount all filesystems, or mount the given filesystem.
5807 zfs_do_mount(int argc, char **argv)
5809 return (share_mount(OP_MOUNT, argc, argv));
5813 * zfs share -a [nfs | smb]
5814 * zfs share filesystem
5816 * Share all filesystems, or share the given filesystem.
5819 zfs_do_share(int argc, char **argv)
5821 return (share_mount(OP_SHARE, argc, argv));
5824 typedef struct unshare_unmount_node {
5825 zfs_handle_t *un_zhp;
5827 uu_avl_node_t un_avlnode;
5828 } unshare_unmount_node_t;
5832 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
5834 const unshare_unmount_node_t *l = larg;
5835 const unshare_unmount_node_t *r = rarg;
5837 return (strcmp(l->un_mountp, r->un_mountp));
5841 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5842 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
5843 * and unmount it appropriately.
5846 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
5850 struct stat64 statbuf;
5851 struct extmnttab entry;
5852 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5856 * Search for the path in /etc/mtab. Rather than looking for the
5857 * specific path, which can be fooled by non-standard paths (i.e. ".."
5858 * or "//"), we stat() the path and search for the corresponding
5859 * (major,minor) device pair.
5861 if (stat64(path, &statbuf) != 0) {
5862 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5863 cmdname, path, strerror(errno));
5866 path_inode = statbuf.st_ino;
5869 * Search for the given (major,minor) pair in the mount table.
5871 rewind(mnttab_file);
5872 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
5873 if (entry.mnt_major == major(statbuf.st_dev) &&
5874 entry.mnt_minor == minor(statbuf.st_dev))
5878 if (op == OP_SHARE) {
5879 (void) fprintf(stderr, gettext("cannot %s '%s': not "
5880 "currently mounted\n"), cmdname, path);
5883 (void) fprintf(stderr, gettext("warning: %s not in mtab\n"),
5885 if ((ret = umount2(path, flags)) != 0)
5886 (void) fprintf(stderr, gettext("%s: %s\n"), path,
5891 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
5892 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
5893 "filesystem\n"), cmdname, path);
5897 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
5898 ZFS_TYPE_FILESYSTEM)) == NULL)
5902 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
5903 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5904 cmdname, path, strerror(errno));
5906 } else if (statbuf.st_ino != path_inode) {
5907 (void) fprintf(stderr, gettext("cannot "
5908 "%s '%s': not a mountpoint\n"), cmdname, path);
5912 if (op == OP_SHARE) {
5913 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5914 char smbshare_prop[ZFS_MAXPROPLEN];
5916 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
5917 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
5918 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
5919 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
5921 if (strcmp(nfs_mnt_prop, "off") == 0 &&
5922 strcmp(smbshare_prop, "off") == 0) {
5923 (void) fprintf(stderr, gettext("cannot unshare "
5924 "'%s': legacy share\n"), path);
5925 (void) fprintf(stderr, gettext("use exportfs(8) "
5926 "or smbcontrol(1) to unshare this filesystem\n"));
5927 } else if (!zfs_is_shared(zhp)) {
5928 (void) fprintf(stderr, gettext("cannot unshare '%s': "
5929 "not currently shared\n"), path);
5931 ret = zfs_unshareall_bypath(zhp, path);
5934 char mtpt_prop[ZFS_MAXPROPLEN];
5936 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
5937 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
5940 ret = zfs_unmount(zhp, NULL, flags);
5941 } else if (strcmp(mtpt_prop, "legacy") == 0) {
5942 (void) fprintf(stderr, gettext("cannot unmount "
5943 "'%s': legacy mountpoint\n"),
5945 (void) fprintf(stderr, gettext("use umount(8) "
5946 "to unmount this filesystem\n"));
5948 ret = zfs_unmountall(zhp, flags);
5959 * Generic callback for unsharing or unmounting a filesystem.
5962 unshare_unmount(int op, int argc, char **argv)
5969 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5970 char sharesmb[ZFS_MAXPROPLEN];
5973 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
5982 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5993 * We could make use of zfs_for_each() to walk all datasets in
5994 * the system, but this would be very inefficient, especially
5995 * since we would have to linearly search /etc/mtab for each
5996 * one. Instead, do one pass through /etc/mtab looking for
5997 * zfs entries and call zfs_unmount() for each one.
5999 * Things get a little tricky if the administrator has created
6000 * mountpoints beneath other ZFS filesystems. In this case, we
6001 * have to unmount the deepest filesystems first. To accomplish
6002 * this, we place all the mountpoints in an AVL tree sorted by
6003 * the special type (dataset name), and walk the result in
6004 * reverse to make sure to get any snapshots first.
6006 struct mnttab entry;
6007 uu_avl_pool_t *pool;
6008 uu_avl_t *tree = NULL;
6009 unshare_unmount_node_t *node;
6011 uu_avl_walk_t *walk;
6014 (void) fprintf(stderr, gettext("too many arguments\n"));
6018 if (((pool = uu_avl_pool_create("unmount_pool",
6019 sizeof (unshare_unmount_node_t),
6020 offsetof(unshare_unmount_node_t, un_avlnode),
6021 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6022 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6025 rewind(mnttab_file);
6026 while (getmntent(mnttab_file, &entry) == 0) {
6028 /* ignore non-ZFS entries */
6029 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6032 /* ignore snapshots */
6033 if (strchr(entry.mnt_special, '@') != NULL)
6036 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6037 ZFS_TYPE_FILESYSTEM)) == NULL) {
6044 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6046 sizeof (nfs_mnt_prop),
6047 NULL, NULL, 0, B_FALSE) == 0);
6048 if (strcmp(nfs_mnt_prop, "off") != 0)
6050 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6052 sizeof (nfs_mnt_prop),
6053 NULL, NULL, 0, B_FALSE) == 0);
6054 if (strcmp(nfs_mnt_prop, "off") == 0)
6058 /* Ignore legacy mounts */
6059 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6061 sizeof (nfs_mnt_prop),
6062 NULL, NULL, 0, B_FALSE) == 0);
6063 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6065 /* Ignore canmount=noauto mounts */
6066 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6067 ZFS_CANMOUNT_NOAUTO)
6073 node = safe_malloc(sizeof (unshare_unmount_node_t));
6075 node->un_mountp = safe_strdup(entry.mnt_mountp);
6077 uu_avl_node_init(node, &node->un_avlnode, pool);
6079 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6080 uu_avl_insert(tree, node, idx);
6082 zfs_close(node->un_zhp);
6083 free(node->un_mountp);
6089 * Walk the AVL tree in reverse, unmounting each filesystem and
6090 * removing it from the AVL tree in the process.
6092 if ((walk = uu_avl_walk_start(tree,
6093 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6096 while ((node = uu_avl_walk_next(walk)) != NULL) {
6097 uu_avl_remove(tree, node);
6101 if (zfs_unshareall_bypath(node->un_zhp,
6102 node->un_mountp) != 0)
6107 if (zfs_unmount(node->un_zhp,
6108 node->un_mountp, flags) != 0)
6113 zfs_close(node->un_zhp);
6114 free(node->un_mountp);
6118 uu_avl_walk_end(walk);
6119 uu_avl_destroy(tree);
6120 uu_avl_pool_destroy(pool);
6125 (void) fprintf(stderr,
6126 gettext("missing filesystem argument\n"));
6128 (void) fprintf(stderr,
6129 gettext("too many arguments\n"));
6134 * We have an argument, but it may be a full path or a ZFS
6135 * filesystem. Pass full paths off to unmount_path() (shared by
6136 * manual_unmount), otherwise open the filesystem and pass to
6139 if (argv[0][0] == '/')
6140 return (unshare_unmount_path(op, argv[0],
6143 if ((zhp = zfs_open(g_zfs, argv[0],
6144 ZFS_TYPE_FILESYSTEM)) == NULL)
6147 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6148 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6149 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6150 NULL, 0, B_FALSE) == 0);
6154 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6156 sizeof (nfs_mnt_prop),
6157 NULL, NULL, 0, B_FALSE) == 0);
6158 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6159 sharesmb, sizeof (sharesmb), NULL, NULL,
6162 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6163 strcmp(sharesmb, "off") == 0) {
6164 (void) fprintf(stderr, gettext("cannot "
6165 "unshare '%s': legacy share\n"),
6167 (void) fprintf(stderr, gettext("use "
6168 "unshare(1M) to unshare this "
6171 } else if (!zfs_is_shared(zhp)) {
6172 (void) fprintf(stderr, gettext("cannot "
6173 "unshare '%s': not currently "
6174 "shared\n"), zfs_get_name(zhp));
6176 } else if (zfs_unshareall(zhp) != 0) {
6182 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6183 (void) fprintf(stderr, gettext("cannot "
6184 "unmount '%s': legacy "
6185 "mountpoint\n"), zfs_get_name(zhp));
6186 (void) fprintf(stderr, gettext("use "
6187 "umount(1M) to unmount this "
6190 } else if (!zfs_is_mounted(zhp, NULL)) {
6191 (void) fprintf(stderr, gettext("cannot "
6192 "unmount '%s': not currently "
6196 } else if (zfs_unmountall(zhp, flags) != 0) {
6210 * zfs unmount filesystem
6212 * Unmount all filesystems, or a specific ZFS filesystem.
6215 zfs_do_unmount(int argc, char **argv)
6217 return (unshare_unmount(OP_MOUNT, argc, argv));
6222 * zfs unshare filesystem
6224 * Unshare all filesystems, or a specific ZFS filesystem.
6227 zfs_do_unshare(int argc, char **argv)
6229 return (unshare_unmount(OP_SHARE, argc, argv));
6233 find_command_idx(char *command, int *idx)
6237 for (i = 0; i < NCOMMAND; i++) {
6238 if (command_table[i].name == NULL)
6241 if (strcmp(command, command_table[i].name) == 0) {
6250 zfs_do_diff(int argc, char **argv)
6254 char *tosnap = NULL;
6255 char *fromsnap = NULL;
6260 while ((c = getopt(argc, argv, "FHt")) != -1) {
6263 flags |= ZFS_DIFF_CLASSIFY;
6266 flags |= ZFS_DIFF_PARSEABLE;
6269 flags |= ZFS_DIFF_TIMESTAMP;
6272 (void) fprintf(stderr,
6273 gettext("invalid option '%c'\n"), optopt);
6282 (void) fprintf(stderr,
6283 gettext("must provide at least one snapshot name\n"));
6288 (void) fprintf(stderr, gettext("too many arguments\n"));
6293 tosnap = (argc == 2) ? argv[1] : NULL;
6296 if (*fromsnap != '@')
6297 copy = strdup(fromsnap);
6299 copy = strdup(tosnap);
6303 if ((atp = strchr(copy, '@')))
6306 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6312 * Ignore SIGPIPE so that the library can give us
6313 * information on any failure
6315 (void) sigignore(SIGPIPE);
6317 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6325 main(int argc, char **argv)
6331 (void) setlocale(LC_ALL, "");
6332 (void) textdomain(TEXT_DOMAIN);
6336 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6337 (void) fprintf(stderr, gettext("internal error: unable to "
6338 "open %s\n"), MNTTAB);
6343 * Make sure the user has specified some command.
6346 (void) fprintf(stderr, gettext("missing command\n"));
6353 * The 'umount' command is an alias for 'unmount'
6355 if (strcmp(cmdname, "umount") == 0)
6356 cmdname = "unmount";
6359 * The 'recv' command is an alias for 'receive'
6361 if (strcmp(cmdname, "recv") == 0)
6362 cmdname = "receive";
6365 * The 'snap' command is an alias for 'snapshot'
6367 if (strcmp(cmdname, "snap") == 0)
6368 cmdname = "snapshot";
6373 if ((strcmp(cmdname, "-?") == 0) ||
6374 (strcmp(cmdname, "--help") == 0))
6377 if ((g_zfs = libzfs_init()) == NULL)
6380 zpool_set_history_str("zfs", argc, argv, history_str);
6381 verify(zpool_stage_history(g_zfs, history_str) == 0);
6383 libzfs_print_on_error(g_zfs, B_TRUE);
6386 * Run the appropriate command.
6388 libzfs_mnttab_cache(g_zfs, B_FALSE);
6389 if (find_command_idx(cmdname, &i) == 0) {
6390 current_command = &command_table[i];
6391 ret = command_table[i].func(argc - 1, argv + 1);
6392 } else if (strchr(cmdname, '=') != NULL) {
6393 verify(find_command_idx("set", &i) == 0);
6394 current_command = &command_table[i];
6395 ret = command_table[i].func(argc, argv);
6397 (void) fprintf(stderr, gettext("unrecognized "
6398 "command '%s'\n"), cmdname);
6404 (void) fclose(mnttab_file);
6407 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6408 * for the purposes of running ::findleaks.
6410 if (getenv("ZFS_ABORT") != NULL) {
6411 (void) printf("dumping core by request\n");