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 (c) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2013 Steven Hartland. All rights reserved.
27 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
36 #include <libnvpair.h>
49 #include <sys/mkdev.h>
50 #include <sys/mntent.h>
51 #include <sys/mnttab.h>
52 #include <sys/mount.h>
54 #include <sys/fs/zfs.h>
55 #include <sys/types.h>
59 #include <libzfs_core.h>
61 #include <zfs_deleg.h>
65 #include <directory.h>
66 #endif /* HAVE_IDMAP */
70 #include "zfs_comutil.h"
71 #include "libzfs_impl.h"
73 libzfs_handle_t *g_zfs;
75 static FILE *mnttab_file;
76 static char history_str[HIS_MAX_RECORD_LEN];
77 static boolean_t log_history = B_TRUE;
79 static int zfs_do_clone(int argc, char **argv);
80 static int zfs_do_create(int argc, char **argv);
81 static int zfs_do_destroy(int argc, char **argv);
82 static int zfs_do_get(int argc, char **argv);
83 static int zfs_do_inherit(int argc, char **argv);
84 static int zfs_do_list(int argc, char **argv);
85 static int zfs_do_mount(int argc, char **argv);
86 static int zfs_do_rename(int argc, char **argv);
87 static int zfs_do_rollback(int argc, char **argv);
88 static int zfs_do_set(int argc, char **argv);
89 static int zfs_do_upgrade(int argc, char **argv);
90 static int zfs_do_snapshot(int argc, char **argv);
91 static int zfs_do_unmount(int argc, char **argv);
92 static int zfs_do_share(int argc, char **argv);
93 static int zfs_do_unshare(int argc, char **argv);
94 static int zfs_do_send(int argc, char **argv);
95 static int zfs_do_receive(int argc, char **argv);
96 static int zfs_do_promote(int argc, char **argv);
97 static int zfs_do_userspace(int argc, char **argv);
98 static int zfs_do_allow(int argc, char **argv);
99 static int zfs_do_unallow(int argc, char **argv);
100 static int zfs_do_hold(int argc, char **argv);
101 static int zfs_do_holds(int argc, char **argv);
102 static int zfs_do_release(int argc, char **argv);
103 static int zfs_do_diff(int argc, char **argv);
106 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
111 _umem_debug_init(void)
113 return ("default,verbose"); /* $UMEM_DEBUG setting */
117 _umem_logging_init(void)
119 return ("fail,contents"); /* $UMEM_LOGGING setting */
152 typedef struct zfs_command {
154 int (*func)(int argc, char **argv);
159 * Master command table. Each ZFS command has a name, associated function, and
160 * usage message. The usage messages need to be internationalized, so we have
161 * to have a function to return the usage message based on a command index.
163 * These commands are organized according to how they are displayed in the usage
164 * message. An empty command (one with a NULL name) indicates an empty line in
165 * the generic usage message.
167 static zfs_command_t command_table[] = {
168 { "create", zfs_do_create, HELP_CREATE },
169 { "destroy", zfs_do_destroy, HELP_DESTROY },
171 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
172 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
173 { "clone", zfs_do_clone, HELP_CLONE },
174 { "promote", zfs_do_promote, HELP_PROMOTE },
175 { "rename", zfs_do_rename, HELP_RENAME },
177 { "list", zfs_do_list, HELP_LIST },
179 { "set", zfs_do_set, HELP_SET },
180 { "get", zfs_do_get, HELP_GET },
181 { "inherit", zfs_do_inherit, HELP_INHERIT },
182 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
183 { "userspace", zfs_do_userspace, HELP_USERSPACE },
184 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
186 { "mount", zfs_do_mount, HELP_MOUNT },
187 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
188 { "share", zfs_do_share, HELP_SHARE },
189 { "unshare", zfs_do_unshare, HELP_UNSHARE },
191 { "send", zfs_do_send, HELP_SEND },
192 { "receive", zfs_do_receive, HELP_RECEIVE },
194 { "allow", zfs_do_allow, HELP_ALLOW },
196 { "unallow", zfs_do_unallow, HELP_UNALLOW },
198 { "hold", zfs_do_hold, HELP_HOLD },
199 { "holds", zfs_do_holds, HELP_HOLDS },
200 { "release", zfs_do_release, HELP_RELEASE },
201 { "diff", zfs_do_diff, HELP_DIFF },
204 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
206 zfs_command_t *current_command;
209 get_usage(zfs_help_t idx)
213 return (gettext("\tclone [-p] [-o property=value] ... "
214 "<snapshot> <filesystem|volume>\n"));
216 return (gettext("\tcreate [-p] [-o property=value] ... "
218 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
219 "-V <size> <volume>\n"));
221 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
222 "\tdestroy [-dnpRrv] "
223 "<filesystem|volume>@<snap>[%<snap>][,...]\n"));
225 return (gettext("\tget [-rHp] [-d max] "
226 "[-o \"all\" | field[,...]] [-t type[,...]] "
227 "[-s source[,...]]\n"
228 "\t <\"all\" | property[,...]> "
229 "[filesystem|volume|snapshot] ...\n"));
231 return (gettext("\tinherit [-rS] <property> "
232 "<filesystem|volume|snapshot> ...\n"));
234 return (gettext("\tupgrade [-v]\n"
235 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
237 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
238 "[-s property]...\n\t [-S property]... [-t type[,...]] "
239 "[filesystem|volume|snapshot] ...\n"));
241 return (gettext("\tmount\n"
242 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
244 return (gettext("\tpromote <clone-filesystem>\n"));
246 return (gettext("\treceive [-vnFu] <filesystem|volume|"
248 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
250 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
251 "<filesystem|volume|snapshot>\n"
252 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
253 "\trename -r <snapshot> <snapshot>"));
255 return (gettext("\trollback [-rRf] <snapshot>\n"));
257 return (gettext("\tsend [-DnPpRrv] [-[iI] snapshot] "
260 return (gettext("\tset <property=value> "
261 "<filesystem|volume|snapshot> ...\n"));
263 return (gettext("\tshare <-a | filesystem>\n"));
265 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
266 "<filesystem@snapname|volume@snapname> ...\n"));
268 return (gettext("\tunmount [-f] "
269 "<-a | filesystem|mountpoint>\n"));
271 return (gettext("\tunshare "
272 "<-a | filesystem|mountpoint>\n"));
274 return (gettext("\tallow <filesystem|volume>\n"
276 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
277 "\t <filesystem|volume>\n"
278 "\tallow [-ld] -e <perm|@setname>[,...] "
279 "<filesystem|volume>\n"
280 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
281 "\tallow -s @setname <perm|@setname>[,...] "
282 "<filesystem|volume>\n"));
284 return (gettext("\tunallow [-rldug] "
285 "<\"everyone\"|user|group>[,...]\n"
286 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
287 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
288 "<filesystem|volume>\n"
289 "\tunallow [-r] -c [<perm|@setname>[,...]] "
290 "<filesystem|volume>\n"
291 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
292 "<filesystem|volume>\n"));
294 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
295 "[-s field]...\n\t [-S field]... [-t type[,...]] "
296 "<filesystem|snapshot>\n"));
297 case HELP_GROUPSPACE:
298 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
299 "[-s field]...\n\t [-S field]... [-t type[,...]] "
300 "<filesystem|snapshot>\n"));
302 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
304 return (gettext("\tholds [-r] <snapshot> ...\n"));
306 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
308 return (gettext("\tdiff [-FHt] <snapshot> "
309 "[snapshot|filesystem]\n"));
319 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
324 * Utility function to guarantee malloc() success.
328 safe_malloc(size_t size)
332 if ((data = calloc(1, size)) == NULL)
339 safe_strdup(char *str)
341 char *dupstr = strdup(str);
350 * Callback routine that will print out information for each of
354 usage_prop_cb(int prop, void *cb)
358 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
360 if (zfs_prop_readonly(prop))
361 (void) fprintf(fp, " NO ");
363 (void) fprintf(fp, "YES ");
365 if (zfs_prop_inheritable(prop))
366 (void) fprintf(fp, " YES ");
368 (void) fprintf(fp, " NO ");
370 if (zfs_prop_values(prop) == NULL)
371 (void) fprintf(fp, "-\n");
373 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
379 * Display usage message. If we're inside a command, display only the usage for
380 * that command. Otherwise, iterate over the entire command table and display
381 * a complete usage message.
384 usage(boolean_t requested)
387 boolean_t show_properties = B_FALSE;
388 FILE *fp = requested ? stdout : stderr;
390 if (current_command == NULL) {
392 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
394 gettext("where 'command' is one of the following:\n\n"));
396 for (i = 0; i < NCOMMAND; i++) {
397 if (command_table[i].name == NULL)
398 (void) fprintf(fp, "\n");
400 (void) fprintf(fp, "%s",
401 get_usage(command_table[i].usage));
404 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
405 "pool/[dataset/]*dataset[@name]\n"));
407 (void) fprintf(fp, gettext("usage:\n"));
408 (void) fprintf(fp, "%s", get_usage(current_command->usage));
411 if (current_command != NULL &&
412 (strcmp(current_command->name, "set") == 0 ||
413 strcmp(current_command->name, "get") == 0 ||
414 strcmp(current_command->name, "inherit") == 0 ||
415 strcmp(current_command->name, "list") == 0))
416 show_properties = B_TRUE;
418 if (show_properties) {
420 gettext("\nThe following properties are supported:\n"));
422 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
423 "PROPERTY", "EDIT", "INHERIT", "VALUES");
425 /* Iterate over all properties */
426 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
429 (void) fprintf(fp, "\t%-15s ", "userused@...");
430 (void) fprintf(fp, " NO NO <size>\n");
431 (void) fprintf(fp, "\t%-15s ", "groupused@...");
432 (void) fprintf(fp, " NO NO <size>\n");
433 (void) fprintf(fp, "\t%-15s ", "userquota@...");
434 (void) fprintf(fp, "YES NO <size> | none\n");
435 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
436 (void) fprintf(fp, "YES NO <size> | none\n");
437 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
438 (void) fprintf(fp, " NO NO <size>\n");
440 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
441 "with standard units such as K, M, G, etc.\n"));
442 (void) fprintf(fp, gettext("\nUser-defined properties can "
443 "be specified by using a name containing a colon (:).\n"));
444 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
445 "properties must be appended with\n"
446 "a user or group specifier of one of these forms:\n"
447 " POSIX name (eg: \"matt\")\n"
448 " POSIX id (eg: \"126829\")\n"
449 " SMB name@domain (eg: \"matt@sun\")\n"
450 " SMB SID (eg: \"S-1-234-567-89\")\n"));
453 gettext("\nFor the property list, run: %s\n"),
456 gettext("\nFor the delegated permission list, run: %s\n"),
457 "zfs allow|unallow");
461 * See comments at end of main().
463 if (getenv("ZFS_ABORT") != NULL) {
464 (void) printf("dumping core by request\n");
468 exit(requested ? 0 : 2);
472 parseprop(nvlist_t *props)
474 char *propname = optarg;
475 char *propval, *strval;
477 if ((propval = strchr(propname, '=')) == NULL) {
478 (void) fprintf(stderr, gettext("missing "
479 "'=' for -o option\n"));
484 if (nvlist_lookup_string(props, propname, &strval) == 0) {
485 (void) fprintf(stderr, gettext("property '%s' "
486 "specified multiple times\n"), propname);
489 if (nvlist_add_string(props, propname, propval) != 0)
495 parse_depth(char *opt, int *flags)
500 depth = (int)strtol(opt, &tmp, 0);
502 (void) fprintf(stderr,
503 gettext("%s is not an integer\n"), optarg);
507 (void) fprintf(stderr,
508 gettext("Depth can not be negative.\n"));
511 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
515 #define PROGRESS_DELAY 2 /* seconds */
517 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";
518 static time_t pt_begin;
519 static char *pt_header = NULL;
520 static boolean_t pt_shown;
523 start_progress_timer(void)
525 pt_begin = time(NULL) + PROGRESS_DELAY;
530 set_progress_header(char *header)
532 assert(pt_header == NULL);
533 pt_header = safe_strdup(header);
535 (void) printf("%s: ", header);
536 (void) fflush(stdout);
541 update_progress(char *update)
543 if (!pt_shown && time(NULL) > pt_begin) {
544 int len = strlen(update);
546 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
548 (void) fflush(stdout);
550 } else if (pt_shown) {
551 int len = strlen(update);
553 (void) printf("%s%*.*s", update, len, len, pt_reverse);
554 (void) fflush(stdout);
559 finish_progress(char *done)
562 (void) printf("%s\n", done);
563 (void) fflush(stdout);
570 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
572 * Given an existing dataset, create a writable copy whose initial contents
573 * are the same as the source. The newly created dataset maintains a
574 * dependency on the original; the original cannot be destroyed so long as
577 * The '-p' flag creates all the non-existing ancestors of the target first.
580 zfs_do_clone(int argc, char **argv)
582 zfs_handle_t *zhp = NULL;
583 boolean_t parents = B_FALSE;
588 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
592 while ((c = getopt(argc, argv, "o:p")) != -1) {
595 if (parseprop(props))
602 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
611 /* check number of arguments */
613 (void) fprintf(stderr, gettext("missing source dataset "
618 (void) fprintf(stderr, gettext("missing target dataset "
623 (void) fprintf(stderr, gettext("too many arguments\n"));
627 /* open the source dataset */
628 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
631 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
634 * Now create the ancestors of the target dataset. If the
635 * target already exists and '-p' option was used we should not
638 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
641 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
646 ret = zfs_clone(zhp, argv[1], props);
648 /* create the mountpoint if necessary */
651 int canmount = ZFS_CANMOUNT_OFF;
654 (void) zpool_log_history(g_zfs, history_str);
655 log_history = B_FALSE;
658 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
661 * if the user doesn't want the dataset automatically
662 * mounted, then skip the mount/share step.
664 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT,
665 zfs_get_type(clone), B_FALSE))
666 canmount = zfs_prop_get_int(clone,
669 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME &&
670 canmount == ZFS_CANMOUNT_ON)
671 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
672 ret = zfs_share(clone);
691 * zfs create [-p] [-o prop=value] ... fs
692 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
694 * Create a new dataset. This command can be used to create filesystems
695 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
696 * For volumes, the user must specify a size to be used.
698 * The '-s' flag applies only to volumes, and indicates that we should not try
699 * to set the reservation for this volume. By default we set a reservation
700 * equal to the size for any volume. For pools with SPA_VERSION >=
701 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
703 * The '-p' flag creates all the non-existing ancestors of the target first.
706 zfs_do_create(int argc, char **argv)
708 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
709 zfs_handle_t *zhp = NULL;
710 uint64_t volsize = 0;
712 boolean_t noreserve = B_FALSE;
713 boolean_t bflag = B_FALSE;
714 boolean_t parents = B_FALSE;
718 int canmount = ZFS_CANMOUNT_OFF;
720 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
724 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
727 type = ZFS_TYPE_VOLUME;
728 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
729 (void) fprintf(stderr, gettext("bad volume "
730 "size '%s': %s\n"), optarg,
731 libzfs_error_description(g_zfs));
735 if (nvlist_add_uint64(props,
736 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
745 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
746 (void) fprintf(stderr, gettext("bad volume "
747 "block size '%s': %s\n"), optarg,
748 libzfs_error_description(g_zfs));
752 if (nvlist_add_uint64(props,
753 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
758 if (parseprop(props))
765 (void) fprintf(stderr, gettext("missing size "
770 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
776 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
777 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
778 "used when creating a volume\n"));
785 /* check number of arguments */
787 (void) fprintf(stderr, gettext("missing %s argument\n"),
788 zfs_type_to_name(type));
792 (void) fprintf(stderr, gettext("too many arguments\n"));
796 if (type == ZFS_TYPE_VOLUME && !noreserve) {
797 zpool_handle_t *zpool_handle;
798 uint64_t spa_version;
800 zfs_prop_t resv_prop;
803 if ((p = strchr(argv[0], '/')))
805 zpool_handle = zpool_open(g_zfs, argv[0]);
808 if (zpool_handle == NULL)
810 spa_version = zpool_get_prop_int(zpool_handle,
811 ZPOOL_PROP_VERSION, NULL);
812 zpool_close(zpool_handle);
813 if (spa_version >= SPA_VERSION_REFRESERVATION)
814 resv_prop = ZFS_PROP_REFRESERVATION;
816 resv_prop = ZFS_PROP_RESERVATION;
817 volsize = zvol_volsize_to_reservation(volsize, props);
819 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
821 if (nvlist_add_uint64(props,
822 zfs_prop_to_name(resv_prop), volsize) != 0) {
829 if (parents && zfs_name_valid(argv[0], type)) {
831 * Now create the ancestors of target dataset. If the target
832 * already exists and '-p' option was used we should not
835 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
839 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
844 if (zfs_create(g_zfs, argv[0], type, props) != 0)
848 (void) zpool_log_history(g_zfs, history_str);
849 log_history = B_FALSE;
852 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
857 * if the user doesn't want the dataset automatically mounted,
858 * then skip the mount/share step
860 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type, B_FALSE))
861 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
864 * Mount and/or share the new filesystem as appropriate. We provide a
865 * verbose error message to let the user know that their filesystem was
866 * in fact created, even if we failed to mount or share it.
868 if (canmount == ZFS_CANMOUNT_ON) {
869 if (zfs_mount(zhp, NULL, 0) != 0) {
870 (void) fprintf(stderr, gettext("filesystem "
871 "successfully created, but not mounted\n"));
873 } else if (zfs_share(zhp) != 0) {
874 (void) fprintf(stderr, gettext("filesystem "
875 "successfully created, but not shared\n"));
892 * zfs destroy [-rRf] <fs, vol>
893 * zfs destroy [-rRd] <snap>
895 * -r Recursively destroy all children
896 * -R Recursively destroy all dependents, including clones
897 * -f Force unmounting of any dependents
898 * -d If we can't destroy now, mark for deferred destruction
900 * Destroys the given dataset. By default, it will unmount any filesystems,
901 * and refuse to destroy a dataset that has any dependents. A dependent can
902 * either be a child, or a clone of a child.
904 typedef struct destroy_cbdata {
907 boolean_t cb_recurse;
909 boolean_t cb_doclones;
910 zfs_handle_t *cb_target;
911 boolean_t cb_defer_destroy;
912 boolean_t cb_verbose;
913 boolean_t cb_parsable;
916 nvlist_t *cb_batchedsnaps;
918 /* first snap in contiguous run */
920 /* previous snap in contiguous run */
927 * Check for any dependents based on the '-r' or '-R' flags.
930 destroy_check_dependent(zfs_handle_t *zhp, void *data)
932 destroy_cbdata_t *cbp = data;
933 const char *tname = zfs_get_name(cbp->cb_target);
934 const char *name = zfs_get_name(zhp);
936 if (strncmp(tname, name, strlen(tname)) == 0 &&
937 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
939 * This is a direct descendant, not a clone somewhere else in
946 (void) fprintf(stderr, gettext("cannot destroy '%s': "
947 "%s has children\n"),
948 zfs_get_name(cbp->cb_target),
949 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
950 (void) fprintf(stderr, gettext("use '-r' to destroy "
951 "the following datasets:\n"));
952 cbp->cb_first = B_FALSE;
953 cbp->cb_error = B_TRUE;
956 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
959 * This is a clone. We only want to report this if the '-r'
960 * wasn't specified, or the target is a snapshot.
962 if (!cbp->cb_recurse &&
963 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
967 (void) fprintf(stderr, gettext("cannot destroy '%s': "
968 "%s has dependent clones\n"),
969 zfs_get_name(cbp->cb_target),
970 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
971 (void) fprintf(stderr, gettext("use '-R' to destroy "
972 "the following datasets:\n"));
973 cbp->cb_first = B_FALSE;
974 cbp->cb_error = B_TRUE;
975 cbp->cb_dryrun = B_TRUE;
978 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
987 destroy_callback(zfs_handle_t *zhp, void *data)
989 destroy_cbdata_t *cb = data;
990 const char *name = zfs_get_name(zhp);
992 if (cb->cb_verbose) {
993 if (cb->cb_parsable) {
994 (void) printf("destroy\t%s\n", name);
995 } else if (cb->cb_dryrun) {
996 (void) printf(gettext("would destroy %s\n"),
999 (void) printf(gettext("will destroy %s\n"),
1005 * Ignore pools (which we've already flagged as an error before getting
1008 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1009 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1013 if (cb->cb_dryrun) {
1019 * We batch up all contiguous snapshots (even of different
1020 * filesystems) and destroy them with one ioctl. We can't
1021 * simply do all snap deletions and then all fs deletions,
1022 * because we must delete a clone before its origin.
1024 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1025 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1027 int error = zfs_destroy_snaps_nvl(g_zfs,
1028 cb->cb_batchedsnaps, B_FALSE);
1029 fnvlist_free(cb->cb_batchedsnaps);
1030 cb->cb_batchedsnaps = fnvlist_alloc();
1033 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1034 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1045 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1047 destroy_cbdata_t *cb = arg;
1048 const char *name = zfs_get_name(zhp);
1051 if (nvlist_exists(cb->cb_nvl, name)) {
1052 if (cb->cb_firstsnap == NULL)
1053 cb->cb_firstsnap = strdup(name);
1054 if (cb->cb_prevsnap != NULL)
1055 free(cb->cb_prevsnap);
1056 /* this snap continues the current range */
1057 cb->cb_prevsnap = strdup(name);
1058 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1060 if (cb->cb_verbose) {
1061 if (cb->cb_parsable) {
1062 (void) printf("destroy\t%s\n", name);
1063 } else if (cb->cb_dryrun) {
1064 (void) printf(gettext("would destroy %s\n"),
1067 (void) printf(gettext("will destroy %s\n"),
1071 } else if (cb->cb_firstsnap != NULL) {
1072 /* end of this range */
1074 err = lzc_snaprange_space(cb->cb_firstsnap,
1075 cb->cb_prevsnap, &used);
1076 cb->cb_snapused += used;
1077 free(cb->cb_firstsnap);
1078 cb->cb_firstsnap = NULL;
1079 free(cb->cb_prevsnap);
1080 cb->cb_prevsnap = NULL;
1087 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1090 assert(cb->cb_firstsnap == NULL);
1091 assert(cb->cb_prevsnap == NULL);
1092 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1093 if (cb->cb_firstsnap != NULL) {
1096 err = lzc_snaprange_space(cb->cb_firstsnap,
1097 cb->cb_prevsnap, &used);
1099 cb->cb_snapused += used;
1100 free(cb->cb_firstsnap);
1101 cb->cb_firstsnap = NULL;
1102 free(cb->cb_prevsnap);
1103 cb->cb_prevsnap = NULL;
1109 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1111 destroy_cbdata_t *cb = arg;
1114 /* Check for clones. */
1115 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1116 cb->cb_target = zhp;
1117 cb->cb_first = B_TRUE;
1118 err = zfs_iter_dependents(zhp, B_TRUE,
1119 destroy_check_dependent, cb);
1123 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1131 gather_snapshots(zfs_handle_t *zhp, void *arg)
1133 destroy_cbdata_t *cb = arg;
1136 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1142 if (cb->cb_verbose) {
1143 err = destroy_print_snapshots(zhp, cb);
1149 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1157 destroy_clones(destroy_cbdata_t *cb)
1160 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1162 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1163 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1166 boolean_t defer = cb->cb_defer_destroy;
1170 * We can't defer destroy non-snapshots, so set it to
1171 * false while destroying the clones.
1173 cb->cb_defer_destroy = B_FALSE;
1174 err = zfs_iter_dependents(zhp, B_FALSE,
1175 destroy_callback, cb);
1176 cb->cb_defer_destroy = defer;
1186 zfs_do_destroy(int argc, char **argv)
1188 destroy_cbdata_t cb = { 0 };
1192 zfs_handle_t *zhp = NULL;
1194 zfs_type_t type = ZFS_TYPE_DATASET;
1197 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1200 cb.cb_verbose = B_TRUE;
1203 cb.cb_verbose = B_TRUE;
1204 cb.cb_parsable = B_TRUE;
1207 cb.cb_dryrun = B_TRUE;
1210 cb.cb_defer_destroy = B_TRUE;
1211 type = ZFS_TYPE_SNAPSHOT;
1214 cb.cb_force = B_TRUE;
1217 cb.cb_recurse = B_TRUE;
1220 cb.cb_recurse = B_TRUE;
1221 cb.cb_doclones = B_TRUE;
1225 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1234 /* check number of arguments */
1236 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1240 (void) fprintf(stderr, gettext("too many arguments\n"));
1244 at = strchr(argv[0], '@');
1247 /* Build the list of snaps to destroy in cb_nvl. */
1248 cb.cb_nvl = fnvlist_alloc();
1251 zhp = zfs_open(g_zfs, argv[0],
1252 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1256 cb.cb_snapspec = at + 1;
1257 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1263 if (nvlist_empty(cb.cb_nvl)) {
1264 (void) fprintf(stderr, gettext("could not find any "
1265 "snapshots to destroy; check snapshot names.\n"));
1270 if (cb.cb_verbose) {
1272 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1273 if (cb.cb_parsable) {
1274 (void) printf("reclaim\t%llu\n",
1275 (u_longlong_t)cb.cb_snapused);
1276 } else if (cb.cb_dryrun) {
1277 (void) printf(gettext("would reclaim %s\n"),
1280 (void) printf(gettext("will reclaim %s\n"),
1285 if (!cb.cb_dryrun) {
1286 if (cb.cb_doclones) {
1287 cb.cb_batchedsnaps = fnvlist_alloc();
1288 err = destroy_clones(&cb);
1290 err = zfs_destroy_snaps_nvl(g_zfs,
1291 cb.cb_batchedsnaps, B_FALSE);
1299 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1300 cb.cb_defer_destroy);
1307 /* Open the given dataset */
1308 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1314 * Perform an explicit check for pools before going any further.
1316 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1317 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1318 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1319 "operation does not apply to pools\n"),
1321 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1322 "%s' to destroy all datasets in the pool\n"),
1324 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1325 "to destroy the pool itself\n"), zfs_get_name(zhp));
1331 * Check for any dependents and/or clones.
1333 cb.cb_first = B_TRUE;
1334 if (!cb.cb_doclones &&
1335 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1346 cb.cb_batchedsnaps = fnvlist_alloc();
1347 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1354 * Do the real thing. The callback will close the
1355 * handle regardless of whether it succeeds or not.
1357 err = destroy_callback(zhp, &cb);
1360 err = zfs_destroy_snaps_nvl(g_zfs,
1361 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1368 fnvlist_free(cb.cb_batchedsnaps);
1369 fnvlist_free(cb.cb_nvl);
1376 is_recvd_column(zprop_get_cbdata_t *cbp)
1379 zfs_get_column_t col;
1381 for (i = 0; i < ZFS_GET_NCOLS &&
1382 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1383 if (col == GET_COL_RECVD)
1389 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1390 * < all | property[,property]... > < fs | snap | vol > ...
1392 * -r recurse over any child datasets
1393 * -H scripted mode. Headers are stripped, and fields are separated
1394 * by tabs instead of spaces.
1395 * -o Set of fields to display. One of "name,property,value,
1396 * received,source". Default is "name,property,value,source".
1397 * "all" is an alias for all five.
1398 * -s Set of sources to allow. One of
1399 * "local,default,inherited,received,temporary,none". Default is
1401 * -p Display values in parsable (literal) format.
1403 * Prints properties for the given datasets. The user can control which
1404 * columns to display as well as which property types to allow.
1408 * Invoked to display the properties for a single dataset.
1411 get_callback(zfs_handle_t *zhp, void *data)
1413 char buf[ZFS_MAXPROPLEN];
1414 char rbuf[ZFS_MAXPROPLEN];
1415 zprop_source_t sourcetype;
1416 char source[ZFS_MAXNAMELEN];
1417 zprop_get_cbdata_t *cbp = data;
1418 nvlist_t *user_props = zfs_get_user_props(zhp);
1419 zprop_list_t *pl = cbp->cb_proplist;
1423 boolean_t received = is_recvd_column(cbp);
1425 for (; pl != NULL; pl = pl->pl_next) {
1426 char *recvdval = NULL;
1428 * Skip the special fake placeholder. This will also skip over
1429 * the name property when 'all' is specified.
1431 if (pl->pl_prop == ZFS_PROP_NAME &&
1432 pl == cbp->cb_proplist)
1435 if (pl->pl_prop != ZPROP_INVAL) {
1436 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1437 sizeof (buf), &sourcetype, source,
1439 cbp->cb_literal) != 0) {
1442 if (!zfs_prop_valid_for_type(pl->pl_prop,
1443 ZFS_TYPE_DATASET, B_FALSE)) {
1444 (void) fprintf(stderr,
1445 gettext("No such property '%s'\n"),
1446 zfs_prop_to_name(pl->pl_prop));
1449 sourcetype = ZPROP_SRC_NONE;
1450 (void) strlcpy(buf, "-", sizeof (buf));
1453 if (received && (zfs_prop_get_recvd(zhp,
1454 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1455 cbp->cb_literal) == 0))
1458 zprop_print_one_property(zfs_get_name(zhp), cbp,
1459 zfs_prop_to_name(pl->pl_prop),
1460 buf, sourcetype, source, recvdval);
1461 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1462 sourcetype = ZPROP_SRC_LOCAL;
1464 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1465 buf, sizeof (buf), cbp->cb_literal) != 0) {
1466 sourcetype = ZPROP_SRC_NONE;
1467 (void) strlcpy(buf, "-", sizeof (buf));
1470 zprop_print_one_property(zfs_get_name(zhp), cbp,
1471 pl->pl_user_prop, buf, sourcetype, source, NULL);
1472 } else if (zfs_prop_written(pl->pl_user_prop)) {
1473 sourcetype = ZPROP_SRC_LOCAL;
1475 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1476 buf, sizeof (buf), cbp->cb_literal) != 0) {
1477 sourcetype = ZPROP_SRC_NONE;
1478 (void) strlcpy(buf, "-", sizeof (buf));
1481 zprop_print_one_property(zfs_get_name(zhp), cbp,
1482 pl->pl_user_prop, buf, sourcetype, source, NULL);
1484 if (nvlist_lookup_nvlist(user_props,
1485 pl->pl_user_prop, &propval) != 0) {
1488 sourcetype = ZPROP_SRC_NONE;
1491 verify(nvlist_lookup_string(propval,
1492 ZPROP_VALUE, &strval) == 0);
1493 verify(nvlist_lookup_string(propval,
1494 ZPROP_SOURCE, &sourceval) == 0);
1496 if (strcmp(sourceval,
1497 zfs_get_name(zhp)) == 0) {
1498 sourcetype = ZPROP_SRC_LOCAL;
1499 } else if (strcmp(sourceval,
1500 ZPROP_SOURCE_VAL_RECVD) == 0) {
1501 sourcetype = ZPROP_SRC_RECEIVED;
1503 sourcetype = ZPROP_SRC_INHERITED;
1504 (void) strlcpy(source,
1505 sourceval, sizeof (source));
1509 if (received && (zfs_prop_get_recvd(zhp,
1510 pl->pl_user_prop, rbuf, sizeof (rbuf),
1511 cbp->cb_literal) == 0))
1514 zprop_print_one_property(zfs_get_name(zhp), cbp,
1515 pl->pl_user_prop, strval, sourcetype,
1524 zfs_do_get(int argc, char **argv)
1526 zprop_get_cbdata_t cb = { 0 };
1527 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1528 int types = ZFS_TYPE_DATASET;
1529 char *value, *fields;
1532 zprop_list_t fake_name = { 0 };
1535 * Set up default columns and sources.
1537 cb.cb_sources = ZPROP_SRC_ALL;
1538 cb.cb_columns[0] = GET_COL_NAME;
1539 cb.cb_columns[1] = GET_COL_PROPERTY;
1540 cb.cb_columns[2] = GET_COL_VALUE;
1541 cb.cb_columns[3] = GET_COL_SOURCE;
1542 cb.cb_type = ZFS_TYPE_DATASET;
1545 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1548 cb.cb_literal = B_TRUE;
1551 limit = parse_depth(optarg, &flags);
1554 flags |= ZFS_ITER_RECURSE;
1557 cb.cb_scripted = B_TRUE;
1560 (void) fprintf(stderr, gettext("missing argument for "
1561 "'%c' option\n"), optopt);
1566 * Process the set of columns to display. We zero out
1567 * the structure to give us a blank slate.
1569 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1571 while (*optarg != '\0') {
1572 static char *col_subopts[] =
1573 { "name", "property", "value", "received",
1574 "source", "all", NULL };
1576 if (i == ZFS_GET_NCOLS) {
1577 (void) fprintf(stderr, gettext("too "
1578 "many fields given to -o "
1583 switch (getsubopt(&optarg, col_subopts,
1586 cb.cb_columns[i++] = GET_COL_NAME;
1589 cb.cb_columns[i++] = GET_COL_PROPERTY;
1592 cb.cb_columns[i++] = GET_COL_VALUE;
1595 cb.cb_columns[i++] = GET_COL_RECVD;
1596 flags |= ZFS_ITER_RECVD_PROPS;
1599 cb.cb_columns[i++] = GET_COL_SOURCE;
1603 (void) fprintf(stderr,
1604 gettext("\"all\" conflicts "
1605 "with specific fields "
1606 "given to -o option\n"));
1609 cb.cb_columns[0] = GET_COL_NAME;
1610 cb.cb_columns[1] = GET_COL_PROPERTY;
1611 cb.cb_columns[2] = GET_COL_VALUE;
1612 cb.cb_columns[3] = GET_COL_RECVD;
1613 cb.cb_columns[4] = GET_COL_SOURCE;
1614 flags |= ZFS_ITER_RECVD_PROPS;
1618 (void) fprintf(stderr,
1619 gettext("invalid column name "
1628 while (*optarg != '\0') {
1629 static char *source_subopts[] = {
1630 "local", "default", "inherited",
1631 "received", "temporary", "none",
1634 switch (getsubopt(&optarg, source_subopts,
1637 cb.cb_sources |= ZPROP_SRC_LOCAL;
1640 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1643 cb.cb_sources |= ZPROP_SRC_INHERITED;
1646 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1649 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1652 cb.cb_sources |= ZPROP_SRC_NONE;
1655 (void) fprintf(stderr,
1656 gettext("invalid source "
1665 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1666 while (*optarg != '\0') {
1667 static char *type_subopts[] = { "filesystem",
1668 "volume", "snapshot", "all", NULL };
1670 switch (getsubopt(&optarg, type_subopts,
1673 types |= ZFS_TYPE_FILESYSTEM;
1676 types |= ZFS_TYPE_VOLUME;
1679 types |= ZFS_TYPE_SNAPSHOT;
1682 types = ZFS_TYPE_DATASET;
1686 (void) fprintf(stderr,
1687 gettext("invalid type '%s'\n"),
1695 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1705 (void) fprintf(stderr, gettext("missing property "
1712 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1720 * As part of zfs_expand_proplist(), we keep track of the maximum column
1721 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1722 * need to know the maximum name length. However, the user likely did
1723 * not specify 'name' as one of the properties to fetch, so we need to
1724 * make sure we always include at least this property for
1725 * print_get_headers() to work properly.
1727 if (cb.cb_proplist != NULL) {
1728 fake_name.pl_prop = ZFS_PROP_NAME;
1729 fake_name.pl_width = strlen(gettext("NAME"));
1730 fake_name.pl_next = cb.cb_proplist;
1731 cb.cb_proplist = &fake_name;
1734 cb.cb_first = B_TRUE;
1736 /* run for each object */
1737 ret = zfs_for_each(argc, argv, flags, types, NULL,
1738 &cb.cb_proplist, limit, get_callback, &cb);
1740 if (cb.cb_proplist == &fake_name)
1741 zprop_free_list(fake_name.pl_next);
1743 zprop_free_list(cb.cb_proplist);
1749 * inherit [-rS] <property> <fs|vol> ...
1751 * -r Recurse over all children
1752 * -S Revert to received value, if any
1754 * For each dataset specified on the command line, inherit the given property
1755 * from its parent. Inheriting a property at the pool level will cause it to
1756 * use the default value. The '-r' flag will recurse over all children, and is
1757 * useful for setting a property on a hierarchy-wide basis, regardless of any
1758 * local modifications for each dataset.
1761 typedef struct inherit_cbdata {
1762 const char *cb_propname;
1763 boolean_t cb_received;
1767 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1769 inherit_cbdata_t *cb = data;
1770 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1773 * If we're doing it recursively, then ignore properties that
1774 * are not valid for this type of dataset.
1776 if (prop != ZPROP_INVAL &&
1777 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp), B_FALSE))
1780 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1784 inherit_cb(zfs_handle_t *zhp, void *data)
1786 inherit_cbdata_t *cb = data;
1788 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1792 zfs_do_inherit(int argc, char **argv)
1796 inherit_cbdata_t cb = { 0 };
1800 boolean_t received = B_FALSE;
1803 while ((c = getopt(argc, argv, "rS")) != -1) {
1806 flags |= ZFS_ITER_RECURSE;
1813 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1822 /* check number of arguments */
1824 (void) fprintf(stderr, gettext("missing property argument\n"));
1828 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1836 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1837 if (zfs_prop_readonly(prop)) {
1838 (void) fprintf(stderr, gettext(
1839 "%s property is read-only\n"),
1843 if (!zfs_prop_inheritable(prop) && !received) {
1844 (void) fprintf(stderr, gettext("'%s' property cannot "
1845 "be inherited\n"), propname);
1846 if (prop == ZFS_PROP_QUOTA ||
1847 prop == ZFS_PROP_RESERVATION ||
1848 prop == ZFS_PROP_REFQUOTA ||
1849 prop == ZFS_PROP_REFRESERVATION)
1850 (void) fprintf(stderr, gettext("use 'zfs set "
1851 "%s=none' to clear\n"), propname);
1854 if (received && (prop == ZFS_PROP_VOLSIZE ||
1855 prop == ZFS_PROP_VERSION)) {
1856 (void) fprintf(stderr, gettext("'%s' property cannot "
1857 "be reverted to a received value\n"), propname);
1860 } else if (!zfs_prop_user(propname)) {
1861 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1866 cb.cb_propname = propname;
1867 cb.cb_received = received;
1869 if (flags & ZFS_ITER_RECURSE) {
1870 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1871 NULL, NULL, 0, inherit_recurse_cb, &cb);
1873 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1874 NULL, NULL, 0, inherit_cb, &cb);
1880 typedef struct upgrade_cbdata {
1881 uint64_t cb_numupgraded;
1882 uint64_t cb_numsamegraded;
1883 uint64_t cb_numfailed;
1884 uint64_t cb_version;
1886 boolean_t cb_foundone;
1887 char cb_lastfs[ZFS_MAXNAMELEN];
1891 same_pool(zfs_handle_t *zhp, const char *name)
1893 int len1 = strcspn(name, "/@");
1894 const char *zhname = zfs_get_name(zhp);
1895 int len2 = strcspn(zhname, "/@");
1899 return (strncmp(name, zhname, len1) == 0);
1903 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1905 upgrade_cbdata_t *cb = data;
1906 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1908 /* list if it's old/new */
1909 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1910 (cb->cb_newer && version > ZPL_VERSION)) {
1913 str = gettext("The following filesystems are "
1914 "formatted using a newer software version and\n"
1915 "cannot be accessed on the current system.\n\n");
1917 str = gettext("The following filesystems are "
1918 "out of date, and can be upgraded. After being\n"
1919 "upgraded, these filesystems (and any 'zfs send' "
1920 "streams generated from\n"
1921 "subsequent snapshots) will no longer be "
1922 "accessible by older software versions.\n\n");
1925 if (!cb->cb_foundone) {
1927 (void) printf(gettext("VER FILESYSTEM\n"));
1928 (void) printf(gettext("--- ------------\n"));
1929 cb->cb_foundone = B_TRUE;
1932 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1939 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1941 upgrade_cbdata_t *cb = data;
1942 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1943 int needed_spa_version;
1946 if (zfs_spa_version(zhp, &spa_version) < 0)
1949 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1951 if (needed_spa_version < 0)
1954 if (spa_version < needed_spa_version) {
1956 (void) printf(gettext("%s: can not be "
1957 "upgraded; the pool version needs to first "
1958 "be upgraded\nto version %d\n\n"),
1959 zfs_get_name(zhp), needed_spa_version);
1965 if (version < cb->cb_version) {
1967 (void) snprintf(verstr, sizeof (verstr),
1968 "%llu", (u_longlong_t)cb->cb_version);
1969 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1971 * If they did "zfs upgrade -a", then we could
1972 * be doing ioctls to different pools. We need
1973 * to log this history once to each pool, and bypass
1974 * the normal history logging that happens in main().
1976 (void) zpool_log_history(g_zfs, history_str);
1977 log_history = B_FALSE;
1979 if (zfs_prop_set(zhp, "version", verstr) == 0)
1980 cb->cb_numupgraded++;
1983 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1984 } else if (version > cb->cb_version) {
1985 /* can't downgrade */
1986 (void) printf(gettext("%s: can not be downgraded; "
1987 "it is already at version %u\n"),
1988 zfs_get_name(zhp), version);
1991 cb->cb_numsamegraded++;
1999 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2002 zfs_do_upgrade(int argc, char **argv)
2004 boolean_t all = B_FALSE;
2005 boolean_t showversions = B_FALSE;
2007 upgrade_cbdata_t cb = { 0 };
2009 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2012 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2015 flags |= ZFS_ITER_RECURSE;
2018 showversions = B_TRUE;
2021 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2022 optarg, &cb.cb_version) != 0) {
2023 (void) fprintf(stderr,
2024 gettext("invalid version %s\n"), optarg);
2033 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2042 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2044 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2045 cb.cb_version || argc))
2047 if ((all || argc) && (showversions))
2053 /* Show info on available versions. */
2054 (void) printf(gettext("The following filesystem versions are "
2056 (void) printf(gettext("VER DESCRIPTION\n"));
2057 (void) printf("--- -----------------------------------------"
2058 "---------------\n");
2059 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2060 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2061 (void) printf(gettext(" 3 Case insensitive and filesystem "
2062 "user identifier (FUID)\n"));
2063 (void) printf(gettext(" 4 userquota, groupquota "
2065 (void) printf(gettext(" 5 System attributes\n"));
2066 (void) printf(gettext("\nFor more information on a particular "
2067 "version, including supported releases,\n"));
2068 (void) printf("see the ZFS Administration Guide.\n\n");
2070 } else if (argc || all) {
2071 /* Upgrade filesystems */
2072 if (cb.cb_version == 0)
2073 cb.cb_version = ZPL_VERSION;
2074 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2075 NULL, NULL, 0, upgrade_set_callback, &cb);
2076 (void) printf(gettext("%llu filesystems upgraded\n"),
2077 (u_longlong_t)cb.cb_numupgraded);
2078 if (cb.cb_numsamegraded) {
2079 (void) printf(gettext("%llu filesystems already at "
2081 (u_longlong_t)cb.cb_numsamegraded);
2083 if (cb.cb_numfailed != 0)
2086 /* List old-version filesytems */
2088 (void) printf(gettext("This system is currently running "
2089 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2091 flags |= ZFS_ITER_RECURSE;
2092 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2093 NULL, NULL, 0, upgrade_list_callback, &cb);
2095 found = cb.cb_foundone;
2096 cb.cb_foundone = B_FALSE;
2097 cb.cb_newer = B_TRUE;
2099 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2100 NULL, NULL, 0, upgrade_list_callback, &cb);
2102 if (!cb.cb_foundone && !found) {
2103 (void) printf(gettext("All filesystems are "
2104 "formatted with the current version.\n"));
2112 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2113 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2114 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2115 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2117 * -H Scripted mode; elide headers and separate columns by tabs.
2118 * -i Translate SID to POSIX ID.
2119 * -n Print numeric ID instead of user/group name.
2120 * -o Control which fields to display.
2121 * -p Use exact (parsable) numeric output.
2122 * -s Specify sort columns, descending order.
2123 * -S Specify sort columns, ascending order.
2124 * -t Control which object types to display.
2126 * Displays space consumed by, and quotas on, each user in the specified
2127 * filesystem or snapshot.
2130 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2131 enum us_field_types {
2137 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2138 static char *us_field_names[] = { "type", "name", "used", "quota" };
2139 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2141 #define USTYPE_PSX_GRP (1 << 0)
2142 #define USTYPE_PSX_USR (1 << 1)
2143 #define USTYPE_SMB_GRP (1 << 2)
2144 #define USTYPE_SMB_USR (1 << 3)
2145 #define USTYPE_ALL \
2146 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2148 static int us_type_bits[] = {
2155 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2158 typedef struct us_node {
2160 uu_avl_node_t usn_avlnode;
2161 uu_list_node_t usn_listnode;
2164 typedef struct us_cbdata {
2166 uu_avl_pool_t *cb_avl_pool;
2168 boolean_t cb_numname;
2169 boolean_t cb_nicenum;
2170 boolean_t cb_sid2posix;
2171 zfs_userquota_prop_t cb_prop;
2172 zfs_sort_column_t *cb_sortcol;
2173 size_t cb_width[USFIELD_LAST];
2176 static boolean_t us_populated = B_FALSE;
2179 zfs_sort_column_t *si_sortcol;
2180 boolean_t si_numname;
2184 us_field_index(char *field)
2188 for (i = 0; i < USFIELD_LAST; i++) {
2189 if (strcmp(field, us_field_names[i]) == 0)
2197 us_compare(const void *larg, const void *rarg, void *unused)
2199 const us_node_t *l = larg;
2200 const us_node_t *r = rarg;
2201 us_sort_info_t *si = (us_sort_info_t *)unused;
2202 zfs_sort_column_t *sortcol = si->si_sortcol;
2203 boolean_t numname = si->si_numname;
2204 nvlist_t *lnvl = l->usn_nvl;
2205 nvlist_t *rnvl = r->usn_nvl;
2209 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2216 zfs_prop_t prop = sortcol->sc_prop;
2217 const char *propname = NULL;
2218 boolean_t reverse = sortcol->sc_reverse;
2223 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2224 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2226 rc = (rv32 < lv32) ? 1 : -1;
2231 (void) nvlist_lookup_uint64(lnvl, propname,
2233 (void) nvlist_lookup_uint64(rnvl, propname,
2236 rc = (rv64 < lv64) ? 1 : -1;
2238 (void) nvlist_lookup_string(lnvl, propname,
2240 (void) nvlist_lookup_string(rnvl, propname,
2242 rc = strcmp(lvstr, rvstr);
2246 case ZFS_PROP_QUOTA:
2249 if (prop == ZFS_PROP_USED)
2253 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2254 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2256 rc = (rv64 < lv64) ? 1 : -1;
2264 return (reverse ? 1 : -1);
2266 return (reverse ? -1 : 1);
2271 * If entries still seem to be the same, check if they are of the same
2272 * type (smbentity is added only if we are doing SID to POSIX ID
2273 * translation where we can have duplicate type/name combinations).
2275 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2276 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2278 return (lvb < rvb ? -1 : 1);
2283 static inline const char *
2284 us_type2str(unsigned field_type)
2286 switch (field_type) {
2287 case USTYPE_PSX_USR:
2288 return ("POSIX User");
2289 case USTYPE_PSX_GRP:
2290 return ("POSIX Group");
2291 case USTYPE_SMB_USR:
2292 return ("SMB User");
2293 case USTYPE_SMB_GRP:
2294 return ("SMB Group");
2296 return ("Undefined");
2301 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2303 us_cbdata_t *cb = (us_cbdata_t *)arg;
2304 zfs_userquota_prop_t prop = cb->cb_prop;
2309 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2310 uu_avl_t *avl = cb->cb_avl;
2314 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2316 const char *typestr;
2320 int typeidx, nameidx, sizeidx;
2321 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2322 boolean_t smbentity = B_FALSE;
2324 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2326 node = safe_malloc(sizeof (us_node_t));
2327 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2328 node->usn_nvl = props;
2330 if (domain != NULL && domain[0] != '\0') {
2333 char sid[ZFS_MAXNAMELEN + 32];
2337 directory_error_t e;
2341 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2343 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2344 type = USTYPE_SMB_GRP;
2345 err = sid_to_id(sid, B_FALSE, &id);
2347 type = USTYPE_SMB_USR;
2348 err = sid_to_id(sid, B_TRUE, &id);
2353 if (!cb->cb_sid2posix) {
2354 e = directory_name_from_sid(NULL, sid, &name,
2357 directory_error_free(e);
2367 #endif /* HAVE_IDMAP */
2370 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2372 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2373 type = USTYPE_PSX_GRP;
2374 if (!cb->cb_numname) {
2377 if ((g = getgrgid(rid)) != NULL)
2381 type = USTYPE_PSX_USR;
2382 if (!cb->cb_numname) {
2385 if ((p = getpwuid(rid)) != NULL)
2392 * Make sure that the type/name combination is unique when doing
2393 * SID to POSIX ID translation (hence changing the type from SMB to
2396 if (cb->cb_sid2posix &&
2397 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2400 /* Calculate/update width of TYPE field */
2401 typestr = us_type2str(type);
2402 typelen = strlen(gettext(typestr));
2403 typeidx = us_field_index("type");
2404 if (typelen > cb->cb_width[typeidx])
2405 cb->cb_width[typeidx] = typelen;
2406 if (nvlist_add_uint32(props, "type", type) != 0)
2409 /* Calculate/update width of NAME field */
2410 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2411 if (nvlist_add_uint64(props, "name", rid) != 0)
2413 namelen = snprintf(NULL, 0, "%u", rid);
2415 if (nvlist_add_string(props, "name", name) != 0)
2417 namelen = strlen(name);
2419 nameidx = us_field_index("name");
2420 if (namelen > cb->cb_width[nameidx])
2421 cb->cb_width[nameidx] = namelen;
2424 * Check if this type/name combination is in the list and update it;
2425 * otherwise add new node to the list.
2427 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2428 uu_avl_insert(avl, node, idx);
2433 props = node->usn_nvl;
2436 /* Calculate/update width of USED/QUOTA fields */
2438 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2440 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
2441 (u_longlong_t)space);
2442 sizelen = strlen(sizebuf);
2443 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2445 if (!nvlist_exists(props, "quota"))
2446 (void) nvlist_add_uint64(props, "quota", 0);
2449 if (!nvlist_exists(props, "used"))
2450 (void) nvlist_add_uint64(props, "used", 0);
2452 sizeidx = us_field_index(propname);
2453 if (sizelen > cb->cb_width[sizeidx])
2454 cb->cb_width[sizeidx] = sizelen;
2456 if (nvlist_add_uint64(props, propname, space) != 0)
2463 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2464 size_t *width, us_node_t *node)
2466 nvlist_t *nvl = node->usn_nvl;
2467 char valstr[ZFS_MAXNAMELEN];
2468 boolean_t first = B_TRUE;
2474 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2475 if (!(ustype & types))
2478 while ((field = fields[cfield]) != USFIELD_LAST) {
2479 nvpair_t *nvp = NULL;
2483 char *strval = NULL;
2485 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2486 if (strcmp(nvpair_name(nvp),
2487 us_field_names[field]) == 0)
2491 type = nvpair_type(nvp);
2493 case DATA_TYPE_UINT32:
2494 (void) nvpair_value_uint32(nvp, &val32);
2496 case DATA_TYPE_UINT64:
2497 (void) nvpair_value_uint64(nvp, &val64);
2499 case DATA_TYPE_STRING:
2500 (void) nvpair_value_string(nvp, &strval);
2503 (void) fprintf(stderr, "invalid data type\n");
2508 strval = (char *)us_type2str(val32);
2511 if (type == DATA_TYPE_UINT64) {
2512 (void) sprintf(valstr, "%llu",
2513 (u_longlong_t) val64);
2519 if (type == DATA_TYPE_UINT64) {
2521 (void) sprintf(valstr, "%llu",
2522 (u_longlong_t) val64);
2524 zfs_nicenum(val64, valstr,
2527 if (field == USFIELD_QUOTA &&
2528 strcmp(valstr, "0") == 0)
2538 (void) printf("\t");
2543 (void) printf("%s", strval);
2544 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2545 (void) printf("%-*s", (int) width[field], strval);
2547 (void) printf("%*s", (int) width[field], strval);
2553 (void) printf("\n");
2557 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2558 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2566 boolean_t first = B_TRUE;
2568 while ((field = fields[cfield]) != USFIELD_LAST) {
2569 col = gettext(us_field_hdr[field]);
2570 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2571 (void) printf(first ? "%-*s" : " %-*s",
2572 (int) width[field], col);
2574 (void) printf(first ? "%*s" : " %*s",
2575 (int) width[field], col);
2580 (void) printf("\n");
2583 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2584 print_us_node(scripted, parsable, fields, types, width, node);
2586 nvlist_free(node->usn_nvl);
2591 zfs_do_userspace(int argc, char **argv)
2594 zfs_userquota_prop_t p;
2595 uu_avl_pool_t *avl_pool;
2597 uu_avl_walk_t *walk;
2599 char deffields[] = "type,name,used,quota";
2600 char *ofield = NULL;
2601 char *tfield = NULL;
2605 boolean_t scripted = B_FALSE;
2606 boolean_t prtnum = B_FALSE;
2607 boolean_t parsable = B_FALSE;
2608 boolean_t sid2posix = B_FALSE;
2611 zfs_sort_column_t *sortcol = NULL;
2612 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2616 uu_list_pool_t *listpool;
2618 uu_avl_index_t idx = 0;
2619 uu_list_index_t idx2 = 0;
2624 if (strcmp(argv[0], "groupspace") == 0)
2625 /* Toggle default group types */
2626 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2628 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2644 if (zfs_add_sort_column(&sortcol, optarg,
2645 c == 's' ? B_FALSE : B_TRUE) != 0) {
2646 (void) fprintf(stderr,
2647 gettext("invalid field '%s'\n"), optarg);
2658 (void) fprintf(stderr, gettext("missing argument for "
2659 "'%c' option\n"), optopt);
2663 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2673 (void) fprintf(stderr, gettext("missing dataset name\n"));
2677 (void) fprintf(stderr, gettext("too many arguments\n"));
2681 /* Use default output fields if not specified using -o */
2685 if ((delim = strchr(ofield, ',')) != NULL)
2687 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2688 (void) fprintf(stderr, gettext("invalid type '%s' "
2689 "for -o option\n"), ofield);
2694 } while (delim != NULL);
2695 fields[cfield] = USFIELD_LAST;
2697 /* Override output types (-t option) */
2698 if (tfield != NULL) {
2702 boolean_t found = B_FALSE;
2704 if ((delim = strchr(tfield, ',')) != NULL)
2706 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2708 if (strcmp(tfield, us_type_names[i]) == 0) {
2710 types |= us_type_bits[i];
2715 (void) fprintf(stderr, gettext("invalid type "
2716 "'%s' for -t option\n"), tfield);
2721 } while (delim != NULL);
2724 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2727 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2728 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2730 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2733 /* Always add default sorting columns */
2734 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2735 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2737 cb.cb_sortcol = sortcol;
2738 cb.cb_numname = prtnum;
2739 cb.cb_nicenum = !parsable;
2740 cb.cb_avl_pool = avl_pool;
2741 cb.cb_avl = avl_tree;
2742 cb.cb_sid2posix = sid2posix;
2744 for (i = 0; i < USFIELD_LAST; i++)
2745 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2747 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2748 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2749 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2750 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2751 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2754 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2759 if ((node = uu_avl_first(avl_tree)) == NULL)
2762 us_populated = B_TRUE;
2764 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2765 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2766 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2767 uu_list_node_init(node, &node->usn_listnode, listpool);
2769 while (node != NULL) {
2771 node = uu_avl_next(avl_tree, node);
2772 uu_avl_remove(avl_tree, rmnode);
2773 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2774 uu_list_insert(list, rmnode, idx2);
2777 for (node = uu_list_first(list); node != NULL;
2778 node = uu_list_next(list, node)) {
2779 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2781 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2782 uu_avl_insert(avl_tree, node, idx);
2785 uu_list_destroy(list);
2786 uu_list_pool_destroy(listpool);
2788 /* Print and free node nvlist memory */
2789 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2792 zfs_free_sort_columns(sortcol);
2794 /* Clean up the AVL tree */
2795 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2798 while ((node = uu_avl_walk_next(walk)) != NULL) {
2799 uu_avl_remove(cb.cb_avl, node);
2803 uu_avl_walk_end(walk);
2804 uu_avl_destroy(avl_tree);
2805 uu_avl_pool_destroy(avl_pool);
2811 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
2812 * [-t type[,...]] [filesystem|volume|snapshot] ...
2814 * -H Scripted mode; elide headers and separate columns by tabs
2815 * -p Display values in parsable (literal) format.
2816 * -r Recurse over all children
2817 * -d Limit recursion by depth.
2818 * -o Control which fields to display.
2819 * -s Specify sort columns, descending order.
2820 * -S Specify sort columns, ascending order.
2821 * -t Control which object types to display.
2823 * When given no arguments, list all filesystems in the system.
2824 * Otherwise, list the specified datasets, optionally recursing down them if
2825 * '-r' is specified.
2827 typedef struct list_cbdata {
2829 boolean_t cb_literal;
2830 boolean_t cb_scripted;
2831 zprop_list_t *cb_proplist;
2835 * Given a list of columns to display, output appropriate headers for each one.
2838 print_header(list_cbdata_t *cb)
2840 zprop_list_t *pl = cb->cb_proplist;
2841 char headerbuf[ZFS_MAXPROPLEN];
2844 boolean_t first = B_TRUE;
2845 boolean_t right_justify;
2847 for (; pl != NULL; pl = pl->pl_next) {
2854 right_justify = B_FALSE;
2855 if (pl->pl_prop != ZPROP_INVAL) {
2856 header = zfs_prop_column_name(pl->pl_prop);
2857 right_justify = zfs_prop_align_right(pl->pl_prop);
2859 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2860 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2861 headerbuf[i] = '\0';
2865 if (pl->pl_next == NULL && !right_justify)
2866 (void) printf("%s", header);
2867 else if (right_justify)
2868 (void) printf("%*s", (int)pl->pl_width, header);
2870 (void) printf("%-*s", (int)pl->pl_width, header);
2873 (void) printf("\n");
2877 * Given a dataset and a list of fields, print out all the properties according
2878 * to the described layout.
2881 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2883 zprop_list_t *pl = cb->cb_proplist;
2884 boolean_t first = B_TRUE;
2885 char property[ZFS_MAXPROPLEN];
2886 nvlist_t *userprops = zfs_get_user_props(zhp);
2889 boolean_t right_justify;
2891 for (; pl != NULL; pl = pl->pl_next) {
2893 if (cb->cb_scripted)
2894 (void) printf("\t");
2901 if (pl->pl_prop == ZFS_PROP_NAME) {
2902 (void) strlcpy(property, zfs_get_name(zhp),
2905 right_justify = zfs_prop_align_right(pl->pl_prop);
2906 } else if (pl->pl_prop != ZPROP_INVAL) {
2907 if (zfs_prop_get(zhp, pl->pl_prop, property,
2908 sizeof (property), NULL, NULL, 0,
2909 cb->cb_literal) != 0)
2913 right_justify = zfs_prop_align_right(pl->pl_prop);
2914 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2915 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2916 property, sizeof (property), cb->cb_literal) != 0)
2920 right_justify = B_TRUE;
2921 } else if (zfs_prop_written(pl->pl_user_prop)) {
2922 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2923 property, sizeof (property), cb->cb_literal) != 0)
2927 right_justify = B_TRUE;
2929 if (nvlist_lookup_nvlist(userprops,
2930 pl->pl_user_prop, &propval) != 0)
2933 verify(nvlist_lookup_string(propval,
2934 ZPROP_VALUE, &propstr) == 0);
2935 right_justify = B_FALSE;
2939 * If this is being called in scripted mode, or if this is the
2940 * last column and it is left-justified, don't include a width
2943 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2944 (void) printf("%s", propstr);
2945 else if (right_justify)
2946 (void) printf("%*s", (int)pl->pl_width, propstr);
2948 (void) printf("%-*s", (int)pl->pl_width, propstr);
2951 (void) printf("\n");
2955 * Generic callback function to list a dataset or snapshot.
2958 list_callback(zfs_handle_t *zhp, void *data)
2960 list_cbdata_t *cbp = data;
2962 if (cbp->cb_first) {
2963 if (!cbp->cb_scripted)
2965 cbp->cb_first = B_FALSE;
2968 print_dataset(zhp, cbp);
2974 zfs_do_list(int argc, char **argv)
2977 static char default_fields[] =
2978 "name,used,available,referenced,mountpoint";
2979 int types = ZFS_TYPE_DATASET;
2980 boolean_t types_specified = B_FALSE;
2981 char *fields = NULL;
2982 list_cbdata_t cb = { 0 };
2986 zfs_sort_column_t *sortcol = NULL;
2987 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2990 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
2996 cb.cb_literal = B_TRUE;
2997 flags |= ZFS_ITER_LITERAL_PROPS;
3000 limit = parse_depth(optarg, &flags);
3003 flags |= ZFS_ITER_RECURSE;
3006 cb.cb_scripted = B_TRUE;
3009 if (zfs_add_sort_column(&sortcol, optarg,
3011 (void) fprintf(stderr,
3012 gettext("invalid property '%s'\n"), optarg);
3017 if (zfs_add_sort_column(&sortcol, optarg,
3019 (void) fprintf(stderr,
3020 gettext("invalid property '%s'\n"), optarg);
3026 types_specified = B_TRUE;
3027 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3028 while (*optarg != '\0') {
3029 static char *type_subopts[] = { "filesystem",
3030 "volume", "snapshot", "snap", "all", NULL };
3032 switch (getsubopt(&optarg, type_subopts,
3035 types |= ZFS_TYPE_FILESYSTEM;
3038 types |= ZFS_TYPE_VOLUME;
3042 types |= ZFS_TYPE_SNAPSHOT;
3045 types = ZFS_TYPE_DATASET;
3049 (void) fprintf(stderr,
3050 gettext("invalid type '%s'\n"),
3057 (void) fprintf(stderr, gettext("missing argument for "
3058 "'%c' option\n"), optopt);
3062 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3072 fields = default_fields;
3075 * If we are only going to list snapshot names and sort by name,
3076 * then we can use faster version.
3078 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3079 flags |= ZFS_ITER_SIMPLE;
3082 * If "-o space" and no types were specified, don't display snapshots.
3084 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3085 types &= ~ZFS_TYPE_SNAPSHOT;
3088 * If the user specifies '-o all', the zprop_get_list() doesn't
3089 * normally include the name of the dataset. For 'zfs list', we always
3090 * want this property to be first.
3092 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3096 cb.cb_first = B_TRUE;
3098 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3099 limit, list_callback, &cb);
3101 zprop_free_list(cb.cb_proplist);
3102 zfs_free_sort_columns(sortcol);
3104 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3105 (void) fprintf(stderr, gettext("no datasets available\n"));
3111 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3112 * zfs rename [-f] -p <fs | vol> <fs | vol>
3113 * zfs rename -r <snap> <snap>
3115 * Renames the given dataset to another of the same type.
3117 * The '-p' flag creates all the non-existing ancestors of the target first.
3121 zfs_do_rename(int argc, char **argv)
3126 boolean_t recurse = B_FALSE;
3127 boolean_t parents = B_FALSE;
3128 boolean_t force_unmount = B_FALSE;
3131 while ((c = getopt(argc, argv, "prf")) != -1) {
3140 force_unmount = B_TRUE;
3144 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3153 /* check number of arguments */
3155 (void) fprintf(stderr, gettext("missing source dataset "
3160 (void) fprintf(stderr, gettext("missing target dataset "
3165 (void) fprintf(stderr, gettext("too many arguments\n"));
3169 if (recurse && parents) {
3170 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3175 if (recurse && strchr(argv[0], '@') == 0) {
3176 (void) fprintf(stderr, gettext("source dataset for recursive "
3177 "rename must be a snapshot\n"));
3181 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3182 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3185 /* If we were asked and the name looks good, try to create ancestors. */
3186 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3187 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3192 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3201 * Promotes the given clone fs to be the parent
3205 zfs_do_promote(int argc, char **argv)
3211 if (argc > 1 && argv[1][0] == '-') {
3212 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3217 /* check number of arguments */
3219 (void) fprintf(stderr, gettext("missing clone filesystem"
3224 (void) fprintf(stderr, gettext("too many arguments\n"));
3228 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3232 ret = (zfs_promote(zhp) != 0);
3240 * zfs rollback [-rRf] <snapshot>
3242 * -r Delete any intervening snapshots before doing rollback
3243 * -R Delete any snapshots and their clones
3244 * -f ignored for backwards compatability
3246 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3247 * since then and making it the active dataset. If more recent snapshots exist,
3248 * the command will complain unless the '-r' flag is given.
3250 typedef struct rollback_cbdata {
3256 boolean_t cb_recurse;
3257 boolean_t cb_dependent;
3258 } rollback_cbdata_t;
3261 * Report any snapshots more recent than the one specified. Used when '-r' is
3262 * not specified. We reuse this same callback for the snapshot dependents - if
3263 * 'cb_dependent' is set, then this is a dependent and we should report it
3264 * without checking the transaction group.
3267 rollback_check(zfs_handle_t *zhp, void *data)
3269 rollback_cbdata_t *cbp = data;
3271 if (cbp->cb_doclones) {
3276 if (!cbp->cb_dependent) {
3277 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
3278 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3279 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3282 if (cbp->cb_first && !cbp->cb_recurse) {
3283 (void) fprintf(stderr, gettext("cannot "
3284 "rollback to '%s': more recent snapshots "
3287 (void) fprintf(stderr, gettext("use '-r' to "
3288 "force deletion of the following "
3294 if (cbp->cb_recurse) {
3295 cbp->cb_dependent = B_TRUE;
3296 if (zfs_iter_dependents(zhp, B_TRUE,
3297 rollback_check, cbp) != 0) {
3301 cbp->cb_dependent = B_FALSE;
3303 (void) fprintf(stderr, "%s\n",
3308 if (cbp->cb_first && cbp->cb_recurse) {
3309 (void) fprintf(stderr, gettext("cannot rollback to "
3310 "'%s': clones of previous snapshots exist\n"),
3312 (void) fprintf(stderr, gettext("use '-R' to "
3313 "force deletion of the following clones and "
3319 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3327 zfs_do_rollback(int argc, char **argv)
3331 boolean_t force = B_FALSE;
3332 rollback_cbdata_t cb = { 0 };
3333 zfs_handle_t *zhp, *snap;
3334 char parentname[ZFS_MAXNAMELEN];
3338 while ((c = getopt(argc, argv, "rRf")) != -1) {
3351 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3360 /* check number of arguments */
3362 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3366 (void) fprintf(stderr, gettext("too many arguments\n"));
3370 /* open the snapshot */
3371 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3374 /* open the parent dataset */
3375 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3376 verify((delim = strrchr(parentname, '@')) != NULL);
3378 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3384 * Check for more recent snapshots and/or clones based on the presence
3387 cb.cb_target = argv[0];
3388 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3389 cb.cb_first = B_TRUE;
3391 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
3394 if ((ret = cb.cb_error) != 0)
3398 * Rollback parent to the given snapshot.
3400 ret = zfs_rollback(zhp, snap, force);
3413 * zfs set property=value { fs | snap | vol } ...
3415 * Sets the given property for all datasets specified on the command line.
3417 typedef struct set_cbdata {
3423 set_callback(zfs_handle_t *zhp, void *data)
3425 set_cbdata_t *cbp = data;
3427 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3428 switch (libzfs_errno(g_zfs)) {
3429 case EZFS_MOUNTFAILED:
3430 (void) fprintf(stderr, gettext("property may be set "
3431 "but unable to remount filesystem\n"));
3433 case EZFS_SHARENFSFAILED:
3434 (void) fprintf(stderr, gettext("property may be set "
3435 "but unable to reshare filesystem\n"));
3444 zfs_do_set(int argc, char **argv)
3449 /* check for options */
3450 if (argc > 1 && argv[1][0] == '-') {
3451 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3456 /* check number of arguments */
3458 (void) fprintf(stderr, gettext("missing property=value "
3463 (void) fprintf(stderr, gettext("missing dataset name\n"));
3467 /* validate property=value argument */
3468 cb.cb_propname = argv[1];
3469 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3470 (cb.cb_value[1] == '\0')) {
3471 (void) fprintf(stderr, gettext("missing value in "
3472 "property=value argument\n"));
3476 *cb.cb_value = '\0';
3479 if (*cb.cb_propname == '\0') {
3480 (void) fprintf(stderr,
3481 gettext("missing property in property=value argument\n"));
3485 ret = zfs_for_each(argc - 2, argv + 2, 0,
3486 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3491 typedef struct snap_cbdata {
3493 boolean_t sd_recursive;
3494 const char *sd_snapname;
3498 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3500 snap_cbdata_t *sd = arg;
3505 if (sd->sd_recursive &&
3506 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3511 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3514 fnvlist_add_boolean(sd->sd_nvl, name);
3517 if (sd->sd_recursive)
3518 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3524 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3526 * Creates a snapshot with the given name. While functionally equivalent to
3527 * 'zfs create', it is a separate command to differentiate intent.
3530 zfs_do_snapshot(int argc, char **argv)
3535 snap_cbdata_t sd = { 0 };
3536 boolean_t multiple_snaps = B_FALSE;
3538 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3540 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3544 while ((c = getopt(argc, argv, "ro:")) != -1) {
3547 if (parseprop(props))
3551 sd.sd_recursive = B_TRUE;
3552 multiple_snaps = B_TRUE;
3555 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3564 /* check number of arguments */
3566 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3571 multiple_snaps = B_TRUE;
3572 for (; argc > 0; argc--, argv++) {
3576 atp = strchr(argv[0], '@');
3580 sd.sd_snapname = atp + 1;
3581 zhp = zfs_open(g_zfs, argv[0],
3582 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3585 if (zfs_snapshot_cb(zhp, &sd) != 0)
3589 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3590 nvlist_free(sd.sd_nvl);
3592 if (ret != 0 && multiple_snaps)
3593 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3597 nvlist_free(sd.sd_nvl);
3604 * Send a backup stream to stdout.
3607 zfs_do_send(int argc, char **argv)
3609 char *fromname = NULL;
3610 char *toname = NULL;
3613 sendflags_t flags = { 0 };
3615 nvlist_t *dbgnv = NULL;
3616 boolean_t extraverbose = B_FALSE;
3619 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3630 flags.doall = B_TRUE;
3633 flags.replicate = B_TRUE;
3636 flags.props = B_TRUE;
3639 flags.parsable = B_TRUE;
3640 flags.verbose = B_TRUE;
3644 extraverbose = B_TRUE;
3645 flags.verbose = B_TRUE;
3646 flags.progress = B_TRUE;
3649 flags.dedup = B_TRUE;
3652 flags.dryrun = B_TRUE;
3655 (void) fprintf(stderr, gettext("missing argument for "
3656 "'%c' option\n"), optopt);
3660 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3669 /* check number of arguments */
3671 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3675 (void) fprintf(stderr, gettext("too many arguments\n"));
3679 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3680 (void) fprintf(stderr,
3681 gettext("Error: Stream can not be written to a terminal.\n"
3682 "You must redirect standard output.\n"));
3686 cp = strchr(argv[0], '@');
3688 (void) fprintf(stderr,
3689 gettext("argument must be a snapshot\n"));
3694 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3699 * If they specified the full path to the snapshot, chop off
3700 * everything except the short name of the snapshot, but special
3701 * case if they specify the origin.
3703 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3704 char origin[ZFS_MAXNAMELEN];
3707 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3708 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3710 if (strcmp(origin, fromname) == 0) {
3712 flags.fromorigin = B_TRUE;
3715 if (cp != fromname && strcmp(argv[0], fromname)) {
3716 (void) fprintf(stderr,
3717 gettext("incremental source must be "
3718 "in same filesystem\n"));
3722 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3723 (void) fprintf(stderr,
3724 gettext("invalid incremental source\n"));
3730 if (flags.replicate && fromname == NULL)
3731 flags.doall = B_TRUE;
3733 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3734 extraverbose ? &dbgnv : NULL);
3736 if (extraverbose && dbgnv != NULL) {
3738 * dump_nvlist prints to stdout, but that's been
3739 * redirected to a file. Make it print to stderr
3742 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3743 dump_nvlist(dbgnv, 0);
3752 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3754 * Restore a backup stream from stdin.
3757 zfs_do_receive(int argc, char **argv)
3760 recvflags_t flags = { 0 };
3763 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3766 flags.isprefix = B_TRUE;
3769 flags.isprefix = B_TRUE;
3770 flags.istail = B_TRUE;
3773 flags.dryrun = B_TRUE;
3776 flags.nomount = B_TRUE;
3779 flags.verbose = B_TRUE;
3782 flags.force = B_TRUE;
3785 (void) fprintf(stderr, gettext("missing argument for "
3786 "'%c' option\n"), optopt);
3790 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3799 /* check number of arguments */
3801 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3805 (void) fprintf(stderr, gettext("too many arguments\n"));
3809 if (isatty(STDIN_FILENO)) {
3810 (void) fprintf(stderr,
3811 gettext("Error: Backup stream can not be read "
3812 "from a terminal.\n"
3813 "You must redirect standard input.\n"));
3817 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3823 * allow/unallow stuff
3825 /* copied from zfs/sys/dsl_deleg.h */
3826 #define ZFS_DELEG_PERM_CREATE "create"
3827 #define ZFS_DELEG_PERM_DESTROY "destroy"
3828 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3829 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3830 #define ZFS_DELEG_PERM_CLONE "clone"
3831 #define ZFS_DELEG_PERM_PROMOTE "promote"
3832 #define ZFS_DELEG_PERM_RENAME "rename"
3833 #define ZFS_DELEG_PERM_MOUNT "mount"
3834 #define ZFS_DELEG_PERM_SHARE "share"
3835 #define ZFS_DELEG_PERM_SEND "send"
3836 #define ZFS_DELEG_PERM_RECEIVE "receive"
3837 #define ZFS_DELEG_PERM_ALLOW "allow"
3838 #define ZFS_DELEG_PERM_USERPROP "userprop"
3839 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3840 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3841 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3842 #define ZFS_DELEG_PERM_USERUSED "userused"
3843 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3844 #define ZFS_DELEG_PERM_HOLD "hold"
3845 #define ZFS_DELEG_PERM_RELEASE "release"
3846 #define ZFS_DELEG_PERM_DIFF "diff"
3848 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3850 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3851 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3852 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3853 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3854 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3855 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3856 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3857 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3858 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3859 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3860 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3861 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3862 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3863 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3864 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3865 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3867 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3868 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3869 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3870 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3871 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3872 { NULL, ZFS_DELEG_NOTE_NONE }
3875 /* permission structure */
3876 typedef struct deleg_perm {
3877 zfs_deleg_who_type_t dp_who_type;
3878 const char *dp_name;
3880 boolean_t dp_descend;
3884 typedef struct deleg_perm_node {
3885 deleg_perm_t dpn_perm;
3887 uu_avl_node_t dpn_avl_node;
3888 } deleg_perm_node_t;
3890 typedef struct fs_perm fs_perm_t;
3892 /* permissions set */
3893 typedef struct who_perm {
3894 zfs_deleg_who_type_t who_type;
3895 const char *who_name; /* id */
3896 char who_ug_name[256]; /* user/group name */
3897 fs_perm_t *who_fsperm; /* uplink */
3899 uu_avl_t *who_deleg_perm_avl; /* permissions */
3903 typedef struct who_perm_node {
3904 who_perm_t who_perm;
3905 uu_avl_node_t who_avl_node;
3908 typedef struct fs_perm_set fs_perm_set_t;
3909 /* fs permissions */
3911 const char *fsp_name;
3913 uu_avl_t *fsp_sc_avl; /* sets,create */
3914 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3916 fs_perm_set_t *fsp_set; /* uplink */
3920 typedef struct fs_perm_node {
3921 fs_perm_t fspn_fsperm;
3924 uu_list_node_t fspn_list_node;
3927 /* top level structure */
3928 struct fs_perm_set {
3929 uu_list_pool_t *fsps_list_pool;
3930 uu_list_t *fsps_list; /* list of fs_perms */
3932 uu_avl_pool_t *fsps_named_set_avl_pool;
3933 uu_avl_pool_t *fsps_who_perm_avl_pool;
3934 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
3937 static inline const char *
3938 deleg_perm_type(zfs_deleg_note_t note)
3944 case ZFS_DELEG_NOTE_GROUPQUOTA:
3945 case ZFS_DELEG_NOTE_GROUPUSED:
3946 case ZFS_DELEG_NOTE_USERPROP:
3947 case ZFS_DELEG_NOTE_USERQUOTA:
3948 case ZFS_DELEG_NOTE_USERUSED:
3950 return (gettext("other"));
3952 return (gettext("subcommand"));
3957 who_type2weight(zfs_deleg_who_type_t who_type)
3961 case ZFS_DELEG_NAMED_SET_SETS:
3962 case ZFS_DELEG_NAMED_SET:
3965 case ZFS_DELEG_CREATE_SETS:
3966 case ZFS_DELEG_CREATE:
3969 case ZFS_DELEG_USER_SETS:
3970 case ZFS_DELEG_USER:
3973 case ZFS_DELEG_GROUP_SETS:
3974 case ZFS_DELEG_GROUP:
3977 case ZFS_DELEG_EVERYONE_SETS:
3978 case ZFS_DELEG_EVERYONE:
3990 who_perm_compare(const void *larg, const void *rarg, void *unused)
3992 const who_perm_node_t *l = larg;
3993 const who_perm_node_t *r = rarg;
3994 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
3995 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
3996 int lweight = who_type2weight(ltype);
3997 int rweight = who_type2weight(rtype);
3998 int res = lweight - rweight;
4000 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4001 ZFS_MAX_DELEG_NAME-1);
4013 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4015 const deleg_perm_node_t *l = larg;
4016 const deleg_perm_node_t *r = rarg;
4017 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4018 ZFS_MAX_DELEG_NAME-1);
4030 fs_perm_set_init(fs_perm_set_t *fspset)
4032 bzero(fspset, sizeof (fs_perm_set_t));
4034 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4035 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4036 NULL, UU_DEFAULT)) == NULL)
4038 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4039 UU_DEFAULT)) == NULL)
4042 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4043 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4044 who_perm_node_t, who_avl_node), who_perm_compare,
4045 UU_DEFAULT)) == NULL)
4048 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4049 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4050 who_perm_node_t, who_avl_node), who_perm_compare,
4051 UU_DEFAULT)) == NULL)
4054 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4055 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4056 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4061 static inline void fs_perm_fini(fs_perm_t *);
4062 static inline void who_perm_fini(who_perm_t *);
4065 fs_perm_set_fini(fs_perm_set_t *fspset)
4067 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4069 while (node != NULL) {
4070 fs_perm_node_t *next_node =
4071 uu_list_next(fspset->fsps_list, node);
4072 fs_perm_t *fsperm = &node->fspn_fsperm;
4073 fs_perm_fini(fsperm);
4074 uu_list_remove(fspset->fsps_list, node);
4079 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4080 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4081 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4085 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4088 deleg_perm->dp_who_type = type;
4089 deleg_perm->dp_name = name;
4093 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4094 zfs_deleg_who_type_t type, const char *name)
4096 uu_avl_pool_t *pool;
4097 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4099 bzero(who_perm, sizeof (who_perm_t));
4101 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4102 UU_DEFAULT)) == NULL)
4105 who_perm->who_type = type;
4106 who_perm->who_name = name;
4107 who_perm->who_fsperm = fsperm;
4111 who_perm_fini(who_perm_t *who_perm)
4113 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4115 while (node != NULL) {
4116 deleg_perm_node_t *next_node =
4117 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4119 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4124 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4128 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4130 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4131 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4133 bzero(fsperm, sizeof (fs_perm_t));
4135 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4139 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4143 fsperm->fsp_set = fspset;
4144 fsperm->fsp_name = fsname;
4148 fs_perm_fini(fs_perm_t *fsperm)
4150 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4151 while (node != NULL) {
4152 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4154 who_perm_t *who_perm = &node->who_perm;
4155 who_perm_fini(who_perm);
4156 uu_avl_remove(fsperm->fsp_sc_avl, node);
4161 node = uu_avl_first(fsperm->fsp_uge_avl);
4162 while (node != NULL) {
4163 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4165 who_perm_t *who_perm = &node->who_perm;
4166 who_perm_fini(who_perm);
4167 uu_avl_remove(fsperm->fsp_uge_avl, node);
4172 uu_avl_destroy(fsperm->fsp_sc_avl);
4173 uu_avl_destroy(fsperm->fsp_uge_avl);
4177 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4178 zfs_deleg_who_type_t who_type, const char *name, char locality)
4180 uu_avl_index_t idx = 0;
4182 deleg_perm_node_t *found_node = NULL;
4183 deleg_perm_t *deleg_perm = &node->dpn_perm;
4185 deleg_perm_init(deleg_perm, who_type, name);
4187 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4189 uu_avl_insert(avl, node, idx);
4192 deleg_perm = &node->dpn_perm;
4197 case ZFS_DELEG_LOCAL:
4198 deleg_perm->dp_local = B_TRUE;
4200 case ZFS_DELEG_DESCENDENT:
4201 deleg_perm->dp_descend = B_TRUE;
4206 assert(B_FALSE); /* invalid locality */
4211 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4213 nvpair_t *nvp = NULL;
4214 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4215 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4216 zfs_deleg_who_type_t who_type = who_perm->who_type;
4218 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4219 const char *name = nvpair_name(nvp);
4220 data_type_t type = nvpair_type(nvp);
4221 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4222 deleg_perm_node_t *node =
4223 safe_malloc(sizeof (deleg_perm_node_t));
4225 VERIFY(type == DATA_TYPE_BOOLEAN);
4227 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4228 set_deleg_perm_node(avl, node, who_type, name, locality);
4235 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4237 nvpair_t *nvp = NULL;
4238 fs_perm_set_t *fspset = fsperm->fsp_set;
4240 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4241 nvlist_t *nvl2 = NULL;
4242 const char *name = nvpair_name(nvp);
4243 uu_avl_t *avl = NULL;
4244 uu_avl_pool_t *avl_pool = NULL;
4245 zfs_deleg_who_type_t perm_type = name[0];
4246 char perm_locality = name[1];
4247 const char *perm_name = name + 3;
4248 boolean_t is_set = B_TRUE;
4249 who_perm_t *who_perm = NULL;
4251 assert('$' == name[2]);
4253 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4256 switch (perm_type) {
4257 case ZFS_DELEG_CREATE:
4258 case ZFS_DELEG_CREATE_SETS:
4259 case ZFS_DELEG_NAMED_SET:
4260 case ZFS_DELEG_NAMED_SET_SETS:
4261 avl_pool = fspset->fsps_named_set_avl_pool;
4262 avl = fsperm->fsp_sc_avl;
4264 case ZFS_DELEG_USER:
4265 case ZFS_DELEG_USER_SETS:
4266 case ZFS_DELEG_GROUP:
4267 case ZFS_DELEG_GROUP_SETS:
4268 case ZFS_DELEG_EVERYONE:
4269 case ZFS_DELEG_EVERYONE_SETS:
4270 avl_pool = fspset->fsps_who_perm_avl_pool;
4271 avl = fsperm->fsp_uge_avl;
4278 who_perm_node_t *found_node = NULL;
4279 who_perm_node_t *node = safe_malloc(
4280 sizeof (who_perm_node_t));
4281 who_perm = &node->who_perm;
4282 uu_avl_index_t idx = 0;
4284 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4285 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4287 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4289 if (avl == fsperm->fsp_uge_avl) {
4291 struct passwd *p = NULL;
4292 struct group *g = NULL;
4293 const char *nice_name = NULL;
4295 switch (perm_type) {
4296 case ZFS_DELEG_USER_SETS:
4297 case ZFS_DELEG_USER:
4298 rid = atoi(perm_name);
4301 nice_name = p->pw_name;
4303 case ZFS_DELEG_GROUP_SETS:
4304 case ZFS_DELEG_GROUP:
4305 rid = atoi(perm_name);
4308 nice_name = g->gr_name;
4314 if (nice_name != NULL)
4316 node->who_perm.who_ug_name,
4320 uu_avl_insert(avl, node, idx);
4323 who_perm = &node->who_perm;
4327 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4334 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4336 nvpair_t *nvp = NULL;
4337 uu_avl_index_t idx = 0;
4339 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4340 nvlist_t *nvl2 = NULL;
4341 const char *fsname = nvpair_name(nvp);
4342 data_type_t type = nvpair_type(nvp);
4343 fs_perm_t *fsperm = NULL;
4344 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4348 fsperm = &node->fspn_fsperm;
4350 VERIFY(DATA_TYPE_NVLIST == type);
4352 uu_list_node_init(node, &node->fspn_list_node,
4353 fspset->fsps_list_pool);
4355 idx = uu_list_numnodes(fspset->fsps_list);
4356 fs_perm_init(fsperm, fspset, fsname);
4358 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4361 (void) parse_fs_perm(fsperm, nvl2);
4363 uu_list_insert(fspset->fsps_list, node, idx);
4369 static inline const char *
4370 deleg_perm_comment(zfs_deleg_note_t note)
4372 const char *str = "";
4377 case ZFS_DELEG_NOTE_ALLOW:
4378 str = gettext("Must also have the permission that is being"
4379 "\n\t\t\t\tallowed");
4381 case ZFS_DELEG_NOTE_CLONE:
4382 str = gettext("Must also have the 'create' ability and 'mount'"
4383 "\n\t\t\t\tability in the origin file system");
4385 case ZFS_DELEG_NOTE_CREATE:
4386 str = gettext("Must also have the 'mount' ability");
4388 case ZFS_DELEG_NOTE_DESTROY:
4389 str = gettext("Must also have the 'mount' ability");
4391 case ZFS_DELEG_NOTE_DIFF:
4392 str = gettext("Allows lookup of paths within a dataset;"
4393 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4394 "\n\t\t\t\tin order to use zfs diff");
4396 case ZFS_DELEG_NOTE_HOLD:
4397 str = gettext("Allows adding a user hold to a snapshot");
4399 case ZFS_DELEG_NOTE_MOUNT:
4400 str = gettext("Allows mount/umount of ZFS datasets");
4402 case ZFS_DELEG_NOTE_PROMOTE:
4403 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4404 " 'promote' ability in the origin file system");
4406 case ZFS_DELEG_NOTE_RECEIVE:
4407 str = gettext("Must also have the 'mount' and 'create'"
4410 case ZFS_DELEG_NOTE_RELEASE:
4411 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4412 "might destroy the snapshot");
4414 case ZFS_DELEG_NOTE_RENAME:
4415 str = gettext("Must also have the 'mount' and 'create'"
4416 "\n\t\t\t\tability in the new parent");
4418 case ZFS_DELEG_NOTE_ROLLBACK:
4421 case ZFS_DELEG_NOTE_SEND:
4424 case ZFS_DELEG_NOTE_SHARE:
4425 str = gettext("Allows sharing file systems over NFS or SMB"
4426 "\n\t\t\t\tprotocols");
4428 case ZFS_DELEG_NOTE_SNAPSHOT:
4432 * case ZFS_DELEG_NOTE_VSCAN:
4433 * str = gettext("");
4437 case ZFS_DELEG_NOTE_GROUPQUOTA:
4438 str = gettext("Allows accessing any groupquota@... property");
4440 case ZFS_DELEG_NOTE_GROUPUSED:
4441 str = gettext("Allows reading any groupused@... property");
4443 case ZFS_DELEG_NOTE_USERPROP:
4444 str = gettext("Allows changing any user property");
4446 case ZFS_DELEG_NOTE_USERQUOTA:
4447 str = gettext("Allows accessing any userquota@... property");
4449 case ZFS_DELEG_NOTE_USERUSED:
4450 str = gettext("Allows reading any userused@... property");
4468 boolean_t recursive; /* unallow only */
4469 boolean_t prt_usage;
4471 boolean_t prt_perms;
4474 const char *dataset;
4478 prop_cmp(const void *a, const void *b)
4480 const char *str1 = *(const char **)a;
4481 const char *str2 = *(const char **)b;
4482 return (strcmp(str1, str2));
4486 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4488 const char *opt_desc[] = {
4489 "-h", gettext("show this help message and exit"),
4490 "-l", gettext("set permission locally"),
4491 "-d", gettext("set permission for descents"),
4492 "-u", gettext("set permission for user"),
4493 "-g", gettext("set permission for group"),
4494 "-e", gettext("set permission for everyone"),
4495 "-c", gettext("set create time permission"),
4496 "-s", gettext("define permission set"),
4498 "-r", gettext("remove permissions recursively"),
4500 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4501 size_t allow_size = unallow_size - 2;
4502 const char *props[ZFS_NUM_PROPS];
4505 FILE *fp = requested ? stdout : stderr;
4506 zprop_desc_t *pdtbl = zfs_prop_get_table();
4507 const char *fmt = gettext("%-16s %-14s\t%s\n");
4509 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4511 (void) fprintf(fp, gettext("Options:\n"));
4512 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4513 const char *opt = opt_desc[i++];
4514 const char *optdsc = opt_desc[i];
4515 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4518 (void) fprintf(fp, gettext("\nThe following permissions are "
4520 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4522 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4523 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4524 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4525 const char *perm_type = deleg_perm_type(perm_note);
4526 const char *perm_comment = deleg_perm_comment(perm_note);
4527 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4530 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4531 zprop_desc_t *pd = &pdtbl[i];
4532 if (pd->pd_visible != B_TRUE)
4535 if (pd->pd_attr == PROP_READONLY)
4538 props[count++] = pd->pd_name;
4540 props[count] = NULL;
4542 qsort(props, count, sizeof (char *), prop_cmp);
4544 for (i = 0; i < count; i++)
4545 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4548 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4550 exit(requested ? 0 : 2);
4553 static inline const char *
4554 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4557 if (un && argc == expected_argc - 1)
4559 else if (argc == expected_argc)
4560 *permsp = argv[argc - 2];
4562 allow_usage(un, B_FALSE,
4563 gettext("wrong number of parameters\n"));
4565 return (argv[argc - 1]);
4569 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4571 int uge_sum = opts->user + opts->group + opts->everyone;
4572 int csuge_sum = opts->create + opts->set + uge_sum;
4573 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4574 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4577 allow_usage(un, B_FALSE,
4578 gettext("-u, -g, and -e are mutually exclusive\n"));
4580 if (opts->prt_usage) {
4581 if (argc == 0 && all_sum == 0)
4582 allow_usage(un, B_TRUE, NULL);
4589 allow_usage(un, B_FALSE,
4590 gettext("invalid options combined with -s\n"));
4592 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4593 if (argv[0][0] != '@')
4594 allow_usage(un, B_FALSE,
4595 gettext("invalid set name: missing '@' prefix\n"));
4596 opts->who = argv[0];
4597 } else if (opts->create) {
4598 if (ldcsuge_sum > 1)
4599 allow_usage(un, B_FALSE,
4600 gettext("invalid options combined with -c\n"));
4601 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4602 } else if (opts->everyone) {
4604 allow_usage(un, B_FALSE,
4605 gettext("invalid options combined with -e\n"));
4606 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4607 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4609 opts->everyone = B_TRUE;
4612 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4613 } else if (argc == 1 && !un) {
4614 opts->prt_perms = B_TRUE;
4615 opts->dataset = argv[argc-1];
4617 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4618 opts->who = argv[0];
4621 if (!opts->local && !opts->descend) {
4622 opts->local = B_TRUE;
4623 opts->descend = B_TRUE;
4628 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4629 const char *who, char *perms, nvlist_t *top_nvl)
4632 char ld[2] = { '\0', '\0' };
4633 char who_buf[ZFS_MAXNAMELEN+32];
4634 char base_type = ZFS_DELEG_WHO_UNKNOWN;
4635 char set_type = ZFS_DELEG_WHO_UNKNOWN;
4636 nvlist_t *base_nvl = NULL;
4637 nvlist_t *set_nvl = NULL;
4640 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4642 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4646 case ZFS_DELEG_NAMED_SET_SETS:
4647 case ZFS_DELEG_NAMED_SET:
4648 set_type = ZFS_DELEG_NAMED_SET_SETS;
4649 base_type = ZFS_DELEG_NAMED_SET;
4650 ld[0] = ZFS_DELEG_NA;
4652 case ZFS_DELEG_CREATE_SETS:
4653 case ZFS_DELEG_CREATE:
4654 set_type = ZFS_DELEG_CREATE_SETS;
4655 base_type = ZFS_DELEG_CREATE;
4656 ld[0] = ZFS_DELEG_NA;
4658 case ZFS_DELEG_USER_SETS:
4659 case ZFS_DELEG_USER:
4660 set_type = ZFS_DELEG_USER_SETS;
4661 base_type = ZFS_DELEG_USER;
4663 ld[0] = ZFS_DELEG_LOCAL;
4665 ld[1] = ZFS_DELEG_DESCENDENT;
4667 case ZFS_DELEG_GROUP_SETS:
4668 case ZFS_DELEG_GROUP:
4669 set_type = ZFS_DELEG_GROUP_SETS;
4670 base_type = ZFS_DELEG_GROUP;
4672 ld[0] = ZFS_DELEG_LOCAL;
4674 ld[1] = ZFS_DELEG_DESCENDENT;
4676 case ZFS_DELEG_EVERYONE_SETS:
4677 case ZFS_DELEG_EVERYONE:
4678 set_type = ZFS_DELEG_EVERYONE_SETS;
4679 base_type = ZFS_DELEG_EVERYONE;
4681 ld[0] = ZFS_DELEG_LOCAL;
4683 ld[1] = ZFS_DELEG_DESCENDENT;
4688 if (perms != NULL) {
4690 char *end = curr + strlen(perms);
4692 while (curr < end) {
4693 char *delim = strchr(curr, ',');
4704 (void) nvlist_add_boolean(nvl, curr);
4710 for (i = 0; i < 2; i++) {
4711 char locality = ld[i];
4715 if (!nvlist_empty(base_nvl)) {
4717 (void) snprintf(who_buf,
4718 sizeof (who_buf), "%c%c$%s",
4719 base_type, locality, who);
4721 (void) snprintf(who_buf,
4722 sizeof (who_buf), "%c%c$",
4723 base_type, locality);
4725 (void) nvlist_add_nvlist(top_nvl, who_buf,
4730 if (!nvlist_empty(set_nvl)) {
4732 (void) snprintf(who_buf,
4733 sizeof (who_buf), "%c%c$%s",
4734 set_type, locality, who);
4736 (void) snprintf(who_buf,
4737 sizeof (who_buf), "%c%c$",
4738 set_type, locality);
4740 (void) nvlist_add_nvlist(top_nvl, who_buf,
4745 for (i = 0; i < 2; i++) {
4746 char locality = ld[i];
4751 (void) snprintf(who_buf, sizeof (who_buf),
4752 "%c%c$%s", base_type, locality, who);
4754 (void) snprintf(who_buf, sizeof (who_buf),
4755 "%c%c$", base_type, locality);
4756 (void) nvlist_add_boolean(top_nvl, who_buf);
4759 (void) snprintf(who_buf, sizeof (who_buf),
4760 "%c%c$%s", set_type, locality, who);
4762 (void) snprintf(who_buf, sizeof (who_buf),
4763 "%c%c$", set_type, locality);
4764 (void) nvlist_add_boolean(top_nvl, who_buf);
4770 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4772 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4776 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4777 opts->descend, opts->who, opts->perms, *nvlp);
4778 } else if (opts->create) {
4779 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4780 opts->descend, NULL, opts->perms, *nvlp);
4781 } else if (opts->everyone) {
4782 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4783 opts->descend, NULL, opts->perms, *nvlp);
4785 char *curr = opts->who;
4786 char *end = curr + strlen(curr);
4788 while (curr < end) {
4790 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
4792 char *delim = strchr(curr, ',');
4795 struct passwd *p = NULL;
4796 struct group *g = NULL;
4804 rid = (uid_t)strtol(curr, &endch, 0);
4806 who_type = ZFS_DELEG_USER;
4815 (void) snprintf(errbuf, 256, gettext(
4816 "invalid user %s"), curr);
4817 allow_usage(un, B_TRUE, errbuf);
4819 } else if (opts->group) {
4820 who_type = ZFS_DELEG_GROUP;
4829 (void) snprintf(errbuf, 256, gettext(
4830 "invalid group %s"), curr);
4831 allow_usage(un, B_TRUE, errbuf);
4834 if (*endch != '\0') {
4841 if (*endch != '\0') {
4849 who_type = ZFS_DELEG_USER;
4851 } else if (g != NULL) {
4852 who_type = ZFS_DELEG_GROUP;
4855 (void) snprintf(errbuf, 256, gettext(
4856 "invalid user/group %s"), curr);
4857 allow_usage(un, B_TRUE, errbuf);
4861 (void) sprintf(id, "%u", rid);
4864 store_allow_perm(who_type, opts->local,
4865 opts->descend, who, opts->perms, *nvlp);
4874 print_set_creat_perms(uu_avl_t *who_avl)
4876 const char *sc_title[] = {
4877 gettext("Permission sets:\n"),
4878 gettext("Create time permissions:\n"),
4881 const char **title_ptr = sc_title;
4882 who_perm_node_t *who_node = NULL;
4883 int prev_weight = -1;
4885 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4886 who_node = uu_avl_next(who_avl, who_node)) {
4887 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4888 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4889 const char *who_name = who_node->who_perm.who_name;
4890 int weight = who_type2weight(who_type);
4891 boolean_t first = B_TRUE;
4892 deleg_perm_node_t *deleg_node;
4894 if (prev_weight != weight) {
4895 (void) printf("%s", *title_ptr++);
4896 prev_weight = weight;
4899 if (who_name == NULL || strnlen(who_name, 1) == 0)
4900 (void) printf("\t");
4902 (void) printf("\t%s ", who_name);
4904 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4905 deleg_node = uu_avl_next(avl, deleg_node)) {
4908 deleg_node->dpn_perm.dp_name);
4911 (void) printf(",%s",
4912 deleg_node->dpn_perm.dp_name);
4915 (void) printf("\n");
4920 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4923 who_perm_node_t *who_node = NULL;
4924 boolean_t prt_title = B_TRUE;
4925 uu_avl_walk_t *walk;
4927 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4930 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4931 const char *who_name = who_node->who_perm.who_name;
4932 const char *nice_who_name = who_node->who_perm.who_ug_name;
4933 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4934 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4936 deleg_perm_node_t *deleg_node;
4937 boolean_t prt_who = B_TRUE;
4939 for (deleg_node = uu_avl_first(avl);
4941 deleg_node = uu_avl_next(avl, deleg_node)) {
4942 if (local != deleg_node->dpn_perm.dp_local ||
4943 descend != deleg_node->dpn_perm.dp_descend)
4947 const char *who = NULL;
4949 prt_title = B_FALSE;
4950 (void) printf("%s", title);
4954 case ZFS_DELEG_USER_SETS:
4955 case ZFS_DELEG_USER:
4956 who = gettext("user");
4958 who_name = nice_who_name;
4960 case ZFS_DELEG_GROUP_SETS:
4961 case ZFS_DELEG_GROUP:
4962 who = gettext("group");
4964 who_name = nice_who_name;
4966 case ZFS_DELEG_EVERYONE_SETS:
4967 case ZFS_DELEG_EVERYONE:
4968 who = gettext("everyone");
4975 if (who_name == NULL)
4976 (void) printf("\t%s", who);
4978 (void) printf("\t%s %s", who, who_name);
4981 (void) printf("%c%s", delim,
4982 deleg_node->dpn_perm.dp_name);
4987 (void) printf("\n");
4990 uu_avl_walk_end(walk);
4994 print_fs_perms(fs_perm_set_t *fspset)
4996 fs_perm_node_t *node = NULL;
4997 char buf[ZFS_MAXNAMELEN+32];
4998 const char *dsname = buf;
5000 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5001 node = uu_list_next(fspset->fsps_list, node)) {
5002 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5003 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5006 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5007 gettext("---- Permissions on %s "),
5008 node->fspn_fsperm.fsp_name);
5009 (void) printf("%s", dsname);
5010 left = 70 - strlen(buf);
5013 (void) printf("\n");
5015 print_set_creat_perms(sc_avl);
5016 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5017 gettext("Local permissions:\n"));
5018 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5019 gettext("Descendent permissions:\n"));
5020 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5021 gettext("Local+Descendent permissions:\n"));
5025 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5027 struct deleg_perms {
5033 set_deleg_perms(zfs_handle_t *zhp, void *data)
5035 struct deleg_perms *perms = (struct deleg_perms *)data;
5036 zfs_type_t zfs_type = zfs_get_type(zhp);
5038 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5041 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5045 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5048 nvlist_t *perm_nvl = NULL;
5049 nvlist_t *update_perm_nvl = NULL;
5052 struct allow_opts opts = { 0 };
5054 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5057 while ((c = getopt(argc, argv, optstr)) != -1) {
5060 opts.local = B_TRUE;
5063 opts.descend = B_TRUE;
5069 opts.group = B_TRUE;
5072 opts.everyone = B_TRUE;
5078 opts.create = B_TRUE;
5081 opts.recursive = B_TRUE;
5084 (void) fprintf(stderr, gettext("missing argument for "
5085 "'%c' option\n"), optopt);
5089 opts.prt_usage = B_TRUE;
5092 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5101 /* check arguments */
5102 parse_allow_args(argc, argv, un, &opts);
5104 /* try to open the dataset */
5105 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5106 ZFS_TYPE_VOLUME)) == NULL) {
5107 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5112 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5115 fs_perm_set_init(&fs_perm_set);
5116 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5117 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5122 print_fs_perms(&fs_perm_set);
5124 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5125 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5128 if (un && opts.recursive) {
5129 struct deleg_perms data = { un, update_perm_nvl };
5130 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5139 nvlist_free(perm_nvl);
5140 if (update_perm_nvl != NULL)
5141 nvlist_free(update_perm_nvl);
5143 fs_perm_set_fini(&fs_perm_set);
5151 zfs_do_allow(int argc, char **argv)
5153 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5157 zfs_do_unallow(int argc, char **argv)
5159 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5163 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5168 boolean_t recursive = B_FALSE;
5169 const char *opts = holding ? "rt" : "r";
5173 while ((c = getopt(argc, argv, opts)) != -1) {
5179 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5188 /* check number of arguments */
5196 if (holding && tag[0] == '.') {
5197 /* tags starting with '.' are reserved for libzfs */
5198 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5202 for (i = 0; i < argc; ++i) {
5204 char parent[ZFS_MAXNAMELEN];
5206 char *path = argv[i];
5208 delim = strchr(path, '@');
5209 if (delim == NULL) {
5210 (void) fprintf(stderr,
5211 gettext("'%s' is not a snapshot\n"), path);
5215 (void) strncpy(parent, path, delim - path);
5216 parent[delim - path] = '\0';
5218 zhp = zfs_open(g_zfs, parent,
5219 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5225 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5228 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5234 return (errors != 0);
5238 * zfs hold [-r] [-t] <tag> <snap> ...
5240 * -r Recursively hold
5242 * Apply a user-hold with the given tag to the list of snapshots.
5245 zfs_do_hold(int argc, char **argv)
5247 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5251 * zfs release [-r] <tag> <snap> ...
5253 * -r Recursively release
5255 * Release a user-hold with the given tag from the list of snapshots.
5258 zfs_do_release(int argc, char **argv)
5260 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5263 typedef struct holds_cbdata {
5264 boolean_t cb_recursive;
5265 const char *cb_snapname;
5267 size_t cb_max_namelen;
5268 size_t cb_max_taglen;
5271 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5272 #define DATETIME_BUF_LEN (32)
5277 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
5280 nvpair_t *nvp = NULL;
5281 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5285 for (i = 0; i < 3; i++) {
5286 col = gettext(hdr_cols[i]);
5288 (void) printf("%-*s ", i ? tagwidth : nwidth,
5291 (void) printf("%s\n", col);
5295 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5296 char *zname = nvpair_name(nvp);
5298 nvpair_t *nvp2 = NULL;
5299 (void) nvpair_value_nvlist(nvp, &nvl2);
5300 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5301 char tsbuf[DATETIME_BUF_LEN];
5302 char *tagname = nvpair_name(nvp2);
5306 char sep = scripted ? '\t' : ' ';
5307 int sepnum = scripted ? 1 : 2;
5309 (void) nvpair_value_uint64(nvp2, &val);
5311 (void) localtime_r(&time, &t);
5312 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5313 gettext(STRFTIME_FMT_STR), &t);
5315 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5316 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5322 * Generic callback function to list a dataset or snapshot.
5325 holds_callback(zfs_handle_t *zhp, void *data)
5327 holds_cbdata_t *cbp = data;
5328 nvlist_t *top_nvl = *cbp->cb_nvlp;
5329 nvlist_t *nvl = NULL;
5330 nvpair_t *nvp = NULL;
5331 const char *zname = zfs_get_name(zhp);
5332 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5334 if (cbp->cb_recursive) {
5335 const char *snapname;
5336 char *delim = strchr(zname, '@');
5340 snapname = delim + 1;
5341 if (strcmp(cbp->cb_snapname, snapname))
5345 if (zfs_get_holds(zhp, &nvl) != 0)
5348 if (znamelen > cbp->cb_max_namelen)
5349 cbp->cb_max_namelen = znamelen;
5351 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5352 const char *tag = nvpair_name(nvp);
5353 size_t taglen = strnlen(tag, MAXNAMELEN);
5354 if (taglen > cbp->cb_max_taglen)
5355 cbp->cb_max_taglen = taglen;
5358 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5362 * zfs holds [-r] <snap> ...
5364 * -r Recursively hold
5367 zfs_do_holds(int argc, char **argv)
5372 boolean_t scripted = B_FALSE;
5373 boolean_t recursive = B_FALSE;
5374 const char *opts = "rH";
5377 int types = ZFS_TYPE_SNAPSHOT;
5378 holds_cbdata_t cb = { 0 };
5385 while ((c = getopt(argc, argv, opts)) != -1) {
5394 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5401 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5402 flags |= ZFS_ITER_RECURSE;
5408 /* check number of arguments */
5412 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5415 for (i = 0; i < argc; ++i) {
5416 char *snapshot = argv[i];
5418 const char *snapname;
5420 delim = strchr(snapshot, '@');
5421 if (delim == NULL) {
5422 (void) fprintf(stderr,
5423 gettext("'%s' is not a snapshot\n"), snapshot);
5427 snapname = delim + 1;
5429 snapshot[delim - snapshot] = '\0';
5431 cb.cb_recursive = recursive;
5432 cb.cb_snapname = snapname;
5436 * 1. collect holds data, set format options
5438 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5439 holds_callback, &cb);
5445 * 2. print holds data
5447 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5449 if (nvlist_empty(nvl))
5450 (void) fprintf(stderr, gettext("no datasets available\n"));
5454 return (0 != errors);
5457 #define CHECK_SPINNER 30
5458 #define SPINNER_TIME 3 /* seconds */
5459 #define MOUNT_TIME 5 /* seconds */
5462 get_one_dataset(zfs_handle_t *zhp, void *data)
5464 static char *spin[] = { "-", "\\", "|", "/" };
5465 static int spinval = 0;
5466 static int spincheck = 0;
5467 static time_t last_spin_time = (time_t)0;
5468 get_all_cb_t *cbp = data;
5469 zfs_type_t type = zfs_get_type(zhp);
5471 if (cbp->cb_verbose) {
5472 if (--spincheck < 0) {
5473 time_t now = time(NULL);
5474 if (last_spin_time + SPINNER_TIME < now) {
5475 update_progress(spin[spinval++ % 4]);
5476 last_spin_time = now;
5478 spincheck = CHECK_SPINNER;
5483 * Iterate over any nested datasets.
5485 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5491 * Skip any datasets whose type does not match.
5493 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5497 libzfs_add_handle(cbp, zhp);
5498 assert(cbp->cb_used <= cbp->cb_alloc);
5504 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5506 get_all_cb_t cb = { 0 };
5507 cb.cb_verbose = verbose;
5508 cb.cb_getone = get_one_dataset;
5511 set_progress_header(gettext("Reading ZFS config"));
5512 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5514 *dslist = cb.cb_handles;
5515 *count = cb.cb_used;
5518 finish_progress(gettext("done."));
5522 * Generic callback for sharing or mounting filesystems. Because the code is so
5523 * similar, we have a common function with an extra parameter to determine which
5524 * mode we are using.
5526 #define OP_SHARE 0x1
5527 #define OP_MOUNT 0x2
5530 * Share or mount a dataset.
5533 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5534 boolean_t explicit, const char *options)
5536 char mountpoint[ZFS_MAXPROPLEN];
5537 char shareopts[ZFS_MAXPROPLEN];
5538 char smbshareopts[ZFS_MAXPROPLEN];
5539 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5541 uint64_t zoned, canmount;
5542 boolean_t shared_nfs, shared_smb;
5544 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5547 * Check to make sure we can mount/share this dataset. If we
5548 * are in the global zone and the filesystem is exported to a
5549 * local zone, or if we are in a local zone and the
5550 * filesystem is not exported, then it is an error.
5552 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5554 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5558 (void) fprintf(stderr, gettext("cannot %s '%s': "
5559 "dataset is exported to a local zone\n"), cmdname,
5563 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5567 (void) fprintf(stderr, gettext("cannot %s '%s': "
5568 "permission denied\n"), cmdname,
5574 * Ignore any filesystems which don't apply to us. This
5575 * includes those with a legacy mountpoint, or those with
5576 * legacy share options.
5578 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5579 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5580 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5581 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5582 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5583 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5585 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5586 strcmp(smbshareopts, "off") == 0) {
5590 (void) fprintf(stderr, gettext("cannot share '%s': "
5591 "legacy share\n"), zfs_get_name(zhp));
5592 (void) fprintf(stderr, gettext("use share(1M) to "
5593 "share this filesystem, or set "
5594 "sharenfs property on\n"));
5599 * We cannot share or mount legacy filesystems. If the
5600 * shareopts is non-legacy but the mountpoint is legacy, we
5601 * treat it as a legacy share.
5603 if (strcmp(mountpoint, "legacy") == 0) {
5607 (void) fprintf(stderr, gettext("cannot %s '%s': "
5608 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5609 (void) fprintf(stderr, gettext("use %s(1M) to "
5610 "%s this filesystem\n"), cmdname, cmdname);
5614 if (strcmp(mountpoint, "none") == 0) {
5618 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5619 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5624 * canmount explicit outcome
5625 * on no pass through
5626 * on yes pass through
5628 * off yes display error, return 1
5629 * noauto no return 0
5630 * noauto yes pass through
5632 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5633 if (canmount == ZFS_CANMOUNT_OFF) {
5637 (void) fprintf(stderr, gettext("cannot %s '%s': "
5638 "'canmount' property is set to 'off'\n"), cmdname,
5641 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5646 * At this point, we have verified that the mountpoint and/or
5647 * shareopts are appropriate for auto management. If the
5648 * filesystem is already mounted or shared, return (failing
5649 * for explicit requests); otherwise mount or share the
5655 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5656 shared_smb = zfs_is_shared_smb(zhp, NULL);
5658 if ((shared_nfs && shared_smb) ||
5659 ((shared_nfs && strcmp(shareopts, "on") == 0) &&
5660 (strcmp(smbshareopts, "off") == 0)) ||
5661 ((shared_smb && strcmp(smbshareopts, "on") == 0) &&
5662 (strcmp(shareopts, "off") == 0))) {
5666 (void) fprintf(stderr, gettext("cannot share "
5667 "'%s': filesystem already shared\n"),
5672 if (!zfs_is_mounted(zhp, NULL) &&
5673 zfs_mount(zhp, NULL, 0) != 0)
5676 if (protocol == NULL) {
5677 if (zfs_shareall(zhp) != 0)
5679 } else if (strcmp(protocol, "nfs") == 0) {
5680 if (zfs_share_nfs(zhp))
5682 } else if (strcmp(protocol, "smb") == 0) {
5683 if (zfs_share_smb(zhp))
5686 (void) fprintf(stderr, gettext("cannot share "
5687 "'%s': invalid share type '%s' "
5689 zfs_get_name(zhp), protocol);
5696 if (options == NULL)
5697 mnt.mnt_mntopts = "";
5699 mnt.mnt_mntopts = (char *)options;
5701 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5702 zfs_is_mounted(zhp, NULL)) {
5706 (void) fprintf(stderr, gettext("cannot mount "
5707 "'%s': filesystem already mounted\n"),
5712 if (zfs_mount(zhp, options, flags) != 0)
5721 * Reports progress in the form "(current/total)". Not thread-safe.
5724 report_mount_progress(int current, int total)
5726 static time_t last_progress_time = 0;
5727 time_t now = time(NULL);
5730 /* report 1..n instead of 0..n-1 */
5733 /* display header if we're here for the first time */
5735 set_progress_header(gettext("Mounting ZFS filesystems"));
5736 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5737 /* too soon to report again */
5741 last_progress_time = now;
5743 (void) sprintf(info, "(%d/%d)", current, total);
5745 if (current == total)
5746 finish_progress(info);
5748 update_progress(info);
5752 append_options(char *mntopts, char *newopts)
5754 int len = strlen(mntopts);
5756 /* original length plus new string to append plus 1 for the comma */
5757 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5758 (void) fprintf(stderr, gettext("the opts argument for "
5759 "'%s' option is too long (more than %d chars)\n"),
5760 "-o", MNT_LINE_MAX);
5765 mntopts[len++] = ',';
5767 (void) strcpy(&mntopts[len], newopts);
5771 share_mount(int op, int argc, char **argv)
5774 boolean_t verbose = B_FALSE;
5776 char *options = NULL;
5780 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5790 if (*optarg == '\0') {
5791 (void) fprintf(stderr, gettext("empty mount "
5792 "options (-o) specified\n"));
5796 if (options == NULL)
5797 options = safe_malloc(MNT_LINE_MAX + 1);
5799 /* option validation is done later */
5800 append_options(options, optarg);
5803 flags |= MS_OVERLAY;
5806 (void) fprintf(stderr, gettext("missing argument for "
5807 "'%c' option\n"), optopt);
5811 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5820 /* check number of arguments */
5822 zfs_handle_t **dslist = NULL;
5823 size_t i, count = 0;
5824 char *protocol = NULL;
5826 if (op == OP_SHARE && argc > 0) {
5827 if (strcmp(argv[0], "nfs") != 0 &&
5828 strcmp(argv[0], "smb") != 0) {
5829 (void) fprintf(stderr, gettext("share type "
5830 "must be 'nfs' or 'smb'\n"));
5839 (void) fprintf(stderr, gettext("too many arguments\n"));
5843 start_progress_timer();
5844 get_all_datasets(&dslist, &count, verbose);
5849 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5851 for (i = 0; i < count; i++) {
5853 report_mount_progress(i, count);
5855 if (share_mount_one(dslist[i], op, flags, protocol,
5856 B_FALSE, options) != 0)
5858 zfs_close(dslist[i]);
5862 } else if (argc == 0) {
5863 struct mnttab entry;
5865 if ((op == OP_SHARE) || (options != NULL)) {
5866 (void) fprintf(stderr, gettext("missing filesystem "
5867 "argument (specify -a for all)\n"));
5872 * When mount is given no arguments, go through /etc/mtab and
5873 * display any active ZFS mounts. We hide any snapshots, since
5874 * they are controlled automatically.
5877 /* Reopen MNTTAB to prevent reading stale data from open file */
5878 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
5881 while (getmntent(mnttab_file, &entry) == 0) {
5882 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5883 strchr(entry.mnt_special, '@') != NULL)
5886 (void) printf("%-30s %s\n", entry.mnt_special,
5894 (void) fprintf(stderr,
5895 gettext("too many arguments\n"));
5899 if ((zhp = zfs_open(g_zfs, argv[0],
5900 ZFS_TYPE_FILESYSTEM)) == NULL) {
5903 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5913 * zfs mount -a [nfs]
5914 * zfs mount filesystem
5916 * Mount all filesystems, or mount the given filesystem.
5919 zfs_do_mount(int argc, char **argv)
5921 return (share_mount(OP_MOUNT, argc, argv));
5925 * zfs share -a [nfs | smb]
5926 * zfs share filesystem
5928 * Share all filesystems, or share the given filesystem.
5931 zfs_do_share(int argc, char **argv)
5933 return (share_mount(OP_SHARE, argc, argv));
5936 typedef struct unshare_unmount_node {
5937 zfs_handle_t *un_zhp;
5939 uu_avl_node_t un_avlnode;
5940 } unshare_unmount_node_t;
5944 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
5946 const unshare_unmount_node_t *l = larg;
5947 const unshare_unmount_node_t *r = rarg;
5949 return (strcmp(l->un_mountp, r->un_mountp));
5953 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5954 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
5955 * and unmount it appropriately.
5958 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
5962 struct stat64 statbuf;
5963 struct extmnttab entry;
5964 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5968 * Search for the path in /etc/mtab. Rather than looking for the
5969 * specific path, which can be fooled by non-standard paths (i.e. ".."
5970 * or "//"), we stat() the path and search for the corresponding
5971 * (major,minor) device pair.
5973 if (stat64(path, &statbuf) != 0) {
5974 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5975 cmdname, path, strerror(errno));
5978 path_inode = statbuf.st_ino;
5981 * Search for the given (major,minor) pair in the mount table.
5984 /* Reopen MNTTAB to prevent reading stale data from open file */
5985 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
5988 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
5989 if (entry.mnt_major == major(statbuf.st_dev) &&
5990 entry.mnt_minor == minor(statbuf.st_dev))
5994 if (op == OP_SHARE) {
5995 (void) fprintf(stderr, gettext("cannot %s '%s': not "
5996 "currently mounted\n"), cmdname, path);
5999 (void) fprintf(stderr, gettext("warning: %s not in mtab\n"),
6001 if ((ret = umount2(path, flags)) != 0)
6002 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6007 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6008 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6009 "filesystem\n"), cmdname, path);
6013 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6014 ZFS_TYPE_FILESYSTEM)) == NULL)
6018 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6019 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6020 cmdname, path, strerror(errno));
6022 } else if (statbuf.st_ino != path_inode) {
6023 (void) fprintf(stderr, gettext("cannot "
6024 "%s '%s': not a mountpoint\n"), cmdname, path);
6028 if (op == OP_SHARE) {
6029 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6030 char smbshare_prop[ZFS_MAXPROPLEN];
6032 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6033 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6034 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6035 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6037 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6038 strcmp(smbshare_prop, "off") == 0) {
6039 (void) fprintf(stderr, gettext("cannot unshare "
6040 "'%s': legacy share\n"), path);
6041 (void) fprintf(stderr, gettext("use exportfs(8) "
6042 "or smbcontrol(1) to unshare this filesystem\n"));
6043 } else if (!zfs_is_shared(zhp)) {
6044 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6045 "not currently shared\n"), path);
6047 ret = zfs_unshareall_bypath(zhp, path);
6050 char mtpt_prop[ZFS_MAXPROPLEN];
6052 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6053 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6056 ret = zfs_unmount(zhp, NULL, flags);
6057 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6058 (void) fprintf(stderr, gettext("cannot unmount "
6059 "'%s': legacy mountpoint\n"),
6061 (void) fprintf(stderr, gettext("use umount(8) "
6062 "to unmount this filesystem\n"));
6064 ret = zfs_unmountall(zhp, flags);
6075 * Generic callback for unsharing or unmounting a filesystem.
6078 unshare_unmount(int op, int argc, char **argv)
6085 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6086 char sharesmb[ZFS_MAXPROPLEN];
6089 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6098 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6109 * We could make use of zfs_for_each() to walk all datasets in
6110 * the system, but this would be very inefficient, especially
6111 * since we would have to linearly search /etc/mtab for each
6112 * one. Instead, do one pass through /etc/mtab looking for
6113 * zfs entries and call zfs_unmount() for each one.
6115 * Things get a little tricky if the administrator has created
6116 * mountpoints beneath other ZFS filesystems. In this case, we
6117 * have to unmount the deepest filesystems first. To accomplish
6118 * this, we place all the mountpoints in an AVL tree sorted by
6119 * the special type (dataset name), and walk the result in
6120 * reverse to make sure to get any snapshots first.
6122 struct mnttab entry;
6123 uu_avl_pool_t *pool;
6124 uu_avl_t *tree = NULL;
6125 unshare_unmount_node_t *node;
6127 uu_avl_walk_t *walk;
6130 (void) fprintf(stderr, gettext("too many arguments\n"));
6134 if (((pool = uu_avl_pool_create("unmount_pool",
6135 sizeof (unshare_unmount_node_t),
6136 offsetof(unshare_unmount_node_t, un_avlnode),
6137 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6138 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6141 /* Reopen MNTTAB to prevent reading stale data from open file */
6142 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6145 while (getmntent(mnttab_file, &entry) == 0) {
6147 /* ignore non-ZFS entries */
6148 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6151 /* ignore snapshots */
6152 if (strchr(entry.mnt_special, '@') != NULL)
6155 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6156 ZFS_TYPE_FILESYSTEM)) == NULL) {
6163 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6165 sizeof (nfs_mnt_prop),
6166 NULL, NULL, 0, B_FALSE) == 0);
6167 if (strcmp(nfs_mnt_prop, "off") != 0)
6169 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6171 sizeof (nfs_mnt_prop),
6172 NULL, NULL, 0, B_FALSE) == 0);
6173 if (strcmp(nfs_mnt_prop, "off") == 0)
6177 /* Ignore legacy mounts */
6178 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6180 sizeof (nfs_mnt_prop),
6181 NULL, NULL, 0, B_FALSE) == 0);
6182 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6184 /* Ignore canmount=noauto mounts */
6185 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6186 ZFS_CANMOUNT_NOAUTO)
6192 node = safe_malloc(sizeof (unshare_unmount_node_t));
6194 node->un_mountp = safe_strdup(entry.mnt_mountp);
6196 uu_avl_node_init(node, &node->un_avlnode, pool);
6198 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6199 uu_avl_insert(tree, node, idx);
6201 zfs_close(node->un_zhp);
6202 free(node->un_mountp);
6208 * Walk the AVL tree in reverse, unmounting each filesystem and
6209 * removing it from the AVL tree in the process.
6211 if ((walk = uu_avl_walk_start(tree,
6212 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6215 while ((node = uu_avl_walk_next(walk)) != NULL) {
6216 uu_avl_remove(tree, node);
6220 if (zfs_unshareall_bypath(node->un_zhp,
6221 node->un_mountp) != 0)
6226 if (zfs_unmount(node->un_zhp,
6227 node->un_mountp, flags) != 0)
6232 zfs_close(node->un_zhp);
6233 free(node->un_mountp);
6237 uu_avl_walk_end(walk);
6238 uu_avl_destroy(tree);
6239 uu_avl_pool_destroy(pool);
6244 (void) fprintf(stderr,
6245 gettext("missing filesystem argument\n"));
6247 (void) fprintf(stderr,
6248 gettext("too many arguments\n"));
6253 * We have an argument, but it may be a full path or a ZFS
6254 * filesystem. Pass full paths off to unmount_path() (shared by
6255 * manual_unmount), otherwise open the filesystem and pass to
6258 if (argv[0][0] == '/')
6259 return (unshare_unmount_path(op, argv[0],
6262 if ((zhp = zfs_open(g_zfs, argv[0],
6263 ZFS_TYPE_FILESYSTEM)) == NULL)
6266 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6267 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6268 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6269 NULL, 0, B_FALSE) == 0);
6273 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6275 sizeof (nfs_mnt_prop),
6276 NULL, NULL, 0, B_FALSE) == 0);
6277 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6278 sharesmb, sizeof (sharesmb), NULL, NULL,
6281 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6282 strcmp(sharesmb, "off") == 0) {
6283 (void) fprintf(stderr, gettext("cannot "
6284 "unshare '%s': legacy share\n"),
6286 (void) fprintf(stderr, gettext("use "
6287 "unshare(1M) to unshare this "
6290 } else if (!zfs_is_shared(zhp)) {
6291 (void) fprintf(stderr, gettext("cannot "
6292 "unshare '%s': not currently "
6293 "shared\n"), zfs_get_name(zhp));
6295 } else if (zfs_unshareall(zhp) != 0) {
6301 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6302 (void) fprintf(stderr, gettext("cannot "
6303 "unmount '%s': legacy "
6304 "mountpoint\n"), zfs_get_name(zhp));
6305 (void) fprintf(stderr, gettext("use "
6306 "umount(1M) to unmount this "
6309 } else if (!zfs_is_mounted(zhp, NULL)) {
6310 (void) fprintf(stderr, gettext("cannot "
6311 "unmount '%s': not currently "
6315 } else if (zfs_unmountall(zhp, flags) != 0) {
6329 * zfs unmount filesystem
6331 * Unmount all filesystems, or a specific ZFS filesystem.
6334 zfs_do_unmount(int argc, char **argv)
6336 return (unshare_unmount(OP_MOUNT, argc, argv));
6341 * zfs unshare filesystem
6343 * Unshare all filesystems, or a specific ZFS filesystem.
6346 zfs_do_unshare(int argc, char **argv)
6348 return (unshare_unmount(OP_SHARE, argc, argv));
6352 find_command_idx(char *command, int *idx)
6356 for (i = 0; i < NCOMMAND; i++) {
6357 if (command_table[i].name == NULL)
6360 if (strcmp(command, command_table[i].name) == 0) {
6369 zfs_do_diff(int argc, char **argv)
6373 char *tosnap = NULL;
6374 char *fromsnap = NULL;
6379 while ((c = getopt(argc, argv, "FHt")) != -1) {
6382 flags |= ZFS_DIFF_CLASSIFY;
6385 flags |= ZFS_DIFF_PARSEABLE;
6388 flags |= ZFS_DIFF_TIMESTAMP;
6391 (void) fprintf(stderr,
6392 gettext("invalid option '%c'\n"), optopt);
6401 (void) fprintf(stderr,
6402 gettext("must provide at least one snapshot name\n"));
6407 (void) fprintf(stderr, gettext("too many arguments\n"));
6412 tosnap = (argc == 2) ? argv[1] : NULL;
6415 if (*fromsnap != '@')
6416 copy = strdup(fromsnap);
6418 copy = strdup(tosnap);
6422 if ((atp = strchr(copy, '@')))
6425 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6431 * Ignore SIGPIPE so that the library can give us
6432 * information on any failure
6434 (void) sigignore(SIGPIPE);
6436 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6444 main(int argc, char **argv)
6450 (void) setlocale(LC_ALL, "");
6451 (void) textdomain(TEXT_DOMAIN);
6456 * Make sure the user has specified some command.
6459 (void) fprintf(stderr, gettext("missing command\n"));
6466 * The 'umount' command is an alias for 'unmount'
6468 if (strcmp(cmdname, "umount") == 0)
6469 cmdname = "unmount";
6472 * The 'recv' command is an alias for 'receive'
6474 if (strcmp(cmdname, "recv") == 0)
6475 cmdname = "receive";
6478 * The 'snap' command is an alias for 'snapshot'
6480 if (strcmp(cmdname, "snap") == 0)
6481 cmdname = "snapshot";
6486 if ((strcmp(cmdname, "-?") == 0) ||
6487 (strcmp(cmdname, "--help") == 0))
6490 if ((g_zfs = libzfs_init()) == NULL)
6493 mnttab_file = g_zfs->libzfs_mnttab;
6495 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6497 libzfs_print_on_error(g_zfs, B_TRUE);
6500 * Run the appropriate command.
6502 libzfs_mnttab_cache(g_zfs, B_TRUE);
6503 if (find_command_idx(cmdname, &i) == 0) {
6504 current_command = &command_table[i];
6505 ret = command_table[i].func(argc - 1, argv + 1);
6506 } else if (strchr(cmdname, '=') != NULL) {
6507 verify(find_command_idx("set", &i) == 0);
6508 current_command = &command_table[i];
6509 ret = command_table[i].func(argc, argv);
6511 (void) fprintf(stderr, gettext("unrecognized "
6512 "command '%s'\n"), cmdname);
6517 if (ret == 0 && log_history)
6518 (void) zpool_log_history(g_zfs, history_str);
6523 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6524 * for the purposes of running ::findleaks.
6526 if (getenv("ZFS_ABORT") != NULL) {
6527 (void) printf("dumping core by request\n");