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>
76 #include "zfs_comutil.h"
78 libzfs_handle_t *g_zfs;
80 static FILE *mnttab_file;
81 static char history_str[HIS_MAX_RECORD_LEN];
82 static boolean_t log_history = B_TRUE;
84 static int zfs_do_clone(int argc, char **argv);
85 static int zfs_do_create(int argc, char **argv);
86 static int zfs_do_destroy(int argc, char **argv);
87 static int zfs_do_get(int argc, char **argv);
88 static int zfs_do_inherit(int argc, char **argv);
89 static int zfs_do_list(int argc, char **argv);
90 static int zfs_do_mount(int argc, char **argv);
91 static int zfs_do_rename(int argc, char **argv);
92 static int zfs_do_rollback(int argc, char **argv);
93 static int zfs_do_set(int argc, char **argv);
94 static int zfs_do_upgrade(int argc, char **argv);
95 static int zfs_do_snapshot(int argc, char **argv);
96 static int zfs_do_unmount(int argc, char **argv);
97 static int zfs_do_share(int argc, char **argv);
98 static int zfs_do_unshare(int argc, char **argv);
99 static int zfs_do_send(int argc, char **argv);
100 static int zfs_do_receive(int argc, char **argv);
101 static int zfs_do_promote(int argc, char **argv);
102 static int zfs_do_userspace(int argc, char **argv);
103 static int zfs_do_allow(int argc, char **argv);
104 static int zfs_do_unallow(int argc, char **argv);
105 static int zfs_do_hold(int argc, char **argv);
106 static int zfs_do_holds(int argc, char **argv);
107 static int zfs_do_release(int argc, char **argv);
108 static int zfs_do_diff(int argc, char **argv);
109 static int zfs_do_jail(int argc, char **argv);
110 static int zfs_do_unjail(int argc, char **argv);
111 static int zfs_do_bookmark(int argc, char **argv);
114 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
119 _umem_debug_init(void)
121 return ("default,verbose"); /* $UMEM_DEBUG setting */
125 _umem_logging_init(void)
127 return ("fail,contents"); /* $UMEM_LOGGING setting */
163 typedef struct zfs_command {
165 int (*func)(int argc, char **argv);
170 * Master command table. Each ZFS command has a name, associated function, and
171 * usage message. The usage messages need to be internationalized, so we have
172 * to have a function to return the usage message based on a command index.
174 * These commands are organized according to how they are displayed in the usage
175 * message. An empty command (one with a NULL name) indicates an empty line in
176 * the generic usage message.
178 static zfs_command_t command_table[] = {
179 { "create", zfs_do_create, HELP_CREATE },
180 { "destroy", zfs_do_destroy, HELP_DESTROY },
182 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
183 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
184 { "clone", zfs_do_clone, HELP_CLONE },
185 { "promote", zfs_do_promote, HELP_PROMOTE },
186 { "rename", zfs_do_rename, HELP_RENAME },
187 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
189 { "list", zfs_do_list, HELP_LIST },
191 { "set", zfs_do_set, HELP_SET },
192 { "get", zfs_do_get, HELP_GET },
193 { "inherit", zfs_do_inherit, HELP_INHERIT },
194 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
195 { "userspace", zfs_do_userspace, HELP_USERSPACE },
196 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
198 { "mount", zfs_do_mount, HELP_MOUNT },
199 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
200 { "share", zfs_do_share, HELP_SHARE },
201 { "unshare", zfs_do_unshare, HELP_UNSHARE },
203 { "send", zfs_do_send, HELP_SEND },
204 { "receive", zfs_do_receive, HELP_RECEIVE },
206 { "allow", zfs_do_allow, HELP_ALLOW },
208 { "unallow", zfs_do_unallow, HELP_UNALLOW },
210 { "hold", zfs_do_hold, HELP_HOLD },
211 { "holds", zfs_do_holds, HELP_HOLDS },
212 { "release", zfs_do_release, HELP_RELEASE },
213 { "diff", zfs_do_diff, HELP_DIFF },
215 { "jail", zfs_do_jail, HELP_JAIL },
216 { "unjail", zfs_do_unjail, HELP_UNJAIL },
219 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
221 zfs_command_t *current_command;
224 get_usage(zfs_help_t idx)
228 return (gettext("\tclone [-p] [-o property=value] ... "
229 "<snapshot> <filesystem|volume>\n"));
231 return (gettext("\tcreate [-pu] [-o property=value] ... "
233 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
234 "-V <size> <volume>\n"));
236 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
237 "\tdestroy [-dnpRrv] "
238 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
239 "\tdestroy <filesystem|volume>#<bookmark>\n"));
241 return (gettext("\tget [-rHp] [-d max] "
242 "[-o \"all\" | field[,...]]\n"
243 "\t [-t type[,...]] [-s source[,...]]\n"
244 "\t <\"all\" | property[,...]> "
245 "[filesystem|volume|snapshot] ...\n"));
247 return (gettext("\tinherit [-rS] <property> "
248 "<filesystem|volume|snapshot> ...\n"));
250 return (gettext("\tupgrade [-v]\n"
251 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
253 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
255 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
257 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
258 "[-s property]...\n\t [-S property]... [-t type[,...]] "
259 "[filesystem|volume|snapshot] ...\n"));
261 return (gettext("\tmount\n"
262 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
264 return (gettext("\tpromote <clone-filesystem>\n"));
266 return (gettext("\treceive|recv [-vnFu] <filesystem|volume|"
268 "\treceive|recv [-vnFu] [-d | -e] <filesystem>\n"));
270 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
271 "<filesystem|volume|snapshot>\n"
272 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
273 "\trename -r <snapshot> <snapshot>\n"
274 "\trename -u [-p] <filesystem> <filesystem>"));
276 return (gettext("\trollback [-rRf] <snapshot>\n"));
278 return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] "
280 "\tsend [-Le] [-i snapshot|bookmark] "
281 "<filesystem|volume|snapshot>\n"));
283 return (gettext("\tset <property=value> "
284 "<filesystem|volume|snapshot> ...\n"));
286 return (gettext("\tshare <-a | filesystem>\n"));
288 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
289 "<filesystem|volume>@<snap> ...\n"));
291 return (gettext("\tunmount|umount [-f] "
292 "<-a | filesystem|mountpoint>\n"));
294 return (gettext("\tunshare "
295 "<-a | filesystem|mountpoint>\n"));
297 return (gettext("\tallow <filesystem|volume>\n"
299 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
300 "\t <filesystem|volume>\n"
301 "\tallow [-ld] -e <perm|@setname>[,...] "
302 "<filesystem|volume>\n"
303 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
304 "\tallow -s @setname <perm|@setname>[,...] "
305 "<filesystem|volume>\n"));
307 return (gettext("\tunallow [-rldug] "
308 "<\"everyone\"|user|group>[,...]\n"
309 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
310 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
311 "<filesystem|volume>\n"
312 "\tunallow [-r] -c [<perm|@setname>[,...]] "
313 "<filesystem|volume>\n"
314 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
315 "<filesystem|volume>\n"));
317 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
319 "\t [-S field] ... [-t type[,...]] "
320 "<filesystem|snapshot>\n"));
321 case HELP_GROUPSPACE:
322 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
324 "\t [-S field] ... [-t type[,...]] "
325 "<filesystem|snapshot>\n"));
327 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
329 return (gettext("\tholds [-r] <snapshot> ...\n"));
331 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
333 return (gettext("\tdiff [-FHt] <snapshot> "
334 "[snapshot|filesystem]\n"));
336 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
346 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
351 * Utility function to guarantee malloc() success.
355 safe_malloc(size_t size)
359 if ((data = calloc(1, size)) == NULL)
366 safe_strdup(char *str)
368 char *dupstr = strdup(str);
377 * Callback routine that will print out information for each of
381 usage_prop_cb(int prop, void *cb)
385 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
387 if (zfs_prop_readonly(prop))
388 (void) fprintf(fp, " NO ");
390 (void) fprintf(fp, "YES ");
392 if (zfs_prop_inheritable(prop))
393 (void) fprintf(fp, " YES ");
395 (void) fprintf(fp, " NO ");
397 if (zfs_prop_values(prop) == NULL)
398 (void) fprintf(fp, "-\n");
400 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
406 * Display usage message. If we're inside a command, display only the usage for
407 * that command. Otherwise, iterate over the entire command table and display
408 * a complete usage message.
411 usage(boolean_t requested)
414 boolean_t show_properties = B_FALSE;
415 FILE *fp = requested ? stdout : stderr;
417 if (current_command == NULL) {
419 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
421 gettext("where 'command' is one of the following:\n\n"));
423 for (i = 0; i < NCOMMAND; i++) {
424 if (command_table[i].name == NULL)
425 (void) fprintf(fp, "\n");
427 (void) fprintf(fp, "%s",
428 get_usage(command_table[i].usage));
431 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
432 "pool/[dataset/]*dataset[@name]\n"));
434 (void) fprintf(fp, gettext("usage:\n"));
435 (void) fprintf(fp, "%s", get_usage(current_command->usage));
438 if (current_command != NULL &&
439 (strcmp(current_command->name, "set") == 0 ||
440 strcmp(current_command->name, "get") == 0 ||
441 strcmp(current_command->name, "inherit") == 0 ||
442 strcmp(current_command->name, "list") == 0))
443 show_properties = B_TRUE;
445 if (show_properties) {
447 gettext("\nThe following properties are supported:\n"));
449 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
450 "PROPERTY", "EDIT", "INHERIT", "VALUES");
452 /* Iterate over all properties */
453 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
456 (void) fprintf(fp, "\t%-15s ", "userused@...");
457 (void) fprintf(fp, " NO NO <size>\n");
458 (void) fprintf(fp, "\t%-15s ", "groupused@...");
459 (void) fprintf(fp, " NO NO <size>\n");
460 (void) fprintf(fp, "\t%-15s ", "userquota@...");
461 (void) fprintf(fp, "YES NO <size> | none\n");
462 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
463 (void) fprintf(fp, "YES NO <size> | none\n");
464 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
465 (void) fprintf(fp, " NO NO <size>\n");
467 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
468 "with standard units such as K, M, G, etc.\n"));
469 (void) fprintf(fp, gettext("\nUser-defined properties can "
470 "be specified by using a name containing a colon (:).\n"));
471 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
472 "properties must be appended with\n"
473 "a user or group specifier of one of these forms:\n"
474 " POSIX name (eg: \"matt\")\n"
475 " POSIX id (eg: \"126829\")\n"
476 " SMB name@domain (eg: \"matt@sun\")\n"
477 " SMB SID (eg: \"S-1-234-567-89\")\n"));
480 gettext("\nFor the property list, run: %s\n"),
483 gettext("\nFor the delegated permission list, run: %s\n"),
484 "zfs allow|unallow");
488 * See comments at end of main().
490 if (getenv("ZFS_ABORT") != NULL) {
491 (void) printf("dumping core by request\n");
495 exit(requested ? 0 : 2);
499 parseprop(nvlist_t *props, char *propname)
501 char *propval, *strval;
503 if ((propval = strchr(propname, '=')) == NULL) {
504 (void) fprintf(stderr, gettext("missing "
505 "'=' for -o option\n"));
510 if (nvlist_lookup_string(props, propname, &strval) == 0) {
511 (void) fprintf(stderr, gettext("property '%s' "
512 "specified multiple times\n"), propname);
515 if (nvlist_add_string(props, propname, propval) != 0)
521 parse_depth(char *opt, int *flags)
526 depth = (int)strtol(opt, &tmp, 0);
528 (void) fprintf(stderr,
529 gettext("%s is not an integer\n"), opt);
533 (void) fprintf(stderr,
534 gettext("Depth can not be negative.\n"));
537 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
541 #define PROGRESS_DELAY 2 /* seconds */
543 static char *pt_reverse = "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
544 static time_t pt_begin;
545 static char *pt_header = NULL;
546 static boolean_t pt_shown;
549 start_progress_timer(void)
551 pt_begin = time(NULL) + PROGRESS_DELAY;
556 set_progress_header(char *header)
558 assert(pt_header == NULL);
559 pt_header = safe_strdup(header);
561 (void) printf("%s: ", header);
562 (void) fflush(stdout);
567 update_progress(char *update)
569 if (!pt_shown && time(NULL) > pt_begin) {
570 int len = strlen(update);
572 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
574 (void) fflush(stdout);
576 } else if (pt_shown) {
577 int len = strlen(update);
579 (void) printf("%s%*.*s", update, len, len, pt_reverse);
580 (void) fflush(stdout);
585 finish_progress(char *done)
588 (void) printf("%s\n", done);
589 (void) fflush(stdout);
596 * Check if the dataset is mountable and should be automatically mounted.
599 should_auto_mount(zfs_handle_t *zhp)
601 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp)))
603 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON);
607 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
609 * Given an existing dataset, create a writable copy whose initial contents
610 * are the same as the source. The newly created dataset maintains a
611 * dependency on the original; the original cannot be destroyed so long as
614 * The '-p' flag creates all the non-existing ancestors of the target first.
617 zfs_do_clone(int argc, char **argv)
619 zfs_handle_t *zhp = NULL;
620 boolean_t parents = B_FALSE;
625 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
629 while ((c = getopt(argc, argv, "o:p")) != -1) {
632 if (parseprop(props, optarg))
639 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
648 /* check number of arguments */
650 (void) fprintf(stderr, gettext("missing source dataset "
655 (void) fprintf(stderr, gettext("missing target dataset "
660 (void) fprintf(stderr, gettext("too many arguments\n"));
664 /* open the source dataset */
665 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
668 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
671 * Now create the ancestors of the target dataset. If the
672 * target already exists and '-p' option was used we should not
675 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
678 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
683 ret = zfs_clone(zhp, argv[1], props);
685 /* create the mountpoint if necessary */
689 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
692 * If the user doesn't want the dataset
693 * automatically mounted, then skip the mount/share
696 if (should_auto_mount(clone)) {
697 if ((ret = zfs_mount(clone, NULL, 0)) != 0) {
698 (void) fprintf(stderr, gettext("clone "
699 "successfully created, "
700 "but not mounted\n"));
701 } else if ((ret = zfs_share(clone)) != 0) {
702 (void) fprintf(stderr, gettext("clone "
703 "successfully created, "
704 "but not shared\n"));
725 * zfs create [-pu] [-o prop=value] ... fs
726 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
728 * Create a new dataset. This command can be used to create filesystems
729 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
730 * For volumes, the user must specify a size to be used.
732 * The '-s' flag applies only to volumes, and indicates that we should not try
733 * to set the reservation for this volume. By default we set a reservation
734 * equal to the size for any volume. For pools with SPA_VERSION >=
735 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
737 * The '-p' flag creates all the non-existing ancestors of the target first.
739 * The '-u' flag prevents mounting of newly created file system.
742 zfs_do_create(int argc, char **argv)
744 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
745 zfs_handle_t *zhp = NULL;
748 boolean_t noreserve = B_FALSE;
749 boolean_t bflag = B_FALSE;
750 boolean_t parents = B_FALSE;
751 boolean_t nomount = B_FALSE;
756 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
760 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) {
763 type = ZFS_TYPE_VOLUME;
764 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
765 (void) fprintf(stderr, gettext("bad volume "
766 "size '%s': %s\n"), optarg,
767 libzfs_error_description(g_zfs));
771 if (nvlist_add_uint64(props,
772 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
781 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
782 (void) fprintf(stderr, gettext("bad volume "
783 "block size '%s': %s\n"), optarg,
784 libzfs_error_description(g_zfs));
788 if (nvlist_add_uint64(props,
789 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
794 if (parseprop(props, optarg))
804 (void) fprintf(stderr, gettext("missing size "
808 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
814 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
815 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
816 "used when creating a volume\n"));
819 if (nomount && type != ZFS_TYPE_FILESYSTEM) {
820 (void) fprintf(stderr, gettext("'-u' can only be "
821 "used when creating a file system\n"));
828 /* check number of arguments */
830 (void) fprintf(stderr, gettext("missing %s argument\n"),
831 zfs_type_to_name(type));
835 (void) fprintf(stderr, gettext("too many arguments\n"));
839 if (type == ZFS_TYPE_VOLUME && !noreserve) {
840 zpool_handle_t *zpool_handle;
841 uint64_t spa_version;
843 zfs_prop_t resv_prop;
846 if (p = strchr(argv[0], '/'))
848 zpool_handle = zpool_open(g_zfs, argv[0]);
851 if (zpool_handle == NULL)
853 spa_version = zpool_get_prop_int(zpool_handle,
854 ZPOOL_PROP_VERSION, NULL);
855 zpool_close(zpool_handle);
856 if (spa_version >= SPA_VERSION_REFRESERVATION)
857 resv_prop = ZFS_PROP_REFRESERVATION;
859 resv_prop = ZFS_PROP_RESERVATION;
860 volsize = zvol_volsize_to_reservation(volsize, props);
862 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
864 if (nvlist_add_uint64(props,
865 zfs_prop_to_name(resv_prop), volsize) != 0) {
872 if (parents && zfs_name_valid(argv[0], type)) {
874 * Now create the ancestors of target dataset. If the target
875 * already exists and '-p' option was used we should not
878 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
882 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
887 if (zfs_create(g_zfs, argv[0], type, props) != 0)
890 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
896 * Mount and/or share the new filesystem as appropriate. We provide a
897 * verbose error message to let the user know that their filesystem was
898 * in fact created, even if we failed to mount or share it.
899 * If the user doesn't want the dataset automatically mounted,
900 * then skip the mount/share step altogether.
902 if (!nomount && should_auto_mount(zhp)) {
903 if (zfs_mount(zhp, NULL, 0) != 0) {
904 (void) fprintf(stderr, gettext("filesystem "
905 "successfully created, but not mounted\n"));
907 } else if (zfs_share(zhp) != 0) {
908 (void) fprintf(stderr, gettext("filesystem "
909 "successfully created, but not shared\n"));
926 * zfs destroy [-rRf] <fs, vol>
927 * zfs destroy [-rRd] <snap>
929 * -r Recursively destroy all children
930 * -R Recursively destroy all dependents, including clones
931 * -f Force unmounting of any dependents
932 * -d If we can't destroy now, mark for deferred destruction
934 * Destroys the given dataset. By default, it will unmount any filesystems,
935 * and refuse to destroy a dataset that has any dependents. A dependent can
936 * either be a child, or a clone of a child.
938 typedef struct destroy_cbdata {
941 boolean_t cb_recurse;
943 boolean_t cb_doclones;
944 zfs_handle_t *cb_target;
945 boolean_t cb_defer_destroy;
946 boolean_t cb_verbose;
947 boolean_t cb_parsable;
950 nvlist_t *cb_batchedsnaps;
952 /* first snap in contiguous run */
954 /* previous snap in contiguous run */
962 * Check for any dependents based on the '-r' or '-R' flags.
965 destroy_check_dependent(zfs_handle_t *zhp, void *data)
967 destroy_cbdata_t *cbp = data;
968 const char *tname = zfs_get_name(cbp->cb_target);
969 const char *name = zfs_get_name(zhp);
971 if (strncmp(tname, name, strlen(tname)) == 0 &&
972 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
974 * This is a direct descendant, not a clone somewhere else in
981 (void) fprintf(stderr, gettext("cannot destroy '%s': "
982 "%s has children\n"),
983 zfs_get_name(cbp->cb_target),
984 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
985 (void) fprintf(stderr, gettext("use '-r' to destroy "
986 "the following datasets:\n"));
987 cbp->cb_first = B_FALSE;
988 cbp->cb_error = B_TRUE;
991 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
994 * This is a clone. We only want to report this if the '-r'
995 * wasn't specified, or the target is a snapshot.
997 if (!cbp->cb_recurse &&
998 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1001 if (cbp->cb_first) {
1002 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1003 "%s has dependent clones\n"),
1004 zfs_get_name(cbp->cb_target),
1005 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1006 (void) fprintf(stderr, gettext("use '-R' to destroy "
1007 "the following datasets:\n"));
1008 cbp->cb_first = B_FALSE;
1009 cbp->cb_error = B_TRUE;
1010 cbp->cb_dryrun = B_TRUE;
1013 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1022 destroy_callback(zfs_handle_t *zhp, void *data)
1024 destroy_cbdata_t *cb = data;
1025 const char *name = zfs_get_name(zhp);
1027 if (cb->cb_verbose) {
1028 if (cb->cb_parsable) {
1029 (void) printf("destroy\t%s\n", name);
1030 } else if (cb->cb_dryrun) {
1031 (void) printf(gettext("would destroy %s\n"),
1034 (void) printf(gettext("will destroy %s\n"),
1040 * Ignore pools (which we've already flagged as an error before getting
1043 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1044 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1048 if (cb->cb_dryrun) {
1054 * We batch up all contiguous snapshots (even of different
1055 * filesystems) and destroy them with one ioctl. We can't
1056 * simply do all snap deletions and then all fs deletions,
1057 * because we must delete a clone before its origin.
1059 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1060 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1062 int error = zfs_destroy_snaps_nvl(g_zfs,
1063 cb->cb_batchedsnaps, B_FALSE);
1064 fnvlist_free(cb->cb_batchedsnaps);
1065 cb->cb_batchedsnaps = fnvlist_alloc();
1068 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1069 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1080 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1082 destroy_cbdata_t *cb = arg;
1083 const char *name = zfs_get_name(zhp);
1086 if (nvlist_exists(cb->cb_nvl, name)) {
1087 if (cb->cb_firstsnap == NULL)
1088 cb->cb_firstsnap = strdup(name);
1089 if (cb->cb_prevsnap != NULL)
1090 free(cb->cb_prevsnap);
1091 /* this snap continues the current range */
1092 cb->cb_prevsnap = strdup(name);
1093 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1095 if (cb->cb_verbose) {
1096 if (cb->cb_parsable) {
1097 (void) printf("destroy\t%s\n", name);
1098 } else if (cb->cb_dryrun) {
1099 (void) printf(gettext("would destroy %s\n"),
1102 (void) printf(gettext("will destroy %s\n"),
1106 } else if (cb->cb_firstsnap != NULL) {
1107 /* end of this range */
1109 err = lzc_snaprange_space(cb->cb_firstsnap,
1110 cb->cb_prevsnap, &used);
1111 cb->cb_snapused += used;
1112 free(cb->cb_firstsnap);
1113 cb->cb_firstsnap = NULL;
1114 free(cb->cb_prevsnap);
1115 cb->cb_prevsnap = NULL;
1122 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1125 assert(cb->cb_firstsnap == NULL);
1126 assert(cb->cb_prevsnap == NULL);
1127 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1128 if (cb->cb_firstsnap != NULL) {
1131 err = lzc_snaprange_space(cb->cb_firstsnap,
1132 cb->cb_prevsnap, &used);
1134 cb->cb_snapused += used;
1135 free(cb->cb_firstsnap);
1136 cb->cb_firstsnap = NULL;
1137 free(cb->cb_prevsnap);
1138 cb->cb_prevsnap = NULL;
1144 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1146 destroy_cbdata_t *cb = arg;
1149 /* Check for clones. */
1150 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1151 cb->cb_target = zhp;
1152 cb->cb_first = B_TRUE;
1153 err = zfs_iter_dependents(zhp, B_TRUE,
1154 destroy_check_dependent, cb);
1158 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1166 gather_snapshots(zfs_handle_t *zhp, void *arg)
1168 destroy_cbdata_t *cb = arg;
1171 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1177 if (cb->cb_verbose) {
1178 err = destroy_print_snapshots(zhp, cb);
1184 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1192 destroy_clones(destroy_cbdata_t *cb)
1195 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1197 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1198 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1201 boolean_t defer = cb->cb_defer_destroy;
1205 * We can't defer destroy non-snapshots, so set it to
1206 * false while destroying the clones.
1208 cb->cb_defer_destroy = B_FALSE;
1209 err = zfs_iter_dependents(zhp, B_FALSE,
1210 destroy_callback, cb);
1211 cb->cb_defer_destroy = defer;
1221 zfs_do_destroy(int argc, char **argv)
1223 destroy_cbdata_t cb = { 0 };
1227 zfs_handle_t *zhp = NULL;
1229 zfs_type_t type = ZFS_TYPE_DATASET;
1232 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1235 cb.cb_verbose = B_TRUE;
1238 cb.cb_verbose = B_TRUE;
1239 cb.cb_parsable = B_TRUE;
1242 cb.cb_dryrun = B_TRUE;
1245 cb.cb_defer_destroy = B_TRUE;
1246 type = ZFS_TYPE_SNAPSHOT;
1249 cb.cb_force = B_TRUE;
1252 cb.cb_recurse = B_TRUE;
1255 cb.cb_recurse = B_TRUE;
1256 cb.cb_doclones = B_TRUE;
1260 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1269 /* check number of arguments */
1271 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1275 (void) fprintf(stderr, gettext("too many arguments\n"));
1279 at = strchr(argv[0], '@');
1280 pound = strchr(argv[0], '#');
1283 /* Build the list of snaps to destroy in cb_nvl. */
1284 cb.cb_nvl = fnvlist_alloc();
1287 zhp = zfs_open(g_zfs, argv[0],
1288 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1292 cb.cb_snapspec = at + 1;
1293 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1299 if (nvlist_empty(cb.cb_nvl)) {
1300 (void) fprintf(stderr, gettext("could not find any "
1301 "snapshots to destroy; check snapshot names.\n"));
1306 if (cb.cb_verbose) {
1308 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1309 if (cb.cb_parsable) {
1310 (void) printf("reclaim\t%llu\n",
1312 } else if (cb.cb_dryrun) {
1313 (void) printf(gettext("would reclaim %s\n"),
1316 (void) printf(gettext("will reclaim %s\n"),
1321 if (!cb.cb_dryrun) {
1322 if (cb.cb_doclones) {
1323 cb.cb_batchedsnaps = fnvlist_alloc();
1324 err = destroy_clones(&cb);
1326 err = zfs_destroy_snaps_nvl(g_zfs,
1327 cb.cb_batchedsnaps, B_FALSE);
1335 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1336 cb.cb_defer_destroy);
1342 } else if (pound != NULL) {
1347 (void) fprintf(stderr,
1348 "dryrun is not supported with bookmark\n");
1352 if (cb.cb_defer_destroy) {
1353 (void) fprintf(stderr,
1354 "defer destroy is not supported with bookmark\n");
1358 if (cb.cb_recurse) {
1359 (void) fprintf(stderr,
1360 "recursive is not supported with bookmark\n");
1364 if (!zfs_bookmark_exists(argv[0])) {
1365 (void) fprintf(stderr, gettext("bookmark '%s' "
1366 "does not exist.\n"), argv[0]);
1370 nvl = fnvlist_alloc();
1371 fnvlist_add_boolean(nvl, argv[0]);
1373 err = lzc_destroy_bookmarks(nvl, NULL);
1375 (void) zfs_standard_error(g_zfs, err,
1376 "cannot destroy bookmark");
1379 nvlist_free(cb.cb_nvl);
1383 /* Open the given dataset */
1384 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1390 * Perform an explicit check for pools before going any further.
1392 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1393 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1394 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1395 "operation does not apply to pools\n"),
1397 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1398 "%s' to destroy all datasets in the pool\n"),
1400 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1401 "to destroy the pool itself\n"), zfs_get_name(zhp));
1407 * Check for any dependents and/or clones.
1409 cb.cb_first = B_TRUE;
1410 if (!cb.cb_doclones &&
1411 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1422 cb.cb_batchedsnaps = fnvlist_alloc();
1423 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1430 * Do the real thing. The callback will close the
1431 * handle regardless of whether it succeeds or not.
1433 err = destroy_callback(zhp, &cb);
1436 err = zfs_destroy_snaps_nvl(g_zfs,
1437 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1444 fnvlist_free(cb.cb_batchedsnaps);
1445 fnvlist_free(cb.cb_nvl);
1452 is_recvd_column(zprop_get_cbdata_t *cbp)
1455 zfs_get_column_t col;
1457 for (i = 0; i < ZFS_GET_NCOLS &&
1458 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1459 if (col == GET_COL_RECVD)
1465 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1466 * < all | property[,property]... > < fs | snap | vol > ...
1468 * -r recurse over any child datasets
1469 * -H scripted mode. Headers are stripped, and fields are separated
1470 * by tabs instead of spaces.
1471 * -o Set of fields to display. One of "name,property,value,
1472 * received,source". Default is "name,property,value,source".
1473 * "all" is an alias for all five.
1474 * -s Set of sources to allow. One of
1475 * "local,default,inherited,received,temporary,none". Default is
1477 * -p Display values in parsable (literal) format.
1479 * Prints properties for the given datasets. The user can control which
1480 * columns to display as well as which property types to allow.
1484 * Invoked to display the properties for a single dataset.
1487 get_callback(zfs_handle_t *zhp, void *data)
1489 char buf[ZFS_MAXPROPLEN];
1490 char rbuf[ZFS_MAXPROPLEN];
1491 zprop_source_t sourcetype;
1492 char source[ZFS_MAXNAMELEN];
1493 zprop_get_cbdata_t *cbp = data;
1494 nvlist_t *user_props = zfs_get_user_props(zhp);
1495 zprop_list_t *pl = cbp->cb_proplist;
1499 boolean_t received = is_recvd_column(cbp);
1501 for (; pl != NULL; pl = pl->pl_next) {
1502 char *recvdval = NULL;
1504 * Skip the special fake placeholder. This will also skip over
1505 * the name property when 'all' is specified.
1507 if (pl->pl_prop == ZFS_PROP_NAME &&
1508 pl == cbp->cb_proplist)
1511 if (pl->pl_prop != ZPROP_INVAL) {
1512 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1513 sizeof (buf), &sourcetype, source,
1515 cbp->cb_literal) != 0) {
1518 if (!zfs_prop_valid_for_type(pl->pl_prop,
1519 ZFS_TYPE_DATASET)) {
1520 (void) fprintf(stderr,
1521 gettext("No such property '%s'\n"),
1522 zfs_prop_to_name(pl->pl_prop));
1525 sourcetype = ZPROP_SRC_NONE;
1526 (void) strlcpy(buf, "-", sizeof (buf));
1529 if (received && (zfs_prop_get_recvd(zhp,
1530 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1531 cbp->cb_literal) == 0))
1534 zprop_print_one_property(zfs_get_name(zhp), cbp,
1535 zfs_prop_to_name(pl->pl_prop),
1536 buf, sourcetype, source, recvdval);
1537 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1538 sourcetype = ZPROP_SRC_LOCAL;
1540 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1541 buf, sizeof (buf), cbp->cb_literal) != 0) {
1542 sourcetype = ZPROP_SRC_NONE;
1543 (void) strlcpy(buf, "-", sizeof (buf));
1546 zprop_print_one_property(zfs_get_name(zhp), cbp,
1547 pl->pl_user_prop, buf, sourcetype, source, NULL);
1548 } else if (zfs_prop_written(pl->pl_user_prop)) {
1549 sourcetype = ZPROP_SRC_LOCAL;
1551 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1552 buf, sizeof (buf), cbp->cb_literal) != 0) {
1553 sourcetype = ZPROP_SRC_NONE;
1554 (void) strlcpy(buf, "-", sizeof (buf));
1557 zprop_print_one_property(zfs_get_name(zhp), cbp,
1558 pl->pl_user_prop, buf, sourcetype, source, NULL);
1560 if (nvlist_lookup_nvlist(user_props,
1561 pl->pl_user_prop, &propval) != 0) {
1564 sourcetype = ZPROP_SRC_NONE;
1567 verify(nvlist_lookup_string(propval,
1568 ZPROP_VALUE, &strval) == 0);
1569 verify(nvlist_lookup_string(propval,
1570 ZPROP_SOURCE, &sourceval) == 0);
1572 if (strcmp(sourceval,
1573 zfs_get_name(zhp)) == 0) {
1574 sourcetype = ZPROP_SRC_LOCAL;
1575 } else if (strcmp(sourceval,
1576 ZPROP_SOURCE_VAL_RECVD) == 0) {
1577 sourcetype = ZPROP_SRC_RECEIVED;
1579 sourcetype = ZPROP_SRC_INHERITED;
1580 (void) strlcpy(source,
1581 sourceval, sizeof (source));
1585 if (received && (zfs_prop_get_recvd(zhp,
1586 pl->pl_user_prop, rbuf, sizeof (rbuf),
1587 cbp->cb_literal) == 0))
1590 zprop_print_one_property(zfs_get_name(zhp), cbp,
1591 pl->pl_user_prop, strval, sourcetype,
1600 zfs_do_get(int argc, char **argv)
1602 zprop_get_cbdata_t cb = { 0 };
1603 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1604 int types = ZFS_TYPE_DATASET;
1605 char *value, *fields;
1608 zprop_list_t fake_name = { 0 };
1611 * Set up default columns and sources.
1613 cb.cb_sources = ZPROP_SRC_ALL;
1614 cb.cb_columns[0] = GET_COL_NAME;
1615 cb.cb_columns[1] = GET_COL_PROPERTY;
1616 cb.cb_columns[2] = GET_COL_VALUE;
1617 cb.cb_columns[3] = GET_COL_SOURCE;
1618 cb.cb_type = ZFS_TYPE_DATASET;
1621 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1624 cb.cb_literal = B_TRUE;
1627 limit = parse_depth(optarg, &flags);
1630 flags |= ZFS_ITER_RECURSE;
1633 cb.cb_scripted = B_TRUE;
1636 (void) fprintf(stderr, gettext("missing argument for "
1637 "'%c' option\n"), optopt);
1642 * Process the set of columns to display. We zero out
1643 * the structure to give us a blank slate.
1645 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1647 while (*optarg != '\0') {
1648 static char *col_subopts[] =
1649 { "name", "property", "value", "received",
1650 "source", "all", NULL };
1652 if (i == ZFS_GET_NCOLS) {
1653 (void) fprintf(stderr, gettext("too "
1654 "many fields given to -o "
1659 switch (getsubopt(&optarg, col_subopts,
1662 cb.cb_columns[i++] = GET_COL_NAME;
1665 cb.cb_columns[i++] = GET_COL_PROPERTY;
1668 cb.cb_columns[i++] = GET_COL_VALUE;
1671 cb.cb_columns[i++] = GET_COL_RECVD;
1672 flags |= ZFS_ITER_RECVD_PROPS;
1675 cb.cb_columns[i++] = GET_COL_SOURCE;
1679 (void) fprintf(stderr,
1680 gettext("\"all\" conflicts "
1681 "with specific fields "
1682 "given to -o option\n"));
1685 cb.cb_columns[0] = GET_COL_NAME;
1686 cb.cb_columns[1] = GET_COL_PROPERTY;
1687 cb.cb_columns[2] = GET_COL_VALUE;
1688 cb.cb_columns[3] = GET_COL_RECVD;
1689 cb.cb_columns[4] = GET_COL_SOURCE;
1690 flags |= ZFS_ITER_RECVD_PROPS;
1694 (void) fprintf(stderr,
1695 gettext("invalid column name "
1704 while (*optarg != '\0') {
1705 static char *source_subopts[] = {
1706 "local", "default", "inherited",
1707 "received", "temporary", "none",
1710 switch (getsubopt(&optarg, source_subopts,
1713 cb.cb_sources |= ZPROP_SRC_LOCAL;
1716 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1719 cb.cb_sources |= ZPROP_SRC_INHERITED;
1722 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1725 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1728 cb.cb_sources |= ZPROP_SRC_NONE;
1731 (void) fprintf(stderr,
1732 gettext("invalid source "
1741 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1742 while (*optarg != '\0') {
1743 static char *type_subopts[] = { "filesystem",
1744 "volume", "snapshot", "bookmark",
1747 switch (getsubopt(&optarg, type_subopts,
1750 types |= ZFS_TYPE_FILESYSTEM;
1753 types |= ZFS_TYPE_VOLUME;
1756 types |= ZFS_TYPE_SNAPSHOT;
1759 types |= ZFS_TYPE_BOOKMARK;
1762 types = ZFS_TYPE_DATASET |
1767 (void) fprintf(stderr,
1768 gettext("invalid type '%s'\n"),
1776 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1786 (void) fprintf(stderr, gettext("missing property "
1793 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1801 * As part of zfs_expand_proplist(), we keep track of the maximum column
1802 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1803 * need to know the maximum name length. However, the user likely did
1804 * not specify 'name' as one of the properties to fetch, so we need to
1805 * make sure we always include at least this property for
1806 * print_get_headers() to work properly.
1808 if (cb.cb_proplist != NULL) {
1809 fake_name.pl_prop = ZFS_PROP_NAME;
1810 fake_name.pl_width = strlen(gettext("NAME"));
1811 fake_name.pl_next = cb.cb_proplist;
1812 cb.cb_proplist = &fake_name;
1815 cb.cb_first = B_TRUE;
1817 /* run for each object */
1818 ret = zfs_for_each(argc, argv, flags, types, NULL,
1819 &cb.cb_proplist, limit, get_callback, &cb);
1821 if (cb.cb_proplist == &fake_name)
1822 zprop_free_list(fake_name.pl_next);
1824 zprop_free_list(cb.cb_proplist);
1830 * inherit [-rS] <property> <fs|vol> ...
1832 * -r Recurse over all children
1833 * -S Revert to received value, if any
1835 * For each dataset specified on the command line, inherit the given property
1836 * from its parent. Inheriting a property at the pool level will cause it to
1837 * use the default value. The '-r' flag will recurse over all children, and is
1838 * useful for setting a property on a hierarchy-wide basis, regardless of any
1839 * local modifications for each dataset.
1842 typedef struct inherit_cbdata {
1843 const char *cb_propname;
1844 boolean_t cb_received;
1848 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1850 inherit_cbdata_t *cb = data;
1851 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1854 * If we're doing it recursively, then ignore properties that
1855 * are not valid for this type of dataset.
1857 if (prop != ZPROP_INVAL &&
1858 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1861 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1865 inherit_cb(zfs_handle_t *zhp, void *data)
1867 inherit_cbdata_t *cb = data;
1869 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1873 zfs_do_inherit(int argc, char **argv)
1877 inherit_cbdata_t cb = { 0 };
1881 boolean_t received = B_FALSE;
1884 while ((c = getopt(argc, argv, "rS")) != -1) {
1887 flags |= ZFS_ITER_RECURSE;
1894 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1903 /* check number of arguments */
1905 (void) fprintf(stderr, gettext("missing property argument\n"));
1909 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1917 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1918 if (zfs_prop_readonly(prop)) {
1919 (void) fprintf(stderr, gettext(
1920 "%s property is read-only\n"),
1924 if (!zfs_prop_inheritable(prop) && !received) {
1925 (void) fprintf(stderr, gettext("'%s' property cannot "
1926 "be inherited\n"), propname);
1927 if (prop == ZFS_PROP_QUOTA ||
1928 prop == ZFS_PROP_RESERVATION ||
1929 prop == ZFS_PROP_REFQUOTA ||
1930 prop == ZFS_PROP_REFRESERVATION) {
1931 (void) fprintf(stderr, gettext("use 'zfs set "
1932 "%s=none' to clear\n"), propname);
1933 (void) fprintf(stderr, gettext("use 'zfs "
1934 "inherit -S %s' to revert to received "
1935 "value\n"), propname);
1939 if (received && (prop == ZFS_PROP_VOLSIZE ||
1940 prop == ZFS_PROP_VERSION)) {
1941 (void) fprintf(stderr, gettext("'%s' property cannot "
1942 "be reverted to a received value\n"), propname);
1945 } else if (!zfs_prop_user(propname)) {
1946 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1951 cb.cb_propname = propname;
1952 cb.cb_received = received;
1954 if (flags & ZFS_ITER_RECURSE) {
1955 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1956 NULL, NULL, 0, inherit_recurse_cb, &cb);
1958 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1959 NULL, NULL, 0, inherit_cb, &cb);
1965 typedef struct upgrade_cbdata {
1966 uint64_t cb_numupgraded;
1967 uint64_t cb_numsamegraded;
1968 uint64_t cb_numfailed;
1969 uint64_t cb_version;
1971 boolean_t cb_foundone;
1972 char cb_lastfs[ZFS_MAXNAMELEN];
1976 same_pool(zfs_handle_t *zhp, const char *name)
1978 int len1 = strcspn(name, "/@");
1979 const char *zhname = zfs_get_name(zhp);
1980 int len2 = strcspn(zhname, "/@");
1984 return (strncmp(name, zhname, len1) == 0);
1988 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1990 upgrade_cbdata_t *cb = data;
1991 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1993 /* list if it's old/new */
1994 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1995 (cb->cb_newer && version > ZPL_VERSION)) {
1998 str = gettext("The following filesystems are "
1999 "formatted using a newer software version and\n"
2000 "cannot be accessed on the current system.\n\n");
2002 str = gettext("The following filesystems are "
2003 "out of date, and can be upgraded. After being\n"
2004 "upgraded, these filesystems (and any 'zfs send' "
2005 "streams generated from\n"
2006 "subsequent snapshots) will no longer be "
2007 "accessible by older software versions.\n\n");
2010 if (!cb->cb_foundone) {
2012 (void) printf(gettext("VER FILESYSTEM\n"));
2013 (void) printf(gettext("--- ------------\n"));
2014 cb->cb_foundone = B_TRUE;
2017 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2024 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2026 upgrade_cbdata_t *cb = data;
2027 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2028 int needed_spa_version;
2031 if (zfs_spa_version(zhp, &spa_version) < 0)
2034 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2036 if (needed_spa_version < 0)
2039 if (spa_version < needed_spa_version) {
2041 (void) printf(gettext("%s: can not be "
2042 "upgraded; the pool version needs to first "
2043 "be upgraded\nto version %d\n\n"),
2044 zfs_get_name(zhp), needed_spa_version);
2050 if (version < cb->cb_version) {
2052 (void) snprintf(verstr, sizeof (verstr),
2053 "%llu", cb->cb_version);
2054 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2056 * If they did "zfs upgrade -a", then we could
2057 * be doing ioctls to different pools. We need
2058 * to log this history once to each pool, and bypass
2059 * the normal history logging that happens in main().
2061 (void) zpool_log_history(g_zfs, history_str);
2062 log_history = B_FALSE;
2064 if (zfs_prop_set(zhp, "version", verstr) == 0)
2065 cb->cb_numupgraded++;
2068 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2069 } else if (version > cb->cb_version) {
2070 /* can't downgrade */
2071 (void) printf(gettext("%s: can not be downgraded; "
2072 "it is already at version %u\n"),
2073 zfs_get_name(zhp), version);
2076 cb->cb_numsamegraded++;
2084 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2087 zfs_do_upgrade(int argc, char **argv)
2089 boolean_t all = B_FALSE;
2090 boolean_t showversions = B_FALSE;
2092 upgrade_cbdata_t cb = { 0 };
2094 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2097 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2100 flags |= ZFS_ITER_RECURSE;
2103 showversions = B_TRUE;
2106 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2107 optarg, &cb.cb_version) != 0) {
2108 (void) fprintf(stderr,
2109 gettext("invalid version %s\n"), optarg);
2118 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2127 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2129 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2130 cb.cb_version || argc))
2132 if ((all || argc) && (showversions))
2138 /* Show info on available versions. */
2139 (void) printf(gettext("The following filesystem versions are "
2141 (void) printf(gettext("VER DESCRIPTION\n"));
2142 (void) printf("--- -----------------------------------------"
2143 "---------------\n");
2144 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2145 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2146 (void) printf(gettext(" 3 Case insensitive and filesystem "
2147 "user identifier (FUID)\n"));
2148 (void) printf(gettext(" 4 userquota, groupquota "
2150 (void) printf(gettext(" 5 System attributes\n"));
2151 (void) printf(gettext("\nFor more information on a particular "
2152 "version, including supported releases,\n"));
2153 (void) printf("see the ZFS Administration Guide.\n\n");
2155 } else if (argc || all) {
2156 /* Upgrade filesystems */
2157 if (cb.cb_version == 0)
2158 cb.cb_version = ZPL_VERSION;
2159 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2160 NULL, NULL, 0, upgrade_set_callback, &cb);
2161 (void) printf(gettext("%llu filesystems upgraded\n"),
2163 if (cb.cb_numsamegraded) {
2164 (void) printf(gettext("%llu filesystems already at "
2166 cb.cb_numsamegraded);
2168 if (cb.cb_numfailed != 0)
2171 /* List old-version filesytems */
2173 (void) printf(gettext("This system is currently running "
2174 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2176 flags |= ZFS_ITER_RECURSE;
2177 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2178 NULL, NULL, 0, upgrade_list_callback, &cb);
2180 found = cb.cb_foundone;
2181 cb.cb_foundone = B_FALSE;
2182 cb.cb_newer = B_TRUE;
2184 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2185 NULL, NULL, 0, upgrade_list_callback, &cb);
2187 if (!cb.cb_foundone && !found) {
2188 (void) printf(gettext("All filesystems are "
2189 "formatted with the current version.\n"));
2197 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2198 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2199 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2200 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2202 * -H Scripted mode; elide headers and separate columns by tabs.
2203 * -i Translate SID to POSIX ID.
2204 * -n Print numeric ID instead of user/group name.
2205 * -o Control which fields to display.
2206 * -p Use exact (parsable) numeric output.
2207 * -s Specify sort columns, descending order.
2208 * -S Specify sort columns, ascending order.
2209 * -t Control which object types to display.
2211 * Displays space consumed by, and quotas on, each user in the specified
2212 * filesystem or snapshot.
2215 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2216 enum us_field_types {
2222 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2223 static char *us_field_names[] = { "type", "name", "used", "quota" };
2224 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2226 #define USTYPE_PSX_GRP (1 << 0)
2227 #define USTYPE_PSX_USR (1 << 1)
2228 #define USTYPE_SMB_GRP (1 << 2)
2229 #define USTYPE_SMB_USR (1 << 3)
2230 #define USTYPE_ALL \
2231 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2233 static int us_type_bits[] = {
2240 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2243 typedef struct us_node {
2245 uu_avl_node_t usn_avlnode;
2246 uu_list_node_t usn_listnode;
2249 typedef struct us_cbdata {
2251 uu_avl_pool_t *cb_avl_pool;
2253 boolean_t cb_numname;
2254 boolean_t cb_nicenum;
2255 boolean_t cb_sid2posix;
2256 zfs_userquota_prop_t cb_prop;
2257 zfs_sort_column_t *cb_sortcol;
2258 size_t cb_width[USFIELD_LAST];
2261 static boolean_t us_populated = B_FALSE;
2264 zfs_sort_column_t *si_sortcol;
2265 boolean_t si_numname;
2269 us_field_index(char *field)
2273 for (i = 0; i < USFIELD_LAST; i++) {
2274 if (strcmp(field, us_field_names[i]) == 0)
2282 us_compare(const void *larg, const void *rarg, void *unused)
2284 const us_node_t *l = larg;
2285 const us_node_t *r = rarg;
2286 us_sort_info_t *si = (us_sort_info_t *)unused;
2287 zfs_sort_column_t *sortcol = si->si_sortcol;
2288 boolean_t numname = si->si_numname;
2289 nvlist_t *lnvl = l->usn_nvl;
2290 nvlist_t *rnvl = r->usn_nvl;
2294 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2301 zfs_prop_t prop = sortcol->sc_prop;
2302 const char *propname = NULL;
2303 boolean_t reverse = sortcol->sc_reverse;
2308 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2309 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2311 rc = (rv32 < lv32) ? 1 : -1;
2316 (void) nvlist_lookup_uint64(lnvl, propname,
2318 (void) nvlist_lookup_uint64(rnvl, propname,
2321 rc = (rv64 < lv64) ? 1 : -1;
2323 (void) nvlist_lookup_string(lnvl, propname,
2325 (void) nvlist_lookup_string(rnvl, propname,
2327 rc = strcmp(lvstr, rvstr);
2331 case ZFS_PROP_QUOTA:
2334 if (prop == ZFS_PROP_USED)
2338 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2339 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2341 rc = (rv64 < lv64) ? 1 : -1;
2347 return (reverse ? 1 : -1);
2349 return (reverse ? -1 : 1);
2354 * If entries still seem to be the same, check if they are of the same
2355 * type (smbentity is added only if we are doing SID to POSIX ID
2356 * translation where we can have duplicate type/name combinations).
2358 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2359 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2361 return (lvb < rvb ? -1 : 1);
2366 static inline const char *
2367 us_type2str(unsigned field_type)
2369 switch (field_type) {
2370 case USTYPE_PSX_USR:
2371 return ("POSIX User");
2372 case USTYPE_PSX_GRP:
2373 return ("POSIX Group");
2374 case USTYPE_SMB_USR:
2375 return ("SMB User");
2376 case USTYPE_SMB_GRP:
2377 return ("SMB Group");
2379 return ("Undefined");
2384 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2386 us_cbdata_t *cb = (us_cbdata_t *)arg;
2387 zfs_userquota_prop_t prop = cb->cb_prop;
2392 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2393 uu_avl_t *avl = cb->cb_avl;
2397 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2399 const char *typestr;
2403 int typeidx, nameidx, sizeidx;
2404 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2405 boolean_t smbentity = B_FALSE;
2407 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2409 node = safe_malloc(sizeof (us_node_t));
2410 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2411 node->usn_nvl = props;
2413 if (domain != NULL && domain[0] != '\0') {
2415 char sid[ZFS_MAXNAMELEN + 32];
2419 int flag = IDMAP_REQ_FLG_USE_CACHE;
2424 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2426 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2427 type = USTYPE_SMB_GRP;
2429 err = sid_to_id(sid, B_FALSE, &id);
2432 type = USTYPE_SMB_USR;
2434 err = sid_to_id(sid, B_TRUE, &id);
2441 if (!cb->cb_sid2posix) {
2442 if (type == USTYPE_SMB_USR) {
2443 (void) idmap_getwinnamebyuid(rid, flag,
2446 (void) idmap_getwinnamebygid(rid, flag,
2456 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2458 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2459 type = USTYPE_PSX_GRP;
2460 if (!cb->cb_numname) {
2463 if ((g = getgrgid(rid)) != NULL)
2467 type = USTYPE_PSX_USR;
2468 if (!cb->cb_numname) {
2471 if ((p = getpwuid(rid)) != NULL)
2478 * Make sure that the type/name combination is unique when doing
2479 * SID to POSIX ID translation (hence changing the type from SMB to
2482 if (cb->cb_sid2posix &&
2483 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2486 /* Calculate/update width of TYPE field */
2487 typestr = us_type2str(type);
2488 typelen = strlen(gettext(typestr));
2489 typeidx = us_field_index("type");
2490 if (typelen > cb->cb_width[typeidx])
2491 cb->cb_width[typeidx] = typelen;
2492 if (nvlist_add_uint32(props, "type", type) != 0)
2495 /* Calculate/update width of NAME field */
2496 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2497 if (nvlist_add_uint64(props, "name", rid) != 0)
2499 namelen = snprintf(NULL, 0, "%u", rid);
2501 if (nvlist_add_string(props, "name", name) != 0)
2503 namelen = strlen(name);
2505 nameidx = us_field_index("name");
2506 if (namelen > cb->cb_width[nameidx])
2507 cb->cb_width[nameidx] = namelen;
2510 * Check if this type/name combination is in the list and update it;
2511 * otherwise add new node to the list.
2513 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2514 uu_avl_insert(avl, node, idx);
2519 props = node->usn_nvl;
2522 /* Calculate/update width of USED/QUOTA fields */
2524 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2526 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2527 sizelen = strlen(sizebuf);
2528 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2530 if (!nvlist_exists(props, "quota"))
2531 (void) nvlist_add_uint64(props, "quota", 0);
2534 if (!nvlist_exists(props, "used"))
2535 (void) nvlist_add_uint64(props, "used", 0);
2537 sizeidx = us_field_index(propname);
2538 if (sizelen > cb->cb_width[sizeidx])
2539 cb->cb_width[sizeidx] = sizelen;
2541 if (nvlist_add_uint64(props, propname, space) != 0)
2548 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2549 size_t *width, us_node_t *node)
2551 nvlist_t *nvl = node->usn_nvl;
2552 char valstr[ZFS_MAXNAMELEN];
2553 boolean_t first = B_TRUE;
2559 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2560 if (!(ustype & types))
2563 while ((field = fields[cfield]) != USFIELD_LAST) {
2564 nvpair_t *nvp = NULL;
2568 char *strval = NULL;
2570 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2571 if (strcmp(nvpair_name(nvp),
2572 us_field_names[field]) == 0)
2576 type = nvpair_type(nvp);
2578 case DATA_TYPE_UINT32:
2579 (void) nvpair_value_uint32(nvp, &val32);
2581 case DATA_TYPE_UINT64:
2582 (void) nvpair_value_uint64(nvp, &val64);
2584 case DATA_TYPE_STRING:
2585 (void) nvpair_value_string(nvp, &strval);
2588 (void) fprintf(stderr, "invalid data type\n");
2593 strval = (char *)us_type2str(val32);
2596 if (type == DATA_TYPE_UINT64) {
2597 (void) sprintf(valstr, "%llu", val64);
2603 if (type == DATA_TYPE_UINT64) {
2605 (void) sprintf(valstr, "%llu", val64);
2607 zfs_nicenum(val64, valstr,
2610 if (field == USFIELD_QUOTA &&
2611 strcmp(valstr, "0") == 0)
2621 (void) printf("\t");
2626 (void) printf("%s", strval);
2627 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2628 (void) printf("%-*s", width[field], strval);
2630 (void) printf("%*s", width[field], strval);
2636 (void) printf("\n");
2640 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2641 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2649 boolean_t first = B_TRUE;
2651 while ((field = fields[cfield]) != USFIELD_LAST) {
2652 col = gettext(us_field_hdr[field]);
2653 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2654 (void) printf(first ? "%-*s" : " %-*s",
2657 (void) printf(first ? "%*s" : " %*s",
2663 (void) printf("\n");
2666 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2667 print_us_node(scripted, parsable, fields, types, width, node);
2669 nvlist_free(node->usn_nvl);
2674 zfs_do_userspace(int argc, char **argv)
2677 zfs_userquota_prop_t p;
2679 uu_avl_pool_t *avl_pool;
2681 uu_avl_walk_t *walk;
2683 char deffields[] = "type,name,used,quota";
2684 char *ofield = NULL;
2685 char *tfield = NULL;
2689 boolean_t scripted = B_FALSE;
2690 boolean_t prtnum = B_FALSE;
2691 boolean_t parsable = B_FALSE;
2692 boolean_t sid2posix = B_FALSE;
2695 zfs_sort_column_t *sortcol = NULL;
2696 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2700 uu_list_pool_t *listpool;
2702 uu_avl_index_t idx = 0;
2703 uu_list_index_t idx2 = 0;
2708 if (strcmp(argv[0], "groupspace") == 0)
2709 /* Toggle default group types */
2710 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2712 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2728 if (zfs_add_sort_column(&sortcol, optarg,
2729 c == 's' ? B_FALSE : B_TRUE) != 0) {
2730 (void) fprintf(stderr,
2731 gettext("invalid field '%s'\n"), optarg);
2742 (void) fprintf(stderr, gettext("missing argument for "
2743 "'%c' option\n"), optopt);
2747 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2757 (void) fprintf(stderr, gettext("missing dataset name\n"));
2761 (void) fprintf(stderr, gettext("too many arguments\n"));
2765 /* Use default output fields if not specified using -o */
2769 if ((delim = strchr(ofield, ',')) != NULL)
2771 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2772 (void) fprintf(stderr, gettext("invalid type '%s' "
2773 "for -o option\n"), ofield);
2778 } while (delim != NULL);
2779 fields[cfield] = USFIELD_LAST;
2781 /* Override output types (-t option) */
2782 if (tfield != NULL) {
2786 boolean_t found = B_FALSE;
2788 if ((delim = strchr(tfield, ',')) != NULL)
2790 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2792 if (strcmp(tfield, us_type_names[i]) == 0) {
2794 types |= us_type_bits[i];
2799 (void) fprintf(stderr, gettext("invalid type "
2800 "'%s' for -t option\n"), tfield);
2805 } while (delim != NULL);
2808 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2811 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2812 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2814 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2817 /* Always add default sorting columns */
2818 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2819 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2821 cb.cb_sortcol = sortcol;
2822 cb.cb_numname = prtnum;
2823 cb.cb_nicenum = !parsable;
2824 cb.cb_avl_pool = avl_pool;
2825 cb.cb_avl = avl_tree;
2826 cb.cb_sid2posix = sid2posix;
2828 for (i = 0; i < USFIELD_LAST; i++)
2829 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2831 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2832 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2833 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2834 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2835 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2838 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2843 if ((node = uu_avl_first(avl_tree)) == NULL)
2846 us_populated = B_TRUE;
2848 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2849 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2850 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2851 uu_list_node_init(node, &node->usn_listnode, listpool);
2853 while (node != NULL) {
2855 node = uu_avl_next(avl_tree, node);
2856 uu_avl_remove(avl_tree, rmnode);
2857 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2858 uu_list_insert(list, rmnode, idx2);
2861 for (node = uu_list_first(list); node != NULL;
2862 node = uu_list_next(list, node)) {
2863 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2865 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2866 uu_avl_insert(avl_tree, node, idx);
2869 uu_list_destroy(list);
2870 uu_list_pool_destroy(listpool);
2872 /* Print and free node nvlist memory */
2873 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2876 zfs_free_sort_columns(sortcol);
2878 /* Clean up the AVL tree */
2879 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2882 while ((node = uu_avl_walk_next(walk)) != NULL) {
2883 uu_avl_remove(cb.cb_avl, node);
2887 uu_avl_walk_end(walk);
2888 uu_avl_destroy(avl_tree);
2889 uu_avl_pool_destroy(avl_pool);
2895 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2896 * [-t type[,...]] [filesystem|volume|snapshot] ...
2898 * -H Scripted mode; elide headers and separate columns by tabs.
2899 * -p Display values in parsable (literal) format.
2900 * -r Recurse over all children.
2901 * -d Limit recursion by depth.
2902 * -o Control which fields to display.
2903 * -s Specify sort columns, descending order.
2904 * -S Specify sort columns, ascending order.
2905 * -t Control which object types to display.
2907 * When given no arguments, list all filesystems in the system.
2908 * Otherwise, list the specified datasets, optionally recursing down them if
2909 * '-r' is specified.
2911 typedef struct list_cbdata {
2913 boolean_t cb_literal;
2914 boolean_t cb_scripted;
2915 zprop_list_t *cb_proplist;
2919 * Given a list of columns to display, output appropriate headers for each one.
2922 print_header(list_cbdata_t *cb)
2924 zprop_list_t *pl = cb->cb_proplist;
2925 char headerbuf[ZFS_MAXPROPLEN];
2928 boolean_t first = B_TRUE;
2929 boolean_t right_justify;
2931 for (; pl != NULL; pl = pl->pl_next) {
2938 right_justify = B_FALSE;
2939 if (pl->pl_prop != ZPROP_INVAL) {
2940 header = zfs_prop_column_name(pl->pl_prop);
2941 right_justify = zfs_prop_align_right(pl->pl_prop);
2943 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2944 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2945 headerbuf[i] = '\0';
2949 if (pl->pl_next == NULL && !right_justify)
2950 (void) printf("%s", header);
2951 else if (right_justify)
2952 (void) printf("%*s", pl->pl_width, header);
2954 (void) printf("%-*s", pl->pl_width, header);
2957 (void) printf("\n");
2961 * Given a dataset and a list of fields, print out all the properties according
2962 * to the described layout.
2965 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2967 zprop_list_t *pl = cb->cb_proplist;
2968 boolean_t first = B_TRUE;
2969 char property[ZFS_MAXPROPLEN];
2970 nvlist_t *userprops = zfs_get_user_props(zhp);
2973 boolean_t right_justify;
2975 for (; pl != NULL; pl = pl->pl_next) {
2977 if (cb->cb_scripted)
2978 (void) printf("\t");
2985 if (pl->pl_prop == ZFS_PROP_NAME) {
2986 (void) strlcpy(property, zfs_get_name(zhp),
2989 right_justify = zfs_prop_align_right(pl->pl_prop);
2990 } else if (pl->pl_prop != ZPROP_INVAL) {
2991 if (zfs_prop_get(zhp, pl->pl_prop, property,
2992 sizeof (property), NULL, NULL, 0,
2993 cb->cb_literal) != 0)
2997 right_justify = zfs_prop_align_right(pl->pl_prop);
2998 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2999 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3000 property, sizeof (property), cb->cb_literal) != 0)
3004 right_justify = B_TRUE;
3005 } else if (zfs_prop_written(pl->pl_user_prop)) {
3006 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3007 property, sizeof (property), cb->cb_literal) != 0)
3011 right_justify = B_TRUE;
3013 if (nvlist_lookup_nvlist(userprops,
3014 pl->pl_user_prop, &propval) != 0)
3017 verify(nvlist_lookup_string(propval,
3018 ZPROP_VALUE, &propstr) == 0);
3019 right_justify = B_FALSE;
3023 * If this is being called in scripted mode, or if this is the
3024 * last column and it is left-justified, don't include a width
3027 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3028 (void) printf("%s", propstr);
3029 else if (right_justify)
3030 (void) printf("%*s", pl->pl_width, propstr);
3032 (void) printf("%-*s", pl->pl_width, propstr);
3035 (void) printf("\n");
3039 * Generic callback function to list a dataset or snapshot.
3042 list_callback(zfs_handle_t *zhp, void *data)
3044 list_cbdata_t *cbp = data;
3046 if (cbp->cb_first) {
3047 if (!cbp->cb_scripted)
3049 cbp->cb_first = B_FALSE;
3052 print_dataset(zhp, cbp);
3058 zfs_do_list(int argc, char **argv)
3061 static char default_fields[] =
3062 "name,used,available,referenced,mountpoint";
3063 int types = ZFS_TYPE_DATASET;
3064 boolean_t types_specified = B_FALSE;
3065 char *fields = NULL;
3066 list_cbdata_t cb = { 0 };
3070 zfs_sort_column_t *sortcol = NULL;
3071 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3074 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3080 cb.cb_literal = B_TRUE;
3081 flags |= ZFS_ITER_LITERAL_PROPS;
3084 limit = parse_depth(optarg, &flags);
3087 flags |= ZFS_ITER_RECURSE;
3090 cb.cb_scripted = B_TRUE;
3093 if (zfs_add_sort_column(&sortcol, optarg,
3095 (void) fprintf(stderr,
3096 gettext("invalid property '%s'\n"), optarg);
3101 if (zfs_add_sort_column(&sortcol, optarg,
3103 (void) fprintf(stderr,
3104 gettext("invalid property '%s'\n"), optarg);
3110 types_specified = B_TRUE;
3111 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3112 while (*optarg != '\0') {
3113 static char *type_subopts[] = { "filesystem",
3114 "volume", "snapshot", "snap", "bookmark",
3117 switch (getsubopt(&optarg, type_subopts,
3120 types |= ZFS_TYPE_FILESYSTEM;
3123 types |= ZFS_TYPE_VOLUME;
3127 types |= ZFS_TYPE_SNAPSHOT;
3130 types |= ZFS_TYPE_BOOKMARK;
3133 types = ZFS_TYPE_DATASET |
3137 (void) fprintf(stderr,
3138 gettext("invalid type '%s'\n"),
3145 (void) fprintf(stderr, gettext("missing argument for "
3146 "'%c' option\n"), optopt);
3150 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3160 fields = default_fields;
3163 * If we are only going to list snapshot names and sort by name,
3164 * then we can use faster version.
3166 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3167 flags |= ZFS_ITER_SIMPLE;
3170 * If "-o space" and no types were specified, don't display snapshots.
3172 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3173 types &= ~ZFS_TYPE_SNAPSHOT;
3176 * If the user specifies '-o all', the zprop_get_list() doesn't
3177 * normally include the name of the dataset. For 'zfs list', we always
3178 * want this property to be first.
3180 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3184 cb.cb_first = B_TRUE;
3186 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3187 limit, list_callback, &cb);
3189 zprop_free_list(cb.cb_proplist);
3190 zfs_free_sort_columns(sortcol);
3192 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3193 (void) printf(gettext("no datasets available\n"));
3199 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3200 * zfs rename [-f] -p <fs | vol> <fs | vol>
3201 * zfs rename -r <snap> <snap>
3202 * zfs rename -u [-p] <fs> <fs>
3204 * Renames the given dataset to another of the same type.
3206 * The '-p' flag creates all the non-existing ancestors of the target first.
3210 zfs_do_rename(int argc, char **argv)
3213 renameflags_t flags = { 0 };
3217 boolean_t parents = B_FALSE;
3218 char *snapshot = NULL;
3221 while ((c = getopt(argc, argv, "fpru")) != -1) {
3227 flags.recurse = B_TRUE;
3230 flags.nounmount = B_TRUE;
3233 flags.forceunmount = B_TRUE;
3237 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3246 /* check number of arguments */
3248 (void) fprintf(stderr, gettext("missing source dataset "
3253 (void) fprintf(stderr, gettext("missing target dataset "
3258 (void) fprintf(stderr, gettext("too many arguments\n"));
3262 if (flags.recurse && parents) {
3263 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3268 if (flags.recurse && strchr(argv[0], '@') == 0) {
3269 (void) fprintf(stderr, gettext("source dataset for recursive "
3270 "rename must be a snapshot\n"));
3274 if (flags.nounmount && parents) {
3275 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3280 if (flags.nounmount)
3281 types = ZFS_TYPE_FILESYSTEM;
3283 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3285 types = ZFS_TYPE_DATASET;
3287 if (flags.recurse) {
3289 * When we do recursive rename we are fine when the given
3290 * snapshot for the given dataset doesn't exist - it can
3291 * still exists below.
3294 snapshot = strchr(argv[0], '@');
3295 assert(snapshot != NULL);
3300 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3303 /* If we were asked and the name looks good, try to create ancestors. */
3304 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3305 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3310 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3319 * Promotes the given clone fs to be the parent
3323 zfs_do_promote(int argc, char **argv)
3329 if (argc > 1 && argv[1][0] == '-') {
3330 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3335 /* check number of arguments */
3337 (void) fprintf(stderr, gettext("missing clone filesystem"
3342 (void) fprintf(stderr, gettext("too many arguments\n"));
3346 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3350 ret = (zfs_promote(zhp) != 0);
3358 * zfs rollback [-rRf] <snapshot>
3360 * -r Delete any intervening snapshots before doing rollback
3361 * -R Delete any snapshots and their clones
3362 * -f ignored for backwards compatability
3364 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3365 * since then and making it the active dataset. If more recent snapshots exist,
3366 * the command will complain unless the '-r' flag is given.
3368 typedef struct rollback_cbdata {
3374 boolean_t cb_recurse;
3375 } rollback_cbdata_t;
3378 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3380 rollback_cbdata_t *cbp = data;
3382 if (cbp->cb_first && cbp->cb_recurse) {
3383 (void) fprintf(stderr, gettext("cannot rollback to "
3384 "'%s': clones of previous snapshots exist\n"),
3386 (void) fprintf(stderr, gettext("use '-R' to "
3387 "force deletion of the following clones and "
3393 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3400 * Report any snapshots more recent than the one specified. Used when '-r' is
3401 * not specified. We reuse this same callback for the snapshot dependents - if
3402 * 'cb_dependent' is set, then this is a dependent and we should report it
3403 * without checking the transaction group.
3406 rollback_check(zfs_handle_t *zhp, void *data)
3408 rollback_cbdata_t *cbp = data;
3410 if (cbp->cb_doclones) {
3415 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3416 if (cbp->cb_first && !cbp->cb_recurse) {
3417 (void) fprintf(stderr, gettext("cannot "
3418 "rollback to '%s': more recent snapshots "
3419 "or bookmarks exist\n"),
3421 (void) fprintf(stderr, gettext("use '-r' to "
3422 "force deletion of the following "
3423 "snapshots and bookmarks:\n"));
3428 if (cbp->cb_recurse) {
3429 if (zfs_iter_dependents(zhp, B_TRUE,
3430 rollback_check_dependent, cbp) != 0) {
3435 (void) fprintf(stderr, "%s\n",
3444 zfs_do_rollback(int argc, char **argv)
3448 boolean_t force = B_FALSE;
3449 rollback_cbdata_t cb = { 0 };
3450 zfs_handle_t *zhp, *snap;
3451 char parentname[ZFS_MAXNAMELEN];
3455 while ((c = getopt(argc, argv, "rRf")) != -1) {
3468 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3477 /* check number of arguments */
3479 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3483 (void) fprintf(stderr, gettext("too many arguments\n"));
3487 /* open the snapshot */
3488 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3491 /* open the parent dataset */
3492 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3493 verify((delim = strrchr(parentname, '@')) != NULL);
3495 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3501 * Check for more recent snapshots and/or clones based on the presence
3504 cb.cb_target = argv[0];
3505 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3506 cb.cb_first = B_TRUE;
3508 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3510 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3513 if ((ret = cb.cb_error) != 0)
3517 * Rollback parent to the given snapshot.
3519 ret = zfs_rollback(zhp, snap, force);
3532 * zfs set property=value { fs | snap | vol } ...
3534 * Sets the given property for all datasets specified on the command line.
3536 typedef struct set_cbdata {
3542 set_callback(zfs_handle_t *zhp, void *data)
3544 set_cbdata_t *cbp = data;
3546 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3547 switch (libzfs_errno(g_zfs)) {
3548 case EZFS_MOUNTFAILED:
3549 (void) fprintf(stderr, gettext("property may be set "
3550 "but unable to remount filesystem\n"));
3552 case EZFS_SHARENFSFAILED:
3553 (void) fprintf(stderr, gettext("property may be set "
3554 "but unable to reshare filesystem\n"));
3563 zfs_do_set(int argc, char **argv)
3568 /* check for options */
3569 if (argc > 1 && argv[1][0] == '-') {
3570 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3575 /* check number of arguments */
3577 (void) fprintf(stderr, gettext("missing property=value "
3582 (void) fprintf(stderr, gettext("missing dataset name\n"));
3586 /* validate property=value argument */
3587 cb.cb_propname = argv[1];
3588 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3589 (cb.cb_value[1] == '\0')) {
3590 (void) fprintf(stderr, gettext("missing value in "
3591 "property=value argument\n"));
3595 *cb.cb_value = '\0';
3598 if (*cb.cb_propname == '\0') {
3599 (void) fprintf(stderr,
3600 gettext("missing property in property=value argument\n"));
3604 ret = zfs_for_each(argc - 2, argv + 2, 0,
3605 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3610 typedef struct snap_cbdata {
3612 boolean_t sd_recursive;
3613 const char *sd_snapname;
3617 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3619 snap_cbdata_t *sd = arg;
3624 if (sd->sd_recursive &&
3625 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3630 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3633 fnvlist_add_boolean(sd->sd_nvl, name);
3636 if (sd->sd_recursive)
3637 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3643 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3645 * Creates a snapshot with the given name. While functionally equivalent to
3646 * 'zfs create', it is a separate command to differentiate intent.
3649 zfs_do_snapshot(int argc, char **argv)
3654 snap_cbdata_t sd = { 0 };
3655 boolean_t multiple_snaps = B_FALSE;
3657 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3659 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3663 while ((c = getopt(argc, argv, "ro:")) != -1) {
3666 if (parseprop(props, optarg))
3670 sd.sd_recursive = B_TRUE;
3671 multiple_snaps = B_TRUE;
3674 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3683 /* check number of arguments */
3685 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3690 multiple_snaps = B_TRUE;
3691 for (; argc > 0; argc--, argv++) {
3695 atp = strchr(argv[0], '@');
3699 sd.sd_snapname = atp + 1;
3700 zhp = zfs_open(g_zfs, argv[0],
3701 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3704 if (zfs_snapshot_cb(zhp, &sd) != 0)
3708 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3709 nvlist_free(sd.sd_nvl);
3711 if (ret != 0 && multiple_snaps)
3712 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3716 nvlist_free(sd.sd_nvl);
3723 * Send a backup stream to stdout.
3726 zfs_do_send(int argc, char **argv)
3728 char *fromname = NULL;
3729 char *toname = NULL;
3732 sendflags_t flags = { 0 };
3734 nvlist_t *dbgnv = NULL;
3735 boolean_t extraverbose = B_FALSE;
3738 while ((c = getopt(argc, argv, ":i:I:RDpvnPLe")) != -1) {
3749 flags.doall = B_TRUE;
3752 flags.replicate = B_TRUE;
3755 flags.props = B_TRUE;
3758 flags.parsable = B_TRUE;
3759 flags.verbose = B_TRUE;
3763 extraverbose = B_TRUE;
3764 flags.verbose = B_TRUE;
3765 flags.progress = B_TRUE;
3768 flags.dedup = B_TRUE;
3771 flags.dryrun = B_TRUE;
3774 flags.largeblock = B_TRUE;
3777 flags.embed_data = B_TRUE;
3780 (void) fprintf(stderr, gettext("missing argument for "
3781 "'%c' option\n"), optopt);
3785 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3794 /* check number of arguments */
3796 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3800 (void) fprintf(stderr, gettext("too many arguments\n"));
3804 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3805 (void) fprintf(stderr,
3806 gettext("Error: Stream can not be written to a terminal.\n"
3807 "You must redirect standard output.\n"));
3812 * Special case sending a filesystem, or from a bookmark.
3814 if (strchr(argv[0], '@') == NULL ||
3815 (fromname && strchr(fromname, '#') != NULL)) {
3816 char frombuf[ZFS_MAXNAMELEN];
3817 enum lzc_send_flags lzc_flags = 0;
3819 if (flags.replicate || flags.doall || flags.props ||
3820 flags.dedup || flags.dryrun || flags.verbose ||
3822 (void) fprintf(stderr,
3824 "Unsupported flag with filesystem or bookmark.\n"));
3828 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3832 if (flags.largeblock)
3833 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3834 if (flags.embed_data)
3835 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3837 if (fromname != NULL &&
3838 (fromname[0] == '#' || fromname[0] == '@')) {
3840 * Incremental source name begins with # or @.
3841 * Default to same fs as target.
3843 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3844 cp = strchr(frombuf, '@');
3847 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3850 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3855 cp = strchr(argv[0], '@');
3858 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3863 * If they specified the full path to the snapshot, chop off
3864 * everything except the short name of the snapshot, but special
3865 * case if they specify the origin.
3867 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3868 char origin[ZFS_MAXNAMELEN];
3871 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3872 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3874 if (strcmp(origin, fromname) == 0) {
3876 flags.fromorigin = B_TRUE;
3879 if (cp != fromname && strcmp(argv[0], fromname)) {
3880 (void) fprintf(stderr,
3881 gettext("incremental source must be "
3882 "in same filesystem\n"));
3886 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3887 (void) fprintf(stderr,
3888 gettext("invalid incremental source\n"));
3894 if (flags.replicate && fromname == NULL)
3895 flags.doall = B_TRUE;
3897 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3898 extraverbose ? &dbgnv : NULL);
3900 if (extraverbose && dbgnv != NULL) {
3902 * dump_nvlist prints to stdout, but that's been
3903 * redirected to a file. Make it print to stderr
3906 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3907 dump_nvlist(dbgnv, 0);
3916 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3918 * Restore a backup stream from stdin.
3921 zfs_do_receive(int argc, char **argv)
3924 recvflags_t flags = { 0 };
3927 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3930 flags.isprefix = B_TRUE;
3933 flags.isprefix = B_TRUE;
3934 flags.istail = B_TRUE;
3937 flags.dryrun = B_TRUE;
3940 flags.nomount = B_TRUE;
3943 flags.verbose = B_TRUE;
3946 flags.force = B_TRUE;
3949 (void) fprintf(stderr, gettext("missing argument for "
3950 "'%c' option\n"), optopt);
3954 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3963 /* check number of arguments */
3965 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3969 (void) fprintf(stderr, gettext("too many arguments\n"));
3973 if (isatty(STDIN_FILENO)) {
3974 (void) fprintf(stderr,
3975 gettext("Error: Backup stream can not be read "
3976 "from a terminal.\n"
3977 "You must redirect standard input.\n"));
3981 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3987 * allow/unallow stuff
3989 /* copied from zfs/sys/dsl_deleg.h */
3990 #define ZFS_DELEG_PERM_CREATE "create"
3991 #define ZFS_DELEG_PERM_DESTROY "destroy"
3992 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3993 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3994 #define ZFS_DELEG_PERM_CLONE "clone"
3995 #define ZFS_DELEG_PERM_PROMOTE "promote"
3996 #define ZFS_DELEG_PERM_RENAME "rename"
3997 #define ZFS_DELEG_PERM_MOUNT "mount"
3998 #define ZFS_DELEG_PERM_SHARE "share"
3999 #define ZFS_DELEG_PERM_SEND "send"
4000 #define ZFS_DELEG_PERM_RECEIVE "receive"
4001 #define ZFS_DELEG_PERM_ALLOW "allow"
4002 #define ZFS_DELEG_PERM_USERPROP "userprop"
4003 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4004 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4005 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4006 #define ZFS_DELEG_PERM_USERUSED "userused"
4007 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4008 #define ZFS_DELEG_PERM_HOLD "hold"
4009 #define ZFS_DELEG_PERM_RELEASE "release"
4010 #define ZFS_DELEG_PERM_DIFF "diff"
4011 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4013 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4015 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4016 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4017 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4018 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4019 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4020 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4021 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4022 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4023 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4024 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4025 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4026 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4027 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4028 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4029 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4030 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4031 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4033 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4034 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4035 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4036 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4037 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4038 { NULL, ZFS_DELEG_NOTE_NONE }
4041 /* permission structure */
4042 typedef struct deleg_perm {
4043 zfs_deleg_who_type_t dp_who_type;
4044 const char *dp_name;
4046 boolean_t dp_descend;
4050 typedef struct deleg_perm_node {
4051 deleg_perm_t dpn_perm;
4053 uu_avl_node_t dpn_avl_node;
4054 } deleg_perm_node_t;
4056 typedef struct fs_perm fs_perm_t;
4058 /* permissions set */
4059 typedef struct who_perm {
4060 zfs_deleg_who_type_t who_type;
4061 const char *who_name; /* id */
4062 char who_ug_name[256]; /* user/group name */
4063 fs_perm_t *who_fsperm; /* uplink */
4065 uu_avl_t *who_deleg_perm_avl; /* permissions */
4069 typedef struct who_perm_node {
4070 who_perm_t who_perm;
4071 uu_avl_node_t who_avl_node;
4074 typedef struct fs_perm_set fs_perm_set_t;
4075 /* fs permissions */
4077 const char *fsp_name;
4079 uu_avl_t *fsp_sc_avl; /* sets,create */
4080 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4082 fs_perm_set_t *fsp_set; /* uplink */
4086 typedef struct fs_perm_node {
4087 fs_perm_t fspn_fsperm;
4090 uu_list_node_t fspn_list_node;
4093 /* top level structure */
4094 struct fs_perm_set {
4095 uu_list_pool_t *fsps_list_pool;
4096 uu_list_t *fsps_list; /* list of fs_perms */
4098 uu_avl_pool_t *fsps_named_set_avl_pool;
4099 uu_avl_pool_t *fsps_who_perm_avl_pool;
4100 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4103 static inline const char *
4104 deleg_perm_type(zfs_deleg_note_t note)
4110 case ZFS_DELEG_NOTE_GROUPQUOTA:
4111 case ZFS_DELEG_NOTE_GROUPUSED:
4112 case ZFS_DELEG_NOTE_USERPROP:
4113 case ZFS_DELEG_NOTE_USERQUOTA:
4114 case ZFS_DELEG_NOTE_USERUSED:
4116 return (gettext("other"));
4118 return (gettext("subcommand"));
4123 who_type2weight(zfs_deleg_who_type_t who_type)
4127 case ZFS_DELEG_NAMED_SET_SETS:
4128 case ZFS_DELEG_NAMED_SET:
4131 case ZFS_DELEG_CREATE_SETS:
4132 case ZFS_DELEG_CREATE:
4135 case ZFS_DELEG_USER_SETS:
4136 case ZFS_DELEG_USER:
4139 case ZFS_DELEG_GROUP_SETS:
4140 case ZFS_DELEG_GROUP:
4143 case ZFS_DELEG_EVERYONE_SETS:
4144 case ZFS_DELEG_EVERYONE:
4156 who_perm_compare(const void *larg, const void *rarg, void *unused)
4158 const who_perm_node_t *l = larg;
4159 const who_perm_node_t *r = rarg;
4160 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4161 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4162 int lweight = who_type2weight(ltype);
4163 int rweight = who_type2weight(rtype);
4164 int res = lweight - rweight;
4166 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4167 ZFS_MAX_DELEG_NAME-1);
4179 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4181 const deleg_perm_node_t *l = larg;
4182 const deleg_perm_node_t *r = rarg;
4183 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4184 ZFS_MAX_DELEG_NAME-1);
4196 fs_perm_set_init(fs_perm_set_t *fspset)
4198 bzero(fspset, sizeof (fs_perm_set_t));
4200 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4201 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4202 NULL, UU_DEFAULT)) == NULL)
4204 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4205 UU_DEFAULT)) == NULL)
4208 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4209 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4210 who_perm_node_t, who_avl_node), who_perm_compare,
4211 UU_DEFAULT)) == NULL)
4214 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4215 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4216 who_perm_node_t, who_avl_node), who_perm_compare,
4217 UU_DEFAULT)) == NULL)
4220 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4221 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4222 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4227 static inline void fs_perm_fini(fs_perm_t *);
4228 static inline void who_perm_fini(who_perm_t *);
4231 fs_perm_set_fini(fs_perm_set_t *fspset)
4233 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4235 while (node != NULL) {
4236 fs_perm_node_t *next_node =
4237 uu_list_next(fspset->fsps_list, node);
4238 fs_perm_t *fsperm = &node->fspn_fsperm;
4239 fs_perm_fini(fsperm);
4240 uu_list_remove(fspset->fsps_list, node);
4245 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4246 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4247 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4251 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4254 deleg_perm->dp_who_type = type;
4255 deleg_perm->dp_name = name;
4259 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4260 zfs_deleg_who_type_t type, const char *name)
4262 uu_avl_pool_t *pool;
4263 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4265 bzero(who_perm, sizeof (who_perm_t));
4267 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4268 UU_DEFAULT)) == NULL)
4271 who_perm->who_type = type;
4272 who_perm->who_name = name;
4273 who_perm->who_fsperm = fsperm;
4277 who_perm_fini(who_perm_t *who_perm)
4279 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4281 while (node != NULL) {
4282 deleg_perm_node_t *next_node =
4283 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4285 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4290 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4294 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4296 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4297 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4299 bzero(fsperm, sizeof (fs_perm_t));
4301 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4305 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4309 fsperm->fsp_set = fspset;
4310 fsperm->fsp_name = fsname;
4314 fs_perm_fini(fs_perm_t *fsperm)
4316 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4317 while (node != NULL) {
4318 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4320 who_perm_t *who_perm = &node->who_perm;
4321 who_perm_fini(who_perm);
4322 uu_avl_remove(fsperm->fsp_sc_avl, node);
4327 node = uu_avl_first(fsperm->fsp_uge_avl);
4328 while (node != NULL) {
4329 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4331 who_perm_t *who_perm = &node->who_perm;
4332 who_perm_fini(who_perm);
4333 uu_avl_remove(fsperm->fsp_uge_avl, node);
4338 uu_avl_destroy(fsperm->fsp_sc_avl);
4339 uu_avl_destroy(fsperm->fsp_uge_avl);
4343 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4344 zfs_deleg_who_type_t who_type, const char *name, char locality)
4346 uu_avl_index_t idx = 0;
4348 deleg_perm_node_t *found_node = NULL;
4349 deleg_perm_t *deleg_perm = &node->dpn_perm;
4351 deleg_perm_init(deleg_perm, who_type, name);
4353 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4355 uu_avl_insert(avl, node, idx);
4358 deleg_perm = &node->dpn_perm;
4363 case ZFS_DELEG_LOCAL:
4364 deleg_perm->dp_local = B_TRUE;
4366 case ZFS_DELEG_DESCENDENT:
4367 deleg_perm->dp_descend = B_TRUE;
4372 assert(B_FALSE); /* invalid locality */
4377 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4379 nvpair_t *nvp = NULL;
4380 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4381 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4382 zfs_deleg_who_type_t who_type = who_perm->who_type;
4384 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4385 const char *name = nvpair_name(nvp);
4386 data_type_t type = nvpair_type(nvp);
4387 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4388 deleg_perm_node_t *node =
4389 safe_malloc(sizeof (deleg_perm_node_t));
4391 assert(type == DATA_TYPE_BOOLEAN);
4393 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4394 set_deleg_perm_node(avl, node, who_type, name, locality);
4401 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4403 nvpair_t *nvp = NULL;
4404 fs_perm_set_t *fspset = fsperm->fsp_set;
4406 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4407 nvlist_t *nvl2 = NULL;
4408 const char *name = nvpair_name(nvp);
4409 uu_avl_t *avl = NULL;
4410 uu_avl_pool_t *avl_pool;
4411 zfs_deleg_who_type_t perm_type = name[0];
4412 char perm_locality = name[1];
4413 const char *perm_name = name + 3;
4414 boolean_t is_set = B_TRUE;
4415 who_perm_t *who_perm = NULL;
4417 assert('$' == name[2]);
4419 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4422 switch (perm_type) {
4423 case ZFS_DELEG_CREATE:
4424 case ZFS_DELEG_CREATE_SETS:
4425 case ZFS_DELEG_NAMED_SET:
4426 case ZFS_DELEG_NAMED_SET_SETS:
4427 avl_pool = fspset->fsps_named_set_avl_pool;
4428 avl = fsperm->fsp_sc_avl;
4430 case ZFS_DELEG_USER:
4431 case ZFS_DELEG_USER_SETS:
4432 case ZFS_DELEG_GROUP:
4433 case ZFS_DELEG_GROUP_SETS:
4434 case ZFS_DELEG_EVERYONE:
4435 case ZFS_DELEG_EVERYONE_SETS:
4436 avl_pool = fspset->fsps_who_perm_avl_pool;
4437 avl = fsperm->fsp_uge_avl;
4442 who_perm_node_t *found_node = NULL;
4443 who_perm_node_t *node = safe_malloc(
4444 sizeof (who_perm_node_t));
4445 who_perm = &node->who_perm;
4446 uu_avl_index_t idx = 0;
4448 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4449 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4451 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4453 if (avl == fsperm->fsp_uge_avl) {
4455 struct passwd *p = NULL;
4456 struct group *g = NULL;
4457 const char *nice_name = NULL;
4459 switch (perm_type) {
4460 case ZFS_DELEG_USER_SETS:
4461 case ZFS_DELEG_USER:
4462 rid = atoi(perm_name);
4465 nice_name = p->pw_name;
4467 case ZFS_DELEG_GROUP_SETS:
4468 case ZFS_DELEG_GROUP:
4469 rid = atoi(perm_name);
4472 nice_name = g->gr_name;
4476 if (nice_name != NULL)
4478 node->who_perm.who_ug_name,
4482 uu_avl_insert(avl, node, idx);
4485 who_perm = &node->who_perm;
4489 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4496 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4498 nvpair_t *nvp = NULL;
4499 uu_avl_index_t idx = 0;
4501 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4502 nvlist_t *nvl2 = NULL;
4503 const char *fsname = nvpair_name(nvp);
4504 data_type_t type = nvpair_type(nvp);
4505 fs_perm_t *fsperm = NULL;
4506 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4510 fsperm = &node->fspn_fsperm;
4512 assert(DATA_TYPE_NVLIST == type);
4514 uu_list_node_init(node, &node->fspn_list_node,
4515 fspset->fsps_list_pool);
4517 idx = uu_list_numnodes(fspset->fsps_list);
4518 fs_perm_init(fsperm, fspset, fsname);
4520 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4523 (void) parse_fs_perm(fsperm, nvl2);
4525 uu_list_insert(fspset->fsps_list, node, idx);
4531 static inline const char *
4532 deleg_perm_comment(zfs_deleg_note_t note)
4534 const char *str = "";
4539 case ZFS_DELEG_NOTE_ALLOW:
4540 str = gettext("Must also have the permission that is being"
4541 "\n\t\t\t\tallowed");
4543 case ZFS_DELEG_NOTE_CLONE:
4544 str = gettext("Must also have the 'create' ability and 'mount'"
4545 "\n\t\t\t\tability in the origin file system");
4547 case ZFS_DELEG_NOTE_CREATE:
4548 str = gettext("Must also have the 'mount' ability");
4550 case ZFS_DELEG_NOTE_DESTROY:
4551 str = gettext("Must also have the 'mount' ability");
4553 case ZFS_DELEG_NOTE_DIFF:
4554 str = gettext("Allows lookup of paths within a dataset;"
4555 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4556 "\n\t\t\t\tin order to use zfs diff");
4558 case ZFS_DELEG_NOTE_HOLD:
4559 str = gettext("Allows adding a user hold to a snapshot");
4561 case ZFS_DELEG_NOTE_MOUNT:
4562 str = gettext("Allows mount/umount of ZFS datasets");
4564 case ZFS_DELEG_NOTE_PROMOTE:
4565 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4566 " 'promote' ability in the origin file system");
4568 case ZFS_DELEG_NOTE_RECEIVE:
4569 str = gettext("Must also have the 'mount' and 'create'"
4572 case ZFS_DELEG_NOTE_RELEASE:
4573 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4574 "might destroy the snapshot");
4576 case ZFS_DELEG_NOTE_RENAME:
4577 str = gettext("Must also have the 'mount' and 'create'"
4578 "\n\t\t\t\tability in the new parent");
4580 case ZFS_DELEG_NOTE_ROLLBACK:
4583 case ZFS_DELEG_NOTE_SEND:
4586 case ZFS_DELEG_NOTE_SHARE:
4587 str = gettext("Allows sharing file systems over NFS or SMB"
4588 "\n\t\t\t\tprotocols");
4590 case ZFS_DELEG_NOTE_SNAPSHOT:
4594 * case ZFS_DELEG_NOTE_VSCAN:
4595 * str = gettext("");
4599 case ZFS_DELEG_NOTE_GROUPQUOTA:
4600 str = gettext("Allows accessing any groupquota@... property");
4602 case ZFS_DELEG_NOTE_GROUPUSED:
4603 str = gettext("Allows reading any groupused@... property");
4605 case ZFS_DELEG_NOTE_USERPROP:
4606 str = gettext("Allows changing any user property");
4608 case ZFS_DELEG_NOTE_USERQUOTA:
4609 str = gettext("Allows accessing any userquota@... property");
4611 case ZFS_DELEG_NOTE_USERUSED:
4612 str = gettext("Allows reading any userused@... property");
4630 boolean_t recursive; /* unallow only */
4631 boolean_t prt_usage;
4633 boolean_t prt_perms;
4636 const char *dataset;
4640 prop_cmp(const void *a, const void *b)
4642 const char *str1 = *(const char **)a;
4643 const char *str2 = *(const char **)b;
4644 return (strcmp(str1, str2));
4648 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4650 const char *opt_desc[] = {
4651 "-h", gettext("show this help message and exit"),
4652 "-l", gettext("set permission locally"),
4653 "-d", gettext("set permission for descents"),
4654 "-u", gettext("set permission for user"),
4655 "-g", gettext("set permission for group"),
4656 "-e", gettext("set permission for everyone"),
4657 "-c", gettext("set create time permission"),
4658 "-s", gettext("define permission set"),
4660 "-r", gettext("remove permissions recursively"),
4662 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4663 size_t allow_size = unallow_size - 2;
4664 const char *props[ZFS_NUM_PROPS];
4667 FILE *fp = requested ? stdout : stderr;
4668 zprop_desc_t *pdtbl = zfs_prop_get_table();
4669 const char *fmt = gettext("%-16s %-14s\t%s\n");
4671 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4673 (void) fprintf(fp, gettext("Options:\n"));
4674 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4675 const char *opt = opt_desc[i++];
4676 const char *optdsc = opt_desc[i];
4677 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4680 (void) fprintf(fp, gettext("\nThe following permissions are "
4682 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4684 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4685 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4686 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4687 const char *perm_type = deleg_perm_type(perm_note);
4688 const char *perm_comment = deleg_perm_comment(perm_note);
4689 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4692 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4693 zprop_desc_t *pd = &pdtbl[i];
4694 if (pd->pd_visible != B_TRUE)
4697 if (pd->pd_attr == PROP_READONLY)
4700 props[count++] = pd->pd_name;
4702 props[count] = NULL;
4704 qsort(props, count, sizeof (char *), prop_cmp);
4706 for (i = 0; i < count; i++)
4707 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4710 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4712 exit(requested ? 0 : 2);
4715 static inline const char *
4716 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4719 if (un && argc == expected_argc - 1)
4721 else if (argc == expected_argc)
4722 *permsp = argv[argc - 2];
4724 allow_usage(un, B_FALSE,
4725 gettext("wrong number of parameters\n"));
4727 return (argv[argc - 1]);
4731 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4733 int uge_sum = opts->user + opts->group + opts->everyone;
4734 int csuge_sum = opts->create + opts->set + uge_sum;
4735 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4736 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4739 allow_usage(un, B_FALSE,
4740 gettext("-u, -g, and -e are mutually exclusive\n"));
4742 if (opts->prt_usage)
4743 if (argc == 0 && all_sum == 0)
4744 allow_usage(un, B_TRUE, NULL);
4750 allow_usage(un, B_FALSE,
4751 gettext("invalid options combined with -s\n"));
4753 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4754 if (argv[0][0] != '@')
4755 allow_usage(un, B_FALSE,
4756 gettext("invalid set name: missing '@' prefix\n"));
4757 opts->who = argv[0];
4758 } else if (opts->create) {
4759 if (ldcsuge_sum > 1)
4760 allow_usage(un, B_FALSE,
4761 gettext("invalid options combined with -c\n"));
4762 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4763 } else if (opts->everyone) {
4765 allow_usage(un, B_FALSE,
4766 gettext("invalid options combined with -e\n"));
4767 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4768 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4770 opts->everyone = B_TRUE;
4773 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4774 } else if (argc == 1 && !un) {
4775 opts->prt_perms = B_TRUE;
4776 opts->dataset = argv[argc-1];
4778 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4779 opts->who = argv[0];
4782 if (!opts->local && !opts->descend) {
4783 opts->local = B_TRUE;
4784 opts->descend = B_TRUE;
4789 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4790 const char *who, char *perms, nvlist_t *top_nvl)
4793 char ld[2] = { '\0', '\0' };
4794 char who_buf[ZFS_MAXNAMELEN+32];
4797 nvlist_t *base_nvl = NULL;
4798 nvlist_t *set_nvl = NULL;
4801 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4803 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4807 case ZFS_DELEG_NAMED_SET_SETS:
4808 case ZFS_DELEG_NAMED_SET:
4809 set_type = ZFS_DELEG_NAMED_SET_SETS;
4810 base_type = ZFS_DELEG_NAMED_SET;
4811 ld[0] = ZFS_DELEG_NA;
4813 case ZFS_DELEG_CREATE_SETS:
4814 case ZFS_DELEG_CREATE:
4815 set_type = ZFS_DELEG_CREATE_SETS;
4816 base_type = ZFS_DELEG_CREATE;
4817 ld[0] = ZFS_DELEG_NA;
4819 case ZFS_DELEG_USER_SETS:
4820 case ZFS_DELEG_USER:
4821 set_type = ZFS_DELEG_USER_SETS;
4822 base_type = ZFS_DELEG_USER;
4824 ld[0] = ZFS_DELEG_LOCAL;
4826 ld[1] = ZFS_DELEG_DESCENDENT;
4828 case ZFS_DELEG_GROUP_SETS:
4829 case ZFS_DELEG_GROUP:
4830 set_type = ZFS_DELEG_GROUP_SETS;
4831 base_type = ZFS_DELEG_GROUP;
4833 ld[0] = ZFS_DELEG_LOCAL;
4835 ld[1] = ZFS_DELEG_DESCENDENT;
4837 case ZFS_DELEG_EVERYONE_SETS:
4838 case ZFS_DELEG_EVERYONE:
4839 set_type = ZFS_DELEG_EVERYONE_SETS;
4840 base_type = ZFS_DELEG_EVERYONE;
4842 ld[0] = ZFS_DELEG_LOCAL;
4844 ld[1] = ZFS_DELEG_DESCENDENT;
4847 if (perms != NULL) {
4849 char *end = curr + strlen(perms);
4851 while (curr < end) {
4852 char *delim = strchr(curr, ',');
4863 (void) nvlist_add_boolean(nvl, curr);
4869 for (i = 0; i < 2; i++) {
4870 char locality = ld[i];
4874 if (!nvlist_empty(base_nvl)) {
4876 (void) snprintf(who_buf,
4877 sizeof (who_buf), "%c%c$%s",
4878 base_type, locality, who);
4880 (void) snprintf(who_buf,
4881 sizeof (who_buf), "%c%c$",
4882 base_type, locality);
4884 (void) nvlist_add_nvlist(top_nvl, who_buf,
4889 if (!nvlist_empty(set_nvl)) {
4891 (void) snprintf(who_buf,
4892 sizeof (who_buf), "%c%c$%s",
4893 set_type, locality, who);
4895 (void) snprintf(who_buf,
4896 sizeof (who_buf), "%c%c$",
4897 set_type, locality);
4899 (void) nvlist_add_nvlist(top_nvl, who_buf,
4904 for (i = 0; i < 2; i++) {
4905 char locality = ld[i];
4910 (void) snprintf(who_buf, sizeof (who_buf),
4911 "%c%c$%s", base_type, locality, who);
4913 (void) snprintf(who_buf, sizeof (who_buf),
4914 "%c%c$", base_type, locality);
4915 (void) nvlist_add_boolean(top_nvl, who_buf);
4918 (void) snprintf(who_buf, sizeof (who_buf),
4919 "%c%c$%s", set_type, locality, who);
4921 (void) snprintf(who_buf, sizeof (who_buf),
4922 "%c%c$", set_type, locality);
4923 (void) nvlist_add_boolean(top_nvl, who_buf);
4929 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4931 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4935 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4936 opts->descend, opts->who, opts->perms, *nvlp);
4937 } else if (opts->create) {
4938 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4939 opts->descend, NULL, opts->perms, *nvlp);
4940 } else if (opts->everyone) {
4941 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4942 opts->descend, NULL, opts->perms, *nvlp);
4944 char *curr = opts->who;
4945 char *end = curr + strlen(curr);
4947 while (curr < end) {
4949 zfs_deleg_who_type_t who_type;
4951 char *delim = strchr(curr, ',');
4954 struct passwd *p = NULL;
4955 struct group *g = NULL;
4963 rid = (uid_t)strtol(curr, &endch, 0);
4965 who_type = ZFS_DELEG_USER;
4974 (void) snprintf(errbuf, 256, gettext(
4975 "invalid user %s"), curr);
4976 allow_usage(un, B_TRUE, errbuf);
4978 } else if (opts->group) {
4979 who_type = ZFS_DELEG_GROUP;
4988 (void) snprintf(errbuf, 256, gettext(
4989 "invalid group %s"), curr);
4990 allow_usage(un, B_TRUE, errbuf);
4993 if (*endch != '\0') {
5000 if (*endch != '\0') {
5007 who_type = ZFS_DELEG_USER;
5009 } else if (g != NULL) {
5010 who_type = ZFS_DELEG_GROUP;
5013 (void) snprintf(errbuf, 256, gettext(
5014 "invalid user/group %s"), curr);
5015 allow_usage(un, B_TRUE, errbuf);
5019 (void) sprintf(id, "%u", rid);
5022 store_allow_perm(who_type, opts->local,
5023 opts->descend, who, opts->perms, *nvlp);
5032 print_set_creat_perms(uu_avl_t *who_avl)
5034 const char *sc_title[] = {
5035 gettext("Permission sets:\n"),
5036 gettext("Create time permissions:\n"),
5039 const char **title_ptr = sc_title;
5040 who_perm_node_t *who_node = NULL;
5041 int prev_weight = -1;
5043 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5044 who_node = uu_avl_next(who_avl, who_node)) {
5045 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5046 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5047 const char *who_name = who_node->who_perm.who_name;
5048 int weight = who_type2weight(who_type);
5049 boolean_t first = B_TRUE;
5050 deleg_perm_node_t *deleg_node;
5052 if (prev_weight != weight) {
5053 (void) printf(*title_ptr++);
5054 prev_weight = weight;
5057 if (who_name == NULL || strnlen(who_name, 1) == 0)
5058 (void) printf("\t");
5060 (void) printf("\t%s ", who_name);
5062 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5063 deleg_node = uu_avl_next(avl, deleg_node)) {
5066 deleg_node->dpn_perm.dp_name);
5069 (void) printf(",%s",
5070 deleg_node->dpn_perm.dp_name);
5073 (void) printf("\n");
5078 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5081 who_perm_node_t *who_node = NULL;
5082 boolean_t prt_title = B_TRUE;
5083 uu_avl_walk_t *walk;
5085 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5088 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5089 const char *who_name = who_node->who_perm.who_name;
5090 const char *nice_who_name = who_node->who_perm.who_ug_name;
5091 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5092 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5094 deleg_perm_node_t *deleg_node;
5095 boolean_t prt_who = B_TRUE;
5097 for (deleg_node = uu_avl_first(avl);
5099 deleg_node = uu_avl_next(avl, deleg_node)) {
5100 if (local != deleg_node->dpn_perm.dp_local ||
5101 descend != deleg_node->dpn_perm.dp_descend)
5105 const char *who = NULL;
5107 prt_title = B_FALSE;
5108 (void) printf(title);
5112 case ZFS_DELEG_USER_SETS:
5113 case ZFS_DELEG_USER:
5114 who = gettext("user");
5116 who_name = nice_who_name;
5118 case ZFS_DELEG_GROUP_SETS:
5119 case ZFS_DELEG_GROUP:
5120 who = gettext("group");
5122 who_name = nice_who_name;
5124 case ZFS_DELEG_EVERYONE_SETS:
5125 case ZFS_DELEG_EVERYONE:
5126 who = gettext("everyone");
5131 if (who_name == NULL)
5132 (void) printf("\t%s", who);
5134 (void) printf("\t%s %s", who, who_name);
5137 (void) printf("%c%s", delim,
5138 deleg_node->dpn_perm.dp_name);
5143 (void) printf("\n");
5146 uu_avl_walk_end(walk);
5150 print_fs_perms(fs_perm_set_t *fspset)
5152 fs_perm_node_t *node = NULL;
5153 char buf[ZFS_MAXNAMELEN+32];
5154 const char *dsname = buf;
5156 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5157 node = uu_list_next(fspset->fsps_list, node)) {
5158 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5159 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5162 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5163 gettext("---- Permissions on %s "),
5164 node->fspn_fsperm.fsp_name);
5165 (void) printf(dsname);
5166 left = 70 - strlen(buf);
5169 (void) printf("\n");
5171 print_set_creat_perms(sc_avl);
5172 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5173 gettext("Local permissions:\n"));
5174 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5175 gettext("Descendent permissions:\n"));
5176 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5177 gettext("Local+Descendent permissions:\n"));
5181 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5183 struct deleg_perms {
5189 set_deleg_perms(zfs_handle_t *zhp, void *data)
5191 struct deleg_perms *perms = (struct deleg_perms *)data;
5192 zfs_type_t zfs_type = zfs_get_type(zhp);
5194 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5197 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5201 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5204 nvlist_t *perm_nvl = NULL;
5205 nvlist_t *update_perm_nvl = NULL;
5208 struct allow_opts opts = { 0 };
5210 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5213 while ((c = getopt(argc, argv, optstr)) != -1) {
5216 opts.local = B_TRUE;
5219 opts.descend = B_TRUE;
5225 opts.group = B_TRUE;
5228 opts.everyone = B_TRUE;
5234 opts.create = B_TRUE;
5237 opts.recursive = B_TRUE;
5240 (void) fprintf(stderr, gettext("missing argument for "
5241 "'%c' option\n"), optopt);
5245 opts.prt_usage = B_TRUE;
5248 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5257 /* check arguments */
5258 parse_allow_args(argc, argv, un, &opts);
5260 /* try to open the dataset */
5261 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5262 ZFS_TYPE_VOLUME)) == NULL) {
5263 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5268 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5271 fs_perm_set_init(&fs_perm_set);
5272 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5273 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5278 print_fs_perms(&fs_perm_set);
5280 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5281 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5284 if (un && opts.recursive) {
5285 struct deleg_perms data = { un, update_perm_nvl };
5286 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5295 nvlist_free(perm_nvl);
5296 if (update_perm_nvl != NULL)
5297 nvlist_free(update_perm_nvl);
5299 fs_perm_set_fini(&fs_perm_set);
5307 zfs_do_allow(int argc, char **argv)
5309 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5313 zfs_do_unallow(int argc, char **argv)
5315 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5319 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5324 boolean_t recursive = B_FALSE;
5325 const char *opts = holding ? "rt" : "r";
5329 while ((c = getopt(argc, argv, opts)) != -1) {
5335 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5344 /* check number of arguments */
5352 if (holding && tag[0] == '.') {
5353 /* tags starting with '.' are reserved for libzfs */
5354 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5358 for (i = 0; i < argc; ++i) {
5360 char parent[ZFS_MAXNAMELEN];
5362 char *path = argv[i];
5364 delim = strchr(path, '@');
5365 if (delim == NULL) {
5366 (void) fprintf(stderr,
5367 gettext("'%s' is not a snapshot\n"), path);
5371 (void) strncpy(parent, path, delim - path);
5372 parent[delim - path] = '\0';
5374 zhp = zfs_open(g_zfs, parent,
5375 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5381 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5384 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5390 return (errors != 0);
5394 * zfs hold [-r] [-t] <tag> <snap> ...
5396 * -r Recursively hold
5398 * Apply a user-hold with the given tag to the list of snapshots.
5401 zfs_do_hold(int argc, char **argv)
5403 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5407 * zfs release [-r] <tag> <snap> ...
5409 * -r Recursively release
5411 * Release a user-hold with the given tag from the list of snapshots.
5414 zfs_do_release(int argc, char **argv)
5416 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5419 typedef struct holds_cbdata {
5420 boolean_t cb_recursive;
5421 const char *cb_snapname;
5423 size_t cb_max_namelen;
5424 size_t cb_max_taglen;
5427 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5428 #define DATETIME_BUF_LEN (32)
5433 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5436 nvpair_t *nvp = NULL;
5437 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5441 for (i = 0; i < 3; i++) {
5442 col = gettext(hdr_cols[i]);
5444 (void) printf("%-*s ", i ? tagwidth : nwidth,
5447 (void) printf("%s\n", col);
5451 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5452 char *zname = nvpair_name(nvp);
5454 nvpair_t *nvp2 = NULL;
5455 (void) nvpair_value_nvlist(nvp, &nvl2);
5456 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5457 char tsbuf[DATETIME_BUF_LEN];
5458 char *tagname = nvpair_name(nvp2);
5462 char sep = scripted ? '\t' : ' ';
5463 size_t sepnum = scripted ? 1 : 2;
5465 (void) nvpair_value_uint64(nvp2, &val);
5467 (void) localtime_r(&time, &t);
5468 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5469 gettext(STRFTIME_FMT_STR), &t);
5471 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5472 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5478 * Generic callback function to list a dataset or snapshot.
5481 holds_callback(zfs_handle_t *zhp, void *data)
5483 holds_cbdata_t *cbp = data;
5484 nvlist_t *top_nvl = *cbp->cb_nvlp;
5485 nvlist_t *nvl = NULL;
5486 nvpair_t *nvp = NULL;
5487 const char *zname = zfs_get_name(zhp);
5488 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5490 if (cbp->cb_recursive) {
5491 const char *snapname;
5492 char *delim = strchr(zname, '@');
5496 snapname = delim + 1;
5497 if (strcmp(cbp->cb_snapname, snapname))
5501 if (zfs_get_holds(zhp, &nvl) != 0)
5504 if (znamelen > cbp->cb_max_namelen)
5505 cbp->cb_max_namelen = znamelen;
5507 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5508 const char *tag = nvpair_name(nvp);
5509 size_t taglen = strnlen(tag, MAXNAMELEN);
5510 if (taglen > cbp->cb_max_taglen)
5511 cbp->cb_max_taglen = taglen;
5514 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5518 * zfs holds [-r] <snap> ...
5520 * -r Recursively hold
5523 zfs_do_holds(int argc, char **argv)
5528 boolean_t scripted = B_FALSE;
5529 boolean_t recursive = B_FALSE;
5530 const char *opts = "rH";
5533 int types = ZFS_TYPE_SNAPSHOT;
5534 holds_cbdata_t cb = { 0 };
5541 while ((c = getopt(argc, argv, opts)) != -1) {
5550 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5557 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5558 flags |= ZFS_ITER_RECURSE;
5564 /* check number of arguments */
5568 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5571 for (i = 0; i < argc; ++i) {
5572 char *snapshot = argv[i];
5574 const char *snapname;
5576 delim = strchr(snapshot, '@');
5577 if (delim == NULL) {
5578 (void) fprintf(stderr,
5579 gettext("'%s' is not a snapshot\n"), snapshot);
5583 snapname = delim + 1;
5585 snapshot[delim - snapshot] = '\0';
5587 cb.cb_recursive = recursive;
5588 cb.cb_snapname = snapname;
5592 * 1. collect holds data, set format options
5594 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5595 holds_callback, &cb);
5601 * 2. print holds data
5603 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5605 if (nvlist_empty(nvl))
5606 (void) printf(gettext("no datasets available\n"));
5610 return (0 != errors);
5613 #define CHECK_SPINNER 30
5614 #define SPINNER_TIME 3 /* seconds */
5615 #define MOUNT_TIME 5 /* seconds */
5618 get_one_dataset(zfs_handle_t *zhp, void *data)
5620 static char *spin[] = { "-", "\\", "|", "/" };
5621 static int spinval = 0;
5622 static int spincheck = 0;
5623 static time_t last_spin_time = (time_t)0;
5624 get_all_cb_t *cbp = data;
5625 zfs_type_t type = zfs_get_type(zhp);
5627 if (cbp->cb_verbose) {
5628 if (--spincheck < 0) {
5629 time_t now = time(NULL);
5630 if (last_spin_time + SPINNER_TIME < now) {
5631 update_progress(spin[spinval++ % 4]);
5632 last_spin_time = now;
5634 spincheck = CHECK_SPINNER;
5639 * Interate over any nested datasets.
5641 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5647 * Skip any datasets whose type does not match.
5649 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5653 libzfs_add_handle(cbp, zhp);
5654 assert(cbp->cb_used <= cbp->cb_alloc);
5660 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5662 get_all_cb_t cb = { 0 };
5663 cb.cb_verbose = verbose;
5664 cb.cb_getone = get_one_dataset;
5667 set_progress_header(gettext("Reading ZFS config"));
5668 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5670 *dslist = cb.cb_handles;
5671 *count = cb.cb_used;
5674 finish_progress(gettext("done."));
5678 * Generic callback for sharing or mounting filesystems. Because the code is so
5679 * similar, we have a common function with an extra parameter to determine which
5680 * mode we are using.
5682 #define OP_SHARE 0x1
5683 #define OP_MOUNT 0x2
5686 * Share or mount a dataset.
5689 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5690 boolean_t explicit, const char *options)
5692 char mountpoint[ZFS_MAXPROPLEN];
5693 char shareopts[ZFS_MAXPROPLEN];
5694 char smbshareopts[ZFS_MAXPROPLEN];
5695 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5697 uint64_t zoned, canmount;
5698 boolean_t shared_nfs, shared_smb;
5700 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5703 * Check to make sure we can mount/share this dataset. If we
5704 * are in the global zone and the filesystem is exported to a
5705 * local zone, or if we are in a local zone and the
5706 * filesystem is not exported, then it is an error.
5708 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5710 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5714 (void) fprintf(stderr, gettext("cannot %s '%s': "
5715 "dataset is exported to a local zone\n"), cmdname,
5719 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5723 (void) fprintf(stderr, gettext("cannot %s '%s': "
5724 "permission denied\n"), cmdname,
5730 * Ignore any filesystems which don't apply to us. This
5731 * includes those with a legacy mountpoint, or those with
5732 * legacy share options.
5734 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5735 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5736 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5737 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5738 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5739 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5741 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5742 strcmp(smbshareopts, "off") == 0) {
5746 (void) fprintf(stderr, gettext("cannot share '%s': "
5747 "legacy share\n"), zfs_get_name(zhp));
5748 (void) fprintf(stderr, gettext("to "
5749 "share this filesystem set "
5750 "sharenfs property on\n"));
5755 * We cannot share or mount legacy filesystems. If the
5756 * shareopts is non-legacy but the mountpoint is legacy, we
5757 * treat it as a legacy share.
5759 if (strcmp(mountpoint, "legacy") == 0) {
5763 (void) fprintf(stderr, gettext("cannot %s '%s': "
5764 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5765 (void) fprintf(stderr, gettext("use %s(8) to "
5766 "%s this filesystem\n"), cmdname, cmdname);
5770 if (strcmp(mountpoint, "none") == 0) {
5774 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5775 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5780 * canmount explicit outcome
5781 * on no pass through
5782 * on yes pass through
5784 * off yes display error, return 1
5785 * noauto no return 0
5786 * noauto yes pass through
5788 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5789 if (canmount == ZFS_CANMOUNT_OFF) {
5793 (void) fprintf(stderr, gettext("cannot %s '%s': "
5794 "'canmount' property is set to 'off'\n"), cmdname,
5797 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5802 * At this point, we have verified that the mountpoint and/or
5803 * shareopts are appropriate for auto management. If the
5804 * filesystem is already mounted or shared, return (failing
5805 * for explicit requests); otherwise mount or share the
5811 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5812 shared_smb = zfs_is_shared_smb(zhp, NULL);
5814 if (shared_nfs && shared_smb ||
5815 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5816 strcmp(smbshareopts, "off") == 0) ||
5817 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5818 strcmp(shareopts, "off") == 0)) {
5822 (void) fprintf(stderr, gettext("cannot share "
5823 "'%s': filesystem already shared\n"),
5828 if (!zfs_is_mounted(zhp, NULL) &&
5829 zfs_mount(zhp, NULL, 0) != 0)
5832 if (protocol == NULL) {
5833 if (zfs_shareall(zhp) != 0)
5835 } else if (strcmp(protocol, "nfs") == 0) {
5836 if (zfs_share_nfs(zhp))
5838 } else if (strcmp(protocol, "smb") == 0) {
5839 if (zfs_share_smb(zhp))
5842 (void) fprintf(stderr, gettext("cannot share "
5843 "'%s': invalid share type '%s' "
5845 zfs_get_name(zhp), protocol);
5852 if (options == NULL)
5853 mnt.mnt_mntopts = "";
5855 mnt.mnt_mntopts = (char *)options;
5857 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5858 zfs_is_mounted(zhp, NULL)) {
5862 (void) fprintf(stderr, gettext("cannot mount "
5863 "'%s': filesystem already mounted\n"),
5868 if (zfs_mount(zhp, options, flags) != 0)
5877 * Reports progress in the form "(current/total)". Not thread-safe.
5880 report_mount_progress(int current, int total)
5882 static time_t last_progress_time = 0;
5883 time_t now = time(NULL);
5886 /* report 1..n instead of 0..n-1 */
5889 /* display header if we're here for the first time */
5891 set_progress_header(gettext("Mounting ZFS filesystems"));
5892 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5893 /* too soon to report again */
5897 last_progress_time = now;
5899 (void) sprintf(info, "(%d/%d)", current, total);
5901 if (current == total)
5902 finish_progress(info);
5904 update_progress(info);
5908 append_options(char *mntopts, char *newopts)
5910 int len = strlen(mntopts);
5912 /* original length plus new string to append plus 1 for the comma */
5913 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5914 (void) fprintf(stderr, gettext("the opts argument for "
5915 "'%c' option is too long (more than %d chars)\n"),
5916 "-o", MNT_LINE_MAX);
5921 mntopts[len++] = ',';
5923 (void) strcpy(&mntopts[len], newopts);
5927 share_mount(int op, int argc, char **argv)
5930 boolean_t verbose = B_FALSE;
5932 char *options = NULL;
5936 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5946 if (*optarg == '\0') {
5947 (void) fprintf(stderr, gettext("empty mount "
5948 "options (-o) specified\n"));
5952 if (options == NULL)
5953 options = safe_malloc(MNT_LINE_MAX + 1);
5955 /* option validation is done later */
5956 append_options(options, optarg);
5960 warnx("no overlay mounts support on FreeBSD, ignoring");
5963 (void) fprintf(stderr, gettext("missing argument for "
5964 "'%c' option\n"), optopt);
5968 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5977 /* check number of arguments */
5979 zfs_handle_t **dslist = NULL;
5980 size_t i, count = 0;
5981 char *protocol = NULL;
5983 if (op == OP_SHARE && argc > 0) {
5984 if (strcmp(argv[0], "nfs") != 0 &&
5985 strcmp(argv[0], "smb") != 0) {
5986 (void) fprintf(stderr, gettext("share type "
5987 "must be 'nfs' or 'smb'\n"));
5996 (void) fprintf(stderr, gettext("too many arguments\n"));
6000 start_progress_timer();
6001 get_all_datasets(&dslist, &count, verbose);
6006 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6008 for (i = 0; i < count; i++) {
6010 report_mount_progress(i, count);
6012 if (share_mount_one(dslist[i], op, flags, protocol,
6013 B_FALSE, options) != 0)
6015 zfs_close(dslist[i]);
6019 } else if (argc == 0) {
6020 struct mnttab entry;
6022 if ((op == OP_SHARE) || (options != NULL)) {
6023 (void) fprintf(stderr, gettext("missing filesystem "
6024 "argument (specify -a for all)\n"));
6029 * When mount is given no arguments, go through /etc/mnttab and
6030 * display any active ZFS mounts. We hide any snapshots, since
6031 * they are controlled automatically.
6033 rewind(mnttab_file);
6034 while (getmntent(mnttab_file, &entry) == 0) {
6035 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6036 strchr(entry.mnt_special, '@') != NULL)
6039 (void) printf("%-30s %s\n", entry.mnt_special,
6047 (void) fprintf(stderr,
6048 gettext("too many arguments\n"));
6052 if ((zhp = zfs_open(g_zfs, argv[0],
6053 ZFS_TYPE_FILESYSTEM)) == NULL) {
6056 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6066 * zfs mount -a [nfs]
6067 * zfs mount filesystem
6069 * Mount all filesystems, or mount the given filesystem.
6072 zfs_do_mount(int argc, char **argv)
6074 return (share_mount(OP_MOUNT, argc, argv));
6078 * zfs share -a [nfs | smb]
6079 * zfs share filesystem
6081 * Share all filesystems, or share the given filesystem.
6084 zfs_do_share(int argc, char **argv)
6086 return (share_mount(OP_SHARE, argc, argv));
6089 typedef struct unshare_unmount_node {
6090 zfs_handle_t *un_zhp;
6092 uu_avl_node_t un_avlnode;
6093 } unshare_unmount_node_t;
6097 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6099 const unshare_unmount_node_t *l = larg;
6100 const unshare_unmount_node_t *r = rarg;
6102 return (strcmp(l->un_mountp, r->un_mountp));
6106 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6107 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6108 * and unmount it appropriately.
6111 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6115 struct stat64 statbuf;
6116 struct extmnttab entry;
6117 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6121 * Search for the path in /etc/mnttab. Rather than looking for the
6122 * specific path, which can be fooled by non-standard paths (i.e. ".."
6123 * or "//"), we stat() the path and search for the corresponding
6124 * (major,minor) device pair.
6126 if (stat64(path, &statbuf) != 0) {
6127 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6128 cmdname, path, strerror(errno));
6131 path_inode = statbuf.st_ino;
6134 * Search for the given (major,minor) pair in the mount table.
6137 rewind(mnttab_file);
6138 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6139 if (entry.mnt_major == major(statbuf.st_dev) &&
6140 entry.mnt_minor == minor(statbuf.st_dev))
6147 if (statfs(path, &sfs) != 0) {
6148 (void) fprintf(stderr, "%s: %s\n", path,
6152 statfs2mnttab(&sfs, &entry);
6156 if (op == OP_SHARE) {
6157 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6158 "currently mounted\n"), cmdname, path);
6161 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6163 if ((ret = umount2(path, flags)) != 0)
6164 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6169 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6170 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6171 "filesystem\n"), cmdname, path);
6175 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6176 ZFS_TYPE_FILESYSTEM)) == NULL)
6180 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6181 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6182 cmdname, path, strerror(errno));
6184 } else if (statbuf.st_ino != path_inode) {
6185 (void) fprintf(stderr, gettext("cannot "
6186 "%s '%s': not a mountpoint\n"), cmdname, path);
6190 if (op == OP_SHARE) {
6191 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6192 char smbshare_prop[ZFS_MAXPROPLEN];
6194 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6195 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6196 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6197 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6199 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6200 strcmp(smbshare_prop, "off") == 0) {
6201 (void) fprintf(stderr, gettext("cannot unshare "
6202 "'%s': legacy share\n"), path);
6204 (void) fprintf(stderr, gettext("use "
6205 "unshare(1M) to unshare this filesystem\n"));
6207 } else if (!zfs_is_shared(zhp)) {
6208 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6209 "not currently shared\n"), path);
6211 ret = zfs_unshareall_bypath(zhp, path);
6214 char mtpt_prop[ZFS_MAXPROPLEN];
6216 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6217 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6220 ret = zfs_unmount(zhp, NULL, flags);
6221 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6222 (void) fprintf(stderr, gettext("cannot unmount "
6223 "'%s': legacy mountpoint\n"),
6225 (void) fprintf(stderr, gettext("use umount(8) "
6226 "to unmount this filesystem\n"));
6228 ret = zfs_unmountall(zhp, flags);
6239 * Generic callback for unsharing or unmounting a filesystem.
6242 unshare_unmount(int op, int argc, char **argv)
6249 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6250 char sharesmb[ZFS_MAXPROPLEN];
6253 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6262 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6273 * We could make use of zfs_for_each() to walk all datasets in
6274 * the system, but this would be very inefficient, especially
6275 * since we would have to linearly search /etc/mnttab for each
6276 * one. Instead, do one pass through /etc/mnttab looking for
6277 * zfs entries and call zfs_unmount() for each one.
6279 * Things get a little tricky if the administrator has created
6280 * mountpoints beneath other ZFS filesystems. In this case, we
6281 * have to unmount the deepest filesystems first. To accomplish
6282 * this, we place all the mountpoints in an AVL tree sorted by
6283 * the special type (dataset name), and walk the result in
6284 * reverse to make sure to get any snapshots first.
6286 struct mnttab entry;
6287 uu_avl_pool_t *pool;
6289 unshare_unmount_node_t *node;
6291 uu_avl_walk_t *walk;
6294 (void) fprintf(stderr, gettext("too many arguments\n"));
6298 if (((pool = uu_avl_pool_create("unmount_pool",
6299 sizeof (unshare_unmount_node_t),
6300 offsetof(unshare_unmount_node_t, un_avlnode),
6301 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6302 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6305 rewind(mnttab_file);
6306 while (getmntent(mnttab_file, &entry) == 0) {
6308 /* ignore non-ZFS entries */
6309 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6312 /* ignore snapshots */
6313 if (strchr(entry.mnt_special, '@') != NULL)
6316 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6317 ZFS_TYPE_FILESYSTEM)) == NULL) {
6324 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6326 sizeof (nfs_mnt_prop),
6327 NULL, NULL, 0, B_FALSE) == 0);
6328 if (strcmp(nfs_mnt_prop, "off") != 0)
6330 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6332 sizeof (nfs_mnt_prop),
6333 NULL, NULL, 0, B_FALSE) == 0);
6334 if (strcmp(nfs_mnt_prop, "off") == 0)
6338 /* Ignore legacy mounts */
6339 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6341 sizeof (nfs_mnt_prop),
6342 NULL, NULL, 0, B_FALSE) == 0);
6343 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6345 /* Ignore canmount=noauto mounts */
6346 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6347 ZFS_CANMOUNT_NOAUTO)
6353 node = safe_malloc(sizeof (unshare_unmount_node_t));
6355 node->un_mountp = safe_strdup(entry.mnt_mountp);
6357 uu_avl_node_init(node, &node->un_avlnode, pool);
6359 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6360 uu_avl_insert(tree, node, idx);
6362 zfs_close(node->un_zhp);
6363 free(node->un_mountp);
6369 * Walk the AVL tree in reverse, unmounting each filesystem and
6370 * removing it from the AVL tree in the process.
6372 if ((walk = uu_avl_walk_start(tree,
6373 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6376 while ((node = uu_avl_walk_next(walk)) != NULL) {
6377 uu_avl_remove(tree, node);
6381 if (zfs_unshareall_bypath(node->un_zhp,
6382 node->un_mountp) != 0)
6387 if (zfs_unmount(node->un_zhp,
6388 node->un_mountp, flags) != 0)
6393 zfs_close(node->un_zhp);
6394 free(node->un_mountp);
6398 uu_avl_walk_end(walk);
6399 uu_avl_destroy(tree);
6400 uu_avl_pool_destroy(pool);
6405 (void) fprintf(stderr,
6406 gettext("missing filesystem argument\n"));
6408 (void) fprintf(stderr,
6409 gettext("too many arguments\n"));
6414 * We have an argument, but it may be a full path or a ZFS
6415 * filesystem. Pass full paths off to unmount_path() (shared by
6416 * manual_unmount), otherwise open the filesystem and pass to
6419 if (argv[0][0] == '/')
6420 return (unshare_unmount_path(op, argv[0],
6423 if ((zhp = zfs_open(g_zfs, argv[0],
6424 ZFS_TYPE_FILESYSTEM)) == NULL)
6427 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6428 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6429 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6430 NULL, 0, B_FALSE) == 0);
6434 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6436 sizeof (nfs_mnt_prop),
6437 NULL, NULL, 0, B_FALSE) == 0);
6438 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6439 sharesmb, sizeof (sharesmb), NULL, NULL,
6442 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6443 strcmp(sharesmb, "off") == 0) {
6444 (void) fprintf(stderr, gettext("cannot "
6445 "unshare '%s': legacy share\n"),
6448 (void) fprintf(stderr, gettext("use "
6449 "unshare(1M) to unshare this "
6453 } else if (!zfs_is_shared(zhp)) {
6454 (void) fprintf(stderr, gettext("cannot "
6455 "unshare '%s': not currently "
6456 "shared\n"), zfs_get_name(zhp));
6458 } else if (zfs_unshareall(zhp) != 0) {
6464 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6465 (void) fprintf(stderr, gettext("cannot "
6466 "unmount '%s': legacy "
6467 "mountpoint\n"), zfs_get_name(zhp));
6468 (void) fprintf(stderr, gettext("use "
6469 "umount(8) to unmount this "
6472 } else if (!zfs_is_mounted(zhp, NULL)) {
6473 (void) fprintf(stderr, gettext("cannot "
6474 "unmount '%s': not currently "
6478 } else if (zfs_unmountall(zhp, flags) != 0) {
6492 * zfs unmount filesystem
6494 * Unmount all filesystems, or a specific ZFS filesystem.
6497 zfs_do_unmount(int argc, char **argv)
6499 return (unshare_unmount(OP_MOUNT, argc, argv));
6504 * zfs unshare filesystem
6506 * Unshare all filesystems, or a specific ZFS filesystem.
6509 zfs_do_unshare(int argc, char **argv)
6511 return (unshare_unmount(OP_SHARE, argc, argv));
6515 * Attach/detach the given dataset to/from the given jail
6519 do_jail(int argc, char **argv, int attach)
6524 /* check number of arguments */
6526 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6530 (void) fprintf(stderr, gettext("too many arguments\n"));
6534 jailid = jail_getid(argv[1]);
6536 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6540 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6544 ret = (zfs_jail(zhp, jailid, attach) != 0);
6551 * zfs jail jailid filesystem
6553 * Attach the given dataset to the given jail
6557 zfs_do_jail(int argc, char **argv)
6560 return (do_jail(argc, argv, 1));
6564 * zfs unjail jailid filesystem
6566 * Detach the given dataset from the given jail
6570 zfs_do_unjail(int argc, char **argv)
6573 return (do_jail(argc, argv, 0));
6577 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6578 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6581 manual_mount(int argc, char **argv)
6584 char mountpoint[ZFS_MAXPROPLEN];
6585 char mntopts[MNT_LINE_MAX] = { '\0' };
6589 char *dataset, *path;
6592 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6595 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6598 flags |= MS_OVERLAY;
6601 flags |= MS_NOMNTTAB;
6604 (void) fprintf(stderr, gettext("missing argument for "
6605 "'%c' option\n"), optopt);
6609 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6611 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6620 /* check that we only have two arguments */
6623 (void) fprintf(stderr, gettext("missing dataset "
6626 (void) fprintf(stderr,
6627 gettext("missing mountpoint argument\n"));
6629 (void) fprintf(stderr, gettext("too many arguments\n"));
6630 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6637 /* try to open the dataset */
6638 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6641 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6642 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6644 /* check for legacy mountpoint and complain appropriately */
6646 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6647 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6648 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6649 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6654 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6655 "mounted using 'mount -t zfs'\n"), dataset);
6656 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6657 "instead.\n"), path);
6658 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6659 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6660 (void) fprintf(stderr, gettext("See zfs(8) for more "
6669 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6670 * unmounts of non-legacy filesystems, as this is the dominant administrative
6674 manual_unmount(int argc, char **argv)
6680 while ((c = getopt(argc, argv, "f")) != -1) {
6686 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6688 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6697 /* check arguments */
6700 (void) fprintf(stderr, gettext("missing path "
6703 (void) fprintf(stderr, gettext("too many arguments\n"));
6704 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6708 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6712 find_command_idx(char *command, int *idx)
6716 for (i = 0; i < NCOMMAND; i++) {
6717 if (command_table[i].name == NULL)
6720 if (strcmp(command, command_table[i].name) == 0) {
6729 zfs_do_diff(int argc, char **argv)
6733 char *tosnap = NULL;
6734 char *fromsnap = NULL;
6739 while ((c = getopt(argc, argv, "FHt")) != -1) {
6742 flags |= ZFS_DIFF_CLASSIFY;
6745 flags |= ZFS_DIFF_PARSEABLE;
6748 flags |= ZFS_DIFF_TIMESTAMP;
6751 (void) fprintf(stderr,
6752 gettext("invalid option '%c'\n"), optopt);
6761 (void) fprintf(stderr,
6762 gettext("must provide at least one snapshot name\n"));
6767 (void) fprintf(stderr, gettext("too many arguments\n"));
6772 tosnap = (argc == 2) ? argv[1] : NULL;
6775 if (*fromsnap != '@')
6776 copy = strdup(fromsnap);
6778 copy = strdup(tosnap);
6782 if (atp = strchr(copy, '@'))
6785 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6791 * Ignore SIGPIPE so that the library can give us
6792 * information on any failure
6794 (void) sigignore(SIGPIPE);
6796 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6804 * zfs bookmark <fs@snap> <fs#bmark>
6806 * Creates a bookmark with the given name from the given snapshot.
6809 zfs_do_bookmark(int argc, char **argv)
6811 char snapname[ZFS_MAXNAMELEN];
6818 while ((c = getopt(argc, argv, "")) != -1) {
6821 (void) fprintf(stderr,
6822 gettext("invalid option '%c'\n"), optopt);
6830 /* check number of arguments */
6832 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6836 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6840 if (strchr(argv[1], '#') == NULL) {
6841 (void) fprintf(stderr,
6842 gettext("invalid bookmark name '%s' -- "
6843 "must contain a '#'\n"), argv[1]);
6847 if (argv[0][0] == '@') {
6849 * Snapshot name begins with @.
6850 * Default to same fs as bookmark.
6852 (void) strncpy(snapname, argv[1], sizeof (snapname));
6853 *strchr(snapname, '#') = '\0';
6854 (void) strlcat(snapname, argv[0], sizeof (snapname));
6856 (void) strncpy(snapname, argv[0], sizeof (snapname));
6858 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6864 nvl = fnvlist_alloc();
6865 fnvlist_add_string(nvl, argv[1], snapname);
6866 ret = lzc_bookmark(nvl, NULL);
6870 const char *err_msg;
6873 (void) snprintf(errbuf, sizeof (errbuf),
6874 dgettext(TEXT_DOMAIN,
6875 "cannot create bookmark '%s'"), argv[1]);
6879 err_msg = "bookmark is in a different pool";
6882 err_msg = "bookmark exists";
6885 err_msg = "invalid argument";
6888 err_msg = "bookmark feature not enabled";
6891 err_msg = "out of space";
6894 err_msg = "unknown error";
6897 (void) fprintf(stderr, "%s: %s\n", errbuf,
6898 dgettext(TEXT_DOMAIN, err_msg));
6909 main(int argc, char **argv)
6916 (void) setlocale(LC_ALL, "");
6917 (void) textdomain(TEXT_DOMAIN);
6921 if ((g_zfs = libzfs_init()) == NULL) {
6922 (void) fprintf(stderr, gettext("internal error: failed to "
6923 "initialize ZFS library\n"));
6927 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6929 libzfs_print_on_error(g_zfs, B_TRUE);
6931 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6932 (void) fprintf(stderr, gettext("internal error: unable to "
6933 "open %s\n"), MNTTAB);
6938 * This command also doubles as the /etc/fs mount and unmount program.
6939 * Determine if we should take this behavior based on argv[0].
6941 progname = basename(argv[0]);
6942 if (strcmp(progname, "mount") == 0) {
6943 ret = manual_mount(argc, argv);
6944 } else if (strcmp(progname, "umount") == 0) {
6945 ret = manual_unmount(argc, argv);
6948 * Make sure the user has specified some command.
6951 (void) fprintf(stderr, gettext("missing command\n"));
6958 * The 'umount' command is an alias for 'unmount'
6960 if (strcmp(cmdname, "umount") == 0)
6961 cmdname = "unmount";
6964 * The 'recv' command is an alias for 'receive'
6966 if (strcmp(cmdname, "recv") == 0)
6967 cmdname = "receive";
6970 * The 'snap' command is an alias for 'snapshot'
6972 if (strcmp(cmdname, "snap") == 0)
6973 cmdname = "snapshot";
6978 if (strcmp(cmdname, "-?") == 0)
6982 * Run the appropriate command.
6984 libzfs_mnttab_cache(g_zfs, B_TRUE);
6985 if (find_command_idx(cmdname, &i) == 0) {
6986 current_command = &command_table[i];
6987 ret = command_table[i].func(argc - 1, argv + 1);
6988 } else if (strchr(cmdname, '=') != NULL) {
6989 verify(find_command_idx("set", &i) == 0);
6990 current_command = &command_table[i];
6991 ret = command_table[i].func(argc, argv);
6993 (void) fprintf(stderr, gettext("unrecognized "
6994 "command '%s'\n"), cmdname);
6997 libzfs_mnttab_cache(g_zfs, B_FALSE);
7000 (void) fclose(mnttab_file);
7002 if (ret == 0 && log_history)
7003 (void) zpool_log_history(g_zfs, history_str);
7008 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7009 * for the purposes of running ::findleaks.
7011 if (getenv("ZFS_ABORT") != NULL) {
7012 (void) printf("dumping core by request\n");