4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright 2012 Milan Jurik. All rights reserved.
27 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
29 * All rights reserved.
30 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
39 #include <libnvpair.h>
52 #include <sys/mntent.h>
53 #include <sys/mnttab.h>
54 #include <sys/mount.h>
56 #include <sys/fs/zfs.h>
57 #include <sys/types.h>
62 #include <zfs_deleg.h>
66 #include <directory.h>
71 #include "zfs_comutil.h"
73 libzfs_handle_t *g_zfs;
75 static FILE *mnttab_file;
76 static char history_str[HIS_MAX_RECORD_LEN];
78 static int zfs_do_clone(int argc, char **argv);
79 static int zfs_do_create(int argc, char **argv);
80 static int zfs_do_destroy(int argc, char **argv);
81 static int zfs_do_get(int argc, char **argv);
82 static int zfs_do_inherit(int argc, char **argv);
83 static int zfs_do_list(int argc, char **argv);
84 static int zfs_do_mount(int argc, char **argv);
85 static int zfs_do_rename(int argc, char **argv);
86 static int zfs_do_rollback(int argc, char **argv);
87 static int zfs_do_set(int argc, char **argv);
88 static int zfs_do_upgrade(int argc, char **argv);
89 static int zfs_do_snapshot(int argc, char **argv);
90 static int zfs_do_unmount(int argc, char **argv);
91 static int zfs_do_share(int argc, char **argv);
92 static int zfs_do_unshare(int argc, char **argv);
93 static int zfs_do_send(int argc, char **argv);
94 static int zfs_do_receive(int argc, char **argv);
95 static int zfs_do_promote(int argc, char **argv);
96 static int zfs_do_userspace(int argc, char **argv);
97 static int zfs_do_allow(int argc, char **argv);
98 static int zfs_do_unallow(int argc, char **argv);
99 static int zfs_do_hold(int argc, char **argv);
100 static int zfs_do_holds(int argc, char **argv);
101 static int zfs_do_release(int argc, char **argv);
102 static int zfs_do_diff(int argc, char **argv);
103 static int zfs_do_jail(int argc, char **argv);
104 static int zfs_do_unjail(int argc, char **argv);
107 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
112 _umem_debug_init(void)
114 return ("default,verbose"); /* $UMEM_DEBUG setting */
118 _umem_logging_init(void)
120 return ("fail,contents"); /* $UMEM_LOGGING setting */
155 typedef struct zfs_command {
157 int (*func)(int argc, char **argv);
162 * Master command table. Each ZFS command has a name, associated function, and
163 * usage message. The usage messages need to be internationalized, so we have
164 * to have a function to return the usage message based on a command index.
166 * These commands are organized according to how they are displayed in the usage
167 * message. An empty command (one with a NULL name) indicates an empty line in
168 * the generic usage message.
170 static zfs_command_t command_table[] = {
171 { "create", zfs_do_create, HELP_CREATE },
172 { "destroy", zfs_do_destroy, HELP_DESTROY },
174 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
175 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
176 { "clone", zfs_do_clone, HELP_CLONE },
177 { "promote", zfs_do_promote, HELP_PROMOTE },
178 { "rename", zfs_do_rename, HELP_RENAME },
180 { "list", zfs_do_list, HELP_LIST },
182 { "set", zfs_do_set, HELP_SET },
183 { "get", zfs_do_get, HELP_GET },
184 { "inherit", zfs_do_inherit, HELP_INHERIT },
185 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
186 { "userspace", zfs_do_userspace, HELP_USERSPACE },
187 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
189 { "mount", zfs_do_mount, HELP_MOUNT },
190 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
191 { "share", zfs_do_share, HELP_SHARE },
192 { "unshare", zfs_do_unshare, HELP_UNSHARE },
194 { "send", zfs_do_send, HELP_SEND },
195 { "receive", zfs_do_receive, HELP_RECEIVE },
197 { "allow", zfs_do_allow, HELP_ALLOW },
199 { "unallow", zfs_do_unallow, HELP_UNALLOW },
201 { "hold", zfs_do_hold, HELP_HOLD },
202 { "holds", zfs_do_holds, HELP_HOLDS },
203 { "release", zfs_do_release, HELP_RELEASE },
204 { "diff", zfs_do_diff, HELP_DIFF },
206 { "jail", zfs_do_jail, HELP_JAIL },
207 { "unjail", zfs_do_unjail, HELP_UNJAIL },
210 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
212 zfs_command_t *current_command;
215 get_usage(zfs_help_t idx)
219 return (gettext("\tclone [-p] [-o property=value] ... "
220 "<snapshot> <filesystem|volume>\n"));
222 return (gettext("\tcreate [-p] [-o property=value] ... "
224 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
225 "-V <size> <volume>\n"));
227 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
228 "\tdestroy [-dnpRrv] "
229 "<snapshot>[%<snapname>][,...]\n"));
231 return (gettext("\tget [-rHp] [-d max] "
232 "[-o \"all\" | field[,...]] [-t type[,...]] "
233 "[-s source[,...]]\n"
234 "\t <\"all\" | property[,...]> "
235 "[filesystem|volume|snapshot] ...\n"));
237 return (gettext("\tinherit [-rS] <property> "
238 "<filesystem|volume|snapshot> ...\n"));
240 return (gettext("\tupgrade [-v]\n"
241 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
243 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
245 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
247 return (gettext("\tlist [-rH][-d max] "
248 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
249 "\t [-S property] ... "
250 "[filesystem|volume|snapshot] ...\n"));
252 return (gettext("\tmount\n"
253 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
255 return (gettext("\tpromote <clone-filesystem>\n"));
257 return (gettext("\treceive [-vnFu] <filesystem|volume|"
259 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
261 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
262 "<filesystem|volume|snapshot>\n"
263 "\trename [-f] -p <filesystem|volume> "
264 "<filesystem|volume>\n"
265 "\trename -r <snapshot> <snapshot>\n"
266 "\trename -u [-p] <filesystem> <filesystem>"));
268 return (gettext("\trollback [-rRf] <snapshot>\n"));
270 return (gettext("\tsend [-DnPpRv] "
271 "[-i snapshot | -I snapshot] <snapshot>\n"));
273 return (gettext("\tset <property=value> "
274 "<filesystem|volume|snapshot> ...\n"));
276 return (gettext("\tshare <-a | filesystem>\n"));
278 return (gettext("\tsnapshot [-r] [-o property=value] ... "
279 "<filesystem@snapname|volume@snapname>\n"));
281 return (gettext("\tunmount [-f] "
282 "<-a | filesystem|mountpoint>\n"));
284 return (gettext("\tunshare "
285 "<-a | filesystem|mountpoint>\n"));
287 return (gettext("\tallow <filesystem|volume>\n"
289 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
290 "\t <filesystem|volume>\n"
291 "\tallow [-ld] -e <perm|@setname>[,...] "
292 "<filesystem|volume>\n"
293 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
294 "\tallow -s @setname <perm|@setname>[,...] "
295 "<filesystem|volume>\n"));
297 return (gettext("\tunallow [-rldug] "
298 "<\"everyone\"|user|group>[,...]\n"
299 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
300 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
301 "<filesystem|volume>\n"
302 "\tunallow [-r] -c [<perm|@setname>[,...]] "
303 "<filesystem|volume>\n"
304 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
305 "<filesystem|volume>\n"));
307 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
308 "[-s field] ...\n\t[-S field] ... "
309 "[-t type[,...]] <filesystem|snapshot>\n"));
310 case HELP_GROUPSPACE:
311 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
312 "[-s field] ...\n\t[-S field] ... "
313 "[-t type[,...]] <filesystem|snapshot>\n"));
315 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
317 return (gettext("\tholds [-r] <snapshot> ...\n"));
319 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
321 return (gettext("\tdiff [-FHt] <snapshot> "
322 "[snapshot|filesystem]\n"));
332 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
337 * Utility function to guarantee malloc() success.
341 safe_malloc(size_t size)
345 if ((data = calloc(1, size)) == NULL)
352 safe_strdup(char *str)
354 char *dupstr = strdup(str);
363 * Callback routine that will print out information for each of
367 usage_prop_cb(int prop, void *cb)
371 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
373 if (zfs_prop_readonly(prop))
374 (void) fprintf(fp, " NO ");
376 (void) fprintf(fp, "YES ");
378 if (zfs_prop_inheritable(prop))
379 (void) fprintf(fp, " YES ");
381 (void) fprintf(fp, " NO ");
383 if (zfs_prop_values(prop) == NULL)
384 (void) fprintf(fp, "-\n");
386 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
392 * Display usage message. If we're inside a command, display only the usage for
393 * that command. Otherwise, iterate over the entire command table and display
394 * a complete usage message.
397 usage(boolean_t requested)
400 boolean_t show_properties = B_FALSE;
401 FILE *fp = requested ? stdout : stderr;
403 if (current_command == NULL) {
405 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
407 gettext("where 'command' is one of the following:\n\n"));
409 for (i = 0; i < NCOMMAND; i++) {
410 if (command_table[i].name == NULL)
411 (void) fprintf(fp, "\n");
413 (void) fprintf(fp, "%s",
414 get_usage(command_table[i].usage));
417 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
418 "pool/[dataset/]*dataset[@name]\n"));
420 (void) fprintf(fp, gettext("usage:\n"));
421 (void) fprintf(fp, "%s", get_usage(current_command->usage));
424 if (current_command != NULL &&
425 (strcmp(current_command->name, "set") == 0 ||
426 strcmp(current_command->name, "get") == 0 ||
427 strcmp(current_command->name, "inherit") == 0 ||
428 strcmp(current_command->name, "list") == 0))
429 show_properties = B_TRUE;
431 if (show_properties) {
433 gettext("\nThe following properties are supported:\n"));
435 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
436 "PROPERTY", "EDIT", "INHERIT", "VALUES");
438 /* Iterate over all properties */
439 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
442 (void) fprintf(fp, "\t%-15s ", "userused@...");
443 (void) fprintf(fp, " NO NO <size>\n");
444 (void) fprintf(fp, "\t%-15s ", "groupused@...");
445 (void) fprintf(fp, " NO NO <size>\n");
446 (void) fprintf(fp, "\t%-15s ", "userquota@...");
447 (void) fprintf(fp, "YES NO <size> | none\n");
448 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
449 (void) fprintf(fp, "YES NO <size> | none\n");
450 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
451 (void) fprintf(fp, " NO NO <size>\n");
453 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
454 "with standard units such as K, M, G, etc.\n"));
455 (void) fprintf(fp, gettext("\nUser-defined properties can "
456 "be specified by using a name containing a colon (:).\n"));
457 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
458 "properties must be appended with\n"
459 "a user or group specifier of one of these forms:\n"
460 " POSIX name (eg: \"matt\")\n"
461 " POSIX id (eg: \"126829\")\n"
462 " SMB name@domain (eg: \"matt@sun\")\n"
463 " SMB SID (eg: \"S-1-234-567-89\")\n"));
466 gettext("\nFor the property list, run: %s\n"),
469 gettext("\nFor the delegated permission list, run: %s\n"),
470 "zfs allow|unallow");
474 * See comments at end of main().
476 if (getenv("ZFS_ABORT") != NULL) {
477 (void) printf("dumping core by request\n");
481 exit(requested ? 0 : 2);
485 parseprop(nvlist_t *props)
487 char *propname = optarg;
488 char *propval, *strval;
490 if ((propval = strchr(propname, '=')) == NULL) {
491 (void) fprintf(stderr, gettext("missing "
492 "'=' for -o option\n"));
497 if (nvlist_lookup_string(props, propname, &strval) == 0) {
498 (void) fprintf(stderr, gettext("property '%s' "
499 "specified multiple times\n"), propname);
502 if (nvlist_add_string(props, propname, propval) != 0)
508 parse_depth(char *opt, int *flags)
513 depth = (int)strtol(opt, &tmp, 0);
515 (void) fprintf(stderr,
516 gettext("%s is not an integer\n"), optarg);
520 (void) fprintf(stderr,
521 gettext("Depth can not be negative.\n"));
524 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
528 #define PROGRESS_DELAY 2 /* seconds */
530 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";
531 static time_t pt_begin;
532 static char *pt_header = NULL;
533 static boolean_t pt_shown;
536 start_progress_timer(void)
538 pt_begin = time(NULL) + PROGRESS_DELAY;
543 set_progress_header(char *header)
545 assert(pt_header == NULL);
546 pt_header = safe_strdup(header);
548 (void) printf("%s: ", header);
549 (void) fflush(stdout);
554 update_progress(char *update)
556 if (!pt_shown && time(NULL) > pt_begin) {
557 int len = strlen(update);
559 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
561 (void) fflush(stdout);
563 } else if (pt_shown) {
564 int len = strlen(update);
566 (void) printf("%s%*.*s", update, len, len, pt_reverse);
567 (void) fflush(stdout);
572 finish_progress(char *done)
575 (void) printf("%s\n", done);
576 (void) fflush(stdout);
582 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
584 * Given an existing dataset, create a writable copy whose initial contents
585 * are the same as the source. The newly created dataset maintains a
586 * dependency on the original; the original cannot be destroyed so long as
589 * The '-p' flag creates all the non-existing ancestors of the target first.
592 zfs_do_clone(int argc, char **argv)
594 zfs_handle_t *zhp = NULL;
595 boolean_t parents = B_FALSE;
600 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
604 while ((c = getopt(argc, argv, "o:p")) != -1) {
607 if (parseprop(props))
614 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
623 /* check number of arguments */
625 (void) fprintf(stderr, gettext("missing source dataset "
630 (void) fprintf(stderr, gettext("missing target dataset "
635 (void) fprintf(stderr, gettext("too many arguments\n"));
639 /* open the source dataset */
640 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
643 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
646 * Now create the ancestors of the target dataset. If the
647 * target already exists and '-p' option was used we should not
650 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
653 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
658 ret = zfs_clone(zhp, argv[1], props);
660 /* create the mountpoint if necessary */
664 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
666 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
667 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
668 ret = zfs_share(clone);
687 * zfs create [-p] [-o prop=value] ... fs
688 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
690 * Create a new dataset. This command can be used to create filesystems
691 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
692 * For volumes, the user must specify a size to be used.
694 * The '-s' flag applies only to volumes, and indicates that we should not try
695 * to set the reservation for this volume. By default we set a reservation
696 * equal to the size for any volume. For pools with SPA_VERSION >=
697 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
699 * The '-p' flag creates all the non-existing ancestors of the target first.
702 zfs_do_create(int argc, char **argv)
704 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
705 zfs_handle_t *zhp = NULL;
708 boolean_t noreserve = B_FALSE;
709 boolean_t bflag = B_FALSE;
710 boolean_t parents = B_FALSE;
714 int canmount = ZFS_CANMOUNT_OFF;
716 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
720 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
723 type = ZFS_TYPE_VOLUME;
724 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
725 (void) fprintf(stderr, gettext("bad volume "
726 "size '%s': %s\n"), optarg,
727 libzfs_error_description(g_zfs));
731 if (nvlist_add_uint64(props,
732 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
741 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
742 (void) fprintf(stderr, gettext("bad volume "
743 "block size '%s': %s\n"), optarg,
744 libzfs_error_description(g_zfs));
748 if (nvlist_add_uint64(props,
749 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
754 if (parseprop(props))
761 (void) fprintf(stderr, gettext("missing size "
765 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
771 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
772 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
773 "used when creating a volume\n"));
780 /* check number of arguments */
782 (void) fprintf(stderr, gettext("missing %s argument\n"),
783 zfs_type_to_name(type));
787 (void) fprintf(stderr, gettext("too many arguments\n"));
791 if (type == ZFS_TYPE_VOLUME && !noreserve) {
792 zpool_handle_t *zpool_handle;
793 uint64_t spa_version;
795 zfs_prop_t resv_prop;
798 if (p = strchr(argv[0], '/'))
800 zpool_handle = zpool_open(g_zfs, argv[0]);
803 if (zpool_handle == NULL)
805 spa_version = zpool_get_prop_int(zpool_handle,
806 ZPOOL_PROP_VERSION, NULL);
807 zpool_close(zpool_handle);
808 if (spa_version >= SPA_VERSION_REFRESERVATION)
809 resv_prop = ZFS_PROP_REFRESERVATION;
811 resv_prop = ZFS_PROP_RESERVATION;
812 volsize = zvol_volsize_to_reservation(volsize, props);
814 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
816 if (nvlist_add_uint64(props,
817 zfs_prop_to_name(resv_prop), volsize) != 0) {
824 if (parents && zfs_name_valid(argv[0], type)) {
826 * Now create the ancestors of target dataset. If the target
827 * already exists and '-p' option was used we should not
830 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
834 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
839 if (zfs_create(g_zfs, argv[0], type, props) != 0)
842 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
847 * if the user doesn't want the dataset automatically mounted,
848 * then skip the mount/share step
850 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
851 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
854 * Mount and/or share the new filesystem as appropriate. We provide a
855 * verbose error message to let the user know that their filesystem was
856 * in fact created, even if we failed to mount or share it.
858 if (canmount == ZFS_CANMOUNT_ON) {
859 if (zfs_mount(zhp, NULL, 0) != 0) {
860 (void) fprintf(stderr, gettext("filesystem "
861 "successfully created, but not mounted\n"));
863 } else if (zfs_share(zhp) != 0) {
864 (void) fprintf(stderr, gettext("filesystem "
865 "successfully created, but not shared\n"));
882 * zfs destroy [-rRf] <fs, vol>
883 * zfs destroy [-rRd] <snap>
885 * -r Recursively destroy all children
886 * -R Recursively destroy all dependents, including clones
887 * -f Force unmounting of any dependents
888 * -d If we can't destroy now, mark for deferred destruction
890 * Destroys the given dataset. By default, it will unmount any filesystems,
891 * and refuse to destroy a dataset that has any dependents. A dependent can
892 * either be a child, or a clone of a child.
894 typedef struct destroy_cbdata {
897 boolean_t cb_recurse;
899 boolean_t cb_doclones;
900 zfs_handle_t *cb_target;
901 boolean_t cb_defer_destroy;
902 boolean_t cb_verbose;
903 boolean_t cb_parsable;
907 /* first snap in contiguous run */
908 zfs_handle_t *cb_firstsnap;
909 /* previous snap in contiguous run */
910 zfs_handle_t *cb_prevsnap;
916 * Check for any dependents based on the '-r' or '-R' flags.
919 destroy_check_dependent(zfs_handle_t *zhp, void *data)
921 destroy_cbdata_t *cbp = data;
922 const char *tname = zfs_get_name(cbp->cb_target);
923 const char *name = zfs_get_name(zhp);
925 if (strncmp(tname, name, strlen(tname)) == 0 &&
926 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
928 * This is a direct descendant, not a clone somewhere else in
935 (void) fprintf(stderr, gettext("cannot destroy '%s': "
936 "%s has children\n"),
937 zfs_get_name(cbp->cb_target),
938 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
939 (void) fprintf(stderr, gettext("use '-r' to destroy "
940 "the following datasets:\n"));
941 cbp->cb_first = B_FALSE;
942 cbp->cb_error = B_TRUE;
945 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
948 * This is a clone. We only want to report this if the '-r'
949 * wasn't specified, or the target is a snapshot.
951 if (!cbp->cb_recurse &&
952 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
956 (void) fprintf(stderr, gettext("cannot destroy '%s': "
957 "%s has dependent clones\n"),
958 zfs_get_name(cbp->cb_target),
959 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
960 (void) fprintf(stderr, gettext("use '-R' to destroy "
961 "the following datasets:\n"));
962 cbp->cb_first = B_FALSE;
963 cbp->cb_error = B_TRUE;
964 cbp->cb_dryrun = B_TRUE;
967 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
976 destroy_callback(zfs_handle_t *zhp, void *data)
978 destroy_cbdata_t *cb = data;
979 const char *name = zfs_get_name(zhp);
981 if (cb->cb_verbose) {
982 if (cb->cb_parsable) {
983 (void) printf("destroy\t%s\n", name);
984 } else if (cb->cb_dryrun) {
985 (void) printf(gettext("would destroy %s\n"),
988 (void) printf(gettext("will destroy %s\n"),
994 * Ignore pools (which we've already flagged as an error before getting
997 if (strchr(zfs_get_name(zhp), '/') == NULL &&
998 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1003 if (!cb->cb_dryrun) {
1004 if (zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1005 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1016 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1018 destroy_cbdata_t *cb = arg;
1019 const char *name = zfs_get_name(zhp);
1022 if (nvlist_exists(cb->cb_nvl, name)) {
1023 if (cb->cb_firstsnap == NULL)
1024 cb->cb_firstsnap = zfs_handle_dup(zhp);
1025 if (cb->cb_prevsnap != NULL)
1026 zfs_close(cb->cb_prevsnap);
1027 /* this snap continues the current range */
1028 cb->cb_prevsnap = zfs_handle_dup(zhp);
1029 if (cb->cb_verbose) {
1030 if (cb->cb_parsable) {
1031 (void) printf("destroy\t%s\n", name);
1032 } else if (cb->cb_dryrun) {
1033 (void) printf(gettext("would destroy %s\n"),
1036 (void) printf(gettext("will destroy %s\n"),
1040 } else if (cb->cb_firstsnap != NULL) {
1041 /* end of this range */
1043 err = zfs_get_snapused_int(cb->cb_firstsnap,
1044 cb->cb_prevsnap, &used);
1045 cb->cb_snapused += used;
1046 zfs_close(cb->cb_firstsnap);
1047 cb->cb_firstsnap = NULL;
1048 zfs_close(cb->cb_prevsnap);
1049 cb->cb_prevsnap = NULL;
1056 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1059 assert(cb->cb_firstsnap == NULL);
1060 assert(cb->cb_prevsnap == NULL);
1061 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1062 if (cb->cb_firstsnap != NULL) {
1065 err = zfs_get_snapused_int(cb->cb_firstsnap,
1066 cb->cb_prevsnap, &used);
1068 cb->cb_snapused += used;
1069 zfs_close(cb->cb_firstsnap);
1070 cb->cb_firstsnap = NULL;
1071 zfs_close(cb->cb_prevsnap);
1072 cb->cb_prevsnap = NULL;
1078 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1080 destroy_cbdata_t *cb = arg;
1083 /* Check for clones. */
1084 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1085 cb->cb_target = zhp;
1086 cb->cb_first = B_TRUE;
1087 err = zfs_iter_dependents(zhp, B_TRUE,
1088 destroy_check_dependent, cb);
1092 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1100 gather_snapshots(zfs_handle_t *zhp, void *arg)
1102 destroy_cbdata_t *cb = arg;
1105 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1111 if (cb->cb_verbose) {
1112 err = destroy_print_snapshots(zhp, cb);
1118 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1126 destroy_clones(destroy_cbdata_t *cb)
1129 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1131 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1132 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1135 boolean_t defer = cb->cb_defer_destroy;
1139 * We can't defer destroy non-snapshots, so set it to
1140 * false while destroying the clones.
1142 cb->cb_defer_destroy = B_FALSE;
1143 err = zfs_iter_dependents(zhp, B_FALSE,
1144 destroy_callback, cb);
1145 cb->cb_defer_destroy = defer;
1155 zfs_do_destroy(int argc, char **argv)
1157 destroy_cbdata_t cb = { 0 };
1161 zfs_type_t type = ZFS_TYPE_DATASET;
1164 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1167 cb.cb_verbose = B_TRUE;
1170 cb.cb_verbose = B_TRUE;
1171 cb.cb_parsable = B_TRUE;
1174 cb.cb_dryrun = B_TRUE;
1177 cb.cb_defer_destroy = B_TRUE;
1178 type = ZFS_TYPE_SNAPSHOT;
1181 cb.cb_force = B_TRUE;
1184 cb.cb_recurse = B_TRUE;
1187 cb.cb_recurse = B_TRUE;
1188 cb.cb_doclones = B_TRUE;
1192 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1201 /* check number of arguments */
1203 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1207 (void) fprintf(stderr, gettext("too many arguments\n"));
1211 at = strchr(argv[0], '@');
1215 /* Build the list of snaps to destroy in cb_nvl. */
1216 if (nvlist_alloc(&cb.cb_nvl, NV_UNIQUE_NAME, 0) != 0)
1220 zhp = zfs_open(g_zfs, argv[0],
1221 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1225 cb.cb_snapspec = at + 1;
1226 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1229 nvlist_free(cb.cb_nvl);
1233 if (nvlist_empty(cb.cb_nvl)) {
1234 (void) fprintf(stderr, gettext("could not find any "
1235 "snapshots to destroy; check snapshot names.\n"));
1237 nvlist_free(cb.cb_nvl);
1241 if (cb.cb_verbose) {
1243 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1244 if (cb.cb_parsable) {
1245 (void) printf("reclaim\t%llu\n",
1247 } else if (cb.cb_dryrun) {
1248 (void) printf(gettext("would reclaim %s\n"),
1251 (void) printf(gettext("will reclaim %s\n"),
1256 if (!cb.cb_dryrun) {
1258 err = destroy_clones(&cb);
1260 err = zfs_destroy_snaps_nvl(zhp, cb.cb_nvl,
1261 cb.cb_defer_destroy);
1266 nvlist_free(cb.cb_nvl);
1270 /* Open the given dataset */
1271 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1277 * Perform an explicit check for pools before going any further.
1279 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1280 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1281 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1282 "operation does not apply to pools\n"),
1284 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1285 "%s' to destroy all datasets in the pool\n"),
1287 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1288 "to destroy the pool itself\n"), zfs_get_name(zhp));
1294 * Check for any dependents and/or clones.
1296 cb.cb_first = B_TRUE;
1297 if (!cb.cb_doclones &&
1298 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1309 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1316 * Do the real thing. The callback will close the
1317 * handle regardless of whether it succeeds or not.
1319 if (destroy_callback(zhp, &cb) != 0)
1327 is_recvd_column(zprop_get_cbdata_t *cbp)
1330 zfs_get_column_t col;
1332 for (i = 0; i < ZFS_GET_NCOLS &&
1333 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1334 if (col == GET_COL_RECVD)
1340 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1341 * < all | property[,property]... > < fs | snap | vol > ...
1343 * -r recurse over any child datasets
1344 * -H scripted mode. Headers are stripped, and fields are separated
1345 * by tabs instead of spaces.
1346 * -o Set of fields to display. One of "name,property,value,
1347 * received,source". Default is "name,property,value,source".
1348 * "all" is an alias for all five.
1349 * -s Set of sources to allow. One of
1350 * "local,default,inherited,received,temporary,none". Default is
1352 * -p Display values in parsable (literal) format.
1354 * Prints properties for the given datasets. The user can control which
1355 * columns to display as well as which property types to allow.
1359 * Invoked to display the properties for a single dataset.
1362 get_callback(zfs_handle_t *zhp, void *data)
1364 char buf[ZFS_MAXPROPLEN];
1365 char rbuf[ZFS_MAXPROPLEN];
1366 zprop_source_t sourcetype;
1367 char source[ZFS_MAXNAMELEN];
1368 zprop_get_cbdata_t *cbp = data;
1369 nvlist_t *user_props = zfs_get_user_props(zhp);
1370 zprop_list_t *pl = cbp->cb_proplist;
1374 boolean_t received = is_recvd_column(cbp);
1376 for (; pl != NULL; pl = pl->pl_next) {
1377 char *recvdval = NULL;
1379 * Skip the special fake placeholder. This will also skip over
1380 * the name property when 'all' is specified.
1382 if (pl->pl_prop == ZFS_PROP_NAME &&
1383 pl == cbp->cb_proplist)
1386 if (pl->pl_prop != ZPROP_INVAL) {
1387 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1388 sizeof (buf), &sourcetype, source,
1390 cbp->cb_literal) != 0) {
1393 if (!zfs_prop_valid_for_type(pl->pl_prop,
1394 ZFS_TYPE_DATASET)) {
1395 (void) fprintf(stderr,
1396 gettext("No such property '%s'\n"),
1397 zfs_prop_to_name(pl->pl_prop));
1400 sourcetype = ZPROP_SRC_NONE;
1401 (void) strlcpy(buf, "-", sizeof (buf));
1404 if (received && (zfs_prop_get_recvd(zhp,
1405 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1406 cbp->cb_literal) == 0))
1409 zprop_print_one_property(zfs_get_name(zhp), cbp,
1410 zfs_prop_to_name(pl->pl_prop),
1411 buf, sourcetype, source, recvdval);
1412 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1413 sourcetype = ZPROP_SRC_LOCAL;
1415 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1416 buf, sizeof (buf), cbp->cb_literal) != 0) {
1417 sourcetype = ZPROP_SRC_NONE;
1418 (void) strlcpy(buf, "-", sizeof (buf));
1421 zprop_print_one_property(zfs_get_name(zhp), cbp,
1422 pl->pl_user_prop, buf, sourcetype, source, NULL);
1423 } else if (zfs_prop_written(pl->pl_user_prop)) {
1424 sourcetype = ZPROP_SRC_LOCAL;
1426 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1427 buf, sizeof (buf), cbp->cb_literal) != 0) {
1428 sourcetype = ZPROP_SRC_NONE;
1429 (void) strlcpy(buf, "-", sizeof (buf));
1432 zprop_print_one_property(zfs_get_name(zhp), cbp,
1433 pl->pl_user_prop, buf, sourcetype, source, NULL);
1435 if (nvlist_lookup_nvlist(user_props,
1436 pl->pl_user_prop, &propval) != 0) {
1439 sourcetype = ZPROP_SRC_NONE;
1442 verify(nvlist_lookup_string(propval,
1443 ZPROP_VALUE, &strval) == 0);
1444 verify(nvlist_lookup_string(propval,
1445 ZPROP_SOURCE, &sourceval) == 0);
1447 if (strcmp(sourceval,
1448 zfs_get_name(zhp)) == 0) {
1449 sourcetype = ZPROP_SRC_LOCAL;
1450 } else if (strcmp(sourceval,
1451 ZPROP_SOURCE_VAL_RECVD) == 0) {
1452 sourcetype = ZPROP_SRC_RECEIVED;
1454 sourcetype = ZPROP_SRC_INHERITED;
1455 (void) strlcpy(source,
1456 sourceval, sizeof (source));
1460 if (received && (zfs_prop_get_recvd(zhp,
1461 pl->pl_user_prop, rbuf, sizeof (rbuf),
1462 cbp->cb_literal) == 0))
1465 zprop_print_one_property(zfs_get_name(zhp), cbp,
1466 pl->pl_user_prop, strval, sourcetype,
1475 zfs_do_get(int argc, char **argv)
1477 zprop_get_cbdata_t cb = { 0 };
1478 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1479 int types = ZFS_TYPE_DATASET;
1480 char *value, *fields;
1483 zprop_list_t fake_name = { 0 };
1486 * Set up default columns and sources.
1488 cb.cb_sources = ZPROP_SRC_ALL;
1489 cb.cb_columns[0] = GET_COL_NAME;
1490 cb.cb_columns[1] = GET_COL_PROPERTY;
1491 cb.cb_columns[2] = GET_COL_VALUE;
1492 cb.cb_columns[3] = GET_COL_SOURCE;
1493 cb.cb_type = ZFS_TYPE_DATASET;
1496 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1499 cb.cb_literal = B_TRUE;
1502 limit = parse_depth(optarg, &flags);
1505 flags |= ZFS_ITER_RECURSE;
1508 cb.cb_scripted = B_TRUE;
1511 (void) fprintf(stderr, gettext("missing argument for "
1512 "'%c' option\n"), optopt);
1517 * Process the set of columns to display. We zero out
1518 * the structure to give us a blank slate.
1520 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1522 while (*optarg != '\0') {
1523 static char *col_subopts[] =
1524 { "name", "property", "value", "received",
1525 "source", "all", NULL };
1527 if (i == ZFS_GET_NCOLS) {
1528 (void) fprintf(stderr, gettext("too "
1529 "many fields given to -o "
1534 switch (getsubopt(&optarg, col_subopts,
1537 cb.cb_columns[i++] = GET_COL_NAME;
1540 cb.cb_columns[i++] = GET_COL_PROPERTY;
1543 cb.cb_columns[i++] = GET_COL_VALUE;
1546 cb.cb_columns[i++] = GET_COL_RECVD;
1547 flags |= ZFS_ITER_RECVD_PROPS;
1550 cb.cb_columns[i++] = GET_COL_SOURCE;
1554 (void) fprintf(stderr,
1555 gettext("\"all\" conflicts "
1556 "with specific fields "
1557 "given to -o option\n"));
1560 cb.cb_columns[0] = GET_COL_NAME;
1561 cb.cb_columns[1] = GET_COL_PROPERTY;
1562 cb.cb_columns[2] = GET_COL_VALUE;
1563 cb.cb_columns[3] = GET_COL_RECVD;
1564 cb.cb_columns[4] = GET_COL_SOURCE;
1565 flags |= ZFS_ITER_RECVD_PROPS;
1569 (void) fprintf(stderr,
1570 gettext("invalid column name "
1579 while (*optarg != '\0') {
1580 static char *source_subopts[] = {
1581 "local", "default", "inherited",
1582 "received", "temporary", "none",
1585 switch (getsubopt(&optarg, source_subopts,
1588 cb.cb_sources |= ZPROP_SRC_LOCAL;
1591 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1594 cb.cb_sources |= ZPROP_SRC_INHERITED;
1597 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1600 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1603 cb.cb_sources |= ZPROP_SRC_NONE;
1606 (void) fprintf(stderr,
1607 gettext("invalid source "
1616 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1617 while (*optarg != '\0') {
1618 static char *type_subopts[] = { "filesystem",
1619 "volume", "snapshot", "all", NULL };
1621 switch (getsubopt(&optarg, type_subopts,
1624 types |= ZFS_TYPE_FILESYSTEM;
1627 types |= ZFS_TYPE_VOLUME;
1630 types |= ZFS_TYPE_SNAPSHOT;
1633 types = ZFS_TYPE_DATASET;
1637 (void) fprintf(stderr,
1638 gettext("invalid type '%s'\n"),
1646 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1656 (void) fprintf(stderr, gettext("missing property "
1663 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1671 * As part of zfs_expand_proplist(), we keep track of the maximum column
1672 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1673 * need to know the maximum name length. However, the user likely did
1674 * not specify 'name' as one of the properties to fetch, so we need to
1675 * make sure we always include at least this property for
1676 * print_get_headers() to work properly.
1678 if (cb.cb_proplist != NULL) {
1679 fake_name.pl_prop = ZFS_PROP_NAME;
1680 fake_name.pl_width = strlen(gettext("NAME"));
1681 fake_name.pl_next = cb.cb_proplist;
1682 cb.cb_proplist = &fake_name;
1685 cb.cb_first = B_TRUE;
1687 /* run for each object */
1688 ret = zfs_for_each(argc, argv, flags, types, NULL,
1689 &cb.cb_proplist, limit, get_callback, &cb);
1691 if (cb.cb_proplist == &fake_name)
1692 zprop_free_list(fake_name.pl_next);
1694 zprop_free_list(cb.cb_proplist);
1700 * inherit [-rS] <property> <fs|vol> ...
1702 * -r Recurse over all children
1703 * -S Revert to received value, if any
1705 * For each dataset specified on the command line, inherit the given property
1706 * from its parent. Inheriting a property at the pool level will cause it to
1707 * use the default value. The '-r' flag will recurse over all children, and is
1708 * useful for setting a property on a hierarchy-wide basis, regardless of any
1709 * local modifications for each dataset.
1712 typedef struct inherit_cbdata {
1713 const char *cb_propname;
1714 boolean_t cb_received;
1718 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1720 inherit_cbdata_t *cb = data;
1721 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1724 * If we're doing it recursively, then ignore properties that
1725 * are not valid for this type of dataset.
1727 if (prop != ZPROP_INVAL &&
1728 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1731 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1735 inherit_cb(zfs_handle_t *zhp, void *data)
1737 inherit_cbdata_t *cb = data;
1739 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1743 zfs_do_inherit(int argc, char **argv)
1747 inherit_cbdata_t cb = { 0 };
1751 boolean_t received = B_FALSE;
1754 while ((c = getopt(argc, argv, "rS")) != -1) {
1757 flags |= ZFS_ITER_RECURSE;
1764 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1773 /* check number of arguments */
1775 (void) fprintf(stderr, gettext("missing property argument\n"));
1779 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1787 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1788 if (zfs_prop_readonly(prop)) {
1789 (void) fprintf(stderr, gettext(
1790 "%s property is read-only\n"),
1794 if (!zfs_prop_inheritable(prop) && !received) {
1795 (void) fprintf(stderr, gettext("'%s' property cannot "
1796 "be inherited\n"), propname);
1797 if (prop == ZFS_PROP_QUOTA ||
1798 prop == ZFS_PROP_RESERVATION ||
1799 prop == ZFS_PROP_REFQUOTA ||
1800 prop == ZFS_PROP_REFRESERVATION)
1801 (void) fprintf(stderr, gettext("use 'zfs set "
1802 "%s=none' to clear\n"), propname);
1805 if (received && (prop == ZFS_PROP_VOLSIZE ||
1806 prop == ZFS_PROP_VERSION)) {
1807 (void) fprintf(stderr, gettext("'%s' property cannot "
1808 "be reverted to a received value\n"), propname);
1811 } else if (!zfs_prop_user(propname)) {
1812 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1817 cb.cb_propname = propname;
1818 cb.cb_received = received;
1820 if (flags & ZFS_ITER_RECURSE) {
1821 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1822 NULL, NULL, 0, inherit_recurse_cb, &cb);
1824 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1825 NULL, NULL, 0, inherit_cb, &cb);
1831 typedef struct upgrade_cbdata {
1832 uint64_t cb_numupgraded;
1833 uint64_t cb_numsamegraded;
1834 uint64_t cb_numfailed;
1835 uint64_t cb_version;
1837 boolean_t cb_foundone;
1838 char cb_lastfs[ZFS_MAXNAMELEN];
1842 same_pool(zfs_handle_t *zhp, const char *name)
1844 int len1 = strcspn(name, "/@");
1845 const char *zhname = zfs_get_name(zhp);
1846 int len2 = strcspn(zhname, "/@");
1850 return (strncmp(name, zhname, len1) == 0);
1854 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1856 upgrade_cbdata_t *cb = data;
1857 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1859 /* list if it's old/new */
1860 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1861 (cb->cb_newer && version > ZPL_VERSION)) {
1864 str = gettext("The following filesystems are "
1865 "formatted using a newer software version and\n"
1866 "cannot be accessed on the current system.\n\n");
1868 str = gettext("The following filesystems are "
1869 "out of date, and can be upgraded. After being\n"
1870 "upgraded, these filesystems (and any 'zfs send' "
1871 "streams generated from\n"
1872 "subsequent snapshots) will no longer be "
1873 "accessible by older software versions.\n\n");
1876 if (!cb->cb_foundone) {
1878 (void) printf(gettext("VER FILESYSTEM\n"));
1879 (void) printf(gettext("--- ------------\n"));
1880 cb->cb_foundone = B_TRUE;
1883 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1890 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1892 upgrade_cbdata_t *cb = data;
1893 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1894 int needed_spa_version;
1897 if (zfs_spa_version(zhp, &spa_version) < 0)
1900 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1902 if (needed_spa_version < 0)
1905 if (spa_version < needed_spa_version) {
1907 (void) printf(gettext("%s: can not be "
1908 "upgraded; the pool version needs to first "
1909 "be upgraded\nto version %d\n\n"),
1910 zfs_get_name(zhp), needed_spa_version);
1916 if (version < cb->cb_version) {
1918 (void) snprintf(verstr, sizeof (verstr),
1919 "%llu", cb->cb_version);
1920 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1922 * If they did "zfs upgrade -a", then we could
1923 * be doing ioctls to different pools. We need
1924 * to log this history once to each pool.
1926 verify(zpool_stage_history(g_zfs, history_str) == 0);
1928 if (zfs_prop_set(zhp, "version", verstr) == 0)
1929 cb->cb_numupgraded++;
1932 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1933 } else if (version > cb->cb_version) {
1934 /* can't downgrade */
1935 (void) printf(gettext("%s: can not be downgraded; "
1936 "it is already at version %u\n"),
1937 zfs_get_name(zhp), version);
1940 cb->cb_numsamegraded++;
1948 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1951 zfs_do_upgrade(int argc, char **argv)
1953 boolean_t all = B_FALSE;
1954 boolean_t showversions = B_FALSE;
1956 upgrade_cbdata_t cb = { 0 };
1958 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1961 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1964 flags |= ZFS_ITER_RECURSE;
1967 showversions = B_TRUE;
1970 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1971 optarg, &cb.cb_version) != 0) {
1972 (void) fprintf(stderr,
1973 gettext("invalid version %s\n"), optarg);
1982 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1991 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1993 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1994 cb.cb_version || argc))
1996 if ((all || argc) && (showversions))
2002 /* Show info on available versions. */
2003 (void) printf(gettext("The following filesystem versions are "
2005 (void) printf(gettext("VER DESCRIPTION\n"));
2006 (void) printf("--- -----------------------------------------"
2007 "---------------\n");
2008 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2009 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2010 (void) printf(gettext(" 3 Case insensitive and filesystem "
2011 "user identifier (FUID)\n"));
2012 (void) printf(gettext(" 4 userquota, groupquota "
2014 (void) printf(gettext(" 5 System attributes\n"));
2015 (void) printf(gettext("\nFor more information on a particular "
2016 "version, including supported releases,\n"));
2017 (void) printf("see the ZFS Administration Guide.\n\n");
2019 } else if (argc || all) {
2020 /* Upgrade filesystems */
2021 if (cb.cb_version == 0)
2022 cb.cb_version = ZPL_VERSION;
2023 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2024 NULL, NULL, 0, upgrade_set_callback, &cb);
2025 (void) printf(gettext("%llu filesystems upgraded\n"),
2027 if (cb.cb_numsamegraded) {
2028 (void) printf(gettext("%llu filesystems already at "
2030 cb.cb_numsamegraded);
2032 if (cb.cb_numfailed != 0)
2035 /* List old-version filesytems */
2037 (void) printf(gettext("This system is currently running "
2038 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2040 flags |= ZFS_ITER_RECURSE;
2041 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2042 NULL, NULL, 0, upgrade_list_callback, &cb);
2044 found = cb.cb_foundone;
2045 cb.cb_foundone = B_FALSE;
2046 cb.cb_newer = B_TRUE;
2048 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2049 NULL, NULL, 0, upgrade_list_callback, &cb);
2051 if (!cb.cb_foundone && !found) {
2052 (void) printf(gettext("All filesystems are "
2053 "formatted with the current version.\n"));
2061 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2062 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2063 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2064 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2066 * -H Scripted mode; elide headers and separate columns by tabs.
2067 * -i Translate SID to POSIX ID.
2068 * -n Print numeric ID instead of user/group name.
2069 * -o Control which fields to display.
2070 * -p Use exact (parseable) numeric output.
2071 * -s Specify sort columns, descending order.
2072 * -S Specify sort columns, ascending order.
2073 * -t Control which object types to display.
2075 * Displays space consumed by, and quotas on, each user in the specified
2076 * filesystem or snapshot.
2079 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2080 enum us_field_types {
2086 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2087 static char *us_field_names[] = { "type", "name", "used", "quota" };
2088 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2090 #define USTYPE_PSX_GRP (1 << 0)
2091 #define USTYPE_PSX_USR (1 << 1)
2092 #define USTYPE_SMB_GRP (1 << 2)
2093 #define USTYPE_SMB_USR (1 << 3)
2094 #define USTYPE_ALL \
2095 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2097 static int us_type_bits[] = {
2104 static char *us_type_names[] = { "posixgroup", "posxiuser", "smbgroup",
2107 typedef struct us_node {
2109 uu_avl_node_t usn_avlnode;
2110 uu_list_node_t usn_listnode;
2113 typedef struct us_cbdata {
2115 uu_avl_pool_t *cb_avl_pool;
2117 boolean_t cb_numname;
2118 boolean_t cb_nicenum;
2119 boolean_t cb_sid2posix;
2120 zfs_userquota_prop_t cb_prop;
2121 zfs_sort_column_t *cb_sortcol;
2122 size_t cb_width[USFIELD_LAST];
2125 static boolean_t us_populated = B_FALSE;
2128 zfs_sort_column_t *si_sortcol;
2129 boolean_t si_numname;
2133 us_field_index(char *field)
2137 for (i = 0; i < USFIELD_LAST; i++) {
2138 if (strcmp(field, us_field_names[i]) == 0)
2146 us_compare(const void *larg, const void *rarg, void *unused)
2148 const us_node_t *l = larg;
2149 const us_node_t *r = rarg;
2150 us_sort_info_t *si = (us_sort_info_t *)unused;
2151 zfs_sort_column_t *sortcol = si->si_sortcol;
2152 boolean_t numname = si->si_numname;
2153 nvlist_t *lnvl = l->usn_nvl;
2154 nvlist_t *rnvl = r->usn_nvl;
2158 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2165 zfs_prop_t prop = sortcol->sc_prop;
2166 const char *propname = NULL;
2167 boolean_t reverse = sortcol->sc_reverse;
2172 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2173 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2175 rc = (rv32 < lv32) ? 1 : -1;
2180 (void) nvlist_lookup_uint64(lnvl, propname,
2182 (void) nvlist_lookup_uint64(rnvl, propname,
2185 rc = (rv64 < lv64) ? 1 : -1;
2187 (void) nvlist_lookup_string(lnvl, propname,
2189 (void) nvlist_lookup_string(rnvl, propname,
2191 rc = strcmp(lvstr, rvstr);
2195 case ZFS_PROP_QUOTA:
2198 if (prop == ZFS_PROP_USED)
2202 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2203 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2205 rc = (rv64 < lv64) ? 1 : -1;
2211 return (reverse ? 1 : -1);
2213 return (reverse ? -1 : 1);
2218 * If entries still seem to be the same, check if they are of the same
2219 * type (smbentity is added only if we are doing SID to POSIX ID
2220 * translation where we can have duplicate type/name combinations).
2222 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2223 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2225 return (lvb < rvb ? -1 : 1);
2230 static inline const char *
2231 us_type2str(unsigned field_type)
2233 switch (field_type) {
2234 case USTYPE_PSX_USR:
2235 return ("POSIX User");
2236 case USTYPE_PSX_GRP:
2237 return ("POSIX Group");
2238 case USTYPE_SMB_USR:
2239 return ("SMB User");
2240 case USTYPE_SMB_GRP:
2241 return ("SMB Group");
2243 return ("Undefined");
2248 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2250 us_cbdata_t *cb = (us_cbdata_t *)arg;
2251 zfs_userquota_prop_t prop = cb->cb_prop;
2256 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2257 uu_avl_t *avl = cb->cb_avl;
2261 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2263 const char *typestr;
2267 int typeidx, nameidx, sizeidx;
2268 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2269 boolean_t smbentity = B_FALSE;
2271 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2273 node = safe_malloc(sizeof (us_node_t));
2274 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2275 node->usn_nvl = props;
2277 if (domain != NULL && domain[0] != '\0') {
2279 char sid[ZFS_MAXNAMELEN + 32];
2284 directory_error_t e;
2289 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2291 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2292 type = USTYPE_SMB_GRP;
2294 err = sid_to_id(sid, B_FALSE, &id);
2297 type = USTYPE_SMB_USR;
2299 err = sid_to_id(sid, B_TRUE, &id);
2306 if (!cb->cb_sid2posix) {
2307 e = directory_name_from_sid(NULL, sid, &name,
2310 directory_error_free(e);
2318 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2320 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2321 type = USTYPE_PSX_GRP;
2322 if (!cb->cb_numname) {
2325 if ((g = getgrgid(rid)) != NULL)
2329 type = USTYPE_PSX_USR;
2330 if (!cb->cb_numname) {
2333 if ((p = getpwuid(rid)) != NULL)
2340 * Make sure that the type/name combination is unique when doing
2341 * SID to POSIX ID translation (hence changing the type from SMB to
2344 if (cb->cb_sid2posix &&
2345 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2348 /* Calculate/update width of TYPE field */
2349 typestr = us_type2str(type);
2350 typelen = strlen(gettext(typestr));
2351 typeidx = us_field_index("type");
2352 if (typelen > cb->cb_width[typeidx])
2353 cb->cb_width[typeidx] = typelen;
2354 if (nvlist_add_uint32(props, "type", type) != 0)
2357 /* Calculate/update width of NAME field */
2358 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2359 if (nvlist_add_uint64(props, "name", rid) != 0)
2361 namelen = snprintf(NULL, 0, "%u", rid);
2363 if (nvlist_add_string(props, "name", name) != 0)
2365 namelen = strlen(name);
2367 nameidx = us_field_index("name");
2368 if (namelen > cb->cb_width[nameidx])
2369 cb->cb_width[nameidx] = namelen;
2372 * Check if this type/name combination is in the list and update it;
2373 * otherwise add new node to the list.
2375 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2376 uu_avl_insert(avl, node, idx);
2381 props = node->usn_nvl;
2384 /* Calculate/update width of USED/QUOTA fields */
2386 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2388 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2389 sizelen = strlen(sizebuf);
2390 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2392 if (!nvlist_exists(props, "quota"))
2393 (void) nvlist_add_uint64(props, "quota", 0);
2396 if (!nvlist_exists(props, "used"))
2397 (void) nvlist_add_uint64(props, "used", 0);
2399 sizeidx = us_field_index(propname);
2400 if (sizelen > cb->cb_width[sizeidx])
2401 cb->cb_width[sizeidx] = sizelen;
2403 if (nvlist_add_uint64(props, propname, space) != 0)
2410 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2411 size_t *width, us_node_t *node)
2413 nvlist_t *nvl = node->usn_nvl;
2414 char valstr[ZFS_MAXNAMELEN];
2415 boolean_t first = B_TRUE;
2421 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2422 if (!(ustype & types))
2425 while ((field = fields[cfield]) != USFIELD_LAST) {
2426 nvpair_t *nvp = NULL;
2430 char *strval = NULL;
2432 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2433 if (strcmp(nvpair_name(nvp),
2434 us_field_names[field]) == 0)
2438 type = nvpair_type(nvp);
2440 case DATA_TYPE_UINT32:
2441 (void) nvpair_value_uint32(nvp, &val32);
2443 case DATA_TYPE_UINT64:
2444 (void) nvpair_value_uint64(nvp, &val64);
2446 case DATA_TYPE_STRING:
2447 (void) nvpair_value_string(nvp, &strval);
2450 (void) fprintf(stderr, "invalid data type\n");
2455 strval = (char *)us_type2str(val32);
2458 if (type == DATA_TYPE_UINT64) {
2459 (void) sprintf(valstr, "%llu", val64);
2465 if (type == DATA_TYPE_UINT64) {
2467 (void) sprintf(valstr, "%llu", val64);
2469 zfs_nicenum(val64, valstr,
2472 if (field == USFIELD_QUOTA &&
2473 strcmp(valstr, "0") == 0)
2483 (void) printf("\t");
2488 (void) printf("%s", strval);
2489 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2490 (void) printf("%-*s", width[field], strval);
2492 (void) printf("%*s", width[field], strval);
2498 (void) printf("\n");
2502 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2503 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2511 boolean_t first = B_TRUE;
2513 while ((field = fields[cfield]) != USFIELD_LAST) {
2514 col = gettext(us_field_hdr[field]);
2515 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2516 (void) printf(first ? "%-*s" : " %-*s",
2519 (void) printf(first ? "%*s" : " %*s",
2525 (void) printf("\n");
2528 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2529 print_us_node(scripted, parsable, fields, types, width, node);
2531 nvlist_free(node->usn_nvl);
2536 zfs_do_userspace(int argc, char **argv)
2539 zfs_userquota_prop_t p;
2541 uu_avl_pool_t *avl_pool;
2543 uu_avl_walk_t *walk;
2545 char deffields[] = "type,name,used,quota";
2546 char *ofield = NULL;
2547 char *tfield = NULL;
2551 boolean_t scripted = B_FALSE;
2552 boolean_t prtnum = B_FALSE;
2553 boolean_t parsable = B_FALSE;
2554 boolean_t sid2posix = B_FALSE;
2557 zfs_sort_column_t *sortcol = NULL;
2558 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2562 uu_list_pool_t *listpool;
2564 uu_avl_index_t idx = 0;
2565 uu_list_index_t idx2 = 0;
2570 if (strcmp(argv[0], "groupspace") == 0)
2571 /* Toggle default group types */
2572 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2574 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2590 if (zfs_add_sort_column(&sortcol, optarg,
2591 c == 's' ? B_FALSE : B_TRUE) != 0) {
2592 (void) fprintf(stderr,
2593 gettext("invalid field '%s'\n"), optarg);
2604 (void) fprintf(stderr, gettext("missing argument for "
2605 "'%c' option\n"), optopt);
2609 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2619 (void) fprintf(stderr, gettext("missing dataset name\n"));
2623 (void) fprintf(stderr, gettext("too many arguments\n"));
2627 /* Use default output fields if not specified using -o */
2631 if ((delim = strchr(ofield, ',')) != NULL)
2633 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2634 (void) fprintf(stderr, gettext("invalid type '%s' "
2635 "for -o option\n"), ofield);
2640 } while (delim != NULL);
2641 fields[cfield] = USFIELD_LAST;
2643 /* Override output types (-t option) */
2644 if (tfield != NULL) {
2648 boolean_t found = B_FALSE;
2650 if ((delim = strchr(tfield, ',')) != NULL)
2652 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2654 if (strcmp(tfield, us_type_names[i]) == 0) {
2656 types |= us_type_bits[i];
2661 (void) fprintf(stderr, gettext("invalid type "
2662 "'%s' for -t option\n"), tfield);
2667 } while (delim != NULL);
2670 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2673 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2674 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2676 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2679 /* Always add default sorting columns */
2680 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2681 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2683 cb.cb_sortcol = sortcol;
2684 cb.cb_numname = prtnum;
2685 cb.cb_nicenum = !parsable;
2686 cb.cb_avl_pool = avl_pool;
2687 cb.cb_avl = avl_tree;
2688 cb.cb_sid2posix = sid2posix;
2690 for (i = 0; i < USFIELD_LAST; i++)
2691 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2693 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2694 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2695 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2696 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2697 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2700 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2705 if ((node = uu_avl_first(avl_tree)) == NULL)
2708 us_populated = B_TRUE;
2710 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2711 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2712 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2713 uu_list_node_init(node, &node->usn_listnode, listpool);
2715 while (node != NULL) {
2717 node = uu_avl_next(avl_tree, node);
2718 uu_avl_remove(avl_tree, rmnode);
2719 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2720 uu_list_insert(list, rmnode, idx2);
2723 for (node = uu_list_first(list); node != NULL;
2724 node = uu_list_next(list, node)) {
2725 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2727 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2728 uu_avl_insert(avl_tree, node, idx);
2731 uu_list_destroy(list);
2732 uu_list_pool_destroy(listpool);
2734 /* Print and free node nvlist memory */
2735 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2738 zfs_free_sort_columns(sortcol);
2740 /* Clean up the AVL tree */
2741 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2744 while ((node = uu_avl_walk_next(walk)) != NULL) {
2745 uu_avl_remove(cb.cb_avl, node);
2749 uu_avl_walk_end(walk);
2750 uu_avl_destroy(avl_tree);
2751 uu_avl_pool_destroy(avl_pool);
2757 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2758 * [-s property [-s property]...] [-S property [-S property]...]
2761 * -r Recurse over all children
2762 * -d Limit recursion by depth.
2763 * -H Scripted mode; elide headers and separate columns by tabs
2764 * -o Control which fields to display.
2765 * -t Control which object types to display.
2766 * -s Specify sort columns, descending order.
2767 * -S Specify sort columns, ascending order.
2769 * When given no arguments, lists all filesystems in the system.
2770 * Otherwise, list the specified datasets, optionally recursing down them if
2771 * '-r' is specified.
2773 typedef struct list_cbdata {
2775 boolean_t cb_scripted;
2776 zprop_list_t *cb_proplist;
2780 * Given a list of columns to display, output appropriate headers for each one.
2783 print_header(zprop_list_t *pl)
2785 char headerbuf[ZFS_MAXPROPLEN];
2788 boolean_t first = B_TRUE;
2789 boolean_t right_justify;
2791 for (; pl != NULL; pl = pl->pl_next) {
2798 right_justify = B_FALSE;
2799 if (pl->pl_prop != ZPROP_INVAL) {
2800 header = zfs_prop_column_name(pl->pl_prop);
2801 right_justify = zfs_prop_align_right(pl->pl_prop);
2803 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2804 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2805 headerbuf[i] = '\0';
2809 if (pl->pl_next == NULL && !right_justify)
2810 (void) printf("%s", header);
2811 else if (right_justify)
2812 (void) printf("%*s", pl->pl_width, header);
2814 (void) printf("%-*s", pl->pl_width, header);
2817 (void) printf("\n");
2821 * Given a dataset and a list of fields, print out all the properties according
2822 * to the described layout.
2825 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
2827 boolean_t first = B_TRUE;
2828 char property[ZFS_MAXPROPLEN];
2829 nvlist_t *userprops = zfs_get_user_props(zhp);
2832 boolean_t right_justify;
2835 for (; pl != NULL; pl = pl->pl_next) {
2838 (void) printf("\t");
2845 if (pl->pl_prop == ZFS_PROP_NAME) {
2846 (void) strlcpy(property, zfs_get_name(zhp),
2849 right_justify = zfs_prop_align_right(pl->pl_prop);
2850 } else if (pl->pl_prop != ZPROP_INVAL) {
2851 if (zfs_prop_get(zhp, pl->pl_prop, property,
2852 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2857 right_justify = zfs_prop_align_right(pl->pl_prop);
2858 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2859 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2860 property, sizeof (property), B_FALSE) != 0)
2864 right_justify = B_TRUE;
2865 } else if (zfs_prop_written(pl->pl_user_prop)) {
2866 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2867 property, sizeof (property), B_FALSE) != 0)
2871 right_justify = B_TRUE;
2873 if (nvlist_lookup_nvlist(userprops,
2874 pl->pl_user_prop, &propval) != 0)
2877 verify(nvlist_lookup_string(propval,
2878 ZPROP_VALUE, &propstr) == 0);
2879 right_justify = B_FALSE;
2882 width = pl->pl_width;
2885 * If this is being called in scripted mode, or if this is the
2886 * last column and it is left-justified, don't include a width
2889 if (scripted || (pl->pl_next == NULL && !right_justify))
2890 (void) printf("%s", propstr);
2891 else if (right_justify)
2892 (void) printf("%*s", width, propstr);
2894 (void) printf("%-*s", width, propstr);
2897 (void) printf("\n");
2901 * Generic callback function to list a dataset or snapshot.
2904 list_callback(zfs_handle_t *zhp, void *data)
2906 list_cbdata_t *cbp = data;
2908 if (cbp->cb_first) {
2909 if (!cbp->cb_scripted)
2910 print_header(cbp->cb_proplist);
2911 cbp->cb_first = B_FALSE;
2914 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2920 zfs_do_list(int argc, char **argv)
2923 boolean_t scripted = B_FALSE;
2924 static char default_fields[] =
2925 "name,used,available,referenced,mountpoint";
2926 int types = ZFS_TYPE_DATASET;
2927 boolean_t types_specified = B_FALSE;
2928 char *fields = NULL;
2929 list_cbdata_t cb = { 0 };
2933 zfs_sort_column_t *sortcol = NULL;
2934 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2937 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2943 limit = parse_depth(optarg, &flags);
2946 flags |= ZFS_ITER_RECURSE;
2952 if (zfs_add_sort_column(&sortcol, optarg,
2954 (void) fprintf(stderr,
2955 gettext("invalid property '%s'\n"), optarg);
2960 if (zfs_add_sort_column(&sortcol, optarg,
2962 (void) fprintf(stderr,
2963 gettext("invalid property '%s'\n"), optarg);
2969 types_specified = B_TRUE;
2970 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
2971 while (*optarg != '\0') {
2972 static char *type_subopts[] = { "filesystem",
2973 "volume", "snapshot", "all", NULL };
2975 switch (getsubopt(&optarg, type_subopts,
2978 types |= ZFS_TYPE_FILESYSTEM;
2981 types |= ZFS_TYPE_VOLUME;
2984 types |= ZFS_TYPE_SNAPSHOT;
2987 types = ZFS_TYPE_DATASET;
2991 (void) fprintf(stderr,
2992 gettext("invalid type '%s'\n"),
2999 (void) fprintf(stderr, gettext("missing argument for "
3000 "'%c' option\n"), optopt);
3004 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3014 fields = default_fields;
3017 * If we are only going to list snapshot names and sort by name,
3018 * then we can use faster version.
3020 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3021 flags |= ZFS_ITER_SIMPLE;
3024 * If "-o space" and no types were specified, don't display snapshots.
3026 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3027 types &= ~ZFS_TYPE_SNAPSHOT;
3030 * If the user specifies '-o all', the zprop_get_list() doesn't
3031 * normally include the name of the dataset. For 'zfs list', we always
3032 * want this property to be first.
3034 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3038 cb.cb_scripted = scripted;
3039 cb.cb_first = B_TRUE;
3041 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3042 limit, list_callback, &cb);
3044 zprop_free_list(cb.cb_proplist);
3045 zfs_free_sort_columns(sortcol);
3047 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3048 (void) printf(gettext("no datasets available\n"));
3054 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3055 * zfs rename [-f] -p <fs | vol> <fs | vol>
3056 * zfs rename -r <snap> <snap>
3057 * zfs rename -u [-p] <fs> <fs>
3059 * Renames the given dataset to another of the same type.
3061 * The '-p' flag creates all the non-existing ancestors of the target first.
3065 zfs_do_rename(int argc, char **argv)
3068 renameflags_t flags = { 0 };
3072 boolean_t parents = B_FALSE;
3075 while ((c = getopt(argc, argv, "fpru")) != -1) {
3081 flags.recurse = B_TRUE;
3084 flags.nounmount = B_TRUE;
3087 flags.forceunmount = B_TRUE;
3091 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3100 /* check number of arguments */
3102 (void) fprintf(stderr, gettext("missing source dataset "
3107 (void) fprintf(stderr, gettext("missing target dataset "
3112 (void) fprintf(stderr, gettext("too many arguments\n"));
3116 if (flags.recurse && parents) {
3117 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3122 if (flags.recurse && strchr(argv[0], '@') == 0) {
3123 (void) fprintf(stderr, gettext("source dataset for recursive "
3124 "rename must be a snapshot\n"));
3128 if (flags.nounmount && parents) {
3129 (void) fprintf(stderr, gettext("-u and -r options are mutually "
3134 if (flags.nounmount)
3135 types = ZFS_TYPE_FILESYSTEM;
3137 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3139 types = ZFS_TYPE_DATASET;
3141 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3144 /* If we were asked and the name looks good, try to create ancestors. */
3145 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3146 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3151 ret = (zfs_rename(zhp, argv[1], flags) != 0);
3160 * Promotes the given clone fs to be the parent
3164 zfs_do_promote(int argc, char **argv)
3170 if (argc > 1 && argv[1][0] == '-') {
3171 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3176 /* check number of arguments */
3178 (void) fprintf(stderr, gettext("missing clone filesystem"
3183 (void) fprintf(stderr, gettext("too many arguments\n"));
3187 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3191 ret = (zfs_promote(zhp) != 0);
3199 * zfs rollback [-rRf] <snapshot>
3201 * -r Delete any intervening snapshots before doing rollback
3202 * -R Delete any snapshots and their clones
3203 * -f ignored for backwards compatability
3205 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3206 * since then and making it the active dataset. If more recent snapshots exist,
3207 * the command will complain unless the '-r' flag is given.
3209 typedef struct rollback_cbdata {
3215 boolean_t cb_recurse;
3216 boolean_t cb_dependent;
3217 } rollback_cbdata_t;
3220 * Report any snapshots more recent than the one specified. Used when '-r' is
3221 * not specified. We reuse this same callback for the snapshot dependents - if
3222 * 'cb_dependent' is set, then this is a dependent and we should report it
3223 * without checking the transaction group.
3226 rollback_check(zfs_handle_t *zhp, void *data)
3228 rollback_cbdata_t *cbp = data;
3230 if (cbp->cb_doclones) {
3235 if (!cbp->cb_dependent) {
3236 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
3237 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3238 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3241 if (cbp->cb_first && !cbp->cb_recurse) {
3242 (void) fprintf(stderr, gettext("cannot "
3243 "rollback to '%s': more recent snapshots "
3246 (void) fprintf(stderr, gettext("use '-r' to "
3247 "force deletion of the following "
3253 if (cbp->cb_recurse) {
3254 cbp->cb_dependent = B_TRUE;
3255 if (zfs_iter_dependents(zhp, B_TRUE,
3256 rollback_check, cbp) != 0) {
3260 cbp->cb_dependent = B_FALSE;
3262 (void) fprintf(stderr, "%s\n",
3267 if (cbp->cb_first && cbp->cb_recurse) {
3268 (void) fprintf(stderr, gettext("cannot rollback to "
3269 "'%s': clones of previous snapshots exist\n"),
3271 (void) fprintf(stderr, gettext("use '-R' to "
3272 "force deletion of the following clones and "
3278 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3286 zfs_do_rollback(int argc, char **argv)
3290 boolean_t force = B_FALSE;
3291 rollback_cbdata_t cb = { 0 };
3292 zfs_handle_t *zhp, *snap;
3293 char parentname[ZFS_MAXNAMELEN];
3297 while ((c = getopt(argc, argv, "rRf")) != -1) {
3310 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3319 /* check number of arguments */
3321 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3325 (void) fprintf(stderr, gettext("too many arguments\n"));
3329 /* open the snapshot */
3330 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3333 /* open the parent dataset */
3334 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3335 verify((delim = strrchr(parentname, '@')) != NULL);
3337 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3343 * Check for more recent snapshots and/or clones based on the presence
3346 cb.cb_target = argv[0];
3347 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3348 cb.cb_first = B_TRUE;
3350 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
3353 if ((ret = cb.cb_error) != 0)
3357 * Rollback parent to the given snapshot.
3359 ret = zfs_rollback(zhp, snap, force);
3372 * zfs set property=value { fs | snap | vol } ...
3374 * Sets the given property for all datasets specified on the command line.
3376 typedef struct set_cbdata {
3382 set_callback(zfs_handle_t *zhp, void *data)
3384 set_cbdata_t *cbp = data;
3386 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3387 switch (libzfs_errno(g_zfs)) {
3388 case EZFS_MOUNTFAILED:
3389 (void) fprintf(stderr, gettext("property may be set "
3390 "but unable to remount filesystem\n"));
3392 case EZFS_SHARENFSFAILED:
3393 (void) fprintf(stderr, gettext("property may be set "
3394 "but unable to reshare filesystem\n"));
3403 zfs_do_set(int argc, char **argv)
3408 /* check for options */
3409 if (argc > 1 && argv[1][0] == '-') {
3410 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3415 /* check number of arguments */
3417 (void) fprintf(stderr, gettext("missing property=value "
3422 (void) fprintf(stderr, gettext("missing dataset name\n"));
3426 /* validate property=value argument */
3427 cb.cb_propname = argv[1];
3428 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3429 (cb.cb_value[1] == '\0')) {
3430 (void) fprintf(stderr, gettext("missing value in "
3431 "property=value argument\n"));
3435 *cb.cb_value = '\0';
3438 if (*cb.cb_propname == '\0') {
3439 (void) fprintf(stderr,
3440 gettext("missing property in property=value argument\n"));
3444 ret = zfs_for_each(argc - 2, argv + 2, 0,
3445 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3451 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3453 * Creates a snapshot with the given name. While functionally equivalent to
3454 * 'zfs create', it is a separate command to differentiate intent.
3457 zfs_do_snapshot(int argc, char **argv)
3459 boolean_t recursive = B_FALSE;
3464 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3468 while ((c = getopt(argc, argv, "ro:")) != -1) {
3471 if (parseprop(props))
3478 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3487 /* check number of arguments */
3489 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3493 (void) fprintf(stderr, gettext("too many arguments\n"));
3497 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
3499 if (ret && recursive)
3500 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3510 * Send a backup stream to stdout.
3513 zfs_do_send(int argc, char **argv)
3515 char *fromname = NULL;
3516 char *toname = NULL;
3519 sendflags_t flags = { 0 };
3521 nvlist_t *dbgnv = NULL;
3522 boolean_t extraverbose = B_FALSE;
3525 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3536 flags.doall = B_TRUE;
3539 flags.replicate = B_TRUE;
3542 flags.props = B_TRUE;
3545 flags.parsable = B_TRUE;
3546 flags.verbose = B_TRUE;
3550 extraverbose = B_TRUE;
3551 flags.verbose = B_TRUE;
3552 flags.progress = B_TRUE;
3555 flags.dedup = B_TRUE;
3558 flags.dryrun = B_TRUE;
3561 (void) fprintf(stderr, gettext("missing argument for "
3562 "'%c' option\n"), optopt);
3566 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3575 /* check number of arguments */
3577 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3581 (void) fprintf(stderr, gettext("too many arguments\n"));
3585 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3586 (void) fprintf(stderr,
3587 gettext("Error: Stream can not be written to a terminal.\n"
3588 "You must redirect standard output.\n"));
3592 cp = strchr(argv[0], '@');
3594 (void) fprintf(stderr,
3595 gettext("argument must be a snapshot\n"));
3600 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3605 * If they specified the full path to the snapshot, chop off
3606 * everything except the short name of the snapshot, but special
3607 * case if they specify the origin.
3609 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3610 char origin[ZFS_MAXNAMELEN];
3613 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3614 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3616 if (strcmp(origin, fromname) == 0) {
3618 flags.fromorigin = B_TRUE;
3621 if (cp != fromname && strcmp(argv[0], fromname)) {
3622 (void) fprintf(stderr,
3623 gettext("incremental source must be "
3624 "in same filesystem\n"));
3628 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3629 (void) fprintf(stderr,
3630 gettext("invalid incremental source\n"));
3636 if (flags.replicate && fromname == NULL)
3637 flags.doall = B_TRUE;
3639 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3640 extraverbose ? &dbgnv : NULL);
3642 if (extraverbose && dbgnv != NULL) {
3644 * dump_nvlist prints to stdout, but that's been
3645 * redirected to a file. Make it print to stderr
3648 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3649 dump_nvlist(dbgnv, 0);
3658 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3660 * Restore a backup stream from stdin.
3663 zfs_do_receive(int argc, char **argv)
3666 recvflags_t flags = { 0 };
3669 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3672 flags.isprefix = B_TRUE;
3675 flags.isprefix = B_TRUE;
3676 flags.istail = B_TRUE;
3679 flags.dryrun = B_TRUE;
3682 flags.nomount = B_TRUE;
3685 flags.verbose = B_TRUE;
3688 flags.force = B_TRUE;
3691 (void) fprintf(stderr, gettext("missing argument for "
3692 "'%c' option\n"), optopt);
3696 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3705 /* check number of arguments */
3707 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3711 (void) fprintf(stderr, gettext("too many arguments\n"));
3715 if (isatty(STDIN_FILENO)) {
3716 (void) fprintf(stderr,
3717 gettext("Error: Backup stream can not be read "
3718 "from a terminal.\n"
3719 "You must redirect standard input.\n"));
3723 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3729 * allow/unallow stuff
3731 /* copied from zfs/sys/dsl_deleg.h */
3732 #define ZFS_DELEG_PERM_CREATE "create"
3733 #define ZFS_DELEG_PERM_DESTROY "destroy"
3734 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3735 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3736 #define ZFS_DELEG_PERM_CLONE "clone"
3737 #define ZFS_DELEG_PERM_PROMOTE "promote"
3738 #define ZFS_DELEG_PERM_RENAME "rename"
3739 #define ZFS_DELEG_PERM_MOUNT "mount"
3740 #define ZFS_DELEG_PERM_SHARE "share"
3741 #define ZFS_DELEG_PERM_SEND "send"
3742 #define ZFS_DELEG_PERM_RECEIVE "receive"
3743 #define ZFS_DELEG_PERM_ALLOW "allow"
3744 #define ZFS_DELEG_PERM_USERPROP "userprop"
3745 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3746 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3747 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3748 #define ZFS_DELEG_PERM_USERUSED "userused"
3749 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3750 #define ZFS_DELEG_PERM_HOLD "hold"
3751 #define ZFS_DELEG_PERM_RELEASE "release"
3752 #define ZFS_DELEG_PERM_DIFF "diff"
3754 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3756 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3757 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3758 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3759 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3760 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3761 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3762 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3763 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3764 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3765 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3766 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3767 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3768 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3769 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3770 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3771 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3773 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3774 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3775 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3776 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3777 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3778 { NULL, ZFS_DELEG_NOTE_NONE }
3781 /* permission structure */
3782 typedef struct deleg_perm {
3783 zfs_deleg_who_type_t dp_who_type;
3784 const char *dp_name;
3786 boolean_t dp_descend;
3790 typedef struct deleg_perm_node {
3791 deleg_perm_t dpn_perm;
3793 uu_avl_node_t dpn_avl_node;
3794 } deleg_perm_node_t;
3796 typedef struct fs_perm fs_perm_t;
3798 /* permissions set */
3799 typedef struct who_perm {
3800 zfs_deleg_who_type_t who_type;
3801 const char *who_name; /* id */
3802 char who_ug_name[256]; /* user/group name */
3803 fs_perm_t *who_fsperm; /* uplink */
3805 uu_avl_t *who_deleg_perm_avl; /* permissions */
3809 typedef struct who_perm_node {
3810 who_perm_t who_perm;
3811 uu_avl_node_t who_avl_node;
3814 typedef struct fs_perm_set fs_perm_set_t;
3815 /* fs permissions */
3817 const char *fsp_name;
3819 uu_avl_t *fsp_sc_avl; /* sets,create */
3820 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3822 fs_perm_set_t *fsp_set; /* uplink */
3826 typedef struct fs_perm_node {
3827 fs_perm_t fspn_fsperm;
3830 uu_list_node_t fspn_list_node;
3833 /* top level structure */
3834 struct fs_perm_set {
3835 uu_list_pool_t *fsps_list_pool;
3836 uu_list_t *fsps_list; /* list of fs_perms */
3838 uu_avl_pool_t *fsps_named_set_avl_pool;
3839 uu_avl_pool_t *fsps_who_perm_avl_pool;
3840 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
3843 static inline const char *
3844 deleg_perm_type(zfs_deleg_note_t note)
3850 case ZFS_DELEG_NOTE_GROUPQUOTA:
3851 case ZFS_DELEG_NOTE_GROUPUSED:
3852 case ZFS_DELEG_NOTE_USERPROP:
3853 case ZFS_DELEG_NOTE_USERQUOTA:
3854 case ZFS_DELEG_NOTE_USERUSED:
3856 return (gettext("other"));
3858 return (gettext("subcommand"));
3863 who_type2weight(zfs_deleg_who_type_t who_type)
3867 case ZFS_DELEG_NAMED_SET_SETS:
3868 case ZFS_DELEG_NAMED_SET:
3871 case ZFS_DELEG_CREATE_SETS:
3872 case ZFS_DELEG_CREATE:
3875 case ZFS_DELEG_USER_SETS:
3876 case ZFS_DELEG_USER:
3879 case ZFS_DELEG_GROUP_SETS:
3880 case ZFS_DELEG_GROUP:
3883 case ZFS_DELEG_EVERYONE_SETS:
3884 case ZFS_DELEG_EVERYONE:
3896 who_perm_compare(const void *larg, const void *rarg, void *unused)
3898 const who_perm_node_t *l = larg;
3899 const who_perm_node_t *r = rarg;
3900 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
3901 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
3902 int lweight = who_type2weight(ltype);
3903 int rweight = who_type2weight(rtype);
3904 int res = lweight - rweight;
3906 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
3907 ZFS_MAX_DELEG_NAME-1);
3919 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
3921 const deleg_perm_node_t *l = larg;
3922 const deleg_perm_node_t *r = rarg;
3923 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
3924 ZFS_MAX_DELEG_NAME-1);
3936 fs_perm_set_init(fs_perm_set_t *fspset)
3938 bzero(fspset, sizeof (fs_perm_set_t));
3940 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
3941 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
3942 NULL, UU_DEFAULT)) == NULL)
3944 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
3945 UU_DEFAULT)) == NULL)
3948 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
3949 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
3950 who_perm_node_t, who_avl_node), who_perm_compare,
3951 UU_DEFAULT)) == NULL)
3954 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
3955 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
3956 who_perm_node_t, who_avl_node), who_perm_compare,
3957 UU_DEFAULT)) == NULL)
3960 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
3961 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
3962 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
3967 static inline void fs_perm_fini(fs_perm_t *);
3968 static inline void who_perm_fini(who_perm_t *);
3971 fs_perm_set_fini(fs_perm_set_t *fspset)
3973 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
3975 while (node != NULL) {
3976 fs_perm_node_t *next_node =
3977 uu_list_next(fspset->fsps_list, node);
3978 fs_perm_t *fsperm = &node->fspn_fsperm;
3979 fs_perm_fini(fsperm);
3980 uu_list_remove(fspset->fsps_list, node);
3985 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
3986 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
3987 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
3991 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
3994 deleg_perm->dp_who_type = type;
3995 deleg_perm->dp_name = name;
3999 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4000 zfs_deleg_who_type_t type, const char *name)
4002 uu_avl_pool_t *pool;
4003 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4005 bzero(who_perm, sizeof (who_perm_t));
4007 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4008 UU_DEFAULT)) == NULL)
4011 who_perm->who_type = type;
4012 who_perm->who_name = name;
4013 who_perm->who_fsperm = fsperm;
4017 who_perm_fini(who_perm_t *who_perm)
4019 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4021 while (node != NULL) {
4022 deleg_perm_node_t *next_node =
4023 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4025 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4030 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4034 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4036 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4037 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4039 bzero(fsperm, sizeof (fs_perm_t));
4041 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4045 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4049 fsperm->fsp_set = fspset;
4050 fsperm->fsp_name = fsname;
4054 fs_perm_fini(fs_perm_t *fsperm)
4056 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4057 while (node != NULL) {
4058 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4060 who_perm_t *who_perm = &node->who_perm;
4061 who_perm_fini(who_perm);
4062 uu_avl_remove(fsperm->fsp_sc_avl, node);
4067 node = uu_avl_first(fsperm->fsp_uge_avl);
4068 while (node != NULL) {
4069 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4071 who_perm_t *who_perm = &node->who_perm;
4072 who_perm_fini(who_perm);
4073 uu_avl_remove(fsperm->fsp_uge_avl, node);
4078 uu_avl_destroy(fsperm->fsp_sc_avl);
4079 uu_avl_destroy(fsperm->fsp_uge_avl);
4083 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4084 zfs_deleg_who_type_t who_type, const char *name, char locality)
4086 uu_avl_index_t idx = 0;
4088 deleg_perm_node_t *found_node = NULL;
4089 deleg_perm_t *deleg_perm = &node->dpn_perm;
4091 deleg_perm_init(deleg_perm, who_type, name);
4093 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4095 uu_avl_insert(avl, node, idx);
4098 deleg_perm = &node->dpn_perm;
4103 case ZFS_DELEG_LOCAL:
4104 deleg_perm->dp_local = B_TRUE;
4106 case ZFS_DELEG_DESCENDENT:
4107 deleg_perm->dp_descend = B_TRUE;
4112 assert(B_FALSE); /* invalid locality */
4117 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4119 nvpair_t *nvp = NULL;
4120 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4121 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4122 zfs_deleg_who_type_t who_type = who_perm->who_type;
4124 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4125 const char *name = nvpair_name(nvp);
4126 data_type_t type = nvpair_type(nvp);
4127 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4128 deleg_perm_node_t *node =
4129 safe_malloc(sizeof (deleg_perm_node_t));
4131 assert(type == DATA_TYPE_BOOLEAN);
4133 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4134 set_deleg_perm_node(avl, node, who_type, name, locality);
4141 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4143 nvpair_t *nvp = NULL;
4144 fs_perm_set_t *fspset = fsperm->fsp_set;
4146 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4147 nvlist_t *nvl2 = NULL;
4148 const char *name = nvpair_name(nvp);
4149 uu_avl_t *avl = NULL;
4150 uu_avl_pool_t *avl_pool;
4151 zfs_deleg_who_type_t perm_type = name[0];
4152 char perm_locality = name[1];
4153 const char *perm_name = name + 3;
4154 boolean_t is_set = B_TRUE;
4155 who_perm_t *who_perm = NULL;
4157 assert('$' == name[2]);
4159 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4162 switch (perm_type) {
4163 case ZFS_DELEG_CREATE:
4164 case ZFS_DELEG_CREATE_SETS:
4165 case ZFS_DELEG_NAMED_SET:
4166 case ZFS_DELEG_NAMED_SET_SETS:
4167 avl_pool = fspset->fsps_named_set_avl_pool;
4168 avl = fsperm->fsp_sc_avl;
4170 case ZFS_DELEG_USER:
4171 case ZFS_DELEG_USER_SETS:
4172 case ZFS_DELEG_GROUP:
4173 case ZFS_DELEG_GROUP_SETS:
4174 case ZFS_DELEG_EVERYONE:
4175 case ZFS_DELEG_EVERYONE_SETS:
4176 avl_pool = fspset->fsps_who_perm_avl_pool;
4177 avl = fsperm->fsp_uge_avl;
4182 who_perm_node_t *found_node = NULL;
4183 who_perm_node_t *node = safe_malloc(
4184 sizeof (who_perm_node_t));
4185 who_perm = &node->who_perm;
4186 uu_avl_index_t idx = 0;
4188 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4189 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4191 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4193 if (avl == fsperm->fsp_uge_avl) {
4195 struct passwd *p = NULL;
4196 struct group *g = NULL;
4197 const char *nice_name = NULL;
4199 switch (perm_type) {
4200 case ZFS_DELEG_USER_SETS:
4201 case ZFS_DELEG_USER:
4202 rid = atoi(perm_name);
4205 nice_name = p->pw_name;
4207 case ZFS_DELEG_GROUP_SETS:
4208 case ZFS_DELEG_GROUP:
4209 rid = atoi(perm_name);
4212 nice_name = g->gr_name;
4216 if (nice_name != NULL)
4218 node->who_perm.who_ug_name,
4222 uu_avl_insert(avl, node, idx);
4225 who_perm = &node->who_perm;
4229 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4236 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4238 nvpair_t *nvp = NULL;
4239 uu_avl_index_t idx = 0;
4241 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4242 nvlist_t *nvl2 = NULL;
4243 const char *fsname = nvpair_name(nvp);
4244 data_type_t type = nvpair_type(nvp);
4245 fs_perm_t *fsperm = NULL;
4246 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4250 fsperm = &node->fspn_fsperm;
4252 assert(DATA_TYPE_NVLIST == type);
4254 uu_list_node_init(node, &node->fspn_list_node,
4255 fspset->fsps_list_pool);
4257 idx = uu_list_numnodes(fspset->fsps_list);
4258 fs_perm_init(fsperm, fspset, fsname);
4260 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4263 (void) parse_fs_perm(fsperm, nvl2);
4265 uu_list_insert(fspset->fsps_list, node, idx);
4271 static inline const char *
4272 deleg_perm_comment(zfs_deleg_note_t note)
4274 const char *str = "";
4279 case ZFS_DELEG_NOTE_ALLOW:
4280 str = gettext("Must also have the permission that is being"
4281 "\n\t\t\t\tallowed");
4283 case ZFS_DELEG_NOTE_CLONE:
4284 str = gettext("Must also have the 'create' ability and 'mount'"
4285 "\n\t\t\t\tability in the origin file system");
4287 case ZFS_DELEG_NOTE_CREATE:
4288 str = gettext("Must also have the 'mount' ability");
4290 case ZFS_DELEG_NOTE_DESTROY:
4291 str = gettext("Must also have the 'mount' ability");
4293 case ZFS_DELEG_NOTE_DIFF:
4294 str = gettext("Allows lookup of paths within a dataset;"
4295 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4296 "\n\t\t\t\tin order to use zfs diff");
4298 case ZFS_DELEG_NOTE_HOLD:
4299 str = gettext("Allows adding a user hold to a snapshot");
4301 case ZFS_DELEG_NOTE_MOUNT:
4302 str = gettext("Allows mount/umount of ZFS datasets");
4304 case ZFS_DELEG_NOTE_PROMOTE:
4305 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4306 " 'promote' ability in the origin file system");
4308 case ZFS_DELEG_NOTE_RECEIVE:
4309 str = gettext("Must also have the 'mount' and 'create'"
4312 case ZFS_DELEG_NOTE_RELEASE:
4313 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4314 "might destroy the snapshot");
4316 case ZFS_DELEG_NOTE_RENAME:
4317 str = gettext("Must also have the 'mount' and 'create'"
4318 "\n\t\t\t\tability in the new parent");
4320 case ZFS_DELEG_NOTE_ROLLBACK:
4323 case ZFS_DELEG_NOTE_SEND:
4326 case ZFS_DELEG_NOTE_SHARE:
4327 str = gettext("Allows sharing file systems over NFS or SMB"
4328 "\n\t\t\t\tprotocols");
4330 case ZFS_DELEG_NOTE_SNAPSHOT:
4334 * case ZFS_DELEG_NOTE_VSCAN:
4335 * str = gettext("");
4339 case ZFS_DELEG_NOTE_GROUPQUOTA:
4340 str = gettext("Allows accessing any groupquota@... property");
4342 case ZFS_DELEG_NOTE_GROUPUSED:
4343 str = gettext("Allows reading any groupused@... property");
4345 case ZFS_DELEG_NOTE_USERPROP:
4346 str = gettext("Allows changing any user property");
4348 case ZFS_DELEG_NOTE_USERQUOTA:
4349 str = gettext("Allows accessing any userquota@... property");
4351 case ZFS_DELEG_NOTE_USERUSED:
4352 str = gettext("Allows reading any userused@... property");
4370 boolean_t recursive; /* unallow only */
4371 boolean_t prt_usage;
4373 boolean_t prt_perms;
4376 const char *dataset;
4380 prop_cmp(const void *a, const void *b)
4382 const char *str1 = *(const char **)a;
4383 const char *str2 = *(const char **)b;
4384 return (strcmp(str1, str2));
4388 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4390 const char *opt_desc[] = {
4391 "-h", gettext("show this help message and exit"),
4392 "-l", gettext("set permission locally"),
4393 "-d", gettext("set permission for descents"),
4394 "-u", gettext("set permission for user"),
4395 "-g", gettext("set permission for group"),
4396 "-e", gettext("set permission for everyone"),
4397 "-c", gettext("set create time permission"),
4398 "-s", gettext("define permission set"),
4400 "-r", gettext("remove permissions recursively"),
4402 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4403 size_t allow_size = unallow_size - 2;
4404 const char *props[ZFS_NUM_PROPS];
4407 FILE *fp = requested ? stdout : stderr;
4408 zprop_desc_t *pdtbl = zfs_prop_get_table();
4409 const char *fmt = gettext("%-16s %-14s\t%s\n");
4411 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4413 (void) fprintf(fp, gettext("Options:\n"));
4414 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4415 const char *opt = opt_desc[i++];
4416 const char *optdsc = opt_desc[i];
4417 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4420 (void) fprintf(fp, gettext("\nThe following permissions are "
4422 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4424 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4425 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4426 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4427 const char *perm_type = deleg_perm_type(perm_note);
4428 const char *perm_comment = deleg_perm_comment(perm_note);
4429 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4432 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4433 zprop_desc_t *pd = &pdtbl[i];
4434 if (pd->pd_visible != B_TRUE)
4437 if (pd->pd_attr == PROP_READONLY)
4440 props[count++] = pd->pd_name;
4442 props[count] = NULL;
4444 qsort(props, count, sizeof (char *), prop_cmp);
4446 for (i = 0; i < count; i++)
4447 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4450 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4452 exit(requested ? 0 : 2);
4455 static inline const char *
4456 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4459 if (un && argc == expected_argc - 1)
4461 else if (argc == expected_argc)
4462 *permsp = argv[argc - 2];
4464 allow_usage(un, B_FALSE,
4465 gettext("wrong number of parameters\n"));
4467 return (argv[argc - 1]);
4471 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4473 int uge_sum = opts->user + opts->group + opts->everyone;
4474 int csuge_sum = opts->create + opts->set + uge_sum;
4475 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4476 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4479 allow_usage(un, B_FALSE,
4480 gettext("-u, -g, and -e are mutually exclusive\n"));
4482 if (opts->prt_usage)
4483 if (argc == 0 && all_sum == 0)
4484 allow_usage(un, B_TRUE, NULL);
4490 allow_usage(un, B_FALSE,
4491 gettext("invalid options combined with -s\n"));
4493 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4494 if (argv[0][0] != '@')
4495 allow_usage(un, B_FALSE,
4496 gettext("invalid set name: missing '@' prefix\n"));
4497 opts->who = argv[0];
4498 } else if (opts->create) {
4499 if (ldcsuge_sum > 1)
4500 allow_usage(un, B_FALSE,
4501 gettext("invalid options combined with -c\n"));
4502 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4503 } else if (opts->everyone) {
4505 allow_usage(un, B_FALSE,
4506 gettext("invalid options combined with -e\n"));
4507 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4508 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4510 opts->everyone = B_TRUE;
4513 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4514 } else if (argc == 1 && !un) {
4515 opts->prt_perms = B_TRUE;
4516 opts->dataset = argv[argc-1];
4518 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4519 opts->who = argv[0];
4522 if (!opts->local && !opts->descend) {
4523 opts->local = B_TRUE;
4524 opts->descend = B_TRUE;
4529 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4530 const char *who, char *perms, nvlist_t *top_nvl)
4533 char ld[2] = { '\0', '\0' };
4534 char who_buf[ZFS_MAXNAMELEN+32];
4537 nvlist_t *base_nvl = NULL;
4538 nvlist_t *set_nvl = NULL;
4541 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4543 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4547 case ZFS_DELEG_NAMED_SET_SETS:
4548 case ZFS_DELEG_NAMED_SET:
4549 set_type = ZFS_DELEG_NAMED_SET_SETS;
4550 base_type = ZFS_DELEG_NAMED_SET;
4551 ld[0] = ZFS_DELEG_NA;
4553 case ZFS_DELEG_CREATE_SETS:
4554 case ZFS_DELEG_CREATE:
4555 set_type = ZFS_DELEG_CREATE_SETS;
4556 base_type = ZFS_DELEG_CREATE;
4557 ld[0] = ZFS_DELEG_NA;
4559 case ZFS_DELEG_USER_SETS:
4560 case ZFS_DELEG_USER:
4561 set_type = ZFS_DELEG_USER_SETS;
4562 base_type = ZFS_DELEG_USER;
4564 ld[0] = ZFS_DELEG_LOCAL;
4566 ld[1] = ZFS_DELEG_DESCENDENT;
4568 case ZFS_DELEG_GROUP_SETS:
4569 case ZFS_DELEG_GROUP:
4570 set_type = ZFS_DELEG_GROUP_SETS;
4571 base_type = ZFS_DELEG_GROUP;
4573 ld[0] = ZFS_DELEG_LOCAL;
4575 ld[1] = ZFS_DELEG_DESCENDENT;
4577 case ZFS_DELEG_EVERYONE_SETS:
4578 case ZFS_DELEG_EVERYONE:
4579 set_type = ZFS_DELEG_EVERYONE_SETS;
4580 base_type = ZFS_DELEG_EVERYONE;
4582 ld[0] = ZFS_DELEG_LOCAL;
4584 ld[1] = ZFS_DELEG_DESCENDENT;
4587 if (perms != NULL) {
4589 char *end = curr + strlen(perms);
4591 while (curr < end) {
4592 char *delim = strchr(curr, ',');
4603 (void) nvlist_add_boolean(nvl, curr);
4609 for (i = 0; i < 2; i++) {
4610 char locality = ld[i];
4614 if (!nvlist_empty(base_nvl)) {
4616 (void) snprintf(who_buf,
4617 sizeof (who_buf), "%c%c$%s",
4618 base_type, locality, who);
4620 (void) snprintf(who_buf,
4621 sizeof (who_buf), "%c%c$",
4622 base_type, locality);
4624 (void) nvlist_add_nvlist(top_nvl, who_buf,
4629 if (!nvlist_empty(set_nvl)) {
4631 (void) snprintf(who_buf,
4632 sizeof (who_buf), "%c%c$%s",
4633 set_type, locality, who);
4635 (void) snprintf(who_buf,
4636 sizeof (who_buf), "%c%c$",
4637 set_type, locality);
4639 (void) nvlist_add_nvlist(top_nvl, who_buf,
4644 for (i = 0; i < 2; i++) {
4645 char locality = ld[i];
4650 (void) snprintf(who_buf, sizeof (who_buf),
4651 "%c%c$%s", base_type, locality, who);
4653 (void) snprintf(who_buf, sizeof (who_buf),
4654 "%c%c$", base_type, locality);
4655 (void) nvlist_add_boolean(top_nvl, who_buf);
4658 (void) snprintf(who_buf, sizeof (who_buf),
4659 "%c%c$%s", set_type, locality, who);
4661 (void) snprintf(who_buf, sizeof (who_buf),
4662 "%c%c$", set_type, locality);
4663 (void) nvlist_add_boolean(top_nvl, who_buf);
4669 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4671 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4675 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4676 opts->descend, opts->who, opts->perms, *nvlp);
4677 } else if (opts->create) {
4678 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4679 opts->descend, NULL, opts->perms, *nvlp);
4680 } else if (opts->everyone) {
4681 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4682 opts->descend, NULL, opts->perms, *nvlp);
4684 char *curr = opts->who;
4685 char *end = curr + strlen(curr);
4687 while (curr < end) {
4689 zfs_deleg_who_type_t who_type;
4691 char *delim = strchr(curr, ',');
4694 struct passwd *p = NULL;
4695 struct group *g = NULL;
4703 rid = (uid_t)strtol(curr, &endch, 0);
4705 who_type = ZFS_DELEG_USER;
4714 (void) snprintf(errbuf, 256, gettext(
4715 "invalid user %s"), curr);
4716 allow_usage(un, B_TRUE, errbuf);
4718 } else if (opts->group) {
4719 who_type = ZFS_DELEG_GROUP;
4728 (void) snprintf(errbuf, 256, gettext(
4729 "invalid group %s"), curr);
4730 allow_usage(un, B_TRUE, errbuf);
4733 if (*endch != '\0') {
4740 if (*endch != '\0') {
4747 who_type = ZFS_DELEG_USER;
4749 } else if (g != NULL) {
4750 who_type = ZFS_DELEG_GROUP;
4753 (void) snprintf(errbuf, 256, gettext(
4754 "invalid user/group %s"), curr);
4755 allow_usage(un, B_TRUE, errbuf);
4759 (void) sprintf(id, "%u", rid);
4762 store_allow_perm(who_type, opts->local,
4763 opts->descend, who, opts->perms, *nvlp);
4772 print_set_creat_perms(uu_avl_t *who_avl)
4774 const char *sc_title[] = {
4775 gettext("Permission sets:\n"),
4776 gettext("Create time permissions:\n"),
4779 const char **title_ptr = sc_title;
4780 who_perm_node_t *who_node = NULL;
4781 int prev_weight = -1;
4783 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4784 who_node = uu_avl_next(who_avl, who_node)) {
4785 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4786 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4787 const char *who_name = who_node->who_perm.who_name;
4788 int weight = who_type2weight(who_type);
4789 boolean_t first = B_TRUE;
4790 deleg_perm_node_t *deleg_node;
4792 if (prev_weight != weight) {
4793 (void) printf(*title_ptr++);
4794 prev_weight = weight;
4797 if (who_name == NULL || strnlen(who_name, 1) == 0)
4798 (void) printf("\t");
4800 (void) printf("\t%s ", who_name);
4802 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4803 deleg_node = uu_avl_next(avl, deleg_node)) {
4806 deleg_node->dpn_perm.dp_name);
4809 (void) printf(",%s",
4810 deleg_node->dpn_perm.dp_name);
4813 (void) printf("\n");
4818 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4821 who_perm_node_t *who_node = NULL;
4822 boolean_t prt_title = B_TRUE;
4823 uu_avl_walk_t *walk;
4825 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4828 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4829 const char *who_name = who_node->who_perm.who_name;
4830 const char *nice_who_name = who_node->who_perm.who_ug_name;
4831 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4832 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4834 deleg_perm_node_t *deleg_node;
4835 boolean_t prt_who = B_TRUE;
4837 for (deleg_node = uu_avl_first(avl);
4839 deleg_node = uu_avl_next(avl, deleg_node)) {
4840 if (local != deleg_node->dpn_perm.dp_local ||
4841 descend != deleg_node->dpn_perm.dp_descend)
4845 const char *who = NULL;
4847 prt_title = B_FALSE;
4848 (void) printf(title);
4852 case ZFS_DELEG_USER_SETS:
4853 case ZFS_DELEG_USER:
4854 who = gettext("user");
4856 who_name = nice_who_name;
4858 case ZFS_DELEG_GROUP_SETS:
4859 case ZFS_DELEG_GROUP:
4860 who = gettext("group");
4862 who_name = nice_who_name;
4864 case ZFS_DELEG_EVERYONE_SETS:
4865 case ZFS_DELEG_EVERYONE:
4866 who = gettext("everyone");
4871 if (who_name == NULL)
4872 (void) printf("\t%s", who);
4874 (void) printf("\t%s %s", who, who_name);
4877 (void) printf("%c%s", delim,
4878 deleg_node->dpn_perm.dp_name);
4883 (void) printf("\n");
4886 uu_avl_walk_end(walk);
4890 print_fs_perms(fs_perm_set_t *fspset)
4892 fs_perm_node_t *node = NULL;
4893 char buf[ZFS_MAXNAMELEN+32];
4894 const char *dsname = buf;
4896 for (node = uu_list_first(fspset->fsps_list); node != NULL;
4897 node = uu_list_next(fspset->fsps_list, node)) {
4898 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
4899 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
4902 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
4903 gettext("---- Permissions on %s "),
4904 node->fspn_fsperm.fsp_name);
4905 (void) printf(dsname);
4906 left = 70 - strlen(buf);
4909 (void) printf("\n");
4911 print_set_creat_perms(sc_avl);
4912 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
4913 gettext("Local permissions:\n"));
4914 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
4915 gettext("Descendent permissions:\n"));
4916 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
4917 gettext("Local+Descendent permissions:\n"));
4921 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
4923 struct deleg_perms {
4929 set_deleg_perms(zfs_handle_t *zhp, void *data)
4931 struct deleg_perms *perms = (struct deleg_perms *)data;
4932 zfs_type_t zfs_type = zfs_get_type(zhp);
4934 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
4937 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
4941 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
4944 nvlist_t *perm_nvl = NULL;
4945 nvlist_t *update_perm_nvl = NULL;
4948 struct allow_opts opts = { 0 };
4950 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
4953 while ((c = getopt(argc, argv, optstr)) != -1) {
4956 opts.local = B_TRUE;
4959 opts.descend = B_TRUE;
4965 opts.group = B_TRUE;
4968 opts.everyone = B_TRUE;
4974 opts.create = B_TRUE;
4977 opts.recursive = B_TRUE;
4980 (void) fprintf(stderr, gettext("missing argument for "
4981 "'%c' option\n"), optopt);
4985 opts.prt_usage = B_TRUE;
4988 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4997 /* check arguments */
4998 parse_allow_args(argc, argv, un, &opts);
5000 /* try to open the dataset */
5001 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5002 ZFS_TYPE_VOLUME)) == NULL) {
5003 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5008 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5011 fs_perm_set_init(&fs_perm_set);
5012 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5013 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5018 print_fs_perms(&fs_perm_set);
5020 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5021 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5024 if (un && opts.recursive) {
5025 struct deleg_perms data = { un, update_perm_nvl };
5026 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5035 nvlist_free(perm_nvl);
5036 if (update_perm_nvl != NULL)
5037 nvlist_free(update_perm_nvl);
5039 fs_perm_set_fini(&fs_perm_set);
5047 * zfs allow [-r] [-t] <tag> <snap> ...
5049 * -r Recursively hold
5050 * -t Temporary hold (hidden option)
5052 * Apply a user-hold with the given tag to the list of snapshots.
5055 zfs_do_allow(int argc, char **argv)
5057 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5061 * zfs unallow [-r] [-t] <tag> <snap> ...
5063 * -r Recursively hold
5064 * -t Temporary hold (hidden option)
5066 * Apply a user-hold with the given tag to the list of snapshots.
5069 zfs_do_unallow(int argc, char **argv)
5071 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5075 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5080 boolean_t recursive = B_FALSE;
5081 boolean_t temphold = B_FALSE;
5082 const char *opts = holding ? "rt" : "r";
5086 while ((c = getopt(argc, argv, opts)) != -1) {
5095 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5104 /* check number of arguments */
5112 if (holding && tag[0] == '.') {
5113 /* tags starting with '.' are reserved for libzfs */
5114 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5118 for (i = 0; i < argc; ++i) {
5120 char parent[ZFS_MAXNAMELEN];
5122 char *path = argv[i];
5124 delim = strchr(path, '@');
5125 if (delim == NULL) {
5126 (void) fprintf(stderr,
5127 gettext("'%s' is not a snapshot\n"), path);
5131 (void) strncpy(parent, path, delim - path);
5132 parent[delim - path] = '\0';
5134 zhp = zfs_open(g_zfs, parent,
5135 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5141 if (zfs_hold(zhp, delim+1, tag, recursive,
5142 temphold, B_FALSE, -1, 0, 0) != 0)
5145 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5151 return (errors != 0);
5155 * zfs hold [-r] [-t] <tag> <snap> ...
5157 * -r Recursively hold
5158 * -t Temporary hold (hidden option)
5160 * Apply a user-hold with the given tag to the list of snapshots.
5163 zfs_do_hold(int argc, char **argv)
5165 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5169 * zfs release [-r] <tag> <snap> ...
5171 * -r Recursively release
5173 * Release a user-hold with the given tag from the list of snapshots.
5176 zfs_do_release(int argc, char **argv)
5178 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5181 typedef struct holds_cbdata {
5182 boolean_t cb_recursive;
5183 const char *cb_snapname;
5185 size_t cb_max_namelen;
5186 size_t cb_max_taglen;
5189 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5190 #define DATETIME_BUF_LEN (32)
5195 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5198 nvpair_t *nvp = NULL;
5199 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5203 for (i = 0; i < 3; i++) {
5204 col = gettext(hdr_cols[i]);
5206 (void) printf("%-*s ", i ? tagwidth : nwidth,
5209 (void) printf("%s\n", col);
5213 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5214 char *zname = nvpair_name(nvp);
5216 nvpair_t *nvp2 = NULL;
5217 (void) nvpair_value_nvlist(nvp, &nvl2);
5218 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5219 char tsbuf[DATETIME_BUF_LEN];
5220 char *tagname = nvpair_name(nvp2);
5224 char sep = scripted ? '\t' : ' ';
5225 size_t sepnum = scripted ? 1 : 2;
5227 (void) nvpair_value_uint64(nvp2, &val);
5229 (void) localtime_r(&time, &t);
5230 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5231 gettext(STRFTIME_FMT_STR), &t);
5233 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5234 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5240 * Generic callback function to list a dataset or snapshot.
5243 holds_callback(zfs_handle_t *zhp, void *data)
5245 holds_cbdata_t *cbp = data;
5246 nvlist_t *top_nvl = *cbp->cb_nvlp;
5247 nvlist_t *nvl = NULL;
5248 nvpair_t *nvp = NULL;
5249 const char *zname = zfs_get_name(zhp);
5250 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5252 if (cbp->cb_recursive) {
5253 const char *snapname;
5254 char *delim = strchr(zname, '@');
5258 snapname = delim + 1;
5259 if (strcmp(cbp->cb_snapname, snapname))
5263 if (zfs_get_holds(zhp, &nvl) != 0)
5266 if (znamelen > cbp->cb_max_namelen)
5267 cbp->cb_max_namelen = znamelen;
5269 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5270 const char *tag = nvpair_name(nvp);
5271 size_t taglen = strnlen(tag, MAXNAMELEN);
5272 if (taglen > cbp->cb_max_taglen)
5273 cbp->cb_max_taglen = taglen;
5276 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5280 * zfs holds [-r] <snap> ...
5282 * -r Recursively hold
5285 zfs_do_holds(int argc, char **argv)
5290 boolean_t scripted = B_FALSE;
5291 boolean_t recursive = B_FALSE;
5292 const char *opts = "rH";
5295 int types = ZFS_TYPE_SNAPSHOT;
5296 holds_cbdata_t cb = { 0 };
5303 while ((c = getopt(argc, argv, opts)) != -1) {
5312 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5319 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5320 flags |= ZFS_ITER_RECURSE;
5326 /* check number of arguments */
5330 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5333 for (i = 0; i < argc; ++i) {
5334 char *snapshot = argv[i];
5336 const char *snapname;
5338 delim = strchr(snapshot, '@');
5339 if (delim == NULL) {
5340 (void) fprintf(stderr,
5341 gettext("'%s' is not a snapshot\n"), snapshot);
5345 snapname = delim + 1;
5347 snapshot[delim - snapshot] = '\0';
5349 cb.cb_recursive = recursive;
5350 cb.cb_snapname = snapname;
5354 * 1. collect holds data, set format options
5356 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5357 holds_callback, &cb);
5363 * 2. print holds data
5365 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5367 if (nvlist_empty(nvl))
5368 (void) printf(gettext("no datasets available\n"));
5372 return (0 != errors);
5375 #define CHECK_SPINNER 30
5376 #define SPINNER_TIME 3 /* seconds */
5377 #define MOUNT_TIME 5 /* seconds */
5380 get_one_dataset(zfs_handle_t *zhp, void *data)
5382 static char *spin[] = { "-", "\\", "|", "/" };
5383 static int spinval = 0;
5384 static int spincheck = 0;
5385 static time_t last_spin_time = (time_t)0;
5386 get_all_cb_t *cbp = data;
5387 zfs_type_t type = zfs_get_type(zhp);
5389 if (cbp->cb_verbose) {
5390 if (--spincheck < 0) {
5391 time_t now = time(NULL);
5392 if (last_spin_time + SPINNER_TIME < now) {
5393 update_progress(spin[spinval++ % 4]);
5394 last_spin_time = now;
5396 spincheck = CHECK_SPINNER;
5401 * Interate over any nested datasets.
5403 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5409 * Skip any datasets whose type does not match.
5411 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5415 libzfs_add_handle(cbp, zhp);
5416 assert(cbp->cb_used <= cbp->cb_alloc);
5422 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5424 get_all_cb_t cb = { 0 };
5425 cb.cb_verbose = verbose;
5426 cb.cb_getone = get_one_dataset;
5429 set_progress_header(gettext("Reading ZFS config"));
5430 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5432 *dslist = cb.cb_handles;
5433 *count = cb.cb_used;
5436 finish_progress(gettext("done."));
5440 * Generic callback for sharing or mounting filesystems. Because the code is so
5441 * similar, we have a common function with an extra parameter to determine which
5442 * mode we are using.
5444 #define OP_SHARE 0x1
5445 #define OP_MOUNT 0x2
5448 * Share or mount a dataset.
5451 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5452 boolean_t explicit, const char *options)
5454 char mountpoint[ZFS_MAXPROPLEN];
5455 char shareopts[ZFS_MAXPROPLEN];
5456 char smbshareopts[ZFS_MAXPROPLEN];
5457 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5459 uint64_t zoned, canmount;
5460 boolean_t shared_nfs, shared_smb;
5462 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5465 * Check to make sure we can mount/share this dataset. If we
5466 * are in the global zone and the filesystem is exported to a
5467 * local zone, or if we are in a local zone and the
5468 * filesystem is not exported, then it is an error.
5470 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5472 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5476 (void) fprintf(stderr, gettext("cannot %s '%s': "
5477 "dataset is exported to a local zone\n"), cmdname,
5481 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5485 (void) fprintf(stderr, gettext("cannot %s '%s': "
5486 "permission denied\n"), cmdname,
5492 * Ignore any filesystems which don't apply to us. This
5493 * includes those with a legacy mountpoint, or those with
5494 * legacy share options.
5496 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5497 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5498 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5499 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5500 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5501 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5503 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5504 strcmp(smbshareopts, "off") == 0) {
5508 (void) fprintf(stderr, gettext("cannot share '%s': "
5509 "legacy share\n"), zfs_get_name(zhp));
5510 (void) fprintf(stderr, gettext("use share(1M) to "
5511 "share this filesystem, or set "
5512 "sharenfs property on\n"));
5517 * We cannot share or mount legacy filesystems. If the
5518 * shareopts is non-legacy but the mountpoint is legacy, we
5519 * treat it as a legacy share.
5521 if (strcmp(mountpoint, "legacy") == 0) {
5525 (void) fprintf(stderr, gettext("cannot %s '%s': "
5526 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5527 (void) fprintf(stderr, gettext("use %s(1M) to "
5528 "%s this filesystem\n"), cmdname, cmdname);
5532 if (strcmp(mountpoint, "none") == 0) {
5536 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5537 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5542 * canmount explicit outcome
5543 * on no pass through
5544 * on yes pass through
5546 * off yes display error, return 1
5547 * noauto no return 0
5548 * noauto yes pass through
5550 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5551 if (canmount == ZFS_CANMOUNT_OFF) {
5555 (void) fprintf(stderr, gettext("cannot %s '%s': "
5556 "'canmount' property is set to 'off'\n"), cmdname,
5559 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5564 * At this point, we have verified that the mountpoint and/or
5565 * shareopts are appropriate for auto management. If the
5566 * filesystem is already mounted or shared, return (failing
5567 * for explicit requests); otherwise mount or share the
5573 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5574 shared_smb = zfs_is_shared_smb(zhp, NULL);
5576 if (shared_nfs && shared_smb ||
5577 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5578 strcmp(smbshareopts, "off") == 0) ||
5579 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5580 strcmp(shareopts, "off") == 0)) {
5584 (void) fprintf(stderr, gettext("cannot share "
5585 "'%s': filesystem already shared\n"),
5590 if (!zfs_is_mounted(zhp, NULL) &&
5591 zfs_mount(zhp, NULL, 0) != 0)
5594 if (protocol == NULL) {
5595 if (zfs_shareall(zhp) != 0)
5597 } else if (strcmp(protocol, "nfs") == 0) {
5598 if (zfs_share_nfs(zhp))
5600 } else if (strcmp(protocol, "smb") == 0) {
5601 if (zfs_share_smb(zhp))
5604 (void) fprintf(stderr, gettext("cannot share "
5605 "'%s': invalid share type '%s' "
5607 zfs_get_name(zhp), protocol);
5614 if (options == NULL)
5615 mnt.mnt_mntopts = "";
5617 mnt.mnt_mntopts = (char *)options;
5619 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5620 zfs_is_mounted(zhp, NULL)) {
5624 (void) fprintf(stderr, gettext("cannot mount "
5625 "'%s': filesystem already mounted\n"),
5630 if (zfs_mount(zhp, options, flags) != 0)
5639 * Reports progress in the form "(current/total)". Not thread-safe.
5642 report_mount_progress(int current, int total)
5644 static time_t last_progress_time = 0;
5645 time_t now = time(NULL);
5648 /* report 1..n instead of 0..n-1 */
5651 /* display header if we're here for the first time */
5653 set_progress_header(gettext("Mounting ZFS filesystems"));
5654 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5655 /* too soon to report again */
5659 last_progress_time = now;
5661 (void) sprintf(info, "(%d/%d)", current, total);
5663 if (current == total)
5664 finish_progress(info);
5666 update_progress(info);
5670 append_options(char *mntopts, char *newopts)
5672 int len = strlen(mntopts);
5674 /* original length plus new string to append plus 1 for the comma */
5675 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5676 (void) fprintf(stderr, gettext("the opts argument for "
5677 "'%c' option is too long (more than %d chars)\n"),
5678 "-o", MNT_LINE_MAX);
5683 mntopts[len++] = ',';
5685 (void) strcpy(&mntopts[len], newopts);
5689 share_mount(int op, int argc, char **argv)
5692 boolean_t verbose = B_FALSE;
5694 char *options = NULL;
5698 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5708 if (*optarg == '\0') {
5709 (void) fprintf(stderr, gettext("empty mount "
5710 "options (-o) specified\n"));
5714 if (options == NULL)
5715 options = safe_malloc(MNT_LINE_MAX + 1);
5717 /* option validation is done later */
5718 append_options(options, optarg);
5722 warnx("no overlay mounts support on FreeBSD, ignoring");
5725 (void) fprintf(stderr, gettext("missing argument for "
5726 "'%c' option\n"), optopt);
5730 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5739 /* check number of arguments */
5741 zfs_handle_t **dslist = NULL;
5742 size_t i, count = 0;
5743 char *protocol = NULL;
5745 if (op == OP_SHARE && argc > 0) {
5746 if (strcmp(argv[0], "nfs") != 0 &&
5747 strcmp(argv[0], "smb") != 0) {
5748 (void) fprintf(stderr, gettext("share type "
5749 "must be 'nfs' or 'smb'\n"));
5758 (void) fprintf(stderr, gettext("too many arguments\n"));
5762 start_progress_timer();
5763 get_all_datasets(&dslist, &count, verbose);
5768 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5770 for (i = 0; i < count; i++) {
5772 report_mount_progress(i, count);
5774 if (share_mount_one(dslist[i], op, flags, protocol,
5775 B_FALSE, options) != 0)
5777 zfs_close(dslist[i]);
5781 } else if (argc == 0) {
5782 struct mnttab entry;
5784 if ((op == OP_SHARE) || (options != NULL)) {
5785 (void) fprintf(stderr, gettext("missing filesystem "
5786 "argument (specify -a for all)\n"));
5791 * When mount is given no arguments, go through /etc/mnttab and
5792 * display any active ZFS mounts. We hide any snapshots, since
5793 * they are controlled automatically.
5795 rewind(mnttab_file);
5796 while (getmntent(mnttab_file, &entry) == 0) {
5797 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5798 strchr(entry.mnt_special, '@') != NULL)
5801 (void) printf("%-30s %s\n", entry.mnt_special,
5809 (void) fprintf(stderr,
5810 gettext("too many arguments\n"));
5814 if ((zhp = zfs_open(g_zfs, argv[0],
5815 ZFS_TYPE_FILESYSTEM)) == NULL) {
5818 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5828 * zfs mount -a [nfs]
5829 * zfs mount filesystem
5831 * Mount all filesystems, or mount the given filesystem.
5834 zfs_do_mount(int argc, char **argv)
5836 return (share_mount(OP_MOUNT, argc, argv));
5840 * zfs share -a [nfs | smb]
5841 * zfs share filesystem
5843 * Share all filesystems, or share the given filesystem.
5846 zfs_do_share(int argc, char **argv)
5848 return (share_mount(OP_SHARE, argc, argv));
5851 typedef struct unshare_unmount_node {
5852 zfs_handle_t *un_zhp;
5854 uu_avl_node_t un_avlnode;
5855 } unshare_unmount_node_t;
5859 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
5861 const unshare_unmount_node_t *l = larg;
5862 const unshare_unmount_node_t *r = rarg;
5864 return (strcmp(l->un_mountp, r->un_mountp));
5868 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5869 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
5870 * and unmount it appropriately.
5873 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
5877 struct stat64 statbuf;
5878 struct extmnttab entry;
5879 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5883 * Search for the path in /etc/mnttab. Rather than looking for the
5884 * specific path, which can be fooled by non-standard paths (i.e. ".."
5885 * or "//"), we stat() the path and search for the corresponding
5886 * (major,minor) device pair.
5888 if (stat64(path, &statbuf) != 0) {
5889 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5890 cmdname, path, strerror(errno));
5893 path_inode = statbuf.st_ino;
5896 * Search for the given (major,minor) pair in the mount table.
5899 rewind(mnttab_file);
5900 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
5901 if (entry.mnt_major == major(statbuf.st_dev) &&
5902 entry.mnt_minor == minor(statbuf.st_dev))
5909 if (statfs(path, &sfs) != 0) {
5910 (void) fprintf(stderr, "%s: %s\n", path,
5914 statfs2mnttab(&sfs, &entry);
5918 if (op == OP_SHARE) {
5919 (void) fprintf(stderr, gettext("cannot %s '%s': not "
5920 "currently mounted\n"), cmdname, path);
5923 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
5925 if ((ret = umount2(path, flags)) != 0)
5926 (void) fprintf(stderr, gettext("%s: %s\n"), path,
5931 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
5932 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
5933 "filesystem\n"), cmdname, path);
5937 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
5938 ZFS_TYPE_FILESYSTEM)) == NULL)
5942 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
5943 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5944 cmdname, path, strerror(errno));
5946 } else if (statbuf.st_ino != path_inode) {
5947 (void) fprintf(stderr, gettext("cannot "
5948 "%s '%s': not a mountpoint\n"), cmdname, path);
5952 if (op == OP_SHARE) {
5953 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5954 char smbshare_prop[ZFS_MAXPROPLEN];
5956 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
5957 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
5958 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
5959 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
5961 if (strcmp(nfs_mnt_prop, "off") == 0 &&
5962 strcmp(smbshare_prop, "off") == 0) {
5963 (void) fprintf(stderr, gettext("cannot unshare "
5964 "'%s': legacy share\n"), path);
5965 (void) fprintf(stderr, gettext("use "
5966 "unshare(1M) to unshare this filesystem\n"));
5967 } else if (!zfs_is_shared(zhp)) {
5968 (void) fprintf(stderr, gettext("cannot unshare '%s': "
5969 "not currently shared\n"), path);
5971 ret = zfs_unshareall_bypath(zhp, path);
5974 char mtpt_prop[ZFS_MAXPROPLEN];
5976 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
5977 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
5980 ret = zfs_unmount(zhp, NULL, flags);
5981 } else if (strcmp(mtpt_prop, "legacy") == 0) {
5982 (void) fprintf(stderr, gettext("cannot unmount "
5983 "'%s': legacy mountpoint\n"),
5985 (void) fprintf(stderr, gettext("use umount(1M) "
5986 "to unmount this filesystem\n"));
5988 ret = zfs_unmountall(zhp, flags);
5999 * Generic callback for unsharing or unmounting a filesystem.
6002 unshare_unmount(int op, int argc, char **argv)
6009 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6010 char sharesmb[ZFS_MAXPROPLEN];
6013 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6022 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6033 * We could make use of zfs_for_each() to walk all datasets in
6034 * the system, but this would be very inefficient, especially
6035 * since we would have to linearly search /etc/mnttab for each
6036 * one. Instead, do one pass through /etc/mnttab looking for
6037 * zfs entries and call zfs_unmount() for each one.
6039 * Things get a little tricky if the administrator has created
6040 * mountpoints beneath other ZFS filesystems. In this case, we
6041 * have to unmount the deepest filesystems first. To accomplish
6042 * this, we place all the mountpoints in an AVL tree sorted by
6043 * the special type (dataset name), and walk the result in
6044 * reverse to make sure to get any snapshots first.
6046 struct mnttab entry;
6047 uu_avl_pool_t *pool;
6049 unshare_unmount_node_t *node;
6051 uu_avl_walk_t *walk;
6054 (void) fprintf(stderr, gettext("too many arguments\n"));
6058 if (((pool = uu_avl_pool_create("unmount_pool",
6059 sizeof (unshare_unmount_node_t),
6060 offsetof(unshare_unmount_node_t, un_avlnode),
6061 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6062 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6065 rewind(mnttab_file);
6066 while (getmntent(mnttab_file, &entry) == 0) {
6068 /* ignore non-ZFS entries */
6069 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6072 /* ignore snapshots */
6073 if (strchr(entry.mnt_special, '@') != NULL)
6076 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6077 ZFS_TYPE_FILESYSTEM)) == NULL) {
6084 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6086 sizeof (nfs_mnt_prop),
6087 NULL, NULL, 0, B_FALSE) == 0);
6088 if (strcmp(nfs_mnt_prop, "off") != 0)
6090 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6092 sizeof (nfs_mnt_prop),
6093 NULL, NULL, 0, B_FALSE) == 0);
6094 if (strcmp(nfs_mnt_prop, "off") == 0)
6098 /* Ignore legacy mounts */
6099 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6101 sizeof (nfs_mnt_prop),
6102 NULL, NULL, 0, B_FALSE) == 0);
6103 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6105 /* Ignore canmount=noauto mounts */
6106 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6107 ZFS_CANMOUNT_NOAUTO)
6113 node = safe_malloc(sizeof (unshare_unmount_node_t));
6115 node->un_mountp = safe_strdup(entry.mnt_mountp);
6117 uu_avl_node_init(node, &node->un_avlnode, pool);
6119 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6120 uu_avl_insert(tree, node, idx);
6122 zfs_close(node->un_zhp);
6123 free(node->un_mountp);
6129 * Walk the AVL tree in reverse, unmounting each filesystem and
6130 * removing it from the AVL tree in the process.
6132 if ((walk = uu_avl_walk_start(tree,
6133 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6136 while ((node = uu_avl_walk_next(walk)) != NULL) {
6137 uu_avl_remove(tree, node);
6141 if (zfs_unshareall_bypath(node->un_zhp,
6142 node->un_mountp) != 0)
6147 if (zfs_unmount(node->un_zhp,
6148 node->un_mountp, flags) != 0)
6153 zfs_close(node->un_zhp);
6154 free(node->un_mountp);
6158 uu_avl_walk_end(walk);
6159 uu_avl_destroy(tree);
6160 uu_avl_pool_destroy(pool);
6165 (void) fprintf(stderr,
6166 gettext("missing filesystem argument\n"));
6168 (void) fprintf(stderr,
6169 gettext("too many arguments\n"));
6174 * We have an argument, but it may be a full path or a ZFS
6175 * filesystem. Pass full paths off to unmount_path() (shared by
6176 * manual_unmount), otherwise open the filesystem and pass to
6179 if (argv[0][0] == '/')
6180 return (unshare_unmount_path(op, argv[0],
6183 if ((zhp = zfs_open(g_zfs, argv[0],
6184 ZFS_TYPE_FILESYSTEM)) == NULL)
6187 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6188 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6189 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6190 NULL, 0, B_FALSE) == 0);
6194 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6196 sizeof (nfs_mnt_prop),
6197 NULL, NULL, 0, B_FALSE) == 0);
6198 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6199 sharesmb, sizeof (sharesmb), NULL, NULL,
6202 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6203 strcmp(sharesmb, "off") == 0) {
6204 (void) fprintf(stderr, gettext("cannot "
6205 "unshare '%s': legacy share\n"),
6207 (void) fprintf(stderr, gettext("use "
6208 "unshare(1M) to unshare this "
6211 } else if (!zfs_is_shared(zhp)) {
6212 (void) fprintf(stderr, gettext("cannot "
6213 "unshare '%s': not currently "
6214 "shared\n"), zfs_get_name(zhp));
6216 } else if (zfs_unshareall(zhp) != 0) {
6222 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6223 (void) fprintf(stderr, gettext("cannot "
6224 "unmount '%s': legacy "
6225 "mountpoint\n"), zfs_get_name(zhp));
6226 (void) fprintf(stderr, gettext("use "
6227 "umount(1M) to unmount this "
6230 } else if (!zfs_is_mounted(zhp, NULL)) {
6231 (void) fprintf(stderr, gettext("cannot "
6232 "unmount '%s': not currently "
6236 } else if (zfs_unmountall(zhp, flags) != 0) {
6250 * zfs unmount filesystem
6252 * Unmount all filesystems, or a specific ZFS filesystem.
6255 zfs_do_unmount(int argc, char **argv)
6257 return (unshare_unmount(OP_MOUNT, argc, argv));
6262 * zfs unshare filesystem
6264 * Unshare all filesystems, or a specific ZFS filesystem.
6267 zfs_do_unshare(int argc, char **argv)
6269 return (unshare_unmount(OP_SHARE, argc, argv));
6273 * Attach/detach the given dataset to/from the given jail
6277 do_jail(int argc, char **argv, int attach)
6282 /* check number of arguments */
6284 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6288 (void) fprintf(stderr, gettext("too many arguments\n"));
6292 jailid = jail_getid(argv[1]);
6294 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6298 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6302 ret = (zfs_jail(zhp, jailid, attach) != 0);
6309 * zfs jail jailid filesystem
6311 * Attach the given dataset to the given jail
6315 zfs_do_jail(int argc, char **argv)
6318 return (do_jail(argc, argv, 1));
6322 * zfs unjail jailid filesystem
6324 * Detach the given dataset from the given jail
6328 zfs_do_unjail(int argc, char **argv)
6331 return (do_jail(argc, argv, 0));
6335 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6336 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6339 manual_mount(int argc, char **argv)
6342 char mountpoint[ZFS_MAXPROPLEN];
6343 char mntopts[MNT_LINE_MAX] = { '\0' };
6347 char *dataset, *path;
6350 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6353 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6356 flags |= MS_OVERLAY;
6359 flags |= MS_NOMNTTAB;
6362 (void) fprintf(stderr, gettext("missing argument for "
6363 "'%c' option\n"), optopt);
6367 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6369 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6378 /* check that we only have two arguments */
6381 (void) fprintf(stderr, gettext("missing dataset "
6384 (void) fprintf(stderr,
6385 gettext("missing mountpoint argument\n"));
6387 (void) fprintf(stderr, gettext("too many arguments\n"));
6388 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6395 /* try to open the dataset */
6396 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6399 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6400 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6402 /* check for legacy mountpoint and complain appropriately */
6404 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6405 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6406 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6407 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6412 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6413 "mounted using 'mount -F zfs'\n"), dataset);
6414 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6415 "instead.\n"), path);
6416 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
6417 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6418 (void) fprintf(stderr, gettext("See zfs(1M) for more "
6427 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6428 * unmounts of non-legacy filesystems, as this is the dominant administrative
6432 manual_unmount(int argc, char **argv)
6438 while ((c = getopt(argc, argv, "f")) != -1) {
6444 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6446 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6455 /* check arguments */
6458 (void) fprintf(stderr, gettext("missing path "
6461 (void) fprintf(stderr, gettext("too many arguments\n"));
6462 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6466 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6470 find_command_idx(char *command, int *idx)
6474 for (i = 0; i < NCOMMAND; i++) {
6475 if (command_table[i].name == NULL)
6478 if (strcmp(command, command_table[i].name) == 0) {
6487 zfs_do_diff(int argc, char **argv)
6491 char *tosnap = NULL;
6492 char *fromsnap = NULL;
6497 while ((c = getopt(argc, argv, "FHt")) != -1) {
6500 flags |= ZFS_DIFF_CLASSIFY;
6503 flags |= ZFS_DIFF_PARSEABLE;
6506 flags |= ZFS_DIFF_TIMESTAMP;
6509 (void) fprintf(stderr,
6510 gettext("invalid option '%c'\n"), optopt);
6519 (void) fprintf(stderr,
6520 gettext("must provide at least one snapshot name\n"));
6525 (void) fprintf(stderr, gettext("too many arguments\n"));
6530 tosnap = (argc == 2) ? argv[1] : NULL;
6533 if (*fromsnap != '@')
6534 copy = strdup(fromsnap);
6536 copy = strdup(tosnap);
6540 if (atp = strchr(copy, '@'))
6543 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6549 * Ignore SIGPIPE so that the library can give us
6550 * information on any failure
6552 (void) sigignore(SIGPIPE);
6554 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6562 main(int argc, char **argv)
6569 (void) setlocale(LC_ALL, "");
6570 (void) textdomain(TEXT_DOMAIN);
6574 if ((g_zfs = libzfs_init()) == NULL) {
6575 (void) fprintf(stderr, gettext("internal error: failed to "
6576 "initialize ZFS library\n"));
6580 zpool_set_history_str("zfs", argc, argv, history_str);
6581 verify(zpool_stage_history(g_zfs, history_str) == 0);
6583 libzfs_print_on_error(g_zfs, B_TRUE);
6585 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6586 (void) fprintf(stderr, gettext("internal error: unable to "
6587 "open %s\n"), MNTTAB);
6592 * This command also doubles as the /etc/fs mount and unmount program.
6593 * Determine if we should take this behavior based on argv[0].
6595 progname = basename(argv[0]);
6596 if (strcmp(progname, "mount") == 0) {
6597 ret = manual_mount(argc, argv);
6598 } else if (strcmp(progname, "umount") == 0) {
6599 ret = manual_unmount(argc, argv);
6602 * Make sure the user has specified some command.
6605 (void) fprintf(stderr, gettext("missing command\n"));
6612 * The 'umount' command is an alias for 'unmount'
6614 if (strcmp(cmdname, "umount") == 0)
6615 cmdname = "unmount";
6618 * The 'recv' command is an alias for 'receive'
6620 if (strcmp(cmdname, "recv") == 0)
6621 cmdname = "receive";
6626 if (strcmp(cmdname, "-?") == 0)
6630 * Run the appropriate command.
6632 libzfs_mnttab_cache(g_zfs, B_TRUE);
6633 if (find_command_idx(cmdname, &i) == 0) {
6634 current_command = &command_table[i];
6635 ret = command_table[i].func(argc - 1, argv + 1);
6636 } else if (strchr(cmdname, '=') != NULL) {
6637 verify(find_command_idx("set", &i) == 0);
6638 current_command = &command_table[i];
6639 ret = command_table[i].func(argc, argv);
6641 (void) fprintf(stderr, gettext("unrecognized "
6642 "command '%s'\n"), cmdname);
6645 libzfs_mnttab_cache(g_zfs, B_FALSE);
6648 (void) fclose(mnttab_file);
6653 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6654 * for the purposes of running ::findleaks.
6656 if (getenv("ZFS_ABORT") != NULL) {
6657 (void) printf("dumping core by request\n");