4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * All rights reserved.
29 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
30 * Copyright (c) 2013 Steven Hartland. All rights reserved.
31 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
40 #include <libnvpair.h>
53 #include <sys/mntent.h>
54 #include <sys/mnttab.h>
55 #include <sys/mount.h>
57 #include <sys/fs/zfs.h>
58 #include <sys/types.h>
62 #include <libzfs_core.h>
64 #include <zfs_deleg.h>
68 #include <directory.h>
73 #include "zfs_comutil.h"
75 libzfs_handle_t *g_zfs;
77 static FILE *mnttab_file;
78 static char history_str[HIS_MAX_RECORD_LEN];
79 static boolean_t log_history = B_TRUE;
81 static int zfs_do_clone(int argc, char **argv);
82 static int zfs_do_create(int argc, char **argv);
83 static int zfs_do_destroy(int argc, char **argv);
84 static int zfs_do_get(int argc, char **argv);
85 static int zfs_do_inherit(int argc, char **argv);
86 static int zfs_do_list(int argc, char **argv);
87 static int zfs_do_mount(int argc, char **argv);
88 static int zfs_do_rename(int argc, char **argv);
89 static int zfs_do_rollback(int argc, char **argv);
90 static int zfs_do_set(int argc, char **argv);
91 static int zfs_do_upgrade(int argc, char **argv);
92 static int zfs_do_snapshot(int argc, char **argv);
93 static int zfs_do_unmount(int argc, char **argv);
94 static int zfs_do_share(int argc, char **argv);
95 static int zfs_do_unshare(int argc, char **argv);
96 static int zfs_do_send(int argc, char **argv);
97 static int zfs_do_receive(int argc, char **argv);
98 static int zfs_do_promote(int argc, char **argv);
99 static int zfs_do_userspace(int argc, char **argv);
100 static int zfs_do_allow(int argc, char **argv);
101 static int zfs_do_unallow(int argc, char **argv);
102 static int zfs_do_hold(int argc, char **argv);
103 static int zfs_do_holds(int argc, char **argv);
104 static int zfs_do_release(int argc, char **argv);
105 static int zfs_do_diff(int argc, char **argv);
106 static int zfs_do_jail(int argc, char **argv);
107 static int zfs_do_unjail(int argc, char **argv);
108 static int zfs_do_bookmark(int argc, char **argv);
111 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
116 _umem_debug_init(void)
118 return ("default,verbose"); /* $UMEM_DEBUG setting */
122 _umem_logging_init(void)
124 return ("fail,contents"); /* $UMEM_LOGGING setting */
160 typedef struct zfs_command {
162 int (*func)(int argc, char **argv);
167 * Master command table. Each ZFS command has a name, associated function, and
168 * usage message. The usage messages need to be internationalized, so we have
169 * to have a function to return the usage message based on a command index.
171 * These commands are organized according to how they are displayed in the usage
172 * message. An empty command (one with a NULL name) indicates an empty line in
173 * the generic usage message.
175 static zfs_command_t command_table[] = {
176 { "create", zfs_do_create, HELP_CREATE },
177 { "destroy", zfs_do_destroy, HELP_DESTROY },
179 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
180 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
181 { "clone", zfs_do_clone, HELP_CLONE },
182 { "promote", zfs_do_promote, HELP_PROMOTE },
183 { "rename", zfs_do_rename, HELP_RENAME },
184 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
186 { "list", zfs_do_list, HELP_LIST },
188 { "set", zfs_do_set, HELP_SET },
189 { "get", zfs_do_get, HELP_GET },
190 { "inherit", zfs_do_inherit, HELP_INHERIT },
191 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
192 { "userspace", zfs_do_userspace, HELP_USERSPACE },
193 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
195 { "mount", zfs_do_mount, HELP_MOUNT },
196 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
197 { "share", zfs_do_share, HELP_SHARE },
198 { "unshare", zfs_do_unshare, HELP_UNSHARE },
200 { "send", zfs_do_send, HELP_SEND },
201 { "receive", zfs_do_receive, HELP_RECEIVE },
203 { "allow", zfs_do_allow, HELP_ALLOW },
205 { "unallow", zfs_do_unallow, HELP_UNALLOW },
207 { "hold", zfs_do_hold, HELP_HOLD },
208 { "holds", zfs_do_holds, HELP_HOLDS },
209 { "release", zfs_do_release, HELP_RELEASE },
210 { "diff", zfs_do_diff, HELP_DIFF },
212 { "jail", zfs_do_jail, HELP_JAIL },
213 { "unjail", zfs_do_unjail, HELP_UNJAIL },
216 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
218 zfs_command_t *current_command;
221 get_usage(zfs_help_t idx)
225 return (gettext("\tclone [-p] [-o property=value] ... "
226 "<snapshot> <filesystem|volume>\n"));
228 return (gettext("\tcreate [-p] [-o property=value] ... "
230 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
231 "-V <size> <volume>\n"));
233 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
234 "\tdestroy [-dnpRrv] "
235 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
236 "\tdestroy <filesystem|volume>#<bookmark>\n"));
238 return (gettext("\tget [-rHp] [-d max] "
239 "[-o \"all\" | field[,...]]\n"
240 "\t [-t type[,...]] [-s source[,...]]\n"
241 "\t <\"all\" | property[,...]> "
242 "[filesystem|volume|snapshot] ...\n"));
244 return (gettext("\tinherit [-rS] <property> "
245 "<filesystem|volume|snapshot> ...\n"));
247 return (gettext("\tupgrade [-v]\n"
248 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
250 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
252 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
254 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
255 "[-s property]...\n\t [-S property]... [-t type[,...]] "
256 "[filesystem|volume|snapshot] ...\n"));
258 return (gettext("\tmount\n"
259 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
261 return (gettext("\tpromote <clone-filesystem>\n"));
263 return (gettext("\treceive|recv [-vnFu] <filesystem|volume|"
265 "\treceive|recv [-vnFu] [-d | -e] <filesystem>\n"));
267 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
268 "<filesystem|volume|snapshot>\n"
269 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
270 "\trename -r <snapshot> <snapshot>\n"
271 "\trename -u [-p] <filesystem> <filesystem>"));
273 return (gettext("\trollback [-rRf] <snapshot>\n"));
275 return (gettext("\tsend [-DnPpRv] [-[iI] snapshot] "
277 "\tsend [-i snapshot|bookmark] "
278 "<filesystem|volume|snapshot>\n"));
280 return (gettext("\tset <property=value> "
281 "<filesystem|volume|snapshot> ...\n"));
283 return (gettext("\tshare <-a | filesystem>\n"));
285 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
286 "<filesystem|volume>@<snap> ...\n"));
288 return (gettext("\tunmount|umount [-f] "
289 "<-a | filesystem|mountpoint>\n"));
291 return (gettext("\tunshare "
292 "<-a | filesystem|mountpoint>\n"));
294 return (gettext("\tallow <filesystem|volume>\n"
296 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
297 "\t <filesystem|volume>\n"
298 "\tallow [-ld] -e <perm|@setname>[,...] "
299 "<filesystem|volume>\n"
300 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
301 "\tallow -s @setname <perm|@setname>[,...] "
302 "<filesystem|volume>\n"));
304 return (gettext("\tunallow [-rldug] "
305 "<\"everyone\"|user|group>[,...]\n"
306 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
307 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
308 "<filesystem|volume>\n"
309 "\tunallow [-r] -c [<perm|@setname>[,...]] "
310 "<filesystem|volume>\n"
311 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
312 "<filesystem|volume>\n"));
314 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
316 "\t [-S field] ... [-t type[,...]] "
317 "<filesystem|snapshot>\n"));
318 case HELP_GROUPSPACE:
319 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
321 "\t [-S field] ... [-t type[,...]] "
322 "<filesystem|snapshot>\n"));
324 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
326 return (gettext("\tholds [-r] <snapshot> ...\n"));
328 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
330 return (gettext("\tdiff [-FHt] <snapshot> "
331 "[snapshot|filesystem]\n"));
333 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
343 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
348 * Utility function to guarantee malloc() success.
352 safe_malloc(size_t size)
356 if ((data = calloc(1, size)) == NULL)
363 safe_strdup(char *str)
365 char *dupstr = strdup(str);
374 * Callback routine that will print out information for each of
378 usage_prop_cb(int prop, void *cb)
382 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
384 if (zfs_prop_readonly(prop))
385 (void) fprintf(fp, " NO ");
387 (void) fprintf(fp, "YES ");
389 if (zfs_prop_inheritable(prop))
390 (void) fprintf(fp, " YES ");
392 (void) fprintf(fp, " NO ");
394 if (zfs_prop_values(prop) == NULL)
395 (void) fprintf(fp, "-\n");
397 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
403 * Display usage message. If we're inside a command, display only the usage for
404 * that command. Otherwise, iterate over the entire command table and display
405 * a complete usage message.
408 usage(boolean_t requested)
411 boolean_t show_properties = B_FALSE;
412 FILE *fp = requested ? stdout : stderr;
414 if (current_command == NULL) {
416 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
418 gettext("where 'command' is one of the following:\n\n"));
420 for (i = 0; i < NCOMMAND; i++) {
421 if (command_table[i].name == NULL)
422 (void) fprintf(fp, "\n");
424 (void) fprintf(fp, "%s",
425 get_usage(command_table[i].usage));
428 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
429 "pool/[dataset/]*dataset[@name]\n"));
431 (void) fprintf(fp, gettext("usage:\n"));
432 (void) fprintf(fp, "%s", get_usage(current_command->usage));
435 if (current_command != NULL &&
436 (strcmp(current_command->name, "set") == 0 ||
437 strcmp(current_command->name, "get") == 0 ||
438 strcmp(current_command->name, "inherit") == 0 ||
439 strcmp(current_command->name, "list") == 0))
440 show_properties = B_TRUE;
442 if (show_properties) {
444 gettext("\nThe following properties are supported:\n"));
446 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
447 "PROPERTY", "EDIT", "INHERIT", "VALUES");
449 /* Iterate over all properties */
450 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
453 (void) fprintf(fp, "\t%-15s ", "userused@...");
454 (void) fprintf(fp, " NO NO <size>\n");
455 (void) fprintf(fp, "\t%-15s ", "groupused@...");
456 (void) fprintf(fp, " NO NO <size>\n");
457 (void) fprintf(fp, "\t%-15s ", "userquota@...");
458 (void) fprintf(fp, "YES NO <size> | none\n");
459 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
460 (void) fprintf(fp, "YES NO <size> | none\n");
461 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
462 (void) fprintf(fp, " NO NO <size>\n");
464 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
465 "with standard units such as K, M, G, etc.\n"));
466 (void) fprintf(fp, gettext("\nUser-defined properties can "
467 "be specified by using a name containing a colon (:).\n"));
468 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
469 "properties must be appended with\n"
470 "a user or group specifier of one of these forms:\n"
471 " POSIX name (eg: \"matt\")\n"
472 " POSIX id (eg: \"126829\")\n"
473 " SMB name@domain (eg: \"matt@sun\")\n"
474 " SMB SID (eg: \"S-1-234-567-89\")\n"));
477 gettext("\nFor the property list, run: %s\n"),
480 gettext("\nFor the delegated permission list, run: %s\n"),
481 "zfs allow|unallow");
485 * See comments at end of main().
487 if (getenv("ZFS_ABORT") != NULL) {
488 (void) printf("dumping core by request\n");
492 exit(requested ? 0 : 2);
496 parseprop(nvlist_t *props, char *propname)
498 char *propval, *strval;
500 if ((propval = strchr(propname, '=')) == NULL) {
501 (void) fprintf(stderr, gettext("missing "
502 "'=' for -o option\n"));
507 if (nvlist_lookup_string(props, propname, &strval) == 0) {
508 (void) fprintf(stderr, gettext("property '%s' "
509 "specified multiple times\n"), propname);
512 if (nvlist_add_string(props, propname, propval) != 0)
518 parse_depth(char *opt, int *flags)
523 depth = (int)strtol(opt, &tmp, 0);
525 (void) fprintf(stderr,
526 gettext("%s is not an integer\n"), opt);
530 (void) fprintf(stderr,
531 gettext("Depth can not be negative.\n"));
534 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
538 #define PROGRESS_DELAY 2 /* seconds */
540 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";
541 static time_t pt_begin;
542 static char *pt_header = NULL;
543 static boolean_t pt_shown;
546 start_progress_timer(void)
548 pt_begin = time(NULL) + PROGRESS_DELAY;
553 set_progress_header(char *header)
555 assert(pt_header == NULL);
556 pt_header = safe_strdup(header);
558 (void) printf("%s: ", header);
559 (void) fflush(stdout);
564 update_progress(char *update)
566 if (!pt_shown && time(NULL) > pt_begin) {
567 int len = strlen(update);
569 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
571 (void) fflush(stdout);
573 } else if (pt_shown) {
574 int len = strlen(update);
576 (void) printf("%s%*.*s", update, len, len, pt_reverse);
577 (void) fflush(stdout);
582 finish_progress(char *done)
585 (void) printf("%s\n", done);
586 (void) fflush(stdout);
592 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
594 * Given an existing dataset, create a writable copy whose initial contents
595 * are the same as the source. The newly created dataset maintains a
596 * dependency on the original; the original cannot be destroyed so long as
599 * The '-p' flag creates all the non-existing ancestors of the target first.
602 zfs_do_clone(int argc, char **argv)
604 zfs_handle_t *zhp = NULL;
605 boolean_t parents = B_FALSE;
610 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
614 while ((c = getopt(argc, argv, "o:p")) != -1) {
617 if (parseprop(props, optarg))
624 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
633 /* check number of arguments */
635 (void) fprintf(stderr, gettext("missing source dataset "
640 (void) fprintf(stderr, gettext("missing target dataset "
645 (void) fprintf(stderr, gettext("too many arguments\n"));
649 /* open the source dataset */
650 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
653 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
656 * Now create the ancestors of the target dataset. If the
657 * target already exists and '-p' option was used we should not
660 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
663 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
668 ret = zfs_clone(zhp, argv[1], props);
670 /* create the mountpoint if necessary */
674 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
676 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
677 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
678 ret = zfs_share(clone);
697 * zfs create [-p] [-o prop=value] ... fs
698 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
700 * Create a new dataset. This command can be used to create filesystems
701 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
702 * For volumes, the user must specify a size to be used.
704 * The '-s' flag applies only to volumes, and indicates that we should not try
705 * to set the reservation for this volume. By default we set a reservation
706 * equal to the size for any volume. For pools with SPA_VERSION >=
707 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
709 * The '-p' flag creates all the non-existing ancestors of the target first.
712 zfs_do_create(int argc, char **argv)
714 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
715 zfs_handle_t *zhp = NULL;
718 boolean_t noreserve = B_FALSE;
719 boolean_t bflag = B_FALSE;
720 boolean_t parents = B_FALSE;
724 int canmount = ZFS_CANMOUNT_OFF;
726 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
730 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
733 type = ZFS_TYPE_VOLUME;
734 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
735 (void) fprintf(stderr, gettext("bad volume "
736 "size '%s': %s\n"), optarg,
737 libzfs_error_description(g_zfs));
741 if (nvlist_add_uint64(props,
742 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
751 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
752 (void) fprintf(stderr, gettext("bad volume "
753 "block size '%s': %s\n"), optarg,
754 libzfs_error_description(g_zfs));
758 if (nvlist_add_uint64(props,
759 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
764 if (parseprop(props, optarg))
771 (void) fprintf(stderr, gettext("missing size "
775 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
781 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
782 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
783 "used when creating a volume\n"));
790 /* check number of arguments */
792 (void) fprintf(stderr, gettext("missing %s argument\n"),
793 zfs_type_to_name(type));
797 (void) fprintf(stderr, gettext("too many arguments\n"));
801 if (type == ZFS_TYPE_VOLUME && !noreserve) {
802 zpool_handle_t *zpool_handle;
803 uint64_t spa_version;
805 zfs_prop_t resv_prop;
808 if (p = strchr(argv[0], '/'))
810 zpool_handle = zpool_open(g_zfs, argv[0]);
813 if (zpool_handle == NULL)
815 spa_version = zpool_get_prop_int(zpool_handle,
816 ZPOOL_PROP_VERSION, NULL);
817 zpool_close(zpool_handle);
818 if (spa_version >= SPA_VERSION_REFRESERVATION)
819 resv_prop = ZFS_PROP_REFRESERVATION;
821 resv_prop = ZFS_PROP_RESERVATION;
822 volsize = zvol_volsize_to_reservation(volsize, props);
824 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
826 if (nvlist_add_uint64(props,
827 zfs_prop_to_name(resv_prop), volsize) != 0) {
834 if (parents && zfs_name_valid(argv[0], type)) {
836 * Now create the ancestors of target dataset. If the target
837 * already exists and '-p' option was used we should not
840 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
844 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
849 if (zfs_create(g_zfs, argv[0], type, props) != 0)
852 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
857 * if the user doesn't want the dataset automatically mounted,
858 * then skip the mount/share step
860 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
861 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
864 * Mount and/or share the new filesystem as appropriate. We provide a
865 * verbose error message to let the user know that their filesystem was
866 * in fact created, even if we failed to mount or share it.
868 if (canmount == ZFS_CANMOUNT_ON) {
869 if (zfs_mount(zhp, NULL, 0) != 0) {
870 (void) fprintf(stderr, gettext("filesystem "
871 "successfully created, but not mounted\n"));
873 } else if (zfs_share(zhp) != 0) {
874 (void) fprintf(stderr, gettext("filesystem "
875 "successfully created, but not shared\n"));
892 * zfs destroy [-rRf] <fs, vol>
893 * zfs destroy [-rRd] <snap>
895 * -r Recursively destroy all children
896 * -R Recursively destroy all dependents, including clones
897 * -f Force unmounting of any dependents
898 * -d If we can't destroy now, mark for deferred destruction
900 * Destroys the given dataset. By default, it will unmount any filesystems,
901 * and refuse to destroy a dataset that has any dependents. A dependent can
902 * either be a child, or a clone of a child.
904 typedef struct destroy_cbdata {
907 boolean_t cb_recurse;
909 boolean_t cb_doclones;
910 zfs_handle_t *cb_target;
911 boolean_t cb_defer_destroy;
912 boolean_t cb_verbose;
913 boolean_t cb_parsable;
916 nvlist_t *cb_batchedsnaps;
918 /* first snap in contiguous run */
920 /* previous snap in contiguous run */
928 * Check for any dependents based on the '-r' or '-R' flags.
931 destroy_check_dependent(zfs_handle_t *zhp, void *data)
933 destroy_cbdata_t *cbp = data;
934 const char *tname = zfs_get_name(cbp->cb_target);
935 const char *name = zfs_get_name(zhp);
937 if (strncmp(tname, name, strlen(tname)) == 0 &&
938 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
940 * This is a direct descendant, not a clone somewhere else in
947 (void) fprintf(stderr, gettext("cannot destroy '%s': "
948 "%s has children\n"),
949 zfs_get_name(cbp->cb_target),
950 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
951 (void) fprintf(stderr, gettext("use '-r' to destroy "
952 "the following datasets:\n"));
953 cbp->cb_first = B_FALSE;
954 cbp->cb_error = B_TRUE;
957 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
960 * This is a clone. We only want to report this if the '-r'
961 * wasn't specified, or the target is a snapshot.
963 if (!cbp->cb_recurse &&
964 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
968 (void) fprintf(stderr, gettext("cannot destroy '%s': "
969 "%s has dependent clones\n"),
970 zfs_get_name(cbp->cb_target),
971 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
972 (void) fprintf(stderr, gettext("use '-R' to destroy "
973 "the following datasets:\n"));
974 cbp->cb_first = B_FALSE;
975 cbp->cb_error = B_TRUE;
976 cbp->cb_dryrun = B_TRUE;
979 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
988 destroy_callback(zfs_handle_t *zhp, void *data)
990 destroy_cbdata_t *cb = data;
991 const char *name = zfs_get_name(zhp);
993 if (cb->cb_verbose) {
994 if (cb->cb_parsable) {
995 (void) printf("destroy\t%s\n", name);
996 } else if (cb->cb_dryrun) {
997 (void) printf(gettext("would destroy %s\n"),
1000 (void) printf(gettext("will destroy %s\n"),
1006 * Ignore pools (which we've already flagged as an error before getting
1009 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1010 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1014 if (cb->cb_dryrun) {
1020 * We batch up all contiguous snapshots (even of different
1021 * filesystems) and destroy them with one ioctl. We can't
1022 * simply do all snap deletions and then all fs deletions,
1023 * because we must delete a clone before its origin.
1025 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1026 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1028 int error = zfs_destroy_snaps_nvl(g_zfs,
1029 cb->cb_batchedsnaps, B_FALSE);
1030 fnvlist_free(cb->cb_batchedsnaps);
1031 cb->cb_batchedsnaps = fnvlist_alloc();
1034 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1035 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1046 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1048 destroy_cbdata_t *cb = arg;
1049 const char *name = zfs_get_name(zhp);
1052 if (nvlist_exists(cb->cb_nvl, name)) {
1053 if (cb->cb_firstsnap == NULL)
1054 cb->cb_firstsnap = strdup(name);
1055 if (cb->cb_prevsnap != NULL)
1056 free(cb->cb_prevsnap);
1057 /* this snap continues the current range */
1058 cb->cb_prevsnap = strdup(name);
1059 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1061 if (cb->cb_verbose) {
1062 if (cb->cb_parsable) {
1063 (void) printf("destroy\t%s\n", name);
1064 } else if (cb->cb_dryrun) {
1065 (void) printf(gettext("would destroy %s\n"),
1068 (void) printf(gettext("will destroy %s\n"),
1072 } else if (cb->cb_firstsnap != NULL) {
1073 /* end of this range */
1075 err = lzc_snaprange_space(cb->cb_firstsnap,
1076 cb->cb_prevsnap, &used);
1077 cb->cb_snapused += used;
1078 free(cb->cb_firstsnap);
1079 cb->cb_firstsnap = NULL;
1080 free(cb->cb_prevsnap);
1081 cb->cb_prevsnap = NULL;
1088 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1091 assert(cb->cb_firstsnap == NULL);
1092 assert(cb->cb_prevsnap == NULL);
1093 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1094 if (cb->cb_firstsnap != NULL) {
1097 err = lzc_snaprange_space(cb->cb_firstsnap,
1098 cb->cb_prevsnap, &used);
1100 cb->cb_snapused += used;
1101 free(cb->cb_firstsnap);
1102 cb->cb_firstsnap = NULL;
1103 free(cb->cb_prevsnap);
1104 cb->cb_prevsnap = NULL;
1110 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1112 destroy_cbdata_t *cb = arg;
1115 /* Check for clones. */
1116 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1117 cb->cb_target = zhp;
1118 cb->cb_first = B_TRUE;
1119 err = zfs_iter_dependents(zhp, B_TRUE,
1120 destroy_check_dependent, cb);
1124 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1132 gather_snapshots(zfs_handle_t *zhp, void *arg)
1134 destroy_cbdata_t *cb = arg;
1137 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1143 if (cb->cb_verbose) {
1144 err = destroy_print_snapshots(zhp, cb);
1150 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1158 destroy_clones(destroy_cbdata_t *cb)
1161 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1163 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1164 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1167 boolean_t defer = cb->cb_defer_destroy;
1171 * We can't defer destroy non-snapshots, so set it to
1172 * false while destroying the clones.
1174 cb->cb_defer_destroy = B_FALSE;
1175 err = zfs_iter_dependents(zhp, B_FALSE,
1176 destroy_callback, cb);
1177 cb->cb_defer_destroy = defer;
1187 zfs_do_destroy(int argc, char **argv)
1189 destroy_cbdata_t cb = { 0 };
1193 zfs_handle_t *zhp = NULL;
1195 zfs_type_t type = ZFS_TYPE_DATASET;
1198 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1201 cb.cb_verbose = B_TRUE;
1204 cb.cb_verbose = B_TRUE;
1205 cb.cb_parsable = B_TRUE;
1208 cb.cb_dryrun = B_TRUE;
1211 cb.cb_defer_destroy = B_TRUE;
1212 type = ZFS_TYPE_SNAPSHOT;
1215 cb.cb_force = B_TRUE;
1218 cb.cb_recurse = B_TRUE;
1221 cb.cb_recurse = B_TRUE;
1222 cb.cb_doclones = B_TRUE;
1226 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1235 /* check number of arguments */
1237 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1241 (void) fprintf(stderr, gettext("too many arguments\n"));
1245 at = strchr(argv[0], '@');
1246 pound = strchr(argv[0], '#');
1249 /* Build the list of snaps to destroy in cb_nvl. */
1250 cb.cb_nvl = fnvlist_alloc();
1253 zhp = zfs_open(g_zfs, argv[0],
1254 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1258 cb.cb_snapspec = at + 1;
1259 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1265 if (nvlist_empty(cb.cb_nvl)) {
1266 (void) fprintf(stderr, gettext("could not find any "
1267 "snapshots to destroy; check snapshot names.\n"));
1272 if (cb.cb_verbose) {
1274 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1275 if (cb.cb_parsable) {
1276 (void) printf("reclaim\t%llu\n",
1278 } else if (cb.cb_dryrun) {
1279 (void) printf(gettext("would reclaim %s\n"),
1282 (void) printf(gettext("will reclaim %s\n"),
1287 if (!cb.cb_dryrun) {
1288 if (cb.cb_doclones) {
1289 cb.cb_batchedsnaps = fnvlist_alloc();
1290 err = destroy_clones(&cb);
1292 err = zfs_destroy_snaps_nvl(g_zfs,
1293 cb.cb_batchedsnaps, B_FALSE);
1301 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1302 cb.cb_defer_destroy);
1308 } else if (pound != NULL) {
1313 (void) fprintf(stderr,
1314 "dryrun is not supported with bookmark\n");
1318 if (cb.cb_defer_destroy) {
1319 (void) fprintf(stderr,
1320 "defer destroy is not supported with bookmark\n");
1324 if (cb.cb_recurse) {
1325 (void) fprintf(stderr,
1326 "recursive is not supported with bookmark\n");
1330 if (!zfs_bookmark_exists(argv[0])) {
1331 (void) fprintf(stderr, gettext("bookmark '%s' "
1332 "does not exist.\n"), argv[0]);
1336 nvl = fnvlist_alloc();
1337 fnvlist_add_boolean(nvl, argv[0]);
1339 err = lzc_destroy_bookmarks(nvl, NULL);
1341 (void) zfs_standard_error(g_zfs, err,
1342 "cannot destroy bookmark");
1345 nvlist_free(cb.cb_nvl);
1349 /* Open the given dataset */
1350 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1356 * Perform an explicit check for pools before going any further.
1358 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1359 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1360 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1361 "operation does not apply to pools\n"),
1363 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1364 "%s' to destroy all datasets in the pool\n"),
1366 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1367 "to destroy the pool itself\n"), zfs_get_name(zhp));
1373 * Check for any dependents and/or clones.
1375 cb.cb_first = B_TRUE;
1376 if (!cb.cb_doclones &&
1377 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1388 cb.cb_batchedsnaps = fnvlist_alloc();
1389 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1396 * Do the real thing. The callback will close the
1397 * handle regardless of whether it succeeds or not.
1399 err = destroy_callback(zhp, &cb);
1402 err = zfs_destroy_snaps_nvl(g_zfs,
1403 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1410 fnvlist_free(cb.cb_batchedsnaps);
1411 fnvlist_free(cb.cb_nvl);
1418 is_recvd_column(zprop_get_cbdata_t *cbp)
1421 zfs_get_column_t col;
1423 for (i = 0; i < ZFS_GET_NCOLS &&
1424 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1425 if (col == GET_COL_RECVD)
1431 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1432 * < all | property[,property]... > < fs | snap | vol > ...
1434 * -r recurse over any child datasets
1435 * -H scripted mode. Headers are stripped, and fields are separated
1436 * by tabs instead of spaces.
1437 * -o Set of fields to display. One of "name,property,value,
1438 * received,source". Default is "name,property,value,source".
1439 * "all" is an alias for all five.
1440 * -s Set of sources to allow. One of
1441 * "local,default,inherited,received,temporary,none". Default is
1443 * -p Display values in parsable (literal) format.
1445 * Prints properties for the given datasets. The user can control which
1446 * columns to display as well as which property types to allow.
1450 * Invoked to display the properties for a single dataset.
1453 get_callback(zfs_handle_t *zhp, void *data)
1455 char buf[ZFS_MAXPROPLEN];
1456 char rbuf[ZFS_MAXPROPLEN];
1457 zprop_source_t sourcetype;
1458 char source[ZFS_MAXNAMELEN];
1459 zprop_get_cbdata_t *cbp = data;
1460 nvlist_t *user_props = zfs_get_user_props(zhp);
1461 zprop_list_t *pl = cbp->cb_proplist;
1465 boolean_t received = is_recvd_column(cbp);
1467 for (; pl != NULL; pl = pl->pl_next) {
1468 char *recvdval = NULL;
1470 * Skip the special fake placeholder. This will also skip over
1471 * the name property when 'all' is specified.
1473 if (pl->pl_prop == ZFS_PROP_NAME &&
1474 pl == cbp->cb_proplist)
1477 if (pl->pl_prop != ZPROP_INVAL) {
1478 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1479 sizeof (buf), &sourcetype, source,
1481 cbp->cb_literal) != 0) {
1484 if (!zfs_prop_valid_for_type(pl->pl_prop,
1485 ZFS_TYPE_DATASET)) {
1486 (void) fprintf(stderr,
1487 gettext("No such property '%s'\n"),
1488 zfs_prop_to_name(pl->pl_prop));
1491 sourcetype = ZPROP_SRC_NONE;
1492 (void) strlcpy(buf, "-", sizeof (buf));
1495 if (received && (zfs_prop_get_recvd(zhp,
1496 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1497 cbp->cb_literal) == 0))
1500 zprop_print_one_property(zfs_get_name(zhp), cbp,
1501 zfs_prop_to_name(pl->pl_prop),
1502 buf, sourcetype, source, recvdval);
1503 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1504 sourcetype = ZPROP_SRC_LOCAL;
1506 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1507 buf, sizeof (buf), cbp->cb_literal) != 0) {
1508 sourcetype = ZPROP_SRC_NONE;
1509 (void) strlcpy(buf, "-", sizeof (buf));
1512 zprop_print_one_property(zfs_get_name(zhp), cbp,
1513 pl->pl_user_prop, buf, sourcetype, source, NULL);
1514 } else if (zfs_prop_written(pl->pl_user_prop)) {
1515 sourcetype = ZPROP_SRC_LOCAL;
1517 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1518 buf, sizeof (buf), cbp->cb_literal) != 0) {
1519 sourcetype = ZPROP_SRC_NONE;
1520 (void) strlcpy(buf, "-", sizeof (buf));
1523 zprop_print_one_property(zfs_get_name(zhp), cbp,
1524 pl->pl_user_prop, buf, sourcetype, source, NULL);
1526 if (nvlist_lookup_nvlist(user_props,
1527 pl->pl_user_prop, &propval) != 0) {
1530 sourcetype = ZPROP_SRC_NONE;
1533 verify(nvlist_lookup_string(propval,
1534 ZPROP_VALUE, &strval) == 0);
1535 verify(nvlist_lookup_string(propval,
1536 ZPROP_SOURCE, &sourceval) == 0);
1538 if (strcmp(sourceval,
1539 zfs_get_name(zhp)) == 0) {
1540 sourcetype = ZPROP_SRC_LOCAL;
1541 } else if (strcmp(sourceval,
1542 ZPROP_SOURCE_VAL_RECVD) == 0) {
1543 sourcetype = ZPROP_SRC_RECEIVED;
1545 sourcetype = ZPROP_SRC_INHERITED;
1546 (void) strlcpy(source,
1547 sourceval, sizeof (source));
1551 if (received && (zfs_prop_get_recvd(zhp,
1552 pl->pl_user_prop, rbuf, sizeof (rbuf),
1553 cbp->cb_literal) == 0))
1556 zprop_print_one_property(zfs_get_name(zhp), cbp,
1557 pl->pl_user_prop, strval, sourcetype,
1566 zfs_do_get(int argc, char **argv)
1568 zprop_get_cbdata_t cb = { 0 };
1569 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1570 int types = ZFS_TYPE_DATASET;
1571 char *value, *fields;
1574 zprop_list_t fake_name = { 0 };
1577 * Set up default columns and sources.
1579 cb.cb_sources = ZPROP_SRC_ALL;
1580 cb.cb_columns[0] = GET_COL_NAME;
1581 cb.cb_columns[1] = GET_COL_PROPERTY;
1582 cb.cb_columns[2] = GET_COL_VALUE;
1583 cb.cb_columns[3] = GET_COL_SOURCE;
1584 cb.cb_type = ZFS_TYPE_DATASET;
1587 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1590 cb.cb_literal = B_TRUE;
1593 limit = parse_depth(optarg, &flags);
1596 flags |= ZFS_ITER_RECURSE;
1599 cb.cb_scripted = B_TRUE;
1602 (void) fprintf(stderr, gettext("missing argument for "
1603 "'%c' option\n"), optopt);
1608 * Process the set of columns to display. We zero out
1609 * the structure to give us a blank slate.
1611 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1613 while (*optarg != '\0') {
1614 static char *col_subopts[] =
1615 { "name", "property", "value", "received",
1616 "source", "all", NULL };
1618 if (i == ZFS_GET_NCOLS) {
1619 (void) fprintf(stderr, gettext("too "
1620 "many fields given to -o "
1625 switch (getsubopt(&optarg, col_subopts,
1628 cb.cb_columns[i++] = GET_COL_NAME;
1631 cb.cb_columns[i++] = GET_COL_PROPERTY;
1634 cb.cb_columns[i++] = GET_COL_VALUE;
1637 cb.cb_columns[i++] = GET_COL_RECVD;
1638 flags |= ZFS_ITER_RECVD_PROPS;
1641 cb.cb_columns[i++] = GET_COL_SOURCE;
1645 (void) fprintf(stderr,
1646 gettext("\"all\" conflicts "
1647 "with specific fields "
1648 "given to -o option\n"));
1651 cb.cb_columns[0] = GET_COL_NAME;
1652 cb.cb_columns[1] = GET_COL_PROPERTY;
1653 cb.cb_columns[2] = GET_COL_VALUE;
1654 cb.cb_columns[3] = GET_COL_RECVD;
1655 cb.cb_columns[4] = GET_COL_SOURCE;
1656 flags |= ZFS_ITER_RECVD_PROPS;
1660 (void) fprintf(stderr,
1661 gettext("invalid column name "
1670 while (*optarg != '\0') {
1671 static char *source_subopts[] = {
1672 "local", "default", "inherited",
1673 "received", "temporary", "none",
1676 switch (getsubopt(&optarg, source_subopts,
1679 cb.cb_sources |= ZPROP_SRC_LOCAL;
1682 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1685 cb.cb_sources |= ZPROP_SRC_INHERITED;
1688 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1691 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1694 cb.cb_sources |= ZPROP_SRC_NONE;
1697 (void) fprintf(stderr,
1698 gettext("invalid source "
1707 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1708 while (*optarg != '\0') {
1709 static char *type_subopts[] = { "filesystem",
1710 "volume", "snapshot", "bookmark",
1713 switch (getsubopt(&optarg, type_subopts,
1716 types |= ZFS_TYPE_FILESYSTEM;
1719 types |= ZFS_TYPE_VOLUME;
1722 types |= ZFS_TYPE_SNAPSHOT;
1725 types |= ZFS_TYPE_BOOKMARK;
1728 types = ZFS_TYPE_DATASET |
1733 (void) fprintf(stderr,
1734 gettext("invalid type '%s'\n"),
1742 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1752 (void) fprintf(stderr, gettext("missing property "
1759 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1767 * As part of zfs_expand_proplist(), we keep track of the maximum column
1768 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1769 * need to know the maximum name length. However, the user likely did
1770 * not specify 'name' as one of the properties to fetch, so we need to
1771 * make sure we always include at least this property for
1772 * print_get_headers() to work properly.
1774 if (cb.cb_proplist != NULL) {
1775 fake_name.pl_prop = ZFS_PROP_NAME;
1776 fake_name.pl_width = strlen(gettext("NAME"));
1777 fake_name.pl_next = cb.cb_proplist;
1778 cb.cb_proplist = &fake_name;
1781 cb.cb_first = B_TRUE;
1783 /* run for each object */
1784 ret = zfs_for_each(argc, argv, flags, types, NULL,
1785 &cb.cb_proplist, limit, get_callback, &cb);
1787 if (cb.cb_proplist == &fake_name)
1788 zprop_free_list(fake_name.pl_next);
1790 zprop_free_list(cb.cb_proplist);
1796 * inherit [-rS] <property> <fs|vol> ...
1798 * -r Recurse over all children
1799 * -S Revert to received value, if any
1801 * For each dataset specified on the command line, inherit the given property
1802 * from its parent. Inheriting a property at the pool level will cause it to
1803 * use the default value. The '-r' flag will recurse over all children, and is
1804 * useful for setting a property on a hierarchy-wide basis, regardless of any
1805 * local modifications for each dataset.
1808 typedef struct inherit_cbdata {
1809 const char *cb_propname;
1810 boolean_t cb_received;
1814 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1816 inherit_cbdata_t *cb = data;
1817 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1820 * If we're doing it recursively, then ignore properties that
1821 * are not valid for this type of dataset.
1823 if (prop != ZPROP_INVAL &&
1824 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1827 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1831 inherit_cb(zfs_handle_t *zhp, void *data)
1833 inherit_cbdata_t *cb = data;
1835 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1839 zfs_do_inherit(int argc, char **argv)
1843 inherit_cbdata_t cb = { 0 };
1847 boolean_t received = B_FALSE;
1850 while ((c = getopt(argc, argv, "rS")) != -1) {
1853 flags |= ZFS_ITER_RECURSE;
1860 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1869 /* check number of arguments */
1871 (void) fprintf(stderr, gettext("missing property argument\n"));
1875 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1883 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1884 if (zfs_prop_readonly(prop)) {
1885 (void) fprintf(stderr, gettext(
1886 "%s property is read-only\n"),
1890 if (!zfs_prop_inheritable(prop) && !received) {
1891 (void) fprintf(stderr, gettext("'%s' property cannot "
1892 "be inherited\n"), propname);
1893 if (prop == ZFS_PROP_QUOTA ||
1894 prop == ZFS_PROP_RESERVATION ||
1895 prop == ZFS_PROP_REFQUOTA ||
1896 prop == ZFS_PROP_REFRESERVATION)
1897 (void) fprintf(stderr, gettext("use 'zfs set "
1898 "%s=none' to clear\n"), propname);
1901 if (received && (prop == ZFS_PROP_VOLSIZE ||
1902 prop == ZFS_PROP_VERSION)) {
1903 (void) fprintf(stderr, gettext("'%s' property cannot "
1904 "be reverted to a received value\n"), propname);
1907 } else if (!zfs_prop_user(propname)) {
1908 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1913 cb.cb_propname = propname;
1914 cb.cb_received = received;
1916 if (flags & ZFS_ITER_RECURSE) {
1917 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1918 NULL, NULL, 0, inherit_recurse_cb, &cb);
1920 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1921 NULL, NULL, 0, inherit_cb, &cb);
1927 typedef struct upgrade_cbdata {
1928 uint64_t cb_numupgraded;
1929 uint64_t cb_numsamegraded;
1930 uint64_t cb_numfailed;
1931 uint64_t cb_version;
1933 boolean_t cb_foundone;
1934 char cb_lastfs[ZFS_MAXNAMELEN];
1938 same_pool(zfs_handle_t *zhp, const char *name)
1940 int len1 = strcspn(name, "/@");
1941 const char *zhname = zfs_get_name(zhp);
1942 int len2 = strcspn(zhname, "/@");
1946 return (strncmp(name, zhname, len1) == 0);
1950 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1952 upgrade_cbdata_t *cb = data;
1953 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1955 /* list if it's old/new */
1956 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1957 (cb->cb_newer && version > ZPL_VERSION)) {
1960 str = gettext("The following filesystems are "
1961 "formatted using a newer software version and\n"
1962 "cannot be accessed on the current system.\n\n");
1964 str = gettext("The following filesystems are "
1965 "out of date, and can be upgraded. After being\n"
1966 "upgraded, these filesystems (and any 'zfs send' "
1967 "streams generated from\n"
1968 "subsequent snapshots) will no longer be "
1969 "accessible by older software versions.\n\n");
1972 if (!cb->cb_foundone) {
1974 (void) printf(gettext("VER FILESYSTEM\n"));
1975 (void) printf(gettext("--- ------------\n"));
1976 cb->cb_foundone = B_TRUE;
1979 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1986 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1988 upgrade_cbdata_t *cb = data;
1989 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1990 int needed_spa_version;
1993 if (zfs_spa_version(zhp, &spa_version) < 0)
1996 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1998 if (needed_spa_version < 0)
2001 if (spa_version < needed_spa_version) {
2003 (void) printf(gettext("%s: can not be "
2004 "upgraded; the pool version needs to first "
2005 "be upgraded\nto version %d\n\n"),
2006 zfs_get_name(zhp), needed_spa_version);
2012 if (version < cb->cb_version) {
2014 (void) snprintf(verstr, sizeof (verstr),
2015 "%llu", cb->cb_version);
2016 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2018 * If they did "zfs upgrade -a", then we could
2019 * be doing ioctls to different pools. We need
2020 * to log this history once to each pool, and bypass
2021 * the normal history logging that happens in main().
2023 (void) zpool_log_history(g_zfs, history_str);
2024 log_history = B_FALSE;
2026 if (zfs_prop_set(zhp, "version", verstr) == 0)
2027 cb->cb_numupgraded++;
2030 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2031 } else if (version > cb->cb_version) {
2032 /* can't downgrade */
2033 (void) printf(gettext("%s: can not be downgraded; "
2034 "it is already at version %u\n"),
2035 zfs_get_name(zhp), version);
2038 cb->cb_numsamegraded++;
2046 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2049 zfs_do_upgrade(int argc, char **argv)
2051 boolean_t all = B_FALSE;
2052 boolean_t showversions = B_FALSE;
2054 upgrade_cbdata_t cb = { 0 };
2056 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2059 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2062 flags |= ZFS_ITER_RECURSE;
2065 showversions = B_TRUE;
2068 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2069 optarg, &cb.cb_version) != 0) {
2070 (void) fprintf(stderr,
2071 gettext("invalid version %s\n"), optarg);
2080 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2089 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2091 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2092 cb.cb_version || argc))
2094 if ((all || argc) && (showversions))
2100 /* Show info on available versions. */
2101 (void) printf(gettext("The following filesystem versions are "
2103 (void) printf(gettext("VER DESCRIPTION\n"));
2104 (void) printf("--- -----------------------------------------"
2105 "---------------\n");
2106 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2107 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2108 (void) printf(gettext(" 3 Case insensitive and filesystem "
2109 "user identifier (FUID)\n"));
2110 (void) printf(gettext(" 4 userquota, groupquota "
2112 (void) printf(gettext(" 5 System attributes\n"));
2113 (void) printf(gettext("\nFor more information on a particular "
2114 "version, including supported releases,\n"));
2115 (void) printf("see the ZFS Administration Guide.\n\n");
2117 } else if (argc || all) {
2118 /* Upgrade filesystems */
2119 if (cb.cb_version == 0)
2120 cb.cb_version = ZPL_VERSION;
2121 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2122 NULL, NULL, 0, upgrade_set_callback, &cb);
2123 (void) printf(gettext("%llu filesystems upgraded\n"),
2125 if (cb.cb_numsamegraded) {
2126 (void) printf(gettext("%llu filesystems already at "
2128 cb.cb_numsamegraded);
2130 if (cb.cb_numfailed != 0)
2133 /* List old-version filesytems */
2135 (void) printf(gettext("This system is currently running "
2136 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2138 flags |= ZFS_ITER_RECURSE;
2139 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2140 NULL, NULL, 0, upgrade_list_callback, &cb);
2142 found = cb.cb_foundone;
2143 cb.cb_foundone = B_FALSE;
2144 cb.cb_newer = B_TRUE;
2146 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2147 NULL, NULL, 0, upgrade_list_callback, &cb);
2149 if (!cb.cb_foundone && !found) {
2150 (void) printf(gettext("All filesystems are "
2151 "formatted with the current version.\n"));
2159 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2160 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2161 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2162 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2164 * -H Scripted mode; elide headers and separate columns by tabs.
2165 * -i Translate SID to POSIX ID.
2166 * -n Print numeric ID instead of user/group name.
2167 * -o Control which fields to display.
2168 * -p Use exact (parsable) numeric output.
2169 * -s Specify sort columns, descending order.
2170 * -S Specify sort columns, ascending order.
2171 * -t Control which object types to display.
2173 * Displays space consumed by, and quotas on, each user in the specified
2174 * filesystem or snapshot.
2177 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2178 enum us_field_types {
2184 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2185 static char *us_field_names[] = { "type", "name", "used", "quota" };
2186 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2188 #define USTYPE_PSX_GRP (1 << 0)
2189 #define USTYPE_PSX_USR (1 << 1)
2190 #define USTYPE_SMB_GRP (1 << 2)
2191 #define USTYPE_SMB_USR (1 << 3)
2192 #define USTYPE_ALL \
2193 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2195 static int us_type_bits[] = {
2202 static char *us_type_names[] = { "posixgroup", "posxiuser", "smbgroup",
2205 typedef struct us_node {
2207 uu_avl_node_t usn_avlnode;
2208 uu_list_node_t usn_listnode;
2211 typedef struct us_cbdata {
2213 uu_avl_pool_t *cb_avl_pool;
2215 boolean_t cb_numname;
2216 boolean_t cb_nicenum;
2217 boolean_t cb_sid2posix;
2218 zfs_userquota_prop_t cb_prop;
2219 zfs_sort_column_t *cb_sortcol;
2220 size_t cb_width[USFIELD_LAST];
2223 static boolean_t us_populated = B_FALSE;
2226 zfs_sort_column_t *si_sortcol;
2227 boolean_t si_numname;
2231 us_field_index(char *field)
2235 for (i = 0; i < USFIELD_LAST; i++) {
2236 if (strcmp(field, us_field_names[i]) == 0)
2244 us_compare(const void *larg, const void *rarg, void *unused)
2246 const us_node_t *l = larg;
2247 const us_node_t *r = rarg;
2248 us_sort_info_t *si = (us_sort_info_t *)unused;
2249 zfs_sort_column_t *sortcol = si->si_sortcol;
2250 boolean_t numname = si->si_numname;
2251 nvlist_t *lnvl = l->usn_nvl;
2252 nvlist_t *rnvl = r->usn_nvl;
2256 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2263 zfs_prop_t prop = sortcol->sc_prop;
2264 const char *propname = NULL;
2265 boolean_t reverse = sortcol->sc_reverse;
2270 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2271 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2273 rc = (rv32 < lv32) ? 1 : -1;
2278 (void) nvlist_lookup_uint64(lnvl, propname,
2280 (void) nvlist_lookup_uint64(rnvl, propname,
2283 rc = (rv64 < lv64) ? 1 : -1;
2285 (void) nvlist_lookup_string(lnvl, propname,
2287 (void) nvlist_lookup_string(rnvl, propname,
2289 rc = strcmp(lvstr, rvstr);
2293 case ZFS_PROP_QUOTA:
2296 if (prop == ZFS_PROP_USED)
2300 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2301 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2303 rc = (rv64 < lv64) ? 1 : -1;
2309 return (reverse ? 1 : -1);
2311 return (reverse ? -1 : 1);
2316 * If entries still seem to be the same, check if they are of the same
2317 * type (smbentity is added only if we are doing SID to POSIX ID
2318 * translation where we can have duplicate type/name combinations).
2320 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2321 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2323 return (lvb < rvb ? -1 : 1);
2328 static inline const char *
2329 us_type2str(unsigned field_type)
2331 switch (field_type) {
2332 case USTYPE_PSX_USR:
2333 return ("POSIX User");
2334 case USTYPE_PSX_GRP:
2335 return ("POSIX Group");
2336 case USTYPE_SMB_USR:
2337 return ("SMB User");
2338 case USTYPE_SMB_GRP:
2339 return ("SMB Group");
2341 return ("Undefined");
2346 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2348 us_cbdata_t *cb = (us_cbdata_t *)arg;
2349 zfs_userquota_prop_t prop = cb->cb_prop;
2354 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2355 uu_avl_t *avl = cb->cb_avl;
2359 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2361 const char *typestr;
2365 int typeidx, nameidx, sizeidx;
2366 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2367 boolean_t smbentity = B_FALSE;
2369 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2371 node = safe_malloc(sizeof (us_node_t));
2372 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2373 node->usn_nvl = props;
2375 if (domain != NULL && domain[0] != '\0') {
2377 char sid[ZFS_MAXNAMELEN + 32];
2382 directory_error_t e;
2387 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2389 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2390 type = USTYPE_SMB_GRP;
2392 err = sid_to_id(sid, B_FALSE, &id);
2395 type = USTYPE_SMB_USR;
2397 err = sid_to_id(sid, B_TRUE, &id);
2404 if (!cb->cb_sid2posix) {
2405 e = directory_name_from_sid(NULL, sid, &name,
2408 directory_error_free(e);
2416 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2418 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2419 type = USTYPE_PSX_GRP;
2420 if (!cb->cb_numname) {
2423 if ((g = getgrgid(rid)) != NULL)
2427 type = USTYPE_PSX_USR;
2428 if (!cb->cb_numname) {
2431 if ((p = getpwuid(rid)) != NULL)
2438 * Make sure that the type/name combination is unique when doing
2439 * SID to POSIX ID translation (hence changing the type from SMB to
2442 if (cb->cb_sid2posix &&
2443 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2446 /* Calculate/update width of TYPE field */
2447 typestr = us_type2str(type);
2448 typelen = strlen(gettext(typestr));
2449 typeidx = us_field_index("type");
2450 if (typelen > cb->cb_width[typeidx])
2451 cb->cb_width[typeidx] = typelen;
2452 if (nvlist_add_uint32(props, "type", type) != 0)
2455 /* Calculate/update width of NAME field */
2456 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2457 if (nvlist_add_uint64(props, "name", rid) != 0)
2459 namelen = snprintf(NULL, 0, "%u", rid);
2461 if (nvlist_add_string(props, "name", name) != 0)
2463 namelen = strlen(name);
2465 nameidx = us_field_index("name");
2466 if (namelen > cb->cb_width[nameidx])
2467 cb->cb_width[nameidx] = namelen;
2470 * Check if this type/name combination is in the list and update it;
2471 * otherwise add new node to the list.
2473 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2474 uu_avl_insert(avl, node, idx);
2479 props = node->usn_nvl;
2482 /* Calculate/update width of USED/QUOTA fields */
2484 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2486 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2487 sizelen = strlen(sizebuf);
2488 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2490 if (!nvlist_exists(props, "quota"))
2491 (void) nvlist_add_uint64(props, "quota", 0);
2494 if (!nvlist_exists(props, "used"))
2495 (void) nvlist_add_uint64(props, "used", 0);
2497 sizeidx = us_field_index(propname);
2498 if (sizelen > cb->cb_width[sizeidx])
2499 cb->cb_width[sizeidx] = sizelen;
2501 if (nvlist_add_uint64(props, propname, space) != 0)
2508 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2509 size_t *width, us_node_t *node)
2511 nvlist_t *nvl = node->usn_nvl;
2512 char valstr[ZFS_MAXNAMELEN];
2513 boolean_t first = B_TRUE;
2519 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2520 if (!(ustype & types))
2523 while ((field = fields[cfield]) != USFIELD_LAST) {
2524 nvpair_t *nvp = NULL;
2528 char *strval = NULL;
2530 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2531 if (strcmp(nvpair_name(nvp),
2532 us_field_names[field]) == 0)
2536 type = nvpair_type(nvp);
2538 case DATA_TYPE_UINT32:
2539 (void) nvpair_value_uint32(nvp, &val32);
2541 case DATA_TYPE_UINT64:
2542 (void) nvpair_value_uint64(nvp, &val64);
2544 case DATA_TYPE_STRING:
2545 (void) nvpair_value_string(nvp, &strval);
2548 (void) fprintf(stderr, "invalid data type\n");
2553 strval = (char *)us_type2str(val32);
2556 if (type == DATA_TYPE_UINT64) {
2557 (void) sprintf(valstr, "%llu", val64);
2563 if (type == DATA_TYPE_UINT64) {
2565 (void) sprintf(valstr, "%llu", val64);
2567 zfs_nicenum(val64, valstr,
2570 if (field == USFIELD_QUOTA &&
2571 strcmp(valstr, "0") == 0)
2581 (void) printf("\t");
2586 (void) printf("%s", strval);
2587 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2588 (void) printf("%-*s", width[field], strval);
2590 (void) printf("%*s", width[field], strval);
2596 (void) printf("\n");
2600 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2601 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2609 boolean_t first = B_TRUE;
2611 while ((field = fields[cfield]) != USFIELD_LAST) {
2612 col = gettext(us_field_hdr[field]);
2613 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2614 (void) printf(first ? "%-*s" : " %-*s",
2617 (void) printf(first ? "%*s" : " %*s",
2623 (void) printf("\n");
2626 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2627 print_us_node(scripted, parsable, fields, types, width, node);
2629 nvlist_free(node->usn_nvl);
2634 zfs_do_userspace(int argc, char **argv)
2637 zfs_userquota_prop_t p;
2639 uu_avl_pool_t *avl_pool;
2641 uu_avl_walk_t *walk;
2643 char deffields[] = "type,name,used,quota";
2644 char *ofield = NULL;
2645 char *tfield = NULL;
2649 boolean_t scripted = B_FALSE;
2650 boolean_t prtnum = B_FALSE;
2651 boolean_t parsable = B_FALSE;
2652 boolean_t sid2posix = B_FALSE;
2655 zfs_sort_column_t *sortcol = NULL;
2656 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2660 uu_list_pool_t *listpool;
2662 uu_avl_index_t idx = 0;
2663 uu_list_index_t idx2 = 0;
2668 if (strcmp(argv[0], "groupspace") == 0)
2669 /* Toggle default group types */
2670 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2672 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2688 if (zfs_add_sort_column(&sortcol, optarg,
2689 c == 's' ? B_FALSE : B_TRUE) != 0) {
2690 (void) fprintf(stderr,
2691 gettext("invalid field '%s'\n"), optarg);
2702 (void) fprintf(stderr, gettext("missing argument for "
2703 "'%c' option\n"), optopt);
2707 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2717 (void) fprintf(stderr, gettext("missing dataset name\n"));
2721 (void) fprintf(stderr, gettext("too many arguments\n"));
2725 /* Use default output fields if not specified using -o */
2729 if ((delim = strchr(ofield, ',')) != NULL)
2731 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2732 (void) fprintf(stderr, gettext("invalid type '%s' "
2733 "for -o option\n"), ofield);
2738 } while (delim != NULL);
2739 fields[cfield] = USFIELD_LAST;
2741 /* Override output types (-t option) */
2742 if (tfield != NULL) {
2746 boolean_t found = B_FALSE;
2748 if ((delim = strchr(tfield, ',')) != NULL)
2750 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2752 if (strcmp(tfield, us_type_names[i]) == 0) {
2754 types |= us_type_bits[i];
2759 (void) fprintf(stderr, gettext("invalid type "
2760 "'%s' for -t option\n"), tfield);
2765 } while (delim != NULL);
2768 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2771 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2772 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2774 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2777 /* Always add default sorting columns */
2778 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2779 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2781 cb.cb_sortcol = sortcol;
2782 cb.cb_numname = prtnum;
2783 cb.cb_nicenum = !parsable;
2784 cb.cb_avl_pool = avl_pool;
2785 cb.cb_avl = avl_tree;
2786 cb.cb_sid2posix = sid2posix;
2788 for (i = 0; i < USFIELD_LAST; i++)
2789 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2791 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2792 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2793 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2794 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2795 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2798 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2803 if ((node = uu_avl_first(avl_tree)) == NULL)
2806 us_populated = B_TRUE;
2808 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2809 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2810 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2811 uu_list_node_init(node, &node->usn_listnode, listpool);
2813 while (node != NULL) {
2815 node = uu_avl_next(avl_tree, node);
2816 uu_avl_remove(avl_tree, rmnode);
2817 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2818 uu_list_insert(list, rmnode, idx2);
2821 for (node = uu_list_first(list); node != NULL;
2822 node = uu_list_next(list, node)) {
2823 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2825 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2826 uu_avl_insert(avl_tree, node, idx);
2829 uu_list_destroy(list);
2830 uu_list_pool_destroy(listpool);
2832 /* Print and free node nvlist memory */
2833 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2836 zfs_free_sort_columns(sortcol);
2838 /* Clean up the AVL tree */
2839 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2842 while ((node = uu_avl_walk_next(walk)) != NULL) {
2843 uu_avl_remove(cb.cb_avl, node);
2847 uu_avl_walk_end(walk);
2848 uu_avl_destroy(avl_tree);
2849 uu_avl_pool_destroy(avl_pool);
2855 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2856 * [-t type[,...]] [filesystem|volume|snapshot] ...
2858 * -H Scripted mode; elide headers and separate columns by tabs.
2859 * -p Display values in parsable (literal) format.
2860 * -r Recurse over all children.
2861 * -d Limit recursion by depth.
2862 * -o Control which fields to display.
2863 * -s Specify sort columns, descending order.
2864 * -S Specify sort columns, ascending order.
2865 * -t Control which object types to display.
2867 * When given no arguments, list all filesystems in the system.
2868 * Otherwise, list the specified datasets, optionally recursing down them if
2869 * '-r' is specified.
2871 typedef struct list_cbdata {
2873 boolean_t cb_literal;
2874 boolean_t cb_scripted;
2875 zprop_list_t *cb_proplist;
2879 * Given a list of columns to display, output appropriate headers for each one.
2882 print_header(list_cbdata_t *cb)
2884 zprop_list_t *pl = cb->cb_proplist;
2885 char headerbuf[ZFS_MAXPROPLEN];
2888 boolean_t first = B_TRUE;
2889 boolean_t right_justify;
2891 for (; pl != NULL; pl = pl->pl_next) {
2898 right_justify = B_FALSE;
2899 if (pl->pl_prop != ZPROP_INVAL) {
2900 header = zfs_prop_column_name(pl->pl_prop);
2901 right_justify = zfs_prop_align_right(pl->pl_prop);
2903 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2904 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2905 headerbuf[i] = '\0';
2909 if (pl->pl_next == NULL && !right_justify)
2910 (void) printf("%s", header);
2911 else if (right_justify)
2912 (void) printf("%*s", pl->pl_width, header);
2914 (void) printf("%-*s", pl->pl_width, header);
2917 (void) printf("\n");
2921 * Given a dataset and a list of fields, print out all the properties according
2922 * to the described layout.
2925 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2927 zprop_list_t *pl = cb->cb_proplist;
2928 boolean_t first = B_TRUE;
2929 char property[ZFS_MAXPROPLEN];
2930 nvlist_t *userprops = zfs_get_user_props(zhp);
2933 boolean_t right_justify;
2935 for (; pl != NULL; pl = pl->pl_next) {
2937 if (cb->cb_scripted)
2938 (void) printf("\t");
2945 if (pl->pl_prop == ZFS_PROP_NAME) {
2946 (void) strlcpy(property, zfs_get_name(zhp),
2949 right_justify = zfs_prop_align_right(pl->pl_prop);
2950 } else if (pl->pl_prop != ZPROP_INVAL) {
2951 if (zfs_prop_get(zhp, pl->pl_prop, property,
2952 sizeof (property), NULL, NULL, 0,
2953 cb->cb_literal) != 0)
2957 right_justify = zfs_prop_align_right(pl->pl_prop);
2958 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2959 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2960 property, sizeof (property), cb->cb_literal) != 0)
2964 right_justify = B_TRUE;
2965 } else if (zfs_prop_written(pl->pl_user_prop)) {
2966 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2967 property, sizeof (property), cb->cb_literal) != 0)
2971 right_justify = B_TRUE;
2973 if (nvlist_lookup_nvlist(userprops,
2974 pl->pl_user_prop, &propval) != 0)
2977 verify(nvlist_lookup_string(propval,
2978 ZPROP_VALUE, &propstr) == 0);
2979 right_justify = B_FALSE;
2983 * If this is being called in scripted mode, or if this is the
2984 * last column and it is left-justified, don't include a width
2987 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2988 (void) printf("%s", propstr);
2989 else if (right_justify)
2990 (void) printf("%*s", pl->pl_width, propstr);
2992 (void) printf("%-*s", pl->pl_width, propstr);
2995 (void) printf("\n");
2999 * Generic callback function to list a dataset or snapshot.
3002 list_callback(zfs_handle_t *zhp, void *data)
3004 list_cbdata_t *cbp = data;
3006 if (cbp->cb_first) {
3007 if (!cbp->cb_scripted)
3009 cbp->cb_first = B_FALSE;
3012 print_dataset(zhp, cbp);
3018 zfs_do_list(int argc, char **argv)
3021 static char default_fields[] =
3022 "name,used,available,referenced,mountpoint";
3023 int types = ZFS_TYPE_DATASET;
3024 boolean_t types_specified = B_FALSE;
3025 char *fields = NULL;
3026 list_cbdata_t cb = { 0 };
3030 zfs_sort_column_t *sortcol = NULL;
3031 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3034 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3040 cb.cb_literal = B_TRUE;
3041 flags |= ZFS_ITER_LITERAL_PROPS;
3044 limit = parse_depth(optarg, &flags);
3047 flags |= ZFS_ITER_RECURSE;
3050 cb.cb_scripted = B_TRUE;
3053 if (zfs_add_sort_column(&sortcol, optarg,
3055 (void) fprintf(stderr,
3056 gettext("invalid property '%s'\n"), optarg);
3061 if (zfs_add_sort_column(&sortcol, optarg,
3063 (void) fprintf(stderr,
3064 gettext("invalid property '%s'\n"), optarg);
3070 types_specified = B_TRUE;
3071 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3072 while (*optarg != '\0') {
3073 static char *type_subopts[] = { "filesystem",
3074 "volume", "snapshot", "snap", "bookmark",
3077 switch (getsubopt(&optarg, type_subopts,
3080 types |= ZFS_TYPE_FILESYSTEM;
3083 types |= ZFS_TYPE_VOLUME;
3087 types |= ZFS_TYPE_SNAPSHOT;
3090 types |= ZFS_TYPE_BOOKMARK;
3093 types = ZFS_TYPE_DATASET |
3097 (void) fprintf(stderr,
3098 gettext("invalid type '%s'\n"),
3105 (void) fprintf(stderr, gettext("missing argument for "
3106 "'%c' option\n"), optopt);
3110 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3120 fields = default_fields;
3123 * If we are only going to list snapshot names and sort by name,
3124 * then we can use faster version.
3126 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3127 flags |= ZFS_ITER_SIMPLE;
3130 * If "-o space" and no types were specified, don't display snapshots.
3132 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3133 types &= ~ZFS_TYPE_SNAPSHOT;
3136 * If the user specifies '-o all', the zprop_get_list() doesn't
3137 * normally include the name of the dataset. For 'zfs list', we always
3138 * want this property to be first.
3140 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3144 cb.cb_first = B_TRUE;
3146 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3147 limit, list_callback, &cb);
3149 zprop_free_list(cb.cb_proplist);
3150 zfs_free_sort_columns(sortcol);
3152 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3153 (void) printf(gettext("no datasets available\n"));
3159 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3160 * zfs rename [-f] -p <fs | vol> <fs | vol>
3161 * zfs rename -r <snap> <snap>
3162 * zfs rename -u [-p] <fs> <fs>
3164 * Renames the given dataset to another of the same type.
3166 * The '-p' flag creates all the non-existing ancestors of the target first.
3170 zfs_do_rename(int argc, char **argv)
3173 renameflags_t flags = { 0 };
3177 boolean_t parents = B_FALSE;
3178 char *snapshot = NULL;
3181 while ((c = getopt(argc, argv, "fpru")) != -1) {
3187 flags.recurse = B_TRUE;
3190 flags.nounmount = B_TRUE;
3193 flags.forceunmount = B_TRUE;
3197 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3206 /* check number of arguments */
3208 (void) fprintf(stderr, gettext("missing source dataset "
3213 (void) fprintf(stderr, gettext("missing target dataset "
3218 (void) fprintf(stderr, gettext("too many arguments\n"));
3222 if (flags.recurse && parents) {
3223 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3228 if (flags.recurse && strchr(argv[0], '@') == 0) {
3229 (void) fprintf(stderr, gettext("source dataset for recursive "
3230 "rename must be a snapshot\n"));
3234 if (flags.nounmount && parents) {
3235 (void) fprintf(stderr, gettext("-u and -r options are mutually "
3240 if (flags.nounmount)
3241 types = ZFS_TYPE_FILESYSTEM;
3243 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3245 types = ZFS_TYPE_DATASET;
3247 if (flags.recurse) {
3249 * When we do recursive rename we are fine when the given
3250 * snapshot for the given dataset doesn't exist - it can
3251 * still exists below.
3254 snapshot = strchr(argv[0], '@');
3255 assert(snapshot != NULL);
3260 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3263 /* If we were asked and the name looks good, try to create ancestors. */
3264 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3265 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3270 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3279 * Promotes the given clone fs to be the parent
3283 zfs_do_promote(int argc, char **argv)
3289 if (argc > 1 && argv[1][0] == '-') {
3290 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3295 /* check number of arguments */
3297 (void) fprintf(stderr, gettext("missing clone filesystem"
3302 (void) fprintf(stderr, gettext("too many arguments\n"));
3306 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3310 ret = (zfs_promote(zhp) != 0);
3318 * zfs rollback [-rRf] <snapshot>
3320 * -r Delete any intervening snapshots before doing rollback
3321 * -R Delete any snapshots and their clones
3322 * -f ignored for backwards compatability
3324 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3325 * since then and making it the active dataset. If more recent snapshots exist,
3326 * the command will complain unless the '-r' flag is given.
3328 typedef struct rollback_cbdata {
3334 boolean_t cb_recurse;
3335 } rollback_cbdata_t;
3338 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3340 rollback_cbdata_t *cbp = data;
3342 if (cbp->cb_first && cbp->cb_recurse) {
3343 (void) fprintf(stderr, gettext("cannot rollback to "
3344 "'%s': clones of previous snapshots exist\n"),
3346 (void) fprintf(stderr, gettext("use '-R' to "
3347 "force deletion of the following clones and "
3353 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3359 * Report any snapshots more recent than the one specified. Used when '-r' is
3360 * not specified. We reuse this same callback for the snapshot dependents - if
3361 * 'cb_dependent' is set, then this is a dependent and we should report it
3362 * without checking the transaction group.
3365 rollback_check(zfs_handle_t *zhp, void *data)
3367 rollback_cbdata_t *cbp = data;
3369 if (cbp->cb_doclones) {
3374 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3375 if (cbp->cb_first && !cbp->cb_recurse) {
3376 (void) fprintf(stderr, gettext("cannot "
3377 "rollback to '%s': more recent snapshots "
3378 "or bookmarks exist\n"),
3380 (void) fprintf(stderr, gettext("use '-r' to "
3381 "force deletion of the following "
3382 "snapshots and bookmarks:\n"));
3387 if (cbp->cb_recurse) {
3388 if (zfs_iter_dependents(zhp, B_TRUE,
3389 rollback_check_dependent, cbp) != 0) {
3394 (void) fprintf(stderr, "%s\n",
3403 zfs_do_rollback(int argc, char **argv)
3407 boolean_t force = B_FALSE;
3408 rollback_cbdata_t cb = { 0 };
3409 zfs_handle_t *zhp, *snap;
3410 char parentname[ZFS_MAXNAMELEN];
3414 while ((c = getopt(argc, argv, "rRf")) != -1) {
3427 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3436 /* check number of arguments */
3438 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3442 (void) fprintf(stderr, gettext("too many arguments\n"));
3446 /* open the snapshot */
3447 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3450 /* open the parent dataset */
3451 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3452 verify((delim = strrchr(parentname, '@')) != NULL);
3454 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3460 * Check for more recent snapshots and/or clones based on the presence
3463 cb.cb_target = argv[0];
3464 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3465 cb.cb_first = B_TRUE;
3467 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3469 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3472 if ((ret = cb.cb_error) != 0)
3476 * Rollback parent to the given snapshot.
3478 ret = zfs_rollback(zhp, snap, force);
3491 * zfs set property=value { fs | snap | vol } ...
3493 * Sets the given property for all datasets specified on the command line.
3495 typedef struct set_cbdata {
3501 set_callback(zfs_handle_t *zhp, void *data)
3503 set_cbdata_t *cbp = data;
3505 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3506 switch (libzfs_errno(g_zfs)) {
3507 case EZFS_MOUNTFAILED:
3508 (void) fprintf(stderr, gettext("property may be set "
3509 "but unable to remount filesystem\n"));
3511 case EZFS_SHARENFSFAILED:
3512 (void) fprintf(stderr, gettext("property may be set "
3513 "but unable to reshare filesystem\n"));
3522 zfs_do_set(int argc, char **argv)
3527 /* check for options */
3528 if (argc > 1 && argv[1][0] == '-') {
3529 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3534 /* check number of arguments */
3536 (void) fprintf(stderr, gettext("missing property=value "
3541 (void) fprintf(stderr, gettext("missing dataset name\n"));
3545 /* validate property=value argument */
3546 cb.cb_propname = argv[1];
3547 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3548 (cb.cb_value[1] == '\0')) {
3549 (void) fprintf(stderr, gettext("missing value in "
3550 "property=value argument\n"));
3554 *cb.cb_value = '\0';
3557 if (*cb.cb_propname == '\0') {
3558 (void) fprintf(stderr,
3559 gettext("missing property in property=value argument\n"));
3563 ret = zfs_for_each(argc - 2, argv + 2, 0,
3564 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3569 typedef struct snap_cbdata {
3571 boolean_t sd_recursive;
3572 const char *sd_snapname;
3576 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3578 snap_cbdata_t *sd = arg;
3583 if (sd->sd_recursive &&
3584 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3589 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3592 fnvlist_add_boolean(sd->sd_nvl, name);
3595 if (sd->sd_recursive)
3596 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3602 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3604 * Creates a snapshot with the given name. While functionally equivalent to
3605 * 'zfs create', it is a separate command to differentiate intent.
3608 zfs_do_snapshot(int argc, char **argv)
3613 snap_cbdata_t sd = { 0 };
3614 boolean_t multiple_snaps = B_FALSE;
3616 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3618 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3622 while ((c = getopt(argc, argv, "ro:")) != -1) {
3625 if (parseprop(props, optarg))
3629 sd.sd_recursive = B_TRUE;
3630 multiple_snaps = B_TRUE;
3633 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3642 /* check number of arguments */
3644 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3649 multiple_snaps = B_TRUE;
3650 for (; argc > 0; argc--, argv++) {
3654 atp = strchr(argv[0], '@');
3658 sd.sd_snapname = atp + 1;
3659 zhp = zfs_open(g_zfs, argv[0],
3660 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3663 if (zfs_snapshot_cb(zhp, &sd) != 0)
3667 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3668 nvlist_free(sd.sd_nvl);
3670 if (ret != 0 && multiple_snaps)
3671 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3675 nvlist_free(sd.sd_nvl);
3682 * Send a backup stream to stdout.
3685 zfs_do_send(int argc, char **argv)
3687 char *fromname = NULL;
3688 char *toname = NULL;
3691 sendflags_t flags = { 0 };
3693 nvlist_t *dbgnv = NULL;
3694 boolean_t extraverbose = B_FALSE;
3697 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3708 flags.doall = B_TRUE;
3711 flags.replicate = B_TRUE;
3714 flags.props = B_TRUE;
3717 flags.parsable = B_TRUE;
3718 flags.verbose = B_TRUE;
3722 extraverbose = B_TRUE;
3723 flags.verbose = B_TRUE;
3724 flags.progress = B_TRUE;
3727 flags.dedup = B_TRUE;
3730 flags.dryrun = B_TRUE;
3733 (void) fprintf(stderr, gettext("missing argument for "
3734 "'%c' option\n"), optopt);
3738 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3747 /* check number of arguments */
3749 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3753 (void) fprintf(stderr, gettext("too many arguments\n"));
3757 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3758 (void) fprintf(stderr,
3759 gettext("Error: Stream can not be written to a terminal.\n"
3760 "You must redirect standard output.\n"));
3765 * Special case sending a filesystem, or from a bookmark.
3767 if (strchr(argv[0], '@') == NULL ||
3768 (fromname && strchr(fromname, '#') != NULL)) {
3769 char frombuf[ZFS_MAXNAMELEN];
3771 if (flags.replicate || flags.doall || flags.props ||
3772 flags.dedup || flags.dryrun || flags.verbose ||
3774 (void) fprintf(stderr,
3776 "Unsupported flag with filesystem or bookmark.\n"));
3780 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3784 if (fromname != NULL &&
3785 (fromname[0] == '#' || fromname[0] == '@')) {
3787 * Incremental source name begins with # or @.
3788 * Default to same fs as target.
3790 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3791 cp = strchr(frombuf, '@');
3794 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3797 err = zfs_send_one(zhp, fromname, STDOUT_FILENO);
3802 cp = strchr(argv[0], '@');
3805 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3810 * If they specified the full path to the snapshot, chop off
3811 * everything except the short name of the snapshot, but special
3812 * case if they specify the origin.
3814 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3815 char origin[ZFS_MAXNAMELEN];
3818 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3819 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3821 if (strcmp(origin, fromname) == 0) {
3823 flags.fromorigin = B_TRUE;
3826 if (cp != fromname && strcmp(argv[0], fromname)) {
3827 (void) fprintf(stderr,
3828 gettext("incremental source must be "
3829 "in same filesystem\n"));
3833 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3834 (void) fprintf(stderr,
3835 gettext("invalid incremental source\n"));
3841 if (flags.replicate && fromname == NULL)
3842 flags.doall = B_TRUE;
3844 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3845 extraverbose ? &dbgnv : NULL);
3847 if (extraverbose && dbgnv != NULL) {
3849 * dump_nvlist prints to stdout, but that's been
3850 * redirected to a file. Make it print to stderr
3853 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3854 dump_nvlist(dbgnv, 0);
3863 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3865 * Restore a backup stream from stdin.
3868 zfs_do_receive(int argc, char **argv)
3871 recvflags_t flags = { 0 };
3874 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3877 flags.isprefix = B_TRUE;
3880 flags.isprefix = B_TRUE;
3881 flags.istail = B_TRUE;
3884 flags.dryrun = B_TRUE;
3887 flags.nomount = B_TRUE;
3890 flags.verbose = B_TRUE;
3893 flags.force = B_TRUE;
3896 (void) fprintf(stderr, gettext("missing argument for "
3897 "'%c' option\n"), optopt);
3901 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3910 /* check number of arguments */
3912 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3916 (void) fprintf(stderr, gettext("too many arguments\n"));
3920 if (isatty(STDIN_FILENO)) {
3921 (void) fprintf(stderr,
3922 gettext("Error: Backup stream can not be read "
3923 "from a terminal.\n"
3924 "You must redirect standard input.\n"));
3928 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3934 * allow/unallow stuff
3936 /* copied from zfs/sys/dsl_deleg.h */
3937 #define ZFS_DELEG_PERM_CREATE "create"
3938 #define ZFS_DELEG_PERM_DESTROY "destroy"
3939 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3940 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3941 #define ZFS_DELEG_PERM_CLONE "clone"
3942 #define ZFS_DELEG_PERM_PROMOTE "promote"
3943 #define ZFS_DELEG_PERM_RENAME "rename"
3944 #define ZFS_DELEG_PERM_MOUNT "mount"
3945 #define ZFS_DELEG_PERM_SHARE "share"
3946 #define ZFS_DELEG_PERM_SEND "send"
3947 #define ZFS_DELEG_PERM_RECEIVE "receive"
3948 #define ZFS_DELEG_PERM_ALLOW "allow"
3949 #define ZFS_DELEG_PERM_USERPROP "userprop"
3950 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3951 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3952 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3953 #define ZFS_DELEG_PERM_USERUSED "userused"
3954 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3955 #define ZFS_DELEG_PERM_HOLD "hold"
3956 #define ZFS_DELEG_PERM_RELEASE "release"
3957 #define ZFS_DELEG_PERM_DIFF "diff"
3958 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
3960 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3962 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3963 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3964 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3965 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3966 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3967 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3968 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3969 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3970 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3971 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3972 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3973 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3974 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3975 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3976 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3977 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3978 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
3980 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3981 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3982 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3983 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3984 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3985 { NULL, ZFS_DELEG_NOTE_NONE }
3988 /* permission structure */
3989 typedef struct deleg_perm {
3990 zfs_deleg_who_type_t dp_who_type;
3991 const char *dp_name;
3993 boolean_t dp_descend;
3997 typedef struct deleg_perm_node {
3998 deleg_perm_t dpn_perm;
4000 uu_avl_node_t dpn_avl_node;
4001 } deleg_perm_node_t;
4003 typedef struct fs_perm fs_perm_t;
4005 /* permissions set */
4006 typedef struct who_perm {
4007 zfs_deleg_who_type_t who_type;
4008 const char *who_name; /* id */
4009 char who_ug_name[256]; /* user/group name */
4010 fs_perm_t *who_fsperm; /* uplink */
4012 uu_avl_t *who_deleg_perm_avl; /* permissions */
4016 typedef struct who_perm_node {
4017 who_perm_t who_perm;
4018 uu_avl_node_t who_avl_node;
4021 typedef struct fs_perm_set fs_perm_set_t;
4022 /* fs permissions */
4024 const char *fsp_name;
4026 uu_avl_t *fsp_sc_avl; /* sets,create */
4027 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4029 fs_perm_set_t *fsp_set; /* uplink */
4033 typedef struct fs_perm_node {
4034 fs_perm_t fspn_fsperm;
4037 uu_list_node_t fspn_list_node;
4040 /* top level structure */
4041 struct fs_perm_set {
4042 uu_list_pool_t *fsps_list_pool;
4043 uu_list_t *fsps_list; /* list of fs_perms */
4045 uu_avl_pool_t *fsps_named_set_avl_pool;
4046 uu_avl_pool_t *fsps_who_perm_avl_pool;
4047 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4050 static inline const char *
4051 deleg_perm_type(zfs_deleg_note_t note)
4057 case ZFS_DELEG_NOTE_GROUPQUOTA:
4058 case ZFS_DELEG_NOTE_GROUPUSED:
4059 case ZFS_DELEG_NOTE_USERPROP:
4060 case ZFS_DELEG_NOTE_USERQUOTA:
4061 case ZFS_DELEG_NOTE_USERUSED:
4063 return (gettext("other"));
4065 return (gettext("subcommand"));
4070 who_type2weight(zfs_deleg_who_type_t who_type)
4074 case ZFS_DELEG_NAMED_SET_SETS:
4075 case ZFS_DELEG_NAMED_SET:
4078 case ZFS_DELEG_CREATE_SETS:
4079 case ZFS_DELEG_CREATE:
4082 case ZFS_DELEG_USER_SETS:
4083 case ZFS_DELEG_USER:
4086 case ZFS_DELEG_GROUP_SETS:
4087 case ZFS_DELEG_GROUP:
4090 case ZFS_DELEG_EVERYONE_SETS:
4091 case ZFS_DELEG_EVERYONE:
4103 who_perm_compare(const void *larg, const void *rarg, void *unused)
4105 const who_perm_node_t *l = larg;
4106 const who_perm_node_t *r = rarg;
4107 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4108 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4109 int lweight = who_type2weight(ltype);
4110 int rweight = who_type2weight(rtype);
4111 int res = lweight - rweight;
4113 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4114 ZFS_MAX_DELEG_NAME-1);
4126 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4128 const deleg_perm_node_t *l = larg;
4129 const deleg_perm_node_t *r = rarg;
4130 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4131 ZFS_MAX_DELEG_NAME-1);
4143 fs_perm_set_init(fs_perm_set_t *fspset)
4145 bzero(fspset, sizeof (fs_perm_set_t));
4147 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4148 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4149 NULL, UU_DEFAULT)) == NULL)
4151 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4152 UU_DEFAULT)) == NULL)
4155 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4156 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4157 who_perm_node_t, who_avl_node), who_perm_compare,
4158 UU_DEFAULT)) == NULL)
4161 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4162 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4163 who_perm_node_t, who_avl_node), who_perm_compare,
4164 UU_DEFAULT)) == NULL)
4167 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4168 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4169 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4174 static inline void fs_perm_fini(fs_perm_t *);
4175 static inline void who_perm_fini(who_perm_t *);
4178 fs_perm_set_fini(fs_perm_set_t *fspset)
4180 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4182 while (node != NULL) {
4183 fs_perm_node_t *next_node =
4184 uu_list_next(fspset->fsps_list, node);
4185 fs_perm_t *fsperm = &node->fspn_fsperm;
4186 fs_perm_fini(fsperm);
4187 uu_list_remove(fspset->fsps_list, node);
4192 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4193 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4194 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4198 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4201 deleg_perm->dp_who_type = type;
4202 deleg_perm->dp_name = name;
4206 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4207 zfs_deleg_who_type_t type, const char *name)
4209 uu_avl_pool_t *pool;
4210 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4212 bzero(who_perm, sizeof (who_perm_t));
4214 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4215 UU_DEFAULT)) == NULL)
4218 who_perm->who_type = type;
4219 who_perm->who_name = name;
4220 who_perm->who_fsperm = fsperm;
4224 who_perm_fini(who_perm_t *who_perm)
4226 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4228 while (node != NULL) {
4229 deleg_perm_node_t *next_node =
4230 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4232 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4237 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4241 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4243 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4244 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4246 bzero(fsperm, sizeof (fs_perm_t));
4248 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4252 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4256 fsperm->fsp_set = fspset;
4257 fsperm->fsp_name = fsname;
4261 fs_perm_fini(fs_perm_t *fsperm)
4263 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4264 while (node != NULL) {
4265 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4267 who_perm_t *who_perm = &node->who_perm;
4268 who_perm_fini(who_perm);
4269 uu_avl_remove(fsperm->fsp_sc_avl, node);
4274 node = uu_avl_first(fsperm->fsp_uge_avl);
4275 while (node != NULL) {
4276 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4278 who_perm_t *who_perm = &node->who_perm;
4279 who_perm_fini(who_perm);
4280 uu_avl_remove(fsperm->fsp_uge_avl, node);
4285 uu_avl_destroy(fsperm->fsp_sc_avl);
4286 uu_avl_destroy(fsperm->fsp_uge_avl);
4290 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4291 zfs_deleg_who_type_t who_type, const char *name, char locality)
4293 uu_avl_index_t idx = 0;
4295 deleg_perm_node_t *found_node = NULL;
4296 deleg_perm_t *deleg_perm = &node->dpn_perm;
4298 deleg_perm_init(deleg_perm, who_type, name);
4300 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4302 uu_avl_insert(avl, node, idx);
4305 deleg_perm = &node->dpn_perm;
4310 case ZFS_DELEG_LOCAL:
4311 deleg_perm->dp_local = B_TRUE;
4313 case ZFS_DELEG_DESCENDENT:
4314 deleg_perm->dp_descend = B_TRUE;
4319 assert(B_FALSE); /* invalid locality */
4324 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4326 nvpair_t *nvp = NULL;
4327 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4328 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4329 zfs_deleg_who_type_t who_type = who_perm->who_type;
4331 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4332 const char *name = nvpair_name(nvp);
4333 data_type_t type = nvpair_type(nvp);
4334 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4335 deleg_perm_node_t *node =
4336 safe_malloc(sizeof (deleg_perm_node_t));
4338 assert(type == DATA_TYPE_BOOLEAN);
4340 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4341 set_deleg_perm_node(avl, node, who_type, name, locality);
4348 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4350 nvpair_t *nvp = NULL;
4351 fs_perm_set_t *fspset = fsperm->fsp_set;
4353 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4354 nvlist_t *nvl2 = NULL;
4355 const char *name = nvpair_name(nvp);
4356 uu_avl_t *avl = NULL;
4357 uu_avl_pool_t *avl_pool;
4358 zfs_deleg_who_type_t perm_type = name[0];
4359 char perm_locality = name[1];
4360 const char *perm_name = name + 3;
4361 boolean_t is_set = B_TRUE;
4362 who_perm_t *who_perm = NULL;
4364 assert('$' == name[2]);
4366 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4369 switch (perm_type) {
4370 case ZFS_DELEG_CREATE:
4371 case ZFS_DELEG_CREATE_SETS:
4372 case ZFS_DELEG_NAMED_SET:
4373 case ZFS_DELEG_NAMED_SET_SETS:
4374 avl_pool = fspset->fsps_named_set_avl_pool;
4375 avl = fsperm->fsp_sc_avl;
4377 case ZFS_DELEG_USER:
4378 case ZFS_DELEG_USER_SETS:
4379 case ZFS_DELEG_GROUP:
4380 case ZFS_DELEG_GROUP_SETS:
4381 case ZFS_DELEG_EVERYONE:
4382 case ZFS_DELEG_EVERYONE_SETS:
4383 avl_pool = fspset->fsps_who_perm_avl_pool;
4384 avl = fsperm->fsp_uge_avl;
4389 who_perm_node_t *found_node = NULL;
4390 who_perm_node_t *node = safe_malloc(
4391 sizeof (who_perm_node_t));
4392 who_perm = &node->who_perm;
4393 uu_avl_index_t idx = 0;
4395 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4396 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4398 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4400 if (avl == fsperm->fsp_uge_avl) {
4402 struct passwd *p = NULL;
4403 struct group *g = NULL;
4404 const char *nice_name = NULL;
4406 switch (perm_type) {
4407 case ZFS_DELEG_USER_SETS:
4408 case ZFS_DELEG_USER:
4409 rid = atoi(perm_name);
4412 nice_name = p->pw_name;
4414 case ZFS_DELEG_GROUP_SETS:
4415 case ZFS_DELEG_GROUP:
4416 rid = atoi(perm_name);
4419 nice_name = g->gr_name;
4423 if (nice_name != NULL)
4425 node->who_perm.who_ug_name,
4429 uu_avl_insert(avl, node, idx);
4432 who_perm = &node->who_perm;
4436 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4443 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4445 nvpair_t *nvp = NULL;
4446 uu_avl_index_t idx = 0;
4448 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4449 nvlist_t *nvl2 = NULL;
4450 const char *fsname = nvpair_name(nvp);
4451 data_type_t type = nvpair_type(nvp);
4452 fs_perm_t *fsperm = NULL;
4453 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4457 fsperm = &node->fspn_fsperm;
4459 assert(DATA_TYPE_NVLIST == type);
4461 uu_list_node_init(node, &node->fspn_list_node,
4462 fspset->fsps_list_pool);
4464 idx = uu_list_numnodes(fspset->fsps_list);
4465 fs_perm_init(fsperm, fspset, fsname);
4467 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4470 (void) parse_fs_perm(fsperm, nvl2);
4472 uu_list_insert(fspset->fsps_list, node, idx);
4478 static inline const char *
4479 deleg_perm_comment(zfs_deleg_note_t note)
4481 const char *str = "";
4486 case ZFS_DELEG_NOTE_ALLOW:
4487 str = gettext("Must also have the permission that is being"
4488 "\n\t\t\t\tallowed");
4490 case ZFS_DELEG_NOTE_CLONE:
4491 str = gettext("Must also have the 'create' ability and 'mount'"
4492 "\n\t\t\t\tability in the origin file system");
4494 case ZFS_DELEG_NOTE_CREATE:
4495 str = gettext("Must also have the 'mount' ability");
4497 case ZFS_DELEG_NOTE_DESTROY:
4498 str = gettext("Must also have the 'mount' ability");
4500 case ZFS_DELEG_NOTE_DIFF:
4501 str = gettext("Allows lookup of paths within a dataset;"
4502 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4503 "\n\t\t\t\tin order to use zfs diff");
4505 case ZFS_DELEG_NOTE_HOLD:
4506 str = gettext("Allows adding a user hold to a snapshot");
4508 case ZFS_DELEG_NOTE_MOUNT:
4509 str = gettext("Allows mount/umount of ZFS datasets");
4511 case ZFS_DELEG_NOTE_PROMOTE:
4512 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4513 " 'promote' ability in the origin file system");
4515 case ZFS_DELEG_NOTE_RECEIVE:
4516 str = gettext("Must also have the 'mount' and 'create'"
4519 case ZFS_DELEG_NOTE_RELEASE:
4520 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4521 "might destroy the snapshot");
4523 case ZFS_DELEG_NOTE_RENAME:
4524 str = gettext("Must also have the 'mount' and 'create'"
4525 "\n\t\t\t\tability in the new parent");
4527 case ZFS_DELEG_NOTE_ROLLBACK:
4530 case ZFS_DELEG_NOTE_SEND:
4533 case ZFS_DELEG_NOTE_SHARE:
4534 str = gettext("Allows sharing file systems over NFS or SMB"
4535 "\n\t\t\t\tprotocols");
4537 case ZFS_DELEG_NOTE_SNAPSHOT:
4541 * case ZFS_DELEG_NOTE_VSCAN:
4542 * str = gettext("");
4546 case ZFS_DELEG_NOTE_GROUPQUOTA:
4547 str = gettext("Allows accessing any groupquota@... property");
4549 case ZFS_DELEG_NOTE_GROUPUSED:
4550 str = gettext("Allows reading any groupused@... property");
4552 case ZFS_DELEG_NOTE_USERPROP:
4553 str = gettext("Allows changing any user property");
4555 case ZFS_DELEG_NOTE_USERQUOTA:
4556 str = gettext("Allows accessing any userquota@... property");
4558 case ZFS_DELEG_NOTE_USERUSED:
4559 str = gettext("Allows reading any userused@... property");
4577 boolean_t recursive; /* unallow only */
4578 boolean_t prt_usage;
4580 boolean_t prt_perms;
4583 const char *dataset;
4587 prop_cmp(const void *a, const void *b)
4589 const char *str1 = *(const char **)a;
4590 const char *str2 = *(const char **)b;
4591 return (strcmp(str1, str2));
4595 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4597 const char *opt_desc[] = {
4598 "-h", gettext("show this help message and exit"),
4599 "-l", gettext("set permission locally"),
4600 "-d", gettext("set permission for descents"),
4601 "-u", gettext("set permission for user"),
4602 "-g", gettext("set permission for group"),
4603 "-e", gettext("set permission for everyone"),
4604 "-c", gettext("set create time permission"),
4605 "-s", gettext("define permission set"),
4607 "-r", gettext("remove permissions recursively"),
4609 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4610 size_t allow_size = unallow_size - 2;
4611 const char *props[ZFS_NUM_PROPS];
4614 FILE *fp = requested ? stdout : stderr;
4615 zprop_desc_t *pdtbl = zfs_prop_get_table();
4616 const char *fmt = gettext("%-16s %-14s\t%s\n");
4618 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4620 (void) fprintf(fp, gettext("Options:\n"));
4621 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4622 const char *opt = opt_desc[i++];
4623 const char *optdsc = opt_desc[i];
4624 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4627 (void) fprintf(fp, gettext("\nThe following permissions are "
4629 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4631 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4632 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4633 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4634 const char *perm_type = deleg_perm_type(perm_note);
4635 const char *perm_comment = deleg_perm_comment(perm_note);
4636 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4639 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4640 zprop_desc_t *pd = &pdtbl[i];
4641 if (pd->pd_visible != B_TRUE)
4644 if (pd->pd_attr == PROP_READONLY)
4647 props[count++] = pd->pd_name;
4649 props[count] = NULL;
4651 qsort(props, count, sizeof (char *), prop_cmp);
4653 for (i = 0; i < count; i++)
4654 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4657 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4659 exit(requested ? 0 : 2);
4662 static inline const char *
4663 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4666 if (un && argc == expected_argc - 1)
4668 else if (argc == expected_argc)
4669 *permsp = argv[argc - 2];
4671 allow_usage(un, B_FALSE,
4672 gettext("wrong number of parameters\n"));
4674 return (argv[argc - 1]);
4678 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4680 int uge_sum = opts->user + opts->group + opts->everyone;
4681 int csuge_sum = opts->create + opts->set + uge_sum;
4682 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4683 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4686 allow_usage(un, B_FALSE,
4687 gettext("-u, -g, and -e are mutually exclusive\n"));
4689 if (opts->prt_usage)
4690 if (argc == 0 && all_sum == 0)
4691 allow_usage(un, B_TRUE, NULL);
4697 allow_usage(un, B_FALSE,
4698 gettext("invalid options combined with -s\n"));
4700 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4701 if (argv[0][0] != '@')
4702 allow_usage(un, B_FALSE,
4703 gettext("invalid set name: missing '@' prefix\n"));
4704 opts->who = argv[0];
4705 } else if (opts->create) {
4706 if (ldcsuge_sum > 1)
4707 allow_usage(un, B_FALSE,
4708 gettext("invalid options combined with -c\n"));
4709 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4710 } else if (opts->everyone) {
4712 allow_usage(un, B_FALSE,
4713 gettext("invalid options combined with -e\n"));
4714 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4715 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4717 opts->everyone = B_TRUE;
4720 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4721 } else if (argc == 1 && !un) {
4722 opts->prt_perms = B_TRUE;
4723 opts->dataset = argv[argc-1];
4725 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4726 opts->who = argv[0];
4729 if (!opts->local && !opts->descend) {
4730 opts->local = B_TRUE;
4731 opts->descend = B_TRUE;
4736 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4737 const char *who, char *perms, nvlist_t *top_nvl)
4740 char ld[2] = { '\0', '\0' };
4741 char who_buf[ZFS_MAXNAMELEN+32];
4744 nvlist_t *base_nvl = NULL;
4745 nvlist_t *set_nvl = NULL;
4748 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4750 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4754 case ZFS_DELEG_NAMED_SET_SETS:
4755 case ZFS_DELEG_NAMED_SET:
4756 set_type = ZFS_DELEG_NAMED_SET_SETS;
4757 base_type = ZFS_DELEG_NAMED_SET;
4758 ld[0] = ZFS_DELEG_NA;
4760 case ZFS_DELEG_CREATE_SETS:
4761 case ZFS_DELEG_CREATE:
4762 set_type = ZFS_DELEG_CREATE_SETS;
4763 base_type = ZFS_DELEG_CREATE;
4764 ld[0] = ZFS_DELEG_NA;
4766 case ZFS_DELEG_USER_SETS:
4767 case ZFS_DELEG_USER:
4768 set_type = ZFS_DELEG_USER_SETS;
4769 base_type = ZFS_DELEG_USER;
4771 ld[0] = ZFS_DELEG_LOCAL;
4773 ld[1] = ZFS_DELEG_DESCENDENT;
4775 case ZFS_DELEG_GROUP_SETS:
4776 case ZFS_DELEG_GROUP:
4777 set_type = ZFS_DELEG_GROUP_SETS;
4778 base_type = ZFS_DELEG_GROUP;
4780 ld[0] = ZFS_DELEG_LOCAL;
4782 ld[1] = ZFS_DELEG_DESCENDENT;
4784 case ZFS_DELEG_EVERYONE_SETS:
4785 case ZFS_DELEG_EVERYONE:
4786 set_type = ZFS_DELEG_EVERYONE_SETS;
4787 base_type = ZFS_DELEG_EVERYONE;
4789 ld[0] = ZFS_DELEG_LOCAL;
4791 ld[1] = ZFS_DELEG_DESCENDENT;
4794 if (perms != NULL) {
4796 char *end = curr + strlen(perms);
4798 while (curr < end) {
4799 char *delim = strchr(curr, ',');
4810 (void) nvlist_add_boolean(nvl, curr);
4816 for (i = 0; i < 2; i++) {
4817 char locality = ld[i];
4821 if (!nvlist_empty(base_nvl)) {
4823 (void) snprintf(who_buf,
4824 sizeof (who_buf), "%c%c$%s",
4825 base_type, locality, who);
4827 (void) snprintf(who_buf,
4828 sizeof (who_buf), "%c%c$",
4829 base_type, locality);
4831 (void) nvlist_add_nvlist(top_nvl, who_buf,
4836 if (!nvlist_empty(set_nvl)) {
4838 (void) snprintf(who_buf,
4839 sizeof (who_buf), "%c%c$%s",
4840 set_type, locality, who);
4842 (void) snprintf(who_buf,
4843 sizeof (who_buf), "%c%c$",
4844 set_type, locality);
4846 (void) nvlist_add_nvlist(top_nvl, who_buf,
4851 for (i = 0; i < 2; i++) {
4852 char locality = ld[i];
4857 (void) snprintf(who_buf, sizeof (who_buf),
4858 "%c%c$%s", base_type, locality, who);
4860 (void) snprintf(who_buf, sizeof (who_buf),
4861 "%c%c$", base_type, locality);
4862 (void) nvlist_add_boolean(top_nvl, who_buf);
4865 (void) snprintf(who_buf, sizeof (who_buf),
4866 "%c%c$%s", set_type, locality, who);
4868 (void) snprintf(who_buf, sizeof (who_buf),
4869 "%c%c$", set_type, locality);
4870 (void) nvlist_add_boolean(top_nvl, who_buf);
4876 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4878 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4882 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4883 opts->descend, opts->who, opts->perms, *nvlp);
4884 } else if (opts->create) {
4885 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4886 opts->descend, NULL, opts->perms, *nvlp);
4887 } else if (opts->everyone) {
4888 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4889 opts->descend, NULL, opts->perms, *nvlp);
4891 char *curr = opts->who;
4892 char *end = curr + strlen(curr);
4894 while (curr < end) {
4896 zfs_deleg_who_type_t who_type;
4898 char *delim = strchr(curr, ',');
4901 struct passwd *p = NULL;
4902 struct group *g = NULL;
4910 rid = (uid_t)strtol(curr, &endch, 0);
4912 who_type = ZFS_DELEG_USER;
4921 (void) snprintf(errbuf, 256, gettext(
4922 "invalid user %s"), curr);
4923 allow_usage(un, B_TRUE, errbuf);
4925 } else if (opts->group) {
4926 who_type = ZFS_DELEG_GROUP;
4935 (void) snprintf(errbuf, 256, gettext(
4936 "invalid group %s"), curr);
4937 allow_usage(un, B_TRUE, errbuf);
4940 if (*endch != '\0') {
4947 if (*endch != '\0') {
4954 who_type = ZFS_DELEG_USER;
4956 } else if (g != NULL) {
4957 who_type = ZFS_DELEG_GROUP;
4960 (void) snprintf(errbuf, 256, gettext(
4961 "invalid user/group %s"), curr);
4962 allow_usage(un, B_TRUE, errbuf);
4966 (void) sprintf(id, "%u", rid);
4969 store_allow_perm(who_type, opts->local,
4970 opts->descend, who, opts->perms, *nvlp);
4979 print_set_creat_perms(uu_avl_t *who_avl)
4981 const char *sc_title[] = {
4982 gettext("Permission sets:\n"),
4983 gettext("Create time permissions:\n"),
4986 const char **title_ptr = sc_title;
4987 who_perm_node_t *who_node = NULL;
4988 int prev_weight = -1;
4990 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4991 who_node = uu_avl_next(who_avl, who_node)) {
4992 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4993 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4994 const char *who_name = who_node->who_perm.who_name;
4995 int weight = who_type2weight(who_type);
4996 boolean_t first = B_TRUE;
4997 deleg_perm_node_t *deleg_node;
4999 if (prev_weight != weight) {
5000 (void) printf(*title_ptr++);
5001 prev_weight = weight;
5004 if (who_name == NULL || strnlen(who_name, 1) == 0)
5005 (void) printf("\t");
5007 (void) printf("\t%s ", who_name);
5009 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5010 deleg_node = uu_avl_next(avl, deleg_node)) {
5013 deleg_node->dpn_perm.dp_name);
5016 (void) printf(",%s",
5017 deleg_node->dpn_perm.dp_name);
5020 (void) printf("\n");
5025 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5028 who_perm_node_t *who_node = NULL;
5029 boolean_t prt_title = B_TRUE;
5030 uu_avl_walk_t *walk;
5032 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5035 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5036 const char *who_name = who_node->who_perm.who_name;
5037 const char *nice_who_name = who_node->who_perm.who_ug_name;
5038 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5039 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5041 deleg_perm_node_t *deleg_node;
5042 boolean_t prt_who = B_TRUE;
5044 for (deleg_node = uu_avl_first(avl);
5046 deleg_node = uu_avl_next(avl, deleg_node)) {
5047 if (local != deleg_node->dpn_perm.dp_local ||
5048 descend != deleg_node->dpn_perm.dp_descend)
5052 const char *who = NULL;
5054 prt_title = B_FALSE;
5055 (void) printf(title);
5059 case ZFS_DELEG_USER_SETS:
5060 case ZFS_DELEG_USER:
5061 who = gettext("user");
5063 who_name = nice_who_name;
5065 case ZFS_DELEG_GROUP_SETS:
5066 case ZFS_DELEG_GROUP:
5067 who = gettext("group");
5069 who_name = nice_who_name;
5071 case ZFS_DELEG_EVERYONE_SETS:
5072 case ZFS_DELEG_EVERYONE:
5073 who = gettext("everyone");
5078 if (who_name == NULL)
5079 (void) printf("\t%s", who);
5081 (void) printf("\t%s %s", who, who_name);
5084 (void) printf("%c%s", delim,
5085 deleg_node->dpn_perm.dp_name);
5090 (void) printf("\n");
5093 uu_avl_walk_end(walk);
5097 print_fs_perms(fs_perm_set_t *fspset)
5099 fs_perm_node_t *node = NULL;
5100 char buf[ZFS_MAXNAMELEN+32];
5101 const char *dsname = buf;
5103 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5104 node = uu_list_next(fspset->fsps_list, node)) {
5105 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5106 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5109 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5110 gettext("---- Permissions on %s "),
5111 node->fspn_fsperm.fsp_name);
5112 (void) printf(dsname);
5113 left = 70 - strlen(buf);
5116 (void) printf("\n");
5118 print_set_creat_perms(sc_avl);
5119 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5120 gettext("Local permissions:\n"));
5121 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5122 gettext("Descendent permissions:\n"));
5123 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5124 gettext("Local+Descendent permissions:\n"));
5128 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5130 struct deleg_perms {
5136 set_deleg_perms(zfs_handle_t *zhp, void *data)
5138 struct deleg_perms *perms = (struct deleg_perms *)data;
5139 zfs_type_t zfs_type = zfs_get_type(zhp);
5141 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5144 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5148 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5151 nvlist_t *perm_nvl = NULL;
5152 nvlist_t *update_perm_nvl = NULL;
5155 struct allow_opts opts = { 0 };
5157 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5160 while ((c = getopt(argc, argv, optstr)) != -1) {
5163 opts.local = B_TRUE;
5166 opts.descend = B_TRUE;
5172 opts.group = B_TRUE;
5175 opts.everyone = B_TRUE;
5181 opts.create = B_TRUE;
5184 opts.recursive = B_TRUE;
5187 (void) fprintf(stderr, gettext("missing argument for "
5188 "'%c' option\n"), optopt);
5192 opts.prt_usage = B_TRUE;
5195 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5204 /* check arguments */
5205 parse_allow_args(argc, argv, un, &opts);
5207 /* try to open the dataset */
5208 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5209 ZFS_TYPE_VOLUME)) == NULL) {
5210 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5215 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5218 fs_perm_set_init(&fs_perm_set);
5219 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5220 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5225 print_fs_perms(&fs_perm_set);
5227 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5228 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5231 if (un && opts.recursive) {
5232 struct deleg_perms data = { un, update_perm_nvl };
5233 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5242 nvlist_free(perm_nvl);
5243 if (update_perm_nvl != NULL)
5244 nvlist_free(update_perm_nvl);
5246 fs_perm_set_fini(&fs_perm_set);
5254 zfs_do_allow(int argc, char **argv)
5256 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5260 zfs_do_unallow(int argc, char **argv)
5262 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5266 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5271 boolean_t recursive = B_FALSE;
5272 const char *opts = holding ? "rt" : "r";
5276 while ((c = getopt(argc, argv, opts)) != -1) {
5282 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5291 /* check number of arguments */
5299 if (holding && tag[0] == '.') {
5300 /* tags starting with '.' are reserved for libzfs */
5301 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5305 for (i = 0; i < argc; ++i) {
5307 char parent[ZFS_MAXNAMELEN];
5309 char *path = argv[i];
5311 delim = strchr(path, '@');
5312 if (delim == NULL) {
5313 (void) fprintf(stderr,
5314 gettext("'%s' is not a snapshot\n"), path);
5318 (void) strncpy(parent, path, delim - path);
5319 parent[delim - path] = '\0';
5321 zhp = zfs_open(g_zfs, parent,
5322 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5328 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5331 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5337 return (errors != 0);
5341 * zfs hold [-r] [-t] <tag> <snap> ...
5343 * -r Recursively hold
5345 * Apply a user-hold with the given tag to the list of snapshots.
5348 zfs_do_hold(int argc, char **argv)
5350 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5354 * zfs release [-r] <tag> <snap> ...
5356 * -r Recursively release
5358 * Release a user-hold with the given tag from the list of snapshots.
5361 zfs_do_release(int argc, char **argv)
5363 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5366 typedef struct holds_cbdata {
5367 boolean_t cb_recursive;
5368 const char *cb_snapname;
5370 size_t cb_max_namelen;
5371 size_t cb_max_taglen;
5374 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5375 #define DATETIME_BUF_LEN (32)
5380 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5383 nvpair_t *nvp = NULL;
5384 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5388 for (i = 0; i < 3; i++) {
5389 col = gettext(hdr_cols[i]);
5391 (void) printf("%-*s ", i ? tagwidth : nwidth,
5394 (void) printf("%s\n", col);
5398 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5399 char *zname = nvpair_name(nvp);
5401 nvpair_t *nvp2 = NULL;
5402 (void) nvpair_value_nvlist(nvp, &nvl2);
5403 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5404 char tsbuf[DATETIME_BUF_LEN];
5405 char *tagname = nvpair_name(nvp2);
5409 char sep = scripted ? '\t' : ' ';
5410 size_t sepnum = scripted ? 1 : 2;
5412 (void) nvpair_value_uint64(nvp2, &val);
5414 (void) localtime_r(&time, &t);
5415 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5416 gettext(STRFTIME_FMT_STR), &t);
5418 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5419 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5425 * Generic callback function to list a dataset or snapshot.
5428 holds_callback(zfs_handle_t *zhp, void *data)
5430 holds_cbdata_t *cbp = data;
5431 nvlist_t *top_nvl = *cbp->cb_nvlp;
5432 nvlist_t *nvl = NULL;
5433 nvpair_t *nvp = NULL;
5434 const char *zname = zfs_get_name(zhp);
5435 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5437 if (cbp->cb_recursive) {
5438 const char *snapname;
5439 char *delim = strchr(zname, '@');
5443 snapname = delim + 1;
5444 if (strcmp(cbp->cb_snapname, snapname))
5448 if (zfs_get_holds(zhp, &nvl) != 0)
5451 if (znamelen > cbp->cb_max_namelen)
5452 cbp->cb_max_namelen = znamelen;
5454 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5455 const char *tag = nvpair_name(nvp);
5456 size_t taglen = strnlen(tag, MAXNAMELEN);
5457 if (taglen > cbp->cb_max_taglen)
5458 cbp->cb_max_taglen = taglen;
5461 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5465 * zfs holds [-r] <snap> ...
5467 * -r Recursively hold
5470 zfs_do_holds(int argc, char **argv)
5475 boolean_t scripted = B_FALSE;
5476 boolean_t recursive = B_FALSE;
5477 const char *opts = "rH";
5480 int types = ZFS_TYPE_SNAPSHOT;
5481 holds_cbdata_t cb = { 0 };
5488 while ((c = getopt(argc, argv, opts)) != -1) {
5497 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5504 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5505 flags |= ZFS_ITER_RECURSE;
5511 /* check number of arguments */
5515 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5518 for (i = 0; i < argc; ++i) {
5519 char *snapshot = argv[i];
5521 const char *snapname;
5523 delim = strchr(snapshot, '@');
5524 if (delim == NULL) {
5525 (void) fprintf(stderr,
5526 gettext("'%s' is not a snapshot\n"), snapshot);
5530 snapname = delim + 1;
5532 snapshot[delim - snapshot] = '\0';
5534 cb.cb_recursive = recursive;
5535 cb.cb_snapname = snapname;
5539 * 1. collect holds data, set format options
5541 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5542 holds_callback, &cb);
5548 * 2. print holds data
5550 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5552 if (nvlist_empty(nvl))
5553 (void) printf(gettext("no datasets available\n"));
5557 return (0 != errors);
5560 #define CHECK_SPINNER 30
5561 #define SPINNER_TIME 3 /* seconds */
5562 #define MOUNT_TIME 5 /* seconds */
5565 get_one_dataset(zfs_handle_t *zhp, void *data)
5567 static char *spin[] = { "-", "\\", "|", "/" };
5568 static int spinval = 0;
5569 static int spincheck = 0;
5570 static time_t last_spin_time = (time_t)0;
5571 get_all_cb_t *cbp = data;
5572 zfs_type_t type = zfs_get_type(zhp);
5574 if (cbp->cb_verbose) {
5575 if (--spincheck < 0) {
5576 time_t now = time(NULL);
5577 if (last_spin_time + SPINNER_TIME < now) {
5578 update_progress(spin[spinval++ % 4]);
5579 last_spin_time = now;
5581 spincheck = CHECK_SPINNER;
5586 * Interate over any nested datasets.
5588 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5594 * Skip any datasets whose type does not match.
5596 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5600 libzfs_add_handle(cbp, zhp);
5601 assert(cbp->cb_used <= cbp->cb_alloc);
5607 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5609 get_all_cb_t cb = { 0 };
5610 cb.cb_verbose = verbose;
5611 cb.cb_getone = get_one_dataset;
5614 set_progress_header(gettext("Reading ZFS config"));
5615 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5617 *dslist = cb.cb_handles;
5618 *count = cb.cb_used;
5621 finish_progress(gettext("done."));
5625 * Generic callback for sharing or mounting filesystems. Because the code is so
5626 * similar, we have a common function with an extra parameter to determine which
5627 * mode we are using.
5629 #define OP_SHARE 0x1
5630 #define OP_MOUNT 0x2
5633 * Share or mount a dataset.
5636 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5637 boolean_t explicit, const char *options)
5639 char mountpoint[ZFS_MAXPROPLEN];
5640 char shareopts[ZFS_MAXPROPLEN];
5641 char smbshareopts[ZFS_MAXPROPLEN];
5642 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5644 uint64_t zoned, canmount;
5645 boolean_t shared_nfs, shared_smb;
5647 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5650 * Check to make sure we can mount/share this dataset. If we
5651 * are in the global zone and the filesystem is exported to a
5652 * local zone, or if we are in a local zone and the
5653 * filesystem is not exported, then it is an error.
5655 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5657 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5661 (void) fprintf(stderr, gettext("cannot %s '%s': "
5662 "dataset is exported to a local zone\n"), cmdname,
5666 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5670 (void) fprintf(stderr, gettext("cannot %s '%s': "
5671 "permission denied\n"), cmdname,
5677 * Ignore any filesystems which don't apply to us. This
5678 * includes those with a legacy mountpoint, or those with
5679 * legacy share options.
5681 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5682 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5683 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5684 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5685 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5686 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5688 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5689 strcmp(smbshareopts, "off") == 0) {
5693 (void) fprintf(stderr, gettext("cannot share '%s': "
5694 "legacy share\n"), zfs_get_name(zhp));
5695 (void) fprintf(stderr, gettext("to "
5696 "share this filesystem set "
5697 "sharenfs property on\n"));
5702 * We cannot share or mount legacy filesystems. If the
5703 * shareopts is non-legacy but the mountpoint is legacy, we
5704 * treat it as a legacy share.
5706 if (strcmp(mountpoint, "legacy") == 0) {
5710 (void) fprintf(stderr, gettext("cannot %s '%s': "
5711 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5712 (void) fprintf(stderr, gettext("use %s(8) to "
5713 "%s this filesystem\n"), cmdname, cmdname);
5717 if (strcmp(mountpoint, "none") == 0) {
5721 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5722 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5727 * canmount explicit outcome
5728 * on no pass through
5729 * on yes pass through
5731 * off yes display error, return 1
5732 * noauto no return 0
5733 * noauto yes pass through
5735 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5736 if (canmount == ZFS_CANMOUNT_OFF) {
5740 (void) fprintf(stderr, gettext("cannot %s '%s': "
5741 "'canmount' property is set to 'off'\n"), cmdname,
5744 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5749 * At this point, we have verified that the mountpoint and/or
5750 * shareopts are appropriate for auto management. If the
5751 * filesystem is already mounted or shared, return (failing
5752 * for explicit requests); otherwise mount or share the
5758 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5759 shared_smb = zfs_is_shared_smb(zhp, NULL);
5761 if (shared_nfs && shared_smb ||
5762 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5763 strcmp(smbshareopts, "off") == 0) ||
5764 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5765 strcmp(shareopts, "off") == 0)) {
5769 (void) fprintf(stderr, gettext("cannot share "
5770 "'%s': filesystem already shared\n"),
5775 if (!zfs_is_mounted(zhp, NULL) &&
5776 zfs_mount(zhp, NULL, 0) != 0)
5779 if (protocol == NULL) {
5780 if (zfs_shareall(zhp) != 0)
5782 } else if (strcmp(protocol, "nfs") == 0) {
5783 if (zfs_share_nfs(zhp))
5785 } else if (strcmp(protocol, "smb") == 0) {
5786 if (zfs_share_smb(zhp))
5789 (void) fprintf(stderr, gettext("cannot share "
5790 "'%s': invalid share type '%s' "
5792 zfs_get_name(zhp), protocol);
5799 if (options == NULL)
5800 mnt.mnt_mntopts = "";
5802 mnt.mnt_mntopts = (char *)options;
5804 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5805 zfs_is_mounted(zhp, NULL)) {
5809 (void) fprintf(stderr, gettext("cannot mount "
5810 "'%s': filesystem already mounted\n"),
5815 if (zfs_mount(zhp, options, flags) != 0)
5824 * Reports progress in the form "(current/total)". Not thread-safe.
5827 report_mount_progress(int current, int total)
5829 static time_t last_progress_time = 0;
5830 time_t now = time(NULL);
5833 /* report 1..n instead of 0..n-1 */
5836 /* display header if we're here for the first time */
5838 set_progress_header(gettext("Mounting ZFS filesystems"));
5839 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5840 /* too soon to report again */
5844 last_progress_time = now;
5846 (void) sprintf(info, "(%d/%d)", current, total);
5848 if (current == total)
5849 finish_progress(info);
5851 update_progress(info);
5855 append_options(char *mntopts, char *newopts)
5857 int len = strlen(mntopts);
5859 /* original length plus new string to append plus 1 for the comma */
5860 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5861 (void) fprintf(stderr, gettext("the opts argument for "
5862 "'%c' option is too long (more than %d chars)\n"),
5863 "-o", MNT_LINE_MAX);
5868 mntopts[len++] = ',';
5870 (void) strcpy(&mntopts[len], newopts);
5874 share_mount(int op, int argc, char **argv)
5877 boolean_t verbose = B_FALSE;
5879 char *options = NULL;
5883 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5893 if (*optarg == '\0') {
5894 (void) fprintf(stderr, gettext("empty mount "
5895 "options (-o) specified\n"));
5899 if (options == NULL)
5900 options = safe_malloc(MNT_LINE_MAX + 1);
5902 /* option validation is done later */
5903 append_options(options, optarg);
5907 warnx("no overlay mounts support on FreeBSD, ignoring");
5910 (void) fprintf(stderr, gettext("missing argument for "
5911 "'%c' option\n"), optopt);
5915 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5924 /* check number of arguments */
5926 zfs_handle_t **dslist = NULL;
5927 size_t i, count = 0;
5928 char *protocol = NULL;
5930 if (op == OP_SHARE && argc > 0) {
5931 if (strcmp(argv[0], "nfs") != 0 &&
5932 strcmp(argv[0], "smb") != 0) {
5933 (void) fprintf(stderr, gettext("share type "
5934 "must be 'nfs' or 'smb'\n"));
5943 (void) fprintf(stderr, gettext("too many arguments\n"));
5947 start_progress_timer();
5948 get_all_datasets(&dslist, &count, verbose);
5953 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5955 for (i = 0; i < count; i++) {
5957 report_mount_progress(i, count);
5959 if (share_mount_one(dslist[i], op, flags, protocol,
5960 B_FALSE, options) != 0)
5962 zfs_close(dslist[i]);
5966 } else if (argc == 0) {
5967 struct mnttab entry;
5969 if ((op == OP_SHARE) || (options != NULL)) {
5970 (void) fprintf(stderr, gettext("missing filesystem "
5971 "argument (specify -a for all)\n"));
5976 * When mount is given no arguments, go through /etc/mnttab and
5977 * display any active ZFS mounts. We hide any snapshots, since
5978 * they are controlled automatically.
5980 rewind(mnttab_file);
5981 while (getmntent(mnttab_file, &entry) == 0) {
5982 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5983 strchr(entry.mnt_special, '@') != NULL)
5986 (void) printf("%-30s %s\n", entry.mnt_special,
5994 (void) fprintf(stderr,
5995 gettext("too many arguments\n"));
5999 if ((zhp = zfs_open(g_zfs, argv[0],
6000 ZFS_TYPE_FILESYSTEM)) == NULL) {
6003 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6013 * zfs mount -a [nfs]
6014 * zfs mount filesystem
6016 * Mount all filesystems, or mount the given filesystem.
6019 zfs_do_mount(int argc, char **argv)
6021 return (share_mount(OP_MOUNT, argc, argv));
6025 * zfs share -a [nfs | smb]
6026 * zfs share filesystem
6028 * Share all filesystems, or share the given filesystem.
6031 zfs_do_share(int argc, char **argv)
6033 return (share_mount(OP_SHARE, argc, argv));
6036 typedef struct unshare_unmount_node {
6037 zfs_handle_t *un_zhp;
6039 uu_avl_node_t un_avlnode;
6040 } unshare_unmount_node_t;
6044 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6046 const unshare_unmount_node_t *l = larg;
6047 const unshare_unmount_node_t *r = rarg;
6049 return (strcmp(l->un_mountp, r->un_mountp));
6053 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6054 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6055 * and unmount it appropriately.
6058 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6062 struct stat64 statbuf;
6063 struct extmnttab entry;
6064 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6068 * Search for the path in /etc/mnttab. Rather than looking for the
6069 * specific path, which can be fooled by non-standard paths (i.e. ".."
6070 * or "//"), we stat() the path and search for the corresponding
6071 * (major,minor) device pair.
6073 if (stat64(path, &statbuf) != 0) {
6074 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6075 cmdname, path, strerror(errno));
6078 path_inode = statbuf.st_ino;
6081 * Search for the given (major,minor) pair in the mount table.
6084 rewind(mnttab_file);
6085 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6086 if (entry.mnt_major == major(statbuf.st_dev) &&
6087 entry.mnt_minor == minor(statbuf.st_dev))
6094 if (statfs(path, &sfs) != 0) {
6095 (void) fprintf(stderr, "%s: %s\n", path,
6099 statfs2mnttab(&sfs, &entry);
6103 if (op == OP_SHARE) {
6104 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6105 "currently mounted\n"), cmdname, path);
6108 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6110 if ((ret = umount2(path, flags)) != 0)
6111 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6116 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6117 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6118 "filesystem\n"), cmdname, path);
6122 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6123 ZFS_TYPE_FILESYSTEM)) == NULL)
6127 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6128 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6129 cmdname, path, strerror(errno));
6131 } else if (statbuf.st_ino != path_inode) {
6132 (void) fprintf(stderr, gettext("cannot "
6133 "%s '%s': not a mountpoint\n"), cmdname, path);
6137 if (op == OP_SHARE) {
6138 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6139 char smbshare_prop[ZFS_MAXPROPLEN];
6141 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6142 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6143 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6144 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6146 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6147 strcmp(smbshare_prop, "off") == 0) {
6148 (void) fprintf(stderr, gettext("cannot unshare "
6149 "'%s': legacy share\n"), path);
6151 (void) fprintf(stderr, gettext("use "
6152 "unshare(1M) to unshare this filesystem\n"));
6154 } else if (!zfs_is_shared(zhp)) {
6155 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6156 "not currently shared\n"), path);
6158 ret = zfs_unshareall_bypath(zhp, path);
6161 char mtpt_prop[ZFS_MAXPROPLEN];
6163 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6164 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6167 ret = zfs_unmount(zhp, NULL, flags);
6168 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6169 (void) fprintf(stderr, gettext("cannot unmount "
6170 "'%s': legacy mountpoint\n"),
6172 (void) fprintf(stderr, gettext("use umount(8) "
6173 "to unmount this filesystem\n"));
6175 ret = zfs_unmountall(zhp, flags);
6186 * Generic callback for unsharing or unmounting a filesystem.
6189 unshare_unmount(int op, int argc, char **argv)
6196 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6197 char sharesmb[ZFS_MAXPROPLEN];
6200 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6209 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6220 * We could make use of zfs_for_each() to walk all datasets in
6221 * the system, but this would be very inefficient, especially
6222 * since we would have to linearly search /etc/mnttab for each
6223 * one. Instead, do one pass through /etc/mnttab looking for
6224 * zfs entries and call zfs_unmount() for each one.
6226 * Things get a little tricky if the administrator has created
6227 * mountpoints beneath other ZFS filesystems. In this case, we
6228 * have to unmount the deepest filesystems first. To accomplish
6229 * this, we place all the mountpoints in an AVL tree sorted by
6230 * the special type (dataset name), and walk the result in
6231 * reverse to make sure to get any snapshots first.
6233 struct mnttab entry;
6234 uu_avl_pool_t *pool;
6236 unshare_unmount_node_t *node;
6238 uu_avl_walk_t *walk;
6241 (void) fprintf(stderr, gettext("too many arguments\n"));
6245 if (((pool = uu_avl_pool_create("unmount_pool",
6246 sizeof (unshare_unmount_node_t),
6247 offsetof(unshare_unmount_node_t, un_avlnode),
6248 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6249 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6252 rewind(mnttab_file);
6253 while (getmntent(mnttab_file, &entry) == 0) {
6255 /* ignore non-ZFS entries */
6256 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6259 /* ignore snapshots */
6260 if (strchr(entry.mnt_special, '@') != NULL)
6263 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6264 ZFS_TYPE_FILESYSTEM)) == NULL) {
6271 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6273 sizeof (nfs_mnt_prop),
6274 NULL, NULL, 0, B_FALSE) == 0);
6275 if (strcmp(nfs_mnt_prop, "off") != 0)
6277 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6279 sizeof (nfs_mnt_prop),
6280 NULL, NULL, 0, B_FALSE) == 0);
6281 if (strcmp(nfs_mnt_prop, "off") == 0)
6285 /* Ignore legacy mounts */
6286 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6288 sizeof (nfs_mnt_prop),
6289 NULL, NULL, 0, B_FALSE) == 0);
6290 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6292 /* Ignore canmount=noauto mounts */
6293 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6294 ZFS_CANMOUNT_NOAUTO)
6300 node = safe_malloc(sizeof (unshare_unmount_node_t));
6302 node->un_mountp = safe_strdup(entry.mnt_mountp);
6304 uu_avl_node_init(node, &node->un_avlnode, pool);
6306 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6307 uu_avl_insert(tree, node, idx);
6309 zfs_close(node->un_zhp);
6310 free(node->un_mountp);
6316 * Walk the AVL tree in reverse, unmounting each filesystem and
6317 * removing it from the AVL tree in the process.
6319 if ((walk = uu_avl_walk_start(tree,
6320 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6323 while ((node = uu_avl_walk_next(walk)) != NULL) {
6324 uu_avl_remove(tree, node);
6328 if (zfs_unshareall_bypath(node->un_zhp,
6329 node->un_mountp) != 0)
6334 if (zfs_unmount(node->un_zhp,
6335 node->un_mountp, flags) != 0)
6340 zfs_close(node->un_zhp);
6341 free(node->un_mountp);
6345 uu_avl_walk_end(walk);
6346 uu_avl_destroy(tree);
6347 uu_avl_pool_destroy(pool);
6352 (void) fprintf(stderr,
6353 gettext("missing filesystem argument\n"));
6355 (void) fprintf(stderr,
6356 gettext("too many arguments\n"));
6361 * We have an argument, but it may be a full path or a ZFS
6362 * filesystem. Pass full paths off to unmount_path() (shared by
6363 * manual_unmount), otherwise open the filesystem and pass to
6366 if (argv[0][0] == '/')
6367 return (unshare_unmount_path(op, argv[0],
6370 if ((zhp = zfs_open(g_zfs, argv[0],
6371 ZFS_TYPE_FILESYSTEM)) == NULL)
6374 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6375 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6376 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6377 NULL, 0, B_FALSE) == 0);
6381 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6383 sizeof (nfs_mnt_prop),
6384 NULL, NULL, 0, B_FALSE) == 0);
6385 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6386 sharesmb, sizeof (sharesmb), NULL, NULL,
6389 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6390 strcmp(sharesmb, "off") == 0) {
6391 (void) fprintf(stderr, gettext("cannot "
6392 "unshare '%s': legacy share\n"),
6395 (void) fprintf(stderr, gettext("use "
6396 "unshare(1M) to unshare this "
6400 } else if (!zfs_is_shared(zhp)) {
6401 (void) fprintf(stderr, gettext("cannot "
6402 "unshare '%s': not currently "
6403 "shared\n"), zfs_get_name(zhp));
6405 } else if (zfs_unshareall(zhp) != 0) {
6411 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6412 (void) fprintf(stderr, gettext("cannot "
6413 "unmount '%s': legacy "
6414 "mountpoint\n"), zfs_get_name(zhp));
6415 (void) fprintf(stderr, gettext("use "
6416 "umount(8) to unmount this "
6419 } else if (!zfs_is_mounted(zhp, NULL)) {
6420 (void) fprintf(stderr, gettext("cannot "
6421 "unmount '%s': not currently "
6425 } else if (zfs_unmountall(zhp, flags) != 0) {
6439 * zfs unmount filesystem
6441 * Unmount all filesystems, or a specific ZFS filesystem.
6444 zfs_do_unmount(int argc, char **argv)
6446 return (unshare_unmount(OP_MOUNT, argc, argv));
6451 * zfs unshare filesystem
6453 * Unshare all filesystems, or a specific ZFS filesystem.
6456 zfs_do_unshare(int argc, char **argv)
6458 return (unshare_unmount(OP_SHARE, argc, argv));
6462 * Attach/detach the given dataset to/from the given jail
6466 do_jail(int argc, char **argv, int attach)
6471 /* check number of arguments */
6473 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6477 (void) fprintf(stderr, gettext("too many arguments\n"));
6481 jailid = jail_getid(argv[1]);
6483 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6487 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6491 ret = (zfs_jail(zhp, jailid, attach) != 0);
6498 * zfs jail jailid filesystem
6500 * Attach the given dataset to the given jail
6504 zfs_do_jail(int argc, char **argv)
6507 return (do_jail(argc, argv, 1));
6511 * zfs unjail jailid filesystem
6513 * Detach the given dataset from the given jail
6517 zfs_do_unjail(int argc, char **argv)
6520 return (do_jail(argc, argv, 0));
6524 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6525 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6528 manual_mount(int argc, char **argv)
6531 char mountpoint[ZFS_MAXPROPLEN];
6532 char mntopts[MNT_LINE_MAX] = { '\0' };
6536 char *dataset, *path;
6539 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6542 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6545 flags |= MS_OVERLAY;
6548 flags |= MS_NOMNTTAB;
6551 (void) fprintf(stderr, gettext("missing argument for "
6552 "'%c' option\n"), optopt);
6556 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6558 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6567 /* check that we only have two arguments */
6570 (void) fprintf(stderr, gettext("missing dataset "
6573 (void) fprintf(stderr,
6574 gettext("missing mountpoint argument\n"));
6576 (void) fprintf(stderr, gettext("too many arguments\n"));
6577 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6584 /* try to open the dataset */
6585 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6588 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6589 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6591 /* check for legacy mountpoint and complain appropriately */
6593 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6594 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6595 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6596 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6601 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6602 "mounted using 'mount -t zfs'\n"), dataset);
6603 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6604 "instead.\n"), path);
6605 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6606 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6607 (void) fprintf(stderr, gettext("See zfs(8) for more "
6616 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6617 * unmounts of non-legacy filesystems, as this is the dominant administrative
6621 manual_unmount(int argc, char **argv)
6627 while ((c = getopt(argc, argv, "f")) != -1) {
6633 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6635 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6644 /* check arguments */
6647 (void) fprintf(stderr, gettext("missing path "
6650 (void) fprintf(stderr, gettext("too many arguments\n"));
6651 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6655 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6659 find_command_idx(char *command, int *idx)
6663 for (i = 0; i < NCOMMAND; i++) {
6664 if (command_table[i].name == NULL)
6667 if (strcmp(command, command_table[i].name) == 0) {
6676 zfs_do_diff(int argc, char **argv)
6680 char *tosnap = NULL;
6681 char *fromsnap = NULL;
6686 while ((c = getopt(argc, argv, "FHt")) != -1) {
6689 flags |= ZFS_DIFF_CLASSIFY;
6692 flags |= ZFS_DIFF_PARSEABLE;
6695 flags |= ZFS_DIFF_TIMESTAMP;
6698 (void) fprintf(stderr,
6699 gettext("invalid option '%c'\n"), optopt);
6708 (void) fprintf(stderr,
6709 gettext("must provide at least one snapshot name\n"));
6714 (void) fprintf(stderr, gettext("too many arguments\n"));
6719 tosnap = (argc == 2) ? argv[1] : NULL;
6722 if (*fromsnap != '@')
6723 copy = strdup(fromsnap);
6725 copy = strdup(tosnap);
6729 if (atp = strchr(copy, '@'))
6732 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6738 * Ignore SIGPIPE so that the library can give us
6739 * information on any failure
6741 (void) sigignore(SIGPIPE);
6743 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6751 * zfs bookmark <fs@snap> <fs#bmark>
6753 * Creates a bookmark with the given name from the given snapshot.
6756 zfs_do_bookmark(int argc, char **argv)
6758 char snapname[ZFS_MAXNAMELEN];
6765 while ((c = getopt(argc, argv, "")) != -1) {
6768 (void) fprintf(stderr,
6769 gettext("invalid option '%c'\n"), optopt);
6777 /* check number of arguments */
6779 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6783 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6787 if (strchr(argv[1], '#') == NULL) {
6788 (void) fprintf(stderr,
6789 gettext("invalid bookmark name '%s' -- "
6790 "must contain a '#'\n"), argv[1]);
6794 if (argv[0][0] == '@') {
6796 * Snapshot name begins with @.
6797 * Default to same fs as bookmark.
6799 (void) strncpy(snapname, argv[1], sizeof (snapname));
6800 *strchr(snapname, '#') = '\0';
6801 (void) strlcat(snapname, argv[0], sizeof (snapname));
6803 (void) strncpy(snapname, argv[0], sizeof (snapname));
6805 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6811 nvl = fnvlist_alloc();
6812 fnvlist_add_string(nvl, argv[1], snapname);
6813 ret = lzc_bookmark(nvl, NULL);
6817 const char *err_msg;
6820 (void) snprintf(errbuf, sizeof (errbuf),
6821 dgettext(TEXT_DOMAIN,
6822 "cannot create bookmark '%s'"), argv[1]);
6826 err_msg = "bookmark is in a different pool";
6829 err_msg = "bookmark exists";
6832 err_msg = "invalid argument";
6835 err_msg = "bookmark feature not enabled";
6838 err_msg = "unknown error";
6841 (void) fprintf(stderr, "%s: %s\n", errbuf,
6842 dgettext(TEXT_DOMAIN, err_msg));
6853 main(int argc, char **argv)
6860 (void) setlocale(LC_ALL, "");
6861 (void) textdomain(TEXT_DOMAIN);
6865 if ((g_zfs = libzfs_init()) == NULL) {
6866 (void) fprintf(stderr, gettext("internal error: failed to "
6867 "initialize ZFS library\n"));
6871 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6873 libzfs_print_on_error(g_zfs, B_TRUE);
6875 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6876 (void) fprintf(stderr, gettext("internal error: unable to "
6877 "open %s\n"), MNTTAB);
6882 * This command also doubles as the /etc/fs mount and unmount program.
6883 * Determine if we should take this behavior based on argv[0].
6885 progname = basename(argv[0]);
6886 if (strcmp(progname, "mount") == 0) {
6887 ret = manual_mount(argc, argv);
6888 } else if (strcmp(progname, "umount") == 0) {
6889 ret = manual_unmount(argc, argv);
6892 * Make sure the user has specified some command.
6895 (void) fprintf(stderr, gettext("missing command\n"));
6902 * The 'umount' command is an alias for 'unmount'
6904 if (strcmp(cmdname, "umount") == 0)
6905 cmdname = "unmount";
6908 * The 'recv' command is an alias for 'receive'
6910 if (strcmp(cmdname, "recv") == 0)
6911 cmdname = "receive";
6914 * The 'snap' command is an alias for 'snapshot'
6916 if (strcmp(cmdname, "snap") == 0)
6917 cmdname = "snapshot";
6922 if (strcmp(cmdname, "-?") == 0)
6926 * Run the appropriate command.
6928 libzfs_mnttab_cache(g_zfs, B_TRUE);
6929 if (find_command_idx(cmdname, &i) == 0) {
6930 current_command = &command_table[i];
6931 ret = command_table[i].func(argc - 1, argv + 1);
6932 } else if (strchr(cmdname, '=') != NULL) {
6933 verify(find_command_idx("set", &i) == 0);
6934 current_command = &command_table[i];
6935 ret = command_table[i].func(argc, argv);
6937 (void) fprintf(stderr, gettext("unrecognized "
6938 "command '%s'\n"), cmdname);
6941 libzfs_mnttab_cache(g_zfs, B_FALSE);
6944 (void) fclose(mnttab_file);
6946 if (ret == 0 && log_history)
6947 (void) zpool_log_history(g_zfs, history_str);
6952 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6953 * for the purposes of running ::findleaks.
6955 if (getenv("ZFS_ABORT") != NULL) {
6956 (void) printf("dumping core by request\n");