4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2014 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 [-DnPpRvLe] [-[iI] snapshot] "
279 "\tsend [-Le] [-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, char *propname)
500 char *propval, *strval;
502 if ((propval = strchr(propname, '=')) == NULL) {
503 (void) fprintf(stderr, gettext("missing "
504 "'=' for -o option\n"));
509 if (nvlist_lookup_string(props, propname, &strval) == 0) {
510 (void) fprintf(stderr, gettext("property '%s' "
511 "specified multiple times\n"), propname);
514 if (nvlist_add_string(props, propname, propval) != 0)
520 parse_depth(char *opt, int *flags)
525 depth = (int)strtol(opt, &tmp, 0);
527 (void) fprintf(stderr,
528 gettext("%s is not an integer\n"), opt);
532 (void) fprintf(stderr,
533 gettext("Depth can not be negative.\n"));
536 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
540 #define PROGRESS_DELAY 2 /* seconds */
542 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";
543 static time_t pt_begin;
544 static char *pt_header = NULL;
545 static boolean_t pt_shown;
548 start_progress_timer(void)
550 pt_begin = time(NULL) + PROGRESS_DELAY;
555 set_progress_header(char *header)
557 assert(pt_header == NULL);
558 pt_header = safe_strdup(header);
560 (void) printf("%s: ", header);
561 (void) fflush(stdout);
566 update_progress(char *update)
568 if (!pt_shown && time(NULL) > pt_begin) {
569 int len = strlen(update);
571 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
573 (void) fflush(stdout);
575 } else if (pt_shown) {
576 int len = strlen(update);
578 (void) printf("%s%*.*s", update, len, len, pt_reverse);
579 (void) fflush(stdout);
584 finish_progress(char *done)
587 (void) printf("%s\n", done);
588 (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, optarg))
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, optarg))
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", "posixuser", "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));
3374 * Report any snapshots more recent than the one specified. Used when '-r' is
3375 * not specified. We reuse this same callback for the snapshot dependents - if
3376 * 'cb_dependent' is set, then this is a dependent and we should report it
3377 * without checking the transaction group.
3380 rollback_check(zfs_handle_t *zhp, void *data)
3382 rollback_cbdata_t *cbp = data;
3384 if (cbp->cb_doclones) {
3389 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3390 if (cbp->cb_first && !cbp->cb_recurse) {
3391 (void) fprintf(stderr, gettext("cannot "
3392 "rollback to '%s': more recent snapshots "
3393 "or bookmarks exist\n"),
3395 (void) fprintf(stderr, gettext("use '-r' to "
3396 "force deletion of the following "
3397 "snapshots and bookmarks:\n"));
3402 if (cbp->cb_recurse) {
3403 if (zfs_iter_dependents(zhp, B_TRUE,
3404 rollback_check_dependent, cbp) != 0) {
3409 (void) fprintf(stderr, "%s\n",
3418 zfs_do_rollback(int argc, char **argv)
3422 boolean_t force = B_FALSE;
3423 rollback_cbdata_t cb = { 0 };
3424 zfs_handle_t *zhp, *snap;
3425 char parentname[ZFS_MAXNAMELEN];
3429 while ((c = getopt(argc, argv, "rRf")) != -1) {
3442 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3451 /* check number of arguments */
3453 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3457 (void) fprintf(stderr, gettext("too many arguments\n"));
3461 /* open the snapshot */
3462 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3465 /* open the parent dataset */
3466 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3467 verify((delim = strrchr(parentname, '@')) != NULL);
3469 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3475 * Check for more recent snapshots and/or clones based on the presence
3478 cb.cb_target = argv[0];
3479 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3480 cb.cb_first = B_TRUE;
3482 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3484 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3487 if ((ret = cb.cb_error) != 0)
3491 * Rollback parent to the given snapshot.
3493 ret = zfs_rollback(zhp, snap, force);
3506 * zfs set property=value { fs | snap | vol } ...
3508 * Sets the given property for all datasets specified on the command line.
3510 typedef struct set_cbdata {
3516 set_callback(zfs_handle_t *zhp, void *data)
3518 set_cbdata_t *cbp = data;
3520 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3521 switch (libzfs_errno(g_zfs)) {
3522 case EZFS_MOUNTFAILED:
3523 (void) fprintf(stderr, gettext("property may be set "
3524 "but unable to remount filesystem\n"));
3526 case EZFS_SHARENFSFAILED:
3527 (void) fprintf(stderr, gettext("property may be set "
3528 "but unable to reshare filesystem\n"));
3537 zfs_do_set(int argc, char **argv)
3542 /* check for options */
3543 if (argc > 1 && argv[1][0] == '-') {
3544 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3549 /* check number of arguments */
3551 (void) fprintf(stderr, gettext("missing property=value "
3556 (void) fprintf(stderr, gettext("missing dataset name\n"));
3560 /* validate property=value argument */
3561 cb.cb_propname = argv[1];
3562 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3563 (cb.cb_value[1] == '\0')) {
3564 (void) fprintf(stderr, gettext("missing value in "
3565 "property=value argument\n"));
3569 *cb.cb_value = '\0';
3572 if (*cb.cb_propname == '\0') {
3573 (void) fprintf(stderr,
3574 gettext("missing property in property=value argument\n"));
3578 ret = zfs_for_each(argc - 2, argv + 2, 0,
3579 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3584 typedef struct snap_cbdata {
3586 boolean_t sd_recursive;
3587 const char *sd_snapname;
3591 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3593 snap_cbdata_t *sd = arg;
3598 if (sd->sd_recursive &&
3599 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3604 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3607 fnvlist_add_boolean(sd->sd_nvl, name);
3610 if (sd->sd_recursive)
3611 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3617 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3619 * Creates a snapshot with the given name. While functionally equivalent to
3620 * 'zfs create', it is a separate command to differentiate intent.
3623 zfs_do_snapshot(int argc, char **argv)
3628 snap_cbdata_t sd = { 0 };
3629 boolean_t multiple_snaps = B_FALSE;
3631 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3633 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3637 while ((c = getopt(argc, argv, "ro:")) != -1) {
3640 if (parseprop(props, optarg))
3644 sd.sd_recursive = B_TRUE;
3645 multiple_snaps = B_TRUE;
3648 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3657 /* check number of arguments */
3659 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3664 multiple_snaps = B_TRUE;
3665 for (; argc > 0; argc--, argv++) {
3669 atp = strchr(argv[0], '@');
3673 sd.sd_snapname = atp + 1;
3674 zhp = zfs_open(g_zfs, argv[0],
3675 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3678 if (zfs_snapshot_cb(zhp, &sd) != 0)
3682 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3683 nvlist_free(sd.sd_nvl);
3685 if (ret != 0 && multiple_snaps)
3686 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3690 nvlist_free(sd.sd_nvl);
3697 * Send a backup stream to stdout.
3700 zfs_do_send(int argc, char **argv)
3702 char *fromname = NULL;
3703 char *toname = NULL;
3706 sendflags_t flags = { 0 };
3708 nvlist_t *dbgnv = NULL;
3709 boolean_t extraverbose = B_FALSE;
3712 while ((c = getopt(argc, argv, ":i:I:RDpvnPLe")) != -1) {
3723 flags.doall = B_TRUE;
3726 flags.replicate = B_TRUE;
3729 flags.props = B_TRUE;
3732 flags.parsable = B_TRUE;
3733 flags.verbose = B_TRUE;
3737 extraverbose = B_TRUE;
3738 flags.verbose = B_TRUE;
3739 flags.progress = B_TRUE;
3742 flags.dedup = B_TRUE;
3745 flags.dryrun = B_TRUE;
3748 flags.largeblock = B_TRUE;
3751 flags.embed_data = B_TRUE;
3754 (void) fprintf(stderr, gettext("missing argument for "
3755 "'%c' option\n"), optopt);
3759 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3768 /* check number of arguments */
3770 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3774 (void) fprintf(stderr, gettext("too many arguments\n"));
3778 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3779 (void) fprintf(stderr,
3780 gettext("Error: Stream can not be written to a terminal.\n"
3781 "You must redirect standard output.\n"));
3786 * Special case sending a filesystem, or from a bookmark.
3788 if (strchr(argv[0], '@') == NULL ||
3789 (fromname && strchr(fromname, '#') != NULL)) {
3790 char frombuf[ZFS_MAXNAMELEN];
3791 enum lzc_send_flags lzc_flags = 0;
3793 if (flags.replicate || flags.doall || flags.props ||
3794 flags.dedup || flags.dryrun || flags.verbose ||
3796 (void) fprintf(stderr,
3798 "Unsupported flag with filesystem or bookmark.\n"));
3802 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3806 if (flags.largeblock)
3807 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3808 if (flags.embed_data)
3809 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3811 if (fromname != NULL &&
3812 (fromname[0] == '#' || fromname[0] == '@')) {
3814 * Incremental source name begins with # or @.
3815 * Default to same fs as target.
3817 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3818 cp = strchr(frombuf, '@');
3821 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3824 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3829 cp = strchr(argv[0], '@');
3832 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3837 * If they specified the full path to the snapshot, chop off
3838 * everything except the short name of the snapshot, but special
3839 * case if they specify the origin.
3841 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3842 char origin[ZFS_MAXNAMELEN];
3845 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3846 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3848 if (strcmp(origin, fromname) == 0) {
3850 flags.fromorigin = B_TRUE;
3853 if (cp != fromname && strcmp(argv[0], fromname)) {
3854 (void) fprintf(stderr,
3855 gettext("incremental source must be "
3856 "in same filesystem\n"));
3860 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3861 (void) fprintf(stderr,
3862 gettext("invalid incremental source\n"));
3868 if (flags.replicate && fromname == NULL)
3869 flags.doall = B_TRUE;
3871 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3872 extraverbose ? &dbgnv : NULL);
3874 if (extraverbose && dbgnv != NULL) {
3876 * dump_nvlist prints to stdout, but that's been
3877 * redirected to a file. Make it print to stderr
3880 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3881 dump_nvlist(dbgnv, 0);
3890 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3892 * Restore a backup stream from stdin.
3895 zfs_do_receive(int argc, char **argv)
3898 recvflags_t flags = { 0 };
3901 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3904 flags.isprefix = B_TRUE;
3907 flags.isprefix = B_TRUE;
3908 flags.istail = B_TRUE;
3911 flags.dryrun = B_TRUE;
3914 flags.nomount = B_TRUE;
3917 flags.verbose = B_TRUE;
3920 flags.force = B_TRUE;
3923 (void) fprintf(stderr, gettext("missing argument for "
3924 "'%c' option\n"), optopt);
3928 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3937 /* check number of arguments */
3939 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3943 (void) fprintf(stderr, gettext("too many arguments\n"));
3947 if (isatty(STDIN_FILENO)) {
3948 (void) fprintf(stderr,
3949 gettext("Error: Backup stream can not be read "
3950 "from a terminal.\n"
3951 "You must redirect standard input.\n"));
3955 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3961 * allow/unallow stuff
3963 /* copied from zfs/sys/dsl_deleg.h */
3964 #define ZFS_DELEG_PERM_CREATE "create"
3965 #define ZFS_DELEG_PERM_DESTROY "destroy"
3966 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3967 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3968 #define ZFS_DELEG_PERM_CLONE "clone"
3969 #define ZFS_DELEG_PERM_PROMOTE "promote"
3970 #define ZFS_DELEG_PERM_RENAME "rename"
3971 #define ZFS_DELEG_PERM_MOUNT "mount"
3972 #define ZFS_DELEG_PERM_SHARE "share"
3973 #define ZFS_DELEG_PERM_SEND "send"
3974 #define ZFS_DELEG_PERM_RECEIVE "receive"
3975 #define ZFS_DELEG_PERM_ALLOW "allow"
3976 #define ZFS_DELEG_PERM_USERPROP "userprop"
3977 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3978 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3979 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3980 #define ZFS_DELEG_PERM_USERUSED "userused"
3981 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3982 #define ZFS_DELEG_PERM_HOLD "hold"
3983 #define ZFS_DELEG_PERM_RELEASE "release"
3984 #define ZFS_DELEG_PERM_DIFF "diff"
3985 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
3987 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3989 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3990 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3991 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3992 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3993 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3994 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3995 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3996 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3997 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3998 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3999 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4000 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4001 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4002 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4003 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4004 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4005 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4007 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4008 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4009 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4010 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4011 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4012 { NULL, ZFS_DELEG_NOTE_NONE }
4015 /* permission structure */
4016 typedef struct deleg_perm {
4017 zfs_deleg_who_type_t dp_who_type;
4018 const char *dp_name;
4020 boolean_t dp_descend;
4024 typedef struct deleg_perm_node {
4025 deleg_perm_t dpn_perm;
4027 uu_avl_node_t dpn_avl_node;
4028 } deleg_perm_node_t;
4030 typedef struct fs_perm fs_perm_t;
4032 /* permissions set */
4033 typedef struct who_perm {
4034 zfs_deleg_who_type_t who_type;
4035 const char *who_name; /* id */
4036 char who_ug_name[256]; /* user/group name */
4037 fs_perm_t *who_fsperm; /* uplink */
4039 uu_avl_t *who_deleg_perm_avl; /* permissions */
4043 typedef struct who_perm_node {
4044 who_perm_t who_perm;
4045 uu_avl_node_t who_avl_node;
4048 typedef struct fs_perm_set fs_perm_set_t;
4049 /* fs permissions */
4051 const char *fsp_name;
4053 uu_avl_t *fsp_sc_avl; /* sets,create */
4054 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4056 fs_perm_set_t *fsp_set; /* uplink */
4060 typedef struct fs_perm_node {
4061 fs_perm_t fspn_fsperm;
4064 uu_list_node_t fspn_list_node;
4067 /* top level structure */
4068 struct fs_perm_set {
4069 uu_list_pool_t *fsps_list_pool;
4070 uu_list_t *fsps_list; /* list of fs_perms */
4072 uu_avl_pool_t *fsps_named_set_avl_pool;
4073 uu_avl_pool_t *fsps_who_perm_avl_pool;
4074 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4077 static inline const char *
4078 deleg_perm_type(zfs_deleg_note_t note)
4084 case ZFS_DELEG_NOTE_GROUPQUOTA:
4085 case ZFS_DELEG_NOTE_GROUPUSED:
4086 case ZFS_DELEG_NOTE_USERPROP:
4087 case ZFS_DELEG_NOTE_USERQUOTA:
4088 case ZFS_DELEG_NOTE_USERUSED:
4090 return (gettext("other"));
4092 return (gettext("subcommand"));
4097 who_type2weight(zfs_deleg_who_type_t who_type)
4101 case ZFS_DELEG_NAMED_SET_SETS:
4102 case ZFS_DELEG_NAMED_SET:
4105 case ZFS_DELEG_CREATE_SETS:
4106 case ZFS_DELEG_CREATE:
4109 case ZFS_DELEG_USER_SETS:
4110 case ZFS_DELEG_USER:
4113 case ZFS_DELEG_GROUP_SETS:
4114 case ZFS_DELEG_GROUP:
4117 case ZFS_DELEG_EVERYONE_SETS:
4118 case ZFS_DELEG_EVERYONE:
4130 who_perm_compare(const void *larg, const void *rarg, void *unused)
4132 const who_perm_node_t *l = larg;
4133 const who_perm_node_t *r = rarg;
4134 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4135 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4136 int lweight = who_type2weight(ltype);
4137 int rweight = who_type2weight(rtype);
4138 int res = lweight - rweight;
4140 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4141 ZFS_MAX_DELEG_NAME-1);
4153 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4155 const deleg_perm_node_t *l = larg;
4156 const deleg_perm_node_t *r = rarg;
4157 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4158 ZFS_MAX_DELEG_NAME-1);
4170 fs_perm_set_init(fs_perm_set_t *fspset)
4172 bzero(fspset, sizeof (fs_perm_set_t));
4174 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4175 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4176 NULL, UU_DEFAULT)) == NULL)
4178 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4179 UU_DEFAULT)) == NULL)
4182 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4183 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4184 who_perm_node_t, who_avl_node), who_perm_compare,
4185 UU_DEFAULT)) == NULL)
4188 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4189 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4190 who_perm_node_t, who_avl_node), who_perm_compare,
4191 UU_DEFAULT)) == NULL)
4194 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4195 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4196 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4201 static inline void fs_perm_fini(fs_perm_t *);
4202 static inline void who_perm_fini(who_perm_t *);
4205 fs_perm_set_fini(fs_perm_set_t *fspset)
4207 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4209 while (node != NULL) {
4210 fs_perm_node_t *next_node =
4211 uu_list_next(fspset->fsps_list, node);
4212 fs_perm_t *fsperm = &node->fspn_fsperm;
4213 fs_perm_fini(fsperm);
4214 uu_list_remove(fspset->fsps_list, node);
4219 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4220 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4221 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4225 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4228 deleg_perm->dp_who_type = type;
4229 deleg_perm->dp_name = name;
4233 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4234 zfs_deleg_who_type_t type, const char *name)
4236 uu_avl_pool_t *pool;
4237 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4239 bzero(who_perm, sizeof (who_perm_t));
4241 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4242 UU_DEFAULT)) == NULL)
4245 who_perm->who_type = type;
4246 who_perm->who_name = name;
4247 who_perm->who_fsperm = fsperm;
4251 who_perm_fini(who_perm_t *who_perm)
4253 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4255 while (node != NULL) {
4256 deleg_perm_node_t *next_node =
4257 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4259 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4264 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4268 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4270 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4271 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4273 bzero(fsperm, sizeof (fs_perm_t));
4275 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4279 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4283 fsperm->fsp_set = fspset;
4284 fsperm->fsp_name = fsname;
4288 fs_perm_fini(fs_perm_t *fsperm)
4290 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4291 while (node != NULL) {
4292 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4294 who_perm_t *who_perm = &node->who_perm;
4295 who_perm_fini(who_perm);
4296 uu_avl_remove(fsperm->fsp_sc_avl, node);
4301 node = uu_avl_first(fsperm->fsp_uge_avl);
4302 while (node != NULL) {
4303 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4305 who_perm_t *who_perm = &node->who_perm;
4306 who_perm_fini(who_perm);
4307 uu_avl_remove(fsperm->fsp_uge_avl, node);
4312 uu_avl_destroy(fsperm->fsp_sc_avl);
4313 uu_avl_destroy(fsperm->fsp_uge_avl);
4317 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4318 zfs_deleg_who_type_t who_type, const char *name, char locality)
4320 uu_avl_index_t idx = 0;
4322 deleg_perm_node_t *found_node = NULL;
4323 deleg_perm_t *deleg_perm = &node->dpn_perm;
4325 deleg_perm_init(deleg_perm, who_type, name);
4327 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4329 uu_avl_insert(avl, node, idx);
4332 deleg_perm = &node->dpn_perm;
4337 case ZFS_DELEG_LOCAL:
4338 deleg_perm->dp_local = B_TRUE;
4340 case ZFS_DELEG_DESCENDENT:
4341 deleg_perm->dp_descend = B_TRUE;
4346 assert(B_FALSE); /* invalid locality */
4351 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4353 nvpair_t *nvp = NULL;
4354 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4355 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4356 zfs_deleg_who_type_t who_type = who_perm->who_type;
4358 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4359 const char *name = nvpair_name(nvp);
4360 data_type_t type = nvpair_type(nvp);
4361 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4362 deleg_perm_node_t *node =
4363 safe_malloc(sizeof (deleg_perm_node_t));
4365 assert(type == DATA_TYPE_BOOLEAN);
4367 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4368 set_deleg_perm_node(avl, node, who_type, name, locality);
4375 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4377 nvpair_t *nvp = NULL;
4378 fs_perm_set_t *fspset = fsperm->fsp_set;
4380 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4381 nvlist_t *nvl2 = NULL;
4382 const char *name = nvpair_name(nvp);
4383 uu_avl_t *avl = NULL;
4384 uu_avl_pool_t *avl_pool;
4385 zfs_deleg_who_type_t perm_type = name[0];
4386 char perm_locality = name[1];
4387 const char *perm_name = name + 3;
4388 boolean_t is_set = B_TRUE;
4389 who_perm_t *who_perm = NULL;
4391 assert('$' == name[2]);
4393 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4396 switch (perm_type) {
4397 case ZFS_DELEG_CREATE:
4398 case ZFS_DELEG_CREATE_SETS:
4399 case ZFS_DELEG_NAMED_SET:
4400 case ZFS_DELEG_NAMED_SET_SETS:
4401 avl_pool = fspset->fsps_named_set_avl_pool;
4402 avl = fsperm->fsp_sc_avl;
4404 case ZFS_DELEG_USER:
4405 case ZFS_DELEG_USER_SETS:
4406 case ZFS_DELEG_GROUP:
4407 case ZFS_DELEG_GROUP_SETS:
4408 case ZFS_DELEG_EVERYONE:
4409 case ZFS_DELEG_EVERYONE_SETS:
4410 avl_pool = fspset->fsps_who_perm_avl_pool;
4411 avl = fsperm->fsp_uge_avl;
4416 who_perm_node_t *found_node = NULL;
4417 who_perm_node_t *node = safe_malloc(
4418 sizeof (who_perm_node_t));
4419 who_perm = &node->who_perm;
4420 uu_avl_index_t idx = 0;
4422 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4423 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4425 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4427 if (avl == fsperm->fsp_uge_avl) {
4429 struct passwd *p = NULL;
4430 struct group *g = NULL;
4431 const char *nice_name = NULL;
4433 switch (perm_type) {
4434 case ZFS_DELEG_USER_SETS:
4435 case ZFS_DELEG_USER:
4436 rid = atoi(perm_name);
4439 nice_name = p->pw_name;
4441 case ZFS_DELEG_GROUP_SETS:
4442 case ZFS_DELEG_GROUP:
4443 rid = atoi(perm_name);
4446 nice_name = g->gr_name;
4450 if (nice_name != NULL)
4452 node->who_perm.who_ug_name,
4456 uu_avl_insert(avl, node, idx);
4459 who_perm = &node->who_perm;
4463 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4470 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4472 nvpair_t *nvp = NULL;
4473 uu_avl_index_t idx = 0;
4475 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4476 nvlist_t *nvl2 = NULL;
4477 const char *fsname = nvpair_name(nvp);
4478 data_type_t type = nvpair_type(nvp);
4479 fs_perm_t *fsperm = NULL;
4480 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4484 fsperm = &node->fspn_fsperm;
4486 assert(DATA_TYPE_NVLIST == type);
4488 uu_list_node_init(node, &node->fspn_list_node,
4489 fspset->fsps_list_pool);
4491 idx = uu_list_numnodes(fspset->fsps_list);
4492 fs_perm_init(fsperm, fspset, fsname);
4494 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4497 (void) parse_fs_perm(fsperm, nvl2);
4499 uu_list_insert(fspset->fsps_list, node, idx);
4505 static inline const char *
4506 deleg_perm_comment(zfs_deleg_note_t note)
4508 const char *str = "";
4513 case ZFS_DELEG_NOTE_ALLOW:
4514 str = gettext("Must also have the permission that is being"
4515 "\n\t\t\t\tallowed");
4517 case ZFS_DELEG_NOTE_CLONE:
4518 str = gettext("Must also have the 'create' ability and 'mount'"
4519 "\n\t\t\t\tability in the origin file system");
4521 case ZFS_DELEG_NOTE_CREATE:
4522 str = gettext("Must also have the 'mount' ability");
4524 case ZFS_DELEG_NOTE_DESTROY:
4525 str = gettext("Must also have the 'mount' ability");
4527 case ZFS_DELEG_NOTE_DIFF:
4528 str = gettext("Allows lookup of paths within a dataset;"
4529 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4530 "\n\t\t\t\tin order to use zfs diff");
4532 case ZFS_DELEG_NOTE_HOLD:
4533 str = gettext("Allows adding a user hold to a snapshot");
4535 case ZFS_DELEG_NOTE_MOUNT:
4536 str = gettext("Allows mount/umount of ZFS datasets");
4538 case ZFS_DELEG_NOTE_PROMOTE:
4539 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4540 " 'promote' ability in the origin file system");
4542 case ZFS_DELEG_NOTE_RECEIVE:
4543 str = gettext("Must also have the 'mount' and 'create'"
4546 case ZFS_DELEG_NOTE_RELEASE:
4547 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4548 "might destroy the snapshot");
4550 case ZFS_DELEG_NOTE_RENAME:
4551 str = gettext("Must also have the 'mount' and 'create'"
4552 "\n\t\t\t\tability in the new parent");
4554 case ZFS_DELEG_NOTE_ROLLBACK:
4557 case ZFS_DELEG_NOTE_SEND:
4560 case ZFS_DELEG_NOTE_SHARE:
4561 str = gettext("Allows sharing file systems over NFS or SMB"
4562 "\n\t\t\t\tprotocols");
4564 case ZFS_DELEG_NOTE_SNAPSHOT:
4568 * case ZFS_DELEG_NOTE_VSCAN:
4569 * str = gettext("");
4573 case ZFS_DELEG_NOTE_GROUPQUOTA:
4574 str = gettext("Allows accessing any groupquota@... property");
4576 case ZFS_DELEG_NOTE_GROUPUSED:
4577 str = gettext("Allows reading any groupused@... property");
4579 case ZFS_DELEG_NOTE_USERPROP:
4580 str = gettext("Allows changing any user property");
4582 case ZFS_DELEG_NOTE_USERQUOTA:
4583 str = gettext("Allows accessing any userquota@... property");
4585 case ZFS_DELEG_NOTE_USERUSED:
4586 str = gettext("Allows reading any userused@... property");
4604 boolean_t recursive; /* unallow only */
4605 boolean_t prt_usage;
4607 boolean_t prt_perms;
4610 const char *dataset;
4614 prop_cmp(const void *a, const void *b)
4616 const char *str1 = *(const char **)a;
4617 const char *str2 = *(const char **)b;
4618 return (strcmp(str1, str2));
4622 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4624 const char *opt_desc[] = {
4625 "-h", gettext("show this help message and exit"),
4626 "-l", gettext("set permission locally"),
4627 "-d", gettext("set permission for descents"),
4628 "-u", gettext("set permission for user"),
4629 "-g", gettext("set permission for group"),
4630 "-e", gettext("set permission for everyone"),
4631 "-c", gettext("set create time permission"),
4632 "-s", gettext("define permission set"),
4634 "-r", gettext("remove permissions recursively"),
4636 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4637 size_t allow_size = unallow_size - 2;
4638 const char *props[ZFS_NUM_PROPS];
4641 FILE *fp = requested ? stdout : stderr;
4642 zprop_desc_t *pdtbl = zfs_prop_get_table();
4643 const char *fmt = gettext("%-16s %-14s\t%s\n");
4645 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4647 (void) fprintf(fp, gettext("Options:\n"));
4648 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4649 const char *opt = opt_desc[i++];
4650 const char *optdsc = opt_desc[i];
4651 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4654 (void) fprintf(fp, gettext("\nThe following permissions are "
4656 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4658 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4659 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4660 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4661 const char *perm_type = deleg_perm_type(perm_note);
4662 const char *perm_comment = deleg_perm_comment(perm_note);
4663 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4666 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4667 zprop_desc_t *pd = &pdtbl[i];
4668 if (pd->pd_visible != B_TRUE)
4671 if (pd->pd_attr == PROP_READONLY)
4674 props[count++] = pd->pd_name;
4676 props[count] = NULL;
4678 qsort(props, count, sizeof (char *), prop_cmp);
4680 for (i = 0; i < count; i++)
4681 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4684 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4686 exit(requested ? 0 : 2);
4689 static inline const char *
4690 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4693 if (un && argc == expected_argc - 1)
4695 else if (argc == expected_argc)
4696 *permsp = argv[argc - 2];
4698 allow_usage(un, B_FALSE,
4699 gettext("wrong number of parameters\n"));
4701 return (argv[argc - 1]);
4705 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4707 int uge_sum = opts->user + opts->group + opts->everyone;
4708 int csuge_sum = opts->create + opts->set + uge_sum;
4709 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4710 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4713 allow_usage(un, B_FALSE,
4714 gettext("-u, -g, and -e are mutually exclusive\n"));
4716 if (opts->prt_usage)
4717 if (argc == 0 && all_sum == 0)
4718 allow_usage(un, B_TRUE, NULL);
4724 allow_usage(un, B_FALSE,
4725 gettext("invalid options combined with -s\n"));
4727 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4728 if (argv[0][0] != '@')
4729 allow_usage(un, B_FALSE,
4730 gettext("invalid set name: missing '@' prefix\n"));
4731 opts->who = argv[0];
4732 } else if (opts->create) {
4733 if (ldcsuge_sum > 1)
4734 allow_usage(un, B_FALSE,
4735 gettext("invalid options combined with -c\n"));
4736 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4737 } else if (opts->everyone) {
4739 allow_usage(un, B_FALSE,
4740 gettext("invalid options combined with -e\n"));
4741 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4742 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4744 opts->everyone = B_TRUE;
4747 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4748 } else if (argc == 1 && !un) {
4749 opts->prt_perms = B_TRUE;
4750 opts->dataset = argv[argc-1];
4752 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4753 opts->who = argv[0];
4756 if (!opts->local && !opts->descend) {
4757 opts->local = B_TRUE;
4758 opts->descend = B_TRUE;
4763 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4764 const char *who, char *perms, nvlist_t *top_nvl)
4767 char ld[2] = { '\0', '\0' };
4768 char who_buf[ZFS_MAXNAMELEN+32];
4771 nvlist_t *base_nvl = NULL;
4772 nvlist_t *set_nvl = NULL;
4775 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4777 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4781 case ZFS_DELEG_NAMED_SET_SETS:
4782 case ZFS_DELEG_NAMED_SET:
4783 set_type = ZFS_DELEG_NAMED_SET_SETS;
4784 base_type = ZFS_DELEG_NAMED_SET;
4785 ld[0] = ZFS_DELEG_NA;
4787 case ZFS_DELEG_CREATE_SETS:
4788 case ZFS_DELEG_CREATE:
4789 set_type = ZFS_DELEG_CREATE_SETS;
4790 base_type = ZFS_DELEG_CREATE;
4791 ld[0] = ZFS_DELEG_NA;
4793 case ZFS_DELEG_USER_SETS:
4794 case ZFS_DELEG_USER:
4795 set_type = ZFS_DELEG_USER_SETS;
4796 base_type = ZFS_DELEG_USER;
4798 ld[0] = ZFS_DELEG_LOCAL;
4800 ld[1] = ZFS_DELEG_DESCENDENT;
4802 case ZFS_DELEG_GROUP_SETS:
4803 case ZFS_DELEG_GROUP:
4804 set_type = ZFS_DELEG_GROUP_SETS;
4805 base_type = ZFS_DELEG_GROUP;
4807 ld[0] = ZFS_DELEG_LOCAL;
4809 ld[1] = ZFS_DELEG_DESCENDENT;
4811 case ZFS_DELEG_EVERYONE_SETS:
4812 case ZFS_DELEG_EVERYONE:
4813 set_type = ZFS_DELEG_EVERYONE_SETS;
4814 base_type = ZFS_DELEG_EVERYONE;
4816 ld[0] = ZFS_DELEG_LOCAL;
4818 ld[1] = ZFS_DELEG_DESCENDENT;
4821 if (perms != NULL) {
4823 char *end = curr + strlen(perms);
4825 while (curr < end) {
4826 char *delim = strchr(curr, ',');
4837 (void) nvlist_add_boolean(nvl, curr);
4843 for (i = 0; i < 2; i++) {
4844 char locality = ld[i];
4848 if (!nvlist_empty(base_nvl)) {
4850 (void) snprintf(who_buf,
4851 sizeof (who_buf), "%c%c$%s",
4852 base_type, locality, who);
4854 (void) snprintf(who_buf,
4855 sizeof (who_buf), "%c%c$",
4856 base_type, locality);
4858 (void) nvlist_add_nvlist(top_nvl, who_buf,
4863 if (!nvlist_empty(set_nvl)) {
4865 (void) snprintf(who_buf,
4866 sizeof (who_buf), "%c%c$%s",
4867 set_type, locality, who);
4869 (void) snprintf(who_buf,
4870 sizeof (who_buf), "%c%c$",
4871 set_type, locality);
4873 (void) nvlist_add_nvlist(top_nvl, who_buf,
4878 for (i = 0; i < 2; i++) {
4879 char locality = ld[i];
4884 (void) snprintf(who_buf, sizeof (who_buf),
4885 "%c%c$%s", base_type, locality, who);
4887 (void) snprintf(who_buf, sizeof (who_buf),
4888 "%c%c$", base_type, locality);
4889 (void) nvlist_add_boolean(top_nvl, who_buf);
4892 (void) snprintf(who_buf, sizeof (who_buf),
4893 "%c%c$%s", set_type, locality, who);
4895 (void) snprintf(who_buf, sizeof (who_buf),
4896 "%c%c$", set_type, locality);
4897 (void) nvlist_add_boolean(top_nvl, who_buf);
4903 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4905 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4909 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4910 opts->descend, opts->who, opts->perms, *nvlp);
4911 } else if (opts->create) {
4912 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4913 opts->descend, NULL, opts->perms, *nvlp);
4914 } else if (opts->everyone) {
4915 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4916 opts->descend, NULL, opts->perms, *nvlp);
4918 char *curr = opts->who;
4919 char *end = curr + strlen(curr);
4921 while (curr < end) {
4923 zfs_deleg_who_type_t who_type;
4925 char *delim = strchr(curr, ',');
4928 struct passwd *p = NULL;
4929 struct group *g = NULL;
4937 rid = (uid_t)strtol(curr, &endch, 0);
4939 who_type = ZFS_DELEG_USER;
4948 (void) snprintf(errbuf, 256, gettext(
4949 "invalid user %s"), curr);
4950 allow_usage(un, B_TRUE, errbuf);
4952 } else if (opts->group) {
4953 who_type = ZFS_DELEG_GROUP;
4962 (void) snprintf(errbuf, 256, gettext(
4963 "invalid group %s"), curr);
4964 allow_usage(un, B_TRUE, errbuf);
4967 if (*endch != '\0') {
4974 if (*endch != '\0') {
4981 who_type = ZFS_DELEG_USER;
4983 } else if (g != NULL) {
4984 who_type = ZFS_DELEG_GROUP;
4987 (void) snprintf(errbuf, 256, gettext(
4988 "invalid user/group %s"), curr);
4989 allow_usage(un, B_TRUE, errbuf);
4993 (void) sprintf(id, "%u", rid);
4996 store_allow_perm(who_type, opts->local,
4997 opts->descend, who, opts->perms, *nvlp);
5006 print_set_creat_perms(uu_avl_t *who_avl)
5008 const char *sc_title[] = {
5009 gettext("Permission sets:\n"),
5010 gettext("Create time permissions:\n"),
5013 const char **title_ptr = sc_title;
5014 who_perm_node_t *who_node = NULL;
5015 int prev_weight = -1;
5017 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5018 who_node = uu_avl_next(who_avl, who_node)) {
5019 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5020 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5021 const char *who_name = who_node->who_perm.who_name;
5022 int weight = who_type2weight(who_type);
5023 boolean_t first = B_TRUE;
5024 deleg_perm_node_t *deleg_node;
5026 if (prev_weight != weight) {
5027 (void) printf(*title_ptr++);
5028 prev_weight = weight;
5031 if (who_name == NULL || strnlen(who_name, 1) == 0)
5032 (void) printf("\t");
5034 (void) printf("\t%s ", who_name);
5036 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5037 deleg_node = uu_avl_next(avl, deleg_node)) {
5040 deleg_node->dpn_perm.dp_name);
5043 (void) printf(",%s",
5044 deleg_node->dpn_perm.dp_name);
5047 (void) printf("\n");
5052 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5055 who_perm_node_t *who_node = NULL;
5056 boolean_t prt_title = B_TRUE;
5057 uu_avl_walk_t *walk;
5059 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5062 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5063 const char *who_name = who_node->who_perm.who_name;
5064 const char *nice_who_name = who_node->who_perm.who_ug_name;
5065 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5066 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5068 deleg_perm_node_t *deleg_node;
5069 boolean_t prt_who = B_TRUE;
5071 for (deleg_node = uu_avl_first(avl);
5073 deleg_node = uu_avl_next(avl, deleg_node)) {
5074 if (local != deleg_node->dpn_perm.dp_local ||
5075 descend != deleg_node->dpn_perm.dp_descend)
5079 const char *who = NULL;
5081 prt_title = B_FALSE;
5082 (void) printf(title);
5086 case ZFS_DELEG_USER_SETS:
5087 case ZFS_DELEG_USER:
5088 who = gettext("user");
5090 who_name = nice_who_name;
5092 case ZFS_DELEG_GROUP_SETS:
5093 case ZFS_DELEG_GROUP:
5094 who = gettext("group");
5096 who_name = nice_who_name;
5098 case ZFS_DELEG_EVERYONE_SETS:
5099 case ZFS_DELEG_EVERYONE:
5100 who = gettext("everyone");
5105 if (who_name == NULL)
5106 (void) printf("\t%s", who);
5108 (void) printf("\t%s %s", who, who_name);
5111 (void) printf("%c%s", delim,
5112 deleg_node->dpn_perm.dp_name);
5117 (void) printf("\n");
5120 uu_avl_walk_end(walk);
5124 print_fs_perms(fs_perm_set_t *fspset)
5126 fs_perm_node_t *node = NULL;
5127 char buf[ZFS_MAXNAMELEN+32];
5128 const char *dsname = buf;
5130 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5131 node = uu_list_next(fspset->fsps_list, node)) {
5132 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5133 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5136 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5137 gettext("---- Permissions on %s "),
5138 node->fspn_fsperm.fsp_name);
5139 (void) printf(dsname);
5140 left = 70 - strlen(buf);
5143 (void) printf("\n");
5145 print_set_creat_perms(sc_avl);
5146 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5147 gettext("Local permissions:\n"));
5148 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5149 gettext("Descendent permissions:\n"));
5150 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5151 gettext("Local+Descendent permissions:\n"));
5155 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5157 struct deleg_perms {
5163 set_deleg_perms(zfs_handle_t *zhp, void *data)
5165 struct deleg_perms *perms = (struct deleg_perms *)data;
5166 zfs_type_t zfs_type = zfs_get_type(zhp);
5168 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5171 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5175 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5178 nvlist_t *perm_nvl = NULL;
5179 nvlist_t *update_perm_nvl = NULL;
5182 struct allow_opts opts = { 0 };
5184 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5187 while ((c = getopt(argc, argv, optstr)) != -1) {
5190 opts.local = B_TRUE;
5193 opts.descend = B_TRUE;
5199 opts.group = B_TRUE;
5202 opts.everyone = B_TRUE;
5208 opts.create = B_TRUE;
5211 opts.recursive = B_TRUE;
5214 (void) fprintf(stderr, gettext("missing argument for "
5215 "'%c' option\n"), optopt);
5219 opts.prt_usage = B_TRUE;
5222 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5231 /* check arguments */
5232 parse_allow_args(argc, argv, un, &opts);
5234 /* try to open the dataset */
5235 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5236 ZFS_TYPE_VOLUME)) == NULL) {
5237 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5242 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5245 fs_perm_set_init(&fs_perm_set);
5246 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5247 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5252 print_fs_perms(&fs_perm_set);
5254 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5255 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5258 if (un && opts.recursive) {
5259 struct deleg_perms data = { un, update_perm_nvl };
5260 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5269 nvlist_free(perm_nvl);
5270 if (update_perm_nvl != NULL)
5271 nvlist_free(update_perm_nvl);
5273 fs_perm_set_fini(&fs_perm_set);
5281 zfs_do_allow(int argc, char **argv)
5283 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5287 zfs_do_unallow(int argc, char **argv)
5289 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5293 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5298 boolean_t recursive = B_FALSE;
5299 const char *opts = holding ? "rt" : "r";
5303 while ((c = getopt(argc, argv, opts)) != -1) {
5309 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5318 /* check number of arguments */
5326 if (holding && tag[0] == '.') {
5327 /* tags starting with '.' are reserved for libzfs */
5328 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5332 for (i = 0; i < argc; ++i) {
5334 char parent[ZFS_MAXNAMELEN];
5336 char *path = argv[i];
5338 delim = strchr(path, '@');
5339 if (delim == NULL) {
5340 (void) fprintf(stderr,
5341 gettext("'%s' is not a snapshot\n"), path);
5345 (void) strncpy(parent, path, delim - path);
5346 parent[delim - path] = '\0';
5348 zhp = zfs_open(g_zfs, parent,
5349 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5355 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5358 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5364 return (errors != 0);
5368 * zfs hold [-r] [-t] <tag> <snap> ...
5370 * -r Recursively hold
5372 * Apply a user-hold with the given tag to the list of snapshots.
5375 zfs_do_hold(int argc, char **argv)
5377 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5381 * zfs release [-r] <tag> <snap> ...
5383 * -r Recursively release
5385 * Release a user-hold with the given tag from the list of snapshots.
5388 zfs_do_release(int argc, char **argv)
5390 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5393 typedef struct holds_cbdata {
5394 boolean_t cb_recursive;
5395 const char *cb_snapname;
5397 size_t cb_max_namelen;
5398 size_t cb_max_taglen;
5401 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5402 #define DATETIME_BUF_LEN (32)
5407 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5410 nvpair_t *nvp = NULL;
5411 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5415 for (i = 0; i < 3; i++) {
5416 col = gettext(hdr_cols[i]);
5418 (void) printf("%-*s ", i ? tagwidth : nwidth,
5421 (void) printf("%s\n", col);
5425 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5426 char *zname = nvpair_name(nvp);
5428 nvpair_t *nvp2 = NULL;
5429 (void) nvpair_value_nvlist(nvp, &nvl2);
5430 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5431 char tsbuf[DATETIME_BUF_LEN];
5432 char *tagname = nvpair_name(nvp2);
5436 char sep = scripted ? '\t' : ' ';
5437 size_t sepnum = scripted ? 1 : 2;
5439 (void) nvpair_value_uint64(nvp2, &val);
5441 (void) localtime_r(&time, &t);
5442 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5443 gettext(STRFTIME_FMT_STR), &t);
5445 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5446 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5452 * Generic callback function to list a dataset or snapshot.
5455 holds_callback(zfs_handle_t *zhp, void *data)
5457 holds_cbdata_t *cbp = data;
5458 nvlist_t *top_nvl = *cbp->cb_nvlp;
5459 nvlist_t *nvl = NULL;
5460 nvpair_t *nvp = NULL;
5461 const char *zname = zfs_get_name(zhp);
5462 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5464 if (cbp->cb_recursive) {
5465 const char *snapname;
5466 char *delim = strchr(zname, '@');
5470 snapname = delim + 1;
5471 if (strcmp(cbp->cb_snapname, snapname))
5475 if (zfs_get_holds(zhp, &nvl) != 0)
5478 if (znamelen > cbp->cb_max_namelen)
5479 cbp->cb_max_namelen = znamelen;
5481 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5482 const char *tag = nvpair_name(nvp);
5483 size_t taglen = strnlen(tag, MAXNAMELEN);
5484 if (taglen > cbp->cb_max_taglen)
5485 cbp->cb_max_taglen = taglen;
5488 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5492 * zfs holds [-r] <snap> ...
5494 * -r Recursively hold
5497 zfs_do_holds(int argc, char **argv)
5502 boolean_t scripted = B_FALSE;
5503 boolean_t recursive = B_FALSE;
5504 const char *opts = "rH";
5507 int types = ZFS_TYPE_SNAPSHOT;
5508 holds_cbdata_t cb = { 0 };
5515 while ((c = getopt(argc, argv, opts)) != -1) {
5524 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5531 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5532 flags |= ZFS_ITER_RECURSE;
5538 /* check number of arguments */
5542 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5545 for (i = 0; i < argc; ++i) {
5546 char *snapshot = argv[i];
5548 const char *snapname;
5550 delim = strchr(snapshot, '@');
5551 if (delim == NULL) {
5552 (void) fprintf(stderr,
5553 gettext("'%s' is not a snapshot\n"), snapshot);
5557 snapname = delim + 1;
5559 snapshot[delim - snapshot] = '\0';
5561 cb.cb_recursive = recursive;
5562 cb.cb_snapname = snapname;
5566 * 1. collect holds data, set format options
5568 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5569 holds_callback, &cb);
5575 * 2. print holds data
5577 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5579 if (nvlist_empty(nvl))
5580 (void) printf(gettext("no datasets available\n"));
5584 return (0 != errors);
5587 #define CHECK_SPINNER 30
5588 #define SPINNER_TIME 3 /* seconds */
5589 #define MOUNT_TIME 5 /* seconds */
5592 get_one_dataset(zfs_handle_t *zhp, void *data)
5594 static char *spin[] = { "-", "\\", "|", "/" };
5595 static int spinval = 0;
5596 static int spincheck = 0;
5597 static time_t last_spin_time = (time_t)0;
5598 get_all_cb_t *cbp = data;
5599 zfs_type_t type = zfs_get_type(zhp);
5601 if (cbp->cb_verbose) {
5602 if (--spincheck < 0) {
5603 time_t now = time(NULL);
5604 if (last_spin_time + SPINNER_TIME < now) {
5605 update_progress(spin[spinval++ % 4]);
5606 last_spin_time = now;
5608 spincheck = CHECK_SPINNER;
5613 * Interate over any nested datasets.
5615 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5621 * Skip any datasets whose type does not match.
5623 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5627 libzfs_add_handle(cbp, zhp);
5628 assert(cbp->cb_used <= cbp->cb_alloc);
5634 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5636 get_all_cb_t cb = { 0 };
5637 cb.cb_verbose = verbose;
5638 cb.cb_getone = get_one_dataset;
5641 set_progress_header(gettext("Reading ZFS config"));
5642 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5644 *dslist = cb.cb_handles;
5645 *count = cb.cb_used;
5648 finish_progress(gettext("done."));
5652 * Generic callback for sharing or mounting filesystems. Because the code is so
5653 * similar, we have a common function with an extra parameter to determine which
5654 * mode we are using.
5656 #define OP_SHARE 0x1
5657 #define OP_MOUNT 0x2
5660 * Share or mount a dataset.
5663 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5664 boolean_t explicit, const char *options)
5666 char mountpoint[ZFS_MAXPROPLEN];
5667 char shareopts[ZFS_MAXPROPLEN];
5668 char smbshareopts[ZFS_MAXPROPLEN];
5669 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5671 uint64_t zoned, canmount;
5672 boolean_t shared_nfs, shared_smb;
5674 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5677 * Check to make sure we can mount/share this dataset. If we
5678 * are in the global zone and the filesystem is exported to a
5679 * local zone, or if we are in a local zone and the
5680 * filesystem is not exported, then it is an error.
5682 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5684 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5688 (void) fprintf(stderr, gettext("cannot %s '%s': "
5689 "dataset is exported to a local zone\n"), cmdname,
5693 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5697 (void) fprintf(stderr, gettext("cannot %s '%s': "
5698 "permission denied\n"), cmdname,
5704 * Ignore any filesystems which don't apply to us. This
5705 * includes those with a legacy mountpoint, or those with
5706 * legacy share options.
5708 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5709 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5710 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5711 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5712 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5713 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5715 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5716 strcmp(smbshareopts, "off") == 0) {
5720 (void) fprintf(stderr, gettext("cannot share '%s': "
5721 "legacy share\n"), zfs_get_name(zhp));
5722 (void) fprintf(stderr, gettext("to "
5723 "share this filesystem set "
5724 "sharenfs property on\n"));
5729 * We cannot share or mount legacy filesystems. If the
5730 * shareopts is non-legacy but the mountpoint is legacy, we
5731 * treat it as a legacy share.
5733 if (strcmp(mountpoint, "legacy") == 0) {
5737 (void) fprintf(stderr, gettext("cannot %s '%s': "
5738 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5739 (void) fprintf(stderr, gettext("use %s(8) to "
5740 "%s this filesystem\n"), cmdname, cmdname);
5744 if (strcmp(mountpoint, "none") == 0) {
5748 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5749 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5754 * canmount explicit outcome
5755 * on no pass through
5756 * on yes pass through
5758 * off yes display error, return 1
5759 * noauto no return 0
5760 * noauto yes pass through
5762 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5763 if (canmount == ZFS_CANMOUNT_OFF) {
5767 (void) fprintf(stderr, gettext("cannot %s '%s': "
5768 "'canmount' property is set to 'off'\n"), cmdname,
5771 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5776 * At this point, we have verified that the mountpoint and/or
5777 * shareopts are appropriate for auto management. If the
5778 * filesystem is already mounted or shared, return (failing
5779 * for explicit requests); otherwise mount or share the
5785 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5786 shared_smb = zfs_is_shared_smb(zhp, NULL);
5788 if (shared_nfs && shared_smb ||
5789 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5790 strcmp(smbshareopts, "off") == 0) ||
5791 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5792 strcmp(shareopts, "off") == 0)) {
5796 (void) fprintf(stderr, gettext("cannot share "
5797 "'%s': filesystem already shared\n"),
5802 if (!zfs_is_mounted(zhp, NULL) &&
5803 zfs_mount(zhp, NULL, 0) != 0)
5806 if (protocol == NULL) {
5807 if (zfs_shareall(zhp) != 0)
5809 } else if (strcmp(protocol, "nfs") == 0) {
5810 if (zfs_share_nfs(zhp))
5812 } else if (strcmp(protocol, "smb") == 0) {
5813 if (zfs_share_smb(zhp))
5816 (void) fprintf(stderr, gettext("cannot share "
5817 "'%s': invalid share type '%s' "
5819 zfs_get_name(zhp), protocol);
5826 if (options == NULL)
5827 mnt.mnt_mntopts = "";
5829 mnt.mnt_mntopts = (char *)options;
5831 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5832 zfs_is_mounted(zhp, NULL)) {
5836 (void) fprintf(stderr, gettext("cannot mount "
5837 "'%s': filesystem already mounted\n"),
5842 if (zfs_mount(zhp, options, flags) != 0)
5851 * Reports progress in the form "(current/total)". Not thread-safe.
5854 report_mount_progress(int current, int total)
5856 static time_t last_progress_time = 0;
5857 time_t now = time(NULL);
5860 /* report 1..n instead of 0..n-1 */
5863 /* display header if we're here for the first time */
5865 set_progress_header(gettext("Mounting ZFS filesystems"));
5866 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5867 /* too soon to report again */
5871 last_progress_time = now;
5873 (void) sprintf(info, "(%d/%d)", current, total);
5875 if (current == total)
5876 finish_progress(info);
5878 update_progress(info);
5882 append_options(char *mntopts, char *newopts)
5884 int len = strlen(mntopts);
5886 /* original length plus new string to append plus 1 for the comma */
5887 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5888 (void) fprintf(stderr, gettext("the opts argument for "
5889 "'%c' option is too long (more than %d chars)\n"),
5890 "-o", MNT_LINE_MAX);
5895 mntopts[len++] = ',';
5897 (void) strcpy(&mntopts[len], newopts);
5901 share_mount(int op, int argc, char **argv)
5904 boolean_t verbose = B_FALSE;
5906 char *options = NULL;
5910 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5920 if (*optarg == '\0') {
5921 (void) fprintf(stderr, gettext("empty mount "
5922 "options (-o) specified\n"));
5926 if (options == NULL)
5927 options = safe_malloc(MNT_LINE_MAX + 1);
5929 /* option validation is done later */
5930 append_options(options, optarg);
5934 warnx("no overlay mounts support on FreeBSD, ignoring");
5937 (void) fprintf(stderr, gettext("missing argument for "
5938 "'%c' option\n"), optopt);
5942 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5951 /* check number of arguments */
5953 zfs_handle_t **dslist = NULL;
5954 size_t i, count = 0;
5955 char *protocol = NULL;
5957 if (op == OP_SHARE && argc > 0) {
5958 if (strcmp(argv[0], "nfs") != 0 &&
5959 strcmp(argv[0], "smb") != 0) {
5960 (void) fprintf(stderr, gettext("share type "
5961 "must be 'nfs' or 'smb'\n"));
5970 (void) fprintf(stderr, gettext("too many arguments\n"));
5974 start_progress_timer();
5975 get_all_datasets(&dslist, &count, verbose);
5980 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5982 for (i = 0; i < count; i++) {
5984 report_mount_progress(i, count);
5986 if (share_mount_one(dslist[i], op, flags, protocol,
5987 B_FALSE, options) != 0)
5989 zfs_close(dslist[i]);
5993 } else if (argc == 0) {
5994 struct mnttab entry;
5996 if ((op == OP_SHARE) || (options != NULL)) {
5997 (void) fprintf(stderr, gettext("missing filesystem "
5998 "argument (specify -a for all)\n"));
6003 * When mount is given no arguments, go through /etc/mnttab and
6004 * display any active ZFS mounts. We hide any snapshots, since
6005 * they are controlled automatically.
6007 rewind(mnttab_file);
6008 while (getmntent(mnttab_file, &entry) == 0) {
6009 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6010 strchr(entry.mnt_special, '@') != NULL)
6013 (void) printf("%-30s %s\n", entry.mnt_special,
6021 (void) fprintf(stderr,
6022 gettext("too many arguments\n"));
6026 if ((zhp = zfs_open(g_zfs, argv[0],
6027 ZFS_TYPE_FILESYSTEM)) == NULL) {
6030 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6040 * zfs mount -a [nfs]
6041 * zfs mount filesystem
6043 * Mount all filesystems, or mount the given filesystem.
6046 zfs_do_mount(int argc, char **argv)
6048 return (share_mount(OP_MOUNT, argc, argv));
6052 * zfs share -a [nfs | smb]
6053 * zfs share filesystem
6055 * Share all filesystems, or share the given filesystem.
6058 zfs_do_share(int argc, char **argv)
6060 return (share_mount(OP_SHARE, argc, argv));
6063 typedef struct unshare_unmount_node {
6064 zfs_handle_t *un_zhp;
6066 uu_avl_node_t un_avlnode;
6067 } unshare_unmount_node_t;
6071 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6073 const unshare_unmount_node_t *l = larg;
6074 const unshare_unmount_node_t *r = rarg;
6076 return (strcmp(l->un_mountp, r->un_mountp));
6080 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6081 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6082 * and unmount it appropriately.
6085 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6089 struct stat64 statbuf;
6090 struct extmnttab entry;
6091 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6095 * Search for the path in /etc/mnttab. Rather than looking for the
6096 * specific path, which can be fooled by non-standard paths (i.e. ".."
6097 * or "//"), we stat() the path and search for the corresponding
6098 * (major,minor) device pair.
6100 if (stat64(path, &statbuf) != 0) {
6101 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6102 cmdname, path, strerror(errno));
6105 path_inode = statbuf.st_ino;
6108 * Search for the given (major,minor) pair in the mount table.
6111 rewind(mnttab_file);
6112 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6113 if (entry.mnt_major == major(statbuf.st_dev) &&
6114 entry.mnt_minor == minor(statbuf.st_dev))
6121 if (statfs(path, &sfs) != 0) {
6122 (void) fprintf(stderr, "%s: %s\n", path,
6126 statfs2mnttab(&sfs, &entry);
6130 if (op == OP_SHARE) {
6131 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6132 "currently mounted\n"), cmdname, path);
6135 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6137 if ((ret = umount2(path, flags)) != 0)
6138 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6143 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6144 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6145 "filesystem\n"), cmdname, path);
6149 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6150 ZFS_TYPE_FILESYSTEM)) == NULL)
6154 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6155 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6156 cmdname, path, strerror(errno));
6158 } else if (statbuf.st_ino != path_inode) {
6159 (void) fprintf(stderr, gettext("cannot "
6160 "%s '%s': not a mountpoint\n"), cmdname, path);
6164 if (op == OP_SHARE) {
6165 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6166 char smbshare_prop[ZFS_MAXPROPLEN];
6168 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6169 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6170 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6171 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6173 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6174 strcmp(smbshare_prop, "off") == 0) {
6175 (void) fprintf(stderr, gettext("cannot unshare "
6176 "'%s': legacy share\n"), path);
6178 (void) fprintf(stderr, gettext("use "
6179 "unshare(1M) to unshare this filesystem\n"));
6181 } else if (!zfs_is_shared(zhp)) {
6182 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6183 "not currently shared\n"), path);
6185 ret = zfs_unshareall_bypath(zhp, path);
6188 char mtpt_prop[ZFS_MAXPROPLEN];
6190 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6191 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6194 ret = zfs_unmount(zhp, NULL, flags);
6195 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6196 (void) fprintf(stderr, gettext("cannot unmount "
6197 "'%s': legacy mountpoint\n"),
6199 (void) fprintf(stderr, gettext("use umount(8) "
6200 "to unmount this filesystem\n"));
6202 ret = zfs_unmountall(zhp, flags);
6213 * Generic callback for unsharing or unmounting a filesystem.
6216 unshare_unmount(int op, int argc, char **argv)
6223 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6224 char sharesmb[ZFS_MAXPROPLEN];
6227 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6236 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6247 * We could make use of zfs_for_each() to walk all datasets in
6248 * the system, but this would be very inefficient, especially
6249 * since we would have to linearly search /etc/mnttab for each
6250 * one. Instead, do one pass through /etc/mnttab looking for
6251 * zfs entries and call zfs_unmount() for each one.
6253 * Things get a little tricky if the administrator has created
6254 * mountpoints beneath other ZFS filesystems. In this case, we
6255 * have to unmount the deepest filesystems first. To accomplish
6256 * this, we place all the mountpoints in an AVL tree sorted by
6257 * the special type (dataset name), and walk the result in
6258 * reverse to make sure to get any snapshots first.
6260 struct mnttab entry;
6261 uu_avl_pool_t *pool;
6263 unshare_unmount_node_t *node;
6265 uu_avl_walk_t *walk;
6268 (void) fprintf(stderr, gettext("too many arguments\n"));
6272 if (((pool = uu_avl_pool_create("unmount_pool",
6273 sizeof (unshare_unmount_node_t),
6274 offsetof(unshare_unmount_node_t, un_avlnode),
6275 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6276 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6279 rewind(mnttab_file);
6280 while (getmntent(mnttab_file, &entry) == 0) {
6282 /* ignore non-ZFS entries */
6283 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6286 /* ignore snapshots */
6287 if (strchr(entry.mnt_special, '@') != NULL)
6290 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6291 ZFS_TYPE_FILESYSTEM)) == NULL) {
6298 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6300 sizeof (nfs_mnt_prop),
6301 NULL, NULL, 0, B_FALSE) == 0);
6302 if (strcmp(nfs_mnt_prop, "off") != 0)
6304 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6306 sizeof (nfs_mnt_prop),
6307 NULL, NULL, 0, B_FALSE) == 0);
6308 if (strcmp(nfs_mnt_prop, "off") == 0)
6312 /* Ignore legacy mounts */
6313 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6315 sizeof (nfs_mnt_prop),
6316 NULL, NULL, 0, B_FALSE) == 0);
6317 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6319 /* Ignore canmount=noauto mounts */
6320 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6321 ZFS_CANMOUNT_NOAUTO)
6327 node = safe_malloc(sizeof (unshare_unmount_node_t));
6329 node->un_mountp = safe_strdup(entry.mnt_mountp);
6331 uu_avl_node_init(node, &node->un_avlnode, pool);
6333 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6334 uu_avl_insert(tree, node, idx);
6336 zfs_close(node->un_zhp);
6337 free(node->un_mountp);
6343 * Walk the AVL tree in reverse, unmounting each filesystem and
6344 * removing it from the AVL tree in the process.
6346 if ((walk = uu_avl_walk_start(tree,
6347 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6350 while ((node = uu_avl_walk_next(walk)) != NULL) {
6351 uu_avl_remove(tree, node);
6355 if (zfs_unshareall_bypath(node->un_zhp,
6356 node->un_mountp) != 0)
6361 if (zfs_unmount(node->un_zhp,
6362 node->un_mountp, flags) != 0)
6367 zfs_close(node->un_zhp);
6368 free(node->un_mountp);
6372 uu_avl_walk_end(walk);
6373 uu_avl_destroy(tree);
6374 uu_avl_pool_destroy(pool);
6379 (void) fprintf(stderr,
6380 gettext("missing filesystem argument\n"));
6382 (void) fprintf(stderr,
6383 gettext("too many arguments\n"));
6388 * We have an argument, but it may be a full path or a ZFS
6389 * filesystem. Pass full paths off to unmount_path() (shared by
6390 * manual_unmount), otherwise open the filesystem and pass to
6393 if (argv[0][0] == '/')
6394 return (unshare_unmount_path(op, argv[0],
6397 if ((zhp = zfs_open(g_zfs, argv[0],
6398 ZFS_TYPE_FILESYSTEM)) == NULL)
6401 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6402 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6403 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6404 NULL, 0, B_FALSE) == 0);
6408 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6410 sizeof (nfs_mnt_prop),
6411 NULL, NULL, 0, B_FALSE) == 0);
6412 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6413 sharesmb, sizeof (sharesmb), NULL, NULL,
6416 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6417 strcmp(sharesmb, "off") == 0) {
6418 (void) fprintf(stderr, gettext("cannot "
6419 "unshare '%s': legacy share\n"),
6422 (void) fprintf(stderr, gettext("use "
6423 "unshare(1M) to unshare this "
6427 } else if (!zfs_is_shared(zhp)) {
6428 (void) fprintf(stderr, gettext("cannot "
6429 "unshare '%s': not currently "
6430 "shared\n"), zfs_get_name(zhp));
6432 } else if (zfs_unshareall(zhp) != 0) {
6438 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6439 (void) fprintf(stderr, gettext("cannot "
6440 "unmount '%s': legacy "
6441 "mountpoint\n"), zfs_get_name(zhp));
6442 (void) fprintf(stderr, gettext("use "
6443 "umount(8) to unmount this "
6446 } else if (!zfs_is_mounted(zhp, NULL)) {
6447 (void) fprintf(stderr, gettext("cannot "
6448 "unmount '%s': not currently "
6452 } else if (zfs_unmountall(zhp, flags) != 0) {
6466 * zfs unmount filesystem
6468 * Unmount all filesystems, or a specific ZFS filesystem.
6471 zfs_do_unmount(int argc, char **argv)
6473 return (unshare_unmount(OP_MOUNT, argc, argv));
6478 * zfs unshare filesystem
6480 * Unshare all filesystems, or a specific ZFS filesystem.
6483 zfs_do_unshare(int argc, char **argv)
6485 return (unshare_unmount(OP_SHARE, argc, argv));
6489 * Attach/detach the given dataset to/from the given jail
6493 do_jail(int argc, char **argv, int attach)
6498 /* check number of arguments */
6500 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6504 (void) fprintf(stderr, gettext("too many arguments\n"));
6508 jailid = jail_getid(argv[1]);
6510 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6514 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6518 ret = (zfs_jail(zhp, jailid, attach) != 0);
6525 * zfs jail jailid filesystem
6527 * Attach the given dataset to the given jail
6531 zfs_do_jail(int argc, char **argv)
6534 return (do_jail(argc, argv, 1));
6538 * zfs unjail jailid filesystem
6540 * Detach the given dataset from the given jail
6544 zfs_do_unjail(int argc, char **argv)
6547 return (do_jail(argc, argv, 0));
6551 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6552 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6555 manual_mount(int argc, char **argv)
6558 char mountpoint[ZFS_MAXPROPLEN];
6559 char mntopts[MNT_LINE_MAX] = { '\0' };
6563 char *dataset, *path;
6566 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6569 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6572 flags |= MS_OVERLAY;
6575 flags |= MS_NOMNTTAB;
6578 (void) fprintf(stderr, gettext("missing argument for "
6579 "'%c' option\n"), optopt);
6583 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6585 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6594 /* check that we only have two arguments */
6597 (void) fprintf(stderr, gettext("missing dataset "
6600 (void) fprintf(stderr,
6601 gettext("missing mountpoint argument\n"));
6603 (void) fprintf(stderr, gettext("too many arguments\n"));
6604 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6611 /* try to open the dataset */
6612 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6615 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6616 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6618 /* check for legacy mountpoint and complain appropriately */
6620 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6621 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6622 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6623 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6628 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6629 "mounted using 'mount -t zfs'\n"), dataset);
6630 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6631 "instead.\n"), path);
6632 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6633 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6634 (void) fprintf(stderr, gettext("See zfs(8) for more "
6643 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6644 * unmounts of non-legacy filesystems, as this is the dominant administrative
6648 manual_unmount(int argc, char **argv)
6654 while ((c = getopt(argc, argv, "f")) != -1) {
6660 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6662 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6671 /* check arguments */
6674 (void) fprintf(stderr, gettext("missing path "
6677 (void) fprintf(stderr, gettext("too many arguments\n"));
6678 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6682 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6686 find_command_idx(char *command, int *idx)
6690 for (i = 0; i < NCOMMAND; i++) {
6691 if (command_table[i].name == NULL)
6694 if (strcmp(command, command_table[i].name) == 0) {
6703 zfs_do_diff(int argc, char **argv)
6707 char *tosnap = NULL;
6708 char *fromsnap = NULL;
6713 while ((c = getopt(argc, argv, "FHt")) != -1) {
6716 flags |= ZFS_DIFF_CLASSIFY;
6719 flags |= ZFS_DIFF_PARSEABLE;
6722 flags |= ZFS_DIFF_TIMESTAMP;
6725 (void) fprintf(stderr,
6726 gettext("invalid option '%c'\n"), optopt);
6735 (void) fprintf(stderr,
6736 gettext("must provide at least one snapshot name\n"));
6741 (void) fprintf(stderr, gettext("too many arguments\n"));
6746 tosnap = (argc == 2) ? argv[1] : NULL;
6749 if (*fromsnap != '@')
6750 copy = strdup(fromsnap);
6752 copy = strdup(tosnap);
6756 if (atp = strchr(copy, '@'))
6759 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6765 * Ignore SIGPIPE so that the library can give us
6766 * information on any failure
6768 (void) sigignore(SIGPIPE);
6770 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6778 * zfs bookmark <fs@snap> <fs#bmark>
6780 * Creates a bookmark with the given name from the given snapshot.
6783 zfs_do_bookmark(int argc, char **argv)
6785 char snapname[ZFS_MAXNAMELEN];
6792 while ((c = getopt(argc, argv, "")) != -1) {
6795 (void) fprintf(stderr,
6796 gettext("invalid option '%c'\n"), optopt);
6804 /* check number of arguments */
6806 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6810 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6814 if (strchr(argv[1], '#') == NULL) {
6815 (void) fprintf(stderr,
6816 gettext("invalid bookmark name '%s' -- "
6817 "must contain a '#'\n"), argv[1]);
6821 if (argv[0][0] == '@') {
6823 * Snapshot name begins with @.
6824 * Default to same fs as bookmark.
6826 (void) strncpy(snapname, argv[1], sizeof (snapname));
6827 *strchr(snapname, '#') = '\0';
6828 (void) strlcat(snapname, argv[0], sizeof (snapname));
6830 (void) strncpy(snapname, argv[0], sizeof (snapname));
6832 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6838 nvl = fnvlist_alloc();
6839 fnvlist_add_string(nvl, argv[1], snapname);
6840 ret = lzc_bookmark(nvl, NULL);
6844 const char *err_msg;
6847 (void) snprintf(errbuf, sizeof (errbuf),
6848 dgettext(TEXT_DOMAIN,
6849 "cannot create bookmark '%s'"), argv[1]);
6853 err_msg = "bookmark is in a different pool";
6856 err_msg = "bookmark exists";
6859 err_msg = "invalid argument";
6862 err_msg = "bookmark feature not enabled";
6865 err_msg = "out of space";
6868 err_msg = "unknown error";
6871 (void) fprintf(stderr, "%s: %s\n", errbuf,
6872 dgettext(TEXT_DOMAIN, err_msg));
6883 main(int argc, char **argv)
6890 (void) setlocale(LC_ALL, "");
6891 (void) textdomain(TEXT_DOMAIN);
6895 if ((g_zfs = libzfs_init()) == NULL) {
6896 (void) fprintf(stderr, gettext("internal error: failed to "
6897 "initialize ZFS library\n"));
6901 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6903 libzfs_print_on_error(g_zfs, B_TRUE);
6905 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6906 (void) fprintf(stderr, gettext("internal error: unable to "
6907 "open %s\n"), MNTTAB);
6912 * This command also doubles as the /etc/fs mount and unmount program.
6913 * Determine if we should take this behavior based on argv[0].
6915 progname = basename(argv[0]);
6916 if (strcmp(progname, "mount") == 0) {
6917 ret = manual_mount(argc, argv);
6918 } else if (strcmp(progname, "umount") == 0) {
6919 ret = manual_unmount(argc, argv);
6922 * Make sure the user has specified some command.
6925 (void) fprintf(stderr, gettext("missing command\n"));
6932 * The 'umount' command is an alias for 'unmount'
6934 if (strcmp(cmdname, "umount") == 0)
6935 cmdname = "unmount";
6938 * The 'recv' command is an alias for 'receive'
6940 if (strcmp(cmdname, "recv") == 0)
6941 cmdname = "receive";
6944 * The 'snap' command is an alias for 'snapshot'
6946 if (strcmp(cmdname, "snap") == 0)
6947 cmdname = "snapshot";
6952 if (strcmp(cmdname, "-?") == 0)
6956 * Run the appropriate command.
6958 libzfs_mnttab_cache(g_zfs, B_TRUE);
6959 if (find_command_idx(cmdname, &i) == 0) {
6960 current_command = &command_table[i];
6961 ret = command_table[i].func(argc - 1, argv + 1);
6962 } else if (strchr(cmdname, '=') != NULL) {
6963 verify(find_command_idx("set", &i) == 0);
6964 current_command = &command_table[i];
6965 ret = command_table[i].func(argc, argv);
6967 (void) fprintf(stderr, gettext("unrecognized "
6968 "command '%s'\n"), cmdname);
6971 libzfs_mnttab_cache(g_zfs, B_FALSE);
6974 (void) fclose(mnttab_file);
6976 if (ret == 0 && log_history)
6977 (void) zpool_log_history(g_zfs, history_str);
6982 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6983 * for the purposes of running ::findleaks.
6985 if (getenv("ZFS_ABORT") != NULL) {
6986 (void) printf("dumping core by request\n");