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 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>.
26 * All rights reserved.
35 #include <libnvpair.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>
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);
99 static int zfs_do_jail(int argc, char **argv);
100 static int zfs_do_unjail(int argc, char **argv);
103 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
108 _umem_debug_init(void)
110 return ("default,verbose"); /* $UMEM_DEBUG setting */
114 _umem_logging_init(void)
116 return ("fail,contents"); /* $UMEM_LOGGING setting */
151 typedef struct zfs_command {
153 int (*func)(int argc, char **argv);
158 * Master command table. Each ZFS command has a name, associated function, and
159 * usage message. The usage messages need to be internationalized, so we have
160 * to have a function to return the usage message based on a command index.
162 * These commands are organized according to how they are displayed in the usage
163 * message. An empty command (one with a NULL name) indicates an empty line in
164 * the generic usage message.
166 static zfs_command_t command_table[] = {
167 { "create", zfs_do_create, HELP_CREATE },
168 { "destroy", zfs_do_destroy, HELP_DESTROY },
170 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
171 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
172 { "clone", zfs_do_clone, HELP_CLONE },
173 { "promote", zfs_do_promote, HELP_PROMOTE },
174 { "rename", zfs_do_rename, HELP_RENAME },
176 { "list", zfs_do_list, HELP_LIST },
178 { "set", zfs_do_set, HELP_SET },
179 { "get", zfs_do_get, HELP_GET },
180 { "inherit", zfs_do_inherit, HELP_INHERIT },
181 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
182 { "userspace", zfs_do_userspace, HELP_USERSPACE },
183 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
185 { "mount", zfs_do_mount, HELP_MOUNT },
186 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
187 { "share", zfs_do_share, HELP_SHARE },
188 { "unshare", zfs_do_unshare, HELP_UNSHARE },
190 { "send", zfs_do_send, HELP_SEND },
191 { "receive", zfs_do_receive, HELP_RECEIVE },
193 { "allow", zfs_do_allow, HELP_ALLOW },
195 { "unallow", zfs_do_unallow, HELP_UNALLOW },
197 { "hold", zfs_do_hold, HELP_HOLD },
198 { "holds", zfs_do_holds, HELP_HOLDS },
199 { "release", zfs_do_release, HELP_RELEASE },
200 { "diff", zfs_do_diff, HELP_DIFF },
202 { "jail", zfs_do_jail, HELP_JAIL },
203 { "unjail", zfs_do_unjail, HELP_UNJAIL },
206 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
208 zfs_command_t *current_command;
211 get_usage(zfs_help_t idx)
215 return (gettext("\tclone [-p] [-o property=value] ... "
216 "<snapshot> <filesystem|volume>\n"));
218 return (gettext("\tcreate [-p] [-o property=value] ... "
220 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
221 "-V <size> <volume>\n"));
223 return (gettext("\tdestroy [-rRf] <filesystem|volume>\n"
224 "\tdestroy [-rRd] <snapshot>\n"));
226 return (gettext("\tget [-rHp] [-d max] "
227 "[-o \"all\" | field[,...]] [-s source[,...]]\n"
228 "\t <\"all\" | property[,...]> "
229 "[filesystem|volume|snapshot] ...\n"));
231 return (gettext("\tinherit [-rS] <property> "
232 "<filesystem|volume|snapshot> ...\n"));
234 return (gettext("\tupgrade [-v]\n"
235 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
237 return (gettext("\tjail <jailid> <filesystem>\n"));
239 return (gettext("\tunjail <jailid> <filesystem>\n"));
241 return (gettext("\tlist [-rH][-d max] "
242 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
243 "\t [-S property] ... "
244 "[filesystem|volume|snapshot] ...\n"));
246 return (gettext("\tmount\n"
247 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
249 return (gettext("\tpromote <clone-filesystem>\n"));
251 return (gettext("\treceive [-vnFu] <filesystem|volume|"
253 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
255 return (gettext("\trename <filesystem|volume|snapshot> "
256 "<filesystem|volume|snapshot>\n"
257 "\trename -p <filesystem|volume> <filesystem|volume>\n"
258 "\trename -r <snapshot> <snapshot>\n"
259 "\trename -u [-p] <filesystem> <filesystem>"));
261 return (gettext("\trollback [-rRf] <snapshot>\n"));
263 return (gettext("\tsend [-DvRp] "
264 "[-i snapshot | -I snapshot] <snapshot>\n"));
266 return (gettext("\tset <property=value> "
267 "<filesystem|volume|snapshot> ...\n"));
269 return (gettext("\tshare <-a | filesystem>\n"));
271 return (gettext("\tsnapshot [-r] [-o property=value] ... "
272 "<filesystem@snapname|volume@snapname>\n"));
274 return (gettext("\tunmount [-f] "
275 "<-a | filesystem|mountpoint>\n"));
277 return (gettext("\tunshare "
278 "<-a | filesystem|mountpoint>\n"));
280 return (gettext("\tallow <filesystem|volume>\n"
282 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
283 "\t <filesystem|volume>\n"
284 "\tallow [-ld] -e <perm|@setname>[,...] "
285 "<filesystem|volume>\n"
286 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
287 "\tallow -s @setname <perm|@setname>[,...] "
288 "<filesystem|volume>\n"));
290 return (gettext("\tunallow [-rldug] "
291 "<\"everyone\"|user|group>[,...]\n"
292 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
293 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
294 "<filesystem|volume>\n"
295 "\tunallow [-r] -c [<perm|@setname>[,...]] "
296 "<filesystem|volume>\n"
297 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
298 "<filesystem|volume>\n"));
300 return (gettext("\tuserspace [-niHp] [-o field[,...]] "
301 "[-sS field] ... [-t type[,...]]\n"
302 "\t <filesystem|snapshot>\n"));
303 case HELP_GROUPSPACE:
304 return (gettext("\tgroupspace [-niHp] [-o field[,...]] "
305 "[-sS field] ... [-t type[,...]]\n"
306 "\t <filesystem|snapshot>\n"));
308 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
310 return (gettext("\tholds [-r] <snapshot> ...\n"));
312 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
314 return (gettext("\tdiff [-FHt] <snapshot> "
315 "[snapshot|filesystem]\n"));
325 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
330 * Utility function to guarantee malloc() success.
334 safe_malloc(size_t size)
338 if ((data = calloc(1, size)) == NULL)
345 safe_strdup(char *str)
347 char *dupstr = strdup(str);
356 * Callback routine that will print out information for each of
360 usage_prop_cb(int prop, void *cb)
364 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
366 if (zfs_prop_readonly(prop))
367 (void) fprintf(fp, " NO ");
369 (void) fprintf(fp, "YES ");
371 if (zfs_prop_inheritable(prop))
372 (void) fprintf(fp, " YES ");
374 (void) fprintf(fp, " NO ");
376 if (zfs_prop_values(prop) == NULL)
377 (void) fprintf(fp, "-\n");
379 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
385 * Display usage message. If we're inside a command, display only the usage for
386 * that command. Otherwise, iterate over the entire command table and display
387 * a complete usage message.
390 usage(boolean_t requested)
393 boolean_t show_properties = B_FALSE;
394 FILE *fp = requested ? stdout : stderr;
396 if (current_command == NULL) {
398 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
400 gettext("where 'command' is one of the following:\n\n"));
402 for (i = 0; i < NCOMMAND; i++) {
403 if (command_table[i].name == NULL)
404 (void) fprintf(fp, "\n");
406 (void) fprintf(fp, "%s",
407 get_usage(command_table[i].usage));
410 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
411 "pool/[dataset/]*dataset[@name]\n"));
413 (void) fprintf(fp, gettext("usage:\n"));
414 (void) fprintf(fp, "%s", get_usage(current_command->usage));
417 if (current_command != NULL &&
418 (strcmp(current_command->name, "set") == 0 ||
419 strcmp(current_command->name, "get") == 0 ||
420 strcmp(current_command->name, "inherit") == 0 ||
421 strcmp(current_command->name, "list") == 0))
422 show_properties = B_TRUE;
424 if (show_properties) {
426 gettext("\nThe following properties are supported:\n"));
428 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
429 "PROPERTY", "EDIT", "INHERIT", "VALUES");
431 /* Iterate over all properties */
432 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
435 (void) fprintf(fp, "\t%-15s ", "userused@...");
436 (void) fprintf(fp, " NO NO <size>\n");
437 (void) fprintf(fp, "\t%-15s ", "groupused@...");
438 (void) fprintf(fp, " NO NO <size>\n");
439 (void) fprintf(fp, "\t%-15s ", "userquota@...");
440 (void) fprintf(fp, "YES NO <size> | none\n");
441 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
442 (void) fprintf(fp, "YES NO <size> | none\n");
444 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
445 "with standard units such as K, M, G, etc.\n"));
446 (void) fprintf(fp, gettext("\nUser-defined properties can "
447 "be specified by using a name containing a colon (:).\n"));
448 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
449 "properties must be appended with\n"
450 "a user or group specifier of one of these forms:\n"
451 " POSIX name (eg: \"matt\")\n"
452 " POSIX id (eg: \"126829\")\n"
453 " SMB name@domain (eg: \"matt@sun\")\n"
454 " SMB SID (eg: \"S-1-234-567-89\")\n"));
457 gettext("\nFor the property list, run: %s\n"),
460 gettext("\nFor the delegated permission list, run: %s\n"),
461 "zfs allow|unallow");
465 * See comments at end of main().
467 if (getenv("ZFS_ABORT") != NULL) {
468 (void) printf("dumping core by request\n");
472 exit(requested ? 0 : 2);
476 parseprop(nvlist_t *props)
478 char *propname = optarg;
479 char *propval, *strval;
481 if ((propval = strchr(propname, '=')) == NULL) {
482 (void) fprintf(stderr, gettext("missing "
483 "'=' for -o option\n"));
488 if (nvlist_lookup_string(props, propname, &strval) == 0) {
489 (void) fprintf(stderr, gettext("property '%s' "
490 "specified multiple times\n"), propname);
493 if (nvlist_add_string(props, propname, propval) != 0)
499 parse_depth(char *opt, int *flags)
504 depth = (int)strtol(opt, &tmp, 0);
506 (void) fprintf(stderr,
507 gettext("%s is not an integer\n"), optarg);
511 (void) fprintf(stderr,
512 gettext("Depth can not be negative.\n"));
515 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
519 #define PROGRESS_DELAY 2 /* seconds */
521 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";
522 static time_t pt_begin;
523 static char *pt_header = NULL;
524 static boolean_t pt_shown;
527 start_progress_timer(void)
529 pt_begin = time(NULL) + PROGRESS_DELAY;
534 set_progress_header(char *header)
536 assert(pt_header == NULL);
537 pt_header = safe_strdup(header);
539 (void) printf("%s: ", header);
540 (void) fflush(stdout);
545 update_progress(char *update)
547 if (!pt_shown && time(NULL) > pt_begin) {
548 int len = strlen(update);
550 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
552 (void) fflush(stdout);
554 } else if (pt_shown) {
555 int len = strlen(update);
557 (void) printf("%s%*.*s", update, len, len, pt_reverse);
558 (void) fflush(stdout);
563 finish_progress(char *done)
566 (void) printf("%s\n", done);
567 (void) fflush(stdout);
573 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
575 * Given an existing dataset, create a writable copy whose initial contents
576 * are the same as the source. The newly created dataset maintains a
577 * dependency on the original; the original cannot be destroyed so long as
580 * The '-p' flag creates all the non-existing ancestors of the target first.
583 zfs_do_clone(int argc, char **argv)
585 zfs_handle_t *zhp = NULL;
586 boolean_t parents = B_FALSE;
591 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
595 while ((c = getopt(argc, argv, "o:p")) != -1) {
598 if (parseprop(props))
605 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
614 /* check number of arguments */
616 (void) fprintf(stderr, gettext("missing source dataset "
621 (void) fprintf(stderr, gettext("missing target dataset "
626 (void) fprintf(stderr, gettext("too many arguments\n"));
630 /* open the source dataset */
631 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
634 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
637 * Now create the ancestors of the target dataset. If the
638 * target already exists and '-p' option was used we should not
641 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
644 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
649 ret = zfs_clone(zhp, argv[1], props);
651 /* create the mountpoint if necessary */
655 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
657 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
658 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
659 ret = zfs_share(clone);
678 * zfs create [-p] [-o prop=value] ... fs
679 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
681 * Create a new dataset. This command can be used to create filesystems
682 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
683 * For volumes, the user must specify a size to be used.
685 * The '-s' flag applies only to volumes, and indicates that we should not try
686 * to set the reservation for this volume. By default we set a reservation
687 * equal to the size for any volume. For pools with SPA_VERSION >=
688 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
690 * The '-p' flag creates all the non-existing ancestors of the target first.
693 zfs_do_create(int argc, char **argv)
695 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
696 zfs_handle_t *zhp = NULL;
699 boolean_t noreserve = B_FALSE;
700 boolean_t bflag = B_FALSE;
701 boolean_t parents = B_FALSE;
705 int canmount = ZFS_CANMOUNT_OFF;
707 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
711 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
714 type = ZFS_TYPE_VOLUME;
715 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
716 (void) fprintf(stderr, gettext("bad volume "
717 "size '%s': %s\n"), optarg,
718 libzfs_error_description(g_zfs));
722 if (nvlist_add_uint64(props,
723 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
732 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
733 (void) fprintf(stderr, gettext("bad volume "
734 "block size '%s': %s\n"), optarg,
735 libzfs_error_description(g_zfs));
739 if (nvlist_add_uint64(props,
740 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
745 if (parseprop(props))
752 (void) fprintf(stderr, gettext("missing size "
757 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
763 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
764 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
765 "used when creating a volume\n"));
772 /* check number of arguments */
774 (void) fprintf(stderr, gettext("missing %s argument\n"),
775 zfs_type_to_name(type));
779 (void) fprintf(stderr, gettext("too many arguments\n"));
783 if (type == ZFS_TYPE_VOLUME && !noreserve) {
784 zpool_handle_t *zpool_handle;
785 uint64_t spa_version;
787 zfs_prop_t resv_prop;
790 if (p = strchr(argv[0], '/'))
792 zpool_handle = zpool_open(g_zfs, argv[0]);
795 if (zpool_handle == NULL)
797 spa_version = zpool_get_prop_int(zpool_handle,
798 ZPOOL_PROP_VERSION, NULL);
799 zpool_close(zpool_handle);
800 if (spa_version >= SPA_VERSION_REFRESERVATION)
801 resv_prop = ZFS_PROP_REFRESERVATION;
803 resv_prop = ZFS_PROP_RESERVATION;
804 volsize = zvol_volsize_to_reservation(volsize, props);
806 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
808 if (nvlist_add_uint64(props,
809 zfs_prop_to_name(resv_prop), volsize) != 0) {
816 if (parents && zfs_name_valid(argv[0], type)) {
818 * Now create the ancestors of target dataset. If the target
819 * already exists and '-p' option was used we should not
822 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
826 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
831 if (zfs_create(g_zfs, argv[0], type, props) != 0)
834 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
839 * if the user doesn't want the dataset automatically mounted,
840 * then skip the mount/share step
842 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
843 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
846 * Mount and/or share the new filesystem as appropriate. We provide a
847 * verbose error message to let the user know that their filesystem was
848 * in fact created, even if we failed to mount or share it.
850 if (canmount == ZFS_CANMOUNT_ON) {
851 if (zfs_mount(zhp, NULL, 0) != 0) {
852 (void) fprintf(stderr, gettext("filesystem "
853 "successfully created, but not mounted\n"));
855 } else if (zfs_share(zhp) != 0) {
856 (void) fprintf(stderr, gettext("filesystem "
857 "successfully created, but not shared\n"));
874 * zfs destroy [-rRf] <fs, vol>
875 * zfs destroy [-rRd] <snap>
877 * -r Recursively destroy all children
878 * -R Recursively destroy all dependents, including clones
879 * -f Force unmounting of any dependents
880 * -d If we can't destroy now, mark for deferred destruction
882 * Destroys the given dataset. By default, it will unmount any filesystems,
883 * and refuse to destroy a dataset that has any dependents. A dependent can
884 * either be a child, or a clone of a child.
886 typedef struct destroy_cbdata {
893 boolean_t cb_closezhp;
894 zfs_handle_t *cb_target;
896 boolean_t cb_defer_destroy;
900 * Check for any dependents based on the '-r' or '-R' flags.
903 destroy_check_dependent(zfs_handle_t *zhp, void *data)
905 destroy_cbdata_t *cbp = data;
906 const char *tname = zfs_get_name(cbp->cb_target);
907 const char *name = zfs_get_name(zhp);
909 if (strncmp(tname, name, strlen(tname)) == 0 &&
910 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
912 * This is a direct descendant, not a clone somewhere else in
919 (void) fprintf(stderr, gettext("cannot destroy '%s': "
920 "%s has children\n"),
921 zfs_get_name(cbp->cb_target),
922 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
923 (void) fprintf(stderr, gettext("use '-r' to destroy "
924 "the following datasets:\n"));
925 cbp->cb_first = B_FALSE;
929 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
932 * This is a clone. We only want to report this if the '-r'
933 * wasn't specified, or the target is a snapshot.
935 if (!cbp->cb_recurse &&
936 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
940 (void) fprintf(stderr, gettext("cannot destroy '%s': "
941 "%s has dependent clones\n"),
942 zfs_get_name(cbp->cb_target),
943 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
944 (void) fprintf(stderr, gettext("use '-R' to destroy "
945 "the following datasets:\n"));
946 cbp->cb_first = B_FALSE;
950 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
959 destroy_callback(zfs_handle_t *zhp, void *data)
961 destroy_cbdata_t *cbp = data;
964 * Ignore pools (which we've already flagged as an error before getting
967 if (strchr(zfs_get_name(zhp), '/') == NULL &&
968 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
974 * Bail out on the first error.
976 if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
977 zfs_destroy(zhp, cbp->cb_defer_destroy) != 0) {
987 destroy_snap_clones(zfs_handle_t *zhp, void *arg)
989 destroy_cbdata_t *cbp = arg;
990 char thissnap[MAXPATHLEN];
992 boolean_t closezhp = cbp->cb_closezhp;
995 (void) snprintf(thissnap, sizeof (thissnap),
996 "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
998 libzfs_print_on_error(g_zfs, B_FALSE);
999 szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
1000 libzfs_print_on_error(g_zfs, B_TRUE);
1003 * Destroy any clones of this snapshot
1005 if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
1015 cbp->cb_closezhp = B_TRUE;
1016 rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
1023 zfs_do_destroy(int argc, char **argv)
1025 destroy_cbdata_t cb = { 0 };
1029 zfs_type_t type = ZFS_TYPE_DATASET;
1032 while ((c = getopt(argc, argv, "dfrR")) != -1) {
1035 cb.cb_defer_destroy = B_TRUE;
1036 type = ZFS_TYPE_SNAPSHOT;
1050 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1059 /* check number of arguments */
1061 (void) fprintf(stderr, gettext("missing path argument\n"));
1065 (void) fprintf(stderr, gettext("too many arguments\n"));
1070 * If we are doing recursive destroy of a snapshot, then the
1071 * named snapshot may not exist. Go straight to libzfs.
1073 if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
1077 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
1082 if (cb.cb_doclones) {
1083 boolean_t defer = cb.cb_defer_destroy;
1086 * Temporarily ignore the defer_destroy setting since
1087 * it's not supported for clones.
1089 cb.cb_defer_destroy = B_FALSE;
1090 cb.cb_snapname = cp;
1091 if (destroy_snap_clones(zhp, &cb) != 0) {
1095 cb.cb_defer_destroy = defer;
1098 ret = zfs_destroy_snaps(zhp, cp, cb.cb_defer_destroy);
1101 (void) fprintf(stderr,
1102 gettext("no snapshots destroyed\n"));
1107 /* Open the given dataset */
1108 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1114 * Perform an explicit check for pools before going any further.
1116 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1117 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1118 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1119 "operation does not apply to pools\n"),
1121 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1122 "%s' to destroy all datasets in the pool\n"),
1124 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1125 "to destroy the pool itself\n"), zfs_get_name(zhp));
1131 * Check for any dependents and/or clones.
1133 cb.cb_first = B_TRUE;
1134 if (!cb.cb_doclones && !cb.cb_defer_destroy &&
1135 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1141 if (cb.cb_error || (!cb.cb_defer_destroy &&
1142 (zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0))) {
1148 * Do the real thing. The callback will close the handle regardless of
1149 * whether it succeeds or not.
1152 if (destroy_callback(zhp, &cb) != 0)
1159 is_recvd_column(zprop_get_cbdata_t *cbp)
1162 zfs_get_column_t col;
1164 for (i = 0; i < ZFS_GET_NCOLS &&
1165 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1166 if (col == GET_COL_RECVD)
1172 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1173 * < all | property[,property]... > < fs | snap | vol > ...
1175 * -r recurse over any child datasets
1176 * -H scripted mode. Headers are stripped, and fields are separated
1177 * by tabs instead of spaces.
1178 * -o Set of fields to display. One of "name,property,value,
1179 * received,source". Default is "name,property,value,source".
1180 * "all" is an alias for all five.
1181 * -s Set of sources to allow. One of
1182 * "local,default,inherited,received,temporary,none". Default is
1184 * -p Display values in parsable (literal) format.
1186 * Prints properties for the given datasets. The user can control which
1187 * columns to display as well as which property types to allow.
1191 * Invoked to display the properties for a single dataset.
1194 get_callback(zfs_handle_t *zhp, void *data)
1196 char buf[ZFS_MAXPROPLEN];
1197 char rbuf[ZFS_MAXPROPLEN];
1198 zprop_source_t sourcetype;
1199 char source[ZFS_MAXNAMELEN];
1200 zprop_get_cbdata_t *cbp = data;
1201 nvlist_t *user_props = zfs_get_user_props(zhp);
1202 zprop_list_t *pl = cbp->cb_proplist;
1206 boolean_t received = is_recvd_column(cbp);
1208 for (; pl != NULL; pl = pl->pl_next) {
1209 char *recvdval = NULL;
1211 * Skip the special fake placeholder. This will also skip over
1212 * the name property when 'all' is specified.
1214 if (pl->pl_prop == ZFS_PROP_NAME &&
1215 pl == cbp->cb_proplist)
1218 if (pl->pl_prop != ZPROP_INVAL) {
1219 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1220 sizeof (buf), &sourcetype, source,
1222 cbp->cb_literal) != 0) {
1225 if (!zfs_prop_valid_for_type(pl->pl_prop,
1226 ZFS_TYPE_DATASET)) {
1227 (void) fprintf(stderr,
1228 gettext("No such property '%s'\n"),
1229 zfs_prop_to_name(pl->pl_prop));
1232 sourcetype = ZPROP_SRC_NONE;
1233 (void) strlcpy(buf, "-", sizeof (buf));
1236 if (received && (zfs_prop_get_recvd(zhp,
1237 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1238 cbp->cb_literal) == 0))
1241 zprop_print_one_property(zfs_get_name(zhp), cbp,
1242 zfs_prop_to_name(pl->pl_prop),
1243 buf, sourcetype, source, recvdval);
1244 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1245 sourcetype = ZPROP_SRC_LOCAL;
1247 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1248 buf, sizeof (buf), cbp->cb_literal) != 0) {
1249 sourcetype = ZPROP_SRC_NONE;
1250 (void) strlcpy(buf, "-", sizeof (buf));
1253 zprop_print_one_property(zfs_get_name(zhp), cbp,
1254 pl->pl_user_prop, buf, sourcetype, source, NULL);
1256 if (nvlist_lookup_nvlist(user_props,
1257 pl->pl_user_prop, &propval) != 0) {
1260 sourcetype = ZPROP_SRC_NONE;
1263 verify(nvlist_lookup_string(propval,
1264 ZPROP_VALUE, &strval) == 0);
1265 verify(nvlist_lookup_string(propval,
1266 ZPROP_SOURCE, &sourceval) == 0);
1268 if (strcmp(sourceval,
1269 zfs_get_name(zhp)) == 0) {
1270 sourcetype = ZPROP_SRC_LOCAL;
1271 } else if (strcmp(sourceval,
1272 ZPROP_SOURCE_VAL_RECVD) == 0) {
1273 sourcetype = ZPROP_SRC_RECEIVED;
1275 sourcetype = ZPROP_SRC_INHERITED;
1276 (void) strlcpy(source,
1277 sourceval, sizeof (source));
1281 if (received && (zfs_prop_get_recvd(zhp,
1282 pl->pl_user_prop, rbuf, sizeof (rbuf),
1283 cbp->cb_literal) == 0))
1286 zprop_print_one_property(zfs_get_name(zhp), cbp,
1287 pl->pl_user_prop, strval, sourcetype,
1296 zfs_do_get(int argc, char **argv)
1298 zprop_get_cbdata_t cb = { 0 };
1299 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1300 char *value, *fields;
1303 zprop_list_t fake_name = { 0 };
1306 * Set up default columns and sources.
1308 cb.cb_sources = ZPROP_SRC_ALL;
1309 cb.cb_columns[0] = GET_COL_NAME;
1310 cb.cb_columns[1] = GET_COL_PROPERTY;
1311 cb.cb_columns[2] = GET_COL_VALUE;
1312 cb.cb_columns[3] = GET_COL_SOURCE;
1313 cb.cb_type = ZFS_TYPE_DATASET;
1316 while ((c = getopt(argc, argv, ":d:o:s:rHp")) != -1) {
1319 cb.cb_literal = B_TRUE;
1322 limit = parse_depth(optarg, &flags);
1325 flags |= ZFS_ITER_RECURSE;
1328 cb.cb_scripted = B_TRUE;
1331 (void) fprintf(stderr, gettext("missing argument for "
1332 "'%c' option\n"), optopt);
1337 * Process the set of columns to display. We zero out
1338 * the structure to give us a blank slate.
1340 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1342 while (*optarg != '\0') {
1343 static char *col_subopts[] =
1344 { "name", "property", "value", "received",
1345 "source", "all", NULL };
1347 if (i == ZFS_GET_NCOLS) {
1348 (void) fprintf(stderr, gettext("too "
1349 "many fields given to -o "
1354 switch (getsubopt(&optarg, col_subopts,
1357 cb.cb_columns[i++] = GET_COL_NAME;
1360 cb.cb_columns[i++] = GET_COL_PROPERTY;
1363 cb.cb_columns[i++] = GET_COL_VALUE;
1366 cb.cb_columns[i++] = GET_COL_RECVD;
1367 flags |= ZFS_ITER_RECVD_PROPS;
1370 cb.cb_columns[i++] = GET_COL_SOURCE;
1374 (void) fprintf(stderr,
1375 gettext("\"all\" conflicts "
1376 "with specific fields "
1377 "given to -o option\n"));
1380 cb.cb_columns[0] = GET_COL_NAME;
1381 cb.cb_columns[1] = GET_COL_PROPERTY;
1382 cb.cb_columns[2] = GET_COL_VALUE;
1383 cb.cb_columns[3] = GET_COL_RECVD;
1384 cb.cb_columns[4] = GET_COL_SOURCE;
1385 flags |= ZFS_ITER_RECVD_PROPS;
1389 (void) fprintf(stderr,
1390 gettext("invalid column name "
1399 while (*optarg != '\0') {
1400 static char *source_subopts[] = {
1401 "local", "default", "inherited",
1402 "received", "temporary", "none",
1405 switch (getsubopt(&optarg, source_subopts,
1408 cb.cb_sources |= ZPROP_SRC_LOCAL;
1411 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1414 cb.cb_sources |= ZPROP_SRC_INHERITED;
1417 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1420 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1423 cb.cb_sources |= ZPROP_SRC_NONE;
1426 (void) fprintf(stderr,
1427 gettext("invalid source "
1435 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1445 (void) fprintf(stderr, gettext("missing property "
1452 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1460 * As part of zfs_expand_proplist(), we keep track of the maximum column
1461 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1462 * need to know the maximum name length. However, the user likely did
1463 * not specify 'name' as one of the properties to fetch, so we need to
1464 * make sure we always include at least this property for
1465 * print_get_headers() to work properly.
1467 if (cb.cb_proplist != NULL) {
1468 fake_name.pl_prop = ZFS_PROP_NAME;
1469 fake_name.pl_width = strlen(gettext("NAME"));
1470 fake_name.pl_next = cb.cb_proplist;
1471 cb.cb_proplist = &fake_name;
1474 cb.cb_first = B_TRUE;
1476 /* run for each object */
1477 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL,
1478 &cb.cb_proplist, limit, get_callback, &cb);
1480 if (cb.cb_proplist == &fake_name)
1481 zprop_free_list(fake_name.pl_next);
1483 zprop_free_list(cb.cb_proplist);
1489 * inherit [-rS] <property> <fs|vol> ...
1491 * -r Recurse over all children
1492 * -S Revert to received value, if any
1494 * For each dataset specified on the command line, inherit the given property
1495 * from its parent. Inheriting a property at the pool level will cause it to
1496 * use the default value. The '-r' flag will recurse over all children, and is
1497 * useful for setting a property on a hierarchy-wide basis, regardless of any
1498 * local modifications for each dataset.
1501 typedef struct inherit_cbdata {
1502 const char *cb_propname;
1503 boolean_t cb_received;
1507 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1509 inherit_cbdata_t *cb = data;
1510 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1513 * If we're doing it recursively, then ignore properties that
1514 * are not valid for this type of dataset.
1516 if (prop != ZPROP_INVAL &&
1517 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1520 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1524 inherit_cb(zfs_handle_t *zhp, void *data)
1526 inherit_cbdata_t *cb = data;
1528 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1532 zfs_do_inherit(int argc, char **argv)
1536 inherit_cbdata_t cb = { 0 };
1540 boolean_t received = B_FALSE;
1543 while ((c = getopt(argc, argv, "rS")) != -1) {
1546 flags |= ZFS_ITER_RECURSE;
1553 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1562 /* check number of arguments */
1564 (void) fprintf(stderr, gettext("missing property argument\n"));
1568 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1576 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1577 if (zfs_prop_readonly(prop)) {
1578 (void) fprintf(stderr, gettext(
1579 "%s property is read-only\n"),
1583 if (!zfs_prop_inheritable(prop) && !received) {
1584 (void) fprintf(stderr, gettext("'%s' property cannot "
1585 "be inherited\n"), propname);
1586 if (prop == ZFS_PROP_QUOTA ||
1587 prop == ZFS_PROP_RESERVATION ||
1588 prop == ZFS_PROP_REFQUOTA ||
1589 prop == ZFS_PROP_REFRESERVATION)
1590 (void) fprintf(stderr, gettext("use 'zfs set "
1591 "%s=none' to clear\n"), propname);
1594 if (received && (prop == ZFS_PROP_VOLSIZE ||
1595 prop == ZFS_PROP_VERSION)) {
1596 (void) fprintf(stderr, gettext("'%s' property cannot "
1597 "be reverted to a received value\n"), propname);
1600 } else if (!zfs_prop_user(propname)) {
1601 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1606 cb.cb_propname = propname;
1607 cb.cb_received = received;
1609 if (flags & ZFS_ITER_RECURSE) {
1610 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1611 NULL, NULL, 0, inherit_recurse_cb, &cb);
1613 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1614 NULL, NULL, 0, inherit_cb, &cb);
1620 typedef struct upgrade_cbdata {
1621 uint64_t cb_numupgraded;
1622 uint64_t cb_numsamegraded;
1623 uint64_t cb_numfailed;
1624 uint64_t cb_version;
1626 boolean_t cb_foundone;
1627 char cb_lastfs[ZFS_MAXNAMELEN];
1631 same_pool(zfs_handle_t *zhp, const char *name)
1633 int len1 = strcspn(name, "/@");
1634 const char *zhname = zfs_get_name(zhp);
1635 int len2 = strcspn(zhname, "/@");
1639 return (strncmp(name, zhname, len1) == 0);
1643 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1645 upgrade_cbdata_t *cb = data;
1646 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1648 /* list if it's old/new */
1649 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1650 (cb->cb_newer && version > ZPL_VERSION)) {
1653 str = gettext("The following filesystems are "
1654 "formatted using a newer software version and\n"
1655 "cannot be accessed on the current system.\n\n");
1657 str = gettext("The following filesystems are "
1658 "out of date, and can be upgraded. After being\n"
1659 "upgraded, these filesystems (and any 'zfs send' "
1660 "streams generated from\n"
1661 "subsequent snapshots) will no longer be "
1662 "accessible by older software versions.\n\n");
1665 if (!cb->cb_foundone) {
1667 (void) printf(gettext("VER FILESYSTEM\n"));
1668 (void) printf(gettext("--- ------------\n"));
1669 cb->cb_foundone = B_TRUE;
1672 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1679 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1681 upgrade_cbdata_t *cb = data;
1682 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1683 int needed_spa_version;
1686 if (zfs_spa_version(zhp, &spa_version) < 0)
1689 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1691 if (needed_spa_version < 0)
1694 if (spa_version < needed_spa_version) {
1696 (void) printf(gettext("%s: can not be "
1697 "upgraded; the pool version needs to first "
1698 "be upgraded\nto version %d\n\n"),
1699 zfs_get_name(zhp), needed_spa_version);
1705 if (version < cb->cb_version) {
1707 (void) snprintf(verstr, sizeof (verstr),
1708 "%llu", cb->cb_version);
1709 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1711 * If they did "zfs upgrade -a", then we could
1712 * be doing ioctls to different pools. We need
1713 * to log this history once to each pool.
1715 verify(zpool_stage_history(g_zfs, history_str) == 0);
1717 if (zfs_prop_set(zhp, "version", verstr) == 0)
1718 cb->cb_numupgraded++;
1721 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1722 } else if (version > cb->cb_version) {
1723 /* can't downgrade */
1724 (void) printf(gettext("%s: can not be downgraded; "
1725 "it is already at version %u\n"),
1726 zfs_get_name(zhp), version);
1729 cb->cb_numsamegraded++;
1737 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1740 zfs_do_upgrade(int argc, char **argv)
1742 boolean_t all = B_FALSE;
1743 boolean_t showversions = B_FALSE;
1745 upgrade_cbdata_t cb = { 0 };
1747 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1750 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1753 flags |= ZFS_ITER_RECURSE;
1756 showversions = B_TRUE;
1759 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1760 optarg, &cb.cb_version) != 0) {
1761 (void) fprintf(stderr,
1762 gettext("invalid version %s\n"), optarg);
1771 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1780 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1782 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1783 cb.cb_version || argc))
1785 if ((all || argc) && (showversions))
1791 /* Show info on available versions. */
1792 (void) printf(gettext("The following filesystem versions are "
1794 (void) printf(gettext("VER DESCRIPTION\n"));
1795 (void) printf("--- -----------------------------------------"
1796 "---------------\n");
1797 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1798 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1799 (void) printf(gettext(" 3 Case insensitive and File system "
1800 "unique identifier (FUID)\n"));
1801 (void) printf(gettext(" 4 userquota, groupquota "
1803 (void) printf(gettext(" 5 System attributes\n"));
1804 (void) printf(gettext("\nFor more information on a particular "
1805 "version, including supported releases,\n"));
1806 (void) printf("see the ZFS Administration Guide.\n\n");
1808 } else if (argc || all) {
1809 /* Upgrade filesystems */
1810 if (cb.cb_version == 0)
1811 cb.cb_version = ZPL_VERSION;
1812 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
1813 NULL, NULL, 0, upgrade_set_callback, &cb);
1814 (void) printf(gettext("%llu filesystems upgraded\n"),
1816 if (cb.cb_numsamegraded) {
1817 (void) printf(gettext("%llu filesystems already at "
1819 cb.cb_numsamegraded);
1821 if (cb.cb_numfailed != 0)
1824 /* List old-version filesytems */
1826 (void) printf(gettext("This system is currently running "
1827 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1829 flags |= ZFS_ITER_RECURSE;
1830 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1831 NULL, NULL, 0, upgrade_list_callback, &cb);
1833 found = cb.cb_foundone;
1834 cb.cb_foundone = B_FALSE;
1835 cb.cb_newer = B_TRUE;
1837 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1838 NULL, NULL, 0, upgrade_list_callback, &cb);
1840 if (!cb.cb_foundone && !found) {
1841 (void) printf(gettext("All filesystems are "
1842 "formatted with the current version.\n"));
1849 #define USTYPE_USR_BIT (0)
1850 #define USTYPE_GRP_BIT (1)
1851 #define USTYPE_PSX_BIT (2)
1852 #define USTYPE_SMB_BIT (3)
1854 #define USTYPE_USR (1 << USTYPE_USR_BIT)
1855 #define USTYPE_GRP (1 << USTYPE_GRP_BIT)
1857 #define USTYPE_PSX (1 << USTYPE_PSX_BIT)
1858 #define USTYPE_SMB (1 << USTYPE_SMB_BIT)
1860 #define USTYPE_PSX_USR (USTYPE_PSX | USTYPE_USR)
1861 #define USTYPE_SMB_USR (USTYPE_SMB | USTYPE_USR)
1862 #define USTYPE_PSX_GRP (USTYPE_PSX | USTYPE_GRP)
1863 #define USTYPE_SMB_GRP (USTYPE_SMB | USTYPE_GRP)
1864 #define USTYPE_ALL (USTYPE_PSX_USR | USTYPE_SMB_USR \
1865 | USTYPE_PSX_GRP | USTYPE_SMB_GRP)
1868 #define USPROP_USED_BIT (0)
1869 #define USPROP_QUOTA_BIT (1)
1871 #define USPROP_USED (1 << USPROP_USED_BIT)
1872 #define USPROP_QUOTA (1 << USPROP_QUOTA_BIT)
1874 typedef struct us_node {
1876 uu_avl_node_t usn_avlnode;
1877 uu_list_node_t usn_listnode;
1880 typedef struct us_cbdata {
1882 uu_avl_pool_t *cb_avl_pool;
1884 boolean_t cb_numname;
1885 boolean_t cb_nicenum;
1886 boolean_t cb_sid2posix;
1887 zfs_userquota_prop_t cb_prop;
1888 zfs_sort_column_t *cb_sortcol;
1889 size_t cb_max_typelen;
1890 size_t cb_max_namelen;
1891 size_t cb_max_usedlen;
1892 size_t cb_max_quotalen;
1896 zfs_sort_column_t *si_sortcol;
1897 boolean_t si_num_name;
1898 boolean_t si_parsable;
1902 us_compare(const void *larg, const void *rarg, void *unused)
1904 const us_node_t *l = larg;
1905 const us_node_t *r = rarg;
1907 us_sort_info_t *si = (us_sort_info_t *)unused;
1908 zfs_sort_column_t *sortcol = si->si_sortcol;
1909 boolean_t num_name = si->si_num_name;
1910 nvlist_t *lnvl = l->usn_nvl;
1911 nvlist_t *rnvl = r->usn_nvl;
1913 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
1920 zfs_prop_t prop = sortcol->sc_prop;
1921 const char *propname = NULL;
1922 boolean_t reverse = sortcol->sc_reverse;
1927 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
1928 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
1930 rc = (rv32 > lv32) ? 1 : -1;
1935 (void) nvlist_lookup_uint32(lnvl, propname,
1937 (void) nvlist_lookup_uint32(rnvl, propname,
1940 rc = (rv32 > lv32) ? 1 : -1;
1942 (void) nvlist_lookup_string(lnvl, propname,
1944 (void) nvlist_lookup_string(rnvl, propname,
1946 rc = strcmp(lvstr, rvstr);
1951 case ZFS_PROP_QUOTA:
1952 if (ZFS_PROP_USED == prop)
1956 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
1957 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
1959 rc = (rv64 > lv64) ? 1 : -1;
1964 return (reverse ? 1 : -1);
1966 return (reverse ? -1 : 1);
1972 static inline const char *
1973 us_type2str(unsigned field_type)
1975 switch (field_type) {
1976 case USTYPE_PSX_USR:
1977 return ("POSIX User");
1978 case USTYPE_PSX_GRP:
1979 return ("POSIX Group");
1980 case USTYPE_SMB_USR:
1981 return ("SMB User");
1982 case USTYPE_SMB_GRP:
1983 return ("SMB Group");
1985 return ("Undefined");
1993 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
1995 us_cbdata_t *cb = (us_cbdata_t *)arg;
1996 zfs_userquota_prop_t prop = cb->cb_prop;
2002 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2003 uu_avl_t *avl = cb->cb_avl;
2007 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2009 const char *typestr;
2013 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2015 if (domain == NULL || domain[0] == '\0') {
2017 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2018 type = USTYPE_PSX_GRP;
2019 struct group *g = getgrgid(rid);
2023 type = USTYPE_PSX_USR;
2024 struct passwd *p = getpwuid(rid);
2029 char sid[ZFS_MAXNAMELEN+32];
2034 directory_error_t e;
2037 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2039 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2040 type = USTYPE_SMB_GRP;
2042 err = sid_to_id(sid, B_FALSE, &id);
2045 type = USTYPE_SMB_USR;
2047 err = sid_to_id(sid, B_TRUE, &id);
2055 e = directory_name_from_sid(NULL, sid, &name, &classes);
2057 directory_error_free(e);
2068 * if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA)
2074 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED)
2079 (void) snprintf(namebuf, sizeof (namebuf), "%u", rid);
2084 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2086 (void) sprintf(sizebuf, "%llu", space);
2088 node = safe_malloc(sizeof (us_node_t));
2089 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2091 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
2096 if (nvlist_add_uint32(props, "type", type) != 0)
2099 if (cb->cb_numname) {
2100 if (nvlist_add_uint32(props, "name", rid) != 0)
2102 namelen = strlen(namebuf);
2104 if (nvlist_add_string(props, "name", name) != 0)
2106 namelen = strlen(name);
2109 typestr = us_type2str(type);
2110 typelen = strlen(gettext(typestr));
2111 if (typelen > cb->cb_max_typelen)
2112 cb->cb_max_typelen = typelen;
2114 if (namelen > cb->cb_max_namelen)
2115 cb->cb_max_namelen = namelen;
2117 sizelen = strlen(sizebuf);
2118 if (0 == strcmp(propname, "used")) {
2119 if (sizelen > cb->cb_max_usedlen)
2120 cb->cb_max_usedlen = sizelen;
2122 if (sizelen > cb->cb_max_quotalen)
2123 cb->cb_max_quotalen = sizelen;
2126 node->usn_nvl = props;
2128 n = uu_avl_find(avl, node, &sortinfo, &idx);
2130 uu_avl_insert(avl, node, idx);
2135 props = node->usn_nvl;
2138 if (nvlist_add_uint64(props, propname, space) != 0)
2144 static inline boolean_t
2145 usprop_check(zfs_userquota_prop_t p, unsigned types, unsigned props)
2151 case ZFS_PROP_USERUSED:
2155 case ZFS_PROP_USERQUOTA:
2157 prop = USPROP_QUOTA;
2159 case ZFS_PROP_GROUPUSED:
2163 case ZFS_PROP_GROUPQUOTA:
2165 prop = USPROP_QUOTA;
2171 return (type & types && prop & props);
2174 #define USFIELD_TYPE (1 << 0)
2175 #define USFIELD_NAME (1 << 1)
2176 #define USFIELD_USED (1 << 2)
2177 #define USFIELD_QUOTA (1 << 3)
2178 #define USFIELD_ALL (USFIELD_TYPE | USFIELD_NAME | USFIELD_USED | USFIELD_QUOTA)
2181 parsefields(unsigned *fieldsp, char **names, unsigned *bits, size_t len)
2183 char *field = optarg;
2188 boolean_t found = B_FALSE;
2189 delim = strchr(field, ',');
2193 for (i = 0; i < len; i++)
2194 if (0 == strcmp(field, names[i])) {
2196 *fieldsp |= bits[i];
2201 (void) fprintf(stderr, gettext("invalid type '%s'"
2202 "for -t option\n"), field);
2213 static char *type_names[] = { "posixuser", "smbuser", "posixgroup", "smbgroup",
2215 static unsigned type_bits[] = {
2223 static char *us_field_names[] = { "type", "name", "used", "quota" };
2224 static unsigned us_field_bits[] = {
2232 print_us_node(boolean_t scripted, boolean_t parseable, unsigned fields,
2233 size_t type_width, size_t name_width, size_t used_width,
2234 size_t quota_width, us_node_t *node)
2236 nvlist_t *nvl = node->usn_nvl;
2237 nvpair_t *nvp = NULL;
2238 char valstr[ZFS_MAXNAMELEN];
2239 boolean_t first = B_TRUE;
2240 boolean_t quota_found = B_FALSE;
2242 if (fields & USFIELD_QUOTA && !nvlist_exists(nvl, "quota"))
2243 if (nvlist_add_string(nvl, "quota", "none") != 0)
2246 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2247 char *pname = nvpair_name(nvp);
2248 data_type_t type = nvpair_type(nvp);
2251 char *strval = NULL;
2255 for (i = 0; i < 4; i++) {
2256 if (0 == strcmp(pname, us_field_names[i])) {
2257 field = us_field_bits[i];
2262 if (!(field & fields))
2266 case DATA_TYPE_UINT32:
2267 (void) nvpair_value_uint32(nvp, &val32);
2269 case DATA_TYPE_UINT64:
2270 (void) nvpair_value_uint64(nvp, &val64);
2272 case DATA_TYPE_STRING:
2273 (void) nvpair_value_string(nvp, &strval);
2276 (void) fprintf(stderr, "Invalid data type\n");
2281 (void) printf("\t");
2287 strval = (char *)us_type2str(val32);
2291 if (type == DATA_TYPE_UINT64) {
2292 (void) sprintf(valstr, "%llu", val64);
2299 if (type == DATA_TYPE_UINT64) {
2300 (void) nvpair_value_uint64(nvp, &val64);
2302 (void) sprintf(valstr, "%llu", val64);
2304 zfs_nicenum(val64, valstr,
2309 if (field == USFIELD_USED)
2312 quota_found = B_FALSE;
2313 width = quota_width;
2319 if (field == USFIELD_QUOTA && !quota_found)
2320 (void) printf("%*s", width, strval);
2322 if (type == DATA_TYPE_STRING)
2323 (void) printf("%-*s", width, strval);
2325 (void) printf("%*s", width, strval);
2332 (void) printf("\n");
2336 print_us(boolean_t scripted, boolean_t parsable, unsigned fields,
2337 unsigned type_width, unsigned name_width, unsigned used_width,
2338 unsigned quota_width, boolean_t rmnode, uu_avl_t *avl)
2340 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2344 size_t width[4] = { type_width, name_width, used_width, quota_width };
2347 boolean_t first = B_TRUE;
2348 for (i = 0; i < 4; i++) {
2349 unsigned field = us_field_bits[i];
2350 if (!(field & fields))
2353 col = gettext(us_field_hdr[i]);
2354 if (field == USFIELD_TYPE || field == USFIELD_NAME)
2355 (void) printf(first?"%-*s":" %-*s", width[i],
2358 (void) printf(first?"%*s":" %*s", width[i],
2362 (void) printf("\n");
2365 for (node = uu_avl_first(avl); node != NULL;
2366 node = uu_avl_next(avl, node)) {
2367 print_us_node(scripted, parsable, fields, type_width,
2368 name_width, used_width, used_width, node);
2370 nvlist_free(node->usn_nvl);
2375 zfs_do_userspace(int argc, char **argv)
2378 zfs_userquota_prop_t p;
2380 uu_avl_pool_t *avl_pool;
2382 uu_avl_walk_t *walk;
2385 boolean_t scripted = B_FALSE;
2386 boolean_t prtnum = B_FALSE;
2387 boolean_t parseable = B_FALSE;
2388 boolean_t sid2posix = B_FALSE;
2391 zfs_sort_column_t *default_sortcol = NULL;
2392 zfs_sort_column_t *sortcol = NULL;
2393 unsigned types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2394 unsigned fields = 0;
2395 unsigned props = USPROP_USED | USPROP_QUOTA;
2398 boolean_t resort_avl = B_FALSE;
2404 if (0 == strcmp(cmd, "groupspace"))
2405 /* toggle default group types */
2406 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2409 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2421 if (parsefields(&fields, us_field_names, us_field_bits,
2426 if (zfs_add_sort_column(&sortcol, optarg,
2428 (void) fprintf(stderr,
2429 gettext("invalid property '%s'\n"), optarg);
2434 if (zfs_add_sort_column(&sortcol, optarg,
2436 (void) fprintf(stderr,
2437 gettext("invalid property '%s'\n"), optarg);
2442 if (parsefields(&types, type_names, type_bits, 5))
2449 (void) fprintf(stderr, gettext("missing argument for "
2450 "'%c' option\n"), optopt);
2454 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2463 /* ok, now we have sorted by default colums (type,name) avl tree */
2465 zfs_sort_column_t *sc;
2466 for (sc = sortcol; sc; sc = sc->sc_next) {
2467 if (sc->sc_prop == ZFS_PROP_QUOTA) {
2468 resort_avl = B_TRUE;
2475 fields = USFIELD_ALL;
2477 if ((zhp = zfs_open(g_zfs, argv[argc-1], ZFS_TYPE_DATASET)) == NULL)
2480 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2481 offsetof(us_node_t, usn_avlnode),
2482 us_compare, UU_DEFAULT)) == NULL)
2484 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2487 if (sortcol && !resort_avl)
2488 cb.cb_sortcol = sortcol;
2490 (void) zfs_add_sort_column(&default_sortcol, "type", B_FALSE);
2491 (void) zfs_add_sort_column(&default_sortcol, "name", B_FALSE);
2492 cb.cb_sortcol = default_sortcol;
2494 cb.cb_numname = prtnum;
2495 cb.cb_nicenum = !parseable;
2496 cb.cb_avl_pool = avl_pool;
2497 cb.cb_avl = avl_tree;
2498 cb.cb_sid2posix = sid2posix;
2499 cb.cb_max_typelen = strlen(gettext("TYPE"));
2500 cb.cb_max_namelen = strlen(gettext("NAME"));
2501 cb.cb_max_usedlen = strlen(gettext("USED"));
2502 cb.cb_max_quotalen = strlen(gettext("QUOTA"));
2504 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2505 if (!usprop_check(p, types, props))
2509 error = zfs_userspace(zhp, p, userspace_cb, &cb);
2518 uu_list_pool_t *listpool;
2520 uu_avl_index_t idx = 0;
2521 uu_list_index_t idx2 = 0;
2522 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2523 offsetof(us_node_t, usn_listnode), NULL,
2525 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2527 node = uu_avl_first(avl_tree);
2528 uu_list_node_init(node, &node->usn_listnode, listpool);
2529 while (node != NULL) {
2531 node = uu_avl_next(avl_tree, node);
2532 uu_avl_remove(avl_tree, rmnode);
2533 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL) {
2534 uu_list_insert(list, rmnode, idx2);
2538 for (node = uu_list_first(list); node != NULL;
2539 node = uu_list_next(list, node)) {
2540 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2541 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) ==
2543 uu_avl_insert(avl_tree, node, idx);
2546 uu_list_destroy(list);
2549 /* print & free node`s nvlist memory */
2550 print_us(scripted, parseable, fields, cb.cb_max_typelen,
2551 cb.cb_max_namelen, cb.cb_max_usedlen,
2552 cb.cb_max_quotalen, B_TRUE, cb.cb_avl);
2555 zfs_free_sort_columns(sortcol);
2556 zfs_free_sort_columns(default_sortcol);
2559 * Finally, clean up the AVL tree.
2561 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2564 while ((node = uu_avl_walk_next(walk)) != NULL) {
2565 uu_avl_remove(cb.cb_avl, node);
2569 uu_avl_walk_end(walk);
2570 uu_avl_destroy(avl_tree);
2571 uu_avl_pool_destroy(avl_pool);
2577 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2578 * [-s property [-s property]...] [-S property [-S property]...]
2581 * -r Recurse over all children
2582 * -d Limit recursion by depth.
2583 * -H Scripted mode; elide headers and separate columns by tabs
2584 * -o Control which fields to display.
2585 * -t Control which object types to display.
2586 * -s Specify sort columns, descending order.
2587 * -S Specify sort columns, ascending order.
2589 * When given no arguments, lists all filesystems in the system.
2590 * Otherwise, list the specified datasets, optionally recursing down them if
2591 * '-r' is specified.
2593 typedef struct list_cbdata {
2595 boolean_t cb_scripted;
2596 zprop_list_t *cb_proplist;
2600 * Given a list of columns to display, output appropriate headers for each one.
2603 print_header(zprop_list_t *pl)
2605 char headerbuf[ZFS_MAXPROPLEN];
2608 boolean_t first = B_TRUE;
2609 boolean_t right_justify;
2611 for (; pl != NULL; pl = pl->pl_next) {
2618 right_justify = B_FALSE;
2619 if (pl->pl_prop != ZPROP_INVAL) {
2620 header = zfs_prop_column_name(pl->pl_prop);
2621 right_justify = zfs_prop_align_right(pl->pl_prop);
2623 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2624 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2625 headerbuf[i] = '\0';
2629 if (pl->pl_next == NULL && !right_justify)
2630 (void) printf("%s", header);
2631 else if (right_justify)
2632 (void) printf("%*s", pl->pl_width, header);
2634 (void) printf("%-*s", pl->pl_width, header);
2637 (void) printf("\n");
2641 * Given a dataset and a list of fields, print out all the properties according
2642 * to the described layout.
2645 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
2647 boolean_t first = B_TRUE;
2648 char property[ZFS_MAXPROPLEN];
2649 nvlist_t *userprops = zfs_get_user_props(zhp);
2652 boolean_t right_justify;
2655 for (; pl != NULL; pl = pl->pl_next) {
2658 (void) printf("\t");
2665 if (pl->pl_prop != ZPROP_INVAL) {
2666 if (zfs_prop_get(zhp, pl->pl_prop, property,
2667 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2672 right_justify = zfs_prop_align_right(pl->pl_prop);
2673 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2674 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2675 property, sizeof (property), B_FALSE) != 0)
2679 right_justify = B_TRUE;
2681 if (nvlist_lookup_nvlist(userprops,
2682 pl->pl_user_prop, &propval) != 0)
2685 verify(nvlist_lookup_string(propval,
2686 ZPROP_VALUE, &propstr) == 0);
2687 right_justify = B_FALSE;
2690 width = pl->pl_width;
2693 * If this is being called in scripted mode, or if this is the
2694 * last column and it is left-justified, don't include a width
2697 if (scripted || (pl->pl_next == NULL && !right_justify))
2698 (void) printf("%s", propstr);
2699 else if (right_justify)
2700 (void) printf("%*s", width, propstr);
2702 (void) printf("%-*s", width, propstr);
2705 (void) printf("\n");
2709 * Generic callback function to list a dataset or snapshot.
2712 list_callback(zfs_handle_t *zhp, void *data)
2714 list_cbdata_t *cbp = data;
2716 if (cbp->cb_first) {
2717 if (!cbp->cb_scripted)
2718 print_header(cbp->cb_proplist);
2719 cbp->cb_first = B_FALSE;
2722 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2728 zfs_do_list(int argc, char **argv)
2731 boolean_t scripted = B_FALSE;
2732 static char default_fields[] =
2733 "name,used,available,referenced,mountpoint";
2734 int types = ZFS_TYPE_DATASET;
2735 boolean_t types_specified = B_FALSE;
2736 char *fields = NULL;
2737 list_cbdata_t cb = { 0 };
2741 zfs_sort_column_t *sortcol = NULL;
2742 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2745 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2751 limit = parse_depth(optarg, &flags);
2754 flags |= ZFS_ITER_RECURSE;
2760 if (zfs_add_sort_column(&sortcol, optarg,
2762 (void) fprintf(stderr,
2763 gettext("invalid property '%s'\n"), optarg);
2768 if (zfs_add_sort_column(&sortcol, optarg,
2770 (void) fprintf(stderr,
2771 gettext("invalid property '%s'\n"), optarg);
2777 types_specified = B_TRUE;
2778 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2779 while (*optarg != '\0') {
2780 static char *type_subopts[] = { "filesystem",
2781 "volume", "snapshot", "all", NULL };
2783 switch (getsubopt(&optarg, type_subopts,
2786 types |= ZFS_TYPE_FILESYSTEM;
2789 types |= ZFS_TYPE_VOLUME;
2792 types |= ZFS_TYPE_SNAPSHOT;
2795 types = ZFS_TYPE_DATASET;
2799 (void) fprintf(stderr,
2800 gettext("invalid type '%s'\n"),
2807 (void) fprintf(stderr, gettext("missing argument for "
2808 "'%c' option\n"), optopt);
2812 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2822 fields = default_fields;
2825 * If "-o space" and no types were specified, don't display snapshots.
2827 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
2828 types &= ~ZFS_TYPE_SNAPSHOT;
2831 * If the user specifies '-o all', the zprop_get_list() doesn't
2832 * normally include the name of the dataset. For 'zfs list', we always
2833 * want this property to be first.
2835 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
2839 cb.cb_scripted = scripted;
2840 cb.cb_first = B_TRUE;
2842 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
2843 limit, list_callback, &cb);
2845 zprop_free_list(cb.cb_proplist);
2846 zfs_free_sort_columns(sortcol);
2848 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
2849 (void) printf(gettext("no datasets available\n"));
2855 * zfs rename <fs | snap | vol> <fs | snap | vol>
2856 * zfs rename -p <fs | vol> <fs | vol>
2857 * zfs rename -r <snap> <snap>
2858 * zfs rename -u [-p] <fs> <fs>
2860 * Renames the given dataset to another of the same type.
2862 * The '-p' flag creates all the non-existing ancestors of the target first.
2866 zfs_do_rename(int argc, char **argv)
2869 renameflags_t flags = { 0 };
2871 boolean_t parents = B_FALSE;
2874 while ((c = getopt(argc, argv, "pru")) != -1) {
2880 flags.recurse = B_TRUE;
2883 flags.nounmount = B_TRUE;
2887 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2896 /* check number of arguments */
2898 (void) fprintf(stderr, gettext("missing source dataset "
2903 (void) fprintf(stderr, gettext("missing target dataset "
2908 (void) fprintf(stderr, gettext("too many arguments\n"));
2912 if (flags.recurse && parents) {
2913 (void) fprintf(stderr, gettext("-p and -r options are mutually "
2918 if (flags.recurse && strchr(argv[0], '@') == 0) {
2919 (void) fprintf(stderr, gettext("source dataset for recursive "
2920 "rename must be a snapshot\n"));
2924 if (flags.nounmount && parents) {
2925 (void) fprintf(stderr, gettext("-u and -r options are mutually "
2930 if (flags.nounmount)
2931 types = ZFS_TYPE_FILESYSTEM;
2933 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
2935 types = ZFS_TYPE_DATASET;
2937 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
2940 /* If we were asked and the name looks good, try to create ancestors. */
2941 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
2942 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
2947 ret = (zfs_rename(zhp, argv[1], flags) != 0);
2956 * Promotes the given clone fs to be the parent
2960 zfs_do_promote(int argc, char **argv)
2966 if (argc > 1 && argv[1][0] == '-') {
2967 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2972 /* check number of arguments */
2974 (void) fprintf(stderr, gettext("missing clone filesystem"
2979 (void) fprintf(stderr, gettext("too many arguments\n"));
2983 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2987 ret = (zfs_promote(zhp) != 0);
2995 * zfs rollback [-rRf] <snapshot>
2997 * -r Delete any intervening snapshots before doing rollback
2998 * -R Delete any snapshots and their clones
2999 * -f ignored for backwards compatability
3001 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3002 * since then and making it the active dataset. If more recent snapshots exist,
3003 * the command will complain unless the '-r' flag is given.
3005 typedef struct rollback_cbdata {
3011 boolean_t cb_recurse;
3012 boolean_t cb_dependent;
3013 } rollback_cbdata_t;
3016 * Report any snapshots more recent than the one specified. Used when '-r' is
3017 * not specified. We reuse this same callback for the snapshot dependents - if
3018 * 'cb_dependent' is set, then this is a dependent and we should report it
3019 * without checking the transaction group.
3022 rollback_check(zfs_handle_t *zhp, void *data)
3024 rollback_cbdata_t *cbp = data;
3026 if (cbp->cb_doclones) {
3031 if (!cbp->cb_dependent) {
3032 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
3033 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3034 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3037 if (cbp->cb_first && !cbp->cb_recurse) {
3038 (void) fprintf(stderr, gettext("cannot "
3039 "rollback to '%s': more recent snapshots "
3042 (void) fprintf(stderr, gettext("use '-r' to "
3043 "force deletion of the following "
3049 if (cbp->cb_recurse) {
3050 cbp->cb_dependent = B_TRUE;
3051 if (zfs_iter_dependents(zhp, B_TRUE,
3052 rollback_check, cbp) != 0) {
3056 cbp->cb_dependent = B_FALSE;
3058 (void) fprintf(stderr, "%s\n",
3063 if (cbp->cb_first && cbp->cb_recurse) {
3064 (void) fprintf(stderr, gettext("cannot rollback to "
3065 "'%s': clones of previous snapshots exist\n"),
3067 (void) fprintf(stderr, gettext("use '-R' to "
3068 "force deletion of the following clones and "
3074 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3082 zfs_do_rollback(int argc, char **argv)
3086 boolean_t force = B_FALSE;
3087 rollback_cbdata_t cb = { 0 };
3088 zfs_handle_t *zhp, *snap;
3089 char parentname[ZFS_MAXNAMELEN];
3093 while ((c = getopt(argc, argv, "rRf")) != -1) {
3106 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3115 /* check number of arguments */
3117 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3121 (void) fprintf(stderr, gettext("too many arguments\n"));
3125 /* open the snapshot */
3126 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3129 /* open the parent dataset */
3130 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3131 verify((delim = strrchr(parentname, '@')) != NULL);
3133 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3139 * Check for more recent snapshots and/or clones based on the presence
3142 cb.cb_target = argv[0];
3143 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3144 cb.cb_first = B_TRUE;
3146 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
3149 if ((ret = cb.cb_error) != 0)
3153 * Rollback parent to the given snapshot.
3155 ret = zfs_rollback(zhp, snap, force);
3168 * zfs set property=value { fs | snap | vol } ...
3170 * Sets the given property for all datasets specified on the command line.
3172 typedef struct set_cbdata {
3178 set_callback(zfs_handle_t *zhp, void *data)
3180 set_cbdata_t *cbp = data;
3182 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3183 switch (libzfs_errno(g_zfs)) {
3184 case EZFS_MOUNTFAILED:
3185 (void) fprintf(stderr, gettext("property may be set "
3186 "but unable to remount filesystem\n"));
3188 case EZFS_SHARENFSFAILED:
3189 (void) fprintf(stderr, gettext("property may be set "
3190 "but unable to reshare filesystem\n"));
3199 zfs_do_set(int argc, char **argv)
3204 /* check for options */
3205 if (argc > 1 && argv[1][0] == '-') {
3206 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3211 /* check number of arguments */
3213 (void) fprintf(stderr, gettext("missing property=value "
3218 (void) fprintf(stderr, gettext("missing dataset name\n"));
3222 /* validate property=value argument */
3223 cb.cb_propname = argv[1];
3224 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3225 (cb.cb_value[1] == '\0')) {
3226 (void) fprintf(stderr, gettext("missing value in "
3227 "property=value argument\n"));
3231 *cb.cb_value = '\0';
3234 if (*cb.cb_propname == '\0') {
3235 (void) fprintf(stderr,
3236 gettext("missing property in property=value argument\n"));
3240 ret = zfs_for_each(argc - 2, argv + 2, 0,
3241 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3247 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3249 * Creates a snapshot with the given name. While functionally equivalent to
3250 * 'zfs create', it is a separate command to differentiate intent.
3253 zfs_do_snapshot(int argc, char **argv)
3255 boolean_t recursive = B_FALSE;
3260 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3264 while ((c = getopt(argc, argv, "ro:")) != -1) {
3267 if (parseprop(props))
3274 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3283 /* check number of arguments */
3285 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3289 (void) fprintf(stderr, gettext("too many arguments\n"));
3293 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
3295 if (ret && recursive)
3296 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3306 * zfs send [-vDp] -R [-i|-I <@snap>] <fs@snap>
3307 * zfs send [-vDp] [-i|-I <@snap>] <fs@snap>
3309 * Send a backup stream to stdout.
3312 zfs_do_send(int argc, char **argv)
3314 char *fromname = NULL;
3315 char *toname = NULL;
3318 sendflags_t flags = { 0 };
3321 boolean_t extraverbose = B_FALSE;
3324 while ((c = getopt(argc, argv, ":i:I:RDpv")) != -1) {
3335 flags.doall = B_TRUE;
3338 flags.replicate = B_TRUE;
3341 flags.props = B_TRUE;
3345 extraverbose = B_TRUE;
3346 flags.verbose = B_TRUE;
3349 flags.dedup = B_TRUE;
3352 (void) fprintf(stderr, gettext("missing argument for "
3353 "'%c' option\n"), optopt);
3357 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3366 /* check number of arguments */
3368 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3372 (void) fprintf(stderr, gettext("too many arguments\n"));
3376 if (isatty(STDOUT_FILENO)) {
3377 (void) fprintf(stderr,
3378 gettext("Error: Stream can not be written to a terminal.\n"
3379 "You must redirect standard output.\n"));
3383 cp = strchr(argv[0], '@');
3385 (void) fprintf(stderr,
3386 gettext("argument must be a snapshot\n"));
3391 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3396 * If they specified the full path to the snapshot, chop off
3397 * everything except the short name of the snapshot, but special
3398 * case if they specify the origin.
3400 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3401 char origin[ZFS_MAXNAMELEN];
3404 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3405 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3407 if (strcmp(origin, fromname) == 0) {
3409 flags.fromorigin = B_TRUE;
3412 if (cp != fromname && strcmp(argv[0], fromname)) {
3413 (void) fprintf(stderr,
3414 gettext("incremental source must be "
3415 "in same filesystem\n"));
3419 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3420 (void) fprintf(stderr,
3421 gettext("invalid incremental source\n"));
3427 if (flags.replicate && fromname == NULL)
3428 flags.doall = B_TRUE;
3430 err = zfs_send(zhp, fromname, toname, flags, STDOUT_FILENO, NULL, 0,
3431 extraverbose ? &dbgnv : NULL);
3435 * dump_nvlist prints to stdout, but that's been
3436 * redirected to a file. Make it print to stderr
3439 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3440 dump_nvlist(dbgnv, 0);
3449 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3451 * Restore a backup stream from stdin.
3454 zfs_do_receive(int argc, char **argv)
3457 recvflags_t flags = { 0 };
3460 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3463 flags.isprefix = B_TRUE;
3466 flags.isprefix = B_TRUE;
3467 flags.istail = B_TRUE;
3470 flags.dryrun = B_TRUE;
3473 flags.nomount = B_TRUE;
3476 flags.verbose = B_TRUE;
3479 flags.force = B_TRUE;
3482 (void) fprintf(stderr, gettext("missing argument for "
3483 "'%c' option\n"), optopt);
3487 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3496 /* check number of arguments */
3498 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3502 (void) fprintf(stderr, gettext("too many arguments\n"));
3506 if (isatty(STDIN_FILENO)) {
3507 (void) fprintf(stderr,
3508 gettext("Error: Backup stream can not be read "
3509 "from a terminal.\n"
3510 "You must redirect standard input.\n"));
3514 err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
3520 * allow/unallow stuff
3522 /* copied from zfs/sys/dsl_deleg.h */
3523 #define ZFS_DELEG_PERM_CREATE "create"
3524 #define ZFS_DELEG_PERM_DESTROY "destroy"
3525 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3526 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3527 #define ZFS_DELEG_PERM_CLONE "clone"
3528 #define ZFS_DELEG_PERM_PROMOTE "promote"
3529 #define ZFS_DELEG_PERM_RENAME "rename"
3530 #define ZFS_DELEG_PERM_MOUNT "mount"
3531 #define ZFS_DELEG_PERM_SHARE "share"
3532 #define ZFS_DELEG_PERM_SEND "send"
3533 #define ZFS_DELEG_PERM_RECEIVE "receive"
3534 #define ZFS_DELEG_PERM_ALLOW "allow"
3535 #define ZFS_DELEG_PERM_USERPROP "userprop"
3536 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3537 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3538 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3539 #define ZFS_DELEG_PERM_USERUSED "userused"
3540 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3541 #define ZFS_DELEG_PERM_HOLD "hold"
3542 #define ZFS_DELEG_PERM_RELEASE "release"
3543 #define ZFS_DELEG_PERM_DIFF "diff"
3545 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3547 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3548 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3549 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3550 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3551 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3552 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3553 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3554 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3555 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3556 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3557 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3558 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3559 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3560 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3561 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3562 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3564 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3565 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3566 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3567 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3568 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3569 { NULL, ZFS_DELEG_NOTE_NONE }
3572 /* permission structure */
3573 typedef struct deleg_perm {
3574 zfs_deleg_who_type_t dp_who_type;
3575 const char *dp_name;
3577 boolean_t dp_descend;
3581 typedef struct deleg_perm_node {
3582 deleg_perm_t dpn_perm;
3584 uu_avl_node_t dpn_avl_node;
3585 } deleg_perm_node_t;
3587 typedef struct fs_perm fs_perm_t;
3589 /* permissions set */
3590 typedef struct who_perm {
3591 zfs_deleg_who_type_t who_type;
3592 const char *who_name; /* id */
3593 char who_ug_name[256]; /* user/group name */
3594 fs_perm_t *who_fsperm; /* uplink */
3596 uu_avl_t *who_deleg_perm_avl; /* permissions */
3600 typedef struct who_perm_node {
3601 who_perm_t who_perm;
3602 uu_avl_node_t who_avl_node;
3605 typedef struct fs_perm_set fs_perm_set_t;
3606 /* fs permissions */
3608 const char *fsp_name;
3610 uu_avl_t *fsp_sc_avl; /* sets,create */
3611 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3613 fs_perm_set_t *fsp_set; /* uplink */
3617 typedef struct fs_perm_node {
3618 fs_perm_t fspn_fsperm;
3621 uu_list_node_t fspn_list_node;
3624 /* top level structure */
3625 struct fs_perm_set {
3626 uu_list_pool_t *fsps_list_pool;
3627 uu_list_t *fsps_list; /* list of fs_perms */
3629 uu_avl_pool_t *fsps_named_set_avl_pool;
3630 uu_avl_pool_t *fsps_who_perm_avl_pool;
3631 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
3634 static inline const char *
3635 deleg_perm_type(zfs_deleg_note_t note)
3641 case ZFS_DELEG_NOTE_GROUPQUOTA:
3642 case ZFS_DELEG_NOTE_GROUPUSED:
3643 case ZFS_DELEG_NOTE_USERPROP:
3644 case ZFS_DELEG_NOTE_USERQUOTA:
3645 case ZFS_DELEG_NOTE_USERUSED:
3647 return (gettext("other"));
3649 return (gettext("subcommand"));
3654 who_type2weight(zfs_deleg_who_type_t who_type)
3658 case ZFS_DELEG_NAMED_SET_SETS:
3659 case ZFS_DELEG_NAMED_SET:
3662 case ZFS_DELEG_CREATE_SETS:
3663 case ZFS_DELEG_CREATE:
3666 case ZFS_DELEG_USER_SETS:
3667 case ZFS_DELEG_USER:
3670 case ZFS_DELEG_GROUP_SETS:
3671 case ZFS_DELEG_GROUP:
3674 case ZFS_DELEG_EVERYONE_SETS:
3675 case ZFS_DELEG_EVERYONE:
3687 who_perm_compare(const void *larg, const void *rarg, void *unused)
3689 const who_perm_node_t *l = larg;
3690 const who_perm_node_t *r = rarg;
3691 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
3692 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
3693 int lweight = who_type2weight(ltype);
3694 int rweight = who_type2weight(rtype);
3695 int res = lweight - rweight;
3697 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
3698 ZFS_MAX_DELEG_NAME-1);
3710 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
3712 const deleg_perm_node_t *l = larg;
3713 const deleg_perm_node_t *r = rarg;
3714 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
3715 ZFS_MAX_DELEG_NAME-1);
3727 fs_perm_set_init(fs_perm_set_t *fspset)
3729 bzero(fspset, sizeof (fs_perm_set_t));
3731 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
3732 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
3733 NULL, UU_DEFAULT)) == NULL)
3735 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
3736 UU_DEFAULT)) == NULL)
3739 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
3740 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
3741 who_perm_node_t, who_avl_node), who_perm_compare,
3742 UU_DEFAULT)) == NULL)
3745 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
3746 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
3747 who_perm_node_t, who_avl_node), who_perm_compare,
3748 UU_DEFAULT)) == NULL)
3751 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
3752 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
3753 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
3758 static inline void fs_perm_fini(fs_perm_t *);
3759 static inline void who_perm_fini(who_perm_t *);
3762 fs_perm_set_fini(fs_perm_set_t *fspset)
3764 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
3766 while (node != NULL) {
3767 fs_perm_node_t *next_node =
3768 uu_list_next(fspset->fsps_list, node);
3769 fs_perm_t *fsperm = &node->fspn_fsperm;
3770 fs_perm_fini(fsperm);
3771 uu_list_remove(fspset->fsps_list, node);
3776 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
3777 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
3778 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
3782 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
3785 deleg_perm->dp_who_type = type;
3786 deleg_perm->dp_name = name;
3790 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
3791 zfs_deleg_who_type_t type, const char *name)
3793 uu_avl_pool_t *pool;
3794 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
3796 bzero(who_perm, sizeof (who_perm_t));
3798 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
3799 UU_DEFAULT)) == NULL)
3802 who_perm->who_type = type;
3803 who_perm->who_name = name;
3804 who_perm->who_fsperm = fsperm;
3808 who_perm_fini(who_perm_t *who_perm)
3810 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
3812 while (node != NULL) {
3813 deleg_perm_node_t *next_node =
3814 uu_avl_next(who_perm->who_deleg_perm_avl, node);
3816 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
3821 uu_avl_destroy(who_perm->who_deleg_perm_avl);
3825 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
3827 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
3828 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
3830 bzero(fsperm, sizeof (fs_perm_t));
3832 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
3836 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
3840 fsperm->fsp_set = fspset;
3841 fsperm->fsp_name = fsname;
3845 fs_perm_fini(fs_perm_t *fsperm)
3847 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
3848 while (node != NULL) {
3849 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
3851 who_perm_t *who_perm = &node->who_perm;
3852 who_perm_fini(who_perm);
3853 uu_avl_remove(fsperm->fsp_sc_avl, node);
3858 node = uu_avl_first(fsperm->fsp_uge_avl);
3859 while (node != NULL) {
3860 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
3862 who_perm_t *who_perm = &node->who_perm;
3863 who_perm_fini(who_perm);
3864 uu_avl_remove(fsperm->fsp_uge_avl, node);
3869 uu_avl_destroy(fsperm->fsp_sc_avl);
3870 uu_avl_destroy(fsperm->fsp_uge_avl);
3874 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
3875 zfs_deleg_who_type_t who_type, const char *name, char locality)
3877 uu_avl_index_t idx = 0;
3879 deleg_perm_node_t *found_node = NULL;
3880 deleg_perm_t *deleg_perm = &node->dpn_perm;
3882 deleg_perm_init(deleg_perm, who_type, name);
3884 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
3886 uu_avl_insert(avl, node, idx);
3889 deleg_perm = &node->dpn_perm;
3894 case ZFS_DELEG_LOCAL:
3895 deleg_perm->dp_local = B_TRUE;
3897 case ZFS_DELEG_DESCENDENT:
3898 deleg_perm->dp_descend = B_TRUE;
3903 assert(B_FALSE); /* invalid locality */
3908 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
3910 nvpair_t *nvp = NULL;
3911 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
3912 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
3913 zfs_deleg_who_type_t who_type = who_perm->who_type;
3915 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
3916 const char *name = nvpair_name(nvp);
3917 data_type_t type = nvpair_type(nvp);
3918 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
3919 deleg_perm_node_t *node =
3920 safe_malloc(sizeof (deleg_perm_node_t));
3922 assert(type == DATA_TYPE_BOOLEAN);
3924 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
3925 set_deleg_perm_node(avl, node, who_type, name, locality);
3932 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
3934 nvpair_t *nvp = NULL;
3935 fs_perm_set_t *fspset = fsperm->fsp_set;
3937 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
3938 nvlist_t *nvl2 = NULL;
3939 const char *name = nvpair_name(nvp);
3940 uu_avl_t *avl = NULL;
3941 uu_avl_pool_t *avl_pool;
3942 zfs_deleg_who_type_t perm_type = name[0];
3943 char perm_locality = name[1];
3944 const char *perm_name = name + 3;
3945 boolean_t is_set = B_TRUE;
3946 who_perm_t *who_perm = NULL;
3948 assert('$' == name[2]);
3950 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
3953 switch (perm_type) {
3954 case ZFS_DELEG_CREATE:
3955 case ZFS_DELEG_CREATE_SETS:
3956 case ZFS_DELEG_NAMED_SET:
3957 case ZFS_DELEG_NAMED_SET_SETS:
3958 avl_pool = fspset->fsps_named_set_avl_pool;
3959 avl = fsperm->fsp_sc_avl;
3961 case ZFS_DELEG_USER:
3962 case ZFS_DELEG_USER_SETS:
3963 case ZFS_DELEG_GROUP:
3964 case ZFS_DELEG_GROUP_SETS:
3965 case ZFS_DELEG_EVERYONE:
3966 case ZFS_DELEG_EVERYONE_SETS:
3967 avl_pool = fspset->fsps_who_perm_avl_pool;
3968 avl = fsperm->fsp_uge_avl;
3973 who_perm_node_t *found_node = NULL;
3974 who_perm_node_t *node = safe_malloc(
3975 sizeof (who_perm_node_t));
3976 who_perm = &node->who_perm;
3977 uu_avl_index_t idx = 0;
3979 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
3980 who_perm_init(who_perm, fsperm, perm_type, perm_name);
3982 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
3984 if (avl == fsperm->fsp_uge_avl) {
3986 struct passwd *p = NULL;
3987 struct group *g = NULL;
3988 const char *nice_name = NULL;
3990 switch (perm_type) {
3991 case ZFS_DELEG_USER_SETS:
3992 case ZFS_DELEG_USER:
3993 rid = atoi(perm_name);
3996 nice_name = p->pw_name;
3998 case ZFS_DELEG_GROUP_SETS:
3999 case ZFS_DELEG_GROUP:
4000 rid = atoi(perm_name);
4003 nice_name = g->gr_name;
4007 if (nice_name != NULL)
4009 node->who_perm.who_ug_name,
4013 uu_avl_insert(avl, node, idx);
4016 who_perm = &node->who_perm;
4020 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4027 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4029 nvpair_t *nvp = NULL;
4030 uu_avl_index_t idx = 0;
4032 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4033 nvlist_t *nvl2 = NULL;
4034 const char *fsname = nvpair_name(nvp);
4035 data_type_t type = nvpair_type(nvp);
4036 fs_perm_t *fsperm = NULL;
4037 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4041 fsperm = &node->fspn_fsperm;
4043 assert(DATA_TYPE_NVLIST == type);
4045 uu_list_node_init(node, &node->fspn_list_node,
4046 fspset->fsps_list_pool);
4048 idx = uu_list_numnodes(fspset->fsps_list);
4049 fs_perm_init(fsperm, fspset, fsname);
4051 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4054 (void) parse_fs_perm(fsperm, nvl2);
4056 uu_list_insert(fspset->fsps_list, node, idx);
4062 static inline const char *
4063 deleg_perm_comment(zfs_deleg_note_t note)
4065 const char *str = "";
4070 case ZFS_DELEG_NOTE_ALLOW:
4071 str = gettext("Must also have the permission that is being"
4072 "\n\t\t\t\tallowed");
4074 case ZFS_DELEG_NOTE_CLONE:
4075 str = gettext("Must also have the 'create' ability and 'mount'"
4076 "\n\t\t\t\tability in the origin file system");
4078 case ZFS_DELEG_NOTE_CREATE:
4079 str = gettext("Must also have the 'mount' ability");
4081 case ZFS_DELEG_NOTE_DESTROY:
4082 str = gettext("Must also have the 'mount' ability");
4084 case ZFS_DELEG_NOTE_DIFF:
4085 str = gettext("Allows lookup of paths within a dataset;"
4086 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4087 "\n\t\t\t\tin order to use zfs diff");
4089 case ZFS_DELEG_NOTE_HOLD:
4090 str = gettext("Allows adding a user hold to a snapshot");
4092 case ZFS_DELEG_NOTE_MOUNT:
4093 str = gettext("Allows mount/umount of ZFS datasets");
4095 case ZFS_DELEG_NOTE_PROMOTE:
4096 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4097 " 'promote' ability in the origin file system");
4099 case ZFS_DELEG_NOTE_RECEIVE:
4100 str = gettext("Must also have the 'mount' and 'create'"
4103 case ZFS_DELEG_NOTE_RELEASE:
4104 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4105 "might destroy the snapshot");
4107 case ZFS_DELEG_NOTE_RENAME:
4108 str = gettext("Must also have the 'mount' and 'create'"
4109 "\n\t\t\t\tability in the new parent");
4111 case ZFS_DELEG_NOTE_ROLLBACK:
4114 case ZFS_DELEG_NOTE_SEND:
4117 case ZFS_DELEG_NOTE_SHARE:
4118 str = gettext("Allows sharing file systems over NFS or SMB"
4119 "\n\t\t\t\tprotocols");
4121 case ZFS_DELEG_NOTE_SNAPSHOT:
4125 * case ZFS_DELEG_NOTE_VSCAN:
4126 * str = gettext("");
4130 case ZFS_DELEG_NOTE_GROUPQUOTA:
4131 str = gettext("Allows accessing any groupquota@... property");
4133 case ZFS_DELEG_NOTE_GROUPUSED:
4134 str = gettext("Allows reading any groupused@... property");
4136 case ZFS_DELEG_NOTE_USERPROP:
4137 str = gettext("Allows changing any user property");
4139 case ZFS_DELEG_NOTE_USERQUOTA:
4140 str = gettext("Allows accessing any userquota@... property");
4142 case ZFS_DELEG_NOTE_USERUSED:
4143 str = gettext("Allows reading any userused@... property");
4161 boolean_t recursive; /* unallow only */
4162 boolean_t prt_usage;
4164 boolean_t prt_perms;
4167 const char *dataset;
4171 prop_cmp(const void *a, const void *b)
4173 const char *str1 = *(const char **)a;
4174 const char *str2 = *(const char **)b;
4175 return (strcmp(str1, str2));
4179 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4181 const char *opt_desc[] = {
4182 "-h", gettext("show this help message and exit"),
4183 "-l", gettext("set permission locally"),
4184 "-d", gettext("set permission for descents"),
4185 "-u", gettext("set permission for user"),
4186 "-g", gettext("set permission for group"),
4187 "-e", gettext("set permission for everyone"),
4188 "-c", gettext("set create time permission"),
4189 "-s", gettext("define permission set"),
4191 "-r", gettext("remove permissions recursively"),
4193 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4194 size_t allow_size = unallow_size - 2;
4195 const char *props[ZFS_NUM_PROPS];
4198 FILE *fp = requested ? stdout : stderr;
4199 zprop_desc_t *pdtbl = zfs_prop_get_table();
4200 const char *fmt = gettext("%-16s %-14s\t%s\n");
4202 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4204 (void) fprintf(fp, gettext("Options:\n"));
4205 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4206 const char *opt = opt_desc[i++];
4207 const char *optdsc = opt_desc[i];
4208 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4211 (void) fprintf(fp, gettext("\nThe following permissions are "
4213 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4215 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4216 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4217 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4218 const char *perm_type = deleg_perm_type(perm_note);
4219 const char *perm_comment = deleg_perm_comment(perm_note);
4220 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4223 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4224 zprop_desc_t *pd = &pdtbl[i];
4225 if (pd->pd_visible != B_TRUE)
4228 if (pd->pd_attr == PROP_READONLY)
4231 props[count++] = pd->pd_name;
4233 props[count] = NULL;
4235 qsort(props, count, sizeof (char *), prop_cmp);
4237 for (i = 0; i < count; i++)
4238 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4241 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4243 exit(requested ? 0 : 2);
4246 static inline const char *
4247 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4250 if (un && argc == expected_argc - 1)
4252 else if (argc == expected_argc)
4253 *permsp = argv[argc - 2];
4255 allow_usage(un, B_FALSE,
4256 gettext("wrong number of parameters\n"));
4258 return (argv[argc - 1]);
4262 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4264 int uge_sum = opts->user + opts->group + opts->everyone;
4265 int csuge_sum = opts->create + opts->set + uge_sum;
4266 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4267 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4270 allow_usage(un, B_FALSE,
4271 gettext("-u, -g, and -e are mutually exclusive\n"));
4273 if (opts->prt_usage)
4274 if (argc == 0 && all_sum == 0)
4275 allow_usage(un, B_TRUE, NULL);
4281 allow_usage(un, B_FALSE,
4282 gettext("invalid options combined with -s\n"));
4284 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4285 if (argv[0][0] != '@')
4286 allow_usage(un, B_FALSE,
4287 gettext("invalid set name: missing '@' prefix\n"));
4288 opts->who = argv[0];
4289 } else if (opts->create) {
4290 if (ldcsuge_sum > 1)
4291 allow_usage(un, B_FALSE,
4292 gettext("invalid options combined with -c\n"));
4293 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4294 } else if (opts->everyone) {
4296 allow_usage(un, B_FALSE,
4297 gettext("invalid options combined with -e\n"));
4298 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4299 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4301 opts->everyone = B_TRUE;
4304 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4305 } else if (argc == 1) {
4306 opts->prt_perms = B_TRUE;
4307 opts->dataset = argv[argc-1];
4309 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4310 opts->who = argv[0];
4313 if (!opts->local && !opts->descend) {
4314 opts->local = B_TRUE;
4315 opts->descend = B_TRUE;
4320 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4321 const char *who, char *perms, nvlist_t *top_nvl)
4324 char ld[2] = { '\0', '\0' };
4325 char who_buf[ZFS_MAXNAMELEN+32];
4328 nvlist_t *base_nvl = NULL;
4329 nvlist_t *set_nvl = NULL;
4332 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4334 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4338 case ZFS_DELEG_NAMED_SET_SETS:
4339 case ZFS_DELEG_NAMED_SET:
4340 set_type = ZFS_DELEG_NAMED_SET_SETS;
4341 base_type = ZFS_DELEG_NAMED_SET;
4342 ld[0] = ZFS_DELEG_NA;
4344 case ZFS_DELEG_CREATE_SETS:
4345 case ZFS_DELEG_CREATE:
4346 set_type = ZFS_DELEG_CREATE_SETS;
4347 base_type = ZFS_DELEG_CREATE;
4348 ld[0] = ZFS_DELEG_NA;
4350 case ZFS_DELEG_USER_SETS:
4351 case ZFS_DELEG_USER:
4352 set_type = ZFS_DELEG_USER_SETS;
4353 base_type = ZFS_DELEG_USER;
4355 ld[0] = ZFS_DELEG_LOCAL;
4357 ld[1] = ZFS_DELEG_DESCENDENT;
4359 case ZFS_DELEG_GROUP_SETS:
4360 case ZFS_DELEG_GROUP:
4361 set_type = ZFS_DELEG_GROUP_SETS;
4362 base_type = ZFS_DELEG_GROUP;
4364 ld[0] = ZFS_DELEG_LOCAL;
4366 ld[1] = ZFS_DELEG_DESCENDENT;
4368 case ZFS_DELEG_EVERYONE_SETS:
4369 case ZFS_DELEG_EVERYONE:
4370 set_type = ZFS_DELEG_EVERYONE_SETS;
4371 base_type = ZFS_DELEG_EVERYONE;
4373 ld[0] = ZFS_DELEG_LOCAL;
4375 ld[1] = ZFS_DELEG_DESCENDENT;
4378 if (perms != NULL) {
4380 char *end = curr + strlen(perms);
4382 while (curr < end) {
4383 char *delim = strchr(curr, ',');
4394 (void) nvlist_add_boolean(nvl, curr);
4400 for (i = 0; i < 2; i++) {
4401 char locality = ld[i];
4405 if (!nvlist_empty(base_nvl)) {
4407 (void) snprintf(who_buf,
4408 sizeof (who_buf), "%c%c$%s",
4409 base_type, locality, who);
4411 (void) snprintf(who_buf,
4412 sizeof (who_buf), "%c%c$",
4413 base_type, locality);
4415 (void) nvlist_add_nvlist(top_nvl, who_buf,
4420 if (!nvlist_empty(set_nvl)) {
4422 (void) snprintf(who_buf,
4423 sizeof (who_buf), "%c%c$%s",
4424 set_type, locality, who);
4426 (void) snprintf(who_buf,
4427 sizeof (who_buf), "%c%c$",
4428 set_type, locality);
4430 (void) nvlist_add_nvlist(top_nvl, who_buf,
4435 for (i = 0; i < 2; i++) {
4436 char locality = ld[i];
4441 (void) snprintf(who_buf, sizeof (who_buf),
4442 "%c%c$%s", base_type, locality, who);
4444 (void) snprintf(who_buf, sizeof (who_buf),
4445 "%c%c$", base_type, locality);
4446 (void) nvlist_add_boolean(top_nvl, who_buf);
4449 (void) snprintf(who_buf, sizeof (who_buf),
4450 "%c%c$%s", set_type, locality, who);
4452 (void) snprintf(who_buf, sizeof (who_buf),
4453 "%c%c$", set_type, locality);
4454 (void) nvlist_add_boolean(top_nvl, who_buf);
4460 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4462 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4466 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4467 opts->descend, opts->who, opts->perms, *nvlp);
4468 } else if (opts->create) {
4469 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4470 opts->descend, NULL, opts->perms, *nvlp);
4471 } else if (opts->everyone) {
4472 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4473 opts->descend, NULL, opts->perms, *nvlp);
4475 char *curr = opts->who;
4476 char *end = curr + strlen(curr);
4478 while (curr < end) {
4480 zfs_deleg_who_type_t who_type;
4482 char *delim = strchr(curr, ',');
4485 struct passwd *p = NULL;
4486 struct group *g = NULL;
4494 rid = (uid_t)strtol(curr, &endch, 0);
4496 who_type = ZFS_DELEG_USER;
4505 (void) snprintf(errbuf, 256, gettext(
4506 "invalid user %s"), curr);
4507 allow_usage(un, B_TRUE, errbuf);
4509 } else if (opts->group) {
4510 who_type = ZFS_DELEG_GROUP;
4519 (void) snprintf(errbuf, 256, gettext(
4520 "invalid group %s"), curr);
4521 allow_usage(un, B_TRUE, errbuf);
4524 if (*endch != '\0') {
4531 if (*endch != '\0') {
4538 who_type = ZFS_DELEG_USER;
4540 } else if (g != NULL) {
4541 who_type = ZFS_DELEG_GROUP;
4544 (void) snprintf(errbuf, 256, gettext(
4545 "invalid user/group %s"), curr);
4546 allow_usage(un, B_TRUE, errbuf);
4550 (void) sprintf(id, "%u", rid);
4553 store_allow_perm(who_type, opts->local,
4554 opts->descend, who, opts->perms, *nvlp);
4563 print_set_creat_perms(uu_avl_t *who_avl)
4565 const char *sc_title[] = {
4566 gettext("Permission sets:\n"),
4567 gettext("Create time permissions:\n"),
4570 const char **title_ptr = sc_title;
4571 who_perm_node_t *who_node = NULL;
4572 int prev_weight = -1;
4574 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4575 who_node = uu_avl_next(who_avl, who_node)) {
4576 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4577 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4578 const char *who_name = who_node->who_perm.who_name;
4579 int weight = who_type2weight(who_type);
4580 boolean_t first = B_TRUE;
4581 deleg_perm_node_t *deleg_node;
4583 if (prev_weight != weight) {
4584 (void) printf(*title_ptr++);
4585 prev_weight = weight;
4588 if (who_name == NULL || strnlen(who_name, 1) == 0)
4589 (void) printf("\t");
4591 (void) printf("\t%s ", who_name);
4593 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4594 deleg_node = uu_avl_next(avl, deleg_node)) {
4597 deleg_node->dpn_perm.dp_name);
4600 (void) printf(",%s",
4601 deleg_node->dpn_perm.dp_name);
4604 (void) printf("\n");
4609 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4612 who_perm_node_t *who_node = NULL;
4613 boolean_t prt_title = B_TRUE;
4614 uu_avl_walk_t *walk;
4616 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4619 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4620 const char *who_name = who_node->who_perm.who_name;
4621 const char *nice_who_name = who_node->who_perm.who_ug_name;
4622 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4623 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4625 deleg_perm_node_t *deleg_node;
4626 boolean_t prt_who = B_TRUE;
4628 for (deleg_node = uu_avl_first(avl);
4630 deleg_node = uu_avl_next(avl, deleg_node)) {
4631 if (local != deleg_node->dpn_perm.dp_local ||
4632 descend != deleg_node->dpn_perm.dp_descend)
4636 const char *who = NULL;
4638 prt_title = B_FALSE;
4639 (void) printf(title);
4643 case ZFS_DELEG_USER_SETS:
4644 case ZFS_DELEG_USER:
4645 who = gettext("user");
4647 who_name = nice_who_name;
4649 case ZFS_DELEG_GROUP_SETS:
4650 case ZFS_DELEG_GROUP:
4651 who = gettext("group");
4653 who_name = nice_who_name;
4655 case ZFS_DELEG_EVERYONE_SETS:
4656 case ZFS_DELEG_EVERYONE:
4657 who = gettext("everyone");
4662 if (who_name == NULL)
4663 (void) printf("\t%s", who);
4665 (void) printf("\t%s %s", who, who_name);
4668 (void) printf("%c%s", delim,
4669 deleg_node->dpn_perm.dp_name);
4674 (void) printf("\n");
4677 uu_avl_walk_end(walk);
4681 print_fs_perms(fs_perm_set_t *fspset)
4683 fs_perm_node_t *node = NULL;
4684 char buf[ZFS_MAXNAMELEN+32];
4685 const char *dsname = buf;
4687 for (node = uu_list_first(fspset->fsps_list); node != NULL;
4688 node = uu_list_next(fspset->fsps_list, node)) {
4689 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
4690 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
4693 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
4694 gettext("---- Permissions on %s "),
4695 node->fspn_fsperm.fsp_name);
4696 (void) printf(dsname);
4697 left = 70 - strlen(buf);
4700 (void) printf("\n");
4702 print_set_creat_perms(sc_avl);
4703 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
4704 gettext("Local permissions:\n"));
4705 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
4706 gettext("Descendent permissions:\n"));
4707 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
4708 gettext("Local+Descendent permissions:\n"));
4712 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
4714 struct deleg_perms {
4720 set_deleg_perms(zfs_handle_t *zhp, void *data)
4722 struct deleg_perms *perms = (struct deleg_perms *)data;
4723 zfs_type_t zfs_type = zfs_get_type(zhp);
4725 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
4728 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
4732 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
4735 nvlist_t *perm_nvl = NULL;
4736 nvlist_t *update_perm_nvl = NULL;
4739 struct allow_opts opts = { 0 };
4741 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
4744 while ((c = getopt(argc, argv, optstr)) != -1) {
4747 opts.local = B_TRUE;
4750 opts.descend = B_TRUE;
4756 opts.group = B_TRUE;
4759 opts.everyone = B_TRUE;
4765 opts.create = B_TRUE;
4768 opts.recursive = B_TRUE;
4771 (void) fprintf(stderr, gettext("missing argument for "
4772 "'%c' option\n"), optopt);
4776 opts.prt_usage = B_TRUE;
4779 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4788 /* check arguments */
4789 parse_allow_args(argc, argv, un, &opts);
4791 /* try to open the dataset */
4792 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM))
4794 (void) fprintf(stderr, "Failed to open Dataset *%s*\n",
4799 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
4802 fs_perm_set_init(&fs_perm_set);
4803 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
4804 (void) fprintf(stderr, "Failed to parse fsacl permissionsn");
4809 print_fs_perms(&fs_perm_set);
4811 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
4812 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
4815 if (un && opts.recursive) {
4816 struct deleg_perms data = { un, update_perm_nvl };
4817 if (zfs_iter_filesystems(zhp, set_deleg_perms,
4826 nvlist_free(perm_nvl);
4827 if (update_perm_nvl != NULL)
4828 nvlist_free(update_perm_nvl);
4830 fs_perm_set_fini(&fs_perm_set);
4838 * zfs allow [-r] [-t] <tag> <snap> ...
4840 * -r Recursively hold
4841 * -t Temporary hold (hidden option)
4843 * Apply a user-hold with the given tag to the list of snapshots.
4846 zfs_do_allow(int argc, char **argv)
4848 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
4852 * zfs unallow [-r] [-t] <tag> <snap> ...
4854 * -r Recursively hold
4855 * -t Temporary hold (hidden option)
4857 * Apply a user-hold with the given tag to the list of snapshots.
4860 zfs_do_unallow(int argc, char **argv)
4862 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
4866 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
4871 boolean_t recursive = B_FALSE;
4872 boolean_t temphold = B_FALSE;
4873 const char *opts = holding ? "rt" : "r";
4877 while ((c = getopt(argc, argv, opts)) != -1) {
4886 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4895 /* check number of arguments */
4903 if (holding && tag[0] == '.') {
4904 /* tags starting with '.' are reserved for libzfs */
4905 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
4909 for (i = 0; i < argc; ++i) {
4911 char parent[ZFS_MAXNAMELEN];
4913 char *path = argv[i];
4915 delim = strchr(path, '@');
4916 if (delim == NULL) {
4917 (void) fprintf(stderr,
4918 gettext("'%s' is not a snapshot\n"), path);
4922 (void) strncpy(parent, path, delim - path);
4923 parent[delim - path] = '\0';
4925 zhp = zfs_open(g_zfs, parent,
4926 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4932 if (zfs_hold(zhp, delim+1, tag, recursive,
4933 temphold, B_FALSE, -1, 0, 0) != 0)
4936 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
4942 return (errors != 0);
4946 * zfs hold [-r] [-t] <tag> <snap> ...
4948 * -r Recursively hold
4949 * -t Temporary hold (hidden option)
4951 * Apply a user-hold with the given tag to the list of snapshots.
4954 zfs_do_hold(int argc, char **argv)
4956 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
4960 * zfs release [-r] <tag> <snap> ...
4962 * -r Recursively release
4964 * Release a user-hold with the given tag from the list of snapshots.
4967 zfs_do_release(int argc, char **argv)
4969 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
4972 typedef struct holds_cbdata {
4973 boolean_t cb_recursive;
4974 const char *cb_snapname;
4976 size_t cb_max_namelen;
4977 size_t cb_max_taglen;
4980 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
4981 #define DATETIME_BUF_LEN (32)
4986 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
4989 nvpair_t *nvp = NULL;
4990 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
4994 for (i = 0; i < 3; i++) {
4995 col = gettext(hdr_cols[i]);
4997 (void) printf("%-*s ", i ? tagwidth : nwidth,
5000 (void) printf("%s\n", col);
5004 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5005 char *zname = nvpair_name(nvp);
5007 nvpair_t *nvp2 = NULL;
5008 (void) nvpair_value_nvlist(nvp, &nvl2);
5009 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5010 char tsbuf[DATETIME_BUF_LEN];
5011 char *tagname = nvpair_name(nvp2);
5015 char sep = scripted ? '\t' : ' ';
5016 size_t sepnum = scripted ? 1 : 2;
5018 (void) nvpair_value_uint64(nvp2, &val);
5020 (void) localtime_r(&time, &t);
5021 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5022 gettext(STRFTIME_FMT_STR), &t);
5024 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5025 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5031 * Generic callback function to list a dataset or snapshot.
5034 holds_callback(zfs_handle_t *zhp, void *data)
5036 holds_cbdata_t *cbp = data;
5037 nvlist_t *top_nvl = *cbp->cb_nvlp;
5038 nvlist_t *nvl = NULL;
5039 nvpair_t *nvp = NULL;
5040 const char *zname = zfs_get_name(zhp);
5041 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5043 if (cbp->cb_recursive) {
5044 const char *snapname;
5045 char *delim = strchr(zname, '@');
5049 snapname = delim + 1;
5050 if (strcmp(cbp->cb_snapname, snapname))
5054 if (zfs_get_holds(zhp, &nvl) != 0)
5057 if (znamelen > cbp->cb_max_namelen)
5058 cbp->cb_max_namelen = znamelen;
5060 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5061 const char *tag = nvpair_name(nvp);
5062 size_t taglen = strnlen(tag, MAXNAMELEN);
5063 if (taglen > cbp->cb_max_taglen)
5064 cbp->cb_max_taglen = taglen;
5067 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5071 * zfs holds [-r] <snap> ...
5073 * -r Recursively hold
5076 zfs_do_holds(int argc, char **argv)
5081 boolean_t scripted = B_FALSE;
5082 boolean_t recursive = B_FALSE;
5083 const char *opts = "rH";
5086 int types = ZFS_TYPE_SNAPSHOT;
5087 holds_cbdata_t cb = { 0 };
5094 while ((c = getopt(argc, argv, opts)) != -1) {
5103 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5110 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5111 flags |= ZFS_ITER_RECURSE;
5117 /* check number of arguments */
5121 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5124 for (i = 0; i < argc; ++i) {
5125 char *snapshot = argv[i];
5127 const char *snapname;
5129 delim = strchr(snapshot, '@');
5130 if (delim == NULL) {
5131 (void) fprintf(stderr,
5132 gettext("'%s' is not a snapshot\n"), snapshot);
5136 snapname = delim + 1;
5138 snapshot[delim - snapshot] = '\0';
5140 cb.cb_recursive = recursive;
5141 cb.cb_snapname = snapname;
5145 * 1. collect holds data, set format options
5147 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5148 holds_callback, &cb);
5154 * 2. print holds data
5156 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5158 if (nvlist_empty(nvl))
5159 (void) printf(gettext("no datasets available\n"));
5163 return (0 != errors);
5166 #define CHECK_SPINNER 30
5167 #define SPINNER_TIME 3 /* seconds */
5168 #define MOUNT_TIME 5 /* seconds */
5171 get_one_dataset(zfs_handle_t *zhp, void *data)
5173 static char *spin[] = { "-", "\\", "|", "/" };
5174 static int spinval = 0;
5175 static int spincheck = 0;
5176 static time_t last_spin_time = (time_t)0;
5177 get_all_cb_t *cbp = data;
5178 zfs_type_t type = zfs_get_type(zhp);
5180 if (cbp->cb_verbose) {
5181 if (--spincheck < 0) {
5182 time_t now = time(NULL);
5183 if (last_spin_time + SPINNER_TIME < now) {
5184 update_progress(spin[spinval++ % 4]);
5185 last_spin_time = now;
5187 spincheck = CHECK_SPINNER;
5192 * Interate over any nested datasets.
5194 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5200 * Skip any datasets whose type does not match.
5202 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5206 libzfs_add_handle(cbp, zhp);
5207 assert(cbp->cb_used <= cbp->cb_alloc);
5213 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5215 get_all_cb_t cb = { 0 };
5216 cb.cb_verbose = verbose;
5217 cb.cb_getone = get_one_dataset;
5220 set_progress_header(gettext("Reading ZFS config"));
5221 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5223 *dslist = cb.cb_handles;
5224 *count = cb.cb_used;
5227 finish_progress(gettext("done."));
5231 * Generic callback for sharing or mounting filesystems. Because the code is so
5232 * similar, we have a common function with an extra parameter to determine which
5233 * mode we are using.
5235 #define OP_SHARE 0x1
5236 #define OP_MOUNT 0x2
5239 * Share or mount a dataset.
5242 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5243 boolean_t explicit, const char *options)
5245 char mountpoint[ZFS_MAXPROPLEN];
5246 char shareopts[ZFS_MAXPROPLEN];
5247 char smbshareopts[ZFS_MAXPROPLEN];
5248 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5250 uint64_t zoned, canmount;
5251 boolean_t shared_nfs, shared_smb;
5253 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5256 * Check to make sure we can mount/share this dataset. If we
5257 * are in the global zone and the filesystem is exported to a
5258 * local zone, or if we are in a local zone and the
5259 * filesystem is not exported, then it is an error.
5261 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5263 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5267 (void) fprintf(stderr, gettext("cannot %s '%s': "
5268 "dataset is exported to a local zone\n"), cmdname,
5272 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5276 (void) fprintf(stderr, gettext("cannot %s '%s': "
5277 "permission denied\n"), cmdname,
5283 * Ignore any filesystems which don't apply to us. This
5284 * includes those with a legacy mountpoint, or those with
5285 * legacy share options.
5287 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5288 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5289 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5290 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5291 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5292 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5294 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5295 strcmp(smbshareopts, "off") == 0) {
5299 (void) fprintf(stderr, gettext("cannot share '%s': "
5300 "legacy share\n"), zfs_get_name(zhp));
5301 (void) fprintf(stderr, gettext("use share(1M) to "
5302 "share this filesystem, or set "
5303 "sharenfs property on\n"));
5308 * We cannot share or mount legacy filesystems. If the
5309 * shareopts is non-legacy but the mountpoint is legacy, we
5310 * treat it as a legacy share.
5312 if (strcmp(mountpoint, "legacy") == 0) {
5316 (void) fprintf(stderr, gettext("cannot %s '%s': "
5317 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5318 (void) fprintf(stderr, gettext("use %s(1M) to "
5319 "%s this filesystem\n"), cmdname, cmdname);
5323 if (strcmp(mountpoint, "none") == 0) {
5327 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5328 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5333 * canmount explicit outcome
5334 * on no pass through
5335 * on yes pass through
5337 * off yes display error, return 1
5338 * noauto no return 0
5339 * noauto yes pass through
5341 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5342 if (canmount == ZFS_CANMOUNT_OFF) {
5346 (void) fprintf(stderr, gettext("cannot %s '%s': "
5347 "'canmount' property is set to 'off'\n"), cmdname,
5350 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5355 * At this point, we have verified that the mountpoint and/or
5356 * shareopts are appropriate for auto management. If the
5357 * filesystem is already mounted or shared, return (failing
5358 * for explicit requests); otherwise mount or share the
5364 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5365 shared_smb = zfs_is_shared_smb(zhp, NULL);
5367 if (shared_nfs && shared_smb ||
5368 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5369 strcmp(smbshareopts, "off") == 0) ||
5370 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5371 strcmp(shareopts, "off") == 0)) {
5375 (void) fprintf(stderr, gettext("cannot share "
5376 "'%s': filesystem already shared\n"),
5381 if (!zfs_is_mounted(zhp, NULL) &&
5382 zfs_mount(zhp, NULL, 0) != 0)
5385 if (protocol == NULL) {
5386 if (zfs_shareall(zhp) != 0)
5388 } else if (strcmp(protocol, "nfs") == 0) {
5389 if (zfs_share_nfs(zhp))
5391 } else if (strcmp(protocol, "smb") == 0) {
5392 if (zfs_share_smb(zhp))
5395 (void) fprintf(stderr, gettext("cannot share "
5396 "'%s': invalid share type '%s' "
5398 zfs_get_name(zhp), protocol);
5405 if (options == NULL)
5406 mnt.mnt_mntopts = "";
5408 mnt.mnt_mntopts = (char *)options;
5410 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5411 zfs_is_mounted(zhp, NULL)) {
5415 (void) fprintf(stderr, gettext("cannot mount "
5416 "'%s': filesystem already mounted\n"),
5421 if (zfs_mount(zhp, options, flags) != 0)
5430 * Reports progress in the form "(current/total)". Not thread-safe.
5433 report_mount_progress(int current, int total)
5435 static time_t last_progress_time = 0;
5436 time_t now = time(NULL);
5439 /* report 1..n instead of 0..n-1 */
5442 /* display header if we're here for the first time */
5444 set_progress_header(gettext("Mounting ZFS filesystems"));
5445 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5446 /* too soon to report again */
5450 last_progress_time = now;
5452 (void) sprintf(info, "(%d/%d)", current, total);
5454 if (current == total)
5455 finish_progress(info);
5457 update_progress(info);
5461 append_options(char *mntopts, char *newopts)
5463 int len = strlen(mntopts);
5465 /* original length plus new string to append plus 1 for the comma */
5466 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5467 (void) fprintf(stderr, gettext("the opts argument for "
5468 "'%c' option is too long (more than %d chars)\n"),
5469 "-o", MNT_LINE_MAX);
5474 mntopts[len++] = ',';
5476 (void) strcpy(&mntopts[len], newopts);
5480 share_mount(int op, int argc, char **argv)
5483 boolean_t verbose = B_FALSE;
5485 char *options = NULL;
5489 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5499 if (*optarg == '\0') {
5500 (void) fprintf(stderr, gettext("empty mount "
5501 "options (-o) specified\n"));
5505 if (options == NULL)
5506 options = safe_malloc(MNT_LINE_MAX + 1);
5508 /* option validation is done later */
5509 append_options(options, optarg);
5513 warnx("no overlay mounts support on FreeBSD, ignoring");
5516 (void) fprintf(stderr, gettext("missing argument for "
5517 "'%c' option\n"), optopt);
5521 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5530 /* check number of arguments */
5532 zfs_handle_t **dslist = NULL;
5533 size_t i, count = 0;
5534 char *protocol = NULL;
5536 if (op == OP_SHARE && argc > 0) {
5537 if (strcmp(argv[0], "nfs") != 0 &&
5538 strcmp(argv[0], "smb") != 0) {
5539 (void) fprintf(stderr, gettext("share type "
5540 "must be 'nfs' or 'smb'\n"));
5549 (void) fprintf(stderr, gettext("too many arguments\n"));
5553 start_progress_timer();
5554 get_all_datasets(&dslist, &count, verbose);
5559 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5561 for (i = 0; i < count; i++) {
5563 report_mount_progress(i, count);
5565 if (share_mount_one(dslist[i], op, flags, protocol,
5566 B_FALSE, options) != 0)
5568 zfs_close(dslist[i]);
5572 } else if (argc == 0) {
5573 struct mnttab entry;
5575 if ((op == OP_SHARE) || (options != NULL)) {
5576 (void) fprintf(stderr, gettext("missing filesystem "
5577 "argument (specify -a for all)\n"));
5582 * When mount is given no arguments, go through /etc/mnttab and
5583 * display any active ZFS mounts. We hide any snapshots, since
5584 * they are controlled automatically.
5586 rewind(mnttab_file);
5587 while (getmntent(mnttab_file, &entry) == 0) {
5588 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5589 strchr(entry.mnt_special, '@') != NULL)
5592 (void) printf("%-30s %s\n", entry.mnt_special,
5600 (void) fprintf(stderr,
5601 gettext("too many arguments\n"));
5605 if ((zhp = zfs_open(g_zfs, argv[0],
5606 ZFS_TYPE_FILESYSTEM)) == NULL) {
5609 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5619 * zfs mount -a [nfs]
5620 * zfs mount filesystem
5622 * Mount all filesystems, or mount the given filesystem.
5625 zfs_do_mount(int argc, char **argv)
5627 return (share_mount(OP_MOUNT, argc, argv));
5631 * zfs share -a [nfs | smb]
5632 * zfs share filesystem
5634 * Share all filesystems, or share the given filesystem.
5637 zfs_do_share(int argc, char **argv)
5639 return (share_mount(OP_SHARE, argc, argv));
5642 typedef struct unshare_unmount_node {
5643 zfs_handle_t *un_zhp;
5645 uu_avl_node_t un_avlnode;
5646 } unshare_unmount_node_t;
5650 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
5652 const unshare_unmount_node_t *l = larg;
5653 const unshare_unmount_node_t *r = rarg;
5655 return (strcmp(l->un_mountp, r->un_mountp));
5659 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5660 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
5661 * and unmount it appropriately.
5664 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
5668 struct stat64 statbuf;
5669 struct extmnttab entry;
5670 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5674 * Search for the path in /etc/mnttab. Rather than looking for the
5675 * specific path, which can be fooled by non-standard paths (i.e. ".."
5676 * or "//"), we stat() the path and search for the corresponding
5677 * (major,minor) device pair.
5679 if (stat64(path, &statbuf) != 0) {
5680 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5681 cmdname, path, strerror(errno));
5684 path_inode = statbuf.st_ino;
5687 * Search for the given (major,minor) pair in the mount table.
5690 rewind(mnttab_file);
5691 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
5692 if (entry.mnt_major == major(statbuf.st_dev) &&
5693 entry.mnt_minor == minor(statbuf.st_dev))
5700 if (statfs(path, &sfs) != 0) {
5701 (void) fprintf(stderr, "%s: %s\n", path,
5705 statfs2mnttab(&sfs, &entry);
5709 if (op == OP_SHARE) {
5710 (void) fprintf(stderr, gettext("cannot %s '%s': not "
5711 "currently mounted\n"), cmdname, path);
5714 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
5716 if ((ret = umount2(path, flags)) != 0)
5717 (void) fprintf(stderr, gettext("%s: %s\n"), path,
5722 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
5723 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
5724 "filesystem\n"), cmdname, path);
5728 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
5729 ZFS_TYPE_FILESYSTEM)) == NULL)
5733 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
5734 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5735 cmdname, path, strerror(errno));
5737 } else if (statbuf.st_ino != path_inode) {
5738 (void) fprintf(stderr, gettext("cannot "
5739 "%s '%s': not a mountpoint\n"), cmdname, path);
5743 if (op == OP_SHARE) {
5744 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5745 char smbshare_prop[ZFS_MAXPROPLEN];
5747 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
5748 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
5749 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
5750 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
5752 if (strcmp(nfs_mnt_prop, "off") == 0 &&
5753 strcmp(smbshare_prop, "off") == 0) {
5754 (void) fprintf(stderr, gettext("cannot unshare "
5755 "'%s': legacy share\n"), path);
5756 (void) fprintf(stderr, gettext("use "
5757 "unshare(1M) to unshare this filesystem\n"));
5758 } else if (!zfs_is_shared(zhp)) {
5759 (void) fprintf(stderr, gettext("cannot unshare '%s': "
5760 "not currently shared\n"), path);
5762 ret = zfs_unshareall_bypath(zhp, path);
5765 char mtpt_prop[ZFS_MAXPROPLEN];
5767 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
5768 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
5771 ret = zfs_unmount(zhp, NULL, flags);
5772 } else if (strcmp(mtpt_prop, "legacy") == 0) {
5773 (void) fprintf(stderr, gettext("cannot unmount "
5774 "'%s': legacy mountpoint\n"),
5776 (void) fprintf(stderr, gettext("use umount(1M) "
5777 "to unmount this filesystem\n"));
5779 ret = zfs_unmountall(zhp, flags);
5790 * Generic callback for unsharing or unmounting a filesystem.
5793 unshare_unmount(int op, int argc, char **argv)
5800 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5801 char sharesmb[ZFS_MAXPROPLEN];
5804 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
5813 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5824 * We could make use of zfs_for_each() to walk all datasets in
5825 * the system, but this would be very inefficient, especially
5826 * since we would have to linearly search /etc/mnttab for each
5827 * one. Instead, do one pass through /etc/mnttab looking for
5828 * zfs entries and call zfs_unmount() for each one.
5830 * Things get a little tricky if the administrator has created
5831 * mountpoints beneath other ZFS filesystems. In this case, we
5832 * have to unmount the deepest filesystems first. To accomplish
5833 * this, we place all the mountpoints in an AVL tree sorted by
5834 * the special type (dataset name), and walk the result in
5835 * reverse to make sure to get any snapshots first.
5837 struct mnttab entry;
5838 uu_avl_pool_t *pool;
5840 unshare_unmount_node_t *node;
5842 uu_avl_walk_t *walk;
5845 (void) fprintf(stderr, gettext("too many arguments\n"));
5849 if (((pool = uu_avl_pool_create("unmount_pool",
5850 sizeof (unshare_unmount_node_t),
5851 offsetof(unshare_unmount_node_t, un_avlnode),
5852 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
5853 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
5856 rewind(mnttab_file);
5857 while (getmntent(mnttab_file, &entry) == 0) {
5859 /* ignore non-ZFS entries */
5860 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
5863 /* ignore snapshots */
5864 if (strchr(entry.mnt_special, '@') != NULL)
5867 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
5868 ZFS_TYPE_FILESYSTEM)) == NULL) {
5875 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
5877 sizeof (nfs_mnt_prop),
5878 NULL, NULL, 0, B_FALSE) == 0);
5879 if (strcmp(nfs_mnt_prop, "off") != 0)
5881 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
5883 sizeof (nfs_mnt_prop),
5884 NULL, NULL, 0, B_FALSE) == 0);
5885 if (strcmp(nfs_mnt_prop, "off") == 0)
5889 /* Ignore legacy mounts */
5890 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
5892 sizeof (nfs_mnt_prop),
5893 NULL, NULL, 0, B_FALSE) == 0);
5894 if (strcmp(nfs_mnt_prop, "legacy") == 0)
5896 /* Ignore canmount=noauto mounts */
5897 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
5898 ZFS_CANMOUNT_NOAUTO)
5904 node = safe_malloc(sizeof (unshare_unmount_node_t));
5906 node->un_mountp = safe_strdup(entry.mnt_mountp);
5908 uu_avl_node_init(node, &node->un_avlnode, pool);
5910 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
5911 uu_avl_insert(tree, node, idx);
5913 zfs_close(node->un_zhp);
5914 free(node->un_mountp);
5920 * Walk the AVL tree in reverse, unmounting each filesystem and
5921 * removing it from the AVL tree in the process.
5923 if ((walk = uu_avl_walk_start(tree,
5924 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
5927 while ((node = uu_avl_walk_next(walk)) != NULL) {
5928 uu_avl_remove(tree, node);
5932 if (zfs_unshareall_bypath(node->un_zhp,
5933 node->un_mountp) != 0)
5938 if (zfs_unmount(node->un_zhp,
5939 node->un_mountp, flags) != 0)
5944 zfs_close(node->un_zhp);
5945 free(node->un_mountp);
5949 uu_avl_walk_end(walk);
5950 uu_avl_destroy(tree);
5951 uu_avl_pool_destroy(pool);
5956 (void) fprintf(stderr,
5957 gettext("missing filesystem argument\n"));
5959 (void) fprintf(stderr,
5960 gettext("too many arguments\n"));
5965 * We have an argument, but it may be a full path or a ZFS
5966 * filesystem. Pass full paths off to unmount_path() (shared by
5967 * manual_unmount), otherwise open the filesystem and pass to
5970 if (argv[0][0] == '/')
5971 return (unshare_unmount_path(op, argv[0],
5974 if ((zhp = zfs_open(g_zfs, argv[0],
5975 ZFS_TYPE_FILESYSTEM)) == NULL)
5978 verify(zfs_prop_get(zhp, op == OP_SHARE ?
5979 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
5980 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
5981 NULL, 0, B_FALSE) == 0);
5985 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
5987 sizeof (nfs_mnt_prop),
5988 NULL, NULL, 0, B_FALSE) == 0);
5989 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
5990 sharesmb, sizeof (sharesmb), NULL, NULL,
5993 if (strcmp(nfs_mnt_prop, "off") == 0 &&
5994 strcmp(sharesmb, "off") == 0) {
5995 (void) fprintf(stderr, gettext("cannot "
5996 "unshare '%s': legacy share\n"),
5998 (void) fprintf(stderr, gettext("use "
5999 "unshare(1M) to unshare this "
6002 } else if (!zfs_is_shared(zhp)) {
6003 (void) fprintf(stderr, gettext("cannot "
6004 "unshare '%s': not currently "
6005 "shared\n"), zfs_get_name(zhp));
6007 } else if (zfs_unshareall(zhp) != 0) {
6013 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6014 (void) fprintf(stderr, gettext("cannot "
6015 "unmount '%s': legacy "
6016 "mountpoint\n"), zfs_get_name(zhp));
6017 (void) fprintf(stderr, gettext("use "
6018 "umount(1M) to unmount this "
6021 } else if (!zfs_is_mounted(zhp, NULL)) {
6022 (void) fprintf(stderr, gettext("cannot "
6023 "unmount '%s': not currently "
6027 } else if (zfs_unmountall(zhp, flags) != 0) {
6041 * zfs unmount filesystem
6043 * Unmount all filesystems, or a specific ZFS filesystem.
6046 zfs_do_unmount(int argc, char **argv)
6048 return (unshare_unmount(OP_MOUNT, argc, argv));
6053 * zfs unshare filesystem
6055 * Unshare all filesystems, or a specific ZFS filesystem.
6058 zfs_do_unshare(int argc, char **argv)
6060 return (unshare_unmount(OP_SHARE, argc, argv));
6064 * Attach/detach the given dataset to/from the given jail
6068 do_jail(int argc, char **argv, int attach)
6073 /* check number of arguments */
6075 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6079 (void) fprintf(stderr, gettext("too many arguments\n"));
6083 jailid = atoi(argv[1]);
6085 (void) fprintf(stderr, gettext("invalid jailid\n"));
6089 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6093 ret = (zfs_jail(zhp, jailid, attach) != 0);
6100 * zfs jail jailid filesystem
6102 * Attach the given dataset to the given jail
6106 zfs_do_jail(int argc, char **argv)
6109 return (do_jail(argc, argv, 1));
6113 * zfs unjail jailid filesystem
6115 * Detach the given dataset from the given jail
6119 zfs_do_unjail(int argc, char **argv)
6122 return (do_jail(argc, argv, 0));
6126 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6127 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6130 manual_mount(int argc, char **argv)
6133 char mountpoint[ZFS_MAXPROPLEN];
6134 char mntopts[MNT_LINE_MAX] = { '\0' };
6138 char *dataset, *path;
6141 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6144 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6147 flags |= MS_OVERLAY;
6150 flags |= MS_NOMNTTAB;
6153 (void) fprintf(stderr, gettext("missing argument for "
6154 "'%c' option\n"), optopt);
6158 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6160 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6169 /* check that we only have two arguments */
6172 (void) fprintf(stderr, gettext("missing dataset "
6175 (void) fprintf(stderr,
6176 gettext("missing mountpoint argument\n"));
6178 (void) fprintf(stderr, gettext("too many arguments\n"));
6179 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6186 /* try to open the dataset */
6187 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6190 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6191 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6193 /* check for legacy mountpoint and complain appropriately */
6195 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6196 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6197 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6198 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6203 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6204 "mounted using 'mount -F zfs'\n"), dataset);
6205 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6206 "instead.\n"), path);
6207 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
6208 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6209 (void) fprintf(stderr, gettext("See zfs(1M) for more "
6218 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6219 * unmounts of non-legacy filesystems, as this is the dominant administrative
6223 manual_unmount(int argc, char **argv)
6229 while ((c = getopt(argc, argv, "f")) != -1) {
6235 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6237 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6246 /* check arguments */
6249 (void) fprintf(stderr, gettext("missing path "
6252 (void) fprintf(stderr, gettext("too many arguments\n"));
6253 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6257 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6261 find_command_idx(char *command, int *idx)
6265 for (i = 0; i < NCOMMAND; i++) {
6266 if (command_table[i].name == NULL)
6269 if (strcmp(command, command_table[i].name) == 0) {
6278 zfs_do_diff(int argc, char **argv)
6282 char *tosnap = NULL;
6283 char *fromsnap = NULL;
6288 while ((c = getopt(argc, argv, "FHt")) != -1) {
6291 flags |= ZFS_DIFF_CLASSIFY;
6294 flags |= ZFS_DIFF_PARSEABLE;
6297 flags |= ZFS_DIFF_TIMESTAMP;
6300 (void) fprintf(stderr,
6301 gettext("invalid option '%c'\n"), optopt);
6310 (void) fprintf(stderr,
6311 gettext("must provide at least one snapshot name\n"));
6316 (void) fprintf(stderr, gettext("too many arguments\n"));
6321 tosnap = (argc == 2) ? argv[1] : NULL;
6324 if (*fromsnap != '@')
6325 copy = strdup(fromsnap);
6327 copy = strdup(tosnap);
6331 if (atp = strchr(copy, '@'))
6334 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6340 * Ignore SIGPIPE so that the library can give us
6341 * information on any failure
6343 (void) sigignore(SIGPIPE);
6345 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6353 main(int argc, char **argv)
6360 (void) setlocale(LC_ALL, "");
6361 (void) textdomain(TEXT_DOMAIN);
6365 if ((g_zfs = libzfs_init()) == NULL) {
6366 (void) fprintf(stderr, gettext("internal error: failed to "
6367 "initialize ZFS library\n"));
6371 zpool_set_history_str("zfs", argc, argv, history_str);
6372 verify(zpool_stage_history(g_zfs, history_str) == 0);
6374 libzfs_print_on_error(g_zfs, B_TRUE);
6376 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6377 (void) fprintf(stderr, gettext("internal error: unable to "
6378 "open %s\n"), MNTTAB);
6383 * This command also doubles as the /etc/fs mount and unmount program.
6384 * Determine if we should take this behavior based on argv[0].
6386 progname = basename(argv[0]);
6387 if (strcmp(progname, "mount") == 0) {
6388 ret = manual_mount(argc, argv);
6389 } else if (strcmp(progname, "umount") == 0) {
6390 ret = manual_unmount(argc, argv);
6393 * Make sure the user has specified some command.
6396 (void) fprintf(stderr, gettext("missing command\n"));
6403 * The 'umount' command is an alias for 'unmount'
6405 if (strcmp(cmdname, "umount") == 0)
6406 cmdname = "unmount";
6409 * The 'recv' command is an alias for 'receive'
6411 if (strcmp(cmdname, "recv") == 0)
6412 cmdname = "receive";
6417 if (strcmp(cmdname, "-?") == 0)
6421 * Run the appropriate command.
6423 libzfs_mnttab_cache(g_zfs, B_TRUE);
6424 if (find_command_idx(cmdname, &i) == 0) {
6425 current_command = &command_table[i];
6426 ret = command_table[i].func(argc - 1, argv + 1);
6427 } else if (strchr(cmdname, '=') != NULL) {
6428 verify(find_command_idx("set", &i) == 0);
6429 current_command = &command_table[i];
6430 ret = command_table[i].func(argc, argv);
6432 (void) fprintf(stderr, gettext("unrecognized "
6433 "command '%s'\n"), cmdname);
6436 libzfs_mnttab_cache(g_zfs, B_FALSE);
6439 (void) fclose(mnttab_file);
6444 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6445 * for the purposes of running ::findleaks.
6447 if (getenv("ZFS_ABORT") != NULL) {
6448 (void) printf("dumping core by request\n");