4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011 by Delphix. All rights reserved.
26 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
27 * All rights reserved.
28 * Copyright (c) 2011 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
37 #include <libnvpair.h>
50 #include <sys/mntent.h>
51 #include <sys/mnttab.h>
52 #include <sys/mount.h>
54 #include <sys/fs/zfs.h>
55 #include <sys/types.h>
60 #include <zfs_deleg.h>
64 #include <directory.h>
69 #include "zfs_comutil.h"
71 libzfs_handle_t *g_zfs;
73 static FILE *mnttab_file;
74 static char history_str[HIS_MAX_RECORD_LEN];
76 static int zfs_do_clone(int argc, char **argv);
77 static int zfs_do_create(int argc, char **argv);
78 static int zfs_do_destroy(int argc, char **argv);
79 static int zfs_do_get(int argc, char **argv);
80 static int zfs_do_inherit(int argc, char **argv);
81 static int zfs_do_list(int argc, char **argv);
82 static int zfs_do_mount(int argc, char **argv);
83 static int zfs_do_rename(int argc, char **argv);
84 static int zfs_do_rollback(int argc, char **argv);
85 static int zfs_do_set(int argc, char **argv);
86 static int zfs_do_upgrade(int argc, char **argv);
87 static int zfs_do_snapshot(int argc, char **argv);
88 static int zfs_do_unmount(int argc, char **argv);
89 static int zfs_do_share(int argc, char **argv);
90 static int zfs_do_unshare(int argc, char **argv);
91 static int zfs_do_send(int argc, char **argv);
92 static int zfs_do_receive(int argc, char **argv);
93 static int zfs_do_promote(int argc, char **argv);
94 static int zfs_do_userspace(int argc, char **argv);
95 static int zfs_do_allow(int argc, char **argv);
96 static int zfs_do_unallow(int argc, char **argv);
97 static int zfs_do_hold(int argc, char **argv);
98 static int zfs_do_holds(int argc, char **argv);
99 static int zfs_do_release(int argc, char **argv);
100 static int zfs_do_diff(int argc, char **argv);
101 static int zfs_do_jail(int argc, char **argv);
102 static int zfs_do_unjail(int argc, char **argv);
105 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
110 _umem_debug_init(void)
112 return ("default,verbose"); /* $UMEM_DEBUG setting */
116 _umem_logging_init(void)
118 return ("fail,contents"); /* $UMEM_LOGGING setting */
153 typedef struct zfs_command {
155 int (*func)(int argc, char **argv);
160 * Master command table. Each ZFS command has a name, associated function, and
161 * usage message. The usage messages need to be internationalized, so we have
162 * to have a function to return the usage message based on a command index.
164 * These commands are organized according to how they are displayed in the usage
165 * message. An empty command (one with a NULL name) indicates an empty line in
166 * the generic usage message.
168 static zfs_command_t command_table[] = {
169 { "create", zfs_do_create, HELP_CREATE },
170 { "destroy", zfs_do_destroy, HELP_DESTROY },
172 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
173 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
174 { "clone", zfs_do_clone, HELP_CLONE },
175 { "promote", zfs_do_promote, HELP_PROMOTE },
176 { "rename", zfs_do_rename, HELP_RENAME },
178 { "list", zfs_do_list, HELP_LIST },
180 { "set", zfs_do_set, HELP_SET },
181 { "get", zfs_do_get, HELP_GET },
182 { "inherit", zfs_do_inherit, HELP_INHERIT },
183 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
184 { "userspace", zfs_do_userspace, HELP_USERSPACE },
185 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
187 { "mount", zfs_do_mount, HELP_MOUNT },
188 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
189 { "share", zfs_do_share, HELP_SHARE },
190 { "unshare", zfs_do_unshare, HELP_UNSHARE },
192 { "send", zfs_do_send, HELP_SEND },
193 { "receive", zfs_do_receive, HELP_RECEIVE },
195 { "allow", zfs_do_allow, HELP_ALLOW },
197 { "unallow", zfs_do_unallow, HELP_UNALLOW },
199 { "hold", zfs_do_hold, HELP_HOLD },
200 { "holds", zfs_do_holds, HELP_HOLDS },
201 { "release", zfs_do_release, HELP_RELEASE },
202 { "diff", zfs_do_diff, HELP_DIFF },
204 { "jail", zfs_do_jail, HELP_JAIL },
205 { "unjail", zfs_do_unjail, HELP_UNJAIL },
208 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
210 zfs_command_t *current_command;
213 get_usage(zfs_help_t idx)
217 return (gettext("\tclone [-p] [-o property=value] ... "
218 "<snapshot> <filesystem|volume>\n"));
220 return (gettext("\tcreate [-p] [-o property=value] ... "
222 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
223 "-V <size> <volume>\n"));
225 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
226 "\tdestroy [-dnpRrv] "
227 "<snapshot>[%<snapname>][,...]\n"));
229 return (gettext("\tget [-rHp] [-d max] "
230 "[-o \"all\" | field[,...]] [-t type[,...]] "
231 "[-s source[,...]]\n"
232 "\t <\"all\" | property[,...]> "
233 "[filesystem|volume|snapshot] ...\n"));
235 return (gettext("\tinherit [-rS] <property> "
236 "<filesystem|volume|snapshot> ...\n"));
238 return (gettext("\tupgrade [-v]\n"
239 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
241 return (gettext("\tjail <jailid> <filesystem>\n"));
243 return (gettext("\tunjail <jailid> <filesystem>\n"));
245 return (gettext("\tlist [-rH][-d max] "
246 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
247 "\t [-S property] ... "
248 "[filesystem|volume|snapshot] ...\n"));
250 return (gettext("\tmount\n"
251 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
253 return (gettext("\tpromote <clone-filesystem>\n"));
255 return (gettext("\treceive [-vnFu] <filesystem|volume|"
257 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
259 return (gettext("\trename <filesystem|volume|snapshot> "
260 "<filesystem|volume|snapshot>\n"
261 "\trename -p <filesystem|volume> <filesystem|volume>\n"
262 "\trename -r <snapshot> <snapshot>\n"
263 "\trename -u [-p] <filesystem> <filesystem>"));
265 return (gettext("\trollback [-rRf] <snapshot>\n"));
267 return (gettext("\tsend [-DnPpRrv] "
268 "[-i snapshot | -I snapshot] <snapshot>\n"));
270 return (gettext("\tset <property=value> "
271 "<filesystem|volume|snapshot> ...\n"));
273 return (gettext("\tshare <-a | filesystem>\n"));
275 return (gettext("\tsnapshot [-r] [-o property=value] ... "
276 "<filesystem@snapname|volume@snapname>\n"));
278 return (gettext("\tunmount [-f] "
279 "<-a | filesystem|mountpoint>\n"));
281 return (gettext("\tunshare "
282 "<-a | filesystem|mountpoint>\n"));
284 return (gettext("\tallow <filesystem|volume>\n"
286 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
287 "\t <filesystem|volume>\n"
288 "\tallow [-ld] -e <perm|@setname>[,...] "
289 "<filesystem|volume>\n"
290 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
291 "\tallow -s @setname <perm|@setname>[,...] "
292 "<filesystem|volume>\n"));
294 return (gettext("\tunallow [-rldug] "
295 "<\"everyone\"|user|group>[,...]\n"
296 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
297 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
298 "<filesystem|volume>\n"
299 "\tunallow [-r] -c [<perm|@setname>[,...]] "
300 "<filesystem|volume>\n"
301 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
302 "<filesystem|volume>\n"));
304 return (gettext("\tuserspace [-niHp] [-o field[,...]] "
305 "[-sS field] ... [-t type[,...]]\n"
306 "\t <filesystem|snapshot>\n"));
307 case HELP_GROUPSPACE:
308 return (gettext("\tgroupspace [-niHp] [-o field[,...]] "
309 "[-sS field] ... [-t type[,...]]\n"
310 "\t <filesystem|snapshot>\n"));
312 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
314 return (gettext("\tholds [-r] <snapshot> ...\n"));
316 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
318 return (gettext("\tdiff [-FHt] <snapshot> "
319 "[snapshot|filesystem]\n"));
329 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
334 * Utility function to guarantee malloc() success.
338 safe_malloc(size_t size)
342 if ((data = calloc(1, size)) == NULL)
349 safe_strdup(char *str)
351 char *dupstr = strdup(str);
360 * Callback routine that will print out information for each of
364 usage_prop_cb(int prop, void *cb)
368 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
370 if (zfs_prop_readonly(prop))
371 (void) fprintf(fp, " NO ");
373 (void) fprintf(fp, "YES ");
375 if (zfs_prop_inheritable(prop))
376 (void) fprintf(fp, " YES ");
378 (void) fprintf(fp, " NO ");
380 if (zfs_prop_values(prop) == NULL)
381 (void) fprintf(fp, "-\n");
383 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
389 * Display usage message. If we're inside a command, display only the usage for
390 * that command. Otherwise, iterate over the entire command table and display
391 * a complete usage message.
394 usage(boolean_t requested)
397 boolean_t show_properties = B_FALSE;
398 FILE *fp = requested ? stdout : stderr;
400 if (current_command == NULL) {
402 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
404 gettext("where 'command' is one of the following:\n\n"));
406 for (i = 0; i < NCOMMAND; i++) {
407 if (command_table[i].name == NULL)
408 (void) fprintf(fp, "\n");
410 (void) fprintf(fp, "%s",
411 get_usage(command_table[i].usage));
414 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
415 "pool/[dataset/]*dataset[@name]\n"));
417 (void) fprintf(fp, gettext("usage:\n"));
418 (void) fprintf(fp, "%s", get_usage(current_command->usage));
421 if (current_command != NULL &&
422 (strcmp(current_command->name, "set") == 0 ||
423 strcmp(current_command->name, "get") == 0 ||
424 strcmp(current_command->name, "inherit") == 0 ||
425 strcmp(current_command->name, "list") == 0))
426 show_properties = B_TRUE;
428 if (show_properties) {
430 gettext("\nThe following properties are supported:\n"));
432 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
433 "PROPERTY", "EDIT", "INHERIT", "VALUES");
435 /* Iterate over all properties */
436 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
439 (void) fprintf(fp, "\t%-15s ", "userused@...");
440 (void) fprintf(fp, " NO NO <size>\n");
441 (void) fprintf(fp, "\t%-15s ", "groupused@...");
442 (void) fprintf(fp, " NO NO <size>\n");
443 (void) fprintf(fp, "\t%-15s ", "userquota@...");
444 (void) fprintf(fp, "YES NO <size> | none\n");
445 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
446 (void) fprintf(fp, "YES NO <size> | none\n");
447 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
448 (void) fprintf(fp, " NO NO <size>\n");
450 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
451 "with standard units such as K, M, G, etc.\n"));
452 (void) fprintf(fp, gettext("\nUser-defined properties can "
453 "be specified by using a name containing a colon (:).\n"));
454 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
455 "properties must be appended with\n"
456 "a user or group specifier of one of these forms:\n"
457 " POSIX name (eg: \"matt\")\n"
458 " POSIX id (eg: \"126829\")\n"
459 " SMB name@domain (eg: \"matt@sun\")\n"
460 " SMB SID (eg: \"S-1-234-567-89\")\n"));
463 gettext("\nFor the property list, run: %s\n"),
466 gettext("\nFor the delegated permission list, run: %s\n"),
467 "zfs allow|unallow");
471 * See comments at end of main().
473 if (getenv("ZFS_ABORT") != NULL) {
474 (void) printf("dumping core by request\n");
478 exit(requested ? 0 : 2);
482 parseprop(nvlist_t *props)
484 char *propname = optarg;
485 char *propval, *strval;
487 if ((propval = strchr(propname, '=')) == NULL) {
488 (void) fprintf(stderr, gettext("missing "
489 "'=' for -o option\n"));
494 if (nvlist_lookup_string(props, propname, &strval) == 0) {
495 (void) fprintf(stderr, gettext("property '%s' "
496 "specified multiple times\n"), propname);
499 if (nvlist_add_string(props, propname, propval) != 0)
505 parse_depth(char *opt, int *flags)
510 depth = (int)strtol(opt, &tmp, 0);
512 (void) fprintf(stderr,
513 gettext("%s is not an integer\n"), optarg);
517 (void) fprintf(stderr,
518 gettext("Depth can not be negative.\n"));
521 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
525 #define PROGRESS_DELAY 2 /* seconds */
527 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";
528 static time_t pt_begin;
529 static char *pt_header = NULL;
530 static boolean_t pt_shown;
533 start_progress_timer(void)
535 pt_begin = time(NULL) + PROGRESS_DELAY;
540 set_progress_header(char *header)
542 assert(pt_header == NULL);
543 pt_header = safe_strdup(header);
545 (void) printf("%s: ", header);
546 (void) fflush(stdout);
551 update_progress(char *update)
553 if (!pt_shown && time(NULL) > pt_begin) {
554 int len = strlen(update);
556 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
558 (void) fflush(stdout);
560 } else if (pt_shown) {
561 int len = strlen(update);
563 (void) printf("%s%*.*s", update, len, len, pt_reverse);
564 (void) fflush(stdout);
569 finish_progress(char *done)
572 (void) printf("%s\n", done);
573 (void) fflush(stdout);
579 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
581 * Given an existing dataset, create a writable copy whose initial contents
582 * are the same as the source. The newly created dataset maintains a
583 * dependency on the original; the original cannot be destroyed so long as
586 * The '-p' flag creates all the non-existing ancestors of the target first.
589 zfs_do_clone(int argc, char **argv)
591 zfs_handle_t *zhp = NULL;
592 boolean_t parents = B_FALSE;
597 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
601 while ((c = getopt(argc, argv, "o:p")) != -1) {
604 if (parseprop(props))
611 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
620 /* check number of arguments */
622 (void) fprintf(stderr, gettext("missing source dataset "
627 (void) fprintf(stderr, gettext("missing target dataset "
632 (void) fprintf(stderr, gettext("too many arguments\n"));
636 /* open the source dataset */
637 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
640 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
643 * Now create the ancestors of the target dataset. If the
644 * target already exists and '-p' option was used we should not
647 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
650 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
655 ret = zfs_clone(zhp, argv[1], props);
657 /* create the mountpoint if necessary */
661 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
663 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
664 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
665 ret = zfs_share(clone);
684 * zfs create [-p] [-o prop=value] ... fs
685 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
687 * Create a new dataset. This command can be used to create filesystems
688 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
689 * For volumes, the user must specify a size to be used.
691 * The '-s' flag applies only to volumes, and indicates that we should not try
692 * to set the reservation for this volume. By default we set a reservation
693 * equal to the size for any volume. For pools with SPA_VERSION >=
694 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
696 * The '-p' flag creates all the non-existing ancestors of the target first.
699 zfs_do_create(int argc, char **argv)
701 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
702 zfs_handle_t *zhp = NULL;
705 boolean_t noreserve = B_FALSE;
706 boolean_t bflag = B_FALSE;
707 boolean_t parents = B_FALSE;
711 int canmount = ZFS_CANMOUNT_OFF;
713 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
717 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
720 type = ZFS_TYPE_VOLUME;
721 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
722 (void) fprintf(stderr, gettext("bad volume "
723 "size '%s': %s\n"), optarg,
724 libzfs_error_description(g_zfs));
728 if (nvlist_add_uint64(props,
729 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
738 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
739 (void) fprintf(stderr, gettext("bad volume "
740 "block size '%s': %s\n"), optarg,
741 libzfs_error_description(g_zfs));
745 if (nvlist_add_uint64(props,
746 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
751 if (parseprop(props))
758 (void) fprintf(stderr, gettext("missing size "
763 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
769 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
770 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
771 "used when creating a volume\n"));
778 /* check number of arguments */
780 (void) fprintf(stderr, gettext("missing %s argument\n"),
781 zfs_type_to_name(type));
785 (void) fprintf(stderr, gettext("too many arguments\n"));
789 if (type == ZFS_TYPE_VOLUME && !noreserve) {
790 zpool_handle_t *zpool_handle;
791 uint64_t spa_version;
793 zfs_prop_t resv_prop;
796 if (p = strchr(argv[0], '/'))
798 zpool_handle = zpool_open(g_zfs, argv[0]);
801 if (zpool_handle == NULL)
803 spa_version = zpool_get_prop_int(zpool_handle,
804 ZPOOL_PROP_VERSION, NULL);
805 zpool_close(zpool_handle);
806 if (spa_version >= SPA_VERSION_REFRESERVATION)
807 resv_prop = ZFS_PROP_REFRESERVATION;
809 resv_prop = ZFS_PROP_RESERVATION;
810 volsize = zvol_volsize_to_reservation(volsize, props);
812 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
814 if (nvlist_add_uint64(props,
815 zfs_prop_to_name(resv_prop), volsize) != 0) {
822 if (parents && zfs_name_valid(argv[0], type)) {
824 * Now create the ancestors of target dataset. If the target
825 * already exists and '-p' option was used we should not
828 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
832 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
837 if (zfs_create(g_zfs, argv[0], type, props) != 0)
840 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
845 * if the user doesn't want the dataset automatically mounted,
846 * then skip the mount/share step
848 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
849 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
852 * Mount and/or share the new filesystem as appropriate. We provide a
853 * verbose error message to let the user know that their filesystem was
854 * in fact created, even if we failed to mount or share it.
856 if (canmount == ZFS_CANMOUNT_ON) {
857 if (zfs_mount(zhp, NULL, 0) != 0) {
858 (void) fprintf(stderr, gettext("filesystem "
859 "successfully created, but not mounted\n"));
861 } else if (zfs_share(zhp) != 0) {
862 (void) fprintf(stderr, gettext("filesystem "
863 "successfully created, but not shared\n"));
880 * zfs destroy [-rRf] <fs, vol>
881 * zfs destroy [-rRd] <snap>
883 * -r Recursively destroy all children
884 * -R Recursively destroy all dependents, including clones
885 * -f Force unmounting of any dependents
886 * -d If we can't destroy now, mark for deferred destruction
888 * Destroys the given dataset. By default, it will unmount any filesystems,
889 * and refuse to destroy a dataset that has any dependents. A dependent can
890 * either be a child, or a clone of a child.
892 typedef struct destroy_cbdata {
895 boolean_t cb_recurse;
897 boolean_t cb_doclones;
898 zfs_handle_t *cb_target;
899 boolean_t cb_defer_destroy;
900 boolean_t cb_verbose;
901 boolean_t cb_parsable;
905 /* first snap in contiguous run */
906 zfs_handle_t *cb_firstsnap;
907 /* previous snap in contiguous run */
908 zfs_handle_t *cb_prevsnap;
914 * Check for any dependents based on the '-r' or '-R' flags.
917 destroy_check_dependent(zfs_handle_t *zhp, void *data)
919 destroy_cbdata_t *cbp = data;
920 const char *tname = zfs_get_name(cbp->cb_target);
921 const char *name = zfs_get_name(zhp);
923 if (strncmp(tname, name, strlen(tname)) == 0 &&
924 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
926 * This is a direct descendant, not a clone somewhere else in
933 (void) fprintf(stderr, gettext("cannot destroy '%s': "
934 "%s has children\n"),
935 zfs_get_name(cbp->cb_target),
936 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
937 (void) fprintf(stderr, gettext("use '-r' to destroy "
938 "the following datasets:\n"));
939 cbp->cb_first = B_FALSE;
940 cbp->cb_error = B_TRUE;
943 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
946 * This is a clone. We only want to report this if the '-r'
947 * wasn't specified, or the target is a snapshot.
949 if (!cbp->cb_recurse &&
950 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
954 (void) fprintf(stderr, gettext("cannot destroy '%s': "
955 "%s has dependent clones\n"),
956 zfs_get_name(cbp->cb_target),
957 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
958 (void) fprintf(stderr, gettext("use '-R' to destroy "
959 "the following datasets:\n"));
960 cbp->cb_first = B_FALSE;
961 cbp->cb_error = B_TRUE;
962 cbp->cb_dryrun = B_TRUE;
965 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
974 destroy_callback(zfs_handle_t *zhp, void *data)
976 destroy_cbdata_t *cb = data;
977 const char *name = zfs_get_name(zhp);
979 if (cb->cb_verbose) {
980 if (cb->cb_parsable) {
981 (void) printf("destroy\t%s\n", name);
982 } else if (cb->cb_dryrun) {
983 (void) printf(gettext("would destroy %s\n"),
986 (void) printf(gettext("will destroy %s\n"),
992 * Ignore pools (which we've already flagged as an error before getting
995 if (strchr(zfs_get_name(zhp), '/') == NULL &&
996 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1001 if (!cb->cb_dryrun) {
1002 if (zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1003 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1014 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1016 destroy_cbdata_t *cb = arg;
1017 const char *name = zfs_get_name(zhp);
1020 if (nvlist_exists(cb->cb_nvl, name)) {
1021 if (cb->cb_firstsnap == NULL)
1022 cb->cb_firstsnap = zfs_handle_dup(zhp);
1023 if (cb->cb_prevsnap != NULL)
1024 zfs_close(cb->cb_prevsnap);
1025 /* this snap continues the current range */
1026 cb->cb_prevsnap = zfs_handle_dup(zhp);
1027 if (cb->cb_verbose) {
1028 if (cb->cb_parsable) {
1029 (void) printf("destroy\t%s\n", name);
1030 } else if (cb->cb_dryrun) {
1031 (void) printf(gettext("would destroy %s\n"),
1034 (void) printf(gettext("will destroy %s\n"),
1038 } else if (cb->cb_firstsnap != NULL) {
1039 /* end of this range */
1041 err = zfs_get_snapused_int(cb->cb_firstsnap,
1042 cb->cb_prevsnap, &used);
1043 cb->cb_snapused += used;
1044 zfs_close(cb->cb_firstsnap);
1045 cb->cb_firstsnap = NULL;
1046 zfs_close(cb->cb_prevsnap);
1047 cb->cb_prevsnap = NULL;
1054 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1057 assert(cb->cb_firstsnap == NULL);
1058 assert(cb->cb_prevsnap == NULL);
1059 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1060 if (cb->cb_firstsnap != NULL) {
1063 err = zfs_get_snapused_int(cb->cb_firstsnap,
1064 cb->cb_prevsnap, &used);
1066 cb->cb_snapused += used;
1067 zfs_close(cb->cb_firstsnap);
1068 cb->cb_firstsnap = NULL;
1069 zfs_close(cb->cb_prevsnap);
1070 cb->cb_prevsnap = NULL;
1076 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1078 destroy_cbdata_t *cb = arg;
1081 /* Check for clones. */
1082 if (!cb->cb_doclones) {
1083 cb->cb_target = zhp;
1084 cb->cb_first = B_TRUE;
1085 err = zfs_iter_dependents(zhp, B_TRUE,
1086 destroy_check_dependent, cb);
1090 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1098 gather_snapshots(zfs_handle_t *zhp, void *arg)
1100 destroy_cbdata_t *cb = arg;
1103 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1109 if (cb->cb_verbose) {
1110 err = destroy_print_snapshots(zhp, cb);
1116 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1124 destroy_clones(destroy_cbdata_t *cb)
1127 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1129 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1130 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1133 boolean_t defer = cb->cb_defer_destroy;
1137 * We can't defer destroy non-snapshots, so set it to
1138 * false while destroying the clones.
1140 cb->cb_defer_destroy = B_FALSE;
1141 err = zfs_iter_dependents(zhp, B_FALSE,
1142 destroy_callback, cb);
1143 cb->cb_defer_destroy = defer;
1153 zfs_do_destroy(int argc, char **argv)
1155 destroy_cbdata_t cb = { 0 };
1159 zfs_type_t type = ZFS_TYPE_DATASET;
1162 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1165 cb.cb_verbose = B_TRUE;
1168 cb.cb_verbose = B_TRUE;
1169 cb.cb_parsable = B_TRUE;
1172 cb.cb_dryrun = B_TRUE;
1175 cb.cb_defer_destroy = B_TRUE;
1176 type = ZFS_TYPE_SNAPSHOT;
1179 cb.cb_force = B_TRUE;
1182 cb.cb_recurse = B_TRUE;
1185 cb.cb_recurse = B_TRUE;
1186 cb.cb_doclones = B_TRUE;
1190 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1199 /* check number of arguments */
1201 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1205 (void) fprintf(stderr, gettext("too many arguments\n"));
1209 at = strchr(argv[0], '@');
1213 /* Build the list of snaps to destroy in cb_nvl. */
1214 if (nvlist_alloc(&cb.cb_nvl, NV_UNIQUE_NAME, 0) != 0)
1218 zhp = zfs_open(g_zfs, argv[0],
1219 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1223 cb.cb_snapspec = at + 1;
1224 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1227 nvlist_free(cb.cb_nvl);
1231 if (nvlist_empty(cb.cb_nvl)) {
1232 (void) fprintf(stderr, gettext("could not find any "
1233 "snapshots to destroy; check snapshot names.\n"));
1235 nvlist_free(cb.cb_nvl);
1239 if (cb.cb_verbose) {
1241 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1242 if (cb.cb_parsable) {
1243 (void) printf("reclaim\t%llu\n",
1245 } else if (cb.cb_dryrun) {
1246 (void) printf(gettext("would reclaim %s\n"),
1249 (void) printf(gettext("will reclaim %s\n"),
1254 if (!cb.cb_dryrun) {
1256 err = destroy_clones(&cb);
1258 err = zfs_destroy_snaps_nvl(zhp, cb.cb_nvl,
1259 cb.cb_defer_destroy);
1264 nvlist_free(cb.cb_nvl);
1268 /* Open the given dataset */
1269 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1275 * Perform an explicit check for pools before going any further.
1277 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1278 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1279 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1280 "operation does not apply to pools\n"),
1282 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1283 "%s' to destroy all datasets in the pool\n"),
1285 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1286 "to destroy the pool itself\n"), zfs_get_name(zhp));
1292 * Check for any dependents and/or clones.
1294 cb.cb_first = B_TRUE;
1295 if (!cb.cb_doclones &&
1296 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1307 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1314 * Do the real thing. The callback will close the
1315 * handle regardless of whether it succeeds or not.
1317 if (destroy_callback(zhp, &cb) != 0)
1325 is_recvd_column(zprop_get_cbdata_t *cbp)
1328 zfs_get_column_t col;
1330 for (i = 0; i < ZFS_GET_NCOLS &&
1331 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1332 if (col == GET_COL_RECVD)
1338 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1339 * < all | property[,property]... > < fs | snap | vol > ...
1341 * -r recurse over any child datasets
1342 * -H scripted mode. Headers are stripped, and fields are separated
1343 * by tabs instead of spaces.
1344 * -o Set of fields to display. One of "name,property,value,
1345 * received,source". Default is "name,property,value,source".
1346 * "all" is an alias for all five.
1347 * -s Set of sources to allow. One of
1348 * "local,default,inherited,received,temporary,none". Default is
1350 * -p Display values in parsable (literal) format.
1352 * Prints properties for the given datasets. The user can control which
1353 * columns to display as well as which property types to allow.
1357 * Invoked to display the properties for a single dataset.
1360 get_callback(zfs_handle_t *zhp, void *data)
1362 char buf[ZFS_MAXPROPLEN];
1363 char rbuf[ZFS_MAXPROPLEN];
1364 zprop_source_t sourcetype;
1365 char source[ZFS_MAXNAMELEN];
1366 zprop_get_cbdata_t *cbp = data;
1367 nvlist_t *user_props = zfs_get_user_props(zhp);
1368 zprop_list_t *pl = cbp->cb_proplist;
1372 boolean_t received = is_recvd_column(cbp);
1374 for (; pl != NULL; pl = pl->pl_next) {
1375 char *recvdval = NULL;
1377 * Skip the special fake placeholder. This will also skip over
1378 * the name property when 'all' is specified.
1380 if (pl->pl_prop == ZFS_PROP_NAME &&
1381 pl == cbp->cb_proplist)
1384 if (pl->pl_prop != ZPROP_INVAL) {
1385 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1386 sizeof (buf), &sourcetype, source,
1388 cbp->cb_literal) != 0) {
1391 if (!zfs_prop_valid_for_type(pl->pl_prop,
1392 ZFS_TYPE_DATASET)) {
1393 (void) fprintf(stderr,
1394 gettext("No such property '%s'\n"),
1395 zfs_prop_to_name(pl->pl_prop));
1398 sourcetype = ZPROP_SRC_NONE;
1399 (void) strlcpy(buf, "-", sizeof (buf));
1402 if (received && (zfs_prop_get_recvd(zhp,
1403 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1404 cbp->cb_literal) == 0))
1407 zprop_print_one_property(zfs_get_name(zhp), cbp,
1408 zfs_prop_to_name(pl->pl_prop),
1409 buf, sourcetype, source, recvdval);
1410 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1411 sourcetype = ZPROP_SRC_LOCAL;
1413 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1414 buf, sizeof (buf), cbp->cb_literal) != 0) {
1415 sourcetype = ZPROP_SRC_NONE;
1416 (void) strlcpy(buf, "-", sizeof (buf));
1419 zprop_print_one_property(zfs_get_name(zhp), cbp,
1420 pl->pl_user_prop, buf, sourcetype, source, NULL);
1421 } else if (zfs_prop_written(pl->pl_user_prop)) {
1422 sourcetype = ZPROP_SRC_LOCAL;
1424 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1425 buf, sizeof (buf), cbp->cb_literal) != 0) {
1426 sourcetype = ZPROP_SRC_NONE;
1427 (void) strlcpy(buf, "-", sizeof (buf));
1430 zprop_print_one_property(zfs_get_name(zhp), cbp,
1431 pl->pl_user_prop, buf, sourcetype, source, NULL);
1433 if (nvlist_lookup_nvlist(user_props,
1434 pl->pl_user_prop, &propval) != 0) {
1437 sourcetype = ZPROP_SRC_NONE;
1440 verify(nvlist_lookup_string(propval,
1441 ZPROP_VALUE, &strval) == 0);
1442 verify(nvlist_lookup_string(propval,
1443 ZPROP_SOURCE, &sourceval) == 0);
1445 if (strcmp(sourceval,
1446 zfs_get_name(zhp)) == 0) {
1447 sourcetype = ZPROP_SRC_LOCAL;
1448 } else if (strcmp(sourceval,
1449 ZPROP_SOURCE_VAL_RECVD) == 0) {
1450 sourcetype = ZPROP_SRC_RECEIVED;
1452 sourcetype = ZPROP_SRC_INHERITED;
1453 (void) strlcpy(source,
1454 sourceval, sizeof (source));
1458 if (received && (zfs_prop_get_recvd(zhp,
1459 pl->pl_user_prop, rbuf, sizeof (rbuf),
1460 cbp->cb_literal) == 0))
1463 zprop_print_one_property(zfs_get_name(zhp), cbp,
1464 pl->pl_user_prop, strval, sourcetype,
1473 zfs_do_get(int argc, char **argv)
1475 zprop_get_cbdata_t cb = { 0 };
1476 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1477 int types = ZFS_TYPE_DATASET;
1478 char *value, *fields;
1481 zprop_list_t fake_name = { 0 };
1484 * Set up default columns and sources.
1486 cb.cb_sources = ZPROP_SRC_ALL;
1487 cb.cb_columns[0] = GET_COL_NAME;
1488 cb.cb_columns[1] = GET_COL_PROPERTY;
1489 cb.cb_columns[2] = GET_COL_VALUE;
1490 cb.cb_columns[3] = GET_COL_SOURCE;
1491 cb.cb_type = ZFS_TYPE_DATASET;
1494 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1497 cb.cb_literal = B_TRUE;
1500 limit = parse_depth(optarg, &flags);
1503 flags |= ZFS_ITER_RECURSE;
1506 cb.cb_scripted = B_TRUE;
1509 (void) fprintf(stderr, gettext("missing argument for "
1510 "'%c' option\n"), optopt);
1515 * Process the set of columns to display. We zero out
1516 * the structure to give us a blank slate.
1518 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1520 while (*optarg != '\0') {
1521 static char *col_subopts[] =
1522 { "name", "property", "value", "received",
1523 "source", "all", NULL };
1525 if (i == ZFS_GET_NCOLS) {
1526 (void) fprintf(stderr, gettext("too "
1527 "many fields given to -o "
1532 switch (getsubopt(&optarg, col_subopts,
1535 cb.cb_columns[i++] = GET_COL_NAME;
1538 cb.cb_columns[i++] = GET_COL_PROPERTY;
1541 cb.cb_columns[i++] = GET_COL_VALUE;
1544 cb.cb_columns[i++] = GET_COL_RECVD;
1545 flags |= ZFS_ITER_RECVD_PROPS;
1548 cb.cb_columns[i++] = GET_COL_SOURCE;
1552 (void) fprintf(stderr,
1553 gettext("\"all\" conflicts "
1554 "with specific fields "
1555 "given to -o option\n"));
1558 cb.cb_columns[0] = GET_COL_NAME;
1559 cb.cb_columns[1] = GET_COL_PROPERTY;
1560 cb.cb_columns[2] = GET_COL_VALUE;
1561 cb.cb_columns[3] = GET_COL_RECVD;
1562 cb.cb_columns[4] = GET_COL_SOURCE;
1563 flags |= ZFS_ITER_RECVD_PROPS;
1567 (void) fprintf(stderr,
1568 gettext("invalid column name "
1577 while (*optarg != '\0') {
1578 static char *source_subopts[] = {
1579 "local", "default", "inherited",
1580 "received", "temporary", "none",
1583 switch (getsubopt(&optarg, source_subopts,
1586 cb.cb_sources |= ZPROP_SRC_LOCAL;
1589 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1592 cb.cb_sources |= ZPROP_SRC_INHERITED;
1595 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1598 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1601 cb.cb_sources |= ZPROP_SRC_NONE;
1604 (void) fprintf(stderr,
1605 gettext("invalid source "
1614 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1615 while (*optarg != '\0') {
1616 static char *type_subopts[] = { "filesystem",
1617 "volume", "snapshot", "all", NULL };
1619 switch (getsubopt(&optarg, type_subopts,
1622 types |= ZFS_TYPE_FILESYSTEM;
1625 types |= ZFS_TYPE_VOLUME;
1628 types |= ZFS_TYPE_SNAPSHOT;
1631 types = ZFS_TYPE_DATASET;
1635 (void) fprintf(stderr,
1636 gettext("invalid type '%s'\n"),
1644 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1654 (void) fprintf(stderr, gettext("missing property "
1661 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1669 * As part of zfs_expand_proplist(), we keep track of the maximum column
1670 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1671 * need to know the maximum name length. However, the user likely did
1672 * not specify 'name' as one of the properties to fetch, so we need to
1673 * make sure we always include at least this property for
1674 * print_get_headers() to work properly.
1676 if (cb.cb_proplist != NULL) {
1677 fake_name.pl_prop = ZFS_PROP_NAME;
1678 fake_name.pl_width = strlen(gettext("NAME"));
1679 fake_name.pl_next = cb.cb_proplist;
1680 cb.cb_proplist = &fake_name;
1683 cb.cb_first = B_TRUE;
1685 /* run for each object */
1686 ret = zfs_for_each(argc, argv, flags, types, NULL,
1687 &cb.cb_proplist, limit, get_callback, &cb);
1689 if (cb.cb_proplist == &fake_name)
1690 zprop_free_list(fake_name.pl_next);
1692 zprop_free_list(cb.cb_proplist);
1698 * inherit [-rS] <property> <fs|vol> ...
1700 * -r Recurse over all children
1701 * -S Revert to received value, if any
1703 * For each dataset specified on the command line, inherit the given property
1704 * from its parent. Inheriting a property at the pool level will cause it to
1705 * use the default value. The '-r' flag will recurse over all children, and is
1706 * useful for setting a property on a hierarchy-wide basis, regardless of any
1707 * local modifications for each dataset.
1710 typedef struct inherit_cbdata {
1711 const char *cb_propname;
1712 boolean_t cb_received;
1716 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1718 inherit_cbdata_t *cb = data;
1719 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1722 * If we're doing it recursively, then ignore properties that
1723 * are not valid for this type of dataset.
1725 if (prop != ZPROP_INVAL &&
1726 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1729 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1733 inherit_cb(zfs_handle_t *zhp, void *data)
1735 inherit_cbdata_t *cb = data;
1737 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1741 zfs_do_inherit(int argc, char **argv)
1745 inherit_cbdata_t cb = { 0 };
1749 boolean_t received = B_FALSE;
1752 while ((c = getopt(argc, argv, "rS")) != -1) {
1755 flags |= ZFS_ITER_RECURSE;
1762 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1771 /* check number of arguments */
1773 (void) fprintf(stderr, gettext("missing property argument\n"));
1777 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1785 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1786 if (zfs_prop_readonly(prop)) {
1787 (void) fprintf(stderr, gettext(
1788 "%s property is read-only\n"),
1792 if (!zfs_prop_inheritable(prop) && !received) {
1793 (void) fprintf(stderr, gettext("'%s' property cannot "
1794 "be inherited\n"), propname);
1795 if (prop == ZFS_PROP_QUOTA ||
1796 prop == ZFS_PROP_RESERVATION ||
1797 prop == ZFS_PROP_REFQUOTA ||
1798 prop == ZFS_PROP_REFRESERVATION)
1799 (void) fprintf(stderr, gettext("use 'zfs set "
1800 "%s=none' to clear\n"), propname);
1803 if (received && (prop == ZFS_PROP_VOLSIZE ||
1804 prop == ZFS_PROP_VERSION)) {
1805 (void) fprintf(stderr, gettext("'%s' property cannot "
1806 "be reverted to a received value\n"), propname);
1809 } else if (!zfs_prop_user(propname)) {
1810 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1815 cb.cb_propname = propname;
1816 cb.cb_received = received;
1818 if (flags & ZFS_ITER_RECURSE) {
1819 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1820 NULL, NULL, 0, inherit_recurse_cb, &cb);
1822 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1823 NULL, NULL, 0, inherit_cb, &cb);
1829 typedef struct upgrade_cbdata {
1830 uint64_t cb_numupgraded;
1831 uint64_t cb_numsamegraded;
1832 uint64_t cb_numfailed;
1833 uint64_t cb_version;
1835 boolean_t cb_foundone;
1836 char cb_lastfs[ZFS_MAXNAMELEN];
1840 same_pool(zfs_handle_t *zhp, const char *name)
1842 int len1 = strcspn(name, "/@");
1843 const char *zhname = zfs_get_name(zhp);
1844 int len2 = strcspn(zhname, "/@");
1848 return (strncmp(name, zhname, len1) == 0);
1852 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1854 upgrade_cbdata_t *cb = data;
1855 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1857 /* list if it's old/new */
1858 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1859 (cb->cb_newer && version > ZPL_VERSION)) {
1862 str = gettext("The following filesystems are "
1863 "formatted using a newer software version and\n"
1864 "cannot be accessed on the current system.\n\n");
1866 str = gettext("The following filesystems are "
1867 "out of date, and can be upgraded. After being\n"
1868 "upgraded, these filesystems (and any 'zfs send' "
1869 "streams generated from\n"
1870 "subsequent snapshots) will no longer be "
1871 "accessible by older software versions.\n\n");
1874 if (!cb->cb_foundone) {
1876 (void) printf(gettext("VER FILESYSTEM\n"));
1877 (void) printf(gettext("--- ------------\n"));
1878 cb->cb_foundone = B_TRUE;
1881 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1888 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1890 upgrade_cbdata_t *cb = data;
1891 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1892 int needed_spa_version;
1895 if (zfs_spa_version(zhp, &spa_version) < 0)
1898 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1900 if (needed_spa_version < 0)
1903 if (spa_version < needed_spa_version) {
1905 (void) printf(gettext("%s: can not be "
1906 "upgraded; the pool version needs to first "
1907 "be upgraded\nto version %d\n\n"),
1908 zfs_get_name(zhp), needed_spa_version);
1914 if (version < cb->cb_version) {
1916 (void) snprintf(verstr, sizeof (verstr),
1917 "%llu", cb->cb_version);
1918 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1920 * If they did "zfs upgrade -a", then we could
1921 * be doing ioctls to different pools. We need
1922 * to log this history once to each pool.
1924 verify(zpool_stage_history(g_zfs, history_str) == 0);
1926 if (zfs_prop_set(zhp, "version", verstr) == 0)
1927 cb->cb_numupgraded++;
1930 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1931 } else if (version > cb->cb_version) {
1932 /* can't downgrade */
1933 (void) printf(gettext("%s: can not be downgraded; "
1934 "it is already at version %u\n"),
1935 zfs_get_name(zhp), version);
1938 cb->cb_numsamegraded++;
1946 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1949 zfs_do_upgrade(int argc, char **argv)
1951 boolean_t all = B_FALSE;
1952 boolean_t showversions = B_FALSE;
1954 upgrade_cbdata_t cb = { 0 };
1956 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1959 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
1962 flags |= ZFS_ITER_RECURSE;
1965 showversions = B_TRUE;
1968 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
1969 optarg, &cb.cb_version) != 0) {
1970 (void) fprintf(stderr,
1971 gettext("invalid version %s\n"), optarg);
1980 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1989 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
1991 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
1992 cb.cb_version || argc))
1994 if ((all || argc) && (showversions))
2000 /* Show info on available versions. */
2001 (void) printf(gettext("The following filesystem versions are "
2003 (void) printf(gettext("VER DESCRIPTION\n"));
2004 (void) printf("--- -----------------------------------------"
2005 "---------------\n");
2006 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2007 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2008 (void) printf(gettext(" 3 Case insensitive and filesystem "
2009 "user identifier (FUID)\n"));
2010 (void) printf(gettext(" 4 userquota, groupquota "
2012 (void) printf(gettext(" 5 System attributes\n"));
2013 (void) printf(gettext("\nFor more information on a particular "
2014 "version, including supported releases,\n"));
2015 (void) printf("see the ZFS Administration Guide.\n\n");
2017 } else if (argc || all) {
2018 /* Upgrade filesystems */
2019 if (cb.cb_version == 0)
2020 cb.cb_version = ZPL_VERSION;
2021 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2022 NULL, NULL, 0, upgrade_set_callback, &cb);
2023 (void) printf(gettext("%llu filesystems upgraded\n"),
2025 if (cb.cb_numsamegraded) {
2026 (void) printf(gettext("%llu filesystems already at "
2028 cb.cb_numsamegraded);
2030 if (cb.cb_numfailed != 0)
2033 /* List old-version filesytems */
2035 (void) printf(gettext("This system is currently running "
2036 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2038 flags |= ZFS_ITER_RECURSE;
2039 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2040 NULL, NULL, 0, upgrade_list_callback, &cb);
2042 found = cb.cb_foundone;
2043 cb.cb_foundone = B_FALSE;
2044 cb.cb_newer = B_TRUE;
2046 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2047 NULL, NULL, 0, upgrade_list_callback, &cb);
2049 if (!cb.cb_foundone && !found) {
2050 (void) printf(gettext("All filesystems are "
2051 "formatted with the current version.\n"));
2058 #define USTYPE_USR_BIT (0)
2059 #define USTYPE_GRP_BIT (1)
2060 #define USTYPE_PSX_BIT (2)
2061 #define USTYPE_SMB_BIT (3)
2063 #define USTYPE_USR (1 << USTYPE_USR_BIT)
2064 #define USTYPE_GRP (1 << USTYPE_GRP_BIT)
2066 #define USTYPE_PSX (1 << USTYPE_PSX_BIT)
2067 #define USTYPE_SMB (1 << USTYPE_SMB_BIT)
2069 #define USTYPE_PSX_USR (USTYPE_PSX | USTYPE_USR)
2070 #define USTYPE_SMB_USR (USTYPE_SMB | USTYPE_USR)
2071 #define USTYPE_PSX_GRP (USTYPE_PSX | USTYPE_GRP)
2072 #define USTYPE_SMB_GRP (USTYPE_SMB | USTYPE_GRP)
2073 #define USTYPE_ALL (USTYPE_PSX_USR | USTYPE_SMB_USR \
2074 | USTYPE_PSX_GRP | USTYPE_SMB_GRP)
2077 #define USPROP_USED_BIT (0)
2078 #define USPROP_QUOTA_BIT (1)
2080 #define USPROP_USED (1 << USPROP_USED_BIT)
2081 #define USPROP_QUOTA (1 << USPROP_QUOTA_BIT)
2083 typedef struct us_node {
2085 uu_avl_node_t usn_avlnode;
2086 uu_list_node_t usn_listnode;
2089 typedef struct us_cbdata {
2091 uu_avl_pool_t *cb_avl_pool;
2093 boolean_t cb_numname;
2094 boolean_t cb_nicenum;
2095 boolean_t cb_sid2posix;
2096 zfs_userquota_prop_t cb_prop;
2097 zfs_sort_column_t *cb_sortcol;
2098 size_t cb_max_typelen;
2099 size_t cb_max_namelen;
2100 size_t cb_max_usedlen;
2101 size_t cb_max_quotalen;
2105 zfs_sort_column_t *si_sortcol;
2106 boolean_t si_num_name;
2107 boolean_t si_parsable;
2111 us_compare(const void *larg, const void *rarg, void *unused)
2113 const us_node_t *l = larg;
2114 const us_node_t *r = rarg;
2116 us_sort_info_t *si = (us_sort_info_t *)unused;
2117 zfs_sort_column_t *sortcol = si->si_sortcol;
2118 boolean_t num_name = si->si_num_name;
2119 nvlist_t *lnvl = l->usn_nvl;
2120 nvlist_t *rnvl = r->usn_nvl;
2122 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2129 zfs_prop_t prop = sortcol->sc_prop;
2130 const char *propname = NULL;
2131 boolean_t reverse = sortcol->sc_reverse;
2136 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2137 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2139 rc = (rv32 > lv32) ? 1 : -1;
2144 (void) nvlist_lookup_uint32(lnvl, propname,
2146 (void) nvlist_lookup_uint32(rnvl, propname,
2149 rc = (rv32 > lv32) ? 1 : -1;
2151 (void) nvlist_lookup_string(lnvl, propname,
2153 (void) nvlist_lookup_string(rnvl, propname,
2155 rc = strcmp(lvstr, rvstr);
2160 case ZFS_PROP_QUOTA:
2161 if (ZFS_PROP_USED == prop)
2165 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2166 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2168 rc = (rv64 > lv64) ? 1 : -1;
2173 return (reverse ? 1 : -1);
2175 return (reverse ? -1 : 1);
2181 static inline const char *
2182 us_type2str(unsigned field_type)
2184 switch (field_type) {
2185 case USTYPE_PSX_USR:
2186 return ("POSIX User");
2187 case USTYPE_PSX_GRP:
2188 return ("POSIX Group");
2189 case USTYPE_SMB_USR:
2190 return ("SMB User");
2191 case USTYPE_SMB_GRP:
2192 return ("SMB Group");
2194 return ("Undefined");
2202 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2204 us_cbdata_t *cb = (us_cbdata_t *)arg;
2205 zfs_userquota_prop_t prop = cb->cb_prop;
2211 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2212 uu_avl_t *avl = cb->cb_avl;
2216 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2218 const char *typestr;
2222 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2224 if (domain == NULL || domain[0] == '\0') {
2226 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2227 type = USTYPE_PSX_GRP;
2228 struct group *g = getgrgid(rid);
2232 type = USTYPE_PSX_USR;
2233 struct passwd *p = getpwuid(rid);
2238 char sid[ZFS_MAXNAMELEN+32];
2243 directory_error_t e;
2246 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2248 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2249 type = USTYPE_SMB_GRP;
2251 err = sid_to_id(sid, B_FALSE, &id);
2254 type = USTYPE_SMB_USR;
2256 err = sid_to_id(sid, B_TRUE, &id);
2264 e = directory_name_from_sid(NULL, sid, &name, &classes);
2266 directory_error_free(e);
2277 * if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA)
2283 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED)
2288 (void) snprintf(namebuf, sizeof (namebuf), "%u", rid);
2293 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2295 (void) sprintf(sizebuf, "%llu", space);
2297 node = safe_malloc(sizeof (us_node_t));
2298 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2300 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
2305 if (nvlist_add_uint32(props, "type", type) != 0)
2308 if (cb->cb_numname) {
2309 if (nvlist_add_uint32(props, "name", rid) != 0)
2311 namelen = strlen(namebuf);
2313 if (nvlist_add_string(props, "name", name) != 0)
2315 namelen = strlen(name);
2318 typestr = us_type2str(type);
2319 typelen = strlen(gettext(typestr));
2320 if (typelen > cb->cb_max_typelen)
2321 cb->cb_max_typelen = typelen;
2323 if (namelen > cb->cb_max_namelen)
2324 cb->cb_max_namelen = namelen;
2326 sizelen = strlen(sizebuf);
2327 if (0 == strcmp(propname, "used")) {
2328 if (sizelen > cb->cb_max_usedlen)
2329 cb->cb_max_usedlen = sizelen;
2331 if (sizelen > cb->cb_max_quotalen)
2332 cb->cb_max_quotalen = sizelen;
2335 node->usn_nvl = props;
2337 n = uu_avl_find(avl, node, &sortinfo, &idx);
2339 uu_avl_insert(avl, node, idx);
2344 props = node->usn_nvl;
2347 if (nvlist_add_uint64(props, propname, space) != 0)
2353 static inline boolean_t
2354 usprop_check(zfs_userquota_prop_t p, unsigned types, unsigned props)
2360 case ZFS_PROP_USERUSED:
2364 case ZFS_PROP_USERQUOTA:
2366 prop = USPROP_QUOTA;
2368 case ZFS_PROP_GROUPUSED:
2372 case ZFS_PROP_GROUPQUOTA:
2374 prop = USPROP_QUOTA;
2380 return (type & types && prop & props);
2383 #define USFIELD_TYPE (1 << 0)
2384 #define USFIELD_NAME (1 << 1)
2385 #define USFIELD_USED (1 << 2)
2386 #define USFIELD_QUOTA (1 << 3)
2387 #define USFIELD_ALL (USFIELD_TYPE | USFIELD_NAME | USFIELD_USED | USFIELD_QUOTA)
2390 parsefields(unsigned *fieldsp, char **names, unsigned *bits, size_t len)
2392 char *field = optarg;
2397 boolean_t found = B_FALSE;
2398 delim = strchr(field, ',');
2402 for (i = 0; i < len; i++)
2403 if (0 == strcmp(field, names[i])) {
2405 *fieldsp |= bits[i];
2410 (void) fprintf(stderr, gettext("invalid type '%s'"
2411 "for -t option\n"), field);
2422 static char *type_names[] = { "posixuser", "smbuser", "posixgroup", "smbgroup",
2424 static unsigned type_bits[] = {
2432 static char *us_field_names[] = { "type", "name", "used", "quota" };
2433 static unsigned us_field_bits[] = {
2441 print_us_node(boolean_t scripted, boolean_t parseable, unsigned fields,
2442 size_t type_width, size_t name_width, size_t used_width,
2443 size_t quota_width, us_node_t *node)
2445 nvlist_t *nvl = node->usn_nvl;
2446 nvpair_t *nvp = NULL;
2447 char valstr[ZFS_MAXNAMELEN];
2448 boolean_t first = B_TRUE;
2449 boolean_t quota_found = B_FALSE;
2451 if (fields & USFIELD_QUOTA && !nvlist_exists(nvl, "quota"))
2452 if (nvlist_add_string(nvl, "quota", "none") != 0)
2455 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2456 char *pname = nvpair_name(nvp);
2457 data_type_t type = nvpair_type(nvp);
2460 char *strval = NULL;
2464 for (i = 0; i < 4; i++) {
2465 if (0 == strcmp(pname, us_field_names[i])) {
2466 field = us_field_bits[i];
2471 if (!(field & fields))
2475 case DATA_TYPE_UINT32:
2476 (void) nvpair_value_uint32(nvp, &val32);
2478 case DATA_TYPE_UINT64:
2479 (void) nvpair_value_uint64(nvp, &val64);
2481 case DATA_TYPE_STRING:
2482 (void) nvpair_value_string(nvp, &strval);
2485 (void) fprintf(stderr, "Invalid data type\n");
2490 (void) printf("\t");
2496 strval = (char *)us_type2str(val32);
2500 if (type == DATA_TYPE_UINT64) {
2501 (void) sprintf(valstr, "%llu", val64);
2508 if (type == DATA_TYPE_UINT64) {
2509 (void) nvpair_value_uint64(nvp, &val64);
2511 (void) sprintf(valstr, "%llu", val64);
2513 zfs_nicenum(val64, valstr,
2518 if (field == USFIELD_USED)
2521 quota_found = B_FALSE;
2522 width = quota_width;
2528 if (field == USFIELD_QUOTA && !quota_found)
2529 (void) printf("%*s", width, strval);
2531 if (type == DATA_TYPE_STRING)
2532 (void) printf("%-*s", width, strval);
2534 (void) printf("%*s", width, strval);
2541 (void) printf("\n");
2545 print_us(boolean_t scripted, boolean_t parsable, unsigned fields,
2546 unsigned type_width, unsigned name_width, unsigned used_width,
2547 unsigned quota_width, boolean_t rmnode, uu_avl_t *avl)
2549 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2553 size_t width[4] = { type_width, name_width, used_width, quota_width };
2556 boolean_t first = B_TRUE;
2557 for (i = 0; i < 4; i++) {
2558 unsigned field = us_field_bits[i];
2559 if (!(field & fields))
2562 col = gettext(us_field_hdr[i]);
2563 if (field == USFIELD_TYPE || field == USFIELD_NAME)
2564 (void) printf(first?"%-*s":" %-*s", width[i],
2567 (void) printf(first?"%*s":" %*s", width[i],
2571 (void) printf("\n");
2574 for (node = uu_avl_first(avl); node != NULL;
2575 node = uu_avl_next(avl, node)) {
2576 print_us_node(scripted, parsable, fields, type_width,
2577 name_width, used_width, used_width, node);
2579 nvlist_free(node->usn_nvl);
2584 zfs_do_userspace(int argc, char **argv)
2587 zfs_userquota_prop_t p;
2589 uu_avl_pool_t *avl_pool;
2591 uu_avl_walk_t *walk;
2594 boolean_t scripted = B_FALSE;
2595 boolean_t prtnum = B_FALSE;
2596 boolean_t parseable = B_FALSE;
2597 boolean_t sid2posix = B_FALSE;
2600 zfs_sort_column_t *default_sortcol = NULL;
2601 zfs_sort_column_t *sortcol = NULL;
2602 unsigned types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2603 unsigned fields = 0;
2604 unsigned props = USPROP_USED | USPROP_QUOTA;
2607 boolean_t resort_avl = B_FALSE;
2613 if (0 == strcmp(cmd, "groupspace"))
2614 /* toggle default group types */
2615 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2618 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2630 if (parsefields(&fields, us_field_names, us_field_bits,
2635 if (zfs_add_sort_column(&sortcol, optarg,
2637 (void) fprintf(stderr,
2638 gettext("invalid property '%s'\n"), optarg);
2643 if (zfs_add_sort_column(&sortcol, optarg,
2645 (void) fprintf(stderr,
2646 gettext("invalid property '%s'\n"), optarg);
2651 if (parsefields(&types, type_names, type_bits, 5))
2658 (void) fprintf(stderr, gettext("missing argument for "
2659 "'%c' option\n"), optopt);
2663 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2672 /* ok, now we have sorted by default colums (type,name) avl tree */
2674 zfs_sort_column_t *sc;
2675 for (sc = sortcol; sc; sc = sc->sc_next) {
2676 if (sc->sc_prop == ZFS_PROP_QUOTA) {
2677 resort_avl = B_TRUE;
2684 fields = USFIELD_ALL;
2686 if ((zhp = zfs_open(g_zfs, argv[argc-1], ZFS_TYPE_DATASET)) == NULL)
2689 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2690 offsetof(us_node_t, usn_avlnode),
2691 us_compare, UU_DEFAULT)) == NULL)
2693 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2696 if (sortcol && !resort_avl)
2697 cb.cb_sortcol = sortcol;
2699 (void) zfs_add_sort_column(&default_sortcol, "type", B_FALSE);
2700 (void) zfs_add_sort_column(&default_sortcol, "name", B_FALSE);
2701 cb.cb_sortcol = default_sortcol;
2703 cb.cb_numname = prtnum;
2704 cb.cb_nicenum = !parseable;
2705 cb.cb_avl_pool = avl_pool;
2706 cb.cb_avl = avl_tree;
2707 cb.cb_sid2posix = sid2posix;
2708 cb.cb_max_typelen = strlen(gettext("TYPE"));
2709 cb.cb_max_namelen = strlen(gettext("NAME"));
2710 cb.cb_max_usedlen = strlen(gettext("USED"));
2711 cb.cb_max_quotalen = strlen(gettext("QUOTA"));
2713 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2714 if (!usprop_check(p, types, props))
2718 error = zfs_userspace(zhp, p, userspace_cb, &cb);
2727 uu_list_pool_t *listpool;
2729 uu_avl_index_t idx = 0;
2730 uu_list_index_t idx2 = 0;
2731 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2732 offsetof(us_node_t, usn_listnode), NULL,
2734 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2736 node = uu_avl_first(avl_tree);
2737 uu_list_node_init(node, &node->usn_listnode, listpool);
2738 while (node != NULL) {
2740 node = uu_avl_next(avl_tree, node);
2741 uu_avl_remove(avl_tree, rmnode);
2742 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL) {
2743 uu_list_insert(list, rmnode, idx2);
2747 for (node = uu_list_first(list); node != NULL;
2748 node = uu_list_next(list, node)) {
2749 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2750 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) ==
2752 uu_avl_insert(avl_tree, node, idx);
2755 uu_list_destroy(list);
2758 /* print & free node`s nvlist memory */
2759 print_us(scripted, parseable, fields, cb.cb_max_typelen,
2760 cb.cb_max_namelen, cb.cb_max_usedlen,
2761 cb.cb_max_quotalen, B_TRUE, cb.cb_avl);
2764 zfs_free_sort_columns(sortcol);
2765 zfs_free_sort_columns(default_sortcol);
2768 * Finally, clean up the AVL tree.
2770 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2773 while ((node = uu_avl_walk_next(walk)) != NULL) {
2774 uu_avl_remove(cb.cb_avl, node);
2778 uu_avl_walk_end(walk);
2779 uu_avl_destroy(avl_tree);
2780 uu_avl_pool_destroy(avl_pool);
2786 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2787 * [-s property [-s property]...] [-S property [-S property]...]
2790 * -r Recurse over all children
2791 * -d Limit recursion by depth.
2792 * -H Scripted mode; elide headers and separate columns by tabs
2793 * -o Control which fields to display.
2794 * -t Control which object types to display.
2795 * -s Specify sort columns, descending order.
2796 * -S Specify sort columns, ascending order.
2798 * When given no arguments, lists all filesystems in the system.
2799 * Otherwise, list the specified datasets, optionally recursing down them if
2800 * '-r' is specified.
2802 typedef struct list_cbdata {
2804 boolean_t cb_scripted;
2805 zprop_list_t *cb_proplist;
2809 * Given a list of columns to display, output appropriate headers for each one.
2812 print_header(zprop_list_t *pl)
2814 char headerbuf[ZFS_MAXPROPLEN];
2817 boolean_t first = B_TRUE;
2818 boolean_t right_justify;
2820 for (; pl != NULL; pl = pl->pl_next) {
2827 right_justify = B_FALSE;
2828 if (pl->pl_prop != ZPROP_INVAL) {
2829 header = zfs_prop_column_name(pl->pl_prop);
2830 right_justify = zfs_prop_align_right(pl->pl_prop);
2832 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2833 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2834 headerbuf[i] = '\0';
2838 if (pl->pl_next == NULL && !right_justify)
2839 (void) printf("%s", header);
2840 else if (right_justify)
2841 (void) printf("%*s", pl->pl_width, header);
2843 (void) printf("%-*s", pl->pl_width, header);
2846 (void) printf("\n");
2850 * Given a dataset and a list of fields, print out all the properties according
2851 * to the described layout.
2854 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
2856 boolean_t first = B_TRUE;
2857 char property[ZFS_MAXPROPLEN];
2858 nvlist_t *userprops = zfs_get_user_props(zhp);
2861 boolean_t right_justify;
2864 for (; pl != NULL; pl = pl->pl_next) {
2867 (void) printf("\t");
2874 if (pl->pl_prop == ZFS_PROP_NAME) {
2875 (void) strlcpy(property, zfs_get_name(zhp),
2878 right_justify = zfs_prop_align_right(pl->pl_prop);
2879 } else if (pl->pl_prop != ZPROP_INVAL) {
2880 if (zfs_prop_get(zhp, pl->pl_prop, property,
2881 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2886 right_justify = zfs_prop_align_right(pl->pl_prop);
2887 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2888 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2889 property, sizeof (property), B_FALSE) != 0)
2893 right_justify = B_TRUE;
2894 } else if (zfs_prop_written(pl->pl_user_prop)) {
2895 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2896 property, sizeof (property), B_FALSE) != 0)
2900 right_justify = B_TRUE;
2902 if (nvlist_lookup_nvlist(userprops,
2903 pl->pl_user_prop, &propval) != 0)
2906 verify(nvlist_lookup_string(propval,
2907 ZPROP_VALUE, &propstr) == 0);
2908 right_justify = B_FALSE;
2911 width = pl->pl_width;
2914 * If this is being called in scripted mode, or if this is the
2915 * last column and it is left-justified, don't include a width
2918 if (scripted || (pl->pl_next == NULL && !right_justify))
2919 (void) printf("%s", propstr);
2920 else if (right_justify)
2921 (void) printf("%*s", width, propstr);
2923 (void) printf("%-*s", width, propstr);
2926 (void) printf("\n");
2930 * Generic callback function to list a dataset or snapshot.
2933 list_callback(zfs_handle_t *zhp, void *data)
2935 list_cbdata_t *cbp = data;
2937 if (cbp->cb_first) {
2938 if (!cbp->cb_scripted)
2939 print_header(cbp->cb_proplist);
2940 cbp->cb_first = B_FALSE;
2943 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2949 zfs_do_list(int argc, char **argv)
2952 boolean_t scripted = B_FALSE;
2953 static char default_fields[] =
2954 "name,used,available,referenced,mountpoint";
2955 int types = ZFS_TYPE_DATASET;
2956 boolean_t types_specified = B_FALSE;
2957 char *fields = NULL;
2958 list_cbdata_t cb = { 0 };
2962 zfs_sort_column_t *sortcol = NULL;
2963 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2966 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2972 limit = parse_depth(optarg, &flags);
2975 flags |= ZFS_ITER_RECURSE;
2981 if (zfs_add_sort_column(&sortcol, optarg,
2983 (void) fprintf(stderr,
2984 gettext("invalid property '%s'\n"), optarg);
2989 if (zfs_add_sort_column(&sortcol, optarg,
2991 (void) fprintf(stderr,
2992 gettext("invalid property '%s'\n"), optarg);
2998 types_specified = B_TRUE;
2999 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3000 while (*optarg != '\0') {
3001 static char *type_subopts[] = { "filesystem",
3002 "volume", "snapshot", "all", NULL };
3004 switch (getsubopt(&optarg, type_subopts,
3007 types |= ZFS_TYPE_FILESYSTEM;
3010 types |= ZFS_TYPE_VOLUME;
3013 types |= ZFS_TYPE_SNAPSHOT;
3016 types = ZFS_TYPE_DATASET;
3020 (void) fprintf(stderr,
3021 gettext("invalid type '%s'\n"),
3028 (void) fprintf(stderr, gettext("missing argument for "
3029 "'%c' option\n"), optopt);
3033 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3043 fields = default_fields;
3046 * If we are only going to list snapshot names and sort by name,
3047 * then we can use faster version.
3049 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3050 flags |= ZFS_ITER_SIMPLE;
3053 * If "-o space" and no types were specified, don't display snapshots.
3055 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3056 types &= ~ZFS_TYPE_SNAPSHOT;
3059 * If the user specifies '-o all', the zprop_get_list() doesn't
3060 * normally include the name of the dataset. For 'zfs list', we always
3061 * want this property to be first.
3063 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3067 cb.cb_scripted = scripted;
3068 cb.cb_first = B_TRUE;
3070 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3071 limit, list_callback, &cb);
3073 zprop_free_list(cb.cb_proplist);
3074 zfs_free_sort_columns(sortcol);
3076 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3077 (void) printf(gettext("no datasets available\n"));
3083 * zfs rename <fs | snap | vol> <fs | snap | vol>
3084 * zfs rename -p <fs | vol> <fs | vol>
3085 * zfs rename -r <snap> <snap>
3086 * zfs rename -u [-p] <fs> <fs>
3088 * Renames the given dataset to another of the same type.
3090 * The '-p' flag creates all the non-existing ancestors of the target first.
3094 zfs_do_rename(int argc, char **argv)
3097 renameflags_t flags = { 0 };
3101 boolean_t parents = B_FALSE;
3104 while ((c = getopt(argc, argv, "pru")) != -1) {
3110 flags.recurse = B_TRUE;
3113 flags.nounmount = B_TRUE;
3117 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3126 /* check number of arguments */
3128 (void) fprintf(stderr, gettext("missing source dataset "
3133 (void) fprintf(stderr, gettext("missing target dataset "
3138 (void) fprintf(stderr, gettext("too many arguments\n"));
3142 if (flags.recurse && parents) {
3143 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3148 if (flags.recurse && strchr(argv[0], '@') == 0) {
3149 (void) fprintf(stderr, gettext("source dataset for recursive "
3150 "rename must be a snapshot\n"));
3154 if (flags.nounmount && parents) {
3155 (void) fprintf(stderr, gettext("-u and -r options are mutually "
3160 if (flags.nounmount)
3161 types = ZFS_TYPE_FILESYSTEM;
3163 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3165 types = ZFS_TYPE_DATASET;
3167 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3170 /* If we were asked and the name looks good, try to create ancestors. */
3171 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3172 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3177 ret = (zfs_rename(zhp, argv[1], flags) != 0);
3186 * Promotes the given clone fs to be the parent
3190 zfs_do_promote(int argc, char **argv)
3196 if (argc > 1 && argv[1][0] == '-') {
3197 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3202 /* check number of arguments */
3204 (void) fprintf(stderr, gettext("missing clone filesystem"
3209 (void) fprintf(stderr, gettext("too many arguments\n"));
3213 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3217 ret = (zfs_promote(zhp) != 0);
3225 * zfs rollback [-rRf] <snapshot>
3227 * -r Delete any intervening snapshots before doing rollback
3228 * -R Delete any snapshots and their clones
3229 * -f ignored for backwards compatability
3231 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3232 * since then and making it the active dataset. If more recent snapshots exist,
3233 * the command will complain unless the '-r' flag is given.
3235 typedef struct rollback_cbdata {
3241 boolean_t cb_recurse;
3242 boolean_t cb_dependent;
3243 } rollback_cbdata_t;
3246 * Report any snapshots more recent than the one specified. Used when '-r' is
3247 * not specified. We reuse this same callback for the snapshot dependents - if
3248 * 'cb_dependent' is set, then this is a dependent and we should report it
3249 * without checking the transaction group.
3252 rollback_check(zfs_handle_t *zhp, void *data)
3254 rollback_cbdata_t *cbp = data;
3256 if (cbp->cb_doclones) {
3261 if (!cbp->cb_dependent) {
3262 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
3263 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3264 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3267 if (cbp->cb_first && !cbp->cb_recurse) {
3268 (void) fprintf(stderr, gettext("cannot "
3269 "rollback to '%s': more recent snapshots "
3272 (void) fprintf(stderr, gettext("use '-r' to "
3273 "force deletion of the following "
3279 if (cbp->cb_recurse) {
3280 cbp->cb_dependent = B_TRUE;
3281 if (zfs_iter_dependents(zhp, B_TRUE,
3282 rollback_check, cbp) != 0) {
3286 cbp->cb_dependent = B_FALSE;
3288 (void) fprintf(stderr, "%s\n",
3293 if (cbp->cb_first && cbp->cb_recurse) {
3294 (void) fprintf(stderr, gettext("cannot rollback to "
3295 "'%s': clones of previous snapshots exist\n"),
3297 (void) fprintf(stderr, gettext("use '-R' to "
3298 "force deletion of the following clones and "
3304 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3312 zfs_do_rollback(int argc, char **argv)
3316 boolean_t force = B_FALSE;
3317 rollback_cbdata_t cb = { 0 };
3318 zfs_handle_t *zhp, *snap;
3319 char parentname[ZFS_MAXNAMELEN];
3323 while ((c = getopt(argc, argv, "rRf")) != -1) {
3336 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3345 /* check number of arguments */
3347 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3351 (void) fprintf(stderr, gettext("too many arguments\n"));
3355 /* open the snapshot */
3356 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3359 /* open the parent dataset */
3360 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3361 verify((delim = strrchr(parentname, '@')) != NULL);
3363 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3369 * Check for more recent snapshots and/or clones based on the presence
3372 cb.cb_target = argv[0];
3373 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3374 cb.cb_first = B_TRUE;
3376 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
3379 if ((ret = cb.cb_error) != 0)
3383 * Rollback parent to the given snapshot.
3385 ret = zfs_rollback(zhp, snap, force);
3398 * zfs set property=value { fs | snap | vol } ...
3400 * Sets the given property for all datasets specified on the command line.
3402 typedef struct set_cbdata {
3408 set_callback(zfs_handle_t *zhp, void *data)
3410 set_cbdata_t *cbp = data;
3412 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3413 switch (libzfs_errno(g_zfs)) {
3414 case EZFS_MOUNTFAILED:
3415 (void) fprintf(stderr, gettext("property may be set "
3416 "but unable to remount filesystem\n"));
3418 case EZFS_SHARENFSFAILED:
3419 (void) fprintf(stderr, gettext("property may be set "
3420 "but unable to reshare filesystem\n"));
3429 zfs_do_set(int argc, char **argv)
3434 /* check for options */
3435 if (argc > 1 && argv[1][0] == '-') {
3436 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3441 /* check number of arguments */
3443 (void) fprintf(stderr, gettext("missing property=value "
3448 (void) fprintf(stderr, gettext("missing dataset name\n"));
3452 /* validate property=value argument */
3453 cb.cb_propname = argv[1];
3454 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3455 (cb.cb_value[1] == '\0')) {
3456 (void) fprintf(stderr, gettext("missing value in "
3457 "property=value argument\n"));
3461 *cb.cb_value = '\0';
3464 if (*cb.cb_propname == '\0') {
3465 (void) fprintf(stderr,
3466 gettext("missing property in property=value argument\n"));
3470 ret = zfs_for_each(argc - 2, argv + 2, 0,
3471 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3477 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3479 * Creates a snapshot with the given name. While functionally equivalent to
3480 * 'zfs create', it is a separate command to differentiate intent.
3483 zfs_do_snapshot(int argc, char **argv)
3485 boolean_t recursive = B_FALSE;
3490 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3494 while ((c = getopt(argc, argv, "ro:")) != -1) {
3497 if (parseprop(props))
3504 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3513 /* check number of arguments */
3515 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3519 (void) fprintf(stderr, gettext("too many arguments\n"));
3523 ret = zfs_snapshot(g_zfs, argv[0], recursive, props);
3525 if (ret && recursive)
3526 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3536 * Send a backup stream to stdout.
3539 zfs_do_send(int argc, char **argv)
3541 char *fromname = NULL;
3542 char *toname = NULL;
3545 sendflags_t flags = { 0 };
3547 nvlist_t *dbgnv = NULL;
3548 boolean_t extraverbose = B_FALSE;
3551 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3562 flags.doall = B_TRUE;
3565 flags.replicate = B_TRUE;
3568 flags.props = B_TRUE;
3571 flags.parsable = B_TRUE;
3572 flags.verbose = B_TRUE;
3576 extraverbose = B_TRUE;
3577 flags.verbose = B_TRUE;
3580 flags.dedup = B_TRUE;
3583 flags.dryrun = B_TRUE;
3586 (void) fprintf(stderr, gettext("missing argument for "
3587 "'%c' option\n"), optopt);
3591 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3600 /* check number of arguments */
3602 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3606 (void) fprintf(stderr, gettext("too many arguments\n"));
3610 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3611 (void) fprintf(stderr,
3612 gettext("Error: Stream can not be written to a terminal.\n"
3613 "You must redirect standard output.\n"));
3617 cp = strchr(argv[0], '@');
3619 (void) fprintf(stderr,
3620 gettext("argument must be a snapshot\n"));
3625 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3630 * If they specified the full path to the snapshot, chop off
3631 * everything except the short name of the snapshot, but special
3632 * case if they specify the origin.
3634 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3635 char origin[ZFS_MAXNAMELEN];
3638 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3639 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3641 if (strcmp(origin, fromname) == 0) {
3643 flags.fromorigin = B_TRUE;
3646 if (cp != fromname && strcmp(argv[0], fromname)) {
3647 (void) fprintf(stderr,
3648 gettext("incremental source must be "
3649 "in same filesystem\n"));
3653 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3654 (void) fprintf(stderr,
3655 gettext("invalid incremental source\n"));
3661 if (flags.replicate && fromname == NULL)
3662 flags.doall = B_TRUE;
3664 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3665 extraverbose ? &dbgnv : NULL);
3667 if (extraverbose && dbgnv != NULL) {
3669 * dump_nvlist prints to stdout, but that's been
3670 * redirected to a file. Make it print to stderr
3673 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3674 dump_nvlist(dbgnv, 0);
3683 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3685 * Restore a backup stream from stdin.
3688 zfs_do_receive(int argc, char **argv)
3691 recvflags_t flags = { 0 };
3694 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3697 flags.isprefix = B_TRUE;
3700 flags.isprefix = B_TRUE;
3701 flags.istail = B_TRUE;
3704 flags.dryrun = B_TRUE;
3707 flags.nomount = B_TRUE;
3710 flags.verbose = B_TRUE;
3713 flags.force = B_TRUE;
3716 (void) fprintf(stderr, gettext("missing argument for "
3717 "'%c' option\n"), optopt);
3721 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3730 /* check number of arguments */
3732 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3736 (void) fprintf(stderr, gettext("too many arguments\n"));
3740 if (isatty(STDIN_FILENO)) {
3741 (void) fprintf(stderr,
3742 gettext("Error: Backup stream can not be read "
3743 "from a terminal.\n"
3744 "You must redirect standard input.\n"));
3748 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3754 * allow/unallow stuff
3756 /* copied from zfs/sys/dsl_deleg.h */
3757 #define ZFS_DELEG_PERM_CREATE "create"
3758 #define ZFS_DELEG_PERM_DESTROY "destroy"
3759 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3760 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3761 #define ZFS_DELEG_PERM_CLONE "clone"
3762 #define ZFS_DELEG_PERM_PROMOTE "promote"
3763 #define ZFS_DELEG_PERM_RENAME "rename"
3764 #define ZFS_DELEG_PERM_MOUNT "mount"
3765 #define ZFS_DELEG_PERM_SHARE "share"
3766 #define ZFS_DELEG_PERM_SEND "send"
3767 #define ZFS_DELEG_PERM_RECEIVE "receive"
3768 #define ZFS_DELEG_PERM_ALLOW "allow"
3769 #define ZFS_DELEG_PERM_USERPROP "userprop"
3770 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3771 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3772 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3773 #define ZFS_DELEG_PERM_USERUSED "userused"
3774 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3775 #define ZFS_DELEG_PERM_HOLD "hold"
3776 #define ZFS_DELEG_PERM_RELEASE "release"
3777 #define ZFS_DELEG_PERM_DIFF "diff"
3779 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3781 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3782 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3783 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3784 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3785 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3786 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3787 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3788 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3789 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3790 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3791 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3792 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3793 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3794 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3795 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3796 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3798 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3799 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3800 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3801 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3802 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3803 { NULL, ZFS_DELEG_NOTE_NONE }
3806 /* permission structure */
3807 typedef struct deleg_perm {
3808 zfs_deleg_who_type_t dp_who_type;
3809 const char *dp_name;
3811 boolean_t dp_descend;
3815 typedef struct deleg_perm_node {
3816 deleg_perm_t dpn_perm;
3818 uu_avl_node_t dpn_avl_node;
3819 } deleg_perm_node_t;
3821 typedef struct fs_perm fs_perm_t;
3823 /* permissions set */
3824 typedef struct who_perm {
3825 zfs_deleg_who_type_t who_type;
3826 const char *who_name; /* id */
3827 char who_ug_name[256]; /* user/group name */
3828 fs_perm_t *who_fsperm; /* uplink */
3830 uu_avl_t *who_deleg_perm_avl; /* permissions */
3834 typedef struct who_perm_node {
3835 who_perm_t who_perm;
3836 uu_avl_node_t who_avl_node;
3839 typedef struct fs_perm_set fs_perm_set_t;
3840 /* fs permissions */
3842 const char *fsp_name;
3844 uu_avl_t *fsp_sc_avl; /* sets,create */
3845 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3847 fs_perm_set_t *fsp_set; /* uplink */
3851 typedef struct fs_perm_node {
3852 fs_perm_t fspn_fsperm;
3855 uu_list_node_t fspn_list_node;
3858 /* top level structure */
3859 struct fs_perm_set {
3860 uu_list_pool_t *fsps_list_pool;
3861 uu_list_t *fsps_list; /* list of fs_perms */
3863 uu_avl_pool_t *fsps_named_set_avl_pool;
3864 uu_avl_pool_t *fsps_who_perm_avl_pool;
3865 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
3868 static inline const char *
3869 deleg_perm_type(zfs_deleg_note_t note)
3875 case ZFS_DELEG_NOTE_GROUPQUOTA:
3876 case ZFS_DELEG_NOTE_GROUPUSED:
3877 case ZFS_DELEG_NOTE_USERPROP:
3878 case ZFS_DELEG_NOTE_USERQUOTA:
3879 case ZFS_DELEG_NOTE_USERUSED:
3881 return (gettext("other"));
3883 return (gettext("subcommand"));
3888 who_type2weight(zfs_deleg_who_type_t who_type)
3892 case ZFS_DELEG_NAMED_SET_SETS:
3893 case ZFS_DELEG_NAMED_SET:
3896 case ZFS_DELEG_CREATE_SETS:
3897 case ZFS_DELEG_CREATE:
3900 case ZFS_DELEG_USER_SETS:
3901 case ZFS_DELEG_USER:
3904 case ZFS_DELEG_GROUP_SETS:
3905 case ZFS_DELEG_GROUP:
3908 case ZFS_DELEG_EVERYONE_SETS:
3909 case ZFS_DELEG_EVERYONE:
3921 who_perm_compare(const void *larg, const void *rarg, void *unused)
3923 const who_perm_node_t *l = larg;
3924 const who_perm_node_t *r = rarg;
3925 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
3926 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
3927 int lweight = who_type2weight(ltype);
3928 int rweight = who_type2weight(rtype);
3929 int res = lweight - rweight;
3931 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
3932 ZFS_MAX_DELEG_NAME-1);
3944 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
3946 const deleg_perm_node_t *l = larg;
3947 const deleg_perm_node_t *r = rarg;
3948 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
3949 ZFS_MAX_DELEG_NAME-1);
3961 fs_perm_set_init(fs_perm_set_t *fspset)
3963 bzero(fspset, sizeof (fs_perm_set_t));
3965 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
3966 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
3967 NULL, UU_DEFAULT)) == NULL)
3969 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
3970 UU_DEFAULT)) == NULL)
3973 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
3974 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
3975 who_perm_node_t, who_avl_node), who_perm_compare,
3976 UU_DEFAULT)) == NULL)
3979 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
3980 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
3981 who_perm_node_t, who_avl_node), who_perm_compare,
3982 UU_DEFAULT)) == NULL)
3985 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
3986 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
3987 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
3992 static inline void fs_perm_fini(fs_perm_t *);
3993 static inline void who_perm_fini(who_perm_t *);
3996 fs_perm_set_fini(fs_perm_set_t *fspset)
3998 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4000 while (node != NULL) {
4001 fs_perm_node_t *next_node =
4002 uu_list_next(fspset->fsps_list, node);
4003 fs_perm_t *fsperm = &node->fspn_fsperm;
4004 fs_perm_fini(fsperm);
4005 uu_list_remove(fspset->fsps_list, node);
4010 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4011 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4012 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4016 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4019 deleg_perm->dp_who_type = type;
4020 deleg_perm->dp_name = name;
4024 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4025 zfs_deleg_who_type_t type, const char *name)
4027 uu_avl_pool_t *pool;
4028 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4030 bzero(who_perm, sizeof (who_perm_t));
4032 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4033 UU_DEFAULT)) == NULL)
4036 who_perm->who_type = type;
4037 who_perm->who_name = name;
4038 who_perm->who_fsperm = fsperm;
4042 who_perm_fini(who_perm_t *who_perm)
4044 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4046 while (node != NULL) {
4047 deleg_perm_node_t *next_node =
4048 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4050 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4055 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4059 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4061 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4062 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4064 bzero(fsperm, sizeof (fs_perm_t));
4066 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4070 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4074 fsperm->fsp_set = fspset;
4075 fsperm->fsp_name = fsname;
4079 fs_perm_fini(fs_perm_t *fsperm)
4081 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4082 while (node != NULL) {
4083 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4085 who_perm_t *who_perm = &node->who_perm;
4086 who_perm_fini(who_perm);
4087 uu_avl_remove(fsperm->fsp_sc_avl, node);
4092 node = uu_avl_first(fsperm->fsp_uge_avl);
4093 while (node != NULL) {
4094 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4096 who_perm_t *who_perm = &node->who_perm;
4097 who_perm_fini(who_perm);
4098 uu_avl_remove(fsperm->fsp_uge_avl, node);
4103 uu_avl_destroy(fsperm->fsp_sc_avl);
4104 uu_avl_destroy(fsperm->fsp_uge_avl);
4108 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4109 zfs_deleg_who_type_t who_type, const char *name, char locality)
4111 uu_avl_index_t idx = 0;
4113 deleg_perm_node_t *found_node = NULL;
4114 deleg_perm_t *deleg_perm = &node->dpn_perm;
4116 deleg_perm_init(deleg_perm, who_type, name);
4118 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4120 uu_avl_insert(avl, node, idx);
4123 deleg_perm = &node->dpn_perm;
4128 case ZFS_DELEG_LOCAL:
4129 deleg_perm->dp_local = B_TRUE;
4131 case ZFS_DELEG_DESCENDENT:
4132 deleg_perm->dp_descend = B_TRUE;
4137 assert(B_FALSE); /* invalid locality */
4142 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4144 nvpair_t *nvp = NULL;
4145 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4146 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4147 zfs_deleg_who_type_t who_type = who_perm->who_type;
4149 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4150 const char *name = nvpair_name(nvp);
4151 data_type_t type = nvpair_type(nvp);
4152 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4153 deleg_perm_node_t *node =
4154 safe_malloc(sizeof (deleg_perm_node_t));
4156 assert(type == DATA_TYPE_BOOLEAN);
4158 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4159 set_deleg_perm_node(avl, node, who_type, name, locality);
4166 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4168 nvpair_t *nvp = NULL;
4169 fs_perm_set_t *fspset = fsperm->fsp_set;
4171 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4172 nvlist_t *nvl2 = NULL;
4173 const char *name = nvpair_name(nvp);
4174 uu_avl_t *avl = NULL;
4175 uu_avl_pool_t *avl_pool;
4176 zfs_deleg_who_type_t perm_type = name[0];
4177 char perm_locality = name[1];
4178 const char *perm_name = name + 3;
4179 boolean_t is_set = B_TRUE;
4180 who_perm_t *who_perm = NULL;
4182 assert('$' == name[2]);
4184 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4187 switch (perm_type) {
4188 case ZFS_DELEG_CREATE:
4189 case ZFS_DELEG_CREATE_SETS:
4190 case ZFS_DELEG_NAMED_SET:
4191 case ZFS_DELEG_NAMED_SET_SETS:
4192 avl_pool = fspset->fsps_named_set_avl_pool;
4193 avl = fsperm->fsp_sc_avl;
4195 case ZFS_DELEG_USER:
4196 case ZFS_DELEG_USER_SETS:
4197 case ZFS_DELEG_GROUP:
4198 case ZFS_DELEG_GROUP_SETS:
4199 case ZFS_DELEG_EVERYONE:
4200 case ZFS_DELEG_EVERYONE_SETS:
4201 avl_pool = fspset->fsps_who_perm_avl_pool;
4202 avl = fsperm->fsp_uge_avl;
4207 who_perm_node_t *found_node = NULL;
4208 who_perm_node_t *node = safe_malloc(
4209 sizeof (who_perm_node_t));
4210 who_perm = &node->who_perm;
4211 uu_avl_index_t idx = 0;
4213 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4214 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4216 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4218 if (avl == fsperm->fsp_uge_avl) {
4220 struct passwd *p = NULL;
4221 struct group *g = NULL;
4222 const char *nice_name = NULL;
4224 switch (perm_type) {
4225 case ZFS_DELEG_USER_SETS:
4226 case ZFS_DELEG_USER:
4227 rid = atoi(perm_name);
4230 nice_name = p->pw_name;
4232 case ZFS_DELEG_GROUP_SETS:
4233 case ZFS_DELEG_GROUP:
4234 rid = atoi(perm_name);
4237 nice_name = g->gr_name;
4241 if (nice_name != NULL)
4243 node->who_perm.who_ug_name,
4247 uu_avl_insert(avl, node, idx);
4250 who_perm = &node->who_perm;
4254 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4261 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4263 nvpair_t *nvp = NULL;
4264 uu_avl_index_t idx = 0;
4266 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4267 nvlist_t *nvl2 = NULL;
4268 const char *fsname = nvpair_name(nvp);
4269 data_type_t type = nvpair_type(nvp);
4270 fs_perm_t *fsperm = NULL;
4271 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4275 fsperm = &node->fspn_fsperm;
4277 assert(DATA_TYPE_NVLIST == type);
4279 uu_list_node_init(node, &node->fspn_list_node,
4280 fspset->fsps_list_pool);
4282 idx = uu_list_numnodes(fspset->fsps_list);
4283 fs_perm_init(fsperm, fspset, fsname);
4285 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4288 (void) parse_fs_perm(fsperm, nvl2);
4290 uu_list_insert(fspset->fsps_list, node, idx);
4296 static inline const char *
4297 deleg_perm_comment(zfs_deleg_note_t note)
4299 const char *str = "";
4304 case ZFS_DELEG_NOTE_ALLOW:
4305 str = gettext("Must also have the permission that is being"
4306 "\n\t\t\t\tallowed");
4308 case ZFS_DELEG_NOTE_CLONE:
4309 str = gettext("Must also have the 'create' ability and 'mount'"
4310 "\n\t\t\t\tability in the origin file system");
4312 case ZFS_DELEG_NOTE_CREATE:
4313 str = gettext("Must also have the 'mount' ability");
4315 case ZFS_DELEG_NOTE_DESTROY:
4316 str = gettext("Must also have the 'mount' ability");
4318 case ZFS_DELEG_NOTE_DIFF:
4319 str = gettext("Allows lookup of paths within a dataset;"
4320 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4321 "\n\t\t\t\tin order to use zfs diff");
4323 case ZFS_DELEG_NOTE_HOLD:
4324 str = gettext("Allows adding a user hold to a snapshot");
4326 case ZFS_DELEG_NOTE_MOUNT:
4327 str = gettext("Allows mount/umount of ZFS datasets");
4329 case ZFS_DELEG_NOTE_PROMOTE:
4330 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4331 " 'promote' ability in the origin file system");
4333 case ZFS_DELEG_NOTE_RECEIVE:
4334 str = gettext("Must also have the 'mount' and 'create'"
4337 case ZFS_DELEG_NOTE_RELEASE:
4338 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4339 "might destroy the snapshot");
4341 case ZFS_DELEG_NOTE_RENAME:
4342 str = gettext("Must also have the 'mount' and 'create'"
4343 "\n\t\t\t\tability in the new parent");
4345 case ZFS_DELEG_NOTE_ROLLBACK:
4348 case ZFS_DELEG_NOTE_SEND:
4351 case ZFS_DELEG_NOTE_SHARE:
4352 str = gettext("Allows sharing file systems over NFS or SMB"
4353 "\n\t\t\t\tprotocols");
4355 case ZFS_DELEG_NOTE_SNAPSHOT:
4359 * case ZFS_DELEG_NOTE_VSCAN:
4360 * str = gettext("");
4364 case ZFS_DELEG_NOTE_GROUPQUOTA:
4365 str = gettext("Allows accessing any groupquota@... property");
4367 case ZFS_DELEG_NOTE_GROUPUSED:
4368 str = gettext("Allows reading any groupused@... property");
4370 case ZFS_DELEG_NOTE_USERPROP:
4371 str = gettext("Allows changing any user property");
4373 case ZFS_DELEG_NOTE_USERQUOTA:
4374 str = gettext("Allows accessing any userquota@... property");
4376 case ZFS_DELEG_NOTE_USERUSED:
4377 str = gettext("Allows reading any userused@... property");
4395 boolean_t recursive; /* unallow only */
4396 boolean_t prt_usage;
4398 boolean_t prt_perms;
4401 const char *dataset;
4405 prop_cmp(const void *a, const void *b)
4407 const char *str1 = *(const char **)a;
4408 const char *str2 = *(const char **)b;
4409 return (strcmp(str1, str2));
4413 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4415 const char *opt_desc[] = {
4416 "-h", gettext("show this help message and exit"),
4417 "-l", gettext("set permission locally"),
4418 "-d", gettext("set permission for descents"),
4419 "-u", gettext("set permission for user"),
4420 "-g", gettext("set permission for group"),
4421 "-e", gettext("set permission for everyone"),
4422 "-c", gettext("set create time permission"),
4423 "-s", gettext("define permission set"),
4425 "-r", gettext("remove permissions recursively"),
4427 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4428 size_t allow_size = unallow_size - 2;
4429 const char *props[ZFS_NUM_PROPS];
4432 FILE *fp = requested ? stdout : stderr;
4433 zprop_desc_t *pdtbl = zfs_prop_get_table();
4434 const char *fmt = gettext("%-16s %-14s\t%s\n");
4436 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4438 (void) fprintf(fp, gettext("Options:\n"));
4439 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4440 const char *opt = opt_desc[i++];
4441 const char *optdsc = opt_desc[i];
4442 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4445 (void) fprintf(fp, gettext("\nThe following permissions are "
4447 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4449 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4450 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4451 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4452 const char *perm_type = deleg_perm_type(perm_note);
4453 const char *perm_comment = deleg_perm_comment(perm_note);
4454 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4457 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4458 zprop_desc_t *pd = &pdtbl[i];
4459 if (pd->pd_visible != B_TRUE)
4462 if (pd->pd_attr == PROP_READONLY)
4465 props[count++] = pd->pd_name;
4467 props[count] = NULL;
4469 qsort(props, count, sizeof (char *), prop_cmp);
4471 for (i = 0; i < count; i++)
4472 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4475 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4477 exit(requested ? 0 : 2);
4480 static inline const char *
4481 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4484 if (un && argc == expected_argc - 1)
4486 else if (argc == expected_argc)
4487 *permsp = argv[argc - 2];
4489 allow_usage(un, B_FALSE,
4490 gettext("wrong number of parameters\n"));
4492 return (argv[argc - 1]);
4496 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4498 int uge_sum = opts->user + opts->group + opts->everyone;
4499 int csuge_sum = opts->create + opts->set + uge_sum;
4500 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4501 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4504 allow_usage(un, B_FALSE,
4505 gettext("-u, -g, and -e are mutually exclusive\n"));
4507 if (opts->prt_usage)
4508 if (argc == 0 && all_sum == 0)
4509 allow_usage(un, B_TRUE, NULL);
4515 allow_usage(un, B_FALSE,
4516 gettext("invalid options combined with -s\n"));
4518 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4519 if (argv[0][0] != '@')
4520 allow_usage(un, B_FALSE,
4521 gettext("invalid set name: missing '@' prefix\n"));
4522 opts->who = argv[0];
4523 } else if (opts->create) {
4524 if (ldcsuge_sum > 1)
4525 allow_usage(un, B_FALSE,
4526 gettext("invalid options combined with -c\n"));
4527 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4528 } else if (opts->everyone) {
4530 allow_usage(un, B_FALSE,
4531 gettext("invalid options combined with -e\n"));
4532 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4533 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4535 opts->everyone = B_TRUE;
4538 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4539 } else if (argc == 1 && !un) {
4540 opts->prt_perms = B_TRUE;
4541 opts->dataset = argv[argc-1];
4543 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4544 opts->who = argv[0];
4547 if (!opts->local && !opts->descend) {
4548 opts->local = B_TRUE;
4549 opts->descend = B_TRUE;
4554 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4555 const char *who, char *perms, nvlist_t *top_nvl)
4558 char ld[2] = { '\0', '\0' };
4559 char who_buf[ZFS_MAXNAMELEN+32];
4562 nvlist_t *base_nvl = NULL;
4563 nvlist_t *set_nvl = NULL;
4566 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4568 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4572 case ZFS_DELEG_NAMED_SET_SETS:
4573 case ZFS_DELEG_NAMED_SET:
4574 set_type = ZFS_DELEG_NAMED_SET_SETS;
4575 base_type = ZFS_DELEG_NAMED_SET;
4576 ld[0] = ZFS_DELEG_NA;
4578 case ZFS_DELEG_CREATE_SETS:
4579 case ZFS_DELEG_CREATE:
4580 set_type = ZFS_DELEG_CREATE_SETS;
4581 base_type = ZFS_DELEG_CREATE;
4582 ld[0] = ZFS_DELEG_NA;
4584 case ZFS_DELEG_USER_SETS:
4585 case ZFS_DELEG_USER:
4586 set_type = ZFS_DELEG_USER_SETS;
4587 base_type = ZFS_DELEG_USER;
4589 ld[0] = ZFS_DELEG_LOCAL;
4591 ld[1] = ZFS_DELEG_DESCENDENT;
4593 case ZFS_DELEG_GROUP_SETS:
4594 case ZFS_DELEG_GROUP:
4595 set_type = ZFS_DELEG_GROUP_SETS;
4596 base_type = ZFS_DELEG_GROUP;
4598 ld[0] = ZFS_DELEG_LOCAL;
4600 ld[1] = ZFS_DELEG_DESCENDENT;
4602 case ZFS_DELEG_EVERYONE_SETS:
4603 case ZFS_DELEG_EVERYONE:
4604 set_type = ZFS_DELEG_EVERYONE_SETS;
4605 base_type = ZFS_DELEG_EVERYONE;
4607 ld[0] = ZFS_DELEG_LOCAL;
4609 ld[1] = ZFS_DELEG_DESCENDENT;
4612 if (perms != NULL) {
4614 char *end = curr + strlen(perms);
4616 while (curr < end) {
4617 char *delim = strchr(curr, ',');
4628 (void) nvlist_add_boolean(nvl, curr);
4634 for (i = 0; i < 2; i++) {
4635 char locality = ld[i];
4639 if (!nvlist_empty(base_nvl)) {
4641 (void) snprintf(who_buf,
4642 sizeof (who_buf), "%c%c$%s",
4643 base_type, locality, who);
4645 (void) snprintf(who_buf,
4646 sizeof (who_buf), "%c%c$",
4647 base_type, locality);
4649 (void) nvlist_add_nvlist(top_nvl, who_buf,
4654 if (!nvlist_empty(set_nvl)) {
4656 (void) snprintf(who_buf,
4657 sizeof (who_buf), "%c%c$%s",
4658 set_type, locality, who);
4660 (void) snprintf(who_buf,
4661 sizeof (who_buf), "%c%c$",
4662 set_type, locality);
4664 (void) nvlist_add_nvlist(top_nvl, who_buf,
4669 for (i = 0; i < 2; i++) {
4670 char locality = ld[i];
4675 (void) snprintf(who_buf, sizeof (who_buf),
4676 "%c%c$%s", base_type, locality, who);
4678 (void) snprintf(who_buf, sizeof (who_buf),
4679 "%c%c$", base_type, locality);
4680 (void) nvlist_add_boolean(top_nvl, who_buf);
4683 (void) snprintf(who_buf, sizeof (who_buf),
4684 "%c%c$%s", set_type, locality, who);
4686 (void) snprintf(who_buf, sizeof (who_buf),
4687 "%c%c$", set_type, locality);
4688 (void) nvlist_add_boolean(top_nvl, who_buf);
4694 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4696 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4700 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4701 opts->descend, opts->who, opts->perms, *nvlp);
4702 } else if (opts->create) {
4703 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4704 opts->descend, NULL, opts->perms, *nvlp);
4705 } else if (opts->everyone) {
4706 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4707 opts->descend, NULL, opts->perms, *nvlp);
4709 char *curr = opts->who;
4710 char *end = curr + strlen(curr);
4712 while (curr < end) {
4714 zfs_deleg_who_type_t who_type;
4716 char *delim = strchr(curr, ',');
4719 struct passwd *p = NULL;
4720 struct group *g = NULL;
4728 rid = (uid_t)strtol(curr, &endch, 0);
4730 who_type = ZFS_DELEG_USER;
4739 (void) snprintf(errbuf, 256, gettext(
4740 "invalid user %s"), curr);
4741 allow_usage(un, B_TRUE, errbuf);
4743 } else if (opts->group) {
4744 who_type = ZFS_DELEG_GROUP;
4753 (void) snprintf(errbuf, 256, gettext(
4754 "invalid group %s"), curr);
4755 allow_usage(un, B_TRUE, errbuf);
4758 if (*endch != '\0') {
4765 if (*endch != '\0') {
4772 who_type = ZFS_DELEG_USER;
4774 } else if (g != NULL) {
4775 who_type = ZFS_DELEG_GROUP;
4778 (void) snprintf(errbuf, 256, gettext(
4779 "invalid user/group %s"), curr);
4780 allow_usage(un, B_TRUE, errbuf);
4784 (void) sprintf(id, "%u", rid);
4787 store_allow_perm(who_type, opts->local,
4788 opts->descend, who, opts->perms, *nvlp);
4797 print_set_creat_perms(uu_avl_t *who_avl)
4799 const char *sc_title[] = {
4800 gettext("Permission sets:\n"),
4801 gettext("Create time permissions:\n"),
4804 const char **title_ptr = sc_title;
4805 who_perm_node_t *who_node = NULL;
4806 int prev_weight = -1;
4808 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4809 who_node = uu_avl_next(who_avl, who_node)) {
4810 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4811 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4812 const char *who_name = who_node->who_perm.who_name;
4813 int weight = who_type2weight(who_type);
4814 boolean_t first = B_TRUE;
4815 deleg_perm_node_t *deleg_node;
4817 if (prev_weight != weight) {
4818 (void) printf(*title_ptr++);
4819 prev_weight = weight;
4822 if (who_name == NULL || strnlen(who_name, 1) == 0)
4823 (void) printf("\t");
4825 (void) printf("\t%s ", who_name);
4827 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4828 deleg_node = uu_avl_next(avl, deleg_node)) {
4831 deleg_node->dpn_perm.dp_name);
4834 (void) printf(",%s",
4835 deleg_node->dpn_perm.dp_name);
4838 (void) printf("\n");
4843 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4846 who_perm_node_t *who_node = NULL;
4847 boolean_t prt_title = B_TRUE;
4848 uu_avl_walk_t *walk;
4850 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4853 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4854 const char *who_name = who_node->who_perm.who_name;
4855 const char *nice_who_name = who_node->who_perm.who_ug_name;
4856 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4857 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4859 deleg_perm_node_t *deleg_node;
4860 boolean_t prt_who = B_TRUE;
4862 for (deleg_node = uu_avl_first(avl);
4864 deleg_node = uu_avl_next(avl, deleg_node)) {
4865 if (local != deleg_node->dpn_perm.dp_local ||
4866 descend != deleg_node->dpn_perm.dp_descend)
4870 const char *who = NULL;
4872 prt_title = B_FALSE;
4873 (void) printf(title);
4877 case ZFS_DELEG_USER_SETS:
4878 case ZFS_DELEG_USER:
4879 who = gettext("user");
4881 who_name = nice_who_name;
4883 case ZFS_DELEG_GROUP_SETS:
4884 case ZFS_DELEG_GROUP:
4885 who = gettext("group");
4887 who_name = nice_who_name;
4889 case ZFS_DELEG_EVERYONE_SETS:
4890 case ZFS_DELEG_EVERYONE:
4891 who = gettext("everyone");
4896 if (who_name == NULL)
4897 (void) printf("\t%s", who);
4899 (void) printf("\t%s %s", who, who_name);
4902 (void) printf("%c%s", delim,
4903 deleg_node->dpn_perm.dp_name);
4908 (void) printf("\n");
4911 uu_avl_walk_end(walk);
4915 print_fs_perms(fs_perm_set_t *fspset)
4917 fs_perm_node_t *node = NULL;
4918 char buf[ZFS_MAXNAMELEN+32];
4919 const char *dsname = buf;
4921 for (node = uu_list_first(fspset->fsps_list); node != NULL;
4922 node = uu_list_next(fspset->fsps_list, node)) {
4923 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
4924 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
4927 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
4928 gettext("---- Permissions on %s "),
4929 node->fspn_fsperm.fsp_name);
4930 (void) printf(dsname);
4931 left = 70 - strlen(buf);
4934 (void) printf("\n");
4936 print_set_creat_perms(sc_avl);
4937 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
4938 gettext("Local permissions:\n"));
4939 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
4940 gettext("Descendent permissions:\n"));
4941 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
4942 gettext("Local+Descendent permissions:\n"));
4946 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
4948 struct deleg_perms {
4954 set_deleg_perms(zfs_handle_t *zhp, void *data)
4956 struct deleg_perms *perms = (struct deleg_perms *)data;
4957 zfs_type_t zfs_type = zfs_get_type(zhp);
4959 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
4962 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
4966 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
4969 nvlist_t *perm_nvl = NULL;
4970 nvlist_t *update_perm_nvl = NULL;
4973 struct allow_opts opts = { 0 };
4975 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
4978 while ((c = getopt(argc, argv, optstr)) != -1) {
4981 opts.local = B_TRUE;
4984 opts.descend = B_TRUE;
4990 opts.group = B_TRUE;
4993 opts.everyone = B_TRUE;
4999 opts.create = B_TRUE;
5002 opts.recursive = B_TRUE;
5005 (void) fprintf(stderr, gettext("missing argument for "
5006 "'%c' option\n"), optopt);
5010 opts.prt_usage = B_TRUE;
5013 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5022 /* check arguments */
5023 parse_allow_args(argc, argv, un, &opts);
5025 /* try to open the dataset */
5026 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5027 ZFS_TYPE_VOLUME)) == NULL) {
5028 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5033 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5036 fs_perm_set_init(&fs_perm_set);
5037 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5038 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5043 print_fs_perms(&fs_perm_set);
5045 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5046 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5049 if (un && opts.recursive) {
5050 struct deleg_perms data = { un, update_perm_nvl };
5051 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5060 nvlist_free(perm_nvl);
5061 if (update_perm_nvl != NULL)
5062 nvlist_free(update_perm_nvl);
5064 fs_perm_set_fini(&fs_perm_set);
5072 * zfs allow [-r] [-t] <tag> <snap> ...
5074 * -r Recursively hold
5075 * -t Temporary hold (hidden option)
5077 * Apply a user-hold with the given tag to the list of snapshots.
5080 zfs_do_allow(int argc, char **argv)
5082 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5086 * zfs unallow [-r] [-t] <tag> <snap> ...
5088 * -r Recursively hold
5089 * -t Temporary hold (hidden option)
5091 * Apply a user-hold with the given tag to the list of snapshots.
5094 zfs_do_unallow(int argc, char **argv)
5096 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5100 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5105 boolean_t recursive = B_FALSE;
5106 boolean_t temphold = B_FALSE;
5107 const char *opts = holding ? "rt" : "r";
5111 while ((c = getopt(argc, argv, opts)) != -1) {
5120 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5129 /* check number of arguments */
5137 if (holding && tag[0] == '.') {
5138 /* tags starting with '.' are reserved for libzfs */
5139 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5143 for (i = 0; i < argc; ++i) {
5145 char parent[ZFS_MAXNAMELEN];
5147 char *path = argv[i];
5149 delim = strchr(path, '@');
5150 if (delim == NULL) {
5151 (void) fprintf(stderr,
5152 gettext("'%s' is not a snapshot\n"), path);
5156 (void) strncpy(parent, path, delim - path);
5157 parent[delim - path] = '\0';
5159 zhp = zfs_open(g_zfs, parent,
5160 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5166 if (zfs_hold(zhp, delim+1, tag, recursive,
5167 temphold, B_FALSE, -1, 0, 0) != 0)
5170 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5176 return (errors != 0);
5180 * zfs hold [-r] [-t] <tag> <snap> ...
5182 * -r Recursively hold
5183 * -t Temporary hold (hidden option)
5185 * Apply a user-hold with the given tag to the list of snapshots.
5188 zfs_do_hold(int argc, char **argv)
5190 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5194 * zfs release [-r] <tag> <snap> ...
5196 * -r Recursively release
5198 * Release a user-hold with the given tag from the list of snapshots.
5201 zfs_do_release(int argc, char **argv)
5203 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5206 typedef struct holds_cbdata {
5207 boolean_t cb_recursive;
5208 const char *cb_snapname;
5210 size_t cb_max_namelen;
5211 size_t cb_max_taglen;
5214 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5215 #define DATETIME_BUF_LEN (32)
5220 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5223 nvpair_t *nvp = NULL;
5224 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5228 for (i = 0; i < 3; i++) {
5229 col = gettext(hdr_cols[i]);
5231 (void) printf("%-*s ", i ? tagwidth : nwidth,
5234 (void) printf("%s\n", col);
5238 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5239 char *zname = nvpair_name(nvp);
5241 nvpair_t *nvp2 = NULL;
5242 (void) nvpair_value_nvlist(nvp, &nvl2);
5243 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5244 char tsbuf[DATETIME_BUF_LEN];
5245 char *tagname = nvpair_name(nvp2);
5249 char sep = scripted ? '\t' : ' ';
5250 size_t sepnum = scripted ? 1 : 2;
5252 (void) nvpair_value_uint64(nvp2, &val);
5254 (void) localtime_r(&time, &t);
5255 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5256 gettext(STRFTIME_FMT_STR), &t);
5258 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5259 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5265 * Generic callback function to list a dataset or snapshot.
5268 holds_callback(zfs_handle_t *zhp, void *data)
5270 holds_cbdata_t *cbp = data;
5271 nvlist_t *top_nvl = *cbp->cb_nvlp;
5272 nvlist_t *nvl = NULL;
5273 nvpair_t *nvp = NULL;
5274 const char *zname = zfs_get_name(zhp);
5275 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5277 if (cbp->cb_recursive) {
5278 const char *snapname;
5279 char *delim = strchr(zname, '@');
5283 snapname = delim + 1;
5284 if (strcmp(cbp->cb_snapname, snapname))
5288 if (zfs_get_holds(zhp, &nvl) != 0)
5291 if (znamelen > cbp->cb_max_namelen)
5292 cbp->cb_max_namelen = znamelen;
5294 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5295 const char *tag = nvpair_name(nvp);
5296 size_t taglen = strnlen(tag, MAXNAMELEN);
5297 if (taglen > cbp->cb_max_taglen)
5298 cbp->cb_max_taglen = taglen;
5301 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5305 * zfs holds [-r] <snap> ...
5307 * -r Recursively hold
5310 zfs_do_holds(int argc, char **argv)
5315 boolean_t scripted = B_FALSE;
5316 boolean_t recursive = B_FALSE;
5317 const char *opts = "rH";
5320 int types = ZFS_TYPE_SNAPSHOT;
5321 holds_cbdata_t cb = { 0 };
5328 while ((c = getopt(argc, argv, opts)) != -1) {
5337 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5344 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5345 flags |= ZFS_ITER_RECURSE;
5351 /* check number of arguments */
5355 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5358 for (i = 0; i < argc; ++i) {
5359 char *snapshot = argv[i];
5361 const char *snapname;
5363 delim = strchr(snapshot, '@');
5364 if (delim == NULL) {
5365 (void) fprintf(stderr,
5366 gettext("'%s' is not a snapshot\n"), snapshot);
5370 snapname = delim + 1;
5372 snapshot[delim - snapshot] = '\0';
5374 cb.cb_recursive = recursive;
5375 cb.cb_snapname = snapname;
5379 * 1. collect holds data, set format options
5381 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5382 holds_callback, &cb);
5388 * 2. print holds data
5390 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5392 if (nvlist_empty(nvl))
5393 (void) printf(gettext("no datasets available\n"));
5397 return (0 != errors);
5400 #define CHECK_SPINNER 30
5401 #define SPINNER_TIME 3 /* seconds */
5402 #define MOUNT_TIME 5 /* seconds */
5405 get_one_dataset(zfs_handle_t *zhp, void *data)
5407 static char *spin[] = { "-", "\\", "|", "/" };
5408 static int spinval = 0;
5409 static int spincheck = 0;
5410 static time_t last_spin_time = (time_t)0;
5411 get_all_cb_t *cbp = data;
5412 zfs_type_t type = zfs_get_type(zhp);
5414 if (cbp->cb_verbose) {
5415 if (--spincheck < 0) {
5416 time_t now = time(NULL);
5417 if (last_spin_time + SPINNER_TIME < now) {
5418 update_progress(spin[spinval++ % 4]);
5419 last_spin_time = now;
5421 spincheck = CHECK_SPINNER;
5426 * Interate over any nested datasets.
5428 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5434 * Skip any datasets whose type does not match.
5436 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5440 libzfs_add_handle(cbp, zhp);
5441 assert(cbp->cb_used <= cbp->cb_alloc);
5447 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5449 get_all_cb_t cb = { 0 };
5450 cb.cb_verbose = verbose;
5451 cb.cb_getone = get_one_dataset;
5454 set_progress_header(gettext("Reading ZFS config"));
5455 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5457 *dslist = cb.cb_handles;
5458 *count = cb.cb_used;
5461 finish_progress(gettext("done."));
5465 * Generic callback for sharing or mounting filesystems. Because the code is so
5466 * similar, we have a common function with an extra parameter to determine which
5467 * mode we are using.
5469 #define OP_SHARE 0x1
5470 #define OP_MOUNT 0x2
5473 * Share or mount a dataset.
5476 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5477 boolean_t explicit, const char *options)
5479 char mountpoint[ZFS_MAXPROPLEN];
5480 char shareopts[ZFS_MAXPROPLEN];
5481 char smbshareopts[ZFS_MAXPROPLEN];
5482 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5484 uint64_t zoned, canmount;
5485 boolean_t shared_nfs, shared_smb;
5487 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5490 * Check to make sure we can mount/share this dataset. If we
5491 * are in the global zone and the filesystem is exported to a
5492 * local zone, or if we are in a local zone and the
5493 * filesystem is not exported, then it is an error.
5495 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5497 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5501 (void) fprintf(stderr, gettext("cannot %s '%s': "
5502 "dataset is exported to a local zone\n"), cmdname,
5506 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5510 (void) fprintf(stderr, gettext("cannot %s '%s': "
5511 "permission denied\n"), cmdname,
5517 * Ignore any filesystems which don't apply to us. This
5518 * includes those with a legacy mountpoint, or those with
5519 * legacy share options.
5521 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5522 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5523 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5524 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5525 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5526 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5528 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5529 strcmp(smbshareopts, "off") == 0) {
5533 (void) fprintf(stderr, gettext("cannot share '%s': "
5534 "legacy share\n"), zfs_get_name(zhp));
5535 (void) fprintf(stderr, gettext("use share(1M) to "
5536 "share this filesystem, or set "
5537 "sharenfs property on\n"));
5542 * We cannot share or mount legacy filesystems. If the
5543 * shareopts is non-legacy but the mountpoint is legacy, we
5544 * treat it as a legacy share.
5546 if (strcmp(mountpoint, "legacy") == 0) {
5550 (void) fprintf(stderr, gettext("cannot %s '%s': "
5551 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5552 (void) fprintf(stderr, gettext("use %s(1M) to "
5553 "%s this filesystem\n"), cmdname, cmdname);
5557 if (strcmp(mountpoint, "none") == 0) {
5561 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5562 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5567 * canmount explicit outcome
5568 * on no pass through
5569 * on yes pass through
5571 * off yes display error, return 1
5572 * noauto no return 0
5573 * noauto yes pass through
5575 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5576 if (canmount == ZFS_CANMOUNT_OFF) {
5580 (void) fprintf(stderr, gettext("cannot %s '%s': "
5581 "'canmount' property is set to 'off'\n"), cmdname,
5584 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5589 * At this point, we have verified that the mountpoint and/or
5590 * shareopts are appropriate for auto management. If the
5591 * filesystem is already mounted or shared, return (failing
5592 * for explicit requests); otherwise mount or share the
5598 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5599 shared_smb = zfs_is_shared_smb(zhp, NULL);
5601 if (shared_nfs && shared_smb ||
5602 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5603 strcmp(smbshareopts, "off") == 0) ||
5604 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5605 strcmp(shareopts, "off") == 0)) {
5609 (void) fprintf(stderr, gettext("cannot share "
5610 "'%s': filesystem already shared\n"),
5615 if (!zfs_is_mounted(zhp, NULL) &&
5616 zfs_mount(zhp, NULL, 0) != 0)
5619 if (protocol == NULL) {
5620 if (zfs_shareall(zhp) != 0)
5622 } else if (strcmp(protocol, "nfs") == 0) {
5623 if (zfs_share_nfs(zhp))
5625 } else if (strcmp(protocol, "smb") == 0) {
5626 if (zfs_share_smb(zhp))
5629 (void) fprintf(stderr, gettext("cannot share "
5630 "'%s': invalid share type '%s' "
5632 zfs_get_name(zhp), protocol);
5639 if (options == NULL)
5640 mnt.mnt_mntopts = "";
5642 mnt.mnt_mntopts = (char *)options;
5644 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5645 zfs_is_mounted(zhp, NULL)) {
5649 (void) fprintf(stderr, gettext("cannot mount "
5650 "'%s': filesystem already mounted\n"),
5655 if (zfs_mount(zhp, options, flags) != 0)
5664 * Reports progress in the form "(current/total)". Not thread-safe.
5667 report_mount_progress(int current, int total)
5669 static time_t last_progress_time = 0;
5670 time_t now = time(NULL);
5673 /* report 1..n instead of 0..n-1 */
5676 /* display header if we're here for the first time */
5678 set_progress_header(gettext("Mounting ZFS filesystems"));
5679 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5680 /* too soon to report again */
5684 last_progress_time = now;
5686 (void) sprintf(info, "(%d/%d)", current, total);
5688 if (current == total)
5689 finish_progress(info);
5691 update_progress(info);
5695 append_options(char *mntopts, char *newopts)
5697 int len = strlen(mntopts);
5699 /* original length plus new string to append plus 1 for the comma */
5700 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5701 (void) fprintf(stderr, gettext("the opts argument for "
5702 "'%c' option is too long (more than %d chars)\n"),
5703 "-o", MNT_LINE_MAX);
5708 mntopts[len++] = ',';
5710 (void) strcpy(&mntopts[len], newopts);
5714 share_mount(int op, int argc, char **argv)
5717 boolean_t verbose = B_FALSE;
5719 char *options = NULL;
5723 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5733 if (*optarg == '\0') {
5734 (void) fprintf(stderr, gettext("empty mount "
5735 "options (-o) specified\n"));
5739 if (options == NULL)
5740 options = safe_malloc(MNT_LINE_MAX + 1);
5742 /* option validation is done later */
5743 append_options(options, optarg);
5747 warnx("no overlay mounts support on FreeBSD, ignoring");
5750 (void) fprintf(stderr, gettext("missing argument for "
5751 "'%c' option\n"), optopt);
5755 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5764 /* check number of arguments */
5766 zfs_handle_t **dslist = NULL;
5767 size_t i, count = 0;
5768 char *protocol = NULL;
5770 if (op == OP_SHARE && argc > 0) {
5771 if (strcmp(argv[0], "nfs") != 0 &&
5772 strcmp(argv[0], "smb") != 0) {
5773 (void) fprintf(stderr, gettext("share type "
5774 "must be 'nfs' or 'smb'\n"));
5783 (void) fprintf(stderr, gettext("too many arguments\n"));
5787 start_progress_timer();
5788 get_all_datasets(&dslist, &count, verbose);
5793 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5795 for (i = 0; i < count; i++) {
5797 report_mount_progress(i, count);
5799 if (share_mount_one(dslist[i], op, flags, protocol,
5800 B_FALSE, options) != 0)
5802 zfs_close(dslist[i]);
5806 } else if (argc == 0) {
5807 struct mnttab entry;
5809 if ((op == OP_SHARE) || (options != NULL)) {
5810 (void) fprintf(stderr, gettext("missing filesystem "
5811 "argument (specify -a for all)\n"));
5816 * When mount is given no arguments, go through /etc/mnttab and
5817 * display any active ZFS mounts. We hide any snapshots, since
5818 * they are controlled automatically.
5820 rewind(mnttab_file);
5821 while (getmntent(mnttab_file, &entry) == 0) {
5822 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5823 strchr(entry.mnt_special, '@') != NULL)
5826 (void) printf("%-30s %s\n", entry.mnt_special,
5834 (void) fprintf(stderr,
5835 gettext("too many arguments\n"));
5839 if ((zhp = zfs_open(g_zfs, argv[0],
5840 ZFS_TYPE_FILESYSTEM)) == NULL) {
5843 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5853 * zfs mount -a [nfs]
5854 * zfs mount filesystem
5856 * Mount all filesystems, or mount the given filesystem.
5859 zfs_do_mount(int argc, char **argv)
5861 return (share_mount(OP_MOUNT, argc, argv));
5865 * zfs share -a [nfs | smb]
5866 * zfs share filesystem
5868 * Share all filesystems, or share the given filesystem.
5871 zfs_do_share(int argc, char **argv)
5873 return (share_mount(OP_SHARE, argc, argv));
5876 typedef struct unshare_unmount_node {
5877 zfs_handle_t *un_zhp;
5879 uu_avl_node_t un_avlnode;
5880 } unshare_unmount_node_t;
5884 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
5886 const unshare_unmount_node_t *l = larg;
5887 const unshare_unmount_node_t *r = rarg;
5889 return (strcmp(l->un_mountp, r->un_mountp));
5893 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5894 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
5895 * and unmount it appropriately.
5898 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
5902 struct stat64 statbuf;
5903 struct extmnttab entry;
5904 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5908 * Search for the path in /etc/mnttab. Rather than looking for the
5909 * specific path, which can be fooled by non-standard paths (i.e. ".."
5910 * or "//"), we stat() the path and search for the corresponding
5911 * (major,minor) device pair.
5913 if (stat64(path, &statbuf) != 0) {
5914 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5915 cmdname, path, strerror(errno));
5918 path_inode = statbuf.st_ino;
5921 * Search for the given (major,minor) pair in the mount table.
5924 rewind(mnttab_file);
5925 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
5926 if (entry.mnt_major == major(statbuf.st_dev) &&
5927 entry.mnt_minor == minor(statbuf.st_dev))
5934 if (statfs(path, &sfs) != 0) {
5935 (void) fprintf(stderr, "%s: %s\n", path,
5939 statfs2mnttab(&sfs, &entry);
5943 if (op == OP_SHARE) {
5944 (void) fprintf(stderr, gettext("cannot %s '%s': not "
5945 "currently mounted\n"), cmdname, path);
5948 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
5950 if ((ret = umount2(path, flags)) != 0)
5951 (void) fprintf(stderr, gettext("%s: %s\n"), path,
5956 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
5957 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
5958 "filesystem\n"), cmdname, path);
5962 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
5963 ZFS_TYPE_FILESYSTEM)) == NULL)
5967 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
5968 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5969 cmdname, path, strerror(errno));
5971 } else if (statbuf.st_ino != path_inode) {
5972 (void) fprintf(stderr, gettext("cannot "
5973 "%s '%s': not a mountpoint\n"), cmdname, path);
5977 if (op == OP_SHARE) {
5978 char nfs_mnt_prop[ZFS_MAXPROPLEN];
5979 char smbshare_prop[ZFS_MAXPROPLEN];
5981 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
5982 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
5983 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
5984 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
5986 if (strcmp(nfs_mnt_prop, "off") == 0 &&
5987 strcmp(smbshare_prop, "off") == 0) {
5988 (void) fprintf(stderr, gettext("cannot unshare "
5989 "'%s': legacy share\n"), path);
5990 (void) fprintf(stderr, gettext("use "
5991 "unshare(1M) to unshare this filesystem\n"));
5992 } else if (!zfs_is_shared(zhp)) {
5993 (void) fprintf(stderr, gettext("cannot unshare '%s': "
5994 "not currently shared\n"), path);
5996 ret = zfs_unshareall_bypath(zhp, path);
5999 char mtpt_prop[ZFS_MAXPROPLEN];
6001 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6002 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6005 ret = zfs_unmount(zhp, NULL, flags);
6006 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6007 (void) fprintf(stderr, gettext("cannot unmount "
6008 "'%s': legacy mountpoint\n"),
6010 (void) fprintf(stderr, gettext("use umount(1M) "
6011 "to unmount this filesystem\n"));
6013 ret = zfs_unmountall(zhp, flags);
6024 * Generic callback for unsharing or unmounting a filesystem.
6027 unshare_unmount(int op, int argc, char **argv)
6034 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6035 char sharesmb[ZFS_MAXPROPLEN];
6038 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6047 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6058 * We could make use of zfs_for_each() to walk all datasets in
6059 * the system, but this would be very inefficient, especially
6060 * since we would have to linearly search /etc/mnttab for each
6061 * one. Instead, do one pass through /etc/mnttab looking for
6062 * zfs entries and call zfs_unmount() for each one.
6064 * Things get a little tricky if the administrator has created
6065 * mountpoints beneath other ZFS filesystems. In this case, we
6066 * have to unmount the deepest filesystems first. To accomplish
6067 * this, we place all the mountpoints in an AVL tree sorted by
6068 * the special type (dataset name), and walk the result in
6069 * reverse to make sure to get any snapshots first.
6071 struct mnttab entry;
6072 uu_avl_pool_t *pool;
6074 unshare_unmount_node_t *node;
6076 uu_avl_walk_t *walk;
6079 (void) fprintf(stderr, gettext("too many arguments\n"));
6083 if (((pool = uu_avl_pool_create("unmount_pool",
6084 sizeof (unshare_unmount_node_t),
6085 offsetof(unshare_unmount_node_t, un_avlnode),
6086 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6087 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6090 rewind(mnttab_file);
6091 while (getmntent(mnttab_file, &entry) == 0) {
6093 /* ignore non-ZFS entries */
6094 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6097 /* ignore snapshots */
6098 if (strchr(entry.mnt_special, '@') != NULL)
6101 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6102 ZFS_TYPE_FILESYSTEM)) == NULL) {
6109 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6111 sizeof (nfs_mnt_prop),
6112 NULL, NULL, 0, B_FALSE) == 0);
6113 if (strcmp(nfs_mnt_prop, "off") != 0)
6115 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6117 sizeof (nfs_mnt_prop),
6118 NULL, NULL, 0, B_FALSE) == 0);
6119 if (strcmp(nfs_mnt_prop, "off") == 0)
6123 /* Ignore legacy mounts */
6124 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6126 sizeof (nfs_mnt_prop),
6127 NULL, NULL, 0, B_FALSE) == 0);
6128 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6130 /* Ignore canmount=noauto mounts */
6131 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6132 ZFS_CANMOUNT_NOAUTO)
6138 node = safe_malloc(sizeof (unshare_unmount_node_t));
6140 node->un_mountp = safe_strdup(entry.mnt_mountp);
6142 uu_avl_node_init(node, &node->un_avlnode, pool);
6144 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6145 uu_avl_insert(tree, node, idx);
6147 zfs_close(node->un_zhp);
6148 free(node->un_mountp);
6154 * Walk the AVL tree in reverse, unmounting each filesystem and
6155 * removing it from the AVL tree in the process.
6157 if ((walk = uu_avl_walk_start(tree,
6158 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6161 while ((node = uu_avl_walk_next(walk)) != NULL) {
6162 uu_avl_remove(tree, node);
6166 if (zfs_unshareall_bypath(node->un_zhp,
6167 node->un_mountp) != 0)
6172 if (zfs_unmount(node->un_zhp,
6173 node->un_mountp, flags) != 0)
6178 zfs_close(node->un_zhp);
6179 free(node->un_mountp);
6183 uu_avl_walk_end(walk);
6184 uu_avl_destroy(tree);
6185 uu_avl_pool_destroy(pool);
6190 (void) fprintf(stderr,
6191 gettext("missing filesystem argument\n"));
6193 (void) fprintf(stderr,
6194 gettext("too many arguments\n"));
6199 * We have an argument, but it may be a full path or a ZFS
6200 * filesystem. Pass full paths off to unmount_path() (shared by
6201 * manual_unmount), otherwise open the filesystem and pass to
6204 if (argv[0][0] == '/')
6205 return (unshare_unmount_path(op, argv[0],
6208 if ((zhp = zfs_open(g_zfs, argv[0],
6209 ZFS_TYPE_FILESYSTEM)) == NULL)
6212 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6213 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6214 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6215 NULL, 0, B_FALSE) == 0);
6219 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6221 sizeof (nfs_mnt_prop),
6222 NULL, NULL, 0, B_FALSE) == 0);
6223 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6224 sharesmb, sizeof (sharesmb), NULL, NULL,
6227 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6228 strcmp(sharesmb, "off") == 0) {
6229 (void) fprintf(stderr, gettext("cannot "
6230 "unshare '%s': legacy share\n"),
6232 (void) fprintf(stderr, gettext("use "
6233 "unshare(1M) to unshare this "
6236 } else if (!zfs_is_shared(zhp)) {
6237 (void) fprintf(stderr, gettext("cannot "
6238 "unshare '%s': not currently "
6239 "shared\n"), zfs_get_name(zhp));
6241 } else if (zfs_unshareall(zhp) != 0) {
6247 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6248 (void) fprintf(stderr, gettext("cannot "
6249 "unmount '%s': legacy "
6250 "mountpoint\n"), zfs_get_name(zhp));
6251 (void) fprintf(stderr, gettext("use "
6252 "umount(1M) to unmount this "
6255 } else if (!zfs_is_mounted(zhp, NULL)) {
6256 (void) fprintf(stderr, gettext("cannot "
6257 "unmount '%s': not currently "
6261 } else if (zfs_unmountall(zhp, flags) != 0) {
6275 * zfs unmount filesystem
6277 * Unmount all filesystems, or a specific ZFS filesystem.
6280 zfs_do_unmount(int argc, char **argv)
6282 return (unshare_unmount(OP_MOUNT, argc, argv));
6287 * zfs unshare filesystem
6289 * Unshare all filesystems, or a specific ZFS filesystem.
6292 zfs_do_unshare(int argc, char **argv)
6294 return (unshare_unmount(OP_SHARE, argc, argv));
6298 * Attach/detach the given dataset to/from the given jail
6302 do_jail(int argc, char **argv, int attach)
6307 /* check number of arguments */
6309 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6313 (void) fprintf(stderr, gettext("too many arguments\n"));
6317 jailid = atoi(argv[1]);
6319 (void) fprintf(stderr, gettext("invalid jailid\n"));
6323 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6327 ret = (zfs_jail(zhp, jailid, attach) != 0);
6334 * zfs jail jailid filesystem
6336 * Attach the given dataset to the given jail
6340 zfs_do_jail(int argc, char **argv)
6343 return (do_jail(argc, argv, 1));
6347 * zfs unjail jailid filesystem
6349 * Detach the given dataset from the given jail
6353 zfs_do_unjail(int argc, char **argv)
6356 return (do_jail(argc, argv, 0));
6360 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6361 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6364 manual_mount(int argc, char **argv)
6367 char mountpoint[ZFS_MAXPROPLEN];
6368 char mntopts[MNT_LINE_MAX] = { '\0' };
6372 char *dataset, *path;
6375 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6378 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6381 flags |= MS_OVERLAY;
6384 flags |= MS_NOMNTTAB;
6387 (void) fprintf(stderr, gettext("missing argument for "
6388 "'%c' option\n"), optopt);
6392 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6394 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6403 /* check that we only have two arguments */
6406 (void) fprintf(stderr, gettext("missing dataset "
6409 (void) fprintf(stderr,
6410 gettext("missing mountpoint argument\n"));
6412 (void) fprintf(stderr, gettext("too many arguments\n"));
6413 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6420 /* try to open the dataset */
6421 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6424 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6425 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6427 /* check for legacy mountpoint and complain appropriately */
6429 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6430 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6431 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6432 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6437 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6438 "mounted using 'mount -F zfs'\n"), dataset);
6439 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6440 "instead.\n"), path);
6441 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
6442 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6443 (void) fprintf(stderr, gettext("See zfs(1M) for more "
6452 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6453 * unmounts of non-legacy filesystems, as this is the dominant administrative
6457 manual_unmount(int argc, char **argv)
6463 while ((c = getopt(argc, argv, "f")) != -1) {
6469 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6471 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6480 /* check arguments */
6483 (void) fprintf(stderr, gettext("missing path "
6486 (void) fprintf(stderr, gettext("too many arguments\n"));
6487 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6491 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6495 find_command_idx(char *command, int *idx)
6499 for (i = 0; i < NCOMMAND; i++) {
6500 if (command_table[i].name == NULL)
6503 if (strcmp(command, command_table[i].name) == 0) {
6512 zfs_do_diff(int argc, char **argv)
6516 char *tosnap = NULL;
6517 char *fromsnap = NULL;
6522 while ((c = getopt(argc, argv, "FHt")) != -1) {
6525 flags |= ZFS_DIFF_CLASSIFY;
6528 flags |= ZFS_DIFF_PARSEABLE;
6531 flags |= ZFS_DIFF_TIMESTAMP;
6534 (void) fprintf(stderr,
6535 gettext("invalid option '%c'\n"), optopt);
6544 (void) fprintf(stderr,
6545 gettext("must provide at least one snapshot name\n"));
6550 (void) fprintf(stderr, gettext("too many arguments\n"));
6555 tosnap = (argc == 2) ? argv[1] : NULL;
6558 if (*fromsnap != '@')
6559 copy = strdup(fromsnap);
6561 copy = strdup(tosnap);
6565 if (atp = strchr(copy, '@'))
6568 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6574 * Ignore SIGPIPE so that the library can give us
6575 * information on any failure
6577 (void) sigignore(SIGPIPE);
6579 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6587 main(int argc, char **argv)
6594 (void) setlocale(LC_ALL, "");
6595 (void) textdomain(TEXT_DOMAIN);
6599 if ((g_zfs = libzfs_init()) == NULL) {
6600 (void) fprintf(stderr, gettext("internal error: failed to "
6601 "initialize ZFS library\n"));
6605 zpool_set_history_str("zfs", argc, argv, history_str);
6606 verify(zpool_stage_history(g_zfs, history_str) == 0);
6608 libzfs_print_on_error(g_zfs, B_TRUE);
6610 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6611 (void) fprintf(stderr, gettext("internal error: unable to "
6612 "open %s\n"), MNTTAB);
6617 * This command also doubles as the /etc/fs mount and unmount program.
6618 * Determine if we should take this behavior based on argv[0].
6620 progname = basename(argv[0]);
6621 if (strcmp(progname, "mount") == 0) {
6622 ret = manual_mount(argc, argv);
6623 } else if (strcmp(progname, "umount") == 0) {
6624 ret = manual_unmount(argc, argv);
6627 * Make sure the user has specified some command.
6630 (void) fprintf(stderr, gettext("missing command\n"));
6637 * The 'umount' command is an alias for 'unmount'
6639 if (strcmp(cmdname, "umount") == 0)
6640 cmdname = "unmount";
6643 * The 'recv' command is an alias for 'receive'
6645 if (strcmp(cmdname, "recv") == 0)
6646 cmdname = "receive";
6651 if (strcmp(cmdname, "-?") == 0)
6655 * Run the appropriate command.
6657 libzfs_mnttab_cache(g_zfs, B_TRUE);
6658 if (find_command_idx(cmdname, &i) == 0) {
6659 current_command = &command_table[i];
6660 ret = command_table[i].func(argc - 1, argv + 1);
6661 } else if (strchr(cmdname, '=') != NULL) {
6662 verify(find_command_idx("set", &i) == 0);
6663 current_command = &command_table[i];
6664 ret = command_table[i].func(argc, argv);
6666 (void) fprintf(stderr, gettext("unrecognized "
6667 "command '%s'\n"), cmdname);
6670 libzfs_mnttab_cache(g_zfs, B_FALSE);
6673 (void) fclose(mnttab_file);
6678 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6679 * for the purposes of running ::findleaks.
6681 if (getenv("ZFS_ABORT") != NULL) {
6682 (void) printf("dumping core by request\n");