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) 2012 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 <pawel@dawidek.net>.
28 * All rights reserved.
29 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
30 * Copyright (c) 2013 Steven Hartland. All rights reserved.
31 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
40 #include <libnvpair.h>
53 #include <sys/mntent.h>
54 #include <sys/mnttab.h>
55 #include <sys/mount.h>
57 #include <sys/fs/zfs.h>
58 #include <sys/types.h>
64 #include <libzfs_core.h>
66 #include <zfs_deleg.h>
70 #include <directory.h>
75 #include "zfs_comutil.h"
77 libzfs_handle_t *g_zfs;
79 static FILE *mnttab_file;
80 static char history_str[HIS_MAX_RECORD_LEN];
81 static boolean_t log_history = B_TRUE;
83 static int zfs_do_clone(int argc, char **argv);
84 static int zfs_do_create(int argc, char **argv);
85 static int zfs_do_destroy(int argc, char **argv);
86 static int zfs_do_get(int argc, char **argv);
87 static int zfs_do_inherit(int argc, char **argv);
88 static int zfs_do_list(int argc, char **argv);
89 static int zfs_do_mount(int argc, char **argv);
90 static int zfs_do_rename(int argc, char **argv);
91 static int zfs_do_rollback(int argc, char **argv);
92 static int zfs_do_set(int argc, char **argv);
93 static int zfs_do_upgrade(int argc, char **argv);
94 static int zfs_do_snapshot(int argc, char **argv);
95 static int zfs_do_unmount(int argc, char **argv);
96 static int zfs_do_share(int argc, char **argv);
97 static int zfs_do_unshare(int argc, char **argv);
98 static int zfs_do_send(int argc, char **argv);
99 static int zfs_do_receive(int argc, char **argv);
100 static int zfs_do_promote(int argc, char **argv);
101 static int zfs_do_userspace(int argc, char **argv);
102 static int zfs_do_allow(int argc, char **argv);
103 static int zfs_do_unallow(int argc, char **argv);
104 static int zfs_do_hold(int argc, char **argv);
105 static int zfs_do_holds(int argc, char **argv);
106 static int zfs_do_release(int argc, char **argv);
107 static int zfs_do_diff(int argc, char **argv);
108 static int zfs_do_jail(int argc, char **argv);
109 static int zfs_do_unjail(int argc, char **argv);
110 static int zfs_do_bookmark(int argc, char **argv);
113 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
118 _umem_debug_init(void)
120 return ("default,verbose"); /* $UMEM_DEBUG setting */
124 _umem_logging_init(void)
126 return ("fail,contents"); /* $UMEM_LOGGING setting */
162 typedef struct zfs_command {
164 int (*func)(int argc, char **argv);
169 * Master command table. Each ZFS command has a name, associated function, and
170 * usage message. The usage messages need to be internationalized, so we have
171 * to have a function to return the usage message based on a command index.
173 * These commands are organized according to how they are displayed in the usage
174 * message. An empty command (one with a NULL name) indicates an empty line in
175 * the generic usage message.
177 static zfs_command_t command_table[] = {
178 { "create", zfs_do_create, HELP_CREATE },
179 { "destroy", zfs_do_destroy, HELP_DESTROY },
181 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
182 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
183 { "clone", zfs_do_clone, HELP_CLONE },
184 { "promote", zfs_do_promote, HELP_PROMOTE },
185 { "rename", zfs_do_rename, HELP_RENAME },
186 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
188 { "list", zfs_do_list, HELP_LIST },
190 { "set", zfs_do_set, HELP_SET },
191 { "get", zfs_do_get, HELP_GET },
192 { "inherit", zfs_do_inherit, HELP_INHERIT },
193 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
194 { "userspace", zfs_do_userspace, HELP_USERSPACE },
195 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
197 { "mount", zfs_do_mount, HELP_MOUNT },
198 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
199 { "share", zfs_do_share, HELP_SHARE },
200 { "unshare", zfs_do_unshare, HELP_UNSHARE },
202 { "send", zfs_do_send, HELP_SEND },
203 { "receive", zfs_do_receive, HELP_RECEIVE },
205 { "allow", zfs_do_allow, HELP_ALLOW },
207 { "unallow", zfs_do_unallow, HELP_UNALLOW },
209 { "hold", zfs_do_hold, HELP_HOLD },
210 { "holds", zfs_do_holds, HELP_HOLDS },
211 { "release", zfs_do_release, HELP_RELEASE },
212 { "diff", zfs_do_diff, HELP_DIFF },
214 { "jail", zfs_do_jail, HELP_JAIL },
215 { "unjail", zfs_do_unjail, HELP_UNJAIL },
218 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
220 zfs_command_t *current_command;
223 get_usage(zfs_help_t idx)
227 return (gettext("\tclone [-p] [-o property=value] ... "
228 "<snapshot> <filesystem|volume>\n"));
230 return (gettext("\tcreate [-pu] [-o property=value] ... "
232 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
233 "-V <size> <volume>\n"));
235 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
236 "\tdestroy [-dnpRrv] "
237 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
238 "\tdestroy <filesystem|volume>#<bookmark>\n"));
240 return (gettext("\tget [-rHp] [-d max] "
241 "[-o \"all\" | field[,...]]\n"
242 "\t [-t type[,...]] [-s source[,...]]\n"
243 "\t <\"all\" | property[,...]> "
244 "[filesystem|volume|snapshot] ...\n"));
246 return (gettext("\tinherit [-rS] <property> "
247 "<filesystem|volume|snapshot> ...\n"));
249 return (gettext("\tupgrade [-v]\n"
250 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
252 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
254 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
256 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
257 "[-s property]...\n\t [-S property]... [-t type[,...]] "
258 "[filesystem|volume|snapshot] ...\n"));
260 return (gettext("\tmount\n"
261 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
263 return (gettext("\tpromote <clone-filesystem>\n"));
265 return (gettext("\treceive|recv [-vnFu] <filesystem|volume|"
267 "\treceive|recv [-vnFu] [-d | -e] <filesystem>\n"));
269 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
270 "<filesystem|volume|snapshot>\n"
271 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
272 "\trename -r <snapshot> <snapshot>\n"
273 "\trename -u [-p] <filesystem> <filesystem>"));
275 return (gettext("\trollback [-rRf] <snapshot>\n"));
277 return (gettext("\tsend [-DnPpRv] [-[iI] snapshot] "
279 "\tsend [-i snapshot|bookmark] "
280 "<filesystem|volume|snapshot>\n"));
282 return (gettext("\tset <property=value> "
283 "<filesystem|volume|snapshot> ...\n"));
285 return (gettext("\tshare <-a | filesystem>\n"));
287 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
288 "<filesystem|volume>@<snap> ...\n"));
290 return (gettext("\tunmount|umount [-f] "
291 "<-a | filesystem|mountpoint>\n"));
293 return (gettext("\tunshare "
294 "<-a | filesystem|mountpoint>\n"));
296 return (gettext("\tallow <filesystem|volume>\n"
298 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
299 "\t <filesystem|volume>\n"
300 "\tallow [-ld] -e <perm|@setname>[,...] "
301 "<filesystem|volume>\n"
302 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
303 "\tallow -s @setname <perm|@setname>[,...] "
304 "<filesystem|volume>\n"));
306 return (gettext("\tunallow [-rldug] "
307 "<\"everyone\"|user|group>[,...]\n"
308 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
309 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
310 "<filesystem|volume>\n"
311 "\tunallow [-r] -c [<perm|@setname>[,...]] "
312 "<filesystem|volume>\n"
313 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
314 "<filesystem|volume>\n"));
316 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
318 "\t [-S field] ... [-t type[,...]] "
319 "<filesystem|snapshot>\n"));
320 case HELP_GROUPSPACE:
321 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
323 "\t [-S field] ... [-t type[,...]] "
324 "<filesystem|snapshot>\n"));
326 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
328 return (gettext("\tholds [-r] <snapshot> ...\n"));
330 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
332 return (gettext("\tdiff [-FHt] <snapshot> "
333 "[snapshot|filesystem]\n"));
335 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
345 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
350 * Utility function to guarantee malloc() success.
354 safe_malloc(size_t size)
358 if ((data = calloc(1, size)) == NULL)
365 safe_strdup(char *str)
367 char *dupstr = strdup(str);
376 * Callback routine that will print out information for each of
380 usage_prop_cb(int prop, void *cb)
384 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
386 if (zfs_prop_readonly(prop))
387 (void) fprintf(fp, " NO ");
389 (void) fprintf(fp, "YES ");
391 if (zfs_prop_inheritable(prop))
392 (void) fprintf(fp, " YES ");
394 (void) fprintf(fp, " NO ");
396 if (zfs_prop_values(prop) == NULL)
397 (void) fprintf(fp, "-\n");
399 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
405 * Display usage message. If we're inside a command, display only the usage for
406 * that command. Otherwise, iterate over the entire command table and display
407 * a complete usage message.
410 usage(boolean_t requested)
413 boolean_t show_properties = B_FALSE;
414 FILE *fp = requested ? stdout : stderr;
416 if (current_command == NULL) {
418 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
420 gettext("where 'command' is one of the following:\n\n"));
422 for (i = 0; i < NCOMMAND; i++) {
423 if (command_table[i].name == NULL)
424 (void) fprintf(fp, "\n");
426 (void) fprintf(fp, "%s",
427 get_usage(command_table[i].usage));
430 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
431 "pool/[dataset/]*dataset[@name]\n"));
433 (void) fprintf(fp, gettext("usage:\n"));
434 (void) fprintf(fp, "%s", get_usage(current_command->usage));
437 if (current_command != NULL &&
438 (strcmp(current_command->name, "set") == 0 ||
439 strcmp(current_command->name, "get") == 0 ||
440 strcmp(current_command->name, "inherit") == 0 ||
441 strcmp(current_command->name, "list") == 0))
442 show_properties = B_TRUE;
444 if (show_properties) {
446 gettext("\nThe following properties are supported:\n"));
448 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
449 "PROPERTY", "EDIT", "INHERIT", "VALUES");
451 /* Iterate over all properties */
452 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
455 (void) fprintf(fp, "\t%-15s ", "userused@...");
456 (void) fprintf(fp, " NO NO <size>\n");
457 (void) fprintf(fp, "\t%-15s ", "groupused@...");
458 (void) fprintf(fp, " NO NO <size>\n");
459 (void) fprintf(fp, "\t%-15s ", "userquota@...");
460 (void) fprintf(fp, "YES NO <size> | none\n");
461 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
462 (void) fprintf(fp, "YES NO <size> | none\n");
463 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
464 (void) fprintf(fp, " NO NO <size>\n");
466 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
467 "with standard units such as K, M, G, etc.\n"));
468 (void) fprintf(fp, gettext("\nUser-defined properties can "
469 "be specified by using a name containing a colon (:).\n"));
470 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
471 "properties must be appended with\n"
472 "a user or group specifier of one of these forms:\n"
473 " POSIX name (eg: \"matt\")\n"
474 " POSIX id (eg: \"126829\")\n"
475 " SMB name@domain (eg: \"matt@sun\")\n"
476 " SMB SID (eg: \"S-1-234-567-89\")\n"));
479 gettext("\nFor the property list, run: %s\n"),
482 gettext("\nFor the delegated permission list, run: %s\n"),
483 "zfs allow|unallow");
487 * See comments at end of main().
489 if (getenv("ZFS_ABORT") != NULL) {
490 (void) printf("dumping core by request\n");
494 exit(requested ? 0 : 2);
498 parseprop(nvlist_t *props)
500 char *propname = optarg;
501 char *propval, *strval;
503 if ((propval = strchr(propname, '=')) == NULL) {
504 (void) fprintf(stderr, gettext("missing "
505 "'=' for -o option\n"));
510 if (nvlist_lookup_string(props, propname, &strval) == 0) {
511 (void) fprintf(stderr, gettext("property '%s' "
512 "specified multiple times\n"), propname);
515 if (nvlist_add_string(props, propname, propval) != 0)
521 parse_depth(char *opt, int *flags)
526 depth = (int)strtol(opt, &tmp, 0);
528 (void) fprintf(stderr,
529 gettext("%s is not an integer\n"), optarg);
533 (void) fprintf(stderr,
534 gettext("Depth can not be negative.\n"));
537 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
541 #define PROGRESS_DELAY 2 /* seconds */
543 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";
544 static time_t pt_begin;
545 static char *pt_header = NULL;
546 static boolean_t pt_shown;
549 start_progress_timer(void)
551 pt_begin = time(NULL) + PROGRESS_DELAY;
556 set_progress_header(char *header)
558 assert(pt_header == NULL);
559 pt_header = safe_strdup(header);
561 (void) printf("%s: ", header);
562 (void) fflush(stdout);
567 update_progress(char *update)
569 if (!pt_shown && time(NULL) > pt_begin) {
570 int len = strlen(update);
572 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
574 (void) fflush(stdout);
576 } else if (pt_shown) {
577 int len = strlen(update);
579 (void) printf("%s%*.*s", update, len, len, pt_reverse);
580 (void) fflush(stdout);
585 finish_progress(char *done)
588 (void) printf("%s\n", done);
589 (void) fflush(stdout);
595 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
597 * Given an existing dataset, create a writable copy whose initial contents
598 * are the same as the source. The newly created dataset maintains a
599 * dependency on the original; the original cannot be destroyed so long as
602 * The '-p' flag creates all the non-existing ancestors of the target first.
605 zfs_do_clone(int argc, char **argv)
607 zfs_handle_t *zhp = NULL;
608 boolean_t parents = B_FALSE;
613 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
617 while ((c = getopt(argc, argv, "o:p")) != -1) {
620 if (parseprop(props))
627 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
636 /* check number of arguments */
638 (void) fprintf(stderr, gettext("missing source dataset "
643 (void) fprintf(stderr, gettext("missing target dataset "
648 (void) fprintf(stderr, gettext("too many arguments\n"));
652 /* open the source dataset */
653 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
656 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
659 * Now create the ancestors of the target dataset. If the
660 * target already exists and '-p' option was used we should not
663 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
666 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
671 ret = zfs_clone(zhp, argv[1], props);
673 /* create the mountpoint if necessary */
677 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
679 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
680 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
681 ret = zfs_share(clone);
700 * zfs create [-pu] [-o prop=value] ... fs
701 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
703 * Create a new dataset. This command can be used to create filesystems
704 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
705 * For volumes, the user must specify a size to be used.
707 * The '-s' flag applies only to volumes, and indicates that we should not try
708 * to set the reservation for this volume. By default we set a reservation
709 * equal to the size for any volume. For pools with SPA_VERSION >=
710 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
712 * The '-p' flag creates all the non-existing ancestors of the target first.
714 * The '-u' flag prevents mounting of newly created file system.
717 zfs_do_create(int argc, char **argv)
719 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
720 zfs_handle_t *zhp = NULL;
723 boolean_t noreserve = B_FALSE;
724 boolean_t bflag = B_FALSE;
725 boolean_t parents = B_FALSE;
726 boolean_t nomount = B_FALSE;
730 int canmount = ZFS_CANMOUNT_OFF;
732 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
736 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) {
739 type = ZFS_TYPE_VOLUME;
740 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
741 (void) fprintf(stderr, gettext("bad volume "
742 "size '%s': %s\n"), optarg,
743 libzfs_error_description(g_zfs));
747 if (nvlist_add_uint64(props,
748 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
757 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
758 (void) fprintf(stderr, gettext("bad volume "
759 "block size '%s': %s\n"), optarg,
760 libzfs_error_description(g_zfs));
764 if (nvlist_add_uint64(props,
765 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
770 if (parseprop(props))
780 (void) fprintf(stderr, gettext("missing size "
784 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
790 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
791 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
792 "used when creating a volume\n"));
795 if (nomount && type != ZFS_TYPE_FILESYSTEM) {
796 (void) fprintf(stderr, gettext("'-u' can only be "
797 "used when creating a file system\n"));
804 /* check number of arguments */
806 (void) fprintf(stderr, gettext("missing %s argument\n"),
807 zfs_type_to_name(type));
811 (void) fprintf(stderr, gettext("too many arguments\n"));
815 if (type == ZFS_TYPE_VOLUME && !noreserve) {
816 zpool_handle_t *zpool_handle;
817 uint64_t spa_version;
819 zfs_prop_t resv_prop;
822 if (p = strchr(argv[0], '/'))
824 zpool_handle = zpool_open(g_zfs, argv[0]);
827 if (zpool_handle == NULL)
829 spa_version = zpool_get_prop_int(zpool_handle,
830 ZPOOL_PROP_VERSION, NULL);
831 zpool_close(zpool_handle);
832 if (spa_version >= SPA_VERSION_REFRESERVATION)
833 resv_prop = ZFS_PROP_REFRESERVATION;
835 resv_prop = ZFS_PROP_RESERVATION;
836 volsize = zvol_volsize_to_reservation(volsize, props);
838 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
840 if (nvlist_add_uint64(props,
841 zfs_prop_to_name(resv_prop), volsize) != 0) {
848 if (parents && zfs_name_valid(argv[0], type)) {
850 * Now create the ancestors of target dataset. If the target
851 * already exists and '-p' option was used we should not
854 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
858 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
863 if (zfs_create(g_zfs, argv[0], type, props) != 0)
866 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
871 * if the user doesn't want the dataset automatically mounted,
872 * then skip the mount/share step
874 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
875 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
878 * Mount and/or share the new filesystem as appropriate. We provide a
879 * verbose error message to let the user know that their filesystem was
880 * in fact created, even if we failed to mount or share it.
882 if (!nomount && canmount == ZFS_CANMOUNT_ON) {
883 if (zfs_mount(zhp, NULL, 0) != 0) {
884 (void) fprintf(stderr, gettext("filesystem "
885 "successfully created, but not mounted\n"));
887 } else if (zfs_share(zhp) != 0) {
888 (void) fprintf(stderr, gettext("filesystem "
889 "successfully created, but not shared\n"));
906 * zfs destroy [-rRf] <fs, vol>
907 * zfs destroy [-rRd] <snap>
909 * -r Recursively destroy all children
910 * -R Recursively destroy all dependents, including clones
911 * -f Force unmounting of any dependents
912 * -d If we can't destroy now, mark for deferred destruction
914 * Destroys the given dataset. By default, it will unmount any filesystems,
915 * and refuse to destroy a dataset that has any dependents. A dependent can
916 * either be a child, or a clone of a child.
918 typedef struct destroy_cbdata {
921 boolean_t cb_recurse;
923 boolean_t cb_doclones;
924 zfs_handle_t *cb_target;
925 boolean_t cb_defer_destroy;
926 boolean_t cb_verbose;
927 boolean_t cb_parsable;
930 nvlist_t *cb_batchedsnaps;
932 /* first snap in contiguous run */
934 /* previous snap in contiguous run */
942 * Check for any dependents based on the '-r' or '-R' flags.
945 destroy_check_dependent(zfs_handle_t *zhp, void *data)
947 destroy_cbdata_t *cbp = data;
948 const char *tname = zfs_get_name(cbp->cb_target);
949 const char *name = zfs_get_name(zhp);
951 if (strncmp(tname, name, strlen(tname)) == 0 &&
952 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
954 * This is a direct descendant, not a clone somewhere else in
961 (void) fprintf(stderr, gettext("cannot destroy '%s': "
962 "%s has children\n"),
963 zfs_get_name(cbp->cb_target),
964 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
965 (void) fprintf(stderr, gettext("use '-r' to destroy "
966 "the following datasets:\n"));
967 cbp->cb_first = B_FALSE;
968 cbp->cb_error = B_TRUE;
971 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
974 * This is a clone. We only want to report this if the '-r'
975 * wasn't specified, or the target is a snapshot.
977 if (!cbp->cb_recurse &&
978 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
982 (void) fprintf(stderr, gettext("cannot destroy '%s': "
983 "%s has dependent clones\n"),
984 zfs_get_name(cbp->cb_target),
985 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
986 (void) fprintf(stderr, gettext("use '-R' to destroy "
987 "the following datasets:\n"));
988 cbp->cb_first = B_FALSE;
989 cbp->cb_error = B_TRUE;
990 cbp->cb_dryrun = B_TRUE;
993 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1002 destroy_callback(zfs_handle_t *zhp, void *data)
1004 destroy_cbdata_t *cb = data;
1005 const char *name = zfs_get_name(zhp);
1007 if (cb->cb_verbose) {
1008 if (cb->cb_parsable) {
1009 (void) printf("destroy\t%s\n", name);
1010 } else if (cb->cb_dryrun) {
1011 (void) printf(gettext("would destroy %s\n"),
1014 (void) printf(gettext("will destroy %s\n"),
1020 * Ignore pools (which we've already flagged as an error before getting
1023 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1024 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1028 if (cb->cb_dryrun) {
1034 * We batch up all contiguous snapshots (even of different
1035 * filesystems) and destroy them with one ioctl. We can't
1036 * simply do all snap deletions and then all fs deletions,
1037 * because we must delete a clone before its origin.
1039 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1040 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1042 int error = zfs_destroy_snaps_nvl(g_zfs,
1043 cb->cb_batchedsnaps, B_FALSE);
1044 fnvlist_free(cb->cb_batchedsnaps);
1045 cb->cb_batchedsnaps = fnvlist_alloc();
1048 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1049 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1060 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1062 destroy_cbdata_t *cb = arg;
1063 const char *name = zfs_get_name(zhp);
1066 if (nvlist_exists(cb->cb_nvl, name)) {
1067 if (cb->cb_firstsnap == NULL)
1068 cb->cb_firstsnap = strdup(name);
1069 if (cb->cb_prevsnap != NULL)
1070 free(cb->cb_prevsnap);
1071 /* this snap continues the current range */
1072 cb->cb_prevsnap = strdup(name);
1073 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1075 if (cb->cb_verbose) {
1076 if (cb->cb_parsable) {
1077 (void) printf("destroy\t%s\n", name);
1078 } else if (cb->cb_dryrun) {
1079 (void) printf(gettext("would destroy %s\n"),
1082 (void) printf(gettext("will destroy %s\n"),
1086 } else if (cb->cb_firstsnap != NULL) {
1087 /* end of this range */
1089 err = lzc_snaprange_space(cb->cb_firstsnap,
1090 cb->cb_prevsnap, &used);
1091 cb->cb_snapused += used;
1092 free(cb->cb_firstsnap);
1093 cb->cb_firstsnap = NULL;
1094 free(cb->cb_prevsnap);
1095 cb->cb_prevsnap = NULL;
1102 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1105 assert(cb->cb_firstsnap == NULL);
1106 assert(cb->cb_prevsnap == NULL);
1107 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1108 if (cb->cb_firstsnap != NULL) {
1111 err = lzc_snaprange_space(cb->cb_firstsnap,
1112 cb->cb_prevsnap, &used);
1114 cb->cb_snapused += used;
1115 free(cb->cb_firstsnap);
1116 cb->cb_firstsnap = NULL;
1117 free(cb->cb_prevsnap);
1118 cb->cb_prevsnap = NULL;
1124 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1126 destroy_cbdata_t *cb = arg;
1129 /* Check for clones. */
1130 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1131 cb->cb_target = zhp;
1132 cb->cb_first = B_TRUE;
1133 err = zfs_iter_dependents(zhp, B_TRUE,
1134 destroy_check_dependent, cb);
1138 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1146 gather_snapshots(zfs_handle_t *zhp, void *arg)
1148 destroy_cbdata_t *cb = arg;
1151 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1157 if (cb->cb_verbose) {
1158 err = destroy_print_snapshots(zhp, cb);
1164 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1172 destroy_clones(destroy_cbdata_t *cb)
1175 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1177 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1178 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1181 boolean_t defer = cb->cb_defer_destroy;
1185 * We can't defer destroy non-snapshots, so set it to
1186 * false while destroying the clones.
1188 cb->cb_defer_destroy = B_FALSE;
1189 err = zfs_iter_dependents(zhp, B_FALSE,
1190 destroy_callback, cb);
1191 cb->cb_defer_destroy = defer;
1201 zfs_do_destroy(int argc, char **argv)
1203 destroy_cbdata_t cb = { 0 };
1207 zfs_handle_t *zhp = NULL;
1209 zfs_type_t type = ZFS_TYPE_DATASET;
1212 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1215 cb.cb_verbose = B_TRUE;
1218 cb.cb_verbose = B_TRUE;
1219 cb.cb_parsable = B_TRUE;
1222 cb.cb_dryrun = B_TRUE;
1225 cb.cb_defer_destroy = B_TRUE;
1226 type = ZFS_TYPE_SNAPSHOT;
1229 cb.cb_force = B_TRUE;
1232 cb.cb_recurse = B_TRUE;
1235 cb.cb_recurse = B_TRUE;
1236 cb.cb_doclones = B_TRUE;
1240 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1249 /* check number of arguments */
1251 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1255 (void) fprintf(stderr, gettext("too many arguments\n"));
1259 at = strchr(argv[0], '@');
1260 pound = strchr(argv[0], '#');
1263 /* Build the list of snaps to destroy in cb_nvl. */
1264 cb.cb_nvl = fnvlist_alloc();
1267 zhp = zfs_open(g_zfs, argv[0],
1268 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1272 cb.cb_snapspec = at + 1;
1273 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1279 if (nvlist_empty(cb.cb_nvl)) {
1280 (void) fprintf(stderr, gettext("could not find any "
1281 "snapshots to destroy; check snapshot names.\n"));
1286 if (cb.cb_verbose) {
1288 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1289 if (cb.cb_parsable) {
1290 (void) printf("reclaim\t%llu\n",
1292 } else if (cb.cb_dryrun) {
1293 (void) printf(gettext("would reclaim %s\n"),
1296 (void) printf(gettext("will reclaim %s\n"),
1301 if (!cb.cb_dryrun) {
1302 if (cb.cb_doclones) {
1303 cb.cb_batchedsnaps = fnvlist_alloc();
1304 err = destroy_clones(&cb);
1306 err = zfs_destroy_snaps_nvl(g_zfs,
1307 cb.cb_batchedsnaps, B_FALSE);
1315 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1316 cb.cb_defer_destroy);
1322 } else if (pound != NULL) {
1327 (void) fprintf(stderr,
1328 "dryrun is not supported with bookmark\n");
1332 if (cb.cb_defer_destroy) {
1333 (void) fprintf(stderr,
1334 "defer destroy is not supported with bookmark\n");
1338 if (cb.cb_recurse) {
1339 (void) fprintf(stderr,
1340 "recursive is not supported with bookmark\n");
1344 if (!zfs_bookmark_exists(argv[0])) {
1345 (void) fprintf(stderr, gettext("bookmark '%s' "
1346 "does not exist.\n"), argv[0]);
1350 nvl = fnvlist_alloc();
1351 fnvlist_add_boolean(nvl, argv[0]);
1353 err = lzc_destroy_bookmarks(nvl, NULL);
1355 (void) zfs_standard_error(g_zfs, err,
1356 "cannot destroy bookmark");
1359 nvlist_free(cb.cb_nvl);
1363 /* Open the given dataset */
1364 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1370 * Perform an explicit check for pools before going any further.
1372 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1373 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1374 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1375 "operation does not apply to pools\n"),
1377 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1378 "%s' to destroy all datasets in the pool\n"),
1380 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1381 "to destroy the pool itself\n"), zfs_get_name(zhp));
1387 * Check for any dependents and/or clones.
1389 cb.cb_first = B_TRUE;
1390 if (!cb.cb_doclones &&
1391 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1402 cb.cb_batchedsnaps = fnvlist_alloc();
1403 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1410 * Do the real thing. The callback will close the
1411 * handle regardless of whether it succeeds or not.
1413 err = destroy_callback(zhp, &cb);
1416 err = zfs_destroy_snaps_nvl(g_zfs,
1417 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1424 fnvlist_free(cb.cb_batchedsnaps);
1425 fnvlist_free(cb.cb_nvl);
1432 is_recvd_column(zprop_get_cbdata_t *cbp)
1435 zfs_get_column_t col;
1437 for (i = 0; i < ZFS_GET_NCOLS &&
1438 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1439 if (col == GET_COL_RECVD)
1445 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1446 * < all | property[,property]... > < fs | snap | vol > ...
1448 * -r recurse over any child datasets
1449 * -H scripted mode. Headers are stripped, and fields are separated
1450 * by tabs instead of spaces.
1451 * -o Set of fields to display. One of "name,property,value,
1452 * received,source". Default is "name,property,value,source".
1453 * "all" is an alias for all five.
1454 * -s Set of sources to allow. One of
1455 * "local,default,inherited,received,temporary,none". Default is
1457 * -p Display values in parsable (literal) format.
1459 * Prints properties for the given datasets. The user can control which
1460 * columns to display as well as which property types to allow.
1464 * Invoked to display the properties for a single dataset.
1467 get_callback(zfs_handle_t *zhp, void *data)
1469 char buf[ZFS_MAXPROPLEN];
1470 char rbuf[ZFS_MAXPROPLEN];
1471 zprop_source_t sourcetype;
1472 char source[ZFS_MAXNAMELEN];
1473 zprop_get_cbdata_t *cbp = data;
1474 nvlist_t *user_props = zfs_get_user_props(zhp);
1475 zprop_list_t *pl = cbp->cb_proplist;
1479 boolean_t received = is_recvd_column(cbp);
1481 for (; pl != NULL; pl = pl->pl_next) {
1482 char *recvdval = NULL;
1484 * Skip the special fake placeholder. This will also skip over
1485 * the name property when 'all' is specified.
1487 if (pl->pl_prop == ZFS_PROP_NAME &&
1488 pl == cbp->cb_proplist)
1491 if (pl->pl_prop != ZPROP_INVAL) {
1492 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1493 sizeof (buf), &sourcetype, source,
1495 cbp->cb_literal) != 0) {
1498 if (!zfs_prop_valid_for_type(pl->pl_prop,
1499 ZFS_TYPE_DATASET)) {
1500 (void) fprintf(stderr,
1501 gettext("No such property '%s'\n"),
1502 zfs_prop_to_name(pl->pl_prop));
1505 sourcetype = ZPROP_SRC_NONE;
1506 (void) strlcpy(buf, "-", sizeof (buf));
1509 if (received && (zfs_prop_get_recvd(zhp,
1510 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1511 cbp->cb_literal) == 0))
1514 zprop_print_one_property(zfs_get_name(zhp), cbp,
1515 zfs_prop_to_name(pl->pl_prop),
1516 buf, sourcetype, source, recvdval);
1517 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1518 sourcetype = ZPROP_SRC_LOCAL;
1520 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1521 buf, sizeof (buf), cbp->cb_literal) != 0) {
1522 sourcetype = ZPROP_SRC_NONE;
1523 (void) strlcpy(buf, "-", sizeof (buf));
1526 zprop_print_one_property(zfs_get_name(zhp), cbp,
1527 pl->pl_user_prop, buf, sourcetype, source, NULL);
1528 } else if (zfs_prop_written(pl->pl_user_prop)) {
1529 sourcetype = ZPROP_SRC_LOCAL;
1531 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1532 buf, sizeof (buf), cbp->cb_literal) != 0) {
1533 sourcetype = ZPROP_SRC_NONE;
1534 (void) strlcpy(buf, "-", sizeof (buf));
1537 zprop_print_one_property(zfs_get_name(zhp), cbp,
1538 pl->pl_user_prop, buf, sourcetype, source, NULL);
1540 if (nvlist_lookup_nvlist(user_props,
1541 pl->pl_user_prop, &propval) != 0) {
1544 sourcetype = ZPROP_SRC_NONE;
1547 verify(nvlist_lookup_string(propval,
1548 ZPROP_VALUE, &strval) == 0);
1549 verify(nvlist_lookup_string(propval,
1550 ZPROP_SOURCE, &sourceval) == 0);
1552 if (strcmp(sourceval,
1553 zfs_get_name(zhp)) == 0) {
1554 sourcetype = ZPROP_SRC_LOCAL;
1555 } else if (strcmp(sourceval,
1556 ZPROP_SOURCE_VAL_RECVD) == 0) {
1557 sourcetype = ZPROP_SRC_RECEIVED;
1559 sourcetype = ZPROP_SRC_INHERITED;
1560 (void) strlcpy(source,
1561 sourceval, sizeof (source));
1565 if (received && (zfs_prop_get_recvd(zhp,
1566 pl->pl_user_prop, rbuf, sizeof (rbuf),
1567 cbp->cb_literal) == 0))
1570 zprop_print_one_property(zfs_get_name(zhp), cbp,
1571 pl->pl_user_prop, strval, sourcetype,
1580 zfs_do_get(int argc, char **argv)
1582 zprop_get_cbdata_t cb = { 0 };
1583 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1584 int types = ZFS_TYPE_DATASET;
1585 char *value, *fields;
1588 zprop_list_t fake_name = { 0 };
1591 * Set up default columns and sources.
1593 cb.cb_sources = ZPROP_SRC_ALL;
1594 cb.cb_columns[0] = GET_COL_NAME;
1595 cb.cb_columns[1] = GET_COL_PROPERTY;
1596 cb.cb_columns[2] = GET_COL_VALUE;
1597 cb.cb_columns[3] = GET_COL_SOURCE;
1598 cb.cb_type = ZFS_TYPE_DATASET;
1601 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1604 cb.cb_literal = B_TRUE;
1607 limit = parse_depth(optarg, &flags);
1610 flags |= ZFS_ITER_RECURSE;
1613 cb.cb_scripted = B_TRUE;
1616 (void) fprintf(stderr, gettext("missing argument for "
1617 "'%c' option\n"), optopt);
1622 * Process the set of columns to display. We zero out
1623 * the structure to give us a blank slate.
1625 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1627 while (*optarg != '\0') {
1628 static char *col_subopts[] =
1629 { "name", "property", "value", "received",
1630 "source", "all", NULL };
1632 if (i == ZFS_GET_NCOLS) {
1633 (void) fprintf(stderr, gettext("too "
1634 "many fields given to -o "
1639 switch (getsubopt(&optarg, col_subopts,
1642 cb.cb_columns[i++] = GET_COL_NAME;
1645 cb.cb_columns[i++] = GET_COL_PROPERTY;
1648 cb.cb_columns[i++] = GET_COL_VALUE;
1651 cb.cb_columns[i++] = GET_COL_RECVD;
1652 flags |= ZFS_ITER_RECVD_PROPS;
1655 cb.cb_columns[i++] = GET_COL_SOURCE;
1659 (void) fprintf(stderr,
1660 gettext("\"all\" conflicts "
1661 "with specific fields "
1662 "given to -o option\n"));
1665 cb.cb_columns[0] = GET_COL_NAME;
1666 cb.cb_columns[1] = GET_COL_PROPERTY;
1667 cb.cb_columns[2] = GET_COL_VALUE;
1668 cb.cb_columns[3] = GET_COL_RECVD;
1669 cb.cb_columns[4] = GET_COL_SOURCE;
1670 flags |= ZFS_ITER_RECVD_PROPS;
1674 (void) fprintf(stderr,
1675 gettext("invalid column name "
1684 while (*optarg != '\0') {
1685 static char *source_subopts[] = {
1686 "local", "default", "inherited",
1687 "received", "temporary", "none",
1690 switch (getsubopt(&optarg, source_subopts,
1693 cb.cb_sources |= ZPROP_SRC_LOCAL;
1696 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1699 cb.cb_sources |= ZPROP_SRC_INHERITED;
1702 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1705 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1708 cb.cb_sources |= ZPROP_SRC_NONE;
1711 (void) fprintf(stderr,
1712 gettext("invalid source "
1721 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1722 while (*optarg != '\0') {
1723 static char *type_subopts[] = { "filesystem",
1724 "volume", "snapshot", "bookmark",
1727 switch (getsubopt(&optarg, type_subopts,
1730 types |= ZFS_TYPE_FILESYSTEM;
1733 types |= ZFS_TYPE_VOLUME;
1736 types |= ZFS_TYPE_SNAPSHOT;
1739 types |= ZFS_TYPE_BOOKMARK;
1742 types = ZFS_TYPE_DATASET |
1747 (void) fprintf(stderr,
1748 gettext("invalid type '%s'\n"),
1756 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1766 (void) fprintf(stderr, gettext("missing property "
1773 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1781 * As part of zfs_expand_proplist(), we keep track of the maximum column
1782 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1783 * need to know the maximum name length. However, the user likely did
1784 * not specify 'name' as one of the properties to fetch, so we need to
1785 * make sure we always include at least this property for
1786 * print_get_headers() to work properly.
1788 if (cb.cb_proplist != NULL) {
1789 fake_name.pl_prop = ZFS_PROP_NAME;
1790 fake_name.pl_width = strlen(gettext("NAME"));
1791 fake_name.pl_next = cb.cb_proplist;
1792 cb.cb_proplist = &fake_name;
1795 cb.cb_first = B_TRUE;
1797 /* run for each object */
1798 ret = zfs_for_each(argc, argv, flags, types, NULL,
1799 &cb.cb_proplist, limit, get_callback, &cb);
1801 if (cb.cb_proplist == &fake_name)
1802 zprop_free_list(fake_name.pl_next);
1804 zprop_free_list(cb.cb_proplist);
1810 * inherit [-rS] <property> <fs|vol> ...
1812 * -r Recurse over all children
1813 * -S Revert to received value, if any
1815 * For each dataset specified on the command line, inherit the given property
1816 * from its parent. Inheriting a property at the pool level will cause it to
1817 * use the default value. The '-r' flag will recurse over all children, and is
1818 * useful for setting a property on a hierarchy-wide basis, regardless of any
1819 * local modifications for each dataset.
1822 typedef struct inherit_cbdata {
1823 const char *cb_propname;
1824 boolean_t cb_received;
1828 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1830 inherit_cbdata_t *cb = data;
1831 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1834 * If we're doing it recursively, then ignore properties that
1835 * are not valid for this type of dataset.
1837 if (prop != ZPROP_INVAL &&
1838 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1841 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1845 inherit_cb(zfs_handle_t *zhp, void *data)
1847 inherit_cbdata_t *cb = data;
1849 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1853 zfs_do_inherit(int argc, char **argv)
1857 inherit_cbdata_t cb = { 0 };
1861 boolean_t received = B_FALSE;
1864 while ((c = getopt(argc, argv, "rS")) != -1) {
1867 flags |= ZFS_ITER_RECURSE;
1874 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1883 /* check number of arguments */
1885 (void) fprintf(stderr, gettext("missing property argument\n"));
1889 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1897 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1898 if (zfs_prop_readonly(prop)) {
1899 (void) fprintf(stderr, gettext(
1900 "%s property is read-only\n"),
1904 if (!zfs_prop_inheritable(prop) && !received) {
1905 (void) fprintf(stderr, gettext("'%s' property cannot "
1906 "be inherited\n"), propname);
1907 if (prop == ZFS_PROP_QUOTA ||
1908 prop == ZFS_PROP_RESERVATION ||
1909 prop == ZFS_PROP_REFQUOTA ||
1910 prop == ZFS_PROP_REFRESERVATION)
1911 (void) fprintf(stderr, gettext("use 'zfs set "
1912 "%s=none' to clear\n"), propname);
1915 if (received && (prop == ZFS_PROP_VOLSIZE ||
1916 prop == ZFS_PROP_VERSION)) {
1917 (void) fprintf(stderr, gettext("'%s' property cannot "
1918 "be reverted to a received value\n"), propname);
1921 } else if (!zfs_prop_user(propname)) {
1922 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1927 cb.cb_propname = propname;
1928 cb.cb_received = received;
1930 if (flags & ZFS_ITER_RECURSE) {
1931 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1932 NULL, NULL, 0, inherit_recurse_cb, &cb);
1934 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1935 NULL, NULL, 0, inherit_cb, &cb);
1941 typedef struct upgrade_cbdata {
1942 uint64_t cb_numupgraded;
1943 uint64_t cb_numsamegraded;
1944 uint64_t cb_numfailed;
1945 uint64_t cb_version;
1947 boolean_t cb_foundone;
1948 char cb_lastfs[ZFS_MAXNAMELEN];
1952 same_pool(zfs_handle_t *zhp, const char *name)
1954 int len1 = strcspn(name, "/@");
1955 const char *zhname = zfs_get_name(zhp);
1956 int len2 = strcspn(zhname, "/@");
1960 return (strncmp(name, zhname, len1) == 0);
1964 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1966 upgrade_cbdata_t *cb = data;
1967 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1969 /* list if it's old/new */
1970 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1971 (cb->cb_newer && version > ZPL_VERSION)) {
1974 str = gettext("The following filesystems are "
1975 "formatted using a newer software version and\n"
1976 "cannot be accessed on the current system.\n\n");
1978 str = gettext("The following filesystems are "
1979 "out of date, and can be upgraded. After being\n"
1980 "upgraded, these filesystems (and any 'zfs send' "
1981 "streams generated from\n"
1982 "subsequent snapshots) will no longer be "
1983 "accessible by older software versions.\n\n");
1986 if (!cb->cb_foundone) {
1988 (void) printf(gettext("VER FILESYSTEM\n"));
1989 (void) printf(gettext("--- ------------\n"));
1990 cb->cb_foundone = B_TRUE;
1993 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2000 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2002 upgrade_cbdata_t *cb = data;
2003 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2004 int needed_spa_version;
2007 if (zfs_spa_version(zhp, &spa_version) < 0)
2010 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2012 if (needed_spa_version < 0)
2015 if (spa_version < needed_spa_version) {
2017 (void) printf(gettext("%s: can not be "
2018 "upgraded; the pool version needs to first "
2019 "be upgraded\nto version %d\n\n"),
2020 zfs_get_name(zhp), needed_spa_version);
2026 if (version < cb->cb_version) {
2028 (void) snprintf(verstr, sizeof (verstr),
2029 "%llu", cb->cb_version);
2030 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2032 * If they did "zfs upgrade -a", then we could
2033 * be doing ioctls to different pools. We need
2034 * to log this history once to each pool, and bypass
2035 * the normal history logging that happens in main().
2037 (void) zpool_log_history(g_zfs, history_str);
2038 log_history = B_FALSE;
2040 if (zfs_prop_set(zhp, "version", verstr) == 0)
2041 cb->cb_numupgraded++;
2044 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2045 } else if (version > cb->cb_version) {
2046 /* can't downgrade */
2047 (void) printf(gettext("%s: can not be downgraded; "
2048 "it is already at version %u\n"),
2049 zfs_get_name(zhp), version);
2052 cb->cb_numsamegraded++;
2060 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2063 zfs_do_upgrade(int argc, char **argv)
2065 boolean_t all = B_FALSE;
2066 boolean_t showversions = B_FALSE;
2068 upgrade_cbdata_t cb = { 0 };
2070 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2073 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2076 flags |= ZFS_ITER_RECURSE;
2079 showversions = B_TRUE;
2082 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2083 optarg, &cb.cb_version) != 0) {
2084 (void) fprintf(stderr,
2085 gettext("invalid version %s\n"), optarg);
2094 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2103 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2105 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2106 cb.cb_version || argc))
2108 if ((all || argc) && (showversions))
2114 /* Show info on available versions. */
2115 (void) printf(gettext("The following filesystem versions are "
2117 (void) printf(gettext("VER DESCRIPTION\n"));
2118 (void) printf("--- -----------------------------------------"
2119 "---------------\n");
2120 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2121 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2122 (void) printf(gettext(" 3 Case insensitive and filesystem "
2123 "user identifier (FUID)\n"));
2124 (void) printf(gettext(" 4 userquota, groupquota "
2126 (void) printf(gettext(" 5 System attributes\n"));
2127 (void) printf(gettext("\nFor more information on a particular "
2128 "version, including supported releases,\n"));
2129 (void) printf("see the ZFS Administration Guide.\n\n");
2131 } else if (argc || all) {
2132 /* Upgrade filesystems */
2133 if (cb.cb_version == 0)
2134 cb.cb_version = ZPL_VERSION;
2135 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2136 NULL, NULL, 0, upgrade_set_callback, &cb);
2137 (void) printf(gettext("%llu filesystems upgraded\n"),
2139 if (cb.cb_numsamegraded) {
2140 (void) printf(gettext("%llu filesystems already at "
2142 cb.cb_numsamegraded);
2144 if (cb.cb_numfailed != 0)
2147 /* List old-version filesytems */
2149 (void) printf(gettext("This system is currently running "
2150 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2152 flags |= ZFS_ITER_RECURSE;
2153 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2154 NULL, NULL, 0, upgrade_list_callback, &cb);
2156 found = cb.cb_foundone;
2157 cb.cb_foundone = B_FALSE;
2158 cb.cb_newer = B_TRUE;
2160 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2161 NULL, NULL, 0, upgrade_list_callback, &cb);
2163 if (!cb.cb_foundone && !found) {
2164 (void) printf(gettext("All filesystems are "
2165 "formatted with the current version.\n"));
2173 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2174 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2175 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2176 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2178 * -H Scripted mode; elide headers and separate columns by tabs.
2179 * -i Translate SID to POSIX ID.
2180 * -n Print numeric ID instead of user/group name.
2181 * -o Control which fields to display.
2182 * -p Use exact (parsable) numeric output.
2183 * -s Specify sort columns, descending order.
2184 * -S Specify sort columns, ascending order.
2185 * -t Control which object types to display.
2187 * Displays space consumed by, and quotas on, each user in the specified
2188 * filesystem or snapshot.
2191 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2192 enum us_field_types {
2198 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2199 static char *us_field_names[] = { "type", "name", "used", "quota" };
2200 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2202 #define USTYPE_PSX_GRP (1 << 0)
2203 #define USTYPE_PSX_USR (1 << 1)
2204 #define USTYPE_SMB_GRP (1 << 2)
2205 #define USTYPE_SMB_USR (1 << 3)
2206 #define USTYPE_ALL \
2207 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2209 static int us_type_bits[] = {
2216 static char *us_type_names[] = { "posixgroup", "posxiuser", "smbgroup",
2219 typedef struct us_node {
2221 uu_avl_node_t usn_avlnode;
2222 uu_list_node_t usn_listnode;
2225 typedef struct us_cbdata {
2227 uu_avl_pool_t *cb_avl_pool;
2229 boolean_t cb_numname;
2230 boolean_t cb_nicenum;
2231 boolean_t cb_sid2posix;
2232 zfs_userquota_prop_t cb_prop;
2233 zfs_sort_column_t *cb_sortcol;
2234 size_t cb_width[USFIELD_LAST];
2237 static boolean_t us_populated = B_FALSE;
2240 zfs_sort_column_t *si_sortcol;
2241 boolean_t si_numname;
2245 us_field_index(char *field)
2249 for (i = 0; i < USFIELD_LAST; i++) {
2250 if (strcmp(field, us_field_names[i]) == 0)
2258 us_compare(const void *larg, const void *rarg, void *unused)
2260 const us_node_t *l = larg;
2261 const us_node_t *r = rarg;
2262 us_sort_info_t *si = (us_sort_info_t *)unused;
2263 zfs_sort_column_t *sortcol = si->si_sortcol;
2264 boolean_t numname = si->si_numname;
2265 nvlist_t *lnvl = l->usn_nvl;
2266 nvlist_t *rnvl = r->usn_nvl;
2270 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2277 zfs_prop_t prop = sortcol->sc_prop;
2278 const char *propname = NULL;
2279 boolean_t reverse = sortcol->sc_reverse;
2284 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2285 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2287 rc = (rv32 < lv32) ? 1 : -1;
2292 (void) nvlist_lookup_uint64(lnvl, propname,
2294 (void) nvlist_lookup_uint64(rnvl, propname,
2297 rc = (rv64 < lv64) ? 1 : -1;
2299 (void) nvlist_lookup_string(lnvl, propname,
2301 (void) nvlist_lookup_string(rnvl, propname,
2303 rc = strcmp(lvstr, rvstr);
2307 case ZFS_PROP_QUOTA:
2310 if (prop == ZFS_PROP_USED)
2314 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2315 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2317 rc = (rv64 < lv64) ? 1 : -1;
2323 return (reverse ? 1 : -1);
2325 return (reverse ? -1 : 1);
2330 * If entries still seem to be the same, check if they are of the same
2331 * type (smbentity is added only if we are doing SID to POSIX ID
2332 * translation where we can have duplicate type/name combinations).
2334 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2335 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2337 return (lvb < rvb ? -1 : 1);
2342 static inline const char *
2343 us_type2str(unsigned field_type)
2345 switch (field_type) {
2346 case USTYPE_PSX_USR:
2347 return ("POSIX User");
2348 case USTYPE_PSX_GRP:
2349 return ("POSIX Group");
2350 case USTYPE_SMB_USR:
2351 return ("SMB User");
2352 case USTYPE_SMB_GRP:
2353 return ("SMB Group");
2355 return ("Undefined");
2360 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2362 us_cbdata_t *cb = (us_cbdata_t *)arg;
2363 zfs_userquota_prop_t prop = cb->cb_prop;
2368 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2369 uu_avl_t *avl = cb->cb_avl;
2373 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2375 const char *typestr;
2379 int typeidx, nameidx, sizeidx;
2380 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2381 boolean_t smbentity = B_FALSE;
2383 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2385 node = safe_malloc(sizeof (us_node_t));
2386 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2387 node->usn_nvl = props;
2389 if (domain != NULL && domain[0] != '\0') {
2391 char sid[ZFS_MAXNAMELEN + 32];
2396 directory_error_t e;
2401 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2403 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2404 type = USTYPE_SMB_GRP;
2406 err = sid_to_id(sid, B_FALSE, &id);
2409 type = USTYPE_SMB_USR;
2411 err = sid_to_id(sid, B_TRUE, &id);
2418 if (!cb->cb_sid2posix) {
2419 e = directory_name_from_sid(NULL, sid, &name,
2422 directory_error_free(e);
2430 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2432 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2433 type = USTYPE_PSX_GRP;
2434 if (!cb->cb_numname) {
2437 if ((g = getgrgid(rid)) != NULL)
2441 type = USTYPE_PSX_USR;
2442 if (!cb->cb_numname) {
2445 if ((p = getpwuid(rid)) != NULL)
2452 * Make sure that the type/name combination is unique when doing
2453 * SID to POSIX ID translation (hence changing the type from SMB to
2456 if (cb->cb_sid2posix &&
2457 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2460 /* Calculate/update width of TYPE field */
2461 typestr = us_type2str(type);
2462 typelen = strlen(gettext(typestr));
2463 typeidx = us_field_index("type");
2464 if (typelen > cb->cb_width[typeidx])
2465 cb->cb_width[typeidx] = typelen;
2466 if (nvlist_add_uint32(props, "type", type) != 0)
2469 /* Calculate/update width of NAME field */
2470 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2471 if (nvlist_add_uint64(props, "name", rid) != 0)
2473 namelen = snprintf(NULL, 0, "%u", rid);
2475 if (nvlist_add_string(props, "name", name) != 0)
2477 namelen = strlen(name);
2479 nameidx = us_field_index("name");
2480 if (namelen > cb->cb_width[nameidx])
2481 cb->cb_width[nameidx] = namelen;
2484 * Check if this type/name combination is in the list and update it;
2485 * otherwise add new node to the list.
2487 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2488 uu_avl_insert(avl, node, idx);
2493 props = node->usn_nvl;
2496 /* Calculate/update width of USED/QUOTA fields */
2498 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2500 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2501 sizelen = strlen(sizebuf);
2502 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2504 if (!nvlist_exists(props, "quota"))
2505 (void) nvlist_add_uint64(props, "quota", 0);
2508 if (!nvlist_exists(props, "used"))
2509 (void) nvlist_add_uint64(props, "used", 0);
2511 sizeidx = us_field_index(propname);
2512 if (sizelen > cb->cb_width[sizeidx])
2513 cb->cb_width[sizeidx] = sizelen;
2515 if (nvlist_add_uint64(props, propname, space) != 0)
2522 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2523 size_t *width, us_node_t *node)
2525 nvlist_t *nvl = node->usn_nvl;
2526 char valstr[ZFS_MAXNAMELEN];
2527 boolean_t first = B_TRUE;
2533 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2534 if (!(ustype & types))
2537 while ((field = fields[cfield]) != USFIELD_LAST) {
2538 nvpair_t *nvp = NULL;
2542 char *strval = NULL;
2544 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2545 if (strcmp(nvpair_name(nvp),
2546 us_field_names[field]) == 0)
2550 type = nvpair_type(nvp);
2552 case DATA_TYPE_UINT32:
2553 (void) nvpair_value_uint32(nvp, &val32);
2555 case DATA_TYPE_UINT64:
2556 (void) nvpair_value_uint64(nvp, &val64);
2558 case DATA_TYPE_STRING:
2559 (void) nvpair_value_string(nvp, &strval);
2562 (void) fprintf(stderr, "invalid data type\n");
2567 strval = (char *)us_type2str(val32);
2570 if (type == DATA_TYPE_UINT64) {
2571 (void) sprintf(valstr, "%llu", val64);
2577 if (type == DATA_TYPE_UINT64) {
2579 (void) sprintf(valstr, "%llu", val64);
2581 zfs_nicenum(val64, valstr,
2584 if (field == USFIELD_QUOTA &&
2585 strcmp(valstr, "0") == 0)
2595 (void) printf("\t");
2600 (void) printf("%s", strval);
2601 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2602 (void) printf("%-*s", width[field], strval);
2604 (void) printf("%*s", width[field], strval);
2610 (void) printf("\n");
2614 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2615 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2623 boolean_t first = B_TRUE;
2625 while ((field = fields[cfield]) != USFIELD_LAST) {
2626 col = gettext(us_field_hdr[field]);
2627 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2628 (void) printf(first ? "%-*s" : " %-*s",
2631 (void) printf(first ? "%*s" : " %*s",
2637 (void) printf("\n");
2640 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2641 print_us_node(scripted, parsable, fields, types, width, node);
2643 nvlist_free(node->usn_nvl);
2648 zfs_do_userspace(int argc, char **argv)
2651 zfs_userquota_prop_t p;
2653 uu_avl_pool_t *avl_pool;
2655 uu_avl_walk_t *walk;
2657 char deffields[] = "type,name,used,quota";
2658 char *ofield = NULL;
2659 char *tfield = NULL;
2663 boolean_t scripted = B_FALSE;
2664 boolean_t prtnum = B_FALSE;
2665 boolean_t parsable = B_FALSE;
2666 boolean_t sid2posix = B_FALSE;
2669 zfs_sort_column_t *sortcol = NULL;
2670 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2674 uu_list_pool_t *listpool;
2676 uu_avl_index_t idx = 0;
2677 uu_list_index_t idx2 = 0;
2682 if (strcmp(argv[0], "groupspace") == 0)
2683 /* Toggle default group types */
2684 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2686 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2702 if (zfs_add_sort_column(&sortcol, optarg,
2703 c == 's' ? B_FALSE : B_TRUE) != 0) {
2704 (void) fprintf(stderr,
2705 gettext("invalid field '%s'\n"), optarg);
2716 (void) fprintf(stderr, gettext("missing argument for "
2717 "'%c' option\n"), optopt);
2721 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2731 (void) fprintf(stderr, gettext("missing dataset name\n"));
2735 (void) fprintf(stderr, gettext("too many arguments\n"));
2739 /* Use default output fields if not specified using -o */
2743 if ((delim = strchr(ofield, ',')) != NULL)
2745 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2746 (void) fprintf(stderr, gettext("invalid type '%s' "
2747 "for -o option\n"), ofield);
2752 } while (delim != NULL);
2753 fields[cfield] = USFIELD_LAST;
2755 /* Override output types (-t option) */
2756 if (tfield != NULL) {
2760 boolean_t found = B_FALSE;
2762 if ((delim = strchr(tfield, ',')) != NULL)
2764 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2766 if (strcmp(tfield, us_type_names[i]) == 0) {
2768 types |= us_type_bits[i];
2773 (void) fprintf(stderr, gettext("invalid type "
2774 "'%s' for -t option\n"), tfield);
2779 } while (delim != NULL);
2782 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2785 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2786 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2788 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2791 /* Always add default sorting columns */
2792 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2793 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2795 cb.cb_sortcol = sortcol;
2796 cb.cb_numname = prtnum;
2797 cb.cb_nicenum = !parsable;
2798 cb.cb_avl_pool = avl_pool;
2799 cb.cb_avl = avl_tree;
2800 cb.cb_sid2posix = sid2posix;
2802 for (i = 0; i < USFIELD_LAST; i++)
2803 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2805 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2806 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2807 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2808 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2809 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2812 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2817 if ((node = uu_avl_first(avl_tree)) == NULL)
2820 us_populated = B_TRUE;
2822 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2823 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2824 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2825 uu_list_node_init(node, &node->usn_listnode, listpool);
2827 while (node != NULL) {
2829 node = uu_avl_next(avl_tree, node);
2830 uu_avl_remove(avl_tree, rmnode);
2831 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2832 uu_list_insert(list, rmnode, idx2);
2835 for (node = uu_list_first(list); node != NULL;
2836 node = uu_list_next(list, node)) {
2837 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2839 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2840 uu_avl_insert(avl_tree, node, idx);
2843 uu_list_destroy(list);
2844 uu_list_pool_destroy(listpool);
2846 /* Print and free node nvlist memory */
2847 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2850 zfs_free_sort_columns(sortcol);
2852 /* Clean up the AVL tree */
2853 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2856 while ((node = uu_avl_walk_next(walk)) != NULL) {
2857 uu_avl_remove(cb.cb_avl, node);
2861 uu_avl_walk_end(walk);
2862 uu_avl_destroy(avl_tree);
2863 uu_avl_pool_destroy(avl_pool);
2869 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2870 * [-t type[,...]] [filesystem|volume|snapshot] ...
2872 * -H Scripted mode; elide headers and separate columns by tabs.
2873 * -p Display values in parsable (literal) format.
2874 * -r Recurse over all children.
2875 * -d Limit recursion by depth.
2876 * -o Control which fields to display.
2877 * -s Specify sort columns, descending order.
2878 * -S Specify sort columns, ascending order.
2879 * -t Control which object types to display.
2881 * When given no arguments, list all filesystems in the system.
2882 * Otherwise, list the specified datasets, optionally recursing down them if
2883 * '-r' is specified.
2885 typedef struct list_cbdata {
2887 boolean_t cb_literal;
2888 boolean_t cb_scripted;
2889 zprop_list_t *cb_proplist;
2893 * Given a list of columns to display, output appropriate headers for each one.
2896 print_header(list_cbdata_t *cb)
2898 zprop_list_t *pl = cb->cb_proplist;
2899 char headerbuf[ZFS_MAXPROPLEN];
2902 boolean_t first = B_TRUE;
2903 boolean_t right_justify;
2905 for (; pl != NULL; pl = pl->pl_next) {
2912 right_justify = B_FALSE;
2913 if (pl->pl_prop != ZPROP_INVAL) {
2914 header = zfs_prop_column_name(pl->pl_prop);
2915 right_justify = zfs_prop_align_right(pl->pl_prop);
2917 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2918 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2919 headerbuf[i] = '\0';
2923 if (pl->pl_next == NULL && !right_justify)
2924 (void) printf("%s", header);
2925 else if (right_justify)
2926 (void) printf("%*s", pl->pl_width, header);
2928 (void) printf("%-*s", pl->pl_width, header);
2931 (void) printf("\n");
2935 * Given a dataset and a list of fields, print out all the properties according
2936 * to the described layout.
2939 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2941 zprop_list_t *pl = cb->cb_proplist;
2942 boolean_t first = B_TRUE;
2943 char property[ZFS_MAXPROPLEN];
2944 nvlist_t *userprops = zfs_get_user_props(zhp);
2947 boolean_t right_justify;
2949 for (; pl != NULL; pl = pl->pl_next) {
2951 if (cb->cb_scripted)
2952 (void) printf("\t");
2959 if (pl->pl_prop == ZFS_PROP_NAME) {
2960 (void) strlcpy(property, zfs_get_name(zhp),
2963 right_justify = zfs_prop_align_right(pl->pl_prop);
2964 } else if (pl->pl_prop != ZPROP_INVAL) {
2965 if (zfs_prop_get(zhp, pl->pl_prop, property,
2966 sizeof (property), NULL, NULL, 0,
2967 cb->cb_literal) != 0)
2971 right_justify = zfs_prop_align_right(pl->pl_prop);
2972 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2973 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2974 property, sizeof (property), cb->cb_literal) != 0)
2978 right_justify = B_TRUE;
2979 } else if (zfs_prop_written(pl->pl_user_prop)) {
2980 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2981 property, sizeof (property), cb->cb_literal) != 0)
2985 right_justify = B_TRUE;
2987 if (nvlist_lookup_nvlist(userprops,
2988 pl->pl_user_prop, &propval) != 0)
2991 verify(nvlist_lookup_string(propval,
2992 ZPROP_VALUE, &propstr) == 0);
2993 right_justify = B_FALSE;
2997 * If this is being called in scripted mode, or if this is the
2998 * last column and it is left-justified, don't include a width
3001 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3002 (void) printf("%s", propstr);
3003 else if (right_justify)
3004 (void) printf("%*s", pl->pl_width, propstr);
3006 (void) printf("%-*s", pl->pl_width, propstr);
3009 (void) printf("\n");
3013 * Generic callback function to list a dataset or snapshot.
3016 list_callback(zfs_handle_t *zhp, void *data)
3018 list_cbdata_t *cbp = data;
3020 if (cbp->cb_first) {
3021 if (!cbp->cb_scripted)
3023 cbp->cb_first = B_FALSE;
3026 print_dataset(zhp, cbp);
3032 zfs_do_list(int argc, char **argv)
3035 static char default_fields[] =
3036 "name,used,available,referenced,mountpoint";
3037 int types = ZFS_TYPE_DATASET;
3038 boolean_t types_specified = B_FALSE;
3039 char *fields = NULL;
3040 list_cbdata_t cb = { 0 };
3044 zfs_sort_column_t *sortcol = NULL;
3045 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3048 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3054 cb.cb_literal = B_TRUE;
3055 flags |= ZFS_ITER_LITERAL_PROPS;
3058 limit = parse_depth(optarg, &flags);
3061 flags |= ZFS_ITER_RECURSE;
3064 cb.cb_scripted = B_TRUE;
3067 if (zfs_add_sort_column(&sortcol, optarg,
3069 (void) fprintf(stderr,
3070 gettext("invalid property '%s'\n"), optarg);
3075 if (zfs_add_sort_column(&sortcol, optarg,
3077 (void) fprintf(stderr,
3078 gettext("invalid property '%s'\n"), optarg);
3084 types_specified = B_TRUE;
3085 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3086 while (*optarg != '\0') {
3087 static char *type_subopts[] = { "filesystem",
3088 "volume", "snapshot", "snap", "bookmark",
3091 switch (getsubopt(&optarg, type_subopts,
3094 types |= ZFS_TYPE_FILESYSTEM;
3097 types |= ZFS_TYPE_VOLUME;
3101 types |= ZFS_TYPE_SNAPSHOT;
3104 types |= ZFS_TYPE_BOOKMARK;
3107 types = ZFS_TYPE_DATASET |
3111 (void) fprintf(stderr,
3112 gettext("invalid type '%s'\n"),
3119 (void) fprintf(stderr, gettext("missing argument for "
3120 "'%c' option\n"), optopt);
3124 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3134 fields = default_fields;
3137 * If we are only going to list snapshot names and sort by name,
3138 * then we can use faster version.
3140 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3141 flags |= ZFS_ITER_SIMPLE;
3144 * If "-o space" and no types were specified, don't display snapshots.
3146 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3147 types &= ~ZFS_TYPE_SNAPSHOT;
3150 * If the user specifies '-o all', the zprop_get_list() doesn't
3151 * normally include the name of the dataset. For 'zfs list', we always
3152 * want this property to be first.
3154 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3158 cb.cb_first = B_TRUE;
3160 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3161 limit, list_callback, &cb);
3163 zprop_free_list(cb.cb_proplist);
3164 zfs_free_sort_columns(sortcol);
3166 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3167 (void) printf(gettext("no datasets available\n"));
3173 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3174 * zfs rename [-f] -p <fs | vol> <fs | vol>
3175 * zfs rename -r <snap> <snap>
3176 * zfs rename -u [-p] <fs> <fs>
3178 * Renames the given dataset to another of the same type.
3180 * The '-p' flag creates all the non-existing ancestors of the target first.
3184 zfs_do_rename(int argc, char **argv)
3187 renameflags_t flags = { 0 };
3191 boolean_t parents = B_FALSE;
3192 char *snapshot = NULL;
3195 while ((c = getopt(argc, argv, "fpru")) != -1) {
3201 flags.recurse = B_TRUE;
3204 flags.nounmount = B_TRUE;
3207 flags.forceunmount = B_TRUE;
3211 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3220 /* check number of arguments */
3222 (void) fprintf(stderr, gettext("missing source dataset "
3227 (void) fprintf(stderr, gettext("missing target dataset "
3232 (void) fprintf(stderr, gettext("too many arguments\n"));
3236 if (flags.recurse && parents) {
3237 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3242 if (flags.recurse && strchr(argv[0], '@') == 0) {
3243 (void) fprintf(stderr, gettext("source dataset for recursive "
3244 "rename must be a snapshot\n"));
3248 if (flags.nounmount && parents) {
3249 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3254 if (flags.nounmount)
3255 types = ZFS_TYPE_FILESYSTEM;
3257 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3259 types = ZFS_TYPE_DATASET;
3261 if (flags.recurse) {
3263 * When we do recursive rename we are fine when the given
3264 * snapshot for the given dataset doesn't exist - it can
3265 * still exists below.
3268 snapshot = strchr(argv[0], '@');
3269 assert(snapshot != NULL);
3274 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3277 /* If we were asked and the name looks good, try to create ancestors. */
3278 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3279 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3284 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3293 * Promotes the given clone fs to be the parent
3297 zfs_do_promote(int argc, char **argv)
3303 if (argc > 1 && argv[1][0] == '-') {
3304 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3309 /* check number of arguments */
3311 (void) fprintf(stderr, gettext("missing clone filesystem"
3316 (void) fprintf(stderr, gettext("too many arguments\n"));
3320 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3324 ret = (zfs_promote(zhp) != 0);
3332 * zfs rollback [-rRf] <snapshot>
3334 * -r Delete any intervening snapshots before doing rollback
3335 * -R Delete any snapshots and their clones
3336 * -f ignored for backwards compatability
3338 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3339 * since then and making it the active dataset. If more recent snapshots exist,
3340 * the command will complain unless the '-r' flag is given.
3342 typedef struct rollback_cbdata {
3348 boolean_t cb_recurse;
3349 } rollback_cbdata_t;
3352 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3354 rollback_cbdata_t *cbp = data;
3356 if (cbp->cb_first && cbp->cb_recurse) {
3357 (void) fprintf(stderr, gettext("cannot rollback to "
3358 "'%s': clones of previous snapshots exist\n"),
3360 (void) fprintf(stderr, gettext("use '-R' to "
3361 "force deletion of the following clones and "
3367 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3373 * Report any snapshots more recent than the one specified. Used when '-r' is
3374 * not specified. We reuse this same callback for the snapshot dependents - if
3375 * 'cb_dependent' is set, then this is a dependent and we should report it
3376 * without checking the transaction group.
3379 rollback_check(zfs_handle_t *zhp, void *data)
3381 rollback_cbdata_t *cbp = data;
3383 if (cbp->cb_doclones) {
3388 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3389 if (cbp->cb_first && !cbp->cb_recurse) {
3390 (void) fprintf(stderr, gettext("cannot "
3391 "rollback to '%s': more recent snapshots "
3392 "or bookmarks exist\n"),
3394 (void) fprintf(stderr, gettext("use '-r' to "
3395 "force deletion of the following "
3396 "snapshots and bookmarks:\n"));
3401 if (cbp->cb_recurse) {
3402 if (zfs_iter_dependents(zhp, B_TRUE,
3403 rollback_check_dependent, cbp) != 0) {
3408 (void) fprintf(stderr, "%s\n",
3417 zfs_do_rollback(int argc, char **argv)
3421 boolean_t force = B_FALSE;
3422 rollback_cbdata_t cb = { 0 };
3423 zfs_handle_t *zhp, *snap;
3424 char parentname[ZFS_MAXNAMELEN];
3428 while ((c = getopt(argc, argv, "rRf")) != -1) {
3441 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3450 /* check number of arguments */
3452 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3456 (void) fprintf(stderr, gettext("too many arguments\n"));
3460 /* open the snapshot */
3461 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3464 /* open the parent dataset */
3465 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3466 verify((delim = strrchr(parentname, '@')) != NULL);
3468 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3474 * Check for more recent snapshots and/or clones based on the presence
3477 cb.cb_target = argv[0];
3478 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3479 cb.cb_first = B_TRUE;
3481 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3483 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3486 if ((ret = cb.cb_error) != 0)
3490 * Rollback parent to the given snapshot.
3492 ret = zfs_rollback(zhp, snap, force);
3505 * zfs set property=value { fs | snap | vol } ...
3507 * Sets the given property for all datasets specified on the command line.
3509 typedef struct set_cbdata {
3515 set_callback(zfs_handle_t *zhp, void *data)
3517 set_cbdata_t *cbp = data;
3519 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3520 switch (libzfs_errno(g_zfs)) {
3521 case EZFS_MOUNTFAILED:
3522 (void) fprintf(stderr, gettext("property may be set "
3523 "but unable to remount filesystem\n"));
3525 case EZFS_SHARENFSFAILED:
3526 (void) fprintf(stderr, gettext("property may be set "
3527 "but unable to reshare filesystem\n"));
3536 zfs_do_set(int argc, char **argv)
3541 /* check for options */
3542 if (argc > 1 && argv[1][0] == '-') {
3543 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3548 /* check number of arguments */
3550 (void) fprintf(stderr, gettext("missing property=value "
3555 (void) fprintf(stderr, gettext("missing dataset name\n"));
3559 /* validate property=value argument */
3560 cb.cb_propname = argv[1];
3561 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3562 (cb.cb_value[1] == '\0')) {
3563 (void) fprintf(stderr, gettext("missing value in "
3564 "property=value argument\n"));
3568 *cb.cb_value = '\0';
3571 if (*cb.cb_propname == '\0') {
3572 (void) fprintf(stderr,
3573 gettext("missing property in property=value argument\n"));
3577 ret = zfs_for_each(argc - 2, argv + 2, 0,
3578 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3583 typedef struct snap_cbdata {
3585 boolean_t sd_recursive;
3586 const char *sd_snapname;
3590 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3592 snap_cbdata_t *sd = arg;
3597 if (sd->sd_recursive &&
3598 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3603 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3606 fnvlist_add_boolean(sd->sd_nvl, name);
3609 if (sd->sd_recursive)
3610 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3616 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3618 * Creates a snapshot with the given name. While functionally equivalent to
3619 * 'zfs create', it is a separate command to differentiate intent.
3622 zfs_do_snapshot(int argc, char **argv)
3627 snap_cbdata_t sd = { 0 };
3628 boolean_t multiple_snaps = B_FALSE;
3630 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3632 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3636 while ((c = getopt(argc, argv, "ro:")) != -1) {
3639 if (parseprop(props))
3643 sd.sd_recursive = B_TRUE;
3644 multiple_snaps = B_TRUE;
3647 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3656 /* check number of arguments */
3658 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3663 multiple_snaps = B_TRUE;
3664 for (; argc > 0; argc--, argv++) {
3668 atp = strchr(argv[0], '@');
3672 sd.sd_snapname = atp + 1;
3673 zhp = zfs_open(g_zfs, argv[0],
3674 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3677 if (zfs_snapshot_cb(zhp, &sd) != 0)
3681 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3682 nvlist_free(sd.sd_nvl);
3684 if (ret != 0 && multiple_snaps)
3685 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3689 nvlist_free(sd.sd_nvl);
3696 * Send a backup stream to stdout.
3699 zfs_do_send(int argc, char **argv)
3701 char *fromname = NULL;
3702 char *toname = NULL;
3705 sendflags_t flags = { 0 };
3707 nvlist_t *dbgnv = NULL;
3708 boolean_t extraverbose = B_FALSE;
3711 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3722 flags.doall = B_TRUE;
3725 flags.replicate = B_TRUE;
3728 flags.props = B_TRUE;
3731 flags.parsable = B_TRUE;
3732 flags.verbose = B_TRUE;
3736 extraverbose = B_TRUE;
3737 flags.verbose = B_TRUE;
3738 flags.progress = B_TRUE;
3741 flags.dedup = B_TRUE;
3744 flags.dryrun = B_TRUE;
3747 (void) fprintf(stderr, gettext("missing argument for "
3748 "'%c' option\n"), optopt);
3752 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3761 /* check number of arguments */
3763 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3767 (void) fprintf(stderr, gettext("too many arguments\n"));
3771 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3772 (void) fprintf(stderr,
3773 gettext("Error: Stream can not be written to a terminal.\n"
3774 "You must redirect standard output.\n"));
3779 * Special case sending a filesystem, or from a bookmark.
3781 if (strchr(argv[0], '@') == NULL ||
3782 (fromname && strchr(fromname, '#') != NULL)) {
3783 char frombuf[ZFS_MAXNAMELEN];
3785 if (flags.replicate || flags.doall || flags.props ||
3786 flags.dedup || flags.dryrun || flags.verbose ||
3788 (void) fprintf(stderr,
3790 "Unsupported flag with filesystem or bookmark.\n"));
3794 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3798 if (fromname != NULL &&
3799 (fromname[0] == '#' || fromname[0] == '@')) {
3801 * Incremental source name begins with # or @.
3802 * Default to same fs as target.
3804 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3805 cp = strchr(frombuf, '@');
3808 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3811 err = zfs_send_one(zhp, fromname, STDOUT_FILENO);
3816 cp = strchr(argv[0], '@');
3819 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3824 * If they specified the full path to the snapshot, chop off
3825 * everything except the short name of the snapshot, but special
3826 * case if they specify the origin.
3828 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3829 char origin[ZFS_MAXNAMELEN];
3832 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3833 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3835 if (strcmp(origin, fromname) == 0) {
3837 flags.fromorigin = B_TRUE;
3840 if (cp != fromname && strcmp(argv[0], fromname)) {
3841 (void) fprintf(stderr,
3842 gettext("incremental source must be "
3843 "in same filesystem\n"));
3847 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3848 (void) fprintf(stderr,
3849 gettext("invalid incremental source\n"));
3855 if (flags.replicate && fromname == NULL)
3856 flags.doall = B_TRUE;
3858 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3859 extraverbose ? &dbgnv : NULL);
3861 if (extraverbose && dbgnv != NULL) {
3863 * dump_nvlist prints to stdout, but that's been
3864 * redirected to a file. Make it print to stderr
3867 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3868 dump_nvlist(dbgnv, 0);
3877 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3879 * Restore a backup stream from stdin.
3882 zfs_do_receive(int argc, char **argv)
3885 recvflags_t flags = { 0 };
3888 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3891 flags.isprefix = B_TRUE;
3894 flags.isprefix = B_TRUE;
3895 flags.istail = B_TRUE;
3898 flags.dryrun = B_TRUE;
3901 flags.nomount = B_TRUE;
3904 flags.verbose = B_TRUE;
3907 flags.force = B_TRUE;
3910 (void) fprintf(stderr, gettext("missing argument for "
3911 "'%c' option\n"), optopt);
3915 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3924 /* check number of arguments */
3926 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3930 (void) fprintf(stderr, gettext("too many arguments\n"));
3934 if (isatty(STDIN_FILENO)) {
3935 (void) fprintf(stderr,
3936 gettext("Error: Backup stream can not be read "
3937 "from a terminal.\n"
3938 "You must redirect standard input.\n"));
3942 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3948 * allow/unallow stuff
3950 /* copied from zfs/sys/dsl_deleg.h */
3951 #define ZFS_DELEG_PERM_CREATE "create"
3952 #define ZFS_DELEG_PERM_DESTROY "destroy"
3953 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3954 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3955 #define ZFS_DELEG_PERM_CLONE "clone"
3956 #define ZFS_DELEG_PERM_PROMOTE "promote"
3957 #define ZFS_DELEG_PERM_RENAME "rename"
3958 #define ZFS_DELEG_PERM_MOUNT "mount"
3959 #define ZFS_DELEG_PERM_SHARE "share"
3960 #define ZFS_DELEG_PERM_SEND "send"
3961 #define ZFS_DELEG_PERM_RECEIVE "receive"
3962 #define ZFS_DELEG_PERM_ALLOW "allow"
3963 #define ZFS_DELEG_PERM_USERPROP "userprop"
3964 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3965 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3966 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3967 #define ZFS_DELEG_PERM_USERUSED "userused"
3968 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3969 #define ZFS_DELEG_PERM_HOLD "hold"
3970 #define ZFS_DELEG_PERM_RELEASE "release"
3971 #define ZFS_DELEG_PERM_DIFF "diff"
3972 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
3974 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3976 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3977 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3978 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3979 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3980 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3981 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3982 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3983 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3984 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3985 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3986 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3987 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3988 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3989 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3990 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3991 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3992 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
3994 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3995 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3996 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3997 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3998 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3999 { NULL, ZFS_DELEG_NOTE_NONE }
4002 /* permission structure */
4003 typedef struct deleg_perm {
4004 zfs_deleg_who_type_t dp_who_type;
4005 const char *dp_name;
4007 boolean_t dp_descend;
4011 typedef struct deleg_perm_node {
4012 deleg_perm_t dpn_perm;
4014 uu_avl_node_t dpn_avl_node;
4015 } deleg_perm_node_t;
4017 typedef struct fs_perm fs_perm_t;
4019 /* permissions set */
4020 typedef struct who_perm {
4021 zfs_deleg_who_type_t who_type;
4022 const char *who_name; /* id */
4023 char who_ug_name[256]; /* user/group name */
4024 fs_perm_t *who_fsperm; /* uplink */
4026 uu_avl_t *who_deleg_perm_avl; /* permissions */
4030 typedef struct who_perm_node {
4031 who_perm_t who_perm;
4032 uu_avl_node_t who_avl_node;
4035 typedef struct fs_perm_set fs_perm_set_t;
4036 /* fs permissions */
4038 const char *fsp_name;
4040 uu_avl_t *fsp_sc_avl; /* sets,create */
4041 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4043 fs_perm_set_t *fsp_set; /* uplink */
4047 typedef struct fs_perm_node {
4048 fs_perm_t fspn_fsperm;
4051 uu_list_node_t fspn_list_node;
4054 /* top level structure */
4055 struct fs_perm_set {
4056 uu_list_pool_t *fsps_list_pool;
4057 uu_list_t *fsps_list; /* list of fs_perms */
4059 uu_avl_pool_t *fsps_named_set_avl_pool;
4060 uu_avl_pool_t *fsps_who_perm_avl_pool;
4061 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4064 static inline const char *
4065 deleg_perm_type(zfs_deleg_note_t note)
4071 case ZFS_DELEG_NOTE_GROUPQUOTA:
4072 case ZFS_DELEG_NOTE_GROUPUSED:
4073 case ZFS_DELEG_NOTE_USERPROP:
4074 case ZFS_DELEG_NOTE_USERQUOTA:
4075 case ZFS_DELEG_NOTE_USERUSED:
4077 return (gettext("other"));
4079 return (gettext("subcommand"));
4084 who_type2weight(zfs_deleg_who_type_t who_type)
4088 case ZFS_DELEG_NAMED_SET_SETS:
4089 case ZFS_DELEG_NAMED_SET:
4092 case ZFS_DELEG_CREATE_SETS:
4093 case ZFS_DELEG_CREATE:
4096 case ZFS_DELEG_USER_SETS:
4097 case ZFS_DELEG_USER:
4100 case ZFS_DELEG_GROUP_SETS:
4101 case ZFS_DELEG_GROUP:
4104 case ZFS_DELEG_EVERYONE_SETS:
4105 case ZFS_DELEG_EVERYONE:
4117 who_perm_compare(const void *larg, const void *rarg, void *unused)
4119 const who_perm_node_t *l = larg;
4120 const who_perm_node_t *r = rarg;
4121 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4122 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4123 int lweight = who_type2weight(ltype);
4124 int rweight = who_type2weight(rtype);
4125 int res = lweight - rweight;
4127 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4128 ZFS_MAX_DELEG_NAME-1);
4140 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4142 const deleg_perm_node_t *l = larg;
4143 const deleg_perm_node_t *r = rarg;
4144 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4145 ZFS_MAX_DELEG_NAME-1);
4157 fs_perm_set_init(fs_perm_set_t *fspset)
4159 bzero(fspset, sizeof (fs_perm_set_t));
4161 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4162 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4163 NULL, UU_DEFAULT)) == NULL)
4165 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4166 UU_DEFAULT)) == NULL)
4169 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4170 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4171 who_perm_node_t, who_avl_node), who_perm_compare,
4172 UU_DEFAULT)) == NULL)
4175 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4176 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4177 who_perm_node_t, who_avl_node), who_perm_compare,
4178 UU_DEFAULT)) == NULL)
4181 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4182 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4183 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4188 static inline void fs_perm_fini(fs_perm_t *);
4189 static inline void who_perm_fini(who_perm_t *);
4192 fs_perm_set_fini(fs_perm_set_t *fspset)
4194 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4196 while (node != NULL) {
4197 fs_perm_node_t *next_node =
4198 uu_list_next(fspset->fsps_list, node);
4199 fs_perm_t *fsperm = &node->fspn_fsperm;
4200 fs_perm_fini(fsperm);
4201 uu_list_remove(fspset->fsps_list, node);
4206 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4207 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4208 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4212 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4215 deleg_perm->dp_who_type = type;
4216 deleg_perm->dp_name = name;
4220 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4221 zfs_deleg_who_type_t type, const char *name)
4223 uu_avl_pool_t *pool;
4224 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4226 bzero(who_perm, sizeof (who_perm_t));
4228 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4229 UU_DEFAULT)) == NULL)
4232 who_perm->who_type = type;
4233 who_perm->who_name = name;
4234 who_perm->who_fsperm = fsperm;
4238 who_perm_fini(who_perm_t *who_perm)
4240 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4242 while (node != NULL) {
4243 deleg_perm_node_t *next_node =
4244 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4246 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4251 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4255 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4257 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4258 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4260 bzero(fsperm, sizeof (fs_perm_t));
4262 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4266 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4270 fsperm->fsp_set = fspset;
4271 fsperm->fsp_name = fsname;
4275 fs_perm_fini(fs_perm_t *fsperm)
4277 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4278 while (node != NULL) {
4279 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4281 who_perm_t *who_perm = &node->who_perm;
4282 who_perm_fini(who_perm);
4283 uu_avl_remove(fsperm->fsp_sc_avl, node);
4288 node = uu_avl_first(fsperm->fsp_uge_avl);
4289 while (node != NULL) {
4290 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4292 who_perm_t *who_perm = &node->who_perm;
4293 who_perm_fini(who_perm);
4294 uu_avl_remove(fsperm->fsp_uge_avl, node);
4299 uu_avl_destroy(fsperm->fsp_sc_avl);
4300 uu_avl_destroy(fsperm->fsp_uge_avl);
4304 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4305 zfs_deleg_who_type_t who_type, const char *name, char locality)
4307 uu_avl_index_t idx = 0;
4309 deleg_perm_node_t *found_node = NULL;
4310 deleg_perm_t *deleg_perm = &node->dpn_perm;
4312 deleg_perm_init(deleg_perm, who_type, name);
4314 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4316 uu_avl_insert(avl, node, idx);
4319 deleg_perm = &node->dpn_perm;
4324 case ZFS_DELEG_LOCAL:
4325 deleg_perm->dp_local = B_TRUE;
4327 case ZFS_DELEG_DESCENDENT:
4328 deleg_perm->dp_descend = B_TRUE;
4333 assert(B_FALSE); /* invalid locality */
4338 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4340 nvpair_t *nvp = NULL;
4341 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4342 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4343 zfs_deleg_who_type_t who_type = who_perm->who_type;
4345 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4346 const char *name = nvpair_name(nvp);
4347 data_type_t type = nvpair_type(nvp);
4348 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4349 deleg_perm_node_t *node =
4350 safe_malloc(sizeof (deleg_perm_node_t));
4352 assert(type == DATA_TYPE_BOOLEAN);
4354 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4355 set_deleg_perm_node(avl, node, who_type, name, locality);
4362 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4364 nvpair_t *nvp = NULL;
4365 fs_perm_set_t *fspset = fsperm->fsp_set;
4367 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4368 nvlist_t *nvl2 = NULL;
4369 const char *name = nvpair_name(nvp);
4370 uu_avl_t *avl = NULL;
4371 uu_avl_pool_t *avl_pool;
4372 zfs_deleg_who_type_t perm_type = name[0];
4373 char perm_locality = name[1];
4374 const char *perm_name = name + 3;
4375 boolean_t is_set = B_TRUE;
4376 who_perm_t *who_perm = NULL;
4378 assert('$' == name[2]);
4380 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4383 switch (perm_type) {
4384 case ZFS_DELEG_CREATE:
4385 case ZFS_DELEG_CREATE_SETS:
4386 case ZFS_DELEG_NAMED_SET:
4387 case ZFS_DELEG_NAMED_SET_SETS:
4388 avl_pool = fspset->fsps_named_set_avl_pool;
4389 avl = fsperm->fsp_sc_avl;
4391 case ZFS_DELEG_USER:
4392 case ZFS_DELEG_USER_SETS:
4393 case ZFS_DELEG_GROUP:
4394 case ZFS_DELEG_GROUP_SETS:
4395 case ZFS_DELEG_EVERYONE:
4396 case ZFS_DELEG_EVERYONE_SETS:
4397 avl_pool = fspset->fsps_who_perm_avl_pool;
4398 avl = fsperm->fsp_uge_avl;
4403 who_perm_node_t *found_node = NULL;
4404 who_perm_node_t *node = safe_malloc(
4405 sizeof (who_perm_node_t));
4406 who_perm = &node->who_perm;
4407 uu_avl_index_t idx = 0;
4409 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4410 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4412 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4414 if (avl == fsperm->fsp_uge_avl) {
4416 struct passwd *p = NULL;
4417 struct group *g = NULL;
4418 const char *nice_name = NULL;
4420 switch (perm_type) {
4421 case ZFS_DELEG_USER_SETS:
4422 case ZFS_DELEG_USER:
4423 rid = atoi(perm_name);
4426 nice_name = p->pw_name;
4428 case ZFS_DELEG_GROUP_SETS:
4429 case ZFS_DELEG_GROUP:
4430 rid = atoi(perm_name);
4433 nice_name = g->gr_name;
4437 if (nice_name != NULL)
4439 node->who_perm.who_ug_name,
4443 uu_avl_insert(avl, node, idx);
4446 who_perm = &node->who_perm;
4450 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4457 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4459 nvpair_t *nvp = NULL;
4460 uu_avl_index_t idx = 0;
4462 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4463 nvlist_t *nvl2 = NULL;
4464 const char *fsname = nvpair_name(nvp);
4465 data_type_t type = nvpair_type(nvp);
4466 fs_perm_t *fsperm = NULL;
4467 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4471 fsperm = &node->fspn_fsperm;
4473 assert(DATA_TYPE_NVLIST == type);
4475 uu_list_node_init(node, &node->fspn_list_node,
4476 fspset->fsps_list_pool);
4478 idx = uu_list_numnodes(fspset->fsps_list);
4479 fs_perm_init(fsperm, fspset, fsname);
4481 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4484 (void) parse_fs_perm(fsperm, nvl2);
4486 uu_list_insert(fspset->fsps_list, node, idx);
4492 static inline const char *
4493 deleg_perm_comment(zfs_deleg_note_t note)
4495 const char *str = "";
4500 case ZFS_DELEG_NOTE_ALLOW:
4501 str = gettext("Must also have the permission that is being"
4502 "\n\t\t\t\tallowed");
4504 case ZFS_DELEG_NOTE_CLONE:
4505 str = gettext("Must also have the 'create' ability and 'mount'"
4506 "\n\t\t\t\tability in the origin file system");
4508 case ZFS_DELEG_NOTE_CREATE:
4509 str = gettext("Must also have the 'mount' ability");
4511 case ZFS_DELEG_NOTE_DESTROY:
4512 str = gettext("Must also have the 'mount' ability");
4514 case ZFS_DELEG_NOTE_DIFF:
4515 str = gettext("Allows lookup of paths within a dataset;"
4516 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4517 "\n\t\t\t\tin order to use zfs diff");
4519 case ZFS_DELEG_NOTE_HOLD:
4520 str = gettext("Allows adding a user hold to a snapshot");
4522 case ZFS_DELEG_NOTE_MOUNT:
4523 str = gettext("Allows mount/umount of ZFS datasets");
4525 case ZFS_DELEG_NOTE_PROMOTE:
4526 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4527 " 'promote' ability in the origin file system");
4529 case ZFS_DELEG_NOTE_RECEIVE:
4530 str = gettext("Must also have the 'mount' and 'create'"
4533 case ZFS_DELEG_NOTE_RELEASE:
4534 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4535 "might destroy the snapshot");
4537 case ZFS_DELEG_NOTE_RENAME:
4538 str = gettext("Must also have the 'mount' and 'create'"
4539 "\n\t\t\t\tability in the new parent");
4541 case ZFS_DELEG_NOTE_ROLLBACK:
4544 case ZFS_DELEG_NOTE_SEND:
4547 case ZFS_DELEG_NOTE_SHARE:
4548 str = gettext("Allows sharing file systems over NFS or SMB"
4549 "\n\t\t\t\tprotocols");
4551 case ZFS_DELEG_NOTE_SNAPSHOT:
4555 * case ZFS_DELEG_NOTE_VSCAN:
4556 * str = gettext("");
4560 case ZFS_DELEG_NOTE_GROUPQUOTA:
4561 str = gettext("Allows accessing any groupquota@... property");
4563 case ZFS_DELEG_NOTE_GROUPUSED:
4564 str = gettext("Allows reading any groupused@... property");
4566 case ZFS_DELEG_NOTE_USERPROP:
4567 str = gettext("Allows changing any user property");
4569 case ZFS_DELEG_NOTE_USERQUOTA:
4570 str = gettext("Allows accessing any userquota@... property");
4572 case ZFS_DELEG_NOTE_USERUSED:
4573 str = gettext("Allows reading any userused@... property");
4591 boolean_t recursive; /* unallow only */
4592 boolean_t prt_usage;
4594 boolean_t prt_perms;
4597 const char *dataset;
4601 prop_cmp(const void *a, const void *b)
4603 const char *str1 = *(const char **)a;
4604 const char *str2 = *(const char **)b;
4605 return (strcmp(str1, str2));
4609 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4611 const char *opt_desc[] = {
4612 "-h", gettext("show this help message and exit"),
4613 "-l", gettext("set permission locally"),
4614 "-d", gettext("set permission for descents"),
4615 "-u", gettext("set permission for user"),
4616 "-g", gettext("set permission for group"),
4617 "-e", gettext("set permission for everyone"),
4618 "-c", gettext("set create time permission"),
4619 "-s", gettext("define permission set"),
4621 "-r", gettext("remove permissions recursively"),
4623 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4624 size_t allow_size = unallow_size - 2;
4625 const char *props[ZFS_NUM_PROPS];
4628 FILE *fp = requested ? stdout : stderr;
4629 zprop_desc_t *pdtbl = zfs_prop_get_table();
4630 const char *fmt = gettext("%-16s %-14s\t%s\n");
4632 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4634 (void) fprintf(fp, gettext("Options:\n"));
4635 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4636 const char *opt = opt_desc[i++];
4637 const char *optdsc = opt_desc[i];
4638 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4641 (void) fprintf(fp, gettext("\nThe following permissions are "
4643 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4645 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4646 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4647 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4648 const char *perm_type = deleg_perm_type(perm_note);
4649 const char *perm_comment = deleg_perm_comment(perm_note);
4650 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4653 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4654 zprop_desc_t *pd = &pdtbl[i];
4655 if (pd->pd_visible != B_TRUE)
4658 if (pd->pd_attr == PROP_READONLY)
4661 props[count++] = pd->pd_name;
4663 props[count] = NULL;
4665 qsort(props, count, sizeof (char *), prop_cmp);
4667 for (i = 0; i < count; i++)
4668 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4671 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4673 exit(requested ? 0 : 2);
4676 static inline const char *
4677 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4680 if (un && argc == expected_argc - 1)
4682 else if (argc == expected_argc)
4683 *permsp = argv[argc - 2];
4685 allow_usage(un, B_FALSE,
4686 gettext("wrong number of parameters\n"));
4688 return (argv[argc - 1]);
4692 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4694 int uge_sum = opts->user + opts->group + opts->everyone;
4695 int csuge_sum = opts->create + opts->set + uge_sum;
4696 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4697 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4700 allow_usage(un, B_FALSE,
4701 gettext("-u, -g, and -e are mutually exclusive\n"));
4703 if (opts->prt_usage)
4704 if (argc == 0 && all_sum == 0)
4705 allow_usage(un, B_TRUE, NULL);
4711 allow_usage(un, B_FALSE,
4712 gettext("invalid options combined with -s\n"));
4714 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4715 if (argv[0][0] != '@')
4716 allow_usage(un, B_FALSE,
4717 gettext("invalid set name: missing '@' prefix\n"));
4718 opts->who = argv[0];
4719 } else if (opts->create) {
4720 if (ldcsuge_sum > 1)
4721 allow_usage(un, B_FALSE,
4722 gettext("invalid options combined with -c\n"));
4723 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4724 } else if (opts->everyone) {
4726 allow_usage(un, B_FALSE,
4727 gettext("invalid options combined with -e\n"));
4728 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4729 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4731 opts->everyone = B_TRUE;
4734 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4735 } else if (argc == 1 && !un) {
4736 opts->prt_perms = B_TRUE;
4737 opts->dataset = argv[argc-1];
4739 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4740 opts->who = argv[0];
4743 if (!opts->local && !opts->descend) {
4744 opts->local = B_TRUE;
4745 opts->descend = B_TRUE;
4750 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4751 const char *who, char *perms, nvlist_t *top_nvl)
4754 char ld[2] = { '\0', '\0' };
4755 char who_buf[ZFS_MAXNAMELEN+32];
4758 nvlist_t *base_nvl = NULL;
4759 nvlist_t *set_nvl = NULL;
4762 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4764 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4768 case ZFS_DELEG_NAMED_SET_SETS:
4769 case ZFS_DELEG_NAMED_SET:
4770 set_type = ZFS_DELEG_NAMED_SET_SETS;
4771 base_type = ZFS_DELEG_NAMED_SET;
4772 ld[0] = ZFS_DELEG_NA;
4774 case ZFS_DELEG_CREATE_SETS:
4775 case ZFS_DELEG_CREATE:
4776 set_type = ZFS_DELEG_CREATE_SETS;
4777 base_type = ZFS_DELEG_CREATE;
4778 ld[0] = ZFS_DELEG_NA;
4780 case ZFS_DELEG_USER_SETS:
4781 case ZFS_DELEG_USER:
4782 set_type = ZFS_DELEG_USER_SETS;
4783 base_type = ZFS_DELEG_USER;
4785 ld[0] = ZFS_DELEG_LOCAL;
4787 ld[1] = ZFS_DELEG_DESCENDENT;
4789 case ZFS_DELEG_GROUP_SETS:
4790 case ZFS_DELEG_GROUP:
4791 set_type = ZFS_DELEG_GROUP_SETS;
4792 base_type = ZFS_DELEG_GROUP;
4794 ld[0] = ZFS_DELEG_LOCAL;
4796 ld[1] = ZFS_DELEG_DESCENDENT;
4798 case ZFS_DELEG_EVERYONE_SETS:
4799 case ZFS_DELEG_EVERYONE:
4800 set_type = ZFS_DELEG_EVERYONE_SETS;
4801 base_type = ZFS_DELEG_EVERYONE;
4803 ld[0] = ZFS_DELEG_LOCAL;
4805 ld[1] = ZFS_DELEG_DESCENDENT;
4808 if (perms != NULL) {
4810 char *end = curr + strlen(perms);
4812 while (curr < end) {
4813 char *delim = strchr(curr, ',');
4824 (void) nvlist_add_boolean(nvl, curr);
4830 for (i = 0; i < 2; i++) {
4831 char locality = ld[i];
4835 if (!nvlist_empty(base_nvl)) {
4837 (void) snprintf(who_buf,
4838 sizeof (who_buf), "%c%c$%s",
4839 base_type, locality, who);
4841 (void) snprintf(who_buf,
4842 sizeof (who_buf), "%c%c$",
4843 base_type, locality);
4845 (void) nvlist_add_nvlist(top_nvl, who_buf,
4850 if (!nvlist_empty(set_nvl)) {
4852 (void) snprintf(who_buf,
4853 sizeof (who_buf), "%c%c$%s",
4854 set_type, locality, who);
4856 (void) snprintf(who_buf,
4857 sizeof (who_buf), "%c%c$",
4858 set_type, locality);
4860 (void) nvlist_add_nvlist(top_nvl, who_buf,
4865 for (i = 0; i < 2; i++) {
4866 char locality = ld[i];
4871 (void) snprintf(who_buf, sizeof (who_buf),
4872 "%c%c$%s", base_type, locality, who);
4874 (void) snprintf(who_buf, sizeof (who_buf),
4875 "%c%c$", base_type, locality);
4876 (void) nvlist_add_boolean(top_nvl, who_buf);
4879 (void) snprintf(who_buf, sizeof (who_buf),
4880 "%c%c$%s", set_type, locality, who);
4882 (void) snprintf(who_buf, sizeof (who_buf),
4883 "%c%c$", set_type, locality);
4884 (void) nvlist_add_boolean(top_nvl, who_buf);
4890 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4892 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4896 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4897 opts->descend, opts->who, opts->perms, *nvlp);
4898 } else if (opts->create) {
4899 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4900 opts->descend, NULL, opts->perms, *nvlp);
4901 } else if (opts->everyone) {
4902 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4903 opts->descend, NULL, opts->perms, *nvlp);
4905 char *curr = opts->who;
4906 char *end = curr + strlen(curr);
4908 while (curr < end) {
4910 zfs_deleg_who_type_t who_type;
4912 char *delim = strchr(curr, ',');
4915 struct passwd *p = NULL;
4916 struct group *g = NULL;
4924 rid = (uid_t)strtol(curr, &endch, 0);
4926 who_type = ZFS_DELEG_USER;
4935 (void) snprintf(errbuf, 256, gettext(
4936 "invalid user %s"), curr);
4937 allow_usage(un, B_TRUE, errbuf);
4939 } else if (opts->group) {
4940 who_type = ZFS_DELEG_GROUP;
4949 (void) snprintf(errbuf, 256, gettext(
4950 "invalid group %s"), curr);
4951 allow_usage(un, B_TRUE, errbuf);
4954 if (*endch != '\0') {
4961 if (*endch != '\0') {
4968 who_type = ZFS_DELEG_USER;
4970 } else if (g != NULL) {
4971 who_type = ZFS_DELEG_GROUP;
4974 (void) snprintf(errbuf, 256, gettext(
4975 "invalid user/group %s"), curr);
4976 allow_usage(un, B_TRUE, errbuf);
4980 (void) sprintf(id, "%u", rid);
4983 store_allow_perm(who_type, opts->local,
4984 opts->descend, who, opts->perms, *nvlp);
4993 print_set_creat_perms(uu_avl_t *who_avl)
4995 const char *sc_title[] = {
4996 gettext("Permission sets:\n"),
4997 gettext("Create time permissions:\n"),
5000 const char **title_ptr = sc_title;
5001 who_perm_node_t *who_node = NULL;
5002 int prev_weight = -1;
5004 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5005 who_node = uu_avl_next(who_avl, who_node)) {
5006 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5007 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5008 const char *who_name = who_node->who_perm.who_name;
5009 int weight = who_type2weight(who_type);
5010 boolean_t first = B_TRUE;
5011 deleg_perm_node_t *deleg_node;
5013 if (prev_weight != weight) {
5014 (void) printf(*title_ptr++);
5015 prev_weight = weight;
5018 if (who_name == NULL || strnlen(who_name, 1) == 0)
5019 (void) printf("\t");
5021 (void) printf("\t%s ", who_name);
5023 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5024 deleg_node = uu_avl_next(avl, deleg_node)) {
5027 deleg_node->dpn_perm.dp_name);
5030 (void) printf(",%s",
5031 deleg_node->dpn_perm.dp_name);
5034 (void) printf("\n");
5039 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5042 who_perm_node_t *who_node = NULL;
5043 boolean_t prt_title = B_TRUE;
5044 uu_avl_walk_t *walk;
5046 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5049 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5050 const char *who_name = who_node->who_perm.who_name;
5051 const char *nice_who_name = who_node->who_perm.who_ug_name;
5052 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5053 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5055 deleg_perm_node_t *deleg_node;
5056 boolean_t prt_who = B_TRUE;
5058 for (deleg_node = uu_avl_first(avl);
5060 deleg_node = uu_avl_next(avl, deleg_node)) {
5061 if (local != deleg_node->dpn_perm.dp_local ||
5062 descend != deleg_node->dpn_perm.dp_descend)
5066 const char *who = NULL;
5068 prt_title = B_FALSE;
5069 (void) printf(title);
5073 case ZFS_DELEG_USER_SETS:
5074 case ZFS_DELEG_USER:
5075 who = gettext("user");
5077 who_name = nice_who_name;
5079 case ZFS_DELEG_GROUP_SETS:
5080 case ZFS_DELEG_GROUP:
5081 who = gettext("group");
5083 who_name = nice_who_name;
5085 case ZFS_DELEG_EVERYONE_SETS:
5086 case ZFS_DELEG_EVERYONE:
5087 who = gettext("everyone");
5092 if (who_name == NULL)
5093 (void) printf("\t%s", who);
5095 (void) printf("\t%s %s", who, who_name);
5098 (void) printf("%c%s", delim,
5099 deleg_node->dpn_perm.dp_name);
5104 (void) printf("\n");
5107 uu_avl_walk_end(walk);
5111 print_fs_perms(fs_perm_set_t *fspset)
5113 fs_perm_node_t *node = NULL;
5114 char buf[ZFS_MAXNAMELEN+32];
5115 const char *dsname = buf;
5117 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5118 node = uu_list_next(fspset->fsps_list, node)) {
5119 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5120 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5123 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5124 gettext("---- Permissions on %s "),
5125 node->fspn_fsperm.fsp_name);
5126 (void) printf(dsname);
5127 left = 70 - strlen(buf);
5130 (void) printf("\n");
5132 print_set_creat_perms(sc_avl);
5133 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5134 gettext("Local permissions:\n"));
5135 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5136 gettext("Descendent permissions:\n"));
5137 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5138 gettext("Local+Descendent permissions:\n"));
5142 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5144 struct deleg_perms {
5150 set_deleg_perms(zfs_handle_t *zhp, void *data)
5152 struct deleg_perms *perms = (struct deleg_perms *)data;
5153 zfs_type_t zfs_type = zfs_get_type(zhp);
5155 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5158 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5162 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5165 nvlist_t *perm_nvl = NULL;
5166 nvlist_t *update_perm_nvl = NULL;
5169 struct allow_opts opts = { 0 };
5171 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5174 while ((c = getopt(argc, argv, optstr)) != -1) {
5177 opts.local = B_TRUE;
5180 opts.descend = B_TRUE;
5186 opts.group = B_TRUE;
5189 opts.everyone = B_TRUE;
5195 opts.create = B_TRUE;
5198 opts.recursive = B_TRUE;
5201 (void) fprintf(stderr, gettext("missing argument for "
5202 "'%c' option\n"), optopt);
5206 opts.prt_usage = B_TRUE;
5209 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5218 /* check arguments */
5219 parse_allow_args(argc, argv, un, &opts);
5221 /* try to open the dataset */
5222 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5223 ZFS_TYPE_VOLUME)) == NULL) {
5224 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5229 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5232 fs_perm_set_init(&fs_perm_set);
5233 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5234 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5239 print_fs_perms(&fs_perm_set);
5241 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5242 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5245 if (un && opts.recursive) {
5246 struct deleg_perms data = { un, update_perm_nvl };
5247 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5256 nvlist_free(perm_nvl);
5257 if (update_perm_nvl != NULL)
5258 nvlist_free(update_perm_nvl);
5260 fs_perm_set_fini(&fs_perm_set);
5268 zfs_do_allow(int argc, char **argv)
5270 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5274 zfs_do_unallow(int argc, char **argv)
5276 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5280 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5285 boolean_t recursive = B_FALSE;
5286 const char *opts = holding ? "rt" : "r";
5290 while ((c = getopt(argc, argv, opts)) != -1) {
5296 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5305 /* check number of arguments */
5313 if (holding && tag[0] == '.') {
5314 /* tags starting with '.' are reserved for libzfs */
5315 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5319 for (i = 0; i < argc; ++i) {
5321 char parent[ZFS_MAXNAMELEN];
5323 char *path = argv[i];
5325 delim = strchr(path, '@');
5326 if (delim == NULL) {
5327 (void) fprintf(stderr,
5328 gettext("'%s' is not a snapshot\n"), path);
5332 (void) strncpy(parent, path, delim - path);
5333 parent[delim - path] = '\0';
5335 zhp = zfs_open(g_zfs, parent,
5336 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5342 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5345 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5351 return (errors != 0);
5355 * zfs hold [-r] [-t] <tag> <snap> ...
5357 * -r Recursively hold
5359 * Apply a user-hold with the given tag to the list of snapshots.
5362 zfs_do_hold(int argc, char **argv)
5364 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5368 * zfs release [-r] <tag> <snap> ...
5370 * -r Recursively release
5372 * Release a user-hold with the given tag from the list of snapshots.
5375 zfs_do_release(int argc, char **argv)
5377 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5380 typedef struct holds_cbdata {
5381 boolean_t cb_recursive;
5382 const char *cb_snapname;
5384 size_t cb_max_namelen;
5385 size_t cb_max_taglen;
5388 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5389 #define DATETIME_BUF_LEN (32)
5394 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5397 nvpair_t *nvp = NULL;
5398 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5402 for (i = 0; i < 3; i++) {
5403 col = gettext(hdr_cols[i]);
5405 (void) printf("%-*s ", i ? tagwidth : nwidth,
5408 (void) printf("%s\n", col);
5412 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5413 char *zname = nvpair_name(nvp);
5415 nvpair_t *nvp2 = NULL;
5416 (void) nvpair_value_nvlist(nvp, &nvl2);
5417 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5418 char tsbuf[DATETIME_BUF_LEN];
5419 char *tagname = nvpair_name(nvp2);
5423 char sep = scripted ? '\t' : ' ';
5424 size_t sepnum = scripted ? 1 : 2;
5426 (void) nvpair_value_uint64(nvp2, &val);
5428 (void) localtime_r(&time, &t);
5429 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5430 gettext(STRFTIME_FMT_STR), &t);
5432 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5433 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5439 * Generic callback function to list a dataset or snapshot.
5442 holds_callback(zfs_handle_t *zhp, void *data)
5444 holds_cbdata_t *cbp = data;
5445 nvlist_t *top_nvl = *cbp->cb_nvlp;
5446 nvlist_t *nvl = NULL;
5447 nvpair_t *nvp = NULL;
5448 const char *zname = zfs_get_name(zhp);
5449 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5451 if (cbp->cb_recursive) {
5452 const char *snapname;
5453 char *delim = strchr(zname, '@');
5457 snapname = delim + 1;
5458 if (strcmp(cbp->cb_snapname, snapname))
5462 if (zfs_get_holds(zhp, &nvl) != 0)
5465 if (znamelen > cbp->cb_max_namelen)
5466 cbp->cb_max_namelen = znamelen;
5468 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5469 const char *tag = nvpair_name(nvp);
5470 size_t taglen = strnlen(tag, MAXNAMELEN);
5471 if (taglen > cbp->cb_max_taglen)
5472 cbp->cb_max_taglen = taglen;
5475 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5479 * zfs holds [-r] <snap> ...
5481 * -r Recursively hold
5484 zfs_do_holds(int argc, char **argv)
5489 boolean_t scripted = B_FALSE;
5490 boolean_t recursive = B_FALSE;
5491 const char *opts = "rH";
5494 int types = ZFS_TYPE_SNAPSHOT;
5495 holds_cbdata_t cb = { 0 };
5502 while ((c = getopt(argc, argv, opts)) != -1) {
5511 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5518 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5519 flags |= ZFS_ITER_RECURSE;
5525 /* check number of arguments */
5529 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5532 for (i = 0; i < argc; ++i) {
5533 char *snapshot = argv[i];
5535 const char *snapname;
5537 delim = strchr(snapshot, '@');
5538 if (delim == NULL) {
5539 (void) fprintf(stderr,
5540 gettext("'%s' is not a snapshot\n"), snapshot);
5544 snapname = delim + 1;
5546 snapshot[delim - snapshot] = '\0';
5548 cb.cb_recursive = recursive;
5549 cb.cb_snapname = snapname;
5553 * 1. collect holds data, set format options
5555 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5556 holds_callback, &cb);
5562 * 2. print holds data
5564 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5566 if (nvlist_empty(nvl))
5567 (void) printf(gettext("no datasets available\n"));
5571 return (0 != errors);
5574 #define CHECK_SPINNER 30
5575 #define SPINNER_TIME 3 /* seconds */
5576 #define MOUNT_TIME 5 /* seconds */
5579 get_one_dataset(zfs_handle_t *zhp, void *data)
5581 static char *spin[] = { "-", "\\", "|", "/" };
5582 static int spinval = 0;
5583 static int spincheck = 0;
5584 static time_t last_spin_time = (time_t)0;
5585 get_all_cb_t *cbp = data;
5586 zfs_type_t type = zfs_get_type(zhp);
5588 if (cbp->cb_verbose) {
5589 if (--spincheck < 0) {
5590 time_t now = time(NULL);
5591 if (last_spin_time + SPINNER_TIME < now) {
5592 update_progress(spin[spinval++ % 4]);
5593 last_spin_time = now;
5595 spincheck = CHECK_SPINNER;
5600 * Interate over any nested datasets.
5602 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5608 * Skip any datasets whose type does not match.
5610 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5614 libzfs_add_handle(cbp, zhp);
5615 assert(cbp->cb_used <= cbp->cb_alloc);
5621 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5623 get_all_cb_t cb = { 0 };
5624 cb.cb_verbose = verbose;
5625 cb.cb_getone = get_one_dataset;
5628 set_progress_header(gettext("Reading ZFS config"));
5629 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5631 *dslist = cb.cb_handles;
5632 *count = cb.cb_used;
5635 finish_progress(gettext("done."));
5639 * Generic callback for sharing or mounting filesystems. Because the code is so
5640 * similar, we have a common function with an extra parameter to determine which
5641 * mode we are using.
5643 #define OP_SHARE 0x1
5644 #define OP_MOUNT 0x2
5647 * Share or mount a dataset.
5650 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5651 boolean_t explicit, const char *options)
5653 char mountpoint[ZFS_MAXPROPLEN];
5654 char shareopts[ZFS_MAXPROPLEN];
5655 char smbshareopts[ZFS_MAXPROPLEN];
5656 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5658 uint64_t zoned, canmount;
5659 boolean_t shared_nfs, shared_smb;
5661 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5664 * Check to make sure we can mount/share this dataset. If we
5665 * are in the global zone and the filesystem is exported to a
5666 * local zone, or if we are in a local zone and the
5667 * filesystem is not exported, then it is an error.
5669 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5671 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5675 (void) fprintf(stderr, gettext("cannot %s '%s': "
5676 "dataset is exported to a local zone\n"), cmdname,
5680 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5684 (void) fprintf(stderr, gettext("cannot %s '%s': "
5685 "permission denied\n"), cmdname,
5691 * Ignore any filesystems which don't apply to us. This
5692 * includes those with a legacy mountpoint, or those with
5693 * legacy share options.
5695 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5696 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5697 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5698 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5699 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5700 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5702 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5703 strcmp(smbshareopts, "off") == 0) {
5707 (void) fprintf(stderr, gettext("cannot share '%s': "
5708 "legacy share\n"), zfs_get_name(zhp));
5709 (void) fprintf(stderr, gettext("to "
5710 "share this filesystem set "
5711 "sharenfs property on\n"));
5716 * We cannot share or mount legacy filesystems. If the
5717 * shareopts is non-legacy but the mountpoint is legacy, we
5718 * treat it as a legacy share.
5720 if (strcmp(mountpoint, "legacy") == 0) {
5724 (void) fprintf(stderr, gettext("cannot %s '%s': "
5725 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5726 (void) fprintf(stderr, gettext("use %s(8) to "
5727 "%s this filesystem\n"), cmdname, cmdname);
5731 if (strcmp(mountpoint, "none") == 0) {
5735 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5736 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5741 * canmount explicit outcome
5742 * on no pass through
5743 * on yes pass through
5745 * off yes display error, return 1
5746 * noauto no return 0
5747 * noauto yes pass through
5749 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5750 if (canmount == ZFS_CANMOUNT_OFF) {
5754 (void) fprintf(stderr, gettext("cannot %s '%s': "
5755 "'canmount' property is set to 'off'\n"), cmdname,
5758 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5763 * At this point, we have verified that the mountpoint and/or
5764 * shareopts are appropriate for auto management. If the
5765 * filesystem is already mounted or shared, return (failing
5766 * for explicit requests); otherwise mount or share the
5772 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5773 shared_smb = zfs_is_shared_smb(zhp, NULL);
5775 if (shared_nfs && shared_smb ||
5776 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5777 strcmp(smbshareopts, "off") == 0) ||
5778 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5779 strcmp(shareopts, "off") == 0)) {
5783 (void) fprintf(stderr, gettext("cannot share "
5784 "'%s': filesystem already shared\n"),
5789 if (!zfs_is_mounted(zhp, NULL) &&
5790 zfs_mount(zhp, NULL, 0) != 0)
5793 if (protocol == NULL) {
5794 if (zfs_shareall(zhp) != 0)
5796 } else if (strcmp(protocol, "nfs") == 0) {
5797 if (zfs_share_nfs(zhp))
5799 } else if (strcmp(protocol, "smb") == 0) {
5800 if (zfs_share_smb(zhp))
5803 (void) fprintf(stderr, gettext("cannot share "
5804 "'%s': invalid share type '%s' "
5806 zfs_get_name(zhp), protocol);
5813 if (options == NULL)
5814 mnt.mnt_mntopts = "";
5816 mnt.mnt_mntopts = (char *)options;
5818 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5819 zfs_is_mounted(zhp, NULL)) {
5823 (void) fprintf(stderr, gettext("cannot mount "
5824 "'%s': filesystem already mounted\n"),
5829 if (zfs_mount(zhp, options, flags) != 0)
5838 * Reports progress in the form "(current/total)". Not thread-safe.
5841 report_mount_progress(int current, int total)
5843 static time_t last_progress_time = 0;
5844 time_t now = time(NULL);
5847 /* report 1..n instead of 0..n-1 */
5850 /* display header if we're here for the first time */
5852 set_progress_header(gettext("Mounting ZFS filesystems"));
5853 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5854 /* too soon to report again */
5858 last_progress_time = now;
5860 (void) sprintf(info, "(%d/%d)", current, total);
5862 if (current == total)
5863 finish_progress(info);
5865 update_progress(info);
5869 append_options(char *mntopts, char *newopts)
5871 int len = strlen(mntopts);
5873 /* original length plus new string to append plus 1 for the comma */
5874 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5875 (void) fprintf(stderr, gettext("the opts argument for "
5876 "'%c' option is too long (more than %d chars)\n"),
5877 "-o", MNT_LINE_MAX);
5882 mntopts[len++] = ',';
5884 (void) strcpy(&mntopts[len], newopts);
5888 share_mount(int op, int argc, char **argv)
5891 boolean_t verbose = B_FALSE;
5893 char *options = NULL;
5897 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5907 if (*optarg == '\0') {
5908 (void) fprintf(stderr, gettext("empty mount "
5909 "options (-o) specified\n"));
5913 if (options == NULL)
5914 options = safe_malloc(MNT_LINE_MAX + 1);
5916 /* option validation is done later */
5917 append_options(options, optarg);
5921 warnx("no overlay mounts support on FreeBSD, ignoring");
5924 (void) fprintf(stderr, gettext("missing argument for "
5925 "'%c' option\n"), optopt);
5929 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5938 /* check number of arguments */
5940 zfs_handle_t **dslist = NULL;
5941 size_t i, count = 0;
5942 char *protocol = NULL;
5944 if (op == OP_SHARE && argc > 0) {
5945 if (strcmp(argv[0], "nfs") != 0 &&
5946 strcmp(argv[0], "smb") != 0) {
5947 (void) fprintf(stderr, gettext("share type "
5948 "must be 'nfs' or 'smb'\n"));
5957 (void) fprintf(stderr, gettext("too many arguments\n"));
5961 start_progress_timer();
5962 get_all_datasets(&dslist, &count, verbose);
5967 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5969 for (i = 0; i < count; i++) {
5971 report_mount_progress(i, count);
5973 if (share_mount_one(dslist[i], op, flags, protocol,
5974 B_FALSE, options) != 0)
5976 zfs_close(dslist[i]);
5980 } else if (argc == 0) {
5981 struct mnttab entry;
5983 if ((op == OP_SHARE) || (options != NULL)) {
5984 (void) fprintf(stderr, gettext("missing filesystem "
5985 "argument (specify -a for all)\n"));
5990 * When mount is given no arguments, go through /etc/mnttab and
5991 * display any active ZFS mounts. We hide any snapshots, since
5992 * they are controlled automatically.
5994 rewind(mnttab_file);
5995 while (getmntent(mnttab_file, &entry) == 0) {
5996 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5997 strchr(entry.mnt_special, '@') != NULL)
6000 (void) printf("%-30s %s\n", entry.mnt_special,
6008 (void) fprintf(stderr,
6009 gettext("too many arguments\n"));
6013 if ((zhp = zfs_open(g_zfs, argv[0],
6014 ZFS_TYPE_FILESYSTEM)) == NULL) {
6017 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6027 * zfs mount -a [nfs]
6028 * zfs mount filesystem
6030 * Mount all filesystems, or mount the given filesystem.
6033 zfs_do_mount(int argc, char **argv)
6035 return (share_mount(OP_MOUNT, argc, argv));
6039 * zfs share -a [nfs | smb]
6040 * zfs share filesystem
6042 * Share all filesystems, or share the given filesystem.
6045 zfs_do_share(int argc, char **argv)
6047 return (share_mount(OP_SHARE, argc, argv));
6050 typedef struct unshare_unmount_node {
6051 zfs_handle_t *un_zhp;
6053 uu_avl_node_t un_avlnode;
6054 } unshare_unmount_node_t;
6058 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6060 const unshare_unmount_node_t *l = larg;
6061 const unshare_unmount_node_t *r = rarg;
6063 return (strcmp(l->un_mountp, r->un_mountp));
6067 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6068 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6069 * and unmount it appropriately.
6072 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6076 struct stat64 statbuf;
6077 struct extmnttab entry;
6078 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6082 * Search for the path in /etc/mnttab. Rather than looking for the
6083 * specific path, which can be fooled by non-standard paths (i.e. ".."
6084 * or "//"), we stat() the path and search for the corresponding
6085 * (major,minor) device pair.
6087 if (stat64(path, &statbuf) != 0) {
6088 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6089 cmdname, path, strerror(errno));
6092 path_inode = statbuf.st_ino;
6095 * Search for the given (major,minor) pair in the mount table.
6098 rewind(mnttab_file);
6099 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6100 if (entry.mnt_major == major(statbuf.st_dev) &&
6101 entry.mnt_minor == minor(statbuf.st_dev))
6108 if (statfs(path, &sfs) != 0) {
6109 (void) fprintf(stderr, "%s: %s\n", path,
6113 statfs2mnttab(&sfs, &entry);
6117 if (op == OP_SHARE) {
6118 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6119 "currently mounted\n"), cmdname, path);
6122 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6124 if ((ret = umount2(path, flags)) != 0)
6125 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6130 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6131 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6132 "filesystem\n"), cmdname, path);
6136 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6137 ZFS_TYPE_FILESYSTEM)) == NULL)
6141 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6142 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6143 cmdname, path, strerror(errno));
6145 } else if (statbuf.st_ino != path_inode) {
6146 (void) fprintf(stderr, gettext("cannot "
6147 "%s '%s': not a mountpoint\n"), cmdname, path);
6151 if (op == OP_SHARE) {
6152 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6153 char smbshare_prop[ZFS_MAXPROPLEN];
6155 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6156 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6157 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6158 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6160 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6161 strcmp(smbshare_prop, "off") == 0) {
6162 (void) fprintf(stderr, gettext("cannot unshare "
6163 "'%s': legacy share\n"), path);
6165 (void) fprintf(stderr, gettext("use "
6166 "unshare(1M) to unshare this filesystem\n"));
6168 } else if (!zfs_is_shared(zhp)) {
6169 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6170 "not currently shared\n"), path);
6172 ret = zfs_unshareall_bypath(zhp, path);
6175 char mtpt_prop[ZFS_MAXPROPLEN];
6177 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6178 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6181 ret = zfs_unmount(zhp, NULL, flags);
6182 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6183 (void) fprintf(stderr, gettext("cannot unmount "
6184 "'%s': legacy mountpoint\n"),
6186 (void) fprintf(stderr, gettext("use umount(8) "
6187 "to unmount this filesystem\n"));
6189 ret = zfs_unmountall(zhp, flags);
6200 * Generic callback for unsharing or unmounting a filesystem.
6203 unshare_unmount(int op, int argc, char **argv)
6210 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6211 char sharesmb[ZFS_MAXPROPLEN];
6214 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6223 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6234 * We could make use of zfs_for_each() to walk all datasets in
6235 * the system, but this would be very inefficient, especially
6236 * since we would have to linearly search /etc/mnttab for each
6237 * one. Instead, do one pass through /etc/mnttab looking for
6238 * zfs entries and call zfs_unmount() for each one.
6240 * Things get a little tricky if the administrator has created
6241 * mountpoints beneath other ZFS filesystems. In this case, we
6242 * have to unmount the deepest filesystems first. To accomplish
6243 * this, we place all the mountpoints in an AVL tree sorted by
6244 * the special type (dataset name), and walk the result in
6245 * reverse to make sure to get any snapshots first.
6247 struct mnttab entry;
6248 uu_avl_pool_t *pool;
6250 unshare_unmount_node_t *node;
6252 uu_avl_walk_t *walk;
6255 (void) fprintf(stderr, gettext("too many arguments\n"));
6259 if (((pool = uu_avl_pool_create("unmount_pool",
6260 sizeof (unshare_unmount_node_t),
6261 offsetof(unshare_unmount_node_t, un_avlnode),
6262 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6263 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6266 rewind(mnttab_file);
6267 while (getmntent(mnttab_file, &entry) == 0) {
6269 /* ignore non-ZFS entries */
6270 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6273 /* ignore snapshots */
6274 if (strchr(entry.mnt_special, '@') != NULL)
6277 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6278 ZFS_TYPE_FILESYSTEM)) == NULL) {
6285 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6287 sizeof (nfs_mnt_prop),
6288 NULL, NULL, 0, B_FALSE) == 0);
6289 if (strcmp(nfs_mnt_prop, "off") != 0)
6291 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6293 sizeof (nfs_mnt_prop),
6294 NULL, NULL, 0, B_FALSE) == 0);
6295 if (strcmp(nfs_mnt_prop, "off") == 0)
6299 /* Ignore legacy mounts */
6300 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6302 sizeof (nfs_mnt_prop),
6303 NULL, NULL, 0, B_FALSE) == 0);
6304 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6306 /* Ignore canmount=noauto mounts */
6307 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6308 ZFS_CANMOUNT_NOAUTO)
6314 node = safe_malloc(sizeof (unshare_unmount_node_t));
6316 node->un_mountp = safe_strdup(entry.mnt_mountp);
6318 uu_avl_node_init(node, &node->un_avlnode, pool);
6320 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6321 uu_avl_insert(tree, node, idx);
6323 zfs_close(node->un_zhp);
6324 free(node->un_mountp);
6330 * Walk the AVL tree in reverse, unmounting each filesystem and
6331 * removing it from the AVL tree in the process.
6333 if ((walk = uu_avl_walk_start(tree,
6334 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6337 while ((node = uu_avl_walk_next(walk)) != NULL) {
6338 uu_avl_remove(tree, node);
6342 if (zfs_unshareall_bypath(node->un_zhp,
6343 node->un_mountp) != 0)
6348 if (zfs_unmount(node->un_zhp,
6349 node->un_mountp, flags) != 0)
6354 zfs_close(node->un_zhp);
6355 free(node->un_mountp);
6359 uu_avl_walk_end(walk);
6360 uu_avl_destroy(tree);
6361 uu_avl_pool_destroy(pool);
6366 (void) fprintf(stderr,
6367 gettext("missing filesystem argument\n"));
6369 (void) fprintf(stderr,
6370 gettext("too many arguments\n"));
6375 * We have an argument, but it may be a full path or a ZFS
6376 * filesystem. Pass full paths off to unmount_path() (shared by
6377 * manual_unmount), otherwise open the filesystem and pass to
6380 if (argv[0][0] == '/')
6381 return (unshare_unmount_path(op, argv[0],
6384 if ((zhp = zfs_open(g_zfs, argv[0],
6385 ZFS_TYPE_FILESYSTEM)) == NULL)
6388 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6389 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6390 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6391 NULL, 0, B_FALSE) == 0);
6395 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6397 sizeof (nfs_mnt_prop),
6398 NULL, NULL, 0, B_FALSE) == 0);
6399 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6400 sharesmb, sizeof (sharesmb), NULL, NULL,
6403 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6404 strcmp(sharesmb, "off") == 0) {
6405 (void) fprintf(stderr, gettext("cannot "
6406 "unshare '%s': legacy share\n"),
6409 (void) fprintf(stderr, gettext("use "
6410 "unshare(1M) to unshare this "
6414 } else if (!zfs_is_shared(zhp)) {
6415 (void) fprintf(stderr, gettext("cannot "
6416 "unshare '%s': not currently "
6417 "shared\n"), zfs_get_name(zhp));
6419 } else if (zfs_unshareall(zhp) != 0) {
6425 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6426 (void) fprintf(stderr, gettext("cannot "
6427 "unmount '%s': legacy "
6428 "mountpoint\n"), zfs_get_name(zhp));
6429 (void) fprintf(stderr, gettext("use "
6430 "umount(8) to unmount this "
6433 } else if (!zfs_is_mounted(zhp, NULL)) {
6434 (void) fprintf(stderr, gettext("cannot "
6435 "unmount '%s': not currently "
6439 } else if (zfs_unmountall(zhp, flags) != 0) {
6453 * zfs unmount filesystem
6455 * Unmount all filesystems, or a specific ZFS filesystem.
6458 zfs_do_unmount(int argc, char **argv)
6460 return (unshare_unmount(OP_MOUNT, argc, argv));
6465 * zfs unshare filesystem
6467 * Unshare all filesystems, or a specific ZFS filesystem.
6470 zfs_do_unshare(int argc, char **argv)
6472 return (unshare_unmount(OP_SHARE, argc, argv));
6476 * Attach/detach the given dataset to/from the given jail
6480 do_jail(int argc, char **argv, int attach)
6485 /* check number of arguments */
6487 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6491 (void) fprintf(stderr, gettext("too many arguments\n"));
6495 jailid = jail_getid(argv[1]);
6497 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6501 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6505 ret = (zfs_jail(zhp, jailid, attach) != 0);
6512 * zfs jail jailid filesystem
6514 * Attach the given dataset to the given jail
6518 zfs_do_jail(int argc, char **argv)
6521 return (do_jail(argc, argv, 1));
6525 * zfs unjail jailid filesystem
6527 * Detach the given dataset from the given jail
6531 zfs_do_unjail(int argc, char **argv)
6534 return (do_jail(argc, argv, 0));
6538 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6539 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6542 manual_mount(int argc, char **argv)
6545 char mountpoint[ZFS_MAXPROPLEN];
6546 char mntopts[MNT_LINE_MAX] = { '\0' };
6550 char *dataset, *path;
6553 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6556 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6559 flags |= MS_OVERLAY;
6562 flags |= MS_NOMNTTAB;
6565 (void) fprintf(stderr, gettext("missing argument for "
6566 "'%c' option\n"), optopt);
6570 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6572 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6581 /* check that we only have two arguments */
6584 (void) fprintf(stderr, gettext("missing dataset "
6587 (void) fprintf(stderr,
6588 gettext("missing mountpoint argument\n"));
6590 (void) fprintf(stderr, gettext("too many arguments\n"));
6591 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6598 /* try to open the dataset */
6599 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6602 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6603 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6605 /* check for legacy mountpoint and complain appropriately */
6607 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6608 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6609 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6610 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6615 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6616 "mounted using 'mount -t zfs'\n"), dataset);
6617 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6618 "instead.\n"), path);
6619 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6620 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6621 (void) fprintf(stderr, gettext("See zfs(8) for more "
6630 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6631 * unmounts of non-legacy filesystems, as this is the dominant administrative
6635 manual_unmount(int argc, char **argv)
6641 while ((c = getopt(argc, argv, "f")) != -1) {
6647 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6649 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6658 /* check arguments */
6661 (void) fprintf(stderr, gettext("missing path "
6664 (void) fprintf(stderr, gettext("too many arguments\n"));
6665 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6669 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6673 find_command_idx(char *command, int *idx)
6677 for (i = 0; i < NCOMMAND; i++) {
6678 if (command_table[i].name == NULL)
6681 if (strcmp(command, command_table[i].name) == 0) {
6690 zfs_do_diff(int argc, char **argv)
6694 char *tosnap = NULL;
6695 char *fromsnap = NULL;
6700 while ((c = getopt(argc, argv, "FHt")) != -1) {
6703 flags |= ZFS_DIFF_CLASSIFY;
6706 flags |= ZFS_DIFF_PARSEABLE;
6709 flags |= ZFS_DIFF_TIMESTAMP;
6712 (void) fprintf(stderr,
6713 gettext("invalid option '%c'\n"), optopt);
6722 (void) fprintf(stderr,
6723 gettext("must provide at least one snapshot name\n"));
6728 (void) fprintf(stderr, gettext("too many arguments\n"));
6733 tosnap = (argc == 2) ? argv[1] : NULL;
6736 if (*fromsnap != '@')
6737 copy = strdup(fromsnap);
6739 copy = strdup(tosnap);
6743 if (atp = strchr(copy, '@'))
6746 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6752 * Ignore SIGPIPE so that the library can give us
6753 * information on any failure
6755 (void) sigignore(SIGPIPE);
6757 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6765 * zfs bookmark <fs@snap> <fs#bmark>
6767 * Creates a bookmark with the given name from the given snapshot.
6770 zfs_do_bookmark(int argc, char **argv)
6772 char snapname[ZFS_MAXNAMELEN];
6779 while ((c = getopt(argc, argv, "")) != -1) {
6782 (void) fprintf(stderr,
6783 gettext("invalid option '%c'\n"), optopt);
6791 /* check number of arguments */
6793 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6797 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6801 if (strchr(argv[1], '#') == NULL) {
6802 (void) fprintf(stderr,
6803 gettext("invalid bookmark name '%s' -- "
6804 "must contain a '#'\n"), argv[1]);
6808 if (argv[0][0] == '@') {
6810 * Snapshot name begins with @.
6811 * Default to same fs as bookmark.
6813 (void) strncpy(snapname, argv[1], sizeof (snapname));
6814 *strchr(snapname, '#') = '\0';
6815 (void) strlcat(snapname, argv[0], sizeof (snapname));
6817 (void) strncpy(snapname, argv[0], sizeof (snapname));
6819 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6825 nvl = fnvlist_alloc();
6826 fnvlist_add_string(nvl, argv[1], snapname);
6827 ret = lzc_bookmark(nvl, NULL);
6831 const char *err_msg;
6834 (void) snprintf(errbuf, sizeof (errbuf),
6835 dgettext(TEXT_DOMAIN,
6836 "cannot create bookmark '%s'"), argv[1]);
6840 err_msg = "bookmark is in a different pool";
6843 err_msg = "bookmark exists";
6846 err_msg = "invalid argument";
6849 err_msg = "bookmark feature not enabled";
6852 err_msg = "unknown error";
6855 (void) fprintf(stderr, "%s: %s\n", errbuf,
6856 dgettext(TEXT_DOMAIN, err_msg));
6867 main(int argc, char **argv)
6874 (void) setlocale(LC_ALL, "");
6875 (void) textdomain(TEXT_DOMAIN);
6879 if ((g_zfs = libzfs_init()) == NULL) {
6880 (void) fprintf(stderr, gettext("internal error: failed to "
6881 "initialize ZFS library\n"));
6885 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6887 libzfs_print_on_error(g_zfs, B_TRUE);
6889 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6890 (void) fprintf(stderr, gettext("internal error: unable to "
6891 "open %s\n"), MNTTAB);
6896 * This command also doubles as the /etc/fs mount and unmount program.
6897 * Determine if we should take this behavior based on argv[0].
6899 progname = basename(argv[0]);
6900 if (strcmp(progname, "mount") == 0) {
6901 ret = manual_mount(argc, argv);
6902 } else if (strcmp(progname, "umount") == 0) {
6903 ret = manual_unmount(argc, argv);
6906 * Make sure the user has specified some command.
6909 (void) fprintf(stderr, gettext("missing command\n"));
6916 * The 'umount' command is an alias for 'unmount'
6918 if (strcmp(cmdname, "umount") == 0)
6919 cmdname = "unmount";
6922 * The 'recv' command is an alias for 'receive'
6924 if (strcmp(cmdname, "recv") == 0)
6925 cmdname = "receive";
6928 * The 'snap' command is an alias for 'snapshot'
6930 if (strcmp(cmdname, "snap") == 0)
6931 cmdname = "snapshot";
6936 if (strcmp(cmdname, "-?") == 0)
6940 * Run the appropriate command.
6942 libzfs_mnttab_cache(g_zfs, B_TRUE);
6943 if (find_command_idx(cmdname, &i) == 0) {
6944 current_command = &command_table[i];
6945 ret = command_table[i].func(argc - 1, argv + 1);
6946 } else if (strchr(cmdname, '=') != NULL) {
6947 verify(find_command_idx("set", &i) == 0);
6948 current_command = &command_table[i];
6949 ret = command_table[i].func(argc, argv);
6951 (void) fprintf(stderr, gettext("unrecognized "
6952 "command '%s'\n"), cmdname);
6955 libzfs_mnttab_cache(g_zfs, B_FALSE);
6958 (void) fclose(mnttab_file);
6960 if (ret == 0 && log_history)
6961 (void) zpool_log_history(g_zfs, history_str);
6966 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6967 * for the purposes of running ::findleaks.
6969 if (getenv("ZFS_ABORT") != NULL) {
6970 (void) printf("dumping core by request\n");