4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * All rights reserved.
29 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
30 * Copyright (c) 2013 Steven Hartland. All rights reserved.
31 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
40 #include <libnvpair.h>
53 #include <sys/mntent.h>
54 #include <sys/mnttab.h>
55 #include <sys/mount.h>
57 #include <sys/fs/zfs.h>
58 #include <sys/types.h>
64 #include <libzfs_core.h>
66 #include <zfs_deleg.h>
70 #include <directory.h>
76 #include "zfs_comutil.h"
78 libzfs_handle_t *g_zfs;
80 static FILE *mnttab_file;
81 static char history_str[HIS_MAX_RECORD_LEN];
82 static boolean_t log_history = B_TRUE;
84 static int zfs_do_clone(int argc, char **argv);
85 static int zfs_do_create(int argc, char **argv);
86 static int zfs_do_destroy(int argc, char **argv);
87 static int zfs_do_get(int argc, char **argv);
88 static int zfs_do_inherit(int argc, char **argv);
89 static int zfs_do_list(int argc, char **argv);
90 static int zfs_do_mount(int argc, char **argv);
91 static int zfs_do_rename(int argc, char **argv);
92 static int zfs_do_rollback(int argc, char **argv);
93 static int zfs_do_set(int argc, char **argv);
94 static int zfs_do_upgrade(int argc, char **argv);
95 static int zfs_do_snapshot(int argc, char **argv);
96 static int zfs_do_unmount(int argc, char **argv);
97 static int zfs_do_share(int argc, char **argv);
98 static int zfs_do_unshare(int argc, char **argv);
99 static int zfs_do_send(int argc, char **argv);
100 static int zfs_do_receive(int argc, char **argv);
101 static int zfs_do_promote(int argc, char **argv);
102 static int zfs_do_userspace(int argc, char **argv);
103 static int zfs_do_allow(int argc, char **argv);
104 static int zfs_do_unallow(int argc, char **argv);
105 static int zfs_do_hold(int argc, char **argv);
106 static int zfs_do_holds(int argc, char **argv);
107 static int zfs_do_release(int argc, char **argv);
108 static int zfs_do_diff(int argc, char **argv);
109 static int zfs_do_jail(int argc, char **argv);
110 static int zfs_do_unjail(int argc, char **argv);
111 static int zfs_do_bookmark(int argc, char **argv);
114 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
119 _umem_debug_init(void)
121 return ("default,verbose"); /* $UMEM_DEBUG setting */
125 _umem_logging_init(void)
127 return ("fail,contents"); /* $UMEM_LOGGING setting */
163 typedef struct zfs_command {
165 int (*func)(int argc, char **argv);
170 * Master command table. Each ZFS command has a name, associated function, and
171 * usage message. The usage messages need to be internationalized, so we have
172 * to have a function to return the usage message based on a command index.
174 * These commands are organized according to how they are displayed in the usage
175 * message. An empty command (one with a NULL name) indicates an empty line in
176 * the generic usage message.
178 static zfs_command_t command_table[] = {
179 { "create", zfs_do_create, HELP_CREATE },
180 { "destroy", zfs_do_destroy, HELP_DESTROY },
182 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
183 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
184 { "clone", zfs_do_clone, HELP_CLONE },
185 { "promote", zfs_do_promote, HELP_PROMOTE },
186 { "rename", zfs_do_rename, HELP_RENAME },
187 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
189 { "list", zfs_do_list, HELP_LIST },
191 { "set", zfs_do_set, HELP_SET },
192 { "get", zfs_do_get, HELP_GET },
193 { "inherit", zfs_do_inherit, HELP_INHERIT },
194 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
195 { "userspace", zfs_do_userspace, HELP_USERSPACE },
196 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
198 { "mount", zfs_do_mount, HELP_MOUNT },
199 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
200 { "share", zfs_do_share, HELP_SHARE },
201 { "unshare", zfs_do_unshare, HELP_UNSHARE },
203 { "send", zfs_do_send, HELP_SEND },
204 { "receive", zfs_do_receive, HELP_RECEIVE },
206 { "allow", zfs_do_allow, HELP_ALLOW },
208 { "unallow", zfs_do_unallow, HELP_UNALLOW },
210 { "hold", zfs_do_hold, HELP_HOLD },
211 { "holds", zfs_do_holds, HELP_HOLDS },
212 { "release", zfs_do_release, HELP_RELEASE },
213 { "diff", zfs_do_diff, HELP_DIFF },
215 { "jail", zfs_do_jail, HELP_JAIL },
216 { "unjail", zfs_do_unjail, HELP_UNJAIL },
219 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
221 zfs_command_t *current_command;
224 get_usage(zfs_help_t idx)
228 return (gettext("\tclone [-p] [-o property=value] ... "
229 "<snapshot> <filesystem|volume>\n"));
231 return (gettext("\tcreate [-pu] [-o property=value] ... "
233 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
234 "-V <size> <volume>\n"));
236 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
237 "\tdestroy [-dnpRrv] "
238 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
239 "\tdestroy <filesystem|volume>#<bookmark>\n"));
241 return (gettext("\tget [-rHp] [-d max] "
242 "[-o \"all\" | field[,...]]\n"
243 "\t [-t type[,...]] [-s source[,...]]\n"
244 "\t <\"all\" | property[,...]> "
245 "[filesystem|volume|snapshot] ...\n"));
247 return (gettext("\tinherit [-rS] <property> "
248 "<filesystem|volume|snapshot> ...\n"));
250 return (gettext("\tupgrade [-v]\n"
251 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
253 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
255 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
257 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
258 "[-s property]...\n\t [-S property]... [-t type[,...]] "
259 "[filesystem|volume|snapshot] ...\n"));
261 return (gettext("\tmount\n"
262 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
264 return (gettext("\tpromote <clone-filesystem>\n"));
266 return (gettext("\treceive|recv [-vnsFu] <filesystem|volume|"
268 "\treceive|recv [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
270 "\treceive|recv -A <filesystem|volume>\n"));
272 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
273 "<filesystem|volume|snapshot>\n"
274 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
275 "\trename -r <snapshot> <snapshot>\n"
276 "\trename -u [-p] <filesystem> <filesystem>"));
278 return (gettext("\trollback [-rRf] <snapshot>\n"));
280 return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] "
282 "\tsend [-Le] [-i snapshot|bookmark] "
283 "<filesystem|volume|snapshot>\n"
284 "\tsend [-nvPe] -t <receive_resume_token>\n"));
286 return (gettext("\tset <property=value> "
287 "<filesystem|volume|snapshot> ...\n"));
289 return (gettext("\tshare <-a | filesystem>\n"));
291 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
292 "<filesystem|volume>@<snap> ...\n"));
294 return (gettext("\tunmount|umount [-f] "
295 "<-a | filesystem|mountpoint>\n"));
297 return (gettext("\tunshare "
298 "<-a | filesystem|mountpoint>\n"));
300 return (gettext("\tallow <filesystem|volume>\n"
302 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
303 "\t <filesystem|volume>\n"
304 "\tallow [-ld] -e <perm|@setname>[,...] "
305 "<filesystem|volume>\n"
306 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
307 "\tallow -s @setname <perm|@setname>[,...] "
308 "<filesystem|volume>\n"));
310 return (gettext("\tunallow [-rldug] "
311 "<\"everyone\"|user|group>[,...]\n"
312 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
313 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
314 "<filesystem|volume>\n"
315 "\tunallow [-r] -c [<perm|@setname>[,...]] "
316 "<filesystem|volume>\n"
317 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
318 "<filesystem|volume>\n"));
320 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
322 "\t [-S field] ... [-t type[,...]] "
323 "<filesystem|snapshot>\n"));
324 case HELP_GROUPSPACE:
325 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
327 "\t [-S field] ... [-t type[,...]] "
328 "<filesystem|snapshot>\n"));
330 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
332 return (gettext("\tholds [-r] <snapshot> ...\n"));
334 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
336 return (gettext("\tdiff [-FHt] <snapshot> "
337 "[snapshot|filesystem]\n"));
339 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
349 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
354 * Utility function to guarantee malloc() success.
358 safe_malloc(size_t size)
362 if ((data = calloc(1, size)) == NULL)
369 safe_strdup(char *str)
371 char *dupstr = strdup(str);
380 * Callback routine that will print out information for each of
384 usage_prop_cb(int prop, void *cb)
388 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
390 if (zfs_prop_readonly(prop))
391 (void) fprintf(fp, " NO ");
393 (void) fprintf(fp, "YES ");
395 if (zfs_prop_inheritable(prop))
396 (void) fprintf(fp, " YES ");
398 (void) fprintf(fp, " NO ");
400 if (zfs_prop_values(prop) == NULL)
401 (void) fprintf(fp, "-\n");
403 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
409 * Display usage message. If we're inside a command, display only the usage for
410 * that command. Otherwise, iterate over the entire command table and display
411 * a complete usage message.
414 usage(boolean_t requested)
417 boolean_t show_properties = B_FALSE;
418 FILE *fp = requested ? stdout : stderr;
420 if (current_command == NULL) {
422 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
424 gettext("where 'command' is one of the following:\n\n"));
426 for (i = 0; i < NCOMMAND; i++) {
427 if (command_table[i].name == NULL)
428 (void) fprintf(fp, "\n");
430 (void) fprintf(fp, "%s",
431 get_usage(command_table[i].usage));
434 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
435 "pool/[dataset/]*dataset[@name]\n"));
437 (void) fprintf(fp, gettext("usage:\n"));
438 (void) fprintf(fp, "%s", get_usage(current_command->usage));
441 if (current_command != NULL &&
442 (strcmp(current_command->name, "set") == 0 ||
443 strcmp(current_command->name, "get") == 0 ||
444 strcmp(current_command->name, "inherit") == 0 ||
445 strcmp(current_command->name, "list") == 0))
446 show_properties = B_TRUE;
448 if (show_properties) {
450 gettext("\nThe following properties are supported:\n"));
452 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
453 "PROPERTY", "EDIT", "INHERIT", "VALUES");
455 /* Iterate over all properties */
456 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
459 (void) fprintf(fp, "\t%-15s ", "userused@...");
460 (void) fprintf(fp, " NO NO <size>\n");
461 (void) fprintf(fp, "\t%-15s ", "groupused@...");
462 (void) fprintf(fp, " NO NO <size>\n");
463 (void) fprintf(fp, "\t%-15s ", "userquota@...");
464 (void) fprintf(fp, "YES NO <size> | none\n");
465 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
466 (void) fprintf(fp, "YES NO <size> | none\n");
467 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
468 (void) fprintf(fp, " NO NO <size>\n");
470 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
471 "with standard units such as K, M, G, etc.\n"));
472 (void) fprintf(fp, gettext("\nUser-defined properties can "
473 "be specified by using a name containing a colon (:).\n"));
474 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
475 "properties must be appended with\n"
476 "a user or group specifier of one of these forms:\n"
477 " POSIX name (eg: \"matt\")\n"
478 " POSIX id (eg: \"126829\")\n"
479 " SMB name@domain (eg: \"matt@sun\")\n"
480 " SMB SID (eg: \"S-1-234-567-89\")\n"));
483 gettext("\nFor the property list, run: %s\n"),
486 gettext("\nFor the delegated permission list, run: %s\n"),
487 "zfs allow|unallow");
491 * See comments at end of main().
493 if (getenv("ZFS_ABORT") != NULL) {
494 (void) printf("dumping core by request\n");
498 exit(requested ? 0 : 2);
502 parseprop(nvlist_t *props, char *propname)
504 char *propval, *strval;
506 if ((propval = strchr(propname, '=')) == NULL) {
507 (void) fprintf(stderr, gettext("missing "
508 "'=' for -o option\n"));
513 if (nvlist_lookup_string(props, propname, &strval) == 0) {
514 (void) fprintf(stderr, gettext("property '%s' "
515 "specified multiple times\n"), propname);
518 if (nvlist_add_string(props, propname, propval) != 0)
524 parse_depth(char *opt, int *flags)
529 depth = (int)strtol(opt, &tmp, 0);
531 (void) fprintf(stderr,
532 gettext("%s is not an integer\n"), opt);
536 (void) fprintf(stderr,
537 gettext("Depth can not be negative.\n"));
540 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
544 #define PROGRESS_DELAY 2 /* seconds */
546 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";
547 static time_t pt_begin;
548 static char *pt_header = NULL;
549 static boolean_t pt_shown;
552 start_progress_timer(void)
554 pt_begin = time(NULL) + PROGRESS_DELAY;
559 set_progress_header(char *header)
561 assert(pt_header == NULL);
562 pt_header = safe_strdup(header);
564 (void) printf("%s: ", header);
565 (void) fflush(stdout);
570 update_progress(char *update)
572 if (!pt_shown && time(NULL) > pt_begin) {
573 int len = strlen(update);
575 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
577 (void) fflush(stdout);
579 } else if (pt_shown) {
580 int len = strlen(update);
582 (void) printf("%s%*.*s", update, len, len, pt_reverse);
583 (void) fflush(stdout);
588 finish_progress(char *done)
591 (void) printf("%s\n", done);
592 (void) fflush(stdout);
599 * Check if the dataset is mountable and should be automatically mounted.
602 should_auto_mount(zfs_handle_t *zhp)
604 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp)))
606 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON);
610 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
612 * Given an existing dataset, create a writable copy whose initial contents
613 * are the same as the source. The newly created dataset maintains a
614 * dependency on the original; the original cannot be destroyed so long as
617 * The '-p' flag creates all the non-existing ancestors of the target first.
620 zfs_do_clone(int argc, char **argv)
622 zfs_handle_t *zhp = NULL;
623 boolean_t parents = B_FALSE;
628 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
632 while ((c = getopt(argc, argv, "o:p")) != -1) {
635 if (parseprop(props, optarg))
642 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
651 /* check number of arguments */
653 (void) fprintf(stderr, gettext("missing source dataset "
658 (void) fprintf(stderr, gettext("missing target dataset "
663 (void) fprintf(stderr, gettext("too many arguments\n"));
667 /* open the source dataset */
668 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
671 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
674 * Now create the ancestors of the target dataset. If the
675 * target already exists and '-p' option was used we should not
678 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
681 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
686 ret = zfs_clone(zhp, argv[1], props);
688 /* create the mountpoint if necessary */
692 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
695 * If the user doesn't want the dataset
696 * automatically mounted, then skip the mount/share
699 if (should_auto_mount(clone)) {
700 if ((ret = zfs_mount(clone, NULL, 0)) != 0) {
701 (void) fprintf(stderr, gettext("clone "
702 "successfully created, "
703 "but not mounted\n"));
704 } else if ((ret = zfs_share(clone)) != 0) {
705 (void) fprintf(stderr, gettext("clone "
706 "successfully created, "
707 "but not shared\n"));
728 * zfs create [-pu] [-o prop=value] ... fs
729 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
731 * Create a new dataset. This command can be used to create filesystems
732 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
733 * For volumes, the user must specify a size to be used.
735 * The '-s' flag applies only to volumes, and indicates that we should not try
736 * to set the reservation for this volume. By default we set a reservation
737 * equal to the size for any volume. For pools with SPA_VERSION >=
738 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
740 * The '-p' flag creates all the non-existing ancestors of the target first.
742 * The '-u' flag prevents mounting of newly created file system.
745 zfs_do_create(int argc, char **argv)
747 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
748 zfs_handle_t *zhp = NULL;
751 boolean_t noreserve = B_FALSE;
752 boolean_t bflag = B_FALSE;
753 boolean_t parents = B_FALSE;
754 boolean_t nomount = B_FALSE;
759 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
763 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) {
766 type = ZFS_TYPE_VOLUME;
767 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
768 (void) fprintf(stderr, gettext("bad volume "
769 "size '%s': %s\n"), optarg,
770 libzfs_error_description(g_zfs));
774 if (nvlist_add_uint64(props,
775 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
784 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
785 (void) fprintf(stderr, gettext("bad volume "
786 "block size '%s': %s\n"), optarg,
787 libzfs_error_description(g_zfs));
791 if (nvlist_add_uint64(props,
792 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
797 if (parseprop(props, optarg) != 0)
807 (void) fprintf(stderr, gettext("missing size "
811 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
817 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
818 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
819 "used when creating a volume\n"));
822 if (nomount && type != ZFS_TYPE_FILESYSTEM) {
823 (void) fprintf(stderr, gettext("'-u' can only be "
824 "used when creating a file system\n"));
831 /* check number of arguments */
833 (void) fprintf(stderr, gettext("missing %s argument\n"),
834 zfs_type_to_name(type));
838 (void) fprintf(stderr, gettext("too many arguments\n"));
842 if (type == ZFS_TYPE_VOLUME && !noreserve) {
843 zpool_handle_t *zpool_handle;
844 uint64_t spa_version;
846 zfs_prop_t resv_prop;
849 if (p = strchr(argv[0], '/'))
851 zpool_handle = zpool_open(g_zfs, argv[0]);
854 if (zpool_handle == NULL)
856 spa_version = zpool_get_prop_int(zpool_handle,
857 ZPOOL_PROP_VERSION, NULL);
858 zpool_close(zpool_handle);
859 if (spa_version >= SPA_VERSION_REFRESERVATION)
860 resv_prop = ZFS_PROP_REFRESERVATION;
862 resv_prop = ZFS_PROP_RESERVATION;
863 volsize = zvol_volsize_to_reservation(volsize, props);
865 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
867 if (nvlist_add_uint64(props,
868 zfs_prop_to_name(resv_prop), volsize) != 0) {
875 if (parents && zfs_name_valid(argv[0], type)) {
877 * Now create the ancestors of target dataset. If the target
878 * already exists and '-p' option was used we should not
881 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
885 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
890 if (zfs_create(g_zfs, argv[0], type, props) != 0)
893 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
899 * Mount and/or share the new filesystem as appropriate. We provide a
900 * verbose error message to let the user know that their filesystem was
901 * in fact created, even if we failed to mount or share it.
902 * If the user doesn't want the dataset automatically mounted,
903 * then skip the mount/share step altogether.
905 if (!nomount && should_auto_mount(zhp)) {
906 if (zfs_mount(zhp, NULL, 0) != 0) {
907 (void) fprintf(stderr, gettext("filesystem "
908 "successfully created, but not mounted\n"));
910 } else if (zfs_share(zhp) != 0) {
911 (void) fprintf(stderr, gettext("filesystem "
912 "successfully created, but not shared\n"));
929 * zfs destroy [-rRf] <fs, vol>
930 * zfs destroy [-rRd] <snap>
932 * -r Recursively destroy all children
933 * -R Recursively destroy all dependents, including clones
934 * -f Force unmounting of any dependents
935 * -d If we can't destroy now, mark for deferred destruction
937 * Destroys the given dataset. By default, it will unmount any filesystems,
938 * and refuse to destroy a dataset that has any dependents. A dependent can
939 * either be a child, or a clone of a child.
941 typedef struct destroy_cbdata {
944 boolean_t cb_recurse;
946 boolean_t cb_doclones;
947 zfs_handle_t *cb_target;
948 boolean_t cb_defer_destroy;
949 boolean_t cb_verbose;
950 boolean_t cb_parsable;
953 nvlist_t *cb_batchedsnaps;
955 /* first snap in contiguous run */
957 /* previous snap in contiguous run */
965 * Check for any dependents based on the '-r' or '-R' flags.
968 destroy_check_dependent(zfs_handle_t *zhp, void *data)
970 destroy_cbdata_t *cbp = data;
971 const char *tname = zfs_get_name(cbp->cb_target);
972 const char *name = zfs_get_name(zhp);
974 if (strncmp(tname, name, strlen(tname)) == 0 &&
975 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
977 * This is a direct descendant, not a clone somewhere else in
984 (void) fprintf(stderr, gettext("cannot destroy '%s': "
985 "%s has children\n"),
986 zfs_get_name(cbp->cb_target),
987 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
988 (void) fprintf(stderr, gettext("use '-r' to destroy "
989 "the following datasets:\n"));
990 cbp->cb_first = B_FALSE;
991 cbp->cb_error = B_TRUE;
994 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
997 * This is a clone. We only want to report this if the '-r'
998 * wasn't specified, or the target is a snapshot.
1000 if (!cbp->cb_recurse &&
1001 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1004 if (cbp->cb_first) {
1005 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1006 "%s has dependent clones\n"),
1007 zfs_get_name(cbp->cb_target),
1008 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1009 (void) fprintf(stderr, gettext("use '-R' to destroy "
1010 "the following datasets:\n"));
1011 cbp->cb_first = B_FALSE;
1012 cbp->cb_error = B_TRUE;
1013 cbp->cb_dryrun = B_TRUE;
1016 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1025 destroy_callback(zfs_handle_t *zhp, void *data)
1027 destroy_cbdata_t *cb = data;
1028 const char *name = zfs_get_name(zhp);
1030 if (cb->cb_verbose) {
1031 if (cb->cb_parsable) {
1032 (void) printf("destroy\t%s\n", name);
1033 } else if (cb->cb_dryrun) {
1034 (void) printf(gettext("would destroy %s\n"),
1037 (void) printf(gettext("will destroy %s\n"),
1043 * Ignore pools (which we've already flagged as an error before getting
1046 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1047 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1051 if (cb->cb_dryrun) {
1057 * We batch up all contiguous snapshots (even of different
1058 * filesystems) and destroy them with one ioctl. We can't
1059 * simply do all snap deletions and then all fs deletions,
1060 * because we must delete a clone before its origin.
1062 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1063 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1065 int error = zfs_destroy_snaps_nvl(g_zfs,
1066 cb->cb_batchedsnaps, B_FALSE);
1067 fnvlist_free(cb->cb_batchedsnaps);
1068 cb->cb_batchedsnaps = fnvlist_alloc();
1071 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1072 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1083 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1085 destroy_cbdata_t *cb = arg;
1086 const char *name = zfs_get_name(zhp);
1089 if (nvlist_exists(cb->cb_nvl, name)) {
1090 if (cb->cb_firstsnap == NULL)
1091 cb->cb_firstsnap = strdup(name);
1092 if (cb->cb_prevsnap != NULL)
1093 free(cb->cb_prevsnap);
1094 /* this snap continues the current range */
1095 cb->cb_prevsnap = strdup(name);
1096 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1098 if (cb->cb_verbose) {
1099 if (cb->cb_parsable) {
1100 (void) printf("destroy\t%s\n", name);
1101 } else if (cb->cb_dryrun) {
1102 (void) printf(gettext("would destroy %s\n"),
1105 (void) printf(gettext("will destroy %s\n"),
1109 } else if (cb->cb_firstsnap != NULL) {
1110 /* end of this range */
1112 err = lzc_snaprange_space(cb->cb_firstsnap,
1113 cb->cb_prevsnap, &used);
1114 cb->cb_snapused += used;
1115 free(cb->cb_firstsnap);
1116 cb->cb_firstsnap = NULL;
1117 free(cb->cb_prevsnap);
1118 cb->cb_prevsnap = NULL;
1125 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1128 assert(cb->cb_firstsnap == NULL);
1129 assert(cb->cb_prevsnap == NULL);
1130 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1131 if (cb->cb_firstsnap != NULL) {
1134 err = lzc_snaprange_space(cb->cb_firstsnap,
1135 cb->cb_prevsnap, &used);
1137 cb->cb_snapused += used;
1138 free(cb->cb_firstsnap);
1139 cb->cb_firstsnap = NULL;
1140 free(cb->cb_prevsnap);
1141 cb->cb_prevsnap = NULL;
1147 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1149 destroy_cbdata_t *cb = arg;
1152 /* Check for clones. */
1153 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1154 cb->cb_target = zhp;
1155 cb->cb_first = B_TRUE;
1156 err = zfs_iter_dependents(zhp, B_TRUE,
1157 destroy_check_dependent, cb);
1161 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1169 gather_snapshots(zfs_handle_t *zhp, void *arg)
1171 destroy_cbdata_t *cb = arg;
1174 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1180 if (cb->cb_verbose) {
1181 err = destroy_print_snapshots(zhp, cb);
1187 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1195 destroy_clones(destroy_cbdata_t *cb)
1198 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1200 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1201 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1204 boolean_t defer = cb->cb_defer_destroy;
1208 * We can't defer destroy non-snapshots, so set it to
1209 * false while destroying the clones.
1211 cb->cb_defer_destroy = B_FALSE;
1212 err = zfs_iter_dependents(zhp, B_FALSE,
1213 destroy_callback, cb);
1214 cb->cb_defer_destroy = defer;
1224 zfs_do_destroy(int argc, char **argv)
1226 destroy_cbdata_t cb = { 0 };
1230 zfs_handle_t *zhp = NULL;
1232 zfs_type_t type = ZFS_TYPE_DATASET;
1235 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1238 cb.cb_verbose = B_TRUE;
1241 cb.cb_verbose = B_TRUE;
1242 cb.cb_parsable = B_TRUE;
1245 cb.cb_dryrun = B_TRUE;
1248 cb.cb_defer_destroy = B_TRUE;
1249 type = ZFS_TYPE_SNAPSHOT;
1252 cb.cb_force = B_TRUE;
1255 cb.cb_recurse = B_TRUE;
1258 cb.cb_recurse = B_TRUE;
1259 cb.cb_doclones = B_TRUE;
1263 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1272 /* check number of arguments */
1274 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1278 (void) fprintf(stderr, gettext("too many arguments\n"));
1282 at = strchr(argv[0], '@');
1283 pound = strchr(argv[0], '#');
1286 /* Build the list of snaps to destroy in cb_nvl. */
1287 cb.cb_nvl = fnvlist_alloc();
1290 zhp = zfs_open(g_zfs, argv[0],
1291 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1295 cb.cb_snapspec = at + 1;
1296 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1302 if (nvlist_empty(cb.cb_nvl)) {
1303 (void) fprintf(stderr, gettext("could not find any "
1304 "snapshots to destroy; check snapshot names.\n"));
1309 if (cb.cb_verbose) {
1311 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1312 if (cb.cb_parsable) {
1313 (void) printf("reclaim\t%llu\n",
1315 } else if (cb.cb_dryrun) {
1316 (void) printf(gettext("would reclaim %s\n"),
1319 (void) printf(gettext("will reclaim %s\n"),
1324 if (!cb.cb_dryrun) {
1325 if (cb.cb_doclones) {
1326 cb.cb_batchedsnaps = fnvlist_alloc();
1327 err = destroy_clones(&cb);
1329 err = zfs_destroy_snaps_nvl(g_zfs,
1330 cb.cb_batchedsnaps, B_FALSE);
1338 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1339 cb.cb_defer_destroy);
1345 } else if (pound != NULL) {
1350 (void) fprintf(stderr,
1351 "dryrun is not supported with bookmark\n");
1355 if (cb.cb_defer_destroy) {
1356 (void) fprintf(stderr,
1357 "defer destroy is not supported with bookmark\n");
1361 if (cb.cb_recurse) {
1362 (void) fprintf(stderr,
1363 "recursive is not supported with bookmark\n");
1367 if (!zfs_bookmark_exists(argv[0])) {
1368 (void) fprintf(stderr, gettext("bookmark '%s' "
1369 "does not exist.\n"), argv[0]);
1373 nvl = fnvlist_alloc();
1374 fnvlist_add_boolean(nvl, argv[0]);
1376 err = lzc_destroy_bookmarks(nvl, NULL);
1378 (void) zfs_standard_error(g_zfs, err,
1379 "cannot destroy bookmark");
1382 nvlist_free(cb.cb_nvl);
1386 /* Open the given dataset */
1387 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1393 * Perform an explicit check for pools before going any further.
1395 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1396 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1397 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1398 "operation does not apply to pools\n"),
1400 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1401 "%s' to destroy all datasets in the pool\n"),
1403 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1404 "to destroy the pool itself\n"), zfs_get_name(zhp));
1410 * Check for any dependents and/or clones.
1412 cb.cb_first = B_TRUE;
1413 if (!cb.cb_doclones &&
1414 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1425 cb.cb_batchedsnaps = fnvlist_alloc();
1426 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1433 * Do the real thing. The callback will close the
1434 * handle regardless of whether it succeeds or not.
1436 err = destroy_callback(zhp, &cb);
1439 err = zfs_destroy_snaps_nvl(g_zfs,
1440 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1447 fnvlist_free(cb.cb_batchedsnaps);
1448 fnvlist_free(cb.cb_nvl);
1455 is_recvd_column(zprop_get_cbdata_t *cbp)
1458 zfs_get_column_t col;
1460 for (i = 0; i < ZFS_GET_NCOLS &&
1461 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1462 if (col == GET_COL_RECVD)
1468 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1469 * < all | property[,property]... > < fs | snap | vol > ...
1471 * -r recurse over any child datasets
1472 * -H scripted mode. Headers are stripped, and fields are separated
1473 * by tabs instead of spaces.
1474 * -o Set of fields to display. One of "name,property,value,
1475 * received,source". Default is "name,property,value,source".
1476 * "all" is an alias for all five.
1477 * -s Set of sources to allow. One of
1478 * "local,default,inherited,received,temporary,none". Default is
1480 * -p Display values in parsable (literal) format.
1482 * Prints properties for the given datasets. The user can control which
1483 * columns to display as well as which property types to allow.
1487 * Invoked to display the properties for a single dataset.
1490 get_callback(zfs_handle_t *zhp, void *data)
1492 char buf[ZFS_MAXPROPLEN];
1493 char rbuf[ZFS_MAXPROPLEN];
1494 zprop_source_t sourcetype;
1495 char source[ZFS_MAXNAMELEN];
1496 zprop_get_cbdata_t *cbp = data;
1497 nvlist_t *user_props = zfs_get_user_props(zhp);
1498 zprop_list_t *pl = cbp->cb_proplist;
1502 boolean_t received = is_recvd_column(cbp);
1504 for (; pl != NULL; pl = pl->pl_next) {
1505 char *recvdval = NULL;
1507 * Skip the special fake placeholder. This will also skip over
1508 * the name property when 'all' is specified.
1510 if (pl->pl_prop == ZFS_PROP_NAME &&
1511 pl == cbp->cb_proplist)
1514 if (pl->pl_prop != ZPROP_INVAL) {
1515 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1516 sizeof (buf), &sourcetype, source,
1518 cbp->cb_literal) != 0) {
1521 if (!zfs_prop_valid_for_type(pl->pl_prop,
1522 ZFS_TYPE_DATASET)) {
1523 (void) fprintf(stderr,
1524 gettext("No such property '%s'\n"),
1525 zfs_prop_to_name(pl->pl_prop));
1528 sourcetype = ZPROP_SRC_NONE;
1529 (void) strlcpy(buf, "-", sizeof (buf));
1532 if (received && (zfs_prop_get_recvd(zhp,
1533 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1534 cbp->cb_literal) == 0))
1537 zprop_print_one_property(zfs_get_name(zhp), cbp,
1538 zfs_prop_to_name(pl->pl_prop),
1539 buf, sourcetype, source, recvdval);
1540 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1541 sourcetype = ZPROP_SRC_LOCAL;
1543 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1544 buf, sizeof (buf), cbp->cb_literal) != 0) {
1545 sourcetype = ZPROP_SRC_NONE;
1546 (void) strlcpy(buf, "-", sizeof (buf));
1549 zprop_print_one_property(zfs_get_name(zhp), cbp,
1550 pl->pl_user_prop, buf, sourcetype, source, NULL);
1551 } else if (zfs_prop_written(pl->pl_user_prop)) {
1552 sourcetype = ZPROP_SRC_LOCAL;
1554 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1555 buf, sizeof (buf), cbp->cb_literal) != 0) {
1556 sourcetype = ZPROP_SRC_NONE;
1557 (void) strlcpy(buf, "-", sizeof (buf));
1560 zprop_print_one_property(zfs_get_name(zhp), cbp,
1561 pl->pl_user_prop, buf, sourcetype, source, NULL);
1563 if (nvlist_lookup_nvlist(user_props,
1564 pl->pl_user_prop, &propval) != 0) {
1567 sourcetype = ZPROP_SRC_NONE;
1570 verify(nvlist_lookup_string(propval,
1571 ZPROP_VALUE, &strval) == 0);
1572 verify(nvlist_lookup_string(propval,
1573 ZPROP_SOURCE, &sourceval) == 0);
1575 if (strcmp(sourceval,
1576 zfs_get_name(zhp)) == 0) {
1577 sourcetype = ZPROP_SRC_LOCAL;
1578 } else if (strcmp(sourceval,
1579 ZPROP_SOURCE_VAL_RECVD) == 0) {
1580 sourcetype = ZPROP_SRC_RECEIVED;
1582 sourcetype = ZPROP_SRC_INHERITED;
1583 (void) strlcpy(source,
1584 sourceval, sizeof (source));
1588 if (received && (zfs_prop_get_recvd(zhp,
1589 pl->pl_user_prop, rbuf, sizeof (rbuf),
1590 cbp->cb_literal) == 0))
1593 zprop_print_one_property(zfs_get_name(zhp), cbp,
1594 pl->pl_user_prop, strval, sourcetype,
1603 zfs_do_get(int argc, char **argv)
1605 zprop_get_cbdata_t cb = { 0 };
1606 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1607 int types = ZFS_TYPE_DATASET;
1608 char *value, *fields;
1611 zprop_list_t fake_name = { 0 };
1614 * Set up default columns and sources.
1616 cb.cb_sources = ZPROP_SRC_ALL;
1617 cb.cb_columns[0] = GET_COL_NAME;
1618 cb.cb_columns[1] = GET_COL_PROPERTY;
1619 cb.cb_columns[2] = GET_COL_VALUE;
1620 cb.cb_columns[3] = GET_COL_SOURCE;
1621 cb.cb_type = ZFS_TYPE_DATASET;
1624 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1627 cb.cb_literal = B_TRUE;
1630 limit = parse_depth(optarg, &flags);
1633 flags |= ZFS_ITER_RECURSE;
1636 cb.cb_scripted = B_TRUE;
1639 (void) fprintf(stderr, gettext("missing argument for "
1640 "'%c' option\n"), optopt);
1645 * Process the set of columns to display. We zero out
1646 * the structure to give us a blank slate.
1648 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1650 while (*optarg != '\0') {
1651 static char *col_subopts[] =
1652 { "name", "property", "value", "received",
1653 "source", "all", NULL };
1655 if (i == ZFS_GET_NCOLS) {
1656 (void) fprintf(stderr, gettext("too "
1657 "many fields given to -o "
1662 switch (getsubopt(&optarg, col_subopts,
1665 cb.cb_columns[i++] = GET_COL_NAME;
1668 cb.cb_columns[i++] = GET_COL_PROPERTY;
1671 cb.cb_columns[i++] = GET_COL_VALUE;
1674 cb.cb_columns[i++] = GET_COL_RECVD;
1675 flags |= ZFS_ITER_RECVD_PROPS;
1678 cb.cb_columns[i++] = GET_COL_SOURCE;
1682 (void) fprintf(stderr,
1683 gettext("\"all\" conflicts "
1684 "with specific fields "
1685 "given to -o option\n"));
1688 cb.cb_columns[0] = GET_COL_NAME;
1689 cb.cb_columns[1] = GET_COL_PROPERTY;
1690 cb.cb_columns[2] = GET_COL_VALUE;
1691 cb.cb_columns[3] = GET_COL_RECVD;
1692 cb.cb_columns[4] = GET_COL_SOURCE;
1693 flags |= ZFS_ITER_RECVD_PROPS;
1697 (void) fprintf(stderr,
1698 gettext("invalid column name "
1707 while (*optarg != '\0') {
1708 static char *source_subopts[] = {
1709 "local", "default", "inherited",
1710 "received", "temporary", "none",
1713 switch (getsubopt(&optarg, source_subopts,
1716 cb.cb_sources |= ZPROP_SRC_LOCAL;
1719 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1722 cb.cb_sources |= ZPROP_SRC_INHERITED;
1725 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1728 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1731 cb.cb_sources |= ZPROP_SRC_NONE;
1734 (void) fprintf(stderr,
1735 gettext("invalid source "
1744 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1745 while (*optarg != '\0') {
1746 static char *type_subopts[] = { "filesystem",
1747 "volume", "snapshot", "bookmark",
1750 switch (getsubopt(&optarg, type_subopts,
1753 types |= ZFS_TYPE_FILESYSTEM;
1756 types |= ZFS_TYPE_VOLUME;
1759 types |= ZFS_TYPE_SNAPSHOT;
1762 types |= ZFS_TYPE_BOOKMARK;
1765 types = ZFS_TYPE_DATASET |
1770 (void) fprintf(stderr,
1771 gettext("invalid type '%s'\n"),
1779 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1789 (void) fprintf(stderr, gettext("missing property "
1796 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1804 * As part of zfs_expand_proplist(), we keep track of the maximum column
1805 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1806 * need to know the maximum name length. However, the user likely did
1807 * not specify 'name' as one of the properties to fetch, so we need to
1808 * make sure we always include at least this property for
1809 * print_get_headers() to work properly.
1811 if (cb.cb_proplist != NULL) {
1812 fake_name.pl_prop = ZFS_PROP_NAME;
1813 fake_name.pl_width = strlen(gettext("NAME"));
1814 fake_name.pl_next = cb.cb_proplist;
1815 cb.cb_proplist = &fake_name;
1818 cb.cb_first = B_TRUE;
1820 /* run for each object */
1821 ret = zfs_for_each(argc, argv, flags, types, NULL,
1822 &cb.cb_proplist, limit, get_callback, &cb);
1824 if (cb.cb_proplist == &fake_name)
1825 zprop_free_list(fake_name.pl_next);
1827 zprop_free_list(cb.cb_proplist);
1833 * inherit [-rS] <property> <fs|vol> ...
1835 * -r Recurse over all children
1836 * -S Revert to received value, if any
1838 * For each dataset specified on the command line, inherit the given property
1839 * from its parent. Inheriting a property at the pool level will cause it to
1840 * use the default value. The '-r' flag will recurse over all children, and is
1841 * useful for setting a property on a hierarchy-wide basis, regardless of any
1842 * local modifications for each dataset.
1845 typedef struct inherit_cbdata {
1846 const char *cb_propname;
1847 boolean_t cb_received;
1851 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1853 inherit_cbdata_t *cb = data;
1854 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1857 * If we're doing it recursively, then ignore properties that
1858 * are not valid for this type of dataset.
1860 if (prop != ZPROP_INVAL &&
1861 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1864 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1868 inherit_cb(zfs_handle_t *zhp, void *data)
1870 inherit_cbdata_t *cb = data;
1872 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1876 zfs_do_inherit(int argc, char **argv)
1880 inherit_cbdata_t cb = { 0 };
1884 boolean_t received = B_FALSE;
1887 while ((c = getopt(argc, argv, "rS")) != -1) {
1890 flags |= ZFS_ITER_RECURSE;
1897 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1906 /* check number of arguments */
1908 (void) fprintf(stderr, gettext("missing property argument\n"));
1912 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1920 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1921 if (zfs_prop_readonly(prop)) {
1922 (void) fprintf(stderr, gettext(
1923 "%s property is read-only\n"),
1927 if (!zfs_prop_inheritable(prop) && !received) {
1928 (void) fprintf(stderr, gettext("'%s' property cannot "
1929 "be inherited\n"), propname);
1930 if (prop == ZFS_PROP_QUOTA ||
1931 prop == ZFS_PROP_RESERVATION ||
1932 prop == ZFS_PROP_REFQUOTA ||
1933 prop == ZFS_PROP_REFRESERVATION) {
1934 (void) fprintf(stderr, gettext("use 'zfs set "
1935 "%s=none' to clear\n"), propname);
1936 (void) fprintf(stderr, gettext("use 'zfs "
1937 "inherit -S %s' to revert to received "
1938 "value\n"), propname);
1942 if (received && (prop == ZFS_PROP_VOLSIZE ||
1943 prop == ZFS_PROP_VERSION)) {
1944 (void) fprintf(stderr, gettext("'%s' property cannot "
1945 "be reverted to a received value\n"), propname);
1948 } else if (!zfs_prop_user(propname)) {
1949 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1954 cb.cb_propname = propname;
1955 cb.cb_received = received;
1957 if (flags & ZFS_ITER_RECURSE) {
1958 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1959 NULL, NULL, 0, inherit_recurse_cb, &cb);
1961 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1962 NULL, NULL, 0, inherit_cb, &cb);
1968 typedef struct upgrade_cbdata {
1969 uint64_t cb_numupgraded;
1970 uint64_t cb_numsamegraded;
1971 uint64_t cb_numfailed;
1972 uint64_t cb_version;
1974 boolean_t cb_foundone;
1975 char cb_lastfs[ZFS_MAXNAMELEN];
1979 same_pool(zfs_handle_t *zhp, const char *name)
1981 int len1 = strcspn(name, "/@");
1982 const char *zhname = zfs_get_name(zhp);
1983 int len2 = strcspn(zhname, "/@");
1987 return (strncmp(name, zhname, len1) == 0);
1991 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1993 upgrade_cbdata_t *cb = data;
1994 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1996 /* list if it's old/new */
1997 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1998 (cb->cb_newer && version > ZPL_VERSION)) {
2001 str = gettext("The following filesystems are "
2002 "formatted using a newer software version and\n"
2003 "cannot be accessed on the current system.\n\n");
2005 str = gettext("The following filesystems are "
2006 "out of date, and can be upgraded. After being\n"
2007 "upgraded, these filesystems (and any 'zfs send' "
2008 "streams generated from\n"
2009 "subsequent snapshots) will no longer be "
2010 "accessible by older software versions.\n\n");
2013 if (!cb->cb_foundone) {
2015 (void) printf(gettext("VER FILESYSTEM\n"));
2016 (void) printf(gettext("--- ------------\n"));
2017 cb->cb_foundone = B_TRUE;
2020 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2027 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2029 upgrade_cbdata_t *cb = data;
2030 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2031 int needed_spa_version;
2034 if (zfs_spa_version(zhp, &spa_version) < 0)
2037 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2039 if (needed_spa_version < 0)
2042 if (spa_version < needed_spa_version) {
2044 (void) printf(gettext("%s: can not be "
2045 "upgraded; the pool version needs to first "
2046 "be upgraded\nto version %d\n\n"),
2047 zfs_get_name(zhp), needed_spa_version);
2053 if (version < cb->cb_version) {
2055 (void) snprintf(verstr, sizeof (verstr),
2056 "%llu", cb->cb_version);
2057 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2059 * If they did "zfs upgrade -a", then we could
2060 * be doing ioctls to different pools. We need
2061 * to log this history once to each pool, and bypass
2062 * the normal history logging that happens in main().
2064 (void) zpool_log_history(g_zfs, history_str);
2065 log_history = B_FALSE;
2067 if (zfs_prop_set(zhp, "version", verstr) == 0)
2068 cb->cb_numupgraded++;
2071 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2072 } else if (version > cb->cb_version) {
2073 /* can't downgrade */
2074 (void) printf(gettext("%s: can not be downgraded; "
2075 "it is already at version %u\n"),
2076 zfs_get_name(zhp), version);
2079 cb->cb_numsamegraded++;
2087 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2090 zfs_do_upgrade(int argc, char **argv)
2092 boolean_t all = B_FALSE;
2093 boolean_t showversions = B_FALSE;
2095 upgrade_cbdata_t cb = { 0 };
2097 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2100 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2103 flags |= ZFS_ITER_RECURSE;
2106 showversions = B_TRUE;
2109 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2110 optarg, &cb.cb_version) != 0) {
2111 (void) fprintf(stderr,
2112 gettext("invalid version %s\n"), optarg);
2121 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2130 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2132 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2133 cb.cb_version || argc))
2135 if ((all || argc) && (showversions))
2141 /* Show info on available versions. */
2142 (void) printf(gettext("The following filesystem versions are "
2144 (void) printf(gettext("VER DESCRIPTION\n"));
2145 (void) printf("--- -----------------------------------------"
2146 "---------------\n");
2147 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2148 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2149 (void) printf(gettext(" 3 Case insensitive and filesystem "
2150 "user identifier (FUID)\n"));
2151 (void) printf(gettext(" 4 userquota, groupquota "
2153 (void) printf(gettext(" 5 System attributes\n"));
2154 (void) printf(gettext("\nFor more information on a particular "
2155 "version, including supported releases,\n"));
2156 (void) printf("see the ZFS Administration Guide.\n\n");
2158 } else if (argc || all) {
2159 /* Upgrade filesystems */
2160 if (cb.cb_version == 0)
2161 cb.cb_version = ZPL_VERSION;
2162 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2163 NULL, NULL, 0, upgrade_set_callback, &cb);
2164 (void) printf(gettext("%llu filesystems upgraded\n"),
2166 if (cb.cb_numsamegraded) {
2167 (void) printf(gettext("%llu filesystems already at "
2169 cb.cb_numsamegraded);
2171 if (cb.cb_numfailed != 0)
2174 /* List old-version filesytems */
2176 (void) printf(gettext("This system is currently running "
2177 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2179 flags |= ZFS_ITER_RECURSE;
2180 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2181 NULL, NULL, 0, upgrade_list_callback, &cb);
2183 found = cb.cb_foundone;
2184 cb.cb_foundone = B_FALSE;
2185 cb.cb_newer = B_TRUE;
2187 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2188 NULL, NULL, 0, upgrade_list_callback, &cb);
2190 if (!cb.cb_foundone && !found) {
2191 (void) printf(gettext("All filesystems are "
2192 "formatted with the current version.\n"));
2200 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2201 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2202 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2203 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2205 * -H Scripted mode; elide headers and separate columns by tabs.
2206 * -i Translate SID to POSIX ID.
2207 * -n Print numeric ID instead of user/group name.
2208 * -o Control which fields to display.
2209 * -p Use exact (parsable) numeric output.
2210 * -s Specify sort columns, descending order.
2211 * -S Specify sort columns, ascending order.
2212 * -t Control which object types to display.
2214 * Displays space consumed by, and quotas on, each user in the specified
2215 * filesystem or snapshot.
2218 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2219 enum us_field_types {
2225 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2226 static char *us_field_names[] = { "type", "name", "used", "quota" };
2227 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2229 #define USTYPE_PSX_GRP (1 << 0)
2230 #define USTYPE_PSX_USR (1 << 1)
2231 #define USTYPE_SMB_GRP (1 << 2)
2232 #define USTYPE_SMB_USR (1 << 3)
2233 #define USTYPE_ALL \
2234 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2236 static int us_type_bits[] = {
2243 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2246 typedef struct us_node {
2248 uu_avl_node_t usn_avlnode;
2249 uu_list_node_t usn_listnode;
2252 typedef struct us_cbdata {
2254 uu_avl_pool_t *cb_avl_pool;
2256 boolean_t cb_numname;
2257 boolean_t cb_nicenum;
2258 boolean_t cb_sid2posix;
2259 zfs_userquota_prop_t cb_prop;
2260 zfs_sort_column_t *cb_sortcol;
2261 size_t cb_width[USFIELD_LAST];
2264 static boolean_t us_populated = B_FALSE;
2267 zfs_sort_column_t *si_sortcol;
2268 boolean_t si_numname;
2272 us_field_index(char *field)
2276 for (i = 0; i < USFIELD_LAST; i++) {
2277 if (strcmp(field, us_field_names[i]) == 0)
2285 us_compare(const void *larg, const void *rarg, void *unused)
2287 const us_node_t *l = larg;
2288 const us_node_t *r = rarg;
2289 us_sort_info_t *si = (us_sort_info_t *)unused;
2290 zfs_sort_column_t *sortcol = si->si_sortcol;
2291 boolean_t numname = si->si_numname;
2292 nvlist_t *lnvl = l->usn_nvl;
2293 nvlist_t *rnvl = r->usn_nvl;
2297 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2304 zfs_prop_t prop = sortcol->sc_prop;
2305 const char *propname = NULL;
2306 boolean_t reverse = sortcol->sc_reverse;
2311 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2312 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2314 rc = (rv32 < lv32) ? 1 : -1;
2319 (void) nvlist_lookup_uint64(lnvl, propname,
2321 (void) nvlist_lookup_uint64(rnvl, propname,
2324 rc = (rv64 < lv64) ? 1 : -1;
2326 (void) nvlist_lookup_string(lnvl, propname,
2328 (void) nvlist_lookup_string(rnvl, propname,
2330 rc = strcmp(lvstr, rvstr);
2334 case ZFS_PROP_QUOTA:
2337 if (prop == ZFS_PROP_USED)
2341 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2342 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2344 rc = (rv64 < lv64) ? 1 : -1;
2350 return (reverse ? 1 : -1);
2352 return (reverse ? -1 : 1);
2357 * If entries still seem to be the same, check if they are of the same
2358 * type (smbentity is added only if we are doing SID to POSIX ID
2359 * translation where we can have duplicate type/name combinations).
2361 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2362 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2364 return (lvb < rvb ? -1 : 1);
2369 static inline const char *
2370 us_type2str(unsigned field_type)
2372 switch (field_type) {
2373 case USTYPE_PSX_USR:
2374 return ("POSIX User");
2375 case USTYPE_PSX_GRP:
2376 return ("POSIX Group");
2377 case USTYPE_SMB_USR:
2378 return ("SMB User");
2379 case USTYPE_SMB_GRP:
2380 return ("SMB Group");
2382 return ("Undefined");
2387 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2389 us_cbdata_t *cb = (us_cbdata_t *)arg;
2390 zfs_userquota_prop_t prop = cb->cb_prop;
2395 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2396 uu_avl_t *avl = cb->cb_avl;
2400 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2402 const char *typestr;
2406 int typeidx, nameidx, sizeidx;
2407 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2408 boolean_t smbentity = B_FALSE;
2410 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2412 node = safe_malloc(sizeof (us_node_t));
2413 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2414 node->usn_nvl = props;
2416 if (domain != NULL && domain[0] != '\0') {
2418 char sid[ZFS_MAXNAMELEN + 32];
2422 int flag = IDMAP_REQ_FLG_USE_CACHE;
2427 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2429 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2430 type = USTYPE_SMB_GRP;
2432 err = sid_to_id(sid, B_FALSE, &id);
2435 type = USTYPE_SMB_USR;
2437 err = sid_to_id(sid, B_TRUE, &id);
2444 if (!cb->cb_sid2posix) {
2445 if (type == USTYPE_SMB_USR) {
2446 (void) idmap_getwinnamebyuid(rid, flag,
2449 (void) idmap_getwinnamebygid(rid, flag,
2459 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2461 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2462 type = USTYPE_PSX_GRP;
2463 if (!cb->cb_numname) {
2466 if ((g = getgrgid(rid)) != NULL)
2470 type = USTYPE_PSX_USR;
2471 if (!cb->cb_numname) {
2474 if ((p = getpwuid(rid)) != NULL)
2481 * Make sure that the type/name combination is unique when doing
2482 * SID to POSIX ID translation (hence changing the type from SMB to
2485 if (cb->cb_sid2posix &&
2486 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2489 /* Calculate/update width of TYPE field */
2490 typestr = us_type2str(type);
2491 typelen = strlen(gettext(typestr));
2492 typeidx = us_field_index("type");
2493 if (typelen > cb->cb_width[typeidx])
2494 cb->cb_width[typeidx] = typelen;
2495 if (nvlist_add_uint32(props, "type", type) != 0)
2498 /* Calculate/update width of NAME field */
2499 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2500 if (nvlist_add_uint64(props, "name", rid) != 0)
2502 namelen = snprintf(NULL, 0, "%u", rid);
2504 if (nvlist_add_string(props, "name", name) != 0)
2506 namelen = strlen(name);
2508 nameidx = us_field_index("name");
2509 if (namelen > cb->cb_width[nameidx])
2510 cb->cb_width[nameidx] = namelen;
2513 * Check if this type/name combination is in the list and update it;
2514 * otherwise add new node to the list.
2516 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2517 uu_avl_insert(avl, node, idx);
2522 props = node->usn_nvl;
2525 /* Calculate/update width of USED/QUOTA fields */
2527 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2529 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2530 sizelen = strlen(sizebuf);
2531 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2533 if (!nvlist_exists(props, "quota"))
2534 (void) nvlist_add_uint64(props, "quota", 0);
2537 if (!nvlist_exists(props, "used"))
2538 (void) nvlist_add_uint64(props, "used", 0);
2540 sizeidx = us_field_index(propname);
2541 if (sizelen > cb->cb_width[sizeidx])
2542 cb->cb_width[sizeidx] = sizelen;
2544 if (nvlist_add_uint64(props, propname, space) != 0)
2551 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2552 size_t *width, us_node_t *node)
2554 nvlist_t *nvl = node->usn_nvl;
2555 char valstr[ZFS_MAXNAMELEN];
2556 boolean_t first = B_TRUE;
2562 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2563 if (!(ustype & types))
2566 while ((field = fields[cfield]) != USFIELD_LAST) {
2567 nvpair_t *nvp = NULL;
2571 char *strval = NULL;
2573 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2574 if (strcmp(nvpair_name(nvp),
2575 us_field_names[field]) == 0)
2579 type = nvpair_type(nvp);
2581 case DATA_TYPE_UINT32:
2582 (void) nvpair_value_uint32(nvp, &val32);
2584 case DATA_TYPE_UINT64:
2585 (void) nvpair_value_uint64(nvp, &val64);
2587 case DATA_TYPE_STRING:
2588 (void) nvpair_value_string(nvp, &strval);
2591 (void) fprintf(stderr, "invalid data type\n");
2596 strval = (char *)us_type2str(val32);
2599 if (type == DATA_TYPE_UINT64) {
2600 (void) sprintf(valstr, "%llu", val64);
2606 if (type == DATA_TYPE_UINT64) {
2608 (void) sprintf(valstr, "%llu", val64);
2610 zfs_nicenum(val64, valstr,
2613 if (field == USFIELD_QUOTA &&
2614 strcmp(valstr, "0") == 0)
2624 (void) printf("\t");
2629 (void) printf("%s", strval);
2630 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2631 (void) printf("%-*s", width[field], strval);
2633 (void) printf("%*s", width[field], strval);
2639 (void) printf("\n");
2643 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2644 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2652 boolean_t first = B_TRUE;
2654 while ((field = fields[cfield]) != USFIELD_LAST) {
2655 col = gettext(us_field_hdr[field]);
2656 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2657 (void) printf(first ? "%-*s" : " %-*s",
2660 (void) printf(first ? "%*s" : " %*s",
2666 (void) printf("\n");
2669 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2670 print_us_node(scripted, parsable, fields, types, width, node);
2672 nvlist_free(node->usn_nvl);
2677 zfs_do_userspace(int argc, char **argv)
2680 zfs_userquota_prop_t p;
2682 uu_avl_pool_t *avl_pool;
2684 uu_avl_walk_t *walk;
2686 char deffields[] = "type,name,used,quota";
2687 char *ofield = NULL;
2688 char *tfield = NULL;
2692 boolean_t scripted = B_FALSE;
2693 boolean_t prtnum = B_FALSE;
2694 boolean_t parsable = B_FALSE;
2695 boolean_t sid2posix = B_FALSE;
2698 zfs_sort_column_t *sortcol = NULL;
2699 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2703 uu_list_pool_t *listpool;
2705 uu_avl_index_t idx = 0;
2706 uu_list_index_t idx2 = 0;
2711 if (strcmp(argv[0], "groupspace") == 0)
2712 /* Toggle default group types */
2713 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2715 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2731 if (zfs_add_sort_column(&sortcol, optarg,
2732 c == 's' ? B_FALSE : B_TRUE) != 0) {
2733 (void) fprintf(stderr,
2734 gettext("invalid field '%s'\n"), optarg);
2745 (void) fprintf(stderr, gettext("missing argument for "
2746 "'%c' option\n"), optopt);
2750 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2760 (void) fprintf(stderr, gettext("missing dataset name\n"));
2764 (void) fprintf(stderr, gettext("too many arguments\n"));
2768 /* Use default output fields if not specified using -o */
2772 if ((delim = strchr(ofield, ',')) != NULL)
2774 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2775 (void) fprintf(stderr, gettext("invalid type '%s' "
2776 "for -o option\n"), ofield);
2781 } while (delim != NULL);
2782 fields[cfield] = USFIELD_LAST;
2784 /* Override output types (-t option) */
2785 if (tfield != NULL) {
2789 boolean_t found = B_FALSE;
2791 if ((delim = strchr(tfield, ',')) != NULL)
2793 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2795 if (strcmp(tfield, us_type_names[i]) == 0) {
2797 types |= us_type_bits[i];
2802 (void) fprintf(stderr, gettext("invalid type "
2803 "'%s' for -t option\n"), tfield);
2808 } while (delim != NULL);
2811 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2814 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2815 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2817 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2820 /* Always add default sorting columns */
2821 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2822 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2824 cb.cb_sortcol = sortcol;
2825 cb.cb_numname = prtnum;
2826 cb.cb_nicenum = !parsable;
2827 cb.cb_avl_pool = avl_pool;
2828 cb.cb_avl = avl_tree;
2829 cb.cb_sid2posix = sid2posix;
2831 for (i = 0; i < USFIELD_LAST; i++)
2832 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2834 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2835 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2836 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2837 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2838 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2841 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2846 if ((node = uu_avl_first(avl_tree)) == NULL)
2849 us_populated = B_TRUE;
2851 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2852 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2853 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2854 uu_list_node_init(node, &node->usn_listnode, listpool);
2856 while (node != NULL) {
2858 node = uu_avl_next(avl_tree, node);
2859 uu_avl_remove(avl_tree, rmnode);
2860 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2861 uu_list_insert(list, rmnode, idx2);
2864 for (node = uu_list_first(list); node != NULL;
2865 node = uu_list_next(list, node)) {
2866 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2868 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2869 uu_avl_insert(avl_tree, node, idx);
2872 uu_list_destroy(list);
2873 uu_list_pool_destroy(listpool);
2875 /* Print and free node nvlist memory */
2876 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2879 zfs_free_sort_columns(sortcol);
2881 /* Clean up the AVL tree */
2882 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2885 while ((node = uu_avl_walk_next(walk)) != NULL) {
2886 uu_avl_remove(cb.cb_avl, node);
2890 uu_avl_walk_end(walk);
2891 uu_avl_destroy(avl_tree);
2892 uu_avl_pool_destroy(avl_pool);
2898 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2899 * [-t type[,...]] [filesystem|volume|snapshot] ...
2901 * -H Scripted mode; elide headers and separate columns by tabs.
2902 * -p Display values in parsable (literal) format.
2903 * -r Recurse over all children.
2904 * -d Limit recursion by depth.
2905 * -o Control which fields to display.
2906 * -s Specify sort columns, descending order.
2907 * -S Specify sort columns, ascending order.
2908 * -t Control which object types to display.
2910 * When given no arguments, list all filesystems in the system.
2911 * Otherwise, list the specified datasets, optionally recursing down them if
2912 * '-r' is specified.
2914 typedef struct list_cbdata {
2916 boolean_t cb_literal;
2917 boolean_t cb_scripted;
2918 zprop_list_t *cb_proplist;
2922 * Given a list of columns to display, output appropriate headers for each one.
2925 print_header(list_cbdata_t *cb)
2927 zprop_list_t *pl = cb->cb_proplist;
2928 char headerbuf[ZFS_MAXPROPLEN];
2931 boolean_t first = B_TRUE;
2932 boolean_t right_justify;
2934 for (; pl != NULL; pl = pl->pl_next) {
2941 right_justify = B_FALSE;
2942 if (pl->pl_prop != ZPROP_INVAL) {
2943 header = zfs_prop_column_name(pl->pl_prop);
2944 right_justify = zfs_prop_align_right(pl->pl_prop);
2946 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2947 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2948 headerbuf[i] = '\0';
2952 if (pl->pl_next == NULL && !right_justify)
2953 (void) printf("%s", header);
2954 else if (right_justify)
2955 (void) printf("%*s", pl->pl_width, header);
2957 (void) printf("%-*s", pl->pl_width, header);
2960 (void) printf("\n");
2964 * Given a dataset and a list of fields, print out all the properties according
2965 * to the described layout.
2968 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2970 zprop_list_t *pl = cb->cb_proplist;
2971 boolean_t first = B_TRUE;
2972 char property[ZFS_MAXPROPLEN];
2973 nvlist_t *userprops = zfs_get_user_props(zhp);
2976 boolean_t right_justify;
2978 for (; pl != NULL; pl = pl->pl_next) {
2980 if (cb->cb_scripted)
2981 (void) printf("\t");
2988 if (pl->pl_prop == ZFS_PROP_NAME) {
2989 (void) strlcpy(property, zfs_get_name(zhp),
2992 right_justify = zfs_prop_align_right(pl->pl_prop);
2993 } else if (pl->pl_prop != ZPROP_INVAL) {
2994 if (zfs_prop_get(zhp, pl->pl_prop, property,
2995 sizeof (property), NULL, NULL, 0,
2996 cb->cb_literal) != 0)
3000 right_justify = zfs_prop_align_right(pl->pl_prop);
3001 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3002 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3003 property, sizeof (property), cb->cb_literal) != 0)
3007 right_justify = B_TRUE;
3008 } else if (zfs_prop_written(pl->pl_user_prop)) {
3009 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3010 property, sizeof (property), cb->cb_literal) != 0)
3014 right_justify = B_TRUE;
3016 if (nvlist_lookup_nvlist(userprops,
3017 pl->pl_user_prop, &propval) != 0)
3020 verify(nvlist_lookup_string(propval,
3021 ZPROP_VALUE, &propstr) == 0);
3022 right_justify = B_FALSE;
3026 * If this is being called in scripted mode, or if this is the
3027 * last column and it is left-justified, don't include a width
3030 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3031 (void) printf("%s", propstr);
3032 else if (right_justify)
3033 (void) printf("%*s", pl->pl_width, propstr);
3035 (void) printf("%-*s", pl->pl_width, propstr);
3038 (void) printf("\n");
3042 * Generic callback function to list a dataset or snapshot.
3045 list_callback(zfs_handle_t *zhp, void *data)
3047 list_cbdata_t *cbp = data;
3049 if (cbp->cb_first) {
3050 if (!cbp->cb_scripted)
3052 cbp->cb_first = B_FALSE;
3055 print_dataset(zhp, cbp);
3061 zfs_do_list(int argc, char **argv)
3064 static char default_fields[] =
3065 "name,used,available,referenced,mountpoint";
3066 int types = ZFS_TYPE_DATASET;
3067 boolean_t types_specified = B_FALSE;
3068 char *fields = NULL;
3069 list_cbdata_t cb = { 0 };
3073 zfs_sort_column_t *sortcol = NULL;
3074 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3077 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3083 cb.cb_literal = B_TRUE;
3084 flags |= ZFS_ITER_LITERAL_PROPS;
3087 limit = parse_depth(optarg, &flags);
3090 flags |= ZFS_ITER_RECURSE;
3093 cb.cb_scripted = B_TRUE;
3096 if (zfs_add_sort_column(&sortcol, optarg,
3098 (void) fprintf(stderr,
3099 gettext("invalid property '%s'\n"), optarg);
3104 if (zfs_add_sort_column(&sortcol, optarg,
3106 (void) fprintf(stderr,
3107 gettext("invalid property '%s'\n"), optarg);
3113 types_specified = B_TRUE;
3114 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3115 while (*optarg != '\0') {
3116 static char *type_subopts[] = { "filesystem",
3117 "volume", "snapshot", "snap", "bookmark",
3120 switch (getsubopt(&optarg, type_subopts,
3123 types |= ZFS_TYPE_FILESYSTEM;
3126 types |= ZFS_TYPE_VOLUME;
3130 types |= ZFS_TYPE_SNAPSHOT;
3133 types |= ZFS_TYPE_BOOKMARK;
3136 types = ZFS_TYPE_DATASET |
3140 (void) fprintf(stderr,
3141 gettext("invalid type '%s'\n"),
3148 (void) fprintf(stderr, gettext("missing argument for "
3149 "'%c' option\n"), optopt);
3153 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3163 fields = default_fields;
3166 * If we are only going to list snapshot names and sort by name,
3167 * then we can use faster version.
3169 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3170 flags |= ZFS_ITER_SIMPLE;
3173 * If "-o space" and no types were specified, don't display snapshots.
3175 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3176 types &= ~ZFS_TYPE_SNAPSHOT;
3179 * If the user specifies '-o all', the zprop_get_list() doesn't
3180 * normally include the name of the dataset. For 'zfs list', we always
3181 * want this property to be first.
3183 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3187 cb.cb_first = B_TRUE;
3189 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3190 limit, list_callback, &cb);
3192 zprop_free_list(cb.cb_proplist);
3193 zfs_free_sort_columns(sortcol);
3195 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3196 (void) printf(gettext("no datasets available\n"));
3202 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3203 * zfs rename [-f] -p <fs | vol> <fs | vol>
3204 * zfs rename -r <snap> <snap>
3205 * zfs rename -u [-p] <fs> <fs>
3207 * Renames the given dataset to another of the same type.
3209 * The '-p' flag creates all the non-existing ancestors of the target first.
3213 zfs_do_rename(int argc, char **argv)
3216 renameflags_t flags = { 0 };
3220 boolean_t parents = B_FALSE;
3221 char *snapshot = NULL;
3224 while ((c = getopt(argc, argv, "fpru")) != -1) {
3230 flags.recurse = B_TRUE;
3233 flags.nounmount = B_TRUE;
3236 flags.forceunmount = B_TRUE;
3240 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3249 /* check number of arguments */
3251 (void) fprintf(stderr, gettext("missing source dataset "
3256 (void) fprintf(stderr, gettext("missing target dataset "
3261 (void) fprintf(stderr, gettext("too many arguments\n"));
3265 if (flags.recurse && parents) {
3266 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3271 if (flags.recurse && strchr(argv[0], '@') == 0) {
3272 (void) fprintf(stderr, gettext("source dataset for recursive "
3273 "rename must be a snapshot\n"));
3277 if (flags.nounmount && parents) {
3278 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3283 if (flags.nounmount)
3284 types = ZFS_TYPE_FILESYSTEM;
3286 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3288 types = ZFS_TYPE_DATASET;
3290 if (flags.recurse) {
3292 * When we do recursive rename we are fine when the given
3293 * snapshot for the given dataset doesn't exist - it can
3294 * still exists below.
3297 snapshot = strchr(argv[0], '@');
3298 assert(snapshot != NULL);
3303 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3306 /* If we were asked and the name looks good, try to create ancestors. */
3307 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3308 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3313 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3322 * Promotes the given clone fs to be the parent
3326 zfs_do_promote(int argc, char **argv)
3332 if (argc > 1 && argv[1][0] == '-') {
3333 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3338 /* check number of arguments */
3340 (void) fprintf(stderr, gettext("missing clone filesystem"
3345 (void) fprintf(stderr, gettext("too many arguments\n"));
3349 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3353 ret = (zfs_promote(zhp) != 0);
3361 * zfs rollback [-rRf] <snapshot>
3363 * -r Delete any intervening snapshots before doing rollback
3364 * -R Delete any snapshots and their clones
3365 * -f ignored for backwards compatability
3367 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3368 * since then and making it the active dataset. If more recent snapshots exist,
3369 * the command will complain unless the '-r' flag is given.
3371 typedef struct rollback_cbdata {
3377 boolean_t cb_recurse;
3378 } rollback_cbdata_t;
3381 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3383 rollback_cbdata_t *cbp = data;
3385 if (cbp->cb_first && cbp->cb_recurse) {
3386 (void) fprintf(stderr, gettext("cannot rollback to "
3387 "'%s': clones of previous snapshots exist\n"),
3389 (void) fprintf(stderr, gettext("use '-R' to "
3390 "force deletion of the following clones and "
3396 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3403 * Report any snapshots more recent than the one specified. Used when '-r' is
3404 * not specified. We reuse this same callback for the snapshot dependents - if
3405 * 'cb_dependent' is set, then this is a dependent and we should report it
3406 * without checking the transaction group.
3409 rollback_check(zfs_handle_t *zhp, void *data)
3411 rollback_cbdata_t *cbp = data;
3413 if (cbp->cb_doclones) {
3418 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3419 if (cbp->cb_first && !cbp->cb_recurse) {
3420 (void) fprintf(stderr, gettext("cannot "
3421 "rollback to '%s': more recent snapshots "
3422 "or bookmarks exist\n"),
3424 (void) fprintf(stderr, gettext("use '-r' to "
3425 "force deletion of the following "
3426 "snapshots and bookmarks:\n"));
3431 if (cbp->cb_recurse) {
3432 if (zfs_iter_dependents(zhp, B_TRUE,
3433 rollback_check_dependent, cbp) != 0) {
3438 (void) fprintf(stderr, "%s\n",
3447 zfs_do_rollback(int argc, char **argv)
3451 boolean_t force = B_FALSE;
3452 rollback_cbdata_t cb = { 0 };
3453 zfs_handle_t *zhp, *snap;
3454 char parentname[ZFS_MAXNAMELEN];
3458 while ((c = getopt(argc, argv, "rRf")) != -1) {
3471 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3480 /* check number of arguments */
3482 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3486 (void) fprintf(stderr, gettext("too many arguments\n"));
3490 /* open the snapshot */
3491 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3494 /* open the parent dataset */
3495 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3496 verify((delim = strrchr(parentname, '@')) != NULL);
3498 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3504 * Check for more recent snapshots and/or clones based on the presence
3507 cb.cb_target = argv[0];
3508 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3509 cb.cb_first = B_TRUE;
3511 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3513 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3516 if ((ret = cb.cb_error) != 0)
3520 * Rollback parent to the given snapshot.
3522 ret = zfs_rollback(zhp, snap, force);
3535 * zfs set property=value { fs | snap | vol } ...
3537 * Sets the given property for all datasets specified on the command line.
3539 typedef struct set_cbdata {
3545 set_callback(zfs_handle_t *zhp, void *data)
3547 set_cbdata_t *cbp = data;
3549 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3550 switch (libzfs_errno(g_zfs)) {
3551 case EZFS_MOUNTFAILED:
3552 (void) fprintf(stderr, gettext("property may be set "
3553 "but unable to remount filesystem\n"));
3555 case EZFS_SHARENFSFAILED:
3556 (void) fprintf(stderr, gettext("property may be set "
3557 "but unable to reshare filesystem\n"));
3566 zfs_do_set(int argc, char **argv)
3571 /* check for options */
3572 if (argc > 1 && argv[1][0] == '-') {
3573 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3578 /* check number of arguments */
3580 (void) fprintf(stderr, gettext("missing property=value "
3585 (void) fprintf(stderr, gettext("missing dataset name\n"));
3589 /* validate property=value argument */
3590 cb.cb_propname = argv[1];
3591 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3592 (cb.cb_value[1] == '\0')) {
3593 (void) fprintf(stderr, gettext("missing value in "
3594 "property=value argument\n"));
3598 *cb.cb_value = '\0';
3601 if (*cb.cb_propname == '\0') {
3602 (void) fprintf(stderr,
3603 gettext("missing property in property=value argument\n"));
3607 ret = zfs_for_each(argc - 2, argv + 2, 0,
3608 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3613 typedef struct snap_cbdata {
3615 boolean_t sd_recursive;
3616 const char *sd_snapname;
3620 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3622 snap_cbdata_t *sd = arg;
3627 if (sd->sd_recursive &&
3628 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3633 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3636 fnvlist_add_boolean(sd->sd_nvl, name);
3639 if (sd->sd_recursive)
3640 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3646 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3648 * Creates a snapshot with the given name. While functionally equivalent to
3649 * 'zfs create', it is a separate command to differentiate intent.
3652 zfs_do_snapshot(int argc, char **argv)
3657 snap_cbdata_t sd = { 0 };
3658 boolean_t multiple_snaps = B_FALSE;
3660 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3662 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3666 while ((c = getopt(argc, argv, "ro:")) != -1) {
3669 if (parseprop(props, optarg) != 0)
3673 sd.sd_recursive = B_TRUE;
3674 multiple_snaps = B_TRUE;
3677 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3686 /* check number of arguments */
3688 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3693 multiple_snaps = B_TRUE;
3694 for (; argc > 0; argc--, argv++) {
3698 atp = strchr(argv[0], '@');
3702 sd.sd_snapname = atp + 1;
3703 zhp = zfs_open(g_zfs, argv[0],
3704 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3707 if (zfs_snapshot_cb(zhp, &sd) != 0)
3711 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3712 nvlist_free(sd.sd_nvl);
3714 if (ret != 0 && multiple_snaps)
3715 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3719 nvlist_free(sd.sd_nvl);
3726 * Send a backup stream to stdout.
3729 zfs_do_send(int argc, char **argv)
3731 char *fromname = NULL;
3732 char *toname = NULL;
3733 char *resume_token = NULL;
3736 sendflags_t flags = { 0 };
3738 nvlist_t *dbgnv = NULL;
3739 boolean_t extraverbose = B_FALSE;
3742 while ((c = getopt(argc, argv, ":i:I:RDpvnPLet:")) != -1) {
3753 flags.doall = B_TRUE;
3756 flags.replicate = B_TRUE;
3759 flags.props = B_TRUE;
3762 flags.parsable = B_TRUE;
3763 flags.verbose = B_TRUE;
3767 extraverbose = B_TRUE;
3768 flags.verbose = B_TRUE;
3769 flags.progress = B_TRUE;
3772 flags.dedup = B_TRUE;
3775 flags.dryrun = B_TRUE;
3778 flags.largeblock = B_TRUE;
3781 flags.embed_data = B_TRUE;
3784 resume_token = optarg;
3787 (void) fprintf(stderr, gettext("missing argument for "
3788 "'%c' option\n"), optopt);
3792 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3801 if (resume_token != NULL) {
3802 if (fromname != NULL || flags.replicate || flags.props ||
3804 (void) fprintf(stderr,
3805 gettext("invalid flags combined with -t\n"));
3809 (void) fprintf(stderr, gettext("no additional "
3810 "arguments are permitted with -t\n"));
3815 (void) fprintf(stderr,
3816 gettext("missing snapshot argument\n"));
3820 (void) fprintf(stderr, gettext("too many arguments\n"));
3825 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3826 (void) fprintf(stderr,
3827 gettext("Error: Stream can not be written to a terminal.\n"
3828 "You must redirect standard output.\n"));
3832 if (resume_token != NULL) {
3833 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3838 * Special case sending a filesystem, or from a bookmark.
3840 if (strchr(argv[0], '@') == NULL ||
3841 (fromname && strchr(fromname, '#') != NULL)) {
3842 char frombuf[ZFS_MAXNAMELEN];
3843 enum lzc_send_flags lzc_flags = 0;
3845 if (flags.replicate || flags.doall || flags.props ||
3846 flags.dedup || flags.dryrun || flags.verbose ||
3848 (void) fprintf(stderr,
3850 "Unsupported flag with filesystem or bookmark.\n"));
3854 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3858 if (flags.largeblock)
3859 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3860 if (flags.embed_data)
3861 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3863 if (fromname != NULL &&
3864 (fromname[0] == '#' || fromname[0] == '@')) {
3866 * Incremental source name begins with # or @.
3867 * Default to same fs as target.
3869 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3870 cp = strchr(frombuf, '@');
3873 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3876 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3881 cp = strchr(argv[0], '@');
3884 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3889 * If they specified the full path to the snapshot, chop off
3890 * everything except the short name of the snapshot, but special
3891 * case if they specify the origin.
3893 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3894 char origin[ZFS_MAXNAMELEN];
3897 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3898 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3900 if (strcmp(origin, fromname) == 0) {
3902 flags.fromorigin = B_TRUE;
3905 if (cp != fromname && strcmp(argv[0], fromname)) {
3906 (void) fprintf(stderr,
3907 gettext("incremental source must be "
3908 "in same filesystem\n"));
3912 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3913 (void) fprintf(stderr,
3914 gettext("invalid incremental source\n"));
3920 if (flags.replicate && fromname == NULL)
3921 flags.doall = B_TRUE;
3923 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3924 extraverbose ? &dbgnv : NULL);
3926 if (extraverbose && dbgnv != NULL) {
3928 * dump_nvlist prints to stdout, but that's been
3929 * redirected to a file. Make it print to stderr
3932 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3933 dump_nvlist(dbgnv, 0);
3942 * Restore a backup stream from stdin.
3945 zfs_do_receive(int argc, char **argv)
3948 recvflags_t flags = { 0 };
3949 boolean_t abort_resumable = B_FALSE;
3952 nvpair_t *nvp = NULL;
3954 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3958 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
3961 if (parseprop(props, optarg) != 0)
3965 flags.isprefix = B_TRUE;
3968 flags.isprefix = B_TRUE;
3969 flags.istail = B_TRUE;
3972 flags.dryrun = B_TRUE;
3975 flags.nomount = B_TRUE;
3978 flags.verbose = B_TRUE;
3981 flags.resumable = B_TRUE;
3984 flags.force = B_TRUE;
3987 abort_resumable = B_TRUE;
3990 (void) fprintf(stderr, gettext("missing argument for "
3991 "'%c' option\n"), optopt);
3995 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4004 /* check number of arguments */
4006 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4010 (void) fprintf(stderr, gettext("too many arguments\n"));
4014 while ((nvp = nvlist_next_nvpair(props, nvp))) {
4015 if (strcmp(nvpair_name(nvp), "origin") != 0) {
4016 (void) fprintf(stderr, gettext("invalid option"));
4021 if (abort_resumable) {
4022 if (flags.isprefix || flags.istail || flags.dryrun ||
4023 flags.resumable || flags.nomount) {
4024 (void) fprintf(stderr, gettext("invalid option"));
4028 char namebuf[ZFS_MAXNAMELEN];
4029 (void) snprintf(namebuf, sizeof (namebuf),
4030 "%s/%%recv", argv[0]);
4032 if (zfs_dataset_exists(g_zfs, namebuf,
4033 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4034 zfs_handle_t *zhp = zfs_open(g_zfs,
4035 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4038 err = zfs_destroy(zhp, B_FALSE);
4040 zfs_handle_t *zhp = zfs_open(g_zfs,
4041 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4044 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4045 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4046 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4047 (void) fprintf(stderr,
4048 gettext("'%s' does not have any "
4049 "resumable receive state to abort\n"),
4053 err = zfs_destroy(zhp, B_FALSE);
4059 if (isatty(STDIN_FILENO)) {
4060 (void) fprintf(stderr,
4061 gettext("Error: Backup stream can not be read "
4062 "from a terminal.\n"
4063 "You must redirect standard input.\n"));
4066 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4072 * allow/unallow stuff
4074 /* copied from zfs/sys/dsl_deleg.h */
4075 #define ZFS_DELEG_PERM_CREATE "create"
4076 #define ZFS_DELEG_PERM_DESTROY "destroy"
4077 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4078 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4079 #define ZFS_DELEG_PERM_CLONE "clone"
4080 #define ZFS_DELEG_PERM_PROMOTE "promote"
4081 #define ZFS_DELEG_PERM_RENAME "rename"
4082 #define ZFS_DELEG_PERM_MOUNT "mount"
4083 #define ZFS_DELEG_PERM_SHARE "share"
4084 #define ZFS_DELEG_PERM_SEND "send"
4085 #define ZFS_DELEG_PERM_RECEIVE "receive"
4086 #define ZFS_DELEG_PERM_ALLOW "allow"
4087 #define ZFS_DELEG_PERM_USERPROP "userprop"
4088 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4089 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4090 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4091 #define ZFS_DELEG_PERM_USERUSED "userused"
4092 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4093 #define ZFS_DELEG_PERM_HOLD "hold"
4094 #define ZFS_DELEG_PERM_RELEASE "release"
4095 #define ZFS_DELEG_PERM_DIFF "diff"
4096 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4098 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4100 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4101 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4102 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4103 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4104 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4105 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4106 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4107 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4108 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4109 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4110 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4111 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4112 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4113 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4114 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4115 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4116 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4118 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4119 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4120 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4121 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4122 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4123 { NULL, ZFS_DELEG_NOTE_NONE }
4126 /* permission structure */
4127 typedef struct deleg_perm {
4128 zfs_deleg_who_type_t dp_who_type;
4129 const char *dp_name;
4131 boolean_t dp_descend;
4135 typedef struct deleg_perm_node {
4136 deleg_perm_t dpn_perm;
4138 uu_avl_node_t dpn_avl_node;
4139 } deleg_perm_node_t;
4141 typedef struct fs_perm fs_perm_t;
4143 /* permissions set */
4144 typedef struct who_perm {
4145 zfs_deleg_who_type_t who_type;
4146 const char *who_name; /* id */
4147 char who_ug_name[256]; /* user/group name */
4148 fs_perm_t *who_fsperm; /* uplink */
4150 uu_avl_t *who_deleg_perm_avl; /* permissions */
4154 typedef struct who_perm_node {
4155 who_perm_t who_perm;
4156 uu_avl_node_t who_avl_node;
4159 typedef struct fs_perm_set fs_perm_set_t;
4160 /* fs permissions */
4162 const char *fsp_name;
4164 uu_avl_t *fsp_sc_avl; /* sets,create */
4165 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4167 fs_perm_set_t *fsp_set; /* uplink */
4171 typedef struct fs_perm_node {
4172 fs_perm_t fspn_fsperm;
4175 uu_list_node_t fspn_list_node;
4178 /* top level structure */
4179 struct fs_perm_set {
4180 uu_list_pool_t *fsps_list_pool;
4181 uu_list_t *fsps_list; /* list of fs_perms */
4183 uu_avl_pool_t *fsps_named_set_avl_pool;
4184 uu_avl_pool_t *fsps_who_perm_avl_pool;
4185 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4188 static inline const char *
4189 deleg_perm_type(zfs_deleg_note_t note)
4195 case ZFS_DELEG_NOTE_GROUPQUOTA:
4196 case ZFS_DELEG_NOTE_GROUPUSED:
4197 case ZFS_DELEG_NOTE_USERPROP:
4198 case ZFS_DELEG_NOTE_USERQUOTA:
4199 case ZFS_DELEG_NOTE_USERUSED:
4201 return (gettext("other"));
4203 return (gettext("subcommand"));
4208 who_type2weight(zfs_deleg_who_type_t who_type)
4212 case ZFS_DELEG_NAMED_SET_SETS:
4213 case ZFS_DELEG_NAMED_SET:
4216 case ZFS_DELEG_CREATE_SETS:
4217 case ZFS_DELEG_CREATE:
4220 case ZFS_DELEG_USER_SETS:
4221 case ZFS_DELEG_USER:
4224 case ZFS_DELEG_GROUP_SETS:
4225 case ZFS_DELEG_GROUP:
4228 case ZFS_DELEG_EVERYONE_SETS:
4229 case ZFS_DELEG_EVERYONE:
4241 who_perm_compare(const void *larg, const void *rarg, void *unused)
4243 const who_perm_node_t *l = larg;
4244 const who_perm_node_t *r = rarg;
4245 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4246 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4247 int lweight = who_type2weight(ltype);
4248 int rweight = who_type2weight(rtype);
4249 int res = lweight - rweight;
4251 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4252 ZFS_MAX_DELEG_NAME-1);
4264 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4266 const deleg_perm_node_t *l = larg;
4267 const deleg_perm_node_t *r = rarg;
4268 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4269 ZFS_MAX_DELEG_NAME-1);
4281 fs_perm_set_init(fs_perm_set_t *fspset)
4283 bzero(fspset, sizeof (fs_perm_set_t));
4285 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4286 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4287 NULL, UU_DEFAULT)) == NULL)
4289 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4290 UU_DEFAULT)) == NULL)
4293 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4294 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4295 who_perm_node_t, who_avl_node), who_perm_compare,
4296 UU_DEFAULT)) == NULL)
4299 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4300 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4301 who_perm_node_t, who_avl_node), who_perm_compare,
4302 UU_DEFAULT)) == NULL)
4305 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4306 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4307 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4312 static inline void fs_perm_fini(fs_perm_t *);
4313 static inline void who_perm_fini(who_perm_t *);
4316 fs_perm_set_fini(fs_perm_set_t *fspset)
4318 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4320 while (node != NULL) {
4321 fs_perm_node_t *next_node =
4322 uu_list_next(fspset->fsps_list, node);
4323 fs_perm_t *fsperm = &node->fspn_fsperm;
4324 fs_perm_fini(fsperm);
4325 uu_list_remove(fspset->fsps_list, node);
4330 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4331 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4332 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4336 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4339 deleg_perm->dp_who_type = type;
4340 deleg_perm->dp_name = name;
4344 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4345 zfs_deleg_who_type_t type, const char *name)
4347 uu_avl_pool_t *pool;
4348 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4350 bzero(who_perm, sizeof (who_perm_t));
4352 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4353 UU_DEFAULT)) == NULL)
4356 who_perm->who_type = type;
4357 who_perm->who_name = name;
4358 who_perm->who_fsperm = fsperm;
4362 who_perm_fini(who_perm_t *who_perm)
4364 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4366 while (node != NULL) {
4367 deleg_perm_node_t *next_node =
4368 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4370 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4375 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4379 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4381 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4382 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4384 bzero(fsperm, sizeof (fs_perm_t));
4386 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4390 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4394 fsperm->fsp_set = fspset;
4395 fsperm->fsp_name = fsname;
4399 fs_perm_fini(fs_perm_t *fsperm)
4401 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4402 while (node != NULL) {
4403 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4405 who_perm_t *who_perm = &node->who_perm;
4406 who_perm_fini(who_perm);
4407 uu_avl_remove(fsperm->fsp_sc_avl, node);
4412 node = uu_avl_first(fsperm->fsp_uge_avl);
4413 while (node != NULL) {
4414 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4416 who_perm_t *who_perm = &node->who_perm;
4417 who_perm_fini(who_perm);
4418 uu_avl_remove(fsperm->fsp_uge_avl, node);
4423 uu_avl_destroy(fsperm->fsp_sc_avl);
4424 uu_avl_destroy(fsperm->fsp_uge_avl);
4428 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4429 zfs_deleg_who_type_t who_type, const char *name, char locality)
4431 uu_avl_index_t idx = 0;
4433 deleg_perm_node_t *found_node = NULL;
4434 deleg_perm_t *deleg_perm = &node->dpn_perm;
4436 deleg_perm_init(deleg_perm, who_type, name);
4438 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4440 uu_avl_insert(avl, node, idx);
4443 deleg_perm = &node->dpn_perm;
4448 case ZFS_DELEG_LOCAL:
4449 deleg_perm->dp_local = B_TRUE;
4451 case ZFS_DELEG_DESCENDENT:
4452 deleg_perm->dp_descend = B_TRUE;
4457 assert(B_FALSE); /* invalid locality */
4462 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4464 nvpair_t *nvp = NULL;
4465 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4466 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4467 zfs_deleg_who_type_t who_type = who_perm->who_type;
4469 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4470 const char *name = nvpair_name(nvp);
4471 data_type_t type = nvpair_type(nvp);
4472 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4473 deleg_perm_node_t *node =
4474 safe_malloc(sizeof (deleg_perm_node_t));
4476 assert(type == DATA_TYPE_BOOLEAN);
4478 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4479 set_deleg_perm_node(avl, node, who_type, name, locality);
4486 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4488 nvpair_t *nvp = NULL;
4489 fs_perm_set_t *fspset = fsperm->fsp_set;
4491 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4492 nvlist_t *nvl2 = NULL;
4493 const char *name = nvpair_name(nvp);
4494 uu_avl_t *avl = NULL;
4495 uu_avl_pool_t *avl_pool;
4496 zfs_deleg_who_type_t perm_type = name[0];
4497 char perm_locality = name[1];
4498 const char *perm_name = name + 3;
4499 boolean_t is_set = B_TRUE;
4500 who_perm_t *who_perm = NULL;
4502 assert('$' == name[2]);
4504 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4507 switch (perm_type) {
4508 case ZFS_DELEG_CREATE:
4509 case ZFS_DELEG_CREATE_SETS:
4510 case ZFS_DELEG_NAMED_SET:
4511 case ZFS_DELEG_NAMED_SET_SETS:
4512 avl_pool = fspset->fsps_named_set_avl_pool;
4513 avl = fsperm->fsp_sc_avl;
4515 case ZFS_DELEG_USER:
4516 case ZFS_DELEG_USER_SETS:
4517 case ZFS_DELEG_GROUP:
4518 case ZFS_DELEG_GROUP_SETS:
4519 case ZFS_DELEG_EVERYONE:
4520 case ZFS_DELEG_EVERYONE_SETS:
4521 avl_pool = fspset->fsps_who_perm_avl_pool;
4522 avl = fsperm->fsp_uge_avl;
4527 who_perm_node_t *found_node = NULL;
4528 who_perm_node_t *node = safe_malloc(
4529 sizeof (who_perm_node_t));
4530 who_perm = &node->who_perm;
4531 uu_avl_index_t idx = 0;
4533 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4534 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4536 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4538 if (avl == fsperm->fsp_uge_avl) {
4540 struct passwd *p = NULL;
4541 struct group *g = NULL;
4542 const char *nice_name = NULL;
4544 switch (perm_type) {
4545 case ZFS_DELEG_USER_SETS:
4546 case ZFS_DELEG_USER:
4547 rid = atoi(perm_name);
4550 nice_name = p->pw_name;
4552 case ZFS_DELEG_GROUP_SETS:
4553 case ZFS_DELEG_GROUP:
4554 rid = atoi(perm_name);
4557 nice_name = g->gr_name;
4561 if (nice_name != NULL)
4563 node->who_perm.who_ug_name,
4567 uu_avl_insert(avl, node, idx);
4570 who_perm = &node->who_perm;
4574 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4581 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4583 nvpair_t *nvp = NULL;
4584 uu_avl_index_t idx = 0;
4586 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4587 nvlist_t *nvl2 = NULL;
4588 const char *fsname = nvpair_name(nvp);
4589 data_type_t type = nvpair_type(nvp);
4590 fs_perm_t *fsperm = NULL;
4591 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4595 fsperm = &node->fspn_fsperm;
4597 assert(DATA_TYPE_NVLIST == type);
4599 uu_list_node_init(node, &node->fspn_list_node,
4600 fspset->fsps_list_pool);
4602 idx = uu_list_numnodes(fspset->fsps_list);
4603 fs_perm_init(fsperm, fspset, fsname);
4605 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4608 (void) parse_fs_perm(fsperm, nvl2);
4610 uu_list_insert(fspset->fsps_list, node, idx);
4616 static inline const char *
4617 deleg_perm_comment(zfs_deleg_note_t note)
4619 const char *str = "";
4624 case ZFS_DELEG_NOTE_ALLOW:
4625 str = gettext("Must also have the permission that is being"
4626 "\n\t\t\t\tallowed");
4628 case ZFS_DELEG_NOTE_CLONE:
4629 str = gettext("Must also have the 'create' ability and 'mount'"
4630 "\n\t\t\t\tability in the origin file system");
4632 case ZFS_DELEG_NOTE_CREATE:
4633 str = gettext("Must also have the 'mount' ability");
4635 case ZFS_DELEG_NOTE_DESTROY:
4636 str = gettext("Must also have the 'mount' ability");
4638 case ZFS_DELEG_NOTE_DIFF:
4639 str = gettext("Allows lookup of paths within a dataset;"
4640 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4641 "\n\t\t\t\tin order to use zfs diff");
4643 case ZFS_DELEG_NOTE_HOLD:
4644 str = gettext("Allows adding a user hold to a snapshot");
4646 case ZFS_DELEG_NOTE_MOUNT:
4647 str = gettext("Allows mount/umount of ZFS datasets");
4649 case ZFS_DELEG_NOTE_PROMOTE:
4650 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4651 " 'promote' ability in the origin file system");
4653 case ZFS_DELEG_NOTE_RECEIVE:
4654 str = gettext("Must also have the 'mount' and 'create'"
4657 case ZFS_DELEG_NOTE_RELEASE:
4658 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4659 "might destroy the snapshot");
4661 case ZFS_DELEG_NOTE_RENAME:
4662 str = gettext("Must also have the 'mount' and 'create'"
4663 "\n\t\t\t\tability in the new parent");
4665 case ZFS_DELEG_NOTE_ROLLBACK:
4668 case ZFS_DELEG_NOTE_SEND:
4671 case ZFS_DELEG_NOTE_SHARE:
4672 str = gettext("Allows sharing file systems over NFS or SMB"
4673 "\n\t\t\t\tprotocols");
4675 case ZFS_DELEG_NOTE_SNAPSHOT:
4679 * case ZFS_DELEG_NOTE_VSCAN:
4680 * str = gettext("");
4684 case ZFS_DELEG_NOTE_GROUPQUOTA:
4685 str = gettext("Allows accessing any groupquota@... property");
4687 case ZFS_DELEG_NOTE_GROUPUSED:
4688 str = gettext("Allows reading any groupused@... property");
4690 case ZFS_DELEG_NOTE_USERPROP:
4691 str = gettext("Allows changing any user property");
4693 case ZFS_DELEG_NOTE_USERQUOTA:
4694 str = gettext("Allows accessing any userquota@... property");
4696 case ZFS_DELEG_NOTE_USERUSED:
4697 str = gettext("Allows reading any userused@... property");
4715 boolean_t recursive; /* unallow only */
4716 boolean_t prt_usage;
4718 boolean_t prt_perms;
4721 const char *dataset;
4725 prop_cmp(const void *a, const void *b)
4727 const char *str1 = *(const char **)a;
4728 const char *str2 = *(const char **)b;
4729 return (strcmp(str1, str2));
4733 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4735 const char *opt_desc[] = {
4736 "-h", gettext("show this help message and exit"),
4737 "-l", gettext("set permission locally"),
4738 "-d", gettext("set permission for descents"),
4739 "-u", gettext("set permission for user"),
4740 "-g", gettext("set permission for group"),
4741 "-e", gettext("set permission for everyone"),
4742 "-c", gettext("set create time permission"),
4743 "-s", gettext("define permission set"),
4745 "-r", gettext("remove permissions recursively"),
4747 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4748 size_t allow_size = unallow_size - 2;
4749 const char *props[ZFS_NUM_PROPS];
4752 FILE *fp = requested ? stdout : stderr;
4753 zprop_desc_t *pdtbl = zfs_prop_get_table();
4754 const char *fmt = gettext("%-16s %-14s\t%s\n");
4756 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4758 (void) fprintf(fp, gettext("Options:\n"));
4759 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4760 const char *opt = opt_desc[i++];
4761 const char *optdsc = opt_desc[i];
4762 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4765 (void) fprintf(fp, gettext("\nThe following permissions are "
4767 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4769 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4770 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4771 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4772 const char *perm_type = deleg_perm_type(perm_note);
4773 const char *perm_comment = deleg_perm_comment(perm_note);
4774 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4777 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4778 zprop_desc_t *pd = &pdtbl[i];
4779 if (pd->pd_visible != B_TRUE)
4782 if (pd->pd_attr == PROP_READONLY)
4785 props[count++] = pd->pd_name;
4787 props[count] = NULL;
4789 qsort(props, count, sizeof (char *), prop_cmp);
4791 for (i = 0; i < count; i++)
4792 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4795 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4797 exit(requested ? 0 : 2);
4800 static inline const char *
4801 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4804 if (un && argc == expected_argc - 1)
4806 else if (argc == expected_argc)
4807 *permsp = argv[argc - 2];
4809 allow_usage(un, B_FALSE,
4810 gettext("wrong number of parameters\n"));
4812 return (argv[argc - 1]);
4816 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4818 int uge_sum = opts->user + opts->group + opts->everyone;
4819 int csuge_sum = opts->create + opts->set + uge_sum;
4820 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4821 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4824 allow_usage(un, B_FALSE,
4825 gettext("-u, -g, and -e are mutually exclusive\n"));
4827 if (opts->prt_usage)
4828 if (argc == 0 && all_sum == 0)
4829 allow_usage(un, B_TRUE, NULL);
4835 allow_usage(un, B_FALSE,
4836 gettext("invalid options combined with -s\n"));
4838 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4839 if (argv[0][0] != '@')
4840 allow_usage(un, B_FALSE,
4841 gettext("invalid set name: missing '@' prefix\n"));
4842 opts->who = argv[0];
4843 } else if (opts->create) {
4844 if (ldcsuge_sum > 1)
4845 allow_usage(un, B_FALSE,
4846 gettext("invalid options combined with -c\n"));
4847 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4848 } else if (opts->everyone) {
4850 allow_usage(un, B_FALSE,
4851 gettext("invalid options combined with -e\n"));
4852 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4853 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4855 opts->everyone = B_TRUE;
4858 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4859 } else if (argc == 1 && !un) {
4860 opts->prt_perms = B_TRUE;
4861 opts->dataset = argv[argc-1];
4863 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4864 opts->who = argv[0];
4867 if (!opts->local && !opts->descend) {
4868 opts->local = B_TRUE;
4869 opts->descend = B_TRUE;
4874 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4875 const char *who, char *perms, nvlist_t *top_nvl)
4878 char ld[2] = { '\0', '\0' };
4879 char who_buf[ZFS_MAXNAMELEN+32];
4882 nvlist_t *base_nvl = NULL;
4883 nvlist_t *set_nvl = NULL;
4886 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4888 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4892 case ZFS_DELEG_NAMED_SET_SETS:
4893 case ZFS_DELEG_NAMED_SET:
4894 set_type = ZFS_DELEG_NAMED_SET_SETS;
4895 base_type = ZFS_DELEG_NAMED_SET;
4896 ld[0] = ZFS_DELEG_NA;
4898 case ZFS_DELEG_CREATE_SETS:
4899 case ZFS_DELEG_CREATE:
4900 set_type = ZFS_DELEG_CREATE_SETS;
4901 base_type = ZFS_DELEG_CREATE;
4902 ld[0] = ZFS_DELEG_NA;
4904 case ZFS_DELEG_USER_SETS:
4905 case ZFS_DELEG_USER:
4906 set_type = ZFS_DELEG_USER_SETS;
4907 base_type = ZFS_DELEG_USER;
4909 ld[0] = ZFS_DELEG_LOCAL;
4911 ld[1] = ZFS_DELEG_DESCENDENT;
4913 case ZFS_DELEG_GROUP_SETS:
4914 case ZFS_DELEG_GROUP:
4915 set_type = ZFS_DELEG_GROUP_SETS;
4916 base_type = ZFS_DELEG_GROUP;
4918 ld[0] = ZFS_DELEG_LOCAL;
4920 ld[1] = ZFS_DELEG_DESCENDENT;
4922 case ZFS_DELEG_EVERYONE_SETS:
4923 case ZFS_DELEG_EVERYONE:
4924 set_type = ZFS_DELEG_EVERYONE_SETS;
4925 base_type = ZFS_DELEG_EVERYONE;
4927 ld[0] = ZFS_DELEG_LOCAL;
4929 ld[1] = ZFS_DELEG_DESCENDENT;
4932 if (perms != NULL) {
4934 char *end = curr + strlen(perms);
4936 while (curr < end) {
4937 char *delim = strchr(curr, ',');
4948 (void) nvlist_add_boolean(nvl, curr);
4954 for (i = 0; i < 2; i++) {
4955 char locality = ld[i];
4959 if (!nvlist_empty(base_nvl)) {
4961 (void) snprintf(who_buf,
4962 sizeof (who_buf), "%c%c$%s",
4963 base_type, locality, who);
4965 (void) snprintf(who_buf,
4966 sizeof (who_buf), "%c%c$",
4967 base_type, locality);
4969 (void) nvlist_add_nvlist(top_nvl, who_buf,
4974 if (!nvlist_empty(set_nvl)) {
4976 (void) snprintf(who_buf,
4977 sizeof (who_buf), "%c%c$%s",
4978 set_type, locality, who);
4980 (void) snprintf(who_buf,
4981 sizeof (who_buf), "%c%c$",
4982 set_type, locality);
4984 (void) nvlist_add_nvlist(top_nvl, who_buf,
4989 for (i = 0; i < 2; i++) {
4990 char locality = ld[i];
4995 (void) snprintf(who_buf, sizeof (who_buf),
4996 "%c%c$%s", base_type, locality, who);
4998 (void) snprintf(who_buf, sizeof (who_buf),
4999 "%c%c$", base_type, locality);
5000 (void) nvlist_add_boolean(top_nvl, who_buf);
5003 (void) snprintf(who_buf, sizeof (who_buf),
5004 "%c%c$%s", set_type, locality, who);
5006 (void) snprintf(who_buf, sizeof (who_buf),
5007 "%c%c$", set_type, locality);
5008 (void) nvlist_add_boolean(top_nvl, who_buf);
5014 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5016 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5020 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5021 opts->descend, opts->who, opts->perms, *nvlp);
5022 } else if (opts->create) {
5023 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5024 opts->descend, NULL, opts->perms, *nvlp);
5025 } else if (opts->everyone) {
5026 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5027 opts->descend, NULL, opts->perms, *nvlp);
5029 char *curr = opts->who;
5030 char *end = curr + strlen(curr);
5032 while (curr < end) {
5034 zfs_deleg_who_type_t who_type;
5036 char *delim = strchr(curr, ',');
5039 struct passwd *p = NULL;
5040 struct group *g = NULL;
5048 rid = (uid_t)strtol(curr, &endch, 0);
5050 who_type = ZFS_DELEG_USER;
5059 (void) snprintf(errbuf, 256, gettext(
5060 "invalid user %s"), curr);
5061 allow_usage(un, B_TRUE, errbuf);
5063 } else if (opts->group) {
5064 who_type = ZFS_DELEG_GROUP;
5073 (void) snprintf(errbuf, 256, gettext(
5074 "invalid group %s"), curr);
5075 allow_usage(un, B_TRUE, errbuf);
5078 if (*endch != '\0') {
5085 if (*endch != '\0') {
5092 who_type = ZFS_DELEG_USER;
5094 } else if (g != NULL) {
5095 who_type = ZFS_DELEG_GROUP;
5098 (void) snprintf(errbuf, 256, gettext(
5099 "invalid user/group %s"), curr);
5100 allow_usage(un, B_TRUE, errbuf);
5104 (void) sprintf(id, "%u", rid);
5107 store_allow_perm(who_type, opts->local,
5108 opts->descend, who, opts->perms, *nvlp);
5117 print_set_creat_perms(uu_avl_t *who_avl)
5119 const char *sc_title[] = {
5120 gettext("Permission sets:\n"),
5121 gettext("Create time permissions:\n"),
5124 const char **title_ptr = sc_title;
5125 who_perm_node_t *who_node = NULL;
5126 int prev_weight = -1;
5128 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5129 who_node = uu_avl_next(who_avl, who_node)) {
5130 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5131 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5132 const char *who_name = who_node->who_perm.who_name;
5133 int weight = who_type2weight(who_type);
5134 boolean_t first = B_TRUE;
5135 deleg_perm_node_t *deleg_node;
5137 if (prev_weight != weight) {
5138 (void) printf(*title_ptr++);
5139 prev_weight = weight;
5142 if (who_name == NULL || strnlen(who_name, 1) == 0)
5143 (void) printf("\t");
5145 (void) printf("\t%s ", who_name);
5147 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5148 deleg_node = uu_avl_next(avl, deleg_node)) {
5151 deleg_node->dpn_perm.dp_name);
5154 (void) printf(",%s",
5155 deleg_node->dpn_perm.dp_name);
5158 (void) printf("\n");
5163 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5166 who_perm_node_t *who_node = NULL;
5167 boolean_t prt_title = B_TRUE;
5168 uu_avl_walk_t *walk;
5170 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5173 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5174 const char *who_name = who_node->who_perm.who_name;
5175 const char *nice_who_name = who_node->who_perm.who_ug_name;
5176 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5177 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5179 deleg_perm_node_t *deleg_node;
5180 boolean_t prt_who = B_TRUE;
5182 for (deleg_node = uu_avl_first(avl);
5184 deleg_node = uu_avl_next(avl, deleg_node)) {
5185 if (local != deleg_node->dpn_perm.dp_local ||
5186 descend != deleg_node->dpn_perm.dp_descend)
5190 const char *who = NULL;
5192 prt_title = B_FALSE;
5193 (void) printf(title);
5197 case ZFS_DELEG_USER_SETS:
5198 case ZFS_DELEG_USER:
5199 who = gettext("user");
5201 who_name = nice_who_name;
5203 case ZFS_DELEG_GROUP_SETS:
5204 case ZFS_DELEG_GROUP:
5205 who = gettext("group");
5207 who_name = nice_who_name;
5209 case ZFS_DELEG_EVERYONE_SETS:
5210 case ZFS_DELEG_EVERYONE:
5211 who = gettext("everyone");
5216 if (who_name == NULL)
5217 (void) printf("\t%s", who);
5219 (void) printf("\t%s %s", who, who_name);
5222 (void) printf("%c%s", delim,
5223 deleg_node->dpn_perm.dp_name);
5228 (void) printf("\n");
5231 uu_avl_walk_end(walk);
5235 print_fs_perms(fs_perm_set_t *fspset)
5237 fs_perm_node_t *node = NULL;
5238 char buf[ZFS_MAXNAMELEN+32];
5239 const char *dsname = buf;
5241 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5242 node = uu_list_next(fspset->fsps_list, node)) {
5243 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5244 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5247 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5248 gettext("---- Permissions on %s "),
5249 node->fspn_fsperm.fsp_name);
5250 (void) printf(dsname);
5251 left = 70 - strlen(buf);
5254 (void) printf("\n");
5256 print_set_creat_perms(sc_avl);
5257 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5258 gettext("Local permissions:\n"));
5259 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5260 gettext("Descendent permissions:\n"));
5261 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5262 gettext("Local+Descendent permissions:\n"));
5266 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5268 struct deleg_perms {
5274 set_deleg_perms(zfs_handle_t *zhp, void *data)
5276 struct deleg_perms *perms = (struct deleg_perms *)data;
5277 zfs_type_t zfs_type = zfs_get_type(zhp);
5279 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5282 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5286 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5289 nvlist_t *perm_nvl = NULL;
5290 nvlist_t *update_perm_nvl = NULL;
5293 struct allow_opts opts = { 0 };
5295 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5298 while ((c = getopt(argc, argv, optstr)) != -1) {
5301 opts.local = B_TRUE;
5304 opts.descend = B_TRUE;
5310 opts.group = B_TRUE;
5313 opts.everyone = B_TRUE;
5319 opts.create = B_TRUE;
5322 opts.recursive = B_TRUE;
5325 (void) fprintf(stderr, gettext("missing argument for "
5326 "'%c' option\n"), optopt);
5330 opts.prt_usage = B_TRUE;
5333 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5342 /* check arguments */
5343 parse_allow_args(argc, argv, un, &opts);
5345 /* try to open the dataset */
5346 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5347 ZFS_TYPE_VOLUME)) == NULL) {
5348 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5353 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5356 fs_perm_set_init(&fs_perm_set);
5357 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5358 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5363 print_fs_perms(&fs_perm_set);
5365 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5366 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5369 if (un && opts.recursive) {
5370 struct deleg_perms data = { un, update_perm_nvl };
5371 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5380 nvlist_free(perm_nvl);
5381 if (update_perm_nvl != NULL)
5382 nvlist_free(update_perm_nvl);
5384 fs_perm_set_fini(&fs_perm_set);
5392 zfs_do_allow(int argc, char **argv)
5394 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5398 zfs_do_unallow(int argc, char **argv)
5400 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5404 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5409 boolean_t recursive = B_FALSE;
5410 const char *opts = holding ? "rt" : "r";
5414 while ((c = getopt(argc, argv, opts)) != -1) {
5420 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5429 /* check number of arguments */
5437 if (holding && tag[0] == '.') {
5438 /* tags starting with '.' are reserved for libzfs */
5439 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5443 for (i = 0; i < argc; ++i) {
5445 char parent[ZFS_MAXNAMELEN];
5447 char *path = argv[i];
5449 delim = strchr(path, '@');
5450 if (delim == NULL) {
5451 (void) fprintf(stderr,
5452 gettext("'%s' is not a snapshot\n"), path);
5456 (void) strncpy(parent, path, delim - path);
5457 parent[delim - path] = '\0';
5459 zhp = zfs_open(g_zfs, parent,
5460 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5466 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5469 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5475 return (errors != 0);
5479 * zfs hold [-r] [-t] <tag> <snap> ...
5481 * -r Recursively hold
5483 * Apply a user-hold with the given tag to the list of snapshots.
5486 zfs_do_hold(int argc, char **argv)
5488 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5492 * zfs release [-r] <tag> <snap> ...
5494 * -r Recursively release
5496 * Release a user-hold with the given tag from the list of snapshots.
5499 zfs_do_release(int argc, char **argv)
5501 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5504 typedef struct holds_cbdata {
5505 boolean_t cb_recursive;
5506 const char *cb_snapname;
5508 size_t cb_max_namelen;
5509 size_t cb_max_taglen;
5512 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5513 #define DATETIME_BUF_LEN (32)
5518 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5521 nvpair_t *nvp = NULL;
5522 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5526 for (i = 0; i < 3; i++) {
5527 col = gettext(hdr_cols[i]);
5529 (void) printf("%-*s ", i ? tagwidth : nwidth,
5532 (void) printf("%s\n", col);
5536 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5537 char *zname = nvpair_name(nvp);
5539 nvpair_t *nvp2 = NULL;
5540 (void) nvpair_value_nvlist(nvp, &nvl2);
5541 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5542 char tsbuf[DATETIME_BUF_LEN];
5543 char *tagname = nvpair_name(nvp2);
5547 char sep = scripted ? '\t' : ' ';
5548 size_t sepnum = scripted ? 1 : 2;
5550 (void) nvpair_value_uint64(nvp2, &val);
5552 (void) localtime_r(&time, &t);
5553 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5554 gettext(STRFTIME_FMT_STR), &t);
5556 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5557 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5563 * Generic callback function to list a dataset or snapshot.
5566 holds_callback(zfs_handle_t *zhp, void *data)
5568 holds_cbdata_t *cbp = data;
5569 nvlist_t *top_nvl = *cbp->cb_nvlp;
5570 nvlist_t *nvl = NULL;
5571 nvpair_t *nvp = NULL;
5572 const char *zname = zfs_get_name(zhp);
5573 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5575 if (cbp->cb_recursive) {
5576 const char *snapname;
5577 char *delim = strchr(zname, '@');
5581 snapname = delim + 1;
5582 if (strcmp(cbp->cb_snapname, snapname))
5586 if (zfs_get_holds(zhp, &nvl) != 0)
5589 if (znamelen > cbp->cb_max_namelen)
5590 cbp->cb_max_namelen = znamelen;
5592 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5593 const char *tag = nvpair_name(nvp);
5594 size_t taglen = strnlen(tag, MAXNAMELEN);
5595 if (taglen > cbp->cb_max_taglen)
5596 cbp->cb_max_taglen = taglen;
5599 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5603 * zfs holds [-r] <snap> ...
5605 * -r Recursively hold
5608 zfs_do_holds(int argc, char **argv)
5613 boolean_t scripted = B_FALSE;
5614 boolean_t recursive = B_FALSE;
5615 const char *opts = "rH";
5618 int types = ZFS_TYPE_SNAPSHOT;
5619 holds_cbdata_t cb = { 0 };
5626 while ((c = getopt(argc, argv, opts)) != -1) {
5635 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5642 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5643 flags |= ZFS_ITER_RECURSE;
5649 /* check number of arguments */
5653 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5656 for (i = 0; i < argc; ++i) {
5657 char *snapshot = argv[i];
5659 const char *snapname;
5661 delim = strchr(snapshot, '@');
5662 if (delim == NULL) {
5663 (void) fprintf(stderr,
5664 gettext("'%s' is not a snapshot\n"), snapshot);
5668 snapname = delim + 1;
5670 snapshot[delim - snapshot] = '\0';
5672 cb.cb_recursive = recursive;
5673 cb.cb_snapname = snapname;
5677 * 1. collect holds data, set format options
5679 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5680 holds_callback, &cb);
5686 * 2. print holds data
5688 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5690 if (nvlist_empty(nvl))
5691 (void) printf(gettext("no datasets available\n"));
5695 return (0 != errors);
5698 #define CHECK_SPINNER 30
5699 #define SPINNER_TIME 3 /* seconds */
5700 #define MOUNT_TIME 5 /* seconds */
5703 get_one_dataset(zfs_handle_t *zhp, void *data)
5705 static char *spin[] = { "-", "\\", "|", "/" };
5706 static int spinval = 0;
5707 static int spincheck = 0;
5708 static time_t last_spin_time = (time_t)0;
5709 get_all_cb_t *cbp = data;
5710 zfs_type_t type = zfs_get_type(zhp);
5712 if (cbp->cb_verbose) {
5713 if (--spincheck < 0) {
5714 time_t now = time(NULL);
5715 if (last_spin_time + SPINNER_TIME < now) {
5716 update_progress(spin[spinval++ % 4]);
5717 last_spin_time = now;
5719 spincheck = CHECK_SPINNER;
5724 * Interate over any nested datasets.
5726 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5732 * Skip any datasets whose type does not match.
5734 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5738 libzfs_add_handle(cbp, zhp);
5739 assert(cbp->cb_used <= cbp->cb_alloc);
5745 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5747 get_all_cb_t cb = { 0 };
5748 cb.cb_verbose = verbose;
5749 cb.cb_getone = get_one_dataset;
5752 set_progress_header(gettext("Reading ZFS config"));
5753 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5755 *dslist = cb.cb_handles;
5756 *count = cb.cb_used;
5759 finish_progress(gettext("done."));
5763 * Generic callback for sharing or mounting filesystems. Because the code is so
5764 * similar, we have a common function with an extra parameter to determine which
5765 * mode we are using.
5767 #define OP_SHARE 0x1
5768 #define OP_MOUNT 0x2
5771 * Share or mount a dataset.
5774 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5775 boolean_t explicit, const char *options)
5777 char mountpoint[ZFS_MAXPROPLEN];
5778 char shareopts[ZFS_MAXPROPLEN];
5779 char smbshareopts[ZFS_MAXPROPLEN];
5780 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5782 uint64_t zoned, canmount;
5783 boolean_t shared_nfs, shared_smb;
5785 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5788 * Check to make sure we can mount/share this dataset. If we
5789 * are in the global zone and the filesystem is exported to a
5790 * local zone, or if we are in a local zone and the
5791 * filesystem is not exported, then it is an error.
5793 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5795 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5799 (void) fprintf(stderr, gettext("cannot %s '%s': "
5800 "dataset is exported to a local zone\n"), cmdname,
5804 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5808 (void) fprintf(stderr, gettext("cannot %s '%s': "
5809 "permission denied\n"), cmdname,
5815 * Ignore any filesystems which don't apply to us. This
5816 * includes those with a legacy mountpoint, or those with
5817 * legacy share options.
5819 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5820 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5821 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5822 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5823 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5824 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5826 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5827 strcmp(smbshareopts, "off") == 0) {
5831 (void) fprintf(stderr, gettext("cannot share '%s': "
5832 "legacy share\n"), zfs_get_name(zhp));
5833 (void) fprintf(stderr, gettext("to "
5834 "share this filesystem set "
5835 "sharenfs property on\n"));
5840 * We cannot share or mount legacy filesystems. If the
5841 * shareopts is non-legacy but the mountpoint is legacy, we
5842 * treat it as a legacy share.
5844 if (strcmp(mountpoint, "legacy") == 0) {
5848 (void) fprintf(stderr, gettext("cannot %s '%s': "
5849 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5850 (void) fprintf(stderr, gettext("use %s(8) to "
5851 "%s this filesystem\n"), cmdname, cmdname);
5855 if (strcmp(mountpoint, "none") == 0) {
5859 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5860 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5865 * canmount explicit outcome
5866 * on no pass through
5867 * on yes pass through
5869 * off yes display error, return 1
5870 * noauto no return 0
5871 * noauto yes pass through
5873 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5874 if (canmount == ZFS_CANMOUNT_OFF) {
5878 (void) fprintf(stderr, gettext("cannot %s '%s': "
5879 "'canmount' property is set to 'off'\n"), cmdname,
5882 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5887 * If this filesystem is inconsistent and has a receive resume
5888 * token, we can not mount it.
5890 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
5891 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
5892 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
5896 (void) fprintf(stderr, gettext("cannot %s '%s': "
5897 "Contains partially-completed state from "
5898 "\"zfs receive -r\", which can be resumed with "
5899 "\"zfs send -t\"\n"),
5900 cmdname, zfs_get_name(zhp));
5905 * At this point, we have verified that the mountpoint and/or
5906 * shareopts are appropriate for auto management. If the
5907 * filesystem is already mounted or shared, return (failing
5908 * for explicit requests); otherwise mount or share the
5914 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5915 shared_smb = zfs_is_shared_smb(zhp, NULL);
5917 if (shared_nfs && shared_smb ||
5918 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5919 strcmp(smbshareopts, "off") == 0) ||
5920 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5921 strcmp(shareopts, "off") == 0)) {
5925 (void) fprintf(stderr, gettext("cannot share "
5926 "'%s': filesystem already shared\n"),
5931 if (!zfs_is_mounted(zhp, NULL) &&
5932 zfs_mount(zhp, NULL, 0) != 0)
5935 if (protocol == NULL) {
5936 if (zfs_shareall(zhp) != 0)
5938 } else if (strcmp(protocol, "nfs") == 0) {
5939 if (zfs_share_nfs(zhp))
5941 } else if (strcmp(protocol, "smb") == 0) {
5942 if (zfs_share_smb(zhp))
5945 (void) fprintf(stderr, gettext("cannot share "
5946 "'%s': invalid share type '%s' "
5948 zfs_get_name(zhp), protocol);
5955 if (options == NULL)
5956 mnt.mnt_mntopts = "";
5958 mnt.mnt_mntopts = (char *)options;
5960 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5961 zfs_is_mounted(zhp, NULL)) {
5965 (void) fprintf(stderr, gettext("cannot mount "
5966 "'%s': filesystem already mounted\n"),
5971 if (zfs_mount(zhp, options, flags) != 0)
5980 * Reports progress in the form "(current/total)". Not thread-safe.
5983 report_mount_progress(int current, int total)
5985 static time_t last_progress_time = 0;
5986 time_t now = time(NULL);
5989 /* report 1..n instead of 0..n-1 */
5992 /* display header if we're here for the first time */
5994 set_progress_header(gettext("Mounting ZFS filesystems"));
5995 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5996 /* too soon to report again */
6000 last_progress_time = now;
6002 (void) sprintf(info, "(%d/%d)", current, total);
6004 if (current == total)
6005 finish_progress(info);
6007 update_progress(info);
6011 append_options(char *mntopts, char *newopts)
6013 int len = strlen(mntopts);
6015 /* original length plus new string to append plus 1 for the comma */
6016 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6017 (void) fprintf(stderr, gettext("the opts argument for "
6018 "'%c' option is too long (more than %d chars)\n"),
6019 "-o", MNT_LINE_MAX);
6024 mntopts[len++] = ',';
6026 (void) strcpy(&mntopts[len], newopts);
6030 share_mount(int op, int argc, char **argv)
6033 boolean_t verbose = B_FALSE;
6035 char *options = NULL;
6039 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6049 if (*optarg == '\0') {
6050 (void) fprintf(stderr, gettext("empty mount "
6051 "options (-o) specified\n"));
6055 if (options == NULL)
6056 options = safe_malloc(MNT_LINE_MAX + 1);
6058 /* option validation is done later */
6059 append_options(options, optarg);
6063 warnx("no overlay mounts support on FreeBSD, ignoring");
6066 (void) fprintf(stderr, gettext("missing argument for "
6067 "'%c' option\n"), optopt);
6071 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6080 /* check number of arguments */
6082 zfs_handle_t **dslist = NULL;
6083 size_t i, count = 0;
6084 char *protocol = NULL;
6086 if (op == OP_SHARE && argc > 0) {
6087 if (strcmp(argv[0], "nfs") != 0 &&
6088 strcmp(argv[0], "smb") != 0) {
6089 (void) fprintf(stderr, gettext("share type "
6090 "must be 'nfs' or 'smb'\n"));
6099 (void) fprintf(stderr, gettext("too many arguments\n"));
6103 start_progress_timer();
6104 get_all_datasets(&dslist, &count, verbose);
6109 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6111 for (i = 0; i < count; i++) {
6113 report_mount_progress(i, count);
6115 if (share_mount_one(dslist[i], op, flags, protocol,
6116 B_FALSE, options) != 0)
6118 zfs_close(dslist[i]);
6122 } else if (argc == 0) {
6123 struct mnttab entry;
6125 if ((op == OP_SHARE) || (options != NULL)) {
6126 (void) fprintf(stderr, gettext("missing filesystem "
6127 "argument (specify -a for all)\n"));
6132 * When mount is given no arguments, go through /etc/mnttab and
6133 * display any active ZFS mounts. We hide any snapshots, since
6134 * they are controlled automatically.
6136 rewind(mnttab_file);
6137 while (getmntent(mnttab_file, &entry) == 0) {
6138 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6139 strchr(entry.mnt_special, '@') != NULL)
6142 (void) printf("%-30s %s\n", entry.mnt_special,
6150 (void) fprintf(stderr,
6151 gettext("too many arguments\n"));
6155 if ((zhp = zfs_open(g_zfs, argv[0],
6156 ZFS_TYPE_FILESYSTEM)) == NULL) {
6159 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6169 * zfs mount -a [nfs]
6170 * zfs mount filesystem
6172 * Mount all filesystems, or mount the given filesystem.
6175 zfs_do_mount(int argc, char **argv)
6177 return (share_mount(OP_MOUNT, argc, argv));
6181 * zfs share -a [nfs | smb]
6182 * zfs share filesystem
6184 * Share all filesystems, or share the given filesystem.
6187 zfs_do_share(int argc, char **argv)
6189 return (share_mount(OP_SHARE, argc, argv));
6192 typedef struct unshare_unmount_node {
6193 zfs_handle_t *un_zhp;
6195 uu_avl_node_t un_avlnode;
6196 } unshare_unmount_node_t;
6200 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6202 const unshare_unmount_node_t *l = larg;
6203 const unshare_unmount_node_t *r = rarg;
6205 return (strcmp(l->un_mountp, r->un_mountp));
6209 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6210 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6211 * and unmount it appropriately.
6214 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6218 struct stat64 statbuf;
6219 struct extmnttab entry;
6220 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6224 * Search for the path in /etc/mnttab. Rather than looking for the
6225 * specific path, which can be fooled by non-standard paths (i.e. ".."
6226 * or "//"), we stat() the path and search for the corresponding
6227 * (major,minor) device pair.
6229 if (stat64(path, &statbuf) != 0) {
6230 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6231 cmdname, path, strerror(errno));
6234 path_inode = statbuf.st_ino;
6237 * Search for the given (major,minor) pair in the mount table.
6240 rewind(mnttab_file);
6241 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6242 if (entry.mnt_major == major(statbuf.st_dev) &&
6243 entry.mnt_minor == minor(statbuf.st_dev))
6250 if (statfs(path, &sfs) != 0) {
6251 (void) fprintf(stderr, "%s: %s\n", path,
6255 statfs2mnttab(&sfs, &entry);
6259 if (op == OP_SHARE) {
6260 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6261 "currently mounted\n"), cmdname, path);
6264 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6266 if ((ret = umount2(path, flags)) != 0)
6267 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6272 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6273 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6274 "filesystem\n"), cmdname, path);
6278 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6279 ZFS_TYPE_FILESYSTEM)) == NULL)
6283 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6284 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6285 cmdname, path, strerror(errno));
6287 } else if (statbuf.st_ino != path_inode) {
6288 (void) fprintf(stderr, gettext("cannot "
6289 "%s '%s': not a mountpoint\n"), cmdname, path);
6293 if (op == OP_SHARE) {
6294 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6295 char smbshare_prop[ZFS_MAXPROPLEN];
6297 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6298 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6299 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6300 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6302 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6303 strcmp(smbshare_prop, "off") == 0) {
6304 (void) fprintf(stderr, gettext("cannot unshare "
6305 "'%s': legacy share\n"), path);
6307 (void) fprintf(stderr, gettext("use "
6308 "unshare(1M) to unshare this filesystem\n"));
6310 } else if (!zfs_is_shared(zhp)) {
6311 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6312 "not currently shared\n"), path);
6314 ret = zfs_unshareall_bypath(zhp, path);
6317 char mtpt_prop[ZFS_MAXPROPLEN];
6319 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6320 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6323 ret = zfs_unmount(zhp, NULL, flags);
6324 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6325 (void) fprintf(stderr, gettext("cannot unmount "
6326 "'%s': legacy mountpoint\n"),
6328 (void) fprintf(stderr, gettext("use umount(8) "
6329 "to unmount this filesystem\n"));
6331 ret = zfs_unmountall(zhp, flags);
6342 * Generic callback for unsharing or unmounting a filesystem.
6345 unshare_unmount(int op, int argc, char **argv)
6352 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6353 char sharesmb[ZFS_MAXPROPLEN];
6356 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6365 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6376 * We could make use of zfs_for_each() to walk all datasets in
6377 * the system, but this would be very inefficient, especially
6378 * since we would have to linearly search /etc/mnttab for each
6379 * one. Instead, do one pass through /etc/mnttab looking for
6380 * zfs entries and call zfs_unmount() for each one.
6382 * Things get a little tricky if the administrator has created
6383 * mountpoints beneath other ZFS filesystems. In this case, we
6384 * have to unmount the deepest filesystems first. To accomplish
6385 * this, we place all the mountpoints in an AVL tree sorted by
6386 * the special type (dataset name), and walk the result in
6387 * reverse to make sure to get any snapshots first.
6389 struct mnttab entry;
6390 uu_avl_pool_t *pool;
6392 unshare_unmount_node_t *node;
6394 uu_avl_walk_t *walk;
6397 (void) fprintf(stderr, gettext("too many arguments\n"));
6401 if (((pool = uu_avl_pool_create("unmount_pool",
6402 sizeof (unshare_unmount_node_t),
6403 offsetof(unshare_unmount_node_t, un_avlnode),
6404 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6405 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6408 rewind(mnttab_file);
6409 while (getmntent(mnttab_file, &entry) == 0) {
6411 /* ignore non-ZFS entries */
6412 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6415 /* ignore snapshots */
6416 if (strchr(entry.mnt_special, '@') != NULL)
6419 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6420 ZFS_TYPE_FILESYSTEM)) == NULL) {
6427 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6429 sizeof (nfs_mnt_prop),
6430 NULL, NULL, 0, B_FALSE) == 0);
6431 if (strcmp(nfs_mnt_prop, "off") != 0)
6433 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6435 sizeof (nfs_mnt_prop),
6436 NULL, NULL, 0, B_FALSE) == 0);
6437 if (strcmp(nfs_mnt_prop, "off") == 0)
6441 /* Ignore legacy mounts */
6442 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6444 sizeof (nfs_mnt_prop),
6445 NULL, NULL, 0, B_FALSE) == 0);
6446 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6448 /* Ignore canmount=noauto mounts */
6449 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6450 ZFS_CANMOUNT_NOAUTO)
6456 node = safe_malloc(sizeof (unshare_unmount_node_t));
6458 node->un_mountp = safe_strdup(entry.mnt_mountp);
6460 uu_avl_node_init(node, &node->un_avlnode, pool);
6462 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6463 uu_avl_insert(tree, node, idx);
6465 zfs_close(node->un_zhp);
6466 free(node->un_mountp);
6472 * Walk the AVL tree in reverse, unmounting each filesystem and
6473 * removing it from the AVL tree in the process.
6475 if ((walk = uu_avl_walk_start(tree,
6476 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6479 while ((node = uu_avl_walk_next(walk)) != NULL) {
6480 uu_avl_remove(tree, node);
6484 if (zfs_unshareall_bypath(node->un_zhp,
6485 node->un_mountp) != 0)
6490 if (zfs_unmount(node->un_zhp,
6491 node->un_mountp, flags) != 0)
6496 zfs_close(node->un_zhp);
6497 free(node->un_mountp);
6501 uu_avl_walk_end(walk);
6502 uu_avl_destroy(tree);
6503 uu_avl_pool_destroy(pool);
6508 (void) fprintf(stderr,
6509 gettext("missing filesystem argument\n"));
6511 (void) fprintf(stderr,
6512 gettext("too many arguments\n"));
6517 * We have an argument, but it may be a full path or a ZFS
6518 * filesystem. Pass full paths off to unmount_path() (shared by
6519 * manual_unmount), otherwise open the filesystem and pass to
6522 if (argv[0][0] == '/')
6523 return (unshare_unmount_path(op, argv[0],
6526 if ((zhp = zfs_open(g_zfs, argv[0],
6527 ZFS_TYPE_FILESYSTEM)) == NULL)
6530 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6531 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6532 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6533 NULL, 0, B_FALSE) == 0);
6537 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6539 sizeof (nfs_mnt_prop),
6540 NULL, NULL, 0, B_FALSE) == 0);
6541 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6542 sharesmb, sizeof (sharesmb), NULL, NULL,
6545 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6546 strcmp(sharesmb, "off") == 0) {
6547 (void) fprintf(stderr, gettext("cannot "
6548 "unshare '%s': legacy share\n"),
6551 (void) fprintf(stderr, gettext("use "
6552 "unshare(1M) to unshare this "
6556 } else if (!zfs_is_shared(zhp)) {
6557 (void) fprintf(stderr, gettext("cannot "
6558 "unshare '%s': not currently "
6559 "shared\n"), zfs_get_name(zhp));
6561 } else if (zfs_unshareall(zhp) != 0) {
6567 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6568 (void) fprintf(stderr, gettext("cannot "
6569 "unmount '%s': legacy "
6570 "mountpoint\n"), zfs_get_name(zhp));
6571 (void) fprintf(stderr, gettext("use "
6572 "umount(8) to unmount this "
6575 } else if (!zfs_is_mounted(zhp, NULL)) {
6576 (void) fprintf(stderr, gettext("cannot "
6577 "unmount '%s': not currently "
6581 } else if (zfs_unmountall(zhp, flags) != 0) {
6595 * zfs unmount filesystem
6597 * Unmount all filesystems, or a specific ZFS filesystem.
6600 zfs_do_unmount(int argc, char **argv)
6602 return (unshare_unmount(OP_MOUNT, argc, argv));
6607 * zfs unshare filesystem
6609 * Unshare all filesystems, or a specific ZFS filesystem.
6612 zfs_do_unshare(int argc, char **argv)
6614 return (unshare_unmount(OP_SHARE, argc, argv));
6618 * Attach/detach the given dataset to/from the given jail
6622 do_jail(int argc, char **argv, int attach)
6627 /* check number of arguments */
6629 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6633 (void) fprintf(stderr, gettext("too many arguments\n"));
6637 jailid = jail_getid(argv[1]);
6639 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6643 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6647 ret = (zfs_jail(zhp, jailid, attach) != 0);
6654 * zfs jail jailid filesystem
6656 * Attach the given dataset to the given jail
6660 zfs_do_jail(int argc, char **argv)
6663 return (do_jail(argc, argv, 1));
6667 * zfs unjail jailid filesystem
6669 * Detach the given dataset from the given jail
6673 zfs_do_unjail(int argc, char **argv)
6676 return (do_jail(argc, argv, 0));
6680 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6681 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6684 manual_mount(int argc, char **argv)
6687 char mountpoint[ZFS_MAXPROPLEN];
6688 char mntopts[MNT_LINE_MAX] = { '\0' };
6692 char *dataset, *path;
6695 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6698 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6701 flags |= MS_OVERLAY;
6704 flags |= MS_NOMNTTAB;
6707 (void) fprintf(stderr, gettext("missing argument for "
6708 "'%c' option\n"), optopt);
6712 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6714 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6723 /* check that we only have two arguments */
6726 (void) fprintf(stderr, gettext("missing dataset "
6729 (void) fprintf(stderr,
6730 gettext("missing mountpoint argument\n"));
6732 (void) fprintf(stderr, gettext("too many arguments\n"));
6733 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6740 /* try to open the dataset */
6741 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6744 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6745 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6747 /* check for legacy mountpoint and complain appropriately */
6749 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6750 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6751 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6752 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6757 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6758 "mounted using 'mount -t zfs'\n"), dataset);
6759 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6760 "instead.\n"), path);
6761 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6762 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6763 (void) fprintf(stderr, gettext("See zfs(8) for more "
6772 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6773 * unmounts of non-legacy filesystems, as this is the dominant administrative
6777 manual_unmount(int argc, char **argv)
6783 while ((c = getopt(argc, argv, "f")) != -1) {
6789 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6791 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6800 /* check arguments */
6803 (void) fprintf(stderr, gettext("missing path "
6806 (void) fprintf(stderr, gettext("too many arguments\n"));
6807 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6811 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6815 find_command_idx(char *command, int *idx)
6819 for (i = 0; i < NCOMMAND; i++) {
6820 if (command_table[i].name == NULL)
6823 if (strcmp(command, command_table[i].name) == 0) {
6832 zfs_do_diff(int argc, char **argv)
6836 char *tosnap = NULL;
6837 char *fromsnap = NULL;
6842 while ((c = getopt(argc, argv, "FHt")) != -1) {
6845 flags |= ZFS_DIFF_CLASSIFY;
6848 flags |= ZFS_DIFF_PARSEABLE;
6851 flags |= ZFS_DIFF_TIMESTAMP;
6854 (void) fprintf(stderr,
6855 gettext("invalid option '%c'\n"), optopt);
6864 (void) fprintf(stderr,
6865 gettext("must provide at least one snapshot name\n"));
6870 (void) fprintf(stderr, gettext("too many arguments\n"));
6875 tosnap = (argc == 2) ? argv[1] : NULL;
6878 if (*fromsnap != '@')
6879 copy = strdup(fromsnap);
6881 copy = strdup(tosnap);
6885 if (atp = strchr(copy, '@'))
6888 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6894 * Ignore SIGPIPE so that the library can give us
6895 * information on any failure
6897 (void) sigignore(SIGPIPE);
6899 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6907 * zfs bookmark <fs@snap> <fs#bmark>
6909 * Creates a bookmark with the given name from the given snapshot.
6912 zfs_do_bookmark(int argc, char **argv)
6914 char snapname[ZFS_MAXNAMELEN];
6921 while ((c = getopt(argc, argv, "")) != -1) {
6924 (void) fprintf(stderr,
6925 gettext("invalid option '%c'\n"), optopt);
6933 /* check number of arguments */
6935 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6939 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6943 if (strchr(argv[1], '#') == NULL) {
6944 (void) fprintf(stderr,
6945 gettext("invalid bookmark name '%s' -- "
6946 "must contain a '#'\n"), argv[1]);
6950 if (argv[0][0] == '@') {
6952 * Snapshot name begins with @.
6953 * Default to same fs as bookmark.
6955 (void) strncpy(snapname, argv[1], sizeof (snapname));
6956 *strchr(snapname, '#') = '\0';
6957 (void) strlcat(snapname, argv[0], sizeof (snapname));
6959 (void) strncpy(snapname, argv[0], sizeof (snapname));
6961 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6967 nvl = fnvlist_alloc();
6968 fnvlist_add_string(nvl, argv[1], snapname);
6969 ret = lzc_bookmark(nvl, NULL);
6973 const char *err_msg;
6976 (void) snprintf(errbuf, sizeof (errbuf),
6977 dgettext(TEXT_DOMAIN,
6978 "cannot create bookmark '%s'"), argv[1]);
6982 err_msg = "bookmark is in a different pool";
6985 err_msg = "bookmark exists";
6988 err_msg = "invalid argument";
6991 err_msg = "bookmark feature not enabled";
6994 err_msg = "out of space";
6997 err_msg = "unknown error";
7000 (void) fprintf(stderr, "%s: %s\n", errbuf,
7001 dgettext(TEXT_DOMAIN, err_msg));
7012 main(int argc, char **argv)
7019 (void) setlocale(LC_ALL, "");
7020 (void) textdomain(TEXT_DOMAIN);
7024 if ((g_zfs = libzfs_init()) == NULL) {
7025 (void) fprintf(stderr, gettext("internal error: failed to "
7026 "initialize ZFS library\n"));
7030 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7032 libzfs_print_on_error(g_zfs, B_TRUE);
7034 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
7035 (void) fprintf(stderr, gettext("internal error: unable to "
7036 "open %s\n"), MNTTAB);
7041 * This command also doubles as the /etc/fs mount and unmount program.
7042 * Determine if we should take this behavior based on argv[0].
7044 progname = basename(argv[0]);
7045 if (strcmp(progname, "mount") == 0) {
7046 ret = manual_mount(argc, argv);
7047 } else if (strcmp(progname, "umount") == 0) {
7048 ret = manual_unmount(argc, argv);
7051 * Make sure the user has specified some command.
7054 (void) fprintf(stderr, gettext("missing command\n"));
7061 * The 'umount' command is an alias for 'unmount'
7063 if (strcmp(cmdname, "umount") == 0)
7064 cmdname = "unmount";
7067 * The 'recv' command is an alias for 'receive'
7069 if (strcmp(cmdname, "recv") == 0)
7070 cmdname = "receive";
7073 * The 'snap' command is an alias for 'snapshot'
7075 if (strcmp(cmdname, "snap") == 0)
7076 cmdname = "snapshot";
7081 if (strcmp(cmdname, "-?") == 0)
7085 * Run the appropriate command.
7087 libzfs_mnttab_cache(g_zfs, B_TRUE);
7088 if (find_command_idx(cmdname, &i) == 0) {
7089 current_command = &command_table[i];
7090 ret = command_table[i].func(argc - 1, argv + 1);
7091 } else if (strchr(cmdname, '=') != NULL) {
7092 verify(find_command_idx("set", &i) == 0);
7093 current_command = &command_table[i];
7094 ret = command_table[i].func(argc, argv);
7096 (void) fprintf(stderr, gettext("unrecognized "
7097 "command '%s'\n"), cmdname);
7100 libzfs_mnttab_cache(g_zfs, B_FALSE);
7103 (void) fclose(mnttab_file);
7105 if (ret == 0 && log_history)
7106 (void) zpool_log_history(g_zfs, history_str);
7111 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7112 * for the purposes of running ::findleaks.
7114 if (getenv("ZFS_ABORT") != NULL) {
7115 (void) printf("dumping core by request\n");