4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
35 #include <libnvpair.h>
48 #include <sys/mkdev.h>
49 #include <sys/mntent.h>
50 #include <sys/mnttab.h>
51 #include <sys/mount.h>
53 #include <sys/fs/zfs.h>
54 #include <sys/types.h>
58 #include <libzfs_core.h>
60 #include <zfs_deleg.h>
64 #include <directory.h>
65 #endif /* HAVE_IDMAP */
69 #include "zfs_comutil.h"
70 #include "libzfs_impl.h"
72 libzfs_handle_t *g_zfs;
74 static FILE *mnttab_file;
75 static char history_str[HIS_MAX_RECORD_LEN];
76 static boolean_t log_history = B_TRUE;
78 static int zfs_do_clone(int argc, char **argv);
79 static int zfs_do_create(int argc, char **argv);
80 static int zfs_do_destroy(int argc, char **argv);
81 static int zfs_do_get(int argc, char **argv);
82 static int zfs_do_inherit(int argc, char **argv);
83 static int zfs_do_list(int argc, char **argv);
84 static int zfs_do_mount(int argc, char **argv);
85 static int zfs_do_rename(int argc, char **argv);
86 static int zfs_do_rollback(int argc, char **argv);
87 static int zfs_do_set(int argc, char **argv);
88 static int zfs_do_upgrade(int argc, char **argv);
89 static int zfs_do_snapshot(int argc, char **argv);
90 static int zfs_do_unmount(int argc, char **argv);
91 static int zfs_do_share(int argc, char **argv);
92 static int zfs_do_unshare(int argc, char **argv);
93 static int zfs_do_send(int argc, char **argv);
94 static int zfs_do_receive(int argc, char **argv);
95 static int zfs_do_promote(int argc, char **argv);
96 static int zfs_do_userspace(int argc, char **argv);
97 static int zfs_do_allow(int argc, char **argv);
98 static int zfs_do_unallow(int argc, char **argv);
99 static int zfs_do_hold(int argc, char **argv);
100 static int zfs_do_holds(int argc, char **argv);
101 static int zfs_do_release(int argc, char **argv);
102 static int zfs_do_diff(int argc, char **argv);
105 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
110 _umem_debug_init(void)
112 return ("default,verbose"); /* $UMEM_DEBUG setting */
116 _umem_logging_init(void)
118 return ("fail,contents"); /* $UMEM_LOGGING setting */
151 typedef struct zfs_command {
153 int (*func)(int argc, char **argv);
158 * Master command table. Each ZFS command has a name, associated function, and
159 * usage message. The usage messages need to be internationalized, so we have
160 * to have a function to return the usage message based on a command index.
162 * These commands are organized according to how they are displayed in the usage
163 * message. An empty command (one with a NULL name) indicates an empty line in
164 * the generic usage message.
166 static zfs_command_t command_table[] = {
167 { "create", zfs_do_create, HELP_CREATE },
168 { "destroy", zfs_do_destroy, HELP_DESTROY },
170 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
171 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
172 { "clone", zfs_do_clone, HELP_CLONE },
173 { "promote", zfs_do_promote, HELP_PROMOTE },
174 { "rename", zfs_do_rename, HELP_RENAME },
176 { "list", zfs_do_list, HELP_LIST },
178 { "set", zfs_do_set, HELP_SET },
179 { "get", zfs_do_get, HELP_GET },
180 { "inherit", zfs_do_inherit, HELP_INHERIT },
181 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
182 { "userspace", zfs_do_userspace, HELP_USERSPACE },
183 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
185 { "mount", zfs_do_mount, HELP_MOUNT },
186 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
187 { "share", zfs_do_share, HELP_SHARE },
188 { "unshare", zfs_do_unshare, HELP_UNSHARE },
190 { "send", zfs_do_send, HELP_SEND },
191 { "receive", zfs_do_receive, HELP_RECEIVE },
193 { "allow", zfs_do_allow, HELP_ALLOW },
195 { "unallow", zfs_do_unallow, HELP_UNALLOW },
197 { "hold", zfs_do_hold, HELP_HOLD },
198 { "holds", zfs_do_holds, HELP_HOLDS },
199 { "release", zfs_do_release, HELP_RELEASE },
200 { "diff", zfs_do_diff, HELP_DIFF },
203 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
205 zfs_command_t *current_command;
208 get_usage(zfs_help_t idx)
212 return (gettext("\tclone [-p] [-o property=value] ... "
213 "<snapshot> <filesystem|volume>\n"));
215 return (gettext("\tcreate [-p] [-o property=value] ... "
217 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
218 "-V <size> <volume>\n"));
220 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
221 "\tdestroy [-dnpRrv] "
222 "<filesystem|volume>@<snap>[%<snap>][,...]\n"));
224 return (gettext("\tget [-rHp] [-d max] "
225 "[-o \"all\" | field[,...]] [-t type[,...]] "
226 "[-s source[,...]]\n"
227 "\t <\"all\" | property[,...]> "
228 "[filesystem|volume|snapshot] ...\n"));
230 return (gettext("\tinherit [-rS] <property> "
231 "<filesystem|volume|snapshot> ...\n"));
233 return (gettext("\tupgrade [-v]\n"
234 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
236 return (gettext("\tlist [-rH][-d max] "
237 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
238 "\t [-S property] ... "
239 "[filesystem|volume|snapshot|snap] ...\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] ... "
296 "[-t type[,...]] <filesystem|snapshot>\n"));
297 case HELP_GROUPSPACE:
298 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
299 "[-s field] ...\n\t[-S field] ... "
300 "[-t type[,...]] <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 */
652 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
654 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
655 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
656 ret = zfs_share(clone);
675 * zfs create [-p] [-o prop=value] ... fs
676 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
678 * Create a new dataset. This command can be used to create filesystems
679 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
680 * For volumes, the user must specify a size to be used.
682 * The '-s' flag applies only to volumes, and indicates that we should not try
683 * to set the reservation for this volume. By default we set a reservation
684 * equal to the size for any volume. For pools with SPA_VERSION >=
685 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
687 * The '-p' flag creates all the non-existing ancestors of the target first.
690 zfs_do_create(int argc, char **argv)
692 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
693 zfs_handle_t *zhp = NULL;
694 uint64_t volsize = 0;
696 boolean_t noreserve = B_FALSE;
697 boolean_t bflag = B_FALSE;
698 boolean_t parents = B_FALSE;
702 int canmount = ZFS_CANMOUNT_OFF;
704 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
708 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
711 type = ZFS_TYPE_VOLUME;
712 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
713 (void) fprintf(stderr, gettext("bad volume "
714 "size '%s': %s\n"), optarg,
715 libzfs_error_description(g_zfs));
719 if (nvlist_add_uint64(props,
720 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
729 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
730 (void) fprintf(stderr, gettext("bad volume "
731 "block size '%s': %s\n"), optarg,
732 libzfs_error_description(g_zfs));
736 if (nvlist_add_uint64(props,
737 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
742 if (parseprop(props))
749 (void) fprintf(stderr, gettext("missing size "
754 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
760 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
761 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
762 "used when creating a volume\n"));
769 /* check number of arguments */
771 (void) fprintf(stderr, gettext("missing %s argument\n"),
772 zfs_type_to_name(type));
776 (void) fprintf(stderr, gettext("too many arguments\n"));
780 if (type == ZFS_TYPE_VOLUME && !noreserve) {
781 zpool_handle_t *zpool_handle;
782 uint64_t spa_version;
784 zfs_prop_t resv_prop;
787 if ((p = strchr(argv[0], '/')))
789 zpool_handle = zpool_open(g_zfs, argv[0]);
792 if (zpool_handle == NULL)
794 spa_version = zpool_get_prop_int(zpool_handle,
795 ZPOOL_PROP_VERSION, NULL);
796 zpool_close(zpool_handle);
797 if (spa_version >= SPA_VERSION_REFRESERVATION)
798 resv_prop = ZFS_PROP_REFRESERVATION;
800 resv_prop = ZFS_PROP_RESERVATION;
801 volsize = zvol_volsize_to_reservation(volsize, props);
803 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
805 if (nvlist_add_uint64(props,
806 zfs_prop_to_name(resv_prop), volsize) != 0) {
813 if (parents && zfs_name_valid(argv[0], type)) {
815 * Now create the ancestors of target dataset. If the target
816 * already exists and '-p' option was used we should not
819 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
823 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
828 if (zfs_create(g_zfs, argv[0], type, props) != 0)
831 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
836 * if the user doesn't want the dataset automatically mounted,
837 * then skip the mount/share step
839 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
840 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
843 * Mount and/or share the new filesystem as appropriate. We provide a
844 * verbose error message to let the user know that their filesystem was
845 * in fact created, even if we failed to mount or share it.
847 if (canmount == ZFS_CANMOUNT_ON) {
848 if (zfs_mount(zhp, NULL, 0) != 0) {
849 (void) fprintf(stderr, gettext("filesystem "
850 "successfully created, but not mounted\n"));
852 } else if (zfs_share(zhp) != 0) {
853 (void) fprintf(stderr, gettext("filesystem "
854 "successfully created, but not shared\n"));
871 * zfs destroy [-rRf] <fs, vol>
872 * zfs destroy [-rRd] <snap>
874 * -r Recursively destroy all children
875 * -R Recursively destroy all dependents, including clones
876 * -f Force unmounting of any dependents
877 * -d If we can't destroy now, mark for deferred destruction
879 * Destroys the given dataset. By default, it will unmount any filesystems,
880 * and refuse to destroy a dataset that has any dependents. A dependent can
881 * either be a child, or a clone of a child.
883 typedef struct destroy_cbdata {
886 boolean_t cb_recurse;
888 boolean_t cb_doclones;
889 zfs_handle_t *cb_target;
890 boolean_t cb_defer_destroy;
891 boolean_t cb_verbose;
892 boolean_t cb_parsable;
896 /* first snap in contiguous run */
898 /* previous snap in contiguous run */
905 * Check for any dependents based on the '-r' or '-R' flags.
908 destroy_check_dependent(zfs_handle_t *zhp, void *data)
910 destroy_cbdata_t *cbp = data;
911 const char *tname = zfs_get_name(cbp->cb_target);
912 const char *name = zfs_get_name(zhp);
914 if (strncmp(tname, name, strlen(tname)) == 0 &&
915 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
917 * This is a direct descendant, not a clone somewhere else in
924 (void) fprintf(stderr, gettext("cannot destroy '%s': "
925 "%s has children\n"),
926 zfs_get_name(cbp->cb_target),
927 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
928 (void) fprintf(stderr, gettext("use '-r' to destroy "
929 "the following datasets:\n"));
930 cbp->cb_first = B_FALSE;
931 cbp->cb_error = B_TRUE;
934 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
937 * This is a clone. We only want to report this if the '-r'
938 * wasn't specified, or the target is a snapshot.
940 if (!cbp->cb_recurse &&
941 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
945 (void) fprintf(stderr, gettext("cannot destroy '%s': "
946 "%s has dependent clones\n"),
947 zfs_get_name(cbp->cb_target),
948 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
949 (void) fprintf(stderr, gettext("use '-R' to destroy "
950 "the following datasets:\n"));
951 cbp->cb_first = B_FALSE;
952 cbp->cb_error = B_TRUE;
953 cbp->cb_dryrun = B_TRUE;
956 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
965 destroy_callback(zfs_handle_t *zhp, void *data)
967 destroy_cbdata_t *cb = data;
968 const char *name = zfs_get_name(zhp);
970 if (cb->cb_verbose) {
971 if (cb->cb_parsable) {
972 (void) printf("destroy\t%s\n", name);
973 } else if (cb->cb_dryrun) {
974 (void) printf(gettext("would destroy %s\n"),
977 (void) printf(gettext("will destroy %s\n"),
983 * Ignore pools (which we've already flagged as an error before getting
986 if (strchr(zfs_get_name(zhp), '/') == NULL &&
987 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
992 if (!cb->cb_dryrun) {
993 if (zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
994 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1005 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1007 destroy_cbdata_t *cb = arg;
1008 const char *name = zfs_get_name(zhp);
1011 if (nvlist_exists(cb->cb_nvl, name)) {
1012 if (cb->cb_firstsnap == NULL)
1013 cb->cb_firstsnap = strdup(name);
1014 if (cb->cb_prevsnap != NULL)
1015 free(cb->cb_prevsnap);
1016 /* this snap continues the current range */
1017 cb->cb_prevsnap = strdup(name);
1018 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1020 if (cb->cb_verbose) {
1021 if (cb->cb_parsable) {
1022 (void) printf("destroy\t%s\n", name);
1023 } else if (cb->cb_dryrun) {
1024 (void) printf(gettext("would destroy %s\n"),
1027 (void) printf(gettext("will destroy %s\n"),
1031 } else if (cb->cb_firstsnap != NULL) {
1032 /* end of this range */
1034 err = lzc_snaprange_space(cb->cb_firstsnap,
1035 cb->cb_prevsnap, &used);
1036 cb->cb_snapused += used;
1037 free(cb->cb_firstsnap);
1038 cb->cb_firstsnap = NULL;
1039 free(cb->cb_prevsnap);
1040 cb->cb_prevsnap = NULL;
1047 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1050 assert(cb->cb_firstsnap == NULL);
1051 assert(cb->cb_prevsnap == NULL);
1052 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1053 if (cb->cb_firstsnap != NULL) {
1056 err = lzc_snaprange_space(cb->cb_firstsnap,
1057 cb->cb_prevsnap, &used);
1059 cb->cb_snapused += used;
1060 free(cb->cb_firstsnap);
1061 cb->cb_firstsnap = NULL;
1062 free(cb->cb_prevsnap);
1063 cb->cb_prevsnap = NULL;
1069 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1071 destroy_cbdata_t *cb = arg;
1074 /* Check for clones. */
1075 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1076 cb->cb_target = zhp;
1077 cb->cb_first = B_TRUE;
1078 err = zfs_iter_dependents(zhp, B_TRUE,
1079 destroy_check_dependent, cb);
1083 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1091 gather_snapshots(zfs_handle_t *zhp, void *arg)
1093 destroy_cbdata_t *cb = arg;
1096 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1102 if (cb->cb_verbose) {
1103 err = destroy_print_snapshots(zhp, cb);
1109 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1117 destroy_clones(destroy_cbdata_t *cb)
1120 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1122 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1123 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1126 boolean_t defer = cb->cb_defer_destroy;
1130 * We can't defer destroy non-snapshots, so set it to
1131 * false while destroying the clones.
1133 cb->cb_defer_destroy = B_FALSE;
1134 err = zfs_iter_dependents(zhp, B_FALSE,
1135 destroy_callback, cb);
1136 cb->cb_defer_destroy = defer;
1146 zfs_do_destroy(int argc, char **argv)
1148 destroy_cbdata_t cb = { 0 };
1152 zfs_type_t type = ZFS_TYPE_DATASET;
1155 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1158 cb.cb_verbose = B_TRUE;
1161 cb.cb_verbose = B_TRUE;
1162 cb.cb_parsable = B_TRUE;
1165 cb.cb_dryrun = B_TRUE;
1168 cb.cb_defer_destroy = B_TRUE;
1169 type = ZFS_TYPE_SNAPSHOT;
1172 cb.cb_force = B_TRUE;
1175 cb.cb_recurse = B_TRUE;
1178 cb.cb_recurse = B_TRUE;
1179 cb.cb_doclones = B_TRUE;
1183 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1192 /* check number of arguments */
1194 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1198 (void) fprintf(stderr, gettext("too many arguments\n"));
1202 at = strchr(argv[0], '@');
1206 /* Build the list of snaps to destroy in cb_nvl. */
1207 if (nvlist_alloc(&cb.cb_nvl, NV_UNIQUE_NAME, 0) != 0)
1211 zhp = zfs_open(g_zfs, argv[0],
1212 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1216 cb.cb_snapspec = at + 1;
1217 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1220 nvlist_free(cb.cb_nvl);
1224 if (nvlist_empty(cb.cb_nvl)) {
1225 (void) fprintf(stderr, gettext("could not find any "
1226 "snapshots to destroy; check snapshot names.\n"));
1228 nvlist_free(cb.cb_nvl);
1232 if (cb.cb_verbose) {
1234 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1235 if (cb.cb_parsable) {
1236 (void) printf("reclaim\t%llu\n",
1237 (u_longlong_t)cb.cb_snapused);
1238 } else if (cb.cb_dryrun) {
1239 (void) printf(gettext("would reclaim %s\n"),
1242 (void) printf(gettext("will reclaim %s\n"),
1247 if (!cb.cb_dryrun) {
1249 err = destroy_clones(&cb);
1251 err = zfs_destroy_snaps_nvl(zhp, cb.cb_nvl,
1252 cb.cb_defer_destroy);
1257 nvlist_free(cb.cb_nvl);
1261 /* Open the given dataset */
1262 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1268 * Perform an explicit check for pools before going any further.
1270 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1271 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1272 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1273 "operation does not apply to pools\n"),
1275 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1276 "%s' to destroy all datasets in the pool\n"),
1278 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1279 "to destroy the pool itself\n"), zfs_get_name(zhp));
1285 * Check for any dependents and/or clones.
1287 cb.cb_first = B_TRUE;
1288 if (!cb.cb_doclones &&
1289 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1300 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1307 * Do the real thing. The callback will close the
1308 * handle regardless of whether it succeeds or not.
1310 if (destroy_callback(zhp, &cb) != 0)
1318 is_recvd_column(zprop_get_cbdata_t *cbp)
1321 zfs_get_column_t col;
1323 for (i = 0; i < ZFS_GET_NCOLS &&
1324 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1325 if (col == GET_COL_RECVD)
1331 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1332 * < all | property[,property]... > < fs | snap | vol > ...
1334 * -r recurse over any child datasets
1335 * -H scripted mode. Headers are stripped, and fields are separated
1336 * by tabs instead of spaces.
1337 * -o Set of fields to display. One of "name,property,value,
1338 * received,source". Default is "name,property,value,source".
1339 * "all" is an alias for all five.
1340 * -s Set of sources to allow. One of
1341 * "local,default,inherited,received,temporary,none". Default is
1343 * -p Display values in parsable (literal) format.
1345 * Prints properties for the given datasets. The user can control which
1346 * columns to display as well as which property types to allow.
1350 * Invoked to display the properties for a single dataset.
1353 get_callback(zfs_handle_t *zhp, void *data)
1355 char buf[ZFS_MAXPROPLEN];
1356 char rbuf[ZFS_MAXPROPLEN];
1357 zprop_source_t sourcetype;
1358 char source[ZFS_MAXNAMELEN];
1359 zprop_get_cbdata_t *cbp = data;
1360 nvlist_t *user_props = zfs_get_user_props(zhp);
1361 zprop_list_t *pl = cbp->cb_proplist;
1365 boolean_t received = is_recvd_column(cbp);
1367 for (; pl != NULL; pl = pl->pl_next) {
1368 char *recvdval = NULL;
1370 * Skip the special fake placeholder. This will also skip over
1371 * the name property when 'all' is specified.
1373 if (pl->pl_prop == ZFS_PROP_NAME &&
1374 pl == cbp->cb_proplist)
1377 if (pl->pl_prop != ZPROP_INVAL) {
1378 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1379 sizeof (buf), &sourcetype, source,
1381 cbp->cb_literal) != 0) {
1384 if (!zfs_prop_valid_for_type(pl->pl_prop,
1385 ZFS_TYPE_DATASET)) {
1386 (void) fprintf(stderr,
1387 gettext("No such property '%s'\n"),
1388 zfs_prop_to_name(pl->pl_prop));
1391 sourcetype = ZPROP_SRC_NONE;
1392 (void) strlcpy(buf, "-", sizeof (buf));
1395 if (received && (zfs_prop_get_recvd(zhp,
1396 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1397 cbp->cb_literal) == 0))
1400 zprop_print_one_property(zfs_get_name(zhp), cbp,
1401 zfs_prop_to_name(pl->pl_prop),
1402 buf, sourcetype, source, recvdval);
1403 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1404 sourcetype = ZPROP_SRC_LOCAL;
1406 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1407 buf, sizeof (buf), cbp->cb_literal) != 0) {
1408 sourcetype = ZPROP_SRC_NONE;
1409 (void) strlcpy(buf, "-", sizeof (buf));
1412 zprop_print_one_property(zfs_get_name(zhp), cbp,
1413 pl->pl_user_prop, buf, sourcetype, source, NULL);
1414 } else if (zfs_prop_written(pl->pl_user_prop)) {
1415 sourcetype = ZPROP_SRC_LOCAL;
1417 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1418 buf, sizeof (buf), cbp->cb_literal) != 0) {
1419 sourcetype = ZPROP_SRC_NONE;
1420 (void) strlcpy(buf, "-", sizeof (buf));
1423 zprop_print_one_property(zfs_get_name(zhp), cbp,
1424 pl->pl_user_prop, buf, sourcetype, source, NULL);
1426 if (nvlist_lookup_nvlist(user_props,
1427 pl->pl_user_prop, &propval) != 0) {
1430 sourcetype = ZPROP_SRC_NONE;
1433 verify(nvlist_lookup_string(propval,
1434 ZPROP_VALUE, &strval) == 0);
1435 verify(nvlist_lookup_string(propval,
1436 ZPROP_SOURCE, &sourceval) == 0);
1438 if (strcmp(sourceval,
1439 zfs_get_name(zhp)) == 0) {
1440 sourcetype = ZPROP_SRC_LOCAL;
1441 } else if (strcmp(sourceval,
1442 ZPROP_SOURCE_VAL_RECVD) == 0) {
1443 sourcetype = ZPROP_SRC_RECEIVED;
1445 sourcetype = ZPROP_SRC_INHERITED;
1446 (void) strlcpy(source,
1447 sourceval, sizeof (source));
1451 if (received && (zfs_prop_get_recvd(zhp,
1452 pl->pl_user_prop, rbuf, sizeof (rbuf),
1453 cbp->cb_literal) == 0))
1456 zprop_print_one_property(zfs_get_name(zhp), cbp,
1457 pl->pl_user_prop, strval, sourcetype,
1466 zfs_do_get(int argc, char **argv)
1468 zprop_get_cbdata_t cb = { 0 };
1469 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1470 int types = ZFS_TYPE_DATASET;
1471 char *value, *fields;
1474 zprop_list_t fake_name = { 0 };
1477 * Set up default columns and sources.
1479 cb.cb_sources = ZPROP_SRC_ALL;
1480 cb.cb_columns[0] = GET_COL_NAME;
1481 cb.cb_columns[1] = GET_COL_PROPERTY;
1482 cb.cb_columns[2] = GET_COL_VALUE;
1483 cb.cb_columns[3] = GET_COL_SOURCE;
1484 cb.cb_type = ZFS_TYPE_DATASET;
1487 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1490 cb.cb_literal = B_TRUE;
1493 limit = parse_depth(optarg, &flags);
1496 flags |= ZFS_ITER_RECURSE;
1499 cb.cb_scripted = B_TRUE;
1502 (void) fprintf(stderr, gettext("missing argument for "
1503 "'%c' option\n"), optopt);
1508 * Process the set of columns to display. We zero out
1509 * the structure to give us a blank slate.
1511 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1513 while (*optarg != '\0') {
1514 static char *col_subopts[] =
1515 { "name", "property", "value", "received",
1516 "source", "all", NULL };
1518 if (i == ZFS_GET_NCOLS) {
1519 (void) fprintf(stderr, gettext("too "
1520 "many fields given to -o "
1525 switch (getsubopt(&optarg, col_subopts,
1528 cb.cb_columns[i++] = GET_COL_NAME;
1531 cb.cb_columns[i++] = GET_COL_PROPERTY;
1534 cb.cb_columns[i++] = GET_COL_VALUE;
1537 cb.cb_columns[i++] = GET_COL_RECVD;
1538 flags |= ZFS_ITER_RECVD_PROPS;
1541 cb.cb_columns[i++] = GET_COL_SOURCE;
1545 (void) fprintf(stderr,
1546 gettext("\"all\" conflicts "
1547 "with specific fields "
1548 "given to -o option\n"));
1551 cb.cb_columns[0] = GET_COL_NAME;
1552 cb.cb_columns[1] = GET_COL_PROPERTY;
1553 cb.cb_columns[2] = GET_COL_VALUE;
1554 cb.cb_columns[3] = GET_COL_RECVD;
1555 cb.cb_columns[4] = GET_COL_SOURCE;
1556 flags |= ZFS_ITER_RECVD_PROPS;
1560 (void) fprintf(stderr,
1561 gettext("invalid column name "
1570 while (*optarg != '\0') {
1571 static char *source_subopts[] = {
1572 "local", "default", "inherited",
1573 "received", "temporary", "none",
1576 switch (getsubopt(&optarg, source_subopts,
1579 cb.cb_sources |= ZPROP_SRC_LOCAL;
1582 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1585 cb.cb_sources |= ZPROP_SRC_INHERITED;
1588 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1591 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1594 cb.cb_sources |= ZPROP_SRC_NONE;
1597 (void) fprintf(stderr,
1598 gettext("invalid source "
1607 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1608 while (*optarg != '\0') {
1609 static char *type_subopts[] = { "filesystem",
1610 "volume", "snapshot", "all", NULL };
1612 switch (getsubopt(&optarg, type_subopts,
1615 types |= ZFS_TYPE_FILESYSTEM;
1618 types |= ZFS_TYPE_VOLUME;
1621 types |= ZFS_TYPE_SNAPSHOT;
1624 types = ZFS_TYPE_DATASET;
1628 (void) fprintf(stderr,
1629 gettext("invalid type '%s'\n"),
1637 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1647 (void) fprintf(stderr, gettext("missing property "
1654 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1662 * As part of zfs_expand_proplist(), we keep track of the maximum column
1663 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1664 * need to know the maximum name length. However, the user likely did
1665 * not specify 'name' as one of the properties to fetch, so we need to
1666 * make sure we always include at least this property for
1667 * print_get_headers() to work properly.
1669 if (cb.cb_proplist != NULL) {
1670 fake_name.pl_prop = ZFS_PROP_NAME;
1671 fake_name.pl_width = strlen(gettext("NAME"));
1672 fake_name.pl_next = cb.cb_proplist;
1673 cb.cb_proplist = &fake_name;
1676 cb.cb_first = B_TRUE;
1678 /* run for each object */
1679 ret = zfs_for_each(argc, argv, flags, types, NULL,
1680 &cb.cb_proplist, limit, get_callback, &cb);
1682 if (cb.cb_proplist == &fake_name)
1683 zprop_free_list(fake_name.pl_next);
1685 zprop_free_list(cb.cb_proplist);
1691 * inherit [-rS] <property> <fs|vol> ...
1693 * -r Recurse over all children
1694 * -S Revert to received value, if any
1696 * For each dataset specified on the command line, inherit the given property
1697 * from its parent. Inheriting a property at the pool level will cause it to
1698 * use the default value. The '-r' flag will recurse over all children, and is
1699 * useful for setting a property on a hierarchy-wide basis, regardless of any
1700 * local modifications for each dataset.
1703 typedef struct inherit_cbdata {
1704 const char *cb_propname;
1705 boolean_t cb_received;
1709 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1711 inherit_cbdata_t *cb = data;
1712 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1715 * If we're doing it recursively, then ignore properties that
1716 * are not valid for this type of dataset.
1718 if (prop != ZPROP_INVAL &&
1719 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1722 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1726 inherit_cb(zfs_handle_t *zhp, void *data)
1728 inherit_cbdata_t *cb = data;
1730 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1734 zfs_do_inherit(int argc, char **argv)
1738 inherit_cbdata_t cb = { 0 };
1742 boolean_t received = B_FALSE;
1745 while ((c = getopt(argc, argv, "rS")) != -1) {
1748 flags |= ZFS_ITER_RECURSE;
1755 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1764 /* check number of arguments */
1766 (void) fprintf(stderr, gettext("missing property argument\n"));
1770 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1778 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1779 if (zfs_prop_readonly(prop)) {
1780 (void) fprintf(stderr, gettext(
1781 "%s property is read-only\n"),
1785 if (!zfs_prop_inheritable(prop) && !received) {
1786 (void) fprintf(stderr, gettext("'%s' property cannot "
1787 "be inherited\n"), propname);
1788 if (prop == ZFS_PROP_QUOTA ||
1789 prop == ZFS_PROP_RESERVATION ||
1790 prop == ZFS_PROP_REFQUOTA ||
1791 prop == ZFS_PROP_REFRESERVATION)
1792 (void) fprintf(stderr, gettext("use 'zfs set "
1793 "%s=none' to clear\n"), propname);
1796 if (received && (prop == ZFS_PROP_VOLSIZE ||
1797 prop == ZFS_PROP_VERSION)) {
1798 (void) fprintf(stderr, gettext("'%s' property cannot "
1799 "be reverted to a received value\n"), propname);
1802 } else if (!zfs_prop_user(propname)) {
1803 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1808 cb.cb_propname = propname;
1809 cb.cb_received = received;
1811 if (flags & ZFS_ITER_RECURSE) {
1812 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1813 NULL, NULL, 0, inherit_recurse_cb, &cb);
1815 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1816 NULL, NULL, 0, inherit_cb, &cb);
1822 typedef struct upgrade_cbdata {
1823 uint64_t cb_numupgraded;
1824 uint64_t cb_numsamegraded;
1825 uint64_t cb_numfailed;
1826 uint64_t cb_version;
1828 boolean_t cb_foundone;
1829 char cb_lastfs[ZFS_MAXNAMELEN];
1833 same_pool(zfs_handle_t *zhp, const char *name)
1835 int len1 = strcspn(name, "/@");
1836 const char *zhname = zfs_get_name(zhp);
1837 int len2 = strcspn(zhname, "/@");
1841 return (strncmp(name, zhname, len1) == 0);
1845 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1847 upgrade_cbdata_t *cb = data;
1848 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1850 /* list if it's old/new */
1851 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1852 (cb->cb_newer && version > ZPL_VERSION)) {
1855 str = gettext("The following filesystems are "
1856 "formatted using a newer software version and\n"
1857 "cannot be accessed on the current system.\n\n");
1859 str = gettext("The following filesystems are "
1860 "out of date, and can be upgraded. After being\n"
1861 "upgraded, these filesystems (and any 'zfs send' "
1862 "streams generated from\n"
1863 "subsequent snapshots) will no longer be "
1864 "accessible by older software versions.\n\n");
1867 if (!cb->cb_foundone) {
1869 (void) printf(gettext("VER FILESYSTEM\n"));
1870 (void) printf(gettext("--- ------------\n"));
1871 cb->cb_foundone = B_TRUE;
1874 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1881 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1883 upgrade_cbdata_t *cb = data;
1884 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1885 int needed_spa_version;
1888 if (zfs_spa_version(zhp, &spa_version) < 0)
1891 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1893 if (needed_spa_version < 0)
1896 if (spa_version < needed_spa_version) {
1898 (void) printf(gettext("%s: can not be "
1899 "upgraded; the pool version needs to first "
1900 "be upgraded\nto version %d\n\n"),
1901 zfs_get_name(zhp), needed_spa_version);
1907 if (version < cb->cb_version) {
1909 (void) snprintf(verstr, sizeof (verstr),
1910 "%llu", (u_longlong_t)cb->cb_version);
1911 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1913 * If they did "zfs upgrade -a", then we could
1914 * be doing ioctls to different pools. We need
1915 * to log this history once to each pool, and bypass
1916 * the normal history logging that happens in main().
1918 (void) zpool_log_history(g_zfs, history_str);
1919 log_history = B_FALSE;
1921 if (zfs_prop_set(zhp, "version", verstr) == 0)
1922 cb->cb_numupgraded++;
1925 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1926 } else if (version > cb->cb_version) {
1927 /* can't downgrade */
1928 (void) printf(gettext("%s: can not be downgraded; "
1929 "it is already at version %u\n"),
1930 zfs_get_name(zhp), version);
1933 cb->cb_numsamegraded++;
1941 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1944 zfs_do_upgrade(int argc, char **argv)
1946 boolean_t all = B_FALSE;
1947 boolean_t showversions = B_FALSE;
1949 upgrade_cbdata_t cb = { 0 };
1951 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1954 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1957 flags |= ZFS_ITER_RECURSE;
1960 showversions = B_TRUE;
1963 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1964 optarg, &cb.cb_version) != 0) {
1965 (void) fprintf(stderr,
1966 gettext("invalid version %s\n"), optarg);
1975 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1984 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1986 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1987 cb.cb_version || argc))
1989 if ((all || argc) && (showversions))
1995 /* Show info on available versions. */
1996 (void) printf(gettext("The following filesystem versions are "
1998 (void) printf(gettext("VER DESCRIPTION\n"));
1999 (void) printf("--- -----------------------------------------"
2000 "---------------\n");
2001 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2002 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2003 (void) printf(gettext(" 3 Case insensitive and filesystem "
2004 "user identifier (FUID)\n"));
2005 (void) printf(gettext(" 4 userquota, groupquota "
2007 (void) printf(gettext(" 5 System attributes\n"));
2008 (void) printf(gettext("\nFor more information on a particular "
2009 "version, including supported releases,\n"));
2010 (void) printf("see the ZFS Administration Guide.\n\n");
2012 } else if (argc || all) {
2013 /* Upgrade filesystems */
2014 if (cb.cb_version == 0)
2015 cb.cb_version = ZPL_VERSION;
2016 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2017 NULL, NULL, 0, upgrade_set_callback, &cb);
2018 (void) printf(gettext("%llu filesystems upgraded\n"),
2019 (u_longlong_t)cb.cb_numupgraded);
2020 if (cb.cb_numsamegraded) {
2021 (void) printf(gettext("%llu filesystems already at "
2023 (u_longlong_t)cb.cb_numsamegraded);
2025 if (cb.cb_numfailed != 0)
2028 /* List old-version filesytems */
2030 (void) printf(gettext("This system is currently running "
2031 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2033 flags |= ZFS_ITER_RECURSE;
2034 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2035 NULL, NULL, 0, upgrade_list_callback, &cb);
2037 found = cb.cb_foundone;
2038 cb.cb_foundone = B_FALSE;
2039 cb.cb_newer = B_TRUE;
2041 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2042 NULL, NULL, 0, upgrade_list_callback, &cb);
2044 if (!cb.cb_foundone && !found) {
2045 (void) printf(gettext("All filesystems are "
2046 "formatted with the current version.\n"));
2054 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2055 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2056 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2057 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2059 * -H Scripted mode; elide headers and separate columns by tabs.
2060 * -i Translate SID to POSIX ID.
2061 * -n Print numeric ID instead of user/group name.
2062 * -o Control which fields to display.
2063 * -p Use exact (parseable) numeric output.
2064 * -s Specify sort columns, descending order.
2065 * -S Specify sort columns, ascending order.
2066 * -t Control which object types to display.
2068 * Displays space consumed by, and quotas on, each user in the specified
2069 * filesystem or snapshot.
2072 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2073 enum us_field_types {
2079 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2080 static char *us_field_names[] = { "type", "name", "used", "quota" };
2081 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2083 #define USTYPE_PSX_GRP (1 << 0)
2084 #define USTYPE_PSX_USR (1 << 1)
2085 #define USTYPE_SMB_GRP (1 << 2)
2086 #define USTYPE_SMB_USR (1 << 3)
2087 #define USTYPE_ALL \
2088 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2090 static int us_type_bits[] = {
2097 static char *us_type_names[] = { "posixgroup", "posxiuser", "smbgroup",
2100 typedef struct us_node {
2102 uu_avl_node_t usn_avlnode;
2103 uu_list_node_t usn_listnode;
2106 typedef struct us_cbdata {
2108 uu_avl_pool_t *cb_avl_pool;
2110 boolean_t cb_numname;
2111 boolean_t cb_nicenum;
2112 boolean_t cb_sid2posix;
2113 zfs_userquota_prop_t cb_prop;
2114 zfs_sort_column_t *cb_sortcol;
2115 size_t cb_width[USFIELD_LAST];
2118 static boolean_t us_populated = B_FALSE;
2121 zfs_sort_column_t *si_sortcol;
2122 boolean_t si_numname;
2126 us_field_index(char *field)
2130 for (i = 0; i < USFIELD_LAST; i++) {
2131 if (strcmp(field, us_field_names[i]) == 0)
2139 us_compare(const void *larg, const void *rarg, void *unused)
2141 const us_node_t *l = larg;
2142 const us_node_t *r = rarg;
2143 us_sort_info_t *si = (us_sort_info_t *)unused;
2144 zfs_sort_column_t *sortcol = si->si_sortcol;
2145 boolean_t numname = si->si_numname;
2146 nvlist_t *lnvl = l->usn_nvl;
2147 nvlist_t *rnvl = r->usn_nvl;
2151 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2158 zfs_prop_t prop = sortcol->sc_prop;
2159 const char *propname = NULL;
2160 boolean_t reverse = sortcol->sc_reverse;
2165 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2166 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2168 rc = (rv32 < lv32) ? 1 : -1;
2173 (void) nvlist_lookup_uint64(lnvl, propname,
2175 (void) nvlist_lookup_uint64(rnvl, propname,
2178 rc = (rv64 < lv64) ? 1 : -1;
2180 (void) nvlist_lookup_string(lnvl, propname,
2182 (void) nvlist_lookup_string(rnvl, propname,
2184 rc = strcmp(lvstr, rvstr);
2188 case ZFS_PROP_QUOTA:
2191 if (prop == ZFS_PROP_USED)
2195 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2196 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2198 rc = (rv64 < lv64) ? 1 : -1;
2206 return (reverse ? 1 : -1);
2208 return (reverse ? -1 : 1);
2213 * If entries still seem to be the same, check if they are of the same
2214 * type (smbentity is added only if we are doing SID to POSIX ID
2215 * translation where we can have duplicate type/name combinations).
2217 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2218 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2220 return (lvb < rvb ? -1 : 1);
2225 static inline const char *
2226 us_type2str(unsigned field_type)
2228 switch (field_type) {
2229 case USTYPE_PSX_USR:
2230 return ("POSIX User");
2231 case USTYPE_PSX_GRP:
2232 return ("POSIX Group");
2233 case USTYPE_SMB_USR:
2234 return ("SMB User");
2235 case USTYPE_SMB_GRP:
2236 return ("SMB Group");
2238 return ("Undefined");
2243 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2245 us_cbdata_t *cb = (us_cbdata_t *)arg;
2246 zfs_userquota_prop_t prop = cb->cb_prop;
2251 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2252 uu_avl_t *avl = cb->cb_avl;
2256 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2258 const char *typestr;
2262 int typeidx, nameidx, sizeidx;
2263 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2264 boolean_t smbentity = B_FALSE;
2266 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2268 node = safe_malloc(sizeof (us_node_t));
2269 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2270 node->usn_nvl = props;
2272 if (domain != NULL && domain[0] != '\0') {
2275 char sid[ZFS_MAXNAMELEN + 32];
2279 directory_error_t e;
2283 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2285 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2286 type = USTYPE_SMB_GRP;
2287 err = sid_to_id(sid, B_FALSE, &id);
2289 type = USTYPE_SMB_USR;
2290 err = sid_to_id(sid, B_TRUE, &id);
2295 if (!cb->cb_sid2posix) {
2296 e = directory_name_from_sid(NULL, sid, &name,
2299 directory_error_free(e);
2309 #endif /* HAVE_IDMAP */
2312 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2314 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2315 type = USTYPE_PSX_GRP;
2316 if (!cb->cb_numname) {
2319 if ((g = getgrgid(rid)) != NULL)
2323 type = USTYPE_PSX_USR;
2324 if (!cb->cb_numname) {
2327 if ((p = getpwuid(rid)) != NULL)
2334 * Make sure that the type/name combination is unique when doing
2335 * SID to POSIX ID translation (hence changing the type from SMB to
2338 if (cb->cb_sid2posix &&
2339 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2342 /* Calculate/update width of TYPE field */
2343 typestr = us_type2str(type);
2344 typelen = strlen(gettext(typestr));
2345 typeidx = us_field_index("type");
2346 if (typelen > cb->cb_width[typeidx])
2347 cb->cb_width[typeidx] = typelen;
2348 if (nvlist_add_uint32(props, "type", type) != 0)
2351 /* Calculate/update width of NAME field */
2352 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2353 if (nvlist_add_uint64(props, "name", rid) != 0)
2355 namelen = snprintf(NULL, 0, "%u", rid);
2357 if (nvlist_add_string(props, "name", name) != 0)
2359 namelen = strlen(name);
2361 nameidx = us_field_index("name");
2362 if (namelen > cb->cb_width[nameidx])
2363 cb->cb_width[nameidx] = namelen;
2366 * Check if this type/name combination is in the list and update it;
2367 * otherwise add new node to the list.
2369 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2370 uu_avl_insert(avl, node, idx);
2375 props = node->usn_nvl;
2378 /* Calculate/update width of USED/QUOTA fields */
2380 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2382 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
2383 (u_longlong_t)space);
2384 sizelen = strlen(sizebuf);
2385 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2387 if (!nvlist_exists(props, "quota"))
2388 (void) nvlist_add_uint64(props, "quota", 0);
2391 if (!nvlist_exists(props, "used"))
2392 (void) nvlist_add_uint64(props, "used", 0);
2394 sizeidx = us_field_index(propname);
2395 if (sizelen > cb->cb_width[sizeidx])
2396 cb->cb_width[sizeidx] = sizelen;
2398 if (nvlist_add_uint64(props, propname, space) != 0)
2405 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2406 size_t *width, us_node_t *node)
2408 nvlist_t *nvl = node->usn_nvl;
2409 char valstr[ZFS_MAXNAMELEN];
2410 boolean_t first = B_TRUE;
2416 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2417 if (!(ustype & types))
2420 while ((field = fields[cfield]) != USFIELD_LAST) {
2421 nvpair_t *nvp = NULL;
2425 char *strval = NULL;
2427 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2428 if (strcmp(nvpair_name(nvp),
2429 us_field_names[field]) == 0)
2433 type = nvpair_type(nvp);
2435 case DATA_TYPE_UINT32:
2436 (void) nvpair_value_uint32(nvp, &val32);
2438 case DATA_TYPE_UINT64:
2439 (void) nvpair_value_uint64(nvp, &val64);
2441 case DATA_TYPE_STRING:
2442 (void) nvpair_value_string(nvp, &strval);
2445 (void) fprintf(stderr, "invalid data type\n");
2450 strval = (char *)us_type2str(val32);
2453 if (type == DATA_TYPE_UINT64) {
2454 (void) sprintf(valstr, "%llu",
2455 (u_longlong_t) val64);
2461 if (type == DATA_TYPE_UINT64) {
2463 (void) sprintf(valstr, "%llu",
2464 (u_longlong_t) val64);
2466 zfs_nicenum(val64, valstr,
2469 if (field == USFIELD_QUOTA &&
2470 strcmp(valstr, "0") == 0)
2480 (void) printf("\t");
2485 (void) printf("%s", strval);
2486 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2487 (void) printf("%-*s", (int) width[field], strval);
2489 (void) printf("%*s", (int) width[field], strval);
2495 (void) printf("\n");
2499 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2500 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2508 boolean_t first = B_TRUE;
2510 while ((field = fields[cfield]) != USFIELD_LAST) {
2511 col = gettext(us_field_hdr[field]);
2512 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2513 (void) printf(first ? "%-*s" : " %-*s",
2514 (int) width[field], col);
2516 (void) printf(first ? "%*s" : " %*s",
2517 (int) width[field], col);
2522 (void) printf("\n");
2525 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2526 print_us_node(scripted, parsable, fields, types, width, node);
2528 nvlist_free(node->usn_nvl);
2533 zfs_do_userspace(int argc, char **argv)
2536 zfs_userquota_prop_t p;
2537 uu_avl_pool_t *avl_pool;
2539 uu_avl_walk_t *walk;
2541 char deffields[] = "type,name,used,quota";
2542 char *ofield = NULL;
2543 char *tfield = NULL;
2547 boolean_t scripted = B_FALSE;
2548 boolean_t prtnum = B_FALSE;
2549 boolean_t parsable = B_FALSE;
2550 boolean_t sid2posix = B_FALSE;
2553 zfs_sort_column_t *sortcol = NULL;
2554 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2558 uu_list_pool_t *listpool;
2560 uu_avl_index_t idx = 0;
2561 uu_list_index_t idx2 = 0;
2566 if (strcmp(argv[0], "groupspace") == 0)
2567 /* Toggle default group types */
2568 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2570 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2586 if (zfs_add_sort_column(&sortcol, optarg,
2587 c == 's' ? B_FALSE : B_TRUE) != 0) {
2588 (void) fprintf(stderr,
2589 gettext("invalid field '%s'\n"), optarg);
2600 (void) fprintf(stderr, gettext("missing argument for "
2601 "'%c' option\n"), optopt);
2605 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2615 (void) fprintf(stderr, gettext("missing dataset name\n"));
2619 (void) fprintf(stderr, gettext("too many arguments\n"));
2623 /* Use default output fields if not specified using -o */
2627 if ((delim = strchr(ofield, ',')) != NULL)
2629 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2630 (void) fprintf(stderr, gettext("invalid type '%s' "
2631 "for -o option\n"), ofield);
2636 } while (delim != NULL);
2637 fields[cfield] = USFIELD_LAST;
2639 /* Override output types (-t option) */
2640 if (tfield != NULL) {
2644 boolean_t found = B_FALSE;
2646 if ((delim = strchr(tfield, ',')) != NULL)
2648 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2650 if (strcmp(tfield, us_type_names[i]) == 0) {
2652 types |= us_type_bits[i];
2657 (void) fprintf(stderr, gettext("invalid type "
2658 "'%s' for -t option\n"), tfield);
2663 } while (delim != NULL);
2666 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2669 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2670 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2672 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2675 /* Always add default sorting columns */
2676 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2677 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2679 cb.cb_sortcol = sortcol;
2680 cb.cb_numname = prtnum;
2681 cb.cb_nicenum = !parsable;
2682 cb.cb_avl_pool = avl_pool;
2683 cb.cb_avl = avl_tree;
2684 cb.cb_sid2posix = sid2posix;
2686 for (i = 0; i < USFIELD_LAST; i++)
2687 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2689 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2690 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2691 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2692 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2693 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2696 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2701 if ((node = uu_avl_first(avl_tree)) == NULL)
2704 us_populated = B_TRUE;
2706 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2707 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2708 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2709 uu_list_node_init(node, &node->usn_listnode, listpool);
2711 while (node != NULL) {
2713 node = uu_avl_next(avl_tree, node);
2714 uu_avl_remove(avl_tree, rmnode);
2715 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2716 uu_list_insert(list, rmnode, idx2);
2719 for (node = uu_list_first(list); node != NULL;
2720 node = uu_list_next(list, node)) {
2721 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2723 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2724 uu_avl_insert(avl_tree, node, idx);
2727 uu_list_destroy(list);
2728 uu_list_pool_destroy(listpool);
2730 /* Print and free node nvlist memory */
2731 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2734 zfs_free_sort_columns(sortcol);
2736 /* Clean up the AVL tree */
2737 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2740 while ((node = uu_avl_walk_next(walk)) != NULL) {
2741 uu_avl_remove(cb.cb_avl, node);
2745 uu_avl_walk_end(walk);
2746 uu_avl_destroy(avl_tree);
2747 uu_avl_pool_destroy(avl_pool);
2753 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2754 * [-s property [-s property]...] [-S property [-S property]...]
2757 * -r Recurse over all children
2758 * -d Limit recursion by depth.
2759 * -H Scripted mode; elide headers and separate columns by tabs
2760 * -o Control which fields to display.
2761 * -t Control which object types to display.
2762 * -s Specify sort columns, descending order.
2763 * -S Specify sort columns, ascending order.
2765 * When given no arguments, lists all filesystems in the system.
2766 * Otherwise, list the specified datasets, optionally recursing down them if
2767 * '-r' is specified.
2769 typedef struct list_cbdata {
2771 boolean_t cb_scripted;
2772 zprop_list_t *cb_proplist;
2776 * Given a list of columns to display, output appropriate headers for each one.
2779 print_header(zprop_list_t *pl)
2781 char headerbuf[ZFS_MAXPROPLEN];
2784 boolean_t first = B_TRUE;
2785 boolean_t right_justify;
2787 for (; pl != NULL; pl = pl->pl_next) {
2794 right_justify = B_FALSE;
2795 if (pl->pl_prop != ZPROP_INVAL) {
2796 header = zfs_prop_column_name(pl->pl_prop);
2797 right_justify = zfs_prop_align_right(pl->pl_prop);
2799 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2800 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2801 headerbuf[i] = '\0';
2805 if (pl->pl_next == NULL && !right_justify)
2806 (void) printf("%s", header);
2807 else if (right_justify)
2808 (void) printf("%*s", (int)pl->pl_width, header);
2810 (void) printf("%-*s", (int)pl->pl_width, header);
2813 (void) printf("\n");
2817 * Given a dataset and a list of fields, print out all the properties according
2818 * to the described layout.
2821 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
2823 boolean_t first = B_TRUE;
2824 char property[ZFS_MAXPROPLEN];
2825 nvlist_t *userprops = zfs_get_user_props(zhp);
2828 boolean_t right_justify;
2831 for (; pl != NULL; pl = pl->pl_next) {
2834 (void) printf("\t");
2841 if (pl->pl_prop == ZFS_PROP_NAME) {
2842 (void) strlcpy(property, zfs_get_name(zhp),
2845 right_justify = zfs_prop_align_right(pl->pl_prop);
2846 } else if (pl->pl_prop != ZPROP_INVAL) {
2847 if (zfs_prop_get(zhp, pl->pl_prop, property,
2848 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2853 right_justify = zfs_prop_align_right(pl->pl_prop);
2854 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2855 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2856 property, sizeof (property), B_FALSE) != 0)
2860 right_justify = B_TRUE;
2861 } else if (zfs_prop_written(pl->pl_user_prop)) {
2862 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2863 property, sizeof (property), B_FALSE) != 0)
2867 right_justify = B_TRUE;
2869 if (nvlist_lookup_nvlist(userprops,
2870 pl->pl_user_prop, &propval) != 0)
2873 verify(nvlist_lookup_string(propval,
2874 ZPROP_VALUE, &propstr) == 0);
2875 right_justify = B_FALSE;
2878 width = pl->pl_width;
2881 * If this is being called in scripted mode, or if this is the
2882 * last column and it is left-justified, don't include a width
2885 if (scripted || (pl->pl_next == NULL && !right_justify))
2886 (void) printf("%s", propstr);
2887 else if (right_justify)
2888 (void) printf("%*s", width, propstr);
2890 (void) printf("%-*s", width, propstr);
2893 (void) printf("\n");
2897 * Generic callback function to list a dataset or snapshot.
2900 list_callback(zfs_handle_t *zhp, void *data)
2902 list_cbdata_t *cbp = data;
2904 if (cbp->cb_first) {
2905 if (!cbp->cb_scripted)
2906 print_header(cbp->cb_proplist);
2907 cbp->cb_first = B_FALSE;
2910 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2916 zfs_do_list(int argc, char **argv)
2919 boolean_t scripted = B_FALSE;
2920 static char default_fields[] =
2921 "name,used,available,referenced,mountpoint";
2922 int types = ZFS_TYPE_DATASET;
2923 boolean_t types_specified = B_FALSE;
2924 char *fields = NULL;
2925 list_cbdata_t cb = { 0 };
2929 zfs_sort_column_t *sortcol = NULL;
2930 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2933 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2939 limit = parse_depth(optarg, &flags);
2942 flags |= ZFS_ITER_RECURSE;
2948 if (zfs_add_sort_column(&sortcol, optarg,
2950 (void) fprintf(stderr,
2951 gettext("invalid property '%s'\n"), optarg);
2956 if (zfs_add_sort_column(&sortcol, optarg,
2958 (void) fprintf(stderr,
2959 gettext("invalid property '%s'\n"), optarg);
2965 types_specified = B_TRUE;
2966 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2967 while (*optarg != '\0') {
2968 static char *type_subopts[] = { "filesystem",
2969 "volume", "snapshot", "snap", "all", NULL };
2971 switch (getsubopt(&optarg, type_subopts,
2974 types |= ZFS_TYPE_FILESYSTEM;
2977 types |= ZFS_TYPE_VOLUME;
2981 types |= ZFS_TYPE_SNAPSHOT;
2984 types = ZFS_TYPE_DATASET;
2988 (void) fprintf(stderr,
2989 gettext("invalid type '%s'\n"),
2996 (void) fprintf(stderr, gettext("missing argument for "
2997 "'%c' option\n"), optopt);
3001 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3011 fields = default_fields;
3014 * If we are only going to list snapshot names and sort by name,
3015 * then we can use faster version.
3017 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3018 flags |= ZFS_ITER_SIMPLE;
3021 * If "-o space" and no types were specified, don't display snapshots.
3023 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3024 types &= ~ZFS_TYPE_SNAPSHOT;
3027 * If the user specifies '-o all', the zprop_get_list() doesn't
3028 * normally include the name of the dataset. For 'zfs list', we always
3029 * want this property to be first.
3031 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3035 cb.cb_scripted = scripted;
3036 cb.cb_first = B_TRUE;
3038 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3039 limit, list_callback, &cb);
3041 zprop_free_list(cb.cb_proplist);
3042 zfs_free_sort_columns(sortcol);
3044 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3045 (void) fprintf(stderr, gettext("no datasets available\n"));
3051 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3052 * zfs rename [-f] -p <fs | vol> <fs | vol>
3053 * zfs rename -r <snap> <snap>
3055 * Renames the given dataset to another of the same type.
3057 * The '-p' flag creates all the non-existing ancestors of the target first.
3061 zfs_do_rename(int argc, char **argv)
3066 boolean_t recurse = B_FALSE;
3067 boolean_t parents = B_FALSE;
3068 boolean_t force_unmount = B_FALSE;
3071 while ((c = getopt(argc, argv, "prf")) != -1) {
3080 force_unmount = B_TRUE;
3084 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3093 /* check number of arguments */
3095 (void) fprintf(stderr, gettext("missing source dataset "
3100 (void) fprintf(stderr, gettext("missing target dataset "
3105 (void) fprintf(stderr, gettext("too many arguments\n"));
3109 if (recurse && parents) {
3110 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3115 if (recurse && strchr(argv[0], '@') == 0) {
3116 (void) fprintf(stderr, gettext("source dataset for recursive "
3117 "rename must be a snapshot\n"));
3121 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3122 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3125 /* If we were asked and the name looks good, try to create ancestors. */
3126 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3127 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3132 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3141 * Promotes the given clone fs to be the parent
3145 zfs_do_promote(int argc, char **argv)
3151 if (argc > 1 && argv[1][0] == '-') {
3152 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3157 /* check number of arguments */
3159 (void) fprintf(stderr, gettext("missing clone filesystem"
3164 (void) fprintf(stderr, gettext("too many arguments\n"));
3168 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3172 ret = (zfs_promote(zhp) != 0);
3180 * zfs rollback [-rRf] <snapshot>
3182 * -r Delete any intervening snapshots before doing rollback
3183 * -R Delete any snapshots and their clones
3184 * -f ignored for backwards compatability
3186 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3187 * since then and making it the active dataset. If more recent snapshots exist,
3188 * the command will complain unless the '-r' flag is given.
3190 typedef struct rollback_cbdata {
3196 boolean_t cb_recurse;
3197 boolean_t cb_dependent;
3198 } rollback_cbdata_t;
3201 * Report any snapshots more recent than the one specified. Used when '-r' is
3202 * not specified. We reuse this same callback for the snapshot dependents - if
3203 * 'cb_dependent' is set, then this is a dependent and we should report it
3204 * without checking the transaction group.
3207 rollback_check(zfs_handle_t *zhp, void *data)
3209 rollback_cbdata_t *cbp = data;
3211 if (cbp->cb_doclones) {
3216 if (!cbp->cb_dependent) {
3217 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
3218 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3219 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3222 if (cbp->cb_first && !cbp->cb_recurse) {
3223 (void) fprintf(stderr, gettext("cannot "
3224 "rollback to '%s': more recent snapshots "
3227 (void) fprintf(stderr, gettext("use '-r' to "
3228 "force deletion of the following "
3234 if (cbp->cb_recurse) {
3235 cbp->cb_dependent = B_TRUE;
3236 if (zfs_iter_dependents(zhp, B_TRUE,
3237 rollback_check, cbp) != 0) {
3241 cbp->cb_dependent = B_FALSE;
3243 (void) fprintf(stderr, "%s\n",
3248 if (cbp->cb_first && cbp->cb_recurse) {
3249 (void) fprintf(stderr, gettext("cannot rollback to "
3250 "'%s': clones of previous snapshots exist\n"),
3252 (void) fprintf(stderr, gettext("use '-R' to "
3253 "force deletion of the following clones and "
3259 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3267 zfs_do_rollback(int argc, char **argv)
3271 boolean_t force = B_FALSE;
3272 rollback_cbdata_t cb = { 0 };
3273 zfs_handle_t *zhp, *snap;
3274 char parentname[ZFS_MAXNAMELEN];
3278 while ((c = getopt(argc, argv, "rRf")) != -1) {
3291 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3300 /* check number of arguments */
3302 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3306 (void) fprintf(stderr, gettext("too many arguments\n"));
3310 /* open the snapshot */
3311 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3314 /* open the parent dataset */
3315 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3316 verify((delim = strrchr(parentname, '@')) != NULL);
3318 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3324 * Check for more recent snapshots and/or clones based on the presence
3327 cb.cb_target = argv[0];
3328 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3329 cb.cb_first = B_TRUE;
3331 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
3334 if ((ret = cb.cb_error) != 0)
3338 * Rollback parent to the given snapshot.
3340 ret = zfs_rollback(zhp, snap, force);
3353 * zfs set property=value { fs | snap | vol } ...
3355 * Sets the given property for all datasets specified on the command line.
3357 typedef struct set_cbdata {
3363 set_callback(zfs_handle_t *zhp, void *data)
3365 set_cbdata_t *cbp = data;
3367 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3368 switch (libzfs_errno(g_zfs)) {
3369 case EZFS_MOUNTFAILED:
3370 (void) fprintf(stderr, gettext("property may be set "
3371 "but unable to remount filesystem\n"));
3373 case EZFS_SHARENFSFAILED:
3374 (void) fprintf(stderr, gettext("property may be set "
3375 "but unable to reshare filesystem\n"));
3384 zfs_do_set(int argc, char **argv)
3389 /* check for options */
3390 if (argc > 1 && argv[1][0] == '-') {
3391 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3396 /* check number of arguments */
3398 (void) fprintf(stderr, gettext("missing property=value "
3403 (void) fprintf(stderr, gettext("missing dataset name\n"));
3407 /* validate property=value argument */
3408 cb.cb_propname = argv[1];
3409 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3410 (cb.cb_value[1] == '\0')) {
3411 (void) fprintf(stderr, gettext("missing value in "
3412 "property=value argument\n"));
3416 *cb.cb_value = '\0';
3419 if (*cb.cb_propname == '\0') {
3420 (void) fprintf(stderr,
3421 gettext("missing property in property=value argument\n"));
3425 ret = zfs_for_each(argc - 2, argv + 2, 0,
3426 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3431 typedef struct snap_cbdata {
3433 boolean_t sd_recursive;
3434 const char *sd_snapname;
3438 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3440 snap_cbdata_t *sd = arg;
3445 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3448 fnvlist_add_boolean(sd->sd_nvl, name);
3451 if (sd->sd_recursive)
3452 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3458 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3460 * Creates a snapshot with the given name. While functionally equivalent to
3461 * 'zfs create', it is a separate command to differentiate intent.
3464 zfs_do_snapshot(int argc, char **argv)
3469 snap_cbdata_t sd = { 0 };
3470 boolean_t multiple_snaps = B_FALSE;
3472 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3474 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3478 while ((c = getopt(argc, argv, "ro:")) != -1) {
3481 if (parseprop(props))
3485 sd.sd_recursive = B_TRUE;
3486 multiple_snaps = B_TRUE;
3489 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3498 /* check number of arguments */
3500 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3505 multiple_snaps = B_TRUE;
3506 for (; argc > 0; argc--, argv++) {
3510 atp = strchr(argv[0], '@');
3514 sd.sd_snapname = atp + 1;
3515 zhp = zfs_open(g_zfs, argv[0],
3516 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3519 if (zfs_snapshot_cb(zhp, &sd) != 0)
3523 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3524 nvlist_free(sd.sd_nvl);
3526 if (ret != 0 && multiple_snaps)
3527 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3531 nvlist_free(sd.sd_nvl);
3538 * Send a backup stream to stdout.
3541 zfs_do_send(int argc, char **argv)
3543 char *fromname = NULL;
3544 char *toname = NULL;
3547 sendflags_t flags = { 0 };
3549 nvlist_t *dbgnv = NULL;
3550 boolean_t extraverbose = B_FALSE;
3553 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3564 flags.doall = B_TRUE;
3567 flags.replicate = B_TRUE;
3570 flags.props = B_TRUE;
3573 flags.parsable = B_TRUE;
3574 flags.verbose = B_TRUE;
3578 extraverbose = B_TRUE;
3579 flags.verbose = B_TRUE;
3580 flags.progress = B_TRUE;
3583 flags.dedup = B_TRUE;
3586 flags.dryrun = B_TRUE;
3589 (void) fprintf(stderr, gettext("missing argument for "
3590 "'%c' option\n"), optopt);
3594 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3603 /* check number of arguments */
3605 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3609 (void) fprintf(stderr, gettext("too many arguments\n"));
3613 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3614 (void) fprintf(stderr,
3615 gettext("Error: Stream can not be written to a terminal.\n"
3616 "You must redirect standard output.\n"));
3620 cp = strchr(argv[0], '@');
3622 (void) fprintf(stderr,
3623 gettext("argument must be a snapshot\n"));
3628 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3633 * If they specified the full path to the snapshot, chop off
3634 * everything except the short name of the snapshot, but special
3635 * case if they specify the origin.
3637 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3638 char origin[ZFS_MAXNAMELEN];
3641 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3642 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3644 if (strcmp(origin, fromname) == 0) {
3646 flags.fromorigin = B_TRUE;
3649 if (cp != fromname && strcmp(argv[0], fromname)) {
3650 (void) fprintf(stderr,
3651 gettext("incremental source must be "
3652 "in same filesystem\n"));
3656 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3657 (void) fprintf(stderr,
3658 gettext("invalid incremental source\n"));
3664 if (flags.replicate && fromname == NULL)
3665 flags.doall = B_TRUE;
3667 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3668 extraverbose ? &dbgnv : NULL);
3670 if (extraverbose && dbgnv != NULL) {
3672 * dump_nvlist prints to stdout, but that's been
3673 * redirected to a file. Make it print to stderr
3676 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3677 dump_nvlist(dbgnv, 0);
3686 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3688 * Restore a backup stream from stdin.
3691 zfs_do_receive(int argc, char **argv)
3694 recvflags_t flags = { 0 };
3697 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3700 flags.isprefix = B_TRUE;
3703 flags.isprefix = B_TRUE;
3704 flags.istail = B_TRUE;
3707 flags.dryrun = B_TRUE;
3710 flags.nomount = B_TRUE;
3713 flags.verbose = B_TRUE;
3716 flags.force = B_TRUE;
3719 (void) fprintf(stderr, gettext("missing argument for "
3720 "'%c' option\n"), optopt);
3724 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3733 /* check number of arguments */
3735 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3739 (void) fprintf(stderr, gettext("too many arguments\n"));
3743 if (isatty(STDIN_FILENO)) {
3744 (void) fprintf(stderr,
3745 gettext("Error: Backup stream can not be read "
3746 "from a terminal.\n"
3747 "You must redirect standard input.\n"));
3751 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3757 * allow/unallow stuff
3759 /* copied from zfs/sys/dsl_deleg.h */
3760 #define ZFS_DELEG_PERM_CREATE "create"
3761 #define ZFS_DELEG_PERM_DESTROY "destroy"
3762 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3763 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3764 #define ZFS_DELEG_PERM_CLONE "clone"
3765 #define ZFS_DELEG_PERM_PROMOTE "promote"
3766 #define ZFS_DELEG_PERM_RENAME "rename"
3767 #define ZFS_DELEG_PERM_MOUNT "mount"
3768 #define ZFS_DELEG_PERM_SHARE "share"
3769 #define ZFS_DELEG_PERM_SEND "send"
3770 #define ZFS_DELEG_PERM_RECEIVE "receive"
3771 #define ZFS_DELEG_PERM_ALLOW "allow"
3772 #define ZFS_DELEG_PERM_USERPROP "userprop"
3773 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3774 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3775 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3776 #define ZFS_DELEG_PERM_USERUSED "userused"
3777 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3778 #define ZFS_DELEG_PERM_HOLD "hold"
3779 #define ZFS_DELEG_PERM_RELEASE "release"
3780 #define ZFS_DELEG_PERM_DIFF "diff"
3782 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3784 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3785 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3786 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3787 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3788 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3789 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3790 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3791 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3792 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3793 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3794 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3795 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3796 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3797 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3798 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3799 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3801 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3802 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3803 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3804 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3805 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3806 { NULL, ZFS_DELEG_NOTE_NONE }
3809 /* permission structure */
3810 typedef struct deleg_perm {
3811 zfs_deleg_who_type_t dp_who_type;
3812 const char *dp_name;
3814 boolean_t dp_descend;
3818 typedef struct deleg_perm_node {
3819 deleg_perm_t dpn_perm;
3821 uu_avl_node_t dpn_avl_node;
3822 } deleg_perm_node_t;
3824 typedef struct fs_perm fs_perm_t;
3826 /* permissions set */
3827 typedef struct who_perm {
3828 zfs_deleg_who_type_t who_type;
3829 const char *who_name; /* id */
3830 char who_ug_name[256]; /* user/group name */
3831 fs_perm_t *who_fsperm; /* uplink */
3833 uu_avl_t *who_deleg_perm_avl; /* permissions */
3837 typedef struct who_perm_node {
3838 who_perm_t who_perm;
3839 uu_avl_node_t who_avl_node;
3842 typedef struct fs_perm_set fs_perm_set_t;
3843 /* fs permissions */
3845 const char *fsp_name;
3847 uu_avl_t *fsp_sc_avl; /* sets,create */
3848 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3850 fs_perm_set_t *fsp_set; /* uplink */
3854 typedef struct fs_perm_node {
3855 fs_perm_t fspn_fsperm;
3858 uu_list_node_t fspn_list_node;
3861 /* top level structure */
3862 struct fs_perm_set {
3863 uu_list_pool_t *fsps_list_pool;
3864 uu_list_t *fsps_list; /* list of fs_perms */
3866 uu_avl_pool_t *fsps_named_set_avl_pool;
3867 uu_avl_pool_t *fsps_who_perm_avl_pool;
3868 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
3871 static inline const char *
3872 deleg_perm_type(zfs_deleg_note_t note)
3878 case ZFS_DELEG_NOTE_GROUPQUOTA:
3879 case ZFS_DELEG_NOTE_GROUPUSED:
3880 case ZFS_DELEG_NOTE_USERPROP:
3881 case ZFS_DELEG_NOTE_USERQUOTA:
3882 case ZFS_DELEG_NOTE_USERUSED:
3884 return (gettext("other"));
3886 return (gettext("subcommand"));
3891 who_type2weight(zfs_deleg_who_type_t who_type)
3895 case ZFS_DELEG_NAMED_SET_SETS:
3896 case ZFS_DELEG_NAMED_SET:
3899 case ZFS_DELEG_CREATE_SETS:
3900 case ZFS_DELEG_CREATE:
3903 case ZFS_DELEG_USER_SETS:
3904 case ZFS_DELEG_USER:
3907 case ZFS_DELEG_GROUP_SETS:
3908 case ZFS_DELEG_GROUP:
3911 case ZFS_DELEG_EVERYONE_SETS:
3912 case ZFS_DELEG_EVERYONE:
3924 who_perm_compare(const void *larg, const void *rarg, void *unused)
3926 const who_perm_node_t *l = larg;
3927 const who_perm_node_t *r = rarg;
3928 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
3929 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
3930 int lweight = who_type2weight(ltype);
3931 int rweight = who_type2weight(rtype);
3932 int res = lweight - rweight;
3934 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
3935 ZFS_MAX_DELEG_NAME-1);
3947 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
3949 const deleg_perm_node_t *l = larg;
3950 const deleg_perm_node_t *r = rarg;
3951 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
3952 ZFS_MAX_DELEG_NAME-1);
3964 fs_perm_set_init(fs_perm_set_t *fspset)
3966 bzero(fspset, sizeof (fs_perm_set_t));
3968 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
3969 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
3970 NULL, UU_DEFAULT)) == NULL)
3972 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
3973 UU_DEFAULT)) == NULL)
3976 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
3977 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
3978 who_perm_node_t, who_avl_node), who_perm_compare,
3979 UU_DEFAULT)) == NULL)
3982 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
3983 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
3984 who_perm_node_t, who_avl_node), who_perm_compare,
3985 UU_DEFAULT)) == NULL)
3988 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
3989 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
3990 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
3995 static inline void fs_perm_fini(fs_perm_t *);
3996 static inline void who_perm_fini(who_perm_t *);
3999 fs_perm_set_fini(fs_perm_set_t *fspset)
4001 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4003 while (node != NULL) {
4004 fs_perm_node_t *next_node =
4005 uu_list_next(fspset->fsps_list, node);
4006 fs_perm_t *fsperm = &node->fspn_fsperm;
4007 fs_perm_fini(fsperm);
4008 uu_list_remove(fspset->fsps_list, node);
4013 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4014 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4015 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4019 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4022 deleg_perm->dp_who_type = type;
4023 deleg_perm->dp_name = name;
4027 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4028 zfs_deleg_who_type_t type, const char *name)
4030 uu_avl_pool_t *pool;
4031 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4033 bzero(who_perm, sizeof (who_perm_t));
4035 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4036 UU_DEFAULT)) == NULL)
4039 who_perm->who_type = type;
4040 who_perm->who_name = name;
4041 who_perm->who_fsperm = fsperm;
4045 who_perm_fini(who_perm_t *who_perm)
4047 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4049 while (node != NULL) {
4050 deleg_perm_node_t *next_node =
4051 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4053 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4058 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4062 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4064 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4065 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4067 bzero(fsperm, sizeof (fs_perm_t));
4069 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4073 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4077 fsperm->fsp_set = fspset;
4078 fsperm->fsp_name = fsname;
4082 fs_perm_fini(fs_perm_t *fsperm)
4084 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4085 while (node != NULL) {
4086 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4088 who_perm_t *who_perm = &node->who_perm;
4089 who_perm_fini(who_perm);
4090 uu_avl_remove(fsperm->fsp_sc_avl, node);
4095 node = uu_avl_first(fsperm->fsp_uge_avl);
4096 while (node != NULL) {
4097 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4099 who_perm_t *who_perm = &node->who_perm;
4100 who_perm_fini(who_perm);
4101 uu_avl_remove(fsperm->fsp_uge_avl, node);
4106 uu_avl_destroy(fsperm->fsp_sc_avl);
4107 uu_avl_destroy(fsperm->fsp_uge_avl);
4111 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4112 zfs_deleg_who_type_t who_type, const char *name, char locality)
4114 uu_avl_index_t idx = 0;
4116 deleg_perm_node_t *found_node = NULL;
4117 deleg_perm_t *deleg_perm = &node->dpn_perm;
4119 deleg_perm_init(deleg_perm, who_type, name);
4121 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4123 uu_avl_insert(avl, node, idx);
4126 deleg_perm = &node->dpn_perm;
4131 case ZFS_DELEG_LOCAL:
4132 deleg_perm->dp_local = B_TRUE;
4134 case ZFS_DELEG_DESCENDENT:
4135 deleg_perm->dp_descend = B_TRUE;
4140 assert(B_FALSE); /* invalid locality */
4145 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4147 nvpair_t *nvp = NULL;
4148 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4149 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4150 zfs_deleg_who_type_t who_type = who_perm->who_type;
4152 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4153 const char *name = nvpair_name(nvp);
4154 data_type_t type = nvpair_type(nvp);
4155 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4156 deleg_perm_node_t *node =
4157 safe_malloc(sizeof (deleg_perm_node_t));
4159 VERIFY(type == DATA_TYPE_BOOLEAN);
4161 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4162 set_deleg_perm_node(avl, node, who_type, name, locality);
4169 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4171 nvpair_t *nvp = NULL;
4172 fs_perm_set_t *fspset = fsperm->fsp_set;
4174 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4175 nvlist_t *nvl2 = NULL;
4176 const char *name = nvpair_name(nvp);
4177 uu_avl_t *avl = NULL;
4178 uu_avl_pool_t *avl_pool = NULL;
4179 zfs_deleg_who_type_t perm_type = name[0];
4180 char perm_locality = name[1];
4181 const char *perm_name = name + 3;
4182 boolean_t is_set = B_TRUE;
4183 who_perm_t *who_perm = NULL;
4185 assert('$' == name[2]);
4187 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4190 switch (perm_type) {
4191 case ZFS_DELEG_CREATE:
4192 case ZFS_DELEG_CREATE_SETS:
4193 case ZFS_DELEG_NAMED_SET:
4194 case ZFS_DELEG_NAMED_SET_SETS:
4195 avl_pool = fspset->fsps_named_set_avl_pool;
4196 avl = fsperm->fsp_sc_avl;
4198 case ZFS_DELEG_USER:
4199 case ZFS_DELEG_USER_SETS:
4200 case ZFS_DELEG_GROUP:
4201 case ZFS_DELEG_GROUP_SETS:
4202 case ZFS_DELEG_EVERYONE:
4203 case ZFS_DELEG_EVERYONE_SETS:
4204 avl_pool = fspset->fsps_who_perm_avl_pool;
4205 avl = fsperm->fsp_uge_avl;
4212 who_perm_node_t *found_node = NULL;
4213 who_perm_node_t *node = safe_malloc(
4214 sizeof (who_perm_node_t));
4215 who_perm = &node->who_perm;
4216 uu_avl_index_t idx = 0;
4218 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4219 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4221 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4223 if (avl == fsperm->fsp_uge_avl) {
4225 struct passwd *p = NULL;
4226 struct group *g = NULL;
4227 const char *nice_name = NULL;
4229 switch (perm_type) {
4230 case ZFS_DELEG_USER_SETS:
4231 case ZFS_DELEG_USER:
4232 rid = atoi(perm_name);
4235 nice_name = p->pw_name;
4237 case ZFS_DELEG_GROUP_SETS:
4238 case ZFS_DELEG_GROUP:
4239 rid = atoi(perm_name);
4242 nice_name = g->gr_name;
4248 if (nice_name != NULL)
4250 node->who_perm.who_ug_name,
4254 uu_avl_insert(avl, node, idx);
4257 who_perm = &node->who_perm;
4261 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4268 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4270 nvpair_t *nvp = NULL;
4271 uu_avl_index_t idx = 0;
4273 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4274 nvlist_t *nvl2 = NULL;
4275 const char *fsname = nvpair_name(nvp);
4276 data_type_t type = nvpair_type(nvp);
4277 fs_perm_t *fsperm = NULL;
4278 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4282 fsperm = &node->fspn_fsperm;
4284 VERIFY(DATA_TYPE_NVLIST == type);
4286 uu_list_node_init(node, &node->fspn_list_node,
4287 fspset->fsps_list_pool);
4289 idx = uu_list_numnodes(fspset->fsps_list);
4290 fs_perm_init(fsperm, fspset, fsname);
4292 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4295 (void) parse_fs_perm(fsperm, nvl2);
4297 uu_list_insert(fspset->fsps_list, node, idx);
4303 static inline const char *
4304 deleg_perm_comment(zfs_deleg_note_t note)
4306 const char *str = "";
4311 case ZFS_DELEG_NOTE_ALLOW:
4312 str = gettext("Must also have the permission that is being"
4313 "\n\t\t\t\tallowed");
4315 case ZFS_DELEG_NOTE_CLONE:
4316 str = gettext("Must also have the 'create' ability and 'mount'"
4317 "\n\t\t\t\tability in the origin file system");
4319 case ZFS_DELEG_NOTE_CREATE:
4320 str = gettext("Must also have the 'mount' ability");
4322 case ZFS_DELEG_NOTE_DESTROY:
4323 str = gettext("Must also have the 'mount' ability");
4325 case ZFS_DELEG_NOTE_DIFF:
4326 str = gettext("Allows lookup of paths within a dataset;"
4327 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4328 "\n\t\t\t\tin order to use zfs diff");
4330 case ZFS_DELEG_NOTE_HOLD:
4331 str = gettext("Allows adding a user hold to a snapshot");
4333 case ZFS_DELEG_NOTE_MOUNT:
4334 str = gettext("Allows mount/umount of ZFS datasets");
4336 case ZFS_DELEG_NOTE_PROMOTE:
4337 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4338 " 'promote' ability in the origin file system");
4340 case ZFS_DELEG_NOTE_RECEIVE:
4341 str = gettext("Must also have the 'mount' and 'create'"
4344 case ZFS_DELEG_NOTE_RELEASE:
4345 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4346 "might destroy the snapshot");
4348 case ZFS_DELEG_NOTE_RENAME:
4349 str = gettext("Must also have the 'mount' and 'create'"
4350 "\n\t\t\t\tability in the new parent");
4352 case ZFS_DELEG_NOTE_ROLLBACK:
4355 case ZFS_DELEG_NOTE_SEND:
4358 case ZFS_DELEG_NOTE_SHARE:
4359 str = gettext("Allows sharing file systems over NFS or SMB"
4360 "\n\t\t\t\tprotocols");
4362 case ZFS_DELEG_NOTE_SNAPSHOT:
4366 * case ZFS_DELEG_NOTE_VSCAN:
4367 * str = gettext("");
4371 case ZFS_DELEG_NOTE_GROUPQUOTA:
4372 str = gettext("Allows accessing any groupquota@... property");
4374 case ZFS_DELEG_NOTE_GROUPUSED:
4375 str = gettext("Allows reading any groupused@... property");
4377 case ZFS_DELEG_NOTE_USERPROP:
4378 str = gettext("Allows changing any user property");
4380 case ZFS_DELEG_NOTE_USERQUOTA:
4381 str = gettext("Allows accessing any userquota@... property");
4383 case ZFS_DELEG_NOTE_USERUSED:
4384 str = gettext("Allows reading any userused@... property");
4402 boolean_t recursive; /* unallow only */
4403 boolean_t prt_usage;
4405 boolean_t prt_perms;
4408 const char *dataset;
4412 prop_cmp(const void *a, const void *b)
4414 const char *str1 = *(const char **)a;
4415 const char *str2 = *(const char **)b;
4416 return (strcmp(str1, str2));
4420 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4422 const char *opt_desc[] = {
4423 "-h", gettext("show this help message and exit"),
4424 "-l", gettext("set permission locally"),
4425 "-d", gettext("set permission for descents"),
4426 "-u", gettext("set permission for user"),
4427 "-g", gettext("set permission for group"),
4428 "-e", gettext("set permission for everyone"),
4429 "-c", gettext("set create time permission"),
4430 "-s", gettext("define permission set"),
4432 "-r", gettext("remove permissions recursively"),
4434 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4435 size_t allow_size = unallow_size - 2;
4436 const char *props[ZFS_NUM_PROPS];
4439 FILE *fp = requested ? stdout : stderr;
4440 zprop_desc_t *pdtbl = zfs_prop_get_table();
4441 const char *fmt = gettext("%-16s %-14s\t%s\n");
4443 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4445 (void) fprintf(fp, gettext("Options:\n"));
4446 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4447 const char *opt = opt_desc[i++];
4448 const char *optdsc = opt_desc[i];
4449 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4452 (void) fprintf(fp, gettext("\nThe following permissions are "
4454 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4456 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4457 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4458 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4459 const char *perm_type = deleg_perm_type(perm_note);
4460 const char *perm_comment = deleg_perm_comment(perm_note);
4461 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4464 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4465 zprop_desc_t *pd = &pdtbl[i];
4466 if (pd->pd_visible != B_TRUE)
4469 if (pd->pd_attr == PROP_READONLY)
4472 props[count++] = pd->pd_name;
4474 props[count] = NULL;
4476 qsort(props, count, sizeof (char *), prop_cmp);
4478 for (i = 0; i < count; i++)
4479 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4482 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4484 exit(requested ? 0 : 2);
4487 static inline const char *
4488 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4491 if (un && argc == expected_argc - 1)
4493 else if (argc == expected_argc)
4494 *permsp = argv[argc - 2];
4496 allow_usage(un, B_FALSE,
4497 gettext("wrong number of parameters\n"));
4499 return (argv[argc - 1]);
4503 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4505 int uge_sum = opts->user + opts->group + opts->everyone;
4506 int csuge_sum = opts->create + opts->set + uge_sum;
4507 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4508 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4511 allow_usage(un, B_FALSE,
4512 gettext("-u, -g, and -e are mutually exclusive\n"));
4514 if (opts->prt_usage) {
4515 if (argc == 0 && all_sum == 0)
4516 allow_usage(un, B_TRUE, NULL);
4523 allow_usage(un, B_FALSE,
4524 gettext("invalid options combined with -s\n"));
4526 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4527 if (argv[0][0] != '@')
4528 allow_usage(un, B_FALSE,
4529 gettext("invalid set name: missing '@' prefix\n"));
4530 opts->who = argv[0];
4531 } else if (opts->create) {
4532 if (ldcsuge_sum > 1)
4533 allow_usage(un, B_FALSE,
4534 gettext("invalid options combined with -c\n"));
4535 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4536 } else if (opts->everyone) {
4538 allow_usage(un, B_FALSE,
4539 gettext("invalid options combined with -e\n"));
4540 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4541 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4543 opts->everyone = B_TRUE;
4546 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4547 } else if (argc == 1 && !un) {
4548 opts->prt_perms = B_TRUE;
4549 opts->dataset = argv[argc-1];
4551 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4552 opts->who = argv[0];
4555 if (!opts->local && !opts->descend) {
4556 opts->local = B_TRUE;
4557 opts->descend = B_TRUE;
4562 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4563 const char *who, char *perms, nvlist_t *top_nvl)
4566 char ld[2] = { '\0', '\0' };
4567 char who_buf[ZFS_MAXNAMELEN+32];
4568 char base_type = ZFS_DELEG_WHO_UNKNOWN;
4569 char set_type = ZFS_DELEG_WHO_UNKNOWN;
4570 nvlist_t *base_nvl = NULL;
4571 nvlist_t *set_nvl = NULL;
4574 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4576 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4580 case ZFS_DELEG_NAMED_SET_SETS:
4581 case ZFS_DELEG_NAMED_SET:
4582 set_type = ZFS_DELEG_NAMED_SET_SETS;
4583 base_type = ZFS_DELEG_NAMED_SET;
4584 ld[0] = ZFS_DELEG_NA;
4586 case ZFS_DELEG_CREATE_SETS:
4587 case ZFS_DELEG_CREATE:
4588 set_type = ZFS_DELEG_CREATE_SETS;
4589 base_type = ZFS_DELEG_CREATE;
4590 ld[0] = ZFS_DELEG_NA;
4592 case ZFS_DELEG_USER_SETS:
4593 case ZFS_DELEG_USER:
4594 set_type = ZFS_DELEG_USER_SETS;
4595 base_type = ZFS_DELEG_USER;
4597 ld[0] = ZFS_DELEG_LOCAL;
4599 ld[1] = ZFS_DELEG_DESCENDENT;
4601 case ZFS_DELEG_GROUP_SETS:
4602 case ZFS_DELEG_GROUP:
4603 set_type = ZFS_DELEG_GROUP_SETS;
4604 base_type = ZFS_DELEG_GROUP;
4606 ld[0] = ZFS_DELEG_LOCAL;
4608 ld[1] = ZFS_DELEG_DESCENDENT;
4610 case ZFS_DELEG_EVERYONE_SETS:
4611 case ZFS_DELEG_EVERYONE:
4612 set_type = ZFS_DELEG_EVERYONE_SETS;
4613 base_type = ZFS_DELEG_EVERYONE;
4615 ld[0] = ZFS_DELEG_LOCAL;
4617 ld[1] = ZFS_DELEG_DESCENDENT;
4622 if (perms != NULL) {
4624 char *end = curr + strlen(perms);
4626 while (curr < end) {
4627 char *delim = strchr(curr, ',');
4638 (void) nvlist_add_boolean(nvl, curr);
4644 for (i = 0; i < 2; i++) {
4645 char locality = ld[i];
4649 if (!nvlist_empty(base_nvl)) {
4651 (void) snprintf(who_buf,
4652 sizeof (who_buf), "%c%c$%s",
4653 base_type, locality, who);
4655 (void) snprintf(who_buf,
4656 sizeof (who_buf), "%c%c$",
4657 base_type, locality);
4659 (void) nvlist_add_nvlist(top_nvl, who_buf,
4664 if (!nvlist_empty(set_nvl)) {
4666 (void) snprintf(who_buf,
4667 sizeof (who_buf), "%c%c$%s",
4668 set_type, locality, who);
4670 (void) snprintf(who_buf,
4671 sizeof (who_buf), "%c%c$",
4672 set_type, locality);
4674 (void) nvlist_add_nvlist(top_nvl, who_buf,
4679 for (i = 0; i < 2; i++) {
4680 char locality = ld[i];
4685 (void) snprintf(who_buf, sizeof (who_buf),
4686 "%c%c$%s", base_type, locality, who);
4688 (void) snprintf(who_buf, sizeof (who_buf),
4689 "%c%c$", base_type, locality);
4690 (void) nvlist_add_boolean(top_nvl, who_buf);
4693 (void) snprintf(who_buf, sizeof (who_buf),
4694 "%c%c$%s", set_type, locality, who);
4696 (void) snprintf(who_buf, sizeof (who_buf),
4697 "%c%c$", set_type, locality);
4698 (void) nvlist_add_boolean(top_nvl, who_buf);
4704 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4706 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4710 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4711 opts->descend, opts->who, opts->perms, *nvlp);
4712 } else if (opts->create) {
4713 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4714 opts->descend, NULL, opts->perms, *nvlp);
4715 } else if (opts->everyone) {
4716 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4717 opts->descend, NULL, opts->perms, *nvlp);
4719 char *curr = opts->who;
4720 char *end = curr + strlen(curr);
4722 while (curr < end) {
4724 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
4726 char *delim = strchr(curr, ',');
4729 struct passwd *p = NULL;
4730 struct group *g = NULL;
4738 rid = (uid_t)strtol(curr, &endch, 0);
4740 who_type = ZFS_DELEG_USER;
4749 (void) snprintf(errbuf, 256, gettext(
4750 "invalid user %s"), curr);
4751 allow_usage(un, B_TRUE, errbuf);
4753 } else if (opts->group) {
4754 who_type = ZFS_DELEG_GROUP;
4763 (void) snprintf(errbuf, 256, gettext(
4764 "invalid group %s"), curr);
4765 allow_usage(un, B_TRUE, errbuf);
4768 if (*endch != '\0') {
4775 if (*endch != '\0') {
4783 who_type = ZFS_DELEG_USER;
4785 } else if (g != NULL) {
4786 who_type = ZFS_DELEG_GROUP;
4789 (void) snprintf(errbuf, 256, gettext(
4790 "invalid user/group %s"), curr);
4791 allow_usage(un, B_TRUE, errbuf);
4795 (void) sprintf(id, "%u", rid);
4798 store_allow_perm(who_type, opts->local,
4799 opts->descend, who, opts->perms, *nvlp);
4808 print_set_creat_perms(uu_avl_t *who_avl)
4810 const char *sc_title[] = {
4811 gettext("Permission sets:\n"),
4812 gettext("Create time permissions:\n"),
4815 const char **title_ptr = sc_title;
4816 who_perm_node_t *who_node = NULL;
4817 int prev_weight = -1;
4819 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4820 who_node = uu_avl_next(who_avl, who_node)) {
4821 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4822 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4823 const char *who_name = who_node->who_perm.who_name;
4824 int weight = who_type2weight(who_type);
4825 boolean_t first = B_TRUE;
4826 deleg_perm_node_t *deleg_node;
4828 if (prev_weight != weight) {
4829 (void) printf("%s", *title_ptr++);
4830 prev_weight = weight;
4833 if (who_name == NULL || strnlen(who_name, 1) == 0)
4834 (void) printf("\t");
4836 (void) printf("\t%s ", who_name);
4838 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4839 deleg_node = uu_avl_next(avl, deleg_node)) {
4842 deleg_node->dpn_perm.dp_name);
4845 (void) printf(",%s",
4846 deleg_node->dpn_perm.dp_name);
4849 (void) printf("\n");
4854 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4857 who_perm_node_t *who_node = NULL;
4858 boolean_t prt_title = B_TRUE;
4859 uu_avl_walk_t *walk;
4861 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4864 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4865 const char *who_name = who_node->who_perm.who_name;
4866 const char *nice_who_name = who_node->who_perm.who_ug_name;
4867 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4868 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4870 deleg_perm_node_t *deleg_node;
4871 boolean_t prt_who = B_TRUE;
4873 for (deleg_node = uu_avl_first(avl);
4875 deleg_node = uu_avl_next(avl, deleg_node)) {
4876 if (local != deleg_node->dpn_perm.dp_local ||
4877 descend != deleg_node->dpn_perm.dp_descend)
4881 const char *who = NULL;
4883 prt_title = B_FALSE;
4884 (void) printf("%s", title);
4888 case ZFS_DELEG_USER_SETS:
4889 case ZFS_DELEG_USER:
4890 who = gettext("user");
4892 who_name = nice_who_name;
4894 case ZFS_DELEG_GROUP_SETS:
4895 case ZFS_DELEG_GROUP:
4896 who = gettext("group");
4898 who_name = nice_who_name;
4900 case ZFS_DELEG_EVERYONE_SETS:
4901 case ZFS_DELEG_EVERYONE:
4902 who = gettext("everyone");
4909 if (who_name == NULL)
4910 (void) printf("\t%s", who);
4912 (void) printf("\t%s %s", who, who_name);
4915 (void) printf("%c%s", delim,
4916 deleg_node->dpn_perm.dp_name);
4921 (void) printf("\n");
4924 uu_avl_walk_end(walk);
4928 print_fs_perms(fs_perm_set_t *fspset)
4930 fs_perm_node_t *node = NULL;
4931 char buf[ZFS_MAXNAMELEN+32];
4932 const char *dsname = buf;
4934 for (node = uu_list_first(fspset->fsps_list); node != NULL;
4935 node = uu_list_next(fspset->fsps_list, node)) {
4936 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
4937 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
4940 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
4941 gettext("---- Permissions on %s "),
4942 node->fspn_fsperm.fsp_name);
4943 (void) printf("%s", dsname);
4944 left = 70 - strlen(buf);
4947 (void) printf("\n");
4949 print_set_creat_perms(sc_avl);
4950 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
4951 gettext("Local permissions:\n"));
4952 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
4953 gettext("Descendent permissions:\n"));
4954 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
4955 gettext("Local+Descendent permissions:\n"));
4959 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
4961 struct deleg_perms {
4967 set_deleg_perms(zfs_handle_t *zhp, void *data)
4969 struct deleg_perms *perms = (struct deleg_perms *)data;
4970 zfs_type_t zfs_type = zfs_get_type(zhp);
4972 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
4975 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
4979 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
4982 nvlist_t *perm_nvl = NULL;
4983 nvlist_t *update_perm_nvl = NULL;
4986 struct allow_opts opts = { 0 };
4988 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
4991 while ((c = getopt(argc, argv, optstr)) != -1) {
4994 opts.local = B_TRUE;
4997 opts.descend = B_TRUE;
5003 opts.group = B_TRUE;
5006 opts.everyone = B_TRUE;
5012 opts.create = B_TRUE;
5015 opts.recursive = B_TRUE;
5018 (void) fprintf(stderr, gettext("missing argument for "
5019 "'%c' option\n"), optopt);
5023 opts.prt_usage = B_TRUE;
5026 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5035 /* check arguments */
5036 parse_allow_args(argc, argv, un, &opts);
5038 /* try to open the dataset */
5039 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5040 ZFS_TYPE_VOLUME)) == NULL) {
5041 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5046 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5049 fs_perm_set_init(&fs_perm_set);
5050 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5051 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5056 print_fs_perms(&fs_perm_set);
5058 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5059 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5062 if (un && opts.recursive) {
5063 struct deleg_perms data = { un, update_perm_nvl };
5064 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5073 nvlist_free(perm_nvl);
5074 if (update_perm_nvl != NULL)
5075 nvlist_free(update_perm_nvl);
5077 fs_perm_set_fini(&fs_perm_set);
5085 * zfs allow [-r] [-t] <tag> <snap> ...
5087 * -r Recursively hold
5088 * -t Temporary hold (hidden option)
5090 * Apply a user-hold with the given tag to the list of snapshots.
5093 zfs_do_allow(int argc, char **argv)
5095 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5099 * zfs unallow [-r] [-t] <tag> <snap> ...
5101 * -r Recursively hold
5102 * -t Temporary hold (hidden option)
5104 * Apply a user-hold with the given tag to the list of snapshots.
5107 zfs_do_unallow(int argc, char **argv)
5109 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5113 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5118 boolean_t recursive = B_FALSE;
5119 boolean_t temphold = B_FALSE;
5120 const char *opts = holding ? "rt" : "r";
5124 while ((c = getopt(argc, argv, opts)) != -1) {
5133 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5142 /* check number of arguments */
5150 if (holding && tag[0] == '.') {
5151 /* tags starting with '.' are reserved for libzfs */
5152 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5156 for (i = 0; i < argc; ++i) {
5158 char parent[ZFS_MAXNAMELEN];
5160 char *path = argv[i];
5162 delim = strchr(path, '@');
5163 if (delim == NULL) {
5164 (void) fprintf(stderr,
5165 gettext("'%s' is not a snapshot\n"), path);
5169 (void) strncpy(parent, path, delim - path);
5170 parent[delim - path] = '\0';
5172 zhp = zfs_open(g_zfs, parent,
5173 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5179 if (zfs_hold(zhp, delim+1, tag, recursive,
5180 temphold, B_FALSE, -1, 0, 0) != 0)
5183 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5189 return (errors != 0);
5193 * zfs hold [-r] [-t] <tag> <snap> ...
5195 * -r Recursively hold
5196 * -t Temporary hold (hidden option)
5198 * Apply a user-hold with the given tag to the list of snapshots.
5201 zfs_do_hold(int argc, char **argv)
5203 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5207 * zfs release [-r] <tag> <snap> ...
5209 * -r Recursively release
5211 * Release a user-hold with the given tag from the list of snapshots.
5214 zfs_do_release(int argc, char **argv)
5216 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5219 typedef struct holds_cbdata {
5220 boolean_t cb_recursive;
5221 const char *cb_snapname;
5223 size_t cb_max_namelen;
5224 size_t cb_max_taglen;
5227 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5228 #define DATETIME_BUF_LEN (32)
5233 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
5236 nvpair_t *nvp = NULL;
5237 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5241 for (i = 0; i < 3; i++) {
5242 col = gettext(hdr_cols[i]);
5244 (void) printf("%-*s ", i ? tagwidth : nwidth,
5247 (void) printf("%s\n", col);
5251 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5252 char *zname = nvpair_name(nvp);
5254 nvpair_t *nvp2 = NULL;
5255 (void) nvpair_value_nvlist(nvp, &nvl2);
5256 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5257 char tsbuf[DATETIME_BUF_LEN];
5258 char *tagname = nvpair_name(nvp2);
5262 char sep = scripted ? '\t' : ' ';
5263 int sepnum = scripted ? 1 : 2;
5265 (void) nvpair_value_uint64(nvp2, &val);
5267 (void) localtime_r(&time, &t);
5268 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5269 gettext(STRFTIME_FMT_STR), &t);
5271 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5272 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5278 * Generic callback function to list a dataset or snapshot.
5281 holds_callback(zfs_handle_t *zhp, void *data)
5283 holds_cbdata_t *cbp = data;
5284 nvlist_t *top_nvl = *cbp->cb_nvlp;
5285 nvlist_t *nvl = NULL;
5286 nvpair_t *nvp = NULL;
5287 const char *zname = zfs_get_name(zhp);
5288 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5290 if (cbp->cb_recursive) {
5291 const char *snapname;
5292 char *delim = strchr(zname, '@');
5296 snapname = delim + 1;
5297 if (strcmp(cbp->cb_snapname, snapname))
5301 if (zfs_get_holds(zhp, &nvl) != 0)
5304 if (znamelen > cbp->cb_max_namelen)
5305 cbp->cb_max_namelen = znamelen;
5307 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5308 const char *tag = nvpair_name(nvp);
5309 size_t taglen = strnlen(tag, MAXNAMELEN);
5310 if (taglen > cbp->cb_max_taglen)
5311 cbp->cb_max_taglen = taglen;
5314 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5318 * zfs holds [-r] <snap> ...
5320 * -r Recursively hold
5323 zfs_do_holds(int argc, char **argv)
5328 boolean_t scripted = B_FALSE;
5329 boolean_t recursive = B_FALSE;
5330 const char *opts = "rH";
5333 int types = ZFS_TYPE_SNAPSHOT;
5334 holds_cbdata_t cb = { 0 };
5341 while ((c = getopt(argc, argv, opts)) != -1) {
5350 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5357 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5358 flags |= ZFS_ITER_RECURSE;
5364 /* check number of arguments */
5368 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5371 for (i = 0; i < argc; ++i) {
5372 char *snapshot = argv[i];
5374 const char *snapname;
5376 delim = strchr(snapshot, '@');
5377 if (delim == NULL) {
5378 (void) fprintf(stderr,
5379 gettext("'%s' is not a snapshot\n"), snapshot);
5383 snapname = delim + 1;
5385 snapshot[delim - snapshot] = '\0';
5387 cb.cb_recursive = recursive;
5388 cb.cb_snapname = snapname;
5392 * 1. collect holds data, set format options
5394 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5395 holds_callback, &cb);
5401 * 2. print holds data
5403 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5405 if (nvlist_empty(nvl))
5406 (void) fprintf(stderr, gettext("no datasets available\n"));
5410 return (0 != errors);
5413 #define CHECK_SPINNER 30
5414 #define SPINNER_TIME 3 /* seconds */
5415 #define MOUNT_TIME 5 /* seconds */
5418 get_one_dataset(zfs_handle_t *zhp, void *data)
5420 static char *spin[] = { "-", "\\", "|", "/" };
5421 static int spinval = 0;
5422 static int spincheck = 0;
5423 static time_t last_spin_time = (time_t)0;
5424 get_all_cb_t *cbp = data;
5425 zfs_type_t type = zfs_get_type(zhp);
5427 if (cbp->cb_verbose) {
5428 if (--spincheck < 0) {
5429 time_t now = time(NULL);
5430 if (last_spin_time + SPINNER_TIME < now) {
5431 update_progress(spin[spinval++ % 4]);
5432 last_spin_time = now;
5434 spincheck = CHECK_SPINNER;
5439 * Iterate over any nested datasets.
5441 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5447 * Skip any datasets whose type does not match.
5449 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5453 libzfs_add_handle(cbp, zhp);
5454 assert(cbp->cb_used <= cbp->cb_alloc);
5460 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5462 get_all_cb_t cb = { 0 };
5463 cb.cb_verbose = verbose;
5464 cb.cb_getone = get_one_dataset;
5467 set_progress_header(gettext("Reading ZFS config"));
5468 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5470 *dslist = cb.cb_handles;
5471 *count = cb.cb_used;
5474 finish_progress(gettext("done."));
5478 * Generic callback for sharing or mounting filesystems. Because the code is so
5479 * similar, we have a common function with an extra parameter to determine which
5480 * mode we are using.
5482 #define OP_SHARE 0x1
5483 #define OP_MOUNT 0x2
5486 * Share or mount a dataset.
5489 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5490 boolean_t explicit, const char *options)
5492 char mountpoint[ZFS_MAXPROPLEN];
5493 char shareopts[ZFS_MAXPROPLEN];
5494 char smbshareopts[ZFS_MAXPROPLEN];
5495 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5497 uint64_t zoned, canmount;
5498 boolean_t shared_nfs, shared_smb;
5500 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5503 * Check to make sure we can mount/share this dataset. If we
5504 * are in the global zone and the filesystem is exported to a
5505 * local zone, or if we are in a local zone and the
5506 * filesystem is not exported, then it is an error.
5508 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5510 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5514 (void) fprintf(stderr, gettext("cannot %s '%s': "
5515 "dataset is exported to a local zone\n"), cmdname,
5519 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5523 (void) fprintf(stderr, gettext("cannot %s '%s': "
5524 "permission denied\n"), cmdname,
5530 * Ignore any filesystems which don't apply to us. This
5531 * includes those with a legacy mountpoint, or those with
5532 * legacy share options.
5534 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5535 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5536 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5537 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5538 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5539 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5541 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5542 strcmp(smbshareopts, "off") == 0) {
5546 (void) fprintf(stderr, gettext("cannot share '%s': "
5547 "legacy share\n"), zfs_get_name(zhp));
5548 (void) fprintf(stderr, gettext("use share(1M) to "
5549 "share this filesystem, or set "
5550 "sharenfs property on\n"));
5555 * We cannot share or mount legacy filesystems. If the
5556 * shareopts is non-legacy but the mountpoint is legacy, we
5557 * treat it as a legacy share.
5559 if (strcmp(mountpoint, "legacy") == 0) {
5563 (void) fprintf(stderr, gettext("cannot %s '%s': "
5564 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5565 (void) fprintf(stderr, gettext("use %s(1M) to "
5566 "%s this filesystem\n"), cmdname, cmdname);
5570 if (strcmp(mountpoint, "none") == 0) {
5574 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5575 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5580 * canmount explicit outcome
5581 * on no pass through
5582 * on yes pass through
5584 * off yes display error, return 1
5585 * noauto no return 0
5586 * noauto yes pass through
5588 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5589 if (canmount == ZFS_CANMOUNT_OFF) {
5593 (void) fprintf(stderr, gettext("cannot %s '%s': "
5594 "'canmount' property is set to 'off'\n"), cmdname,
5597 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5602 * At this point, we have verified that the mountpoint and/or
5603 * shareopts are appropriate for auto management. If the
5604 * filesystem is already mounted or shared, return (failing
5605 * for explicit requests); otherwise mount or share the
5611 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5612 shared_smb = zfs_is_shared_smb(zhp, NULL);
5614 if ((shared_nfs && shared_smb) ||
5615 ((shared_nfs && strcmp(shareopts, "on") == 0) &&
5616 (strcmp(smbshareopts, "off") == 0)) ||
5617 ((shared_smb && strcmp(smbshareopts, "on") == 0) &&
5618 (strcmp(shareopts, "off") == 0))) {
5622 (void) fprintf(stderr, gettext("cannot share "
5623 "'%s': filesystem already shared\n"),
5628 if (!zfs_is_mounted(zhp, NULL) &&
5629 zfs_mount(zhp, NULL, 0) != 0)
5632 if (protocol == NULL) {
5633 if (zfs_shareall(zhp) != 0)
5635 } else if (strcmp(protocol, "nfs") == 0) {
5636 if (zfs_share_nfs(zhp))
5638 } else if (strcmp(protocol, "smb") == 0) {
5639 if (zfs_share_smb(zhp))
5642 (void) fprintf(stderr, gettext("cannot share "
5643 "'%s': invalid share type '%s' "
5645 zfs_get_name(zhp), protocol);
5652 if (options == NULL)
5653 mnt.mnt_mntopts = "";
5655 mnt.mnt_mntopts = (char *)options;
5657 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5658 zfs_is_mounted(zhp, NULL)) {
5662 (void) fprintf(stderr, gettext("cannot mount "
5663 "'%s': filesystem already mounted\n"),
5668 if (zfs_mount(zhp, options, flags) != 0)
5677 * Reports progress in the form "(current/total)". Not thread-safe.
5680 report_mount_progress(int current, int total)
5682 static time_t last_progress_time = 0;
5683 time_t now = time(NULL);
5686 /* report 1..n instead of 0..n-1 */
5689 /* display header if we're here for the first time */
5691 set_progress_header(gettext("Mounting ZFS filesystems"));
5692 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5693 /* too soon to report again */
5697 last_progress_time = now;
5699 (void) sprintf(info, "(%d/%d)", current, total);
5701 if (current == total)
5702 finish_progress(info);
5704 update_progress(info);
5708 append_options(char *mntopts, char *newopts)
5710 int len = strlen(mntopts);
5712 /* original length plus new string to append plus 1 for the comma */
5713 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5714 (void) fprintf(stderr, gettext("the opts argument for "
5715 "'%s' option is too long (more than %d chars)\n"),
5716 "-o", MNT_LINE_MAX);
5721 mntopts[len++] = ',';
5723 (void) strcpy(&mntopts[len], newopts);
5727 share_mount(int op, int argc, char **argv)
5730 boolean_t verbose = B_FALSE;
5732 char *options = NULL;
5736 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5746 if (*optarg == '\0') {
5747 (void) fprintf(stderr, gettext("empty mount "
5748 "options (-o) specified\n"));
5752 if (options == NULL)
5753 options = safe_malloc(MNT_LINE_MAX + 1);
5755 /* option validation is done later */
5756 append_options(options, optarg);
5759 flags |= MS_OVERLAY;
5762 (void) fprintf(stderr, gettext("missing argument for "
5763 "'%c' option\n"), optopt);
5767 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5776 /* check number of arguments */
5778 zfs_handle_t **dslist = NULL;
5779 size_t i, count = 0;
5780 char *protocol = NULL;
5782 if (op == OP_SHARE && argc > 0) {
5783 if (strcmp(argv[0], "nfs") != 0 &&
5784 strcmp(argv[0], "smb") != 0) {
5785 (void) fprintf(stderr, gettext("share type "
5786 "must be 'nfs' or 'smb'\n"));
5795 (void) fprintf(stderr, gettext("too many arguments\n"));
5799 start_progress_timer();
5800 get_all_datasets(&dslist, &count, verbose);
5805 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5807 for (i = 0; i < count; i++) {
5809 report_mount_progress(i, count);
5811 if (share_mount_one(dslist[i], op, flags, protocol,
5812 B_FALSE, options) != 0)
5814 zfs_close(dslist[i]);
5818 } else if (argc == 0) {
5819 struct mnttab entry;
5821 if ((op == OP_SHARE) || (options != NULL)) {
5822 (void) fprintf(stderr, gettext("missing filesystem "
5823 "argument (specify -a for all)\n"));
5828 * When mount is given no arguments, go through /etc/mtab and
5829 * display any active ZFS mounts. We hide any snapshots, since
5830 * they are controlled automatically.
5832 rewind(mnttab_file);
5833 while (getmntent(mnttab_file, &entry) == 0) {
5834 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5835 strchr(entry.mnt_special, '@') != NULL)
5838 (void) printf("%-30s %s\n", entry.mnt_special,
5846 (void) fprintf(stderr,
5847 gettext("too many arguments\n"));
5851 if ((zhp = zfs_open(g_zfs, argv[0],
5852 ZFS_TYPE_FILESYSTEM)) == NULL) {
5855 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5865 * zfs mount -a [nfs]
5866 * zfs mount filesystem
5868 * Mount all filesystems, or mount the given filesystem.
5871 zfs_do_mount(int argc, char **argv)
5873 return (share_mount(OP_MOUNT, argc, argv));
5877 * zfs share -a [nfs | smb]
5878 * zfs share filesystem
5880 * Share all filesystems, or share the given filesystem.
5883 zfs_do_share(int argc, char **argv)
5885 return (share_mount(OP_SHARE, argc, argv));
5888 typedef struct unshare_unmount_node {
5889 zfs_handle_t *un_zhp;
5891 uu_avl_node_t un_avlnode;
5892 } unshare_unmount_node_t;
5896 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
5898 const unshare_unmount_node_t *l = larg;
5899 const unshare_unmount_node_t *r = rarg;
5901 return (strcmp(l->un_mountp, r->un_mountp));
5905 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5906 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
5907 * and unmount it appropriately.
5910 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
5914 struct stat64 statbuf;
5915 struct extmnttab entry;
5916 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5920 * Search for the path in /etc/mtab. Rather than looking for the
5921 * specific path, which can be fooled by non-standard paths (i.e. ".."
5922 * or "//"), we stat() the path and search for the corresponding
5923 * (major,minor) device pair.
5925 if (stat64(path, &statbuf) != 0) {
5926 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5927 cmdname, path, strerror(errno));
5930 path_inode = statbuf.st_ino;
5933 * Search for the given (major,minor) pair in the mount table.
5935 rewind(mnttab_file);
5936 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
5937 if (entry.mnt_major == major(statbuf.st_dev) &&
5938 entry.mnt_minor == minor(statbuf.st_dev))
5942 if (op == OP_SHARE) {
5943 (void) fprintf(stderr, gettext("cannot %s '%s': not "
5944 "currently mounted\n"), cmdname, path);
5947 (void) fprintf(stderr, gettext("warning: %s not in mtab\n"),
5949 if ((ret = umount2(path, flags)) != 0)
5950 (void) fprintf(stderr, gettext("%s: %s\n"), path,
5955 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
5956 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
5957 "filesystem\n"), cmdname, path);
5961 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
5962 ZFS_TYPE_FILESYSTEM)) == NULL)
5966 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
5967 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5968 cmdname, path, strerror(errno));
5970 } else if (statbuf.st_ino != path_inode) {
5971 (void) fprintf(stderr, gettext("cannot "
5972 "%s '%s': not a mountpoint\n"), cmdname, path);
5976 if (op == OP_SHARE) {
5977 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5978 char smbshare_prop[ZFS_MAXPROPLEN];
5980 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
5981 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
5982 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
5983 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
5985 if (strcmp(nfs_mnt_prop, "off") == 0 &&
5986 strcmp(smbshare_prop, "off") == 0) {
5987 (void) fprintf(stderr, gettext("cannot unshare "
5988 "'%s': legacy share\n"), path);
5989 (void) fprintf(stderr, gettext("use exportfs(8) "
5990 "or smbcontrol(1) to unshare this filesystem\n"));
5991 } else if (!zfs_is_shared(zhp)) {
5992 (void) fprintf(stderr, gettext("cannot unshare '%s': "
5993 "not currently shared\n"), path);
5995 ret = zfs_unshareall_bypath(zhp, path);
5998 char mtpt_prop[ZFS_MAXPROPLEN];
6000 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6001 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6004 ret = zfs_unmount(zhp, NULL, flags);
6005 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6006 (void) fprintf(stderr, gettext("cannot unmount "
6007 "'%s': legacy mountpoint\n"),
6009 (void) fprintf(stderr, gettext("use umount(8) "
6010 "to unmount this filesystem\n"));
6012 ret = zfs_unmountall(zhp, flags);
6023 * Generic callback for unsharing or unmounting a filesystem.
6026 unshare_unmount(int op, int argc, char **argv)
6033 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6034 char sharesmb[ZFS_MAXPROPLEN];
6037 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6046 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6057 * We could make use of zfs_for_each() to walk all datasets in
6058 * the system, but this would be very inefficient, especially
6059 * since we would have to linearly search /etc/mtab for each
6060 * one. Instead, do one pass through /etc/mtab looking for
6061 * zfs entries and call zfs_unmount() for each one.
6063 * Things get a little tricky if the administrator has created
6064 * mountpoints beneath other ZFS filesystems. In this case, we
6065 * have to unmount the deepest filesystems first. To accomplish
6066 * this, we place all the mountpoints in an AVL tree sorted by
6067 * the special type (dataset name), and walk the result in
6068 * reverse to make sure to get any snapshots first.
6070 struct mnttab entry;
6071 uu_avl_pool_t *pool;
6072 uu_avl_t *tree = NULL;
6073 unshare_unmount_node_t *node;
6075 uu_avl_walk_t *walk;
6078 (void) fprintf(stderr, gettext("too many arguments\n"));
6082 if (((pool = uu_avl_pool_create("unmount_pool",
6083 sizeof (unshare_unmount_node_t),
6084 offsetof(unshare_unmount_node_t, un_avlnode),
6085 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6086 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6089 rewind(mnttab_file);
6090 while (getmntent(mnttab_file, &entry) == 0) {
6092 /* ignore non-ZFS entries */
6093 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6096 /* ignore snapshots */
6097 if (strchr(entry.mnt_special, '@') != NULL)
6100 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6101 ZFS_TYPE_FILESYSTEM)) == NULL) {
6108 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6110 sizeof (nfs_mnt_prop),
6111 NULL, NULL, 0, B_FALSE) == 0);
6112 if (strcmp(nfs_mnt_prop, "off") != 0)
6114 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6116 sizeof (nfs_mnt_prop),
6117 NULL, NULL, 0, B_FALSE) == 0);
6118 if (strcmp(nfs_mnt_prop, "off") == 0)
6122 /* Ignore legacy mounts */
6123 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6125 sizeof (nfs_mnt_prop),
6126 NULL, NULL, 0, B_FALSE) == 0);
6127 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6129 /* Ignore canmount=noauto mounts */
6130 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6131 ZFS_CANMOUNT_NOAUTO)
6137 node = safe_malloc(sizeof (unshare_unmount_node_t));
6139 node->un_mountp = safe_strdup(entry.mnt_mountp);
6141 uu_avl_node_init(node, &node->un_avlnode, pool);
6143 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6144 uu_avl_insert(tree, node, idx);
6146 zfs_close(node->un_zhp);
6147 free(node->un_mountp);
6153 * Walk the AVL tree in reverse, unmounting each filesystem and
6154 * removing it from the AVL tree in the process.
6156 if ((walk = uu_avl_walk_start(tree,
6157 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6160 while ((node = uu_avl_walk_next(walk)) != NULL) {
6161 uu_avl_remove(tree, node);
6165 if (zfs_unshareall_bypath(node->un_zhp,
6166 node->un_mountp) != 0)
6171 if (zfs_unmount(node->un_zhp,
6172 node->un_mountp, flags) != 0)
6177 zfs_close(node->un_zhp);
6178 free(node->un_mountp);
6182 uu_avl_walk_end(walk);
6183 uu_avl_destroy(tree);
6184 uu_avl_pool_destroy(pool);
6189 (void) fprintf(stderr,
6190 gettext("missing filesystem argument\n"));
6192 (void) fprintf(stderr,
6193 gettext("too many arguments\n"));
6198 * We have an argument, but it may be a full path or a ZFS
6199 * filesystem. Pass full paths off to unmount_path() (shared by
6200 * manual_unmount), otherwise open the filesystem and pass to
6203 if (argv[0][0] == '/')
6204 return (unshare_unmount_path(op, argv[0],
6207 if ((zhp = zfs_open(g_zfs, argv[0],
6208 ZFS_TYPE_FILESYSTEM)) == NULL)
6211 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6212 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6213 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6214 NULL, 0, B_FALSE) == 0);
6218 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6220 sizeof (nfs_mnt_prop),
6221 NULL, NULL, 0, B_FALSE) == 0);
6222 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6223 sharesmb, sizeof (sharesmb), NULL, NULL,
6226 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6227 strcmp(sharesmb, "off") == 0) {
6228 (void) fprintf(stderr, gettext("cannot "
6229 "unshare '%s': legacy share\n"),
6231 (void) fprintf(stderr, gettext("use "
6232 "unshare(1M) to unshare this "
6235 } else if (!zfs_is_shared(zhp)) {
6236 (void) fprintf(stderr, gettext("cannot "
6237 "unshare '%s': not currently "
6238 "shared\n"), zfs_get_name(zhp));
6240 } else if (zfs_unshareall(zhp) != 0) {
6246 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6247 (void) fprintf(stderr, gettext("cannot "
6248 "unmount '%s': legacy "
6249 "mountpoint\n"), zfs_get_name(zhp));
6250 (void) fprintf(stderr, gettext("use "
6251 "umount(1M) to unmount this "
6254 } else if (!zfs_is_mounted(zhp, NULL)) {
6255 (void) fprintf(stderr, gettext("cannot "
6256 "unmount '%s': not currently "
6260 } else if (zfs_unmountall(zhp, flags) != 0) {
6274 * zfs unmount filesystem
6276 * Unmount all filesystems, or a specific ZFS filesystem.
6279 zfs_do_unmount(int argc, char **argv)
6281 return (unshare_unmount(OP_MOUNT, argc, argv));
6286 * zfs unshare filesystem
6288 * Unshare all filesystems, or a specific ZFS filesystem.
6291 zfs_do_unshare(int argc, char **argv)
6293 return (unshare_unmount(OP_SHARE, argc, argv));
6297 find_command_idx(char *command, int *idx)
6301 for (i = 0; i < NCOMMAND; i++) {
6302 if (command_table[i].name == NULL)
6305 if (strcmp(command, command_table[i].name) == 0) {
6314 zfs_do_diff(int argc, char **argv)
6318 char *tosnap = NULL;
6319 char *fromsnap = NULL;
6324 while ((c = getopt(argc, argv, "FHt")) != -1) {
6327 flags |= ZFS_DIFF_CLASSIFY;
6330 flags |= ZFS_DIFF_PARSEABLE;
6333 flags |= ZFS_DIFF_TIMESTAMP;
6336 (void) fprintf(stderr,
6337 gettext("invalid option '%c'\n"), optopt);
6346 (void) fprintf(stderr,
6347 gettext("must provide at least one snapshot name\n"));
6352 (void) fprintf(stderr, gettext("too many arguments\n"));
6357 tosnap = (argc == 2) ? argv[1] : NULL;
6360 if (*fromsnap != '@')
6361 copy = strdup(fromsnap);
6363 copy = strdup(tosnap);
6367 if ((atp = strchr(copy, '@')))
6370 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6376 * Ignore SIGPIPE so that the library can give us
6377 * information on any failure
6379 (void) sigignore(SIGPIPE);
6381 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6389 main(int argc, char **argv)
6395 (void) setlocale(LC_ALL, "");
6396 (void) textdomain(TEXT_DOMAIN);
6401 * Make sure the user has specified some command.
6404 (void) fprintf(stderr, gettext("missing command\n"));
6411 * The 'umount' command is an alias for 'unmount'
6413 if (strcmp(cmdname, "umount") == 0)
6414 cmdname = "unmount";
6417 * The 'recv' command is an alias for 'receive'
6419 if (strcmp(cmdname, "recv") == 0)
6420 cmdname = "receive";
6423 * The 'snap' command is an alias for 'snapshot'
6425 if (strcmp(cmdname, "snap") == 0)
6426 cmdname = "snapshot";
6431 if ((strcmp(cmdname, "-?") == 0) ||
6432 (strcmp(cmdname, "--help") == 0))
6435 if ((g_zfs = libzfs_init()) == NULL)
6438 mnttab_file = g_zfs->libzfs_mnttab;
6440 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6442 libzfs_print_on_error(g_zfs, B_TRUE);
6445 * Run the appropriate command.
6447 libzfs_mnttab_cache(g_zfs, B_FALSE);
6448 if (find_command_idx(cmdname, &i) == 0) {
6449 current_command = &command_table[i];
6450 ret = command_table[i].func(argc - 1, argv + 1);
6451 } else if (strchr(cmdname, '=') != NULL) {
6452 verify(find_command_idx("set", &i) == 0);
6453 current_command = &command_table[i];
6454 ret = command_table[i].func(argc, argv);
6456 (void) fprintf(stderr, gettext("unrecognized "
6457 "command '%s'\n"), cmdname);
6463 if (ret == 0 && log_history)
6464 (void) zpool_log_history(g_zfs, history_str);
6467 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6468 * for the purposes of running ::findleaks.
6470 if (getenv("ZFS_ABORT") != NULL) {
6471 (void) printf("dumping core by request\n");