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)
498 char *propname = optarg;
499 char *propval, *strval;
501 if ((propval = strchr(propname, '=')) == NULL) {
502 (void) fprintf(stderr, gettext("missing "
503 "'=' for -o option\n"));
508 if (nvlist_lookup_string(props, propname, &strval) == 0) {
509 (void) fprintf(stderr, gettext("property '%s' "
510 "specified multiple times\n"), propname);
513 if (nvlist_add_string(props, propname, propval) != 0)
519 parse_depth(char *opt, int *flags)
524 depth = (int)strtol(opt, &tmp, 0);
526 (void) fprintf(stderr,
527 gettext("%s is not an integer\n"), optarg);
531 (void) fprintf(stderr,
532 gettext("Depth can not be negative.\n"));
535 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
539 #define PROGRESS_DELAY 2 /* seconds */
541 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";
542 static time_t pt_begin;
543 static char *pt_header = NULL;
544 static boolean_t pt_shown;
547 start_progress_timer(void)
549 pt_begin = time(NULL) + PROGRESS_DELAY;
554 set_progress_header(char *header)
556 assert(pt_header == NULL);
557 pt_header = safe_strdup(header);
559 (void) printf("%s: ", header);
560 (void) fflush(stdout);
565 update_progress(char *update)
567 if (!pt_shown && time(NULL) > pt_begin) {
568 int len = strlen(update);
570 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
572 (void) fflush(stdout);
574 } else if (pt_shown) {
575 int len = strlen(update);
577 (void) printf("%s%*.*s", update, len, len, pt_reverse);
578 (void) fflush(stdout);
583 finish_progress(char *done)
586 (void) printf("%s\n", done);
587 (void) fflush(stdout);
593 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
595 * Given an existing dataset, create a writable copy whose initial contents
596 * are the same as the source. The newly created dataset maintains a
597 * dependency on the original; the original cannot be destroyed so long as
600 * The '-p' flag creates all the non-existing ancestors of the target first.
603 zfs_do_clone(int argc, char **argv)
605 zfs_handle_t *zhp = NULL;
606 boolean_t parents = B_FALSE;
611 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
615 while ((c = getopt(argc, argv, "o:p")) != -1) {
618 if (parseprop(props))
625 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
634 /* check number of arguments */
636 (void) fprintf(stderr, gettext("missing source dataset "
641 (void) fprintf(stderr, gettext("missing target dataset "
646 (void) fprintf(stderr, gettext("too many arguments\n"));
650 /* open the source dataset */
651 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
654 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
657 * Now create the ancestors of the target dataset. If the
658 * target already exists and '-p' option was used we should not
661 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
664 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
669 ret = zfs_clone(zhp, argv[1], props);
671 /* create the mountpoint if necessary */
675 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
677 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
678 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
679 ret = zfs_share(clone);
698 * zfs create [-p] [-o prop=value] ... fs
699 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
701 * Create a new dataset. This command can be used to create filesystems
702 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
703 * For volumes, the user must specify a size to be used.
705 * The '-s' flag applies only to volumes, and indicates that we should not try
706 * to set the reservation for this volume. By default we set a reservation
707 * equal to the size for any volume. For pools with SPA_VERSION >=
708 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
710 * The '-p' flag creates all the non-existing ancestors of the target first.
713 zfs_do_create(int argc, char **argv)
715 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
716 zfs_handle_t *zhp = NULL;
719 boolean_t noreserve = B_FALSE;
720 boolean_t bflag = B_FALSE;
721 boolean_t parents = B_FALSE;
725 int canmount = ZFS_CANMOUNT_OFF;
727 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
731 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
734 type = ZFS_TYPE_VOLUME;
735 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
736 (void) fprintf(stderr, gettext("bad volume "
737 "size '%s': %s\n"), optarg,
738 libzfs_error_description(g_zfs));
742 if (nvlist_add_uint64(props,
743 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
752 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
753 (void) fprintf(stderr, gettext("bad volume "
754 "block size '%s': %s\n"), optarg,
755 libzfs_error_description(g_zfs));
759 if (nvlist_add_uint64(props,
760 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
765 if (parseprop(props))
772 (void) fprintf(stderr, gettext("missing size "
776 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
782 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
783 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
784 "used when creating a volume\n"));
791 /* check number of arguments */
793 (void) fprintf(stderr, gettext("missing %s argument\n"),
794 zfs_type_to_name(type));
798 (void) fprintf(stderr, gettext("too many arguments\n"));
802 if (type == ZFS_TYPE_VOLUME && !noreserve) {
803 zpool_handle_t *zpool_handle;
804 uint64_t spa_version;
806 zfs_prop_t resv_prop;
809 if (p = strchr(argv[0], '/'))
811 zpool_handle = zpool_open(g_zfs, argv[0]);
814 if (zpool_handle == NULL)
816 spa_version = zpool_get_prop_int(zpool_handle,
817 ZPOOL_PROP_VERSION, NULL);
818 zpool_close(zpool_handle);
819 if (spa_version >= SPA_VERSION_REFRESERVATION)
820 resv_prop = ZFS_PROP_REFRESERVATION;
822 resv_prop = ZFS_PROP_RESERVATION;
823 volsize = zvol_volsize_to_reservation(volsize, props);
825 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
827 if (nvlist_add_uint64(props,
828 zfs_prop_to_name(resv_prop), volsize) != 0) {
835 if (parents && zfs_name_valid(argv[0], type)) {
837 * Now create the ancestors of target dataset. If the target
838 * already exists and '-p' option was used we should not
841 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
845 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
850 if (zfs_create(g_zfs, argv[0], type, props) != 0)
853 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
858 * if the user doesn't want the dataset automatically mounted,
859 * then skip the mount/share step
861 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
862 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
865 * Mount and/or share the new filesystem as appropriate. We provide a
866 * verbose error message to let the user know that their filesystem was
867 * in fact created, even if we failed to mount or share it.
869 if (canmount == ZFS_CANMOUNT_ON) {
870 if (zfs_mount(zhp, NULL, 0) != 0) {
871 (void) fprintf(stderr, gettext("filesystem "
872 "successfully created, but not mounted\n"));
874 } else if (zfs_share(zhp) != 0) {
875 (void) fprintf(stderr, gettext("filesystem "
876 "successfully created, but not shared\n"));
893 * zfs destroy [-rRf] <fs, vol>
894 * zfs destroy [-rRd] <snap>
896 * -r Recursively destroy all children
897 * -R Recursively destroy all dependents, including clones
898 * -f Force unmounting of any dependents
899 * -d If we can't destroy now, mark for deferred destruction
901 * Destroys the given dataset. By default, it will unmount any filesystems,
902 * and refuse to destroy a dataset that has any dependents. A dependent can
903 * either be a child, or a clone of a child.
905 typedef struct destroy_cbdata {
908 boolean_t cb_recurse;
910 boolean_t cb_doclones;
911 zfs_handle_t *cb_target;
912 boolean_t cb_defer_destroy;
913 boolean_t cb_verbose;
914 boolean_t cb_parsable;
917 nvlist_t *cb_batchedsnaps;
919 /* first snap in contiguous run */
921 /* previous snap in contiguous run */
929 * Check for any dependents based on the '-r' or '-R' flags.
932 destroy_check_dependent(zfs_handle_t *zhp, void *data)
934 destroy_cbdata_t *cbp = data;
935 const char *tname = zfs_get_name(cbp->cb_target);
936 const char *name = zfs_get_name(zhp);
938 if (strncmp(tname, name, strlen(tname)) == 0 &&
939 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
941 * This is a direct descendant, not a clone somewhere else in
948 (void) fprintf(stderr, gettext("cannot destroy '%s': "
949 "%s has children\n"),
950 zfs_get_name(cbp->cb_target),
951 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
952 (void) fprintf(stderr, gettext("use '-r' to destroy "
953 "the following datasets:\n"));
954 cbp->cb_first = B_FALSE;
955 cbp->cb_error = B_TRUE;
958 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
961 * This is a clone. We only want to report this if the '-r'
962 * wasn't specified, or the target is a snapshot.
964 if (!cbp->cb_recurse &&
965 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
969 (void) fprintf(stderr, gettext("cannot destroy '%s': "
970 "%s has dependent clones\n"),
971 zfs_get_name(cbp->cb_target),
972 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
973 (void) fprintf(stderr, gettext("use '-R' to destroy "
974 "the following datasets:\n"));
975 cbp->cb_first = B_FALSE;
976 cbp->cb_error = B_TRUE;
977 cbp->cb_dryrun = B_TRUE;
980 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
989 destroy_callback(zfs_handle_t *zhp, void *data)
991 destroy_cbdata_t *cb = data;
992 const char *name = zfs_get_name(zhp);
994 if (cb->cb_verbose) {
995 if (cb->cb_parsable) {
996 (void) printf("destroy\t%s\n", name);
997 } else if (cb->cb_dryrun) {
998 (void) printf(gettext("would destroy %s\n"),
1001 (void) printf(gettext("will destroy %s\n"),
1007 * Ignore pools (which we've already flagged as an error before getting
1010 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1011 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1015 if (cb->cb_dryrun) {
1021 * We batch up all contiguous snapshots (even of different
1022 * filesystems) and destroy them with one ioctl. We can't
1023 * simply do all snap deletions and then all fs deletions,
1024 * because we must delete a clone before its origin.
1026 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1027 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1029 int error = zfs_destroy_snaps_nvl(g_zfs,
1030 cb->cb_batchedsnaps, B_FALSE);
1031 fnvlist_free(cb->cb_batchedsnaps);
1032 cb->cb_batchedsnaps = fnvlist_alloc();
1035 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1036 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1047 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1049 destroy_cbdata_t *cb = arg;
1050 const char *name = zfs_get_name(zhp);
1053 if (nvlist_exists(cb->cb_nvl, name)) {
1054 if (cb->cb_firstsnap == NULL)
1055 cb->cb_firstsnap = strdup(name);
1056 if (cb->cb_prevsnap != NULL)
1057 free(cb->cb_prevsnap);
1058 /* this snap continues the current range */
1059 cb->cb_prevsnap = strdup(name);
1060 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1062 if (cb->cb_verbose) {
1063 if (cb->cb_parsable) {
1064 (void) printf("destroy\t%s\n", name);
1065 } else if (cb->cb_dryrun) {
1066 (void) printf(gettext("would destroy %s\n"),
1069 (void) printf(gettext("will destroy %s\n"),
1073 } else if (cb->cb_firstsnap != NULL) {
1074 /* end of this range */
1076 err = lzc_snaprange_space(cb->cb_firstsnap,
1077 cb->cb_prevsnap, &used);
1078 cb->cb_snapused += used;
1079 free(cb->cb_firstsnap);
1080 cb->cb_firstsnap = NULL;
1081 free(cb->cb_prevsnap);
1082 cb->cb_prevsnap = NULL;
1089 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1092 assert(cb->cb_firstsnap == NULL);
1093 assert(cb->cb_prevsnap == NULL);
1094 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1095 if (cb->cb_firstsnap != NULL) {
1098 err = lzc_snaprange_space(cb->cb_firstsnap,
1099 cb->cb_prevsnap, &used);
1101 cb->cb_snapused += used;
1102 free(cb->cb_firstsnap);
1103 cb->cb_firstsnap = NULL;
1104 free(cb->cb_prevsnap);
1105 cb->cb_prevsnap = NULL;
1111 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1113 destroy_cbdata_t *cb = arg;
1116 /* Check for clones. */
1117 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1118 cb->cb_target = zhp;
1119 cb->cb_first = B_TRUE;
1120 err = zfs_iter_dependents(zhp, B_TRUE,
1121 destroy_check_dependent, cb);
1125 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1133 gather_snapshots(zfs_handle_t *zhp, void *arg)
1135 destroy_cbdata_t *cb = arg;
1138 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1144 if (cb->cb_verbose) {
1145 err = destroy_print_snapshots(zhp, cb);
1151 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1159 destroy_clones(destroy_cbdata_t *cb)
1162 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1164 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1165 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1168 boolean_t defer = cb->cb_defer_destroy;
1172 * We can't defer destroy non-snapshots, so set it to
1173 * false while destroying the clones.
1175 cb->cb_defer_destroy = B_FALSE;
1176 err = zfs_iter_dependents(zhp, B_FALSE,
1177 destroy_callback, cb);
1178 cb->cb_defer_destroy = defer;
1188 zfs_do_destroy(int argc, char **argv)
1190 destroy_cbdata_t cb = { 0 };
1194 zfs_handle_t *zhp = NULL;
1196 zfs_type_t type = ZFS_TYPE_DATASET;
1199 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1202 cb.cb_verbose = B_TRUE;
1205 cb.cb_verbose = B_TRUE;
1206 cb.cb_parsable = B_TRUE;
1209 cb.cb_dryrun = B_TRUE;
1212 cb.cb_defer_destroy = B_TRUE;
1213 type = ZFS_TYPE_SNAPSHOT;
1216 cb.cb_force = B_TRUE;
1219 cb.cb_recurse = B_TRUE;
1222 cb.cb_recurse = B_TRUE;
1223 cb.cb_doclones = B_TRUE;
1227 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1236 /* check number of arguments */
1238 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1242 (void) fprintf(stderr, gettext("too many arguments\n"));
1246 at = strchr(argv[0], '@');
1247 pound = strchr(argv[0], '#');
1250 /* Build the list of snaps to destroy in cb_nvl. */
1251 cb.cb_nvl = fnvlist_alloc();
1254 zhp = zfs_open(g_zfs, argv[0],
1255 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1259 cb.cb_snapspec = at + 1;
1260 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1266 if (nvlist_empty(cb.cb_nvl)) {
1267 (void) fprintf(stderr, gettext("could not find any "
1268 "snapshots to destroy; check snapshot names.\n"));
1273 if (cb.cb_verbose) {
1275 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1276 if (cb.cb_parsable) {
1277 (void) printf("reclaim\t%llu\n",
1279 } else if (cb.cb_dryrun) {
1280 (void) printf(gettext("would reclaim %s\n"),
1283 (void) printf(gettext("will reclaim %s\n"),
1288 if (!cb.cb_dryrun) {
1289 if (cb.cb_doclones) {
1290 cb.cb_batchedsnaps = fnvlist_alloc();
1291 err = destroy_clones(&cb);
1293 err = zfs_destroy_snaps_nvl(g_zfs,
1294 cb.cb_batchedsnaps, B_FALSE);
1302 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1303 cb.cb_defer_destroy);
1309 } else if (pound != NULL) {
1314 (void) fprintf(stderr,
1315 "dryrun is not supported with bookmark\n");
1319 if (cb.cb_defer_destroy) {
1320 (void) fprintf(stderr,
1321 "defer destroy is not supported with bookmark\n");
1325 if (cb.cb_recurse) {
1326 (void) fprintf(stderr,
1327 "recursive is not supported with bookmark\n");
1331 if (!zfs_bookmark_exists(argv[0])) {
1332 (void) fprintf(stderr, gettext("bookmark '%s' "
1333 "does not exist.\n"), argv[0]);
1337 nvl = fnvlist_alloc();
1338 fnvlist_add_boolean(nvl, argv[0]);
1340 err = lzc_destroy_bookmarks(nvl, NULL);
1342 (void) zfs_standard_error(g_zfs, err,
1343 "cannot destroy bookmark");
1346 nvlist_free(cb.cb_nvl);
1350 /* Open the given dataset */
1351 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1357 * Perform an explicit check for pools before going any further.
1359 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1360 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1361 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1362 "operation does not apply to pools\n"),
1364 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1365 "%s' to destroy all datasets in the pool\n"),
1367 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1368 "to destroy the pool itself\n"), zfs_get_name(zhp));
1374 * Check for any dependents and/or clones.
1376 cb.cb_first = B_TRUE;
1377 if (!cb.cb_doclones &&
1378 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1389 cb.cb_batchedsnaps = fnvlist_alloc();
1390 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1397 * Do the real thing. The callback will close the
1398 * handle regardless of whether it succeeds or not.
1400 err = destroy_callback(zhp, &cb);
1403 err = zfs_destroy_snaps_nvl(g_zfs,
1404 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1411 fnvlist_free(cb.cb_batchedsnaps);
1412 fnvlist_free(cb.cb_nvl);
1419 is_recvd_column(zprop_get_cbdata_t *cbp)
1422 zfs_get_column_t col;
1424 for (i = 0; i < ZFS_GET_NCOLS &&
1425 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1426 if (col == GET_COL_RECVD)
1432 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1433 * < all | property[,property]... > < fs | snap | vol > ...
1435 * -r recurse over any child datasets
1436 * -H scripted mode. Headers are stripped, and fields are separated
1437 * by tabs instead of spaces.
1438 * -o Set of fields to display. One of "name,property,value,
1439 * received,source". Default is "name,property,value,source".
1440 * "all" is an alias for all five.
1441 * -s Set of sources to allow. One of
1442 * "local,default,inherited,received,temporary,none". Default is
1444 * -p Display values in parsable (literal) format.
1446 * Prints properties for the given datasets. The user can control which
1447 * columns to display as well as which property types to allow.
1451 * Invoked to display the properties for a single dataset.
1454 get_callback(zfs_handle_t *zhp, void *data)
1456 char buf[ZFS_MAXPROPLEN];
1457 char rbuf[ZFS_MAXPROPLEN];
1458 zprop_source_t sourcetype;
1459 char source[ZFS_MAXNAMELEN];
1460 zprop_get_cbdata_t *cbp = data;
1461 nvlist_t *user_props = zfs_get_user_props(zhp);
1462 zprop_list_t *pl = cbp->cb_proplist;
1466 boolean_t received = is_recvd_column(cbp);
1468 for (; pl != NULL; pl = pl->pl_next) {
1469 char *recvdval = NULL;
1471 * Skip the special fake placeholder. This will also skip over
1472 * the name property when 'all' is specified.
1474 if (pl->pl_prop == ZFS_PROP_NAME &&
1475 pl == cbp->cb_proplist)
1478 if (pl->pl_prop != ZPROP_INVAL) {
1479 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1480 sizeof (buf), &sourcetype, source,
1482 cbp->cb_literal) != 0) {
1485 if (!zfs_prop_valid_for_type(pl->pl_prop,
1486 ZFS_TYPE_DATASET)) {
1487 (void) fprintf(stderr,
1488 gettext("No such property '%s'\n"),
1489 zfs_prop_to_name(pl->pl_prop));
1492 sourcetype = ZPROP_SRC_NONE;
1493 (void) strlcpy(buf, "-", sizeof (buf));
1496 if (received && (zfs_prop_get_recvd(zhp,
1497 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1498 cbp->cb_literal) == 0))
1501 zprop_print_one_property(zfs_get_name(zhp), cbp,
1502 zfs_prop_to_name(pl->pl_prop),
1503 buf, sourcetype, source, recvdval);
1504 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1505 sourcetype = ZPROP_SRC_LOCAL;
1507 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1508 buf, sizeof (buf), cbp->cb_literal) != 0) {
1509 sourcetype = ZPROP_SRC_NONE;
1510 (void) strlcpy(buf, "-", sizeof (buf));
1513 zprop_print_one_property(zfs_get_name(zhp), cbp,
1514 pl->pl_user_prop, buf, sourcetype, source, NULL);
1515 } else if (zfs_prop_written(pl->pl_user_prop)) {
1516 sourcetype = ZPROP_SRC_LOCAL;
1518 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1519 buf, sizeof (buf), cbp->cb_literal) != 0) {
1520 sourcetype = ZPROP_SRC_NONE;
1521 (void) strlcpy(buf, "-", sizeof (buf));
1524 zprop_print_one_property(zfs_get_name(zhp), cbp,
1525 pl->pl_user_prop, buf, sourcetype, source, NULL);
1527 if (nvlist_lookup_nvlist(user_props,
1528 pl->pl_user_prop, &propval) != 0) {
1531 sourcetype = ZPROP_SRC_NONE;
1534 verify(nvlist_lookup_string(propval,
1535 ZPROP_VALUE, &strval) == 0);
1536 verify(nvlist_lookup_string(propval,
1537 ZPROP_SOURCE, &sourceval) == 0);
1539 if (strcmp(sourceval,
1540 zfs_get_name(zhp)) == 0) {
1541 sourcetype = ZPROP_SRC_LOCAL;
1542 } else if (strcmp(sourceval,
1543 ZPROP_SOURCE_VAL_RECVD) == 0) {
1544 sourcetype = ZPROP_SRC_RECEIVED;
1546 sourcetype = ZPROP_SRC_INHERITED;
1547 (void) strlcpy(source,
1548 sourceval, sizeof (source));
1552 if (received && (zfs_prop_get_recvd(zhp,
1553 pl->pl_user_prop, rbuf, sizeof (rbuf),
1554 cbp->cb_literal) == 0))
1557 zprop_print_one_property(zfs_get_name(zhp), cbp,
1558 pl->pl_user_prop, strval, sourcetype,
1567 zfs_do_get(int argc, char **argv)
1569 zprop_get_cbdata_t cb = { 0 };
1570 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1571 int types = ZFS_TYPE_DATASET;
1572 char *value, *fields;
1575 zprop_list_t fake_name = { 0 };
1578 * Set up default columns and sources.
1580 cb.cb_sources = ZPROP_SRC_ALL;
1581 cb.cb_columns[0] = GET_COL_NAME;
1582 cb.cb_columns[1] = GET_COL_PROPERTY;
1583 cb.cb_columns[2] = GET_COL_VALUE;
1584 cb.cb_columns[3] = GET_COL_SOURCE;
1585 cb.cb_type = ZFS_TYPE_DATASET;
1588 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1591 cb.cb_literal = B_TRUE;
1594 limit = parse_depth(optarg, &flags);
1597 flags |= ZFS_ITER_RECURSE;
1600 cb.cb_scripted = B_TRUE;
1603 (void) fprintf(stderr, gettext("missing argument for "
1604 "'%c' option\n"), optopt);
1609 * Process the set of columns to display. We zero out
1610 * the structure to give us a blank slate.
1612 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1614 while (*optarg != '\0') {
1615 static char *col_subopts[] =
1616 { "name", "property", "value", "received",
1617 "source", "all", NULL };
1619 if (i == ZFS_GET_NCOLS) {
1620 (void) fprintf(stderr, gettext("too "
1621 "many fields given to -o "
1626 switch (getsubopt(&optarg, col_subopts,
1629 cb.cb_columns[i++] = GET_COL_NAME;
1632 cb.cb_columns[i++] = GET_COL_PROPERTY;
1635 cb.cb_columns[i++] = GET_COL_VALUE;
1638 cb.cb_columns[i++] = GET_COL_RECVD;
1639 flags |= ZFS_ITER_RECVD_PROPS;
1642 cb.cb_columns[i++] = GET_COL_SOURCE;
1646 (void) fprintf(stderr,
1647 gettext("\"all\" conflicts "
1648 "with specific fields "
1649 "given to -o option\n"));
1652 cb.cb_columns[0] = GET_COL_NAME;
1653 cb.cb_columns[1] = GET_COL_PROPERTY;
1654 cb.cb_columns[2] = GET_COL_VALUE;
1655 cb.cb_columns[3] = GET_COL_RECVD;
1656 cb.cb_columns[4] = GET_COL_SOURCE;
1657 flags |= ZFS_ITER_RECVD_PROPS;
1661 (void) fprintf(stderr,
1662 gettext("invalid column name "
1671 while (*optarg != '\0') {
1672 static char *source_subopts[] = {
1673 "local", "default", "inherited",
1674 "received", "temporary", "none",
1677 switch (getsubopt(&optarg, source_subopts,
1680 cb.cb_sources |= ZPROP_SRC_LOCAL;
1683 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1686 cb.cb_sources |= ZPROP_SRC_INHERITED;
1689 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1692 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1695 cb.cb_sources |= ZPROP_SRC_NONE;
1698 (void) fprintf(stderr,
1699 gettext("invalid source "
1708 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1709 while (*optarg != '\0') {
1710 static char *type_subopts[] = { "filesystem",
1711 "volume", "snapshot", "bookmark",
1714 switch (getsubopt(&optarg, type_subopts,
1717 types |= ZFS_TYPE_FILESYSTEM;
1720 types |= ZFS_TYPE_VOLUME;
1723 types |= ZFS_TYPE_SNAPSHOT;
1726 types |= ZFS_TYPE_BOOKMARK;
1729 types = ZFS_TYPE_DATASET |
1734 (void) fprintf(stderr,
1735 gettext("invalid type '%s'\n"),
1743 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1753 (void) fprintf(stderr, gettext("missing property "
1760 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1768 * As part of zfs_expand_proplist(), we keep track of the maximum column
1769 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1770 * need to know the maximum name length. However, the user likely did
1771 * not specify 'name' as one of the properties to fetch, so we need to
1772 * make sure we always include at least this property for
1773 * print_get_headers() to work properly.
1775 if (cb.cb_proplist != NULL) {
1776 fake_name.pl_prop = ZFS_PROP_NAME;
1777 fake_name.pl_width = strlen(gettext("NAME"));
1778 fake_name.pl_next = cb.cb_proplist;
1779 cb.cb_proplist = &fake_name;
1782 cb.cb_first = B_TRUE;
1784 /* run for each object */
1785 ret = zfs_for_each(argc, argv, flags, types, NULL,
1786 &cb.cb_proplist, limit, get_callback, &cb);
1788 if (cb.cb_proplist == &fake_name)
1789 zprop_free_list(fake_name.pl_next);
1791 zprop_free_list(cb.cb_proplist);
1797 * inherit [-rS] <property> <fs|vol> ...
1799 * -r Recurse over all children
1800 * -S Revert to received value, if any
1802 * For each dataset specified on the command line, inherit the given property
1803 * from its parent. Inheriting a property at the pool level will cause it to
1804 * use the default value. The '-r' flag will recurse over all children, and is
1805 * useful for setting a property on a hierarchy-wide basis, regardless of any
1806 * local modifications for each dataset.
1809 typedef struct inherit_cbdata {
1810 const char *cb_propname;
1811 boolean_t cb_received;
1815 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1817 inherit_cbdata_t *cb = data;
1818 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1821 * If we're doing it recursively, then ignore properties that
1822 * are not valid for this type of dataset.
1824 if (prop != ZPROP_INVAL &&
1825 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1828 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1832 inherit_cb(zfs_handle_t *zhp, void *data)
1834 inherit_cbdata_t *cb = data;
1836 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1840 zfs_do_inherit(int argc, char **argv)
1844 inherit_cbdata_t cb = { 0 };
1848 boolean_t received = B_FALSE;
1851 while ((c = getopt(argc, argv, "rS")) != -1) {
1854 flags |= ZFS_ITER_RECURSE;
1861 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1870 /* check number of arguments */
1872 (void) fprintf(stderr, gettext("missing property argument\n"));
1876 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1884 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1885 if (zfs_prop_readonly(prop)) {
1886 (void) fprintf(stderr, gettext(
1887 "%s property is read-only\n"),
1891 if (!zfs_prop_inheritable(prop) && !received) {
1892 (void) fprintf(stderr, gettext("'%s' property cannot "
1893 "be inherited\n"), propname);
1894 if (prop == ZFS_PROP_QUOTA ||
1895 prop == ZFS_PROP_RESERVATION ||
1896 prop == ZFS_PROP_REFQUOTA ||
1897 prop == ZFS_PROP_REFRESERVATION)
1898 (void) fprintf(stderr, gettext("use 'zfs set "
1899 "%s=none' to clear\n"), propname);
1902 if (received && (prop == ZFS_PROP_VOLSIZE ||
1903 prop == ZFS_PROP_VERSION)) {
1904 (void) fprintf(stderr, gettext("'%s' property cannot "
1905 "be reverted to a received value\n"), propname);
1908 } else if (!zfs_prop_user(propname)) {
1909 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1914 cb.cb_propname = propname;
1915 cb.cb_received = received;
1917 if (flags & ZFS_ITER_RECURSE) {
1918 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1919 NULL, NULL, 0, inherit_recurse_cb, &cb);
1921 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1922 NULL, NULL, 0, inherit_cb, &cb);
1928 typedef struct upgrade_cbdata {
1929 uint64_t cb_numupgraded;
1930 uint64_t cb_numsamegraded;
1931 uint64_t cb_numfailed;
1932 uint64_t cb_version;
1934 boolean_t cb_foundone;
1935 char cb_lastfs[ZFS_MAXNAMELEN];
1939 same_pool(zfs_handle_t *zhp, const char *name)
1941 int len1 = strcspn(name, "/@");
1942 const char *zhname = zfs_get_name(zhp);
1943 int len2 = strcspn(zhname, "/@");
1947 return (strncmp(name, zhname, len1) == 0);
1951 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1953 upgrade_cbdata_t *cb = data;
1954 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1956 /* list if it's old/new */
1957 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1958 (cb->cb_newer && version > ZPL_VERSION)) {
1961 str = gettext("The following filesystems are "
1962 "formatted using a newer software version and\n"
1963 "cannot be accessed on the current system.\n\n");
1965 str = gettext("The following filesystems are "
1966 "out of date, and can be upgraded. After being\n"
1967 "upgraded, these filesystems (and any 'zfs send' "
1968 "streams generated from\n"
1969 "subsequent snapshots) will no longer be "
1970 "accessible by older software versions.\n\n");
1973 if (!cb->cb_foundone) {
1975 (void) printf(gettext("VER FILESYSTEM\n"));
1976 (void) printf(gettext("--- ------------\n"));
1977 cb->cb_foundone = B_TRUE;
1980 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1987 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1989 upgrade_cbdata_t *cb = data;
1990 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1991 int needed_spa_version;
1994 if (zfs_spa_version(zhp, &spa_version) < 0)
1997 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1999 if (needed_spa_version < 0)
2002 if (spa_version < needed_spa_version) {
2004 (void) printf(gettext("%s: can not be "
2005 "upgraded; the pool version needs to first "
2006 "be upgraded\nto version %d\n\n"),
2007 zfs_get_name(zhp), needed_spa_version);
2013 if (version < cb->cb_version) {
2015 (void) snprintf(verstr, sizeof (verstr),
2016 "%llu", cb->cb_version);
2017 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2019 * If they did "zfs upgrade -a", then we could
2020 * be doing ioctls to different pools. We need
2021 * to log this history once to each pool, and bypass
2022 * the normal history logging that happens in main().
2024 (void) zpool_log_history(g_zfs, history_str);
2025 log_history = B_FALSE;
2027 if (zfs_prop_set(zhp, "version", verstr) == 0)
2028 cb->cb_numupgraded++;
2031 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2032 } else if (version > cb->cb_version) {
2033 /* can't downgrade */
2034 (void) printf(gettext("%s: can not be downgraded; "
2035 "it is already at version %u\n"),
2036 zfs_get_name(zhp), version);
2039 cb->cb_numsamegraded++;
2047 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2050 zfs_do_upgrade(int argc, char **argv)
2052 boolean_t all = B_FALSE;
2053 boolean_t showversions = B_FALSE;
2055 upgrade_cbdata_t cb = { 0 };
2057 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2060 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2063 flags |= ZFS_ITER_RECURSE;
2066 showversions = B_TRUE;
2069 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2070 optarg, &cb.cb_version) != 0) {
2071 (void) fprintf(stderr,
2072 gettext("invalid version %s\n"), optarg);
2081 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2090 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2092 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2093 cb.cb_version || argc))
2095 if ((all || argc) && (showversions))
2101 /* Show info on available versions. */
2102 (void) printf(gettext("The following filesystem versions are "
2104 (void) printf(gettext("VER DESCRIPTION\n"));
2105 (void) printf("--- -----------------------------------------"
2106 "---------------\n");
2107 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2108 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2109 (void) printf(gettext(" 3 Case insensitive and filesystem "
2110 "user identifier (FUID)\n"));
2111 (void) printf(gettext(" 4 userquota, groupquota "
2113 (void) printf(gettext(" 5 System attributes\n"));
2114 (void) printf(gettext("\nFor more information on a particular "
2115 "version, including supported releases,\n"));
2116 (void) printf("see the ZFS Administration Guide.\n\n");
2118 } else if (argc || all) {
2119 /* Upgrade filesystems */
2120 if (cb.cb_version == 0)
2121 cb.cb_version = ZPL_VERSION;
2122 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2123 NULL, NULL, 0, upgrade_set_callback, &cb);
2124 (void) printf(gettext("%llu filesystems upgraded\n"),
2126 if (cb.cb_numsamegraded) {
2127 (void) printf(gettext("%llu filesystems already at "
2129 cb.cb_numsamegraded);
2131 if (cb.cb_numfailed != 0)
2134 /* List old-version filesytems */
2136 (void) printf(gettext("This system is currently running "
2137 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2139 flags |= ZFS_ITER_RECURSE;
2140 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2141 NULL, NULL, 0, upgrade_list_callback, &cb);
2143 found = cb.cb_foundone;
2144 cb.cb_foundone = B_FALSE;
2145 cb.cb_newer = B_TRUE;
2147 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2148 NULL, NULL, 0, upgrade_list_callback, &cb);
2150 if (!cb.cb_foundone && !found) {
2151 (void) printf(gettext("All filesystems are "
2152 "formatted with the current version.\n"));
2160 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2161 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2162 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2163 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2165 * -H Scripted mode; elide headers and separate columns by tabs.
2166 * -i Translate SID to POSIX ID.
2167 * -n Print numeric ID instead of user/group name.
2168 * -o Control which fields to display.
2169 * -p Use exact (parsable) numeric output.
2170 * -s Specify sort columns, descending order.
2171 * -S Specify sort columns, ascending order.
2172 * -t Control which object types to display.
2174 * Displays space consumed by, and quotas on, each user in the specified
2175 * filesystem or snapshot.
2178 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2179 enum us_field_types {
2185 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2186 static char *us_field_names[] = { "type", "name", "used", "quota" };
2187 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2189 #define USTYPE_PSX_GRP (1 << 0)
2190 #define USTYPE_PSX_USR (1 << 1)
2191 #define USTYPE_SMB_GRP (1 << 2)
2192 #define USTYPE_SMB_USR (1 << 3)
2193 #define USTYPE_ALL \
2194 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2196 static int us_type_bits[] = {
2203 static char *us_type_names[] = { "posixgroup", "posxiuser", "smbgroup",
2206 typedef struct us_node {
2208 uu_avl_node_t usn_avlnode;
2209 uu_list_node_t usn_listnode;
2212 typedef struct us_cbdata {
2214 uu_avl_pool_t *cb_avl_pool;
2216 boolean_t cb_numname;
2217 boolean_t cb_nicenum;
2218 boolean_t cb_sid2posix;
2219 zfs_userquota_prop_t cb_prop;
2220 zfs_sort_column_t *cb_sortcol;
2221 size_t cb_width[USFIELD_LAST];
2224 static boolean_t us_populated = B_FALSE;
2227 zfs_sort_column_t *si_sortcol;
2228 boolean_t si_numname;
2232 us_field_index(char *field)
2236 for (i = 0; i < USFIELD_LAST; i++) {
2237 if (strcmp(field, us_field_names[i]) == 0)
2245 us_compare(const void *larg, const void *rarg, void *unused)
2247 const us_node_t *l = larg;
2248 const us_node_t *r = rarg;
2249 us_sort_info_t *si = (us_sort_info_t *)unused;
2250 zfs_sort_column_t *sortcol = si->si_sortcol;
2251 boolean_t numname = si->si_numname;
2252 nvlist_t *lnvl = l->usn_nvl;
2253 nvlist_t *rnvl = r->usn_nvl;
2257 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2264 zfs_prop_t prop = sortcol->sc_prop;
2265 const char *propname = NULL;
2266 boolean_t reverse = sortcol->sc_reverse;
2271 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2272 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2274 rc = (rv32 < lv32) ? 1 : -1;
2279 (void) nvlist_lookup_uint64(lnvl, propname,
2281 (void) nvlist_lookup_uint64(rnvl, propname,
2284 rc = (rv64 < lv64) ? 1 : -1;
2286 (void) nvlist_lookup_string(lnvl, propname,
2288 (void) nvlist_lookup_string(rnvl, propname,
2290 rc = strcmp(lvstr, rvstr);
2294 case ZFS_PROP_QUOTA:
2297 if (prop == ZFS_PROP_USED)
2301 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2302 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2304 rc = (rv64 < lv64) ? 1 : -1;
2310 return (reverse ? 1 : -1);
2312 return (reverse ? -1 : 1);
2317 * If entries still seem to be the same, check if they are of the same
2318 * type (smbentity is added only if we are doing SID to POSIX ID
2319 * translation where we can have duplicate type/name combinations).
2321 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2322 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2324 return (lvb < rvb ? -1 : 1);
2329 static inline const char *
2330 us_type2str(unsigned field_type)
2332 switch (field_type) {
2333 case USTYPE_PSX_USR:
2334 return ("POSIX User");
2335 case USTYPE_PSX_GRP:
2336 return ("POSIX Group");
2337 case USTYPE_SMB_USR:
2338 return ("SMB User");
2339 case USTYPE_SMB_GRP:
2340 return ("SMB Group");
2342 return ("Undefined");
2347 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2349 us_cbdata_t *cb = (us_cbdata_t *)arg;
2350 zfs_userquota_prop_t prop = cb->cb_prop;
2355 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2356 uu_avl_t *avl = cb->cb_avl;
2360 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2362 const char *typestr;
2366 int typeidx, nameidx, sizeidx;
2367 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2368 boolean_t smbentity = B_FALSE;
2370 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2372 node = safe_malloc(sizeof (us_node_t));
2373 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2374 node->usn_nvl = props;
2376 if (domain != NULL && domain[0] != '\0') {
2378 char sid[ZFS_MAXNAMELEN + 32];
2383 directory_error_t e;
2388 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2390 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2391 type = USTYPE_SMB_GRP;
2393 err = sid_to_id(sid, B_FALSE, &id);
2396 type = USTYPE_SMB_USR;
2398 err = sid_to_id(sid, B_TRUE, &id);
2405 if (!cb->cb_sid2posix) {
2406 e = directory_name_from_sid(NULL, sid, &name,
2409 directory_error_free(e);
2417 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2419 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2420 type = USTYPE_PSX_GRP;
2421 if (!cb->cb_numname) {
2424 if ((g = getgrgid(rid)) != NULL)
2428 type = USTYPE_PSX_USR;
2429 if (!cb->cb_numname) {
2432 if ((p = getpwuid(rid)) != NULL)
2439 * Make sure that the type/name combination is unique when doing
2440 * SID to POSIX ID translation (hence changing the type from SMB to
2443 if (cb->cb_sid2posix &&
2444 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2447 /* Calculate/update width of TYPE field */
2448 typestr = us_type2str(type);
2449 typelen = strlen(gettext(typestr));
2450 typeidx = us_field_index("type");
2451 if (typelen > cb->cb_width[typeidx])
2452 cb->cb_width[typeidx] = typelen;
2453 if (nvlist_add_uint32(props, "type", type) != 0)
2456 /* Calculate/update width of NAME field */
2457 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2458 if (nvlist_add_uint64(props, "name", rid) != 0)
2460 namelen = snprintf(NULL, 0, "%u", rid);
2462 if (nvlist_add_string(props, "name", name) != 0)
2464 namelen = strlen(name);
2466 nameidx = us_field_index("name");
2467 if (namelen > cb->cb_width[nameidx])
2468 cb->cb_width[nameidx] = namelen;
2471 * Check if this type/name combination is in the list and update it;
2472 * otherwise add new node to the list.
2474 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2475 uu_avl_insert(avl, node, idx);
2480 props = node->usn_nvl;
2483 /* Calculate/update width of USED/QUOTA fields */
2485 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2487 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2488 sizelen = strlen(sizebuf);
2489 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2491 if (!nvlist_exists(props, "quota"))
2492 (void) nvlist_add_uint64(props, "quota", 0);
2495 if (!nvlist_exists(props, "used"))
2496 (void) nvlist_add_uint64(props, "used", 0);
2498 sizeidx = us_field_index(propname);
2499 if (sizelen > cb->cb_width[sizeidx])
2500 cb->cb_width[sizeidx] = sizelen;
2502 if (nvlist_add_uint64(props, propname, space) != 0)
2509 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2510 size_t *width, us_node_t *node)
2512 nvlist_t *nvl = node->usn_nvl;
2513 char valstr[ZFS_MAXNAMELEN];
2514 boolean_t first = B_TRUE;
2520 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2521 if (!(ustype & types))
2524 while ((field = fields[cfield]) != USFIELD_LAST) {
2525 nvpair_t *nvp = NULL;
2529 char *strval = NULL;
2531 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2532 if (strcmp(nvpair_name(nvp),
2533 us_field_names[field]) == 0)
2537 type = nvpair_type(nvp);
2539 case DATA_TYPE_UINT32:
2540 (void) nvpair_value_uint32(nvp, &val32);
2542 case DATA_TYPE_UINT64:
2543 (void) nvpair_value_uint64(nvp, &val64);
2545 case DATA_TYPE_STRING:
2546 (void) nvpair_value_string(nvp, &strval);
2549 (void) fprintf(stderr, "invalid data type\n");
2554 strval = (char *)us_type2str(val32);
2557 if (type == DATA_TYPE_UINT64) {
2558 (void) sprintf(valstr, "%llu", val64);
2564 if (type == DATA_TYPE_UINT64) {
2566 (void) sprintf(valstr, "%llu", val64);
2568 zfs_nicenum(val64, valstr,
2571 if (field == USFIELD_QUOTA &&
2572 strcmp(valstr, "0") == 0)
2582 (void) printf("\t");
2587 (void) printf("%s", strval);
2588 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2589 (void) printf("%-*s", width[field], strval);
2591 (void) printf("%*s", width[field], strval);
2597 (void) printf("\n");
2601 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2602 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2610 boolean_t first = B_TRUE;
2612 while ((field = fields[cfield]) != USFIELD_LAST) {
2613 col = gettext(us_field_hdr[field]);
2614 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2615 (void) printf(first ? "%-*s" : " %-*s",
2618 (void) printf(first ? "%*s" : " %*s",
2624 (void) printf("\n");
2627 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2628 print_us_node(scripted, parsable, fields, types, width, node);
2630 nvlist_free(node->usn_nvl);
2635 zfs_do_userspace(int argc, char **argv)
2638 zfs_userquota_prop_t p;
2640 uu_avl_pool_t *avl_pool;
2642 uu_avl_walk_t *walk;
2644 char deffields[] = "type,name,used,quota";
2645 char *ofield = NULL;
2646 char *tfield = NULL;
2650 boolean_t scripted = B_FALSE;
2651 boolean_t prtnum = B_FALSE;
2652 boolean_t parsable = B_FALSE;
2653 boolean_t sid2posix = B_FALSE;
2656 zfs_sort_column_t *sortcol = NULL;
2657 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2661 uu_list_pool_t *listpool;
2663 uu_avl_index_t idx = 0;
2664 uu_list_index_t idx2 = 0;
2669 if (strcmp(argv[0], "groupspace") == 0)
2670 /* Toggle default group types */
2671 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2673 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2689 if (zfs_add_sort_column(&sortcol, optarg,
2690 c == 's' ? B_FALSE : B_TRUE) != 0) {
2691 (void) fprintf(stderr,
2692 gettext("invalid field '%s'\n"), optarg);
2703 (void) fprintf(stderr, gettext("missing argument for "
2704 "'%c' option\n"), optopt);
2708 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2718 (void) fprintf(stderr, gettext("missing dataset name\n"));
2722 (void) fprintf(stderr, gettext("too many arguments\n"));
2726 /* Use default output fields if not specified using -o */
2730 if ((delim = strchr(ofield, ',')) != NULL)
2732 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2733 (void) fprintf(stderr, gettext("invalid type '%s' "
2734 "for -o option\n"), ofield);
2739 } while (delim != NULL);
2740 fields[cfield] = USFIELD_LAST;
2742 /* Override output types (-t option) */
2743 if (tfield != NULL) {
2747 boolean_t found = B_FALSE;
2749 if ((delim = strchr(tfield, ',')) != NULL)
2751 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2753 if (strcmp(tfield, us_type_names[i]) == 0) {
2755 types |= us_type_bits[i];
2760 (void) fprintf(stderr, gettext("invalid type "
2761 "'%s' for -t option\n"), tfield);
2766 } while (delim != NULL);
2769 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2772 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2773 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2775 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2778 /* Always add default sorting columns */
2779 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2780 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2782 cb.cb_sortcol = sortcol;
2783 cb.cb_numname = prtnum;
2784 cb.cb_nicenum = !parsable;
2785 cb.cb_avl_pool = avl_pool;
2786 cb.cb_avl = avl_tree;
2787 cb.cb_sid2posix = sid2posix;
2789 for (i = 0; i < USFIELD_LAST; i++)
2790 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2792 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2793 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2794 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2795 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2796 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2799 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2804 if ((node = uu_avl_first(avl_tree)) == NULL)
2807 us_populated = B_TRUE;
2809 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2810 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2811 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2812 uu_list_node_init(node, &node->usn_listnode, listpool);
2814 while (node != NULL) {
2816 node = uu_avl_next(avl_tree, node);
2817 uu_avl_remove(avl_tree, rmnode);
2818 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2819 uu_list_insert(list, rmnode, idx2);
2822 for (node = uu_list_first(list); node != NULL;
2823 node = uu_list_next(list, node)) {
2824 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2826 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2827 uu_avl_insert(avl_tree, node, idx);
2830 uu_list_destroy(list);
2831 uu_list_pool_destroy(listpool);
2833 /* Print and free node nvlist memory */
2834 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2837 zfs_free_sort_columns(sortcol);
2839 /* Clean up the AVL tree */
2840 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2843 while ((node = uu_avl_walk_next(walk)) != NULL) {
2844 uu_avl_remove(cb.cb_avl, node);
2848 uu_avl_walk_end(walk);
2849 uu_avl_destroy(avl_tree);
2850 uu_avl_pool_destroy(avl_pool);
2856 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2857 * [-t type[,...]] [filesystem|volume|snapshot] ...
2859 * -H Scripted mode; elide headers and separate columns by tabs.
2860 * -p Display values in parsable (literal) format.
2861 * -r Recurse over all children.
2862 * -d Limit recursion by depth.
2863 * -o Control which fields to display.
2864 * -s Specify sort columns, descending order.
2865 * -S Specify sort columns, ascending order.
2866 * -t Control which object types to display.
2868 * When given no arguments, list all filesystems in the system.
2869 * Otherwise, list the specified datasets, optionally recursing down them if
2870 * '-r' is specified.
2872 typedef struct list_cbdata {
2874 boolean_t cb_literal;
2875 boolean_t cb_scripted;
2876 zprop_list_t *cb_proplist;
2880 * Given a list of columns to display, output appropriate headers for each one.
2883 print_header(list_cbdata_t *cb)
2885 zprop_list_t *pl = cb->cb_proplist;
2886 char headerbuf[ZFS_MAXPROPLEN];
2889 boolean_t first = B_TRUE;
2890 boolean_t right_justify;
2892 for (; pl != NULL; pl = pl->pl_next) {
2899 right_justify = B_FALSE;
2900 if (pl->pl_prop != ZPROP_INVAL) {
2901 header = zfs_prop_column_name(pl->pl_prop);
2902 right_justify = zfs_prop_align_right(pl->pl_prop);
2904 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2905 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2906 headerbuf[i] = '\0';
2910 if (pl->pl_next == NULL && !right_justify)
2911 (void) printf("%s", header);
2912 else if (right_justify)
2913 (void) printf("%*s", pl->pl_width, header);
2915 (void) printf("%-*s", pl->pl_width, header);
2918 (void) printf("\n");
2922 * Given a dataset and a list of fields, print out all the properties according
2923 * to the described layout.
2926 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2928 zprop_list_t *pl = cb->cb_proplist;
2929 boolean_t first = B_TRUE;
2930 char property[ZFS_MAXPROPLEN];
2931 nvlist_t *userprops = zfs_get_user_props(zhp);
2934 boolean_t right_justify;
2936 for (; pl != NULL; pl = pl->pl_next) {
2938 if (cb->cb_scripted)
2939 (void) printf("\t");
2946 if (pl->pl_prop == ZFS_PROP_NAME) {
2947 (void) strlcpy(property, zfs_get_name(zhp),
2950 right_justify = zfs_prop_align_right(pl->pl_prop);
2951 } else if (pl->pl_prop != ZPROP_INVAL) {
2952 if (zfs_prop_get(zhp, pl->pl_prop, property,
2953 sizeof (property), NULL, NULL, 0,
2954 cb->cb_literal) != 0)
2958 right_justify = zfs_prop_align_right(pl->pl_prop);
2959 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2960 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2961 property, sizeof (property), cb->cb_literal) != 0)
2965 right_justify = B_TRUE;
2966 } else if (zfs_prop_written(pl->pl_user_prop)) {
2967 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2968 property, sizeof (property), cb->cb_literal) != 0)
2972 right_justify = B_TRUE;
2974 if (nvlist_lookup_nvlist(userprops,
2975 pl->pl_user_prop, &propval) != 0)
2978 verify(nvlist_lookup_string(propval,
2979 ZPROP_VALUE, &propstr) == 0);
2980 right_justify = B_FALSE;
2984 * If this is being called in scripted mode, or if this is the
2985 * last column and it is left-justified, don't include a width
2988 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
2989 (void) printf("%s", propstr);
2990 else if (right_justify)
2991 (void) printf("%*s", pl->pl_width, propstr);
2993 (void) printf("%-*s", pl->pl_width, propstr);
2996 (void) printf("\n");
3000 * Generic callback function to list a dataset or snapshot.
3003 list_callback(zfs_handle_t *zhp, void *data)
3005 list_cbdata_t *cbp = data;
3007 if (cbp->cb_first) {
3008 if (!cbp->cb_scripted)
3010 cbp->cb_first = B_FALSE;
3013 print_dataset(zhp, cbp);
3019 zfs_do_list(int argc, char **argv)
3022 static char default_fields[] =
3023 "name,used,available,referenced,mountpoint";
3024 int types = ZFS_TYPE_DATASET;
3025 boolean_t types_specified = B_FALSE;
3026 char *fields = NULL;
3027 list_cbdata_t cb = { 0 };
3031 zfs_sort_column_t *sortcol = NULL;
3032 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3035 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3041 cb.cb_literal = B_TRUE;
3042 flags |= ZFS_ITER_LITERAL_PROPS;
3045 limit = parse_depth(optarg, &flags);
3048 flags |= ZFS_ITER_RECURSE;
3051 cb.cb_scripted = B_TRUE;
3054 if (zfs_add_sort_column(&sortcol, optarg,
3056 (void) fprintf(stderr,
3057 gettext("invalid property '%s'\n"), optarg);
3062 if (zfs_add_sort_column(&sortcol, optarg,
3064 (void) fprintf(stderr,
3065 gettext("invalid property '%s'\n"), optarg);
3071 types_specified = B_TRUE;
3072 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3073 while (*optarg != '\0') {
3074 static char *type_subopts[] = { "filesystem",
3075 "volume", "snapshot", "snap", "bookmark",
3078 switch (getsubopt(&optarg, type_subopts,
3081 types |= ZFS_TYPE_FILESYSTEM;
3084 types |= ZFS_TYPE_VOLUME;
3088 types |= ZFS_TYPE_SNAPSHOT;
3091 types |= ZFS_TYPE_BOOKMARK;
3094 types = ZFS_TYPE_DATASET |
3098 (void) fprintf(stderr,
3099 gettext("invalid type '%s'\n"),
3106 (void) fprintf(stderr, gettext("missing argument for "
3107 "'%c' option\n"), optopt);
3111 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3121 fields = default_fields;
3124 * If we are only going to list snapshot names and sort by name,
3125 * then we can use faster version.
3127 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3128 flags |= ZFS_ITER_SIMPLE;
3131 * If "-o space" and no types were specified, don't display snapshots.
3133 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3134 types &= ~ZFS_TYPE_SNAPSHOT;
3137 * If the user specifies '-o all', the zprop_get_list() doesn't
3138 * normally include the name of the dataset. For 'zfs list', we always
3139 * want this property to be first.
3141 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3145 cb.cb_first = B_TRUE;
3147 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3148 limit, list_callback, &cb);
3150 zprop_free_list(cb.cb_proplist);
3151 zfs_free_sort_columns(sortcol);
3153 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3154 (void) printf(gettext("no datasets available\n"));
3160 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3161 * zfs rename [-f] -p <fs | vol> <fs | vol>
3162 * zfs rename -r <snap> <snap>
3163 * zfs rename -u [-p] <fs> <fs>
3165 * Renames the given dataset to another of the same type.
3167 * The '-p' flag creates all the non-existing ancestors of the target first.
3171 zfs_do_rename(int argc, char **argv)
3174 renameflags_t flags = { 0 };
3178 boolean_t parents = B_FALSE;
3179 char *snapshot = NULL;
3182 while ((c = getopt(argc, argv, "fpru")) != -1) {
3188 flags.recurse = B_TRUE;
3191 flags.nounmount = B_TRUE;
3194 flags.forceunmount = B_TRUE;
3198 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3207 /* check number of arguments */
3209 (void) fprintf(stderr, gettext("missing source dataset "
3214 (void) fprintf(stderr, gettext("missing target dataset "
3219 (void) fprintf(stderr, gettext("too many arguments\n"));
3223 if (flags.recurse && parents) {
3224 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3229 if (flags.recurse && strchr(argv[0], '@') == 0) {
3230 (void) fprintf(stderr, gettext("source dataset for recursive "
3231 "rename must be a snapshot\n"));
3235 if (flags.nounmount && parents) {
3236 (void) fprintf(stderr, gettext("-u and -r options are mutually "
3241 if (flags.nounmount)
3242 types = ZFS_TYPE_FILESYSTEM;
3244 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3246 types = ZFS_TYPE_DATASET;
3248 if (flags.recurse) {
3250 * When we do recursive rename we are fine when the given
3251 * snapshot for the given dataset doesn't exist - it can
3252 * still exists below.
3255 snapshot = strchr(argv[0], '@');
3256 assert(snapshot != NULL);
3261 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3264 /* If we were asked and the name looks good, try to create ancestors. */
3265 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3266 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3271 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3280 * Promotes the given clone fs to be the parent
3284 zfs_do_promote(int argc, char **argv)
3290 if (argc > 1 && argv[1][0] == '-') {
3291 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3296 /* check number of arguments */
3298 (void) fprintf(stderr, gettext("missing clone filesystem"
3303 (void) fprintf(stderr, gettext("too many arguments\n"));
3307 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3311 ret = (zfs_promote(zhp) != 0);
3319 * zfs rollback [-rRf] <snapshot>
3321 * -r Delete any intervening snapshots before doing rollback
3322 * -R Delete any snapshots and their clones
3323 * -f ignored for backwards compatability
3325 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3326 * since then and making it the active dataset. If more recent snapshots exist,
3327 * the command will complain unless the '-r' flag is given.
3329 typedef struct rollback_cbdata {
3335 boolean_t cb_recurse;
3336 } rollback_cbdata_t;
3339 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3341 rollback_cbdata_t *cbp = data;
3343 if (cbp->cb_first && cbp->cb_recurse) {
3344 (void) fprintf(stderr, gettext("cannot rollback to "
3345 "'%s': clones of previous snapshots exist\n"),
3347 (void) fprintf(stderr, gettext("use '-R' to "
3348 "force deletion of the following clones and "
3354 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3360 * Report any snapshots more recent than the one specified. Used when '-r' is
3361 * not specified. We reuse this same callback for the snapshot dependents - if
3362 * 'cb_dependent' is set, then this is a dependent and we should report it
3363 * without checking the transaction group.
3366 rollback_check(zfs_handle_t *zhp, void *data)
3368 rollback_cbdata_t *cbp = data;
3370 if (cbp->cb_doclones) {
3375 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3376 if (cbp->cb_first && !cbp->cb_recurse) {
3377 (void) fprintf(stderr, gettext("cannot "
3378 "rollback to '%s': more recent snapshots "
3379 "or bookmarks exist\n"),
3381 (void) fprintf(stderr, gettext("use '-r' to "
3382 "force deletion of the following "
3383 "snapshots and bookmarks:\n"));
3388 if (cbp->cb_recurse) {
3389 if (zfs_iter_dependents(zhp, B_TRUE,
3390 rollback_check_dependent, cbp) != 0) {
3395 (void) fprintf(stderr, "%s\n",
3404 zfs_do_rollback(int argc, char **argv)
3408 boolean_t force = B_FALSE;
3409 rollback_cbdata_t cb = { 0 };
3410 zfs_handle_t *zhp, *snap;
3411 char parentname[ZFS_MAXNAMELEN];
3415 while ((c = getopt(argc, argv, "rRf")) != -1) {
3428 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3437 /* check number of arguments */
3439 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3443 (void) fprintf(stderr, gettext("too many arguments\n"));
3447 /* open the snapshot */
3448 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3451 /* open the parent dataset */
3452 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3453 verify((delim = strrchr(parentname, '@')) != NULL);
3455 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3461 * Check for more recent snapshots and/or clones based on the presence
3464 cb.cb_target = argv[0];
3465 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3466 cb.cb_first = B_TRUE;
3468 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3470 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3473 if ((ret = cb.cb_error) != 0)
3477 * Rollback parent to the given snapshot.
3479 ret = zfs_rollback(zhp, snap, force);
3492 * zfs set property=value { fs | snap | vol } ...
3494 * Sets the given property for all datasets specified on the command line.
3496 typedef struct set_cbdata {
3502 set_callback(zfs_handle_t *zhp, void *data)
3504 set_cbdata_t *cbp = data;
3506 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3507 switch (libzfs_errno(g_zfs)) {
3508 case EZFS_MOUNTFAILED:
3509 (void) fprintf(stderr, gettext("property may be set "
3510 "but unable to remount filesystem\n"));
3512 case EZFS_SHARENFSFAILED:
3513 (void) fprintf(stderr, gettext("property may be set "
3514 "but unable to reshare filesystem\n"));
3523 zfs_do_set(int argc, char **argv)
3528 /* check for options */
3529 if (argc > 1 && argv[1][0] == '-') {
3530 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3535 /* check number of arguments */
3537 (void) fprintf(stderr, gettext("missing property=value "
3542 (void) fprintf(stderr, gettext("missing dataset name\n"));
3546 /* validate property=value argument */
3547 cb.cb_propname = argv[1];
3548 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3549 (cb.cb_value[1] == '\0')) {
3550 (void) fprintf(stderr, gettext("missing value in "
3551 "property=value argument\n"));
3555 *cb.cb_value = '\0';
3558 if (*cb.cb_propname == '\0') {
3559 (void) fprintf(stderr,
3560 gettext("missing property in property=value argument\n"));
3564 ret = zfs_for_each(argc - 2, argv + 2, 0,
3565 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3570 typedef struct snap_cbdata {
3572 boolean_t sd_recursive;
3573 const char *sd_snapname;
3577 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3579 snap_cbdata_t *sd = arg;
3584 if (sd->sd_recursive &&
3585 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3590 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3593 fnvlist_add_boolean(sd->sd_nvl, name);
3596 if (sd->sd_recursive)
3597 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3603 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3605 * Creates a snapshot with the given name. While functionally equivalent to
3606 * 'zfs create', it is a separate command to differentiate intent.
3609 zfs_do_snapshot(int argc, char **argv)
3614 snap_cbdata_t sd = { 0 };
3615 boolean_t multiple_snaps = B_FALSE;
3617 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3619 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3623 while ((c = getopt(argc, argv, "ro:")) != -1) {
3626 if (parseprop(props))
3630 sd.sd_recursive = B_TRUE;
3631 multiple_snaps = B_TRUE;
3634 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3643 /* check number of arguments */
3645 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3650 multiple_snaps = B_TRUE;
3651 for (; argc > 0; argc--, argv++) {
3655 atp = strchr(argv[0], '@');
3659 sd.sd_snapname = atp + 1;
3660 zhp = zfs_open(g_zfs, argv[0],
3661 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3664 if (zfs_snapshot_cb(zhp, &sd) != 0)
3668 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3669 nvlist_free(sd.sd_nvl);
3671 if (ret != 0 && multiple_snaps)
3672 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3676 nvlist_free(sd.sd_nvl);
3683 * Send a backup stream to stdout.
3686 zfs_do_send(int argc, char **argv)
3688 char *fromname = NULL;
3689 char *toname = NULL;
3692 sendflags_t flags = { 0 };
3694 nvlist_t *dbgnv = NULL;
3695 boolean_t extraverbose = B_FALSE;
3698 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3709 flags.doall = B_TRUE;
3712 flags.replicate = B_TRUE;
3715 flags.props = B_TRUE;
3718 flags.parsable = B_TRUE;
3719 flags.verbose = B_TRUE;
3723 extraverbose = B_TRUE;
3724 flags.verbose = B_TRUE;
3725 flags.progress = B_TRUE;
3728 flags.dedup = B_TRUE;
3731 flags.dryrun = B_TRUE;
3734 (void) fprintf(stderr, gettext("missing argument for "
3735 "'%c' option\n"), optopt);
3739 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3748 /* check number of arguments */
3750 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3754 (void) fprintf(stderr, gettext("too many arguments\n"));
3758 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3759 (void) fprintf(stderr,
3760 gettext("Error: Stream can not be written to a terminal.\n"
3761 "You must redirect standard output.\n"));
3766 * Special case sending a filesystem, or from a bookmark.
3768 if (strchr(argv[0], '@') == NULL ||
3769 (fromname && strchr(fromname, '#') != NULL)) {
3770 char frombuf[ZFS_MAXNAMELEN];
3772 if (flags.replicate || flags.doall || flags.props ||
3773 flags.dedup || flags.dryrun || flags.verbose ||
3775 (void) fprintf(stderr,
3777 "Unsupported flag with filesystem or bookmark.\n"));
3781 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3785 if (fromname != NULL &&
3786 (fromname[0] == '#' || fromname[0] == '@')) {
3788 * Incremental source name begins with # or @.
3789 * Default to same fs as target.
3791 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3792 cp = strchr(frombuf, '@');
3795 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3798 err = zfs_send_one(zhp, fromname, STDOUT_FILENO);
3803 cp = strchr(argv[0], '@');
3806 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3811 * If they specified the full path to the snapshot, chop off
3812 * everything except the short name of the snapshot, but special
3813 * case if they specify the origin.
3815 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3816 char origin[ZFS_MAXNAMELEN];
3819 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3820 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3822 if (strcmp(origin, fromname) == 0) {
3824 flags.fromorigin = B_TRUE;
3827 if (cp != fromname && strcmp(argv[0], fromname)) {
3828 (void) fprintf(stderr,
3829 gettext("incremental source must be "
3830 "in same filesystem\n"));
3834 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3835 (void) fprintf(stderr,
3836 gettext("invalid incremental source\n"));
3842 if (flags.replicate && fromname == NULL)
3843 flags.doall = B_TRUE;
3845 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3846 extraverbose ? &dbgnv : NULL);
3848 if (extraverbose && dbgnv != NULL) {
3850 * dump_nvlist prints to stdout, but that's been
3851 * redirected to a file. Make it print to stderr
3854 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3855 dump_nvlist(dbgnv, 0);
3864 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3866 * Restore a backup stream from stdin.
3869 zfs_do_receive(int argc, char **argv)
3872 recvflags_t flags = { 0 };
3875 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3878 flags.isprefix = B_TRUE;
3881 flags.isprefix = B_TRUE;
3882 flags.istail = B_TRUE;
3885 flags.dryrun = B_TRUE;
3888 flags.nomount = B_TRUE;
3891 flags.verbose = B_TRUE;
3894 flags.force = B_TRUE;
3897 (void) fprintf(stderr, gettext("missing argument for "
3898 "'%c' option\n"), optopt);
3902 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3911 /* check number of arguments */
3913 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3917 (void) fprintf(stderr, gettext("too many arguments\n"));
3921 if (isatty(STDIN_FILENO)) {
3922 (void) fprintf(stderr,
3923 gettext("Error: Backup stream can not be read "
3924 "from a terminal.\n"
3925 "You must redirect standard input.\n"));
3929 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3935 * allow/unallow stuff
3937 /* copied from zfs/sys/dsl_deleg.h */
3938 #define ZFS_DELEG_PERM_CREATE "create"
3939 #define ZFS_DELEG_PERM_DESTROY "destroy"
3940 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3941 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3942 #define ZFS_DELEG_PERM_CLONE "clone"
3943 #define ZFS_DELEG_PERM_PROMOTE "promote"
3944 #define ZFS_DELEG_PERM_RENAME "rename"
3945 #define ZFS_DELEG_PERM_MOUNT "mount"
3946 #define ZFS_DELEG_PERM_SHARE "share"
3947 #define ZFS_DELEG_PERM_SEND "send"
3948 #define ZFS_DELEG_PERM_RECEIVE "receive"
3949 #define ZFS_DELEG_PERM_ALLOW "allow"
3950 #define ZFS_DELEG_PERM_USERPROP "userprop"
3951 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3952 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3953 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3954 #define ZFS_DELEG_PERM_USERUSED "userused"
3955 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3956 #define ZFS_DELEG_PERM_HOLD "hold"
3957 #define ZFS_DELEG_PERM_RELEASE "release"
3958 #define ZFS_DELEG_PERM_DIFF "diff"
3959 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
3961 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3963 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3964 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3965 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3966 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3967 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3968 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3969 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3970 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3971 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3972 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3973 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3974 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3975 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3976 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3977 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3978 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3979 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
3981 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3982 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3983 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3984 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3985 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3986 { NULL, ZFS_DELEG_NOTE_NONE }
3989 /* permission structure */
3990 typedef struct deleg_perm {
3991 zfs_deleg_who_type_t dp_who_type;
3992 const char *dp_name;
3994 boolean_t dp_descend;
3998 typedef struct deleg_perm_node {
3999 deleg_perm_t dpn_perm;
4001 uu_avl_node_t dpn_avl_node;
4002 } deleg_perm_node_t;
4004 typedef struct fs_perm fs_perm_t;
4006 /* permissions set */
4007 typedef struct who_perm {
4008 zfs_deleg_who_type_t who_type;
4009 const char *who_name; /* id */
4010 char who_ug_name[256]; /* user/group name */
4011 fs_perm_t *who_fsperm; /* uplink */
4013 uu_avl_t *who_deleg_perm_avl; /* permissions */
4017 typedef struct who_perm_node {
4018 who_perm_t who_perm;
4019 uu_avl_node_t who_avl_node;
4022 typedef struct fs_perm_set fs_perm_set_t;
4023 /* fs permissions */
4025 const char *fsp_name;
4027 uu_avl_t *fsp_sc_avl; /* sets,create */
4028 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4030 fs_perm_set_t *fsp_set; /* uplink */
4034 typedef struct fs_perm_node {
4035 fs_perm_t fspn_fsperm;
4038 uu_list_node_t fspn_list_node;
4041 /* top level structure */
4042 struct fs_perm_set {
4043 uu_list_pool_t *fsps_list_pool;
4044 uu_list_t *fsps_list; /* list of fs_perms */
4046 uu_avl_pool_t *fsps_named_set_avl_pool;
4047 uu_avl_pool_t *fsps_who_perm_avl_pool;
4048 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4051 static inline const char *
4052 deleg_perm_type(zfs_deleg_note_t note)
4058 case ZFS_DELEG_NOTE_GROUPQUOTA:
4059 case ZFS_DELEG_NOTE_GROUPUSED:
4060 case ZFS_DELEG_NOTE_USERPROP:
4061 case ZFS_DELEG_NOTE_USERQUOTA:
4062 case ZFS_DELEG_NOTE_USERUSED:
4064 return (gettext("other"));
4066 return (gettext("subcommand"));
4071 who_type2weight(zfs_deleg_who_type_t who_type)
4075 case ZFS_DELEG_NAMED_SET_SETS:
4076 case ZFS_DELEG_NAMED_SET:
4079 case ZFS_DELEG_CREATE_SETS:
4080 case ZFS_DELEG_CREATE:
4083 case ZFS_DELEG_USER_SETS:
4084 case ZFS_DELEG_USER:
4087 case ZFS_DELEG_GROUP_SETS:
4088 case ZFS_DELEG_GROUP:
4091 case ZFS_DELEG_EVERYONE_SETS:
4092 case ZFS_DELEG_EVERYONE:
4104 who_perm_compare(const void *larg, const void *rarg, void *unused)
4106 const who_perm_node_t *l = larg;
4107 const who_perm_node_t *r = rarg;
4108 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4109 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4110 int lweight = who_type2weight(ltype);
4111 int rweight = who_type2weight(rtype);
4112 int res = lweight - rweight;
4114 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4115 ZFS_MAX_DELEG_NAME-1);
4127 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4129 const deleg_perm_node_t *l = larg;
4130 const deleg_perm_node_t *r = rarg;
4131 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4132 ZFS_MAX_DELEG_NAME-1);
4144 fs_perm_set_init(fs_perm_set_t *fspset)
4146 bzero(fspset, sizeof (fs_perm_set_t));
4148 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4149 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4150 NULL, UU_DEFAULT)) == NULL)
4152 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4153 UU_DEFAULT)) == NULL)
4156 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4157 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4158 who_perm_node_t, who_avl_node), who_perm_compare,
4159 UU_DEFAULT)) == NULL)
4162 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4163 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4164 who_perm_node_t, who_avl_node), who_perm_compare,
4165 UU_DEFAULT)) == NULL)
4168 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4169 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4170 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4175 static inline void fs_perm_fini(fs_perm_t *);
4176 static inline void who_perm_fini(who_perm_t *);
4179 fs_perm_set_fini(fs_perm_set_t *fspset)
4181 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4183 while (node != NULL) {
4184 fs_perm_node_t *next_node =
4185 uu_list_next(fspset->fsps_list, node);
4186 fs_perm_t *fsperm = &node->fspn_fsperm;
4187 fs_perm_fini(fsperm);
4188 uu_list_remove(fspset->fsps_list, node);
4193 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4194 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4195 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4199 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4202 deleg_perm->dp_who_type = type;
4203 deleg_perm->dp_name = name;
4207 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4208 zfs_deleg_who_type_t type, const char *name)
4210 uu_avl_pool_t *pool;
4211 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4213 bzero(who_perm, sizeof (who_perm_t));
4215 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4216 UU_DEFAULT)) == NULL)
4219 who_perm->who_type = type;
4220 who_perm->who_name = name;
4221 who_perm->who_fsperm = fsperm;
4225 who_perm_fini(who_perm_t *who_perm)
4227 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4229 while (node != NULL) {
4230 deleg_perm_node_t *next_node =
4231 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4233 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4238 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4242 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4244 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4245 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4247 bzero(fsperm, sizeof (fs_perm_t));
4249 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4253 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4257 fsperm->fsp_set = fspset;
4258 fsperm->fsp_name = fsname;
4262 fs_perm_fini(fs_perm_t *fsperm)
4264 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4265 while (node != NULL) {
4266 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4268 who_perm_t *who_perm = &node->who_perm;
4269 who_perm_fini(who_perm);
4270 uu_avl_remove(fsperm->fsp_sc_avl, node);
4275 node = uu_avl_first(fsperm->fsp_uge_avl);
4276 while (node != NULL) {
4277 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4279 who_perm_t *who_perm = &node->who_perm;
4280 who_perm_fini(who_perm);
4281 uu_avl_remove(fsperm->fsp_uge_avl, node);
4286 uu_avl_destroy(fsperm->fsp_sc_avl);
4287 uu_avl_destroy(fsperm->fsp_uge_avl);
4291 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4292 zfs_deleg_who_type_t who_type, const char *name, char locality)
4294 uu_avl_index_t idx = 0;
4296 deleg_perm_node_t *found_node = NULL;
4297 deleg_perm_t *deleg_perm = &node->dpn_perm;
4299 deleg_perm_init(deleg_perm, who_type, name);
4301 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4303 uu_avl_insert(avl, node, idx);
4306 deleg_perm = &node->dpn_perm;
4311 case ZFS_DELEG_LOCAL:
4312 deleg_perm->dp_local = B_TRUE;
4314 case ZFS_DELEG_DESCENDENT:
4315 deleg_perm->dp_descend = B_TRUE;
4320 assert(B_FALSE); /* invalid locality */
4325 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4327 nvpair_t *nvp = NULL;
4328 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4329 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4330 zfs_deleg_who_type_t who_type = who_perm->who_type;
4332 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4333 const char *name = nvpair_name(nvp);
4334 data_type_t type = nvpair_type(nvp);
4335 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4336 deleg_perm_node_t *node =
4337 safe_malloc(sizeof (deleg_perm_node_t));
4339 assert(type == DATA_TYPE_BOOLEAN);
4341 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4342 set_deleg_perm_node(avl, node, who_type, name, locality);
4349 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4351 nvpair_t *nvp = NULL;
4352 fs_perm_set_t *fspset = fsperm->fsp_set;
4354 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4355 nvlist_t *nvl2 = NULL;
4356 const char *name = nvpair_name(nvp);
4357 uu_avl_t *avl = NULL;
4358 uu_avl_pool_t *avl_pool;
4359 zfs_deleg_who_type_t perm_type = name[0];
4360 char perm_locality = name[1];
4361 const char *perm_name = name + 3;
4362 boolean_t is_set = B_TRUE;
4363 who_perm_t *who_perm = NULL;
4365 assert('$' == name[2]);
4367 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4370 switch (perm_type) {
4371 case ZFS_DELEG_CREATE:
4372 case ZFS_DELEG_CREATE_SETS:
4373 case ZFS_DELEG_NAMED_SET:
4374 case ZFS_DELEG_NAMED_SET_SETS:
4375 avl_pool = fspset->fsps_named_set_avl_pool;
4376 avl = fsperm->fsp_sc_avl;
4378 case ZFS_DELEG_USER:
4379 case ZFS_DELEG_USER_SETS:
4380 case ZFS_DELEG_GROUP:
4381 case ZFS_DELEG_GROUP_SETS:
4382 case ZFS_DELEG_EVERYONE:
4383 case ZFS_DELEG_EVERYONE_SETS:
4384 avl_pool = fspset->fsps_who_perm_avl_pool;
4385 avl = fsperm->fsp_uge_avl;
4390 who_perm_node_t *found_node = NULL;
4391 who_perm_node_t *node = safe_malloc(
4392 sizeof (who_perm_node_t));
4393 who_perm = &node->who_perm;
4394 uu_avl_index_t idx = 0;
4396 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4397 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4399 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4401 if (avl == fsperm->fsp_uge_avl) {
4403 struct passwd *p = NULL;
4404 struct group *g = NULL;
4405 const char *nice_name = NULL;
4407 switch (perm_type) {
4408 case ZFS_DELEG_USER_SETS:
4409 case ZFS_DELEG_USER:
4410 rid = atoi(perm_name);
4413 nice_name = p->pw_name;
4415 case ZFS_DELEG_GROUP_SETS:
4416 case ZFS_DELEG_GROUP:
4417 rid = atoi(perm_name);
4420 nice_name = g->gr_name;
4424 if (nice_name != NULL)
4426 node->who_perm.who_ug_name,
4430 uu_avl_insert(avl, node, idx);
4433 who_perm = &node->who_perm;
4437 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4444 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4446 nvpair_t *nvp = NULL;
4447 uu_avl_index_t idx = 0;
4449 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4450 nvlist_t *nvl2 = NULL;
4451 const char *fsname = nvpair_name(nvp);
4452 data_type_t type = nvpair_type(nvp);
4453 fs_perm_t *fsperm = NULL;
4454 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4458 fsperm = &node->fspn_fsperm;
4460 assert(DATA_TYPE_NVLIST == type);
4462 uu_list_node_init(node, &node->fspn_list_node,
4463 fspset->fsps_list_pool);
4465 idx = uu_list_numnodes(fspset->fsps_list);
4466 fs_perm_init(fsperm, fspset, fsname);
4468 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4471 (void) parse_fs_perm(fsperm, nvl2);
4473 uu_list_insert(fspset->fsps_list, node, idx);
4479 static inline const char *
4480 deleg_perm_comment(zfs_deleg_note_t note)
4482 const char *str = "";
4487 case ZFS_DELEG_NOTE_ALLOW:
4488 str = gettext("Must also have the permission that is being"
4489 "\n\t\t\t\tallowed");
4491 case ZFS_DELEG_NOTE_CLONE:
4492 str = gettext("Must also have the 'create' ability and 'mount'"
4493 "\n\t\t\t\tability in the origin file system");
4495 case ZFS_DELEG_NOTE_CREATE:
4496 str = gettext("Must also have the 'mount' ability");
4498 case ZFS_DELEG_NOTE_DESTROY:
4499 str = gettext("Must also have the 'mount' ability");
4501 case ZFS_DELEG_NOTE_DIFF:
4502 str = gettext("Allows lookup of paths within a dataset;"
4503 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4504 "\n\t\t\t\tin order to use zfs diff");
4506 case ZFS_DELEG_NOTE_HOLD:
4507 str = gettext("Allows adding a user hold to a snapshot");
4509 case ZFS_DELEG_NOTE_MOUNT:
4510 str = gettext("Allows mount/umount of ZFS datasets");
4512 case ZFS_DELEG_NOTE_PROMOTE:
4513 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4514 " 'promote' ability in the origin file system");
4516 case ZFS_DELEG_NOTE_RECEIVE:
4517 str = gettext("Must also have the 'mount' and 'create'"
4520 case ZFS_DELEG_NOTE_RELEASE:
4521 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4522 "might destroy the snapshot");
4524 case ZFS_DELEG_NOTE_RENAME:
4525 str = gettext("Must also have the 'mount' and 'create'"
4526 "\n\t\t\t\tability in the new parent");
4528 case ZFS_DELEG_NOTE_ROLLBACK:
4531 case ZFS_DELEG_NOTE_SEND:
4534 case ZFS_DELEG_NOTE_SHARE:
4535 str = gettext("Allows sharing file systems over NFS or SMB"
4536 "\n\t\t\t\tprotocols");
4538 case ZFS_DELEG_NOTE_SNAPSHOT:
4542 * case ZFS_DELEG_NOTE_VSCAN:
4543 * str = gettext("");
4547 case ZFS_DELEG_NOTE_GROUPQUOTA:
4548 str = gettext("Allows accessing any groupquota@... property");
4550 case ZFS_DELEG_NOTE_GROUPUSED:
4551 str = gettext("Allows reading any groupused@... property");
4553 case ZFS_DELEG_NOTE_USERPROP:
4554 str = gettext("Allows changing any user property");
4556 case ZFS_DELEG_NOTE_USERQUOTA:
4557 str = gettext("Allows accessing any userquota@... property");
4559 case ZFS_DELEG_NOTE_USERUSED:
4560 str = gettext("Allows reading any userused@... property");
4578 boolean_t recursive; /* unallow only */
4579 boolean_t prt_usage;
4581 boolean_t prt_perms;
4584 const char *dataset;
4588 prop_cmp(const void *a, const void *b)
4590 const char *str1 = *(const char **)a;
4591 const char *str2 = *(const char **)b;
4592 return (strcmp(str1, str2));
4596 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4598 const char *opt_desc[] = {
4599 "-h", gettext("show this help message and exit"),
4600 "-l", gettext("set permission locally"),
4601 "-d", gettext("set permission for descents"),
4602 "-u", gettext("set permission for user"),
4603 "-g", gettext("set permission for group"),
4604 "-e", gettext("set permission for everyone"),
4605 "-c", gettext("set create time permission"),
4606 "-s", gettext("define permission set"),
4608 "-r", gettext("remove permissions recursively"),
4610 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4611 size_t allow_size = unallow_size - 2;
4612 const char *props[ZFS_NUM_PROPS];
4615 FILE *fp = requested ? stdout : stderr;
4616 zprop_desc_t *pdtbl = zfs_prop_get_table();
4617 const char *fmt = gettext("%-16s %-14s\t%s\n");
4619 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4621 (void) fprintf(fp, gettext("Options:\n"));
4622 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4623 const char *opt = opt_desc[i++];
4624 const char *optdsc = opt_desc[i];
4625 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4628 (void) fprintf(fp, gettext("\nThe following permissions are "
4630 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4632 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4633 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4634 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4635 const char *perm_type = deleg_perm_type(perm_note);
4636 const char *perm_comment = deleg_perm_comment(perm_note);
4637 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4640 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4641 zprop_desc_t *pd = &pdtbl[i];
4642 if (pd->pd_visible != B_TRUE)
4645 if (pd->pd_attr == PROP_READONLY)
4648 props[count++] = pd->pd_name;
4650 props[count] = NULL;
4652 qsort(props, count, sizeof (char *), prop_cmp);
4654 for (i = 0; i < count; i++)
4655 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4658 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4660 exit(requested ? 0 : 2);
4663 static inline const char *
4664 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4667 if (un && argc == expected_argc - 1)
4669 else if (argc == expected_argc)
4670 *permsp = argv[argc - 2];
4672 allow_usage(un, B_FALSE,
4673 gettext("wrong number of parameters\n"));
4675 return (argv[argc - 1]);
4679 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4681 int uge_sum = opts->user + opts->group + opts->everyone;
4682 int csuge_sum = opts->create + opts->set + uge_sum;
4683 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4684 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4687 allow_usage(un, B_FALSE,
4688 gettext("-u, -g, and -e are mutually exclusive\n"));
4690 if (opts->prt_usage)
4691 if (argc == 0 && all_sum == 0)
4692 allow_usage(un, B_TRUE, NULL);
4698 allow_usage(un, B_FALSE,
4699 gettext("invalid options combined with -s\n"));
4701 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4702 if (argv[0][0] != '@')
4703 allow_usage(un, B_FALSE,
4704 gettext("invalid set name: missing '@' prefix\n"));
4705 opts->who = argv[0];
4706 } else if (opts->create) {
4707 if (ldcsuge_sum > 1)
4708 allow_usage(un, B_FALSE,
4709 gettext("invalid options combined with -c\n"));
4710 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4711 } else if (opts->everyone) {
4713 allow_usage(un, B_FALSE,
4714 gettext("invalid options combined with -e\n"));
4715 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4716 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4718 opts->everyone = B_TRUE;
4721 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4722 } else if (argc == 1 && !un) {
4723 opts->prt_perms = B_TRUE;
4724 opts->dataset = argv[argc-1];
4726 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4727 opts->who = argv[0];
4730 if (!opts->local && !opts->descend) {
4731 opts->local = B_TRUE;
4732 opts->descend = B_TRUE;
4737 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4738 const char *who, char *perms, nvlist_t *top_nvl)
4741 char ld[2] = { '\0', '\0' };
4742 char who_buf[ZFS_MAXNAMELEN+32];
4745 nvlist_t *base_nvl = NULL;
4746 nvlist_t *set_nvl = NULL;
4749 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4751 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4755 case ZFS_DELEG_NAMED_SET_SETS:
4756 case ZFS_DELEG_NAMED_SET:
4757 set_type = ZFS_DELEG_NAMED_SET_SETS;
4758 base_type = ZFS_DELEG_NAMED_SET;
4759 ld[0] = ZFS_DELEG_NA;
4761 case ZFS_DELEG_CREATE_SETS:
4762 case ZFS_DELEG_CREATE:
4763 set_type = ZFS_DELEG_CREATE_SETS;
4764 base_type = ZFS_DELEG_CREATE;
4765 ld[0] = ZFS_DELEG_NA;
4767 case ZFS_DELEG_USER_SETS:
4768 case ZFS_DELEG_USER:
4769 set_type = ZFS_DELEG_USER_SETS;
4770 base_type = ZFS_DELEG_USER;
4772 ld[0] = ZFS_DELEG_LOCAL;
4774 ld[1] = ZFS_DELEG_DESCENDENT;
4776 case ZFS_DELEG_GROUP_SETS:
4777 case ZFS_DELEG_GROUP:
4778 set_type = ZFS_DELEG_GROUP_SETS;
4779 base_type = ZFS_DELEG_GROUP;
4781 ld[0] = ZFS_DELEG_LOCAL;
4783 ld[1] = ZFS_DELEG_DESCENDENT;
4785 case ZFS_DELEG_EVERYONE_SETS:
4786 case ZFS_DELEG_EVERYONE:
4787 set_type = ZFS_DELEG_EVERYONE_SETS;
4788 base_type = ZFS_DELEG_EVERYONE;
4790 ld[0] = ZFS_DELEG_LOCAL;
4792 ld[1] = ZFS_DELEG_DESCENDENT;
4795 if (perms != NULL) {
4797 char *end = curr + strlen(perms);
4799 while (curr < end) {
4800 char *delim = strchr(curr, ',');
4811 (void) nvlist_add_boolean(nvl, curr);
4817 for (i = 0; i < 2; i++) {
4818 char locality = ld[i];
4822 if (!nvlist_empty(base_nvl)) {
4824 (void) snprintf(who_buf,
4825 sizeof (who_buf), "%c%c$%s",
4826 base_type, locality, who);
4828 (void) snprintf(who_buf,
4829 sizeof (who_buf), "%c%c$",
4830 base_type, locality);
4832 (void) nvlist_add_nvlist(top_nvl, who_buf,
4837 if (!nvlist_empty(set_nvl)) {
4839 (void) snprintf(who_buf,
4840 sizeof (who_buf), "%c%c$%s",
4841 set_type, locality, who);
4843 (void) snprintf(who_buf,
4844 sizeof (who_buf), "%c%c$",
4845 set_type, locality);
4847 (void) nvlist_add_nvlist(top_nvl, who_buf,
4852 for (i = 0; i < 2; i++) {
4853 char locality = ld[i];
4858 (void) snprintf(who_buf, sizeof (who_buf),
4859 "%c%c$%s", base_type, locality, who);
4861 (void) snprintf(who_buf, sizeof (who_buf),
4862 "%c%c$", base_type, locality);
4863 (void) nvlist_add_boolean(top_nvl, who_buf);
4866 (void) snprintf(who_buf, sizeof (who_buf),
4867 "%c%c$%s", set_type, locality, who);
4869 (void) snprintf(who_buf, sizeof (who_buf),
4870 "%c%c$", set_type, locality);
4871 (void) nvlist_add_boolean(top_nvl, who_buf);
4877 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4879 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4883 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4884 opts->descend, opts->who, opts->perms, *nvlp);
4885 } else if (opts->create) {
4886 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4887 opts->descend, NULL, opts->perms, *nvlp);
4888 } else if (opts->everyone) {
4889 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4890 opts->descend, NULL, opts->perms, *nvlp);
4892 char *curr = opts->who;
4893 char *end = curr + strlen(curr);
4895 while (curr < end) {
4897 zfs_deleg_who_type_t who_type;
4899 char *delim = strchr(curr, ',');
4902 struct passwd *p = NULL;
4903 struct group *g = NULL;
4911 rid = (uid_t)strtol(curr, &endch, 0);
4913 who_type = ZFS_DELEG_USER;
4922 (void) snprintf(errbuf, 256, gettext(
4923 "invalid user %s"), curr);
4924 allow_usage(un, B_TRUE, errbuf);
4926 } else if (opts->group) {
4927 who_type = ZFS_DELEG_GROUP;
4936 (void) snprintf(errbuf, 256, gettext(
4937 "invalid group %s"), curr);
4938 allow_usage(un, B_TRUE, errbuf);
4941 if (*endch != '\0') {
4948 if (*endch != '\0') {
4955 who_type = ZFS_DELEG_USER;
4957 } else if (g != NULL) {
4958 who_type = ZFS_DELEG_GROUP;
4961 (void) snprintf(errbuf, 256, gettext(
4962 "invalid user/group %s"), curr);
4963 allow_usage(un, B_TRUE, errbuf);
4967 (void) sprintf(id, "%u", rid);
4970 store_allow_perm(who_type, opts->local,
4971 opts->descend, who, opts->perms, *nvlp);
4980 print_set_creat_perms(uu_avl_t *who_avl)
4982 const char *sc_title[] = {
4983 gettext("Permission sets:\n"),
4984 gettext("Create time permissions:\n"),
4987 const char **title_ptr = sc_title;
4988 who_perm_node_t *who_node = NULL;
4989 int prev_weight = -1;
4991 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4992 who_node = uu_avl_next(who_avl, who_node)) {
4993 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4994 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4995 const char *who_name = who_node->who_perm.who_name;
4996 int weight = who_type2weight(who_type);
4997 boolean_t first = B_TRUE;
4998 deleg_perm_node_t *deleg_node;
5000 if (prev_weight != weight) {
5001 (void) printf(*title_ptr++);
5002 prev_weight = weight;
5005 if (who_name == NULL || strnlen(who_name, 1) == 0)
5006 (void) printf("\t");
5008 (void) printf("\t%s ", who_name);
5010 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5011 deleg_node = uu_avl_next(avl, deleg_node)) {
5014 deleg_node->dpn_perm.dp_name);
5017 (void) printf(",%s",
5018 deleg_node->dpn_perm.dp_name);
5021 (void) printf("\n");
5026 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5029 who_perm_node_t *who_node = NULL;
5030 boolean_t prt_title = B_TRUE;
5031 uu_avl_walk_t *walk;
5033 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5036 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5037 const char *who_name = who_node->who_perm.who_name;
5038 const char *nice_who_name = who_node->who_perm.who_ug_name;
5039 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5040 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5042 deleg_perm_node_t *deleg_node;
5043 boolean_t prt_who = B_TRUE;
5045 for (deleg_node = uu_avl_first(avl);
5047 deleg_node = uu_avl_next(avl, deleg_node)) {
5048 if (local != deleg_node->dpn_perm.dp_local ||
5049 descend != deleg_node->dpn_perm.dp_descend)
5053 const char *who = NULL;
5055 prt_title = B_FALSE;
5056 (void) printf(title);
5060 case ZFS_DELEG_USER_SETS:
5061 case ZFS_DELEG_USER:
5062 who = gettext("user");
5064 who_name = nice_who_name;
5066 case ZFS_DELEG_GROUP_SETS:
5067 case ZFS_DELEG_GROUP:
5068 who = gettext("group");
5070 who_name = nice_who_name;
5072 case ZFS_DELEG_EVERYONE_SETS:
5073 case ZFS_DELEG_EVERYONE:
5074 who = gettext("everyone");
5079 if (who_name == NULL)
5080 (void) printf("\t%s", who);
5082 (void) printf("\t%s %s", who, who_name);
5085 (void) printf("%c%s", delim,
5086 deleg_node->dpn_perm.dp_name);
5091 (void) printf("\n");
5094 uu_avl_walk_end(walk);
5098 print_fs_perms(fs_perm_set_t *fspset)
5100 fs_perm_node_t *node = NULL;
5101 char buf[ZFS_MAXNAMELEN+32];
5102 const char *dsname = buf;
5104 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5105 node = uu_list_next(fspset->fsps_list, node)) {
5106 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5107 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5110 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5111 gettext("---- Permissions on %s "),
5112 node->fspn_fsperm.fsp_name);
5113 (void) printf(dsname);
5114 left = 70 - strlen(buf);
5117 (void) printf("\n");
5119 print_set_creat_perms(sc_avl);
5120 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5121 gettext("Local permissions:\n"));
5122 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5123 gettext("Descendent permissions:\n"));
5124 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5125 gettext("Local+Descendent permissions:\n"));
5129 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5131 struct deleg_perms {
5137 set_deleg_perms(zfs_handle_t *zhp, void *data)
5139 struct deleg_perms *perms = (struct deleg_perms *)data;
5140 zfs_type_t zfs_type = zfs_get_type(zhp);
5142 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5145 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5149 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5152 nvlist_t *perm_nvl = NULL;
5153 nvlist_t *update_perm_nvl = NULL;
5156 struct allow_opts opts = { 0 };
5158 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5161 while ((c = getopt(argc, argv, optstr)) != -1) {
5164 opts.local = B_TRUE;
5167 opts.descend = B_TRUE;
5173 opts.group = B_TRUE;
5176 opts.everyone = B_TRUE;
5182 opts.create = B_TRUE;
5185 opts.recursive = B_TRUE;
5188 (void) fprintf(stderr, gettext("missing argument for "
5189 "'%c' option\n"), optopt);
5193 opts.prt_usage = B_TRUE;
5196 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5205 /* check arguments */
5206 parse_allow_args(argc, argv, un, &opts);
5208 /* try to open the dataset */
5209 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5210 ZFS_TYPE_VOLUME)) == NULL) {
5211 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5216 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5219 fs_perm_set_init(&fs_perm_set);
5220 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5221 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5226 print_fs_perms(&fs_perm_set);
5228 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5229 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5232 if (un && opts.recursive) {
5233 struct deleg_perms data = { un, update_perm_nvl };
5234 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5243 nvlist_free(perm_nvl);
5244 if (update_perm_nvl != NULL)
5245 nvlist_free(update_perm_nvl);
5247 fs_perm_set_fini(&fs_perm_set);
5255 zfs_do_allow(int argc, char **argv)
5257 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5261 zfs_do_unallow(int argc, char **argv)
5263 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5267 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5272 boolean_t recursive = B_FALSE;
5273 const char *opts = holding ? "rt" : "r";
5277 while ((c = getopt(argc, argv, opts)) != -1) {
5283 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5292 /* check number of arguments */
5300 if (holding && tag[0] == '.') {
5301 /* tags starting with '.' are reserved for libzfs */
5302 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5306 for (i = 0; i < argc; ++i) {
5308 char parent[ZFS_MAXNAMELEN];
5310 char *path = argv[i];
5312 delim = strchr(path, '@');
5313 if (delim == NULL) {
5314 (void) fprintf(stderr,
5315 gettext("'%s' is not a snapshot\n"), path);
5319 (void) strncpy(parent, path, delim - path);
5320 parent[delim - path] = '\0';
5322 zhp = zfs_open(g_zfs, parent,
5323 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5329 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5332 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5338 return (errors != 0);
5342 * zfs hold [-r] [-t] <tag> <snap> ...
5344 * -r Recursively hold
5346 * Apply a user-hold with the given tag to the list of snapshots.
5349 zfs_do_hold(int argc, char **argv)
5351 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5355 * zfs release [-r] <tag> <snap> ...
5357 * -r Recursively release
5359 * Release a user-hold with the given tag from the list of snapshots.
5362 zfs_do_release(int argc, char **argv)
5364 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5367 typedef struct holds_cbdata {
5368 boolean_t cb_recursive;
5369 const char *cb_snapname;
5371 size_t cb_max_namelen;
5372 size_t cb_max_taglen;
5375 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5376 #define DATETIME_BUF_LEN (32)
5381 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5384 nvpair_t *nvp = NULL;
5385 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5389 for (i = 0; i < 3; i++) {
5390 col = gettext(hdr_cols[i]);
5392 (void) printf("%-*s ", i ? tagwidth : nwidth,
5395 (void) printf("%s\n", col);
5399 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5400 char *zname = nvpair_name(nvp);
5402 nvpair_t *nvp2 = NULL;
5403 (void) nvpair_value_nvlist(nvp, &nvl2);
5404 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5405 char tsbuf[DATETIME_BUF_LEN];
5406 char *tagname = nvpair_name(nvp2);
5410 char sep = scripted ? '\t' : ' ';
5411 size_t sepnum = scripted ? 1 : 2;
5413 (void) nvpair_value_uint64(nvp2, &val);
5415 (void) localtime_r(&time, &t);
5416 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5417 gettext(STRFTIME_FMT_STR), &t);
5419 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5420 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5426 * Generic callback function to list a dataset or snapshot.
5429 holds_callback(zfs_handle_t *zhp, void *data)
5431 holds_cbdata_t *cbp = data;
5432 nvlist_t *top_nvl = *cbp->cb_nvlp;
5433 nvlist_t *nvl = NULL;
5434 nvpair_t *nvp = NULL;
5435 const char *zname = zfs_get_name(zhp);
5436 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5438 if (cbp->cb_recursive) {
5439 const char *snapname;
5440 char *delim = strchr(zname, '@');
5444 snapname = delim + 1;
5445 if (strcmp(cbp->cb_snapname, snapname))
5449 if (zfs_get_holds(zhp, &nvl) != 0)
5452 if (znamelen > cbp->cb_max_namelen)
5453 cbp->cb_max_namelen = znamelen;
5455 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5456 const char *tag = nvpair_name(nvp);
5457 size_t taglen = strnlen(tag, MAXNAMELEN);
5458 if (taglen > cbp->cb_max_taglen)
5459 cbp->cb_max_taglen = taglen;
5462 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5466 * zfs holds [-r] <snap> ...
5468 * -r Recursively hold
5471 zfs_do_holds(int argc, char **argv)
5476 boolean_t scripted = B_FALSE;
5477 boolean_t recursive = B_FALSE;
5478 const char *opts = "rH";
5481 int types = ZFS_TYPE_SNAPSHOT;
5482 holds_cbdata_t cb = { 0 };
5489 while ((c = getopt(argc, argv, opts)) != -1) {
5498 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5505 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5506 flags |= ZFS_ITER_RECURSE;
5512 /* check number of arguments */
5516 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5519 for (i = 0; i < argc; ++i) {
5520 char *snapshot = argv[i];
5522 const char *snapname;
5524 delim = strchr(snapshot, '@');
5525 if (delim == NULL) {
5526 (void) fprintf(stderr,
5527 gettext("'%s' is not a snapshot\n"), snapshot);
5531 snapname = delim + 1;
5533 snapshot[delim - snapshot] = '\0';
5535 cb.cb_recursive = recursive;
5536 cb.cb_snapname = snapname;
5540 * 1. collect holds data, set format options
5542 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5543 holds_callback, &cb);
5549 * 2. print holds data
5551 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5553 if (nvlist_empty(nvl))
5554 (void) printf(gettext("no datasets available\n"));
5558 return (0 != errors);
5561 #define CHECK_SPINNER 30
5562 #define SPINNER_TIME 3 /* seconds */
5563 #define MOUNT_TIME 5 /* seconds */
5566 get_one_dataset(zfs_handle_t *zhp, void *data)
5568 static char *spin[] = { "-", "\\", "|", "/" };
5569 static int spinval = 0;
5570 static int spincheck = 0;
5571 static time_t last_spin_time = (time_t)0;
5572 get_all_cb_t *cbp = data;
5573 zfs_type_t type = zfs_get_type(zhp);
5575 if (cbp->cb_verbose) {
5576 if (--spincheck < 0) {
5577 time_t now = time(NULL);
5578 if (last_spin_time + SPINNER_TIME < now) {
5579 update_progress(spin[spinval++ % 4]);
5580 last_spin_time = now;
5582 spincheck = CHECK_SPINNER;
5587 * Interate over any nested datasets.
5589 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5595 * Skip any datasets whose type does not match.
5597 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5601 libzfs_add_handle(cbp, zhp);
5602 assert(cbp->cb_used <= cbp->cb_alloc);
5608 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5610 get_all_cb_t cb = { 0 };
5611 cb.cb_verbose = verbose;
5612 cb.cb_getone = get_one_dataset;
5615 set_progress_header(gettext("Reading ZFS config"));
5616 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5618 *dslist = cb.cb_handles;
5619 *count = cb.cb_used;
5622 finish_progress(gettext("done."));
5626 * Generic callback for sharing or mounting filesystems. Because the code is so
5627 * similar, we have a common function with an extra parameter to determine which
5628 * mode we are using.
5630 #define OP_SHARE 0x1
5631 #define OP_MOUNT 0x2
5634 * Share or mount a dataset.
5637 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5638 boolean_t explicit, const char *options)
5640 char mountpoint[ZFS_MAXPROPLEN];
5641 char shareopts[ZFS_MAXPROPLEN];
5642 char smbshareopts[ZFS_MAXPROPLEN];
5643 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5645 uint64_t zoned, canmount;
5646 boolean_t shared_nfs, shared_smb;
5648 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5651 * Check to make sure we can mount/share this dataset. If we
5652 * are in the global zone and the filesystem is exported to a
5653 * local zone, or if we are in a local zone and the
5654 * filesystem is not exported, then it is an error.
5656 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5658 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5662 (void) fprintf(stderr, gettext("cannot %s '%s': "
5663 "dataset is exported to a local zone\n"), cmdname,
5667 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5671 (void) fprintf(stderr, gettext("cannot %s '%s': "
5672 "permission denied\n"), cmdname,
5678 * Ignore any filesystems which don't apply to us. This
5679 * includes those with a legacy mountpoint, or those with
5680 * legacy share options.
5682 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5683 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5684 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5685 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5686 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5687 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5689 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5690 strcmp(smbshareopts, "off") == 0) {
5694 (void) fprintf(stderr, gettext("cannot share '%s': "
5695 "legacy share\n"), zfs_get_name(zhp));
5696 (void) fprintf(stderr, gettext("to "
5697 "share this filesystem set "
5698 "sharenfs property on\n"));
5703 * We cannot share or mount legacy filesystems. If the
5704 * shareopts is non-legacy but the mountpoint is legacy, we
5705 * treat it as a legacy share.
5707 if (strcmp(mountpoint, "legacy") == 0) {
5711 (void) fprintf(stderr, gettext("cannot %s '%s': "
5712 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5713 (void) fprintf(stderr, gettext("use %s(8) to "
5714 "%s this filesystem\n"), cmdname, cmdname);
5718 if (strcmp(mountpoint, "none") == 0) {
5722 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5723 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5728 * canmount explicit outcome
5729 * on no pass through
5730 * on yes pass through
5732 * off yes display error, return 1
5733 * noauto no return 0
5734 * noauto yes pass through
5736 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5737 if (canmount == ZFS_CANMOUNT_OFF) {
5741 (void) fprintf(stderr, gettext("cannot %s '%s': "
5742 "'canmount' property is set to 'off'\n"), cmdname,
5745 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5750 * At this point, we have verified that the mountpoint and/or
5751 * shareopts are appropriate for auto management. If the
5752 * filesystem is already mounted or shared, return (failing
5753 * for explicit requests); otherwise mount or share the
5759 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5760 shared_smb = zfs_is_shared_smb(zhp, NULL);
5762 if (shared_nfs && shared_smb ||
5763 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5764 strcmp(smbshareopts, "off") == 0) ||
5765 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5766 strcmp(shareopts, "off") == 0)) {
5770 (void) fprintf(stderr, gettext("cannot share "
5771 "'%s': filesystem already shared\n"),
5776 if (!zfs_is_mounted(zhp, NULL) &&
5777 zfs_mount(zhp, NULL, 0) != 0)
5780 if (protocol == NULL) {
5781 if (zfs_shareall(zhp) != 0)
5783 } else if (strcmp(protocol, "nfs") == 0) {
5784 if (zfs_share_nfs(zhp))
5786 } else if (strcmp(protocol, "smb") == 0) {
5787 if (zfs_share_smb(zhp))
5790 (void) fprintf(stderr, gettext("cannot share "
5791 "'%s': invalid share type '%s' "
5793 zfs_get_name(zhp), protocol);
5800 if (options == NULL)
5801 mnt.mnt_mntopts = "";
5803 mnt.mnt_mntopts = (char *)options;
5805 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5806 zfs_is_mounted(zhp, NULL)) {
5810 (void) fprintf(stderr, gettext("cannot mount "
5811 "'%s': filesystem already mounted\n"),
5816 if (zfs_mount(zhp, options, flags) != 0)
5825 * Reports progress in the form "(current/total)". Not thread-safe.
5828 report_mount_progress(int current, int total)
5830 static time_t last_progress_time = 0;
5831 time_t now = time(NULL);
5834 /* report 1..n instead of 0..n-1 */
5837 /* display header if we're here for the first time */
5839 set_progress_header(gettext("Mounting ZFS filesystems"));
5840 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5841 /* too soon to report again */
5845 last_progress_time = now;
5847 (void) sprintf(info, "(%d/%d)", current, total);
5849 if (current == total)
5850 finish_progress(info);
5852 update_progress(info);
5856 append_options(char *mntopts, char *newopts)
5858 int len = strlen(mntopts);
5860 /* original length plus new string to append plus 1 for the comma */
5861 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5862 (void) fprintf(stderr, gettext("the opts argument for "
5863 "'%c' option is too long (more than %d chars)\n"),
5864 "-o", MNT_LINE_MAX);
5869 mntopts[len++] = ',';
5871 (void) strcpy(&mntopts[len], newopts);
5875 share_mount(int op, int argc, char **argv)
5878 boolean_t verbose = B_FALSE;
5880 char *options = NULL;
5884 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5894 if (*optarg == '\0') {
5895 (void) fprintf(stderr, gettext("empty mount "
5896 "options (-o) specified\n"));
5900 if (options == NULL)
5901 options = safe_malloc(MNT_LINE_MAX + 1);
5903 /* option validation is done later */
5904 append_options(options, optarg);
5908 warnx("no overlay mounts support on FreeBSD, ignoring");
5911 (void) fprintf(stderr, gettext("missing argument for "
5912 "'%c' option\n"), optopt);
5916 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5925 /* check number of arguments */
5927 zfs_handle_t **dslist = NULL;
5928 size_t i, count = 0;
5929 char *protocol = NULL;
5931 if (op == OP_SHARE && argc > 0) {
5932 if (strcmp(argv[0], "nfs") != 0 &&
5933 strcmp(argv[0], "smb") != 0) {
5934 (void) fprintf(stderr, gettext("share type "
5935 "must be 'nfs' or 'smb'\n"));
5944 (void) fprintf(stderr, gettext("too many arguments\n"));
5948 start_progress_timer();
5949 get_all_datasets(&dslist, &count, verbose);
5954 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5956 for (i = 0; i < count; i++) {
5958 report_mount_progress(i, count);
5960 if (share_mount_one(dslist[i], op, flags, protocol,
5961 B_FALSE, options) != 0)
5963 zfs_close(dslist[i]);
5967 } else if (argc == 0) {
5968 struct mnttab entry;
5970 if ((op == OP_SHARE) || (options != NULL)) {
5971 (void) fprintf(stderr, gettext("missing filesystem "
5972 "argument (specify -a for all)\n"));
5977 * When mount is given no arguments, go through /etc/mnttab and
5978 * display any active ZFS mounts. We hide any snapshots, since
5979 * they are controlled automatically.
5981 rewind(mnttab_file);
5982 while (getmntent(mnttab_file, &entry) == 0) {
5983 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5984 strchr(entry.mnt_special, '@') != NULL)
5987 (void) printf("%-30s %s\n", entry.mnt_special,
5995 (void) fprintf(stderr,
5996 gettext("too many arguments\n"));
6000 if ((zhp = zfs_open(g_zfs, argv[0],
6001 ZFS_TYPE_FILESYSTEM)) == NULL) {
6004 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6014 * zfs mount -a [nfs]
6015 * zfs mount filesystem
6017 * Mount all filesystems, or mount the given filesystem.
6020 zfs_do_mount(int argc, char **argv)
6022 return (share_mount(OP_MOUNT, argc, argv));
6026 * zfs share -a [nfs | smb]
6027 * zfs share filesystem
6029 * Share all filesystems, or share the given filesystem.
6032 zfs_do_share(int argc, char **argv)
6034 return (share_mount(OP_SHARE, argc, argv));
6037 typedef struct unshare_unmount_node {
6038 zfs_handle_t *un_zhp;
6040 uu_avl_node_t un_avlnode;
6041 } unshare_unmount_node_t;
6045 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6047 const unshare_unmount_node_t *l = larg;
6048 const unshare_unmount_node_t *r = rarg;
6050 return (strcmp(l->un_mountp, r->un_mountp));
6054 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6055 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6056 * and unmount it appropriately.
6059 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6063 struct stat64 statbuf;
6064 struct extmnttab entry;
6065 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6069 * Search for the path in /etc/mnttab. Rather than looking for the
6070 * specific path, which can be fooled by non-standard paths (i.e. ".."
6071 * or "//"), we stat() the path and search for the corresponding
6072 * (major,minor) device pair.
6074 if (stat64(path, &statbuf) != 0) {
6075 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6076 cmdname, path, strerror(errno));
6079 path_inode = statbuf.st_ino;
6082 * Search for the given (major,minor) pair in the mount table.
6085 rewind(mnttab_file);
6086 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6087 if (entry.mnt_major == major(statbuf.st_dev) &&
6088 entry.mnt_minor == minor(statbuf.st_dev))
6095 if (statfs(path, &sfs) != 0) {
6096 (void) fprintf(stderr, "%s: %s\n", path,
6100 statfs2mnttab(&sfs, &entry);
6104 if (op == OP_SHARE) {
6105 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6106 "currently mounted\n"), cmdname, path);
6109 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6111 if ((ret = umount2(path, flags)) != 0)
6112 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6117 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6118 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6119 "filesystem\n"), cmdname, path);
6123 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6124 ZFS_TYPE_FILESYSTEM)) == NULL)
6128 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6129 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6130 cmdname, path, strerror(errno));
6132 } else if (statbuf.st_ino != path_inode) {
6133 (void) fprintf(stderr, gettext("cannot "
6134 "%s '%s': not a mountpoint\n"), cmdname, path);
6138 if (op == OP_SHARE) {
6139 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6140 char smbshare_prop[ZFS_MAXPROPLEN];
6142 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6143 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6144 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6145 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6147 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6148 strcmp(smbshare_prop, "off") == 0) {
6149 (void) fprintf(stderr, gettext("cannot unshare "
6150 "'%s': legacy share\n"), path);
6152 (void) fprintf(stderr, gettext("use "
6153 "unshare(1M) to unshare this filesystem\n"));
6155 } else if (!zfs_is_shared(zhp)) {
6156 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6157 "not currently shared\n"), path);
6159 ret = zfs_unshareall_bypath(zhp, path);
6162 char mtpt_prop[ZFS_MAXPROPLEN];
6164 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6165 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6168 ret = zfs_unmount(zhp, NULL, flags);
6169 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6170 (void) fprintf(stderr, gettext("cannot unmount "
6171 "'%s': legacy mountpoint\n"),
6173 (void) fprintf(stderr, gettext("use umount(8) "
6174 "to unmount this filesystem\n"));
6176 ret = zfs_unmountall(zhp, flags);
6187 * Generic callback for unsharing or unmounting a filesystem.
6190 unshare_unmount(int op, int argc, char **argv)
6197 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6198 char sharesmb[ZFS_MAXPROPLEN];
6201 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6210 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6221 * We could make use of zfs_for_each() to walk all datasets in
6222 * the system, but this would be very inefficient, especially
6223 * since we would have to linearly search /etc/mnttab for each
6224 * one. Instead, do one pass through /etc/mnttab looking for
6225 * zfs entries and call zfs_unmount() for each one.
6227 * Things get a little tricky if the administrator has created
6228 * mountpoints beneath other ZFS filesystems. In this case, we
6229 * have to unmount the deepest filesystems first. To accomplish
6230 * this, we place all the mountpoints in an AVL tree sorted by
6231 * the special type (dataset name), and walk the result in
6232 * reverse to make sure to get any snapshots first.
6234 struct mnttab entry;
6235 uu_avl_pool_t *pool;
6237 unshare_unmount_node_t *node;
6239 uu_avl_walk_t *walk;
6242 (void) fprintf(stderr, gettext("too many arguments\n"));
6246 if (((pool = uu_avl_pool_create("unmount_pool",
6247 sizeof (unshare_unmount_node_t),
6248 offsetof(unshare_unmount_node_t, un_avlnode),
6249 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6250 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6253 rewind(mnttab_file);
6254 while (getmntent(mnttab_file, &entry) == 0) {
6256 /* ignore non-ZFS entries */
6257 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6260 /* ignore snapshots */
6261 if (strchr(entry.mnt_special, '@') != NULL)
6264 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6265 ZFS_TYPE_FILESYSTEM)) == NULL) {
6272 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6274 sizeof (nfs_mnt_prop),
6275 NULL, NULL, 0, B_FALSE) == 0);
6276 if (strcmp(nfs_mnt_prop, "off") != 0)
6278 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6280 sizeof (nfs_mnt_prop),
6281 NULL, NULL, 0, B_FALSE) == 0);
6282 if (strcmp(nfs_mnt_prop, "off") == 0)
6286 /* Ignore legacy mounts */
6287 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6289 sizeof (nfs_mnt_prop),
6290 NULL, NULL, 0, B_FALSE) == 0);
6291 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6293 /* Ignore canmount=noauto mounts */
6294 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6295 ZFS_CANMOUNT_NOAUTO)
6301 node = safe_malloc(sizeof (unshare_unmount_node_t));
6303 node->un_mountp = safe_strdup(entry.mnt_mountp);
6305 uu_avl_node_init(node, &node->un_avlnode, pool);
6307 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6308 uu_avl_insert(tree, node, idx);
6310 zfs_close(node->un_zhp);
6311 free(node->un_mountp);
6317 * Walk the AVL tree in reverse, unmounting each filesystem and
6318 * removing it from the AVL tree in the process.
6320 if ((walk = uu_avl_walk_start(tree,
6321 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6324 while ((node = uu_avl_walk_next(walk)) != NULL) {
6325 uu_avl_remove(tree, node);
6329 if (zfs_unshareall_bypath(node->un_zhp,
6330 node->un_mountp) != 0)
6335 if (zfs_unmount(node->un_zhp,
6336 node->un_mountp, flags) != 0)
6341 zfs_close(node->un_zhp);
6342 free(node->un_mountp);
6346 uu_avl_walk_end(walk);
6347 uu_avl_destroy(tree);
6348 uu_avl_pool_destroy(pool);
6353 (void) fprintf(stderr,
6354 gettext("missing filesystem argument\n"));
6356 (void) fprintf(stderr,
6357 gettext("too many arguments\n"));
6362 * We have an argument, but it may be a full path or a ZFS
6363 * filesystem. Pass full paths off to unmount_path() (shared by
6364 * manual_unmount), otherwise open the filesystem and pass to
6367 if (argv[0][0] == '/')
6368 return (unshare_unmount_path(op, argv[0],
6371 if ((zhp = zfs_open(g_zfs, argv[0],
6372 ZFS_TYPE_FILESYSTEM)) == NULL)
6375 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6376 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6377 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6378 NULL, 0, B_FALSE) == 0);
6382 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6384 sizeof (nfs_mnt_prop),
6385 NULL, NULL, 0, B_FALSE) == 0);
6386 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6387 sharesmb, sizeof (sharesmb), NULL, NULL,
6390 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6391 strcmp(sharesmb, "off") == 0) {
6392 (void) fprintf(stderr, gettext("cannot "
6393 "unshare '%s': legacy share\n"),
6396 (void) fprintf(stderr, gettext("use "
6397 "unshare(1M) to unshare this "
6401 } else if (!zfs_is_shared(zhp)) {
6402 (void) fprintf(stderr, gettext("cannot "
6403 "unshare '%s': not currently "
6404 "shared\n"), zfs_get_name(zhp));
6406 } else if (zfs_unshareall(zhp) != 0) {
6412 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6413 (void) fprintf(stderr, gettext("cannot "
6414 "unmount '%s': legacy "
6415 "mountpoint\n"), zfs_get_name(zhp));
6416 (void) fprintf(stderr, gettext("use "
6417 "umount(8) to unmount this "
6420 } else if (!zfs_is_mounted(zhp, NULL)) {
6421 (void) fprintf(stderr, gettext("cannot "
6422 "unmount '%s': not currently "
6426 } else if (zfs_unmountall(zhp, flags) != 0) {
6440 * zfs unmount filesystem
6442 * Unmount all filesystems, or a specific ZFS filesystem.
6445 zfs_do_unmount(int argc, char **argv)
6447 return (unshare_unmount(OP_MOUNT, argc, argv));
6452 * zfs unshare filesystem
6454 * Unshare all filesystems, or a specific ZFS filesystem.
6457 zfs_do_unshare(int argc, char **argv)
6459 return (unshare_unmount(OP_SHARE, argc, argv));
6463 * Attach/detach the given dataset to/from the given jail
6467 do_jail(int argc, char **argv, int attach)
6472 /* check number of arguments */
6474 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6478 (void) fprintf(stderr, gettext("too many arguments\n"));
6482 jailid = jail_getid(argv[1]);
6484 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6488 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6492 ret = (zfs_jail(zhp, jailid, attach) != 0);
6499 * zfs jail jailid filesystem
6501 * Attach the given dataset to the given jail
6505 zfs_do_jail(int argc, char **argv)
6508 return (do_jail(argc, argv, 1));
6512 * zfs unjail jailid filesystem
6514 * Detach the given dataset from the given jail
6518 zfs_do_unjail(int argc, char **argv)
6521 return (do_jail(argc, argv, 0));
6525 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6526 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6529 manual_mount(int argc, char **argv)
6532 char mountpoint[ZFS_MAXPROPLEN];
6533 char mntopts[MNT_LINE_MAX] = { '\0' };
6537 char *dataset, *path;
6540 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6543 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6546 flags |= MS_OVERLAY;
6549 flags |= MS_NOMNTTAB;
6552 (void) fprintf(stderr, gettext("missing argument for "
6553 "'%c' option\n"), optopt);
6557 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6559 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6568 /* check that we only have two arguments */
6571 (void) fprintf(stderr, gettext("missing dataset "
6574 (void) fprintf(stderr,
6575 gettext("missing mountpoint argument\n"));
6577 (void) fprintf(stderr, gettext("too many arguments\n"));
6578 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6585 /* try to open the dataset */
6586 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6589 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6590 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6592 /* check for legacy mountpoint and complain appropriately */
6594 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6595 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6596 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6597 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6602 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6603 "mounted using 'mount -t zfs'\n"), dataset);
6604 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6605 "instead.\n"), path);
6606 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6607 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6608 (void) fprintf(stderr, gettext("See zfs(8) for more "
6617 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6618 * unmounts of non-legacy filesystems, as this is the dominant administrative
6622 manual_unmount(int argc, char **argv)
6628 while ((c = getopt(argc, argv, "f")) != -1) {
6634 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6636 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6645 /* check arguments */
6648 (void) fprintf(stderr, gettext("missing path "
6651 (void) fprintf(stderr, gettext("too many arguments\n"));
6652 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6656 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6660 find_command_idx(char *command, int *idx)
6664 for (i = 0; i < NCOMMAND; i++) {
6665 if (command_table[i].name == NULL)
6668 if (strcmp(command, command_table[i].name) == 0) {
6677 zfs_do_diff(int argc, char **argv)
6681 char *tosnap = NULL;
6682 char *fromsnap = NULL;
6687 while ((c = getopt(argc, argv, "FHt")) != -1) {
6690 flags |= ZFS_DIFF_CLASSIFY;
6693 flags |= ZFS_DIFF_PARSEABLE;
6696 flags |= ZFS_DIFF_TIMESTAMP;
6699 (void) fprintf(stderr,
6700 gettext("invalid option '%c'\n"), optopt);
6709 (void) fprintf(stderr,
6710 gettext("must provide at least one snapshot name\n"));
6715 (void) fprintf(stderr, gettext("too many arguments\n"));
6720 tosnap = (argc == 2) ? argv[1] : NULL;
6723 if (*fromsnap != '@')
6724 copy = strdup(fromsnap);
6726 copy = strdup(tosnap);
6730 if (atp = strchr(copy, '@'))
6733 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6739 * Ignore SIGPIPE so that the library can give us
6740 * information on any failure
6742 (void) sigignore(SIGPIPE);
6744 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6752 * zfs bookmark <fs@snap> <fs#bmark>
6754 * Creates a bookmark with the given name from the given snapshot.
6757 zfs_do_bookmark(int argc, char **argv)
6759 char snapname[ZFS_MAXNAMELEN];
6766 while ((c = getopt(argc, argv, "")) != -1) {
6769 (void) fprintf(stderr,
6770 gettext("invalid option '%c'\n"), optopt);
6778 /* check number of arguments */
6780 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6784 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6788 if (strchr(argv[1], '#') == NULL) {
6789 (void) fprintf(stderr,
6790 gettext("invalid bookmark name '%s' -- "
6791 "must contain a '#'\n"), argv[1]);
6795 if (argv[0][0] == '@') {
6797 * Snapshot name begins with @.
6798 * Default to same fs as bookmark.
6800 (void) strncpy(snapname, argv[1], sizeof (snapname));
6801 *strchr(snapname, '#') = '\0';
6802 (void) strlcat(snapname, argv[0], sizeof (snapname));
6804 (void) strncpy(snapname, argv[0], sizeof (snapname));
6806 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6812 nvl = fnvlist_alloc();
6813 fnvlist_add_string(nvl, argv[1], snapname);
6814 ret = lzc_bookmark(nvl, NULL);
6818 const char *err_msg;
6821 (void) snprintf(errbuf, sizeof (errbuf),
6822 dgettext(TEXT_DOMAIN,
6823 "cannot create bookmark '%s'"), argv[1]);
6827 err_msg = "bookmark is in a different pool";
6830 err_msg = "bookmark exists";
6833 err_msg = "invalid argument";
6836 err_msg = "bookmark feature not enabled";
6839 err_msg = "unknown error";
6842 (void) fprintf(stderr, "%s: %s\n", errbuf,
6843 dgettext(TEXT_DOMAIN, err_msg));
6854 main(int argc, char **argv)
6861 (void) setlocale(LC_ALL, "");
6862 (void) textdomain(TEXT_DOMAIN);
6866 if ((g_zfs = libzfs_init()) == NULL) {
6867 (void) fprintf(stderr, gettext("internal error: failed to "
6868 "initialize ZFS library\n"));
6872 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6874 libzfs_print_on_error(g_zfs, B_TRUE);
6876 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6877 (void) fprintf(stderr, gettext("internal error: unable to "
6878 "open %s\n"), MNTTAB);
6883 * This command also doubles as the /etc/fs mount and unmount program.
6884 * Determine if we should take this behavior based on argv[0].
6886 progname = basename(argv[0]);
6887 if (strcmp(progname, "mount") == 0) {
6888 ret = manual_mount(argc, argv);
6889 } else if (strcmp(progname, "umount") == 0) {
6890 ret = manual_unmount(argc, argv);
6893 * Make sure the user has specified some command.
6896 (void) fprintf(stderr, gettext("missing command\n"));
6903 * The 'umount' command is an alias for 'unmount'
6905 if (strcmp(cmdname, "umount") == 0)
6906 cmdname = "unmount";
6909 * The 'recv' command is an alias for 'receive'
6911 if (strcmp(cmdname, "recv") == 0)
6912 cmdname = "receive";
6915 * The 'snap' command is an alias for 'snapshot'
6917 if (strcmp(cmdname, "snap") == 0)
6918 cmdname = "snapshot";
6923 if (strcmp(cmdname, "-?") == 0)
6927 * Run the appropriate command.
6929 libzfs_mnttab_cache(g_zfs, B_TRUE);
6930 if (find_command_idx(cmdname, &i) == 0) {
6931 current_command = &command_table[i];
6932 ret = command_table[i].func(argc - 1, argv + 1);
6933 } else if (strchr(cmdname, '=') != NULL) {
6934 verify(find_command_idx("set", &i) == 0);
6935 current_command = &command_table[i];
6936 ret = command_table[i].func(argc, argv);
6938 (void) fprintf(stderr, gettext("unrecognized "
6939 "command '%s'\n"), cmdname);
6942 libzfs_mnttab_cache(g_zfs, B_FALSE);
6945 (void) fclose(mnttab_file);
6947 if (ret == 0 && log_history)
6948 (void) zpool_log_history(g_zfs, history_str);
6953 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6954 * for the purposes of running ::findleaks.
6956 if (getenv("ZFS_ABORT") != NULL) {
6957 (void) printf("dumping core by request\n");