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) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
35 #include <libnvpair.h>
48 #include <sys/mkdev.h>
49 #include <sys/mntent.h>
50 #include <sys/mnttab.h>
51 #include <sys/mount.h>
53 #include <sys/fs/zfs.h>
54 #include <sys/types.h>
59 #include <zfs_deleg.h>
63 #include <directory.h>
64 #endif /* HAVE_IDMAP */
68 #include "zfs_comutil.h"
69 #include "libzfs_impl.h"
71 libzfs_handle_t *g_zfs;
73 static FILE *mnttab_file;
74 static char history_str[HIS_MAX_RECORD_LEN];
76 static int zfs_do_clone(int argc, char **argv);
77 static int zfs_do_create(int argc, char **argv);
78 static int zfs_do_destroy(int argc, char **argv);
79 static int zfs_do_get(int argc, char **argv);
80 static int zfs_do_inherit(int argc, char **argv);
81 static int zfs_do_list(int argc, char **argv);
82 static int zfs_do_mount(int argc, char **argv);
83 static int zfs_do_rename(int argc, char **argv);
84 static int zfs_do_rollback(int argc, char **argv);
85 static int zfs_do_set(int argc, char **argv);
86 static int zfs_do_upgrade(int argc, char **argv);
87 static int zfs_do_snapshot(int argc, char **argv);
88 static int zfs_do_unmount(int argc, char **argv);
89 static int zfs_do_share(int argc, char **argv);
90 static int zfs_do_unshare(int argc, char **argv);
91 static int zfs_do_send(int argc, char **argv);
92 static int zfs_do_receive(int argc, char **argv);
93 static int zfs_do_promote(int argc, char **argv);
94 static int zfs_do_userspace(int argc, char **argv);
95 static int zfs_do_allow(int argc, char **argv);
96 static int zfs_do_unallow(int argc, char **argv);
97 static int zfs_do_hold(int argc, char **argv);
98 static int zfs_do_holds(int argc, char **argv);
99 static int zfs_do_release(int argc, char **argv);
100 static int zfs_do_diff(int argc, char **argv);
103 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
108 _umem_debug_init(void)
110 return ("default,verbose"); /* $UMEM_DEBUG setting */
114 _umem_logging_init(void)
116 return ("fail,contents"); /* $UMEM_LOGGING setting */
149 typedef struct zfs_command {
151 int (*func)(int argc, char **argv);
156 * Master command table. Each ZFS command has a name, associated function, and
157 * usage message. The usage messages need to be internationalized, so we have
158 * to have a function to return the usage message based on a command index.
160 * These commands are organized according to how they are displayed in the usage
161 * message. An empty command (one with a NULL name) indicates an empty line in
162 * the generic usage message.
164 static zfs_command_t command_table[] = {
165 { "create", zfs_do_create, HELP_CREATE },
166 { "destroy", zfs_do_destroy, HELP_DESTROY },
168 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
169 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
170 { "clone", zfs_do_clone, HELP_CLONE },
171 { "promote", zfs_do_promote, HELP_PROMOTE },
172 { "rename", zfs_do_rename, HELP_RENAME },
174 { "list", zfs_do_list, HELP_LIST },
176 { "set", zfs_do_set, HELP_SET },
177 { "get", zfs_do_get, HELP_GET },
178 { "inherit", zfs_do_inherit, HELP_INHERIT },
179 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
180 { "userspace", zfs_do_userspace, HELP_USERSPACE },
181 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
183 { "mount", zfs_do_mount, HELP_MOUNT },
184 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
185 { "share", zfs_do_share, HELP_SHARE },
186 { "unshare", zfs_do_unshare, HELP_UNSHARE },
188 { "send", zfs_do_send, HELP_SEND },
189 { "receive", zfs_do_receive, HELP_RECEIVE },
191 { "allow", zfs_do_allow, HELP_ALLOW },
193 { "unallow", zfs_do_unallow, HELP_UNALLOW },
195 { "hold", zfs_do_hold, HELP_HOLD },
196 { "holds", zfs_do_holds, HELP_HOLDS },
197 { "release", zfs_do_release, HELP_RELEASE },
198 { "diff", zfs_do_diff, HELP_DIFF },
201 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
203 zfs_command_t *current_command;
206 get_usage(zfs_help_t idx)
210 return (gettext("\tclone [-p] [-o property=value] ... "
211 "<snapshot> <filesystem|volume>\n"));
213 return (gettext("\tcreate [-p] [-o property=value] ... "
215 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
216 "-V <size> <volume>\n"));
218 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
219 "\tdestroy [-dnpRrv] "
220 "<filesystem|volume>@<snap>[%<snap>][,...]\n"));
222 return (gettext("\tget [-rHp] [-d max] "
223 "[-o \"all\" | field[,...]] [-t type[,...]] "
224 "[-s source[,...]]\n"
225 "\t <\"all\" | property[,...]> "
226 "[filesystem|volume|snapshot] ...\n"));
228 return (gettext("\tinherit [-rS] <property> "
229 "<filesystem|volume|snapshot> ...\n"));
231 return (gettext("\tupgrade [-v]\n"
232 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
234 return (gettext("\tlist [-rH][-d max] "
235 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
236 "\t [-S property] ... "
237 "[filesystem|volume|snapshot|snap] ...\n"));
239 return (gettext("\tmount\n"
240 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
242 return (gettext("\tpromote <clone-filesystem>\n"));
244 return (gettext("\treceive [-vnFu] <filesystem|volume|"
246 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
248 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
249 "<filesystem|volume|snapshot>\n"
250 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
251 "\trename -r <snapshot> <snapshot>"));
253 return (gettext("\trollback [-rRf] <snapshot>\n"));
255 return (gettext("\tsend [-DnPpRrv] [-[iI] snapshot] "
258 return (gettext("\tset <property=value> "
259 "<filesystem|volume|snapshot> ...\n"));
261 return (gettext("\tshare <-a | filesystem>\n"));
263 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
264 "<filesystem@snapname|volume@snapname>\n"));
266 return (gettext("\tunmount [-f] "
267 "<-a | filesystem|mountpoint>\n"));
269 return (gettext("\tunshare "
270 "<-a | filesystem|mountpoint>\n"));
272 return (gettext("\tallow <filesystem|volume>\n"
274 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
275 "\t <filesystem|volume>\n"
276 "\tallow [-ld] -e <perm|@setname>[,...] "
277 "<filesystem|volume>\n"
278 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
279 "\tallow -s @setname <perm|@setname>[,...] "
280 "<filesystem|volume>\n"));
282 return (gettext("\tunallow [-rldug] "
283 "<\"everyone\"|user|group>[,...]\n"
284 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
285 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
286 "<filesystem|volume>\n"
287 "\tunallow [-r] -c [<perm|@setname>[,...]] "
288 "<filesystem|volume>\n"
289 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
290 "<filesystem|volume>\n"));
292 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
293 "[-s field] ...\n\t[-S field] ... "
294 "[-t type[,...]] <filesystem|snapshot>\n"));
295 case HELP_GROUPSPACE:
296 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
297 "[-s field] ...\n\t[-S field] ... "
298 "[-t type[,...]] <filesystem|snapshot>\n"));
300 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
302 return (gettext("\tholds [-r] <snapshot> ...\n"));
304 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
306 return (gettext("\tdiff [-FHt] <snapshot> "
307 "[snapshot|filesystem]\n"));
317 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
322 * Utility function to guarantee malloc() success.
326 safe_malloc(size_t size)
330 if ((data = calloc(1, size)) == NULL)
337 safe_strdup(char *str)
339 char *dupstr = strdup(str);
348 * Callback routine that will print out information for each of
352 usage_prop_cb(int prop, void *cb)
356 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
358 if (zfs_prop_readonly(prop))
359 (void) fprintf(fp, " NO ");
361 (void) fprintf(fp, "YES ");
363 if (zfs_prop_inheritable(prop))
364 (void) fprintf(fp, " YES ");
366 (void) fprintf(fp, " NO ");
368 if (zfs_prop_values(prop) == NULL)
369 (void) fprintf(fp, "-\n");
371 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
377 * Display usage message. If we're inside a command, display only the usage for
378 * that command. Otherwise, iterate over the entire command table and display
379 * a complete usage message.
382 usage(boolean_t requested)
385 boolean_t show_properties = B_FALSE;
386 FILE *fp = requested ? stdout : stderr;
388 if (current_command == NULL) {
390 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
392 gettext("where 'command' is one of the following:\n\n"));
394 for (i = 0; i < NCOMMAND; i++) {
395 if (command_table[i].name == NULL)
396 (void) fprintf(fp, "\n");
398 (void) fprintf(fp, "%s",
399 get_usage(command_table[i].usage));
402 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
403 "pool/[dataset/]*dataset[@name]\n"));
405 (void) fprintf(fp, gettext("usage:\n"));
406 (void) fprintf(fp, "%s", get_usage(current_command->usage));
409 if (current_command != NULL &&
410 (strcmp(current_command->name, "set") == 0 ||
411 strcmp(current_command->name, "get") == 0 ||
412 strcmp(current_command->name, "inherit") == 0 ||
413 strcmp(current_command->name, "list") == 0))
414 show_properties = B_TRUE;
416 if (show_properties) {
418 gettext("\nThe following properties are supported:\n"));
420 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
421 "PROPERTY", "EDIT", "INHERIT", "VALUES");
423 /* Iterate over all properties */
424 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
427 (void) fprintf(fp, "\t%-15s ", "userused@...");
428 (void) fprintf(fp, " NO NO <size>\n");
429 (void) fprintf(fp, "\t%-15s ", "groupused@...");
430 (void) fprintf(fp, " NO NO <size>\n");
431 (void) fprintf(fp, "\t%-15s ", "userquota@...");
432 (void) fprintf(fp, "YES NO <size> | none\n");
433 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
434 (void) fprintf(fp, "YES NO <size> | none\n");
435 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
436 (void) fprintf(fp, " NO NO <size>\n");
438 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
439 "with standard units such as K, M, G, etc.\n"));
440 (void) fprintf(fp, gettext("\nUser-defined properties can "
441 "be specified by using a name containing a colon (:).\n"));
442 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
443 "properties must be appended with\n"
444 "a user or group specifier of one of these forms:\n"
445 " POSIX name (eg: \"matt\")\n"
446 " POSIX id (eg: \"126829\")\n"
447 " SMB name@domain (eg: \"matt@sun\")\n"
448 " SMB SID (eg: \"S-1-234-567-89\")\n"));
451 gettext("\nFor the property list, run: %s\n"),
454 gettext("\nFor the delegated permission list, run: %s\n"),
455 "zfs allow|unallow");
459 * See comments at end of main().
461 if (getenv("ZFS_ABORT") != NULL) {
462 (void) printf("dumping core by request\n");
466 exit(requested ? 0 : 2);
470 parseprop(nvlist_t *props)
472 char *propname = optarg;
473 char *propval, *strval;
475 if ((propval = strchr(propname, '=')) == NULL) {
476 (void) fprintf(stderr, gettext("missing "
477 "'=' for -o option\n"));
482 if (nvlist_lookup_string(props, propname, &strval) == 0) {
483 (void) fprintf(stderr, gettext("property '%s' "
484 "specified multiple times\n"), propname);
487 if (nvlist_add_string(props, propname, propval) != 0)
493 parse_depth(char *opt, int *flags)
498 depth = (int)strtol(opt, &tmp, 0);
500 (void) fprintf(stderr,
501 gettext("%s is not an integer\n"), optarg);
505 (void) fprintf(stderr,
506 gettext("Depth can not be negative.\n"));
509 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
513 #define PROGRESS_DELAY 2 /* seconds */
515 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";
516 static time_t pt_begin;
517 static char *pt_header = NULL;
518 static boolean_t pt_shown;
521 start_progress_timer(void)
523 pt_begin = time(NULL) + PROGRESS_DELAY;
528 set_progress_header(char *header)
530 assert(pt_header == NULL);
531 pt_header = safe_strdup(header);
533 (void) printf("%s: ", header);
534 (void) fflush(stdout);
539 update_progress(char *update)
541 if (!pt_shown && time(NULL) > pt_begin) {
542 int len = strlen(update);
544 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
546 (void) fflush(stdout);
548 } else if (pt_shown) {
549 int len = strlen(update);
551 (void) printf("%s%*.*s", update, len, len, pt_reverse);
552 (void) fflush(stdout);
557 finish_progress(char *done)
560 (void) printf("%s\n", done);
561 (void) fflush(stdout);
568 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
570 * Given an existing dataset, create a writable copy whose initial contents
571 * are the same as the source. The newly created dataset maintains a
572 * dependency on the original; the original cannot be destroyed so long as
575 * The '-p' flag creates all the non-existing ancestors of the target first.
578 zfs_do_clone(int argc, char **argv)
580 zfs_handle_t *zhp = NULL;
581 boolean_t parents = B_FALSE;
586 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
590 while ((c = getopt(argc, argv, "o:p")) != -1) {
593 if (parseprop(props))
600 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
609 /* check number of arguments */
611 (void) fprintf(stderr, gettext("missing source dataset "
616 (void) fprintf(stderr, gettext("missing target dataset "
621 (void) fprintf(stderr, gettext("too many arguments\n"));
625 /* open the source dataset */
626 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
629 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
632 * Now create the ancestors of the target dataset. If the
633 * target already exists and '-p' option was used we should not
636 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
639 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
644 ret = zfs_clone(zhp, argv[1], props);
646 /* create the mountpoint if necessary */
650 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
652 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
653 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
654 ret = zfs_share(clone);
673 * zfs create [-p] [-o prop=value] ... fs
674 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
676 * Create a new dataset. This command can be used to create filesystems
677 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
678 * For volumes, the user must specify a size to be used.
680 * The '-s' flag applies only to volumes, and indicates that we should not try
681 * to set the reservation for this volume. By default we set a reservation
682 * equal to the size for any volume. For pools with SPA_VERSION >=
683 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
685 * The '-p' flag creates all the non-existing ancestors of the target first.
688 zfs_do_create(int argc, char **argv)
690 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
691 zfs_handle_t *zhp = NULL;
692 uint64_t volsize = 0;
694 boolean_t noreserve = B_FALSE;
695 boolean_t bflag = B_FALSE;
696 boolean_t parents = B_FALSE;
700 int canmount = ZFS_CANMOUNT_OFF;
702 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
706 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
709 type = ZFS_TYPE_VOLUME;
710 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
711 (void) fprintf(stderr, gettext("bad volume "
712 "size '%s': %s\n"), optarg,
713 libzfs_error_description(g_zfs));
717 if (nvlist_add_uint64(props,
718 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
727 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
728 (void) fprintf(stderr, gettext("bad volume "
729 "block size '%s': %s\n"), optarg,
730 libzfs_error_description(g_zfs));
734 if (nvlist_add_uint64(props,
735 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
740 if (parseprop(props))
747 (void) fprintf(stderr, gettext("missing size "
752 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
758 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
759 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
760 "used when creating a volume\n"));
767 /* check number of arguments */
769 (void) fprintf(stderr, gettext("missing %s argument\n"),
770 zfs_type_to_name(type));
774 (void) fprintf(stderr, gettext("too many arguments\n"));
778 if (type == ZFS_TYPE_VOLUME && !noreserve) {
779 zpool_handle_t *zpool_handle;
780 uint64_t spa_version;
782 zfs_prop_t resv_prop;
785 if ((p = strchr(argv[0], '/')))
787 zpool_handle = zpool_open(g_zfs, argv[0]);
790 if (zpool_handle == NULL)
792 spa_version = zpool_get_prop_int(zpool_handle,
793 ZPOOL_PROP_VERSION, NULL);
794 zpool_close(zpool_handle);
795 if (spa_version >= SPA_VERSION_REFRESERVATION)
796 resv_prop = ZFS_PROP_REFRESERVATION;
798 resv_prop = ZFS_PROP_RESERVATION;
799 volsize = zvol_volsize_to_reservation(volsize, props);
801 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
803 if (nvlist_add_uint64(props,
804 zfs_prop_to_name(resv_prop), volsize) != 0) {
811 if (parents && zfs_name_valid(argv[0], type)) {
813 * Now create the ancestors of target dataset. If the target
814 * already exists and '-p' option was used we should not
817 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
821 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
826 if (zfs_create(g_zfs, argv[0], type, props) != 0)
829 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
834 * if the user doesn't want the dataset automatically mounted,
835 * then skip the mount/share step
837 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
838 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
841 * Mount and/or share the new filesystem as appropriate. We provide a
842 * verbose error message to let the user know that their filesystem was
843 * in fact created, even if we failed to mount or share it.
845 if (canmount == ZFS_CANMOUNT_ON) {
846 if (zfs_mount(zhp, NULL, 0) != 0) {
847 (void) fprintf(stderr, gettext("filesystem "
848 "successfully created, but not mounted\n"));
850 } else if (zfs_share(zhp) != 0) {
851 (void) fprintf(stderr, gettext("filesystem "
852 "successfully created, but not shared\n"));
869 * zfs destroy [-rRf] <fs, vol>
870 * zfs destroy [-rRd] <snap>
872 * -r Recursively destroy all children
873 * -R Recursively destroy all dependents, including clones
874 * -f Force unmounting of any dependents
875 * -d If we can't destroy now, mark for deferred destruction
877 * Destroys the given dataset. By default, it will unmount any filesystems,
878 * and refuse to destroy a dataset that has any dependents. A dependent can
879 * either be a child, or a clone of a child.
881 typedef struct destroy_cbdata {
884 boolean_t cb_recurse;
886 boolean_t cb_doclones;
887 zfs_handle_t *cb_target;
888 boolean_t cb_defer_destroy;
889 boolean_t cb_verbose;
890 boolean_t cb_parsable;
894 /* first snap in contiguous run */
895 zfs_handle_t *cb_firstsnap;
896 /* previous snap in contiguous run */
897 zfs_handle_t *cb_prevsnap;
903 * Check for any dependents based on the '-r' or '-R' flags.
906 destroy_check_dependent(zfs_handle_t *zhp, void *data)
908 destroy_cbdata_t *cbp = data;
909 const char *tname = zfs_get_name(cbp->cb_target);
910 const char *name = zfs_get_name(zhp);
912 if (strncmp(tname, name, strlen(tname)) == 0 &&
913 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
915 * This is a direct descendant, not a clone somewhere else in
922 (void) fprintf(stderr, gettext("cannot destroy '%s': "
923 "%s has children\n"),
924 zfs_get_name(cbp->cb_target),
925 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
926 (void) fprintf(stderr, gettext("use '-r' to destroy "
927 "the following datasets:\n"));
928 cbp->cb_first = B_FALSE;
929 cbp->cb_error = B_TRUE;
932 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
935 * This is a clone. We only want to report this if the '-r'
936 * wasn't specified, or the target is a snapshot.
938 if (!cbp->cb_recurse &&
939 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
943 (void) fprintf(stderr, gettext("cannot destroy '%s': "
944 "%s has dependent clones\n"),
945 zfs_get_name(cbp->cb_target),
946 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
947 (void) fprintf(stderr, gettext("use '-R' to destroy "
948 "the following datasets:\n"));
949 cbp->cb_first = B_FALSE;
950 cbp->cb_error = B_TRUE;
951 cbp->cb_dryrun = B_TRUE;
954 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
963 destroy_callback(zfs_handle_t *zhp, void *data)
965 destroy_cbdata_t *cb = data;
966 const char *name = zfs_get_name(zhp);
968 if (cb->cb_verbose) {
969 if (cb->cb_parsable) {
970 (void) printf("destroy\t%s\n", name);
971 } else if (cb->cb_dryrun) {
972 (void) printf(gettext("would destroy %s\n"),
975 (void) printf(gettext("will destroy %s\n"),
981 * Ignore pools (which we've already flagged as an error before getting
984 if (strchr(zfs_get_name(zhp), '/') == NULL &&
985 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
990 if (!cb->cb_dryrun) {
991 if (zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
992 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1003 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1005 destroy_cbdata_t *cb = arg;
1006 const char *name = zfs_get_name(zhp);
1009 if (nvlist_exists(cb->cb_nvl, name)) {
1010 if (cb->cb_firstsnap == NULL)
1011 cb->cb_firstsnap = zfs_handle_dup(zhp);
1012 if (cb->cb_prevsnap != NULL)
1013 zfs_close(cb->cb_prevsnap);
1014 /* this snap continues the current range */
1015 cb->cb_prevsnap = zfs_handle_dup(zhp);
1016 if (cb->cb_verbose) {
1017 if (cb->cb_parsable) {
1018 (void) printf("destroy\t%s\n", name);
1019 } else if (cb->cb_dryrun) {
1020 (void) printf(gettext("would destroy %s\n"),
1023 (void) printf(gettext("will destroy %s\n"),
1027 } else if (cb->cb_firstsnap != NULL) {
1028 /* end of this range */
1030 err = zfs_get_snapused_int(cb->cb_firstsnap,
1031 cb->cb_prevsnap, &used);
1032 cb->cb_snapused += used;
1033 zfs_close(cb->cb_firstsnap);
1034 cb->cb_firstsnap = NULL;
1035 zfs_close(cb->cb_prevsnap);
1036 cb->cb_prevsnap = NULL;
1043 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1046 assert(cb->cb_firstsnap == NULL);
1047 assert(cb->cb_prevsnap == NULL);
1048 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1049 if (cb->cb_firstsnap != NULL) {
1052 err = zfs_get_snapused_int(cb->cb_firstsnap,
1053 cb->cb_prevsnap, &used);
1055 cb->cb_snapused += used;
1056 zfs_close(cb->cb_firstsnap);
1057 cb->cb_firstsnap = NULL;
1058 zfs_close(cb->cb_prevsnap);
1059 cb->cb_prevsnap = NULL;
1065 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1067 destroy_cbdata_t *cb = arg;
1070 /* Check for clones. */
1071 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1072 cb->cb_target = zhp;
1073 cb->cb_first = B_TRUE;
1074 err = zfs_iter_dependents(zhp, B_TRUE,
1075 destroy_check_dependent, cb);
1079 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1087 gather_snapshots(zfs_handle_t *zhp, void *arg)
1089 destroy_cbdata_t *cb = arg;
1092 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1098 if (cb->cb_verbose) {
1099 err = destroy_print_snapshots(zhp, cb);
1105 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1113 destroy_clones(destroy_cbdata_t *cb)
1116 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1118 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1119 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1122 boolean_t defer = cb->cb_defer_destroy;
1126 * We can't defer destroy non-snapshots, so set it to
1127 * false while destroying the clones.
1129 cb->cb_defer_destroy = B_FALSE;
1130 err = zfs_iter_dependents(zhp, B_FALSE,
1131 destroy_callback, cb);
1132 cb->cb_defer_destroy = defer;
1142 zfs_do_destroy(int argc, char **argv)
1144 destroy_cbdata_t cb = { 0 };
1148 zfs_type_t type = ZFS_TYPE_DATASET;
1151 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1154 cb.cb_verbose = B_TRUE;
1157 cb.cb_verbose = B_TRUE;
1158 cb.cb_parsable = B_TRUE;
1161 cb.cb_dryrun = B_TRUE;
1164 cb.cb_defer_destroy = B_TRUE;
1165 type = ZFS_TYPE_SNAPSHOT;
1168 cb.cb_force = B_TRUE;
1171 cb.cb_recurse = B_TRUE;
1174 cb.cb_recurse = B_TRUE;
1175 cb.cb_doclones = B_TRUE;
1179 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1188 /* check number of arguments */
1190 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1194 (void) fprintf(stderr, gettext("too many arguments\n"));
1198 at = strchr(argv[0], '@');
1202 /* Build the list of snaps to destroy in cb_nvl. */
1203 if (nvlist_alloc(&cb.cb_nvl, NV_UNIQUE_NAME, 0) != 0)
1207 zhp = zfs_open(g_zfs, argv[0],
1208 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1212 cb.cb_snapspec = at + 1;
1213 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1216 nvlist_free(cb.cb_nvl);
1220 if (nvlist_empty(cb.cb_nvl)) {
1221 (void) fprintf(stderr, gettext("could not find any "
1222 "snapshots to destroy; check snapshot names.\n"));
1224 nvlist_free(cb.cb_nvl);
1228 if (cb.cb_verbose) {
1230 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1231 if (cb.cb_parsable) {
1232 (void) printf("reclaim\t%llu\n",
1233 (u_longlong_t)cb.cb_snapused);
1234 } else if (cb.cb_dryrun) {
1235 (void) printf(gettext("would reclaim %s\n"),
1238 (void) printf(gettext("will reclaim %s\n"),
1243 if (!cb.cb_dryrun) {
1245 err = destroy_clones(&cb);
1247 err = zfs_destroy_snaps_nvl(zhp, cb.cb_nvl,
1248 cb.cb_defer_destroy);
1253 nvlist_free(cb.cb_nvl);
1257 /* Open the given dataset */
1258 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1264 * Perform an explicit check for pools before going any further.
1266 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1267 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1268 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1269 "operation does not apply to pools\n"),
1271 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1272 "%s' to destroy all datasets in the pool\n"),
1274 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1275 "to destroy the pool itself\n"), zfs_get_name(zhp));
1281 * Check for any dependents and/or clones.
1283 cb.cb_first = B_TRUE;
1284 if (!cb.cb_doclones &&
1285 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1296 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1303 * Do the real thing. The callback will close the
1304 * handle regardless of whether it succeeds or not.
1306 if (destroy_callback(zhp, &cb) != 0)
1314 is_recvd_column(zprop_get_cbdata_t *cbp)
1317 zfs_get_column_t col;
1319 for (i = 0; i < ZFS_GET_NCOLS &&
1320 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1321 if (col == GET_COL_RECVD)
1327 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1328 * < all | property[,property]... > < fs | snap | vol > ...
1330 * -r recurse over any child datasets
1331 * -H scripted mode. Headers are stripped, and fields are separated
1332 * by tabs instead of spaces.
1333 * -o Set of fields to display. One of "name,property,value,
1334 * received,source". Default is "name,property,value,source".
1335 * "all" is an alias for all five.
1336 * -s Set of sources to allow. One of
1337 * "local,default,inherited,received,temporary,none". Default is
1339 * -p Display values in parsable (literal) format.
1341 * Prints properties for the given datasets. The user can control which
1342 * columns to display as well as which property types to allow.
1346 * Invoked to display the properties for a single dataset.
1349 get_callback(zfs_handle_t *zhp, void *data)
1351 char buf[ZFS_MAXPROPLEN];
1352 char rbuf[ZFS_MAXPROPLEN];
1353 zprop_source_t sourcetype;
1354 char source[ZFS_MAXNAMELEN];
1355 zprop_get_cbdata_t *cbp = data;
1356 nvlist_t *user_props = zfs_get_user_props(zhp);
1357 zprop_list_t *pl = cbp->cb_proplist;
1361 boolean_t received = is_recvd_column(cbp);
1363 for (; pl != NULL; pl = pl->pl_next) {
1364 char *recvdval = NULL;
1366 * Skip the special fake placeholder. This will also skip over
1367 * the name property when 'all' is specified.
1369 if (pl->pl_prop == ZFS_PROP_NAME &&
1370 pl == cbp->cb_proplist)
1373 if (pl->pl_prop != ZPROP_INVAL) {
1374 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1375 sizeof (buf), &sourcetype, source,
1377 cbp->cb_literal) != 0) {
1380 if (!zfs_prop_valid_for_type(pl->pl_prop,
1381 ZFS_TYPE_DATASET)) {
1382 (void) fprintf(stderr,
1383 gettext("No such property '%s'\n"),
1384 zfs_prop_to_name(pl->pl_prop));
1387 sourcetype = ZPROP_SRC_NONE;
1388 (void) strlcpy(buf, "-", sizeof (buf));
1391 if (received && (zfs_prop_get_recvd(zhp,
1392 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1393 cbp->cb_literal) == 0))
1396 zprop_print_one_property(zfs_get_name(zhp), cbp,
1397 zfs_prop_to_name(pl->pl_prop),
1398 buf, sourcetype, source, recvdval);
1399 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1400 sourcetype = ZPROP_SRC_LOCAL;
1402 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1403 buf, sizeof (buf), cbp->cb_literal) != 0) {
1404 sourcetype = ZPROP_SRC_NONE;
1405 (void) strlcpy(buf, "-", sizeof (buf));
1408 zprop_print_one_property(zfs_get_name(zhp), cbp,
1409 pl->pl_user_prop, buf, sourcetype, source, NULL);
1410 } else if (zfs_prop_written(pl->pl_user_prop)) {
1411 sourcetype = ZPROP_SRC_LOCAL;
1413 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1414 buf, sizeof (buf), cbp->cb_literal) != 0) {
1415 sourcetype = ZPROP_SRC_NONE;
1416 (void) strlcpy(buf, "-", sizeof (buf));
1419 zprop_print_one_property(zfs_get_name(zhp), cbp,
1420 pl->pl_user_prop, buf, sourcetype, source, NULL);
1422 if (nvlist_lookup_nvlist(user_props,
1423 pl->pl_user_prop, &propval) != 0) {
1426 sourcetype = ZPROP_SRC_NONE;
1429 verify(nvlist_lookup_string(propval,
1430 ZPROP_VALUE, &strval) == 0);
1431 verify(nvlist_lookup_string(propval,
1432 ZPROP_SOURCE, &sourceval) == 0);
1434 if (strcmp(sourceval,
1435 zfs_get_name(zhp)) == 0) {
1436 sourcetype = ZPROP_SRC_LOCAL;
1437 } else if (strcmp(sourceval,
1438 ZPROP_SOURCE_VAL_RECVD) == 0) {
1439 sourcetype = ZPROP_SRC_RECEIVED;
1441 sourcetype = ZPROP_SRC_INHERITED;
1442 (void) strlcpy(source,
1443 sourceval, sizeof (source));
1447 if (received && (zfs_prop_get_recvd(zhp,
1448 pl->pl_user_prop, rbuf, sizeof (rbuf),
1449 cbp->cb_literal) == 0))
1452 zprop_print_one_property(zfs_get_name(zhp), cbp,
1453 pl->pl_user_prop, strval, sourcetype,
1462 zfs_do_get(int argc, char **argv)
1464 zprop_get_cbdata_t cb = { 0 };
1465 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1466 int types = ZFS_TYPE_DATASET;
1467 char *value, *fields;
1470 zprop_list_t fake_name = { 0 };
1473 * Set up default columns and sources.
1475 cb.cb_sources = ZPROP_SRC_ALL;
1476 cb.cb_columns[0] = GET_COL_NAME;
1477 cb.cb_columns[1] = GET_COL_PROPERTY;
1478 cb.cb_columns[2] = GET_COL_VALUE;
1479 cb.cb_columns[3] = GET_COL_SOURCE;
1480 cb.cb_type = ZFS_TYPE_DATASET;
1483 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1486 cb.cb_literal = B_TRUE;
1489 limit = parse_depth(optarg, &flags);
1492 flags |= ZFS_ITER_RECURSE;
1495 cb.cb_scripted = B_TRUE;
1498 (void) fprintf(stderr, gettext("missing argument for "
1499 "'%c' option\n"), optopt);
1504 * Process the set of columns to display. We zero out
1505 * the structure to give us a blank slate.
1507 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1509 while (*optarg != '\0') {
1510 static char *col_subopts[] =
1511 { "name", "property", "value", "received",
1512 "source", "all", NULL };
1514 if (i == ZFS_GET_NCOLS) {
1515 (void) fprintf(stderr, gettext("too "
1516 "many fields given to -o "
1521 switch (getsubopt(&optarg, col_subopts,
1524 cb.cb_columns[i++] = GET_COL_NAME;
1527 cb.cb_columns[i++] = GET_COL_PROPERTY;
1530 cb.cb_columns[i++] = GET_COL_VALUE;
1533 cb.cb_columns[i++] = GET_COL_RECVD;
1534 flags |= ZFS_ITER_RECVD_PROPS;
1537 cb.cb_columns[i++] = GET_COL_SOURCE;
1541 (void) fprintf(stderr,
1542 gettext("\"all\" conflicts "
1543 "with specific fields "
1544 "given to -o option\n"));
1547 cb.cb_columns[0] = GET_COL_NAME;
1548 cb.cb_columns[1] = GET_COL_PROPERTY;
1549 cb.cb_columns[2] = GET_COL_VALUE;
1550 cb.cb_columns[3] = GET_COL_RECVD;
1551 cb.cb_columns[4] = GET_COL_SOURCE;
1552 flags |= ZFS_ITER_RECVD_PROPS;
1556 (void) fprintf(stderr,
1557 gettext("invalid column name "
1566 while (*optarg != '\0') {
1567 static char *source_subopts[] = {
1568 "local", "default", "inherited",
1569 "received", "temporary", "none",
1572 switch (getsubopt(&optarg, source_subopts,
1575 cb.cb_sources |= ZPROP_SRC_LOCAL;
1578 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1581 cb.cb_sources |= ZPROP_SRC_INHERITED;
1584 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1587 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1590 cb.cb_sources |= ZPROP_SRC_NONE;
1593 (void) fprintf(stderr,
1594 gettext("invalid source "
1603 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1604 while (*optarg != '\0') {
1605 static char *type_subopts[] = { "filesystem",
1606 "volume", "snapshot", "all", NULL };
1608 switch (getsubopt(&optarg, type_subopts,
1611 types |= ZFS_TYPE_FILESYSTEM;
1614 types |= ZFS_TYPE_VOLUME;
1617 types |= ZFS_TYPE_SNAPSHOT;
1620 types = ZFS_TYPE_DATASET;
1624 (void) fprintf(stderr,
1625 gettext("invalid type '%s'\n"),
1633 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1643 (void) fprintf(stderr, gettext("missing property "
1650 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1658 * As part of zfs_expand_proplist(), we keep track of the maximum column
1659 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1660 * need to know the maximum name length. However, the user likely did
1661 * not specify 'name' as one of the properties to fetch, so we need to
1662 * make sure we always include at least this property for
1663 * print_get_headers() to work properly.
1665 if (cb.cb_proplist != NULL) {
1666 fake_name.pl_prop = ZFS_PROP_NAME;
1667 fake_name.pl_width = strlen(gettext("NAME"));
1668 fake_name.pl_next = cb.cb_proplist;
1669 cb.cb_proplist = &fake_name;
1672 cb.cb_first = B_TRUE;
1674 /* run for each object */
1675 ret = zfs_for_each(argc, argv, flags, types, NULL,
1676 &cb.cb_proplist, limit, get_callback, &cb);
1678 if (cb.cb_proplist == &fake_name)
1679 zprop_free_list(fake_name.pl_next);
1681 zprop_free_list(cb.cb_proplist);
1687 * inherit [-rS] <property> <fs|vol> ...
1689 * -r Recurse over all children
1690 * -S Revert to received value, if any
1692 * For each dataset specified on the command line, inherit the given property
1693 * from its parent. Inheriting a property at the pool level will cause it to
1694 * use the default value. The '-r' flag will recurse over all children, and is
1695 * useful for setting a property on a hierarchy-wide basis, regardless of any
1696 * local modifications for each dataset.
1699 typedef struct inherit_cbdata {
1700 const char *cb_propname;
1701 boolean_t cb_received;
1705 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1707 inherit_cbdata_t *cb = data;
1708 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1711 * If we're doing it recursively, then ignore properties that
1712 * are not valid for this type of dataset.
1714 if (prop != ZPROP_INVAL &&
1715 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1718 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1722 inherit_cb(zfs_handle_t *zhp, void *data)
1724 inherit_cbdata_t *cb = data;
1726 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1730 zfs_do_inherit(int argc, char **argv)
1734 inherit_cbdata_t cb = { 0 };
1738 boolean_t received = B_FALSE;
1741 while ((c = getopt(argc, argv, "rS")) != -1) {
1744 flags |= ZFS_ITER_RECURSE;
1751 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1760 /* check number of arguments */
1762 (void) fprintf(stderr, gettext("missing property argument\n"));
1766 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1774 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1775 if (zfs_prop_readonly(prop)) {
1776 (void) fprintf(stderr, gettext(
1777 "%s property is read-only\n"),
1781 if (!zfs_prop_inheritable(prop) && !received) {
1782 (void) fprintf(stderr, gettext("'%s' property cannot "
1783 "be inherited\n"), propname);
1784 if (prop == ZFS_PROP_QUOTA ||
1785 prop == ZFS_PROP_RESERVATION ||
1786 prop == ZFS_PROP_REFQUOTA ||
1787 prop == ZFS_PROP_REFRESERVATION)
1788 (void) fprintf(stderr, gettext("use 'zfs set "
1789 "%s=none' to clear\n"), propname);
1792 if (received && (prop == ZFS_PROP_VOLSIZE ||
1793 prop == ZFS_PROP_VERSION)) {
1794 (void) fprintf(stderr, gettext("'%s' property cannot "
1795 "be reverted to a received value\n"), propname);
1798 } else if (!zfs_prop_user(propname)) {
1799 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1804 cb.cb_propname = propname;
1805 cb.cb_received = received;
1807 if (flags & ZFS_ITER_RECURSE) {
1808 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1809 NULL, NULL, 0, inherit_recurse_cb, &cb);
1811 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1812 NULL, NULL, 0, inherit_cb, &cb);
1818 typedef struct upgrade_cbdata {
1819 uint64_t cb_numupgraded;
1820 uint64_t cb_numsamegraded;
1821 uint64_t cb_numfailed;
1822 uint64_t cb_version;
1824 boolean_t cb_foundone;
1825 char cb_lastfs[ZFS_MAXNAMELEN];
1829 same_pool(zfs_handle_t *zhp, const char *name)
1831 int len1 = strcspn(name, "/@");
1832 const char *zhname = zfs_get_name(zhp);
1833 int len2 = strcspn(zhname, "/@");
1837 return (strncmp(name, zhname, len1) == 0);
1841 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1843 upgrade_cbdata_t *cb = data;
1844 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1846 /* list if it's old/new */
1847 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1848 (cb->cb_newer && version > ZPL_VERSION)) {
1851 str = gettext("The following filesystems are "
1852 "formatted using a newer software version and\n"
1853 "cannot be accessed on the current system.\n\n");
1855 str = gettext("The following filesystems are "
1856 "out of date, and can be upgraded. After being\n"
1857 "upgraded, these filesystems (and any 'zfs send' "
1858 "streams generated from\n"
1859 "subsequent snapshots) will no longer be "
1860 "accessible by older software versions.\n\n");
1863 if (!cb->cb_foundone) {
1865 (void) printf(gettext("VER FILESYSTEM\n"));
1866 (void) printf(gettext("--- ------------\n"));
1867 cb->cb_foundone = B_TRUE;
1870 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1877 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1879 upgrade_cbdata_t *cb = data;
1880 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1881 int needed_spa_version;
1884 if (zfs_spa_version(zhp, &spa_version) < 0)
1887 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1889 if (needed_spa_version < 0)
1892 if (spa_version < needed_spa_version) {
1894 (void) printf(gettext("%s: can not be "
1895 "upgraded; the pool version needs to first "
1896 "be upgraded\nto version %d\n\n"),
1897 zfs_get_name(zhp), needed_spa_version);
1903 if (version < cb->cb_version) {
1905 (void) snprintf(verstr, sizeof (verstr),
1906 "%llu", (u_longlong_t)cb->cb_version);
1907 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1909 * If they did "zfs upgrade -a", then we could
1910 * be doing ioctls to different pools. We need
1911 * to log this history once to each pool.
1913 verify(zpool_stage_history(g_zfs, history_str) == 0);
1915 if (zfs_prop_set(zhp, "version", verstr) == 0)
1916 cb->cb_numupgraded++;
1919 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1920 } else if (version > cb->cb_version) {
1921 /* can't downgrade */
1922 (void) printf(gettext("%s: can not be downgraded; "
1923 "it is already at version %u\n"),
1924 zfs_get_name(zhp), version);
1927 cb->cb_numsamegraded++;
1935 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1938 zfs_do_upgrade(int argc, char **argv)
1940 boolean_t all = B_FALSE;
1941 boolean_t showversions = B_FALSE;
1943 upgrade_cbdata_t cb = { 0 };
1945 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1948 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1951 flags |= ZFS_ITER_RECURSE;
1954 showversions = B_TRUE;
1957 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1958 optarg, &cb.cb_version) != 0) {
1959 (void) fprintf(stderr,
1960 gettext("invalid version %s\n"), optarg);
1969 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1978 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1980 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1981 cb.cb_version || argc))
1983 if ((all || argc) && (showversions))
1989 /* Show info on available versions. */
1990 (void) printf(gettext("The following filesystem versions are "
1992 (void) printf(gettext("VER DESCRIPTION\n"));
1993 (void) printf("--- -----------------------------------------"
1994 "---------------\n");
1995 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1996 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1997 (void) printf(gettext(" 3 Case insensitive and filesystem "
1998 "user identifier (FUID)\n"));
1999 (void) printf(gettext(" 4 userquota, groupquota "
2001 (void) printf(gettext(" 5 System attributes\n"));
2002 (void) printf(gettext("\nFor more information on a particular "
2003 "version, including supported releases,\n"));
2004 (void) printf("see the ZFS Administration Guide.\n\n");
2006 } else if (argc || all) {
2007 /* Upgrade filesystems */
2008 if (cb.cb_version == 0)
2009 cb.cb_version = ZPL_VERSION;
2010 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2011 NULL, NULL, 0, upgrade_set_callback, &cb);
2012 (void) printf(gettext("%llu filesystems upgraded\n"),
2013 (u_longlong_t)cb.cb_numupgraded);
2014 if (cb.cb_numsamegraded) {
2015 (void) printf(gettext("%llu filesystems already at "
2017 (u_longlong_t)cb.cb_numsamegraded);
2019 if (cb.cb_numfailed != 0)
2022 /* List old-version filesytems */
2024 (void) printf(gettext("This system is currently running "
2025 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2027 flags |= ZFS_ITER_RECURSE;
2028 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2029 NULL, NULL, 0, upgrade_list_callback, &cb);
2031 found = cb.cb_foundone;
2032 cb.cb_foundone = B_FALSE;
2033 cb.cb_newer = B_TRUE;
2035 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2036 NULL, NULL, 0, upgrade_list_callback, &cb);
2038 if (!cb.cb_foundone && !found) {
2039 (void) printf(gettext("All filesystems are "
2040 "formatted with the current version.\n"));
2048 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2049 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2050 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2051 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2053 * -H Scripted mode; elide headers and separate columns by tabs.
2054 * -i Translate SID to POSIX ID.
2055 * -n Print numeric ID instead of user/group name.
2056 * -o Control which fields to display.
2057 * -p Use exact (parseable) numeric output.
2058 * -s Specify sort columns, descending order.
2059 * -S Specify sort columns, ascending order.
2060 * -t Control which object types to display.
2062 * Displays space consumed by, and quotas on, each user in the specified
2063 * filesystem or snapshot.
2066 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2067 enum us_field_types {
2073 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2074 static char *us_field_names[] = { "type", "name", "used", "quota" };
2075 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2077 #define USTYPE_PSX_GRP (1 << 0)
2078 #define USTYPE_PSX_USR (1 << 1)
2079 #define USTYPE_SMB_GRP (1 << 2)
2080 #define USTYPE_SMB_USR (1 << 3)
2081 #define USTYPE_ALL \
2082 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2084 static int us_type_bits[] = {
2091 static char *us_type_names[] = { "posixgroup", "posxiuser", "smbgroup",
2094 typedef struct us_node {
2096 uu_avl_node_t usn_avlnode;
2097 uu_list_node_t usn_listnode;
2100 typedef struct us_cbdata {
2102 uu_avl_pool_t *cb_avl_pool;
2104 boolean_t cb_numname;
2105 boolean_t cb_nicenum;
2106 boolean_t cb_sid2posix;
2107 zfs_userquota_prop_t cb_prop;
2108 zfs_sort_column_t *cb_sortcol;
2109 size_t cb_width[USFIELD_LAST];
2112 static boolean_t us_populated = B_FALSE;
2115 zfs_sort_column_t *si_sortcol;
2116 boolean_t si_numname;
2120 us_field_index(char *field)
2124 for (i = 0; i < USFIELD_LAST; i++) {
2125 if (strcmp(field, us_field_names[i]) == 0)
2133 us_compare(const void *larg, const void *rarg, void *unused)
2135 const us_node_t *l = larg;
2136 const us_node_t *r = rarg;
2137 us_sort_info_t *si = (us_sort_info_t *)unused;
2138 zfs_sort_column_t *sortcol = si->si_sortcol;
2139 boolean_t numname = si->si_numname;
2140 nvlist_t *lnvl = l->usn_nvl;
2141 nvlist_t *rnvl = r->usn_nvl;
2145 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2152 zfs_prop_t prop = sortcol->sc_prop;
2153 const char *propname = NULL;
2154 boolean_t reverse = sortcol->sc_reverse;
2159 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2160 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2162 rc = (rv32 < lv32) ? 1 : -1;
2167 (void) nvlist_lookup_uint64(lnvl, propname,
2169 (void) nvlist_lookup_uint64(rnvl, propname,
2172 rc = (rv64 < lv64) ? 1 : -1;
2174 (void) nvlist_lookup_string(lnvl, propname,
2176 (void) nvlist_lookup_string(rnvl, propname,
2178 rc = strcmp(lvstr, rvstr);
2182 case ZFS_PROP_QUOTA:
2185 if (prop == ZFS_PROP_USED)
2189 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2190 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2192 rc = (rv64 < lv64) ? 1 : -1;
2200 return (reverse ? 1 : -1);
2202 return (reverse ? -1 : 1);
2207 * If entries still seem to be the same, check if they are of the same
2208 * type (smbentity is added only if we are doing SID to POSIX ID
2209 * translation where we can have duplicate type/name combinations).
2211 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2212 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2214 return (lvb < rvb ? -1 : 1);
2219 static inline const char *
2220 us_type2str(unsigned field_type)
2222 switch (field_type) {
2223 case USTYPE_PSX_USR:
2224 return ("POSIX User");
2225 case USTYPE_PSX_GRP:
2226 return ("POSIX Group");
2227 case USTYPE_SMB_USR:
2228 return ("SMB User");
2229 case USTYPE_SMB_GRP:
2230 return ("SMB Group");
2232 return ("Undefined");
2237 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2239 us_cbdata_t *cb = (us_cbdata_t *)arg;
2240 zfs_userquota_prop_t prop = cb->cb_prop;
2245 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2246 uu_avl_t *avl = cb->cb_avl;
2250 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2252 const char *typestr;
2256 int typeidx, nameidx, sizeidx;
2257 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2258 boolean_t smbentity = B_FALSE;
2260 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2262 node = safe_malloc(sizeof (us_node_t));
2263 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2264 node->usn_nvl = props;
2266 if (domain != NULL && domain[0] != '\0') {
2269 char sid[ZFS_MAXNAMELEN + 32];
2273 directory_error_t e;
2277 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2279 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2280 type = USTYPE_SMB_GRP;
2281 err = sid_to_id(sid, B_FALSE, &id);
2283 type = USTYPE_SMB_USR;
2284 err = sid_to_id(sid, B_TRUE, &id);
2289 if (!cb->cb_sid2posix) {
2290 e = directory_name_from_sid(NULL, sid, &name,
2293 directory_error_free(e);
2303 #endif /* HAVE_IDMAP */
2306 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2308 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2309 type = USTYPE_PSX_GRP;
2310 if (!cb->cb_numname) {
2313 if ((g = getgrgid(rid)) != NULL)
2317 type = USTYPE_PSX_USR;
2318 if (!cb->cb_numname) {
2321 if ((p = getpwuid(rid)) != NULL)
2328 * Make sure that the type/name combination is unique when doing
2329 * SID to POSIX ID translation (hence changing the type from SMB to
2332 if (cb->cb_sid2posix &&
2333 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2336 /* Calculate/update width of TYPE field */
2337 typestr = us_type2str(type);
2338 typelen = strlen(gettext(typestr));
2339 typeidx = us_field_index("type");
2340 if (typelen > cb->cb_width[typeidx])
2341 cb->cb_width[typeidx] = typelen;
2342 if (nvlist_add_uint32(props, "type", type) != 0)
2345 /* Calculate/update width of NAME field */
2346 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2347 if (nvlist_add_uint64(props, "name", rid) != 0)
2349 namelen = snprintf(NULL, 0, "%u", rid);
2351 if (nvlist_add_string(props, "name", name) != 0)
2353 namelen = strlen(name);
2355 nameidx = us_field_index("name");
2356 if (namelen > cb->cb_width[nameidx])
2357 cb->cb_width[nameidx] = namelen;
2360 * Check if this type/name combination is in the list and update it;
2361 * otherwise add new node to the list.
2363 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2364 uu_avl_insert(avl, node, idx);
2369 props = node->usn_nvl;
2372 /* Calculate/update width of USED/QUOTA fields */
2374 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2376 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
2377 (u_longlong_t)space);
2378 sizelen = strlen(sizebuf);
2379 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2381 if (!nvlist_exists(props, "quota"))
2382 (void) nvlist_add_uint64(props, "quota", 0);
2385 if (!nvlist_exists(props, "used"))
2386 (void) nvlist_add_uint64(props, "used", 0);
2388 sizeidx = us_field_index(propname);
2389 if (sizelen > cb->cb_width[sizeidx])
2390 cb->cb_width[sizeidx] = sizelen;
2392 if (nvlist_add_uint64(props, propname, space) != 0)
2399 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2400 size_t *width, us_node_t *node)
2402 nvlist_t *nvl = node->usn_nvl;
2403 char valstr[ZFS_MAXNAMELEN];
2404 boolean_t first = B_TRUE;
2410 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2411 if (!(ustype & types))
2414 while ((field = fields[cfield]) != USFIELD_LAST) {
2415 nvpair_t *nvp = NULL;
2419 char *strval = NULL;
2421 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2422 if (strcmp(nvpair_name(nvp),
2423 us_field_names[field]) == 0)
2427 type = nvpair_type(nvp);
2429 case DATA_TYPE_UINT32:
2430 (void) nvpair_value_uint32(nvp, &val32);
2432 case DATA_TYPE_UINT64:
2433 (void) nvpair_value_uint64(nvp, &val64);
2435 case DATA_TYPE_STRING:
2436 (void) nvpair_value_string(nvp, &strval);
2439 (void) fprintf(stderr, "invalid data type\n");
2444 strval = (char *)us_type2str(val32);
2447 if (type == DATA_TYPE_UINT64) {
2448 (void) sprintf(valstr, "%llu",
2449 (u_longlong_t) val64);
2455 if (type == DATA_TYPE_UINT64) {
2457 (void) sprintf(valstr, "%llu",
2458 (u_longlong_t) val64);
2460 zfs_nicenum(val64, valstr,
2463 if (field == USFIELD_QUOTA &&
2464 strcmp(valstr, "0") == 0)
2474 (void) printf("\t");
2479 (void) printf("%s", strval);
2480 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2481 (void) printf("%-*s", (int) width[field], strval);
2483 (void) printf("%*s", (int) width[field], strval);
2489 (void) printf("\n");
2493 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2494 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2502 boolean_t first = B_TRUE;
2504 while ((field = fields[cfield]) != USFIELD_LAST) {
2505 col = gettext(us_field_hdr[field]);
2506 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2507 (void) printf(first ? "%-*s" : " %-*s",
2508 (int) width[field], col);
2510 (void) printf(first ? "%*s" : " %*s",
2511 (int) width[field], col);
2516 (void) printf("\n");
2519 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2520 print_us_node(scripted, parsable, fields, types, width, node);
2522 nvlist_free(node->usn_nvl);
2527 zfs_do_userspace(int argc, char **argv)
2530 zfs_userquota_prop_t p;
2531 uu_avl_pool_t *avl_pool;
2533 uu_avl_walk_t *walk;
2535 char deffields[] = "type,name,used,quota";
2536 char *ofield = NULL;
2537 char *tfield = NULL;
2541 boolean_t scripted = B_FALSE;
2542 boolean_t prtnum = B_FALSE;
2543 boolean_t parsable = B_FALSE;
2544 boolean_t sid2posix = B_FALSE;
2547 zfs_sort_column_t *sortcol = NULL;
2548 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2552 uu_list_pool_t *listpool;
2554 uu_avl_index_t idx = 0;
2555 uu_list_index_t idx2 = 0;
2560 if (strcmp(argv[0], "groupspace") == 0)
2561 /* Toggle default group types */
2562 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2564 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2580 if (zfs_add_sort_column(&sortcol, optarg,
2581 c == 's' ? B_FALSE : B_TRUE) != 0) {
2582 (void) fprintf(stderr,
2583 gettext("invalid field '%s'\n"), optarg);
2594 (void) fprintf(stderr, gettext("missing argument for "
2595 "'%c' option\n"), optopt);
2599 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2609 (void) fprintf(stderr, gettext("missing dataset name\n"));
2613 (void) fprintf(stderr, gettext("too many arguments\n"));
2617 /* Use default output fields if not specified using -o */
2621 if ((delim = strchr(ofield, ',')) != NULL)
2623 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2624 (void) fprintf(stderr, gettext("invalid type '%s' "
2625 "for -o option\n"), ofield);
2630 } while (delim != NULL);
2631 fields[cfield] = USFIELD_LAST;
2633 /* Override output types (-t option) */
2634 if (tfield != NULL) {
2638 boolean_t found = B_FALSE;
2640 if ((delim = strchr(tfield, ',')) != NULL)
2642 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2644 if (strcmp(tfield, us_type_names[i]) == 0) {
2646 types |= us_type_bits[i];
2651 (void) fprintf(stderr, gettext("invalid type "
2652 "'%s' for -t option\n"), tfield);
2657 } while (delim != NULL);
2660 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2663 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2664 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2666 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2669 /* Always add default sorting columns */
2670 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2671 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2673 cb.cb_sortcol = sortcol;
2674 cb.cb_numname = prtnum;
2675 cb.cb_nicenum = !parsable;
2676 cb.cb_avl_pool = avl_pool;
2677 cb.cb_avl = avl_tree;
2678 cb.cb_sid2posix = sid2posix;
2680 for (i = 0; i < USFIELD_LAST; i++)
2681 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2683 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2684 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2685 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2686 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2687 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2690 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2695 if ((node = uu_avl_first(avl_tree)) == NULL)
2698 us_populated = B_TRUE;
2700 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2701 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2702 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2703 uu_list_node_init(node, &node->usn_listnode, listpool);
2705 while (node != NULL) {
2707 node = uu_avl_next(avl_tree, node);
2708 uu_avl_remove(avl_tree, rmnode);
2709 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2710 uu_list_insert(list, rmnode, idx2);
2713 for (node = uu_list_first(list); node != NULL;
2714 node = uu_list_next(list, node)) {
2715 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2717 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2718 uu_avl_insert(avl_tree, node, idx);
2721 uu_list_destroy(list);
2722 uu_list_pool_destroy(listpool);
2724 /* Print and free node nvlist memory */
2725 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2728 zfs_free_sort_columns(sortcol);
2730 /* Clean up the AVL tree */
2731 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2734 while ((node = uu_avl_walk_next(walk)) != NULL) {
2735 uu_avl_remove(cb.cb_avl, node);
2739 uu_avl_walk_end(walk);
2740 uu_avl_destroy(avl_tree);
2741 uu_avl_pool_destroy(avl_pool);
2747 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2748 * [-s property [-s property]...] [-S property [-S property]...]
2751 * -r Recurse over all children
2752 * -d Limit recursion by depth.
2753 * -H Scripted mode; elide headers and separate columns by tabs
2754 * -o Control which fields to display.
2755 * -t Control which object types to display.
2756 * -s Specify sort columns, descending order.
2757 * -S Specify sort columns, ascending order.
2759 * When given no arguments, lists all filesystems in the system.
2760 * Otherwise, list the specified datasets, optionally recursing down them if
2761 * '-r' is specified.
2763 typedef struct list_cbdata {
2765 boolean_t cb_scripted;
2766 zprop_list_t *cb_proplist;
2770 * Given a list of columns to display, output appropriate headers for each one.
2773 print_header(zprop_list_t *pl)
2775 char headerbuf[ZFS_MAXPROPLEN];
2778 boolean_t first = B_TRUE;
2779 boolean_t right_justify;
2781 for (; pl != NULL; pl = pl->pl_next) {
2788 right_justify = B_FALSE;
2789 if (pl->pl_prop != ZPROP_INVAL) {
2790 header = zfs_prop_column_name(pl->pl_prop);
2791 right_justify = zfs_prop_align_right(pl->pl_prop);
2793 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2794 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2795 headerbuf[i] = '\0';
2799 if (pl->pl_next == NULL && !right_justify)
2800 (void) printf("%s", header);
2801 else if (right_justify)
2802 (void) printf("%*s", (int)pl->pl_width, header);
2804 (void) printf("%-*s", (int)pl->pl_width, header);
2807 (void) printf("\n");
2811 * Given a dataset and a list of fields, print out all the properties according
2812 * to the described layout.
2815 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
2817 boolean_t first = B_TRUE;
2818 char property[ZFS_MAXPROPLEN];
2819 nvlist_t *userprops = zfs_get_user_props(zhp);
2822 boolean_t right_justify;
2825 for (; pl != NULL; pl = pl->pl_next) {
2828 (void) printf("\t");
2835 if (pl->pl_prop == ZFS_PROP_NAME) {
2836 (void) strlcpy(property, zfs_get_name(zhp),
2839 right_justify = zfs_prop_align_right(pl->pl_prop);
2840 } else if (pl->pl_prop != ZPROP_INVAL) {
2841 if (zfs_prop_get(zhp, pl->pl_prop, property,
2842 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2847 right_justify = zfs_prop_align_right(pl->pl_prop);
2848 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2849 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2850 property, sizeof (property), B_FALSE) != 0)
2854 right_justify = B_TRUE;
2855 } else if (zfs_prop_written(pl->pl_user_prop)) {
2856 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2857 property, sizeof (property), B_FALSE) != 0)
2861 right_justify = B_TRUE;
2863 if (nvlist_lookup_nvlist(userprops,
2864 pl->pl_user_prop, &propval) != 0)
2867 verify(nvlist_lookup_string(propval,
2868 ZPROP_VALUE, &propstr) == 0);
2869 right_justify = B_FALSE;
2872 width = pl->pl_width;
2875 * If this is being called in scripted mode, or if this is the
2876 * last column and it is left-justified, don't include a width
2879 if (scripted || (pl->pl_next == NULL && !right_justify))
2880 (void) printf("%s", propstr);
2881 else if (right_justify)
2882 (void) printf("%*s", width, propstr);
2884 (void) printf("%-*s", width, propstr);
2887 (void) printf("\n");
2891 * Generic callback function to list a dataset or snapshot.
2894 list_callback(zfs_handle_t *zhp, void *data)
2896 list_cbdata_t *cbp = data;
2898 if (cbp->cb_first) {
2899 if (!cbp->cb_scripted)
2900 print_header(cbp->cb_proplist);
2901 cbp->cb_first = B_FALSE;
2904 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2910 zfs_do_list(int argc, char **argv)
2913 boolean_t scripted = B_FALSE;
2914 static char default_fields[] =
2915 "name,used,available,referenced,mountpoint";
2916 int types = ZFS_TYPE_DATASET;
2917 boolean_t types_specified = B_FALSE;
2918 char *fields = NULL;
2919 list_cbdata_t cb = { 0 };
2923 zfs_sort_column_t *sortcol = NULL;
2924 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2927 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2933 limit = parse_depth(optarg, &flags);
2936 flags |= ZFS_ITER_RECURSE;
2942 if (zfs_add_sort_column(&sortcol, optarg,
2944 (void) fprintf(stderr,
2945 gettext("invalid property '%s'\n"), optarg);
2950 if (zfs_add_sort_column(&sortcol, optarg,
2952 (void) fprintf(stderr,
2953 gettext("invalid property '%s'\n"), optarg);
2959 types_specified = B_TRUE;
2960 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2961 while (*optarg != '\0') {
2962 static char *type_subopts[] = { "filesystem",
2963 "volume", "snapshot", "snap", "all", NULL };
2965 switch (getsubopt(&optarg, type_subopts,
2968 types |= ZFS_TYPE_FILESYSTEM;
2971 types |= ZFS_TYPE_VOLUME;
2975 types |= ZFS_TYPE_SNAPSHOT;
2978 types = ZFS_TYPE_DATASET;
2982 (void) fprintf(stderr,
2983 gettext("invalid type '%s'\n"),
2990 (void) fprintf(stderr, gettext("missing argument for "
2991 "'%c' option\n"), optopt);
2995 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3005 fields = default_fields;
3008 * If we are only going to list snapshot names and sort by name,
3009 * then we can use faster version.
3011 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3012 flags |= ZFS_ITER_SIMPLE;
3015 * If "-o space" and no types were specified, don't display snapshots.
3017 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3018 types &= ~ZFS_TYPE_SNAPSHOT;
3021 * If the user specifies '-o all', the zprop_get_list() doesn't
3022 * normally include the name of the dataset. For 'zfs list', we always
3023 * want this property to be first.
3025 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3029 cb.cb_scripted = scripted;
3030 cb.cb_first = B_TRUE;
3032 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3033 limit, list_callback, &cb);
3035 zprop_free_list(cb.cb_proplist);
3036 zfs_free_sort_columns(sortcol);
3038 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3039 (void) fprintf(stderr, gettext("no datasets available\n"));
3045 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3046 * zfs rename [-f] -p <fs | vol> <fs | vol>
3047 * zfs rename -r <snap> <snap>
3049 * Renames the given dataset to another of the same type.
3051 * The '-p' flag creates all the non-existing ancestors of the target first.
3055 zfs_do_rename(int argc, char **argv)
3060 boolean_t recurse = B_FALSE;
3061 boolean_t parents = B_FALSE;
3062 boolean_t force_unmount = B_FALSE;
3065 while ((c = getopt(argc, argv, "prf")) != -1) {
3074 force_unmount = B_TRUE;
3078 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3087 /* check number of arguments */
3089 (void) fprintf(stderr, gettext("missing source dataset "
3094 (void) fprintf(stderr, gettext("missing target dataset "
3099 (void) fprintf(stderr, gettext("too many arguments\n"));
3103 if (recurse && parents) {
3104 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3109 if (recurse && strchr(argv[0], '@') == 0) {
3110 (void) fprintf(stderr, gettext("source dataset for recursive "
3111 "rename must be a snapshot\n"));
3115 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3116 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3119 /* If we were asked and the name looks good, try to create ancestors. */
3120 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3121 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3126 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3135 * Promotes the given clone fs to be the parent
3139 zfs_do_promote(int argc, char **argv)
3145 if (argc > 1 && argv[1][0] == '-') {
3146 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3151 /* check number of arguments */
3153 (void) fprintf(stderr, gettext("missing clone filesystem"
3158 (void) fprintf(stderr, gettext("too many arguments\n"));
3162 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3166 ret = (zfs_promote(zhp) != 0);
3174 * zfs rollback [-rRf] <snapshot>
3176 * -r Delete any intervening snapshots before doing rollback
3177 * -R Delete any snapshots and their clones
3178 * -f ignored for backwards compatability
3180 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3181 * since then and making it the active dataset. If more recent snapshots exist,
3182 * the command will complain unless the '-r' flag is given.
3184 typedef struct rollback_cbdata {
3190 boolean_t cb_recurse;
3191 boolean_t cb_dependent;
3192 } rollback_cbdata_t;
3195 * Report any snapshots more recent than the one specified. Used when '-r' is
3196 * not specified. We reuse this same callback for the snapshot dependents - if
3197 * 'cb_dependent' is set, then this is a dependent and we should report it
3198 * without checking the transaction group.
3201 rollback_check(zfs_handle_t *zhp, void *data)
3203 rollback_cbdata_t *cbp = data;
3205 if (cbp->cb_doclones) {
3210 if (!cbp->cb_dependent) {
3211 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
3212 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3213 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3216 if (cbp->cb_first && !cbp->cb_recurse) {
3217 (void) fprintf(stderr, gettext("cannot "
3218 "rollback to '%s': more recent snapshots "
3221 (void) fprintf(stderr, gettext("use '-r' to "
3222 "force deletion of the following "
3228 if (cbp->cb_recurse) {
3229 cbp->cb_dependent = B_TRUE;
3230 if (zfs_iter_dependents(zhp, B_TRUE,
3231 rollback_check, cbp) != 0) {
3235 cbp->cb_dependent = B_FALSE;
3237 (void) fprintf(stderr, "%s\n",
3242 if (cbp->cb_first && cbp->cb_recurse) {
3243 (void) fprintf(stderr, gettext("cannot rollback to "
3244 "'%s': clones of previous snapshots exist\n"),
3246 (void) fprintf(stderr, gettext("use '-R' to "
3247 "force deletion of the following clones and "
3253 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3261 zfs_do_rollback(int argc, char **argv)
3265 boolean_t force = B_FALSE;
3266 rollback_cbdata_t cb = { 0 };
3267 zfs_handle_t *zhp, *snap;
3268 char parentname[ZFS_MAXNAMELEN];
3272 while ((c = getopt(argc, argv, "rRf")) != -1) {
3285 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3294 /* check number of arguments */
3296 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3300 (void) fprintf(stderr, gettext("too many arguments\n"));
3304 /* open the snapshot */
3305 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3308 /* open the parent dataset */
3309 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3310 verify((delim = strrchr(parentname, '@')) != NULL);
3312 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3318 * Check for more recent snapshots and/or clones based on the presence
3321 cb.cb_target = argv[0];
3322 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3323 cb.cb_first = B_TRUE;
3325 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
3328 if ((ret = cb.cb_error) != 0)
3332 * Rollback parent to the given snapshot.
3334 ret = zfs_rollback(zhp, snap, force);
3347 * zfs set property=value { fs | snap | vol } ...
3349 * Sets the given property for all datasets specified on the command line.
3351 typedef struct set_cbdata {
3357 set_callback(zfs_handle_t *zhp, void *data)
3359 set_cbdata_t *cbp = data;
3361 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3362 switch (libzfs_errno(g_zfs)) {
3363 case EZFS_MOUNTFAILED:
3364 (void) fprintf(stderr, gettext("property may be set "
3365 "but unable to remount filesystem\n"));
3367 case EZFS_SHARENFSFAILED:
3368 (void) fprintf(stderr, gettext("property may be set "
3369 "but unable to reshare filesystem\n"));
3378 zfs_do_set(int argc, char **argv)
3383 /* check for options */
3384 if (argc > 1 && argv[1][0] == '-') {
3385 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3390 /* check number of arguments */
3392 (void) fprintf(stderr, gettext("missing property=value "
3397 (void) fprintf(stderr, gettext("missing dataset name\n"));
3401 /* validate property=value argument */
3402 cb.cb_propname = argv[1];
3403 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3404 (cb.cb_value[1] == '\0')) {
3405 (void) fprintf(stderr, gettext("missing value in "
3406 "property=value argument\n"));
3410 *cb.cb_value = '\0';
3413 if (*cb.cb_propname == '\0') {
3414 (void) fprintf(stderr,
3415 gettext("missing property in property=value argument\n"));
3419 ret = zfs_for_each(argc - 2, argv + 2, 0,
3420 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3426 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3428 * Creates a snapshot with the given name. While functionally equivalent to
3429 * 'zfs create', it is a separate command to differentiate intent.
3432 zfs_do_snapshot(int argc, char **argv)
3434 boolean_t recursive = B_FALSE;
3439 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3443 while ((c = getopt(argc, argv, "ro:")) != -1) {
3446 if (parseprop(props))
3453 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3462 /* check number of arguments */
3464 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3468 (void) fprintf(stderr, gettext("too many arguments\n"));
3472 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
3474 if (ret && recursive)
3475 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3485 * Send a backup stream to stdout.
3488 zfs_do_send(int argc, char **argv)
3490 char *fromname = NULL;
3491 char *toname = NULL;
3494 sendflags_t flags = { 0 };
3496 nvlist_t *dbgnv = NULL;
3497 boolean_t extraverbose = B_FALSE;
3500 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3511 flags.doall = B_TRUE;
3514 flags.replicate = B_TRUE;
3517 flags.props = B_TRUE;
3520 flags.parsable = B_TRUE;
3521 flags.verbose = B_TRUE;
3525 extraverbose = B_TRUE;
3526 flags.verbose = B_TRUE;
3527 flags.progress = B_TRUE;
3530 flags.dedup = B_TRUE;
3533 flags.dryrun = B_TRUE;
3536 (void) fprintf(stderr, gettext("missing argument for "
3537 "'%c' option\n"), optopt);
3541 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3550 /* check number of arguments */
3552 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3556 (void) fprintf(stderr, gettext("too many arguments\n"));
3560 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3561 (void) fprintf(stderr,
3562 gettext("Error: Stream can not be written to a terminal.\n"
3563 "You must redirect standard output.\n"));
3567 cp = strchr(argv[0], '@');
3569 (void) fprintf(stderr,
3570 gettext("argument must be a snapshot\n"));
3575 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3580 * If they specified the full path to the snapshot, chop off
3581 * everything except the short name of the snapshot, but special
3582 * case if they specify the origin.
3584 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3585 char origin[ZFS_MAXNAMELEN];
3588 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3589 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3591 if (strcmp(origin, fromname) == 0) {
3593 flags.fromorigin = B_TRUE;
3596 if (cp != fromname && strcmp(argv[0], fromname)) {
3597 (void) fprintf(stderr,
3598 gettext("incremental source must be "
3599 "in same filesystem\n"));
3603 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3604 (void) fprintf(stderr,
3605 gettext("invalid incremental source\n"));
3611 if (flags.replicate && fromname == NULL)
3612 flags.doall = B_TRUE;
3614 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3615 extraverbose ? &dbgnv : NULL);
3617 if (extraverbose && dbgnv != NULL) {
3619 * dump_nvlist prints to stdout, but that's been
3620 * redirected to a file. Make it print to stderr
3623 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3624 dump_nvlist(dbgnv, 0);
3633 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3635 * Restore a backup stream from stdin.
3638 zfs_do_receive(int argc, char **argv)
3641 recvflags_t flags = { 0 };
3644 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3647 flags.isprefix = B_TRUE;
3650 flags.isprefix = B_TRUE;
3651 flags.istail = B_TRUE;
3654 flags.dryrun = B_TRUE;
3657 flags.nomount = B_TRUE;
3660 flags.verbose = B_TRUE;
3663 flags.force = B_TRUE;
3666 (void) fprintf(stderr, gettext("missing argument for "
3667 "'%c' option\n"), optopt);
3671 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3680 /* check number of arguments */
3682 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3686 (void) fprintf(stderr, gettext("too many arguments\n"));
3690 if (isatty(STDIN_FILENO)) {
3691 (void) fprintf(stderr,
3692 gettext("Error: Backup stream can not be read "
3693 "from a terminal.\n"
3694 "You must redirect standard input.\n"));
3698 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3704 * allow/unallow stuff
3706 /* copied from zfs/sys/dsl_deleg.h */
3707 #define ZFS_DELEG_PERM_CREATE "create"
3708 #define ZFS_DELEG_PERM_DESTROY "destroy"
3709 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3710 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3711 #define ZFS_DELEG_PERM_CLONE "clone"
3712 #define ZFS_DELEG_PERM_PROMOTE "promote"
3713 #define ZFS_DELEG_PERM_RENAME "rename"
3714 #define ZFS_DELEG_PERM_MOUNT "mount"
3715 #define ZFS_DELEG_PERM_SHARE "share"
3716 #define ZFS_DELEG_PERM_SEND "send"
3717 #define ZFS_DELEG_PERM_RECEIVE "receive"
3718 #define ZFS_DELEG_PERM_ALLOW "allow"
3719 #define ZFS_DELEG_PERM_USERPROP "userprop"
3720 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3721 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3722 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3723 #define ZFS_DELEG_PERM_USERUSED "userused"
3724 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3725 #define ZFS_DELEG_PERM_HOLD "hold"
3726 #define ZFS_DELEG_PERM_RELEASE "release"
3727 #define ZFS_DELEG_PERM_DIFF "diff"
3729 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3731 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3732 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3733 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3734 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3735 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3736 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3737 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3738 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3739 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3740 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3741 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3742 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3743 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3744 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3745 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3746 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3748 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3749 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3750 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3751 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3752 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3753 { NULL, ZFS_DELEG_NOTE_NONE }
3756 /* permission structure */
3757 typedef struct deleg_perm {
3758 zfs_deleg_who_type_t dp_who_type;
3759 const char *dp_name;
3761 boolean_t dp_descend;
3765 typedef struct deleg_perm_node {
3766 deleg_perm_t dpn_perm;
3768 uu_avl_node_t dpn_avl_node;
3769 } deleg_perm_node_t;
3771 typedef struct fs_perm fs_perm_t;
3773 /* permissions set */
3774 typedef struct who_perm {
3775 zfs_deleg_who_type_t who_type;
3776 const char *who_name; /* id */
3777 char who_ug_name[256]; /* user/group name */
3778 fs_perm_t *who_fsperm; /* uplink */
3780 uu_avl_t *who_deleg_perm_avl; /* permissions */
3784 typedef struct who_perm_node {
3785 who_perm_t who_perm;
3786 uu_avl_node_t who_avl_node;
3789 typedef struct fs_perm_set fs_perm_set_t;
3790 /* fs permissions */
3792 const char *fsp_name;
3794 uu_avl_t *fsp_sc_avl; /* sets,create */
3795 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3797 fs_perm_set_t *fsp_set; /* uplink */
3801 typedef struct fs_perm_node {
3802 fs_perm_t fspn_fsperm;
3805 uu_list_node_t fspn_list_node;
3808 /* top level structure */
3809 struct fs_perm_set {
3810 uu_list_pool_t *fsps_list_pool;
3811 uu_list_t *fsps_list; /* list of fs_perms */
3813 uu_avl_pool_t *fsps_named_set_avl_pool;
3814 uu_avl_pool_t *fsps_who_perm_avl_pool;
3815 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
3818 static inline const char *
3819 deleg_perm_type(zfs_deleg_note_t note)
3825 case ZFS_DELEG_NOTE_GROUPQUOTA:
3826 case ZFS_DELEG_NOTE_GROUPUSED:
3827 case ZFS_DELEG_NOTE_USERPROP:
3828 case ZFS_DELEG_NOTE_USERQUOTA:
3829 case ZFS_DELEG_NOTE_USERUSED:
3831 return (gettext("other"));
3833 return (gettext("subcommand"));
3838 who_type2weight(zfs_deleg_who_type_t who_type)
3842 case ZFS_DELEG_NAMED_SET_SETS:
3843 case ZFS_DELEG_NAMED_SET:
3846 case ZFS_DELEG_CREATE_SETS:
3847 case ZFS_DELEG_CREATE:
3850 case ZFS_DELEG_USER_SETS:
3851 case ZFS_DELEG_USER:
3854 case ZFS_DELEG_GROUP_SETS:
3855 case ZFS_DELEG_GROUP:
3858 case ZFS_DELEG_EVERYONE_SETS:
3859 case ZFS_DELEG_EVERYONE:
3871 who_perm_compare(const void *larg, const void *rarg, void *unused)
3873 const who_perm_node_t *l = larg;
3874 const who_perm_node_t *r = rarg;
3875 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
3876 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
3877 int lweight = who_type2weight(ltype);
3878 int rweight = who_type2weight(rtype);
3879 int res = lweight - rweight;
3881 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
3882 ZFS_MAX_DELEG_NAME-1);
3894 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
3896 const deleg_perm_node_t *l = larg;
3897 const deleg_perm_node_t *r = rarg;
3898 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
3899 ZFS_MAX_DELEG_NAME-1);
3911 fs_perm_set_init(fs_perm_set_t *fspset)
3913 bzero(fspset, sizeof (fs_perm_set_t));
3915 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
3916 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
3917 NULL, UU_DEFAULT)) == NULL)
3919 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
3920 UU_DEFAULT)) == NULL)
3923 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
3924 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
3925 who_perm_node_t, who_avl_node), who_perm_compare,
3926 UU_DEFAULT)) == NULL)
3929 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
3930 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
3931 who_perm_node_t, who_avl_node), who_perm_compare,
3932 UU_DEFAULT)) == NULL)
3935 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
3936 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
3937 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
3942 static inline void fs_perm_fini(fs_perm_t *);
3943 static inline void who_perm_fini(who_perm_t *);
3946 fs_perm_set_fini(fs_perm_set_t *fspset)
3948 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
3950 while (node != NULL) {
3951 fs_perm_node_t *next_node =
3952 uu_list_next(fspset->fsps_list, node);
3953 fs_perm_t *fsperm = &node->fspn_fsperm;
3954 fs_perm_fini(fsperm);
3955 uu_list_remove(fspset->fsps_list, node);
3960 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
3961 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
3962 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
3966 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
3969 deleg_perm->dp_who_type = type;
3970 deleg_perm->dp_name = name;
3974 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
3975 zfs_deleg_who_type_t type, const char *name)
3977 uu_avl_pool_t *pool;
3978 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
3980 bzero(who_perm, sizeof (who_perm_t));
3982 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
3983 UU_DEFAULT)) == NULL)
3986 who_perm->who_type = type;
3987 who_perm->who_name = name;
3988 who_perm->who_fsperm = fsperm;
3992 who_perm_fini(who_perm_t *who_perm)
3994 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
3996 while (node != NULL) {
3997 deleg_perm_node_t *next_node =
3998 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4000 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4005 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4009 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4011 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4012 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4014 bzero(fsperm, sizeof (fs_perm_t));
4016 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4020 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4024 fsperm->fsp_set = fspset;
4025 fsperm->fsp_name = fsname;
4029 fs_perm_fini(fs_perm_t *fsperm)
4031 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4032 while (node != NULL) {
4033 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4035 who_perm_t *who_perm = &node->who_perm;
4036 who_perm_fini(who_perm);
4037 uu_avl_remove(fsperm->fsp_sc_avl, node);
4042 node = uu_avl_first(fsperm->fsp_uge_avl);
4043 while (node != NULL) {
4044 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4046 who_perm_t *who_perm = &node->who_perm;
4047 who_perm_fini(who_perm);
4048 uu_avl_remove(fsperm->fsp_uge_avl, node);
4053 uu_avl_destroy(fsperm->fsp_sc_avl);
4054 uu_avl_destroy(fsperm->fsp_uge_avl);
4058 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4059 zfs_deleg_who_type_t who_type, const char *name, char locality)
4061 uu_avl_index_t idx = 0;
4063 deleg_perm_node_t *found_node = NULL;
4064 deleg_perm_t *deleg_perm = &node->dpn_perm;
4066 deleg_perm_init(deleg_perm, who_type, name);
4068 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4070 uu_avl_insert(avl, node, idx);
4073 deleg_perm = &node->dpn_perm;
4078 case ZFS_DELEG_LOCAL:
4079 deleg_perm->dp_local = B_TRUE;
4081 case ZFS_DELEG_DESCENDENT:
4082 deleg_perm->dp_descend = B_TRUE;
4087 assert(B_FALSE); /* invalid locality */
4092 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4094 nvpair_t *nvp = NULL;
4095 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4096 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4097 zfs_deleg_who_type_t who_type = who_perm->who_type;
4099 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4100 const char *name = nvpair_name(nvp);
4101 data_type_t type = nvpair_type(nvp);
4102 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4103 deleg_perm_node_t *node =
4104 safe_malloc(sizeof (deleg_perm_node_t));
4106 VERIFY(type == DATA_TYPE_BOOLEAN);
4108 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4109 set_deleg_perm_node(avl, node, who_type, name, locality);
4116 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4118 nvpair_t *nvp = NULL;
4119 fs_perm_set_t *fspset = fsperm->fsp_set;
4121 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4122 nvlist_t *nvl2 = NULL;
4123 const char *name = nvpair_name(nvp);
4124 uu_avl_t *avl = NULL;
4125 uu_avl_pool_t *avl_pool = NULL;
4126 zfs_deleg_who_type_t perm_type = name[0];
4127 char perm_locality = name[1];
4128 const char *perm_name = name + 3;
4129 boolean_t is_set = B_TRUE;
4130 who_perm_t *who_perm = NULL;
4132 assert('$' == name[2]);
4134 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4137 switch (perm_type) {
4138 case ZFS_DELEG_CREATE:
4139 case ZFS_DELEG_CREATE_SETS:
4140 case ZFS_DELEG_NAMED_SET:
4141 case ZFS_DELEG_NAMED_SET_SETS:
4142 avl_pool = fspset->fsps_named_set_avl_pool;
4143 avl = fsperm->fsp_sc_avl;
4145 case ZFS_DELEG_USER:
4146 case ZFS_DELEG_USER_SETS:
4147 case ZFS_DELEG_GROUP:
4148 case ZFS_DELEG_GROUP_SETS:
4149 case ZFS_DELEG_EVERYONE:
4150 case ZFS_DELEG_EVERYONE_SETS:
4151 avl_pool = fspset->fsps_who_perm_avl_pool;
4152 avl = fsperm->fsp_uge_avl;
4159 who_perm_node_t *found_node = NULL;
4160 who_perm_node_t *node = safe_malloc(
4161 sizeof (who_perm_node_t));
4162 who_perm = &node->who_perm;
4163 uu_avl_index_t idx = 0;
4165 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4166 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4168 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4170 if (avl == fsperm->fsp_uge_avl) {
4172 struct passwd *p = NULL;
4173 struct group *g = NULL;
4174 const char *nice_name = NULL;
4176 switch (perm_type) {
4177 case ZFS_DELEG_USER_SETS:
4178 case ZFS_DELEG_USER:
4179 rid = atoi(perm_name);
4182 nice_name = p->pw_name;
4184 case ZFS_DELEG_GROUP_SETS:
4185 case ZFS_DELEG_GROUP:
4186 rid = atoi(perm_name);
4189 nice_name = g->gr_name;
4195 if (nice_name != NULL)
4197 node->who_perm.who_ug_name,
4201 uu_avl_insert(avl, node, idx);
4204 who_perm = &node->who_perm;
4208 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4215 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4217 nvpair_t *nvp = NULL;
4218 uu_avl_index_t idx = 0;
4220 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4221 nvlist_t *nvl2 = NULL;
4222 const char *fsname = nvpair_name(nvp);
4223 data_type_t type = nvpair_type(nvp);
4224 fs_perm_t *fsperm = NULL;
4225 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4229 fsperm = &node->fspn_fsperm;
4231 VERIFY(DATA_TYPE_NVLIST == type);
4233 uu_list_node_init(node, &node->fspn_list_node,
4234 fspset->fsps_list_pool);
4236 idx = uu_list_numnodes(fspset->fsps_list);
4237 fs_perm_init(fsperm, fspset, fsname);
4239 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4242 (void) parse_fs_perm(fsperm, nvl2);
4244 uu_list_insert(fspset->fsps_list, node, idx);
4250 static inline const char *
4251 deleg_perm_comment(zfs_deleg_note_t note)
4253 const char *str = "";
4258 case ZFS_DELEG_NOTE_ALLOW:
4259 str = gettext("Must also have the permission that is being"
4260 "\n\t\t\t\tallowed");
4262 case ZFS_DELEG_NOTE_CLONE:
4263 str = gettext("Must also have the 'create' ability and 'mount'"
4264 "\n\t\t\t\tability in the origin file system");
4266 case ZFS_DELEG_NOTE_CREATE:
4267 str = gettext("Must also have the 'mount' ability");
4269 case ZFS_DELEG_NOTE_DESTROY:
4270 str = gettext("Must also have the 'mount' ability");
4272 case ZFS_DELEG_NOTE_DIFF:
4273 str = gettext("Allows lookup of paths within a dataset;"
4274 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4275 "\n\t\t\t\tin order to use zfs diff");
4277 case ZFS_DELEG_NOTE_HOLD:
4278 str = gettext("Allows adding a user hold to a snapshot");
4280 case ZFS_DELEG_NOTE_MOUNT:
4281 str = gettext("Allows mount/umount of ZFS datasets");
4283 case ZFS_DELEG_NOTE_PROMOTE:
4284 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4285 " 'promote' ability in the origin file system");
4287 case ZFS_DELEG_NOTE_RECEIVE:
4288 str = gettext("Must also have the 'mount' and 'create'"
4291 case ZFS_DELEG_NOTE_RELEASE:
4292 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4293 "might destroy the snapshot");
4295 case ZFS_DELEG_NOTE_RENAME:
4296 str = gettext("Must also have the 'mount' and 'create'"
4297 "\n\t\t\t\tability in the new parent");
4299 case ZFS_DELEG_NOTE_ROLLBACK:
4302 case ZFS_DELEG_NOTE_SEND:
4305 case ZFS_DELEG_NOTE_SHARE:
4306 str = gettext("Allows sharing file systems over NFS or SMB"
4307 "\n\t\t\t\tprotocols");
4309 case ZFS_DELEG_NOTE_SNAPSHOT:
4313 * case ZFS_DELEG_NOTE_VSCAN:
4314 * str = gettext("");
4318 case ZFS_DELEG_NOTE_GROUPQUOTA:
4319 str = gettext("Allows accessing any groupquota@... property");
4321 case ZFS_DELEG_NOTE_GROUPUSED:
4322 str = gettext("Allows reading any groupused@... property");
4324 case ZFS_DELEG_NOTE_USERPROP:
4325 str = gettext("Allows changing any user property");
4327 case ZFS_DELEG_NOTE_USERQUOTA:
4328 str = gettext("Allows accessing any userquota@... property");
4330 case ZFS_DELEG_NOTE_USERUSED:
4331 str = gettext("Allows reading any userused@... property");
4349 boolean_t recursive; /* unallow only */
4350 boolean_t prt_usage;
4352 boolean_t prt_perms;
4355 const char *dataset;
4359 prop_cmp(const void *a, const void *b)
4361 const char *str1 = *(const char **)a;
4362 const char *str2 = *(const char **)b;
4363 return (strcmp(str1, str2));
4367 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4369 const char *opt_desc[] = {
4370 "-h", gettext("show this help message and exit"),
4371 "-l", gettext("set permission locally"),
4372 "-d", gettext("set permission for descents"),
4373 "-u", gettext("set permission for user"),
4374 "-g", gettext("set permission for group"),
4375 "-e", gettext("set permission for everyone"),
4376 "-c", gettext("set create time permission"),
4377 "-s", gettext("define permission set"),
4379 "-r", gettext("remove permissions recursively"),
4381 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4382 size_t allow_size = unallow_size - 2;
4383 const char *props[ZFS_NUM_PROPS];
4386 FILE *fp = requested ? stdout : stderr;
4387 zprop_desc_t *pdtbl = zfs_prop_get_table();
4388 const char *fmt = gettext("%-16s %-14s\t%s\n");
4390 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4392 (void) fprintf(fp, gettext("Options:\n"));
4393 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4394 const char *opt = opt_desc[i++];
4395 const char *optdsc = opt_desc[i];
4396 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4399 (void) fprintf(fp, gettext("\nThe following permissions are "
4401 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4403 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4404 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4405 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4406 const char *perm_type = deleg_perm_type(perm_note);
4407 const char *perm_comment = deleg_perm_comment(perm_note);
4408 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4411 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4412 zprop_desc_t *pd = &pdtbl[i];
4413 if (pd->pd_visible != B_TRUE)
4416 if (pd->pd_attr == PROP_READONLY)
4419 props[count++] = pd->pd_name;
4421 props[count] = NULL;
4423 qsort(props, count, sizeof (char *), prop_cmp);
4425 for (i = 0; i < count; i++)
4426 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4429 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4431 exit(requested ? 0 : 2);
4434 static inline const char *
4435 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4438 if (un && argc == expected_argc - 1)
4440 else if (argc == expected_argc)
4441 *permsp = argv[argc - 2];
4443 allow_usage(un, B_FALSE,
4444 gettext("wrong number of parameters\n"));
4446 return (argv[argc - 1]);
4450 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4452 int uge_sum = opts->user + opts->group + opts->everyone;
4453 int csuge_sum = opts->create + opts->set + uge_sum;
4454 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4455 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4458 allow_usage(un, B_FALSE,
4459 gettext("-u, -g, and -e are mutually exclusive\n"));
4461 if (opts->prt_usage) {
4462 if (argc == 0 && all_sum == 0)
4463 allow_usage(un, B_TRUE, NULL);
4470 allow_usage(un, B_FALSE,
4471 gettext("invalid options combined with -s\n"));
4473 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4474 if (argv[0][0] != '@')
4475 allow_usage(un, B_FALSE,
4476 gettext("invalid set name: missing '@' prefix\n"));
4477 opts->who = argv[0];
4478 } else if (opts->create) {
4479 if (ldcsuge_sum > 1)
4480 allow_usage(un, B_FALSE,
4481 gettext("invalid options combined with -c\n"));
4482 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4483 } else if (opts->everyone) {
4485 allow_usage(un, B_FALSE,
4486 gettext("invalid options combined with -e\n"));
4487 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4488 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4490 opts->everyone = B_TRUE;
4493 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4494 } else if (argc == 1 && !un) {
4495 opts->prt_perms = B_TRUE;
4496 opts->dataset = argv[argc-1];
4498 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4499 opts->who = argv[0];
4502 if (!opts->local && !opts->descend) {
4503 opts->local = B_TRUE;
4504 opts->descend = B_TRUE;
4509 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4510 const char *who, char *perms, nvlist_t *top_nvl)
4513 char ld[2] = { '\0', '\0' };
4514 char who_buf[ZFS_MAXNAMELEN+32];
4515 char base_type = ZFS_DELEG_WHO_UNKNOWN;
4516 char set_type = ZFS_DELEG_WHO_UNKNOWN;
4517 nvlist_t *base_nvl = NULL;
4518 nvlist_t *set_nvl = NULL;
4521 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4523 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4527 case ZFS_DELEG_NAMED_SET_SETS:
4528 case ZFS_DELEG_NAMED_SET:
4529 set_type = ZFS_DELEG_NAMED_SET_SETS;
4530 base_type = ZFS_DELEG_NAMED_SET;
4531 ld[0] = ZFS_DELEG_NA;
4533 case ZFS_DELEG_CREATE_SETS:
4534 case ZFS_DELEG_CREATE:
4535 set_type = ZFS_DELEG_CREATE_SETS;
4536 base_type = ZFS_DELEG_CREATE;
4537 ld[0] = ZFS_DELEG_NA;
4539 case ZFS_DELEG_USER_SETS:
4540 case ZFS_DELEG_USER:
4541 set_type = ZFS_DELEG_USER_SETS;
4542 base_type = ZFS_DELEG_USER;
4544 ld[0] = ZFS_DELEG_LOCAL;
4546 ld[1] = ZFS_DELEG_DESCENDENT;
4548 case ZFS_DELEG_GROUP_SETS:
4549 case ZFS_DELEG_GROUP:
4550 set_type = ZFS_DELEG_GROUP_SETS;
4551 base_type = ZFS_DELEG_GROUP;
4553 ld[0] = ZFS_DELEG_LOCAL;
4555 ld[1] = ZFS_DELEG_DESCENDENT;
4557 case ZFS_DELEG_EVERYONE_SETS:
4558 case ZFS_DELEG_EVERYONE:
4559 set_type = ZFS_DELEG_EVERYONE_SETS;
4560 base_type = ZFS_DELEG_EVERYONE;
4562 ld[0] = ZFS_DELEG_LOCAL;
4564 ld[1] = ZFS_DELEG_DESCENDENT;
4569 if (perms != NULL) {
4571 char *end = curr + strlen(perms);
4573 while (curr < end) {
4574 char *delim = strchr(curr, ',');
4585 (void) nvlist_add_boolean(nvl, curr);
4591 for (i = 0; i < 2; i++) {
4592 char locality = ld[i];
4596 if (!nvlist_empty(base_nvl)) {
4598 (void) snprintf(who_buf,
4599 sizeof (who_buf), "%c%c$%s",
4600 base_type, locality, who);
4602 (void) snprintf(who_buf,
4603 sizeof (who_buf), "%c%c$",
4604 base_type, locality);
4606 (void) nvlist_add_nvlist(top_nvl, who_buf,
4611 if (!nvlist_empty(set_nvl)) {
4613 (void) snprintf(who_buf,
4614 sizeof (who_buf), "%c%c$%s",
4615 set_type, locality, who);
4617 (void) snprintf(who_buf,
4618 sizeof (who_buf), "%c%c$",
4619 set_type, locality);
4621 (void) nvlist_add_nvlist(top_nvl, who_buf,
4626 for (i = 0; i < 2; i++) {
4627 char locality = ld[i];
4632 (void) snprintf(who_buf, sizeof (who_buf),
4633 "%c%c$%s", base_type, locality, who);
4635 (void) snprintf(who_buf, sizeof (who_buf),
4636 "%c%c$", base_type, locality);
4637 (void) nvlist_add_boolean(top_nvl, who_buf);
4640 (void) snprintf(who_buf, sizeof (who_buf),
4641 "%c%c$%s", set_type, locality, who);
4643 (void) snprintf(who_buf, sizeof (who_buf),
4644 "%c%c$", set_type, locality);
4645 (void) nvlist_add_boolean(top_nvl, who_buf);
4651 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4653 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4657 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4658 opts->descend, opts->who, opts->perms, *nvlp);
4659 } else if (opts->create) {
4660 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4661 opts->descend, NULL, opts->perms, *nvlp);
4662 } else if (opts->everyone) {
4663 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4664 opts->descend, NULL, opts->perms, *nvlp);
4666 char *curr = opts->who;
4667 char *end = curr + strlen(curr);
4669 while (curr < end) {
4671 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
4673 char *delim = strchr(curr, ',');
4676 struct passwd *p = NULL;
4677 struct group *g = NULL;
4685 rid = (uid_t)strtol(curr, &endch, 0);
4687 who_type = ZFS_DELEG_USER;
4696 (void) snprintf(errbuf, 256, gettext(
4697 "invalid user %s"), curr);
4698 allow_usage(un, B_TRUE, errbuf);
4700 } else if (opts->group) {
4701 who_type = ZFS_DELEG_GROUP;
4710 (void) snprintf(errbuf, 256, gettext(
4711 "invalid group %s"), curr);
4712 allow_usage(un, B_TRUE, errbuf);
4715 if (*endch != '\0') {
4722 if (*endch != '\0') {
4730 who_type = ZFS_DELEG_USER;
4732 } else if (g != NULL) {
4733 who_type = ZFS_DELEG_GROUP;
4736 (void) snprintf(errbuf, 256, gettext(
4737 "invalid user/group %s"), curr);
4738 allow_usage(un, B_TRUE, errbuf);
4742 (void) sprintf(id, "%u", rid);
4745 store_allow_perm(who_type, opts->local,
4746 opts->descend, who, opts->perms, *nvlp);
4755 print_set_creat_perms(uu_avl_t *who_avl)
4757 const char *sc_title[] = {
4758 gettext("Permission sets:\n"),
4759 gettext("Create time permissions:\n"),
4762 const char **title_ptr = sc_title;
4763 who_perm_node_t *who_node = NULL;
4764 int prev_weight = -1;
4766 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4767 who_node = uu_avl_next(who_avl, who_node)) {
4768 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4769 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4770 const char *who_name = who_node->who_perm.who_name;
4771 int weight = who_type2weight(who_type);
4772 boolean_t first = B_TRUE;
4773 deleg_perm_node_t *deleg_node;
4775 if (prev_weight != weight) {
4776 (void) printf("%s", *title_ptr++);
4777 prev_weight = weight;
4780 if (who_name == NULL || strnlen(who_name, 1) == 0)
4781 (void) printf("\t");
4783 (void) printf("\t%s ", who_name);
4785 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4786 deleg_node = uu_avl_next(avl, deleg_node)) {
4789 deleg_node->dpn_perm.dp_name);
4792 (void) printf(",%s",
4793 deleg_node->dpn_perm.dp_name);
4796 (void) printf("\n");
4801 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4804 who_perm_node_t *who_node = NULL;
4805 boolean_t prt_title = B_TRUE;
4806 uu_avl_walk_t *walk;
4808 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4811 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4812 const char *who_name = who_node->who_perm.who_name;
4813 const char *nice_who_name = who_node->who_perm.who_ug_name;
4814 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4815 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4817 deleg_perm_node_t *deleg_node;
4818 boolean_t prt_who = B_TRUE;
4820 for (deleg_node = uu_avl_first(avl);
4822 deleg_node = uu_avl_next(avl, deleg_node)) {
4823 if (local != deleg_node->dpn_perm.dp_local ||
4824 descend != deleg_node->dpn_perm.dp_descend)
4828 const char *who = NULL;
4830 prt_title = B_FALSE;
4831 (void) printf("%s", title);
4835 case ZFS_DELEG_USER_SETS:
4836 case ZFS_DELEG_USER:
4837 who = gettext("user");
4839 who_name = nice_who_name;
4841 case ZFS_DELEG_GROUP_SETS:
4842 case ZFS_DELEG_GROUP:
4843 who = gettext("group");
4845 who_name = nice_who_name;
4847 case ZFS_DELEG_EVERYONE_SETS:
4848 case ZFS_DELEG_EVERYONE:
4849 who = gettext("everyone");
4856 if (who_name == NULL)
4857 (void) printf("\t%s", who);
4859 (void) printf("\t%s %s", who, who_name);
4862 (void) printf("%c%s", delim,
4863 deleg_node->dpn_perm.dp_name);
4868 (void) printf("\n");
4871 uu_avl_walk_end(walk);
4875 print_fs_perms(fs_perm_set_t *fspset)
4877 fs_perm_node_t *node = NULL;
4878 char buf[ZFS_MAXNAMELEN+32];
4879 const char *dsname = buf;
4881 for (node = uu_list_first(fspset->fsps_list); node != NULL;
4882 node = uu_list_next(fspset->fsps_list, node)) {
4883 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
4884 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
4887 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
4888 gettext("---- Permissions on %s "),
4889 node->fspn_fsperm.fsp_name);
4890 (void) printf("%s", dsname);
4891 left = 70 - strlen(buf);
4894 (void) printf("\n");
4896 print_set_creat_perms(sc_avl);
4897 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
4898 gettext("Local permissions:\n"));
4899 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
4900 gettext("Descendent permissions:\n"));
4901 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
4902 gettext("Local+Descendent permissions:\n"));
4906 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
4908 struct deleg_perms {
4914 set_deleg_perms(zfs_handle_t *zhp, void *data)
4916 struct deleg_perms *perms = (struct deleg_perms *)data;
4917 zfs_type_t zfs_type = zfs_get_type(zhp);
4919 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
4922 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
4926 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
4929 nvlist_t *perm_nvl = NULL;
4930 nvlist_t *update_perm_nvl = NULL;
4933 struct allow_opts opts = { 0 };
4935 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
4938 while ((c = getopt(argc, argv, optstr)) != -1) {
4941 opts.local = B_TRUE;
4944 opts.descend = B_TRUE;
4950 opts.group = B_TRUE;
4953 opts.everyone = B_TRUE;
4959 opts.create = B_TRUE;
4962 opts.recursive = B_TRUE;
4965 (void) fprintf(stderr, gettext("missing argument for "
4966 "'%c' option\n"), optopt);
4970 opts.prt_usage = B_TRUE;
4973 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4982 /* check arguments */
4983 parse_allow_args(argc, argv, un, &opts);
4985 /* try to open the dataset */
4986 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
4987 ZFS_TYPE_VOLUME)) == NULL) {
4988 (void) fprintf(stderr, "Failed to open dataset: %s\n",
4993 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
4996 fs_perm_set_init(&fs_perm_set);
4997 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
4998 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5003 print_fs_perms(&fs_perm_set);
5005 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5006 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5009 if (un && opts.recursive) {
5010 struct deleg_perms data = { un, update_perm_nvl };
5011 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5020 nvlist_free(perm_nvl);
5021 if (update_perm_nvl != NULL)
5022 nvlist_free(update_perm_nvl);
5024 fs_perm_set_fini(&fs_perm_set);
5032 * zfs allow [-r] [-t] <tag> <snap> ...
5034 * -r Recursively hold
5035 * -t Temporary hold (hidden option)
5037 * Apply a user-hold with the given tag to the list of snapshots.
5040 zfs_do_allow(int argc, char **argv)
5042 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5046 * zfs unallow [-r] [-t] <tag> <snap> ...
5048 * -r Recursively hold
5049 * -t Temporary hold (hidden option)
5051 * Apply a user-hold with the given tag to the list of snapshots.
5054 zfs_do_unallow(int argc, char **argv)
5056 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5060 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5065 boolean_t recursive = B_FALSE;
5066 boolean_t temphold = B_FALSE;
5067 const char *opts = holding ? "rt" : "r";
5071 while ((c = getopt(argc, argv, opts)) != -1) {
5080 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5089 /* check number of arguments */
5097 if (holding && tag[0] == '.') {
5098 /* tags starting with '.' are reserved for libzfs */
5099 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5103 for (i = 0; i < argc; ++i) {
5105 char parent[ZFS_MAXNAMELEN];
5107 char *path = argv[i];
5109 delim = strchr(path, '@');
5110 if (delim == NULL) {
5111 (void) fprintf(stderr,
5112 gettext("'%s' is not a snapshot\n"), path);
5116 (void) strncpy(parent, path, delim - path);
5117 parent[delim - path] = '\0';
5119 zhp = zfs_open(g_zfs, parent,
5120 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5126 if (zfs_hold(zhp, delim+1, tag, recursive,
5127 temphold, B_FALSE, -1, 0, 0) != 0)
5130 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5136 return (errors != 0);
5140 * zfs hold [-r] [-t] <tag> <snap> ...
5142 * -r Recursively hold
5143 * -t Temporary hold (hidden option)
5145 * Apply a user-hold with the given tag to the list of snapshots.
5148 zfs_do_hold(int argc, char **argv)
5150 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5154 * zfs release [-r] <tag> <snap> ...
5156 * -r Recursively release
5158 * Release a user-hold with the given tag from the list of snapshots.
5161 zfs_do_release(int argc, char **argv)
5163 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5166 typedef struct holds_cbdata {
5167 boolean_t cb_recursive;
5168 const char *cb_snapname;
5170 size_t cb_max_namelen;
5171 size_t cb_max_taglen;
5174 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5175 #define DATETIME_BUF_LEN (32)
5180 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
5183 nvpair_t *nvp = NULL;
5184 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5188 for (i = 0; i < 3; i++) {
5189 col = gettext(hdr_cols[i]);
5191 (void) printf("%-*s ", i ? tagwidth : nwidth,
5194 (void) printf("%s\n", col);
5198 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5199 char *zname = nvpair_name(nvp);
5201 nvpair_t *nvp2 = NULL;
5202 (void) nvpair_value_nvlist(nvp, &nvl2);
5203 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5204 char tsbuf[DATETIME_BUF_LEN];
5205 char *tagname = nvpair_name(nvp2);
5209 char sep = scripted ? '\t' : ' ';
5210 int sepnum = scripted ? 1 : 2;
5212 (void) nvpair_value_uint64(nvp2, &val);
5214 (void) localtime_r(&time, &t);
5215 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5216 gettext(STRFTIME_FMT_STR), &t);
5218 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5219 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5225 * Generic callback function to list a dataset or snapshot.
5228 holds_callback(zfs_handle_t *zhp, void *data)
5230 holds_cbdata_t *cbp = data;
5231 nvlist_t *top_nvl = *cbp->cb_nvlp;
5232 nvlist_t *nvl = NULL;
5233 nvpair_t *nvp = NULL;
5234 const char *zname = zfs_get_name(zhp);
5235 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5237 if (cbp->cb_recursive) {
5238 const char *snapname;
5239 char *delim = strchr(zname, '@');
5243 snapname = delim + 1;
5244 if (strcmp(cbp->cb_snapname, snapname))
5248 if (zfs_get_holds(zhp, &nvl) != 0)
5251 if (znamelen > cbp->cb_max_namelen)
5252 cbp->cb_max_namelen = znamelen;
5254 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5255 const char *tag = nvpair_name(nvp);
5256 size_t taglen = strnlen(tag, MAXNAMELEN);
5257 if (taglen > cbp->cb_max_taglen)
5258 cbp->cb_max_taglen = taglen;
5261 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5265 * zfs holds [-r] <snap> ...
5267 * -r Recursively hold
5270 zfs_do_holds(int argc, char **argv)
5275 boolean_t scripted = B_FALSE;
5276 boolean_t recursive = B_FALSE;
5277 const char *opts = "rH";
5280 int types = ZFS_TYPE_SNAPSHOT;
5281 holds_cbdata_t cb = { 0 };
5288 while ((c = getopt(argc, argv, opts)) != -1) {
5297 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5304 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5305 flags |= ZFS_ITER_RECURSE;
5311 /* check number of arguments */
5315 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5318 for (i = 0; i < argc; ++i) {
5319 char *snapshot = argv[i];
5321 const char *snapname;
5323 delim = strchr(snapshot, '@');
5324 if (delim == NULL) {
5325 (void) fprintf(stderr,
5326 gettext("'%s' is not a snapshot\n"), snapshot);
5330 snapname = delim + 1;
5332 snapshot[delim - snapshot] = '\0';
5334 cb.cb_recursive = recursive;
5335 cb.cb_snapname = snapname;
5339 * 1. collect holds data, set format options
5341 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5342 holds_callback, &cb);
5348 * 2. print holds data
5350 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5352 if (nvlist_empty(nvl))
5353 (void) fprintf(stderr, gettext("no datasets available\n"));
5357 return (0 != errors);
5360 #define CHECK_SPINNER 30
5361 #define SPINNER_TIME 3 /* seconds */
5362 #define MOUNT_TIME 5 /* seconds */
5365 get_one_dataset(zfs_handle_t *zhp, void *data)
5367 static char *spin[] = { "-", "\\", "|", "/" };
5368 static int spinval = 0;
5369 static int spincheck = 0;
5370 static time_t last_spin_time = (time_t)0;
5371 get_all_cb_t *cbp = data;
5372 zfs_type_t type = zfs_get_type(zhp);
5374 if (cbp->cb_verbose) {
5375 if (--spincheck < 0) {
5376 time_t now = time(NULL);
5377 if (last_spin_time + SPINNER_TIME < now) {
5378 update_progress(spin[spinval++ % 4]);
5379 last_spin_time = now;
5381 spincheck = CHECK_SPINNER;
5386 * Iterate over any nested datasets.
5388 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5394 * Skip any datasets whose type does not match.
5396 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5400 libzfs_add_handle(cbp, zhp);
5401 assert(cbp->cb_used <= cbp->cb_alloc);
5407 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5409 get_all_cb_t cb = { 0 };
5410 cb.cb_verbose = verbose;
5411 cb.cb_getone = get_one_dataset;
5414 set_progress_header(gettext("Reading ZFS config"));
5415 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5417 *dslist = cb.cb_handles;
5418 *count = cb.cb_used;
5421 finish_progress(gettext("done."));
5425 * Generic callback for sharing or mounting filesystems. Because the code is so
5426 * similar, we have a common function with an extra parameter to determine which
5427 * mode we are using.
5429 #define OP_SHARE 0x1
5430 #define OP_MOUNT 0x2
5433 * Share or mount a dataset.
5436 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5437 boolean_t explicit, const char *options)
5439 char mountpoint[ZFS_MAXPROPLEN];
5440 char shareopts[ZFS_MAXPROPLEN];
5441 char smbshareopts[ZFS_MAXPROPLEN];
5442 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5444 uint64_t zoned, canmount;
5445 boolean_t shared_nfs, shared_smb;
5447 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5450 * Check to make sure we can mount/share this dataset. If we
5451 * are in the global zone and the filesystem is exported to a
5452 * local zone, or if we are in a local zone and the
5453 * filesystem is not exported, then it is an error.
5455 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5457 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5461 (void) fprintf(stderr, gettext("cannot %s '%s': "
5462 "dataset is exported to a local zone\n"), cmdname,
5466 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5470 (void) fprintf(stderr, gettext("cannot %s '%s': "
5471 "permission denied\n"), cmdname,
5477 * Ignore any filesystems which don't apply to us. This
5478 * includes those with a legacy mountpoint, or those with
5479 * legacy share options.
5481 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5482 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5483 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5484 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5485 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5486 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5488 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5489 strcmp(smbshareopts, "off") == 0) {
5493 (void) fprintf(stderr, gettext("cannot share '%s': "
5494 "legacy share\n"), zfs_get_name(zhp));
5495 (void) fprintf(stderr, gettext("use share(1M) to "
5496 "share this filesystem, or set "
5497 "sharenfs property on\n"));
5502 * We cannot share or mount legacy filesystems. If the
5503 * shareopts is non-legacy but the mountpoint is legacy, we
5504 * treat it as a legacy share.
5506 if (strcmp(mountpoint, "legacy") == 0) {
5510 (void) fprintf(stderr, gettext("cannot %s '%s': "
5511 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5512 (void) fprintf(stderr, gettext("use %s(1M) to "
5513 "%s this filesystem\n"), cmdname, cmdname);
5517 if (strcmp(mountpoint, "none") == 0) {
5521 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5522 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5527 * canmount explicit outcome
5528 * on no pass through
5529 * on yes pass through
5531 * off yes display error, return 1
5532 * noauto no return 0
5533 * noauto yes pass through
5535 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5536 if (canmount == ZFS_CANMOUNT_OFF) {
5540 (void) fprintf(stderr, gettext("cannot %s '%s': "
5541 "'canmount' property is set to 'off'\n"), cmdname,
5544 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5549 * At this point, we have verified that the mountpoint and/or
5550 * shareopts are appropriate for auto management. If the
5551 * filesystem is already mounted or shared, return (failing
5552 * for explicit requests); otherwise mount or share the
5558 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5559 shared_smb = zfs_is_shared_smb(zhp, NULL);
5561 if ((shared_nfs && shared_smb) ||
5562 ((shared_nfs && strcmp(shareopts, "on") == 0) &&
5563 (strcmp(smbshareopts, "off") == 0)) ||
5564 ((shared_smb && strcmp(smbshareopts, "on") == 0) &&
5565 (strcmp(shareopts, "off") == 0))) {
5569 (void) fprintf(stderr, gettext("cannot share "
5570 "'%s': filesystem already shared\n"),
5575 if (!zfs_is_mounted(zhp, NULL) &&
5576 zfs_mount(zhp, NULL, 0) != 0)
5579 if (protocol == NULL) {
5580 if (zfs_shareall(zhp) != 0)
5582 } else if (strcmp(protocol, "nfs") == 0) {
5583 if (zfs_share_nfs(zhp))
5585 } else if (strcmp(protocol, "smb") == 0) {
5586 if (zfs_share_smb(zhp))
5589 (void) fprintf(stderr, gettext("cannot share "
5590 "'%s': invalid share type '%s' "
5592 zfs_get_name(zhp), protocol);
5599 if (options == NULL)
5600 mnt.mnt_mntopts = "";
5602 mnt.mnt_mntopts = (char *)options;
5604 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5605 zfs_is_mounted(zhp, NULL)) {
5609 (void) fprintf(stderr, gettext("cannot mount "
5610 "'%s': filesystem already mounted\n"),
5615 if (zfs_mount(zhp, options, flags) != 0)
5624 * Reports progress in the form "(current/total)". Not thread-safe.
5627 report_mount_progress(int current, int total)
5629 static time_t last_progress_time = 0;
5630 time_t now = time(NULL);
5633 /* report 1..n instead of 0..n-1 */
5636 /* display header if we're here for the first time */
5638 set_progress_header(gettext("Mounting ZFS filesystems"));
5639 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5640 /* too soon to report again */
5644 last_progress_time = now;
5646 (void) sprintf(info, "(%d/%d)", current, total);
5648 if (current == total)
5649 finish_progress(info);
5651 update_progress(info);
5655 append_options(char *mntopts, char *newopts)
5657 int len = strlen(mntopts);
5659 /* original length plus new string to append plus 1 for the comma */
5660 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5661 (void) fprintf(stderr, gettext("the opts argument for "
5662 "'%s' option is too long (more than %d chars)\n"),
5663 "-o", MNT_LINE_MAX);
5668 mntopts[len++] = ',';
5670 (void) strcpy(&mntopts[len], newopts);
5674 share_mount(int op, int argc, char **argv)
5677 boolean_t verbose = B_FALSE;
5679 char *options = NULL;
5683 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5693 if (*optarg == '\0') {
5694 (void) fprintf(stderr, gettext("empty mount "
5695 "options (-o) specified\n"));
5699 if (options == NULL)
5700 options = safe_malloc(MNT_LINE_MAX + 1);
5702 /* option validation is done later */
5703 append_options(options, optarg);
5706 flags |= MS_OVERLAY;
5709 (void) fprintf(stderr, gettext("missing argument for "
5710 "'%c' option\n"), optopt);
5714 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5723 /* check number of arguments */
5725 zfs_handle_t **dslist = NULL;
5726 size_t i, count = 0;
5727 char *protocol = NULL;
5729 if (op == OP_SHARE && argc > 0) {
5730 if (strcmp(argv[0], "nfs") != 0 &&
5731 strcmp(argv[0], "smb") != 0) {
5732 (void) fprintf(stderr, gettext("share type "
5733 "must be 'nfs' or 'smb'\n"));
5742 (void) fprintf(stderr, gettext("too many arguments\n"));
5746 start_progress_timer();
5747 get_all_datasets(&dslist, &count, verbose);
5752 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5754 for (i = 0; i < count; i++) {
5756 report_mount_progress(i, count);
5758 if (share_mount_one(dslist[i], op, flags, protocol,
5759 B_FALSE, options) != 0)
5761 zfs_close(dslist[i]);
5765 } else if (argc == 0) {
5766 struct mnttab entry;
5768 if ((op == OP_SHARE) || (options != NULL)) {
5769 (void) fprintf(stderr, gettext("missing filesystem "
5770 "argument (specify -a for all)\n"));
5775 * When mount is given no arguments, go through /etc/mtab and
5776 * display any active ZFS mounts. We hide any snapshots, since
5777 * they are controlled automatically.
5779 rewind(mnttab_file);
5780 while (getmntent(mnttab_file, &entry) == 0) {
5781 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5782 strchr(entry.mnt_special, '@') != NULL)
5785 (void) printf("%-30s %s\n", entry.mnt_special,
5793 (void) fprintf(stderr,
5794 gettext("too many arguments\n"));
5798 if ((zhp = zfs_open(g_zfs, argv[0],
5799 ZFS_TYPE_FILESYSTEM)) == NULL) {
5802 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5812 * zfs mount -a [nfs]
5813 * zfs mount filesystem
5815 * Mount all filesystems, or mount the given filesystem.
5818 zfs_do_mount(int argc, char **argv)
5820 return (share_mount(OP_MOUNT, argc, argv));
5824 * zfs share -a [nfs | smb]
5825 * zfs share filesystem
5827 * Share all filesystems, or share the given filesystem.
5830 zfs_do_share(int argc, char **argv)
5832 return (share_mount(OP_SHARE, argc, argv));
5835 typedef struct unshare_unmount_node {
5836 zfs_handle_t *un_zhp;
5838 uu_avl_node_t un_avlnode;
5839 } unshare_unmount_node_t;
5843 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
5845 const unshare_unmount_node_t *l = larg;
5846 const unshare_unmount_node_t *r = rarg;
5848 return (strcmp(l->un_mountp, r->un_mountp));
5852 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5853 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
5854 * and unmount it appropriately.
5857 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
5861 struct stat64 statbuf;
5862 struct extmnttab entry;
5863 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5867 * Search for the path in /etc/mtab. Rather than looking for the
5868 * specific path, which can be fooled by non-standard paths (i.e. ".."
5869 * or "//"), we stat() the path and search for the corresponding
5870 * (major,minor) device pair.
5872 if (stat64(path, &statbuf) != 0) {
5873 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5874 cmdname, path, strerror(errno));
5877 path_inode = statbuf.st_ino;
5880 * Search for the given (major,minor) pair in the mount table.
5882 rewind(mnttab_file);
5883 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
5884 if (entry.mnt_major == major(statbuf.st_dev) &&
5885 entry.mnt_minor == minor(statbuf.st_dev))
5889 if (op == OP_SHARE) {
5890 (void) fprintf(stderr, gettext("cannot %s '%s': not "
5891 "currently mounted\n"), cmdname, path);
5894 (void) fprintf(stderr, gettext("warning: %s not in mtab\n"),
5896 if ((ret = umount2(path, flags)) != 0)
5897 (void) fprintf(stderr, gettext("%s: %s\n"), path,
5902 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
5903 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
5904 "filesystem\n"), cmdname, path);
5908 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
5909 ZFS_TYPE_FILESYSTEM)) == NULL)
5913 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
5914 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5915 cmdname, path, strerror(errno));
5917 } else if (statbuf.st_ino != path_inode) {
5918 (void) fprintf(stderr, gettext("cannot "
5919 "%s '%s': not a mountpoint\n"), cmdname, path);
5923 if (op == OP_SHARE) {
5924 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5925 char smbshare_prop[ZFS_MAXPROPLEN];
5927 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
5928 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
5929 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
5930 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
5932 if (strcmp(nfs_mnt_prop, "off") == 0 &&
5933 strcmp(smbshare_prop, "off") == 0) {
5934 (void) fprintf(stderr, gettext("cannot unshare "
5935 "'%s': legacy share\n"), path);
5936 (void) fprintf(stderr, gettext("use exportfs(8) "
5937 "or smbcontrol(1) to unshare this filesystem\n"));
5938 } else if (!zfs_is_shared(zhp)) {
5939 (void) fprintf(stderr, gettext("cannot unshare '%s': "
5940 "not currently shared\n"), path);
5942 ret = zfs_unshareall_bypath(zhp, path);
5945 char mtpt_prop[ZFS_MAXPROPLEN];
5947 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
5948 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
5951 ret = zfs_unmount(zhp, NULL, flags);
5952 } else if (strcmp(mtpt_prop, "legacy") == 0) {
5953 (void) fprintf(stderr, gettext("cannot unmount "
5954 "'%s': legacy mountpoint\n"),
5956 (void) fprintf(stderr, gettext("use umount(8) "
5957 "to unmount this filesystem\n"));
5959 ret = zfs_unmountall(zhp, flags);
5970 * Generic callback for unsharing or unmounting a filesystem.
5973 unshare_unmount(int op, int argc, char **argv)
5980 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5981 char sharesmb[ZFS_MAXPROPLEN];
5984 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
5993 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6004 * We could make use of zfs_for_each() to walk all datasets in
6005 * the system, but this would be very inefficient, especially
6006 * since we would have to linearly search /etc/mtab for each
6007 * one. Instead, do one pass through /etc/mtab looking for
6008 * zfs entries and call zfs_unmount() for each one.
6010 * Things get a little tricky if the administrator has created
6011 * mountpoints beneath other ZFS filesystems. In this case, we
6012 * have to unmount the deepest filesystems first. To accomplish
6013 * this, we place all the mountpoints in an AVL tree sorted by
6014 * the special type (dataset name), and walk the result in
6015 * reverse to make sure to get any snapshots first.
6017 struct mnttab entry;
6018 uu_avl_pool_t *pool;
6019 uu_avl_t *tree = NULL;
6020 unshare_unmount_node_t *node;
6022 uu_avl_walk_t *walk;
6025 (void) fprintf(stderr, gettext("too many arguments\n"));
6029 if (((pool = uu_avl_pool_create("unmount_pool",
6030 sizeof (unshare_unmount_node_t),
6031 offsetof(unshare_unmount_node_t, un_avlnode),
6032 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6033 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6036 rewind(mnttab_file);
6037 while (getmntent(mnttab_file, &entry) == 0) {
6039 /* ignore non-ZFS entries */
6040 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6043 /* ignore snapshots */
6044 if (strchr(entry.mnt_special, '@') != NULL)
6047 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6048 ZFS_TYPE_FILESYSTEM)) == NULL) {
6055 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6057 sizeof (nfs_mnt_prop),
6058 NULL, NULL, 0, B_FALSE) == 0);
6059 if (strcmp(nfs_mnt_prop, "off") != 0)
6061 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6063 sizeof (nfs_mnt_prop),
6064 NULL, NULL, 0, B_FALSE) == 0);
6065 if (strcmp(nfs_mnt_prop, "off") == 0)
6069 /* Ignore legacy mounts */
6070 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6072 sizeof (nfs_mnt_prop),
6073 NULL, NULL, 0, B_FALSE) == 0);
6074 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6076 /* Ignore canmount=noauto mounts */
6077 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6078 ZFS_CANMOUNT_NOAUTO)
6084 node = safe_malloc(sizeof (unshare_unmount_node_t));
6086 node->un_mountp = safe_strdup(entry.mnt_mountp);
6088 uu_avl_node_init(node, &node->un_avlnode, pool);
6090 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6091 uu_avl_insert(tree, node, idx);
6093 zfs_close(node->un_zhp);
6094 free(node->un_mountp);
6100 * Walk the AVL tree in reverse, unmounting each filesystem and
6101 * removing it from the AVL tree in the process.
6103 if ((walk = uu_avl_walk_start(tree,
6104 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6107 while ((node = uu_avl_walk_next(walk)) != NULL) {
6108 uu_avl_remove(tree, node);
6112 if (zfs_unshareall_bypath(node->un_zhp,
6113 node->un_mountp) != 0)
6118 if (zfs_unmount(node->un_zhp,
6119 node->un_mountp, flags) != 0)
6124 zfs_close(node->un_zhp);
6125 free(node->un_mountp);
6129 uu_avl_walk_end(walk);
6130 uu_avl_destroy(tree);
6131 uu_avl_pool_destroy(pool);
6136 (void) fprintf(stderr,
6137 gettext("missing filesystem argument\n"));
6139 (void) fprintf(stderr,
6140 gettext("too many arguments\n"));
6145 * We have an argument, but it may be a full path or a ZFS
6146 * filesystem. Pass full paths off to unmount_path() (shared by
6147 * manual_unmount), otherwise open the filesystem and pass to
6150 if (argv[0][0] == '/')
6151 return (unshare_unmount_path(op, argv[0],
6154 if ((zhp = zfs_open(g_zfs, argv[0],
6155 ZFS_TYPE_FILESYSTEM)) == NULL)
6158 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6159 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6160 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6161 NULL, 0, B_FALSE) == 0);
6165 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6167 sizeof (nfs_mnt_prop),
6168 NULL, NULL, 0, B_FALSE) == 0);
6169 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6170 sharesmb, sizeof (sharesmb), NULL, NULL,
6173 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6174 strcmp(sharesmb, "off") == 0) {
6175 (void) fprintf(stderr, gettext("cannot "
6176 "unshare '%s': legacy share\n"),
6178 (void) fprintf(stderr, gettext("use "
6179 "unshare(1M) to unshare this "
6182 } else if (!zfs_is_shared(zhp)) {
6183 (void) fprintf(stderr, gettext("cannot "
6184 "unshare '%s': not currently "
6185 "shared\n"), zfs_get_name(zhp));
6187 } else if (zfs_unshareall(zhp) != 0) {
6193 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6194 (void) fprintf(stderr, gettext("cannot "
6195 "unmount '%s': legacy "
6196 "mountpoint\n"), zfs_get_name(zhp));
6197 (void) fprintf(stderr, gettext("use "
6198 "umount(1M) to unmount this "
6201 } else if (!zfs_is_mounted(zhp, NULL)) {
6202 (void) fprintf(stderr, gettext("cannot "
6203 "unmount '%s': not currently "
6207 } else if (zfs_unmountall(zhp, flags) != 0) {
6221 * zfs unmount filesystem
6223 * Unmount all filesystems, or a specific ZFS filesystem.
6226 zfs_do_unmount(int argc, char **argv)
6228 return (unshare_unmount(OP_MOUNT, argc, argv));
6233 * zfs unshare filesystem
6235 * Unshare all filesystems, or a specific ZFS filesystem.
6238 zfs_do_unshare(int argc, char **argv)
6240 return (unshare_unmount(OP_SHARE, argc, argv));
6244 find_command_idx(char *command, int *idx)
6248 for (i = 0; i < NCOMMAND; i++) {
6249 if (command_table[i].name == NULL)
6252 if (strcmp(command, command_table[i].name) == 0) {
6261 zfs_do_diff(int argc, char **argv)
6265 char *tosnap = NULL;
6266 char *fromsnap = NULL;
6271 while ((c = getopt(argc, argv, "FHt")) != -1) {
6274 flags |= ZFS_DIFF_CLASSIFY;
6277 flags |= ZFS_DIFF_PARSEABLE;
6280 flags |= ZFS_DIFF_TIMESTAMP;
6283 (void) fprintf(stderr,
6284 gettext("invalid option '%c'\n"), optopt);
6293 (void) fprintf(stderr,
6294 gettext("must provide at least one snapshot name\n"));
6299 (void) fprintf(stderr, gettext("too many arguments\n"));
6304 tosnap = (argc == 2) ? argv[1] : NULL;
6307 if (*fromsnap != '@')
6308 copy = strdup(fromsnap);
6310 copy = strdup(tosnap);
6314 if ((atp = strchr(copy, '@')))
6317 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6323 * Ignore SIGPIPE so that the library can give us
6324 * information on any failure
6326 (void) sigignore(SIGPIPE);
6328 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6336 main(int argc, char **argv)
6342 (void) setlocale(LC_ALL, "");
6343 (void) textdomain(TEXT_DOMAIN);
6348 * Make sure the user has specified some command.
6351 (void) fprintf(stderr, gettext("missing command\n"));
6358 * The 'umount' command is an alias for 'unmount'
6360 if (strcmp(cmdname, "umount") == 0)
6361 cmdname = "unmount";
6364 * The 'recv' command is an alias for 'receive'
6366 if (strcmp(cmdname, "recv") == 0)
6367 cmdname = "receive";
6370 * The 'snap' command is an alias for 'snapshot'
6372 if (strcmp(cmdname, "snap") == 0)
6373 cmdname = "snapshot";
6378 if ((strcmp(cmdname, "-?") == 0) ||
6379 (strcmp(cmdname, "--help") == 0))
6382 if ((g_zfs = libzfs_init()) == NULL)
6385 mnttab_file = g_zfs->libzfs_mnttab;
6387 zpool_set_history_str("zfs", argc, argv, history_str);
6388 verify(zpool_stage_history(g_zfs, history_str) == 0);
6390 libzfs_print_on_error(g_zfs, B_TRUE);
6393 * Run the appropriate command.
6395 libzfs_mnttab_cache(g_zfs, B_FALSE);
6396 if (find_command_idx(cmdname, &i) == 0) {
6397 current_command = &command_table[i];
6398 ret = command_table[i].func(argc - 1, argv + 1);
6399 } else if (strchr(cmdname, '=') != NULL) {
6400 verify(find_command_idx("set", &i) == 0);
6401 current_command = &command_table[i];
6402 ret = command_table[i].func(argc, argv);
6404 (void) fprintf(stderr, gettext("unrecognized "
6405 "command '%s'\n"), cmdname);
6412 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6413 * for the purposes of running ::findleaks.
6415 if (getenv("ZFS_ABORT") != NULL) {
6416 (void) printf("dumping core by request\n");