4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011 by Delphix. All rights reserved.
34 #include <libnvpair.h>
47 #include <sys/mkdev.h>
48 #include <sys/mntent.h>
49 #include <sys/mnttab.h>
50 #include <sys/mount.h>
52 #include <sys/fs/zfs.h>
53 #include <sys/types.h>
58 #include <zfs_deleg.h>
62 #include <directory.h>
63 #endif /* HAVE_IDMAP */
67 #include "zfs_comutil.h"
69 libzfs_handle_t *g_zfs;
71 static FILE *mnttab_file;
72 static char history_str[HIS_MAX_RECORD_LEN];
74 static int zfs_do_clone(int argc, char **argv);
75 static int zfs_do_create(int argc, char **argv);
76 static int zfs_do_destroy(int argc, char **argv);
77 static int zfs_do_get(int argc, char **argv);
78 static int zfs_do_inherit(int argc, char **argv);
79 static int zfs_do_list(int argc, char **argv);
80 static int zfs_do_mount(int argc, char **argv);
81 static int zfs_do_rename(int argc, char **argv);
82 static int zfs_do_rollback(int argc, char **argv);
83 static int zfs_do_set(int argc, char **argv);
84 static int zfs_do_upgrade(int argc, char **argv);
85 static int zfs_do_snapshot(int argc, char **argv);
86 static int zfs_do_unmount(int argc, char **argv);
87 static int zfs_do_share(int argc, char **argv);
88 static int zfs_do_unshare(int argc, char **argv);
89 static int zfs_do_send(int argc, char **argv);
90 static int zfs_do_receive(int argc, char **argv);
91 static int zfs_do_promote(int argc, char **argv);
92 static int zfs_do_userspace(int argc, char **argv);
93 static int zfs_do_allow(int argc, char **argv);
94 static int zfs_do_unallow(int argc, char **argv);
95 static int zfs_do_hold(int argc, char **argv);
96 static int zfs_do_holds(int argc, char **argv);
97 static int zfs_do_release(int argc, char **argv);
98 static int zfs_do_diff(int argc, char **argv);
101 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
106 _umem_debug_init(void)
108 return ("default,verbose"); /* $UMEM_DEBUG setting */
112 _umem_logging_init(void)
114 return ("fail,contents"); /* $UMEM_LOGGING setting */
147 typedef struct zfs_command {
149 int (*func)(int argc, char **argv);
154 * Master command table. Each ZFS command has a name, associated function, and
155 * usage message. The usage messages need to be internationalized, so we have
156 * to have a function to return the usage message based on a command index.
158 * These commands are organized according to how they are displayed in the usage
159 * message. An empty command (one with a NULL name) indicates an empty line in
160 * the generic usage message.
162 static zfs_command_t command_table[] = {
163 { "create", zfs_do_create, HELP_CREATE },
164 { "destroy", zfs_do_destroy, HELP_DESTROY },
166 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
167 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
168 { "clone", zfs_do_clone, HELP_CLONE },
169 { "promote", zfs_do_promote, HELP_PROMOTE },
170 { "rename", zfs_do_rename, HELP_RENAME },
172 { "list", zfs_do_list, HELP_LIST },
174 { "set", zfs_do_set, HELP_SET },
175 { "get", zfs_do_get, HELP_GET },
176 { "inherit", zfs_do_inherit, HELP_INHERIT },
177 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
178 { "userspace", zfs_do_userspace, HELP_USERSPACE },
179 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
181 { "mount", zfs_do_mount, HELP_MOUNT },
182 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
183 { "share", zfs_do_share, HELP_SHARE },
184 { "unshare", zfs_do_unshare, HELP_UNSHARE },
186 { "send", zfs_do_send, HELP_SEND },
187 { "receive", zfs_do_receive, HELP_RECEIVE },
189 { "allow", zfs_do_allow, HELP_ALLOW },
191 { "unallow", zfs_do_unallow, HELP_UNALLOW },
193 { "hold", zfs_do_hold, HELP_HOLD },
194 { "holds", zfs_do_holds, HELP_HOLDS },
195 { "release", zfs_do_release, HELP_RELEASE },
196 { "diff", zfs_do_diff, HELP_DIFF },
199 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
201 zfs_command_t *current_command;
204 get_usage(zfs_help_t idx)
208 return (gettext("\tclone [-p] [-o property=value] ... "
209 "<snapshot> <filesystem|volume>\n"));
211 return (gettext("\tcreate [-p] [-o property=value] ... "
213 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
214 "-V <size> <volume>\n"));
216 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
217 "\tdestroy [-dnpRrv] "
218 "<filesystem|volume>@<snap>[%<snap>][,...]\n"));
220 return (gettext("\tget [-rHp] [-d max] "
221 "[-o \"all\" | field[,...]] [-t type[,...]] "
222 "[-s source[,...]]\n"
223 "\t <\"all\" | property[,...]> "
224 "[filesystem|volume|snapshot] ...\n"));
226 return (gettext("\tinherit [-rS] <property> "
227 "<filesystem|volume|snapshot> ...\n"));
229 return (gettext("\tupgrade [-v]\n"
230 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
232 return (gettext("\tlist [-rH][-d max] "
233 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
234 "\t [-S property] ... "
235 "[filesystem|volume|snapshot|snap] ...\n"));
237 return (gettext("\tmount\n"
238 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
240 return (gettext("\tpromote <clone-filesystem>\n"));
242 return (gettext("\treceive [-vnFu] <filesystem|volume|"
244 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
246 return (gettext("\trename <filesystem|volume|snapshot> "
247 "<filesystem|volume|snapshot>\n"
248 "\trename -p <filesystem|volume> <filesystem|volume>\n"
249 "\trename -r <snapshot> <snapshot>"));
251 return (gettext("\trollback [-rRf] <snapshot>\n"));
253 return (gettext("\tsend [-DnPpRrv] [-[iI] snapshot] "
256 return (gettext("\tset <property=value> "
257 "<filesystem|volume|snapshot> ...\n"));
259 return (gettext("\tshare <-a | filesystem>\n"));
261 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
262 "<filesystem@snapname|volume@snapname>\n"));
264 return (gettext("\tunmount [-f] "
265 "<-a | filesystem|mountpoint>\n"));
267 return (gettext("\tunshare "
268 "<-a | filesystem|mountpoint>\n"));
270 return (gettext("\tallow <filesystem|volume>\n"
272 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
273 "\t <filesystem|volume>\n"
274 "\tallow [-ld] -e <perm|@setname>[,...] "
275 "<filesystem|volume>\n"
276 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
277 "\tallow -s @setname <perm|@setname>[,...] "
278 "<filesystem|volume>\n"));
280 return (gettext("\tunallow [-rldug] "
281 "<\"everyone\"|user|group>[,...]\n"
282 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
283 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
284 "<filesystem|volume>\n"
285 "\tunallow [-r] -c [<perm|@setname>[,...]] "
286 "<filesystem|volume>\n"
287 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
288 "<filesystem|volume>\n"));
290 return (gettext("\tuserspace [-hniHp] [-o field[,...]] "
291 "[-sS field] ... [-t type[,...]]\n"
292 "\t <filesystem|snapshot>\n"));
293 case HELP_GROUPSPACE:
294 return (gettext("\tgroupspace [-hniHpU] [-o field[,...]] "
295 "[-sS field] ... [-t type[,...]]\n"
296 "\t <filesystem|snapshot>\n"));
298 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
300 return (gettext("\tholds [-r] <snapshot> ...\n"));
302 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
304 return (gettext("\tdiff [-FHt] <snapshot> "
305 "[snapshot|filesystem]\n"));
315 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
320 * Utility function to guarantee malloc() success.
324 safe_malloc(size_t size)
328 if ((data = calloc(1, size)) == NULL)
335 safe_strdup(char *str)
337 char *dupstr = strdup(str);
346 * Callback routine that will print out information for each of
350 usage_prop_cb(int prop, void *cb)
354 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
356 if (zfs_prop_readonly(prop))
357 (void) fprintf(fp, " NO ");
359 (void) fprintf(fp, "YES ");
361 if (zfs_prop_inheritable(prop))
362 (void) fprintf(fp, " YES ");
364 (void) fprintf(fp, " NO ");
366 if (zfs_prop_values(prop) == NULL)
367 (void) fprintf(fp, "-\n");
369 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
375 * Display usage message. If we're inside a command, display only the usage for
376 * that command. Otherwise, iterate over the entire command table and display
377 * a complete usage message.
380 usage(boolean_t requested)
383 boolean_t show_properties = B_FALSE;
384 FILE *fp = requested ? stdout : stderr;
386 if (current_command == NULL) {
388 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
390 gettext("where 'command' is one of the following:\n\n"));
392 for (i = 0; i < NCOMMAND; i++) {
393 if (command_table[i].name == NULL)
394 (void) fprintf(fp, "\n");
396 (void) fprintf(fp, "%s",
397 get_usage(command_table[i].usage));
400 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
401 "pool/[dataset/]*dataset[@name]\n"));
403 (void) fprintf(fp, gettext("usage:\n"));
404 (void) fprintf(fp, "%s", get_usage(current_command->usage));
407 if (current_command != NULL &&
408 (strcmp(current_command->name, "set") == 0 ||
409 strcmp(current_command->name, "get") == 0 ||
410 strcmp(current_command->name, "inherit") == 0 ||
411 strcmp(current_command->name, "list") == 0))
412 show_properties = B_TRUE;
414 if (show_properties) {
416 gettext("\nThe following properties are supported:\n"));
418 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
419 "PROPERTY", "EDIT", "INHERIT", "VALUES");
421 /* Iterate over all properties */
422 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
425 (void) fprintf(fp, "\t%-15s ", "userused@...");
426 (void) fprintf(fp, " NO NO <size>\n");
427 (void) fprintf(fp, "\t%-15s ", "groupused@...");
428 (void) fprintf(fp, " NO NO <size>\n");
429 (void) fprintf(fp, "\t%-15s ", "userquota@...");
430 (void) fprintf(fp, "YES NO <size> | none\n");
431 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
432 (void) fprintf(fp, "YES NO <size> | none\n");
433 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
434 (void) fprintf(fp, " NO NO <size>\n");
436 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
437 "with standard units such as K, M, G, etc.\n"));
438 (void) fprintf(fp, gettext("\nUser-defined properties can "
439 "be specified by using a name containing a colon (:).\n"));
440 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
441 "properties must be appended with\n"
442 "a user or group specifier of one of these forms:\n"
443 " POSIX name (eg: \"matt\")\n"
444 " POSIX id (eg: \"126829\")\n"
445 " SMB name@domain (eg: \"matt@sun\")\n"
446 " SMB SID (eg: \"S-1-234-567-89\")\n"));
449 gettext("\nFor the property list, run: %s\n"),
452 gettext("\nFor the delegated permission list, run: %s\n"),
453 "zfs allow|unallow");
457 * See comments at end of main().
459 if (getenv("ZFS_ABORT") != NULL) {
460 (void) printf("dumping core by request\n");
464 exit(requested ? 0 : 2);
468 parseprop(nvlist_t *props)
470 char *propname = optarg;
471 char *propval, *strval;
473 if ((propval = strchr(propname, '=')) == NULL) {
474 (void) fprintf(stderr, gettext("missing "
475 "'=' for -o option\n"));
480 if (nvlist_lookup_string(props, propname, &strval) == 0) {
481 (void) fprintf(stderr, gettext("property '%s' "
482 "specified multiple times\n"), propname);
485 if (nvlist_add_string(props, propname, propval) != 0)
491 parse_depth(char *opt, int *flags)
496 depth = (int)strtol(opt, &tmp, 0);
498 (void) fprintf(stderr,
499 gettext("%s is not an integer\n"), optarg);
503 (void) fprintf(stderr,
504 gettext("Depth can not be negative.\n"));
507 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
511 #define PROGRESS_DELAY 2 /* seconds */
513 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";
514 static time_t pt_begin;
515 static char *pt_header = NULL;
516 static boolean_t pt_shown;
519 start_progress_timer(void)
521 pt_begin = time(NULL) + PROGRESS_DELAY;
526 set_progress_header(char *header)
528 assert(pt_header == NULL);
529 pt_header = safe_strdup(header);
531 (void) printf("%s: ", header);
532 (void) fflush(stdout);
537 update_progress(char *update)
539 if (!pt_shown && time(NULL) > pt_begin) {
540 int len = strlen(update);
542 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
544 (void) fflush(stdout);
546 } else if (pt_shown) {
547 int len = strlen(update);
549 (void) printf("%s%*.*s", update, len, len, pt_reverse);
550 (void) fflush(stdout);
555 finish_progress(char *done)
558 (void) printf("%s\n", done);
559 (void) fflush(stdout);
566 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
568 * Given an existing dataset, create a writable copy whose initial contents
569 * are the same as the source. The newly created dataset maintains a
570 * dependency on the original; the original cannot be destroyed so long as
573 * The '-p' flag creates all the non-existing ancestors of the target first.
576 zfs_do_clone(int argc, char **argv)
578 zfs_handle_t *zhp = NULL;
579 boolean_t parents = B_FALSE;
584 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
588 while ((c = getopt(argc, argv, "o:p")) != -1) {
591 if (parseprop(props))
598 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
607 /* check number of arguments */
609 (void) fprintf(stderr, gettext("missing source dataset "
614 (void) fprintf(stderr, gettext("missing target dataset "
619 (void) fprintf(stderr, gettext("too many arguments\n"));
623 /* open the source dataset */
624 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
627 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
630 * Now create the ancestors of the target dataset. If the
631 * target already exists and '-p' option was used we should not
634 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
637 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
642 ret = zfs_clone(zhp, argv[1], props);
644 /* create the mountpoint if necessary */
648 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
650 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
651 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
652 ret = zfs_share(clone);
671 * zfs create [-p] [-o prop=value] ... fs
672 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
674 * Create a new dataset. This command can be used to create filesystems
675 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
676 * For volumes, the user must specify a size to be used.
678 * The '-s' flag applies only to volumes, and indicates that we should not try
679 * to set the reservation for this volume. By default we set a reservation
680 * equal to the size for any volume. For pools with SPA_VERSION >=
681 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
683 * The '-p' flag creates all the non-existing ancestors of the target first.
686 zfs_do_create(int argc, char **argv)
688 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
689 zfs_handle_t *zhp = NULL;
690 uint64_t volsize = 0;
692 boolean_t noreserve = B_FALSE;
693 boolean_t bflag = B_FALSE;
694 boolean_t parents = B_FALSE;
698 int canmount = ZFS_CANMOUNT_OFF;
700 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
704 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
707 type = ZFS_TYPE_VOLUME;
708 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
709 (void) fprintf(stderr, gettext("bad volume "
710 "size '%s': %s\n"), optarg,
711 libzfs_error_description(g_zfs));
715 if (nvlist_add_uint64(props,
716 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
725 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
726 (void) fprintf(stderr, gettext("bad volume "
727 "block size '%s': %s\n"), optarg,
728 libzfs_error_description(g_zfs));
732 if (nvlist_add_uint64(props,
733 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
738 if (parseprop(props))
745 (void) fprintf(stderr, gettext("missing size "
750 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
756 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
757 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
758 "used when creating a volume\n"));
765 /* check number of arguments */
767 (void) fprintf(stderr, gettext("missing %s argument\n"),
768 zfs_type_to_name(type));
772 (void) fprintf(stderr, gettext("too many arguments\n"));
776 if (type == ZFS_TYPE_VOLUME && !noreserve) {
777 zpool_handle_t *zpool_handle;
778 uint64_t spa_version;
780 zfs_prop_t resv_prop;
783 if ((p = strchr(argv[0], '/')))
785 zpool_handle = zpool_open(g_zfs, argv[0]);
788 if (zpool_handle == NULL)
790 spa_version = zpool_get_prop_int(zpool_handle,
791 ZPOOL_PROP_VERSION, NULL);
792 zpool_close(zpool_handle);
793 if (spa_version >= SPA_VERSION_REFRESERVATION)
794 resv_prop = ZFS_PROP_REFRESERVATION;
796 resv_prop = ZFS_PROP_RESERVATION;
797 volsize = zvol_volsize_to_reservation(volsize, props);
799 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
801 if (nvlist_add_uint64(props,
802 zfs_prop_to_name(resv_prop), volsize) != 0) {
809 if (parents && zfs_name_valid(argv[0], type)) {
811 * Now create the ancestors of target dataset. If the target
812 * already exists and '-p' option was used we should not
815 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
819 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
824 if (zfs_create(g_zfs, argv[0], type, props) != 0)
827 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
832 * if the user doesn't want the dataset automatically mounted,
833 * then skip the mount/share step
835 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
836 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
839 * Mount and/or share the new filesystem as appropriate. We provide a
840 * verbose error message to let the user know that their filesystem was
841 * in fact created, even if we failed to mount or share it.
843 if (canmount == ZFS_CANMOUNT_ON) {
844 if (zfs_mount(zhp, NULL, 0) != 0) {
845 (void) fprintf(stderr, gettext("filesystem "
846 "successfully created, but not mounted\n"));
848 } else if (zfs_share(zhp) != 0) {
849 (void) fprintf(stderr, gettext("filesystem "
850 "successfully created, but not shared\n"));
867 * zfs destroy [-rRf] <fs, vol>
868 * zfs destroy [-rRd] <snap>
870 * -r Recursively destroy all children
871 * -R Recursively destroy all dependents, including clones
872 * -f Force unmounting of any dependents
873 * -d If we can't destroy now, mark for deferred destruction
875 * Destroys the given dataset. By default, it will unmount any filesystems,
876 * and refuse to destroy a dataset that has any dependents. A dependent can
877 * either be a child, or a clone of a child.
879 typedef struct destroy_cbdata {
882 boolean_t cb_recurse;
884 boolean_t cb_doclones;
885 zfs_handle_t *cb_target;
886 boolean_t cb_defer_destroy;
887 boolean_t cb_verbose;
888 boolean_t cb_parsable;
892 /* first snap in contiguous run */
893 zfs_handle_t *cb_firstsnap;
894 /* previous snap in contiguous run */
895 zfs_handle_t *cb_prevsnap;
901 * Check for any dependents based on the '-r' or '-R' flags.
904 destroy_check_dependent(zfs_handle_t *zhp, void *data)
906 destroy_cbdata_t *cbp = data;
907 const char *tname = zfs_get_name(cbp->cb_target);
908 const char *name = zfs_get_name(zhp);
910 if (strncmp(tname, name, strlen(tname)) == 0 &&
911 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
913 * This is a direct descendant, not a clone somewhere else in
920 (void) fprintf(stderr, gettext("cannot destroy '%s': "
921 "%s has children\n"),
922 zfs_get_name(cbp->cb_target),
923 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
924 (void) fprintf(stderr, gettext("use '-r' to destroy "
925 "the following datasets:\n"));
926 cbp->cb_first = B_FALSE;
927 cbp->cb_error = B_TRUE;
930 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
933 * This is a clone. We only want to report this if the '-r'
934 * wasn't specified, or the target is a snapshot.
936 if (!cbp->cb_recurse &&
937 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
941 (void) fprintf(stderr, gettext("cannot destroy '%s': "
942 "%s has dependent clones\n"),
943 zfs_get_name(cbp->cb_target),
944 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
945 (void) fprintf(stderr, gettext("use '-R' to destroy "
946 "the following datasets:\n"));
947 cbp->cb_first = B_FALSE;
948 cbp->cb_error = B_TRUE;
949 cbp->cb_dryrun = B_TRUE;
952 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
961 destroy_callback(zfs_handle_t *zhp, void *data)
963 destroy_cbdata_t *cb = data;
964 const char *name = zfs_get_name(zhp);
966 if (cb->cb_verbose) {
967 if (cb->cb_parsable) {
968 (void) printf("destroy\t%s\n", name);
969 } else if (cb->cb_dryrun) {
970 (void) printf(gettext("would destroy %s\n"),
973 (void) printf(gettext("will destroy %s\n"),
979 * Ignore pools (which we've already flagged as an error before getting
982 if (strchr(zfs_get_name(zhp), '/') == NULL &&
983 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
988 if (!cb->cb_dryrun) {
989 if (zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
990 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1001 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1003 destroy_cbdata_t *cb = arg;
1004 const char *name = zfs_get_name(zhp);
1007 if (nvlist_exists(cb->cb_nvl, name)) {
1008 if (cb->cb_firstsnap == NULL)
1009 cb->cb_firstsnap = zfs_handle_dup(zhp);
1010 if (cb->cb_prevsnap != NULL)
1011 zfs_close(cb->cb_prevsnap);
1012 /* this snap continues the current range */
1013 cb->cb_prevsnap = zfs_handle_dup(zhp);
1014 if (cb->cb_verbose) {
1015 if (cb->cb_parsable) {
1016 (void) printf("destroy\t%s\n", name);
1017 } else if (cb->cb_dryrun) {
1018 (void) printf(gettext("would destroy %s\n"),
1021 (void) printf(gettext("will destroy %s\n"),
1025 } else if (cb->cb_firstsnap != NULL) {
1026 /* end of this range */
1028 err = zfs_get_snapused_int(cb->cb_firstsnap,
1029 cb->cb_prevsnap, &used);
1030 cb->cb_snapused += used;
1031 zfs_close(cb->cb_firstsnap);
1032 cb->cb_firstsnap = NULL;
1033 zfs_close(cb->cb_prevsnap);
1034 cb->cb_prevsnap = NULL;
1041 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1044 assert(cb->cb_firstsnap == NULL);
1045 assert(cb->cb_prevsnap == NULL);
1046 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1047 if (cb->cb_firstsnap != NULL) {
1050 err = zfs_get_snapused_int(cb->cb_firstsnap,
1051 cb->cb_prevsnap, &used);
1053 cb->cb_snapused += used;
1054 zfs_close(cb->cb_firstsnap);
1055 cb->cb_firstsnap = NULL;
1056 zfs_close(cb->cb_prevsnap);
1057 cb->cb_prevsnap = NULL;
1063 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1065 destroy_cbdata_t *cb = arg;
1068 /* Check for clones. */
1069 if (!cb->cb_doclones) {
1070 cb->cb_target = zhp;
1071 cb->cb_first = B_TRUE;
1072 err = zfs_iter_dependents(zhp, B_TRUE,
1073 destroy_check_dependent, cb);
1077 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1085 gather_snapshots(zfs_handle_t *zhp, void *arg)
1087 destroy_cbdata_t *cb = arg;
1090 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1096 if (cb->cb_verbose) {
1097 err = destroy_print_snapshots(zhp, cb);
1103 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1111 destroy_clones(destroy_cbdata_t *cb)
1114 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1116 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1117 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1120 boolean_t defer = cb->cb_defer_destroy;
1124 * We can't defer destroy non-snapshots, so set it to
1125 * false while destroying the clones.
1127 cb->cb_defer_destroy = B_FALSE;
1128 err = zfs_iter_dependents(zhp, B_FALSE,
1129 destroy_callback, cb);
1130 cb->cb_defer_destroy = defer;
1140 zfs_do_destroy(int argc, char **argv)
1142 destroy_cbdata_t cb = { 0 };
1146 zfs_type_t type = ZFS_TYPE_DATASET;
1149 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1152 cb.cb_verbose = B_TRUE;
1155 cb.cb_verbose = B_TRUE;
1156 cb.cb_parsable = B_TRUE;
1159 cb.cb_dryrun = B_TRUE;
1162 cb.cb_defer_destroy = B_TRUE;
1163 type = ZFS_TYPE_SNAPSHOT;
1166 cb.cb_force = B_TRUE;
1169 cb.cb_recurse = B_TRUE;
1172 cb.cb_recurse = B_TRUE;
1173 cb.cb_doclones = B_TRUE;
1177 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1186 /* check number of arguments */
1188 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1192 (void) fprintf(stderr, gettext("too many arguments\n"));
1196 at = strchr(argv[0], '@');
1200 /* Build the list of snaps to destroy in cb_nvl. */
1201 if (nvlist_alloc(&cb.cb_nvl, NV_UNIQUE_NAME, 0) != 0)
1205 zhp = zfs_open(g_zfs, argv[0],
1206 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1210 cb.cb_snapspec = at + 1;
1211 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1214 nvlist_free(cb.cb_nvl);
1218 if (nvlist_empty(cb.cb_nvl)) {
1219 (void) fprintf(stderr, gettext("could not find any "
1220 "snapshots to destroy; check snapshot names.\n"));
1222 nvlist_free(cb.cb_nvl);
1226 if (cb.cb_verbose) {
1228 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1229 if (cb.cb_parsable) {
1230 (void) printf("reclaim\t%llu\n",
1231 (u_longlong_t)cb.cb_snapused);
1232 } else if (cb.cb_dryrun) {
1233 (void) printf(gettext("would reclaim %s\n"),
1236 (void) printf(gettext("will reclaim %s\n"),
1241 if (!cb.cb_dryrun) {
1243 err = destroy_clones(&cb);
1245 err = zfs_destroy_snaps_nvl(zhp, cb.cb_nvl,
1246 cb.cb_defer_destroy);
1251 nvlist_free(cb.cb_nvl);
1255 /* Open the given dataset */
1256 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1262 * Perform an explicit check for pools before going any further.
1264 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1265 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1266 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1267 "operation does not apply to pools\n"),
1269 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1270 "%s' to destroy all datasets in the pool\n"),
1272 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1273 "to destroy the pool itself\n"), zfs_get_name(zhp));
1279 * Check for any dependents and/or clones.
1281 cb.cb_first = B_TRUE;
1282 if (!cb.cb_doclones &&
1283 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1294 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1301 * Do the real thing. The callback will close the
1302 * handle regardless of whether it succeeds or not.
1304 if (destroy_callback(zhp, &cb) != 0)
1312 is_recvd_column(zprop_get_cbdata_t *cbp)
1315 zfs_get_column_t col;
1317 for (i = 0; i < ZFS_GET_NCOLS &&
1318 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1319 if (col == GET_COL_RECVD)
1325 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1326 * < all | property[,property]... > < fs | snap | vol > ...
1328 * -r recurse over any child datasets
1329 * -H scripted mode. Headers are stripped, and fields are separated
1330 * by tabs instead of spaces.
1331 * -o Set of fields to display. One of "name,property,value,
1332 * received,source". Default is "name,property,value,source".
1333 * "all" is an alias for all five.
1334 * -s Set of sources to allow. One of
1335 * "local,default,inherited,received,temporary,none". Default is
1337 * -p Display values in parsable (literal) format.
1339 * Prints properties for the given datasets. The user can control which
1340 * columns to display as well as which property types to allow.
1344 * Invoked to display the properties for a single dataset.
1347 get_callback(zfs_handle_t *zhp, void *data)
1349 char buf[ZFS_MAXPROPLEN];
1350 char rbuf[ZFS_MAXPROPLEN];
1351 zprop_source_t sourcetype;
1352 char source[ZFS_MAXNAMELEN];
1353 zprop_get_cbdata_t *cbp = data;
1354 nvlist_t *user_props = zfs_get_user_props(zhp);
1355 zprop_list_t *pl = cbp->cb_proplist;
1359 boolean_t received = is_recvd_column(cbp);
1361 for (; pl != NULL; pl = pl->pl_next) {
1362 char *recvdval = NULL;
1364 * Skip the special fake placeholder. This will also skip over
1365 * the name property when 'all' is specified.
1367 if (pl->pl_prop == ZFS_PROP_NAME &&
1368 pl == cbp->cb_proplist)
1371 if (pl->pl_prop != ZPROP_INVAL) {
1372 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1373 sizeof (buf), &sourcetype, source,
1375 cbp->cb_literal) != 0) {
1378 if (!zfs_prop_valid_for_type(pl->pl_prop,
1379 ZFS_TYPE_DATASET)) {
1380 (void) fprintf(stderr,
1381 gettext("No such property '%s'\n"),
1382 zfs_prop_to_name(pl->pl_prop));
1385 sourcetype = ZPROP_SRC_NONE;
1386 (void) strlcpy(buf, "-", sizeof (buf));
1389 if (received && (zfs_prop_get_recvd(zhp,
1390 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1391 cbp->cb_literal) == 0))
1394 zprop_print_one_property(zfs_get_name(zhp), cbp,
1395 zfs_prop_to_name(pl->pl_prop),
1396 buf, sourcetype, source, recvdval);
1397 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1398 sourcetype = ZPROP_SRC_LOCAL;
1400 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1401 buf, sizeof (buf), cbp->cb_literal) != 0) {
1402 sourcetype = ZPROP_SRC_NONE;
1403 (void) strlcpy(buf, "-", sizeof (buf));
1406 zprop_print_one_property(zfs_get_name(zhp), cbp,
1407 pl->pl_user_prop, buf, sourcetype, source, NULL);
1408 } else if (zfs_prop_written(pl->pl_user_prop)) {
1409 sourcetype = ZPROP_SRC_LOCAL;
1411 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1412 buf, sizeof (buf), cbp->cb_literal) != 0) {
1413 sourcetype = ZPROP_SRC_NONE;
1414 (void) strlcpy(buf, "-", sizeof (buf));
1417 zprop_print_one_property(zfs_get_name(zhp), cbp,
1418 pl->pl_user_prop, buf, sourcetype, source, NULL);
1420 if (nvlist_lookup_nvlist(user_props,
1421 pl->pl_user_prop, &propval) != 0) {
1424 sourcetype = ZPROP_SRC_NONE;
1427 verify(nvlist_lookup_string(propval,
1428 ZPROP_VALUE, &strval) == 0);
1429 verify(nvlist_lookup_string(propval,
1430 ZPROP_SOURCE, &sourceval) == 0);
1432 if (strcmp(sourceval,
1433 zfs_get_name(zhp)) == 0) {
1434 sourcetype = ZPROP_SRC_LOCAL;
1435 } else if (strcmp(sourceval,
1436 ZPROP_SOURCE_VAL_RECVD) == 0) {
1437 sourcetype = ZPROP_SRC_RECEIVED;
1439 sourcetype = ZPROP_SRC_INHERITED;
1440 (void) strlcpy(source,
1441 sourceval, sizeof (source));
1445 if (received && (zfs_prop_get_recvd(zhp,
1446 pl->pl_user_prop, rbuf, sizeof (rbuf),
1447 cbp->cb_literal) == 0))
1450 zprop_print_one_property(zfs_get_name(zhp), cbp,
1451 pl->pl_user_prop, strval, sourcetype,
1460 zfs_do_get(int argc, char **argv)
1462 zprop_get_cbdata_t cb = { 0 };
1463 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1464 int types = ZFS_TYPE_DATASET;
1465 char *value, *fields;
1468 zprop_list_t fake_name = { 0 };
1471 * Set up default columns and sources.
1473 cb.cb_sources = ZPROP_SRC_ALL;
1474 cb.cb_columns[0] = GET_COL_NAME;
1475 cb.cb_columns[1] = GET_COL_PROPERTY;
1476 cb.cb_columns[2] = GET_COL_VALUE;
1477 cb.cb_columns[3] = GET_COL_SOURCE;
1478 cb.cb_type = ZFS_TYPE_DATASET;
1481 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1484 cb.cb_literal = B_TRUE;
1487 limit = parse_depth(optarg, &flags);
1490 flags |= ZFS_ITER_RECURSE;
1493 cb.cb_scripted = B_TRUE;
1496 (void) fprintf(stderr, gettext("missing argument for "
1497 "'%c' option\n"), optopt);
1502 * Process the set of columns to display. We zero out
1503 * the structure to give us a blank slate.
1505 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1507 while (*optarg != '\0') {
1508 static char *col_subopts[] =
1509 { "name", "property", "value", "received",
1510 "source", "all", NULL };
1512 if (i == ZFS_GET_NCOLS) {
1513 (void) fprintf(stderr, gettext("too "
1514 "many fields given to -o "
1519 switch (getsubopt(&optarg, col_subopts,
1522 cb.cb_columns[i++] = GET_COL_NAME;
1525 cb.cb_columns[i++] = GET_COL_PROPERTY;
1528 cb.cb_columns[i++] = GET_COL_VALUE;
1531 cb.cb_columns[i++] = GET_COL_RECVD;
1532 flags |= ZFS_ITER_RECVD_PROPS;
1535 cb.cb_columns[i++] = GET_COL_SOURCE;
1539 (void) fprintf(stderr,
1540 gettext("\"all\" conflicts "
1541 "with specific fields "
1542 "given to -o option\n"));
1545 cb.cb_columns[0] = GET_COL_NAME;
1546 cb.cb_columns[1] = GET_COL_PROPERTY;
1547 cb.cb_columns[2] = GET_COL_VALUE;
1548 cb.cb_columns[3] = GET_COL_RECVD;
1549 cb.cb_columns[4] = GET_COL_SOURCE;
1550 flags |= ZFS_ITER_RECVD_PROPS;
1554 (void) fprintf(stderr,
1555 gettext("invalid column name "
1564 while (*optarg != '\0') {
1565 static char *source_subopts[] = {
1566 "local", "default", "inherited",
1567 "received", "temporary", "none",
1570 switch (getsubopt(&optarg, source_subopts,
1573 cb.cb_sources |= ZPROP_SRC_LOCAL;
1576 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1579 cb.cb_sources |= ZPROP_SRC_INHERITED;
1582 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1585 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1588 cb.cb_sources |= ZPROP_SRC_NONE;
1591 (void) fprintf(stderr,
1592 gettext("invalid source "
1601 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1602 while (*optarg != '\0') {
1603 static char *type_subopts[] = { "filesystem",
1604 "volume", "snapshot", "all", NULL };
1606 switch (getsubopt(&optarg, type_subopts,
1609 types |= ZFS_TYPE_FILESYSTEM;
1612 types |= ZFS_TYPE_VOLUME;
1615 types |= ZFS_TYPE_SNAPSHOT;
1618 types = ZFS_TYPE_DATASET;
1622 (void) fprintf(stderr,
1623 gettext("invalid type '%s'\n"),
1631 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1641 (void) fprintf(stderr, gettext("missing property "
1648 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1656 * As part of zfs_expand_proplist(), we keep track of the maximum column
1657 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1658 * need to know the maximum name length. However, the user likely did
1659 * not specify 'name' as one of the properties to fetch, so we need to
1660 * make sure we always include at least this property for
1661 * print_get_headers() to work properly.
1663 if (cb.cb_proplist != NULL) {
1664 fake_name.pl_prop = ZFS_PROP_NAME;
1665 fake_name.pl_width = strlen(gettext("NAME"));
1666 fake_name.pl_next = cb.cb_proplist;
1667 cb.cb_proplist = &fake_name;
1670 cb.cb_first = B_TRUE;
1672 /* run for each object */
1673 ret = zfs_for_each(argc, argv, flags, types, NULL,
1674 &cb.cb_proplist, limit, get_callback, &cb);
1676 if (cb.cb_proplist == &fake_name)
1677 zprop_free_list(fake_name.pl_next);
1679 zprop_free_list(cb.cb_proplist);
1685 * inherit [-rS] <property> <fs|vol> ...
1687 * -r Recurse over all children
1688 * -S Revert to received value, if any
1690 * For each dataset specified on the command line, inherit the given property
1691 * from its parent. Inheriting a property at the pool level will cause it to
1692 * use the default value. The '-r' flag will recurse over all children, and is
1693 * useful for setting a property on a hierarchy-wide basis, regardless of any
1694 * local modifications for each dataset.
1697 typedef struct inherit_cbdata {
1698 const char *cb_propname;
1699 boolean_t cb_received;
1703 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1705 inherit_cbdata_t *cb = data;
1706 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1709 * If we're doing it recursively, then ignore properties that
1710 * are not valid for this type of dataset.
1712 if (prop != ZPROP_INVAL &&
1713 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1716 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1720 inherit_cb(zfs_handle_t *zhp, void *data)
1722 inherit_cbdata_t *cb = data;
1724 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1728 zfs_do_inherit(int argc, char **argv)
1732 inherit_cbdata_t cb = { 0 };
1736 boolean_t received = B_FALSE;
1739 while ((c = getopt(argc, argv, "rS")) != -1) {
1742 flags |= ZFS_ITER_RECURSE;
1749 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1758 /* check number of arguments */
1760 (void) fprintf(stderr, gettext("missing property argument\n"));
1764 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1772 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1773 if (zfs_prop_readonly(prop)) {
1774 (void) fprintf(stderr, gettext(
1775 "%s property is read-only\n"),
1779 if (!zfs_prop_inheritable(prop) && !received) {
1780 (void) fprintf(stderr, gettext("'%s' property cannot "
1781 "be inherited\n"), propname);
1782 if (prop == ZFS_PROP_QUOTA ||
1783 prop == ZFS_PROP_RESERVATION ||
1784 prop == ZFS_PROP_REFQUOTA ||
1785 prop == ZFS_PROP_REFRESERVATION)
1786 (void) fprintf(stderr, gettext("use 'zfs set "
1787 "%s=none' to clear\n"), propname);
1790 if (received && (prop == ZFS_PROP_VOLSIZE ||
1791 prop == ZFS_PROP_VERSION)) {
1792 (void) fprintf(stderr, gettext("'%s' property cannot "
1793 "be reverted to a received value\n"), propname);
1796 } else if (!zfs_prop_user(propname)) {
1797 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1802 cb.cb_propname = propname;
1803 cb.cb_received = received;
1805 if (flags & ZFS_ITER_RECURSE) {
1806 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1807 NULL, NULL, 0, inherit_recurse_cb, &cb);
1809 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1810 NULL, NULL, 0, inherit_cb, &cb);
1816 typedef struct upgrade_cbdata {
1817 uint64_t cb_numupgraded;
1818 uint64_t cb_numsamegraded;
1819 uint64_t cb_numfailed;
1820 uint64_t cb_version;
1822 boolean_t cb_foundone;
1823 char cb_lastfs[ZFS_MAXNAMELEN];
1827 same_pool(zfs_handle_t *zhp, const char *name)
1829 int len1 = strcspn(name, "/@");
1830 const char *zhname = zfs_get_name(zhp);
1831 int len2 = strcspn(zhname, "/@");
1835 return (strncmp(name, zhname, len1) == 0);
1839 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1841 upgrade_cbdata_t *cb = data;
1842 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1844 /* list if it's old/new */
1845 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1846 (cb->cb_newer && version > ZPL_VERSION)) {
1849 str = gettext("The following filesystems are "
1850 "formatted using a newer software version and\n"
1851 "cannot be accessed on the current system.\n\n");
1853 str = gettext("The following filesystems are "
1854 "out of date, and can be upgraded. After being\n"
1855 "upgraded, these filesystems (and any 'zfs send' "
1856 "streams generated from\n"
1857 "subsequent snapshots) will no longer be "
1858 "accessible by older software versions.\n\n");
1861 if (!cb->cb_foundone) {
1863 (void) printf(gettext("VER FILESYSTEM\n"));
1864 (void) printf(gettext("--- ------------\n"));
1865 cb->cb_foundone = B_TRUE;
1868 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1875 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1877 upgrade_cbdata_t *cb = data;
1878 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1879 int needed_spa_version;
1882 if (zfs_spa_version(zhp, &spa_version) < 0)
1885 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1887 if (needed_spa_version < 0)
1890 if (spa_version < needed_spa_version) {
1892 (void) printf(gettext("%s: can not be "
1893 "upgraded; the pool version needs to first "
1894 "be upgraded\nto version %d\n\n"),
1895 zfs_get_name(zhp), needed_spa_version);
1901 if (version < cb->cb_version) {
1903 (void) snprintf(verstr, sizeof (verstr),
1904 "%llu", (u_longlong_t)cb->cb_version);
1905 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1907 * If they did "zfs upgrade -a", then we could
1908 * be doing ioctls to different pools. We need
1909 * to log this history once to each pool.
1911 verify(zpool_stage_history(g_zfs, history_str) == 0);
1913 if (zfs_prop_set(zhp, "version", verstr) == 0)
1914 cb->cb_numupgraded++;
1917 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1918 } else if (version > cb->cb_version) {
1919 /* can't downgrade */
1920 (void) printf(gettext("%s: can not be downgraded; "
1921 "it is already at version %u\n"),
1922 zfs_get_name(zhp), version);
1925 cb->cb_numsamegraded++;
1933 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1936 zfs_do_upgrade(int argc, char **argv)
1938 boolean_t all = B_FALSE;
1939 boolean_t showversions = B_FALSE;
1941 upgrade_cbdata_t cb = { 0 };
1943 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1946 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1949 flags |= ZFS_ITER_RECURSE;
1952 showversions = B_TRUE;
1955 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1956 optarg, &cb.cb_version) != 0) {
1957 (void) fprintf(stderr,
1958 gettext("invalid version %s\n"), optarg);
1967 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1976 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1978 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1979 cb.cb_version || argc))
1981 if ((all || argc) && (showversions))
1987 /* Show info on available versions. */
1988 (void) printf(gettext("The following filesystem versions are "
1990 (void) printf(gettext("VER DESCRIPTION\n"));
1991 (void) printf("--- -----------------------------------------"
1992 "---------------\n");
1993 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1994 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1995 (void) printf(gettext(" 3 Case insensitive and filesystem "
1996 "user identifier (FUID)\n"));
1997 (void) printf(gettext(" 4 userquota, groupquota "
1999 (void) printf(gettext(" 5 System attributes\n"));
2000 (void) printf(gettext("\nFor more information on a particular "
2001 "version, including supported releases,\n"));
2002 (void) printf("see the ZFS Administration Guide.\n\n");
2004 } else if (argc || all) {
2005 /* Upgrade filesystems */
2006 if (cb.cb_version == 0)
2007 cb.cb_version = ZPL_VERSION;
2008 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2009 NULL, NULL, 0, upgrade_set_callback, &cb);
2010 (void) printf(gettext("%llu filesystems upgraded\n"),
2011 (u_longlong_t)cb.cb_numupgraded);
2012 if (cb.cb_numsamegraded) {
2013 (void) printf(gettext("%llu filesystems already at "
2015 (u_longlong_t)cb.cb_numsamegraded);
2017 if (cb.cb_numfailed != 0)
2020 /* List old-version filesytems */
2022 (void) printf(gettext("This system is currently running "
2023 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2025 flags |= ZFS_ITER_RECURSE;
2026 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2027 NULL, NULL, 0, upgrade_list_callback, &cb);
2029 found = cb.cb_foundone;
2030 cb.cb_foundone = B_FALSE;
2031 cb.cb_newer = B_TRUE;
2033 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2034 NULL, NULL, 0, upgrade_list_callback, &cb);
2036 if (!cb.cb_foundone && !found) {
2037 (void) printf(gettext("All filesystems are "
2038 "formatted with the current version.\n"));
2045 #define USTYPE_USR_BIT (0)
2046 #define USTYPE_GRP_BIT (1)
2047 #define USTYPE_PSX_BIT (2)
2048 #define USTYPE_SMB_BIT (3)
2050 #define USTYPE_USR (1 << USTYPE_USR_BIT)
2051 #define USTYPE_GRP (1 << USTYPE_GRP_BIT)
2053 #define USTYPE_PSX (1 << USTYPE_PSX_BIT)
2054 #define USTYPE_SMB (1 << USTYPE_SMB_BIT)
2056 #define USTYPE_PSX_USR (USTYPE_PSX | USTYPE_USR)
2057 #define USTYPE_SMB_USR (USTYPE_SMB | USTYPE_USR)
2058 #define USTYPE_PSX_GRP (USTYPE_PSX | USTYPE_GRP)
2059 #define USTYPE_SMB_GRP (USTYPE_SMB | USTYPE_GRP)
2060 #define USTYPE_ALL (USTYPE_PSX_USR | USTYPE_SMB_USR \
2061 | USTYPE_PSX_GRP | USTYPE_SMB_GRP)
2064 #define USPROP_USED_BIT (0)
2065 #define USPROP_QUOTA_BIT (1)
2067 #define USPROP_USED (1 << USPROP_USED_BIT)
2068 #define USPROP_QUOTA (1 << USPROP_QUOTA_BIT)
2070 typedef struct us_node {
2072 uu_avl_node_t usn_avlnode;
2073 uu_list_node_t usn_listnode;
2076 typedef struct us_cbdata {
2078 uu_avl_pool_t *cb_avl_pool;
2080 boolean_t cb_numname;
2081 boolean_t cb_nicenum;
2082 boolean_t cb_sid2posix;
2083 zfs_userquota_prop_t cb_prop;
2084 zfs_sort_column_t *cb_sortcol;
2085 size_t cb_max_typelen;
2086 size_t cb_max_namelen;
2087 size_t cb_max_usedlen;
2088 size_t cb_max_quotalen;
2092 zfs_sort_column_t *si_sortcol;
2093 boolean_t si_num_name;
2094 boolean_t si_parsable;
2098 us_compare(const void *larg, const void *rarg, void *unused)
2100 const us_node_t *l = larg;
2101 const us_node_t *r = rarg;
2103 us_sort_info_t *si = (us_sort_info_t *)unused;
2104 zfs_sort_column_t *sortcol = si->si_sortcol;
2105 boolean_t num_name = si->si_num_name;
2106 nvlist_t *lnvl = l->usn_nvl;
2107 nvlist_t *rnvl = r->usn_nvl;
2109 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2116 zfs_prop_t prop = sortcol->sc_prop;
2117 const char *propname = NULL;
2118 boolean_t reverse = sortcol->sc_reverse;
2123 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2124 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2126 rc = (rv32 > lv32) ? 1 : -1;
2131 (void) nvlist_lookup_uint32(lnvl, propname,
2133 (void) nvlist_lookup_uint32(rnvl, propname,
2136 rc = (rv32 > lv32) ? 1 : -1;
2138 (void) nvlist_lookup_string(lnvl, propname,
2140 (void) nvlist_lookup_string(rnvl, propname,
2142 rc = strcmp(lvstr, rvstr);
2147 case ZFS_PROP_QUOTA:
2148 if (ZFS_PROP_USED == prop)
2152 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2153 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2155 rc = (rv64 > lv64) ? 1 : -1;
2162 return (reverse ? 1 : -1);
2164 return (reverse ? -1 : 1);
2171 static inline const char *
2172 us_type2str(unsigned field_type)
2174 switch (field_type) {
2175 case USTYPE_PSX_USR:
2176 return ("POSIX User");
2177 case USTYPE_PSX_GRP:
2178 return ("POSIX Group");
2179 case USTYPE_SMB_USR:
2180 return ("SMB User");
2181 case USTYPE_SMB_GRP:
2182 return ("SMB Group");
2184 return ("Undefined");
2192 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2194 us_cbdata_t *cb = (us_cbdata_t *)arg;
2195 zfs_userquota_prop_t prop = cb->cb_prop;
2201 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2202 uu_avl_t *avl = cb->cb_avl;
2206 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2208 const char *typestr;
2212 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2214 if (domain == NULL || domain[0] == '\0') {
2216 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2217 type = USTYPE_PSX_GRP;
2218 struct group *g = getgrgid(rid);
2222 type = USTYPE_PSX_USR;
2223 struct passwd *p = getpwuid(rid);
2229 char sid[ZFS_MAXNAMELEN+32];
2233 directory_error_t e;
2235 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2237 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2238 type = USTYPE_SMB_GRP;
2239 err = sid_to_id(sid, B_FALSE, &id);
2241 type = USTYPE_SMB_USR;
2242 err = sid_to_id(sid, B_TRUE, &id);
2248 e = directory_name_from_sid(NULL, sid, &name, &classes);
2250 directory_error_free(e);
2259 #endif /* HAVE_IDMAP */
2263 * if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA)
2269 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED)
2274 (void) snprintf(namebuf, sizeof (namebuf), "%u", rid);
2279 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2281 (void) sprintf(sizebuf, "%llu", (u_longlong_t)space);
2283 node = safe_malloc(sizeof (us_node_t));
2284 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2286 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
2291 if (nvlist_add_uint32(props, "type", type) != 0)
2294 if (cb->cb_numname) {
2295 if (nvlist_add_uint32(props, "name", rid) != 0)
2297 namelen = strlen(namebuf);
2299 if (nvlist_add_string(props, "name", name) != 0)
2301 namelen = strlen(name);
2304 typestr = us_type2str(type);
2305 typelen = strlen(gettext(typestr));
2306 if (typelen > cb->cb_max_typelen)
2307 cb->cb_max_typelen = typelen;
2309 if (namelen > cb->cb_max_namelen)
2310 cb->cb_max_namelen = namelen;
2312 sizelen = strlen(sizebuf);
2313 if (0 == strcmp(propname, "used")) {
2314 if (sizelen > cb->cb_max_usedlen)
2315 cb->cb_max_usedlen = sizelen;
2317 if (sizelen > cb->cb_max_quotalen)
2318 cb->cb_max_quotalen = sizelen;
2321 node->usn_nvl = props;
2323 n = uu_avl_find(avl, node, &sortinfo, &idx);
2325 uu_avl_insert(avl, node, idx);
2330 props = node->usn_nvl;
2333 if (nvlist_add_uint64(props, propname, space) != 0)
2339 static inline boolean_t
2340 usprop_check(zfs_userquota_prop_t p, unsigned types, unsigned props)
2346 case ZFS_PROP_USERUSED:
2350 case ZFS_PROP_USERQUOTA:
2352 prop = USPROP_QUOTA;
2354 case ZFS_PROP_GROUPUSED:
2358 case ZFS_PROP_GROUPQUOTA:
2360 prop = USPROP_QUOTA;
2366 return (type & types && prop & props);
2369 #define USFIELD_TYPE (1 << 0)
2370 #define USFIELD_NAME (1 << 1)
2371 #define USFIELD_USED (1 << 2)
2372 #define USFIELD_QUOTA (1 << 3)
2373 #define USFIELD_ALL (USFIELD_TYPE | USFIELD_NAME | USFIELD_USED | USFIELD_QUOTA)
2376 parsefields(unsigned *fieldsp, char **names, unsigned *bits, size_t len)
2378 char *field = optarg;
2383 boolean_t found = B_FALSE;
2384 delim = strchr(field, ',');
2388 for (i = 0; i < len; i++)
2389 if (0 == strcmp(field, names[i])) {
2391 *fieldsp |= bits[i];
2396 (void) fprintf(stderr, gettext("invalid type '%s'"
2397 "for -t option\n"), field);
2408 static char *type_names[] = { "posixuser", "smbuser", "posixgroup", "smbgroup",
2410 static unsigned type_bits[] = {
2418 static char *us_field_names[] = { "type", "name", "used", "quota" };
2419 static unsigned us_field_bits[] = {
2427 print_us_node(boolean_t scripted, boolean_t parseable, unsigned fields,
2428 size_t type_width, size_t name_width, size_t used_width,
2429 size_t quota_width, us_node_t *node)
2431 nvlist_t *nvl = node->usn_nvl;
2432 nvpair_t *nvp = NULL;
2433 char valstr[ZFS_MAXNAMELEN];
2434 boolean_t first = B_TRUE;
2435 boolean_t quota_found = B_FALSE;
2437 if (fields & USFIELD_QUOTA && !nvlist_exists(nvl, "quota"))
2438 if (nvlist_add_string(nvl, "quota", "none") != 0)
2441 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2442 char *pname = nvpair_name(nvp);
2443 data_type_t type = nvpair_type(nvp);
2446 char *strval = NULL;
2450 for (i = 0; i < 4; i++) {
2451 if (0 == strcmp(pname, us_field_names[i])) {
2452 field = us_field_bits[i];
2457 if (!(field & fields))
2461 case DATA_TYPE_UINT32:
2462 (void) nvpair_value_uint32(nvp, &val32);
2464 case DATA_TYPE_UINT64:
2465 (void) nvpair_value_uint64(nvp, &val64);
2467 case DATA_TYPE_STRING:
2468 (void) nvpair_value_string(nvp, &strval);
2471 (void) fprintf(stderr, "Invalid data type\n");
2476 (void) printf("\t");
2483 strval = (char *)us_type2str(val32);
2487 if (type == DATA_TYPE_UINT64) {
2488 (void) sprintf(valstr, "%llu",
2489 (u_longlong_t) val64);
2496 if (type == DATA_TYPE_UINT64) {
2497 (void) nvpair_value_uint64(nvp, &val64);
2499 (void) sprintf(valstr, "%llu",
2500 (u_longlong_t) val64);
2502 zfs_nicenum(val64, valstr,
2507 if (field == USFIELD_USED)
2510 quota_found = B_FALSE;
2511 width = quota_width;
2517 if (field == USFIELD_QUOTA && !quota_found)
2518 (void) printf("%*s", width, strval);
2520 if (type == DATA_TYPE_STRING)
2521 (void) printf("%-*s", width, strval);
2523 (void) printf("%*s", width, strval);
2530 (void) printf("\n");
2534 print_us(boolean_t scripted, boolean_t parsable, unsigned fields,
2535 unsigned type_width, unsigned name_width, unsigned used_width,
2536 unsigned quota_width, boolean_t rmnode, uu_avl_t *avl)
2538 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2542 int width[4] = { type_width, name_width, used_width, quota_width };
2545 boolean_t first = B_TRUE;
2546 for (i = 0; i < 4; i++) {
2547 unsigned field = us_field_bits[i];
2548 if (!(field & fields))
2551 col = gettext(us_field_hdr[i]);
2552 if (field == USFIELD_TYPE || field == USFIELD_NAME)
2553 (void) printf(first?"%-*s":" %-*s", width[i],
2556 (void) printf(first?"%*s":" %*s", width[i],
2560 (void) printf("\n");
2563 for (node = uu_avl_first(avl); node != NULL;
2564 node = uu_avl_next(avl, node)) {
2565 print_us_node(scripted, parsable, fields, type_width,
2566 name_width, used_width, used_width, node);
2568 nvlist_free(node->usn_nvl);
2573 zfs_do_userspace(int argc, char **argv)
2576 zfs_userquota_prop_t p;
2577 uu_avl_pool_t *avl_pool;
2579 uu_avl_walk_t *walk;
2582 boolean_t scripted = B_FALSE;
2583 boolean_t prtnum = B_FALSE;
2584 boolean_t parseable = B_FALSE;
2585 boolean_t sid2posix = B_FALSE;
2588 zfs_sort_column_t *default_sortcol = NULL;
2589 zfs_sort_column_t *sortcol = NULL;
2590 unsigned types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2591 unsigned fields = 0;
2592 unsigned props = USPROP_USED | USPROP_QUOTA;
2595 boolean_t resort_avl = B_FALSE;
2601 if (0 == strcmp(cmd, "groupspace"))
2602 /* toggle default group types */
2603 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2606 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2618 if (parsefields(&fields, us_field_names, us_field_bits,
2623 if (zfs_add_sort_column(&sortcol, optarg,
2625 (void) fprintf(stderr,
2626 gettext("invalid property '%s'\n"), optarg);
2631 if (zfs_add_sort_column(&sortcol, optarg,
2633 (void) fprintf(stderr,
2634 gettext("invalid property '%s'\n"), optarg);
2639 if (parsefields(&types, type_names, type_bits, 5))
2646 (void) fprintf(stderr, gettext("missing argument for "
2647 "'%c' option\n"), optopt);
2651 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2660 /* ok, now we have sorted by default colums (type,name) avl tree */
2662 zfs_sort_column_t *sc;
2663 for (sc = sortcol; sc; sc = sc->sc_next) {
2664 if (sc->sc_prop == ZFS_PROP_QUOTA) {
2665 resort_avl = B_TRUE;
2672 fields = USFIELD_ALL;
2674 if ((zhp = zfs_open(g_zfs, argv[argc-1], ZFS_TYPE_DATASET)) == NULL)
2677 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2678 offsetof(us_node_t, usn_avlnode),
2679 us_compare, UU_DEFAULT)) == NULL)
2681 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2684 if (sortcol && !resort_avl)
2685 cb.cb_sortcol = sortcol;
2687 (void) zfs_add_sort_column(&default_sortcol, "type", B_FALSE);
2688 (void) zfs_add_sort_column(&default_sortcol, "name", B_FALSE);
2689 cb.cb_sortcol = default_sortcol;
2691 cb.cb_numname = prtnum;
2692 cb.cb_nicenum = !parseable;
2693 cb.cb_avl_pool = avl_pool;
2694 cb.cb_avl = avl_tree;
2695 cb.cb_sid2posix = sid2posix;
2696 cb.cb_max_typelen = strlen(gettext("TYPE"));
2697 cb.cb_max_namelen = strlen(gettext("NAME"));
2698 cb.cb_max_usedlen = strlen(gettext("USED"));
2699 cb.cb_max_quotalen = strlen(gettext("QUOTA"));
2701 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2702 if (!usprop_check(p, types, props))
2706 error = zfs_userspace(zhp, p, userspace_cb, &cb);
2715 uu_list_pool_t *listpool;
2717 uu_avl_index_t idx = 0;
2718 uu_list_index_t idx2 = 0;
2719 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2720 offsetof(us_node_t, usn_listnode), NULL,
2722 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2724 node = uu_avl_first(avl_tree);
2725 uu_list_node_init(node, &node->usn_listnode, listpool);
2726 while (node != NULL) {
2728 node = uu_avl_next(avl_tree, node);
2729 uu_avl_remove(avl_tree, rmnode);
2730 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL) {
2731 uu_list_insert(list, rmnode, idx2);
2735 for (node = uu_list_first(list); node != NULL;
2736 node = uu_list_next(list, node)) {
2737 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2738 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) ==
2740 uu_avl_insert(avl_tree, node, idx);
2743 uu_list_destroy(list);
2746 /* print & free node`s nvlist memory */
2747 print_us(scripted, parseable, fields, cb.cb_max_typelen,
2748 cb.cb_max_namelen, cb.cb_max_usedlen,
2749 cb.cb_max_quotalen, B_TRUE, cb.cb_avl);
2752 zfs_free_sort_columns(sortcol);
2753 zfs_free_sort_columns(default_sortcol);
2756 * Finally, clean up the AVL tree.
2758 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2761 while ((node = uu_avl_walk_next(walk)) != NULL) {
2762 uu_avl_remove(cb.cb_avl, node);
2766 uu_avl_walk_end(walk);
2767 uu_avl_destroy(avl_tree);
2768 uu_avl_pool_destroy(avl_pool);
2774 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2775 * [-s property [-s property]...] [-S property [-S property]...]
2778 * -r Recurse over all children
2779 * -d Limit recursion by depth.
2780 * -H Scripted mode; elide headers and separate columns by tabs
2781 * -o Control which fields to display.
2782 * -t Control which object types to display.
2783 * -s Specify sort columns, descending order.
2784 * -S Specify sort columns, ascending order.
2786 * When given no arguments, lists all filesystems in the system.
2787 * Otherwise, list the specified datasets, optionally recursing down them if
2788 * '-r' is specified.
2790 typedef struct list_cbdata {
2792 boolean_t cb_scripted;
2793 zprop_list_t *cb_proplist;
2797 * Given a list of columns to display, output appropriate headers for each one.
2800 print_header(zprop_list_t *pl)
2802 char headerbuf[ZFS_MAXPROPLEN];
2805 boolean_t first = B_TRUE;
2806 boolean_t right_justify;
2808 for (; pl != NULL; pl = pl->pl_next) {
2815 right_justify = B_FALSE;
2816 if (pl->pl_prop != ZPROP_INVAL) {
2817 header = zfs_prop_column_name(pl->pl_prop);
2818 right_justify = zfs_prop_align_right(pl->pl_prop);
2820 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2821 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2822 headerbuf[i] = '\0';
2826 if (pl->pl_next == NULL && !right_justify)
2827 (void) printf("%s", header);
2828 else if (right_justify)
2829 (void) printf("%*s", (int)pl->pl_width, header);
2831 (void) printf("%-*s", (int)pl->pl_width, header);
2834 (void) printf("\n");
2838 * Given a dataset and a list of fields, print out all the properties according
2839 * to the described layout.
2842 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
2844 boolean_t first = B_TRUE;
2845 char property[ZFS_MAXPROPLEN];
2846 nvlist_t *userprops = zfs_get_user_props(zhp);
2849 boolean_t right_justify;
2852 for (; pl != NULL; pl = pl->pl_next) {
2855 (void) printf("\t");
2862 if (pl->pl_prop == ZFS_PROP_NAME) {
2863 (void) strlcpy(property, zfs_get_name(zhp),
2866 right_justify = zfs_prop_align_right(pl->pl_prop);
2867 } else if (pl->pl_prop != ZPROP_INVAL) {
2868 if (zfs_prop_get(zhp, pl->pl_prop, property,
2869 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2874 right_justify = zfs_prop_align_right(pl->pl_prop);
2875 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2876 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2877 property, sizeof (property), B_FALSE) != 0)
2881 right_justify = B_TRUE;
2882 } else if (zfs_prop_written(pl->pl_user_prop)) {
2883 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2884 property, sizeof (property), B_FALSE) != 0)
2888 right_justify = B_TRUE;
2890 if (nvlist_lookup_nvlist(userprops,
2891 pl->pl_user_prop, &propval) != 0)
2894 verify(nvlist_lookup_string(propval,
2895 ZPROP_VALUE, &propstr) == 0);
2896 right_justify = B_FALSE;
2899 width = pl->pl_width;
2902 * If this is being called in scripted mode, or if this is the
2903 * last column and it is left-justified, don't include a width
2906 if (scripted || (pl->pl_next == NULL && !right_justify))
2907 (void) printf("%s", propstr);
2908 else if (right_justify)
2909 (void) printf("%*s", width, propstr);
2911 (void) printf("%-*s", width, propstr);
2914 (void) printf("\n");
2918 * Generic callback function to list a dataset or snapshot.
2921 list_callback(zfs_handle_t *zhp, void *data)
2923 list_cbdata_t *cbp = data;
2925 if (cbp->cb_first) {
2926 if (!cbp->cb_scripted)
2927 print_header(cbp->cb_proplist);
2928 cbp->cb_first = B_FALSE;
2931 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2937 zfs_do_list(int argc, char **argv)
2940 boolean_t scripted = B_FALSE;
2941 static char default_fields[] =
2942 "name,used,available,referenced,mountpoint";
2943 int types = ZFS_TYPE_DATASET;
2944 boolean_t types_specified = B_FALSE;
2945 char *fields = NULL;
2946 list_cbdata_t cb = { 0 };
2950 zfs_sort_column_t *sortcol = NULL;
2951 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2954 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2960 limit = parse_depth(optarg, &flags);
2963 flags |= ZFS_ITER_RECURSE;
2969 if (zfs_add_sort_column(&sortcol, optarg,
2971 (void) fprintf(stderr,
2972 gettext("invalid property '%s'\n"), optarg);
2977 if (zfs_add_sort_column(&sortcol, optarg,
2979 (void) fprintf(stderr,
2980 gettext("invalid property '%s'\n"), optarg);
2986 types_specified = B_TRUE;
2987 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2988 while (*optarg != '\0') {
2989 static char *type_subopts[] = { "filesystem",
2990 "volume", "snapshot", "snap", "all", NULL };
2992 switch (getsubopt(&optarg, type_subopts,
2995 types |= ZFS_TYPE_FILESYSTEM;
2998 types |= ZFS_TYPE_VOLUME;
3002 types |= ZFS_TYPE_SNAPSHOT;
3005 types = ZFS_TYPE_DATASET;
3009 (void) fprintf(stderr,
3010 gettext("invalid type '%s'\n"),
3017 (void) fprintf(stderr, gettext("missing argument for "
3018 "'%c' option\n"), optopt);
3022 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3032 fields = default_fields;
3035 * If we are only going to list snapshot names and sort by name,
3036 * then we can use faster version.
3038 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3039 flags |= ZFS_ITER_SIMPLE;
3042 * If "-o space" and no types were specified, don't display snapshots.
3044 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3045 types &= ~ZFS_TYPE_SNAPSHOT;
3048 * If the user specifies '-o all', the zprop_get_list() doesn't
3049 * normally include the name of the dataset. For 'zfs list', we always
3050 * want this property to be first.
3052 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3056 cb.cb_scripted = scripted;
3057 cb.cb_first = B_TRUE;
3059 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3060 limit, list_callback, &cb);
3062 zprop_free_list(cb.cb_proplist);
3063 zfs_free_sort_columns(sortcol);
3065 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3066 (void) fprintf(stderr, gettext("no datasets available\n"));
3072 * zfs rename <fs | snap | vol> <fs | snap | vol>
3073 * zfs rename -p <fs | vol> <fs | vol>
3074 * zfs rename -r <snap> <snap>
3076 * Renames the given dataset to another of the same type.
3078 * The '-p' flag creates all the non-existing ancestors of the target first.
3082 zfs_do_rename(int argc, char **argv)
3087 boolean_t recurse = B_FALSE;
3088 boolean_t parents = B_FALSE;
3091 while ((c = getopt(argc, argv, "pr")) != -1) {
3101 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3110 /* check number of arguments */
3112 (void) fprintf(stderr, gettext("missing source dataset "
3117 (void) fprintf(stderr, gettext("missing target dataset "
3122 (void) fprintf(stderr, gettext("too many arguments\n"));
3126 if (recurse && parents) {
3127 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3132 if (recurse && strchr(argv[0], '@') == 0) {
3133 (void) fprintf(stderr, gettext("source dataset for recursive "
3134 "rename must be a snapshot\n"));
3138 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3139 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3142 /* If we were asked and the name looks good, try to create ancestors. */
3143 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3144 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3149 ret = (zfs_rename(zhp, argv[1], recurse) != 0);
3158 * Promotes the given clone fs to be the parent
3162 zfs_do_promote(int argc, char **argv)
3168 if (argc > 1 && argv[1][0] == '-') {
3169 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3174 /* check number of arguments */
3176 (void) fprintf(stderr, gettext("missing clone filesystem"
3181 (void) fprintf(stderr, gettext("too many arguments\n"));
3185 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3189 ret = (zfs_promote(zhp) != 0);
3197 * zfs rollback [-rRf] <snapshot>
3199 * -r Delete any intervening snapshots before doing rollback
3200 * -R Delete any snapshots and their clones
3201 * -f ignored for backwards compatability
3203 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3204 * since then and making it the active dataset. If more recent snapshots exist,
3205 * the command will complain unless the '-r' flag is given.
3207 typedef struct rollback_cbdata {
3213 boolean_t cb_recurse;
3214 boolean_t cb_dependent;
3215 } rollback_cbdata_t;
3218 * Report any snapshots more recent than the one specified. Used when '-r' is
3219 * not specified. We reuse this same callback for the snapshot dependents - if
3220 * 'cb_dependent' is set, then this is a dependent and we should report it
3221 * without checking the transaction group.
3224 rollback_check(zfs_handle_t *zhp, void *data)
3226 rollback_cbdata_t *cbp = data;
3228 if (cbp->cb_doclones) {
3233 if (!cbp->cb_dependent) {
3234 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
3235 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3236 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3239 if (cbp->cb_first && !cbp->cb_recurse) {
3240 (void) fprintf(stderr, gettext("cannot "
3241 "rollback to '%s': more recent snapshots "
3244 (void) fprintf(stderr, gettext("use '-r' to "
3245 "force deletion of the following "
3251 if (cbp->cb_recurse) {
3252 cbp->cb_dependent = B_TRUE;
3253 if (zfs_iter_dependents(zhp, B_TRUE,
3254 rollback_check, cbp) != 0) {
3258 cbp->cb_dependent = B_FALSE;
3260 (void) fprintf(stderr, "%s\n",
3265 if (cbp->cb_first && cbp->cb_recurse) {
3266 (void) fprintf(stderr, gettext("cannot rollback to "
3267 "'%s': clones of previous snapshots exist\n"),
3269 (void) fprintf(stderr, gettext("use '-R' to "
3270 "force deletion of the following clones and "
3276 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3284 zfs_do_rollback(int argc, char **argv)
3288 boolean_t force = B_FALSE;
3289 rollback_cbdata_t cb = { 0 };
3290 zfs_handle_t *zhp, *snap;
3291 char parentname[ZFS_MAXNAMELEN];
3295 while ((c = getopt(argc, argv, "rRf")) != -1) {
3308 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3317 /* check number of arguments */
3319 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3323 (void) fprintf(stderr, gettext("too many arguments\n"));
3327 /* open the snapshot */
3328 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3331 /* open the parent dataset */
3332 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3333 verify((delim = strrchr(parentname, '@')) != NULL);
3335 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3341 * Check for more recent snapshots and/or clones based on the presence
3344 cb.cb_target = argv[0];
3345 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3346 cb.cb_first = B_TRUE;
3348 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
3351 if ((ret = cb.cb_error) != 0)
3355 * Rollback parent to the given snapshot.
3357 ret = zfs_rollback(zhp, snap, force);
3370 * zfs set property=value { fs | snap | vol } ...
3372 * Sets the given property for all datasets specified on the command line.
3374 typedef struct set_cbdata {
3380 set_callback(zfs_handle_t *zhp, void *data)
3382 set_cbdata_t *cbp = data;
3384 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3385 switch (libzfs_errno(g_zfs)) {
3386 case EZFS_MOUNTFAILED:
3387 (void) fprintf(stderr, gettext("property may be set "
3388 "but unable to remount filesystem\n"));
3390 case EZFS_SHARENFSFAILED:
3391 (void) fprintf(stderr, gettext("property may be set "
3392 "but unable to reshare filesystem\n"));
3401 zfs_do_set(int argc, char **argv)
3406 /* check for options */
3407 if (argc > 1 && argv[1][0] == '-') {
3408 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3413 /* check number of arguments */
3415 (void) fprintf(stderr, gettext("missing property=value "
3420 (void) fprintf(stderr, gettext("missing dataset name\n"));
3424 /* validate property=value argument */
3425 cb.cb_propname = argv[1];
3426 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3427 (cb.cb_value[1] == '\0')) {
3428 (void) fprintf(stderr, gettext("missing value in "
3429 "property=value argument\n"));
3433 *cb.cb_value = '\0';
3436 if (*cb.cb_propname == '\0') {
3437 (void) fprintf(stderr,
3438 gettext("missing property in property=value argument\n"));
3442 ret = zfs_for_each(argc - 2, argv + 2, 0,
3443 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3449 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3451 * Creates a snapshot with the given name. While functionally equivalent to
3452 * 'zfs create', it is a separate command to differentiate intent.
3455 zfs_do_snapshot(int argc, char **argv)
3457 boolean_t recursive = B_FALSE;
3462 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3466 while ((c = getopt(argc, argv, "ro:")) != -1) {
3469 if (parseprop(props))
3476 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3485 /* check number of arguments */
3487 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3491 (void) fprintf(stderr, gettext("too many arguments\n"));
3495 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
3497 if (ret && recursive)
3498 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3508 * Send a backup stream to stdout.
3511 zfs_do_send(int argc, char **argv)
3513 char *fromname = NULL;
3514 char *toname = NULL;
3517 sendflags_t flags = { 0 };
3519 nvlist_t *dbgnv = NULL;
3520 boolean_t extraverbose = B_FALSE;
3523 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3534 flags.doall = B_TRUE;
3537 flags.replicate = B_TRUE;
3540 flags.props = B_TRUE;
3543 flags.parsable = B_TRUE;
3544 flags.verbose = B_TRUE;
3548 extraverbose = B_TRUE;
3549 flags.verbose = B_TRUE;
3552 flags.dedup = B_TRUE;
3555 flags.dryrun = B_TRUE;
3558 (void) fprintf(stderr, gettext("missing argument for "
3559 "'%c' option\n"), optopt);
3563 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3572 /* check number of arguments */
3574 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3578 (void) fprintf(stderr, gettext("too many arguments\n"));
3582 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3583 (void) fprintf(stderr,
3584 gettext("Error: Stream can not be written to a terminal.\n"
3585 "You must redirect standard output.\n"));
3589 cp = strchr(argv[0], '@');
3591 (void) fprintf(stderr,
3592 gettext("argument must be a snapshot\n"));
3597 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3602 * If they specified the full path to the snapshot, chop off
3603 * everything except the short name of the snapshot, but special
3604 * case if they specify the origin.
3606 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3607 char origin[ZFS_MAXNAMELEN];
3610 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3611 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3613 if (strcmp(origin, fromname) == 0) {
3615 flags.fromorigin = B_TRUE;
3618 if (cp != fromname && strcmp(argv[0], fromname)) {
3619 (void) fprintf(stderr,
3620 gettext("incremental source must be "
3621 "in same filesystem\n"));
3625 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3626 (void) fprintf(stderr,
3627 gettext("invalid incremental source\n"));
3633 if (flags.replicate && fromname == NULL)
3634 flags.doall = B_TRUE;
3636 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3637 extraverbose ? &dbgnv : NULL);
3639 if (extraverbose && dbgnv != NULL) {
3641 * dump_nvlist prints to stdout, but that's been
3642 * redirected to a file. Make it print to stderr
3645 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3646 dump_nvlist(dbgnv, 0);
3655 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3657 * Restore a backup stream from stdin.
3660 zfs_do_receive(int argc, char **argv)
3663 recvflags_t flags = { 0 };
3666 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3669 flags.isprefix = B_TRUE;
3672 flags.isprefix = B_TRUE;
3673 flags.istail = B_TRUE;
3676 flags.dryrun = B_TRUE;
3679 flags.nomount = B_TRUE;
3682 flags.verbose = B_TRUE;
3685 flags.force = B_TRUE;
3688 (void) fprintf(stderr, gettext("missing argument for "
3689 "'%c' option\n"), optopt);
3693 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3702 /* check number of arguments */
3704 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3708 (void) fprintf(stderr, gettext("too many arguments\n"));
3712 if (isatty(STDIN_FILENO)) {
3713 (void) fprintf(stderr,
3714 gettext("Error: Backup stream can not be read "
3715 "from a terminal.\n"
3716 "You must redirect standard input.\n"));
3720 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3726 * allow/unallow stuff
3728 /* copied from zfs/sys/dsl_deleg.h */
3729 #define ZFS_DELEG_PERM_CREATE "create"
3730 #define ZFS_DELEG_PERM_DESTROY "destroy"
3731 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3732 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3733 #define ZFS_DELEG_PERM_CLONE "clone"
3734 #define ZFS_DELEG_PERM_PROMOTE "promote"
3735 #define ZFS_DELEG_PERM_RENAME "rename"
3736 #define ZFS_DELEG_PERM_MOUNT "mount"
3737 #define ZFS_DELEG_PERM_SHARE "share"
3738 #define ZFS_DELEG_PERM_SEND "send"
3739 #define ZFS_DELEG_PERM_RECEIVE "receive"
3740 #define ZFS_DELEG_PERM_ALLOW "allow"
3741 #define ZFS_DELEG_PERM_USERPROP "userprop"
3742 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3743 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3744 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3745 #define ZFS_DELEG_PERM_USERUSED "userused"
3746 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3747 #define ZFS_DELEG_PERM_HOLD "hold"
3748 #define ZFS_DELEG_PERM_RELEASE "release"
3749 #define ZFS_DELEG_PERM_DIFF "diff"
3751 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3753 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3754 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3755 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3756 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3757 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3758 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3759 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3760 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3761 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3762 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3763 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3764 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3765 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3766 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3767 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3768 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3770 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3771 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3772 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3773 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3774 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3775 { NULL, ZFS_DELEG_NOTE_NONE }
3778 /* permission structure */
3779 typedef struct deleg_perm {
3780 zfs_deleg_who_type_t dp_who_type;
3781 const char *dp_name;
3783 boolean_t dp_descend;
3787 typedef struct deleg_perm_node {
3788 deleg_perm_t dpn_perm;
3790 uu_avl_node_t dpn_avl_node;
3791 } deleg_perm_node_t;
3793 typedef struct fs_perm fs_perm_t;
3795 /* permissions set */
3796 typedef struct who_perm {
3797 zfs_deleg_who_type_t who_type;
3798 const char *who_name; /* id */
3799 char who_ug_name[256]; /* user/group name */
3800 fs_perm_t *who_fsperm; /* uplink */
3802 uu_avl_t *who_deleg_perm_avl; /* permissions */
3806 typedef struct who_perm_node {
3807 who_perm_t who_perm;
3808 uu_avl_node_t who_avl_node;
3811 typedef struct fs_perm_set fs_perm_set_t;
3812 /* fs permissions */
3814 const char *fsp_name;
3816 uu_avl_t *fsp_sc_avl; /* sets,create */
3817 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3819 fs_perm_set_t *fsp_set; /* uplink */
3823 typedef struct fs_perm_node {
3824 fs_perm_t fspn_fsperm;
3827 uu_list_node_t fspn_list_node;
3830 /* top level structure */
3831 struct fs_perm_set {
3832 uu_list_pool_t *fsps_list_pool;
3833 uu_list_t *fsps_list; /* list of fs_perms */
3835 uu_avl_pool_t *fsps_named_set_avl_pool;
3836 uu_avl_pool_t *fsps_who_perm_avl_pool;
3837 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
3840 static inline const char *
3841 deleg_perm_type(zfs_deleg_note_t note)
3847 case ZFS_DELEG_NOTE_GROUPQUOTA:
3848 case ZFS_DELEG_NOTE_GROUPUSED:
3849 case ZFS_DELEG_NOTE_USERPROP:
3850 case ZFS_DELEG_NOTE_USERQUOTA:
3851 case ZFS_DELEG_NOTE_USERUSED:
3853 return (gettext("other"));
3855 return (gettext("subcommand"));
3860 who_type2weight(zfs_deleg_who_type_t who_type)
3864 case ZFS_DELEG_NAMED_SET_SETS:
3865 case ZFS_DELEG_NAMED_SET:
3868 case ZFS_DELEG_CREATE_SETS:
3869 case ZFS_DELEG_CREATE:
3872 case ZFS_DELEG_USER_SETS:
3873 case ZFS_DELEG_USER:
3876 case ZFS_DELEG_GROUP_SETS:
3877 case ZFS_DELEG_GROUP:
3880 case ZFS_DELEG_EVERYONE_SETS:
3881 case ZFS_DELEG_EVERYONE:
3893 who_perm_compare(const void *larg, const void *rarg, void *unused)
3895 const who_perm_node_t *l = larg;
3896 const who_perm_node_t *r = rarg;
3897 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
3898 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
3899 int lweight = who_type2weight(ltype);
3900 int rweight = who_type2weight(rtype);
3901 int res = lweight - rweight;
3903 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
3904 ZFS_MAX_DELEG_NAME-1);
3916 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
3918 const deleg_perm_node_t *l = larg;
3919 const deleg_perm_node_t *r = rarg;
3920 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
3921 ZFS_MAX_DELEG_NAME-1);
3933 fs_perm_set_init(fs_perm_set_t *fspset)
3935 bzero(fspset, sizeof (fs_perm_set_t));
3937 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
3938 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
3939 NULL, UU_DEFAULT)) == NULL)
3941 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
3942 UU_DEFAULT)) == NULL)
3945 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
3946 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
3947 who_perm_node_t, who_avl_node), who_perm_compare,
3948 UU_DEFAULT)) == NULL)
3951 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
3952 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
3953 who_perm_node_t, who_avl_node), who_perm_compare,
3954 UU_DEFAULT)) == NULL)
3957 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
3958 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
3959 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
3964 static inline void fs_perm_fini(fs_perm_t *);
3965 static inline void who_perm_fini(who_perm_t *);
3968 fs_perm_set_fini(fs_perm_set_t *fspset)
3970 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
3972 while (node != NULL) {
3973 fs_perm_node_t *next_node =
3974 uu_list_next(fspset->fsps_list, node);
3975 fs_perm_t *fsperm = &node->fspn_fsperm;
3976 fs_perm_fini(fsperm);
3977 uu_list_remove(fspset->fsps_list, node);
3982 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
3983 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
3984 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
3988 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
3991 deleg_perm->dp_who_type = type;
3992 deleg_perm->dp_name = name;
3996 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
3997 zfs_deleg_who_type_t type, const char *name)
3999 uu_avl_pool_t *pool;
4000 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4002 bzero(who_perm, sizeof (who_perm_t));
4004 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4005 UU_DEFAULT)) == NULL)
4008 who_perm->who_type = type;
4009 who_perm->who_name = name;
4010 who_perm->who_fsperm = fsperm;
4014 who_perm_fini(who_perm_t *who_perm)
4016 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4018 while (node != NULL) {
4019 deleg_perm_node_t *next_node =
4020 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4022 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4027 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4031 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4033 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4034 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4036 bzero(fsperm, sizeof (fs_perm_t));
4038 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4042 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4046 fsperm->fsp_set = fspset;
4047 fsperm->fsp_name = fsname;
4051 fs_perm_fini(fs_perm_t *fsperm)
4053 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4054 while (node != NULL) {
4055 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4057 who_perm_t *who_perm = &node->who_perm;
4058 who_perm_fini(who_perm);
4059 uu_avl_remove(fsperm->fsp_sc_avl, node);
4064 node = uu_avl_first(fsperm->fsp_uge_avl);
4065 while (node != NULL) {
4066 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4068 who_perm_t *who_perm = &node->who_perm;
4069 who_perm_fini(who_perm);
4070 uu_avl_remove(fsperm->fsp_uge_avl, node);
4075 uu_avl_destroy(fsperm->fsp_sc_avl);
4076 uu_avl_destroy(fsperm->fsp_uge_avl);
4080 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4081 zfs_deleg_who_type_t who_type, const char *name, char locality)
4083 uu_avl_index_t idx = 0;
4085 deleg_perm_node_t *found_node = NULL;
4086 deleg_perm_t *deleg_perm = &node->dpn_perm;
4088 deleg_perm_init(deleg_perm, who_type, name);
4090 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4092 uu_avl_insert(avl, node, idx);
4095 deleg_perm = &node->dpn_perm;
4100 case ZFS_DELEG_LOCAL:
4101 deleg_perm->dp_local = B_TRUE;
4103 case ZFS_DELEG_DESCENDENT:
4104 deleg_perm->dp_descend = B_TRUE;
4109 assert(B_FALSE); /* invalid locality */
4114 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4116 nvpair_t *nvp = NULL;
4117 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4118 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4119 zfs_deleg_who_type_t who_type = who_perm->who_type;
4121 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4122 const char *name = nvpair_name(nvp);
4123 data_type_t type = nvpair_type(nvp);
4124 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4125 deleg_perm_node_t *node =
4126 safe_malloc(sizeof (deleg_perm_node_t));
4128 VERIFY(type == DATA_TYPE_BOOLEAN);
4130 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4131 set_deleg_perm_node(avl, node, who_type, name, locality);
4138 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4140 nvpair_t *nvp = NULL;
4141 fs_perm_set_t *fspset = fsperm->fsp_set;
4143 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4144 nvlist_t *nvl2 = NULL;
4145 const char *name = nvpair_name(nvp);
4146 uu_avl_t *avl = NULL;
4147 uu_avl_pool_t *avl_pool = NULL;
4148 zfs_deleg_who_type_t perm_type = name[0];
4149 char perm_locality = name[1];
4150 const char *perm_name = name + 3;
4151 boolean_t is_set = B_TRUE;
4152 who_perm_t *who_perm = NULL;
4154 assert('$' == name[2]);
4156 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4159 switch (perm_type) {
4160 case ZFS_DELEG_CREATE:
4161 case ZFS_DELEG_CREATE_SETS:
4162 case ZFS_DELEG_NAMED_SET:
4163 case ZFS_DELEG_NAMED_SET_SETS:
4164 avl_pool = fspset->fsps_named_set_avl_pool;
4165 avl = fsperm->fsp_sc_avl;
4167 case ZFS_DELEG_USER:
4168 case ZFS_DELEG_USER_SETS:
4169 case ZFS_DELEG_GROUP:
4170 case ZFS_DELEG_GROUP_SETS:
4171 case ZFS_DELEG_EVERYONE:
4172 case ZFS_DELEG_EVERYONE_SETS:
4173 avl_pool = fspset->fsps_who_perm_avl_pool;
4174 avl = fsperm->fsp_uge_avl;
4181 who_perm_node_t *found_node = NULL;
4182 who_perm_node_t *node = safe_malloc(
4183 sizeof (who_perm_node_t));
4184 who_perm = &node->who_perm;
4185 uu_avl_index_t idx = 0;
4187 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4188 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4190 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4192 if (avl == fsperm->fsp_uge_avl) {
4194 struct passwd *p = NULL;
4195 struct group *g = NULL;
4196 const char *nice_name = NULL;
4198 switch (perm_type) {
4199 case ZFS_DELEG_USER_SETS:
4200 case ZFS_DELEG_USER:
4201 rid = atoi(perm_name);
4204 nice_name = p->pw_name;
4206 case ZFS_DELEG_GROUP_SETS:
4207 case ZFS_DELEG_GROUP:
4208 rid = atoi(perm_name);
4211 nice_name = g->gr_name;
4217 if (nice_name != NULL)
4219 node->who_perm.who_ug_name,
4223 uu_avl_insert(avl, node, idx);
4226 who_perm = &node->who_perm;
4230 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4237 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4239 nvpair_t *nvp = NULL;
4240 uu_avl_index_t idx = 0;
4242 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4243 nvlist_t *nvl2 = NULL;
4244 const char *fsname = nvpair_name(nvp);
4245 data_type_t type = nvpair_type(nvp);
4246 fs_perm_t *fsperm = NULL;
4247 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4251 fsperm = &node->fspn_fsperm;
4253 VERIFY(DATA_TYPE_NVLIST == type);
4255 uu_list_node_init(node, &node->fspn_list_node,
4256 fspset->fsps_list_pool);
4258 idx = uu_list_numnodes(fspset->fsps_list);
4259 fs_perm_init(fsperm, fspset, fsname);
4261 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4264 (void) parse_fs_perm(fsperm, nvl2);
4266 uu_list_insert(fspset->fsps_list, node, idx);
4272 static inline const char *
4273 deleg_perm_comment(zfs_deleg_note_t note)
4275 const char *str = "";
4280 case ZFS_DELEG_NOTE_ALLOW:
4281 str = gettext("Must also have the permission that is being"
4282 "\n\t\t\t\tallowed");
4284 case ZFS_DELEG_NOTE_CLONE:
4285 str = gettext("Must also have the 'create' ability and 'mount'"
4286 "\n\t\t\t\tability in the origin file system");
4288 case ZFS_DELEG_NOTE_CREATE:
4289 str = gettext("Must also have the 'mount' ability");
4291 case ZFS_DELEG_NOTE_DESTROY:
4292 str = gettext("Must also have the 'mount' ability");
4294 case ZFS_DELEG_NOTE_DIFF:
4295 str = gettext("Allows lookup of paths within a dataset;"
4296 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4297 "\n\t\t\t\tin order to use zfs diff");
4299 case ZFS_DELEG_NOTE_HOLD:
4300 str = gettext("Allows adding a user hold to a snapshot");
4302 case ZFS_DELEG_NOTE_MOUNT:
4303 str = gettext("Allows mount/umount of ZFS datasets");
4305 case ZFS_DELEG_NOTE_PROMOTE:
4306 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4307 " 'promote' ability in the origin file system");
4309 case ZFS_DELEG_NOTE_RECEIVE:
4310 str = gettext("Must also have the 'mount' and 'create'"
4313 case ZFS_DELEG_NOTE_RELEASE:
4314 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4315 "might destroy the snapshot");
4317 case ZFS_DELEG_NOTE_RENAME:
4318 str = gettext("Must also have the 'mount' and 'create'"
4319 "\n\t\t\t\tability in the new parent");
4321 case ZFS_DELEG_NOTE_ROLLBACK:
4324 case ZFS_DELEG_NOTE_SEND:
4327 case ZFS_DELEG_NOTE_SHARE:
4328 str = gettext("Allows sharing file systems over NFS or SMB"
4329 "\n\t\t\t\tprotocols");
4331 case ZFS_DELEG_NOTE_SNAPSHOT:
4335 * case ZFS_DELEG_NOTE_VSCAN:
4336 * str = gettext("");
4340 case ZFS_DELEG_NOTE_GROUPQUOTA:
4341 str = gettext("Allows accessing any groupquota@... property");
4343 case ZFS_DELEG_NOTE_GROUPUSED:
4344 str = gettext("Allows reading any groupused@... property");
4346 case ZFS_DELEG_NOTE_USERPROP:
4347 str = gettext("Allows changing any user property");
4349 case ZFS_DELEG_NOTE_USERQUOTA:
4350 str = gettext("Allows accessing any userquota@... property");
4352 case ZFS_DELEG_NOTE_USERUSED:
4353 str = gettext("Allows reading any userused@... property");
4371 boolean_t recursive; /* unallow only */
4372 boolean_t prt_usage;
4374 boolean_t prt_perms;
4377 const char *dataset;
4381 prop_cmp(const void *a, const void *b)
4383 const char *str1 = *(const char **)a;
4384 const char *str2 = *(const char **)b;
4385 return (strcmp(str1, str2));
4389 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4391 const char *opt_desc[] = {
4392 "-h", gettext("show this help message and exit"),
4393 "-l", gettext("set permission locally"),
4394 "-d", gettext("set permission for descents"),
4395 "-u", gettext("set permission for user"),
4396 "-g", gettext("set permission for group"),
4397 "-e", gettext("set permission for everyone"),
4398 "-c", gettext("set create time permission"),
4399 "-s", gettext("define permission set"),
4401 "-r", gettext("remove permissions recursively"),
4403 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4404 size_t allow_size = unallow_size - 2;
4405 const char *props[ZFS_NUM_PROPS];
4408 FILE *fp = requested ? stdout : stderr;
4409 zprop_desc_t *pdtbl = zfs_prop_get_table();
4410 const char *fmt = gettext("%-16s %-14s\t%s\n");
4412 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4414 (void) fprintf(fp, gettext("Options:\n"));
4415 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4416 const char *opt = opt_desc[i++];
4417 const char *optdsc = opt_desc[i];
4418 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4421 (void) fprintf(fp, gettext("\nThe following permissions are "
4423 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4425 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4426 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4427 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4428 const char *perm_type = deleg_perm_type(perm_note);
4429 const char *perm_comment = deleg_perm_comment(perm_note);
4430 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4433 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4434 zprop_desc_t *pd = &pdtbl[i];
4435 if (pd->pd_visible != B_TRUE)
4438 if (pd->pd_attr == PROP_READONLY)
4441 props[count++] = pd->pd_name;
4443 props[count] = NULL;
4445 qsort(props, count, sizeof (char *), prop_cmp);
4447 for (i = 0; i < count; i++)
4448 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4451 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4453 exit(requested ? 0 : 2);
4456 static inline const char *
4457 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4460 if (un && argc == expected_argc - 1)
4462 else if (argc == expected_argc)
4463 *permsp = argv[argc - 2];
4465 allow_usage(un, B_FALSE,
4466 gettext("wrong number of parameters\n"));
4468 return (argv[argc - 1]);
4472 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4474 int uge_sum = opts->user + opts->group + opts->everyone;
4475 int csuge_sum = opts->create + opts->set + uge_sum;
4476 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4477 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4480 allow_usage(un, B_FALSE,
4481 gettext("-u, -g, and -e are mutually exclusive\n"));
4483 if (opts->prt_usage) {
4484 if (argc == 0 && all_sum == 0)
4485 allow_usage(un, B_TRUE, NULL);
4492 allow_usage(un, B_FALSE,
4493 gettext("invalid options combined with -s\n"));
4495 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4496 if (argv[0][0] != '@')
4497 allow_usage(un, B_FALSE,
4498 gettext("invalid set name: missing '@' prefix\n"));
4499 opts->who = argv[0];
4500 } else if (opts->create) {
4501 if (ldcsuge_sum > 1)
4502 allow_usage(un, B_FALSE,
4503 gettext("invalid options combined with -c\n"));
4504 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4505 } else if (opts->everyone) {
4507 allow_usage(un, B_FALSE,
4508 gettext("invalid options combined with -e\n"));
4509 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4510 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4512 opts->everyone = B_TRUE;
4515 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4516 } else if (argc == 1 && !un) {
4517 opts->prt_perms = B_TRUE;
4518 opts->dataset = argv[argc-1];
4520 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4521 opts->who = argv[0];
4524 if (!opts->local && !opts->descend) {
4525 opts->local = B_TRUE;
4526 opts->descend = B_TRUE;
4531 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4532 const char *who, char *perms, nvlist_t *top_nvl)
4535 char ld[2] = { '\0', '\0' };
4536 char who_buf[ZFS_MAXNAMELEN+32];
4537 char base_type = ZFS_DELEG_WHO_UNKNOWN;
4538 char set_type = ZFS_DELEG_WHO_UNKNOWN;
4539 nvlist_t *base_nvl = NULL;
4540 nvlist_t *set_nvl = NULL;
4543 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4545 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4549 case ZFS_DELEG_NAMED_SET_SETS:
4550 case ZFS_DELEG_NAMED_SET:
4551 set_type = ZFS_DELEG_NAMED_SET_SETS;
4552 base_type = ZFS_DELEG_NAMED_SET;
4553 ld[0] = ZFS_DELEG_NA;
4555 case ZFS_DELEG_CREATE_SETS:
4556 case ZFS_DELEG_CREATE:
4557 set_type = ZFS_DELEG_CREATE_SETS;
4558 base_type = ZFS_DELEG_CREATE;
4559 ld[0] = ZFS_DELEG_NA;
4561 case ZFS_DELEG_USER_SETS:
4562 case ZFS_DELEG_USER:
4563 set_type = ZFS_DELEG_USER_SETS;
4564 base_type = ZFS_DELEG_USER;
4566 ld[0] = ZFS_DELEG_LOCAL;
4568 ld[1] = ZFS_DELEG_DESCENDENT;
4570 case ZFS_DELEG_GROUP_SETS:
4571 case ZFS_DELEG_GROUP:
4572 set_type = ZFS_DELEG_GROUP_SETS;
4573 base_type = ZFS_DELEG_GROUP;
4575 ld[0] = ZFS_DELEG_LOCAL;
4577 ld[1] = ZFS_DELEG_DESCENDENT;
4579 case ZFS_DELEG_EVERYONE_SETS:
4580 case ZFS_DELEG_EVERYONE:
4581 set_type = ZFS_DELEG_EVERYONE_SETS;
4582 base_type = ZFS_DELEG_EVERYONE;
4584 ld[0] = ZFS_DELEG_LOCAL;
4586 ld[1] = ZFS_DELEG_DESCENDENT;
4591 if (perms != NULL) {
4593 char *end = curr + strlen(perms);
4595 while (curr < end) {
4596 char *delim = strchr(curr, ',');
4607 (void) nvlist_add_boolean(nvl, curr);
4613 for (i = 0; i < 2; i++) {
4614 char locality = ld[i];
4618 if (!nvlist_empty(base_nvl)) {
4620 (void) snprintf(who_buf,
4621 sizeof (who_buf), "%c%c$%s",
4622 base_type, locality, who);
4624 (void) snprintf(who_buf,
4625 sizeof (who_buf), "%c%c$",
4626 base_type, locality);
4628 (void) nvlist_add_nvlist(top_nvl, who_buf,
4633 if (!nvlist_empty(set_nvl)) {
4635 (void) snprintf(who_buf,
4636 sizeof (who_buf), "%c%c$%s",
4637 set_type, locality, who);
4639 (void) snprintf(who_buf,
4640 sizeof (who_buf), "%c%c$",
4641 set_type, locality);
4643 (void) nvlist_add_nvlist(top_nvl, who_buf,
4648 for (i = 0; i < 2; i++) {
4649 char locality = ld[i];
4654 (void) snprintf(who_buf, sizeof (who_buf),
4655 "%c%c$%s", base_type, locality, who);
4657 (void) snprintf(who_buf, sizeof (who_buf),
4658 "%c%c$", base_type, locality);
4659 (void) nvlist_add_boolean(top_nvl, who_buf);
4662 (void) snprintf(who_buf, sizeof (who_buf),
4663 "%c%c$%s", set_type, locality, who);
4665 (void) snprintf(who_buf, sizeof (who_buf),
4666 "%c%c$", set_type, locality);
4667 (void) nvlist_add_boolean(top_nvl, who_buf);
4673 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4675 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4679 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4680 opts->descend, opts->who, opts->perms, *nvlp);
4681 } else if (opts->create) {
4682 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4683 opts->descend, NULL, opts->perms, *nvlp);
4684 } else if (opts->everyone) {
4685 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4686 opts->descend, NULL, opts->perms, *nvlp);
4688 char *curr = opts->who;
4689 char *end = curr + strlen(curr);
4691 while (curr < end) {
4693 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
4695 char *delim = strchr(curr, ',');
4698 struct passwd *p = NULL;
4699 struct group *g = NULL;
4707 rid = (uid_t)strtol(curr, &endch, 0);
4709 who_type = ZFS_DELEG_USER;
4718 (void) snprintf(errbuf, 256, gettext(
4719 "invalid user %s"), curr);
4720 allow_usage(un, B_TRUE, errbuf);
4722 } else if (opts->group) {
4723 who_type = ZFS_DELEG_GROUP;
4732 (void) snprintf(errbuf, 256, gettext(
4733 "invalid group %s"), curr);
4734 allow_usage(un, B_TRUE, errbuf);
4737 if (*endch != '\0') {
4744 if (*endch != '\0') {
4752 who_type = ZFS_DELEG_USER;
4754 } else if (g != NULL) {
4755 who_type = ZFS_DELEG_GROUP;
4758 (void) snprintf(errbuf, 256, gettext(
4759 "invalid user/group %s"), curr);
4760 allow_usage(un, B_TRUE, errbuf);
4764 (void) sprintf(id, "%u", rid);
4767 store_allow_perm(who_type, opts->local,
4768 opts->descend, who, opts->perms, *nvlp);
4777 print_set_creat_perms(uu_avl_t *who_avl)
4779 const char *sc_title[] = {
4780 gettext("Permission sets:\n"),
4781 gettext("Create time permissions:\n"),
4784 const char **title_ptr = sc_title;
4785 who_perm_node_t *who_node = NULL;
4786 int prev_weight = -1;
4788 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4789 who_node = uu_avl_next(who_avl, who_node)) {
4790 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4791 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4792 const char *who_name = who_node->who_perm.who_name;
4793 int weight = who_type2weight(who_type);
4794 boolean_t first = B_TRUE;
4795 deleg_perm_node_t *deleg_node;
4797 if (prev_weight != weight) {
4798 (void) printf("%s", *title_ptr++);
4799 prev_weight = weight;
4802 if (who_name == NULL || strnlen(who_name, 1) == 0)
4803 (void) printf("\t");
4805 (void) printf("\t%s ", who_name);
4807 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4808 deleg_node = uu_avl_next(avl, deleg_node)) {
4811 deleg_node->dpn_perm.dp_name);
4814 (void) printf(",%s",
4815 deleg_node->dpn_perm.dp_name);
4818 (void) printf("\n");
4823 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4826 who_perm_node_t *who_node = NULL;
4827 boolean_t prt_title = B_TRUE;
4828 uu_avl_walk_t *walk;
4830 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4833 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4834 const char *who_name = who_node->who_perm.who_name;
4835 const char *nice_who_name = who_node->who_perm.who_ug_name;
4836 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4837 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4839 deleg_perm_node_t *deleg_node;
4840 boolean_t prt_who = B_TRUE;
4842 for (deleg_node = uu_avl_first(avl);
4844 deleg_node = uu_avl_next(avl, deleg_node)) {
4845 if (local != deleg_node->dpn_perm.dp_local ||
4846 descend != deleg_node->dpn_perm.dp_descend)
4850 const char *who = NULL;
4852 prt_title = B_FALSE;
4853 (void) printf("%s", title);
4857 case ZFS_DELEG_USER_SETS:
4858 case ZFS_DELEG_USER:
4859 who = gettext("user");
4861 who_name = nice_who_name;
4863 case ZFS_DELEG_GROUP_SETS:
4864 case ZFS_DELEG_GROUP:
4865 who = gettext("group");
4867 who_name = nice_who_name;
4869 case ZFS_DELEG_EVERYONE_SETS:
4870 case ZFS_DELEG_EVERYONE:
4871 who = gettext("everyone");
4878 if (who_name == NULL)
4879 (void) printf("\t%s", who);
4881 (void) printf("\t%s %s", who, who_name);
4884 (void) printf("%c%s", delim,
4885 deleg_node->dpn_perm.dp_name);
4890 (void) printf("\n");
4893 uu_avl_walk_end(walk);
4897 print_fs_perms(fs_perm_set_t *fspset)
4899 fs_perm_node_t *node = NULL;
4900 char buf[ZFS_MAXNAMELEN+32];
4901 const char *dsname = buf;
4903 for (node = uu_list_first(fspset->fsps_list); node != NULL;
4904 node = uu_list_next(fspset->fsps_list, node)) {
4905 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
4906 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
4909 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
4910 gettext("---- Permissions on %s "),
4911 node->fspn_fsperm.fsp_name);
4912 (void) printf("%s", dsname);
4913 left = 70 - strlen(buf);
4916 (void) printf("\n");
4918 print_set_creat_perms(sc_avl);
4919 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
4920 gettext("Local permissions:\n"));
4921 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
4922 gettext("Descendent permissions:\n"));
4923 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
4924 gettext("Local+Descendent permissions:\n"));
4928 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
4930 struct deleg_perms {
4936 set_deleg_perms(zfs_handle_t *zhp, void *data)
4938 struct deleg_perms *perms = (struct deleg_perms *)data;
4939 zfs_type_t zfs_type = zfs_get_type(zhp);
4941 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
4944 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
4948 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
4951 nvlist_t *perm_nvl = NULL;
4952 nvlist_t *update_perm_nvl = NULL;
4955 struct allow_opts opts = { 0 };
4957 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
4960 while ((c = getopt(argc, argv, optstr)) != -1) {
4963 opts.local = B_TRUE;
4966 opts.descend = B_TRUE;
4972 opts.group = B_TRUE;
4975 opts.everyone = B_TRUE;
4981 opts.create = B_TRUE;
4984 opts.recursive = B_TRUE;
4987 (void) fprintf(stderr, gettext("missing argument for "
4988 "'%c' option\n"), optopt);
4992 opts.prt_usage = B_TRUE;
4995 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5004 /* check arguments */
5005 parse_allow_args(argc, argv, un, &opts);
5007 /* try to open the dataset */
5008 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5009 ZFS_TYPE_VOLUME)) == NULL) {
5010 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5015 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5018 fs_perm_set_init(&fs_perm_set);
5019 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5020 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5025 print_fs_perms(&fs_perm_set);
5027 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5028 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5031 if (un && opts.recursive) {
5032 struct deleg_perms data = { un, update_perm_nvl };
5033 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5042 nvlist_free(perm_nvl);
5043 if (update_perm_nvl != NULL)
5044 nvlist_free(update_perm_nvl);
5046 fs_perm_set_fini(&fs_perm_set);
5054 * zfs allow [-r] [-t] <tag> <snap> ...
5056 * -r Recursively hold
5057 * -t Temporary hold (hidden option)
5059 * Apply a user-hold with the given tag to the list of snapshots.
5062 zfs_do_allow(int argc, char **argv)
5064 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5068 * zfs unallow [-r] [-t] <tag> <snap> ...
5070 * -r Recursively hold
5071 * -t Temporary hold (hidden option)
5073 * Apply a user-hold with the given tag to the list of snapshots.
5076 zfs_do_unallow(int argc, char **argv)
5078 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5082 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5087 boolean_t recursive = B_FALSE;
5088 boolean_t temphold = B_FALSE;
5089 const char *opts = holding ? "rt" : "r";
5093 while ((c = getopt(argc, argv, opts)) != -1) {
5102 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5111 /* check number of arguments */
5119 if (holding && tag[0] == '.') {
5120 /* tags starting with '.' are reserved for libzfs */
5121 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5125 for (i = 0; i < argc; ++i) {
5127 char parent[ZFS_MAXNAMELEN];
5129 char *path = argv[i];
5131 delim = strchr(path, '@');
5132 if (delim == NULL) {
5133 (void) fprintf(stderr,
5134 gettext("'%s' is not a snapshot\n"), path);
5138 (void) strncpy(parent, path, delim - path);
5139 parent[delim - path] = '\0';
5141 zhp = zfs_open(g_zfs, parent,
5142 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5148 if (zfs_hold(zhp, delim+1, tag, recursive,
5149 temphold, B_FALSE, -1, 0, 0) != 0)
5152 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5158 return (errors != 0);
5162 * zfs hold [-r] [-t] <tag> <snap> ...
5164 * -r Recursively hold
5165 * -t Temporary hold (hidden option)
5167 * Apply a user-hold with the given tag to the list of snapshots.
5170 zfs_do_hold(int argc, char **argv)
5172 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5176 * zfs release [-r] <tag> <snap> ...
5178 * -r Recursively release
5180 * Release a user-hold with the given tag from the list of snapshots.
5183 zfs_do_release(int argc, char **argv)
5185 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5188 typedef struct holds_cbdata {
5189 boolean_t cb_recursive;
5190 const char *cb_snapname;
5192 size_t cb_max_namelen;
5193 size_t cb_max_taglen;
5196 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5197 #define DATETIME_BUF_LEN (32)
5202 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
5205 nvpair_t *nvp = NULL;
5206 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5210 for (i = 0; i < 3; i++) {
5211 col = gettext(hdr_cols[i]);
5213 (void) printf("%-*s ", i ? tagwidth : nwidth,
5216 (void) printf("%s\n", col);
5220 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5221 char *zname = nvpair_name(nvp);
5223 nvpair_t *nvp2 = NULL;
5224 (void) nvpair_value_nvlist(nvp, &nvl2);
5225 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5226 char tsbuf[DATETIME_BUF_LEN];
5227 char *tagname = nvpair_name(nvp2);
5231 char sep = scripted ? '\t' : ' ';
5232 int sepnum = scripted ? 1 : 2;
5234 (void) nvpair_value_uint64(nvp2, &val);
5236 (void) localtime_r(&time, &t);
5237 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5238 gettext(STRFTIME_FMT_STR), &t);
5240 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5241 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5247 * Generic callback function to list a dataset or snapshot.
5250 holds_callback(zfs_handle_t *zhp, void *data)
5252 holds_cbdata_t *cbp = data;
5253 nvlist_t *top_nvl = *cbp->cb_nvlp;
5254 nvlist_t *nvl = NULL;
5255 nvpair_t *nvp = NULL;
5256 const char *zname = zfs_get_name(zhp);
5257 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5259 if (cbp->cb_recursive) {
5260 const char *snapname;
5261 char *delim = strchr(zname, '@');
5265 snapname = delim + 1;
5266 if (strcmp(cbp->cb_snapname, snapname))
5270 if (zfs_get_holds(zhp, &nvl) != 0)
5273 if (znamelen > cbp->cb_max_namelen)
5274 cbp->cb_max_namelen = znamelen;
5276 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5277 const char *tag = nvpair_name(nvp);
5278 size_t taglen = strnlen(tag, MAXNAMELEN);
5279 if (taglen > cbp->cb_max_taglen)
5280 cbp->cb_max_taglen = taglen;
5283 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5287 * zfs holds [-r] <snap> ...
5289 * -r Recursively hold
5292 zfs_do_holds(int argc, char **argv)
5297 boolean_t scripted = B_FALSE;
5298 boolean_t recursive = B_FALSE;
5299 const char *opts = "rH";
5302 int types = ZFS_TYPE_SNAPSHOT;
5303 holds_cbdata_t cb = { 0 };
5310 while ((c = getopt(argc, argv, opts)) != -1) {
5319 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5326 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5327 flags |= ZFS_ITER_RECURSE;
5333 /* check number of arguments */
5337 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5340 for (i = 0; i < argc; ++i) {
5341 char *snapshot = argv[i];
5343 const char *snapname;
5345 delim = strchr(snapshot, '@');
5346 if (delim == NULL) {
5347 (void) fprintf(stderr,
5348 gettext("'%s' is not a snapshot\n"), snapshot);
5352 snapname = delim + 1;
5354 snapshot[delim - snapshot] = '\0';
5356 cb.cb_recursive = recursive;
5357 cb.cb_snapname = snapname;
5361 * 1. collect holds data, set format options
5363 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5364 holds_callback, &cb);
5370 * 2. print holds data
5372 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5374 if (nvlist_empty(nvl))
5375 (void) fprintf(stderr, gettext("no datasets available\n"));
5379 return (0 != errors);
5382 #define CHECK_SPINNER 30
5383 #define SPINNER_TIME 3 /* seconds */
5384 #define MOUNT_TIME 5 /* seconds */
5387 get_one_dataset(zfs_handle_t *zhp, void *data)
5389 static char *spin[] = { "-", "\\", "|", "/" };
5390 static int spinval = 0;
5391 static int spincheck = 0;
5392 static time_t last_spin_time = (time_t)0;
5393 get_all_cb_t *cbp = data;
5394 zfs_type_t type = zfs_get_type(zhp);
5396 if (cbp->cb_verbose) {
5397 if (--spincheck < 0) {
5398 time_t now = time(NULL);
5399 if (last_spin_time + SPINNER_TIME < now) {
5400 update_progress(spin[spinval++ % 4]);
5401 last_spin_time = now;
5403 spincheck = CHECK_SPINNER;
5408 * Interate over any nested datasets.
5410 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5416 * Skip any datasets whose type does not match.
5418 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5422 libzfs_add_handle(cbp, zhp);
5423 assert(cbp->cb_used <= cbp->cb_alloc);
5429 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5431 get_all_cb_t cb = { 0 };
5432 cb.cb_verbose = verbose;
5433 cb.cb_getone = get_one_dataset;
5436 set_progress_header(gettext("Reading ZFS config"));
5437 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5439 *dslist = cb.cb_handles;
5440 *count = cb.cb_used;
5443 finish_progress(gettext("done."));
5447 * Generic callback for sharing or mounting filesystems. Because the code is so
5448 * similar, we have a common function with an extra parameter to determine which
5449 * mode we are using.
5451 #define OP_SHARE 0x1
5452 #define OP_MOUNT 0x2
5455 * Share or mount a dataset.
5458 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5459 boolean_t explicit, const char *options)
5461 char mountpoint[ZFS_MAXPROPLEN];
5462 char shareopts[ZFS_MAXPROPLEN];
5463 char smbshareopts[ZFS_MAXPROPLEN];
5464 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5466 uint64_t zoned, canmount;
5467 boolean_t shared_nfs, shared_smb;
5469 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5472 * Check to make sure we can mount/share this dataset. If we
5473 * are in the global zone and the filesystem is exported to a
5474 * local zone, or if we are in a local zone and the
5475 * filesystem is not exported, then it is an error.
5477 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5479 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5483 (void) fprintf(stderr, gettext("cannot %s '%s': "
5484 "dataset is exported to a local zone\n"), cmdname,
5488 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5492 (void) fprintf(stderr, gettext("cannot %s '%s': "
5493 "permission denied\n"), cmdname,
5499 * Ignore any filesystems which don't apply to us. This
5500 * includes those with a legacy mountpoint, or those with
5501 * legacy share options.
5503 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5504 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5505 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5506 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5507 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5508 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5510 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5511 strcmp(smbshareopts, "off") == 0) {
5515 (void) fprintf(stderr, gettext("cannot share '%s': "
5516 "legacy share\n"), zfs_get_name(zhp));
5517 (void) fprintf(stderr, gettext("use share(1M) to "
5518 "share this filesystem, or set "
5519 "sharenfs property on\n"));
5524 * We cannot share or mount legacy filesystems. If the
5525 * shareopts is non-legacy but the mountpoint is legacy, we
5526 * treat it as a legacy share.
5528 if (strcmp(mountpoint, "legacy") == 0) {
5532 (void) fprintf(stderr, gettext("cannot %s '%s': "
5533 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5534 (void) fprintf(stderr, gettext("use %s(1M) to "
5535 "%s this filesystem\n"), cmdname, cmdname);
5539 if (strcmp(mountpoint, "none") == 0) {
5543 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5544 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5549 * canmount explicit outcome
5550 * on no pass through
5551 * on yes pass through
5553 * off yes display error, return 1
5554 * noauto no return 0
5555 * noauto yes pass through
5557 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5558 if (canmount == ZFS_CANMOUNT_OFF) {
5562 (void) fprintf(stderr, gettext("cannot %s '%s': "
5563 "'canmount' property is set to 'off'\n"), cmdname,
5566 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5571 * At this point, we have verified that the mountpoint and/or
5572 * shareopts are appropriate for auto management. If the
5573 * filesystem is already mounted or shared, return (failing
5574 * for explicit requests); otherwise mount or share the
5580 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5581 shared_smb = zfs_is_shared_smb(zhp, NULL);
5583 if ((shared_nfs && shared_smb) ||
5584 ((shared_nfs && strcmp(shareopts, "on") == 0) &&
5585 (strcmp(smbshareopts, "off") == 0)) ||
5586 ((shared_smb && strcmp(smbshareopts, "on") == 0) &&
5587 (strcmp(shareopts, "off") == 0))) {
5591 (void) fprintf(stderr, gettext("cannot share "
5592 "'%s': filesystem already shared\n"),
5597 if (!zfs_is_mounted(zhp, NULL) &&
5598 zfs_mount(zhp, NULL, 0) != 0)
5601 if (protocol == NULL) {
5602 if (zfs_shareall(zhp) != 0)
5604 } else if (strcmp(protocol, "nfs") == 0) {
5605 if (zfs_share_nfs(zhp))
5607 } else if (strcmp(protocol, "smb") == 0) {
5608 if (zfs_share_smb(zhp))
5611 (void) fprintf(stderr, gettext("cannot share "
5612 "'%s': invalid share type '%s' "
5614 zfs_get_name(zhp), protocol);
5621 if (options == NULL)
5622 mnt.mnt_mntopts = "";
5624 mnt.mnt_mntopts = (char *)options;
5626 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5627 zfs_is_mounted(zhp, NULL)) {
5631 (void) fprintf(stderr, gettext("cannot mount "
5632 "'%s': filesystem already mounted\n"),
5637 if (zfs_mount(zhp, options, flags) != 0)
5646 * Reports progress in the form "(current/total)". Not thread-safe.
5649 report_mount_progress(int current, int total)
5651 static time_t last_progress_time = 0;
5652 time_t now = time(NULL);
5655 /* report 1..n instead of 0..n-1 */
5658 /* display header if we're here for the first time */
5660 set_progress_header(gettext("Mounting ZFS filesystems"));
5661 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5662 /* too soon to report again */
5666 last_progress_time = now;
5668 (void) sprintf(info, "(%d/%d)", current, total);
5670 if (current == total)
5671 finish_progress(info);
5673 update_progress(info);
5677 append_options(char *mntopts, char *newopts)
5679 int len = strlen(mntopts);
5681 /* original length plus new string to append plus 1 for the comma */
5682 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5683 (void) fprintf(stderr, gettext("the opts argument for "
5684 "'%s' option is too long (more than %d chars)\n"),
5685 "-o", MNT_LINE_MAX);
5690 mntopts[len++] = ',';
5692 (void) strcpy(&mntopts[len], newopts);
5696 share_mount(int op, int argc, char **argv)
5699 boolean_t verbose = B_FALSE;
5701 char *options = NULL;
5705 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5715 if (*optarg == '\0') {
5716 (void) fprintf(stderr, gettext("empty mount "
5717 "options (-o) specified\n"));
5721 if (options == NULL)
5722 options = safe_malloc(MNT_LINE_MAX + 1);
5724 /* option validation is done later */
5725 append_options(options, optarg);
5728 flags |= MS_OVERLAY;
5731 (void) fprintf(stderr, gettext("missing argument for "
5732 "'%c' option\n"), optopt);
5736 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5745 /* check number of arguments */
5747 zfs_handle_t **dslist = NULL;
5748 size_t i, count = 0;
5749 char *protocol = NULL;
5751 if (op == OP_SHARE && argc > 0) {
5752 if (strcmp(argv[0], "nfs") != 0 &&
5753 strcmp(argv[0], "smb") != 0) {
5754 (void) fprintf(stderr, gettext("share type "
5755 "must be 'nfs' or 'smb'\n"));
5764 (void) fprintf(stderr, gettext("too many arguments\n"));
5768 start_progress_timer();
5769 get_all_datasets(&dslist, &count, verbose);
5774 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5776 for (i = 0; i < count; i++) {
5778 report_mount_progress(i, count);
5780 if (share_mount_one(dslist[i], op, flags, protocol,
5781 B_FALSE, options) != 0)
5783 zfs_close(dslist[i]);
5787 } else if (argc == 0) {
5788 struct mnttab entry;
5790 if ((op == OP_SHARE) || (options != NULL)) {
5791 (void) fprintf(stderr, gettext("missing filesystem "
5792 "argument (specify -a for all)\n"));
5797 * When mount is given no arguments, go through /etc/mtab and
5798 * display any active ZFS mounts. We hide any snapshots, since
5799 * they are controlled automatically.
5801 rewind(mnttab_file);
5802 while (getmntent(mnttab_file, &entry) == 0) {
5803 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5804 strchr(entry.mnt_special, '@') != NULL)
5807 (void) printf("%-30s %s\n", entry.mnt_special,
5815 (void) fprintf(stderr,
5816 gettext("too many arguments\n"));
5820 if ((zhp = zfs_open(g_zfs, argv[0],
5821 ZFS_TYPE_FILESYSTEM)) == NULL) {
5824 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5834 * zfs mount -a [nfs]
5835 * zfs mount filesystem
5837 * Mount all filesystems, or mount the given filesystem.
5840 zfs_do_mount(int argc, char **argv)
5842 return (share_mount(OP_MOUNT, argc, argv));
5846 * zfs share -a [nfs | smb]
5847 * zfs share filesystem
5849 * Share all filesystems, or share the given filesystem.
5852 zfs_do_share(int argc, char **argv)
5854 return (share_mount(OP_SHARE, argc, argv));
5857 typedef struct unshare_unmount_node {
5858 zfs_handle_t *un_zhp;
5860 uu_avl_node_t un_avlnode;
5861 } unshare_unmount_node_t;
5865 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
5867 const unshare_unmount_node_t *l = larg;
5868 const unshare_unmount_node_t *r = rarg;
5870 return (strcmp(l->un_mountp, r->un_mountp));
5874 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5875 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
5876 * and unmount it appropriately.
5879 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
5883 struct stat64 statbuf;
5884 struct extmnttab entry;
5885 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5889 * Search for the path in /etc/mtab. Rather than looking for the
5890 * specific path, which can be fooled by non-standard paths (i.e. ".."
5891 * or "//"), we stat() the path and search for the corresponding
5892 * (major,minor) device pair.
5894 if (stat64(path, &statbuf) != 0) {
5895 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5896 cmdname, path, strerror(errno));
5899 path_inode = statbuf.st_ino;
5902 * Search for the given (major,minor) pair in the mount table.
5904 rewind(mnttab_file);
5905 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
5906 if (entry.mnt_major == major(statbuf.st_dev) &&
5907 entry.mnt_minor == minor(statbuf.st_dev))
5911 if (op == OP_SHARE) {
5912 (void) fprintf(stderr, gettext("cannot %s '%s': not "
5913 "currently mounted\n"), cmdname, path);
5916 (void) fprintf(stderr, gettext("warning: %s not in mtab\n"),
5918 if ((ret = umount2(path, flags)) != 0)
5919 (void) fprintf(stderr, gettext("%s: %s\n"), path,
5924 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
5925 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
5926 "filesystem\n"), cmdname, path);
5930 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
5931 ZFS_TYPE_FILESYSTEM)) == NULL)
5935 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
5936 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5937 cmdname, path, strerror(errno));
5939 } else if (statbuf.st_ino != path_inode) {
5940 (void) fprintf(stderr, gettext("cannot "
5941 "%s '%s': not a mountpoint\n"), cmdname, path);
5945 if (op == OP_SHARE) {
5946 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5947 char smbshare_prop[ZFS_MAXPROPLEN];
5949 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
5950 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
5951 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
5952 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
5954 if (strcmp(nfs_mnt_prop, "off") == 0 &&
5955 strcmp(smbshare_prop, "off") == 0) {
5956 (void) fprintf(stderr, gettext("cannot unshare "
5957 "'%s': legacy share\n"), path);
5958 (void) fprintf(stderr, gettext("use exportfs(8) "
5959 "or smbcontrol(1) to unshare this filesystem\n"));
5960 } else if (!zfs_is_shared(zhp)) {
5961 (void) fprintf(stderr, gettext("cannot unshare '%s': "
5962 "not currently shared\n"), path);
5964 ret = zfs_unshareall_bypath(zhp, path);
5967 char mtpt_prop[ZFS_MAXPROPLEN];
5969 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
5970 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
5973 ret = zfs_unmount(zhp, NULL, flags);
5974 } else if (strcmp(mtpt_prop, "legacy") == 0) {
5975 (void) fprintf(stderr, gettext("cannot unmount "
5976 "'%s': legacy mountpoint\n"),
5978 (void) fprintf(stderr, gettext("use umount(8) "
5979 "to unmount this filesystem\n"));
5981 ret = zfs_unmountall(zhp, flags);
5992 * Generic callback for unsharing or unmounting a filesystem.
5995 unshare_unmount(int op, int argc, char **argv)
6002 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6003 char sharesmb[ZFS_MAXPROPLEN];
6006 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6015 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6026 * We could make use of zfs_for_each() to walk all datasets in
6027 * the system, but this would be very inefficient, especially
6028 * since we would have to linearly search /etc/mtab for each
6029 * one. Instead, do one pass through /etc/mtab looking for
6030 * zfs entries and call zfs_unmount() for each one.
6032 * Things get a little tricky if the administrator has created
6033 * mountpoints beneath other ZFS filesystems. In this case, we
6034 * have to unmount the deepest filesystems first. To accomplish
6035 * this, we place all the mountpoints in an AVL tree sorted by
6036 * the special type (dataset name), and walk the result in
6037 * reverse to make sure to get any snapshots first.
6039 struct mnttab entry;
6040 uu_avl_pool_t *pool;
6041 uu_avl_t *tree = NULL;
6042 unshare_unmount_node_t *node;
6044 uu_avl_walk_t *walk;
6047 (void) fprintf(stderr, gettext("too many arguments\n"));
6051 if (((pool = uu_avl_pool_create("unmount_pool",
6052 sizeof (unshare_unmount_node_t),
6053 offsetof(unshare_unmount_node_t, un_avlnode),
6054 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6055 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6058 rewind(mnttab_file);
6059 while (getmntent(mnttab_file, &entry) == 0) {
6061 /* ignore non-ZFS entries */
6062 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6065 /* ignore snapshots */
6066 if (strchr(entry.mnt_special, '@') != NULL)
6069 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6070 ZFS_TYPE_FILESYSTEM)) == NULL) {
6077 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6079 sizeof (nfs_mnt_prop),
6080 NULL, NULL, 0, B_FALSE) == 0);
6081 if (strcmp(nfs_mnt_prop, "off") != 0)
6083 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6085 sizeof (nfs_mnt_prop),
6086 NULL, NULL, 0, B_FALSE) == 0);
6087 if (strcmp(nfs_mnt_prop, "off") == 0)
6091 /* Ignore legacy mounts */
6092 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6094 sizeof (nfs_mnt_prop),
6095 NULL, NULL, 0, B_FALSE) == 0);
6096 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6098 /* Ignore canmount=noauto mounts */
6099 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6100 ZFS_CANMOUNT_NOAUTO)
6106 node = safe_malloc(sizeof (unshare_unmount_node_t));
6108 node->un_mountp = safe_strdup(entry.mnt_mountp);
6110 uu_avl_node_init(node, &node->un_avlnode, pool);
6112 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6113 uu_avl_insert(tree, node, idx);
6115 zfs_close(node->un_zhp);
6116 free(node->un_mountp);
6122 * Walk the AVL tree in reverse, unmounting each filesystem and
6123 * removing it from the AVL tree in the process.
6125 if ((walk = uu_avl_walk_start(tree,
6126 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6129 while ((node = uu_avl_walk_next(walk)) != NULL) {
6130 uu_avl_remove(tree, node);
6134 if (zfs_unshareall_bypath(node->un_zhp,
6135 node->un_mountp) != 0)
6140 if (zfs_unmount(node->un_zhp,
6141 node->un_mountp, flags) != 0)
6146 zfs_close(node->un_zhp);
6147 free(node->un_mountp);
6151 uu_avl_walk_end(walk);
6152 uu_avl_destroy(tree);
6153 uu_avl_pool_destroy(pool);
6158 (void) fprintf(stderr,
6159 gettext("missing filesystem argument\n"));
6161 (void) fprintf(stderr,
6162 gettext("too many arguments\n"));
6167 * We have an argument, but it may be a full path or a ZFS
6168 * filesystem. Pass full paths off to unmount_path() (shared by
6169 * manual_unmount), otherwise open the filesystem and pass to
6172 if (argv[0][0] == '/')
6173 return (unshare_unmount_path(op, argv[0],
6176 if ((zhp = zfs_open(g_zfs, argv[0],
6177 ZFS_TYPE_FILESYSTEM)) == NULL)
6180 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6181 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6182 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6183 NULL, 0, B_FALSE) == 0);
6187 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6189 sizeof (nfs_mnt_prop),
6190 NULL, NULL, 0, B_FALSE) == 0);
6191 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6192 sharesmb, sizeof (sharesmb), NULL, NULL,
6195 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6196 strcmp(sharesmb, "off") == 0) {
6197 (void) fprintf(stderr, gettext("cannot "
6198 "unshare '%s': legacy share\n"),
6200 (void) fprintf(stderr, gettext("use "
6201 "unshare(1M) to unshare this "
6204 } else if (!zfs_is_shared(zhp)) {
6205 (void) fprintf(stderr, gettext("cannot "
6206 "unshare '%s': not currently "
6207 "shared\n"), zfs_get_name(zhp));
6209 } else if (zfs_unshareall(zhp) != 0) {
6215 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6216 (void) fprintf(stderr, gettext("cannot "
6217 "unmount '%s': legacy "
6218 "mountpoint\n"), zfs_get_name(zhp));
6219 (void) fprintf(stderr, gettext("use "
6220 "umount(1M) to unmount this "
6223 } else if (!zfs_is_mounted(zhp, NULL)) {
6224 (void) fprintf(stderr, gettext("cannot "
6225 "unmount '%s': not currently "
6229 } else if (zfs_unmountall(zhp, flags) != 0) {
6243 * zfs unmount filesystem
6245 * Unmount all filesystems, or a specific ZFS filesystem.
6248 zfs_do_unmount(int argc, char **argv)
6250 return (unshare_unmount(OP_MOUNT, argc, argv));
6255 * zfs unshare filesystem
6257 * Unshare all filesystems, or a specific ZFS filesystem.
6260 zfs_do_unshare(int argc, char **argv)
6262 return (unshare_unmount(OP_SHARE, argc, argv));
6266 find_command_idx(char *command, int *idx)
6270 for (i = 0; i < NCOMMAND; i++) {
6271 if (command_table[i].name == NULL)
6274 if (strcmp(command, command_table[i].name) == 0) {
6283 zfs_do_diff(int argc, char **argv)
6287 char *tosnap = NULL;
6288 char *fromsnap = NULL;
6293 while ((c = getopt(argc, argv, "FHt")) != -1) {
6296 flags |= ZFS_DIFF_CLASSIFY;
6299 flags |= ZFS_DIFF_PARSEABLE;
6302 flags |= ZFS_DIFF_TIMESTAMP;
6305 (void) fprintf(stderr,
6306 gettext("invalid option '%c'\n"), optopt);
6315 (void) fprintf(stderr,
6316 gettext("must provide at least one snapshot name\n"));
6321 (void) fprintf(stderr, gettext("too many arguments\n"));
6326 tosnap = (argc == 2) ? argv[1] : NULL;
6329 if (*fromsnap != '@')
6330 copy = strdup(fromsnap);
6332 copy = strdup(tosnap);
6336 if ((atp = strchr(copy, '@')))
6339 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6345 * Ignore SIGPIPE so that the library can give us
6346 * information on any failure
6348 (void) sigignore(SIGPIPE);
6350 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6358 main(int argc, char **argv)
6364 (void) setlocale(LC_ALL, "");
6365 (void) textdomain(TEXT_DOMAIN);
6369 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6370 (void) fprintf(stderr, gettext("internal error: unable to "
6371 "open %s\n"), MNTTAB);
6376 * Make sure the user has specified some command.
6379 (void) fprintf(stderr, gettext("missing command\n"));
6386 * The 'umount' command is an alias for 'unmount'
6388 if (strcmp(cmdname, "umount") == 0)
6389 cmdname = "unmount";
6392 * The 'recv' command is an alias for 'receive'
6394 if (strcmp(cmdname, "recv") == 0)
6395 cmdname = "receive";
6398 * The 'snap' command is an alias for 'snapshot'
6400 if (strcmp(cmdname, "snap") == 0)
6401 cmdname = "snapshot";
6406 if ((strcmp(cmdname, "-?") == 0) ||
6407 (strcmp(cmdname, "--help") == 0))
6410 if ((g_zfs = libzfs_init()) == NULL)
6413 zpool_set_history_str("zfs", argc, argv, history_str);
6414 verify(zpool_stage_history(g_zfs, history_str) == 0);
6416 libzfs_print_on_error(g_zfs, B_TRUE);
6419 * Run the appropriate command.
6421 libzfs_mnttab_cache(g_zfs, B_FALSE);
6422 if (find_command_idx(cmdname, &i) == 0) {
6423 current_command = &command_table[i];
6424 ret = command_table[i].func(argc - 1, argv + 1);
6425 } else if (strchr(cmdname, '=') != NULL) {
6426 verify(find_command_idx("set", &i) == 0);
6427 current_command = &command_table[i];
6428 ret = command_table[i].func(argc, argv);
6430 (void) fprintf(stderr, gettext("unrecognized "
6431 "command '%s'\n"), cmdname);
6437 (void) fclose(mnttab_file);
6440 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6441 * for the purposes of running ::findleaks.
6443 if (getenv("ZFS_ABORT") != NULL) {
6444 (void) printf("dumping core by request\n");