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.
33 #include <libnvpair.h>
46 #include <sys/mntent.h>
47 #include <sys/mnttab.h>
48 #include <sys/mount.h>
50 #include <sys/fs/zfs.h>
51 #include <sys/types.h>
56 #include <zfs_deleg.h>
60 #include <directory.h>
65 #include "zfs_comutil.h"
67 libzfs_handle_t *g_zfs;
69 static FILE *mnttab_file;
70 static char history_str[HIS_MAX_RECORD_LEN];
72 static int zfs_do_clone(int argc, char **argv);
73 static int zfs_do_create(int argc, char **argv);
74 static int zfs_do_destroy(int argc, char **argv);
75 static int zfs_do_get(int argc, char **argv);
76 static int zfs_do_inherit(int argc, char **argv);
77 static int zfs_do_list(int argc, char **argv);
78 static int zfs_do_mount(int argc, char **argv);
79 static int zfs_do_rename(int argc, char **argv);
80 static int zfs_do_rollback(int argc, char **argv);
81 static int zfs_do_set(int argc, char **argv);
82 static int zfs_do_upgrade(int argc, char **argv);
83 static int zfs_do_snapshot(int argc, char **argv);
84 static int zfs_do_unmount(int argc, char **argv);
85 static int zfs_do_share(int argc, char **argv);
86 static int zfs_do_unshare(int argc, char **argv);
87 static int zfs_do_send(int argc, char **argv);
88 static int zfs_do_receive(int argc, char **argv);
89 static int zfs_do_promote(int argc, char **argv);
90 static int zfs_do_userspace(int argc, char **argv);
91 static int zfs_do_allow(int argc, char **argv);
92 static int zfs_do_unallow(int argc, char **argv);
93 static int zfs_do_hold(int argc, char **argv);
94 static int zfs_do_holds(int argc, char **argv);
95 static int zfs_do_release(int argc, char **argv);
96 static int zfs_do_diff(int argc, char **argv);
97 static int zfs_do_jail(int argc, char **argv);
98 static int zfs_do_unjail(int argc, char **argv);
101 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
106 _umem_debug_init(void)
108 return ("default,verbose"); /* $UMEM_DEBUG setting */
112 _umem_logging_init(void)
114 return ("fail,contents"); /* $UMEM_LOGGING setting */
149 typedef struct zfs_command {
151 int (*func)(int argc, char **argv);
156 * Master command table. Each ZFS command has a name, associated function, and
157 * usage message. The usage messages need to be internationalized, so we have
158 * to have a function to return the usage message based on a command index.
160 * These commands are organized according to how they are displayed in the usage
161 * message. An empty command (one with a NULL name) indicates an empty line in
162 * the generic usage message.
164 static zfs_command_t command_table[] = {
165 { "create", zfs_do_create, HELP_CREATE },
166 { "destroy", zfs_do_destroy, HELP_DESTROY },
168 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
169 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
170 { "clone", zfs_do_clone, HELP_CLONE },
171 { "promote", zfs_do_promote, HELP_PROMOTE },
172 { "rename", zfs_do_rename, HELP_RENAME },
174 { "list", zfs_do_list, HELP_LIST },
176 { "set", zfs_do_set, HELP_SET },
177 { "get", zfs_do_get, HELP_GET },
178 { "inherit", zfs_do_inherit, HELP_INHERIT },
179 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
180 { "userspace", zfs_do_userspace, HELP_USERSPACE },
181 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
183 { "mount", zfs_do_mount, HELP_MOUNT },
184 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
185 { "share", zfs_do_share, HELP_SHARE },
186 { "unshare", zfs_do_unshare, HELP_UNSHARE },
188 { "send", zfs_do_send, HELP_SEND },
189 { "receive", zfs_do_receive, HELP_RECEIVE },
191 { "allow", zfs_do_allow, HELP_ALLOW },
193 { "unallow", zfs_do_unallow, HELP_UNALLOW },
195 { "hold", zfs_do_hold, HELP_HOLD },
196 { "holds", zfs_do_holds, HELP_HOLDS },
197 { "release", zfs_do_release, HELP_RELEASE },
198 { "diff", zfs_do_diff, HELP_DIFF },
200 { "jail", zfs_do_jail, HELP_JAIL },
201 { "unjail", zfs_do_unjail, HELP_UNJAIL },
204 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
206 zfs_command_t *current_command;
209 get_usage(zfs_help_t idx)
213 return (gettext("\tclone [-p] [-o property=value] ... "
214 "<snapshot> <filesystem|volume>\n"));
216 return (gettext("\tcreate [-p] [-o property=value] ... "
218 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
219 "-V <size> <volume>\n"));
221 return (gettext("\tdestroy [-rRf] <filesystem|volume>\n"
222 "\tdestroy [-rRd] <snapshot>\n"));
224 return (gettext("\tget [-rHp] [-d max] "
225 "[-o \"all\" | field[,...]] [-s source[,...]]\n"
226 "\t <\"all\" | property[,...]> "
227 "[filesystem|volume|snapshot] ...\n"));
229 return (gettext("\tinherit [-rS] <property> "
230 "<filesystem|volume|snapshot> ...\n"));
232 return (gettext("\tupgrade [-v]\n"
233 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
235 return (gettext("\tjail <jailid> <filesystem>\n"));
237 return (gettext("\tunjail <jailid> <filesystem>\n"));
239 return (gettext("\tlist [-rH][-d max] "
240 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
241 "\t [-S property] ... "
242 "[filesystem|volume|snapshot] ...\n"));
244 return (gettext("\tmount\n"
245 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
247 return (gettext("\tpromote <clone-filesystem>\n"));
249 return (gettext("\treceive [-vnFu] <filesystem|volume|"
251 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
253 return (gettext("\trename <filesystem|volume|snapshot> "
254 "<filesystem|volume|snapshot>\n"
255 "\trename -p <filesystem|volume> <filesystem|volume>\n"
256 "\trename -r <snapshot> <snapshot>"));
258 return (gettext("\trollback [-rRf] <snapshot>\n"));
260 return (gettext("\tsend [-DvRp] "
261 "[-i snapshot | -I snapshot] <snapshot>\n"));
263 return (gettext("\tset <property=value> "
264 "<filesystem|volume|snapshot> ...\n"));
266 return (gettext("\tshare <-a | filesystem>\n"));
268 return (gettext("\tsnapshot [-r] [-o property=value] ... "
269 "<filesystem@snapname|volume@snapname>\n"));
271 return (gettext("\tunmount [-f] "
272 "<-a | filesystem|mountpoint>\n"));
274 return (gettext("\tunshare "
275 "<-a | filesystem|mountpoint>\n"));
277 return (gettext("\tallow <filesystem|volume>\n"
279 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
280 "\t <filesystem|volume>\n"
281 "\tallow [-ld] -e <perm|@setname>[,...] "
282 "<filesystem|volume>\n"
283 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
284 "\tallow -s @setname <perm|@setname>[,...] "
285 "<filesystem|volume>\n"));
287 return (gettext("\tunallow [-rldug] "
288 "<\"everyone\"|user|group>[,...]\n"
289 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
290 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
291 "<filesystem|volume>\n"
292 "\tunallow [-r] -c [<perm|@setname>[,...]] "
293 "<filesystem|volume>\n"
294 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
295 "<filesystem|volume>\n"));
297 return (gettext("\tuserspace [-niHp] [-o field[,...]] "
298 "[-sS field] ... [-t type[,...]]\n"
299 "\t <filesystem|snapshot>\n"));
300 case HELP_GROUPSPACE:
301 return (gettext("\tgroupspace [-niHp] [-o field[,...]] "
302 "[-sS field] ... [-t type[,...]]\n"
303 "\t <filesystem|snapshot>\n"));
305 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
307 return (gettext("\tholds [-r] <snapshot> ...\n"));
309 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
311 return (gettext("\tdiff [-FHt] <snapshot> "
312 "[snapshot|filesystem]\n"));
322 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
327 * Utility function to guarantee malloc() success.
331 safe_malloc(size_t size)
335 if ((data = calloc(1, size)) == NULL)
342 safe_strdup(char *str)
344 char *dupstr = strdup(str);
353 * Callback routine that will print out information for each of
357 usage_prop_cb(int prop, void *cb)
361 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
363 if (zfs_prop_readonly(prop))
364 (void) fprintf(fp, " NO ");
366 (void) fprintf(fp, "YES ");
368 if (zfs_prop_inheritable(prop))
369 (void) fprintf(fp, " YES ");
371 (void) fprintf(fp, " NO ");
373 if (zfs_prop_values(prop) == NULL)
374 (void) fprintf(fp, "-\n");
376 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
382 * Display usage message. If we're inside a command, display only the usage for
383 * that command. Otherwise, iterate over the entire command table and display
384 * a complete usage message.
387 usage(boolean_t requested)
390 boolean_t show_properties = B_FALSE;
391 FILE *fp = requested ? stdout : stderr;
393 if (current_command == NULL) {
395 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
397 gettext("where 'command' is one of the following:\n\n"));
399 for (i = 0; i < NCOMMAND; i++) {
400 if (command_table[i].name == NULL)
401 (void) fprintf(fp, "\n");
403 (void) fprintf(fp, "%s",
404 get_usage(command_table[i].usage));
407 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
408 "pool/[dataset/]*dataset[@name]\n"));
410 (void) fprintf(fp, gettext("usage:\n"));
411 (void) fprintf(fp, "%s", get_usage(current_command->usage));
414 if (current_command != NULL &&
415 (strcmp(current_command->name, "set") == 0 ||
416 strcmp(current_command->name, "get") == 0 ||
417 strcmp(current_command->name, "inherit") == 0 ||
418 strcmp(current_command->name, "list") == 0))
419 show_properties = B_TRUE;
421 if (show_properties) {
423 gettext("\nThe following properties are supported:\n"));
425 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
426 "PROPERTY", "EDIT", "INHERIT", "VALUES");
428 /* Iterate over all properties */
429 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
432 (void) fprintf(fp, "\t%-15s ", "userused@...");
433 (void) fprintf(fp, " NO NO <size>\n");
434 (void) fprintf(fp, "\t%-15s ", "groupused@...");
435 (void) fprintf(fp, " NO NO <size>\n");
436 (void) fprintf(fp, "\t%-15s ", "userquota@...");
437 (void) fprintf(fp, "YES NO <size> | none\n");
438 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
439 (void) fprintf(fp, "YES NO <size> | none\n");
441 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
442 "with standard units such as K, M, G, etc.\n"));
443 (void) fprintf(fp, gettext("\nUser-defined properties can "
444 "be specified by using a name containing a colon (:).\n"));
445 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
446 "properties must be appended with\n"
447 "a user or group specifier of one of these forms:\n"
448 " POSIX name (eg: \"matt\")\n"
449 " POSIX id (eg: \"126829\")\n"
450 " SMB name@domain (eg: \"matt@sun\")\n"
451 " SMB SID (eg: \"S-1-234-567-89\")\n"));
454 gettext("\nFor the property list, run: %s\n"),
457 gettext("\nFor the delegated permission list, run: %s\n"),
458 "zfs allow|unallow");
462 * See comments at end of main().
464 if (getenv("ZFS_ABORT") != NULL) {
465 (void) printf("dumping core by request\n");
469 exit(requested ? 0 : 2);
473 parseprop(nvlist_t *props)
475 char *propname = optarg;
476 char *propval, *strval;
478 if ((propval = strchr(propname, '=')) == NULL) {
479 (void) fprintf(stderr, gettext("missing "
480 "'=' for -o option\n"));
485 if (nvlist_lookup_string(props, propname, &strval) == 0) {
486 (void) fprintf(stderr, gettext("property '%s' "
487 "specified multiple times\n"), propname);
490 if (nvlist_add_string(props, propname, propval) != 0)
496 parse_depth(char *opt, int *flags)
501 depth = (int)strtol(opt, &tmp, 0);
503 (void) fprintf(stderr,
504 gettext("%s is not an integer\n"), optarg);
508 (void) fprintf(stderr,
509 gettext("Depth can not be negative.\n"));
512 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
516 #define PROGRESS_DELAY 2 /* seconds */
518 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";
519 static time_t pt_begin;
520 static char *pt_header = NULL;
521 static boolean_t pt_shown;
524 start_progress_timer(void)
526 pt_begin = time(NULL) + PROGRESS_DELAY;
531 set_progress_header(char *header)
533 assert(pt_header == NULL);
534 pt_header = safe_strdup(header);
536 (void) printf("%s: ", header);
537 (void) fflush(stdout);
542 update_progress(char *update)
544 if (!pt_shown && time(NULL) > pt_begin) {
545 int len = strlen(update);
547 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
549 (void) fflush(stdout);
551 } else if (pt_shown) {
552 int len = strlen(update);
554 (void) printf("%s%*.*s", update, len, len, pt_reverse);
555 (void) fflush(stdout);
560 finish_progress(char *done)
563 (void) printf("%s\n", done);
564 (void) fflush(stdout);
570 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
572 * Given an existing dataset, create a writable copy whose initial contents
573 * are the same as the source. The newly created dataset maintains a
574 * dependency on the original; the original cannot be destroyed so long as
577 * The '-p' flag creates all the non-existing ancestors of the target first.
580 zfs_do_clone(int argc, char **argv)
582 zfs_handle_t *zhp = NULL;
583 boolean_t parents = B_FALSE;
588 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
592 while ((c = getopt(argc, argv, "o:p")) != -1) {
595 if (parseprop(props))
602 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
611 /* check number of arguments */
613 (void) fprintf(stderr, gettext("missing source dataset "
618 (void) fprintf(stderr, gettext("missing target dataset "
623 (void) fprintf(stderr, gettext("too many arguments\n"));
627 /* open the source dataset */
628 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
631 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
634 * Now create the ancestors of the target dataset. If the
635 * target already exists and '-p' option was used we should not
638 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
641 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
646 ret = zfs_clone(zhp, argv[1], props);
648 /* create the mountpoint if necessary */
652 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
654 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
655 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
656 ret = zfs_share(clone);
675 * zfs create [-p] [-o prop=value] ... fs
676 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
678 * Create a new dataset. This command can be used to create filesystems
679 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
680 * For volumes, the user must specify a size to be used.
682 * The '-s' flag applies only to volumes, and indicates that we should not try
683 * to set the reservation for this volume. By default we set a reservation
684 * equal to the size for any volume. For pools with SPA_VERSION >=
685 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
687 * The '-p' flag creates all the non-existing ancestors of the target first.
690 zfs_do_create(int argc, char **argv)
692 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
693 zfs_handle_t *zhp = NULL;
696 boolean_t noreserve = B_FALSE;
697 boolean_t bflag = B_FALSE;
698 boolean_t parents = B_FALSE;
702 int canmount = ZFS_CANMOUNT_OFF;
704 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
708 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
711 type = ZFS_TYPE_VOLUME;
712 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
713 (void) fprintf(stderr, gettext("bad volume "
714 "size '%s': %s\n"), optarg,
715 libzfs_error_description(g_zfs));
719 if (nvlist_add_uint64(props,
720 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
729 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
730 (void) fprintf(stderr, gettext("bad volume "
731 "block size '%s': %s\n"), optarg,
732 libzfs_error_description(g_zfs));
736 if (nvlist_add_uint64(props,
737 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
742 if (parseprop(props))
749 (void) fprintf(stderr, gettext("missing size "
754 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
760 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
761 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
762 "used when creating a volume\n"));
769 /* check number of arguments */
771 (void) fprintf(stderr, gettext("missing %s argument\n"),
772 zfs_type_to_name(type));
776 (void) fprintf(stderr, gettext("too many arguments\n"));
780 if (type == ZFS_TYPE_VOLUME && !noreserve) {
781 zpool_handle_t *zpool_handle;
782 uint64_t spa_version;
784 zfs_prop_t resv_prop;
787 if (p = strchr(argv[0], '/'))
789 zpool_handle = zpool_open(g_zfs, argv[0]);
792 if (zpool_handle == NULL)
794 spa_version = zpool_get_prop_int(zpool_handle,
795 ZPOOL_PROP_VERSION, NULL);
796 zpool_close(zpool_handle);
797 if (spa_version >= SPA_VERSION_REFRESERVATION)
798 resv_prop = ZFS_PROP_REFRESERVATION;
800 resv_prop = ZFS_PROP_RESERVATION;
801 volsize = zvol_volsize_to_reservation(volsize, props);
803 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
805 if (nvlist_add_uint64(props,
806 zfs_prop_to_name(resv_prop), volsize) != 0) {
813 if (parents && zfs_name_valid(argv[0], type)) {
815 * Now create the ancestors of target dataset. If the target
816 * already exists and '-p' option was used we should not
819 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
823 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
828 if (zfs_create(g_zfs, argv[0], type, props) != 0)
831 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
836 * if the user doesn't want the dataset automatically mounted,
837 * then skip the mount/share step
839 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
840 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
843 * Mount and/or share the new filesystem as appropriate. We provide a
844 * verbose error message to let the user know that their filesystem was
845 * in fact created, even if we failed to mount or share it.
847 if (canmount == ZFS_CANMOUNT_ON) {
848 if (zfs_mount(zhp, NULL, 0) != 0) {
849 (void) fprintf(stderr, gettext("filesystem "
850 "successfully created, but not mounted\n"));
852 } else if (zfs_share(zhp) != 0) {
853 (void) fprintf(stderr, gettext("filesystem "
854 "successfully created, but not shared\n"));
871 * zfs destroy [-rRf] <fs, vol>
872 * zfs destroy [-rRd] <snap>
874 * -r Recursively destroy all children
875 * -R Recursively destroy all dependents, including clones
876 * -f Force unmounting of any dependents
877 * -d If we can't destroy now, mark for deferred destruction
879 * Destroys the given dataset. By default, it will unmount any filesystems,
880 * and refuse to destroy a dataset that has any dependents. A dependent can
881 * either be a child, or a clone of a child.
883 typedef struct destroy_cbdata {
890 boolean_t cb_closezhp;
891 zfs_handle_t *cb_target;
893 boolean_t cb_defer_destroy;
897 * Check for any dependents based on the '-r' or '-R' flags.
900 destroy_check_dependent(zfs_handle_t *zhp, void *data)
902 destroy_cbdata_t *cbp = data;
903 const char *tname = zfs_get_name(cbp->cb_target);
904 const char *name = zfs_get_name(zhp);
906 if (strncmp(tname, name, strlen(tname)) == 0 &&
907 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
909 * This is a direct descendant, not a clone somewhere else in
916 (void) fprintf(stderr, gettext("cannot destroy '%s': "
917 "%s has children\n"),
918 zfs_get_name(cbp->cb_target),
919 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
920 (void) fprintf(stderr, gettext("use '-r' to destroy "
921 "the following datasets:\n"));
922 cbp->cb_first = B_FALSE;
926 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
929 * This is a clone. We only want to report this if the '-r'
930 * wasn't specified, or the target is a snapshot.
932 if (!cbp->cb_recurse &&
933 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
937 (void) fprintf(stderr, gettext("cannot destroy '%s': "
938 "%s has dependent clones\n"),
939 zfs_get_name(cbp->cb_target),
940 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
941 (void) fprintf(stderr, gettext("use '-R' to destroy "
942 "the following datasets:\n"));
943 cbp->cb_first = B_FALSE;
947 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
956 destroy_callback(zfs_handle_t *zhp, void *data)
958 destroy_cbdata_t *cbp = data;
961 * Ignore pools (which we've already flagged as an error before getting
964 if (strchr(zfs_get_name(zhp), '/') == NULL &&
965 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
971 * Bail out on the first error.
973 if (zfs_unmount(zhp, NULL, cbp->cb_force ? MS_FORCE : 0) != 0 ||
974 zfs_destroy(zhp, cbp->cb_defer_destroy) != 0) {
984 destroy_snap_clones(zfs_handle_t *zhp, void *arg)
986 destroy_cbdata_t *cbp = arg;
987 char thissnap[MAXPATHLEN];
989 boolean_t closezhp = cbp->cb_closezhp;
992 (void) snprintf(thissnap, sizeof (thissnap),
993 "%s@%s", zfs_get_name(zhp), cbp->cb_snapname);
995 libzfs_print_on_error(g_zfs, B_FALSE);
996 szhp = zfs_open(g_zfs, thissnap, ZFS_TYPE_SNAPSHOT);
997 libzfs_print_on_error(g_zfs, B_TRUE);
1000 * Destroy any clones of this snapshot
1002 if (zfs_iter_dependents(szhp, B_FALSE, destroy_callback,
1012 cbp->cb_closezhp = B_TRUE;
1013 rv = zfs_iter_filesystems(zhp, destroy_snap_clones, arg);
1020 zfs_do_destroy(int argc, char **argv)
1022 destroy_cbdata_t cb = { 0 };
1026 zfs_type_t type = ZFS_TYPE_DATASET;
1029 while ((c = getopt(argc, argv, "dfrR")) != -1) {
1032 cb.cb_defer_destroy = B_TRUE;
1033 type = ZFS_TYPE_SNAPSHOT;
1047 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1056 /* check number of arguments */
1058 (void) fprintf(stderr, gettext("missing path argument\n"));
1062 (void) fprintf(stderr, gettext("too many arguments\n"));
1067 * If we are doing recursive destroy of a snapshot, then the
1068 * named snapshot may not exist. Go straight to libzfs.
1070 if (cb.cb_recurse && (cp = strchr(argv[0], '@'))) {
1074 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
1079 if (cb.cb_doclones) {
1080 boolean_t defer = cb.cb_defer_destroy;
1083 * Temporarily ignore the defer_destroy setting since
1084 * it's not supported for clones.
1086 cb.cb_defer_destroy = B_FALSE;
1087 cb.cb_snapname = cp;
1088 if (destroy_snap_clones(zhp, &cb) != 0) {
1092 cb.cb_defer_destroy = defer;
1095 ret = zfs_destroy_snaps(zhp, cp, cb.cb_defer_destroy);
1098 (void) fprintf(stderr,
1099 gettext("no snapshots destroyed\n"));
1104 /* Open the given dataset */
1105 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1111 * Perform an explicit check for pools before going any further.
1113 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1114 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1115 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1116 "operation does not apply to pools\n"),
1118 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1119 "%s' to destroy all datasets in the pool\n"),
1121 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1122 "to destroy the pool itself\n"), zfs_get_name(zhp));
1128 * Check for any dependents and/or clones.
1130 cb.cb_first = B_TRUE;
1131 if (!cb.cb_doclones && !cb.cb_defer_destroy &&
1132 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1138 if (cb.cb_error || (!cb.cb_defer_destroy &&
1139 (zfs_iter_dependents(zhp, B_FALSE, destroy_callback, &cb) != 0))) {
1145 * Do the real thing. The callback will close the handle regardless of
1146 * whether it succeeds or not.
1149 if (destroy_callback(zhp, &cb) != 0)
1156 is_recvd_column(zprop_get_cbdata_t *cbp)
1159 zfs_get_column_t col;
1161 for (i = 0; i < ZFS_GET_NCOLS &&
1162 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1163 if (col == GET_COL_RECVD)
1169 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1170 * < all | property[,property]... > < fs | snap | vol > ...
1172 * -r recurse over any child datasets
1173 * -H scripted mode. Headers are stripped, and fields are separated
1174 * by tabs instead of spaces.
1175 * -o Set of fields to display. One of "name,property,value,
1176 * received,source". Default is "name,property,value,source".
1177 * "all" is an alias for all five.
1178 * -s Set of sources to allow. One of
1179 * "local,default,inherited,received,temporary,none". Default is
1181 * -p Display values in parsable (literal) format.
1183 * Prints properties for the given datasets. The user can control which
1184 * columns to display as well as which property types to allow.
1188 * Invoked to display the properties for a single dataset.
1191 get_callback(zfs_handle_t *zhp, void *data)
1193 char buf[ZFS_MAXPROPLEN];
1194 char rbuf[ZFS_MAXPROPLEN];
1195 zprop_source_t sourcetype;
1196 char source[ZFS_MAXNAMELEN];
1197 zprop_get_cbdata_t *cbp = data;
1198 nvlist_t *user_props = zfs_get_user_props(zhp);
1199 zprop_list_t *pl = cbp->cb_proplist;
1203 boolean_t received = is_recvd_column(cbp);
1205 for (; pl != NULL; pl = pl->pl_next) {
1206 char *recvdval = NULL;
1208 * Skip the special fake placeholder. This will also skip over
1209 * the name property when 'all' is specified.
1211 if (pl->pl_prop == ZFS_PROP_NAME &&
1212 pl == cbp->cb_proplist)
1215 if (pl->pl_prop != ZPROP_INVAL) {
1216 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1217 sizeof (buf), &sourcetype, source,
1219 cbp->cb_literal) != 0) {
1222 if (!zfs_prop_valid_for_type(pl->pl_prop,
1223 ZFS_TYPE_DATASET)) {
1224 (void) fprintf(stderr,
1225 gettext("No such property '%s'\n"),
1226 zfs_prop_to_name(pl->pl_prop));
1229 sourcetype = ZPROP_SRC_NONE;
1230 (void) strlcpy(buf, "-", sizeof (buf));
1233 if (received && (zfs_prop_get_recvd(zhp,
1234 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1235 cbp->cb_literal) == 0))
1238 zprop_print_one_property(zfs_get_name(zhp), cbp,
1239 zfs_prop_to_name(pl->pl_prop),
1240 buf, sourcetype, source, recvdval);
1241 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1242 sourcetype = ZPROP_SRC_LOCAL;
1244 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1245 buf, sizeof (buf), cbp->cb_literal) != 0) {
1246 sourcetype = ZPROP_SRC_NONE;
1247 (void) strlcpy(buf, "-", sizeof (buf));
1250 zprop_print_one_property(zfs_get_name(zhp), cbp,
1251 pl->pl_user_prop, buf, sourcetype, source, NULL);
1253 if (nvlist_lookup_nvlist(user_props,
1254 pl->pl_user_prop, &propval) != 0) {
1257 sourcetype = ZPROP_SRC_NONE;
1260 verify(nvlist_lookup_string(propval,
1261 ZPROP_VALUE, &strval) == 0);
1262 verify(nvlist_lookup_string(propval,
1263 ZPROP_SOURCE, &sourceval) == 0);
1265 if (strcmp(sourceval,
1266 zfs_get_name(zhp)) == 0) {
1267 sourcetype = ZPROP_SRC_LOCAL;
1268 } else if (strcmp(sourceval,
1269 ZPROP_SOURCE_VAL_RECVD) == 0) {
1270 sourcetype = ZPROP_SRC_RECEIVED;
1272 sourcetype = ZPROP_SRC_INHERITED;
1273 (void) strlcpy(source,
1274 sourceval, sizeof (source));
1278 if (received && (zfs_prop_get_recvd(zhp,
1279 pl->pl_user_prop, rbuf, sizeof (rbuf),
1280 cbp->cb_literal) == 0))
1283 zprop_print_one_property(zfs_get_name(zhp), cbp,
1284 pl->pl_user_prop, strval, sourcetype,
1293 zfs_do_get(int argc, char **argv)
1295 zprop_get_cbdata_t cb = { 0 };
1296 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1297 char *value, *fields;
1300 zprop_list_t fake_name = { 0 };
1303 * Set up default columns and sources.
1305 cb.cb_sources = ZPROP_SRC_ALL;
1306 cb.cb_columns[0] = GET_COL_NAME;
1307 cb.cb_columns[1] = GET_COL_PROPERTY;
1308 cb.cb_columns[2] = GET_COL_VALUE;
1309 cb.cb_columns[3] = GET_COL_SOURCE;
1310 cb.cb_type = ZFS_TYPE_DATASET;
1313 while ((c = getopt(argc, argv, ":d:o:s:rHp")) != -1) {
1316 cb.cb_literal = B_TRUE;
1319 limit = parse_depth(optarg, &flags);
1322 flags |= ZFS_ITER_RECURSE;
1325 cb.cb_scripted = B_TRUE;
1328 (void) fprintf(stderr, gettext("missing argument for "
1329 "'%c' option\n"), optopt);
1334 * Process the set of columns to display. We zero out
1335 * the structure to give us a blank slate.
1337 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1339 while (*optarg != '\0') {
1340 static char *col_subopts[] =
1341 { "name", "property", "value", "received",
1342 "source", "all", NULL };
1344 if (i == ZFS_GET_NCOLS) {
1345 (void) fprintf(stderr, gettext("too "
1346 "many fields given to -o "
1351 switch (getsubopt(&optarg, col_subopts,
1354 cb.cb_columns[i++] = GET_COL_NAME;
1357 cb.cb_columns[i++] = GET_COL_PROPERTY;
1360 cb.cb_columns[i++] = GET_COL_VALUE;
1363 cb.cb_columns[i++] = GET_COL_RECVD;
1364 flags |= ZFS_ITER_RECVD_PROPS;
1367 cb.cb_columns[i++] = GET_COL_SOURCE;
1371 (void) fprintf(stderr,
1372 gettext("\"all\" conflicts "
1373 "with specific fields "
1374 "given to -o option\n"));
1377 cb.cb_columns[0] = GET_COL_NAME;
1378 cb.cb_columns[1] = GET_COL_PROPERTY;
1379 cb.cb_columns[2] = GET_COL_VALUE;
1380 cb.cb_columns[3] = GET_COL_RECVD;
1381 cb.cb_columns[4] = GET_COL_SOURCE;
1382 flags |= ZFS_ITER_RECVD_PROPS;
1386 (void) fprintf(stderr,
1387 gettext("invalid column name "
1396 while (*optarg != '\0') {
1397 static char *source_subopts[] = {
1398 "local", "default", "inherited",
1399 "received", "temporary", "none",
1402 switch (getsubopt(&optarg, source_subopts,
1405 cb.cb_sources |= ZPROP_SRC_LOCAL;
1408 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1411 cb.cb_sources |= ZPROP_SRC_INHERITED;
1414 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1417 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1420 cb.cb_sources |= ZPROP_SRC_NONE;
1423 (void) fprintf(stderr,
1424 gettext("invalid source "
1432 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1442 (void) fprintf(stderr, gettext("missing property "
1449 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1457 * As part of zfs_expand_proplist(), we keep track of the maximum column
1458 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1459 * need to know the maximum name length. However, the user likely did
1460 * not specify 'name' as one of the properties to fetch, so we need to
1461 * make sure we always include at least this property for
1462 * print_get_headers() to work properly.
1464 if (cb.cb_proplist != NULL) {
1465 fake_name.pl_prop = ZFS_PROP_NAME;
1466 fake_name.pl_width = strlen(gettext("NAME"));
1467 fake_name.pl_next = cb.cb_proplist;
1468 cb.cb_proplist = &fake_name;
1471 cb.cb_first = B_TRUE;
1473 /* run for each object */
1474 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL,
1475 &cb.cb_proplist, limit, get_callback, &cb);
1477 if (cb.cb_proplist == &fake_name)
1478 zprop_free_list(fake_name.pl_next);
1480 zprop_free_list(cb.cb_proplist);
1486 * inherit [-rS] <property> <fs|vol> ...
1488 * -r Recurse over all children
1489 * -S Revert to received value, if any
1491 * For each dataset specified on the command line, inherit the given property
1492 * from its parent. Inheriting a property at the pool level will cause it to
1493 * use the default value. The '-r' flag will recurse over all children, and is
1494 * useful for setting a property on a hierarchy-wide basis, regardless of any
1495 * local modifications for each dataset.
1498 typedef struct inherit_cbdata {
1499 const char *cb_propname;
1500 boolean_t cb_received;
1504 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1506 inherit_cbdata_t *cb = data;
1507 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1510 * If we're doing it recursively, then ignore properties that
1511 * are not valid for this type of dataset.
1513 if (prop != ZPROP_INVAL &&
1514 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1517 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1521 inherit_cb(zfs_handle_t *zhp, void *data)
1523 inherit_cbdata_t *cb = data;
1525 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1529 zfs_do_inherit(int argc, char **argv)
1533 inherit_cbdata_t cb = { 0 };
1537 boolean_t received = B_FALSE;
1540 while ((c = getopt(argc, argv, "rS")) != -1) {
1543 flags |= ZFS_ITER_RECURSE;
1550 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1559 /* check number of arguments */
1561 (void) fprintf(stderr, gettext("missing property argument\n"));
1565 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1573 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1574 if (zfs_prop_readonly(prop)) {
1575 (void) fprintf(stderr, gettext(
1576 "%s property is read-only\n"),
1580 if (!zfs_prop_inheritable(prop) && !received) {
1581 (void) fprintf(stderr, gettext("'%s' property cannot "
1582 "be inherited\n"), propname);
1583 if (prop == ZFS_PROP_QUOTA ||
1584 prop == ZFS_PROP_RESERVATION ||
1585 prop == ZFS_PROP_REFQUOTA ||
1586 prop == ZFS_PROP_REFRESERVATION)
1587 (void) fprintf(stderr, gettext("use 'zfs set "
1588 "%s=none' to clear\n"), propname);
1591 if (received && (prop == ZFS_PROP_VOLSIZE ||
1592 prop == ZFS_PROP_VERSION)) {
1593 (void) fprintf(stderr, gettext("'%s' property cannot "
1594 "be reverted to a received value\n"), propname);
1597 } else if (!zfs_prop_user(propname)) {
1598 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1603 cb.cb_propname = propname;
1604 cb.cb_received = received;
1606 if (flags & ZFS_ITER_RECURSE) {
1607 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1608 NULL, NULL, 0, inherit_recurse_cb, &cb);
1610 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1611 NULL, NULL, 0, inherit_cb, &cb);
1617 typedef struct upgrade_cbdata {
1618 uint64_t cb_numupgraded;
1619 uint64_t cb_numsamegraded;
1620 uint64_t cb_numfailed;
1621 uint64_t cb_version;
1623 boolean_t cb_foundone;
1624 char cb_lastfs[ZFS_MAXNAMELEN];
1628 same_pool(zfs_handle_t *zhp, const char *name)
1630 int len1 = strcspn(name, "/@");
1631 const char *zhname = zfs_get_name(zhp);
1632 int len2 = strcspn(zhname, "/@");
1636 return (strncmp(name, zhname, len1) == 0);
1640 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1642 upgrade_cbdata_t *cb = data;
1643 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1645 /* list if it's old/new */
1646 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1647 (cb->cb_newer && version > ZPL_VERSION)) {
1650 str = gettext("The following filesystems are "
1651 "formatted using a newer software version and\n"
1652 "cannot be accessed on the current system.\n\n");
1654 str = gettext("The following filesystems are "
1655 "out of date, and can be upgraded. After being\n"
1656 "upgraded, these filesystems (and any 'zfs send' "
1657 "streams generated from\n"
1658 "subsequent snapshots) will no longer be "
1659 "accessible by older software versions.\n\n");
1662 if (!cb->cb_foundone) {
1664 (void) printf(gettext("VER FILESYSTEM\n"));
1665 (void) printf(gettext("--- ------------\n"));
1666 cb->cb_foundone = B_TRUE;
1669 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1676 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1678 upgrade_cbdata_t *cb = data;
1679 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1680 int needed_spa_version;
1683 if (zfs_spa_version(zhp, &spa_version) < 0)
1686 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1688 if (needed_spa_version < 0)
1691 if (spa_version < needed_spa_version) {
1693 (void) printf(gettext("%s: can not be "
1694 "upgraded; the pool version needs to first "
1695 "be upgraded\nto version %d\n\n"),
1696 zfs_get_name(zhp), needed_spa_version);
1702 if (version < cb->cb_version) {
1704 (void) snprintf(verstr, sizeof (verstr),
1705 "%llu", cb->cb_version);
1706 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1708 * If they did "zfs upgrade -a", then we could
1709 * be doing ioctls to different pools. We need
1710 * to log this history once to each pool.
1712 verify(zpool_stage_history(g_zfs, history_str) == 0);
1714 if (zfs_prop_set(zhp, "version", verstr) == 0)
1715 cb->cb_numupgraded++;
1718 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1719 } else if (version > cb->cb_version) {
1720 /* can't downgrade */
1721 (void) printf(gettext("%s: can not be downgraded; "
1722 "it is already at version %u\n"),
1723 zfs_get_name(zhp), version);
1726 cb->cb_numsamegraded++;
1734 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1737 zfs_do_upgrade(int argc, char **argv)
1739 boolean_t all = B_FALSE;
1740 boolean_t showversions = B_FALSE;
1742 upgrade_cbdata_t cb = { 0 };
1744 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1747 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1750 flags |= ZFS_ITER_RECURSE;
1753 showversions = B_TRUE;
1756 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1757 optarg, &cb.cb_version) != 0) {
1758 (void) fprintf(stderr,
1759 gettext("invalid version %s\n"), optarg);
1768 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1777 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1779 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1780 cb.cb_version || argc))
1782 if ((all || argc) && (showversions))
1788 /* Show info on available versions. */
1789 (void) printf(gettext("The following filesystem versions are "
1791 (void) printf(gettext("VER DESCRIPTION\n"));
1792 (void) printf("--- -----------------------------------------"
1793 "---------------\n");
1794 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
1795 (void) printf(gettext(" 2 Enhanced directory entries\n"));
1796 (void) printf(gettext(" 3 Case insensitive and File system "
1797 "unique identifier (FUID)\n"));
1798 (void) printf(gettext(" 4 userquota, groupquota "
1800 (void) printf(gettext(" 5 System attributes\n"));
1801 (void) printf(gettext("\nFor more information on a particular "
1802 "version, including supported releases,\n"));
1803 (void) printf("see the ZFS Administration Guide.\n\n");
1805 } else if (argc || all) {
1806 /* Upgrade filesystems */
1807 if (cb.cb_version == 0)
1808 cb.cb_version = ZPL_VERSION;
1809 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
1810 NULL, NULL, 0, upgrade_set_callback, &cb);
1811 (void) printf(gettext("%llu filesystems upgraded\n"),
1813 if (cb.cb_numsamegraded) {
1814 (void) printf(gettext("%llu filesystems already at "
1816 cb.cb_numsamegraded);
1818 if (cb.cb_numfailed != 0)
1821 /* List old-version filesytems */
1823 (void) printf(gettext("This system is currently running "
1824 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
1826 flags |= ZFS_ITER_RECURSE;
1827 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1828 NULL, NULL, 0, upgrade_list_callback, &cb);
1830 found = cb.cb_foundone;
1831 cb.cb_foundone = B_FALSE;
1832 cb.cb_newer = B_TRUE;
1834 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
1835 NULL, NULL, 0, upgrade_list_callback, &cb);
1837 if (!cb.cb_foundone && !found) {
1838 (void) printf(gettext("All filesystems are "
1839 "formatted with the current version.\n"));
1846 #define USTYPE_USR_BIT (0)
1847 #define USTYPE_GRP_BIT (1)
1848 #define USTYPE_PSX_BIT (2)
1849 #define USTYPE_SMB_BIT (3)
1851 #define USTYPE_USR (1 << USTYPE_USR_BIT)
1852 #define USTYPE_GRP (1 << USTYPE_GRP_BIT)
1854 #define USTYPE_PSX (1 << USTYPE_PSX_BIT)
1855 #define USTYPE_SMB (1 << USTYPE_SMB_BIT)
1857 #define USTYPE_PSX_USR (USTYPE_PSX | USTYPE_USR)
1858 #define USTYPE_SMB_USR (USTYPE_SMB | USTYPE_USR)
1859 #define USTYPE_PSX_GRP (USTYPE_PSX | USTYPE_GRP)
1860 #define USTYPE_SMB_GRP (USTYPE_SMB | USTYPE_GRP)
1861 #define USTYPE_ALL (USTYPE_PSX_USR | USTYPE_SMB_USR \
1862 | USTYPE_PSX_GRP | USTYPE_SMB_GRP)
1865 #define USPROP_USED_BIT (0)
1866 #define USPROP_QUOTA_BIT (1)
1868 #define USPROP_USED (1 << USPROP_USED_BIT)
1869 #define USPROP_QUOTA (1 << USPROP_QUOTA_BIT)
1871 typedef struct us_node {
1873 uu_avl_node_t usn_avlnode;
1874 uu_list_node_t usn_listnode;
1877 typedef struct us_cbdata {
1879 uu_avl_pool_t *cb_avl_pool;
1881 boolean_t cb_numname;
1882 boolean_t cb_nicenum;
1883 boolean_t cb_sid2posix;
1884 zfs_userquota_prop_t cb_prop;
1885 zfs_sort_column_t *cb_sortcol;
1886 size_t cb_max_typelen;
1887 size_t cb_max_namelen;
1888 size_t cb_max_usedlen;
1889 size_t cb_max_quotalen;
1893 zfs_sort_column_t *si_sortcol;
1894 boolean_t si_num_name;
1895 boolean_t si_parsable;
1899 us_compare(const void *larg, const void *rarg, void *unused)
1901 const us_node_t *l = larg;
1902 const us_node_t *r = rarg;
1904 us_sort_info_t *si = (us_sort_info_t *)unused;
1905 zfs_sort_column_t *sortcol = si->si_sortcol;
1906 boolean_t num_name = si->si_num_name;
1907 nvlist_t *lnvl = l->usn_nvl;
1908 nvlist_t *rnvl = r->usn_nvl;
1910 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
1917 zfs_prop_t prop = sortcol->sc_prop;
1918 const char *propname = NULL;
1919 boolean_t reverse = sortcol->sc_reverse;
1924 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
1925 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
1927 rc = (rv32 > lv32) ? 1 : -1;
1932 (void) nvlist_lookup_uint32(lnvl, propname,
1934 (void) nvlist_lookup_uint32(rnvl, propname,
1937 rc = (rv32 > lv32) ? 1 : -1;
1939 (void) nvlist_lookup_string(lnvl, propname,
1941 (void) nvlist_lookup_string(rnvl, propname,
1943 rc = strcmp(lvstr, rvstr);
1948 case ZFS_PROP_QUOTA:
1949 if (ZFS_PROP_USED == prop)
1953 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
1954 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
1956 rc = (rv64 > lv64) ? 1 : -1;
1961 return (reverse ? 1 : -1);
1963 return (reverse ? -1 : 1);
1969 static inline const char *
1970 us_type2str(unsigned field_type)
1972 switch (field_type) {
1973 case USTYPE_PSX_USR:
1974 return ("POSIX User");
1975 case USTYPE_PSX_GRP:
1976 return ("POSIX Group");
1977 case USTYPE_SMB_USR:
1978 return ("SMB User");
1979 case USTYPE_SMB_GRP:
1980 return ("SMB Group");
1982 return ("Undefined");
1990 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
1992 us_cbdata_t *cb = (us_cbdata_t *)arg;
1993 zfs_userquota_prop_t prop = cb->cb_prop;
1999 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2000 uu_avl_t *avl = cb->cb_avl;
2004 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2006 const char *typestr;
2010 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2012 if (domain == NULL || domain[0] == '\0') {
2014 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2015 type = USTYPE_PSX_GRP;
2016 struct group *g = getgrgid(rid);
2020 type = USTYPE_PSX_USR;
2021 struct passwd *p = getpwuid(rid);
2026 char sid[ZFS_MAXNAMELEN+32];
2031 directory_error_t e;
2034 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2036 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2037 type = USTYPE_SMB_GRP;
2039 err = sid_to_id(sid, B_FALSE, &id);
2042 type = USTYPE_SMB_USR;
2044 err = sid_to_id(sid, B_TRUE, &id);
2052 e = directory_name_from_sid(NULL, sid, &name, &classes);
2054 directory_error_free(e);
2065 * if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA)
2071 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED)
2076 (void) snprintf(namebuf, sizeof (namebuf), "%u", rid);
2081 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2083 (void) sprintf(sizebuf, "%llu", space);
2085 node = safe_malloc(sizeof (us_node_t));
2086 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2088 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
2093 if (nvlist_add_uint32(props, "type", type) != 0)
2096 if (cb->cb_numname) {
2097 if (nvlist_add_uint32(props, "name", rid) != 0)
2099 namelen = strlen(namebuf);
2101 if (nvlist_add_string(props, "name", name) != 0)
2103 namelen = strlen(name);
2106 typestr = us_type2str(type);
2107 typelen = strlen(gettext(typestr));
2108 if (typelen > cb->cb_max_typelen)
2109 cb->cb_max_typelen = typelen;
2111 if (namelen > cb->cb_max_namelen)
2112 cb->cb_max_namelen = namelen;
2114 sizelen = strlen(sizebuf);
2115 if (0 == strcmp(propname, "used")) {
2116 if (sizelen > cb->cb_max_usedlen)
2117 cb->cb_max_usedlen = sizelen;
2119 if (sizelen > cb->cb_max_quotalen)
2120 cb->cb_max_quotalen = sizelen;
2123 node->usn_nvl = props;
2125 n = uu_avl_find(avl, node, &sortinfo, &idx);
2127 uu_avl_insert(avl, node, idx);
2132 props = node->usn_nvl;
2135 if (nvlist_add_uint64(props, propname, space) != 0)
2141 static inline boolean_t
2142 usprop_check(zfs_userquota_prop_t p, unsigned types, unsigned props)
2148 case ZFS_PROP_USERUSED:
2152 case ZFS_PROP_USERQUOTA:
2154 prop = USPROP_QUOTA;
2156 case ZFS_PROP_GROUPUSED:
2160 case ZFS_PROP_GROUPQUOTA:
2162 prop = USPROP_QUOTA;
2168 return (type & types && prop & props);
2171 #define USFIELD_TYPE (1 << 0)
2172 #define USFIELD_NAME (1 << 1)
2173 #define USFIELD_USED (1 << 2)
2174 #define USFIELD_QUOTA (1 << 3)
2175 #define USFIELD_ALL (USFIELD_TYPE | USFIELD_NAME | USFIELD_USED | USFIELD_QUOTA)
2178 parsefields(unsigned *fieldsp, char **names, unsigned *bits, size_t len)
2180 char *field = optarg;
2185 boolean_t found = B_FALSE;
2186 delim = strchr(field, ',');
2190 for (i = 0; i < len; i++)
2191 if (0 == strcmp(field, names[i])) {
2193 *fieldsp |= bits[i];
2198 (void) fprintf(stderr, gettext("invalid type '%s'"
2199 "for -t option\n"), field);
2210 static char *type_names[] = { "posixuser", "smbuser", "posixgroup", "smbgroup",
2212 static unsigned type_bits[] = {
2220 static char *us_field_names[] = { "type", "name", "used", "quota" };
2221 static unsigned us_field_bits[] = {
2229 print_us_node(boolean_t scripted, boolean_t parseable, unsigned fields,
2230 size_t type_width, size_t name_width, size_t used_width,
2231 size_t quota_width, us_node_t *node)
2233 nvlist_t *nvl = node->usn_nvl;
2234 nvpair_t *nvp = NULL;
2235 char valstr[ZFS_MAXNAMELEN];
2236 boolean_t first = B_TRUE;
2237 boolean_t quota_found = B_FALSE;
2239 if (fields & USFIELD_QUOTA && !nvlist_exists(nvl, "quota"))
2240 if (nvlist_add_string(nvl, "quota", "none") != 0)
2243 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2244 char *pname = nvpair_name(nvp);
2245 data_type_t type = nvpair_type(nvp);
2248 char *strval = NULL;
2252 for (i = 0; i < 4; i++) {
2253 if (0 == strcmp(pname, us_field_names[i])) {
2254 field = us_field_bits[i];
2259 if (!(field & fields))
2263 case DATA_TYPE_UINT32:
2264 (void) nvpair_value_uint32(nvp, &val32);
2266 case DATA_TYPE_UINT64:
2267 (void) nvpair_value_uint64(nvp, &val64);
2269 case DATA_TYPE_STRING:
2270 (void) nvpair_value_string(nvp, &strval);
2273 (void) fprintf(stderr, "Invalid data type\n");
2278 (void) printf("\t");
2284 strval = (char *)us_type2str(val32);
2288 if (type == DATA_TYPE_UINT64) {
2289 (void) sprintf(valstr, "%llu", val64);
2296 if (type == DATA_TYPE_UINT64) {
2297 (void) nvpair_value_uint64(nvp, &val64);
2299 (void) sprintf(valstr, "%llu", val64);
2301 zfs_nicenum(val64, valstr,
2306 if (field == USFIELD_USED)
2309 quota_found = B_FALSE;
2310 width = quota_width;
2316 if (field == USFIELD_QUOTA && !quota_found)
2317 (void) printf("%*s", width, strval);
2319 if (type == DATA_TYPE_STRING)
2320 (void) printf("%-*s", width, strval);
2322 (void) printf("%*s", width, strval);
2329 (void) printf("\n");
2333 print_us(boolean_t scripted, boolean_t parsable, unsigned fields,
2334 unsigned type_width, unsigned name_width, unsigned used_width,
2335 unsigned quota_width, boolean_t rmnode, uu_avl_t *avl)
2337 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2341 size_t width[4] = { type_width, name_width, used_width, quota_width };
2344 boolean_t first = B_TRUE;
2345 for (i = 0; i < 4; i++) {
2346 unsigned field = us_field_bits[i];
2347 if (!(field & fields))
2350 col = gettext(us_field_hdr[i]);
2351 if (field == USFIELD_TYPE || field == USFIELD_NAME)
2352 (void) printf(first?"%-*s":" %-*s", width[i],
2355 (void) printf(first?"%*s":" %*s", width[i],
2359 (void) printf("\n");
2362 for (node = uu_avl_first(avl); node != NULL;
2363 node = uu_avl_next(avl, node)) {
2364 print_us_node(scripted, parsable, fields, type_width,
2365 name_width, used_width, used_width, node);
2367 nvlist_free(node->usn_nvl);
2372 zfs_do_userspace(int argc, char **argv)
2375 zfs_userquota_prop_t p;
2377 uu_avl_pool_t *avl_pool;
2379 uu_avl_walk_t *walk;
2382 boolean_t scripted = B_FALSE;
2383 boolean_t prtnum = B_FALSE;
2384 boolean_t parseable = B_FALSE;
2385 boolean_t sid2posix = B_FALSE;
2388 zfs_sort_column_t *default_sortcol = NULL;
2389 zfs_sort_column_t *sortcol = NULL;
2390 unsigned types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2391 unsigned fields = 0;
2392 unsigned props = USPROP_USED | USPROP_QUOTA;
2395 boolean_t resort_avl = B_FALSE;
2401 if (0 == strcmp(cmd, "groupspace"))
2402 /* toggle default group types */
2403 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2406 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2418 if (parsefields(&fields, us_field_names, us_field_bits,
2423 if (zfs_add_sort_column(&sortcol, optarg,
2425 (void) fprintf(stderr,
2426 gettext("invalid property '%s'\n"), optarg);
2431 if (zfs_add_sort_column(&sortcol, optarg,
2433 (void) fprintf(stderr,
2434 gettext("invalid property '%s'\n"), optarg);
2439 if (parsefields(&types, type_names, type_bits, 5))
2446 (void) fprintf(stderr, gettext("missing argument for "
2447 "'%c' option\n"), optopt);
2451 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2460 /* ok, now we have sorted by default colums (type,name) avl tree */
2462 zfs_sort_column_t *sc;
2463 for (sc = sortcol; sc; sc = sc->sc_next) {
2464 if (sc->sc_prop == ZFS_PROP_QUOTA) {
2465 resort_avl = B_TRUE;
2472 fields = USFIELD_ALL;
2474 if ((zhp = zfs_open(g_zfs, argv[argc-1], ZFS_TYPE_DATASET)) == NULL)
2477 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2478 offsetof(us_node_t, usn_avlnode),
2479 us_compare, UU_DEFAULT)) == NULL)
2481 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2484 if (sortcol && !resort_avl)
2485 cb.cb_sortcol = sortcol;
2487 (void) zfs_add_sort_column(&default_sortcol, "type", B_FALSE);
2488 (void) zfs_add_sort_column(&default_sortcol, "name", B_FALSE);
2489 cb.cb_sortcol = default_sortcol;
2491 cb.cb_numname = prtnum;
2492 cb.cb_nicenum = !parseable;
2493 cb.cb_avl_pool = avl_pool;
2494 cb.cb_avl = avl_tree;
2495 cb.cb_sid2posix = sid2posix;
2496 cb.cb_max_typelen = strlen(gettext("TYPE"));
2497 cb.cb_max_namelen = strlen(gettext("NAME"));
2498 cb.cb_max_usedlen = strlen(gettext("USED"));
2499 cb.cb_max_quotalen = strlen(gettext("QUOTA"));
2501 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2502 if (!usprop_check(p, types, props))
2506 error = zfs_userspace(zhp, p, userspace_cb, &cb);
2515 uu_list_pool_t *listpool;
2517 uu_avl_index_t idx = 0;
2518 uu_list_index_t idx2 = 0;
2519 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2520 offsetof(us_node_t, usn_listnode), NULL,
2522 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2524 node = uu_avl_first(avl_tree);
2525 uu_list_node_init(node, &node->usn_listnode, listpool);
2526 while (node != NULL) {
2528 node = uu_avl_next(avl_tree, node);
2529 uu_avl_remove(avl_tree, rmnode);
2530 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL) {
2531 uu_list_insert(list, rmnode, idx2);
2535 for (node = uu_list_first(list); node != NULL;
2536 node = uu_list_next(list, node)) {
2537 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2538 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) ==
2540 uu_avl_insert(avl_tree, node, idx);
2543 uu_list_destroy(list);
2546 /* print & free node`s nvlist memory */
2547 print_us(scripted, parseable, fields, cb.cb_max_typelen,
2548 cb.cb_max_namelen, cb.cb_max_usedlen,
2549 cb.cb_max_quotalen, B_TRUE, cb.cb_avl);
2552 zfs_free_sort_columns(sortcol);
2553 zfs_free_sort_columns(default_sortcol);
2556 * Finally, clean up the AVL tree.
2558 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2561 while ((node = uu_avl_walk_next(walk)) != NULL) {
2562 uu_avl_remove(cb.cb_avl, node);
2566 uu_avl_walk_end(walk);
2567 uu_avl_destroy(avl_tree);
2568 uu_avl_pool_destroy(avl_pool);
2574 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2575 * [-s property [-s property]...] [-S property [-S property]...]
2578 * -r Recurse over all children
2579 * -d Limit recursion by depth.
2580 * -H Scripted mode; elide headers and separate columns by tabs
2581 * -o Control which fields to display.
2582 * -t Control which object types to display.
2583 * -s Specify sort columns, descending order.
2584 * -S Specify sort columns, ascending order.
2586 * When given no arguments, lists all filesystems in the system.
2587 * Otherwise, list the specified datasets, optionally recursing down them if
2588 * '-r' is specified.
2590 typedef struct list_cbdata {
2592 boolean_t cb_scripted;
2593 zprop_list_t *cb_proplist;
2597 * Given a list of columns to display, output appropriate headers for each one.
2600 print_header(zprop_list_t *pl)
2602 char headerbuf[ZFS_MAXPROPLEN];
2605 boolean_t first = B_TRUE;
2606 boolean_t right_justify;
2608 for (; pl != NULL; pl = pl->pl_next) {
2615 right_justify = B_FALSE;
2616 if (pl->pl_prop != ZPROP_INVAL) {
2617 header = zfs_prop_column_name(pl->pl_prop);
2618 right_justify = zfs_prop_align_right(pl->pl_prop);
2620 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2621 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2622 headerbuf[i] = '\0';
2626 if (pl->pl_next == NULL && !right_justify)
2627 (void) printf("%s", header);
2628 else if (right_justify)
2629 (void) printf("%*s", pl->pl_width, header);
2631 (void) printf("%-*s", pl->pl_width, header);
2634 (void) printf("\n");
2638 * Given a dataset and a list of fields, print out all the properties according
2639 * to the described layout.
2642 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
2644 boolean_t first = B_TRUE;
2645 char property[ZFS_MAXPROPLEN];
2646 nvlist_t *userprops = zfs_get_user_props(zhp);
2649 boolean_t right_justify;
2652 for (; pl != NULL; pl = pl->pl_next) {
2655 (void) printf("\t");
2662 if (pl->pl_prop != ZPROP_INVAL) {
2663 if (zfs_prop_get(zhp, pl->pl_prop, property,
2664 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2669 right_justify = zfs_prop_align_right(pl->pl_prop);
2670 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2671 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2672 property, sizeof (property), B_FALSE) != 0)
2676 right_justify = B_TRUE;
2678 if (nvlist_lookup_nvlist(userprops,
2679 pl->pl_user_prop, &propval) != 0)
2682 verify(nvlist_lookup_string(propval,
2683 ZPROP_VALUE, &propstr) == 0);
2684 right_justify = B_FALSE;
2687 width = pl->pl_width;
2690 * If this is being called in scripted mode, or if this is the
2691 * last column and it is left-justified, don't include a width
2694 if (scripted || (pl->pl_next == NULL && !right_justify))
2695 (void) printf("%s", propstr);
2696 else if (right_justify)
2697 (void) printf("%*s", width, propstr);
2699 (void) printf("%-*s", width, propstr);
2702 (void) printf("\n");
2706 * Generic callback function to list a dataset or snapshot.
2709 list_callback(zfs_handle_t *zhp, void *data)
2711 list_cbdata_t *cbp = data;
2713 if (cbp->cb_first) {
2714 if (!cbp->cb_scripted)
2715 print_header(cbp->cb_proplist);
2716 cbp->cb_first = B_FALSE;
2719 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2725 zfs_do_list(int argc, char **argv)
2728 boolean_t scripted = B_FALSE;
2729 static char default_fields[] =
2730 "name,used,available,referenced,mountpoint";
2731 int types = ZFS_TYPE_DATASET;
2732 boolean_t types_specified = B_FALSE;
2733 char *fields = NULL;
2734 list_cbdata_t cb = { 0 };
2738 zfs_sort_column_t *sortcol = NULL;
2739 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2742 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2748 limit = parse_depth(optarg, &flags);
2751 flags |= ZFS_ITER_RECURSE;
2757 if (zfs_add_sort_column(&sortcol, optarg,
2759 (void) fprintf(stderr,
2760 gettext("invalid property '%s'\n"), optarg);
2765 if (zfs_add_sort_column(&sortcol, optarg,
2767 (void) fprintf(stderr,
2768 gettext("invalid property '%s'\n"), optarg);
2774 types_specified = B_TRUE;
2775 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2776 while (*optarg != '\0') {
2777 static char *type_subopts[] = { "filesystem",
2778 "volume", "snapshot", "all", NULL };
2780 switch (getsubopt(&optarg, type_subopts,
2783 types |= ZFS_TYPE_FILESYSTEM;
2786 types |= ZFS_TYPE_VOLUME;
2789 types |= ZFS_TYPE_SNAPSHOT;
2792 types = ZFS_TYPE_DATASET;
2796 (void) fprintf(stderr,
2797 gettext("invalid type '%s'\n"),
2804 (void) fprintf(stderr, gettext("missing argument for "
2805 "'%c' option\n"), optopt);
2809 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2819 fields = default_fields;
2822 * If "-o space" and no types were specified, don't display snapshots.
2824 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
2825 types &= ~ZFS_TYPE_SNAPSHOT;
2828 * If the user specifies '-o all', the zprop_get_list() doesn't
2829 * normally include the name of the dataset. For 'zfs list', we always
2830 * want this property to be first.
2832 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
2836 cb.cb_scripted = scripted;
2837 cb.cb_first = B_TRUE;
2839 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
2840 limit, list_callback, &cb);
2842 zprop_free_list(cb.cb_proplist);
2843 zfs_free_sort_columns(sortcol);
2845 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
2846 (void) printf(gettext("no datasets available\n"));
2852 * zfs rename <fs | snap | vol> <fs | snap | vol>
2853 * zfs rename -p <fs | vol> <fs | vol>
2854 * zfs rename -r <snap> <snap>
2856 * Renames the given dataset to another of the same type.
2858 * The '-p' flag creates all the non-existing ancestors of the target first.
2862 zfs_do_rename(int argc, char **argv)
2867 boolean_t recurse = B_FALSE;
2868 boolean_t parents = B_FALSE;
2871 while ((c = getopt(argc, argv, "pr")) != -1) {
2881 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2890 /* check number of arguments */
2892 (void) fprintf(stderr, gettext("missing source dataset "
2897 (void) fprintf(stderr, gettext("missing target dataset "
2902 (void) fprintf(stderr, gettext("too many arguments\n"));
2906 if (recurse && parents) {
2907 (void) fprintf(stderr, gettext("-p and -r options are mutually "
2912 if (recurse && strchr(argv[0], '@') == 0) {
2913 (void) fprintf(stderr, gettext("source dataset for recursive "
2914 "rename must be a snapshot\n"));
2918 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
2919 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
2922 /* If we were asked and the name looks good, try to create ancestors. */
2923 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
2924 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
2929 ret = (zfs_rename(zhp, argv[1], recurse) != 0);
2938 * Promotes the given clone fs to be the parent
2942 zfs_do_promote(int argc, char **argv)
2948 if (argc > 1 && argv[1][0] == '-') {
2949 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2954 /* check number of arguments */
2956 (void) fprintf(stderr, gettext("missing clone filesystem"
2961 (void) fprintf(stderr, gettext("too many arguments\n"));
2965 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2969 ret = (zfs_promote(zhp) != 0);
2977 * zfs rollback [-rRf] <snapshot>
2979 * -r Delete any intervening snapshots before doing rollback
2980 * -R Delete any snapshots and their clones
2981 * -f ignored for backwards compatability
2983 * Given a filesystem, rollback to a specific snapshot, discarding any changes
2984 * since then and making it the active dataset. If more recent snapshots exist,
2985 * the command will complain unless the '-r' flag is given.
2987 typedef struct rollback_cbdata {
2993 boolean_t cb_recurse;
2994 boolean_t cb_dependent;
2995 } rollback_cbdata_t;
2998 * Report any snapshots more recent than the one specified. Used when '-r' is
2999 * not specified. We reuse this same callback for the snapshot dependents - if
3000 * 'cb_dependent' is set, then this is a dependent and we should report it
3001 * without checking the transaction group.
3004 rollback_check(zfs_handle_t *zhp, void *data)
3006 rollback_cbdata_t *cbp = data;
3008 if (cbp->cb_doclones) {
3013 if (!cbp->cb_dependent) {
3014 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
3015 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3016 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3019 if (cbp->cb_first && !cbp->cb_recurse) {
3020 (void) fprintf(stderr, gettext("cannot "
3021 "rollback to '%s': more recent snapshots "
3024 (void) fprintf(stderr, gettext("use '-r' to "
3025 "force deletion of the following "
3031 if (cbp->cb_recurse) {
3032 cbp->cb_dependent = B_TRUE;
3033 if (zfs_iter_dependents(zhp, B_TRUE,
3034 rollback_check, cbp) != 0) {
3038 cbp->cb_dependent = B_FALSE;
3040 (void) fprintf(stderr, "%s\n",
3045 if (cbp->cb_first && cbp->cb_recurse) {
3046 (void) fprintf(stderr, gettext("cannot rollback to "
3047 "'%s': clones of previous snapshots exist\n"),
3049 (void) fprintf(stderr, gettext("use '-R' to "
3050 "force deletion of the following clones and "
3056 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3064 zfs_do_rollback(int argc, char **argv)
3068 boolean_t force = B_FALSE;
3069 rollback_cbdata_t cb = { 0 };
3070 zfs_handle_t *zhp, *snap;
3071 char parentname[ZFS_MAXNAMELEN];
3075 while ((c = getopt(argc, argv, "rRf")) != -1) {
3088 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3097 /* check number of arguments */
3099 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3103 (void) fprintf(stderr, gettext("too many arguments\n"));
3107 /* open the snapshot */
3108 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3111 /* open the parent dataset */
3112 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3113 verify((delim = strrchr(parentname, '@')) != NULL);
3115 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3121 * Check for more recent snapshots and/or clones based on the presence
3124 cb.cb_target = argv[0];
3125 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3126 cb.cb_first = B_TRUE;
3128 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
3131 if ((ret = cb.cb_error) != 0)
3135 * Rollback parent to the given snapshot.
3137 ret = zfs_rollback(zhp, snap, force);
3150 * zfs set property=value { fs | snap | vol } ...
3152 * Sets the given property for all datasets specified on the command line.
3154 typedef struct set_cbdata {
3160 set_callback(zfs_handle_t *zhp, void *data)
3162 set_cbdata_t *cbp = data;
3164 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3165 switch (libzfs_errno(g_zfs)) {
3166 case EZFS_MOUNTFAILED:
3167 (void) fprintf(stderr, gettext("property may be set "
3168 "but unable to remount filesystem\n"));
3170 case EZFS_SHARENFSFAILED:
3171 (void) fprintf(stderr, gettext("property may be set "
3172 "but unable to reshare filesystem\n"));
3181 zfs_do_set(int argc, char **argv)
3186 /* check for options */
3187 if (argc > 1 && argv[1][0] == '-') {
3188 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3193 /* check number of arguments */
3195 (void) fprintf(stderr, gettext("missing property=value "
3200 (void) fprintf(stderr, gettext("missing dataset name\n"));
3204 /* validate property=value argument */
3205 cb.cb_propname = argv[1];
3206 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3207 (cb.cb_value[1] == '\0')) {
3208 (void) fprintf(stderr, gettext("missing value in "
3209 "property=value argument\n"));
3213 *cb.cb_value = '\0';
3216 if (*cb.cb_propname == '\0') {
3217 (void) fprintf(stderr,
3218 gettext("missing property in property=value argument\n"));
3222 ret = zfs_for_each(argc - 2, argv + 2, 0,
3223 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3229 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3231 * Creates a snapshot with the given name. While functionally equivalent to
3232 * 'zfs create', it is a separate command to differentiate intent.
3235 zfs_do_snapshot(int argc, char **argv)
3237 boolean_t recursive = B_FALSE;
3242 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3246 while ((c = getopt(argc, argv, "ro:")) != -1) {
3249 if (parseprop(props))
3256 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3265 /* check number of arguments */
3267 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3271 (void) fprintf(stderr, gettext("too many arguments\n"));
3275 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
3277 if (ret && recursive)
3278 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3288 * zfs send [-vDp] -R [-i|-I <@snap>] <fs@snap>
3289 * zfs send [-vDp] [-i|-I <@snap>] <fs@snap>
3291 * Send a backup stream to stdout.
3294 zfs_do_send(int argc, char **argv)
3296 char *fromname = NULL;
3297 char *toname = NULL;
3300 sendflags_t flags = { 0 };
3303 boolean_t extraverbose = B_FALSE;
3306 while ((c = getopt(argc, argv, ":i:I:RDpv")) != -1) {
3317 flags.doall = B_TRUE;
3320 flags.replicate = B_TRUE;
3323 flags.props = B_TRUE;
3327 extraverbose = B_TRUE;
3328 flags.verbose = B_TRUE;
3331 flags.dedup = B_TRUE;
3334 (void) fprintf(stderr, gettext("missing argument for "
3335 "'%c' option\n"), optopt);
3339 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3348 /* check number of arguments */
3350 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3354 (void) fprintf(stderr, gettext("too many arguments\n"));
3358 if (isatty(STDOUT_FILENO)) {
3359 (void) fprintf(stderr,
3360 gettext("Error: Stream can not be written to a terminal.\n"
3361 "You must redirect standard output.\n"));
3365 cp = strchr(argv[0], '@');
3367 (void) fprintf(stderr,
3368 gettext("argument must be a snapshot\n"));
3373 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3378 * If they specified the full path to the snapshot, chop off
3379 * everything except the short name of the snapshot, but special
3380 * case if they specify the origin.
3382 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3383 char origin[ZFS_MAXNAMELEN];
3386 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3387 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3389 if (strcmp(origin, fromname) == 0) {
3391 flags.fromorigin = B_TRUE;
3394 if (cp != fromname && strcmp(argv[0], fromname)) {
3395 (void) fprintf(stderr,
3396 gettext("incremental source must be "
3397 "in same filesystem\n"));
3401 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3402 (void) fprintf(stderr,
3403 gettext("invalid incremental source\n"));
3409 if (flags.replicate && fromname == NULL)
3410 flags.doall = B_TRUE;
3412 err = zfs_send(zhp, fromname, toname, flags, STDOUT_FILENO, NULL, 0,
3413 extraverbose ? &dbgnv : NULL);
3417 * dump_nvlist prints to stdout, but that's been
3418 * redirected to a file. Make it print to stderr
3421 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3422 dump_nvlist(dbgnv, 0);
3431 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3433 * Restore a backup stream from stdin.
3436 zfs_do_receive(int argc, char **argv)
3439 recvflags_t flags = { 0 };
3442 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3445 flags.isprefix = B_TRUE;
3448 flags.isprefix = B_TRUE;
3449 flags.istail = B_TRUE;
3452 flags.dryrun = B_TRUE;
3455 flags.nomount = B_TRUE;
3458 flags.verbose = B_TRUE;
3461 flags.force = B_TRUE;
3464 (void) fprintf(stderr, gettext("missing argument for "
3465 "'%c' option\n"), optopt);
3469 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3478 /* check number of arguments */
3480 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3484 (void) fprintf(stderr, gettext("too many arguments\n"));
3488 if (isatty(STDIN_FILENO)) {
3489 (void) fprintf(stderr,
3490 gettext("Error: Backup stream can not be read "
3491 "from a terminal.\n"
3492 "You must redirect standard input.\n"));
3496 err = zfs_receive(g_zfs, argv[0], flags, STDIN_FILENO, NULL);
3502 * allow/unallow stuff
3504 /* copied from zfs/sys/dsl_deleg.h */
3505 #define ZFS_DELEG_PERM_CREATE "create"
3506 #define ZFS_DELEG_PERM_DESTROY "destroy"
3507 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3508 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3509 #define ZFS_DELEG_PERM_CLONE "clone"
3510 #define ZFS_DELEG_PERM_PROMOTE "promote"
3511 #define ZFS_DELEG_PERM_RENAME "rename"
3512 #define ZFS_DELEG_PERM_MOUNT "mount"
3513 #define ZFS_DELEG_PERM_SHARE "share"
3514 #define ZFS_DELEG_PERM_SEND "send"
3515 #define ZFS_DELEG_PERM_RECEIVE "receive"
3516 #define ZFS_DELEG_PERM_ALLOW "allow"
3517 #define ZFS_DELEG_PERM_USERPROP "userprop"
3518 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3519 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3520 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3521 #define ZFS_DELEG_PERM_USERUSED "userused"
3522 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3523 #define ZFS_DELEG_PERM_HOLD "hold"
3524 #define ZFS_DELEG_PERM_RELEASE "release"
3525 #define ZFS_DELEG_PERM_DIFF "diff"
3527 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3529 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3530 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3531 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3532 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3533 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3534 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3535 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3536 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3537 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3538 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3539 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3540 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3541 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3542 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3543 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3544 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3546 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3547 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3548 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3549 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3550 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3551 { NULL, ZFS_DELEG_NOTE_NONE }
3554 /* permission structure */
3555 typedef struct deleg_perm {
3556 zfs_deleg_who_type_t dp_who_type;
3557 const char *dp_name;
3559 boolean_t dp_descend;
3563 typedef struct deleg_perm_node {
3564 deleg_perm_t dpn_perm;
3566 uu_avl_node_t dpn_avl_node;
3567 } deleg_perm_node_t;
3569 typedef struct fs_perm fs_perm_t;
3571 /* permissions set */
3572 typedef struct who_perm {
3573 zfs_deleg_who_type_t who_type;
3574 const char *who_name; /* id */
3575 char who_ug_name[256]; /* user/group name */
3576 fs_perm_t *who_fsperm; /* uplink */
3578 uu_avl_t *who_deleg_perm_avl; /* permissions */
3582 typedef struct who_perm_node {
3583 who_perm_t who_perm;
3584 uu_avl_node_t who_avl_node;
3587 typedef struct fs_perm_set fs_perm_set_t;
3588 /* fs permissions */
3590 const char *fsp_name;
3592 uu_avl_t *fsp_sc_avl; /* sets,create */
3593 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3595 fs_perm_set_t *fsp_set; /* uplink */
3599 typedef struct fs_perm_node {
3600 fs_perm_t fspn_fsperm;
3603 uu_list_node_t fspn_list_node;
3606 /* top level structure */
3607 struct fs_perm_set {
3608 uu_list_pool_t *fsps_list_pool;
3609 uu_list_t *fsps_list; /* list of fs_perms */
3611 uu_avl_pool_t *fsps_named_set_avl_pool;
3612 uu_avl_pool_t *fsps_who_perm_avl_pool;
3613 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
3616 static inline const char *
3617 deleg_perm_type(zfs_deleg_note_t note)
3623 case ZFS_DELEG_NOTE_GROUPQUOTA:
3624 case ZFS_DELEG_NOTE_GROUPUSED:
3625 case ZFS_DELEG_NOTE_USERPROP:
3626 case ZFS_DELEG_NOTE_USERQUOTA:
3627 case ZFS_DELEG_NOTE_USERUSED:
3629 return (gettext("other"));
3631 return (gettext("subcommand"));
3636 who_type2weight(zfs_deleg_who_type_t who_type)
3640 case ZFS_DELEG_NAMED_SET_SETS:
3641 case ZFS_DELEG_NAMED_SET:
3644 case ZFS_DELEG_CREATE_SETS:
3645 case ZFS_DELEG_CREATE:
3648 case ZFS_DELEG_USER_SETS:
3649 case ZFS_DELEG_USER:
3652 case ZFS_DELEG_GROUP_SETS:
3653 case ZFS_DELEG_GROUP:
3656 case ZFS_DELEG_EVERYONE_SETS:
3657 case ZFS_DELEG_EVERYONE:
3669 who_perm_compare(const void *larg, const void *rarg, void *unused)
3671 const who_perm_node_t *l = larg;
3672 const who_perm_node_t *r = rarg;
3673 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
3674 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
3675 int lweight = who_type2weight(ltype);
3676 int rweight = who_type2weight(rtype);
3677 int res = lweight - rweight;
3679 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
3680 ZFS_MAX_DELEG_NAME-1);
3692 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
3694 const deleg_perm_node_t *l = larg;
3695 const deleg_perm_node_t *r = rarg;
3696 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
3697 ZFS_MAX_DELEG_NAME-1);
3709 fs_perm_set_init(fs_perm_set_t *fspset)
3711 bzero(fspset, sizeof (fs_perm_set_t));
3713 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
3714 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
3715 NULL, UU_DEFAULT)) == NULL)
3717 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
3718 UU_DEFAULT)) == NULL)
3721 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
3722 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
3723 who_perm_node_t, who_avl_node), who_perm_compare,
3724 UU_DEFAULT)) == NULL)
3727 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
3728 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
3729 who_perm_node_t, who_avl_node), who_perm_compare,
3730 UU_DEFAULT)) == NULL)
3733 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
3734 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
3735 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
3740 static inline void fs_perm_fini(fs_perm_t *);
3741 static inline void who_perm_fini(who_perm_t *);
3744 fs_perm_set_fini(fs_perm_set_t *fspset)
3746 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
3748 while (node != NULL) {
3749 fs_perm_node_t *next_node =
3750 uu_list_next(fspset->fsps_list, node);
3751 fs_perm_t *fsperm = &node->fspn_fsperm;
3752 fs_perm_fini(fsperm);
3753 uu_list_remove(fspset->fsps_list, node);
3758 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
3759 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
3760 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
3764 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
3767 deleg_perm->dp_who_type = type;
3768 deleg_perm->dp_name = name;
3772 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
3773 zfs_deleg_who_type_t type, const char *name)
3775 uu_avl_pool_t *pool;
3776 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
3778 bzero(who_perm, sizeof (who_perm_t));
3780 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
3781 UU_DEFAULT)) == NULL)
3784 who_perm->who_type = type;
3785 who_perm->who_name = name;
3786 who_perm->who_fsperm = fsperm;
3790 who_perm_fini(who_perm_t *who_perm)
3792 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
3794 while (node != NULL) {
3795 deleg_perm_node_t *next_node =
3796 uu_avl_next(who_perm->who_deleg_perm_avl, node);
3798 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
3803 uu_avl_destroy(who_perm->who_deleg_perm_avl);
3807 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
3809 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
3810 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
3812 bzero(fsperm, sizeof (fs_perm_t));
3814 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
3818 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
3822 fsperm->fsp_set = fspset;
3823 fsperm->fsp_name = fsname;
3827 fs_perm_fini(fs_perm_t *fsperm)
3829 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
3830 while (node != NULL) {
3831 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
3833 who_perm_t *who_perm = &node->who_perm;
3834 who_perm_fini(who_perm);
3835 uu_avl_remove(fsperm->fsp_sc_avl, node);
3840 node = uu_avl_first(fsperm->fsp_uge_avl);
3841 while (node != NULL) {
3842 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
3844 who_perm_t *who_perm = &node->who_perm;
3845 who_perm_fini(who_perm);
3846 uu_avl_remove(fsperm->fsp_uge_avl, node);
3851 uu_avl_destroy(fsperm->fsp_sc_avl);
3852 uu_avl_destroy(fsperm->fsp_uge_avl);
3856 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
3857 zfs_deleg_who_type_t who_type, const char *name, char locality)
3859 uu_avl_index_t idx = 0;
3861 deleg_perm_node_t *found_node = NULL;
3862 deleg_perm_t *deleg_perm = &node->dpn_perm;
3864 deleg_perm_init(deleg_perm, who_type, name);
3866 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
3868 uu_avl_insert(avl, node, idx);
3871 deleg_perm = &node->dpn_perm;
3876 case ZFS_DELEG_LOCAL:
3877 deleg_perm->dp_local = B_TRUE;
3879 case ZFS_DELEG_DESCENDENT:
3880 deleg_perm->dp_descend = B_TRUE;
3885 assert(B_FALSE); /* invalid locality */
3890 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
3892 nvpair_t *nvp = NULL;
3893 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
3894 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
3895 zfs_deleg_who_type_t who_type = who_perm->who_type;
3897 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
3898 const char *name = nvpair_name(nvp);
3899 data_type_t type = nvpair_type(nvp);
3900 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
3901 deleg_perm_node_t *node =
3902 safe_malloc(sizeof (deleg_perm_node_t));
3904 assert(type == DATA_TYPE_BOOLEAN);
3906 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
3907 set_deleg_perm_node(avl, node, who_type, name, locality);
3914 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
3916 nvpair_t *nvp = NULL;
3917 fs_perm_set_t *fspset = fsperm->fsp_set;
3919 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
3920 nvlist_t *nvl2 = NULL;
3921 const char *name = nvpair_name(nvp);
3922 uu_avl_t *avl = NULL;
3923 uu_avl_pool_t *avl_pool;
3924 zfs_deleg_who_type_t perm_type = name[0];
3925 char perm_locality = name[1];
3926 const char *perm_name = name + 3;
3927 boolean_t is_set = B_TRUE;
3928 who_perm_t *who_perm = NULL;
3930 assert('$' == name[2]);
3932 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
3935 switch (perm_type) {
3936 case ZFS_DELEG_CREATE:
3937 case ZFS_DELEG_CREATE_SETS:
3938 case ZFS_DELEG_NAMED_SET:
3939 case ZFS_DELEG_NAMED_SET_SETS:
3940 avl_pool = fspset->fsps_named_set_avl_pool;
3941 avl = fsperm->fsp_sc_avl;
3943 case ZFS_DELEG_USER:
3944 case ZFS_DELEG_USER_SETS:
3945 case ZFS_DELEG_GROUP:
3946 case ZFS_DELEG_GROUP_SETS:
3947 case ZFS_DELEG_EVERYONE:
3948 case ZFS_DELEG_EVERYONE_SETS:
3949 avl_pool = fspset->fsps_who_perm_avl_pool;
3950 avl = fsperm->fsp_uge_avl;
3955 who_perm_node_t *found_node = NULL;
3956 who_perm_node_t *node = safe_malloc(
3957 sizeof (who_perm_node_t));
3958 who_perm = &node->who_perm;
3959 uu_avl_index_t idx = 0;
3961 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
3962 who_perm_init(who_perm, fsperm, perm_type, perm_name);
3964 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
3966 if (avl == fsperm->fsp_uge_avl) {
3968 struct passwd *p = NULL;
3969 struct group *g = NULL;
3970 const char *nice_name = NULL;
3972 switch (perm_type) {
3973 case ZFS_DELEG_USER_SETS:
3974 case ZFS_DELEG_USER:
3975 rid = atoi(perm_name);
3978 nice_name = p->pw_name;
3980 case ZFS_DELEG_GROUP_SETS:
3981 case ZFS_DELEG_GROUP:
3982 rid = atoi(perm_name);
3985 nice_name = g->gr_name;
3989 if (nice_name != NULL)
3991 node->who_perm.who_ug_name,
3995 uu_avl_insert(avl, node, idx);
3998 who_perm = &node->who_perm;
4002 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4009 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4011 nvpair_t *nvp = NULL;
4012 uu_avl_index_t idx = 0;
4014 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4015 nvlist_t *nvl2 = NULL;
4016 const char *fsname = nvpair_name(nvp);
4017 data_type_t type = nvpair_type(nvp);
4018 fs_perm_t *fsperm = NULL;
4019 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4023 fsperm = &node->fspn_fsperm;
4025 assert(DATA_TYPE_NVLIST == type);
4027 uu_list_node_init(node, &node->fspn_list_node,
4028 fspset->fsps_list_pool);
4030 idx = uu_list_numnodes(fspset->fsps_list);
4031 fs_perm_init(fsperm, fspset, fsname);
4033 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4036 (void) parse_fs_perm(fsperm, nvl2);
4038 uu_list_insert(fspset->fsps_list, node, idx);
4044 static inline const char *
4045 deleg_perm_comment(zfs_deleg_note_t note)
4047 const char *str = "";
4052 case ZFS_DELEG_NOTE_ALLOW:
4053 str = gettext("Must also have the permission that is being"
4054 "\n\t\t\t\tallowed");
4056 case ZFS_DELEG_NOTE_CLONE:
4057 str = gettext("Must also have the 'create' ability and 'mount'"
4058 "\n\t\t\t\tability in the origin file system");
4060 case ZFS_DELEG_NOTE_CREATE:
4061 str = gettext("Must also have the 'mount' ability");
4063 case ZFS_DELEG_NOTE_DESTROY:
4064 str = gettext("Must also have the 'mount' ability");
4066 case ZFS_DELEG_NOTE_DIFF:
4067 str = gettext("Allows lookup of paths within a dataset;"
4068 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4069 "\n\t\t\t\tin order to use zfs diff");
4071 case ZFS_DELEG_NOTE_HOLD:
4072 str = gettext("Allows adding a user hold to a snapshot");
4074 case ZFS_DELEG_NOTE_MOUNT:
4075 str = gettext("Allows mount/umount of ZFS datasets");
4077 case ZFS_DELEG_NOTE_PROMOTE:
4078 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4079 " 'promote' ability in the origin file system");
4081 case ZFS_DELEG_NOTE_RECEIVE:
4082 str = gettext("Must also have the 'mount' and 'create'"
4085 case ZFS_DELEG_NOTE_RELEASE:
4086 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4087 "might destroy the snapshot");
4089 case ZFS_DELEG_NOTE_RENAME:
4090 str = gettext("Must also have the 'mount' and 'create'"
4091 "\n\t\t\t\tability in the new parent");
4093 case ZFS_DELEG_NOTE_ROLLBACK:
4096 case ZFS_DELEG_NOTE_SEND:
4099 case ZFS_DELEG_NOTE_SHARE:
4100 str = gettext("Allows sharing file systems over NFS or SMB"
4101 "\n\t\t\t\tprotocols");
4103 case ZFS_DELEG_NOTE_SNAPSHOT:
4107 * case ZFS_DELEG_NOTE_VSCAN:
4108 * str = gettext("");
4112 case ZFS_DELEG_NOTE_GROUPQUOTA:
4113 str = gettext("Allows accessing any groupquota@... property");
4115 case ZFS_DELEG_NOTE_GROUPUSED:
4116 str = gettext("Allows reading any groupused@... property");
4118 case ZFS_DELEG_NOTE_USERPROP:
4119 str = gettext("Allows changing any user property");
4121 case ZFS_DELEG_NOTE_USERQUOTA:
4122 str = gettext("Allows accessing any userquota@... property");
4124 case ZFS_DELEG_NOTE_USERUSED:
4125 str = gettext("Allows reading any userused@... property");
4143 boolean_t recursive; /* unallow only */
4144 boolean_t prt_usage;
4146 boolean_t prt_perms;
4149 const char *dataset;
4153 prop_cmp(const void *a, const void *b)
4155 const char *str1 = *(const char **)a;
4156 const char *str2 = *(const char **)b;
4157 return (strcmp(str1, str2));
4161 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4163 const char *opt_desc[] = {
4164 "-h", gettext("show this help message and exit"),
4165 "-l", gettext("set permission locally"),
4166 "-d", gettext("set permission for descents"),
4167 "-u", gettext("set permission for user"),
4168 "-g", gettext("set permission for group"),
4169 "-e", gettext("set permission for everyone"),
4170 "-c", gettext("set create time permission"),
4171 "-s", gettext("define permission set"),
4173 "-r", gettext("remove permissions recursively"),
4175 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4176 size_t allow_size = unallow_size - 2;
4177 const char *props[ZFS_NUM_PROPS];
4180 FILE *fp = requested ? stdout : stderr;
4181 zprop_desc_t *pdtbl = zfs_prop_get_table();
4182 const char *fmt = gettext("%-16s %-14s\t%s\n");
4184 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4186 (void) fprintf(fp, gettext("Options:\n"));
4187 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4188 const char *opt = opt_desc[i++];
4189 const char *optdsc = opt_desc[i];
4190 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4193 (void) fprintf(fp, gettext("\nThe following permissions are "
4195 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4197 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4198 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4199 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4200 const char *perm_type = deleg_perm_type(perm_note);
4201 const char *perm_comment = deleg_perm_comment(perm_note);
4202 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4205 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4206 zprop_desc_t *pd = &pdtbl[i];
4207 if (pd->pd_visible != B_TRUE)
4210 if (pd->pd_attr == PROP_READONLY)
4213 props[count++] = pd->pd_name;
4215 props[count] = NULL;
4217 qsort(props, count, sizeof (char *), prop_cmp);
4219 for (i = 0; i < count; i++)
4220 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4223 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4225 exit(requested ? 0 : 2);
4228 static inline const char *
4229 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4232 if (un && argc == expected_argc - 1)
4234 else if (argc == expected_argc)
4235 *permsp = argv[argc - 2];
4237 allow_usage(un, B_FALSE,
4238 gettext("wrong number of parameters\n"));
4240 return (argv[argc - 1]);
4244 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4246 int uge_sum = opts->user + opts->group + opts->everyone;
4247 int csuge_sum = opts->create + opts->set + uge_sum;
4248 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4249 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4252 allow_usage(un, B_FALSE,
4253 gettext("-u, -g, and -e are mutually exclusive\n"));
4255 if (opts->prt_usage)
4256 if (argc == 0 && all_sum == 0)
4257 allow_usage(un, B_TRUE, NULL);
4263 allow_usage(un, B_FALSE,
4264 gettext("invalid options combined with -s\n"));
4266 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4267 if (argv[0][0] != '@')
4268 allow_usage(un, B_FALSE,
4269 gettext("invalid set name: missing '@' prefix\n"));
4270 opts->who = argv[0];
4271 } else if (opts->create) {
4272 if (ldcsuge_sum > 1)
4273 allow_usage(un, B_FALSE,
4274 gettext("invalid options combined with -c\n"));
4275 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4276 } else if (opts->everyone) {
4278 allow_usage(un, B_FALSE,
4279 gettext("invalid options combined with -e\n"));
4280 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4281 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4283 opts->everyone = B_TRUE;
4286 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4287 } else if (argc == 1) {
4288 opts->prt_perms = B_TRUE;
4289 opts->dataset = argv[argc-1];
4291 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4292 opts->who = argv[0];
4295 if (!opts->local && !opts->descend) {
4296 opts->local = B_TRUE;
4297 opts->descend = B_TRUE;
4302 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4303 const char *who, char *perms, nvlist_t *top_nvl)
4306 char ld[2] = { '\0', '\0' };
4307 char who_buf[ZFS_MAXNAMELEN+32];
4310 nvlist_t *base_nvl = NULL;
4311 nvlist_t *set_nvl = NULL;
4314 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4316 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4320 case ZFS_DELEG_NAMED_SET_SETS:
4321 case ZFS_DELEG_NAMED_SET:
4322 set_type = ZFS_DELEG_NAMED_SET_SETS;
4323 base_type = ZFS_DELEG_NAMED_SET;
4324 ld[0] = ZFS_DELEG_NA;
4326 case ZFS_DELEG_CREATE_SETS:
4327 case ZFS_DELEG_CREATE:
4328 set_type = ZFS_DELEG_CREATE_SETS;
4329 base_type = ZFS_DELEG_CREATE;
4330 ld[0] = ZFS_DELEG_NA;
4332 case ZFS_DELEG_USER_SETS:
4333 case ZFS_DELEG_USER:
4334 set_type = ZFS_DELEG_USER_SETS;
4335 base_type = ZFS_DELEG_USER;
4337 ld[0] = ZFS_DELEG_LOCAL;
4339 ld[1] = ZFS_DELEG_DESCENDENT;
4341 case ZFS_DELEG_GROUP_SETS:
4342 case ZFS_DELEG_GROUP:
4343 set_type = ZFS_DELEG_GROUP_SETS;
4344 base_type = ZFS_DELEG_GROUP;
4346 ld[0] = ZFS_DELEG_LOCAL;
4348 ld[1] = ZFS_DELEG_DESCENDENT;
4350 case ZFS_DELEG_EVERYONE_SETS:
4351 case ZFS_DELEG_EVERYONE:
4352 set_type = ZFS_DELEG_EVERYONE_SETS;
4353 base_type = ZFS_DELEG_EVERYONE;
4355 ld[0] = ZFS_DELEG_LOCAL;
4357 ld[1] = ZFS_DELEG_DESCENDENT;
4360 if (perms != NULL) {
4362 char *end = curr + strlen(perms);
4364 while (curr < end) {
4365 char *delim = strchr(curr, ',');
4376 (void) nvlist_add_boolean(nvl, curr);
4382 for (i = 0; i < 2; i++) {
4383 char locality = ld[i];
4387 if (!nvlist_empty(base_nvl)) {
4389 (void) snprintf(who_buf,
4390 sizeof (who_buf), "%c%c$%s",
4391 base_type, locality, who);
4393 (void) snprintf(who_buf,
4394 sizeof (who_buf), "%c%c$",
4395 base_type, locality);
4397 (void) nvlist_add_nvlist(top_nvl, who_buf,
4402 if (!nvlist_empty(set_nvl)) {
4404 (void) snprintf(who_buf,
4405 sizeof (who_buf), "%c%c$%s",
4406 set_type, locality, who);
4408 (void) snprintf(who_buf,
4409 sizeof (who_buf), "%c%c$",
4410 set_type, locality);
4412 (void) nvlist_add_nvlist(top_nvl, who_buf,
4417 for (i = 0; i < 2; i++) {
4418 char locality = ld[i];
4423 (void) snprintf(who_buf, sizeof (who_buf),
4424 "%c%c$%s", base_type, locality, who);
4426 (void) snprintf(who_buf, sizeof (who_buf),
4427 "%c%c$", base_type, locality);
4428 (void) nvlist_add_boolean(top_nvl, who_buf);
4431 (void) snprintf(who_buf, sizeof (who_buf),
4432 "%c%c$%s", set_type, locality, who);
4434 (void) snprintf(who_buf, sizeof (who_buf),
4435 "%c%c$", set_type, locality);
4436 (void) nvlist_add_boolean(top_nvl, who_buf);
4442 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4444 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4448 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4449 opts->descend, opts->who, opts->perms, *nvlp);
4450 } else if (opts->create) {
4451 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4452 opts->descend, NULL, opts->perms, *nvlp);
4453 } else if (opts->everyone) {
4454 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4455 opts->descend, NULL, opts->perms, *nvlp);
4457 char *curr = opts->who;
4458 char *end = curr + strlen(curr);
4460 while (curr < end) {
4462 zfs_deleg_who_type_t who_type;
4464 char *delim = strchr(curr, ',');
4467 struct passwd *p = NULL;
4468 struct group *g = NULL;
4476 rid = (uid_t)strtol(curr, &endch, 0);
4478 who_type = ZFS_DELEG_USER;
4487 (void) snprintf(errbuf, 256, gettext(
4488 "invalid user %s"), curr);
4489 allow_usage(un, B_TRUE, errbuf);
4491 } else if (opts->group) {
4492 who_type = ZFS_DELEG_GROUP;
4501 (void) snprintf(errbuf, 256, gettext(
4502 "invalid group %s"), curr);
4503 allow_usage(un, B_TRUE, errbuf);
4506 if (*endch != '\0') {
4513 if (*endch != '\0') {
4520 who_type = ZFS_DELEG_USER;
4522 } else if (g != NULL) {
4523 who_type = ZFS_DELEG_GROUP;
4526 (void) snprintf(errbuf, 256, gettext(
4527 "invalid user/group %s"), curr);
4528 allow_usage(un, B_TRUE, errbuf);
4532 (void) sprintf(id, "%u", rid);
4535 store_allow_perm(who_type, opts->local,
4536 opts->descend, who, opts->perms, *nvlp);
4545 print_set_creat_perms(uu_avl_t *who_avl)
4547 const char *sc_title[] = {
4548 gettext("Permission sets:\n"),
4549 gettext("Create time permissions:\n"),
4552 const char **title_ptr = sc_title;
4553 who_perm_node_t *who_node = NULL;
4554 int prev_weight = -1;
4556 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4557 who_node = uu_avl_next(who_avl, who_node)) {
4558 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4559 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4560 const char *who_name = who_node->who_perm.who_name;
4561 int weight = who_type2weight(who_type);
4562 boolean_t first = B_TRUE;
4563 deleg_perm_node_t *deleg_node;
4565 if (prev_weight != weight) {
4566 (void) printf(*title_ptr++);
4567 prev_weight = weight;
4570 if (who_name == NULL || strnlen(who_name, 1) == 0)
4571 (void) printf("\t");
4573 (void) printf("\t%s ", who_name);
4575 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4576 deleg_node = uu_avl_next(avl, deleg_node)) {
4579 deleg_node->dpn_perm.dp_name);
4582 (void) printf(",%s",
4583 deleg_node->dpn_perm.dp_name);
4586 (void) printf("\n");
4591 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4594 who_perm_node_t *who_node = NULL;
4595 boolean_t prt_title = B_TRUE;
4596 uu_avl_walk_t *walk;
4598 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4601 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4602 const char *who_name = who_node->who_perm.who_name;
4603 const char *nice_who_name = who_node->who_perm.who_ug_name;
4604 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4605 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4607 deleg_perm_node_t *deleg_node;
4608 boolean_t prt_who = B_TRUE;
4610 for (deleg_node = uu_avl_first(avl);
4612 deleg_node = uu_avl_next(avl, deleg_node)) {
4613 if (local != deleg_node->dpn_perm.dp_local ||
4614 descend != deleg_node->dpn_perm.dp_descend)
4618 const char *who = NULL;
4620 prt_title = B_FALSE;
4621 (void) printf(title);
4625 case ZFS_DELEG_USER_SETS:
4626 case ZFS_DELEG_USER:
4627 who = gettext("user");
4629 who_name = nice_who_name;
4631 case ZFS_DELEG_GROUP_SETS:
4632 case ZFS_DELEG_GROUP:
4633 who = gettext("group");
4635 who_name = nice_who_name;
4637 case ZFS_DELEG_EVERYONE_SETS:
4638 case ZFS_DELEG_EVERYONE:
4639 who = gettext("everyone");
4644 if (who_name == NULL)
4645 (void) printf("\t%s", who);
4647 (void) printf("\t%s %s", who, who_name);
4650 (void) printf("%c%s", delim,
4651 deleg_node->dpn_perm.dp_name);
4656 (void) printf("\n");
4659 uu_avl_walk_end(walk);
4663 print_fs_perms(fs_perm_set_t *fspset)
4665 fs_perm_node_t *node = NULL;
4666 char buf[ZFS_MAXNAMELEN+32];
4667 const char *dsname = buf;
4669 for (node = uu_list_first(fspset->fsps_list); node != NULL;
4670 node = uu_list_next(fspset->fsps_list, node)) {
4671 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
4672 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
4675 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
4676 gettext("---- Permissions on %s "),
4677 node->fspn_fsperm.fsp_name);
4678 (void) printf(dsname);
4679 left = 70 - strlen(buf);
4682 (void) printf("\n");
4684 print_set_creat_perms(sc_avl);
4685 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
4686 gettext("Local permissions:\n"));
4687 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
4688 gettext("Descendent permissions:\n"));
4689 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
4690 gettext("Local+Descendent permissions:\n"));
4694 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
4696 struct deleg_perms {
4702 set_deleg_perms(zfs_handle_t *zhp, void *data)
4704 struct deleg_perms *perms = (struct deleg_perms *)data;
4705 zfs_type_t zfs_type = zfs_get_type(zhp);
4707 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
4710 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
4714 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
4717 nvlist_t *perm_nvl = NULL;
4718 nvlist_t *update_perm_nvl = NULL;
4721 struct allow_opts opts = { 0 };
4723 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
4726 while ((c = getopt(argc, argv, optstr)) != -1) {
4729 opts.local = B_TRUE;
4732 opts.descend = B_TRUE;
4738 opts.group = B_TRUE;
4741 opts.everyone = B_TRUE;
4747 opts.create = B_TRUE;
4750 opts.recursive = B_TRUE;
4753 (void) fprintf(stderr, gettext("missing argument for "
4754 "'%c' option\n"), optopt);
4758 opts.prt_usage = B_TRUE;
4761 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4770 /* check arguments */
4771 parse_allow_args(argc, argv, un, &opts);
4773 /* try to open the dataset */
4774 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM))
4776 (void) fprintf(stderr, "Failed to open Dataset *%s*\n",
4781 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
4784 fs_perm_set_init(&fs_perm_set);
4785 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
4786 (void) fprintf(stderr, "Failed to parse fsacl permissionsn");
4791 print_fs_perms(&fs_perm_set);
4793 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
4794 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
4797 if (un && opts.recursive) {
4798 struct deleg_perms data = { un, update_perm_nvl };
4799 if (zfs_iter_filesystems(zhp, set_deleg_perms,
4808 nvlist_free(perm_nvl);
4809 if (update_perm_nvl != NULL)
4810 nvlist_free(update_perm_nvl);
4812 fs_perm_set_fini(&fs_perm_set);
4820 * zfs allow [-r] [-t] <tag> <snap> ...
4822 * -r Recursively hold
4823 * -t Temporary hold (hidden option)
4825 * Apply a user-hold with the given tag to the list of snapshots.
4828 zfs_do_allow(int argc, char **argv)
4830 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
4834 * zfs unallow [-r] [-t] <tag> <snap> ...
4836 * -r Recursively hold
4837 * -t Temporary hold (hidden option)
4839 * Apply a user-hold with the given tag to the list of snapshots.
4842 zfs_do_unallow(int argc, char **argv)
4844 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
4848 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
4853 boolean_t recursive = B_FALSE;
4854 boolean_t temphold = B_FALSE;
4855 const char *opts = holding ? "rt" : "r";
4859 while ((c = getopt(argc, argv, opts)) != -1) {
4868 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4877 /* check number of arguments */
4885 if (holding && tag[0] == '.') {
4886 /* tags starting with '.' are reserved for libzfs */
4887 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
4891 for (i = 0; i < argc; ++i) {
4893 char parent[ZFS_MAXNAMELEN];
4895 char *path = argv[i];
4897 delim = strchr(path, '@');
4898 if (delim == NULL) {
4899 (void) fprintf(stderr,
4900 gettext("'%s' is not a snapshot\n"), path);
4904 (void) strncpy(parent, path, delim - path);
4905 parent[delim - path] = '\0';
4907 zhp = zfs_open(g_zfs, parent,
4908 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4914 if (zfs_hold(zhp, delim+1, tag, recursive,
4915 temphold, B_FALSE, -1, 0, 0) != 0)
4918 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
4924 return (errors != 0);
4928 * zfs hold [-r] [-t] <tag> <snap> ...
4930 * -r Recursively hold
4931 * -t Temporary hold (hidden option)
4933 * Apply a user-hold with the given tag to the list of snapshots.
4936 zfs_do_hold(int argc, char **argv)
4938 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
4942 * zfs release [-r] <tag> <snap> ...
4944 * -r Recursively release
4946 * Release a user-hold with the given tag from the list of snapshots.
4949 zfs_do_release(int argc, char **argv)
4951 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
4954 typedef struct holds_cbdata {
4955 boolean_t cb_recursive;
4956 const char *cb_snapname;
4958 size_t cb_max_namelen;
4959 size_t cb_max_taglen;
4962 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
4963 #define DATETIME_BUF_LEN (32)
4968 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
4971 nvpair_t *nvp = NULL;
4972 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
4976 for (i = 0; i < 3; i++) {
4977 col = gettext(hdr_cols[i]);
4979 (void) printf("%-*s ", i ? tagwidth : nwidth,
4982 (void) printf("%s\n", col);
4986 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4987 char *zname = nvpair_name(nvp);
4989 nvpair_t *nvp2 = NULL;
4990 (void) nvpair_value_nvlist(nvp, &nvl2);
4991 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
4992 char tsbuf[DATETIME_BUF_LEN];
4993 char *tagname = nvpair_name(nvp2);
4997 char sep = scripted ? '\t' : ' ';
4998 size_t sepnum = scripted ? 1 : 2;
5000 (void) nvpair_value_uint64(nvp2, &val);
5002 (void) localtime_r(&time, &t);
5003 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5004 gettext(STRFTIME_FMT_STR), &t);
5006 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5007 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5013 * Generic callback function to list a dataset or snapshot.
5016 holds_callback(zfs_handle_t *zhp, void *data)
5018 holds_cbdata_t *cbp = data;
5019 nvlist_t *top_nvl = *cbp->cb_nvlp;
5020 nvlist_t *nvl = NULL;
5021 nvpair_t *nvp = NULL;
5022 const char *zname = zfs_get_name(zhp);
5023 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5025 if (cbp->cb_recursive) {
5026 const char *snapname;
5027 char *delim = strchr(zname, '@');
5031 snapname = delim + 1;
5032 if (strcmp(cbp->cb_snapname, snapname))
5036 if (zfs_get_holds(zhp, &nvl) != 0)
5039 if (znamelen > cbp->cb_max_namelen)
5040 cbp->cb_max_namelen = znamelen;
5042 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5043 const char *tag = nvpair_name(nvp);
5044 size_t taglen = strnlen(tag, MAXNAMELEN);
5045 if (taglen > cbp->cb_max_taglen)
5046 cbp->cb_max_taglen = taglen;
5049 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5053 * zfs holds [-r] <snap> ...
5055 * -r Recursively hold
5058 zfs_do_holds(int argc, char **argv)
5063 boolean_t scripted = B_FALSE;
5064 boolean_t recursive = B_FALSE;
5065 const char *opts = "rH";
5068 int types = ZFS_TYPE_SNAPSHOT;
5069 holds_cbdata_t cb = { 0 };
5076 while ((c = getopt(argc, argv, opts)) != -1) {
5085 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5092 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5093 flags |= ZFS_ITER_RECURSE;
5099 /* check number of arguments */
5103 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5106 for (i = 0; i < argc; ++i) {
5107 char *snapshot = argv[i];
5109 const char *snapname;
5111 delim = strchr(snapshot, '@');
5112 if (delim == NULL) {
5113 (void) fprintf(stderr,
5114 gettext("'%s' is not a snapshot\n"), snapshot);
5118 snapname = delim + 1;
5120 snapshot[delim - snapshot] = '\0';
5122 cb.cb_recursive = recursive;
5123 cb.cb_snapname = snapname;
5127 * 1. collect holds data, set format options
5129 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5130 holds_callback, &cb);
5136 * 2. print holds data
5138 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5140 if (nvlist_empty(nvl))
5141 (void) printf(gettext("no datasets available\n"));
5145 return (0 != errors);
5148 #define CHECK_SPINNER 30
5149 #define SPINNER_TIME 3 /* seconds */
5150 #define MOUNT_TIME 5 /* seconds */
5153 get_one_dataset(zfs_handle_t *zhp, void *data)
5155 static char *spin[] = { "-", "\\", "|", "/" };
5156 static int spinval = 0;
5157 static int spincheck = 0;
5158 static time_t last_spin_time = (time_t)0;
5159 get_all_cb_t *cbp = data;
5160 zfs_type_t type = zfs_get_type(zhp);
5162 if (cbp->cb_verbose) {
5163 if (--spincheck < 0) {
5164 time_t now = time(NULL);
5165 if (last_spin_time + SPINNER_TIME < now) {
5166 update_progress(spin[spinval++ % 4]);
5167 last_spin_time = now;
5169 spincheck = CHECK_SPINNER;
5174 * Interate over any nested datasets.
5176 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5182 * Skip any datasets whose type does not match.
5184 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5188 libzfs_add_handle(cbp, zhp);
5189 assert(cbp->cb_used <= cbp->cb_alloc);
5195 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5197 get_all_cb_t cb = { 0 };
5198 cb.cb_verbose = verbose;
5199 cb.cb_getone = get_one_dataset;
5202 set_progress_header(gettext("Reading ZFS config"));
5203 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5205 *dslist = cb.cb_handles;
5206 *count = cb.cb_used;
5209 finish_progress(gettext("done."));
5213 * Generic callback for sharing or mounting filesystems. Because the code is so
5214 * similar, we have a common function with an extra parameter to determine which
5215 * mode we are using.
5217 #define OP_SHARE 0x1
5218 #define OP_MOUNT 0x2
5221 * Share or mount a dataset.
5224 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5225 boolean_t explicit, const char *options)
5227 char mountpoint[ZFS_MAXPROPLEN];
5228 char shareopts[ZFS_MAXPROPLEN];
5229 char smbshareopts[ZFS_MAXPROPLEN];
5230 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5232 uint64_t zoned, canmount;
5233 boolean_t shared_nfs, shared_smb;
5235 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5238 * Check to make sure we can mount/share this dataset. If we
5239 * are in the global zone and the filesystem is exported to a
5240 * local zone, or if we are in a local zone and the
5241 * filesystem is not exported, then it is an error.
5243 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5245 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5249 (void) fprintf(stderr, gettext("cannot %s '%s': "
5250 "dataset is exported to a local zone\n"), cmdname,
5254 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5258 (void) fprintf(stderr, gettext("cannot %s '%s': "
5259 "permission denied\n"), cmdname,
5265 * Ignore any filesystems which don't apply to us. This
5266 * includes those with a legacy mountpoint, or those with
5267 * legacy share options.
5269 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5270 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5271 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5272 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5273 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5274 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5276 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5277 strcmp(smbshareopts, "off") == 0) {
5281 (void) fprintf(stderr, gettext("cannot share '%s': "
5282 "legacy share\n"), zfs_get_name(zhp));
5283 (void) fprintf(stderr, gettext("use share(1M) to "
5284 "share this filesystem, or set "
5285 "sharenfs property on\n"));
5290 * We cannot share or mount legacy filesystems. If the
5291 * shareopts is non-legacy but the mountpoint is legacy, we
5292 * treat it as a legacy share.
5294 if (strcmp(mountpoint, "legacy") == 0) {
5298 (void) fprintf(stderr, gettext("cannot %s '%s': "
5299 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5300 (void) fprintf(stderr, gettext("use %s(1M) to "
5301 "%s this filesystem\n"), cmdname, cmdname);
5305 if (strcmp(mountpoint, "none") == 0) {
5309 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5310 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5315 * canmount explicit outcome
5316 * on no pass through
5317 * on yes pass through
5319 * off yes display error, return 1
5320 * noauto no return 0
5321 * noauto yes pass through
5323 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5324 if (canmount == ZFS_CANMOUNT_OFF) {
5328 (void) fprintf(stderr, gettext("cannot %s '%s': "
5329 "'canmount' property is set to 'off'\n"), cmdname,
5332 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5337 * At this point, we have verified that the mountpoint and/or
5338 * shareopts are appropriate for auto management. If the
5339 * filesystem is already mounted or shared, return (failing
5340 * for explicit requests); otherwise mount or share the
5346 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5347 shared_smb = zfs_is_shared_smb(zhp, NULL);
5349 if (shared_nfs && shared_smb ||
5350 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5351 strcmp(smbshareopts, "off") == 0) ||
5352 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5353 strcmp(shareopts, "off") == 0)) {
5357 (void) fprintf(stderr, gettext("cannot share "
5358 "'%s': filesystem already shared\n"),
5363 if (!zfs_is_mounted(zhp, NULL) &&
5364 zfs_mount(zhp, NULL, 0) != 0)
5367 if (protocol == NULL) {
5368 if (zfs_shareall(zhp) != 0)
5370 } else if (strcmp(protocol, "nfs") == 0) {
5371 if (zfs_share_nfs(zhp))
5373 } else if (strcmp(protocol, "smb") == 0) {
5374 if (zfs_share_smb(zhp))
5377 (void) fprintf(stderr, gettext("cannot share "
5378 "'%s': invalid share type '%s' "
5380 zfs_get_name(zhp), protocol);
5387 if (options == NULL)
5388 mnt.mnt_mntopts = "";
5390 mnt.mnt_mntopts = (char *)options;
5392 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5393 zfs_is_mounted(zhp, NULL)) {
5397 (void) fprintf(stderr, gettext("cannot mount "
5398 "'%s': filesystem already mounted\n"),
5403 if (zfs_mount(zhp, options, flags) != 0)
5412 * Reports progress in the form "(current/total)". Not thread-safe.
5415 report_mount_progress(int current, int total)
5417 static time_t last_progress_time = 0;
5418 time_t now = time(NULL);
5421 /* report 1..n instead of 0..n-1 */
5424 /* display header if we're here for the first time */
5426 set_progress_header(gettext("Mounting ZFS filesystems"));
5427 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5428 /* too soon to report again */
5432 last_progress_time = now;
5434 (void) sprintf(info, "(%d/%d)", current, total);
5436 if (current == total)
5437 finish_progress(info);
5439 update_progress(info);
5443 append_options(char *mntopts, char *newopts)
5445 int len = strlen(mntopts);
5447 /* original length plus new string to append plus 1 for the comma */
5448 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5449 (void) fprintf(stderr, gettext("the opts argument for "
5450 "'%c' option is too long (more than %d chars)\n"),
5451 "-o", MNT_LINE_MAX);
5456 mntopts[len++] = ',';
5458 (void) strcpy(&mntopts[len], newopts);
5462 share_mount(int op, int argc, char **argv)
5465 boolean_t verbose = B_FALSE;
5467 char *options = NULL;
5471 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5481 if (*optarg == '\0') {
5482 (void) fprintf(stderr, gettext("empty mount "
5483 "options (-o) specified\n"));
5487 if (options == NULL)
5488 options = safe_malloc(MNT_LINE_MAX + 1);
5490 /* option validation is done later */
5491 append_options(options, optarg);
5495 warnx("no overlay mounts support on FreeBSD, ignoring");
5498 (void) fprintf(stderr, gettext("missing argument for "
5499 "'%c' option\n"), optopt);
5503 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5512 /* check number of arguments */
5514 zfs_handle_t **dslist = NULL;
5515 size_t i, count = 0;
5516 char *protocol = NULL;
5518 if (op == OP_SHARE && argc > 0) {
5519 if (strcmp(argv[0], "nfs") != 0 &&
5520 strcmp(argv[0], "smb") != 0) {
5521 (void) fprintf(stderr, gettext("share type "
5522 "must be 'nfs' or 'smb'\n"));
5531 (void) fprintf(stderr, gettext("too many arguments\n"));
5535 start_progress_timer();
5536 get_all_datasets(&dslist, &count, verbose);
5541 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5543 for (i = 0; i < count; i++) {
5545 report_mount_progress(i, count);
5547 if (share_mount_one(dslist[i], op, flags, protocol,
5548 B_FALSE, options) != 0)
5550 zfs_close(dslist[i]);
5554 } else if (argc == 0) {
5555 struct mnttab entry;
5557 if ((op == OP_SHARE) || (options != NULL)) {
5558 (void) fprintf(stderr, gettext("missing filesystem "
5559 "argument (specify -a for all)\n"));
5564 * When mount is given no arguments, go through /etc/mnttab and
5565 * display any active ZFS mounts. We hide any snapshots, since
5566 * they are controlled automatically.
5568 rewind(mnttab_file);
5569 while (getmntent(mnttab_file, &entry) == 0) {
5570 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5571 strchr(entry.mnt_special, '@') != NULL)
5574 (void) printf("%-30s %s\n", entry.mnt_special,
5582 (void) fprintf(stderr,
5583 gettext("too many arguments\n"));
5587 if ((zhp = zfs_open(g_zfs, argv[0],
5588 ZFS_TYPE_FILESYSTEM)) == NULL) {
5591 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5601 * zfs mount -a [nfs]
5602 * zfs mount filesystem
5604 * Mount all filesystems, or mount the given filesystem.
5607 zfs_do_mount(int argc, char **argv)
5609 return (share_mount(OP_MOUNT, argc, argv));
5613 * zfs share -a [nfs | smb]
5614 * zfs share filesystem
5616 * Share all filesystems, or share the given filesystem.
5619 zfs_do_share(int argc, char **argv)
5621 return (share_mount(OP_SHARE, argc, argv));
5624 typedef struct unshare_unmount_node {
5625 zfs_handle_t *un_zhp;
5627 uu_avl_node_t un_avlnode;
5628 } unshare_unmount_node_t;
5632 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
5634 const unshare_unmount_node_t *l = larg;
5635 const unshare_unmount_node_t *r = rarg;
5637 return (strcmp(l->un_mountp, r->un_mountp));
5641 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5642 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
5643 * and unmount it appropriately.
5646 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
5650 struct stat64 statbuf;
5651 struct extmnttab entry;
5652 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5656 * Search for the path in /etc/mnttab. Rather than looking for the
5657 * specific path, which can be fooled by non-standard paths (i.e. ".."
5658 * or "//"), we stat() the path and search for the corresponding
5659 * (major,minor) device pair.
5661 if (stat64(path, &statbuf) != 0) {
5662 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5663 cmdname, path, strerror(errno));
5666 path_inode = statbuf.st_ino;
5669 * Search for the given (major,minor) pair in the mount table.
5672 rewind(mnttab_file);
5673 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
5674 if (entry.mnt_major == major(statbuf.st_dev) &&
5675 entry.mnt_minor == minor(statbuf.st_dev))
5682 if (statfs(path, &sfs) != 0) {
5683 (void) fprintf(stderr, "%s: %s\n", path,
5687 statfs2mnttab(&sfs, &entry);
5691 if (op == OP_SHARE) {
5692 (void) fprintf(stderr, gettext("cannot %s '%s': not "
5693 "currently mounted\n"), cmdname, path);
5696 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
5698 if ((ret = umount2(path, flags)) != 0)
5699 (void) fprintf(stderr, gettext("%s: %s\n"), path,
5704 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
5705 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
5706 "filesystem\n"), cmdname, path);
5710 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
5711 ZFS_TYPE_FILESYSTEM)) == NULL)
5715 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
5716 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5717 cmdname, path, strerror(errno));
5719 } else if (statbuf.st_ino != path_inode) {
5720 (void) fprintf(stderr, gettext("cannot "
5721 "%s '%s': not a mountpoint\n"), cmdname, path);
5725 if (op == OP_SHARE) {
5726 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5727 char smbshare_prop[ZFS_MAXPROPLEN];
5729 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
5730 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
5731 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
5732 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
5734 if (strcmp(nfs_mnt_prop, "off") == 0 &&
5735 strcmp(smbshare_prop, "off") == 0) {
5736 (void) fprintf(stderr, gettext("cannot unshare "
5737 "'%s': legacy share\n"), path);
5738 (void) fprintf(stderr, gettext("use "
5739 "unshare(1M) to unshare this filesystem\n"));
5740 } else if (!zfs_is_shared(zhp)) {
5741 (void) fprintf(stderr, gettext("cannot unshare '%s': "
5742 "not currently shared\n"), path);
5744 ret = zfs_unshareall_bypath(zhp, path);
5747 char mtpt_prop[ZFS_MAXPROPLEN];
5749 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
5750 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
5753 ret = zfs_unmount(zhp, NULL, flags);
5754 } else if (strcmp(mtpt_prop, "legacy") == 0) {
5755 (void) fprintf(stderr, gettext("cannot unmount "
5756 "'%s': legacy mountpoint\n"),
5758 (void) fprintf(stderr, gettext("use umount(1M) "
5759 "to unmount this filesystem\n"));
5761 ret = zfs_unmountall(zhp, flags);
5772 * Generic callback for unsharing or unmounting a filesystem.
5775 unshare_unmount(int op, int argc, char **argv)
5782 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5783 char sharesmb[ZFS_MAXPROPLEN];
5786 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
5795 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5806 * We could make use of zfs_for_each() to walk all datasets in
5807 * the system, but this would be very inefficient, especially
5808 * since we would have to linearly search /etc/mnttab for each
5809 * one. Instead, do one pass through /etc/mnttab looking for
5810 * zfs entries and call zfs_unmount() for each one.
5812 * Things get a little tricky if the administrator has created
5813 * mountpoints beneath other ZFS filesystems. In this case, we
5814 * have to unmount the deepest filesystems first. To accomplish
5815 * this, we place all the mountpoints in an AVL tree sorted by
5816 * the special type (dataset name), and walk the result in
5817 * reverse to make sure to get any snapshots first.
5819 struct mnttab entry;
5820 uu_avl_pool_t *pool;
5822 unshare_unmount_node_t *node;
5824 uu_avl_walk_t *walk;
5827 (void) fprintf(stderr, gettext("too many arguments\n"));
5831 if (((pool = uu_avl_pool_create("unmount_pool",
5832 sizeof (unshare_unmount_node_t),
5833 offsetof(unshare_unmount_node_t, un_avlnode),
5834 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
5835 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
5838 rewind(mnttab_file);
5839 while (getmntent(mnttab_file, &entry) == 0) {
5841 /* ignore non-ZFS entries */
5842 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
5845 /* ignore snapshots */
5846 if (strchr(entry.mnt_special, '@') != NULL)
5849 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
5850 ZFS_TYPE_FILESYSTEM)) == NULL) {
5857 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
5859 sizeof (nfs_mnt_prop),
5860 NULL, NULL, 0, B_FALSE) == 0);
5861 if (strcmp(nfs_mnt_prop, "off") != 0)
5863 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
5865 sizeof (nfs_mnt_prop),
5866 NULL, NULL, 0, B_FALSE) == 0);
5867 if (strcmp(nfs_mnt_prop, "off") == 0)
5871 /* Ignore legacy mounts */
5872 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
5874 sizeof (nfs_mnt_prop),
5875 NULL, NULL, 0, B_FALSE) == 0);
5876 if (strcmp(nfs_mnt_prop, "legacy") == 0)
5878 /* Ignore canmount=noauto mounts */
5879 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
5880 ZFS_CANMOUNT_NOAUTO)
5886 node = safe_malloc(sizeof (unshare_unmount_node_t));
5888 node->un_mountp = safe_strdup(entry.mnt_mountp);
5890 uu_avl_node_init(node, &node->un_avlnode, pool);
5892 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
5893 uu_avl_insert(tree, node, idx);
5895 zfs_close(node->un_zhp);
5896 free(node->un_mountp);
5902 * Walk the AVL tree in reverse, unmounting each filesystem and
5903 * removing it from the AVL tree in the process.
5905 if ((walk = uu_avl_walk_start(tree,
5906 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
5909 while ((node = uu_avl_walk_next(walk)) != NULL) {
5910 uu_avl_remove(tree, node);
5914 if (zfs_unshareall_bypath(node->un_zhp,
5915 node->un_mountp) != 0)
5920 if (zfs_unmount(node->un_zhp,
5921 node->un_mountp, flags) != 0)
5926 zfs_close(node->un_zhp);
5927 free(node->un_mountp);
5931 uu_avl_walk_end(walk);
5932 uu_avl_destroy(tree);
5933 uu_avl_pool_destroy(pool);
5938 (void) fprintf(stderr,
5939 gettext("missing filesystem argument\n"));
5941 (void) fprintf(stderr,
5942 gettext("too many arguments\n"));
5947 * We have an argument, but it may be a full path or a ZFS
5948 * filesystem. Pass full paths off to unmount_path() (shared by
5949 * manual_unmount), otherwise open the filesystem and pass to
5952 if (argv[0][0] == '/')
5953 return (unshare_unmount_path(op, argv[0],
5956 if ((zhp = zfs_open(g_zfs, argv[0],
5957 ZFS_TYPE_FILESYSTEM)) == NULL)
5960 verify(zfs_prop_get(zhp, op == OP_SHARE ?
5961 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
5962 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
5963 NULL, 0, B_FALSE) == 0);
5967 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
5969 sizeof (nfs_mnt_prop),
5970 NULL, NULL, 0, B_FALSE) == 0);
5971 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
5972 sharesmb, sizeof (sharesmb), NULL, NULL,
5975 if (strcmp(nfs_mnt_prop, "off") == 0 &&
5976 strcmp(sharesmb, "off") == 0) {
5977 (void) fprintf(stderr, gettext("cannot "
5978 "unshare '%s': legacy share\n"),
5980 (void) fprintf(stderr, gettext("use "
5981 "unshare(1M) to unshare this "
5984 } else if (!zfs_is_shared(zhp)) {
5985 (void) fprintf(stderr, gettext("cannot "
5986 "unshare '%s': not currently "
5987 "shared\n"), zfs_get_name(zhp));
5989 } else if (zfs_unshareall(zhp) != 0) {
5995 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
5996 (void) fprintf(stderr, gettext("cannot "
5997 "unmount '%s': legacy "
5998 "mountpoint\n"), zfs_get_name(zhp));
5999 (void) fprintf(stderr, gettext("use "
6000 "umount(1M) to unmount this "
6003 } else if (!zfs_is_mounted(zhp, NULL)) {
6004 (void) fprintf(stderr, gettext("cannot "
6005 "unmount '%s': not currently "
6009 } else if (zfs_unmountall(zhp, flags) != 0) {
6023 * zfs unmount filesystem
6025 * Unmount all filesystems, or a specific ZFS filesystem.
6028 zfs_do_unmount(int argc, char **argv)
6030 return (unshare_unmount(OP_MOUNT, argc, argv));
6035 * zfs unshare filesystem
6037 * Unshare all filesystems, or a specific ZFS filesystem.
6040 zfs_do_unshare(int argc, char **argv)
6042 return (unshare_unmount(OP_SHARE, argc, argv));
6046 * Attach/detach the given dataset to/from the given jail
6050 do_jail(int argc, char **argv, int attach)
6055 /* check number of arguments */
6057 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6061 (void) fprintf(stderr, gettext("too many arguments\n"));
6065 jailid = atoi(argv[1]);
6067 (void) fprintf(stderr, gettext("invalid jailid\n"));
6071 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6075 ret = (zfs_jail(zhp, jailid, attach) != 0);
6082 * zfs jail jailid filesystem
6084 * Attach the given dataset to the given jail
6088 zfs_do_jail(int argc, char **argv)
6091 return (do_jail(argc, argv, 1));
6095 * zfs unjail jailid filesystem
6097 * Detach the given dataset from the given jail
6101 zfs_do_unjail(int argc, char **argv)
6104 return (do_jail(argc, argv, 0));
6108 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6109 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6112 manual_mount(int argc, char **argv)
6115 char mountpoint[ZFS_MAXPROPLEN];
6116 char mntopts[MNT_LINE_MAX] = { '\0' };
6120 char *dataset, *path;
6123 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6126 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6129 flags |= MS_OVERLAY;
6132 flags |= MS_NOMNTTAB;
6135 (void) fprintf(stderr, gettext("missing argument for "
6136 "'%c' option\n"), optopt);
6140 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6142 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6151 /* check that we only have two arguments */
6154 (void) fprintf(stderr, gettext("missing dataset "
6157 (void) fprintf(stderr,
6158 gettext("missing mountpoint argument\n"));
6160 (void) fprintf(stderr, gettext("too many arguments\n"));
6161 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6168 /* try to open the dataset */
6169 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6172 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6173 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6175 /* check for legacy mountpoint and complain appropriately */
6177 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6178 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6179 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6180 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6185 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6186 "mounted using 'mount -F zfs'\n"), dataset);
6187 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6188 "instead.\n"), path);
6189 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
6190 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6191 (void) fprintf(stderr, gettext("See zfs(1M) for more "
6200 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6201 * unmounts of non-legacy filesystems, as this is the dominant administrative
6205 manual_unmount(int argc, char **argv)
6211 while ((c = getopt(argc, argv, "f")) != -1) {
6217 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6219 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6228 /* check arguments */
6231 (void) fprintf(stderr, gettext("missing path "
6234 (void) fprintf(stderr, gettext("too many arguments\n"));
6235 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6239 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6243 find_command_idx(char *command, int *idx)
6247 for (i = 0; i < NCOMMAND; i++) {
6248 if (command_table[i].name == NULL)
6251 if (strcmp(command, command_table[i].name) == 0) {
6260 zfs_do_diff(int argc, char **argv)
6264 char *tosnap = NULL;
6265 char *fromsnap = NULL;
6270 while ((c = getopt(argc, argv, "FHt")) != -1) {
6273 flags |= ZFS_DIFF_CLASSIFY;
6276 flags |= ZFS_DIFF_PARSEABLE;
6279 flags |= ZFS_DIFF_TIMESTAMP;
6282 (void) fprintf(stderr,
6283 gettext("invalid option '%c'\n"), optopt);
6292 (void) fprintf(stderr,
6293 gettext("must provide at least one snapshot name\n"));
6298 (void) fprintf(stderr, gettext("too many arguments\n"));
6303 tosnap = (argc == 2) ? argv[1] : NULL;
6306 if (*fromsnap != '@')
6307 copy = strdup(fromsnap);
6309 copy = strdup(tosnap);
6313 if (atp = strchr(copy, '@'))
6316 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6322 * Ignore SIGPIPE so that the library can give us
6323 * information on any failure
6325 (void) sigignore(SIGPIPE);
6327 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6335 main(int argc, char **argv)
6342 (void) setlocale(LC_ALL, "");
6343 (void) textdomain(TEXT_DOMAIN);
6347 if ((g_zfs = libzfs_init()) == NULL) {
6348 (void) fprintf(stderr, gettext("internal error: failed to "
6349 "initialize ZFS library\n"));
6353 zpool_set_history_str("zfs", argc, argv, history_str);
6354 verify(zpool_stage_history(g_zfs, history_str) == 0);
6356 libzfs_print_on_error(g_zfs, B_TRUE);
6358 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6359 (void) fprintf(stderr, gettext("internal error: unable to "
6360 "open %s\n"), MNTTAB);
6365 * This command also doubles as the /etc/fs mount and unmount program.
6366 * Determine if we should take this behavior based on argv[0].
6368 progname = basename(argv[0]);
6369 if (strcmp(progname, "mount") == 0) {
6370 ret = manual_mount(argc, argv);
6371 } else if (strcmp(progname, "umount") == 0) {
6372 ret = manual_unmount(argc, argv);
6375 * Make sure the user has specified some command.
6378 (void) fprintf(stderr, gettext("missing command\n"));
6385 * The 'umount' command is an alias for 'unmount'
6387 if (strcmp(cmdname, "umount") == 0)
6388 cmdname = "unmount";
6391 * The 'recv' command is an alias for 'receive'
6393 if (strcmp(cmdname, "recv") == 0)
6394 cmdname = "receive";
6399 if (strcmp(cmdname, "-?") == 0)
6403 * Run the appropriate command.
6405 libzfs_mnttab_cache(g_zfs, B_TRUE);
6406 if (find_command_idx(cmdname, &i) == 0) {
6407 current_command = &command_table[i];
6408 ret = command_table[i].func(argc - 1, argv + 1);
6409 } else if (strchr(cmdname, '=') != NULL) {
6410 verify(find_command_idx("set", &i) == 0);
6411 current_command = &command_table[i];
6412 ret = command_table[i].func(argc, argv);
6414 (void) fprintf(stderr, gettext("unrecognized "
6415 "command '%s'\n"), cmdname);
6418 libzfs_mnttab_cache(g_zfs, B_FALSE);
6421 (void) fclose(mnttab_file);
6426 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6427 * for the purposes of running ::findleaks.
6429 if (getenv("ZFS_ABORT") != NULL) {
6430 (void) printf("dumping core by request\n");