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 (c) 2011, 2016 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright (c) 2014 Integros [integros.com]
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
32 * Copyright 2016 Nexenta Systems, Inc.
33 * Copyright (c) 2019 Datto Inc.
43 #include <libnvpair.h>
55 #include <sys/debug.h>
57 #include <sys/mntent.h>
58 #include <sys/mnttab.h>
59 #include <sys/mount.h>
61 #include <sys/fs/zfs.h>
62 #include <sys/types.h>
68 #include <libzfs_core.h>
70 #include <zfs_deleg.h>
74 #include <directory.h>
81 #include "zfs_comutil.h"
83 libzfs_handle_t *g_zfs;
85 static FILE *mnttab_file;
86 static char history_str[HIS_MAX_RECORD_LEN];
87 static boolean_t log_history = B_TRUE;
89 static int zfs_do_clone(int argc, char **argv);
90 static int zfs_do_create(int argc, char **argv);
91 static int zfs_do_destroy(int argc, char **argv);
92 static int zfs_do_get(int argc, char **argv);
93 static int zfs_do_inherit(int argc, char **argv);
94 static int zfs_do_list(int argc, char **argv);
95 static int zfs_do_mount(int argc, char **argv);
96 static int zfs_do_rename(int argc, char **argv);
97 static int zfs_do_rollback(int argc, char **argv);
98 static int zfs_do_set(int argc, char **argv);
99 static int zfs_do_upgrade(int argc, char **argv);
100 static int zfs_do_snapshot(int argc, char **argv);
101 static int zfs_do_unmount(int argc, char **argv);
102 static int zfs_do_share(int argc, char **argv);
103 static int zfs_do_unshare(int argc, char **argv);
104 static int zfs_do_send(int argc, char **argv);
105 static int zfs_do_receive(int argc, char **argv);
106 static int zfs_do_promote(int argc, char **argv);
107 static int zfs_do_userspace(int argc, char **argv);
108 static int zfs_do_allow(int argc, char **argv);
109 static int zfs_do_unallow(int argc, char **argv);
110 static int zfs_do_hold(int argc, char **argv);
111 static int zfs_do_holds(int argc, char **argv);
112 static int zfs_do_release(int argc, char **argv);
113 static int zfs_do_diff(int argc, char **argv);
114 static int zfs_do_jail(int argc, char **argv);
115 static int zfs_do_unjail(int argc, char **argv);
116 static int zfs_do_bookmark(int argc, char **argv);
117 static int zfs_do_remap(int argc, char **argv);
118 static int zfs_do_channel_program(int argc, char **argv);
121 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
126 _umem_debug_init(void)
128 return ("default,verbose"); /* $UMEM_DEBUG setting */
132 _umem_logging_init(void)
134 return ("fail,contents"); /* $UMEM_LOGGING setting */
169 HELP_CHANNEL_PROGRAM,
172 typedef struct zfs_command {
174 int (*func)(int argc, char **argv);
179 * Master command table. Each ZFS command has a name, associated function, and
180 * usage message. The usage messages need to be internationalized, so we have
181 * to have a function to return the usage message based on a command index.
183 * These commands are organized according to how they are displayed in the usage
184 * message. An empty command (one with a NULL name) indicates an empty line in
185 * the generic usage message.
187 static zfs_command_t command_table[] = {
188 { "create", zfs_do_create, HELP_CREATE },
189 { "destroy", zfs_do_destroy, HELP_DESTROY },
191 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
192 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
193 { "clone", zfs_do_clone, HELP_CLONE },
194 { "promote", zfs_do_promote, HELP_PROMOTE },
195 { "rename", zfs_do_rename, HELP_RENAME },
196 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
197 { "program", zfs_do_channel_program, HELP_CHANNEL_PROGRAM },
199 { "list", zfs_do_list, HELP_LIST },
201 { "set", zfs_do_set, HELP_SET },
202 { "get", zfs_do_get, HELP_GET },
203 { "inherit", zfs_do_inherit, HELP_INHERIT },
204 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
205 { "userspace", zfs_do_userspace, HELP_USERSPACE },
206 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
208 { "mount", zfs_do_mount, HELP_MOUNT },
209 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
210 { "share", zfs_do_share, HELP_SHARE },
211 { "unshare", zfs_do_unshare, HELP_UNSHARE },
213 { "send", zfs_do_send, HELP_SEND },
214 { "receive", zfs_do_receive, HELP_RECEIVE },
216 { "allow", zfs_do_allow, HELP_ALLOW },
218 { "unallow", zfs_do_unallow, HELP_UNALLOW },
220 { "hold", zfs_do_hold, HELP_HOLD },
221 { "holds", zfs_do_holds, HELP_HOLDS },
222 { "release", zfs_do_release, HELP_RELEASE },
223 { "diff", zfs_do_diff, HELP_DIFF },
225 { "jail", zfs_do_jail, HELP_JAIL },
226 { "unjail", zfs_do_unjail, HELP_UNJAIL },
227 { "remap", zfs_do_remap, HELP_REMAP },
230 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
232 zfs_command_t *current_command;
235 get_usage(zfs_help_t idx)
239 return (gettext("\tclone [-p] [-o property=value] ... "
240 "<snapshot> <filesystem|volume>\n"));
242 return (gettext("\tcreate [-pu] [-o property=value] ... "
244 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
245 "-V <size> <volume>\n"));
247 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
248 "\tdestroy [-dnpRrv] "
249 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
250 "\tdestroy <filesystem|volume>#<bookmark>\n"));
252 return (gettext("\tget [-rHp] [-d max] "
253 "[-o \"all\" | field[,...]]\n"
254 "\t [-t type[,...]] [-s source[,...]]\n"
255 "\t <\"all\" | property[,...]> "
256 "[filesystem|volume|snapshot|bookmark] ...\n"));
258 return (gettext("\tinherit [-rS] <property> "
259 "<filesystem|volume|snapshot> ...\n"));
261 return (gettext("\tupgrade [-v]\n"
262 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
264 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
266 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
268 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
269 "[-s property]...\n\t [-S property]... [-t type[,...]] "
270 "[filesystem|volume|snapshot] ...\n"));
272 return (gettext("\tmount\n"
273 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
275 return (gettext("\tpromote <clone-filesystem>\n"));
277 return (gettext("\treceive|recv [-vnsFu] <filesystem|volume|"
279 "\treceive|recv [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
281 "\treceive|recv -A <filesystem|volume>\n"));
283 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
284 "<filesystem|volume|snapshot>\n"
285 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
286 "\trename -r <snapshot> <snapshot>\n"
287 "\trename <bookmark> <bookmark>\n"
288 "\trename -u [-p] <filesystem> <filesystem>"));
290 return (gettext("\trollback [-rRf] <snapshot>\n"));
292 return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] "
294 "\tsend [-LPcenv] [-i snapshot|bookmark] "
295 "<filesystem|volume|snapshot>\n"
296 "\tsend [-nvPe] -t <receive_resume_token>\n"));
298 return (gettext("\tset <property=value> ... "
299 "<filesystem|volume|snapshot> ...\n"));
301 return (gettext("\tshare <-a | filesystem>\n"));
303 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
304 "<filesystem|volume>@<snap> ...\n"));
306 return (gettext("\tunmount|umount [-f] "
307 "<-a | filesystem|mountpoint>\n"));
309 return (gettext("\tunshare "
310 "<-a | filesystem|mountpoint>\n"));
312 return (gettext("\tallow <filesystem|volume>\n"
314 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
315 "\t <filesystem|volume>\n"
316 "\tallow [-ld] -e <perm|@setname>[,...] "
317 "<filesystem|volume>\n"
318 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
319 "\tallow -s @setname <perm|@setname>[,...] "
320 "<filesystem|volume>\n"));
322 return (gettext("\tunallow [-rldug] "
323 "<\"everyone\"|user|group>[,...]\n"
324 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
325 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
326 "<filesystem|volume>\n"
327 "\tunallow [-r] -c [<perm|@setname>[,...]] "
328 "<filesystem|volume>\n"
329 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
330 "<filesystem|volume>\n"));
332 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
334 "\t [-S field] ... [-t type[,...]] "
335 "<filesystem|snapshot>\n"));
336 case HELP_GROUPSPACE:
337 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
339 "\t [-S field] ... [-t type[,...]] "
340 "<filesystem|snapshot>\n"));
342 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
344 return (gettext("\tholds [-Hp] [-r|-d depth] "
345 "<filesystem|volume|snapshot> ...\n"));
347 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
349 return (gettext("\tdiff [-FHt] <snapshot> "
350 "[snapshot|filesystem]\n"));
352 return (gettext("\tremap <filesystem | volume>\n"));
354 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
355 case HELP_CHANNEL_PROGRAM:
356 return (gettext("\tprogram [-jn] [-t <instruction limit>] "
357 "[-m <memory limit (b)>] <pool> <program file> "
368 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
373 * Utility function to guarantee malloc() success.
377 safe_malloc(size_t size)
381 if ((data = calloc(1, size)) == NULL)
388 safe_realloc(void *data, size_t size)
391 if ((newp = realloc(data, size)) == NULL) {
400 safe_strdup(char *str)
402 char *dupstr = strdup(str);
411 * Callback routine that will print out information for each of
415 usage_prop_cb(int prop, void *cb)
419 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
421 if (zfs_prop_readonly(prop))
422 (void) fprintf(fp, " NO ");
424 (void) fprintf(fp, "YES ");
426 if (zfs_prop_inheritable(prop))
427 (void) fprintf(fp, " YES ");
429 (void) fprintf(fp, " NO ");
431 if (zfs_prop_values(prop) == NULL)
432 (void) fprintf(fp, "-\n");
434 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
440 * Display usage message. If we're inside a command, display only the usage for
441 * that command. Otherwise, iterate over the entire command table and display
442 * a complete usage message.
445 usage(boolean_t requested)
448 boolean_t show_properties = B_FALSE;
449 FILE *fp = requested ? stdout : stderr;
451 if (current_command == NULL) {
453 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
455 gettext("where 'command' is one of the following:\n\n"));
457 for (i = 0; i < NCOMMAND; i++) {
458 if (command_table[i].name == NULL)
459 (void) fprintf(fp, "\n");
461 (void) fprintf(fp, "%s",
462 get_usage(command_table[i].usage));
465 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
466 "pool/[dataset/]*dataset[@name]\n"));
468 (void) fprintf(fp, gettext("usage:\n"));
469 (void) fprintf(fp, "%s", get_usage(current_command->usage));
472 if (current_command != NULL &&
473 (strcmp(current_command->name, "set") == 0 ||
474 strcmp(current_command->name, "get") == 0 ||
475 strcmp(current_command->name, "inherit") == 0 ||
476 strcmp(current_command->name, "list") == 0))
477 show_properties = B_TRUE;
479 if (show_properties) {
481 gettext("\nThe following properties are supported:\n"));
483 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
484 "PROPERTY", "EDIT", "INHERIT", "VALUES");
486 /* Iterate over all properties */
487 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
490 (void) fprintf(fp, "\t%-15s ", "userused@...");
491 (void) fprintf(fp, " NO NO <size>\n");
492 (void) fprintf(fp, "\t%-15s ", "groupused@...");
493 (void) fprintf(fp, " NO NO <size>\n");
494 (void) fprintf(fp, "\t%-15s ", "userquota@...");
495 (void) fprintf(fp, "YES NO <size> | none\n");
496 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
497 (void) fprintf(fp, "YES NO <size> | none\n");
498 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
499 (void) fprintf(fp, " NO NO <size>\n");
501 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
502 "with standard units such as K, M, G, etc.\n"));
503 (void) fprintf(fp, gettext("\nUser-defined properties can "
504 "be specified by using a name containing a colon (:).\n"));
505 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
506 "properties must be appended with\n"
507 "a user or group specifier of one of these forms:\n"
508 " POSIX name (eg: \"matt\")\n"
509 " POSIX id (eg: \"126829\")\n"
510 " SMB name@domain (eg: \"matt@sun\")\n"
511 " SMB SID (eg: \"S-1-234-567-89\")\n"));
514 gettext("\nFor the property list, run: %s\n"),
517 gettext("\nFor the delegated permission list, run: %s\n"),
518 "zfs allow|unallow");
522 * See comments at end of main().
524 if (getenv("ZFS_ABORT") != NULL) {
525 (void) printf("dumping core by request\n");
529 exit(requested ? 0 : 2);
533 * Take a property=value argument string and add it to the given nvlist.
534 * Modifies the argument inplace.
537 parseprop(nvlist_t *props, char *propname)
539 char *propval, *strval;
541 if ((propval = strchr(propname, '=')) == NULL) {
542 (void) fprintf(stderr, gettext("missing "
543 "'=' for property=value argument\n"));
548 if (nvlist_lookup_string(props, propname, &strval) == 0) {
549 (void) fprintf(stderr, gettext("property '%s' "
550 "specified multiple times\n"), propname);
553 if (nvlist_add_string(props, propname, propval) != 0)
559 parse_depth(char *opt, int *flags)
564 depth = (int)strtol(opt, &tmp, 0);
566 (void) fprintf(stderr,
567 gettext("%s is not an integer\n"), opt);
571 (void) fprintf(stderr,
572 gettext("Depth can not be negative.\n"));
575 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
579 #define PROGRESS_DELAY 2 /* seconds */
581 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";
582 static time_t pt_begin;
583 static char *pt_header = NULL;
584 static boolean_t pt_shown;
587 start_progress_timer(void)
589 pt_begin = time(NULL) + PROGRESS_DELAY;
594 set_progress_header(char *header)
596 assert(pt_header == NULL);
597 pt_header = safe_strdup(header);
599 (void) printf("%s: ", header);
600 (void) fflush(stdout);
605 update_progress(char *update)
607 if (!pt_shown && time(NULL) > pt_begin) {
608 int len = strlen(update);
610 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
612 (void) fflush(stdout);
614 } else if (pt_shown) {
615 int len = strlen(update);
617 (void) printf("%s%*.*s", update, len, len, pt_reverse);
618 (void) fflush(stdout);
623 finish_progress(char *done)
626 (void) printf("%s\n", done);
627 (void) fflush(stdout);
634 * Check if the dataset is mountable and should be automatically mounted.
637 should_auto_mount(zfs_handle_t *zhp)
639 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp)))
641 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON);
645 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
647 * Given an existing dataset, create a writable copy whose initial contents
648 * are the same as the source. The newly created dataset maintains a
649 * dependency on the original; the original cannot be destroyed so long as
652 * The '-p' flag creates all the non-existing ancestors of the target first.
655 zfs_do_clone(int argc, char **argv)
657 zfs_handle_t *zhp = NULL;
658 boolean_t parents = B_FALSE;
663 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
667 while ((c = getopt(argc, argv, "o:p")) != -1) {
670 if (parseprop(props, optarg) != 0)
677 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
686 /* check number of arguments */
688 (void) fprintf(stderr, gettext("missing source dataset "
693 (void) fprintf(stderr, gettext("missing target dataset "
698 (void) fprintf(stderr, gettext("too many arguments\n"));
702 /* open the source dataset */
703 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
706 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
709 * Now create the ancestors of the target dataset. If the
710 * target already exists and '-p' option was used we should not
713 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
716 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
721 ret = zfs_clone(zhp, argv[1], props);
723 /* create the mountpoint if necessary */
727 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
730 * If the user doesn't want the dataset
731 * automatically mounted, then skip the mount/share
734 if (should_auto_mount(clone)) {
735 if ((ret = zfs_mount(clone, NULL, 0)) != 0) {
736 (void) fprintf(stderr, gettext("clone "
737 "successfully created, "
738 "but not mounted\n"));
739 } else if ((ret = zfs_share(clone)) != 0) {
740 (void) fprintf(stderr, gettext("clone "
741 "successfully created, "
742 "but not shared\n"));
763 * zfs create [-pu] [-o prop=value] ... fs
764 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
766 * Create a new dataset. This command can be used to create filesystems
767 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
768 * For volumes, the user must specify a size to be used.
770 * The '-s' flag applies only to volumes, and indicates that we should not try
771 * to set the reservation for this volume. By default we set a reservation
772 * equal to the size for any volume. For pools with SPA_VERSION >=
773 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
775 * The '-p' flag creates all the non-existing ancestors of the target first.
777 * The '-u' flag prevents mounting of newly created file system.
780 zfs_do_create(int argc, char **argv)
782 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
783 zfs_handle_t *zhp = NULL;
784 uint64_t volsize = 0;
786 boolean_t noreserve = B_FALSE;
787 boolean_t bflag = B_FALSE;
788 boolean_t parents = B_FALSE;
789 boolean_t nomount = B_FALSE;
794 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
798 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) {
801 type = ZFS_TYPE_VOLUME;
802 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
803 (void) fprintf(stderr, gettext("bad volume "
804 "size '%s': %s\n"), optarg,
805 libzfs_error_description(g_zfs));
809 if (nvlist_add_uint64(props,
810 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
819 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
820 (void) fprintf(stderr, gettext("bad volume "
821 "block size '%s': %s\n"), optarg,
822 libzfs_error_description(g_zfs));
826 if (nvlist_add_uint64(props,
827 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
832 if (parseprop(props, optarg) != 0)
842 (void) fprintf(stderr, gettext("missing size "
846 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
852 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
853 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
854 "used when creating a volume\n"));
857 if (nomount && type != ZFS_TYPE_FILESYSTEM) {
858 (void) fprintf(stderr, gettext("'-u' can only be "
859 "used when creating a file system\n"));
866 /* check number of arguments */
868 (void) fprintf(stderr, gettext("missing %s argument\n"),
869 zfs_type_to_name(type));
873 (void) fprintf(stderr, gettext("too many arguments\n"));
877 if (type == ZFS_TYPE_VOLUME && !noreserve) {
878 zpool_handle_t *zpool_handle;
879 nvlist_t *real_props = NULL;
880 uint64_t spa_version;
882 zfs_prop_t resv_prop;
886 if ((p = strchr(argv[0], '/')) != NULL)
888 zpool_handle = zpool_open(g_zfs, argv[0]);
891 if (zpool_handle == NULL)
893 spa_version = zpool_get_prop_int(zpool_handle,
894 ZPOOL_PROP_VERSION, NULL);
895 if (spa_version >= SPA_VERSION_REFRESERVATION)
896 resv_prop = ZFS_PROP_REFRESERVATION;
898 resv_prop = ZFS_PROP_RESERVATION;
900 (void) snprintf(msg, sizeof (msg),
901 gettext("cannot create '%s'"), argv[0]);
902 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
903 props, 0, NULL, zpool_handle, msg)) == NULL) {
904 zpool_close(zpool_handle);
907 zpool_close(zpool_handle);
909 volsize = zvol_volsize_to_reservation(volsize, real_props);
910 nvlist_free(real_props);
912 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
914 if (nvlist_add_uint64(props,
915 zfs_prop_to_name(resv_prop), volsize) != 0) {
922 if (parents && zfs_name_valid(argv[0], type)) {
924 * Now create the ancestors of target dataset. If the target
925 * already exists and '-p' option was used we should not
928 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
932 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
937 if (zfs_create(g_zfs, argv[0], type, props) != 0)
940 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
946 * Mount and/or share the new filesystem as appropriate. We provide a
947 * verbose error message to let the user know that their filesystem was
948 * in fact created, even if we failed to mount or share it.
949 * If the user doesn't want the dataset automatically mounted,
950 * then skip the mount/share step altogether.
952 if (!nomount && should_auto_mount(zhp)) {
953 if (zfs_mount(zhp, NULL, 0) != 0) {
954 (void) fprintf(stderr, gettext("filesystem "
955 "successfully created, but not mounted\n"));
957 } else if (zfs_share(zhp) != 0) {
958 (void) fprintf(stderr, gettext("filesystem "
959 "successfully created, but not shared\n"));
976 * zfs destroy [-rRf] <fs, vol>
977 * zfs destroy [-rRd] <snap>
979 * -r Recursively destroy all children
980 * -R Recursively destroy all dependents, including clones
981 * -f Force unmounting of any dependents
982 * -d If we can't destroy now, mark for deferred destruction
984 * Destroys the given dataset. By default, it will unmount any filesystems,
985 * and refuse to destroy a dataset that has any dependents. A dependent can
986 * either be a child, or a clone of a child.
988 typedef struct destroy_cbdata {
991 boolean_t cb_recurse;
993 boolean_t cb_doclones;
994 zfs_handle_t *cb_target;
995 boolean_t cb_defer_destroy;
996 boolean_t cb_verbose;
997 boolean_t cb_parsable;
1000 nvlist_t *cb_batchedsnaps;
1002 /* first snap in contiguous run */
1004 /* previous snap in contiguous run */
1006 int64_t cb_snapused;
1012 * Check for any dependents based on the '-r' or '-R' flags.
1015 destroy_check_dependent(zfs_handle_t *zhp, void *data)
1017 destroy_cbdata_t *cbp = data;
1018 const char *tname = zfs_get_name(cbp->cb_target);
1019 const char *name = zfs_get_name(zhp);
1021 if (strncmp(tname, name, strlen(tname)) == 0 &&
1022 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
1024 * This is a direct descendant, not a clone somewhere else in
1027 if (cbp->cb_recurse)
1030 if (cbp->cb_first) {
1031 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1032 "%s has children\n"),
1033 zfs_get_name(cbp->cb_target),
1034 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1035 (void) fprintf(stderr, gettext("use '-r' to destroy "
1036 "the following datasets:\n"));
1037 cbp->cb_first = B_FALSE;
1038 cbp->cb_error = B_TRUE;
1041 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1044 * This is a clone. We only want to report this if the '-r'
1045 * wasn't specified, or the target is a snapshot.
1047 if (!cbp->cb_recurse &&
1048 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1051 if (cbp->cb_first) {
1052 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1053 "%s has dependent clones\n"),
1054 zfs_get_name(cbp->cb_target),
1055 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1056 (void) fprintf(stderr, gettext("use '-R' to destroy "
1057 "the following datasets:\n"));
1058 cbp->cb_first = B_FALSE;
1059 cbp->cb_error = B_TRUE;
1060 cbp->cb_dryrun = B_TRUE;
1063 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1072 destroy_callback(zfs_handle_t *zhp, void *data)
1074 destroy_cbdata_t *cb = data;
1075 const char *name = zfs_get_name(zhp);
1077 if (cb->cb_verbose) {
1078 if (cb->cb_parsable) {
1079 (void) printf("destroy\t%s\n", name);
1080 } else if (cb->cb_dryrun) {
1081 (void) printf(gettext("would destroy %s\n"),
1084 (void) printf(gettext("will destroy %s\n"),
1090 * Ignore pools (which we've already flagged as an error before getting
1093 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1094 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1098 if (cb->cb_dryrun) {
1104 * We batch up all contiguous snapshots (even of different
1105 * filesystems) and destroy them with one ioctl. We can't
1106 * simply do all snap deletions and then all fs deletions,
1107 * because we must delete a clone before its origin.
1109 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1110 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1112 int error = zfs_destroy_snaps_nvl(g_zfs,
1113 cb->cb_batchedsnaps, B_FALSE);
1114 fnvlist_free(cb->cb_batchedsnaps);
1115 cb->cb_batchedsnaps = fnvlist_alloc();
1118 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1119 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1130 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1132 destroy_cbdata_t *cb = arg;
1133 const char *name = zfs_get_name(zhp);
1136 if (nvlist_exists(cb->cb_nvl, name)) {
1137 if (cb->cb_firstsnap == NULL)
1138 cb->cb_firstsnap = strdup(name);
1139 if (cb->cb_prevsnap != NULL)
1140 free(cb->cb_prevsnap);
1141 /* this snap continues the current range */
1142 cb->cb_prevsnap = strdup(name);
1143 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1145 if (cb->cb_verbose) {
1146 if (cb->cb_parsable) {
1147 (void) printf("destroy\t%s\n", name);
1148 } else if (cb->cb_dryrun) {
1149 (void) printf(gettext("would destroy %s\n"),
1152 (void) printf(gettext("will destroy %s\n"),
1156 } else if (cb->cb_firstsnap != NULL) {
1157 /* end of this range */
1159 err = lzc_snaprange_space(cb->cb_firstsnap,
1160 cb->cb_prevsnap, &used);
1161 cb->cb_snapused += used;
1162 free(cb->cb_firstsnap);
1163 cb->cb_firstsnap = NULL;
1164 free(cb->cb_prevsnap);
1165 cb->cb_prevsnap = NULL;
1172 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1175 assert(cb->cb_firstsnap == NULL);
1176 assert(cb->cb_prevsnap == NULL);
1177 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb, 0, 0);
1178 if (cb->cb_firstsnap != NULL) {
1181 err = lzc_snaprange_space(cb->cb_firstsnap,
1182 cb->cb_prevsnap, &used);
1184 cb->cb_snapused += used;
1185 free(cb->cb_firstsnap);
1186 cb->cb_firstsnap = NULL;
1187 free(cb->cb_prevsnap);
1188 cb->cb_prevsnap = NULL;
1194 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1196 destroy_cbdata_t *cb = arg;
1199 /* Check for clones. */
1200 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1201 cb->cb_target = zhp;
1202 cb->cb_first = B_TRUE;
1203 err = zfs_iter_dependents(zhp, B_TRUE,
1204 destroy_check_dependent, cb);
1208 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1216 gather_snapshots(zfs_handle_t *zhp, void *arg)
1218 destroy_cbdata_t *cb = arg;
1221 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1227 if (cb->cb_verbose) {
1228 err = destroy_print_snapshots(zhp, cb);
1234 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1242 destroy_clones(destroy_cbdata_t *cb)
1245 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1247 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1248 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1251 boolean_t defer = cb->cb_defer_destroy;
1255 * We can't defer destroy non-snapshots, so set it to
1256 * false while destroying the clones.
1258 cb->cb_defer_destroy = B_FALSE;
1259 err = zfs_iter_dependents(zhp, B_FALSE,
1260 destroy_callback, cb);
1261 cb->cb_defer_destroy = defer;
1271 zfs_do_destroy(int argc, char **argv)
1273 destroy_cbdata_t cb = { 0 };
1277 zfs_handle_t *zhp = NULL;
1279 zfs_type_t type = ZFS_TYPE_DATASET;
1282 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1285 cb.cb_verbose = B_TRUE;
1288 cb.cb_verbose = B_TRUE;
1289 cb.cb_parsable = B_TRUE;
1292 cb.cb_dryrun = B_TRUE;
1295 cb.cb_defer_destroy = B_TRUE;
1296 type = ZFS_TYPE_SNAPSHOT;
1299 cb.cb_force = B_TRUE;
1302 cb.cb_recurse = B_TRUE;
1305 cb.cb_recurse = B_TRUE;
1306 cb.cb_doclones = B_TRUE;
1310 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1319 /* check number of arguments */
1321 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1325 (void) fprintf(stderr, gettext("too many arguments\n"));
1329 at = strchr(argv[0], '@');
1330 pound = strchr(argv[0], '#');
1333 /* Build the list of snaps to destroy in cb_nvl. */
1334 cb.cb_nvl = fnvlist_alloc();
1337 zhp = zfs_open(g_zfs, argv[0],
1338 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1342 cb.cb_snapspec = at + 1;
1343 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1349 if (nvlist_empty(cb.cb_nvl)) {
1350 (void) fprintf(stderr, gettext("could not find any "
1351 "snapshots to destroy; check snapshot names.\n"));
1356 if (cb.cb_verbose) {
1358 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1359 if (cb.cb_parsable) {
1360 (void) printf("reclaim\t%llu\n",
1362 } else if (cb.cb_dryrun) {
1363 (void) printf(gettext("would reclaim %s\n"),
1366 (void) printf(gettext("will reclaim %s\n"),
1371 if (!cb.cb_dryrun) {
1372 if (cb.cb_doclones) {
1373 cb.cb_batchedsnaps = fnvlist_alloc();
1374 err = destroy_clones(&cb);
1376 err = zfs_destroy_snaps_nvl(g_zfs,
1377 cb.cb_batchedsnaps, B_FALSE);
1385 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1386 cb.cb_defer_destroy);
1392 } else if (pound != NULL) {
1397 (void) fprintf(stderr,
1398 "dryrun is not supported with bookmark\n");
1402 if (cb.cb_defer_destroy) {
1403 (void) fprintf(stderr,
1404 "defer destroy is not supported with bookmark\n");
1408 if (cb.cb_recurse) {
1409 (void) fprintf(stderr,
1410 "recursive is not supported with bookmark\n");
1414 if (!zfs_bookmark_exists(argv[0])) {
1415 (void) fprintf(stderr, gettext("bookmark '%s' "
1416 "does not exist.\n"), argv[0]);
1420 nvl = fnvlist_alloc();
1421 fnvlist_add_boolean(nvl, argv[0]);
1423 err = lzc_destroy_bookmarks(nvl, NULL);
1425 (void) zfs_standard_error(g_zfs, err,
1426 "cannot destroy bookmark");
1429 nvlist_free(cb.cb_nvl);
1433 /* Open the given dataset */
1434 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1440 * Perform an explicit check for pools before going any further.
1442 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1443 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1444 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1445 "operation does not apply to pools\n"),
1447 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1448 "%s' to destroy all datasets in the pool\n"),
1450 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1451 "to destroy the pool itself\n"), zfs_get_name(zhp));
1457 * Check for any dependents and/or clones.
1459 cb.cb_first = B_TRUE;
1460 if (!cb.cb_doclones &&
1461 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1472 cb.cb_batchedsnaps = fnvlist_alloc();
1473 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1480 * Do the real thing. The callback will close the
1481 * handle regardless of whether it succeeds or not.
1483 err = destroy_callback(zhp, &cb);
1486 err = zfs_destroy_snaps_nvl(g_zfs,
1487 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1494 fnvlist_free(cb.cb_batchedsnaps);
1495 fnvlist_free(cb.cb_nvl);
1502 is_recvd_column(zprop_get_cbdata_t *cbp)
1505 zfs_get_column_t col;
1507 for (i = 0; i < ZFS_GET_NCOLS &&
1508 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1509 if (col == GET_COL_RECVD)
1515 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1516 * < all | property[,property]... > < fs | snap | vol > ...
1518 * -r recurse over any child datasets
1519 * -H scripted mode. Headers are stripped, and fields are separated
1520 * by tabs instead of spaces.
1521 * -o Set of fields to display. One of "name,property,value,
1522 * received,source". Default is "name,property,value,source".
1523 * "all" is an alias for all five.
1524 * -s Set of sources to allow. One of
1525 * "local,default,inherited,received,temporary,none". Default is
1527 * -p Display values in parsable (literal) format.
1529 * Prints properties for the given datasets. The user can control which
1530 * columns to display as well as which property types to allow.
1534 * Invoked to display the properties for a single dataset.
1537 get_callback(zfs_handle_t *zhp, void *data)
1539 char buf[ZFS_MAXPROPLEN];
1540 char rbuf[ZFS_MAXPROPLEN];
1541 zprop_source_t sourcetype;
1542 char source[ZFS_MAX_DATASET_NAME_LEN];
1543 zprop_get_cbdata_t *cbp = data;
1544 nvlist_t *user_props = zfs_get_user_props(zhp);
1545 zprop_list_t *pl = cbp->cb_proplist;
1549 boolean_t received = is_recvd_column(cbp);
1551 for (; pl != NULL; pl = pl->pl_next) {
1552 char *recvdval = NULL;
1554 * Skip the special fake placeholder. This will also skip over
1555 * the name property when 'all' is specified.
1557 if (pl->pl_prop == ZFS_PROP_NAME &&
1558 pl == cbp->cb_proplist)
1561 if (pl->pl_prop != ZPROP_INVAL) {
1562 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1563 sizeof (buf), &sourcetype, source,
1565 cbp->cb_literal) != 0) {
1568 if (!zfs_prop_valid_for_type(pl->pl_prop,
1569 ZFS_TYPE_DATASET)) {
1570 (void) fprintf(stderr,
1571 gettext("No such property '%s'\n"),
1572 zfs_prop_to_name(pl->pl_prop));
1575 sourcetype = ZPROP_SRC_NONE;
1576 (void) strlcpy(buf, "-", sizeof (buf));
1579 if (received && (zfs_prop_get_recvd(zhp,
1580 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1581 cbp->cb_literal) == 0))
1584 zprop_print_one_property(zfs_get_name(zhp), cbp,
1585 zfs_prop_to_name(pl->pl_prop),
1586 buf, sourcetype, source, recvdval);
1587 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1588 sourcetype = ZPROP_SRC_LOCAL;
1590 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1591 buf, sizeof (buf), cbp->cb_literal) != 0) {
1592 sourcetype = ZPROP_SRC_NONE;
1593 (void) strlcpy(buf, "-", sizeof (buf));
1596 zprop_print_one_property(zfs_get_name(zhp), cbp,
1597 pl->pl_user_prop, buf, sourcetype, source, NULL);
1598 } else if (zfs_prop_written(pl->pl_user_prop)) {
1599 sourcetype = ZPROP_SRC_LOCAL;
1601 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1602 buf, sizeof (buf), cbp->cb_literal) != 0) {
1603 sourcetype = ZPROP_SRC_NONE;
1604 (void) strlcpy(buf, "-", sizeof (buf));
1607 zprop_print_one_property(zfs_get_name(zhp), cbp,
1608 pl->pl_user_prop, buf, sourcetype, source, NULL);
1610 if (nvlist_lookup_nvlist(user_props,
1611 pl->pl_user_prop, &propval) != 0) {
1614 sourcetype = ZPROP_SRC_NONE;
1617 verify(nvlist_lookup_string(propval,
1618 ZPROP_VALUE, &strval) == 0);
1619 verify(nvlist_lookup_string(propval,
1620 ZPROP_SOURCE, &sourceval) == 0);
1622 if (strcmp(sourceval,
1623 zfs_get_name(zhp)) == 0) {
1624 sourcetype = ZPROP_SRC_LOCAL;
1625 } else if (strcmp(sourceval,
1626 ZPROP_SOURCE_VAL_RECVD) == 0) {
1627 sourcetype = ZPROP_SRC_RECEIVED;
1629 sourcetype = ZPROP_SRC_INHERITED;
1630 (void) strlcpy(source,
1631 sourceval, sizeof (source));
1635 if (received && (zfs_prop_get_recvd(zhp,
1636 pl->pl_user_prop, rbuf, sizeof (rbuf),
1637 cbp->cb_literal) == 0))
1640 zprop_print_one_property(zfs_get_name(zhp), cbp,
1641 pl->pl_user_prop, strval, sourcetype,
1650 zfs_do_get(int argc, char **argv)
1652 zprop_get_cbdata_t cb = { 0 };
1653 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1654 int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
1655 char *value, *fields;
1658 zprop_list_t fake_name = { 0 };
1661 * Set up default columns and sources.
1663 cb.cb_sources = ZPROP_SRC_ALL;
1664 cb.cb_columns[0] = GET_COL_NAME;
1665 cb.cb_columns[1] = GET_COL_PROPERTY;
1666 cb.cb_columns[2] = GET_COL_VALUE;
1667 cb.cb_columns[3] = GET_COL_SOURCE;
1668 cb.cb_type = ZFS_TYPE_DATASET;
1671 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1674 cb.cb_literal = B_TRUE;
1677 limit = parse_depth(optarg, &flags);
1680 flags |= ZFS_ITER_RECURSE;
1683 cb.cb_scripted = B_TRUE;
1686 (void) fprintf(stderr, gettext("missing argument for "
1687 "'%c' option\n"), optopt);
1692 * Process the set of columns to display. We zero out
1693 * the structure to give us a blank slate.
1695 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1697 while (*optarg != '\0') {
1698 static char *col_subopts[] =
1699 { "name", "property", "value", "received",
1700 "source", "all", NULL };
1702 if (i == ZFS_GET_NCOLS) {
1703 (void) fprintf(stderr, gettext("too "
1704 "many fields given to -o "
1709 switch (getsubopt(&optarg, col_subopts,
1712 cb.cb_columns[i++] = GET_COL_NAME;
1715 cb.cb_columns[i++] = GET_COL_PROPERTY;
1718 cb.cb_columns[i++] = GET_COL_VALUE;
1721 cb.cb_columns[i++] = GET_COL_RECVD;
1722 flags |= ZFS_ITER_RECVD_PROPS;
1725 cb.cb_columns[i++] = GET_COL_SOURCE;
1729 (void) fprintf(stderr,
1730 gettext("\"all\" conflicts "
1731 "with specific fields "
1732 "given to -o option\n"));
1735 cb.cb_columns[0] = GET_COL_NAME;
1736 cb.cb_columns[1] = GET_COL_PROPERTY;
1737 cb.cb_columns[2] = GET_COL_VALUE;
1738 cb.cb_columns[3] = GET_COL_RECVD;
1739 cb.cb_columns[4] = GET_COL_SOURCE;
1740 flags |= ZFS_ITER_RECVD_PROPS;
1744 (void) fprintf(stderr,
1745 gettext("invalid column name "
1746 "'%s'\n"), suboptarg);
1754 while (*optarg != '\0') {
1755 static char *source_subopts[] = {
1756 "local", "default", "inherited",
1757 "received", "temporary", "none",
1760 switch (getsubopt(&optarg, source_subopts,
1763 cb.cb_sources |= ZPROP_SRC_LOCAL;
1766 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1769 cb.cb_sources |= ZPROP_SRC_INHERITED;
1772 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1775 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1778 cb.cb_sources |= ZPROP_SRC_NONE;
1781 (void) fprintf(stderr,
1782 gettext("invalid source "
1783 "'%s'\n"), suboptarg);
1791 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1792 while (*optarg != '\0') {
1793 static char *type_subopts[] = { "filesystem",
1794 "volume", "snapshot", "bookmark",
1797 switch (getsubopt(&optarg, type_subopts,
1800 types |= ZFS_TYPE_FILESYSTEM;
1803 types |= ZFS_TYPE_VOLUME;
1806 types |= ZFS_TYPE_SNAPSHOT;
1809 types |= ZFS_TYPE_BOOKMARK;
1812 types = ZFS_TYPE_DATASET |
1817 (void) fprintf(stderr,
1818 gettext("invalid type '%s'\n"),
1826 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1836 (void) fprintf(stderr, gettext("missing property "
1843 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1851 * As part of zfs_expand_proplist(), we keep track of the maximum column
1852 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1853 * need to know the maximum name length. However, the user likely did
1854 * not specify 'name' as one of the properties to fetch, so we need to
1855 * make sure we always include at least this property for
1856 * print_get_headers() to work properly.
1858 if (cb.cb_proplist != NULL) {
1859 fake_name.pl_prop = ZFS_PROP_NAME;
1860 fake_name.pl_width = strlen(gettext("NAME"));
1861 fake_name.pl_next = cb.cb_proplist;
1862 cb.cb_proplist = &fake_name;
1865 cb.cb_first = B_TRUE;
1867 /* run for each object */
1868 ret = zfs_for_each(argc, argv, flags, types, NULL,
1869 &cb.cb_proplist, limit, get_callback, &cb);
1871 if (cb.cb_proplist == &fake_name)
1872 zprop_free_list(fake_name.pl_next);
1874 zprop_free_list(cb.cb_proplist);
1880 * inherit [-rS] <property> <fs|vol> ...
1882 * -r Recurse over all children
1883 * -S Revert to received value, if any
1885 * For each dataset specified on the command line, inherit the given property
1886 * from its parent. Inheriting a property at the pool level will cause it to
1887 * use the default value. The '-r' flag will recurse over all children, and is
1888 * useful for setting a property on a hierarchy-wide basis, regardless of any
1889 * local modifications for each dataset.
1892 typedef struct inherit_cbdata {
1893 const char *cb_propname;
1894 boolean_t cb_received;
1898 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1900 inherit_cbdata_t *cb = data;
1901 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1904 * If we're doing it recursively, then ignore properties that
1905 * are not valid for this type of dataset.
1907 if (prop != ZPROP_INVAL &&
1908 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1911 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1915 inherit_cb(zfs_handle_t *zhp, void *data)
1917 inherit_cbdata_t *cb = data;
1919 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1923 zfs_do_inherit(int argc, char **argv)
1927 inherit_cbdata_t cb = { 0 };
1931 boolean_t received = B_FALSE;
1934 while ((c = getopt(argc, argv, "rS")) != -1) {
1937 flags |= ZFS_ITER_RECURSE;
1944 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1953 /* check number of arguments */
1955 (void) fprintf(stderr, gettext("missing property argument\n"));
1959 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1967 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1968 if (zfs_prop_readonly(prop)) {
1969 (void) fprintf(stderr, gettext(
1970 "%s property is read-only\n"),
1974 if (!zfs_prop_inheritable(prop) && !received) {
1975 (void) fprintf(stderr, gettext("'%s' property cannot "
1976 "be inherited\n"), propname);
1977 if (prop == ZFS_PROP_QUOTA ||
1978 prop == ZFS_PROP_RESERVATION ||
1979 prop == ZFS_PROP_REFQUOTA ||
1980 prop == ZFS_PROP_REFRESERVATION) {
1981 (void) fprintf(stderr, gettext("use 'zfs set "
1982 "%s=none' to clear\n"), propname);
1983 (void) fprintf(stderr, gettext("use 'zfs "
1984 "inherit -S %s' to revert to received "
1985 "value\n"), propname);
1989 if (received && (prop == ZFS_PROP_VOLSIZE ||
1990 prop == ZFS_PROP_VERSION)) {
1991 (void) fprintf(stderr, gettext("'%s' property cannot "
1992 "be reverted to a received value\n"), propname);
1995 } else if (!zfs_prop_user(propname)) {
1996 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
2001 cb.cb_propname = propname;
2002 cb.cb_received = received;
2004 if (flags & ZFS_ITER_RECURSE) {
2005 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2006 NULL, NULL, 0, inherit_recurse_cb, &cb);
2008 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2009 NULL, NULL, 0, inherit_cb, &cb);
2015 typedef struct upgrade_cbdata {
2016 uint64_t cb_numupgraded;
2017 uint64_t cb_numsamegraded;
2018 uint64_t cb_numfailed;
2019 uint64_t cb_version;
2021 boolean_t cb_foundone;
2022 char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
2026 same_pool(zfs_handle_t *zhp, const char *name)
2028 int len1 = strcspn(name, "/@");
2029 const char *zhname = zfs_get_name(zhp);
2030 int len2 = strcspn(zhname, "/@");
2034 return (strncmp(name, zhname, len1) == 0);
2038 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2040 upgrade_cbdata_t *cb = data;
2041 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2043 /* list if it's old/new */
2044 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2045 (cb->cb_newer && version > ZPL_VERSION)) {
2048 str = gettext("The following filesystems are "
2049 "formatted using a newer software version and\n"
2050 "cannot be accessed on the current system.\n\n");
2052 str = gettext("The following filesystems are "
2053 "out of date, and can be upgraded. After being\n"
2054 "upgraded, these filesystems (and any 'zfs send' "
2055 "streams generated from\n"
2056 "subsequent snapshots) will no longer be "
2057 "accessible by older software versions.\n\n");
2060 if (!cb->cb_foundone) {
2062 (void) printf(gettext("VER FILESYSTEM\n"));
2063 (void) printf(gettext("--- ------------\n"));
2064 cb->cb_foundone = B_TRUE;
2067 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2074 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2076 upgrade_cbdata_t *cb = data;
2077 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2078 int needed_spa_version;
2081 if (zfs_spa_version(zhp, &spa_version) < 0)
2084 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2086 if (needed_spa_version < 0)
2089 if (spa_version < needed_spa_version) {
2091 (void) printf(gettext("%s: can not be "
2092 "upgraded; the pool version needs to first "
2093 "be upgraded\nto version %d\n\n"),
2094 zfs_get_name(zhp), needed_spa_version);
2100 if (version < cb->cb_version) {
2102 (void) snprintf(verstr, sizeof (verstr),
2103 "%llu", cb->cb_version);
2104 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2106 * If they did "zfs upgrade -a", then we could
2107 * be doing ioctls to different pools. We need
2108 * to log this history once to each pool, and bypass
2109 * the normal history logging that happens in main().
2111 (void) zpool_log_history(g_zfs, history_str);
2112 log_history = B_FALSE;
2114 if (zfs_prop_set(zhp, "version", verstr) == 0)
2115 cb->cb_numupgraded++;
2118 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2119 } else if (version > cb->cb_version) {
2120 /* can't downgrade */
2121 (void) printf(gettext("%s: can not be downgraded; "
2122 "it is already at version %u\n"),
2123 zfs_get_name(zhp), version);
2126 cb->cb_numsamegraded++;
2134 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2137 zfs_do_upgrade(int argc, char **argv)
2139 boolean_t all = B_FALSE;
2140 boolean_t showversions = B_FALSE;
2142 upgrade_cbdata_t cb = { 0 };
2144 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2147 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2150 flags |= ZFS_ITER_RECURSE;
2153 showversions = B_TRUE;
2156 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2157 optarg, &cb.cb_version) != 0) {
2158 (void) fprintf(stderr,
2159 gettext("invalid version %s\n"), optarg);
2168 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2177 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2179 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2180 cb.cb_version || argc))
2182 if ((all || argc) && (showversions))
2188 /* Show info on available versions. */
2189 (void) printf(gettext("The following filesystem versions are "
2191 (void) printf(gettext("VER DESCRIPTION\n"));
2192 (void) printf("--- -----------------------------------------"
2193 "---------------\n");
2194 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2195 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2196 (void) printf(gettext(" 3 Case insensitive and filesystem "
2197 "user identifier (FUID)\n"));
2198 (void) printf(gettext(" 4 userquota, groupquota "
2200 (void) printf(gettext(" 5 System attributes\n"));
2201 (void) printf(gettext("\nFor more information on a particular "
2202 "version, including supported releases,\n"));
2203 (void) printf("see the ZFS Administration Guide.\n\n");
2205 } else if (argc || all) {
2206 /* Upgrade filesystems */
2207 if (cb.cb_version == 0)
2208 cb.cb_version = ZPL_VERSION;
2209 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2210 NULL, NULL, 0, upgrade_set_callback, &cb);
2211 (void) printf(gettext("%llu filesystems upgraded\n"),
2213 if (cb.cb_numsamegraded) {
2214 (void) printf(gettext("%llu filesystems already at "
2216 cb.cb_numsamegraded);
2218 if (cb.cb_numfailed != 0)
2221 /* List old-version filesystems */
2223 (void) printf(gettext("This system is currently running "
2224 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2226 flags |= ZFS_ITER_RECURSE;
2227 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2228 NULL, NULL, 0, upgrade_list_callback, &cb);
2230 found = cb.cb_foundone;
2231 cb.cb_foundone = B_FALSE;
2232 cb.cb_newer = B_TRUE;
2234 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2235 NULL, NULL, 0, upgrade_list_callback, &cb);
2237 if (!cb.cb_foundone && !found) {
2238 (void) printf(gettext("All filesystems are "
2239 "formatted with the current version.\n"));
2247 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2248 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2249 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2250 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2252 * -H Scripted mode; elide headers and separate columns by tabs.
2253 * -i Translate SID to POSIX ID.
2254 * -n Print numeric ID instead of user/group name.
2255 * -o Control which fields to display.
2256 * -p Use exact (parsable) numeric output.
2257 * -s Specify sort columns, descending order.
2258 * -S Specify sort columns, ascending order.
2259 * -t Control which object types to display.
2261 * Displays space consumed by, and quotas on, each user in the specified
2262 * filesystem or snapshot.
2265 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2266 enum us_field_types {
2272 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2273 static char *us_field_names[] = { "type", "name", "used", "quota" };
2274 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2276 #define USTYPE_PSX_GRP (1 << 0)
2277 #define USTYPE_PSX_USR (1 << 1)
2278 #define USTYPE_SMB_GRP (1 << 2)
2279 #define USTYPE_SMB_USR (1 << 3)
2280 #define USTYPE_ALL \
2281 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2283 static int us_type_bits[] = {
2290 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2293 typedef struct us_node {
2295 uu_avl_node_t usn_avlnode;
2296 uu_list_node_t usn_listnode;
2299 typedef struct us_cbdata {
2301 uu_avl_pool_t *cb_avl_pool;
2303 boolean_t cb_numname;
2304 boolean_t cb_nicenum;
2305 boolean_t cb_sid2posix;
2306 zfs_userquota_prop_t cb_prop;
2307 zfs_sort_column_t *cb_sortcol;
2308 size_t cb_width[USFIELD_LAST];
2311 static boolean_t us_populated = B_FALSE;
2314 zfs_sort_column_t *si_sortcol;
2315 boolean_t si_numname;
2319 us_field_index(char *field)
2323 for (i = 0; i < USFIELD_LAST; i++) {
2324 if (strcmp(field, us_field_names[i]) == 0)
2332 us_compare(const void *larg, const void *rarg, void *unused)
2334 const us_node_t *l = larg;
2335 const us_node_t *r = rarg;
2336 us_sort_info_t *si = (us_sort_info_t *)unused;
2337 zfs_sort_column_t *sortcol = si->si_sortcol;
2338 boolean_t numname = si->si_numname;
2339 nvlist_t *lnvl = l->usn_nvl;
2340 nvlist_t *rnvl = r->usn_nvl;
2344 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2351 zfs_prop_t prop = sortcol->sc_prop;
2352 const char *propname = NULL;
2353 boolean_t reverse = sortcol->sc_reverse;
2358 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2359 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2361 rc = (rv32 < lv32) ? 1 : -1;
2367 (void) nvlist_lookup_uint64(lnvl, propname,
2369 (void) nvlist_lookup_uint64(rnvl, propname,
2372 rc = (rv64 < lv64) ? 1 : -1;
2374 if ((nvlist_lookup_string(lnvl, propname,
2375 &lvstr) == ENOENT) ||
2376 (nvlist_lookup_string(rnvl, propname,
2377 &rvstr) == ENOENT)) {
2380 rc = strcmp(lvstr, rvstr);
2384 case ZFS_PROP_QUOTA:
2387 if (prop == ZFS_PROP_USED)
2391 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2392 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2394 rc = (rv64 < lv64) ? 1 : -1;
2403 return (reverse ? 1 : -1);
2405 return (reverse ? -1 : 1);
2410 * If entries still seem to be the same, check if they are of the same
2411 * type (smbentity is added only if we are doing SID to POSIX ID
2412 * translation where we can have duplicate type/name combinations).
2414 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2415 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2417 return (lvb < rvb ? -1 : 1);
2422 static inline const char *
2423 us_type2str(unsigned field_type)
2425 switch (field_type) {
2426 case USTYPE_PSX_USR:
2427 return ("POSIX User");
2428 case USTYPE_PSX_GRP:
2429 return ("POSIX Group");
2430 case USTYPE_SMB_USR:
2431 return ("SMB User");
2432 case USTYPE_SMB_GRP:
2433 return ("SMB Group");
2435 return ("Undefined");
2440 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2442 us_cbdata_t *cb = (us_cbdata_t *)arg;
2443 zfs_userquota_prop_t prop = cb->cb_prop;
2448 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2449 uu_avl_t *avl = cb->cb_avl;
2453 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2455 const char *typestr;
2459 int typeidx, nameidx, sizeidx;
2460 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2461 boolean_t smbentity = B_FALSE;
2463 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2465 node = safe_malloc(sizeof (us_node_t));
2466 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2467 node->usn_nvl = props;
2469 if (domain != NULL && domain[0] != '\0') {
2471 char sid[MAXNAMELEN + 32];
2475 int flag = IDMAP_REQ_FLG_USE_CACHE;
2480 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2482 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2483 type = USTYPE_SMB_GRP;
2485 err = sid_to_id(sid, B_FALSE, &id);
2488 type = USTYPE_SMB_USR;
2490 err = sid_to_id(sid, B_TRUE, &id);
2497 if (!cb->cb_sid2posix) {
2498 if (type == USTYPE_SMB_USR) {
2499 (void) idmap_getwinnamebyuid(rid, flag,
2502 (void) idmap_getwinnamebygid(rid, flag,
2512 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2514 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2515 type = USTYPE_PSX_GRP;
2516 if (!cb->cb_numname) {
2519 if ((g = getgrgid(rid)) != NULL)
2523 type = USTYPE_PSX_USR;
2524 if (!cb->cb_numname) {
2527 if ((p = getpwuid(rid)) != NULL)
2534 * Make sure that the type/name combination is unique when doing
2535 * SID to POSIX ID translation (hence changing the type from SMB to
2538 if (cb->cb_sid2posix &&
2539 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2542 /* Calculate/update width of TYPE field */
2543 typestr = us_type2str(type);
2544 typelen = strlen(gettext(typestr));
2545 typeidx = us_field_index("type");
2546 if (typelen > cb->cb_width[typeidx])
2547 cb->cb_width[typeidx] = typelen;
2548 if (nvlist_add_uint32(props, "type", type) != 0)
2551 /* Calculate/update width of NAME field */
2552 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2553 if (nvlist_add_uint64(props, "name", rid) != 0)
2555 namelen = snprintf(NULL, 0, "%u", rid);
2557 if (nvlist_add_string(props, "name", name) != 0)
2559 namelen = strlen(name);
2561 nameidx = us_field_index("name");
2562 if (namelen > cb->cb_width[nameidx])
2563 cb->cb_width[nameidx] = namelen;
2566 * Check if this type/name combination is in the list and update it;
2567 * otherwise add new node to the list.
2569 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2570 uu_avl_insert(avl, node, idx);
2575 props = node->usn_nvl;
2578 /* Calculate/update width of USED/QUOTA fields */
2580 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2582 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2583 sizelen = strlen(sizebuf);
2584 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2586 if (!nvlist_exists(props, "quota"))
2587 (void) nvlist_add_uint64(props, "quota", 0);
2590 if (!nvlist_exists(props, "used"))
2591 (void) nvlist_add_uint64(props, "used", 0);
2593 sizeidx = us_field_index(propname);
2594 if (sizelen > cb->cb_width[sizeidx])
2595 cb->cb_width[sizeidx] = sizelen;
2597 if (nvlist_add_uint64(props, propname, space) != 0)
2604 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2605 size_t *width, us_node_t *node)
2607 nvlist_t *nvl = node->usn_nvl;
2608 char valstr[MAXNAMELEN];
2609 boolean_t first = B_TRUE;
2615 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2616 if (!(ustype & types))
2619 while ((field = fields[cfield]) != USFIELD_LAST) {
2620 nvpair_t *nvp = NULL;
2624 char *strval = NULL;
2626 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2627 if (strcmp(nvpair_name(nvp),
2628 us_field_names[field]) == 0)
2632 type = nvpair_type(nvp);
2634 case DATA_TYPE_UINT32:
2635 (void) nvpair_value_uint32(nvp, &val32);
2637 case DATA_TYPE_UINT64:
2638 (void) nvpair_value_uint64(nvp, &val64);
2640 case DATA_TYPE_STRING:
2641 (void) nvpair_value_string(nvp, &strval);
2644 (void) fprintf(stderr, "invalid data type\n");
2649 strval = (char *)us_type2str(val32);
2652 if (type == DATA_TYPE_UINT64) {
2653 (void) sprintf(valstr, "%llu", val64);
2659 if (type == DATA_TYPE_UINT64) {
2661 (void) sprintf(valstr, "%llu", val64);
2663 zfs_nicenum(val64, valstr,
2666 if (field == USFIELD_QUOTA &&
2667 strcmp(valstr, "0") == 0)
2677 (void) printf("\t");
2682 (void) printf("%s", strval);
2683 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2684 (void) printf("%-*s", width[field], strval);
2686 (void) printf("%*s", width[field], strval);
2692 (void) printf("\n");
2696 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2697 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2705 boolean_t first = B_TRUE;
2707 while ((field = fields[cfield]) != USFIELD_LAST) {
2708 col = gettext(us_field_hdr[field]);
2709 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2710 (void) printf(first ? "%-*s" : " %-*s",
2713 (void) printf(first ? "%*s" : " %*s",
2719 (void) printf("\n");
2722 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2723 print_us_node(scripted, parsable, fields, types, width, node);
2725 nvlist_free(node->usn_nvl);
2730 zfs_do_userspace(int argc, char **argv)
2733 zfs_userquota_prop_t p;
2735 uu_avl_pool_t *avl_pool;
2737 uu_avl_walk_t *walk;
2739 char deffields[] = "type,name,used,quota";
2740 char *ofield = NULL;
2741 char *tfield = NULL;
2745 boolean_t scripted = B_FALSE;
2746 boolean_t prtnum = B_FALSE;
2747 boolean_t parsable = B_FALSE;
2748 boolean_t sid2posix = B_FALSE;
2751 zfs_sort_column_t *sortcol = NULL;
2752 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2756 uu_list_pool_t *listpool;
2758 uu_avl_index_t idx = 0;
2759 uu_list_index_t idx2 = 0;
2764 if (strcmp(argv[0], "groupspace") == 0)
2765 /* Toggle default group types */
2766 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2768 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2784 if (zfs_add_sort_column(&sortcol, optarg,
2785 c == 's' ? B_FALSE : B_TRUE) != 0) {
2786 (void) fprintf(stderr,
2787 gettext("invalid field '%s'\n"), optarg);
2798 (void) fprintf(stderr, gettext("missing argument for "
2799 "'%c' option\n"), optopt);
2803 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2813 (void) fprintf(stderr, gettext("missing dataset name\n"));
2817 (void) fprintf(stderr, gettext("too many arguments\n"));
2821 /* Use default output fields if not specified using -o */
2825 if ((delim = strchr(ofield, ',')) != NULL)
2827 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2828 (void) fprintf(stderr, gettext("invalid type '%s' "
2829 "for -o option\n"), ofield);
2834 } while (delim != NULL);
2835 fields[cfield] = USFIELD_LAST;
2837 /* Override output types (-t option) */
2838 if (tfield != NULL) {
2842 boolean_t found = B_FALSE;
2844 if ((delim = strchr(tfield, ',')) != NULL)
2846 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2848 if (strcmp(tfield, us_type_names[i]) == 0) {
2850 types |= us_type_bits[i];
2855 (void) fprintf(stderr, gettext("invalid type "
2856 "'%s' for -t option\n"), tfield);
2861 } while (delim != NULL);
2864 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2867 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2868 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2870 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2873 /* Always add default sorting columns */
2874 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2875 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2877 cb.cb_sortcol = sortcol;
2878 cb.cb_numname = prtnum;
2879 cb.cb_nicenum = !parsable;
2880 cb.cb_avl_pool = avl_pool;
2881 cb.cb_avl = avl_tree;
2882 cb.cb_sid2posix = sid2posix;
2884 for (i = 0; i < USFIELD_LAST; i++)
2885 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2887 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2888 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2889 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2890 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2891 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2894 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2899 if ((node = uu_avl_first(avl_tree)) == NULL)
2902 us_populated = B_TRUE;
2904 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2905 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2906 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2907 uu_list_node_init(node, &node->usn_listnode, listpool);
2909 while (node != NULL) {
2911 node = uu_avl_next(avl_tree, node);
2912 uu_avl_remove(avl_tree, rmnode);
2913 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2914 uu_list_insert(list, rmnode, idx2);
2917 for (node = uu_list_first(list); node != NULL;
2918 node = uu_list_next(list, node)) {
2919 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2921 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2922 uu_avl_insert(avl_tree, node, idx);
2925 uu_list_destroy(list);
2926 uu_list_pool_destroy(listpool);
2928 /* Print and free node nvlist memory */
2929 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2932 zfs_free_sort_columns(sortcol);
2934 /* Clean up the AVL tree */
2935 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2938 while ((node = uu_avl_walk_next(walk)) != NULL) {
2939 uu_avl_remove(cb.cb_avl, node);
2943 uu_avl_walk_end(walk);
2944 uu_avl_destroy(avl_tree);
2945 uu_avl_pool_destroy(avl_pool);
2951 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2952 * [-t type[,...]] [filesystem|volume|snapshot] ...
2954 * -H Scripted mode; elide headers and separate columns by tabs.
2955 * -p Display values in parsable (literal) format.
2956 * -r Recurse over all children.
2957 * -d Limit recursion by depth.
2958 * -o Control which fields to display.
2959 * -s Specify sort columns, descending order.
2960 * -S Specify sort columns, ascending order.
2961 * -t Control which object types to display.
2963 * When given no arguments, list all filesystems in the system.
2964 * Otherwise, list the specified datasets, optionally recursing down them if
2965 * '-r' is specified.
2967 typedef struct list_cbdata {
2969 boolean_t cb_literal;
2970 boolean_t cb_scripted;
2971 zprop_list_t *cb_proplist;
2975 * Given a list of columns to display, output appropriate headers for each one.
2978 print_header(list_cbdata_t *cb)
2980 zprop_list_t *pl = cb->cb_proplist;
2981 char headerbuf[ZFS_MAXPROPLEN];
2984 boolean_t first = B_TRUE;
2985 boolean_t right_justify;
2987 for (; pl != NULL; pl = pl->pl_next) {
2994 right_justify = B_FALSE;
2995 if (pl->pl_prop != ZPROP_INVAL) {
2996 header = zfs_prop_column_name(pl->pl_prop);
2997 right_justify = zfs_prop_align_right(pl->pl_prop);
2999 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3000 headerbuf[i] = toupper(pl->pl_user_prop[i]);
3001 headerbuf[i] = '\0';
3005 if (pl->pl_next == NULL && !right_justify)
3006 (void) printf("%s", header);
3007 else if (right_justify)
3008 (void) printf("%*s", pl->pl_width, header);
3010 (void) printf("%-*s", pl->pl_width, header);
3013 (void) printf("\n");
3017 * Given a dataset and a list of fields, print out all the properties according
3018 * to the described layout.
3021 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3023 zprop_list_t *pl = cb->cb_proplist;
3024 boolean_t first = B_TRUE;
3025 char property[ZFS_MAXPROPLEN];
3026 nvlist_t *userprops = zfs_get_user_props(zhp);
3029 boolean_t right_justify;
3031 for (; pl != NULL; pl = pl->pl_next) {
3033 if (cb->cb_scripted)
3034 (void) printf("\t");
3041 if (pl->pl_prop == ZFS_PROP_NAME) {
3042 (void) strlcpy(property, zfs_get_name(zhp),
3045 right_justify = zfs_prop_align_right(pl->pl_prop);
3046 } else if (pl->pl_prop != ZPROP_INVAL) {
3047 if (zfs_prop_get(zhp, pl->pl_prop, property,
3048 sizeof (property), NULL, NULL, 0,
3049 cb->cb_literal) != 0)
3053 right_justify = zfs_prop_align_right(pl->pl_prop);
3054 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3055 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3056 property, sizeof (property), cb->cb_literal) != 0)
3060 right_justify = B_TRUE;
3061 } else if (zfs_prop_written(pl->pl_user_prop)) {
3062 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3063 property, sizeof (property), cb->cb_literal) != 0)
3067 right_justify = B_TRUE;
3069 if (nvlist_lookup_nvlist(userprops,
3070 pl->pl_user_prop, &propval) != 0)
3073 verify(nvlist_lookup_string(propval,
3074 ZPROP_VALUE, &propstr) == 0);
3075 right_justify = B_FALSE;
3079 * If this is being called in scripted mode, or if this is the
3080 * last column and it is left-justified, don't include a width
3083 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3084 (void) printf("%s", propstr);
3085 else if (right_justify)
3086 (void) printf("%*s", pl->pl_width, propstr);
3088 (void) printf("%-*s", pl->pl_width, propstr);
3091 (void) printf("\n");
3095 * Generic callback function to list a dataset or snapshot.
3098 list_callback(zfs_handle_t *zhp, void *data)
3100 list_cbdata_t *cbp = data;
3102 if (cbp->cb_first) {
3103 if (!cbp->cb_scripted)
3105 cbp->cb_first = B_FALSE;
3108 print_dataset(zhp, cbp);
3114 zfs_do_list(int argc, char **argv)
3117 static char default_fields[] =
3118 "name,used,available,referenced,mountpoint";
3119 int types = ZFS_TYPE_DATASET;
3120 boolean_t types_specified = B_FALSE;
3121 char *fields = NULL;
3122 list_cbdata_t cb = { 0 };
3126 zfs_sort_column_t *sortcol = NULL;
3127 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3130 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3136 cb.cb_literal = B_TRUE;
3137 flags |= ZFS_ITER_LITERAL_PROPS;
3140 limit = parse_depth(optarg, &flags);
3143 flags |= ZFS_ITER_RECURSE;
3146 cb.cb_scripted = B_TRUE;
3149 if (zfs_add_sort_column(&sortcol, optarg,
3151 (void) fprintf(stderr,
3152 gettext("invalid property '%s'\n"), optarg);
3157 if (zfs_add_sort_column(&sortcol, optarg,
3159 (void) fprintf(stderr,
3160 gettext("invalid property '%s'\n"), optarg);
3166 types_specified = B_TRUE;
3167 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3168 while (*optarg != '\0') {
3169 static char *type_subopts[] = { "filesystem",
3170 "volume", "snapshot", "snap", "bookmark",
3173 switch (getsubopt(&optarg, type_subopts,
3176 types |= ZFS_TYPE_FILESYSTEM;
3179 types |= ZFS_TYPE_VOLUME;
3183 types |= ZFS_TYPE_SNAPSHOT;
3186 types |= ZFS_TYPE_BOOKMARK;
3189 types = ZFS_TYPE_DATASET |
3193 (void) fprintf(stderr,
3194 gettext("invalid type '%s'\n"),
3201 (void) fprintf(stderr, gettext("missing argument for "
3202 "'%c' option\n"), optopt);
3206 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3216 fields = default_fields;
3219 * If we are only going to list snapshot names and sort by name,
3220 * then we can use faster version.
3222 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3223 flags |= ZFS_ITER_SIMPLE;
3226 * If "-o space" and no types were specified, don't display snapshots.
3228 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3229 types &= ~ZFS_TYPE_SNAPSHOT;
3232 * If the user specifies '-o all', the zprop_get_list() doesn't
3233 * normally include the name of the dataset. For 'zfs list', we always
3234 * want this property to be first.
3236 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3240 cb.cb_first = B_TRUE;
3242 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3243 limit, list_callback, &cb);
3245 zprop_free_list(cb.cb_proplist);
3246 zfs_free_sort_columns(sortcol);
3248 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3249 (void) printf(gettext("no datasets available\n"));
3255 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3256 * zfs rename [-f] -p <fs | vol> <fs | vol>
3257 * zfs rename -r <snap> <snap>
3258 * zfs rename <bmark> <bmark>
3259 * zfs rename -u [-p] <fs> <fs>
3261 * Renames the given dataset to another of the same type.
3263 * The '-p' flag creates all the non-existing ancestors of the target first.
3267 zfs_do_rename(int argc, char **argv)
3270 renameflags_t flags = { 0 };
3274 boolean_t parents = B_FALSE;
3275 boolean_t bookmarks = B_FALSE;
3276 char *snapshot = NULL;
3279 while ((c = getopt(argc, argv, "fpru")) != -1) {
3285 flags.recurse = B_TRUE;
3288 flags.nounmount = B_TRUE;
3291 flags.forceunmount = B_TRUE;
3295 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3304 /* check number of arguments */
3306 (void) fprintf(stderr, gettext("missing source dataset "
3311 (void) fprintf(stderr, gettext("missing target dataset "
3316 (void) fprintf(stderr, gettext("too many arguments\n"));
3320 if (flags.recurse && parents) {
3321 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3326 if (flags.recurse && strchr(argv[0], '@') == NULL) {
3327 (void) fprintf(stderr, gettext("source dataset for recursive "
3328 "rename must be a snapshot\n"));
3332 if (flags.nounmount && parents) {
3333 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3338 if (strchr(argv[0], '#') != NULL)
3341 if (bookmarks && (flags.nounmount || flags.recurse ||
3342 flags.forceunmount || parents)) {
3343 (void) fprintf(stderr, gettext("options are not supported "
3344 "for renaming bookmarks\n"));
3348 if (flags.nounmount)
3349 types = ZFS_TYPE_FILESYSTEM;
3351 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3353 types = ZFS_TYPE_BOOKMARK;
3355 types = ZFS_TYPE_DATASET;
3357 if (flags.recurse) {
3359 * When we do recursive rename we are fine when the given
3360 * snapshot for the given dataset doesn't exist - it can
3361 * still exists below.
3364 snapshot = strchr(argv[0], '@');
3365 assert(snapshot != NULL);
3370 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3373 /* If we were asked and the name looks good, try to create ancestors. */
3374 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3375 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3380 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3389 * Promotes the given clone fs to be the parent
3393 zfs_do_promote(int argc, char **argv)
3399 if (argc > 1 && argv[1][0] == '-') {
3400 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3405 /* check number of arguments */
3407 (void) fprintf(stderr, gettext("missing clone filesystem"
3412 (void) fprintf(stderr, gettext("too many arguments\n"));
3416 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3420 ret = (zfs_promote(zhp) != 0);
3428 * zfs rollback [-rRf] <snapshot>
3430 * -r Delete any intervening snapshots before doing rollback
3431 * -R Delete any snapshots and their clones
3432 * -f ignored for backwards compatability
3434 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3435 * since then and making it the active dataset. If more recent snapshots exist,
3436 * the command will complain unless the '-r' flag is given.
3438 typedef struct rollback_cbdata {
3440 uint8_t cb_younger_ds_printed;
3445 boolean_t cb_recurse;
3446 } rollback_cbdata_t;
3449 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3451 rollback_cbdata_t *cbp = data;
3453 if (cbp->cb_first && cbp->cb_recurse) {
3454 (void) fprintf(stderr, gettext("cannot rollback to "
3455 "'%s': clones of previous snapshots exist\n"),
3457 (void) fprintf(stderr, gettext("use '-R' to "
3458 "force deletion of the following clones and "
3464 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3471 * Report some snapshots/bookmarks more recent than the one specified.
3472 * Used when '-r' is not specified. We reuse this same callback for the
3473 * snapshot dependents - if 'cb_dependent' is set, then this is a
3474 * dependent and we should report it without checking the transaction group.
3477 rollback_check(zfs_handle_t *zhp, void *data)
3479 rollback_cbdata_t *cbp = data;
3481 * Max number of younger snapshots and/or bookmarks to display before
3482 * we stop the iteration.
3484 const uint8_t max_younger = 32;
3486 if (cbp->cb_doclones) {
3491 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3492 if (cbp->cb_first && !cbp->cb_recurse) {
3493 (void) fprintf(stderr, gettext("cannot "
3494 "rollback to '%s': more recent snapshots "
3495 "or bookmarks exist\n"),
3497 (void) fprintf(stderr, gettext("use '-r' to "
3498 "force deletion of the following "
3499 "snapshots and bookmarks:\n"));
3504 if (cbp->cb_recurse) {
3505 if (zfs_iter_dependents(zhp, B_TRUE,
3506 rollback_check_dependent, cbp) != 0) {
3511 (void) fprintf(stderr, "%s\n",
3513 cbp->cb_younger_ds_printed++;
3518 if (cbp->cb_younger_ds_printed == max_younger) {
3520 * This non-recursive rollback is going to fail due to the
3521 * presence of snapshots and/or bookmarks that are younger than
3522 * the rollback target.
3523 * We printed some of the offending objects, now we stop
3524 * zfs_iter_snapshot/bookmark iteration so we can fail fast and
3525 * avoid iterating over the rest of the younger objects
3527 (void) fprintf(stderr, gettext("Output limited to %d "
3528 "snapshots/bookmarks\n"), max_younger);
3535 zfs_do_rollback(int argc, char **argv)
3539 boolean_t force = B_FALSE;
3540 rollback_cbdata_t cb = { 0 };
3541 zfs_handle_t *zhp, *snap;
3542 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3544 uint64_t min_txg = 0;
3547 while ((c = getopt(argc, argv, "rRf")) != -1) {
3560 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3569 /* check number of arguments */
3571 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3575 (void) fprintf(stderr, gettext("too many arguments\n"));
3579 /* open the snapshot */
3580 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3583 /* open the parent dataset */
3584 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3585 verify((delim = strrchr(parentname, '@')) != NULL);
3587 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3593 * Check for more recent snapshots and/or clones based on the presence
3596 cb.cb_target = argv[0];
3597 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3598 cb.cb_first = B_TRUE;
3601 if (cb.cb_create > 0)
3602 min_txg = cb.cb_create;
3604 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb,
3607 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3610 if ((ret = cb.cb_error) != 0)
3614 * Rollback parent to the given snapshot.
3616 ret = zfs_rollback(zhp, snap, force);
3629 * zfs set property=value ... { fs | snap | vol } ...
3631 * Sets the given properties for all datasets specified on the command line.
3635 set_callback(zfs_handle_t *zhp, void *data)
3637 nvlist_t *props = data;
3639 if (zfs_prop_set_list(zhp, props) != 0) {
3640 switch (libzfs_errno(g_zfs)) {
3641 case EZFS_MOUNTFAILED:
3642 (void) fprintf(stderr, gettext("property may be set "
3643 "but unable to remount filesystem\n"));
3645 case EZFS_SHARENFSFAILED:
3646 (void) fprintf(stderr, gettext("property may be set "
3647 "but unable to reshare filesystem\n"));
3656 zfs_do_set(int argc, char **argv)
3658 nvlist_t *props = NULL;
3659 int ds_start = -1; /* argv idx of first dataset arg */
3662 /* check for options */
3663 if (argc > 1 && argv[1][0] == '-') {
3664 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3669 /* check number of arguments */
3671 (void) fprintf(stderr, gettext("missing arguments\n"));
3675 if (strchr(argv[1], '=') == NULL) {
3676 (void) fprintf(stderr, gettext("missing property=value "
3679 (void) fprintf(stderr, gettext("missing dataset "
3685 /* validate argument order: prop=val args followed by dataset args */
3686 for (int i = 1; i < argc; i++) {
3687 if (strchr(argv[i], '=') != NULL) {
3689 /* out-of-order prop=val argument */
3690 (void) fprintf(stderr, gettext("invalid "
3691 "argument order\n"), i);
3694 } else if (ds_start < 0) {
3699 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3703 /* Populate a list of property settings */
3704 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3706 for (int i = 1; i < ds_start; i++) {
3707 if ((ret = parseprop(props, argv[i])) != 0)
3711 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3712 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3719 typedef struct snap_cbdata {
3721 boolean_t sd_recursive;
3722 const char *sd_snapname;
3726 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3728 snap_cbdata_t *sd = arg;
3733 if (sd->sd_recursive &&
3734 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3739 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3742 fnvlist_add_boolean(sd->sd_nvl, name);
3745 if (sd->sd_recursive)
3746 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3752 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3754 * Creates a snapshot with the given name. While functionally equivalent to
3755 * 'zfs create', it is a separate command to differentiate intent.
3758 zfs_do_snapshot(int argc, char **argv)
3763 snap_cbdata_t sd = { 0 };
3764 boolean_t multiple_snaps = B_FALSE;
3766 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3768 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3772 while ((c = getopt(argc, argv, "ro:")) != -1) {
3775 if (parseprop(props, optarg) != 0)
3779 sd.sd_recursive = B_TRUE;
3780 multiple_snaps = B_TRUE;
3783 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3792 /* check number of arguments */
3794 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3799 multiple_snaps = B_TRUE;
3800 for (; argc > 0; argc--, argv++) {
3804 atp = strchr(argv[0], '@');
3808 sd.sd_snapname = atp + 1;
3809 zhp = zfs_open(g_zfs, argv[0],
3810 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3813 if (zfs_snapshot_cb(zhp, &sd) != 0)
3817 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3818 nvlist_free(sd.sd_nvl);
3820 if (ret != 0 && multiple_snaps)
3821 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3825 nvlist_free(sd.sd_nvl);
3832 * Send a backup stream to stdout.
3835 zfs_do_send(int argc, char **argv)
3837 char *fromname = NULL;
3838 char *toname = NULL;
3839 char *resume_token = NULL;
3842 sendflags_t flags = { 0 };
3844 nvlist_t *dbgnv = NULL;
3845 boolean_t extraverbose = B_FALSE;
3847 struct option long_options[] = {
3848 {"replicate", no_argument, NULL, 'R'},
3849 {"props", no_argument, NULL, 'p'},
3850 {"parsable", no_argument, NULL, 'P'},
3851 {"dedup", no_argument, NULL, 'D'},
3852 {"verbose", no_argument, NULL, 'v'},
3853 {"dryrun", no_argument, NULL, 'n'},
3854 {"large-block", no_argument, NULL, 'L'},
3855 {"embed", no_argument, NULL, 'e'},
3856 {"resume", required_argument, NULL, 't'},
3857 {"compressed", no_argument, NULL, 'c'},
3862 while ((c = getopt_long(argc, argv, ":i:I:RbDpVvnPLet:c", long_options,
3874 flags.doall = B_TRUE;
3877 flags.replicate = B_TRUE;
3880 flags.props = B_TRUE;
3883 flags.parsable = B_TRUE;
3884 flags.verbose = B_TRUE;
3887 flags.progress = B_TRUE;
3888 flags.progressastitle = B_TRUE;
3892 extraverbose = B_TRUE;
3893 flags.verbose = B_TRUE;
3894 flags.progress = B_TRUE;
3897 flags.dedup = B_TRUE;
3900 flags.dryrun = B_TRUE;
3903 flags.largeblock = B_TRUE;
3906 flags.embed_data = B_TRUE;
3909 resume_token = optarg;
3912 flags.compress = B_TRUE;
3915 (void) fprintf(stderr, gettext("missing argument for "
3916 "'%c' option\n"), optopt);
3922 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3931 if (resume_token != NULL) {
3932 if (fromname != NULL || flags.replicate || flags.props ||
3934 (void) fprintf(stderr,
3935 gettext("invalid flags combined with -t\n"));
3939 (void) fprintf(stderr, gettext("no additional "
3940 "arguments are permitted with -t\n"));
3945 (void) fprintf(stderr,
3946 gettext("missing snapshot argument\n"));
3950 (void) fprintf(stderr, gettext("too many arguments\n"));
3955 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3956 (void) fprintf(stderr,
3957 gettext("Error: Stream can not be written to a terminal.\n"
3958 "You must redirect standard output.\n"));
3962 if (resume_token != NULL) {
3963 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3968 * Special case sending a filesystem, or from a bookmark.
3970 if (strchr(argv[0], '@') == NULL ||
3971 (fromname && strchr(fromname, '#') != NULL)) {
3972 char frombuf[ZFS_MAX_DATASET_NAME_LEN];
3974 if (flags.replicate || flags.doall || flags.props ||
3975 flags.dedup || (strchr(argv[0], '@') == NULL &&
3976 (flags.dryrun || flags.verbose || flags.progress))) {
3977 (void) fprintf(stderr, gettext("Error: "
3978 "Unsupported flag with filesystem or bookmark.\n"));
3982 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3986 if (fromname != NULL &&
3987 (fromname[0] == '#' || fromname[0] == '@')) {
3989 * Incremental source name begins with # or @.
3990 * Default to same fs as target.
3992 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3993 cp = strchr(frombuf, '@');
3996 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3999 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, flags);
4004 cp = strchr(argv[0], '@');
4007 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4012 * If they specified the full path to the snapshot, chop off
4013 * everything except the short name of the snapshot, but special
4014 * case if they specify the origin.
4016 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
4017 char origin[ZFS_MAX_DATASET_NAME_LEN];
4020 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
4021 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
4023 if (strcmp(origin, fromname) == 0) {
4025 flags.fromorigin = B_TRUE;
4028 if (cp != fromname && strcmp(argv[0], fromname)) {
4029 (void) fprintf(stderr,
4030 gettext("incremental source must be "
4031 "in same filesystem\n"));
4035 if (strchr(fromname, '@') || strchr(fromname, '/')) {
4036 (void) fprintf(stderr,
4037 gettext("invalid incremental source\n"));
4043 if (flags.replicate && fromname == NULL)
4044 flags.doall = B_TRUE;
4046 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
4047 extraverbose ? &dbgnv : NULL);
4049 if (extraverbose && dbgnv != NULL) {
4051 * dump_nvlist prints to stdout, but that's been
4052 * redirected to a file. Make it print to stderr
4055 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
4056 dump_nvlist(dbgnv, 0);
4065 * Restore a backup stream from stdin.
4068 zfs_do_receive(int argc, char **argv)
4071 recvflags_t flags = { 0 };
4072 boolean_t abort_resumable = B_FALSE;
4075 nvpair_t *nvp = NULL;
4077 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4081 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
4084 if (parseprop(props, optarg) != 0)
4088 flags.isprefix = B_TRUE;
4091 flags.isprefix = B_TRUE;
4092 flags.istail = B_TRUE;
4095 flags.dryrun = B_TRUE;
4098 flags.nomount = B_TRUE;
4101 flags.verbose = B_TRUE;
4104 flags.resumable = B_TRUE;
4107 flags.force = B_TRUE;
4110 abort_resumable = B_TRUE;
4113 (void) fprintf(stderr, gettext("missing argument for "
4114 "'%c' option\n"), optopt);
4118 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4127 /* check number of arguments */
4129 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4133 (void) fprintf(stderr, gettext("too many arguments\n"));
4137 while ((nvp = nvlist_next_nvpair(props, nvp))) {
4138 if (strcmp(nvpair_name(nvp), "origin") != 0) {
4139 (void) fprintf(stderr, gettext("invalid option"));
4144 if (abort_resumable) {
4145 if (flags.isprefix || flags.istail || flags.dryrun ||
4146 flags.resumable || flags.nomount) {
4147 (void) fprintf(stderr, gettext("invalid option"));
4151 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4152 (void) snprintf(namebuf, sizeof (namebuf),
4153 "%s/%%recv", argv[0]);
4155 if (zfs_dataset_exists(g_zfs, namebuf,
4156 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4157 zfs_handle_t *zhp = zfs_open(g_zfs,
4158 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4161 err = zfs_destroy(zhp, B_FALSE);
4163 zfs_handle_t *zhp = zfs_open(g_zfs,
4164 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4167 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4168 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4169 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4170 (void) fprintf(stderr,
4171 gettext("'%s' does not have any "
4172 "resumable receive state to abort\n"),
4176 err = zfs_destroy(zhp, B_FALSE);
4182 if (isatty(STDIN_FILENO)) {
4183 (void) fprintf(stderr,
4184 gettext("Error: Backup stream can not be read "
4185 "from a terminal.\n"
4186 "You must redirect standard input.\n"));
4189 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4195 * allow/unallow stuff
4197 /* copied from zfs/sys/dsl_deleg.h */
4198 #define ZFS_DELEG_PERM_CREATE "create"
4199 #define ZFS_DELEG_PERM_DESTROY "destroy"
4200 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4201 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4202 #define ZFS_DELEG_PERM_CLONE "clone"
4203 #define ZFS_DELEG_PERM_PROMOTE "promote"
4204 #define ZFS_DELEG_PERM_RENAME "rename"
4205 #define ZFS_DELEG_PERM_MOUNT "mount"
4206 #define ZFS_DELEG_PERM_SHARE "share"
4207 #define ZFS_DELEG_PERM_SEND "send"
4208 #define ZFS_DELEG_PERM_RECEIVE "receive"
4209 #define ZFS_DELEG_PERM_ALLOW "allow"
4210 #define ZFS_DELEG_PERM_USERPROP "userprop"
4211 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4212 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4213 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4214 #define ZFS_DELEG_PERM_USERUSED "userused"
4215 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4216 #define ZFS_DELEG_PERM_HOLD "hold"
4217 #define ZFS_DELEG_PERM_RELEASE "release"
4218 #define ZFS_DELEG_PERM_DIFF "diff"
4219 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4220 #define ZFS_DELEG_PERM_REMAP "remap"
4222 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4224 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4225 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4226 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4227 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4228 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4229 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4230 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4231 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4232 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4233 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4234 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4235 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4236 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4237 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4238 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4239 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4240 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4241 { ZFS_DELEG_PERM_REMAP, ZFS_DELEG_NOTE_REMAP },
4243 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4244 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4245 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4246 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4247 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4248 { NULL, ZFS_DELEG_NOTE_NONE }
4251 /* permission structure */
4252 typedef struct deleg_perm {
4253 zfs_deleg_who_type_t dp_who_type;
4254 const char *dp_name;
4256 boolean_t dp_descend;
4260 typedef struct deleg_perm_node {
4261 deleg_perm_t dpn_perm;
4263 uu_avl_node_t dpn_avl_node;
4264 } deleg_perm_node_t;
4266 typedef struct fs_perm fs_perm_t;
4268 /* permissions set */
4269 typedef struct who_perm {
4270 zfs_deleg_who_type_t who_type;
4271 const char *who_name; /* id */
4272 char who_ug_name[256]; /* user/group name */
4273 fs_perm_t *who_fsperm; /* uplink */
4275 uu_avl_t *who_deleg_perm_avl; /* permissions */
4279 typedef struct who_perm_node {
4280 who_perm_t who_perm;
4281 uu_avl_node_t who_avl_node;
4284 typedef struct fs_perm_set fs_perm_set_t;
4285 /* fs permissions */
4287 const char *fsp_name;
4289 uu_avl_t *fsp_sc_avl; /* sets,create */
4290 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4292 fs_perm_set_t *fsp_set; /* uplink */
4296 typedef struct fs_perm_node {
4297 fs_perm_t fspn_fsperm;
4300 uu_list_node_t fspn_list_node;
4303 /* top level structure */
4304 struct fs_perm_set {
4305 uu_list_pool_t *fsps_list_pool;
4306 uu_list_t *fsps_list; /* list of fs_perms */
4308 uu_avl_pool_t *fsps_named_set_avl_pool;
4309 uu_avl_pool_t *fsps_who_perm_avl_pool;
4310 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4313 static inline const char *
4314 deleg_perm_type(zfs_deleg_note_t note)
4320 case ZFS_DELEG_NOTE_GROUPQUOTA:
4321 case ZFS_DELEG_NOTE_GROUPUSED:
4322 case ZFS_DELEG_NOTE_USERPROP:
4323 case ZFS_DELEG_NOTE_USERQUOTA:
4324 case ZFS_DELEG_NOTE_USERUSED:
4326 return (gettext("other"));
4328 return (gettext("subcommand"));
4333 who_type2weight(zfs_deleg_who_type_t who_type)
4337 case ZFS_DELEG_NAMED_SET_SETS:
4338 case ZFS_DELEG_NAMED_SET:
4341 case ZFS_DELEG_CREATE_SETS:
4342 case ZFS_DELEG_CREATE:
4345 case ZFS_DELEG_USER_SETS:
4346 case ZFS_DELEG_USER:
4349 case ZFS_DELEG_GROUP_SETS:
4350 case ZFS_DELEG_GROUP:
4353 case ZFS_DELEG_EVERYONE_SETS:
4354 case ZFS_DELEG_EVERYONE:
4366 who_perm_compare(const void *larg, const void *rarg, void *unused)
4368 const who_perm_node_t *l = larg;
4369 const who_perm_node_t *r = rarg;
4370 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4371 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4372 int lweight = who_type2weight(ltype);
4373 int rweight = who_type2weight(rtype);
4374 int res = lweight - rweight;
4376 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4377 ZFS_MAX_DELEG_NAME-1);
4389 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4391 const deleg_perm_node_t *l = larg;
4392 const deleg_perm_node_t *r = rarg;
4393 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4394 ZFS_MAX_DELEG_NAME-1);
4406 fs_perm_set_init(fs_perm_set_t *fspset)
4408 bzero(fspset, sizeof (fs_perm_set_t));
4410 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4411 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4412 NULL, UU_DEFAULT)) == NULL)
4414 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4415 UU_DEFAULT)) == NULL)
4418 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4419 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4420 who_perm_node_t, who_avl_node), who_perm_compare,
4421 UU_DEFAULT)) == NULL)
4424 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4425 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4426 who_perm_node_t, who_avl_node), who_perm_compare,
4427 UU_DEFAULT)) == NULL)
4430 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4431 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4432 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4437 static inline void fs_perm_fini(fs_perm_t *);
4438 static inline void who_perm_fini(who_perm_t *);
4441 fs_perm_set_fini(fs_perm_set_t *fspset)
4443 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4445 while (node != NULL) {
4446 fs_perm_node_t *next_node =
4447 uu_list_next(fspset->fsps_list, node);
4448 fs_perm_t *fsperm = &node->fspn_fsperm;
4449 fs_perm_fini(fsperm);
4450 uu_list_remove(fspset->fsps_list, node);
4455 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4456 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4457 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4461 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4464 deleg_perm->dp_who_type = type;
4465 deleg_perm->dp_name = name;
4469 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4470 zfs_deleg_who_type_t type, const char *name)
4472 uu_avl_pool_t *pool;
4473 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4475 bzero(who_perm, sizeof (who_perm_t));
4477 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4478 UU_DEFAULT)) == NULL)
4481 who_perm->who_type = type;
4482 who_perm->who_name = name;
4483 who_perm->who_fsperm = fsperm;
4487 who_perm_fini(who_perm_t *who_perm)
4489 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4491 while (node != NULL) {
4492 deleg_perm_node_t *next_node =
4493 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4495 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4500 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4504 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4506 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4507 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4509 bzero(fsperm, sizeof (fs_perm_t));
4511 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4515 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4519 fsperm->fsp_set = fspset;
4520 fsperm->fsp_name = fsname;
4524 fs_perm_fini(fs_perm_t *fsperm)
4526 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4527 while (node != NULL) {
4528 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4530 who_perm_t *who_perm = &node->who_perm;
4531 who_perm_fini(who_perm);
4532 uu_avl_remove(fsperm->fsp_sc_avl, node);
4537 node = uu_avl_first(fsperm->fsp_uge_avl);
4538 while (node != NULL) {
4539 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4541 who_perm_t *who_perm = &node->who_perm;
4542 who_perm_fini(who_perm);
4543 uu_avl_remove(fsperm->fsp_uge_avl, node);
4548 uu_avl_destroy(fsperm->fsp_sc_avl);
4549 uu_avl_destroy(fsperm->fsp_uge_avl);
4553 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4554 zfs_deleg_who_type_t who_type, const char *name, char locality)
4556 uu_avl_index_t idx = 0;
4558 deleg_perm_node_t *found_node = NULL;
4559 deleg_perm_t *deleg_perm = &node->dpn_perm;
4561 deleg_perm_init(deleg_perm, who_type, name);
4563 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4565 uu_avl_insert(avl, node, idx);
4568 deleg_perm = &node->dpn_perm;
4573 case ZFS_DELEG_LOCAL:
4574 deleg_perm->dp_local = B_TRUE;
4576 case ZFS_DELEG_DESCENDENT:
4577 deleg_perm->dp_descend = B_TRUE;
4582 assert(B_FALSE); /* invalid locality */
4587 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4589 nvpair_t *nvp = NULL;
4590 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4591 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4592 zfs_deleg_who_type_t who_type = who_perm->who_type;
4594 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4595 const char *name = nvpair_name(nvp);
4596 data_type_t type = nvpair_type(nvp);
4597 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4598 deleg_perm_node_t *node =
4599 safe_malloc(sizeof (deleg_perm_node_t));
4601 assert(type == DATA_TYPE_BOOLEAN);
4603 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4604 set_deleg_perm_node(avl, node, who_type, name, locality);
4611 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4613 nvpair_t *nvp = NULL;
4614 fs_perm_set_t *fspset = fsperm->fsp_set;
4616 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4617 nvlist_t *nvl2 = NULL;
4618 const char *name = nvpair_name(nvp);
4619 uu_avl_t *avl = NULL;
4620 uu_avl_pool_t *avl_pool = NULL;
4621 zfs_deleg_who_type_t perm_type = name[0];
4622 char perm_locality = name[1];
4623 const char *perm_name = name + 3;
4624 boolean_t is_set = B_TRUE;
4625 who_perm_t *who_perm = NULL;
4627 assert('$' == name[2]);
4629 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4632 switch (perm_type) {
4633 case ZFS_DELEG_CREATE:
4634 case ZFS_DELEG_CREATE_SETS:
4635 case ZFS_DELEG_NAMED_SET:
4636 case ZFS_DELEG_NAMED_SET_SETS:
4637 avl_pool = fspset->fsps_named_set_avl_pool;
4638 avl = fsperm->fsp_sc_avl;
4640 case ZFS_DELEG_USER:
4641 case ZFS_DELEG_USER_SETS:
4642 case ZFS_DELEG_GROUP:
4643 case ZFS_DELEG_GROUP_SETS:
4644 case ZFS_DELEG_EVERYONE:
4645 case ZFS_DELEG_EVERYONE_SETS:
4646 avl_pool = fspset->fsps_who_perm_avl_pool;
4647 avl = fsperm->fsp_uge_avl;
4651 assert(!"unhandled zfs_deleg_who_type_t");
4655 who_perm_node_t *found_node = NULL;
4656 who_perm_node_t *node = safe_malloc(
4657 sizeof (who_perm_node_t));
4658 who_perm = &node->who_perm;
4659 uu_avl_index_t idx = 0;
4661 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4662 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4664 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4666 if (avl == fsperm->fsp_uge_avl) {
4668 struct passwd *p = NULL;
4669 struct group *g = NULL;
4670 const char *nice_name = NULL;
4672 switch (perm_type) {
4673 case ZFS_DELEG_USER_SETS:
4674 case ZFS_DELEG_USER:
4675 rid = atoi(perm_name);
4678 nice_name = p->pw_name;
4680 case ZFS_DELEG_GROUP_SETS:
4681 case ZFS_DELEG_GROUP:
4682 rid = atoi(perm_name);
4685 nice_name = g->gr_name;
4692 if (nice_name != NULL)
4694 node->who_perm.who_ug_name,
4697 /* User or group unknown */
4699 node->who_perm.who_ug_name,
4701 node->who_perm.who_ug_name),
4702 "(unknown: %d)", rid);
4706 uu_avl_insert(avl, node, idx);
4709 who_perm = &node->who_perm;
4713 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4720 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4722 nvpair_t *nvp = NULL;
4723 uu_avl_index_t idx = 0;
4725 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4726 nvlist_t *nvl2 = NULL;
4727 const char *fsname = nvpair_name(nvp);
4728 data_type_t type = nvpair_type(nvp);
4729 fs_perm_t *fsperm = NULL;
4730 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4734 fsperm = &node->fspn_fsperm;
4736 assert(DATA_TYPE_NVLIST == type);
4738 uu_list_node_init(node, &node->fspn_list_node,
4739 fspset->fsps_list_pool);
4741 idx = uu_list_numnodes(fspset->fsps_list);
4742 fs_perm_init(fsperm, fspset, fsname);
4744 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4747 (void) parse_fs_perm(fsperm, nvl2);
4749 uu_list_insert(fspset->fsps_list, node, idx);
4755 static inline const char *
4756 deleg_perm_comment(zfs_deleg_note_t note)
4758 const char *str = "";
4763 case ZFS_DELEG_NOTE_ALLOW:
4764 str = gettext("Must also have the permission that is being"
4765 "\n\t\t\t\tallowed");
4767 case ZFS_DELEG_NOTE_CLONE:
4768 str = gettext("Must also have the 'create' ability and 'mount'"
4769 "\n\t\t\t\tability in the origin file system");
4771 case ZFS_DELEG_NOTE_CREATE:
4772 str = gettext("Must also have the 'mount' ability");
4774 case ZFS_DELEG_NOTE_DESTROY:
4775 str = gettext("Must also have the 'mount' ability");
4777 case ZFS_DELEG_NOTE_DIFF:
4778 str = gettext("Allows lookup of paths within a dataset;"
4779 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4780 "\n\t\t\t\tin order to use zfs diff");
4782 case ZFS_DELEG_NOTE_HOLD:
4783 str = gettext("Allows adding a user hold to a snapshot");
4785 case ZFS_DELEG_NOTE_MOUNT:
4786 str = gettext("Allows mount/umount of ZFS datasets");
4788 case ZFS_DELEG_NOTE_PROMOTE:
4789 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4790 " 'promote' ability in the origin file system");
4792 case ZFS_DELEG_NOTE_RECEIVE:
4793 str = gettext("Must also have the 'mount' and 'create'"
4796 case ZFS_DELEG_NOTE_RELEASE:
4797 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4798 "might destroy the snapshot");
4800 case ZFS_DELEG_NOTE_RENAME:
4801 str = gettext("Must also have the 'mount' and 'create'"
4802 "\n\t\t\t\tability in the new parent");
4804 case ZFS_DELEG_NOTE_ROLLBACK:
4807 case ZFS_DELEG_NOTE_SEND:
4810 case ZFS_DELEG_NOTE_SHARE:
4811 str = gettext("Allows sharing file systems over NFS or SMB"
4812 "\n\t\t\t\tprotocols");
4814 case ZFS_DELEG_NOTE_SNAPSHOT:
4818 * case ZFS_DELEG_NOTE_VSCAN:
4819 * str = gettext("");
4823 case ZFS_DELEG_NOTE_GROUPQUOTA:
4824 str = gettext("Allows accessing any groupquota@... property");
4826 case ZFS_DELEG_NOTE_GROUPUSED:
4827 str = gettext("Allows reading any groupused@... property");
4829 case ZFS_DELEG_NOTE_USERPROP:
4830 str = gettext("Allows changing any user property");
4832 case ZFS_DELEG_NOTE_USERQUOTA:
4833 str = gettext("Allows accessing any userquota@... property");
4835 case ZFS_DELEG_NOTE_USERUSED:
4836 str = gettext("Allows reading any userused@... property");
4854 boolean_t recursive; /* unallow only */
4855 boolean_t prt_usage;
4857 boolean_t prt_perms;
4860 const char *dataset;
4864 prop_cmp(const void *a, const void *b)
4866 const char *str1 = *(const char **)a;
4867 const char *str2 = *(const char **)b;
4868 return (strcmp(str1, str2));
4872 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4874 const char *opt_desc[] = {
4875 "-h", gettext("show this help message and exit"),
4876 "-l", gettext("set permission locally"),
4877 "-d", gettext("set permission for descents"),
4878 "-u", gettext("set permission for user"),
4879 "-g", gettext("set permission for group"),
4880 "-e", gettext("set permission for everyone"),
4881 "-c", gettext("set create time permission"),
4882 "-s", gettext("define permission set"),
4884 "-r", gettext("remove permissions recursively"),
4886 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4887 size_t allow_size = unallow_size - 2;
4888 const char *props[ZFS_NUM_PROPS];
4891 FILE *fp = requested ? stdout : stderr;
4892 zprop_desc_t *pdtbl = zfs_prop_get_table();
4893 const char *fmt = gettext("%-16s %-14s\t%s\n");
4895 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4897 (void) fprintf(fp, gettext("Options:\n"));
4898 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4899 const char *opt = opt_desc[i++];
4900 const char *optdsc = opt_desc[i];
4901 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4904 (void) fprintf(fp, gettext("\nThe following permissions are "
4906 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4908 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4909 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4910 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4911 const char *perm_type = deleg_perm_type(perm_note);
4912 const char *perm_comment = deleg_perm_comment(perm_note);
4913 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4916 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4917 zprop_desc_t *pd = &pdtbl[i];
4918 if (pd->pd_visible != B_TRUE)
4921 if (pd->pd_attr == PROP_READONLY)
4924 props[count++] = pd->pd_name;
4926 props[count] = NULL;
4928 qsort(props, count, sizeof (char *), prop_cmp);
4930 for (i = 0; i < count; i++)
4931 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4934 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4936 exit(requested ? 0 : 2);
4939 static inline const char *
4940 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4943 if (un && argc == expected_argc - 1)
4945 else if (argc == expected_argc)
4946 *permsp = argv[argc - 2];
4948 allow_usage(un, B_FALSE,
4949 gettext("wrong number of parameters\n"));
4951 return (argv[argc - 1]);
4955 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4957 int uge_sum = opts->user + opts->group + opts->everyone;
4958 int csuge_sum = opts->create + opts->set + uge_sum;
4959 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4960 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4963 allow_usage(un, B_FALSE,
4964 gettext("-u, -g, and -e are mutually exclusive\n"));
4966 if (opts->prt_usage) {
4967 if (argc == 0 && all_sum == 0)
4968 allow_usage(un, B_TRUE, NULL);
4975 allow_usage(un, B_FALSE,
4976 gettext("invalid options combined with -s\n"));
4978 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4979 if (argv[0][0] != '@')
4980 allow_usage(un, B_FALSE,
4981 gettext("invalid set name: missing '@' prefix\n"));
4982 opts->who = argv[0];
4983 } else if (opts->create) {
4984 if (ldcsuge_sum > 1)
4985 allow_usage(un, B_FALSE,
4986 gettext("invalid options combined with -c\n"));
4987 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4988 } else if (opts->everyone) {
4990 allow_usage(un, B_FALSE,
4991 gettext("invalid options combined with -e\n"));
4992 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4993 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4995 opts->everyone = B_TRUE;
4998 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4999 } else if (argc == 1 && !un) {
5000 opts->prt_perms = B_TRUE;
5001 opts->dataset = argv[argc-1];
5003 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5004 opts->who = argv[0];
5007 if (!opts->local && !opts->descend) {
5008 opts->local = B_TRUE;
5009 opts->descend = B_TRUE;
5014 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
5015 const char *who, char *perms, nvlist_t *top_nvl)
5018 char ld[2] = { '\0', '\0' };
5019 char who_buf[MAXNAMELEN + 32];
5020 char base_type = '\0';
5021 char set_type = '\0';
5022 nvlist_t *base_nvl = NULL;
5023 nvlist_t *set_nvl = NULL;
5026 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
5028 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
5032 case ZFS_DELEG_NAMED_SET_SETS:
5033 case ZFS_DELEG_NAMED_SET:
5034 set_type = ZFS_DELEG_NAMED_SET_SETS;
5035 base_type = ZFS_DELEG_NAMED_SET;
5036 ld[0] = ZFS_DELEG_NA;
5038 case ZFS_DELEG_CREATE_SETS:
5039 case ZFS_DELEG_CREATE:
5040 set_type = ZFS_DELEG_CREATE_SETS;
5041 base_type = ZFS_DELEG_CREATE;
5042 ld[0] = ZFS_DELEG_NA;
5044 case ZFS_DELEG_USER_SETS:
5045 case ZFS_DELEG_USER:
5046 set_type = ZFS_DELEG_USER_SETS;
5047 base_type = ZFS_DELEG_USER;
5049 ld[0] = ZFS_DELEG_LOCAL;
5051 ld[1] = ZFS_DELEG_DESCENDENT;
5053 case ZFS_DELEG_GROUP_SETS:
5054 case ZFS_DELEG_GROUP:
5055 set_type = ZFS_DELEG_GROUP_SETS;
5056 base_type = ZFS_DELEG_GROUP;
5058 ld[0] = ZFS_DELEG_LOCAL;
5060 ld[1] = ZFS_DELEG_DESCENDENT;
5062 case ZFS_DELEG_EVERYONE_SETS:
5063 case ZFS_DELEG_EVERYONE:
5064 set_type = ZFS_DELEG_EVERYONE_SETS;
5065 base_type = ZFS_DELEG_EVERYONE;
5067 ld[0] = ZFS_DELEG_LOCAL;
5069 ld[1] = ZFS_DELEG_DESCENDENT;
5073 assert(set_type != '\0' && base_type != '\0');
5076 if (perms != NULL) {
5078 char *end = curr + strlen(perms);
5080 while (curr < end) {
5081 char *delim = strchr(curr, ',');
5092 (void) nvlist_add_boolean(nvl, curr);
5098 for (i = 0; i < 2; i++) {
5099 char locality = ld[i];
5103 if (!nvlist_empty(base_nvl)) {
5105 (void) snprintf(who_buf,
5106 sizeof (who_buf), "%c%c$%s",
5107 base_type, locality, who);
5109 (void) snprintf(who_buf,
5110 sizeof (who_buf), "%c%c$",
5111 base_type, locality);
5113 (void) nvlist_add_nvlist(top_nvl, who_buf,
5118 if (!nvlist_empty(set_nvl)) {
5120 (void) snprintf(who_buf,
5121 sizeof (who_buf), "%c%c$%s",
5122 set_type, locality, who);
5124 (void) snprintf(who_buf,
5125 sizeof (who_buf), "%c%c$",
5126 set_type, locality);
5128 (void) nvlist_add_nvlist(top_nvl, who_buf,
5133 for (i = 0; i < 2; i++) {
5134 char locality = ld[i];
5139 (void) snprintf(who_buf, sizeof (who_buf),
5140 "%c%c$%s", base_type, locality, who);
5142 (void) snprintf(who_buf, sizeof (who_buf),
5143 "%c%c$", base_type, locality);
5144 (void) nvlist_add_boolean(top_nvl, who_buf);
5147 (void) snprintf(who_buf, sizeof (who_buf),
5148 "%c%c$%s", set_type, locality, who);
5150 (void) snprintf(who_buf, sizeof (who_buf),
5151 "%c%c$", set_type, locality);
5152 (void) nvlist_add_boolean(top_nvl, who_buf);
5158 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5160 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5164 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5165 opts->descend, opts->who, opts->perms, *nvlp);
5166 } else if (opts->create) {
5167 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5168 opts->descend, NULL, opts->perms, *nvlp);
5169 } else if (opts->everyone) {
5170 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5171 opts->descend, NULL, opts->perms, *nvlp);
5173 char *curr = opts->who;
5174 char *end = curr + strlen(curr);
5176 while (curr < end) {
5178 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5180 char *delim = strchr(curr, ',');
5183 struct passwd *p = NULL;
5184 struct group *g = NULL;
5192 rid = (uid_t)strtol(curr, &endch, 0);
5194 who_type = ZFS_DELEG_USER;
5202 else if (*endch != '\0') {
5203 (void) snprintf(errbuf, 256, gettext(
5204 "invalid user %s\n"), curr);
5205 allow_usage(un, B_TRUE, errbuf);
5207 } else if (opts->group) {
5208 who_type = ZFS_DELEG_GROUP;
5216 else if (*endch != '\0') {
5217 (void) snprintf(errbuf, 256, gettext(
5218 "invalid group %s\n"), curr);
5219 allow_usage(un, B_TRUE, errbuf);
5222 if (*endch != '\0') {
5229 if (*endch != '\0') {
5237 who_type = ZFS_DELEG_USER;
5239 } else if (g != NULL) {
5240 who_type = ZFS_DELEG_GROUP;
5243 (void) snprintf(errbuf, 256, gettext(
5244 "invalid user/group %s\n"), curr);
5245 allow_usage(un, B_TRUE, errbuf);
5249 (void) sprintf(id, "%u", rid);
5252 store_allow_perm(who_type, opts->local,
5253 opts->descend, who, opts->perms, *nvlp);
5262 print_set_creat_perms(uu_avl_t *who_avl)
5264 const char *sc_title[] = {
5265 gettext("Permission sets:\n"),
5266 gettext("Create time permissions:\n"),
5269 const char **title_ptr = sc_title;
5270 who_perm_node_t *who_node = NULL;
5271 int prev_weight = -1;
5273 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5274 who_node = uu_avl_next(who_avl, who_node)) {
5275 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5276 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5277 const char *who_name = who_node->who_perm.who_name;
5278 int weight = who_type2weight(who_type);
5279 boolean_t first = B_TRUE;
5280 deleg_perm_node_t *deleg_node;
5282 if (prev_weight != weight) {
5283 (void) printf(*title_ptr++);
5284 prev_weight = weight;
5287 if (who_name == NULL || strnlen(who_name, 1) == 0)
5288 (void) printf("\t");
5290 (void) printf("\t%s ", who_name);
5292 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5293 deleg_node = uu_avl_next(avl, deleg_node)) {
5296 deleg_node->dpn_perm.dp_name);
5299 (void) printf(",%s",
5300 deleg_node->dpn_perm.dp_name);
5303 (void) printf("\n");
5308 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5311 who_perm_node_t *who_node = NULL;
5312 boolean_t prt_title = B_TRUE;
5313 uu_avl_walk_t *walk;
5315 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5318 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5319 const char *who_name = who_node->who_perm.who_name;
5320 const char *nice_who_name = who_node->who_perm.who_ug_name;
5321 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5322 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5324 deleg_perm_node_t *deleg_node;
5325 boolean_t prt_who = B_TRUE;
5327 for (deleg_node = uu_avl_first(avl);
5329 deleg_node = uu_avl_next(avl, deleg_node)) {
5330 if (local != deleg_node->dpn_perm.dp_local ||
5331 descend != deleg_node->dpn_perm.dp_descend)
5335 const char *who = NULL;
5337 prt_title = B_FALSE;
5338 (void) printf(title);
5342 case ZFS_DELEG_USER_SETS:
5343 case ZFS_DELEG_USER:
5344 who = gettext("user");
5346 who_name = nice_who_name;
5348 case ZFS_DELEG_GROUP_SETS:
5349 case ZFS_DELEG_GROUP:
5350 who = gettext("group");
5352 who_name = nice_who_name;
5354 case ZFS_DELEG_EVERYONE_SETS:
5355 case ZFS_DELEG_EVERYONE:
5356 who = gettext("everyone");
5361 assert(who != NULL);
5365 if (who_name == NULL)
5366 (void) printf("\t%s", who);
5368 (void) printf("\t%s %s", who, who_name);
5371 (void) printf("%c%s", delim,
5372 deleg_node->dpn_perm.dp_name);
5377 (void) printf("\n");
5380 uu_avl_walk_end(walk);
5384 print_fs_perms(fs_perm_set_t *fspset)
5386 fs_perm_node_t *node = NULL;
5387 char buf[MAXNAMELEN + 32];
5388 const char *dsname = buf;
5390 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5391 node = uu_list_next(fspset->fsps_list, node)) {
5392 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5393 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5396 (void) snprintf(buf, sizeof (buf),
5397 gettext("---- Permissions on %s "),
5398 node->fspn_fsperm.fsp_name);
5399 (void) printf(dsname);
5400 left = 70 - strlen(buf);
5403 (void) printf("\n");
5405 print_set_creat_perms(sc_avl);
5406 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5407 gettext("Local permissions:\n"));
5408 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5409 gettext("Descendent permissions:\n"));
5410 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5411 gettext("Local+Descendent permissions:\n"));
5415 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5417 struct deleg_perms {
5423 set_deleg_perms(zfs_handle_t *zhp, void *data)
5425 struct deleg_perms *perms = (struct deleg_perms *)data;
5426 zfs_type_t zfs_type = zfs_get_type(zhp);
5428 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5431 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5435 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5438 nvlist_t *perm_nvl = NULL;
5439 nvlist_t *update_perm_nvl = NULL;
5442 struct allow_opts opts = { 0 };
5444 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5447 while ((c = getopt(argc, argv, optstr)) != -1) {
5450 opts.local = B_TRUE;
5453 opts.descend = B_TRUE;
5459 opts.group = B_TRUE;
5462 opts.everyone = B_TRUE;
5468 opts.create = B_TRUE;
5471 opts.recursive = B_TRUE;
5474 (void) fprintf(stderr, gettext("missing argument for "
5475 "'%c' option\n"), optopt);
5479 opts.prt_usage = B_TRUE;
5482 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5491 /* check arguments */
5492 parse_allow_args(argc, argv, un, &opts);
5494 /* try to open the dataset */
5495 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5496 ZFS_TYPE_VOLUME)) == NULL) {
5497 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5502 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5505 fs_perm_set_init(&fs_perm_set);
5506 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5507 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5512 print_fs_perms(&fs_perm_set);
5514 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5515 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5518 if (un && opts.recursive) {
5519 struct deleg_perms data = { un, update_perm_nvl };
5520 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5529 nvlist_free(perm_nvl);
5530 nvlist_free(update_perm_nvl);
5532 fs_perm_set_fini(&fs_perm_set);
5540 zfs_do_allow(int argc, char **argv)
5542 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5546 zfs_do_unallow(int argc, char **argv)
5548 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5552 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5557 boolean_t recursive = B_FALSE;
5558 const char *opts = holding ? "rt" : "r";
5562 while ((c = getopt(argc, argv, opts)) != -1) {
5568 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5577 /* check number of arguments */
5585 if (holding && tag[0] == '.') {
5586 /* tags starting with '.' are reserved for libzfs */
5587 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5591 for (i = 0; i < argc; ++i) {
5593 char parent[ZFS_MAX_DATASET_NAME_LEN];
5595 char *path = argv[i];
5597 delim = strchr(path, '@');
5598 if (delim == NULL) {
5599 (void) fprintf(stderr,
5600 gettext("'%s' is not a snapshot\n"), path);
5604 (void) strncpy(parent, path, delim - path);
5605 parent[delim - path] = '\0';
5607 zhp = zfs_open(g_zfs, parent,
5608 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5614 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5617 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5623 return (errors != 0);
5627 * zfs hold [-r] [-t] <tag> <snap> ...
5629 * -r Recursively hold
5631 * Apply a user-hold with the given tag to the list of snapshots.
5634 zfs_do_hold(int argc, char **argv)
5636 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5640 * zfs release [-r] <tag> <snap> ...
5642 * -r Recursively release
5644 * Release a user-hold with the given tag from the list of snapshots.
5647 zfs_do_release(int argc, char **argv)
5649 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5652 typedef struct holds_cbdata {
5653 boolean_t cb_recursive;
5654 const char *cb_snapname;
5656 size_t cb_max_namelen;
5657 size_t cb_max_taglen;
5660 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5661 #define DATETIME_BUF_LEN (32)
5666 print_holds(boolean_t scripted, boolean_t literal, size_t nwidth,
5667 size_t tagwidth, nvlist_t *nvl)
5670 nvpair_t *nvp = NULL;
5671 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5675 for (i = 0; i < 3; i++) {
5676 col = gettext(hdr_cols[i]);
5678 (void) printf("%-*s ", i ? tagwidth : nwidth,
5681 (void) printf("%s\n", col);
5685 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5686 char *zname = nvpair_name(nvp);
5688 nvpair_t *nvp2 = NULL;
5689 (void) nvpair_value_nvlist(nvp, &nvl2);
5690 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5691 char tsbuf[DATETIME_BUF_LEN];
5692 char *tagname = nvpair_name(nvp2);
5697 (void) nvpair_value_uint64(nvp2, &val);
5699 snprintf(tsbuf, DATETIME_BUF_LEN, "%llu", val);
5702 (void) localtime_r(&time, &t);
5703 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5704 gettext(STRFTIME_FMT_STR), &t);
5708 (void) printf("%s\t%s\t%s\n", zname,
5711 (void) printf("%-*s %-*s %s\n", nwidth,
5712 zname, tagwidth, tagname, tsbuf);
5719 * Generic callback function to list a dataset or snapshot.
5722 holds_callback(zfs_handle_t *zhp, void *data)
5724 holds_cbdata_t *cbp = data;
5725 nvlist_t *top_nvl = *cbp->cb_nvlp;
5726 nvlist_t *nvl = NULL;
5727 nvpair_t *nvp = NULL;
5728 const char *zname = zfs_get_name(zhp);
5729 size_t znamelen = strlen(zname);
5731 if (cbp->cb_recursive && cbp->cb_snapname != NULL) {
5732 const char *snapname;
5733 char *delim = strchr(zname, '@');
5737 snapname = delim + 1;
5738 if (strcmp(cbp->cb_snapname, snapname))
5742 if (zfs_get_holds(zhp, &nvl) != 0)
5745 if (znamelen > cbp->cb_max_namelen)
5746 cbp->cb_max_namelen = znamelen;
5748 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5749 const char *tag = nvpair_name(nvp);
5750 size_t taglen = strlen(tag);
5751 if (taglen > cbp->cb_max_taglen)
5752 cbp->cb_max_taglen = taglen;
5755 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5759 * zfs holds [-Hp] [-r | -d max] <dataset|snap> ...
5761 * -H Suppress header output
5762 * -p Output literal values
5763 * -r Recursively search for holds
5764 * -d max Limit depth of recursive search
5767 zfs_do_holds(int argc, char **argv)
5772 boolean_t scripted = B_FALSE;
5773 boolean_t literal = B_FALSE;
5774 boolean_t recursive = B_FALSE;
5775 const char *opts = "d:rHp";
5778 int types = ZFS_TYPE_SNAPSHOT;
5779 holds_cbdata_t cb = { 0 };
5786 while ((c = getopt(argc, argv, opts)) != -1) {
5789 limit = parse_depth(optarg, &flags);
5802 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5809 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5810 flags |= ZFS_ITER_RECURSE;
5816 /* check number of arguments */
5820 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5823 for (i = 0; i < argc; ++i) {
5824 char *snapshot = argv[i];
5826 const char *snapname = NULL;
5828 delim = strchr(snapshot, '@');
5829 if (delim != NULL) {
5830 snapname = delim + 1;
5832 snapshot[delim - snapshot] = '\0';
5835 cb.cb_recursive = recursive;
5836 cb.cb_snapname = snapname;
5840 * 1. collect holds data, set format options
5842 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5843 holds_callback, &cb);
5849 * 2. print holds data
5851 print_holds(scripted, literal, cb.cb_max_namelen, cb.cb_max_taglen,
5854 if (nvlist_empty(nvl))
5855 (void) printf(gettext("no datasets available\n"));
5859 return (0 != errors);
5862 #define CHECK_SPINNER 30
5863 #define SPINNER_TIME 3 /* seconds */
5864 #define MOUNT_TIME 1 /* seconds */
5866 typedef struct get_all_state {
5867 boolean_t ga_verbose;
5868 get_all_cb_t *ga_cbp;
5872 get_one_dataset(zfs_handle_t *zhp, void *data)
5874 static char *spin[] = { "-", "\\", "|", "/" };
5875 static int spinval = 0;
5876 static int spincheck = 0;
5877 static time_t last_spin_time = (time_t)0;
5878 get_all_state_t *state = data;
5879 zfs_type_t type = zfs_get_type(zhp);
5881 if (state->ga_verbose) {
5882 if (--spincheck < 0) {
5883 time_t now = time(NULL);
5884 if (last_spin_time + SPINNER_TIME < now) {
5885 update_progress(spin[spinval++ % 4]);
5886 last_spin_time = now;
5888 spincheck = CHECK_SPINNER;
5893 * Interate over any nested datasets.
5895 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5901 * Skip any datasets whose type does not match.
5903 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5907 libzfs_add_handle(state->ga_cbp, zhp);
5908 assert(state->ga_cbp->cb_used <= state->ga_cbp->cb_alloc);
5914 get_all_datasets(get_all_cb_t *cbp, boolean_t verbose)
5916 get_all_state_t state = {
5917 .ga_verbose = verbose,
5922 set_progress_header(gettext("Reading ZFS config"));
5923 (void) zfs_iter_root(g_zfs, get_one_dataset, &state);
5926 finish_progress(gettext("done."));
5930 * Generic callback for sharing or mounting filesystems. Because the code is so
5931 * similar, we have a common function with an extra parameter to determine which
5932 * mode we are using.
5934 typedef enum { OP_SHARE, OP_MOUNT } share_mount_op_t;
5936 typedef struct share_mount_state {
5937 share_mount_op_t sm_op;
5938 boolean_t sm_verbose;
5941 char *sm_proto; /* only valid for OP_SHARE */
5942 pthread_mutex_t sm_lock; /* protects the remaining fields */
5943 uint_t sm_total; /* number of filesystems to process */
5944 uint_t sm_done; /* number of filesystems processed */
5945 int sm_status; /* -1 if any of the share/mount operations failed */
5946 } share_mount_state_t;
5949 * Share or mount a dataset.
5952 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5953 boolean_t explicit, const char *options)
5955 char mountpoint[ZFS_MAXPROPLEN];
5956 char shareopts[ZFS_MAXPROPLEN];
5957 char smbshareopts[ZFS_MAXPROPLEN];
5958 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5960 uint64_t zoned, canmount;
5961 boolean_t shared_nfs, shared_smb;
5963 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5966 * Check to make sure we can mount/share this dataset. If we
5967 * are in the global zone and the filesystem is exported to a
5968 * local zone, or if we are in a local zone and the
5969 * filesystem is not exported, then it is an error.
5971 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5973 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5977 (void) fprintf(stderr, gettext("cannot %s '%s': "
5978 "dataset is exported to a local zone\n"), cmdname,
5982 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5986 (void) fprintf(stderr, gettext("cannot %s '%s': "
5987 "permission denied\n"), cmdname,
5993 * Ignore any filesystems which don't apply to us. This
5994 * includes those with a legacy mountpoint, or those with
5995 * legacy share options.
5997 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5998 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5999 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
6000 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
6001 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
6002 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
6004 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
6005 strcmp(smbshareopts, "off") == 0) {
6009 (void) fprintf(stderr, gettext("cannot share '%s': "
6010 "legacy share\n"), zfs_get_name(zhp));
6011 (void) fprintf(stderr, gettext("to "
6012 "share this filesystem set "
6013 "sharenfs property on\n"));
6018 * We cannot share or mount legacy filesystems. If the
6019 * shareopts is non-legacy but the mountpoint is legacy, we
6020 * treat it as a legacy share.
6022 if (strcmp(mountpoint, "legacy") == 0) {
6026 (void) fprintf(stderr, gettext("cannot %s '%s': "
6027 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
6028 (void) fprintf(stderr, gettext("use %s(8) to "
6029 "%s this filesystem\n"), cmdname, cmdname);
6033 if (strcmp(mountpoint, "none") == 0) {
6037 (void) fprintf(stderr, gettext("cannot %s '%s': no "
6038 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
6043 * canmount explicit outcome
6044 * on no pass through
6045 * on yes pass through
6047 * off yes display error, return 1
6048 * noauto no return 0
6049 * noauto yes pass through
6051 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
6052 if (canmount == ZFS_CANMOUNT_OFF) {
6056 (void) fprintf(stderr, gettext("cannot %s '%s': "
6057 "'canmount' property is set to 'off'\n"), cmdname,
6060 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
6065 * If this filesystem is inconsistent and has a receive resume
6066 * token, we can not mount it.
6068 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
6069 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
6070 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
6074 (void) fprintf(stderr, gettext("cannot %s '%s': "
6075 "Contains partially-completed state from "
6076 "\"zfs receive -r\", which can be resumed with "
6077 "\"zfs send -t\"\n"),
6078 cmdname, zfs_get_name(zhp));
6083 * At this point, we have verified that the mountpoint and/or
6084 * shareopts are appropriate for auto management. If the
6085 * filesystem is already mounted or shared, return (failing
6086 * for explicit requests); otherwise mount or share the
6092 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
6093 shared_smb = zfs_is_shared_smb(zhp, NULL);
6095 if ((shared_nfs && shared_smb) ||
6096 (shared_nfs && strcmp(shareopts, "on") == 0 &&
6097 strcmp(smbshareopts, "off") == 0) ||
6098 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
6099 strcmp(shareopts, "off") == 0)) {
6103 (void) fprintf(stderr, gettext("cannot share "
6104 "'%s': filesystem already shared\n"),
6109 if (!zfs_is_mounted(zhp, NULL) &&
6110 zfs_mount(zhp, NULL, 0) != 0)
6113 if (protocol == NULL) {
6114 if (zfs_shareall(zhp) != 0)
6116 } else if (strcmp(protocol, "nfs") == 0) {
6117 if (zfs_share_nfs(zhp))
6119 } else if (strcmp(protocol, "smb") == 0) {
6120 if (zfs_share_smb(zhp))
6123 (void) fprintf(stderr, gettext("cannot share "
6124 "'%s': invalid share type '%s' "
6126 zfs_get_name(zhp), protocol);
6133 if (options == NULL)
6134 mnt.mnt_mntopts = "";
6136 mnt.mnt_mntopts = (char *)options;
6138 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6139 zfs_is_mounted(zhp, NULL)) {
6143 (void) fprintf(stderr, gettext("cannot mount "
6144 "'%s': filesystem already mounted\n"),
6149 if (zfs_mount(zhp, options, flags) != 0)
6158 * Reports progress in the form "(current/total)". Not thread-safe.
6161 report_mount_progress(int current, int total)
6163 static time_t last_progress_time = 0;
6164 time_t now = time(NULL);
6167 /* display header if we're here for the first time */
6169 set_progress_header(gettext("Mounting ZFS filesystems"));
6170 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6171 /* too soon to report again */
6175 last_progress_time = now;
6177 (void) sprintf(info, "(%d/%d)", current, total);
6179 if (current == total)
6180 finish_progress(info);
6182 update_progress(info);
6186 * zfs_foreach_mountpoint() callback that mounts or shares on filesystem and
6187 * updates the progress meter
6190 share_mount_one_cb(zfs_handle_t *zhp, void *arg)
6192 share_mount_state_t *sms = arg;
6195 ret = share_mount_one(zhp, sms->sm_op, sms->sm_flags, sms->sm_proto,
6196 B_FALSE, sms->sm_options);
6198 pthread_mutex_lock(&sms->sm_lock);
6200 sms->sm_status = ret;
6202 if (sms->sm_verbose)
6203 report_mount_progress(sms->sm_done, sms->sm_total);
6204 pthread_mutex_unlock(&sms->sm_lock);
6209 append_options(char *mntopts, char *newopts)
6211 int len = strlen(mntopts);
6213 /* original length plus new string to append plus 1 for the comma */
6214 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6215 (void) fprintf(stderr, gettext("the opts argument for "
6216 "'%c' option is too long (more than %d chars)\n"),
6217 "-o", MNT_LINE_MAX);
6222 mntopts[len++] = ',';
6224 (void) strcpy(&mntopts[len], newopts);
6228 share_mount(int op, int argc, char **argv)
6231 boolean_t verbose = B_FALSE;
6233 char *options = NULL;
6237 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6247 if (*optarg == '\0') {
6248 (void) fprintf(stderr, gettext("empty mount "
6249 "options (-o) specified\n"));
6253 if (options == NULL)
6254 options = safe_malloc(MNT_LINE_MAX + 1);
6256 /* option validation is done later */
6257 append_options(options, optarg);
6261 warnx("no overlay mounts support on FreeBSD, ignoring");
6264 (void) fprintf(stderr, gettext("missing argument for "
6265 "'%c' option\n"), optopt);
6269 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6278 /* check number of arguments */
6280 char *protocol = NULL;
6282 if (op == OP_SHARE && argc > 0) {
6283 if (strcmp(argv[0], "nfs") != 0 &&
6284 strcmp(argv[0], "smb") != 0) {
6285 (void) fprintf(stderr, gettext("share type "
6286 "must be 'nfs' or 'smb'\n"));
6295 (void) fprintf(stderr, gettext("too many arguments\n"));
6299 start_progress_timer();
6300 get_all_cb_t cb = { 0 };
6301 get_all_datasets(&cb, verbose);
6303 if (cb.cb_used == 0) {
6304 if (options != NULL)
6310 if (op == OP_SHARE) {
6311 sa_init_selective_arg_t sharearg;
6312 sharearg.zhandle_arr = cb.cb_handles;
6313 sharearg.zhandle_len = cb.cb_used;
6314 if ((ret = zfs_init_libshare_arg(g_zfs,
6315 SA_INIT_SHARE_API_SELECTIVE, &sharearg)) != SA_OK) {
6316 (void) fprintf(stderr, gettext(
6317 "Could not initialize libshare, %d"), ret);
6322 share_mount_state_t share_mount_state = { 0 };
6323 share_mount_state.sm_op = op;
6324 share_mount_state.sm_verbose = verbose;
6325 share_mount_state.sm_flags = flags;
6326 share_mount_state.sm_options = options;
6327 share_mount_state.sm_proto = protocol;
6328 share_mount_state.sm_total = cb.cb_used;
6329 pthread_mutex_init(&share_mount_state.sm_lock, NULL);
6332 * libshare isn't mt-safe, so only do the operation in parallel
6333 * if we're mounting.
6335 zfs_foreach_mountpoint(g_zfs, cb.cb_handles, cb.cb_used,
6336 share_mount_one_cb, &share_mount_state, op == OP_MOUNT);
6337 ret = share_mount_state.sm_status;
6339 for (int i = 0; i < cb.cb_used; i++)
6340 zfs_close(cb.cb_handles[i]);
6341 free(cb.cb_handles);
6342 } else if (argc == 0) {
6343 struct mnttab entry;
6345 if ((op == OP_SHARE) || (options != NULL)) {
6346 (void) fprintf(stderr, gettext("missing filesystem "
6347 "argument (specify -a for all)\n"));
6352 * When mount is given no arguments, go through /etc/mnttab and
6353 * display any active ZFS mounts. We hide any snapshots, since
6354 * they are controlled automatically.
6356 rewind(mnttab_file);
6357 while (getmntent(mnttab_file, &entry) == 0) {
6358 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6359 strchr(entry.mnt_special, '@') != NULL)
6362 (void) printf("%-30s %s\n", entry.mnt_special,
6370 (void) fprintf(stderr,
6371 gettext("too many arguments\n"));
6375 if ((zhp = zfs_open(g_zfs, argv[0],
6376 ZFS_TYPE_FILESYSTEM)) == NULL) {
6379 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6389 * zfs mount -a [nfs]
6390 * zfs mount filesystem
6392 * Mount all filesystems, or mount the given filesystem.
6395 zfs_do_mount(int argc, char **argv)
6397 return (share_mount(OP_MOUNT, argc, argv));
6401 * zfs share -a [nfs | smb]
6402 * zfs share filesystem
6404 * Share all filesystems, or share the given filesystem.
6407 zfs_do_share(int argc, char **argv)
6409 return (share_mount(OP_SHARE, argc, argv));
6412 typedef struct unshare_unmount_node {
6413 zfs_handle_t *un_zhp;
6415 uu_avl_node_t un_avlnode;
6416 } unshare_unmount_node_t;
6420 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6422 const unshare_unmount_node_t *l = larg;
6423 const unshare_unmount_node_t *r = rarg;
6425 return (strcmp(l->un_mountp, r->un_mountp));
6429 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6430 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6431 * and unmount it appropriately.
6434 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6438 struct stat64 statbuf;
6439 struct extmnttab entry;
6440 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6444 * Search for the path in /etc/mnttab. Rather than looking for the
6445 * specific path, which can be fooled by non-standard paths (i.e. ".."
6446 * or "//"), we stat() the path and search for the corresponding
6447 * (major,minor) device pair.
6449 if (stat64(path, &statbuf) != 0) {
6450 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6451 cmdname, path, strerror(errno));
6454 path_inode = statbuf.st_ino;
6457 * Search for the given (major,minor) pair in the mount table.
6460 rewind(mnttab_file);
6461 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6462 if (entry.mnt_major == major(statbuf.st_dev) &&
6463 entry.mnt_minor == minor(statbuf.st_dev))
6470 if (statfs(path, &sfs) != 0) {
6471 (void) fprintf(stderr, "%s: %s\n", path,
6475 statfs2mnttab(&sfs, &entry);
6479 if (op == OP_SHARE) {
6480 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6481 "currently mounted\n"), cmdname, path);
6484 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6486 if ((ret = umount2(path, flags)) != 0)
6487 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6492 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6493 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6494 "filesystem\n"), cmdname, path);
6498 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6499 ZFS_TYPE_FILESYSTEM)) == NULL)
6503 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6504 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6505 cmdname, path, strerror(errno));
6507 } else if (statbuf.st_ino != path_inode) {
6508 (void) fprintf(stderr, gettext("cannot "
6509 "%s '%s': not a mountpoint\n"), cmdname, path);
6513 if (op == OP_SHARE) {
6514 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6515 char smbshare_prop[ZFS_MAXPROPLEN];
6517 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6518 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6519 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6520 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6522 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6523 strcmp(smbshare_prop, "off") == 0) {
6524 (void) fprintf(stderr, gettext("cannot unshare "
6525 "'%s': legacy share\n"), path);
6527 (void) fprintf(stderr, gettext("use "
6528 "unshare(1M) to unshare this filesystem\n"));
6530 } else if (!zfs_is_shared(zhp)) {
6531 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6532 "not currently shared\n"), path);
6534 ret = zfs_unshareall_bypath(zhp, path);
6537 char mtpt_prop[ZFS_MAXPROPLEN];
6539 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6540 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6543 ret = zfs_unmount(zhp, NULL, flags);
6544 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6545 (void) fprintf(stderr, gettext("cannot unmount "
6546 "'%s': legacy mountpoint\n"),
6548 (void) fprintf(stderr, gettext("use umount(8) "
6549 "to unmount this filesystem\n"));
6551 ret = zfs_unmountall(zhp, flags);
6562 * Generic callback for unsharing or unmounting a filesystem.
6565 unshare_unmount(int op, int argc, char **argv)
6572 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6573 char sharesmb[ZFS_MAXPROPLEN];
6576 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6585 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6596 * We could make use of zfs_for_each() to walk all datasets in
6597 * the system, but this would be very inefficient, especially
6598 * since we would have to linearly search /etc/mnttab for each
6599 * one. Instead, do one pass through /etc/mnttab looking for
6600 * zfs entries and call zfs_unmount() for each one.
6602 * Things get a little tricky if the administrator has created
6603 * mountpoints beneath other ZFS filesystems. In this case, we
6604 * have to unmount the deepest filesystems first. To accomplish
6605 * this, we place all the mountpoints in an AVL tree sorted by
6606 * the special type (dataset name), and walk the result in
6607 * reverse to make sure to get any snapshots first.
6609 struct mnttab entry;
6610 uu_avl_pool_t *pool;
6611 uu_avl_t *tree = NULL;
6612 unshare_unmount_node_t *node;
6614 uu_avl_walk_t *walk;
6617 (void) fprintf(stderr, gettext("too many arguments\n"));
6621 if (((pool = uu_avl_pool_create("unmount_pool",
6622 sizeof (unshare_unmount_node_t),
6623 offsetof(unshare_unmount_node_t, un_avlnode),
6624 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6625 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6628 rewind(mnttab_file);
6629 while (getmntent(mnttab_file, &entry) == 0) {
6631 /* ignore non-ZFS entries */
6632 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6635 /* ignore snapshots */
6636 if (strchr(entry.mnt_special, '@') != NULL)
6639 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6640 ZFS_TYPE_FILESYSTEM)) == NULL) {
6646 * Ignore datasets that are excluded/restricted by
6649 if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
6656 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6658 sizeof (nfs_mnt_prop),
6659 NULL, NULL, 0, B_FALSE) == 0);
6660 if (strcmp(nfs_mnt_prop, "off") != 0)
6662 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6664 sizeof (nfs_mnt_prop),
6665 NULL, NULL, 0, B_FALSE) == 0);
6666 if (strcmp(nfs_mnt_prop, "off") == 0)
6670 /* Ignore legacy mounts */
6671 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6673 sizeof (nfs_mnt_prop),
6674 NULL, NULL, 0, B_FALSE) == 0);
6675 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6677 /* Ignore canmount=noauto mounts */
6678 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6679 ZFS_CANMOUNT_NOAUTO)
6685 node = safe_malloc(sizeof (unshare_unmount_node_t));
6687 node->un_mountp = safe_strdup(entry.mnt_mountp);
6689 uu_avl_node_init(node, &node->un_avlnode, pool);
6691 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6692 uu_avl_insert(tree, node, idx);
6694 zfs_close(node->un_zhp);
6695 free(node->un_mountp);
6701 * Walk the AVL tree in reverse, unmounting each filesystem and
6702 * removing it from the AVL tree in the process.
6704 if ((walk = uu_avl_walk_start(tree,
6705 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6708 while ((node = uu_avl_walk_next(walk)) != NULL) {
6709 uu_avl_remove(tree, node);
6713 if (zfs_unshareall_bypath(node->un_zhp,
6714 node->un_mountp) != 0)
6719 if (zfs_unmount(node->un_zhp,
6720 node->un_mountp, flags) != 0)
6725 zfs_close(node->un_zhp);
6726 free(node->un_mountp);
6730 uu_avl_walk_end(walk);
6731 uu_avl_destroy(tree);
6732 uu_avl_pool_destroy(pool);
6737 (void) fprintf(stderr,
6738 gettext("missing filesystem argument\n"));
6740 (void) fprintf(stderr,
6741 gettext("too many arguments\n"));
6746 * We have an argument, but it may be a full path or a ZFS
6747 * filesystem. Pass full paths off to unmount_path() (shared by
6748 * manual_unmount), otherwise open the filesystem and pass to
6751 if (argv[0][0] == '/')
6752 return (unshare_unmount_path(op, argv[0],
6755 if ((zhp = zfs_open(g_zfs, argv[0],
6756 ZFS_TYPE_FILESYSTEM)) == NULL)
6759 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6760 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6761 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6762 NULL, 0, B_FALSE) == 0);
6766 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6768 sizeof (nfs_mnt_prop),
6769 NULL, NULL, 0, B_FALSE) == 0);
6770 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6771 sharesmb, sizeof (sharesmb), NULL, NULL,
6774 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6775 strcmp(sharesmb, "off") == 0) {
6776 (void) fprintf(stderr, gettext("cannot "
6777 "unshare '%s': legacy share\n"),
6780 (void) fprintf(stderr, gettext("use "
6781 "unshare(1M) to unshare this "
6785 } else if (!zfs_is_shared(zhp)) {
6786 (void) fprintf(stderr, gettext("cannot "
6787 "unshare '%s': not currently "
6788 "shared\n"), zfs_get_name(zhp));
6790 } else if (zfs_unshareall(zhp) != 0) {
6796 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6797 (void) fprintf(stderr, gettext("cannot "
6798 "unmount '%s': legacy "
6799 "mountpoint\n"), zfs_get_name(zhp));
6800 (void) fprintf(stderr, gettext("use "
6801 "umount(8) to unmount this "
6804 } else if (!zfs_is_mounted(zhp, NULL)) {
6805 (void) fprintf(stderr, gettext("cannot "
6806 "unmount '%s': not currently "
6810 } else if (zfs_unmountall(zhp, flags) != 0) {
6824 * zfs unmount filesystem
6826 * Unmount all filesystems, or a specific ZFS filesystem.
6829 zfs_do_unmount(int argc, char **argv)
6831 return (unshare_unmount(OP_MOUNT, argc, argv));
6836 * zfs unshare filesystem
6838 * Unshare all filesystems, or a specific ZFS filesystem.
6841 zfs_do_unshare(int argc, char **argv)
6843 return (unshare_unmount(OP_SHARE, argc, argv));
6847 * Attach/detach the given dataset to/from the given jail
6851 do_jail(int argc, char **argv, int attach)
6856 /* check number of arguments */
6858 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6862 (void) fprintf(stderr, gettext("too many arguments\n"));
6866 jailid = jail_getid(argv[1]);
6868 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6872 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6876 ret = (zfs_jail(zhp, jailid, attach) != 0);
6883 * zfs jail jailid filesystem
6885 * Attach the given dataset to the given jail
6889 zfs_do_jail(int argc, char **argv)
6892 return (do_jail(argc, argv, 1));
6896 * zfs unjail jailid filesystem
6898 * Detach the given dataset from the given jail
6902 zfs_do_unjail(int argc, char **argv)
6905 return (do_jail(argc, argv, 0));
6909 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6910 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6913 manual_mount(int argc, char **argv)
6916 char mountpoint[ZFS_MAXPROPLEN];
6917 char mntopts[MNT_LINE_MAX] = { '\0' };
6921 char *dataset, *path;
6924 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6927 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6930 flags |= MS_OVERLAY;
6933 flags |= MS_NOMNTTAB;
6936 (void) fprintf(stderr, gettext("missing argument for "
6937 "'%c' option\n"), optopt);
6941 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6943 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6952 /* check that we only have two arguments */
6955 (void) fprintf(stderr, gettext("missing dataset "
6958 (void) fprintf(stderr,
6959 gettext("missing mountpoint argument\n"));
6961 (void) fprintf(stderr, gettext("too many arguments\n"));
6962 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6969 /* try to open the dataset */
6970 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6973 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6974 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6976 /* check for legacy mountpoint and complain appropriately */
6978 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6979 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6980 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6981 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6986 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6987 "mounted using 'mount -t zfs'\n"), dataset);
6988 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6989 "instead.\n"), path);
6990 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6991 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6992 (void) fprintf(stderr, gettext("See zfs(8) for more "
7001 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
7002 * unmounts of non-legacy filesystems, as this is the dominant administrative
7006 manual_unmount(int argc, char **argv)
7012 while ((c = getopt(argc, argv, "f")) != -1) {
7018 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7020 (void) fprintf(stderr, gettext("usage: unmount [-f] "
7029 /* check arguments */
7032 (void) fprintf(stderr, gettext("missing path "
7035 (void) fprintf(stderr, gettext("too many arguments\n"));
7036 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
7040 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
7044 find_command_idx(char *command, int *idx)
7048 for (i = 0; i < NCOMMAND; i++) {
7049 if (command_table[i].name == NULL)
7052 if (strcmp(command, command_table[i].name) == 0) {
7061 zfs_do_diff(int argc, char **argv)
7065 char *tosnap = NULL;
7066 char *fromsnap = NULL;
7071 while ((c = getopt(argc, argv, "FHt")) != -1) {
7074 flags |= ZFS_DIFF_CLASSIFY;
7077 flags |= ZFS_DIFF_PARSEABLE;
7080 flags |= ZFS_DIFF_TIMESTAMP;
7083 (void) fprintf(stderr,
7084 gettext("invalid option '%c'\n"), optopt);
7093 (void) fprintf(stderr,
7094 gettext("must provide at least one snapshot name\n"));
7099 (void) fprintf(stderr, gettext("too many arguments\n"));
7104 tosnap = (argc == 2) ? argv[1] : NULL;
7107 if (*fromsnap != '@')
7108 copy = strdup(fromsnap);
7110 copy = strdup(tosnap);
7114 if ((atp = strchr(copy, '@')) != NULL)
7117 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
7123 * Ignore SIGPIPE so that the library can give us
7124 * information on any failure
7126 (void) sigignore(SIGPIPE);
7128 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
7136 * zfs remap <filesystem | volume>
7138 * Remap the indirect blocks in the given fileystem or volume.
7141 zfs_do_remap(int argc, char **argv)
7148 while ((c = getopt(argc, argv, "")) != -1) {
7151 (void) fprintf(stderr,
7152 gettext("invalid option '%c'\n"), optopt);
7158 (void) fprintf(stderr, gettext("wrong number of arguments\n"));
7163 err = zfs_remap_indirects(g_zfs, fsname);
7169 * zfs bookmark <fs@snap> <fs#bmark>
7171 * Creates a bookmark with the given name from the given snapshot.
7174 zfs_do_bookmark(int argc, char **argv)
7176 char snapname[ZFS_MAX_DATASET_NAME_LEN];
7183 while ((c = getopt(argc, argv, "")) != -1) {
7186 (void) fprintf(stderr,
7187 gettext("invalid option '%c'\n"), optopt);
7195 /* check number of arguments */
7197 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
7201 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
7205 if (strchr(argv[1], '#') == NULL) {
7206 (void) fprintf(stderr,
7207 gettext("invalid bookmark name '%s' -- "
7208 "must contain a '#'\n"), argv[1]);
7212 if (argv[0][0] == '@') {
7214 * Snapshot name begins with @.
7215 * Default to same fs as bookmark.
7217 (void) strncpy(snapname, argv[1], sizeof (snapname));
7218 *strchr(snapname, '#') = '\0';
7219 (void) strlcat(snapname, argv[0], sizeof (snapname));
7221 (void) strncpy(snapname, argv[0], sizeof (snapname));
7223 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
7229 nvl = fnvlist_alloc();
7230 fnvlist_add_string(nvl, argv[1], snapname);
7231 ret = lzc_bookmark(nvl, NULL);
7235 const char *err_msg;
7238 (void) snprintf(errbuf, sizeof (errbuf),
7239 dgettext(TEXT_DOMAIN,
7240 "cannot create bookmark '%s'"), argv[1]);
7244 err_msg = "bookmark is in a different pool";
7247 err_msg = "bookmark exists";
7250 err_msg = "invalid argument";
7253 err_msg = "bookmark feature not enabled";
7256 err_msg = "out of space";
7259 err_msg = "unknown error";
7262 (void) fprintf(stderr, "%s: %s\n", errbuf,
7263 dgettext(TEXT_DOMAIN, err_msg));
7274 zfs_do_channel_program(int argc, char **argv)
7278 char *progbuf, *filename, *poolname;
7279 size_t progsize, progread;
7281 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
7282 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
7283 boolean_t sync_flag = B_TRUE, json_output = B_FALSE;
7284 zpool_handle_t *zhp;
7288 (c = getopt(argc, argv, "jnt:(instr-limit)m:(memory-limit)"))) {
7296 arg = strtoull(optarg, &endp, 0);
7297 if (errno != 0 || *endp != '\0') {
7298 (void) fprintf(stderr, gettext(
7300 "'%s': expected integer\n"), optarg);
7305 if (arg > ZCP_MAX_INSTRLIMIT || arg == 0) {
7306 (void) fprintf(stderr, gettext(
7307 "Invalid instruction limit: "
7314 ASSERT3U(c, ==, 'm');
7315 if (arg > ZCP_MAX_MEMLIMIT || arg == 0) {
7316 (void) fprintf(stderr, gettext(
7317 "Invalid memory limit: "
7327 sync_flag = B_FALSE;
7331 json_output = B_TRUE;
7335 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7345 (void) fprintf(stderr,
7346 gettext("invalid number of arguments\n"));
7352 if (strcmp(filename, "-") == 0) {
7354 filename = "standard input";
7355 } else if ((fd = open(filename, O_RDONLY)) < 0) {
7356 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
7357 filename, strerror(errno));
7361 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7362 (void) fprintf(stderr, gettext("cannot open pool '%s'"),
7369 * Read in the channel program, expanding the program buffer as
7374 progbuf = safe_malloc(progsize);
7376 ret = read(fd, progbuf + progread, progsize - progread);
7378 if (progread == progsize && ret > 0) {
7380 progbuf = safe_realloc(progbuf, progsize);
7388 (void) fprintf(stderr,
7389 gettext("cannot read '%s': %s\n"),
7390 filename, strerror(errno));
7393 progbuf[progread] = '\0';
7396 * Any remaining arguments are passed as arguments to the lua script as
7399 * "argv" -> [ "arg 1", ... "arg n" ],
7402 nvlist_t *argnvl = fnvlist_alloc();
7403 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);
7406 ret = lzc_channel_program(poolname, progbuf,
7407 instrlimit, memlimit, argnvl, &outnvl);
7409 ret = lzc_channel_program_nosync(poolname, progbuf,
7410 instrlimit, memlimit, argnvl, &outnvl);
7415 * On error, report the error message handed back by lua if one
7416 * exists. Otherwise, generate an appropriate error message,
7417 * falling back on strerror() for an unexpected return code.
7419 char *errstring = NULL;
7420 if (nvlist_exists(outnvl, ZCP_RET_ERROR)) {
7421 (void) nvlist_lookup_string(outnvl,
7422 ZCP_RET_ERROR, &errstring);
7423 if (errstring == NULL)
7424 errstring = strerror(ret);
7429 "Invalid instruction or memory limit.";
7432 errstring = "Return value too large.";
7435 errstring = "Memory limit exhausted.";
7442 errstring = "Timed out.";
7445 errstring = "Permission denied. Channel "
7446 "programs must be run as root.";
7449 errstring = strerror(ret);
7452 (void) fprintf(stderr,
7453 gettext("Channel program execution failed:\n%s\n"),
7457 (void) nvlist_print_json(stdout, outnvl);
7458 } else if (nvlist_empty(outnvl)) {
7459 (void) fprintf(stdout, gettext("Channel program fully "
7460 "executed and did not produce output.\n"));
7462 (void) fprintf(stdout, gettext("Channel program fully "
7463 "executed and produced output:\n"));
7464 dump_nvlist(outnvl, 4);
7469 fnvlist_free(outnvl);
7470 fnvlist_free(argnvl);
7479 main(int argc, char **argv)
7486 (void) setlocale(LC_ALL, "");
7487 (void) textdomain(TEXT_DOMAIN);
7491 if ((g_zfs = libzfs_init()) == NULL) {
7492 (void) fprintf(stderr, gettext("internal error: failed to "
7493 "initialize ZFS library\n"));
7497 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7499 libzfs_print_on_error(g_zfs, B_TRUE);
7501 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
7502 (void) fprintf(stderr, gettext("internal error: unable to "
7503 "open %s\n"), MNTTAB);
7508 * This command also doubles as the /etc/fs mount and unmount program.
7509 * Determine if we should take this behavior based on argv[0].
7511 progname = basename(argv[0]);
7512 if (strcmp(progname, "mount") == 0) {
7513 ret = manual_mount(argc, argv);
7514 } else if (strcmp(progname, "umount") == 0) {
7515 ret = manual_unmount(argc, argv);
7518 * Make sure the user has specified some command.
7521 (void) fprintf(stderr, gettext("missing command\n"));
7528 * The 'umount' command is an alias for 'unmount'
7530 if (strcmp(cmdname, "umount") == 0)
7531 cmdname = "unmount";
7534 * The 'recv' command is an alias for 'receive'
7536 if (strcmp(cmdname, "recv") == 0)
7537 cmdname = "receive";
7540 * The 'snap' command is an alias for 'snapshot'
7542 if (strcmp(cmdname, "snap") == 0)
7543 cmdname = "snapshot";
7548 if (strcmp(cmdname, "-?") == 0)
7552 * Run the appropriate command.
7554 libzfs_mnttab_cache(g_zfs, B_TRUE);
7555 if (find_command_idx(cmdname, &i) == 0) {
7556 current_command = &command_table[i];
7557 ret = command_table[i].func(argc - 1, argv + 1);
7558 } else if (strchr(cmdname, '=') != NULL) {
7559 verify(find_command_idx("set", &i) == 0);
7560 current_command = &command_table[i];
7561 ret = command_table[i].func(argc, argv);
7563 (void) fprintf(stderr, gettext("unrecognized "
7564 "command '%s'\n"), cmdname);
7567 libzfs_mnttab_cache(g_zfs, B_FALSE);
7570 (void) fclose(mnttab_file);
7572 if (ret == 0 && log_history)
7573 (void) zpool_log_history(g_zfs, history_str);
7578 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7579 * for the purposes of running ::findleaks.
7581 if (getenv("ZFS_ABORT") != NULL) {
7582 (void) printf("dumping core by request\n");