4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright 2012 Milan Jurik. All rights reserved.
27 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
29 * All rights reserved.
30 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
31 * Copyright (c) 2013 Steven Hartland. All rights reserved.
40 #include <libnvpair.h>
53 #include <sys/mntent.h>
54 #include <sys/mnttab.h>
55 #include <sys/mount.h>
57 #include <sys/fs/zfs.h>
58 #include <sys/types.h>
62 #include <libzfs_core.h>
64 #include <zfs_deleg.h>
68 #include <directory.h>
73 #include "zfs_comutil.h"
75 libzfs_handle_t *g_zfs;
77 static FILE *mnttab_file;
78 static char history_str[HIS_MAX_RECORD_LEN];
79 static boolean_t log_history = B_TRUE;
81 static int zfs_do_clone(int argc, char **argv);
82 static int zfs_do_create(int argc, char **argv);
83 static int zfs_do_destroy(int argc, char **argv);
84 static int zfs_do_get(int argc, char **argv);
85 static int zfs_do_inherit(int argc, char **argv);
86 static int zfs_do_list(int argc, char **argv);
87 static int zfs_do_mount(int argc, char **argv);
88 static int zfs_do_rename(int argc, char **argv);
89 static int zfs_do_rollback(int argc, char **argv);
90 static int zfs_do_set(int argc, char **argv);
91 static int zfs_do_upgrade(int argc, char **argv);
92 static int zfs_do_snapshot(int argc, char **argv);
93 static int zfs_do_unmount(int argc, char **argv);
94 static int zfs_do_share(int argc, char **argv);
95 static int zfs_do_unshare(int argc, char **argv);
96 static int zfs_do_send(int argc, char **argv);
97 static int zfs_do_receive(int argc, char **argv);
98 static int zfs_do_promote(int argc, char **argv);
99 static int zfs_do_userspace(int argc, char **argv);
100 static int zfs_do_allow(int argc, char **argv);
101 static int zfs_do_unallow(int argc, char **argv);
102 static int zfs_do_hold(int argc, char **argv);
103 static int zfs_do_holds(int argc, char **argv);
104 static int zfs_do_release(int argc, char **argv);
105 static int zfs_do_diff(int argc, char **argv);
106 static int zfs_do_jail(int argc, char **argv);
107 static int zfs_do_unjail(int argc, char **argv);
110 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
115 _umem_debug_init(void)
117 return ("default,verbose"); /* $UMEM_DEBUG setting */
121 _umem_logging_init(void)
123 return ("fail,contents"); /* $UMEM_LOGGING setting */
158 typedef struct zfs_command {
160 int (*func)(int argc, char **argv);
165 * Master command table. Each ZFS command has a name, associated function, and
166 * usage message. The usage messages need to be internationalized, so we have
167 * to have a function to return the usage message based on a command index.
169 * These commands are organized according to how they are displayed in the usage
170 * message. An empty command (one with a NULL name) indicates an empty line in
171 * the generic usage message.
173 static zfs_command_t command_table[] = {
174 { "create", zfs_do_create, HELP_CREATE },
175 { "destroy", zfs_do_destroy, HELP_DESTROY },
177 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
178 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
179 { "clone", zfs_do_clone, HELP_CLONE },
180 { "promote", zfs_do_promote, HELP_PROMOTE },
181 { "rename", zfs_do_rename, HELP_RENAME },
183 { "list", zfs_do_list, HELP_LIST },
185 { "set", zfs_do_set, HELP_SET },
186 { "get", zfs_do_get, HELP_GET },
187 { "inherit", zfs_do_inherit, HELP_INHERIT },
188 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
189 { "userspace", zfs_do_userspace, HELP_USERSPACE },
190 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
192 { "mount", zfs_do_mount, HELP_MOUNT },
193 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
194 { "share", zfs_do_share, HELP_SHARE },
195 { "unshare", zfs_do_unshare, HELP_UNSHARE },
197 { "send", zfs_do_send, HELP_SEND },
198 { "receive", zfs_do_receive, HELP_RECEIVE },
200 { "allow", zfs_do_allow, HELP_ALLOW },
202 { "unallow", zfs_do_unallow, HELP_UNALLOW },
204 { "hold", zfs_do_hold, HELP_HOLD },
205 { "holds", zfs_do_holds, HELP_HOLDS },
206 { "release", zfs_do_release, HELP_RELEASE },
207 { "diff", zfs_do_diff, HELP_DIFF },
209 { "jail", zfs_do_jail, HELP_JAIL },
210 { "unjail", zfs_do_unjail, HELP_UNJAIL },
213 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
215 zfs_command_t *current_command;
218 get_usage(zfs_help_t idx)
222 return (gettext("\tclone [-p] [-o property=value] ... "
223 "<snapshot> <filesystem|volume>\n"));
225 return (gettext("\tcreate [-p] [-o property=value] ... "
227 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
228 "-V <size> <volume>\n"));
230 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
231 "\tdestroy [-dnpRrv] "
232 "<snapshot>[%<snapname>][,...]\n"));
234 return (gettext("\tget [-rHp] [-d max] "
235 "[-o \"all\" | field[,...]] [-t type[,...]] "
236 "[-s source[,...]]\n"
237 "\t <\"all\" | property[,...]> "
238 "[filesystem|volume|snapshot] ...\n"));
240 return (gettext("\tinherit [-rS] <property> "
241 "<filesystem|volume|snapshot> ...\n"));
243 return (gettext("\tupgrade [-v]\n"
244 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
246 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
248 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
250 return (gettext("\tlist [-rH][-d max] "
251 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
252 "\t [-S property] ... "
253 "[filesystem|volume|snapshot] ...\n"));
255 return (gettext("\tmount\n"
256 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
258 return (gettext("\tpromote <clone-filesystem>\n"));
260 return (gettext("\treceive [-vnFu] <filesystem|volume|"
262 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
264 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
265 "<filesystem|volume|snapshot>\n"
266 "\trename [-f] -p <filesystem|volume> "
267 "<filesystem|volume>\n"
268 "\trename -r <snapshot> <snapshot>\n"
269 "\trename -u [-p] <filesystem> <filesystem>"));
271 return (gettext("\trollback [-rRf] <snapshot>\n"));
273 return (gettext("\tsend [-DnPpRv] "
274 "[-i snapshot | -I snapshot] <snapshot>\n"));
276 return (gettext("\tset <property=value> "
277 "<filesystem|volume|snapshot> ...\n"));
279 return (gettext("\tshare <-a | filesystem>\n"));
281 return (gettext("\tsnapshot [-r] [-o property=value] ... "
282 "<filesystem@snapname|volume@snapname> ...\n"));
284 return (gettext("\tunmount [-f] "
285 "<-a | filesystem|mountpoint>\n"));
287 return (gettext("\tunshare "
288 "<-a | filesystem|mountpoint>\n"));
290 return (gettext("\tallow <filesystem|volume>\n"
292 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
293 "\t <filesystem|volume>\n"
294 "\tallow [-ld] -e <perm|@setname>[,...] "
295 "<filesystem|volume>\n"
296 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
297 "\tallow -s @setname <perm|@setname>[,...] "
298 "<filesystem|volume>\n"));
300 return (gettext("\tunallow [-rldug] "
301 "<\"everyone\"|user|group>[,...]\n"
302 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
303 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
304 "<filesystem|volume>\n"
305 "\tunallow [-r] -c [<perm|@setname>[,...]] "
306 "<filesystem|volume>\n"
307 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
308 "<filesystem|volume>\n"));
310 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
311 "[-s field] ...\n\t[-S field] ... "
312 "[-t type[,...]] <filesystem|snapshot>\n"));
313 case HELP_GROUPSPACE:
314 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
315 "[-s field] ...\n\t[-S field] ... "
316 "[-t type[,...]] <filesystem|snapshot>\n"));
318 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
320 return (gettext("\tholds [-r] <snapshot> ...\n"));
322 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
324 return (gettext("\tdiff [-FHt] <snapshot> "
325 "[snapshot|filesystem]\n"));
335 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
340 * Utility function to guarantee malloc() success.
344 safe_malloc(size_t size)
348 if ((data = calloc(1, size)) == NULL)
355 safe_strdup(char *str)
357 char *dupstr = strdup(str);
366 * Callback routine that will print out information for each of
370 usage_prop_cb(int prop, void *cb)
374 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
376 if (zfs_prop_readonly(prop))
377 (void) fprintf(fp, " NO ");
379 (void) fprintf(fp, "YES ");
381 if (zfs_prop_inheritable(prop))
382 (void) fprintf(fp, " YES ");
384 (void) fprintf(fp, " NO ");
386 if (zfs_prop_values(prop) == NULL)
387 (void) fprintf(fp, "-\n");
389 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
395 * Display usage message. If we're inside a command, display only the usage for
396 * that command. Otherwise, iterate over the entire command table and display
397 * a complete usage message.
400 usage(boolean_t requested)
403 boolean_t show_properties = B_FALSE;
404 FILE *fp = requested ? stdout : stderr;
406 if (current_command == NULL) {
408 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
410 gettext("where 'command' is one of the following:\n\n"));
412 for (i = 0; i < NCOMMAND; i++) {
413 if (command_table[i].name == NULL)
414 (void) fprintf(fp, "\n");
416 (void) fprintf(fp, "%s",
417 get_usage(command_table[i].usage));
420 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
421 "pool/[dataset/]*dataset[@name]\n"));
423 (void) fprintf(fp, gettext("usage:\n"));
424 (void) fprintf(fp, "%s", get_usage(current_command->usage));
427 if (current_command != NULL &&
428 (strcmp(current_command->name, "set") == 0 ||
429 strcmp(current_command->name, "get") == 0 ||
430 strcmp(current_command->name, "inherit") == 0 ||
431 strcmp(current_command->name, "list") == 0))
432 show_properties = B_TRUE;
434 if (show_properties) {
436 gettext("\nThe following properties are supported:\n"));
438 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
439 "PROPERTY", "EDIT", "INHERIT", "VALUES");
441 /* Iterate over all properties */
442 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
445 (void) fprintf(fp, "\t%-15s ", "userused@...");
446 (void) fprintf(fp, " NO NO <size>\n");
447 (void) fprintf(fp, "\t%-15s ", "groupused@...");
448 (void) fprintf(fp, " NO NO <size>\n");
449 (void) fprintf(fp, "\t%-15s ", "userquota@...");
450 (void) fprintf(fp, "YES NO <size> | none\n");
451 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
452 (void) fprintf(fp, "YES NO <size> | none\n");
453 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
454 (void) fprintf(fp, " NO NO <size>\n");
456 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
457 "with standard units such as K, M, G, etc.\n"));
458 (void) fprintf(fp, gettext("\nUser-defined properties can "
459 "be specified by using a name containing a colon (:).\n"));
460 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
461 "properties must be appended with\n"
462 "a user or group specifier of one of these forms:\n"
463 " POSIX name (eg: \"matt\")\n"
464 " POSIX id (eg: \"126829\")\n"
465 " SMB name@domain (eg: \"matt@sun\")\n"
466 " SMB SID (eg: \"S-1-234-567-89\")\n"));
469 gettext("\nFor the property list, run: %s\n"),
472 gettext("\nFor the delegated permission list, run: %s\n"),
473 "zfs allow|unallow");
477 * See comments at end of main().
479 if (getenv("ZFS_ABORT") != NULL) {
480 (void) printf("dumping core by request\n");
484 exit(requested ? 0 : 2);
488 parseprop(nvlist_t *props)
490 char *propname = optarg;
491 char *propval, *strval;
493 if ((propval = strchr(propname, '=')) == NULL) {
494 (void) fprintf(stderr, gettext("missing "
495 "'=' for -o option\n"));
500 if (nvlist_lookup_string(props, propname, &strval) == 0) {
501 (void) fprintf(stderr, gettext("property '%s' "
502 "specified multiple times\n"), propname);
505 if (nvlist_add_string(props, propname, propval) != 0)
511 parse_depth(char *opt, int *flags)
516 depth = (int)strtol(opt, &tmp, 0);
518 (void) fprintf(stderr,
519 gettext("%s is not an integer\n"), optarg);
523 (void) fprintf(stderr,
524 gettext("Depth can not be negative.\n"));
527 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
531 #define PROGRESS_DELAY 2 /* seconds */
533 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";
534 static time_t pt_begin;
535 static char *pt_header = NULL;
536 static boolean_t pt_shown;
539 start_progress_timer(void)
541 pt_begin = time(NULL) + PROGRESS_DELAY;
546 set_progress_header(char *header)
548 assert(pt_header == NULL);
549 pt_header = safe_strdup(header);
551 (void) printf("%s: ", header);
552 (void) fflush(stdout);
557 update_progress(char *update)
559 if (!pt_shown && time(NULL) > pt_begin) {
560 int len = strlen(update);
562 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
564 (void) fflush(stdout);
566 } else if (pt_shown) {
567 int len = strlen(update);
569 (void) printf("%s%*.*s", update, len, len, pt_reverse);
570 (void) fflush(stdout);
575 finish_progress(char *done)
578 (void) printf("%s\n", done);
579 (void) fflush(stdout);
585 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
587 * Given an existing dataset, create a writable copy whose initial contents
588 * are the same as the source. The newly created dataset maintains a
589 * dependency on the original; the original cannot be destroyed so long as
592 * The '-p' flag creates all the non-existing ancestors of the target first.
595 zfs_do_clone(int argc, char **argv)
597 zfs_handle_t *zhp = NULL;
598 boolean_t parents = B_FALSE;
603 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
607 while ((c = getopt(argc, argv, "o:p")) != -1) {
610 if (parseprop(props))
617 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
626 /* check number of arguments */
628 (void) fprintf(stderr, gettext("missing source dataset "
633 (void) fprintf(stderr, gettext("missing target dataset "
638 (void) fprintf(stderr, gettext("too many arguments\n"));
642 /* open the source dataset */
643 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
646 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
649 * Now create the ancestors of the target dataset. If the
650 * target already exists and '-p' option was used we should not
653 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
656 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
661 ret = zfs_clone(zhp, argv[1], props);
663 /* create the mountpoint if necessary */
667 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
669 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
670 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
671 ret = zfs_share(clone);
690 * zfs create [-p] [-o prop=value] ... fs
691 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
693 * Create a new dataset. This command can be used to create filesystems
694 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
695 * For volumes, the user must specify a size to be used.
697 * The '-s' flag applies only to volumes, and indicates that we should not try
698 * to set the reservation for this volume. By default we set a reservation
699 * equal to the size for any volume. For pools with SPA_VERSION >=
700 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
702 * The '-p' flag creates all the non-existing ancestors of the target first.
705 zfs_do_create(int argc, char **argv)
707 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
708 zfs_handle_t *zhp = NULL;
711 boolean_t noreserve = B_FALSE;
712 boolean_t bflag = B_FALSE;
713 boolean_t parents = B_FALSE;
717 int canmount = ZFS_CANMOUNT_OFF;
719 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
723 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
726 type = ZFS_TYPE_VOLUME;
727 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
728 (void) fprintf(stderr, gettext("bad volume "
729 "size '%s': %s\n"), optarg,
730 libzfs_error_description(g_zfs));
734 if (nvlist_add_uint64(props,
735 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
744 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
745 (void) fprintf(stderr, gettext("bad volume "
746 "block size '%s': %s\n"), optarg,
747 libzfs_error_description(g_zfs));
751 if (nvlist_add_uint64(props,
752 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
757 if (parseprop(props))
764 (void) fprintf(stderr, gettext("missing size "
768 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
774 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
775 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
776 "used when creating a volume\n"));
783 /* check number of arguments */
785 (void) fprintf(stderr, gettext("missing %s argument\n"),
786 zfs_type_to_name(type));
790 (void) fprintf(stderr, gettext("too many arguments\n"));
794 if (type == ZFS_TYPE_VOLUME && !noreserve) {
795 zpool_handle_t *zpool_handle;
796 uint64_t spa_version;
798 zfs_prop_t resv_prop;
801 if (p = strchr(argv[0], '/'))
803 zpool_handle = zpool_open(g_zfs, argv[0]);
806 if (zpool_handle == NULL)
808 spa_version = zpool_get_prop_int(zpool_handle,
809 ZPOOL_PROP_VERSION, NULL);
810 zpool_close(zpool_handle);
811 if (spa_version >= SPA_VERSION_REFRESERVATION)
812 resv_prop = ZFS_PROP_REFRESERVATION;
814 resv_prop = ZFS_PROP_RESERVATION;
815 volsize = zvol_volsize_to_reservation(volsize, props);
817 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
819 if (nvlist_add_uint64(props,
820 zfs_prop_to_name(resv_prop), volsize) != 0) {
827 if (parents && zfs_name_valid(argv[0], type)) {
829 * Now create the ancestors of target dataset. If the target
830 * already exists and '-p' option was used we should not
833 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
837 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
842 if (zfs_create(g_zfs, argv[0], type, props) != 0)
845 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
850 * if the user doesn't want the dataset automatically mounted,
851 * then skip the mount/share step
853 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
854 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
857 * Mount and/or share the new filesystem as appropriate. We provide a
858 * verbose error message to let the user know that their filesystem was
859 * in fact created, even if we failed to mount or share it.
861 if (canmount == ZFS_CANMOUNT_ON) {
862 if (zfs_mount(zhp, NULL, 0) != 0) {
863 (void) fprintf(stderr, gettext("filesystem "
864 "successfully created, but not mounted\n"));
866 } else if (zfs_share(zhp) != 0) {
867 (void) fprintf(stderr, gettext("filesystem "
868 "successfully created, but not shared\n"));
885 * zfs destroy [-rRf] <fs, vol>
886 * zfs destroy [-rRd] <snap>
888 * -r Recursively destroy all children
889 * -R Recursively destroy all dependents, including clones
890 * -f Force unmounting of any dependents
891 * -d If we can't destroy now, mark for deferred destruction
893 * Destroys the given dataset. By default, it will unmount any filesystems,
894 * and refuse to destroy a dataset that has any dependents. A dependent can
895 * either be a child, or a clone of a child.
897 typedef struct destroy_cbdata {
900 boolean_t cb_recurse;
902 boolean_t cb_doclones;
903 zfs_handle_t *cb_target;
904 boolean_t cb_defer_destroy;
905 boolean_t cb_verbose;
906 boolean_t cb_parsable;
909 nvlist_t *cb_batchedsnaps;
911 /* first snap in contiguous run */
913 /* previous snap in contiguous run */
920 * Check for any dependents based on the '-r' or '-R' flags.
923 destroy_check_dependent(zfs_handle_t *zhp, void *data)
925 destroy_cbdata_t *cbp = data;
926 const char *tname = zfs_get_name(cbp->cb_target);
927 const char *name = zfs_get_name(zhp);
929 if (strncmp(tname, name, strlen(tname)) == 0 &&
930 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
932 * This is a direct descendant, not a clone somewhere else in
939 (void) fprintf(stderr, gettext("cannot destroy '%s': "
940 "%s has children\n"),
941 zfs_get_name(cbp->cb_target),
942 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
943 (void) fprintf(stderr, gettext("use '-r' to destroy "
944 "the following datasets:\n"));
945 cbp->cb_first = B_FALSE;
946 cbp->cb_error = B_TRUE;
949 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
952 * This is a clone. We only want to report this if the '-r'
953 * wasn't specified, or the target is a snapshot.
955 if (!cbp->cb_recurse &&
956 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
960 (void) fprintf(stderr, gettext("cannot destroy '%s': "
961 "%s has dependent clones\n"),
962 zfs_get_name(cbp->cb_target),
963 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
964 (void) fprintf(stderr, gettext("use '-R' to destroy "
965 "the following datasets:\n"));
966 cbp->cb_first = B_FALSE;
967 cbp->cb_error = B_TRUE;
968 cbp->cb_dryrun = B_TRUE;
971 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
980 destroy_callback(zfs_handle_t *zhp, void *data)
982 destroy_cbdata_t *cb = data;
983 const char *name = zfs_get_name(zhp);
985 if (cb->cb_verbose) {
986 if (cb->cb_parsable) {
987 (void) printf("destroy\t%s\n", name);
988 } else if (cb->cb_dryrun) {
989 (void) printf(gettext("would destroy %s\n"),
992 (void) printf(gettext("will destroy %s\n"),
998 * Ignore pools (which we've already flagged as an error before getting
1001 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1002 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1006 if (cb->cb_dryrun) {
1012 * We batch up all contiguous snapshots (even of different
1013 * filesystems) and destroy them with one ioctl. We can't
1014 * simply do all snap deletions and then all fs deletions,
1015 * because we must delete a clone before its origin.
1017 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1018 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1020 int error = zfs_destroy_snaps_nvl(g_zfs,
1021 cb->cb_batchedsnaps, B_FALSE);
1022 fnvlist_free(cb->cb_batchedsnaps);
1023 cb->cb_batchedsnaps = fnvlist_alloc();
1026 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1027 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1038 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1040 destroy_cbdata_t *cb = arg;
1041 const char *name = zfs_get_name(zhp);
1044 if (nvlist_exists(cb->cb_nvl, name)) {
1045 if (cb->cb_firstsnap == NULL)
1046 cb->cb_firstsnap = strdup(name);
1047 if (cb->cb_prevsnap != NULL)
1048 free(cb->cb_prevsnap);
1049 /* this snap continues the current range */
1050 cb->cb_prevsnap = strdup(name);
1051 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1053 if (cb->cb_verbose) {
1054 if (cb->cb_parsable) {
1055 (void) printf("destroy\t%s\n", name);
1056 } else if (cb->cb_dryrun) {
1057 (void) printf(gettext("would destroy %s\n"),
1060 (void) printf(gettext("will destroy %s\n"),
1064 } else if (cb->cb_firstsnap != NULL) {
1065 /* end of this range */
1067 err = lzc_snaprange_space(cb->cb_firstsnap,
1068 cb->cb_prevsnap, &used);
1069 cb->cb_snapused += used;
1070 free(cb->cb_firstsnap);
1071 cb->cb_firstsnap = NULL;
1072 free(cb->cb_prevsnap);
1073 cb->cb_prevsnap = NULL;
1080 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1083 assert(cb->cb_firstsnap == NULL);
1084 assert(cb->cb_prevsnap == NULL);
1085 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1086 if (cb->cb_firstsnap != NULL) {
1089 err = lzc_snaprange_space(cb->cb_firstsnap,
1090 cb->cb_prevsnap, &used);
1092 cb->cb_snapused += used;
1093 free(cb->cb_firstsnap);
1094 cb->cb_firstsnap = NULL;
1095 free(cb->cb_prevsnap);
1096 cb->cb_prevsnap = NULL;
1102 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1104 destroy_cbdata_t *cb = arg;
1107 /* Check for clones. */
1108 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1109 cb->cb_target = zhp;
1110 cb->cb_first = B_TRUE;
1111 err = zfs_iter_dependents(zhp, B_TRUE,
1112 destroy_check_dependent, cb);
1116 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1124 gather_snapshots(zfs_handle_t *zhp, void *arg)
1126 destroy_cbdata_t *cb = arg;
1129 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1135 if (cb->cb_verbose) {
1136 err = destroy_print_snapshots(zhp, cb);
1142 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1150 destroy_clones(destroy_cbdata_t *cb)
1153 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1155 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1156 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1159 boolean_t defer = cb->cb_defer_destroy;
1163 * We can't defer destroy non-snapshots, so set it to
1164 * false while destroying the clones.
1166 cb->cb_defer_destroy = B_FALSE;
1167 err = zfs_iter_dependents(zhp, B_FALSE,
1168 destroy_callback, cb);
1169 cb->cb_defer_destroy = defer;
1179 zfs_do_destroy(int argc, char **argv)
1181 destroy_cbdata_t cb = { 0 };
1185 zfs_handle_t *zhp = NULL;
1187 zfs_type_t type = ZFS_TYPE_DATASET;
1190 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1193 cb.cb_verbose = B_TRUE;
1196 cb.cb_verbose = B_TRUE;
1197 cb.cb_parsable = B_TRUE;
1200 cb.cb_dryrun = B_TRUE;
1203 cb.cb_defer_destroy = B_TRUE;
1204 type = ZFS_TYPE_SNAPSHOT;
1207 cb.cb_force = B_TRUE;
1210 cb.cb_recurse = B_TRUE;
1213 cb.cb_recurse = B_TRUE;
1214 cb.cb_doclones = B_TRUE;
1218 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1227 /* check number of arguments */
1229 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1233 (void) fprintf(stderr, gettext("too many arguments\n"));
1237 at = strchr(argv[0], '@');
1240 /* Build the list of snaps to destroy in cb_nvl. */
1241 cb.cb_nvl = fnvlist_alloc();
1244 zhp = zfs_open(g_zfs, argv[0],
1245 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1249 cb.cb_snapspec = at + 1;
1250 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1256 if (nvlist_empty(cb.cb_nvl)) {
1257 (void) fprintf(stderr, gettext("could not find any "
1258 "snapshots to destroy; check snapshot names.\n"));
1263 if (cb.cb_verbose) {
1265 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1266 if (cb.cb_parsable) {
1267 (void) printf("reclaim\t%llu\n",
1269 } else if (cb.cb_dryrun) {
1270 (void) printf(gettext("would reclaim %s\n"),
1273 (void) printf(gettext("will reclaim %s\n"),
1278 if (!cb.cb_dryrun) {
1279 if (cb.cb_doclones) {
1280 cb.cb_batchedsnaps = fnvlist_alloc();
1281 err = destroy_clones(&cb);
1283 err = zfs_destroy_snaps_nvl(g_zfs,
1284 cb.cb_batchedsnaps, B_FALSE);
1292 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1293 cb.cb_defer_destroy);
1300 /* Open the given dataset */
1301 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1307 * Perform an explicit check for pools before going any further.
1309 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1310 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1311 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1312 "operation does not apply to pools\n"),
1314 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1315 "%s' to destroy all datasets in the pool\n"),
1317 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1318 "to destroy the pool itself\n"), zfs_get_name(zhp));
1324 * Check for any dependents and/or clones.
1326 cb.cb_first = B_TRUE;
1327 if (!cb.cb_doclones &&
1328 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1339 cb.cb_batchedsnaps = fnvlist_alloc();
1340 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1347 * Do the real thing. The callback will close the
1348 * handle regardless of whether it succeeds or not.
1350 err = destroy_callback(zhp, &cb);
1353 err = zfs_destroy_snaps_nvl(g_zfs,
1354 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1361 fnvlist_free(cb.cb_batchedsnaps);
1362 fnvlist_free(cb.cb_nvl);
1369 is_recvd_column(zprop_get_cbdata_t *cbp)
1372 zfs_get_column_t col;
1374 for (i = 0; i < ZFS_GET_NCOLS &&
1375 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1376 if (col == GET_COL_RECVD)
1382 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1383 * < all | property[,property]... > < fs | snap | vol > ...
1385 * -r recurse over any child datasets
1386 * -H scripted mode. Headers are stripped, and fields are separated
1387 * by tabs instead of spaces.
1388 * -o Set of fields to display. One of "name,property,value,
1389 * received,source". Default is "name,property,value,source".
1390 * "all" is an alias for all five.
1391 * -s Set of sources to allow. One of
1392 * "local,default,inherited,received,temporary,none". Default is
1394 * -p Display values in parsable (literal) format.
1396 * Prints properties for the given datasets. The user can control which
1397 * columns to display as well as which property types to allow.
1401 * Invoked to display the properties for a single dataset.
1404 get_callback(zfs_handle_t *zhp, void *data)
1406 char buf[ZFS_MAXPROPLEN];
1407 char rbuf[ZFS_MAXPROPLEN];
1408 zprop_source_t sourcetype;
1409 char source[ZFS_MAXNAMELEN];
1410 zprop_get_cbdata_t *cbp = data;
1411 nvlist_t *user_props = zfs_get_user_props(zhp);
1412 zprop_list_t *pl = cbp->cb_proplist;
1416 boolean_t received = is_recvd_column(cbp);
1418 for (; pl != NULL; pl = pl->pl_next) {
1419 char *recvdval = NULL;
1421 * Skip the special fake placeholder. This will also skip over
1422 * the name property when 'all' is specified.
1424 if (pl->pl_prop == ZFS_PROP_NAME &&
1425 pl == cbp->cb_proplist)
1428 if (pl->pl_prop != ZPROP_INVAL) {
1429 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1430 sizeof (buf), &sourcetype, source,
1432 cbp->cb_literal) != 0) {
1435 if (!zfs_prop_valid_for_type(pl->pl_prop,
1436 ZFS_TYPE_DATASET)) {
1437 (void) fprintf(stderr,
1438 gettext("No such property '%s'\n"),
1439 zfs_prop_to_name(pl->pl_prop));
1442 sourcetype = ZPROP_SRC_NONE;
1443 (void) strlcpy(buf, "-", sizeof (buf));
1446 if (received && (zfs_prop_get_recvd(zhp,
1447 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1448 cbp->cb_literal) == 0))
1451 zprop_print_one_property(zfs_get_name(zhp), cbp,
1452 zfs_prop_to_name(pl->pl_prop),
1453 buf, sourcetype, source, recvdval);
1454 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1455 sourcetype = ZPROP_SRC_LOCAL;
1457 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1458 buf, sizeof (buf), cbp->cb_literal) != 0) {
1459 sourcetype = ZPROP_SRC_NONE;
1460 (void) strlcpy(buf, "-", sizeof (buf));
1463 zprop_print_one_property(zfs_get_name(zhp), cbp,
1464 pl->pl_user_prop, buf, sourcetype, source, NULL);
1465 } else if (zfs_prop_written(pl->pl_user_prop)) {
1466 sourcetype = ZPROP_SRC_LOCAL;
1468 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1469 buf, sizeof (buf), cbp->cb_literal) != 0) {
1470 sourcetype = ZPROP_SRC_NONE;
1471 (void) strlcpy(buf, "-", sizeof (buf));
1474 zprop_print_one_property(zfs_get_name(zhp), cbp,
1475 pl->pl_user_prop, buf, sourcetype, source, NULL);
1477 if (nvlist_lookup_nvlist(user_props,
1478 pl->pl_user_prop, &propval) != 0) {
1481 sourcetype = ZPROP_SRC_NONE;
1484 verify(nvlist_lookup_string(propval,
1485 ZPROP_VALUE, &strval) == 0);
1486 verify(nvlist_lookup_string(propval,
1487 ZPROP_SOURCE, &sourceval) == 0);
1489 if (strcmp(sourceval,
1490 zfs_get_name(zhp)) == 0) {
1491 sourcetype = ZPROP_SRC_LOCAL;
1492 } else if (strcmp(sourceval,
1493 ZPROP_SOURCE_VAL_RECVD) == 0) {
1494 sourcetype = ZPROP_SRC_RECEIVED;
1496 sourcetype = ZPROP_SRC_INHERITED;
1497 (void) strlcpy(source,
1498 sourceval, sizeof (source));
1502 if (received && (zfs_prop_get_recvd(zhp,
1503 pl->pl_user_prop, rbuf, sizeof (rbuf),
1504 cbp->cb_literal) == 0))
1507 zprop_print_one_property(zfs_get_name(zhp), cbp,
1508 pl->pl_user_prop, strval, sourcetype,
1517 zfs_do_get(int argc, char **argv)
1519 zprop_get_cbdata_t cb = { 0 };
1520 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1521 int types = ZFS_TYPE_DATASET;
1522 char *value, *fields;
1525 zprop_list_t fake_name = { 0 };
1528 * Set up default columns and sources.
1530 cb.cb_sources = ZPROP_SRC_ALL;
1531 cb.cb_columns[0] = GET_COL_NAME;
1532 cb.cb_columns[1] = GET_COL_PROPERTY;
1533 cb.cb_columns[2] = GET_COL_VALUE;
1534 cb.cb_columns[3] = GET_COL_SOURCE;
1535 cb.cb_type = ZFS_TYPE_DATASET;
1538 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1541 cb.cb_literal = B_TRUE;
1544 limit = parse_depth(optarg, &flags);
1547 flags |= ZFS_ITER_RECURSE;
1550 cb.cb_scripted = B_TRUE;
1553 (void) fprintf(stderr, gettext("missing argument for "
1554 "'%c' option\n"), optopt);
1559 * Process the set of columns to display. We zero out
1560 * the structure to give us a blank slate.
1562 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1564 while (*optarg != '\0') {
1565 static char *col_subopts[] =
1566 { "name", "property", "value", "received",
1567 "source", "all", NULL };
1569 if (i == ZFS_GET_NCOLS) {
1570 (void) fprintf(stderr, gettext("too "
1571 "many fields given to -o "
1576 switch (getsubopt(&optarg, col_subopts,
1579 cb.cb_columns[i++] = GET_COL_NAME;
1582 cb.cb_columns[i++] = GET_COL_PROPERTY;
1585 cb.cb_columns[i++] = GET_COL_VALUE;
1588 cb.cb_columns[i++] = GET_COL_RECVD;
1589 flags |= ZFS_ITER_RECVD_PROPS;
1592 cb.cb_columns[i++] = GET_COL_SOURCE;
1596 (void) fprintf(stderr,
1597 gettext("\"all\" conflicts "
1598 "with specific fields "
1599 "given to -o option\n"));
1602 cb.cb_columns[0] = GET_COL_NAME;
1603 cb.cb_columns[1] = GET_COL_PROPERTY;
1604 cb.cb_columns[2] = GET_COL_VALUE;
1605 cb.cb_columns[3] = GET_COL_RECVD;
1606 cb.cb_columns[4] = GET_COL_SOURCE;
1607 flags |= ZFS_ITER_RECVD_PROPS;
1611 (void) fprintf(stderr,
1612 gettext("invalid column name "
1621 while (*optarg != '\0') {
1622 static char *source_subopts[] = {
1623 "local", "default", "inherited",
1624 "received", "temporary", "none",
1627 switch (getsubopt(&optarg, source_subopts,
1630 cb.cb_sources |= ZPROP_SRC_LOCAL;
1633 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1636 cb.cb_sources |= ZPROP_SRC_INHERITED;
1639 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1642 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1645 cb.cb_sources |= ZPROP_SRC_NONE;
1648 (void) fprintf(stderr,
1649 gettext("invalid source "
1658 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1659 while (*optarg != '\0') {
1660 static char *type_subopts[] = { "filesystem",
1661 "volume", "snapshot", "all", NULL };
1663 switch (getsubopt(&optarg, type_subopts,
1666 types |= ZFS_TYPE_FILESYSTEM;
1669 types |= ZFS_TYPE_VOLUME;
1672 types |= ZFS_TYPE_SNAPSHOT;
1675 types = ZFS_TYPE_DATASET;
1679 (void) fprintf(stderr,
1680 gettext("invalid type '%s'\n"),
1688 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1698 (void) fprintf(stderr, gettext("missing property "
1705 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1713 * As part of zfs_expand_proplist(), we keep track of the maximum column
1714 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1715 * need to know the maximum name length. However, the user likely did
1716 * not specify 'name' as one of the properties to fetch, so we need to
1717 * make sure we always include at least this property for
1718 * print_get_headers() to work properly.
1720 if (cb.cb_proplist != NULL) {
1721 fake_name.pl_prop = ZFS_PROP_NAME;
1722 fake_name.pl_width = strlen(gettext("NAME"));
1723 fake_name.pl_next = cb.cb_proplist;
1724 cb.cb_proplist = &fake_name;
1727 cb.cb_first = B_TRUE;
1729 /* run for each object */
1730 ret = zfs_for_each(argc, argv, flags, types, NULL,
1731 &cb.cb_proplist, limit, get_callback, &cb);
1733 if (cb.cb_proplist == &fake_name)
1734 zprop_free_list(fake_name.pl_next);
1736 zprop_free_list(cb.cb_proplist);
1742 * inherit [-rS] <property> <fs|vol> ...
1744 * -r Recurse over all children
1745 * -S Revert to received value, if any
1747 * For each dataset specified on the command line, inherit the given property
1748 * from its parent. Inheriting a property at the pool level will cause it to
1749 * use the default value. The '-r' flag will recurse over all children, and is
1750 * useful for setting a property on a hierarchy-wide basis, regardless of any
1751 * local modifications for each dataset.
1754 typedef struct inherit_cbdata {
1755 const char *cb_propname;
1756 boolean_t cb_received;
1760 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1762 inherit_cbdata_t *cb = data;
1763 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1766 * If we're doing it recursively, then ignore properties that
1767 * are not valid for this type of dataset.
1769 if (prop != ZPROP_INVAL &&
1770 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1773 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1777 inherit_cb(zfs_handle_t *zhp, void *data)
1779 inherit_cbdata_t *cb = data;
1781 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1785 zfs_do_inherit(int argc, char **argv)
1789 inherit_cbdata_t cb = { 0 };
1793 boolean_t received = B_FALSE;
1796 while ((c = getopt(argc, argv, "rS")) != -1) {
1799 flags |= ZFS_ITER_RECURSE;
1806 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1815 /* check number of arguments */
1817 (void) fprintf(stderr, gettext("missing property argument\n"));
1821 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1829 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1830 if (zfs_prop_readonly(prop)) {
1831 (void) fprintf(stderr, gettext(
1832 "%s property is read-only\n"),
1836 if (!zfs_prop_inheritable(prop) && !received) {
1837 (void) fprintf(stderr, gettext("'%s' property cannot "
1838 "be inherited\n"), propname);
1839 if (prop == ZFS_PROP_QUOTA ||
1840 prop == ZFS_PROP_RESERVATION ||
1841 prop == ZFS_PROP_REFQUOTA ||
1842 prop == ZFS_PROP_REFRESERVATION)
1843 (void) fprintf(stderr, gettext("use 'zfs set "
1844 "%s=none' to clear\n"), propname);
1847 if (received && (prop == ZFS_PROP_VOLSIZE ||
1848 prop == ZFS_PROP_VERSION)) {
1849 (void) fprintf(stderr, gettext("'%s' property cannot "
1850 "be reverted to a received value\n"), propname);
1853 } else if (!zfs_prop_user(propname)) {
1854 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1859 cb.cb_propname = propname;
1860 cb.cb_received = received;
1862 if (flags & ZFS_ITER_RECURSE) {
1863 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1864 NULL, NULL, 0, inherit_recurse_cb, &cb);
1866 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1867 NULL, NULL, 0, inherit_cb, &cb);
1873 typedef struct upgrade_cbdata {
1874 uint64_t cb_numupgraded;
1875 uint64_t cb_numsamegraded;
1876 uint64_t cb_numfailed;
1877 uint64_t cb_version;
1879 boolean_t cb_foundone;
1880 char cb_lastfs[ZFS_MAXNAMELEN];
1884 same_pool(zfs_handle_t *zhp, const char *name)
1886 int len1 = strcspn(name, "/@");
1887 const char *zhname = zfs_get_name(zhp);
1888 int len2 = strcspn(zhname, "/@");
1892 return (strncmp(name, zhname, len1) == 0);
1896 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1898 upgrade_cbdata_t *cb = data;
1899 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1901 /* list if it's old/new */
1902 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1903 (cb->cb_newer && version > ZPL_VERSION)) {
1906 str = gettext("The following filesystems are "
1907 "formatted using a newer software version and\n"
1908 "cannot be accessed on the current system.\n\n");
1910 str = gettext("The following filesystems are "
1911 "out of date, and can be upgraded. After being\n"
1912 "upgraded, these filesystems (and any 'zfs send' "
1913 "streams generated from\n"
1914 "subsequent snapshots) will no longer be "
1915 "accessible by older software versions.\n\n");
1918 if (!cb->cb_foundone) {
1920 (void) printf(gettext("VER FILESYSTEM\n"));
1921 (void) printf(gettext("--- ------------\n"));
1922 cb->cb_foundone = B_TRUE;
1925 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1932 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1934 upgrade_cbdata_t *cb = data;
1935 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1936 int needed_spa_version;
1939 if (zfs_spa_version(zhp, &spa_version) < 0)
1942 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1944 if (needed_spa_version < 0)
1947 if (spa_version < needed_spa_version) {
1949 (void) printf(gettext("%s: can not be "
1950 "upgraded; the pool version needs to first "
1951 "be upgraded\nto version %d\n\n"),
1952 zfs_get_name(zhp), needed_spa_version);
1958 if (version < cb->cb_version) {
1960 (void) snprintf(verstr, sizeof (verstr),
1961 "%llu", cb->cb_version);
1962 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1964 * If they did "zfs upgrade -a", then we could
1965 * be doing ioctls to different pools. We need
1966 * to log this history once to each pool, and bypass
1967 * the normal history logging that happens in main().
1969 (void) zpool_log_history(g_zfs, history_str);
1970 log_history = B_FALSE;
1972 if (zfs_prop_set(zhp, "version", verstr) == 0)
1973 cb->cb_numupgraded++;
1976 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1977 } else if (version > cb->cb_version) {
1978 /* can't downgrade */
1979 (void) printf(gettext("%s: can not be downgraded; "
1980 "it is already at version %u\n"),
1981 zfs_get_name(zhp), version);
1984 cb->cb_numsamegraded++;
1992 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1995 zfs_do_upgrade(int argc, char **argv)
1997 boolean_t all = B_FALSE;
1998 boolean_t showversions = B_FALSE;
2000 upgrade_cbdata_t cb = { 0 };
2002 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2005 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2008 flags |= ZFS_ITER_RECURSE;
2011 showversions = B_TRUE;
2014 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2015 optarg, &cb.cb_version) != 0) {
2016 (void) fprintf(stderr,
2017 gettext("invalid version %s\n"), optarg);
2026 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2035 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2037 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2038 cb.cb_version || argc))
2040 if ((all || argc) && (showversions))
2046 /* Show info on available versions. */
2047 (void) printf(gettext("The following filesystem versions are "
2049 (void) printf(gettext("VER DESCRIPTION\n"));
2050 (void) printf("--- -----------------------------------------"
2051 "---------------\n");
2052 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2053 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2054 (void) printf(gettext(" 3 Case insensitive and filesystem "
2055 "user identifier (FUID)\n"));
2056 (void) printf(gettext(" 4 userquota, groupquota "
2058 (void) printf(gettext(" 5 System attributes\n"));
2059 (void) printf(gettext("\nFor more information on a particular "
2060 "version, including supported releases,\n"));
2061 (void) printf("see the ZFS Administration Guide.\n\n");
2063 } else if (argc || all) {
2064 /* Upgrade filesystems */
2065 if (cb.cb_version == 0)
2066 cb.cb_version = ZPL_VERSION;
2067 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2068 NULL, NULL, 0, upgrade_set_callback, &cb);
2069 (void) printf(gettext("%llu filesystems upgraded\n"),
2071 if (cb.cb_numsamegraded) {
2072 (void) printf(gettext("%llu filesystems already at "
2074 cb.cb_numsamegraded);
2076 if (cb.cb_numfailed != 0)
2079 /* List old-version filesytems */
2081 (void) printf(gettext("This system is currently running "
2082 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2084 flags |= ZFS_ITER_RECURSE;
2085 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2086 NULL, NULL, 0, upgrade_list_callback, &cb);
2088 found = cb.cb_foundone;
2089 cb.cb_foundone = B_FALSE;
2090 cb.cb_newer = B_TRUE;
2092 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2093 NULL, NULL, 0, upgrade_list_callback, &cb);
2095 if (!cb.cb_foundone && !found) {
2096 (void) printf(gettext("All filesystems are "
2097 "formatted with the current version.\n"));
2105 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2106 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2107 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2108 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2110 * -H Scripted mode; elide headers and separate columns by tabs.
2111 * -i Translate SID to POSIX ID.
2112 * -n Print numeric ID instead of user/group name.
2113 * -o Control which fields to display.
2114 * -p Use exact (parseable) numeric output.
2115 * -s Specify sort columns, descending order.
2116 * -S Specify sort columns, ascending order.
2117 * -t Control which object types to display.
2119 * Displays space consumed by, and quotas on, each user in the specified
2120 * filesystem or snapshot.
2123 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2124 enum us_field_types {
2130 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2131 static char *us_field_names[] = { "type", "name", "used", "quota" };
2132 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2134 #define USTYPE_PSX_GRP (1 << 0)
2135 #define USTYPE_PSX_USR (1 << 1)
2136 #define USTYPE_SMB_GRP (1 << 2)
2137 #define USTYPE_SMB_USR (1 << 3)
2138 #define USTYPE_ALL \
2139 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2141 static int us_type_bits[] = {
2148 static char *us_type_names[] = { "posixgroup", "posxiuser", "smbgroup",
2151 typedef struct us_node {
2153 uu_avl_node_t usn_avlnode;
2154 uu_list_node_t usn_listnode;
2157 typedef struct us_cbdata {
2159 uu_avl_pool_t *cb_avl_pool;
2161 boolean_t cb_numname;
2162 boolean_t cb_nicenum;
2163 boolean_t cb_sid2posix;
2164 zfs_userquota_prop_t cb_prop;
2165 zfs_sort_column_t *cb_sortcol;
2166 size_t cb_width[USFIELD_LAST];
2169 static boolean_t us_populated = B_FALSE;
2172 zfs_sort_column_t *si_sortcol;
2173 boolean_t si_numname;
2177 us_field_index(char *field)
2181 for (i = 0; i < USFIELD_LAST; i++) {
2182 if (strcmp(field, us_field_names[i]) == 0)
2190 us_compare(const void *larg, const void *rarg, void *unused)
2192 const us_node_t *l = larg;
2193 const us_node_t *r = rarg;
2194 us_sort_info_t *si = (us_sort_info_t *)unused;
2195 zfs_sort_column_t *sortcol = si->si_sortcol;
2196 boolean_t numname = si->si_numname;
2197 nvlist_t *lnvl = l->usn_nvl;
2198 nvlist_t *rnvl = r->usn_nvl;
2202 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2209 zfs_prop_t prop = sortcol->sc_prop;
2210 const char *propname = NULL;
2211 boolean_t reverse = sortcol->sc_reverse;
2216 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2217 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2219 rc = (rv32 < lv32) ? 1 : -1;
2224 (void) nvlist_lookup_uint64(lnvl, propname,
2226 (void) nvlist_lookup_uint64(rnvl, propname,
2229 rc = (rv64 < lv64) ? 1 : -1;
2231 (void) nvlist_lookup_string(lnvl, propname,
2233 (void) nvlist_lookup_string(rnvl, propname,
2235 rc = strcmp(lvstr, rvstr);
2239 case ZFS_PROP_QUOTA:
2242 if (prop == ZFS_PROP_USED)
2246 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2247 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2249 rc = (rv64 < lv64) ? 1 : -1;
2255 return (reverse ? 1 : -1);
2257 return (reverse ? -1 : 1);
2262 * If entries still seem to be the same, check if they are of the same
2263 * type (smbentity is added only if we are doing SID to POSIX ID
2264 * translation where we can have duplicate type/name combinations).
2266 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2267 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2269 return (lvb < rvb ? -1 : 1);
2274 static inline const char *
2275 us_type2str(unsigned field_type)
2277 switch (field_type) {
2278 case USTYPE_PSX_USR:
2279 return ("POSIX User");
2280 case USTYPE_PSX_GRP:
2281 return ("POSIX Group");
2282 case USTYPE_SMB_USR:
2283 return ("SMB User");
2284 case USTYPE_SMB_GRP:
2285 return ("SMB Group");
2287 return ("Undefined");
2292 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2294 us_cbdata_t *cb = (us_cbdata_t *)arg;
2295 zfs_userquota_prop_t prop = cb->cb_prop;
2300 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2301 uu_avl_t *avl = cb->cb_avl;
2305 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2307 const char *typestr;
2311 int typeidx, nameidx, sizeidx;
2312 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2313 boolean_t smbentity = B_FALSE;
2315 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2317 node = safe_malloc(sizeof (us_node_t));
2318 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2319 node->usn_nvl = props;
2321 if (domain != NULL && domain[0] != '\0') {
2323 char sid[ZFS_MAXNAMELEN + 32];
2328 directory_error_t e;
2333 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2335 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2336 type = USTYPE_SMB_GRP;
2338 err = sid_to_id(sid, B_FALSE, &id);
2341 type = USTYPE_SMB_USR;
2343 err = sid_to_id(sid, B_TRUE, &id);
2350 if (!cb->cb_sid2posix) {
2351 e = directory_name_from_sid(NULL, sid, &name,
2354 directory_error_free(e);
2362 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2364 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2365 type = USTYPE_PSX_GRP;
2366 if (!cb->cb_numname) {
2369 if ((g = getgrgid(rid)) != NULL)
2373 type = USTYPE_PSX_USR;
2374 if (!cb->cb_numname) {
2377 if ((p = getpwuid(rid)) != NULL)
2384 * Make sure that the type/name combination is unique when doing
2385 * SID to POSIX ID translation (hence changing the type from SMB to
2388 if (cb->cb_sid2posix &&
2389 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2392 /* Calculate/update width of TYPE field */
2393 typestr = us_type2str(type);
2394 typelen = strlen(gettext(typestr));
2395 typeidx = us_field_index("type");
2396 if (typelen > cb->cb_width[typeidx])
2397 cb->cb_width[typeidx] = typelen;
2398 if (nvlist_add_uint32(props, "type", type) != 0)
2401 /* Calculate/update width of NAME field */
2402 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2403 if (nvlist_add_uint64(props, "name", rid) != 0)
2405 namelen = snprintf(NULL, 0, "%u", rid);
2407 if (nvlist_add_string(props, "name", name) != 0)
2409 namelen = strlen(name);
2411 nameidx = us_field_index("name");
2412 if (namelen > cb->cb_width[nameidx])
2413 cb->cb_width[nameidx] = namelen;
2416 * Check if this type/name combination is in the list and update it;
2417 * otherwise add new node to the list.
2419 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2420 uu_avl_insert(avl, node, idx);
2425 props = node->usn_nvl;
2428 /* Calculate/update width of USED/QUOTA fields */
2430 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2432 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2433 sizelen = strlen(sizebuf);
2434 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2436 if (!nvlist_exists(props, "quota"))
2437 (void) nvlist_add_uint64(props, "quota", 0);
2440 if (!nvlist_exists(props, "used"))
2441 (void) nvlist_add_uint64(props, "used", 0);
2443 sizeidx = us_field_index(propname);
2444 if (sizelen > cb->cb_width[sizeidx])
2445 cb->cb_width[sizeidx] = sizelen;
2447 if (nvlist_add_uint64(props, propname, space) != 0)
2454 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2455 size_t *width, us_node_t *node)
2457 nvlist_t *nvl = node->usn_nvl;
2458 char valstr[ZFS_MAXNAMELEN];
2459 boolean_t first = B_TRUE;
2465 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2466 if (!(ustype & types))
2469 while ((field = fields[cfield]) != USFIELD_LAST) {
2470 nvpair_t *nvp = NULL;
2474 char *strval = NULL;
2476 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2477 if (strcmp(nvpair_name(nvp),
2478 us_field_names[field]) == 0)
2482 type = nvpair_type(nvp);
2484 case DATA_TYPE_UINT32:
2485 (void) nvpair_value_uint32(nvp, &val32);
2487 case DATA_TYPE_UINT64:
2488 (void) nvpair_value_uint64(nvp, &val64);
2490 case DATA_TYPE_STRING:
2491 (void) nvpair_value_string(nvp, &strval);
2494 (void) fprintf(stderr, "invalid data type\n");
2499 strval = (char *)us_type2str(val32);
2502 if (type == DATA_TYPE_UINT64) {
2503 (void) sprintf(valstr, "%llu", val64);
2509 if (type == DATA_TYPE_UINT64) {
2511 (void) sprintf(valstr, "%llu", val64);
2513 zfs_nicenum(val64, valstr,
2516 if (field == USFIELD_QUOTA &&
2517 strcmp(valstr, "0") == 0)
2527 (void) printf("\t");
2532 (void) printf("%s", strval);
2533 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2534 (void) printf("%-*s", width[field], strval);
2536 (void) printf("%*s", width[field], strval);
2542 (void) printf("\n");
2546 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2547 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2555 boolean_t first = B_TRUE;
2557 while ((field = fields[cfield]) != USFIELD_LAST) {
2558 col = gettext(us_field_hdr[field]);
2559 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2560 (void) printf(first ? "%-*s" : " %-*s",
2563 (void) printf(first ? "%*s" : " %*s",
2569 (void) printf("\n");
2572 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2573 print_us_node(scripted, parsable, fields, types, width, node);
2575 nvlist_free(node->usn_nvl);
2580 zfs_do_userspace(int argc, char **argv)
2583 zfs_userquota_prop_t p;
2585 uu_avl_pool_t *avl_pool;
2587 uu_avl_walk_t *walk;
2589 char deffields[] = "type,name,used,quota";
2590 char *ofield = NULL;
2591 char *tfield = NULL;
2595 boolean_t scripted = B_FALSE;
2596 boolean_t prtnum = B_FALSE;
2597 boolean_t parsable = B_FALSE;
2598 boolean_t sid2posix = B_FALSE;
2601 zfs_sort_column_t *sortcol = NULL;
2602 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2606 uu_list_pool_t *listpool;
2608 uu_avl_index_t idx = 0;
2609 uu_list_index_t idx2 = 0;
2614 if (strcmp(argv[0], "groupspace") == 0)
2615 /* Toggle default group types */
2616 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2618 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2634 if (zfs_add_sort_column(&sortcol, optarg,
2635 c == 's' ? B_FALSE : B_TRUE) != 0) {
2636 (void) fprintf(stderr,
2637 gettext("invalid field '%s'\n"), optarg);
2648 (void) fprintf(stderr, gettext("missing argument for "
2649 "'%c' option\n"), optopt);
2653 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2663 (void) fprintf(stderr, gettext("missing dataset name\n"));
2667 (void) fprintf(stderr, gettext("too many arguments\n"));
2671 /* Use default output fields if not specified using -o */
2675 if ((delim = strchr(ofield, ',')) != NULL)
2677 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2678 (void) fprintf(stderr, gettext("invalid type '%s' "
2679 "for -o option\n"), ofield);
2684 } while (delim != NULL);
2685 fields[cfield] = USFIELD_LAST;
2687 /* Override output types (-t option) */
2688 if (tfield != NULL) {
2692 boolean_t found = B_FALSE;
2694 if ((delim = strchr(tfield, ',')) != NULL)
2696 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2698 if (strcmp(tfield, us_type_names[i]) == 0) {
2700 types |= us_type_bits[i];
2705 (void) fprintf(stderr, gettext("invalid type "
2706 "'%s' for -t option\n"), tfield);
2711 } while (delim != NULL);
2714 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2717 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2718 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2720 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2723 /* Always add default sorting columns */
2724 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2725 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2727 cb.cb_sortcol = sortcol;
2728 cb.cb_numname = prtnum;
2729 cb.cb_nicenum = !parsable;
2730 cb.cb_avl_pool = avl_pool;
2731 cb.cb_avl = avl_tree;
2732 cb.cb_sid2posix = sid2posix;
2734 for (i = 0; i < USFIELD_LAST; i++)
2735 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2737 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2738 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2739 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2740 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2741 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2744 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2749 if ((node = uu_avl_first(avl_tree)) == NULL)
2752 us_populated = B_TRUE;
2754 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2755 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2756 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2757 uu_list_node_init(node, &node->usn_listnode, listpool);
2759 while (node != NULL) {
2761 node = uu_avl_next(avl_tree, node);
2762 uu_avl_remove(avl_tree, rmnode);
2763 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2764 uu_list_insert(list, rmnode, idx2);
2767 for (node = uu_list_first(list); node != NULL;
2768 node = uu_list_next(list, node)) {
2769 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2771 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2772 uu_avl_insert(avl_tree, node, idx);
2775 uu_list_destroy(list);
2776 uu_list_pool_destroy(listpool);
2778 /* Print and free node nvlist memory */
2779 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2782 zfs_free_sort_columns(sortcol);
2784 /* Clean up the AVL tree */
2785 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2788 while ((node = uu_avl_walk_next(walk)) != NULL) {
2789 uu_avl_remove(cb.cb_avl, node);
2793 uu_avl_walk_end(walk);
2794 uu_avl_destroy(avl_tree);
2795 uu_avl_pool_destroy(avl_pool);
2801 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2802 * [-s property [-s property]...] [-S property [-S property]...]
2805 * -r Recurse over all children
2806 * -d Limit recursion by depth.
2807 * -H Scripted mode; elide headers and separate columns by tabs
2808 * -o Control which fields to display.
2809 * -t Control which object types to display.
2810 * -s Specify sort columns, descending order.
2811 * -S Specify sort columns, ascending order.
2813 * When given no arguments, lists all filesystems in the system.
2814 * Otherwise, list the specified datasets, optionally recursing down them if
2815 * '-r' is specified.
2817 typedef struct list_cbdata {
2819 boolean_t cb_scripted;
2820 zprop_list_t *cb_proplist;
2824 * Given a list of columns to display, output appropriate headers for each one.
2827 print_header(zprop_list_t *pl)
2829 char headerbuf[ZFS_MAXPROPLEN];
2832 boolean_t first = B_TRUE;
2833 boolean_t right_justify;
2835 for (; pl != NULL; pl = pl->pl_next) {
2842 right_justify = B_FALSE;
2843 if (pl->pl_prop != ZPROP_INVAL) {
2844 header = zfs_prop_column_name(pl->pl_prop);
2845 right_justify = zfs_prop_align_right(pl->pl_prop);
2847 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2848 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2849 headerbuf[i] = '\0';
2853 if (pl->pl_next == NULL && !right_justify)
2854 (void) printf("%s", header);
2855 else if (right_justify)
2856 (void) printf("%*s", pl->pl_width, header);
2858 (void) printf("%-*s", pl->pl_width, header);
2861 (void) printf("\n");
2865 * Given a dataset and a list of fields, print out all the properties according
2866 * to the described layout.
2869 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
2871 boolean_t first = B_TRUE;
2872 char property[ZFS_MAXPROPLEN];
2873 nvlist_t *userprops = zfs_get_user_props(zhp);
2876 boolean_t right_justify;
2879 for (; pl != NULL; pl = pl->pl_next) {
2882 (void) printf("\t");
2889 if (pl->pl_prop == ZFS_PROP_NAME) {
2890 (void) strlcpy(property, zfs_get_name(zhp),
2893 right_justify = zfs_prop_align_right(pl->pl_prop);
2894 } else if (pl->pl_prop != ZPROP_INVAL) {
2895 if (zfs_prop_get(zhp, pl->pl_prop, property,
2896 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2901 right_justify = zfs_prop_align_right(pl->pl_prop);
2902 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2903 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2904 property, sizeof (property), B_FALSE) != 0)
2908 right_justify = B_TRUE;
2909 } else if (zfs_prop_written(pl->pl_user_prop)) {
2910 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2911 property, sizeof (property), B_FALSE) != 0)
2915 right_justify = B_TRUE;
2917 if (nvlist_lookup_nvlist(userprops,
2918 pl->pl_user_prop, &propval) != 0)
2921 verify(nvlist_lookup_string(propval,
2922 ZPROP_VALUE, &propstr) == 0);
2923 right_justify = B_FALSE;
2926 width = pl->pl_width;
2929 * If this is being called in scripted mode, or if this is the
2930 * last column and it is left-justified, don't include a width
2933 if (scripted || (pl->pl_next == NULL && !right_justify))
2934 (void) printf("%s", propstr);
2935 else if (right_justify)
2936 (void) printf("%*s", width, propstr);
2938 (void) printf("%-*s", width, propstr);
2941 (void) printf("\n");
2945 * Generic callback function to list a dataset or snapshot.
2948 list_callback(zfs_handle_t *zhp, void *data)
2950 list_cbdata_t *cbp = data;
2952 if (cbp->cb_first) {
2953 if (!cbp->cb_scripted)
2954 print_header(cbp->cb_proplist);
2955 cbp->cb_first = B_FALSE;
2958 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2964 zfs_do_list(int argc, char **argv)
2967 boolean_t scripted = B_FALSE;
2968 static char default_fields[] =
2969 "name,used,available,referenced,mountpoint";
2970 int types = ZFS_TYPE_DATASET;
2971 boolean_t types_specified = B_FALSE;
2972 char *fields = NULL;
2973 list_cbdata_t cb = { 0 };
2977 zfs_sort_column_t *sortcol = NULL;
2978 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2981 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2987 limit = parse_depth(optarg, &flags);
2990 flags |= ZFS_ITER_RECURSE;
2996 if (zfs_add_sort_column(&sortcol, optarg,
2998 (void) fprintf(stderr,
2999 gettext("invalid property '%s'\n"), optarg);
3004 if (zfs_add_sort_column(&sortcol, optarg,
3006 (void) fprintf(stderr,
3007 gettext("invalid property '%s'\n"), optarg);
3013 types_specified = B_TRUE;
3014 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3015 while (*optarg != '\0') {
3016 static char *type_subopts[] = { "filesystem",
3017 "volume", "snapshot", "all", NULL };
3019 switch (getsubopt(&optarg, type_subopts,
3022 types |= ZFS_TYPE_FILESYSTEM;
3025 types |= ZFS_TYPE_VOLUME;
3028 types |= ZFS_TYPE_SNAPSHOT;
3031 types = ZFS_TYPE_DATASET;
3035 (void) fprintf(stderr,
3036 gettext("invalid type '%s'\n"),
3043 (void) fprintf(stderr, gettext("missing argument for "
3044 "'%c' option\n"), optopt);
3048 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3058 fields = default_fields;
3061 * If we are only going to list snapshot names and sort by name,
3062 * then we can use faster version.
3064 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3065 flags |= ZFS_ITER_SIMPLE;
3068 * If "-o space" and no types were specified, don't display snapshots.
3070 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3071 types &= ~ZFS_TYPE_SNAPSHOT;
3074 * If the user specifies '-o all', the zprop_get_list() doesn't
3075 * normally include the name of the dataset. For 'zfs list', we always
3076 * want this property to be first.
3078 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3082 cb.cb_scripted = scripted;
3083 cb.cb_first = B_TRUE;
3085 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3086 limit, list_callback, &cb);
3088 zprop_free_list(cb.cb_proplist);
3089 zfs_free_sort_columns(sortcol);
3091 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3092 (void) printf(gettext("no datasets available\n"));
3098 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3099 * zfs rename [-f] -p <fs | vol> <fs | vol>
3100 * zfs rename -r <snap> <snap>
3101 * zfs rename -u [-p] <fs> <fs>
3103 * Renames the given dataset to another of the same type.
3105 * The '-p' flag creates all the non-existing ancestors of the target first.
3109 zfs_do_rename(int argc, char **argv)
3112 renameflags_t flags = { 0 };
3116 boolean_t parents = B_FALSE;
3117 char *snapshot = NULL;
3120 while ((c = getopt(argc, argv, "fpru")) != -1) {
3126 flags.recurse = B_TRUE;
3129 flags.nounmount = B_TRUE;
3132 flags.forceunmount = B_TRUE;
3136 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3145 /* check number of arguments */
3147 (void) fprintf(stderr, gettext("missing source dataset "
3152 (void) fprintf(stderr, gettext("missing target dataset "
3157 (void) fprintf(stderr, gettext("too many arguments\n"));
3161 if (flags.recurse && parents) {
3162 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3167 if (flags.recurse && strchr(argv[0], '@') == 0) {
3168 (void) fprintf(stderr, gettext("source dataset for recursive "
3169 "rename must be a snapshot\n"));
3173 if (flags.nounmount && parents) {
3174 (void) fprintf(stderr, gettext("-u and -r options are mutually "
3179 if (flags.nounmount)
3180 types = ZFS_TYPE_FILESYSTEM;
3182 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3184 types = ZFS_TYPE_DATASET;
3186 if (flags.recurse) {
3188 * When we do recursive rename we are fine when the given
3189 * snapshot for the given dataset doesn't exist - it can
3190 * still exists below.
3193 snapshot = strchr(argv[0], '@');
3194 assert(snapshot != NULL);
3199 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3202 /* If we were asked and the name looks good, try to create ancestors. */
3203 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3204 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3209 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3218 * Promotes the given clone fs to be the parent
3222 zfs_do_promote(int argc, char **argv)
3228 if (argc > 1 && argv[1][0] == '-') {
3229 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3234 /* check number of arguments */
3236 (void) fprintf(stderr, gettext("missing clone filesystem"
3241 (void) fprintf(stderr, gettext("too many arguments\n"));
3245 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3249 ret = (zfs_promote(zhp) != 0);
3257 * zfs rollback [-rRf] <snapshot>
3259 * -r Delete any intervening snapshots before doing rollback
3260 * -R Delete any snapshots and their clones
3261 * -f ignored for backwards compatability
3263 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3264 * since then and making it the active dataset. If more recent snapshots exist,
3265 * the command will complain unless the '-r' flag is given.
3267 typedef struct rollback_cbdata {
3273 boolean_t cb_recurse;
3274 boolean_t cb_dependent;
3275 } rollback_cbdata_t;
3278 * Report any snapshots more recent than the one specified. Used when '-r' is
3279 * not specified. We reuse this same callback for the snapshot dependents - if
3280 * 'cb_dependent' is set, then this is a dependent and we should report it
3281 * without checking the transaction group.
3284 rollback_check(zfs_handle_t *zhp, void *data)
3286 rollback_cbdata_t *cbp = data;
3288 if (cbp->cb_doclones) {
3293 if (!cbp->cb_dependent) {
3294 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
3295 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3296 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3299 if (cbp->cb_first && !cbp->cb_recurse) {
3300 (void) fprintf(stderr, gettext("cannot "
3301 "rollback to '%s': more recent snapshots "
3304 (void) fprintf(stderr, gettext("use '-r' to "
3305 "force deletion of the following "
3311 if (cbp->cb_recurse) {
3312 cbp->cb_dependent = B_TRUE;
3313 if (zfs_iter_dependents(zhp, B_TRUE,
3314 rollback_check, cbp) != 0) {
3318 cbp->cb_dependent = B_FALSE;
3320 (void) fprintf(stderr, "%s\n",
3325 if (cbp->cb_first && cbp->cb_recurse) {
3326 (void) fprintf(stderr, gettext("cannot rollback to "
3327 "'%s': clones of previous snapshots exist\n"),
3329 (void) fprintf(stderr, gettext("use '-R' to "
3330 "force deletion of the following clones and "
3336 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3344 zfs_do_rollback(int argc, char **argv)
3348 boolean_t force = B_FALSE;
3349 rollback_cbdata_t cb = { 0 };
3350 zfs_handle_t *zhp, *snap;
3351 char parentname[ZFS_MAXNAMELEN];
3355 while ((c = getopt(argc, argv, "rRf")) != -1) {
3368 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3377 /* check number of arguments */
3379 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3383 (void) fprintf(stderr, gettext("too many arguments\n"));
3387 /* open the snapshot */
3388 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3391 /* open the parent dataset */
3392 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3393 verify((delim = strrchr(parentname, '@')) != NULL);
3395 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3401 * Check for more recent snapshots and/or clones based on the presence
3404 cb.cb_target = argv[0];
3405 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3406 cb.cb_first = B_TRUE;
3408 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
3411 if ((ret = cb.cb_error) != 0)
3415 * Rollback parent to the given snapshot.
3417 ret = zfs_rollback(zhp, snap, force);
3430 * zfs set property=value { fs | snap | vol } ...
3432 * Sets the given property for all datasets specified on the command line.
3434 typedef struct set_cbdata {
3440 set_callback(zfs_handle_t *zhp, void *data)
3442 set_cbdata_t *cbp = data;
3444 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3445 switch (libzfs_errno(g_zfs)) {
3446 case EZFS_MOUNTFAILED:
3447 (void) fprintf(stderr, gettext("property may be set "
3448 "but unable to remount filesystem\n"));
3450 case EZFS_SHARENFSFAILED:
3451 (void) fprintf(stderr, gettext("property may be set "
3452 "but unable to reshare filesystem\n"));
3461 zfs_do_set(int argc, char **argv)
3466 /* check for options */
3467 if (argc > 1 && argv[1][0] == '-') {
3468 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3473 /* check number of arguments */
3475 (void) fprintf(stderr, gettext("missing property=value "
3480 (void) fprintf(stderr, gettext("missing dataset name\n"));
3484 /* validate property=value argument */
3485 cb.cb_propname = argv[1];
3486 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3487 (cb.cb_value[1] == '\0')) {
3488 (void) fprintf(stderr, gettext("missing value in "
3489 "property=value argument\n"));
3493 *cb.cb_value = '\0';
3496 if (*cb.cb_propname == '\0') {
3497 (void) fprintf(stderr,
3498 gettext("missing property in property=value argument\n"));
3502 ret = zfs_for_each(argc - 2, argv + 2, 0,
3503 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3508 typedef struct snap_cbdata {
3510 boolean_t sd_recursive;
3511 const char *sd_snapname;
3515 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3517 snap_cbdata_t *sd = arg;
3522 if (sd->sd_recursive &&
3523 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3528 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3531 fnvlist_add_boolean(sd->sd_nvl, name);
3534 if (sd->sd_recursive)
3535 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3541 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3543 * Creates a snapshot with the given name. While functionally equivalent to
3544 * 'zfs create', it is a separate command to differentiate intent.
3547 zfs_do_snapshot(int argc, char **argv)
3552 snap_cbdata_t sd = { 0 };
3553 boolean_t multiple_snaps = B_FALSE;
3555 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3557 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3561 while ((c = getopt(argc, argv, "ro:")) != -1) {
3564 if (parseprop(props))
3568 sd.sd_recursive = B_TRUE;
3569 multiple_snaps = B_TRUE;
3572 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3581 /* check number of arguments */
3583 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3588 multiple_snaps = B_TRUE;
3589 for (; argc > 0; argc--, argv++) {
3593 atp = strchr(argv[0], '@');
3597 sd.sd_snapname = atp + 1;
3598 zhp = zfs_open(g_zfs, argv[0],
3599 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3602 if (zfs_snapshot_cb(zhp, &sd) != 0)
3606 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3607 nvlist_free(sd.sd_nvl);
3609 if (ret != 0 && multiple_snaps)
3610 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3614 nvlist_free(sd.sd_nvl);
3621 * Send a backup stream to stdout.
3624 zfs_do_send(int argc, char **argv)
3626 char *fromname = NULL;
3627 char *toname = NULL;
3630 sendflags_t flags = { 0 };
3632 nvlist_t *dbgnv = NULL;
3633 boolean_t extraverbose = B_FALSE;
3636 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3647 flags.doall = B_TRUE;
3650 flags.replicate = B_TRUE;
3653 flags.props = B_TRUE;
3656 flags.parsable = B_TRUE;
3657 flags.verbose = B_TRUE;
3661 extraverbose = B_TRUE;
3662 flags.verbose = B_TRUE;
3663 flags.progress = B_TRUE;
3666 flags.dedup = B_TRUE;
3669 flags.dryrun = B_TRUE;
3672 (void) fprintf(stderr, gettext("missing argument for "
3673 "'%c' option\n"), optopt);
3677 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3686 /* check number of arguments */
3688 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3692 (void) fprintf(stderr, gettext("too many arguments\n"));
3696 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3697 (void) fprintf(stderr,
3698 gettext("Error: Stream can not be written to a terminal.\n"
3699 "You must redirect standard output.\n"));
3703 cp = strchr(argv[0], '@');
3705 (void) fprintf(stderr,
3706 gettext("argument must be a snapshot\n"));
3711 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3716 * If they specified the full path to the snapshot, chop off
3717 * everything except the short name of the snapshot, but special
3718 * case if they specify the origin.
3720 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3721 char origin[ZFS_MAXNAMELEN];
3724 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3725 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3727 if (strcmp(origin, fromname) == 0) {
3729 flags.fromorigin = B_TRUE;
3732 if (cp != fromname && strcmp(argv[0], fromname)) {
3733 (void) fprintf(stderr,
3734 gettext("incremental source must be "
3735 "in same filesystem\n"));
3739 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3740 (void) fprintf(stderr,
3741 gettext("invalid incremental source\n"));
3747 if (flags.replicate && fromname == NULL)
3748 flags.doall = B_TRUE;
3750 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3751 extraverbose ? &dbgnv : NULL);
3753 if (extraverbose && dbgnv != NULL) {
3755 * dump_nvlist prints to stdout, but that's been
3756 * redirected to a file. Make it print to stderr
3759 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3760 dump_nvlist(dbgnv, 0);
3769 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3771 * Restore a backup stream from stdin.
3774 zfs_do_receive(int argc, char **argv)
3777 recvflags_t flags = { 0 };
3780 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3783 flags.isprefix = B_TRUE;
3786 flags.isprefix = B_TRUE;
3787 flags.istail = B_TRUE;
3790 flags.dryrun = B_TRUE;
3793 flags.nomount = B_TRUE;
3796 flags.verbose = B_TRUE;
3799 flags.force = B_TRUE;
3802 (void) fprintf(stderr, gettext("missing argument for "
3803 "'%c' option\n"), optopt);
3807 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3816 /* check number of arguments */
3818 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3822 (void) fprintf(stderr, gettext("too many arguments\n"));
3826 if (isatty(STDIN_FILENO)) {
3827 (void) fprintf(stderr,
3828 gettext("Error: Backup stream can not be read "
3829 "from a terminal.\n"
3830 "You must redirect standard input.\n"));
3834 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3840 * allow/unallow stuff
3842 /* copied from zfs/sys/dsl_deleg.h */
3843 #define ZFS_DELEG_PERM_CREATE "create"
3844 #define ZFS_DELEG_PERM_DESTROY "destroy"
3845 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3846 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3847 #define ZFS_DELEG_PERM_CLONE "clone"
3848 #define ZFS_DELEG_PERM_PROMOTE "promote"
3849 #define ZFS_DELEG_PERM_RENAME "rename"
3850 #define ZFS_DELEG_PERM_MOUNT "mount"
3851 #define ZFS_DELEG_PERM_SHARE "share"
3852 #define ZFS_DELEG_PERM_SEND "send"
3853 #define ZFS_DELEG_PERM_RECEIVE "receive"
3854 #define ZFS_DELEG_PERM_ALLOW "allow"
3855 #define ZFS_DELEG_PERM_USERPROP "userprop"
3856 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3857 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3858 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3859 #define ZFS_DELEG_PERM_USERUSED "userused"
3860 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3861 #define ZFS_DELEG_PERM_HOLD "hold"
3862 #define ZFS_DELEG_PERM_RELEASE "release"
3863 #define ZFS_DELEG_PERM_DIFF "diff"
3865 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3867 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3868 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3869 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3870 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3871 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3872 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3873 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3874 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3875 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3876 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3877 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3878 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3879 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3880 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3881 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3882 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3884 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3885 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3886 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3887 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3888 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3889 { NULL, ZFS_DELEG_NOTE_NONE }
3892 /* permission structure */
3893 typedef struct deleg_perm {
3894 zfs_deleg_who_type_t dp_who_type;
3895 const char *dp_name;
3897 boolean_t dp_descend;
3901 typedef struct deleg_perm_node {
3902 deleg_perm_t dpn_perm;
3904 uu_avl_node_t dpn_avl_node;
3905 } deleg_perm_node_t;
3907 typedef struct fs_perm fs_perm_t;
3909 /* permissions set */
3910 typedef struct who_perm {
3911 zfs_deleg_who_type_t who_type;
3912 const char *who_name; /* id */
3913 char who_ug_name[256]; /* user/group name */
3914 fs_perm_t *who_fsperm; /* uplink */
3916 uu_avl_t *who_deleg_perm_avl; /* permissions */
3920 typedef struct who_perm_node {
3921 who_perm_t who_perm;
3922 uu_avl_node_t who_avl_node;
3925 typedef struct fs_perm_set fs_perm_set_t;
3926 /* fs permissions */
3928 const char *fsp_name;
3930 uu_avl_t *fsp_sc_avl; /* sets,create */
3931 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3933 fs_perm_set_t *fsp_set; /* uplink */
3937 typedef struct fs_perm_node {
3938 fs_perm_t fspn_fsperm;
3941 uu_list_node_t fspn_list_node;
3944 /* top level structure */
3945 struct fs_perm_set {
3946 uu_list_pool_t *fsps_list_pool;
3947 uu_list_t *fsps_list; /* list of fs_perms */
3949 uu_avl_pool_t *fsps_named_set_avl_pool;
3950 uu_avl_pool_t *fsps_who_perm_avl_pool;
3951 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
3954 static inline const char *
3955 deleg_perm_type(zfs_deleg_note_t note)
3961 case ZFS_DELEG_NOTE_GROUPQUOTA:
3962 case ZFS_DELEG_NOTE_GROUPUSED:
3963 case ZFS_DELEG_NOTE_USERPROP:
3964 case ZFS_DELEG_NOTE_USERQUOTA:
3965 case ZFS_DELEG_NOTE_USERUSED:
3967 return (gettext("other"));
3969 return (gettext("subcommand"));
3974 who_type2weight(zfs_deleg_who_type_t who_type)
3978 case ZFS_DELEG_NAMED_SET_SETS:
3979 case ZFS_DELEG_NAMED_SET:
3982 case ZFS_DELEG_CREATE_SETS:
3983 case ZFS_DELEG_CREATE:
3986 case ZFS_DELEG_USER_SETS:
3987 case ZFS_DELEG_USER:
3990 case ZFS_DELEG_GROUP_SETS:
3991 case ZFS_DELEG_GROUP:
3994 case ZFS_DELEG_EVERYONE_SETS:
3995 case ZFS_DELEG_EVERYONE:
4007 who_perm_compare(const void *larg, const void *rarg, void *unused)
4009 const who_perm_node_t *l = larg;
4010 const who_perm_node_t *r = rarg;
4011 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4012 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4013 int lweight = who_type2weight(ltype);
4014 int rweight = who_type2weight(rtype);
4015 int res = lweight - rweight;
4017 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4018 ZFS_MAX_DELEG_NAME-1);
4030 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4032 const deleg_perm_node_t *l = larg;
4033 const deleg_perm_node_t *r = rarg;
4034 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4035 ZFS_MAX_DELEG_NAME-1);
4047 fs_perm_set_init(fs_perm_set_t *fspset)
4049 bzero(fspset, sizeof (fs_perm_set_t));
4051 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4052 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4053 NULL, UU_DEFAULT)) == NULL)
4055 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4056 UU_DEFAULT)) == NULL)
4059 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4060 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4061 who_perm_node_t, who_avl_node), who_perm_compare,
4062 UU_DEFAULT)) == NULL)
4065 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4066 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4067 who_perm_node_t, who_avl_node), who_perm_compare,
4068 UU_DEFAULT)) == NULL)
4071 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4072 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4073 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4078 static inline void fs_perm_fini(fs_perm_t *);
4079 static inline void who_perm_fini(who_perm_t *);
4082 fs_perm_set_fini(fs_perm_set_t *fspset)
4084 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4086 while (node != NULL) {
4087 fs_perm_node_t *next_node =
4088 uu_list_next(fspset->fsps_list, node);
4089 fs_perm_t *fsperm = &node->fspn_fsperm;
4090 fs_perm_fini(fsperm);
4091 uu_list_remove(fspset->fsps_list, node);
4096 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4097 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4098 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4102 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4105 deleg_perm->dp_who_type = type;
4106 deleg_perm->dp_name = name;
4110 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4111 zfs_deleg_who_type_t type, const char *name)
4113 uu_avl_pool_t *pool;
4114 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4116 bzero(who_perm, sizeof (who_perm_t));
4118 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4119 UU_DEFAULT)) == NULL)
4122 who_perm->who_type = type;
4123 who_perm->who_name = name;
4124 who_perm->who_fsperm = fsperm;
4128 who_perm_fini(who_perm_t *who_perm)
4130 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4132 while (node != NULL) {
4133 deleg_perm_node_t *next_node =
4134 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4136 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4141 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4145 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4147 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4148 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4150 bzero(fsperm, sizeof (fs_perm_t));
4152 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4156 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4160 fsperm->fsp_set = fspset;
4161 fsperm->fsp_name = fsname;
4165 fs_perm_fini(fs_perm_t *fsperm)
4167 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4168 while (node != NULL) {
4169 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4171 who_perm_t *who_perm = &node->who_perm;
4172 who_perm_fini(who_perm);
4173 uu_avl_remove(fsperm->fsp_sc_avl, node);
4178 node = uu_avl_first(fsperm->fsp_uge_avl);
4179 while (node != NULL) {
4180 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4182 who_perm_t *who_perm = &node->who_perm;
4183 who_perm_fini(who_perm);
4184 uu_avl_remove(fsperm->fsp_uge_avl, node);
4189 uu_avl_destroy(fsperm->fsp_sc_avl);
4190 uu_avl_destroy(fsperm->fsp_uge_avl);
4194 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4195 zfs_deleg_who_type_t who_type, const char *name, char locality)
4197 uu_avl_index_t idx = 0;
4199 deleg_perm_node_t *found_node = NULL;
4200 deleg_perm_t *deleg_perm = &node->dpn_perm;
4202 deleg_perm_init(deleg_perm, who_type, name);
4204 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4206 uu_avl_insert(avl, node, idx);
4209 deleg_perm = &node->dpn_perm;
4214 case ZFS_DELEG_LOCAL:
4215 deleg_perm->dp_local = B_TRUE;
4217 case ZFS_DELEG_DESCENDENT:
4218 deleg_perm->dp_descend = B_TRUE;
4223 assert(B_FALSE); /* invalid locality */
4228 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4230 nvpair_t *nvp = NULL;
4231 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4232 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4233 zfs_deleg_who_type_t who_type = who_perm->who_type;
4235 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4236 const char *name = nvpair_name(nvp);
4237 data_type_t type = nvpair_type(nvp);
4238 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4239 deleg_perm_node_t *node =
4240 safe_malloc(sizeof (deleg_perm_node_t));
4242 assert(type == DATA_TYPE_BOOLEAN);
4244 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4245 set_deleg_perm_node(avl, node, who_type, name, locality);
4252 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4254 nvpair_t *nvp = NULL;
4255 fs_perm_set_t *fspset = fsperm->fsp_set;
4257 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4258 nvlist_t *nvl2 = NULL;
4259 const char *name = nvpair_name(nvp);
4260 uu_avl_t *avl = NULL;
4261 uu_avl_pool_t *avl_pool;
4262 zfs_deleg_who_type_t perm_type = name[0];
4263 char perm_locality = name[1];
4264 const char *perm_name = name + 3;
4265 boolean_t is_set = B_TRUE;
4266 who_perm_t *who_perm = NULL;
4268 assert('$' == name[2]);
4270 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4273 switch (perm_type) {
4274 case ZFS_DELEG_CREATE:
4275 case ZFS_DELEG_CREATE_SETS:
4276 case ZFS_DELEG_NAMED_SET:
4277 case ZFS_DELEG_NAMED_SET_SETS:
4278 avl_pool = fspset->fsps_named_set_avl_pool;
4279 avl = fsperm->fsp_sc_avl;
4281 case ZFS_DELEG_USER:
4282 case ZFS_DELEG_USER_SETS:
4283 case ZFS_DELEG_GROUP:
4284 case ZFS_DELEG_GROUP_SETS:
4285 case ZFS_DELEG_EVERYONE:
4286 case ZFS_DELEG_EVERYONE_SETS:
4287 avl_pool = fspset->fsps_who_perm_avl_pool;
4288 avl = fsperm->fsp_uge_avl;
4293 who_perm_node_t *found_node = NULL;
4294 who_perm_node_t *node = safe_malloc(
4295 sizeof (who_perm_node_t));
4296 who_perm = &node->who_perm;
4297 uu_avl_index_t idx = 0;
4299 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4300 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4302 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4304 if (avl == fsperm->fsp_uge_avl) {
4306 struct passwd *p = NULL;
4307 struct group *g = NULL;
4308 const char *nice_name = NULL;
4310 switch (perm_type) {
4311 case ZFS_DELEG_USER_SETS:
4312 case ZFS_DELEG_USER:
4313 rid = atoi(perm_name);
4316 nice_name = p->pw_name;
4318 case ZFS_DELEG_GROUP_SETS:
4319 case ZFS_DELEG_GROUP:
4320 rid = atoi(perm_name);
4323 nice_name = g->gr_name;
4327 if (nice_name != NULL)
4329 node->who_perm.who_ug_name,
4333 uu_avl_insert(avl, node, idx);
4336 who_perm = &node->who_perm;
4340 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4347 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4349 nvpair_t *nvp = NULL;
4350 uu_avl_index_t idx = 0;
4352 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4353 nvlist_t *nvl2 = NULL;
4354 const char *fsname = nvpair_name(nvp);
4355 data_type_t type = nvpair_type(nvp);
4356 fs_perm_t *fsperm = NULL;
4357 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4361 fsperm = &node->fspn_fsperm;
4363 assert(DATA_TYPE_NVLIST == type);
4365 uu_list_node_init(node, &node->fspn_list_node,
4366 fspset->fsps_list_pool);
4368 idx = uu_list_numnodes(fspset->fsps_list);
4369 fs_perm_init(fsperm, fspset, fsname);
4371 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4374 (void) parse_fs_perm(fsperm, nvl2);
4376 uu_list_insert(fspset->fsps_list, node, idx);
4382 static inline const char *
4383 deleg_perm_comment(zfs_deleg_note_t note)
4385 const char *str = "";
4390 case ZFS_DELEG_NOTE_ALLOW:
4391 str = gettext("Must also have the permission that is being"
4392 "\n\t\t\t\tallowed");
4394 case ZFS_DELEG_NOTE_CLONE:
4395 str = gettext("Must also have the 'create' ability and 'mount'"
4396 "\n\t\t\t\tability in the origin file system");
4398 case ZFS_DELEG_NOTE_CREATE:
4399 str = gettext("Must also have the 'mount' ability");
4401 case ZFS_DELEG_NOTE_DESTROY:
4402 str = gettext("Must also have the 'mount' ability");
4404 case ZFS_DELEG_NOTE_DIFF:
4405 str = gettext("Allows lookup of paths within a dataset;"
4406 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4407 "\n\t\t\t\tin order to use zfs diff");
4409 case ZFS_DELEG_NOTE_HOLD:
4410 str = gettext("Allows adding a user hold to a snapshot");
4412 case ZFS_DELEG_NOTE_MOUNT:
4413 str = gettext("Allows mount/umount of ZFS datasets");
4415 case ZFS_DELEG_NOTE_PROMOTE:
4416 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4417 " 'promote' ability in the origin file system");
4419 case ZFS_DELEG_NOTE_RECEIVE:
4420 str = gettext("Must also have the 'mount' and 'create'"
4423 case ZFS_DELEG_NOTE_RELEASE:
4424 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4425 "might destroy the snapshot");
4427 case ZFS_DELEG_NOTE_RENAME:
4428 str = gettext("Must also have the 'mount' and 'create'"
4429 "\n\t\t\t\tability in the new parent");
4431 case ZFS_DELEG_NOTE_ROLLBACK:
4434 case ZFS_DELEG_NOTE_SEND:
4437 case ZFS_DELEG_NOTE_SHARE:
4438 str = gettext("Allows sharing file systems over NFS or SMB"
4439 "\n\t\t\t\tprotocols");
4441 case ZFS_DELEG_NOTE_SNAPSHOT:
4445 * case ZFS_DELEG_NOTE_VSCAN:
4446 * str = gettext("");
4450 case ZFS_DELEG_NOTE_GROUPQUOTA:
4451 str = gettext("Allows accessing any groupquota@... property");
4453 case ZFS_DELEG_NOTE_GROUPUSED:
4454 str = gettext("Allows reading any groupused@... property");
4456 case ZFS_DELEG_NOTE_USERPROP:
4457 str = gettext("Allows changing any user property");
4459 case ZFS_DELEG_NOTE_USERQUOTA:
4460 str = gettext("Allows accessing any userquota@... property");
4462 case ZFS_DELEG_NOTE_USERUSED:
4463 str = gettext("Allows reading any userused@... property");
4481 boolean_t recursive; /* unallow only */
4482 boolean_t prt_usage;
4484 boolean_t prt_perms;
4487 const char *dataset;
4491 prop_cmp(const void *a, const void *b)
4493 const char *str1 = *(const char **)a;
4494 const char *str2 = *(const char **)b;
4495 return (strcmp(str1, str2));
4499 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4501 const char *opt_desc[] = {
4502 "-h", gettext("show this help message and exit"),
4503 "-l", gettext("set permission locally"),
4504 "-d", gettext("set permission for descents"),
4505 "-u", gettext("set permission for user"),
4506 "-g", gettext("set permission for group"),
4507 "-e", gettext("set permission for everyone"),
4508 "-c", gettext("set create time permission"),
4509 "-s", gettext("define permission set"),
4511 "-r", gettext("remove permissions recursively"),
4513 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4514 size_t allow_size = unallow_size - 2;
4515 const char *props[ZFS_NUM_PROPS];
4518 FILE *fp = requested ? stdout : stderr;
4519 zprop_desc_t *pdtbl = zfs_prop_get_table();
4520 const char *fmt = gettext("%-16s %-14s\t%s\n");
4522 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4524 (void) fprintf(fp, gettext("Options:\n"));
4525 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4526 const char *opt = opt_desc[i++];
4527 const char *optdsc = opt_desc[i];
4528 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4531 (void) fprintf(fp, gettext("\nThe following permissions are "
4533 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4535 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4536 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4537 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4538 const char *perm_type = deleg_perm_type(perm_note);
4539 const char *perm_comment = deleg_perm_comment(perm_note);
4540 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4543 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4544 zprop_desc_t *pd = &pdtbl[i];
4545 if (pd->pd_visible != B_TRUE)
4548 if (pd->pd_attr == PROP_READONLY)
4551 props[count++] = pd->pd_name;
4553 props[count] = NULL;
4555 qsort(props, count, sizeof (char *), prop_cmp);
4557 for (i = 0; i < count; i++)
4558 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4561 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4563 exit(requested ? 0 : 2);
4566 static inline const char *
4567 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4570 if (un && argc == expected_argc - 1)
4572 else if (argc == expected_argc)
4573 *permsp = argv[argc - 2];
4575 allow_usage(un, B_FALSE,
4576 gettext("wrong number of parameters\n"));
4578 return (argv[argc - 1]);
4582 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4584 int uge_sum = opts->user + opts->group + opts->everyone;
4585 int csuge_sum = opts->create + opts->set + uge_sum;
4586 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4587 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4590 allow_usage(un, B_FALSE,
4591 gettext("-u, -g, and -e are mutually exclusive\n"));
4593 if (opts->prt_usage)
4594 if (argc == 0 && all_sum == 0)
4595 allow_usage(un, B_TRUE, NULL);
4601 allow_usage(un, B_FALSE,
4602 gettext("invalid options combined with -s\n"));
4604 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4605 if (argv[0][0] != '@')
4606 allow_usage(un, B_FALSE,
4607 gettext("invalid set name: missing '@' prefix\n"));
4608 opts->who = argv[0];
4609 } else if (opts->create) {
4610 if (ldcsuge_sum > 1)
4611 allow_usage(un, B_FALSE,
4612 gettext("invalid options combined with -c\n"));
4613 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4614 } else if (opts->everyone) {
4616 allow_usage(un, B_FALSE,
4617 gettext("invalid options combined with -e\n"));
4618 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4619 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4621 opts->everyone = B_TRUE;
4624 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4625 } else if (argc == 1 && !un) {
4626 opts->prt_perms = B_TRUE;
4627 opts->dataset = argv[argc-1];
4629 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4630 opts->who = argv[0];
4633 if (!opts->local && !opts->descend) {
4634 opts->local = B_TRUE;
4635 opts->descend = B_TRUE;
4640 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4641 const char *who, char *perms, nvlist_t *top_nvl)
4644 char ld[2] = { '\0', '\0' };
4645 char who_buf[ZFS_MAXNAMELEN+32];
4648 nvlist_t *base_nvl = NULL;
4649 nvlist_t *set_nvl = NULL;
4652 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4654 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4658 case ZFS_DELEG_NAMED_SET_SETS:
4659 case ZFS_DELEG_NAMED_SET:
4660 set_type = ZFS_DELEG_NAMED_SET_SETS;
4661 base_type = ZFS_DELEG_NAMED_SET;
4662 ld[0] = ZFS_DELEG_NA;
4664 case ZFS_DELEG_CREATE_SETS:
4665 case ZFS_DELEG_CREATE:
4666 set_type = ZFS_DELEG_CREATE_SETS;
4667 base_type = ZFS_DELEG_CREATE;
4668 ld[0] = ZFS_DELEG_NA;
4670 case ZFS_DELEG_USER_SETS:
4671 case ZFS_DELEG_USER:
4672 set_type = ZFS_DELEG_USER_SETS;
4673 base_type = ZFS_DELEG_USER;
4675 ld[0] = ZFS_DELEG_LOCAL;
4677 ld[1] = ZFS_DELEG_DESCENDENT;
4679 case ZFS_DELEG_GROUP_SETS:
4680 case ZFS_DELEG_GROUP:
4681 set_type = ZFS_DELEG_GROUP_SETS;
4682 base_type = ZFS_DELEG_GROUP;
4684 ld[0] = ZFS_DELEG_LOCAL;
4686 ld[1] = ZFS_DELEG_DESCENDENT;
4688 case ZFS_DELEG_EVERYONE_SETS:
4689 case ZFS_DELEG_EVERYONE:
4690 set_type = ZFS_DELEG_EVERYONE_SETS;
4691 base_type = ZFS_DELEG_EVERYONE;
4693 ld[0] = ZFS_DELEG_LOCAL;
4695 ld[1] = ZFS_DELEG_DESCENDENT;
4698 if (perms != NULL) {
4700 char *end = curr + strlen(perms);
4702 while (curr < end) {
4703 char *delim = strchr(curr, ',');
4714 (void) nvlist_add_boolean(nvl, curr);
4720 for (i = 0; i < 2; i++) {
4721 char locality = ld[i];
4725 if (!nvlist_empty(base_nvl)) {
4727 (void) snprintf(who_buf,
4728 sizeof (who_buf), "%c%c$%s",
4729 base_type, locality, who);
4731 (void) snprintf(who_buf,
4732 sizeof (who_buf), "%c%c$",
4733 base_type, locality);
4735 (void) nvlist_add_nvlist(top_nvl, who_buf,
4740 if (!nvlist_empty(set_nvl)) {
4742 (void) snprintf(who_buf,
4743 sizeof (who_buf), "%c%c$%s",
4744 set_type, locality, who);
4746 (void) snprintf(who_buf,
4747 sizeof (who_buf), "%c%c$",
4748 set_type, locality);
4750 (void) nvlist_add_nvlist(top_nvl, who_buf,
4755 for (i = 0; i < 2; i++) {
4756 char locality = ld[i];
4761 (void) snprintf(who_buf, sizeof (who_buf),
4762 "%c%c$%s", base_type, locality, who);
4764 (void) snprintf(who_buf, sizeof (who_buf),
4765 "%c%c$", base_type, locality);
4766 (void) nvlist_add_boolean(top_nvl, who_buf);
4769 (void) snprintf(who_buf, sizeof (who_buf),
4770 "%c%c$%s", set_type, locality, who);
4772 (void) snprintf(who_buf, sizeof (who_buf),
4773 "%c%c$", set_type, locality);
4774 (void) nvlist_add_boolean(top_nvl, who_buf);
4780 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4782 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4786 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4787 opts->descend, opts->who, opts->perms, *nvlp);
4788 } else if (opts->create) {
4789 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4790 opts->descend, NULL, opts->perms, *nvlp);
4791 } else if (opts->everyone) {
4792 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4793 opts->descend, NULL, opts->perms, *nvlp);
4795 char *curr = opts->who;
4796 char *end = curr + strlen(curr);
4798 while (curr < end) {
4800 zfs_deleg_who_type_t who_type;
4802 char *delim = strchr(curr, ',');
4805 struct passwd *p = NULL;
4806 struct group *g = NULL;
4814 rid = (uid_t)strtol(curr, &endch, 0);
4816 who_type = ZFS_DELEG_USER;
4825 (void) snprintf(errbuf, 256, gettext(
4826 "invalid user %s"), curr);
4827 allow_usage(un, B_TRUE, errbuf);
4829 } else if (opts->group) {
4830 who_type = ZFS_DELEG_GROUP;
4839 (void) snprintf(errbuf, 256, gettext(
4840 "invalid group %s"), curr);
4841 allow_usage(un, B_TRUE, errbuf);
4844 if (*endch != '\0') {
4851 if (*endch != '\0') {
4858 who_type = ZFS_DELEG_USER;
4860 } else if (g != NULL) {
4861 who_type = ZFS_DELEG_GROUP;
4864 (void) snprintf(errbuf, 256, gettext(
4865 "invalid user/group %s"), curr);
4866 allow_usage(un, B_TRUE, errbuf);
4870 (void) sprintf(id, "%u", rid);
4873 store_allow_perm(who_type, opts->local,
4874 opts->descend, who, opts->perms, *nvlp);
4883 print_set_creat_perms(uu_avl_t *who_avl)
4885 const char *sc_title[] = {
4886 gettext("Permission sets:\n"),
4887 gettext("Create time permissions:\n"),
4890 const char **title_ptr = sc_title;
4891 who_perm_node_t *who_node = NULL;
4892 int prev_weight = -1;
4894 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4895 who_node = uu_avl_next(who_avl, who_node)) {
4896 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4897 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4898 const char *who_name = who_node->who_perm.who_name;
4899 int weight = who_type2weight(who_type);
4900 boolean_t first = B_TRUE;
4901 deleg_perm_node_t *deleg_node;
4903 if (prev_weight != weight) {
4904 (void) printf(*title_ptr++);
4905 prev_weight = weight;
4908 if (who_name == NULL || strnlen(who_name, 1) == 0)
4909 (void) printf("\t");
4911 (void) printf("\t%s ", who_name);
4913 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4914 deleg_node = uu_avl_next(avl, deleg_node)) {
4917 deleg_node->dpn_perm.dp_name);
4920 (void) printf(",%s",
4921 deleg_node->dpn_perm.dp_name);
4924 (void) printf("\n");
4929 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4932 who_perm_node_t *who_node = NULL;
4933 boolean_t prt_title = B_TRUE;
4934 uu_avl_walk_t *walk;
4936 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4939 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4940 const char *who_name = who_node->who_perm.who_name;
4941 const char *nice_who_name = who_node->who_perm.who_ug_name;
4942 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4943 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4945 deleg_perm_node_t *deleg_node;
4946 boolean_t prt_who = B_TRUE;
4948 for (deleg_node = uu_avl_first(avl);
4950 deleg_node = uu_avl_next(avl, deleg_node)) {
4951 if (local != deleg_node->dpn_perm.dp_local ||
4952 descend != deleg_node->dpn_perm.dp_descend)
4956 const char *who = NULL;
4958 prt_title = B_FALSE;
4959 (void) printf(title);
4963 case ZFS_DELEG_USER_SETS:
4964 case ZFS_DELEG_USER:
4965 who = gettext("user");
4967 who_name = nice_who_name;
4969 case ZFS_DELEG_GROUP_SETS:
4970 case ZFS_DELEG_GROUP:
4971 who = gettext("group");
4973 who_name = nice_who_name;
4975 case ZFS_DELEG_EVERYONE_SETS:
4976 case ZFS_DELEG_EVERYONE:
4977 who = gettext("everyone");
4982 if (who_name == NULL)
4983 (void) printf("\t%s", who);
4985 (void) printf("\t%s %s", who, who_name);
4988 (void) printf("%c%s", delim,
4989 deleg_node->dpn_perm.dp_name);
4994 (void) printf("\n");
4997 uu_avl_walk_end(walk);
5001 print_fs_perms(fs_perm_set_t *fspset)
5003 fs_perm_node_t *node = NULL;
5004 char buf[ZFS_MAXNAMELEN+32];
5005 const char *dsname = buf;
5007 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5008 node = uu_list_next(fspset->fsps_list, node)) {
5009 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5010 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5013 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5014 gettext("---- Permissions on %s "),
5015 node->fspn_fsperm.fsp_name);
5016 (void) printf(dsname);
5017 left = 70 - strlen(buf);
5020 (void) printf("\n");
5022 print_set_creat_perms(sc_avl);
5023 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5024 gettext("Local permissions:\n"));
5025 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5026 gettext("Descendent permissions:\n"));
5027 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5028 gettext("Local+Descendent permissions:\n"));
5032 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5034 struct deleg_perms {
5040 set_deleg_perms(zfs_handle_t *zhp, void *data)
5042 struct deleg_perms *perms = (struct deleg_perms *)data;
5043 zfs_type_t zfs_type = zfs_get_type(zhp);
5045 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5048 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5052 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5055 nvlist_t *perm_nvl = NULL;
5056 nvlist_t *update_perm_nvl = NULL;
5059 struct allow_opts opts = { 0 };
5061 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5064 while ((c = getopt(argc, argv, optstr)) != -1) {
5067 opts.local = B_TRUE;
5070 opts.descend = B_TRUE;
5076 opts.group = B_TRUE;
5079 opts.everyone = B_TRUE;
5085 opts.create = B_TRUE;
5088 opts.recursive = B_TRUE;
5091 (void) fprintf(stderr, gettext("missing argument for "
5092 "'%c' option\n"), optopt);
5096 opts.prt_usage = B_TRUE;
5099 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5108 /* check arguments */
5109 parse_allow_args(argc, argv, un, &opts);
5111 /* try to open the dataset */
5112 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5113 ZFS_TYPE_VOLUME)) == NULL) {
5114 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5119 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5122 fs_perm_set_init(&fs_perm_set);
5123 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5124 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5129 print_fs_perms(&fs_perm_set);
5131 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5132 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5135 if (un && opts.recursive) {
5136 struct deleg_perms data = { un, update_perm_nvl };
5137 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5146 nvlist_free(perm_nvl);
5147 if (update_perm_nvl != NULL)
5148 nvlist_free(update_perm_nvl);
5150 fs_perm_set_fini(&fs_perm_set);
5158 zfs_do_allow(int argc, char **argv)
5160 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5164 zfs_do_unallow(int argc, char **argv)
5166 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5170 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5175 boolean_t recursive = B_FALSE;
5176 const char *opts = holding ? "rt" : "r";
5180 while ((c = getopt(argc, argv, opts)) != -1) {
5186 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5195 /* check number of arguments */
5203 if (holding && tag[0] == '.') {
5204 /* tags starting with '.' are reserved for libzfs */
5205 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5209 for (i = 0; i < argc; ++i) {
5211 char parent[ZFS_MAXNAMELEN];
5213 char *path = argv[i];
5215 delim = strchr(path, '@');
5216 if (delim == NULL) {
5217 (void) fprintf(stderr,
5218 gettext("'%s' is not a snapshot\n"), path);
5222 (void) strncpy(parent, path, delim - path);
5223 parent[delim - path] = '\0';
5225 zhp = zfs_open(g_zfs, parent,
5226 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5232 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5235 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5241 return (errors != 0);
5245 * zfs hold [-r] [-t] <tag> <snap> ...
5247 * -r Recursively hold
5249 * Apply a user-hold with the given tag to the list of snapshots.
5252 zfs_do_hold(int argc, char **argv)
5254 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5258 * zfs release [-r] <tag> <snap> ...
5260 * -r Recursively release
5262 * Release a user-hold with the given tag from the list of snapshots.
5265 zfs_do_release(int argc, char **argv)
5267 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5270 typedef struct holds_cbdata {
5271 boolean_t cb_recursive;
5272 const char *cb_snapname;
5274 size_t cb_max_namelen;
5275 size_t cb_max_taglen;
5278 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5279 #define DATETIME_BUF_LEN (32)
5284 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5287 nvpair_t *nvp = NULL;
5288 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5292 for (i = 0; i < 3; i++) {
5293 col = gettext(hdr_cols[i]);
5295 (void) printf("%-*s ", i ? tagwidth : nwidth,
5298 (void) printf("%s\n", col);
5302 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5303 char *zname = nvpair_name(nvp);
5305 nvpair_t *nvp2 = NULL;
5306 (void) nvpair_value_nvlist(nvp, &nvl2);
5307 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5308 char tsbuf[DATETIME_BUF_LEN];
5309 char *tagname = nvpair_name(nvp2);
5313 char sep = scripted ? '\t' : ' ';
5314 size_t sepnum = scripted ? 1 : 2;
5316 (void) nvpair_value_uint64(nvp2, &val);
5318 (void) localtime_r(&time, &t);
5319 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5320 gettext(STRFTIME_FMT_STR), &t);
5322 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5323 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5329 * Generic callback function to list a dataset or snapshot.
5332 holds_callback(zfs_handle_t *zhp, void *data)
5334 holds_cbdata_t *cbp = data;
5335 nvlist_t *top_nvl = *cbp->cb_nvlp;
5336 nvlist_t *nvl = NULL;
5337 nvpair_t *nvp = NULL;
5338 const char *zname = zfs_get_name(zhp);
5339 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5341 if (cbp->cb_recursive) {
5342 const char *snapname;
5343 char *delim = strchr(zname, '@');
5347 snapname = delim + 1;
5348 if (strcmp(cbp->cb_snapname, snapname))
5352 if (zfs_get_holds(zhp, &nvl) != 0)
5355 if (znamelen > cbp->cb_max_namelen)
5356 cbp->cb_max_namelen = znamelen;
5358 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5359 const char *tag = nvpair_name(nvp);
5360 size_t taglen = strnlen(tag, MAXNAMELEN);
5361 if (taglen > cbp->cb_max_taglen)
5362 cbp->cb_max_taglen = taglen;
5365 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5369 * zfs holds [-r] <snap> ...
5371 * -r Recursively hold
5374 zfs_do_holds(int argc, char **argv)
5379 boolean_t scripted = B_FALSE;
5380 boolean_t recursive = B_FALSE;
5381 const char *opts = "rH";
5384 int types = ZFS_TYPE_SNAPSHOT;
5385 holds_cbdata_t cb = { 0 };
5392 while ((c = getopt(argc, argv, opts)) != -1) {
5401 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5408 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5409 flags |= ZFS_ITER_RECURSE;
5415 /* check number of arguments */
5419 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5422 for (i = 0; i < argc; ++i) {
5423 char *snapshot = argv[i];
5425 const char *snapname;
5427 delim = strchr(snapshot, '@');
5428 if (delim == NULL) {
5429 (void) fprintf(stderr,
5430 gettext("'%s' is not a snapshot\n"), snapshot);
5434 snapname = delim + 1;
5436 snapshot[delim - snapshot] = '\0';
5438 cb.cb_recursive = recursive;
5439 cb.cb_snapname = snapname;
5443 * 1. collect holds data, set format options
5445 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5446 holds_callback, &cb);
5452 * 2. print holds data
5454 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5456 if (nvlist_empty(nvl))
5457 (void) printf(gettext("no datasets available\n"));
5461 return (0 != errors);
5464 #define CHECK_SPINNER 30
5465 #define SPINNER_TIME 3 /* seconds */
5466 #define MOUNT_TIME 5 /* seconds */
5469 get_one_dataset(zfs_handle_t *zhp, void *data)
5471 static char *spin[] = { "-", "\\", "|", "/" };
5472 static int spinval = 0;
5473 static int spincheck = 0;
5474 static time_t last_spin_time = (time_t)0;
5475 get_all_cb_t *cbp = data;
5476 zfs_type_t type = zfs_get_type(zhp);
5478 if (cbp->cb_verbose) {
5479 if (--spincheck < 0) {
5480 time_t now = time(NULL);
5481 if (last_spin_time + SPINNER_TIME < now) {
5482 update_progress(spin[spinval++ % 4]);
5483 last_spin_time = now;
5485 spincheck = CHECK_SPINNER;
5490 * Interate over any nested datasets.
5492 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5498 * Skip any datasets whose type does not match.
5500 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5504 libzfs_add_handle(cbp, zhp);
5505 assert(cbp->cb_used <= cbp->cb_alloc);
5511 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5513 get_all_cb_t cb = { 0 };
5514 cb.cb_verbose = verbose;
5515 cb.cb_getone = get_one_dataset;
5518 set_progress_header(gettext("Reading ZFS config"));
5519 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5521 *dslist = cb.cb_handles;
5522 *count = cb.cb_used;
5525 finish_progress(gettext("done."));
5529 * Generic callback for sharing or mounting filesystems. Because the code is so
5530 * similar, we have a common function with an extra parameter to determine which
5531 * mode we are using.
5533 #define OP_SHARE 0x1
5534 #define OP_MOUNT 0x2
5537 * Share or mount a dataset.
5540 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5541 boolean_t explicit, const char *options)
5543 char mountpoint[ZFS_MAXPROPLEN];
5544 char shareopts[ZFS_MAXPROPLEN];
5545 char smbshareopts[ZFS_MAXPROPLEN];
5546 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5548 uint64_t zoned, canmount;
5549 boolean_t shared_nfs, shared_smb;
5551 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5554 * Check to make sure we can mount/share this dataset. If we
5555 * are in the global zone and the filesystem is exported to a
5556 * local zone, or if we are in a local zone and the
5557 * filesystem is not exported, then it is an error.
5559 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5561 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5565 (void) fprintf(stderr, gettext("cannot %s '%s': "
5566 "dataset is exported to a local zone\n"), cmdname,
5570 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5574 (void) fprintf(stderr, gettext("cannot %s '%s': "
5575 "permission denied\n"), cmdname,
5581 * Ignore any filesystems which don't apply to us. This
5582 * includes those with a legacy mountpoint, or those with
5583 * legacy share options.
5585 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5586 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5587 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5588 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5589 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5590 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5592 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5593 strcmp(smbshareopts, "off") == 0) {
5597 (void) fprintf(stderr, gettext("cannot share '%s': "
5598 "legacy share\n"), zfs_get_name(zhp));
5599 (void) fprintf(stderr, gettext("to "
5600 "share this filesystem set "
5601 "sharenfs property on\n"));
5606 * We cannot share or mount legacy filesystems. If the
5607 * shareopts is non-legacy but the mountpoint is legacy, we
5608 * treat it as a legacy share.
5610 if (strcmp(mountpoint, "legacy") == 0) {
5614 (void) fprintf(stderr, gettext("cannot %s '%s': "
5615 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5616 (void) fprintf(stderr, gettext("use %s(8) to "
5617 "%s this filesystem\n"), cmdname, cmdname);
5621 if (strcmp(mountpoint, "none") == 0) {
5625 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5626 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5631 * canmount explicit outcome
5632 * on no pass through
5633 * on yes pass through
5635 * off yes display error, return 1
5636 * noauto no return 0
5637 * noauto yes pass through
5639 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5640 if (canmount == ZFS_CANMOUNT_OFF) {
5644 (void) fprintf(stderr, gettext("cannot %s '%s': "
5645 "'canmount' property is set to 'off'\n"), cmdname,
5648 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5653 * At this point, we have verified that the mountpoint and/or
5654 * shareopts are appropriate for auto management. If the
5655 * filesystem is already mounted or shared, return (failing
5656 * for explicit requests); otherwise mount or share the
5662 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5663 shared_smb = zfs_is_shared_smb(zhp, NULL);
5665 if (shared_nfs && shared_smb ||
5666 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5667 strcmp(smbshareopts, "off") == 0) ||
5668 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5669 strcmp(shareopts, "off") == 0)) {
5673 (void) fprintf(stderr, gettext("cannot share "
5674 "'%s': filesystem already shared\n"),
5679 if (!zfs_is_mounted(zhp, NULL) &&
5680 zfs_mount(zhp, NULL, 0) != 0)
5683 if (protocol == NULL) {
5684 if (zfs_shareall(zhp) != 0)
5686 } else if (strcmp(protocol, "nfs") == 0) {
5687 if (zfs_share_nfs(zhp))
5689 } else if (strcmp(protocol, "smb") == 0) {
5690 if (zfs_share_smb(zhp))
5693 (void) fprintf(stderr, gettext("cannot share "
5694 "'%s': invalid share type '%s' "
5696 zfs_get_name(zhp), protocol);
5703 if (options == NULL)
5704 mnt.mnt_mntopts = "";
5706 mnt.mnt_mntopts = (char *)options;
5708 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5709 zfs_is_mounted(zhp, NULL)) {
5713 (void) fprintf(stderr, gettext("cannot mount "
5714 "'%s': filesystem already mounted\n"),
5719 if (zfs_mount(zhp, options, flags) != 0)
5728 * Reports progress in the form "(current/total)". Not thread-safe.
5731 report_mount_progress(int current, int total)
5733 static time_t last_progress_time = 0;
5734 time_t now = time(NULL);
5737 /* report 1..n instead of 0..n-1 */
5740 /* display header if we're here for the first time */
5742 set_progress_header(gettext("Mounting ZFS filesystems"));
5743 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5744 /* too soon to report again */
5748 last_progress_time = now;
5750 (void) sprintf(info, "(%d/%d)", current, total);
5752 if (current == total)
5753 finish_progress(info);
5755 update_progress(info);
5759 append_options(char *mntopts, char *newopts)
5761 int len = strlen(mntopts);
5763 /* original length plus new string to append plus 1 for the comma */
5764 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5765 (void) fprintf(stderr, gettext("the opts argument for "
5766 "'%c' option is too long (more than %d chars)\n"),
5767 "-o", MNT_LINE_MAX);
5772 mntopts[len++] = ',';
5774 (void) strcpy(&mntopts[len], newopts);
5778 share_mount(int op, int argc, char **argv)
5781 boolean_t verbose = B_FALSE;
5783 char *options = NULL;
5787 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5797 if (*optarg == '\0') {
5798 (void) fprintf(stderr, gettext("empty mount "
5799 "options (-o) specified\n"));
5803 if (options == NULL)
5804 options = safe_malloc(MNT_LINE_MAX + 1);
5806 /* option validation is done later */
5807 append_options(options, optarg);
5811 warnx("no overlay mounts support on FreeBSD, ignoring");
5814 (void) fprintf(stderr, gettext("missing argument for "
5815 "'%c' option\n"), optopt);
5819 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5828 /* check number of arguments */
5830 zfs_handle_t **dslist = NULL;
5831 size_t i, count = 0;
5832 char *protocol = NULL;
5834 if (op == OP_SHARE && argc > 0) {
5835 if (strcmp(argv[0], "nfs") != 0 &&
5836 strcmp(argv[0], "smb") != 0) {
5837 (void) fprintf(stderr, gettext("share type "
5838 "must be 'nfs' or 'smb'\n"));
5847 (void) fprintf(stderr, gettext("too many arguments\n"));
5851 start_progress_timer();
5852 get_all_datasets(&dslist, &count, verbose);
5857 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5859 for (i = 0; i < count; i++) {
5861 report_mount_progress(i, count);
5863 if (share_mount_one(dslist[i], op, flags, protocol,
5864 B_FALSE, options) != 0)
5866 zfs_close(dslist[i]);
5870 } else if (argc == 0) {
5871 struct mnttab entry;
5873 if ((op == OP_SHARE) || (options != NULL)) {
5874 (void) fprintf(stderr, gettext("missing filesystem "
5875 "argument (specify -a for all)\n"));
5880 * When mount is given no arguments, go through /etc/mnttab and
5881 * display any active ZFS mounts. We hide any snapshots, since
5882 * they are controlled automatically.
5884 rewind(mnttab_file);
5885 while (getmntent(mnttab_file, &entry) == 0) {
5886 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5887 strchr(entry.mnt_special, '@') != NULL)
5890 (void) printf("%-30s %s\n", entry.mnt_special,
5898 (void) fprintf(stderr,
5899 gettext("too many arguments\n"));
5903 if ((zhp = zfs_open(g_zfs, argv[0],
5904 ZFS_TYPE_FILESYSTEM)) == NULL) {
5907 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5917 * zfs mount -a [nfs]
5918 * zfs mount filesystem
5920 * Mount all filesystems, or mount the given filesystem.
5923 zfs_do_mount(int argc, char **argv)
5925 return (share_mount(OP_MOUNT, argc, argv));
5929 * zfs share -a [nfs | smb]
5930 * zfs share filesystem
5932 * Share all filesystems, or share the given filesystem.
5935 zfs_do_share(int argc, char **argv)
5937 return (share_mount(OP_SHARE, argc, argv));
5940 typedef struct unshare_unmount_node {
5941 zfs_handle_t *un_zhp;
5943 uu_avl_node_t un_avlnode;
5944 } unshare_unmount_node_t;
5948 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
5950 const unshare_unmount_node_t *l = larg;
5951 const unshare_unmount_node_t *r = rarg;
5953 return (strcmp(l->un_mountp, r->un_mountp));
5957 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5958 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
5959 * and unmount it appropriately.
5962 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
5966 struct stat64 statbuf;
5967 struct extmnttab entry;
5968 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5972 * Search for the path in /etc/mnttab. Rather than looking for the
5973 * specific path, which can be fooled by non-standard paths (i.e. ".."
5974 * or "//"), we stat() the path and search for the corresponding
5975 * (major,minor) device pair.
5977 if (stat64(path, &statbuf) != 0) {
5978 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5979 cmdname, path, strerror(errno));
5982 path_inode = statbuf.st_ino;
5985 * Search for the given (major,minor) pair in the mount table.
5988 rewind(mnttab_file);
5989 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
5990 if (entry.mnt_major == major(statbuf.st_dev) &&
5991 entry.mnt_minor == minor(statbuf.st_dev))
5998 if (statfs(path, &sfs) != 0) {
5999 (void) fprintf(stderr, "%s: %s\n", path,
6003 statfs2mnttab(&sfs, &entry);
6007 if (op == OP_SHARE) {
6008 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6009 "currently mounted\n"), cmdname, path);
6012 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6014 if ((ret = umount2(path, flags)) != 0)
6015 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6020 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6021 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6022 "filesystem\n"), cmdname, path);
6026 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6027 ZFS_TYPE_FILESYSTEM)) == NULL)
6031 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6032 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6033 cmdname, path, strerror(errno));
6035 } else if (statbuf.st_ino != path_inode) {
6036 (void) fprintf(stderr, gettext("cannot "
6037 "%s '%s': not a mountpoint\n"), cmdname, path);
6041 if (op == OP_SHARE) {
6042 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6043 char smbshare_prop[ZFS_MAXPROPLEN];
6045 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6046 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6047 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6048 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6050 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6051 strcmp(smbshare_prop, "off") == 0) {
6052 (void) fprintf(stderr, gettext("cannot unshare "
6053 "'%s': legacy share\n"), path);
6055 (void) fprintf(stderr, gettext("use "
6056 "unshare(1M) to unshare this filesystem\n"));
6058 } else if (!zfs_is_shared(zhp)) {
6059 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6060 "not currently shared\n"), path);
6062 ret = zfs_unshareall_bypath(zhp, path);
6065 char mtpt_prop[ZFS_MAXPROPLEN];
6067 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6068 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6071 ret = zfs_unmount(zhp, NULL, flags);
6072 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6073 (void) fprintf(stderr, gettext("cannot unmount "
6074 "'%s': legacy mountpoint\n"),
6076 (void) fprintf(stderr, gettext("use umount(8) "
6077 "to unmount this filesystem\n"));
6079 ret = zfs_unmountall(zhp, flags);
6090 * Generic callback for unsharing or unmounting a filesystem.
6093 unshare_unmount(int op, int argc, char **argv)
6100 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6101 char sharesmb[ZFS_MAXPROPLEN];
6104 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6113 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6124 * We could make use of zfs_for_each() to walk all datasets in
6125 * the system, but this would be very inefficient, especially
6126 * since we would have to linearly search /etc/mnttab for each
6127 * one. Instead, do one pass through /etc/mnttab looking for
6128 * zfs entries and call zfs_unmount() for each one.
6130 * Things get a little tricky if the administrator has created
6131 * mountpoints beneath other ZFS filesystems. In this case, we
6132 * have to unmount the deepest filesystems first. To accomplish
6133 * this, we place all the mountpoints in an AVL tree sorted by
6134 * the special type (dataset name), and walk the result in
6135 * reverse to make sure to get any snapshots first.
6137 struct mnttab entry;
6138 uu_avl_pool_t *pool;
6140 unshare_unmount_node_t *node;
6142 uu_avl_walk_t *walk;
6145 (void) fprintf(stderr, gettext("too many arguments\n"));
6149 if (((pool = uu_avl_pool_create("unmount_pool",
6150 sizeof (unshare_unmount_node_t),
6151 offsetof(unshare_unmount_node_t, un_avlnode),
6152 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6153 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6156 rewind(mnttab_file);
6157 while (getmntent(mnttab_file, &entry) == 0) {
6159 /* ignore non-ZFS entries */
6160 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6163 /* ignore snapshots */
6164 if (strchr(entry.mnt_special, '@') != NULL)
6167 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6168 ZFS_TYPE_FILESYSTEM)) == NULL) {
6175 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6177 sizeof (nfs_mnt_prop),
6178 NULL, NULL, 0, B_FALSE) == 0);
6179 if (strcmp(nfs_mnt_prop, "off") != 0)
6181 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6183 sizeof (nfs_mnt_prop),
6184 NULL, NULL, 0, B_FALSE) == 0);
6185 if (strcmp(nfs_mnt_prop, "off") == 0)
6189 /* Ignore legacy mounts */
6190 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6192 sizeof (nfs_mnt_prop),
6193 NULL, NULL, 0, B_FALSE) == 0);
6194 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6196 /* Ignore canmount=noauto mounts */
6197 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6198 ZFS_CANMOUNT_NOAUTO)
6204 node = safe_malloc(sizeof (unshare_unmount_node_t));
6206 node->un_mountp = safe_strdup(entry.mnt_mountp);
6208 uu_avl_node_init(node, &node->un_avlnode, pool);
6210 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6211 uu_avl_insert(tree, node, idx);
6213 zfs_close(node->un_zhp);
6214 free(node->un_mountp);
6220 * Walk the AVL tree in reverse, unmounting each filesystem and
6221 * removing it from the AVL tree in the process.
6223 if ((walk = uu_avl_walk_start(tree,
6224 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6227 while ((node = uu_avl_walk_next(walk)) != NULL) {
6228 uu_avl_remove(tree, node);
6232 if (zfs_unshareall_bypath(node->un_zhp,
6233 node->un_mountp) != 0)
6238 if (zfs_unmount(node->un_zhp,
6239 node->un_mountp, flags) != 0)
6244 zfs_close(node->un_zhp);
6245 free(node->un_mountp);
6249 uu_avl_walk_end(walk);
6250 uu_avl_destroy(tree);
6251 uu_avl_pool_destroy(pool);
6256 (void) fprintf(stderr,
6257 gettext("missing filesystem argument\n"));
6259 (void) fprintf(stderr,
6260 gettext("too many arguments\n"));
6265 * We have an argument, but it may be a full path or a ZFS
6266 * filesystem. Pass full paths off to unmount_path() (shared by
6267 * manual_unmount), otherwise open the filesystem and pass to
6270 if (argv[0][0] == '/')
6271 return (unshare_unmount_path(op, argv[0],
6274 if ((zhp = zfs_open(g_zfs, argv[0],
6275 ZFS_TYPE_FILESYSTEM)) == NULL)
6278 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6279 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6280 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6281 NULL, 0, B_FALSE) == 0);
6285 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6287 sizeof (nfs_mnt_prop),
6288 NULL, NULL, 0, B_FALSE) == 0);
6289 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6290 sharesmb, sizeof (sharesmb), NULL, NULL,
6293 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6294 strcmp(sharesmb, "off") == 0) {
6295 (void) fprintf(stderr, gettext("cannot "
6296 "unshare '%s': legacy share\n"),
6299 (void) fprintf(stderr, gettext("use "
6300 "unshare(1M) to unshare this "
6304 } else if (!zfs_is_shared(zhp)) {
6305 (void) fprintf(stderr, gettext("cannot "
6306 "unshare '%s': not currently "
6307 "shared\n"), zfs_get_name(zhp));
6309 } else if (zfs_unshareall(zhp) != 0) {
6315 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6316 (void) fprintf(stderr, gettext("cannot "
6317 "unmount '%s': legacy "
6318 "mountpoint\n"), zfs_get_name(zhp));
6319 (void) fprintf(stderr, gettext("use "
6320 "umount(8) to unmount this "
6323 } else if (!zfs_is_mounted(zhp, NULL)) {
6324 (void) fprintf(stderr, gettext("cannot "
6325 "unmount '%s': not currently "
6329 } else if (zfs_unmountall(zhp, flags) != 0) {
6343 * zfs unmount filesystem
6345 * Unmount all filesystems, or a specific ZFS filesystem.
6348 zfs_do_unmount(int argc, char **argv)
6350 return (unshare_unmount(OP_MOUNT, argc, argv));
6355 * zfs unshare filesystem
6357 * Unshare all filesystems, or a specific ZFS filesystem.
6360 zfs_do_unshare(int argc, char **argv)
6362 return (unshare_unmount(OP_SHARE, argc, argv));
6366 * Attach/detach the given dataset to/from the given jail
6370 do_jail(int argc, char **argv, int attach)
6375 /* check number of arguments */
6377 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6381 (void) fprintf(stderr, gettext("too many arguments\n"));
6385 jailid = jail_getid(argv[1]);
6387 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6391 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6395 ret = (zfs_jail(zhp, jailid, attach) != 0);
6402 * zfs jail jailid filesystem
6404 * Attach the given dataset to the given jail
6408 zfs_do_jail(int argc, char **argv)
6411 return (do_jail(argc, argv, 1));
6415 * zfs unjail jailid filesystem
6417 * Detach the given dataset from the given jail
6421 zfs_do_unjail(int argc, char **argv)
6424 return (do_jail(argc, argv, 0));
6428 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6429 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6432 manual_mount(int argc, char **argv)
6435 char mountpoint[ZFS_MAXPROPLEN];
6436 char mntopts[MNT_LINE_MAX] = { '\0' };
6440 char *dataset, *path;
6443 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6446 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6449 flags |= MS_OVERLAY;
6452 flags |= MS_NOMNTTAB;
6455 (void) fprintf(stderr, gettext("missing argument for "
6456 "'%c' option\n"), optopt);
6460 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6462 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6471 /* check that we only have two arguments */
6474 (void) fprintf(stderr, gettext("missing dataset "
6477 (void) fprintf(stderr,
6478 gettext("missing mountpoint argument\n"));
6480 (void) fprintf(stderr, gettext("too many arguments\n"));
6481 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6488 /* try to open the dataset */
6489 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6492 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6493 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6495 /* check for legacy mountpoint and complain appropriately */
6497 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6498 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6499 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6500 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6505 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6506 "mounted using 'mount -t zfs'\n"), dataset);
6507 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6508 "instead.\n"), path);
6509 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6510 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6511 (void) fprintf(stderr, gettext("See zfs(8) for more "
6520 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6521 * unmounts of non-legacy filesystems, as this is the dominant administrative
6525 manual_unmount(int argc, char **argv)
6531 while ((c = getopt(argc, argv, "f")) != -1) {
6537 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6539 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6548 /* check arguments */
6551 (void) fprintf(stderr, gettext("missing path "
6554 (void) fprintf(stderr, gettext("too many arguments\n"));
6555 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6559 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6563 find_command_idx(char *command, int *idx)
6567 for (i = 0; i < NCOMMAND; i++) {
6568 if (command_table[i].name == NULL)
6571 if (strcmp(command, command_table[i].name) == 0) {
6580 zfs_do_diff(int argc, char **argv)
6584 char *tosnap = NULL;
6585 char *fromsnap = NULL;
6590 while ((c = getopt(argc, argv, "FHt")) != -1) {
6593 flags |= ZFS_DIFF_CLASSIFY;
6596 flags |= ZFS_DIFF_PARSEABLE;
6599 flags |= ZFS_DIFF_TIMESTAMP;
6602 (void) fprintf(stderr,
6603 gettext("invalid option '%c'\n"), optopt);
6612 (void) fprintf(stderr,
6613 gettext("must provide at least one snapshot name\n"));
6618 (void) fprintf(stderr, gettext("too many arguments\n"));
6623 tosnap = (argc == 2) ? argv[1] : NULL;
6626 if (*fromsnap != '@')
6627 copy = strdup(fromsnap);
6629 copy = strdup(tosnap);
6633 if (atp = strchr(copy, '@'))
6636 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6642 * Ignore SIGPIPE so that the library can give us
6643 * information on any failure
6645 (void) sigignore(SIGPIPE);
6647 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6655 main(int argc, char **argv)
6662 (void) setlocale(LC_ALL, "");
6663 (void) textdomain(TEXT_DOMAIN);
6667 if ((g_zfs = libzfs_init()) == NULL) {
6668 (void) fprintf(stderr, gettext("internal error: failed to "
6669 "initialize ZFS library\n"));
6673 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6675 libzfs_print_on_error(g_zfs, B_TRUE);
6677 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6678 (void) fprintf(stderr, gettext("internal error: unable to "
6679 "open %s\n"), MNTTAB);
6684 * This command also doubles as the /etc/fs mount and unmount program.
6685 * Determine if we should take this behavior based on argv[0].
6687 progname = basename(argv[0]);
6688 if (strcmp(progname, "mount") == 0) {
6689 ret = manual_mount(argc, argv);
6690 } else if (strcmp(progname, "umount") == 0) {
6691 ret = manual_unmount(argc, argv);
6694 * Make sure the user has specified some command.
6697 (void) fprintf(stderr, gettext("missing command\n"));
6704 * The 'umount' command is an alias for 'unmount'
6706 if (strcmp(cmdname, "umount") == 0)
6707 cmdname = "unmount";
6710 * The 'recv' command is an alias for 'receive'
6712 if (strcmp(cmdname, "recv") == 0)
6713 cmdname = "receive";
6718 if (strcmp(cmdname, "-?") == 0)
6722 * Run the appropriate command.
6724 libzfs_mnttab_cache(g_zfs, B_TRUE);
6725 if (find_command_idx(cmdname, &i) == 0) {
6726 current_command = &command_table[i];
6727 ret = command_table[i].func(argc - 1, argv + 1);
6728 } else if (strchr(cmdname, '=') != NULL) {
6729 verify(find_command_idx("set", &i) == 0);
6730 current_command = &command_table[i];
6731 ret = command_table[i].func(argc, argv);
6733 (void) fprintf(stderr, gettext("unrecognized "
6734 "command '%s'\n"), cmdname);
6737 libzfs_mnttab_cache(g_zfs, B_FALSE);
6740 (void) fclose(mnttab_file);
6742 if (ret == 0 && log_history)
6743 (void) zpool_log_history(g_zfs, history_str);
6748 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6749 * for the purposes of running ::findleaks.
6751 if (getenv("ZFS_ABORT") != NULL) {
6752 (void) printf("dumping core by request\n");