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 * Take a property=value argument string and add it to the given nvlist.
503 * Modifies the argument inplace.
506 parseprop(nvlist_t *props, char *propname)
508 char *propval, *strval;
510 if ((propval = strchr(propname, '=')) == NULL) {
511 (void) fprintf(stderr, gettext("missing "
512 "'=' for property=value argument\n"));
517 if (nvlist_lookup_string(props, propname, &strval) == 0) {
518 (void) fprintf(stderr, gettext("property '%s' "
519 "specified multiple times\n"), propname);
522 if (nvlist_add_string(props, propname, propval) != 0)
528 parse_depth(char *opt, int *flags)
533 depth = (int)strtol(opt, &tmp, 0);
535 (void) fprintf(stderr,
536 gettext("%s is not an integer\n"), opt);
540 (void) fprintf(stderr,
541 gettext("Depth can not be negative.\n"));
544 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
548 #define PROGRESS_DELAY 2 /* seconds */
550 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";
551 static time_t pt_begin;
552 static char *pt_header = NULL;
553 static boolean_t pt_shown;
556 start_progress_timer(void)
558 pt_begin = time(NULL) + PROGRESS_DELAY;
563 set_progress_header(char *header)
565 assert(pt_header == NULL);
566 pt_header = safe_strdup(header);
568 (void) printf("%s: ", header);
569 (void) fflush(stdout);
574 update_progress(char *update)
576 if (!pt_shown && time(NULL) > pt_begin) {
577 int len = strlen(update);
579 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
581 (void) fflush(stdout);
583 } else if (pt_shown) {
584 int len = strlen(update);
586 (void) printf("%s%*.*s", update, len, len, pt_reverse);
587 (void) fflush(stdout);
592 finish_progress(char *done)
595 (void) printf("%s\n", done);
596 (void) fflush(stdout);
603 * Check if the dataset is mountable and should be automatically mounted.
606 should_auto_mount(zfs_handle_t *zhp)
608 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp)))
610 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON);
614 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
616 * Given an existing dataset, create a writable copy whose initial contents
617 * are the same as the source. The newly created dataset maintains a
618 * dependency on the original; the original cannot be destroyed so long as
621 * The '-p' flag creates all the non-existing ancestors of the target first.
624 zfs_do_clone(int argc, char **argv)
626 zfs_handle_t *zhp = NULL;
627 boolean_t parents = B_FALSE;
632 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
636 while ((c = getopt(argc, argv, "o:p")) != -1) {
639 if (parseprop(props, optarg) != 0)
646 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
655 /* check number of arguments */
657 (void) fprintf(stderr, gettext("missing source dataset "
662 (void) fprintf(stderr, gettext("missing target dataset "
667 (void) fprintf(stderr, gettext("too many arguments\n"));
671 /* open the source dataset */
672 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
675 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
678 * Now create the ancestors of the target dataset. If the
679 * target already exists and '-p' option was used we should not
682 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
685 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
690 ret = zfs_clone(zhp, argv[1], props);
692 /* create the mountpoint if necessary */
696 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
699 * If the user doesn't want the dataset
700 * automatically mounted, then skip the mount/share
703 if (should_auto_mount(clone)) {
704 if ((ret = zfs_mount(clone, NULL, 0)) != 0) {
705 (void) fprintf(stderr, gettext("clone "
706 "successfully created, "
707 "but not mounted\n"));
708 } else if ((ret = zfs_share(clone)) != 0) {
709 (void) fprintf(stderr, gettext("clone "
710 "successfully created, "
711 "but not shared\n"));
732 * zfs create [-pu] [-o prop=value] ... fs
733 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
735 * Create a new dataset. This command can be used to create filesystems
736 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
737 * For volumes, the user must specify a size to be used.
739 * The '-s' flag applies only to volumes, and indicates that we should not try
740 * to set the reservation for this volume. By default we set a reservation
741 * equal to the size for any volume. For pools with SPA_VERSION >=
742 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
744 * The '-p' flag creates all the non-existing ancestors of the target first.
746 * The '-u' flag prevents mounting of newly created file system.
749 zfs_do_create(int argc, char **argv)
751 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
752 zfs_handle_t *zhp = NULL;
755 boolean_t noreserve = B_FALSE;
756 boolean_t bflag = B_FALSE;
757 boolean_t parents = B_FALSE;
758 boolean_t nomount = B_FALSE;
763 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
767 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) {
770 type = ZFS_TYPE_VOLUME;
771 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
772 (void) fprintf(stderr, gettext("bad volume "
773 "size '%s': %s\n"), optarg,
774 libzfs_error_description(g_zfs));
778 if (nvlist_add_uint64(props,
779 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
788 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
789 (void) fprintf(stderr, gettext("bad volume "
790 "block size '%s': %s\n"), optarg,
791 libzfs_error_description(g_zfs));
795 if (nvlist_add_uint64(props,
796 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
801 if (parseprop(props, optarg) != 0)
811 (void) fprintf(stderr, gettext("missing size "
815 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
821 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
822 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
823 "used when creating a volume\n"));
826 if (nomount && type != ZFS_TYPE_FILESYSTEM) {
827 (void) fprintf(stderr, gettext("'-u' can only be "
828 "used when creating a file system\n"));
835 /* check number of arguments */
837 (void) fprintf(stderr, gettext("missing %s argument\n"),
838 zfs_type_to_name(type));
842 (void) fprintf(stderr, gettext("too many arguments\n"));
846 if (type == ZFS_TYPE_VOLUME && !noreserve) {
847 zpool_handle_t *zpool_handle;
848 uint64_t spa_version;
850 zfs_prop_t resv_prop;
853 if (p = strchr(argv[0], '/'))
855 zpool_handle = zpool_open(g_zfs, argv[0]);
858 if (zpool_handle == NULL)
860 spa_version = zpool_get_prop_int(zpool_handle,
861 ZPOOL_PROP_VERSION, NULL);
862 zpool_close(zpool_handle);
863 if (spa_version >= SPA_VERSION_REFRESERVATION)
864 resv_prop = ZFS_PROP_REFRESERVATION;
866 resv_prop = ZFS_PROP_RESERVATION;
867 volsize = zvol_volsize_to_reservation(volsize, props);
869 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
871 if (nvlist_add_uint64(props,
872 zfs_prop_to_name(resv_prop), volsize) != 0) {
879 if (parents && zfs_name_valid(argv[0], type)) {
881 * Now create the ancestors of target dataset. If the target
882 * already exists and '-p' option was used we should not
885 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
889 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
894 if (zfs_create(g_zfs, argv[0], type, props) != 0)
897 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
903 * Mount and/or share the new filesystem as appropriate. We provide a
904 * verbose error message to let the user know that their filesystem was
905 * in fact created, even if we failed to mount or share it.
906 * If the user doesn't want the dataset automatically mounted,
907 * then skip the mount/share step altogether.
909 if (!nomount && should_auto_mount(zhp)) {
910 if (zfs_mount(zhp, NULL, 0) != 0) {
911 (void) fprintf(stderr, gettext("filesystem "
912 "successfully created, but not mounted\n"));
914 } else if (zfs_share(zhp) != 0) {
915 (void) fprintf(stderr, gettext("filesystem "
916 "successfully created, but not shared\n"));
933 * zfs destroy [-rRf] <fs, vol>
934 * zfs destroy [-rRd] <snap>
936 * -r Recursively destroy all children
937 * -R Recursively destroy all dependents, including clones
938 * -f Force unmounting of any dependents
939 * -d If we can't destroy now, mark for deferred destruction
941 * Destroys the given dataset. By default, it will unmount any filesystems,
942 * and refuse to destroy a dataset that has any dependents. A dependent can
943 * either be a child, or a clone of a child.
945 typedef struct destroy_cbdata {
948 boolean_t cb_recurse;
950 boolean_t cb_doclones;
951 zfs_handle_t *cb_target;
952 boolean_t cb_defer_destroy;
953 boolean_t cb_verbose;
954 boolean_t cb_parsable;
957 nvlist_t *cb_batchedsnaps;
959 /* first snap in contiguous run */
961 /* previous snap in contiguous run */
969 * Check for any dependents based on the '-r' or '-R' flags.
972 destroy_check_dependent(zfs_handle_t *zhp, void *data)
974 destroy_cbdata_t *cbp = data;
975 const char *tname = zfs_get_name(cbp->cb_target);
976 const char *name = zfs_get_name(zhp);
978 if (strncmp(tname, name, strlen(tname)) == 0 &&
979 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
981 * This is a direct descendant, not a clone somewhere else in
988 (void) fprintf(stderr, gettext("cannot destroy '%s': "
989 "%s has children\n"),
990 zfs_get_name(cbp->cb_target),
991 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
992 (void) fprintf(stderr, gettext("use '-r' to destroy "
993 "the following datasets:\n"));
994 cbp->cb_first = B_FALSE;
995 cbp->cb_error = B_TRUE;
998 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1001 * This is a clone. We only want to report this if the '-r'
1002 * wasn't specified, or the target is a snapshot.
1004 if (!cbp->cb_recurse &&
1005 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1008 if (cbp->cb_first) {
1009 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1010 "%s has dependent clones\n"),
1011 zfs_get_name(cbp->cb_target),
1012 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1013 (void) fprintf(stderr, gettext("use '-R' to destroy "
1014 "the following datasets:\n"));
1015 cbp->cb_first = B_FALSE;
1016 cbp->cb_error = B_TRUE;
1017 cbp->cb_dryrun = B_TRUE;
1020 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1029 destroy_callback(zfs_handle_t *zhp, void *data)
1031 destroy_cbdata_t *cb = data;
1032 const char *name = zfs_get_name(zhp);
1034 if (cb->cb_verbose) {
1035 if (cb->cb_parsable) {
1036 (void) printf("destroy\t%s\n", name);
1037 } else if (cb->cb_dryrun) {
1038 (void) printf(gettext("would destroy %s\n"),
1041 (void) printf(gettext("will destroy %s\n"),
1047 * Ignore pools (which we've already flagged as an error before getting
1050 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1051 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1055 if (cb->cb_dryrun) {
1061 * We batch up all contiguous snapshots (even of different
1062 * filesystems) and destroy them with one ioctl. We can't
1063 * simply do all snap deletions and then all fs deletions,
1064 * because we must delete a clone before its origin.
1066 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1067 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1069 int error = zfs_destroy_snaps_nvl(g_zfs,
1070 cb->cb_batchedsnaps, B_FALSE);
1071 fnvlist_free(cb->cb_batchedsnaps);
1072 cb->cb_batchedsnaps = fnvlist_alloc();
1075 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1076 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1087 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1089 destroy_cbdata_t *cb = arg;
1090 const char *name = zfs_get_name(zhp);
1093 if (nvlist_exists(cb->cb_nvl, name)) {
1094 if (cb->cb_firstsnap == NULL)
1095 cb->cb_firstsnap = strdup(name);
1096 if (cb->cb_prevsnap != NULL)
1097 free(cb->cb_prevsnap);
1098 /* this snap continues the current range */
1099 cb->cb_prevsnap = strdup(name);
1100 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1102 if (cb->cb_verbose) {
1103 if (cb->cb_parsable) {
1104 (void) printf("destroy\t%s\n", name);
1105 } else if (cb->cb_dryrun) {
1106 (void) printf(gettext("would destroy %s\n"),
1109 (void) printf(gettext("will destroy %s\n"),
1113 } else if (cb->cb_firstsnap != NULL) {
1114 /* end of this range */
1116 err = lzc_snaprange_space(cb->cb_firstsnap,
1117 cb->cb_prevsnap, &used);
1118 cb->cb_snapused += used;
1119 free(cb->cb_firstsnap);
1120 cb->cb_firstsnap = NULL;
1121 free(cb->cb_prevsnap);
1122 cb->cb_prevsnap = NULL;
1129 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1132 assert(cb->cb_firstsnap == NULL);
1133 assert(cb->cb_prevsnap == NULL);
1134 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1135 if (cb->cb_firstsnap != NULL) {
1138 err = lzc_snaprange_space(cb->cb_firstsnap,
1139 cb->cb_prevsnap, &used);
1141 cb->cb_snapused += used;
1142 free(cb->cb_firstsnap);
1143 cb->cb_firstsnap = NULL;
1144 free(cb->cb_prevsnap);
1145 cb->cb_prevsnap = NULL;
1151 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1153 destroy_cbdata_t *cb = arg;
1156 /* Check for clones. */
1157 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1158 cb->cb_target = zhp;
1159 cb->cb_first = B_TRUE;
1160 err = zfs_iter_dependents(zhp, B_TRUE,
1161 destroy_check_dependent, cb);
1165 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1173 gather_snapshots(zfs_handle_t *zhp, void *arg)
1175 destroy_cbdata_t *cb = arg;
1178 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1184 if (cb->cb_verbose) {
1185 err = destroy_print_snapshots(zhp, cb);
1191 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1199 destroy_clones(destroy_cbdata_t *cb)
1202 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1204 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1205 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1208 boolean_t defer = cb->cb_defer_destroy;
1212 * We can't defer destroy non-snapshots, so set it to
1213 * false while destroying the clones.
1215 cb->cb_defer_destroy = B_FALSE;
1216 err = zfs_iter_dependents(zhp, B_FALSE,
1217 destroy_callback, cb);
1218 cb->cb_defer_destroy = defer;
1228 zfs_do_destroy(int argc, char **argv)
1230 destroy_cbdata_t cb = { 0 };
1234 zfs_handle_t *zhp = NULL;
1236 zfs_type_t type = ZFS_TYPE_DATASET;
1239 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1242 cb.cb_verbose = B_TRUE;
1245 cb.cb_verbose = B_TRUE;
1246 cb.cb_parsable = B_TRUE;
1249 cb.cb_dryrun = B_TRUE;
1252 cb.cb_defer_destroy = B_TRUE;
1253 type = ZFS_TYPE_SNAPSHOT;
1256 cb.cb_force = B_TRUE;
1259 cb.cb_recurse = B_TRUE;
1262 cb.cb_recurse = B_TRUE;
1263 cb.cb_doclones = B_TRUE;
1267 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1276 /* check number of arguments */
1278 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1282 (void) fprintf(stderr, gettext("too many arguments\n"));
1286 at = strchr(argv[0], '@');
1287 pound = strchr(argv[0], '#');
1290 /* Build the list of snaps to destroy in cb_nvl. */
1291 cb.cb_nvl = fnvlist_alloc();
1294 zhp = zfs_open(g_zfs, argv[0],
1295 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1299 cb.cb_snapspec = at + 1;
1300 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1306 if (nvlist_empty(cb.cb_nvl)) {
1307 (void) fprintf(stderr, gettext("could not find any "
1308 "snapshots to destroy; check snapshot names.\n"));
1313 if (cb.cb_verbose) {
1315 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1316 if (cb.cb_parsable) {
1317 (void) printf("reclaim\t%llu\n",
1319 } else if (cb.cb_dryrun) {
1320 (void) printf(gettext("would reclaim %s\n"),
1323 (void) printf(gettext("will reclaim %s\n"),
1328 if (!cb.cb_dryrun) {
1329 if (cb.cb_doclones) {
1330 cb.cb_batchedsnaps = fnvlist_alloc();
1331 err = destroy_clones(&cb);
1333 err = zfs_destroy_snaps_nvl(g_zfs,
1334 cb.cb_batchedsnaps, B_FALSE);
1342 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1343 cb.cb_defer_destroy);
1349 } else if (pound != NULL) {
1354 (void) fprintf(stderr,
1355 "dryrun is not supported with bookmark\n");
1359 if (cb.cb_defer_destroy) {
1360 (void) fprintf(stderr,
1361 "defer destroy is not supported with bookmark\n");
1365 if (cb.cb_recurse) {
1366 (void) fprintf(stderr,
1367 "recursive is not supported with bookmark\n");
1371 if (!zfs_bookmark_exists(argv[0])) {
1372 (void) fprintf(stderr, gettext("bookmark '%s' "
1373 "does not exist.\n"), argv[0]);
1377 nvl = fnvlist_alloc();
1378 fnvlist_add_boolean(nvl, argv[0]);
1380 err = lzc_destroy_bookmarks(nvl, NULL);
1382 (void) zfs_standard_error(g_zfs, err,
1383 "cannot destroy bookmark");
1386 nvlist_free(cb.cb_nvl);
1390 /* Open the given dataset */
1391 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1397 * Perform an explicit check for pools before going any further.
1399 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1400 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1401 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1402 "operation does not apply to pools\n"),
1404 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1405 "%s' to destroy all datasets in the pool\n"),
1407 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1408 "to destroy the pool itself\n"), zfs_get_name(zhp));
1414 * Check for any dependents and/or clones.
1416 cb.cb_first = B_TRUE;
1417 if (!cb.cb_doclones &&
1418 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1429 cb.cb_batchedsnaps = fnvlist_alloc();
1430 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1437 * Do the real thing. The callback will close the
1438 * handle regardless of whether it succeeds or not.
1440 err = destroy_callback(zhp, &cb);
1443 err = zfs_destroy_snaps_nvl(g_zfs,
1444 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1451 fnvlist_free(cb.cb_batchedsnaps);
1452 fnvlist_free(cb.cb_nvl);
1459 is_recvd_column(zprop_get_cbdata_t *cbp)
1462 zfs_get_column_t col;
1464 for (i = 0; i < ZFS_GET_NCOLS &&
1465 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1466 if (col == GET_COL_RECVD)
1472 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1473 * < all | property[,property]... > < fs | snap | vol > ...
1475 * -r recurse over any child datasets
1476 * -H scripted mode. Headers are stripped, and fields are separated
1477 * by tabs instead of spaces.
1478 * -o Set of fields to display. One of "name,property,value,
1479 * received,source". Default is "name,property,value,source".
1480 * "all" is an alias for all five.
1481 * -s Set of sources to allow. One of
1482 * "local,default,inherited,received,temporary,none". Default is
1484 * -p Display values in parsable (literal) format.
1486 * Prints properties for the given datasets. The user can control which
1487 * columns to display as well as which property types to allow.
1491 * Invoked to display the properties for a single dataset.
1494 get_callback(zfs_handle_t *zhp, void *data)
1496 char buf[ZFS_MAXPROPLEN];
1497 char rbuf[ZFS_MAXPROPLEN];
1498 zprop_source_t sourcetype;
1499 char source[ZFS_MAXNAMELEN];
1500 zprop_get_cbdata_t *cbp = data;
1501 nvlist_t *user_props = zfs_get_user_props(zhp);
1502 zprop_list_t *pl = cbp->cb_proplist;
1506 boolean_t received = is_recvd_column(cbp);
1508 for (; pl != NULL; pl = pl->pl_next) {
1509 char *recvdval = NULL;
1511 * Skip the special fake placeholder. This will also skip over
1512 * the name property when 'all' is specified.
1514 if (pl->pl_prop == ZFS_PROP_NAME &&
1515 pl == cbp->cb_proplist)
1518 if (pl->pl_prop != ZPROP_INVAL) {
1519 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1520 sizeof (buf), &sourcetype, source,
1522 cbp->cb_literal) != 0) {
1525 if (!zfs_prop_valid_for_type(pl->pl_prop,
1526 ZFS_TYPE_DATASET)) {
1527 (void) fprintf(stderr,
1528 gettext("No such property '%s'\n"),
1529 zfs_prop_to_name(pl->pl_prop));
1532 sourcetype = ZPROP_SRC_NONE;
1533 (void) strlcpy(buf, "-", sizeof (buf));
1536 if (received && (zfs_prop_get_recvd(zhp,
1537 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1538 cbp->cb_literal) == 0))
1541 zprop_print_one_property(zfs_get_name(zhp), cbp,
1542 zfs_prop_to_name(pl->pl_prop),
1543 buf, sourcetype, source, recvdval);
1544 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1545 sourcetype = ZPROP_SRC_LOCAL;
1547 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1548 buf, sizeof (buf), cbp->cb_literal) != 0) {
1549 sourcetype = ZPROP_SRC_NONE;
1550 (void) strlcpy(buf, "-", sizeof (buf));
1553 zprop_print_one_property(zfs_get_name(zhp), cbp,
1554 pl->pl_user_prop, buf, sourcetype, source, NULL);
1555 } else if (zfs_prop_written(pl->pl_user_prop)) {
1556 sourcetype = ZPROP_SRC_LOCAL;
1558 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1559 buf, sizeof (buf), cbp->cb_literal) != 0) {
1560 sourcetype = ZPROP_SRC_NONE;
1561 (void) strlcpy(buf, "-", sizeof (buf));
1564 zprop_print_one_property(zfs_get_name(zhp), cbp,
1565 pl->pl_user_prop, buf, sourcetype, source, NULL);
1567 if (nvlist_lookup_nvlist(user_props,
1568 pl->pl_user_prop, &propval) != 0) {
1571 sourcetype = ZPROP_SRC_NONE;
1574 verify(nvlist_lookup_string(propval,
1575 ZPROP_VALUE, &strval) == 0);
1576 verify(nvlist_lookup_string(propval,
1577 ZPROP_SOURCE, &sourceval) == 0);
1579 if (strcmp(sourceval,
1580 zfs_get_name(zhp)) == 0) {
1581 sourcetype = ZPROP_SRC_LOCAL;
1582 } else if (strcmp(sourceval,
1583 ZPROP_SOURCE_VAL_RECVD) == 0) {
1584 sourcetype = ZPROP_SRC_RECEIVED;
1586 sourcetype = ZPROP_SRC_INHERITED;
1587 (void) strlcpy(source,
1588 sourceval, sizeof (source));
1592 if (received && (zfs_prop_get_recvd(zhp,
1593 pl->pl_user_prop, rbuf, sizeof (rbuf),
1594 cbp->cb_literal) == 0))
1597 zprop_print_one_property(zfs_get_name(zhp), cbp,
1598 pl->pl_user_prop, strval, sourcetype,
1607 zfs_do_get(int argc, char **argv)
1609 zprop_get_cbdata_t cb = { 0 };
1610 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1611 int types = ZFS_TYPE_DATASET;
1612 char *value, *fields;
1615 zprop_list_t fake_name = { 0 };
1618 * Set up default columns and sources.
1620 cb.cb_sources = ZPROP_SRC_ALL;
1621 cb.cb_columns[0] = GET_COL_NAME;
1622 cb.cb_columns[1] = GET_COL_PROPERTY;
1623 cb.cb_columns[2] = GET_COL_VALUE;
1624 cb.cb_columns[3] = GET_COL_SOURCE;
1625 cb.cb_type = ZFS_TYPE_DATASET;
1628 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1631 cb.cb_literal = B_TRUE;
1634 limit = parse_depth(optarg, &flags);
1637 flags |= ZFS_ITER_RECURSE;
1640 cb.cb_scripted = B_TRUE;
1643 (void) fprintf(stderr, gettext("missing argument for "
1644 "'%c' option\n"), optopt);
1649 * Process the set of columns to display. We zero out
1650 * the structure to give us a blank slate.
1652 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1654 while (*optarg != '\0') {
1655 static char *col_subopts[] =
1656 { "name", "property", "value", "received",
1657 "source", "all", NULL };
1659 if (i == ZFS_GET_NCOLS) {
1660 (void) fprintf(stderr, gettext("too "
1661 "many fields given to -o "
1666 switch (getsubopt(&optarg, col_subopts,
1669 cb.cb_columns[i++] = GET_COL_NAME;
1672 cb.cb_columns[i++] = GET_COL_PROPERTY;
1675 cb.cb_columns[i++] = GET_COL_VALUE;
1678 cb.cb_columns[i++] = GET_COL_RECVD;
1679 flags |= ZFS_ITER_RECVD_PROPS;
1682 cb.cb_columns[i++] = GET_COL_SOURCE;
1686 (void) fprintf(stderr,
1687 gettext("\"all\" conflicts "
1688 "with specific fields "
1689 "given to -o option\n"));
1692 cb.cb_columns[0] = GET_COL_NAME;
1693 cb.cb_columns[1] = GET_COL_PROPERTY;
1694 cb.cb_columns[2] = GET_COL_VALUE;
1695 cb.cb_columns[3] = GET_COL_RECVD;
1696 cb.cb_columns[4] = GET_COL_SOURCE;
1697 flags |= ZFS_ITER_RECVD_PROPS;
1701 (void) fprintf(stderr,
1702 gettext("invalid column name "
1711 while (*optarg != '\0') {
1712 static char *source_subopts[] = {
1713 "local", "default", "inherited",
1714 "received", "temporary", "none",
1717 switch (getsubopt(&optarg, source_subopts,
1720 cb.cb_sources |= ZPROP_SRC_LOCAL;
1723 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1726 cb.cb_sources |= ZPROP_SRC_INHERITED;
1729 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1732 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1735 cb.cb_sources |= ZPROP_SRC_NONE;
1738 (void) fprintf(stderr,
1739 gettext("invalid source "
1748 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1749 while (*optarg != '\0') {
1750 static char *type_subopts[] = { "filesystem",
1751 "volume", "snapshot", "bookmark",
1754 switch (getsubopt(&optarg, type_subopts,
1757 types |= ZFS_TYPE_FILESYSTEM;
1760 types |= ZFS_TYPE_VOLUME;
1763 types |= ZFS_TYPE_SNAPSHOT;
1766 types |= ZFS_TYPE_BOOKMARK;
1769 types = ZFS_TYPE_DATASET |
1774 (void) fprintf(stderr,
1775 gettext("invalid type '%s'\n"),
1783 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1793 (void) fprintf(stderr, gettext("missing property "
1800 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1808 * As part of zfs_expand_proplist(), we keep track of the maximum column
1809 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1810 * need to know the maximum name length. However, the user likely did
1811 * not specify 'name' as one of the properties to fetch, so we need to
1812 * make sure we always include at least this property for
1813 * print_get_headers() to work properly.
1815 if (cb.cb_proplist != NULL) {
1816 fake_name.pl_prop = ZFS_PROP_NAME;
1817 fake_name.pl_width = strlen(gettext("NAME"));
1818 fake_name.pl_next = cb.cb_proplist;
1819 cb.cb_proplist = &fake_name;
1822 cb.cb_first = B_TRUE;
1824 /* run for each object */
1825 ret = zfs_for_each(argc, argv, flags, types, NULL,
1826 &cb.cb_proplist, limit, get_callback, &cb);
1828 if (cb.cb_proplist == &fake_name)
1829 zprop_free_list(fake_name.pl_next);
1831 zprop_free_list(cb.cb_proplist);
1837 * inherit [-rS] <property> <fs|vol> ...
1839 * -r Recurse over all children
1840 * -S Revert to received value, if any
1842 * For each dataset specified on the command line, inherit the given property
1843 * from its parent. Inheriting a property at the pool level will cause it to
1844 * use the default value. The '-r' flag will recurse over all children, and is
1845 * useful for setting a property on a hierarchy-wide basis, regardless of any
1846 * local modifications for each dataset.
1849 typedef struct inherit_cbdata {
1850 const char *cb_propname;
1851 boolean_t cb_received;
1855 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1857 inherit_cbdata_t *cb = data;
1858 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1861 * If we're doing it recursively, then ignore properties that
1862 * are not valid for this type of dataset.
1864 if (prop != ZPROP_INVAL &&
1865 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1868 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1872 inherit_cb(zfs_handle_t *zhp, void *data)
1874 inherit_cbdata_t *cb = data;
1876 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1880 zfs_do_inherit(int argc, char **argv)
1884 inherit_cbdata_t cb = { 0 };
1888 boolean_t received = B_FALSE;
1891 while ((c = getopt(argc, argv, "rS")) != -1) {
1894 flags |= ZFS_ITER_RECURSE;
1901 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1910 /* check number of arguments */
1912 (void) fprintf(stderr, gettext("missing property argument\n"));
1916 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1924 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1925 if (zfs_prop_readonly(prop)) {
1926 (void) fprintf(stderr, gettext(
1927 "%s property is read-only\n"),
1931 if (!zfs_prop_inheritable(prop) && !received) {
1932 (void) fprintf(stderr, gettext("'%s' property cannot "
1933 "be inherited\n"), propname);
1934 if (prop == ZFS_PROP_QUOTA ||
1935 prop == ZFS_PROP_RESERVATION ||
1936 prop == ZFS_PROP_REFQUOTA ||
1937 prop == ZFS_PROP_REFRESERVATION) {
1938 (void) fprintf(stderr, gettext("use 'zfs set "
1939 "%s=none' to clear\n"), propname);
1940 (void) fprintf(stderr, gettext("use 'zfs "
1941 "inherit -S %s' to revert to received "
1942 "value\n"), propname);
1946 if (received && (prop == ZFS_PROP_VOLSIZE ||
1947 prop == ZFS_PROP_VERSION)) {
1948 (void) fprintf(stderr, gettext("'%s' property cannot "
1949 "be reverted to a received value\n"), propname);
1952 } else if (!zfs_prop_user(propname)) {
1953 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1958 cb.cb_propname = propname;
1959 cb.cb_received = received;
1961 if (flags & ZFS_ITER_RECURSE) {
1962 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1963 NULL, NULL, 0, inherit_recurse_cb, &cb);
1965 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1966 NULL, NULL, 0, inherit_cb, &cb);
1972 typedef struct upgrade_cbdata {
1973 uint64_t cb_numupgraded;
1974 uint64_t cb_numsamegraded;
1975 uint64_t cb_numfailed;
1976 uint64_t cb_version;
1978 boolean_t cb_foundone;
1979 char cb_lastfs[ZFS_MAXNAMELEN];
1983 same_pool(zfs_handle_t *zhp, const char *name)
1985 int len1 = strcspn(name, "/@");
1986 const char *zhname = zfs_get_name(zhp);
1987 int len2 = strcspn(zhname, "/@");
1991 return (strncmp(name, zhname, len1) == 0);
1995 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1997 upgrade_cbdata_t *cb = data;
1998 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2000 /* list if it's old/new */
2001 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2002 (cb->cb_newer && version > ZPL_VERSION)) {
2005 str = gettext("The following filesystems are "
2006 "formatted using a newer software version and\n"
2007 "cannot be accessed on the current system.\n\n");
2009 str = gettext("The following filesystems are "
2010 "out of date, and can be upgraded. After being\n"
2011 "upgraded, these filesystems (and any 'zfs send' "
2012 "streams generated from\n"
2013 "subsequent snapshots) will no longer be "
2014 "accessible by older software versions.\n\n");
2017 if (!cb->cb_foundone) {
2019 (void) printf(gettext("VER FILESYSTEM\n"));
2020 (void) printf(gettext("--- ------------\n"));
2021 cb->cb_foundone = B_TRUE;
2024 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2031 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2033 upgrade_cbdata_t *cb = data;
2034 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2035 int needed_spa_version;
2038 if (zfs_spa_version(zhp, &spa_version) < 0)
2041 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2043 if (needed_spa_version < 0)
2046 if (spa_version < needed_spa_version) {
2048 (void) printf(gettext("%s: can not be "
2049 "upgraded; the pool version needs to first "
2050 "be upgraded\nto version %d\n\n"),
2051 zfs_get_name(zhp), needed_spa_version);
2057 if (version < cb->cb_version) {
2059 (void) snprintf(verstr, sizeof (verstr),
2060 "%llu", cb->cb_version);
2061 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2063 * If they did "zfs upgrade -a", then we could
2064 * be doing ioctls to different pools. We need
2065 * to log this history once to each pool, and bypass
2066 * the normal history logging that happens in main().
2068 (void) zpool_log_history(g_zfs, history_str);
2069 log_history = B_FALSE;
2071 if (zfs_prop_set(zhp, "version", verstr) == 0)
2072 cb->cb_numupgraded++;
2075 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2076 } else if (version > cb->cb_version) {
2077 /* can't downgrade */
2078 (void) printf(gettext("%s: can not be downgraded; "
2079 "it is already at version %u\n"),
2080 zfs_get_name(zhp), version);
2083 cb->cb_numsamegraded++;
2091 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2094 zfs_do_upgrade(int argc, char **argv)
2096 boolean_t all = B_FALSE;
2097 boolean_t showversions = B_FALSE;
2099 upgrade_cbdata_t cb = { 0 };
2101 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2104 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2107 flags |= ZFS_ITER_RECURSE;
2110 showversions = B_TRUE;
2113 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2114 optarg, &cb.cb_version) != 0) {
2115 (void) fprintf(stderr,
2116 gettext("invalid version %s\n"), optarg);
2125 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2134 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2136 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2137 cb.cb_version || argc))
2139 if ((all || argc) && (showversions))
2145 /* Show info on available versions. */
2146 (void) printf(gettext("The following filesystem versions are "
2148 (void) printf(gettext("VER DESCRIPTION\n"));
2149 (void) printf("--- -----------------------------------------"
2150 "---------------\n");
2151 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2152 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2153 (void) printf(gettext(" 3 Case insensitive and filesystem "
2154 "user identifier (FUID)\n"));
2155 (void) printf(gettext(" 4 userquota, groupquota "
2157 (void) printf(gettext(" 5 System attributes\n"));
2158 (void) printf(gettext("\nFor more information on a particular "
2159 "version, including supported releases,\n"));
2160 (void) printf("see the ZFS Administration Guide.\n\n");
2162 } else if (argc || all) {
2163 /* Upgrade filesystems */
2164 if (cb.cb_version == 0)
2165 cb.cb_version = ZPL_VERSION;
2166 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2167 NULL, NULL, 0, upgrade_set_callback, &cb);
2168 (void) printf(gettext("%llu filesystems upgraded\n"),
2170 if (cb.cb_numsamegraded) {
2171 (void) printf(gettext("%llu filesystems already at "
2173 cb.cb_numsamegraded);
2175 if (cb.cb_numfailed != 0)
2178 /* List old-version filesytems */
2180 (void) printf(gettext("This system is currently running "
2181 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2183 flags |= ZFS_ITER_RECURSE;
2184 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2185 NULL, NULL, 0, upgrade_list_callback, &cb);
2187 found = cb.cb_foundone;
2188 cb.cb_foundone = B_FALSE;
2189 cb.cb_newer = B_TRUE;
2191 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2192 NULL, NULL, 0, upgrade_list_callback, &cb);
2194 if (!cb.cb_foundone && !found) {
2195 (void) printf(gettext("All filesystems are "
2196 "formatted with the current version.\n"));
2204 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2205 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2206 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2207 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2209 * -H Scripted mode; elide headers and separate columns by tabs.
2210 * -i Translate SID to POSIX ID.
2211 * -n Print numeric ID instead of user/group name.
2212 * -o Control which fields to display.
2213 * -p Use exact (parsable) numeric output.
2214 * -s Specify sort columns, descending order.
2215 * -S Specify sort columns, ascending order.
2216 * -t Control which object types to display.
2218 * Displays space consumed by, and quotas on, each user in the specified
2219 * filesystem or snapshot.
2222 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2223 enum us_field_types {
2229 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2230 static char *us_field_names[] = { "type", "name", "used", "quota" };
2231 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2233 #define USTYPE_PSX_GRP (1 << 0)
2234 #define USTYPE_PSX_USR (1 << 1)
2235 #define USTYPE_SMB_GRP (1 << 2)
2236 #define USTYPE_SMB_USR (1 << 3)
2237 #define USTYPE_ALL \
2238 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2240 static int us_type_bits[] = {
2247 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2250 typedef struct us_node {
2252 uu_avl_node_t usn_avlnode;
2253 uu_list_node_t usn_listnode;
2256 typedef struct us_cbdata {
2258 uu_avl_pool_t *cb_avl_pool;
2260 boolean_t cb_numname;
2261 boolean_t cb_nicenum;
2262 boolean_t cb_sid2posix;
2263 zfs_userquota_prop_t cb_prop;
2264 zfs_sort_column_t *cb_sortcol;
2265 size_t cb_width[USFIELD_LAST];
2268 static boolean_t us_populated = B_FALSE;
2271 zfs_sort_column_t *si_sortcol;
2272 boolean_t si_numname;
2276 us_field_index(char *field)
2280 for (i = 0; i < USFIELD_LAST; i++) {
2281 if (strcmp(field, us_field_names[i]) == 0)
2289 us_compare(const void *larg, const void *rarg, void *unused)
2291 const us_node_t *l = larg;
2292 const us_node_t *r = rarg;
2293 us_sort_info_t *si = (us_sort_info_t *)unused;
2294 zfs_sort_column_t *sortcol = si->si_sortcol;
2295 boolean_t numname = si->si_numname;
2296 nvlist_t *lnvl = l->usn_nvl;
2297 nvlist_t *rnvl = r->usn_nvl;
2301 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2308 zfs_prop_t prop = sortcol->sc_prop;
2309 const char *propname = NULL;
2310 boolean_t reverse = sortcol->sc_reverse;
2315 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2316 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2318 rc = (rv32 < lv32) ? 1 : -1;
2323 (void) nvlist_lookup_uint64(lnvl, propname,
2325 (void) nvlist_lookup_uint64(rnvl, propname,
2328 rc = (rv64 < lv64) ? 1 : -1;
2330 (void) nvlist_lookup_string(lnvl, propname,
2332 (void) nvlist_lookup_string(rnvl, propname,
2334 rc = strcmp(lvstr, rvstr);
2338 case ZFS_PROP_QUOTA:
2341 if (prop == ZFS_PROP_USED)
2345 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2346 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2348 rc = (rv64 < lv64) ? 1 : -1;
2354 return (reverse ? 1 : -1);
2356 return (reverse ? -1 : 1);
2361 * If entries still seem to be the same, check if they are of the same
2362 * type (smbentity is added only if we are doing SID to POSIX ID
2363 * translation where we can have duplicate type/name combinations).
2365 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2366 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2368 return (lvb < rvb ? -1 : 1);
2373 static inline const char *
2374 us_type2str(unsigned field_type)
2376 switch (field_type) {
2377 case USTYPE_PSX_USR:
2378 return ("POSIX User");
2379 case USTYPE_PSX_GRP:
2380 return ("POSIX Group");
2381 case USTYPE_SMB_USR:
2382 return ("SMB User");
2383 case USTYPE_SMB_GRP:
2384 return ("SMB Group");
2386 return ("Undefined");
2391 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2393 us_cbdata_t *cb = (us_cbdata_t *)arg;
2394 zfs_userquota_prop_t prop = cb->cb_prop;
2399 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2400 uu_avl_t *avl = cb->cb_avl;
2404 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2406 const char *typestr;
2410 int typeidx, nameidx, sizeidx;
2411 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2412 boolean_t smbentity = B_FALSE;
2414 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2416 node = safe_malloc(sizeof (us_node_t));
2417 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2418 node->usn_nvl = props;
2420 if (domain != NULL && domain[0] != '\0') {
2422 char sid[ZFS_MAXNAMELEN + 32];
2426 int flag = IDMAP_REQ_FLG_USE_CACHE;
2431 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2433 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2434 type = USTYPE_SMB_GRP;
2436 err = sid_to_id(sid, B_FALSE, &id);
2439 type = USTYPE_SMB_USR;
2441 err = sid_to_id(sid, B_TRUE, &id);
2448 if (!cb->cb_sid2posix) {
2449 if (type == USTYPE_SMB_USR) {
2450 (void) idmap_getwinnamebyuid(rid, flag,
2453 (void) idmap_getwinnamebygid(rid, flag,
2463 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2465 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2466 type = USTYPE_PSX_GRP;
2467 if (!cb->cb_numname) {
2470 if ((g = getgrgid(rid)) != NULL)
2474 type = USTYPE_PSX_USR;
2475 if (!cb->cb_numname) {
2478 if ((p = getpwuid(rid)) != NULL)
2485 * Make sure that the type/name combination is unique when doing
2486 * SID to POSIX ID translation (hence changing the type from SMB to
2489 if (cb->cb_sid2posix &&
2490 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2493 /* Calculate/update width of TYPE field */
2494 typestr = us_type2str(type);
2495 typelen = strlen(gettext(typestr));
2496 typeidx = us_field_index("type");
2497 if (typelen > cb->cb_width[typeidx])
2498 cb->cb_width[typeidx] = typelen;
2499 if (nvlist_add_uint32(props, "type", type) != 0)
2502 /* Calculate/update width of NAME field */
2503 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2504 if (nvlist_add_uint64(props, "name", rid) != 0)
2506 namelen = snprintf(NULL, 0, "%u", rid);
2508 if (nvlist_add_string(props, "name", name) != 0)
2510 namelen = strlen(name);
2512 nameidx = us_field_index("name");
2513 if (namelen > cb->cb_width[nameidx])
2514 cb->cb_width[nameidx] = namelen;
2517 * Check if this type/name combination is in the list and update it;
2518 * otherwise add new node to the list.
2520 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2521 uu_avl_insert(avl, node, idx);
2526 props = node->usn_nvl;
2529 /* Calculate/update width of USED/QUOTA fields */
2531 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2533 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2534 sizelen = strlen(sizebuf);
2535 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2537 if (!nvlist_exists(props, "quota"))
2538 (void) nvlist_add_uint64(props, "quota", 0);
2541 if (!nvlist_exists(props, "used"))
2542 (void) nvlist_add_uint64(props, "used", 0);
2544 sizeidx = us_field_index(propname);
2545 if (sizelen > cb->cb_width[sizeidx])
2546 cb->cb_width[sizeidx] = sizelen;
2548 if (nvlist_add_uint64(props, propname, space) != 0)
2555 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2556 size_t *width, us_node_t *node)
2558 nvlist_t *nvl = node->usn_nvl;
2559 char valstr[ZFS_MAXNAMELEN];
2560 boolean_t first = B_TRUE;
2566 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2567 if (!(ustype & types))
2570 while ((field = fields[cfield]) != USFIELD_LAST) {
2571 nvpair_t *nvp = NULL;
2575 char *strval = NULL;
2577 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2578 if (strcmp(nvpair_name(nvp),
2579 us_field_names[field]) == 0)
2583 type = nvpair_type(nvp);
2585 case DATA_TYPE_UINT32:
2586 (void) nvpair_value_uint32(nvp, &val32);
2588 case DATA_TYPE_UINT64:
2589 (void) nvpair_value_uint64(nvp, &val64);
2591 case DATA_TYPE_STRING:
2592 (void) nvpair_value_string(nvp, &strval);
2595 (void) fprintf(stderr, "invalid data type\n");
2600 strval = (char *)us_type2str(val32);
2603 if (type == DATA_TYPE_UINT64) {
2604 (void) sprintf(valstr, "%llu", val64);
2610 if (type == DATA_TYPE_UINT64) {
2612 (void) sprintf(valstr, "%llu", val64);
2614 zfs_nicenum(val64, valstr,
2617 if (field == USFIELD_QUOTA &&
2618 strcmp(valstr, "0") == 0)
2628 (void) printf("\t");
2633 (void) printf("%s", strval);
2634 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2635 (void) printf("%-*s", width[field], strval);
2637 (void) printf("%*s", width[field], strval);
2643 (void) printf("\n");
2647 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2648 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2656 boolean_t first = B_TRUE;
2658 while ((field = fields[cfield]) != USFIELD_LAST) {
2659 col = gettext(us_field_hdr[field]);
2660 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2661 (void) printf(first ? "%-*s" : " %-*s",
2664 (void) printf(first ? "%*s" : " %*s",
2670 (void) printf("\n");
2673 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2674 print_us_node(scripted, parsable, fields, types, width, node);
2676 nvlist_free(node->usn_nvl);
2681 zfs_do_userspace(int argc, char **argv)
2684 zfs_userquota_prop_t p;
2686 uu_avl_pool_t *avl_pool;
2688 uu_avl_walk_t *walk;
2690 char deffields[] = "type,name,used,quota";
2691 char *ofield = NULL;
2692 char *tfield = NULL;
2696 boolean_t scripted = B_FALSE;
2697 boolean_t prtnum = B_FALSE;
2698 boolean_t parsable = B_FALSE;
2699 boolean_t sid2posix = B_FALSE;
2702 zfs_sort_column_t *sortcol = NULL;
2703 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2707 uu_list_pool_t *listpool;
2709 uu_avl_index_t idx = 0;
2710 uu_list_index_t idx2 = 0;
2715 if (strcmp(argv[0], "groupspace") == 0)
2716 /* Toggle default group types */
2717 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2719 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2735 if (zfs_add_sort_column(&sortcol, optarg,
2736 c == 's' ? B_FALSE : B_TRUE) != 0) {
2737 (void) fprintf(stderr,
2738 gettext("invalid field '%s'\n"), optarg);
2749 (void) fprintf(stderr, gettext("missing argument for "
2750 "'%c' option\n"), optopt);
2754 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2764 (void) fprintf(stderr, gettext("missing dataset name\n"));
2768 (void) fprintf(stderr, gettext("too many arguments\n"));
2772 /* Use default output fields if not specified using -o */
2776 if ((delim = strchr(ofield, ',')) != NULL)
2778 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2779 (void) fprintf(stderr, gettext("invalid type '%s' "
2780 "for -o option\n"), ofield);
2785 } while (delim != NULL);
2786 fields[cfield] = USFIELD_LAST;
2788 /* Override output types (-t option) */
2789 if (tfield != NULL) {
2793 boolean_t found = B_FALSE;
2795 if ((delim = strchr(tfield, ',')) != NULL)
2797 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2799 if (strcmp(tfield, us_type_names[i]) == 0) {
2801 types |= us_type_bits[i];
2806 (void) fprintf(stderr, gettext("invalid type "
2807 "'%s' for -t option\n"), tfield);
2812 } while (delim != NULL);
2815 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2818 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2819 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2821 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2824 /* Always add default sorting columns */
2825 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2826 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2828 cb.cb_sortcol = sortcol;
2829 cb.cb_numname = prtnum;
2830 cb.cb_nicenum = !parsable;
2831 cb.cb_avl_pool = avl_pool;
2832 cb.cb_avl = avl_tree;
2833 cb.cb_sid2posix = sid2posix;
2835 for (i = 0; i < USFIELD_LAST; i++)
2836 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2838 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2839 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2840 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2841 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2842 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2845 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2850 if ((node = uu_avl_first(avl_tree)) == NULL)
2853 us_populated = B_TRUE;
2855 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2856 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2857 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2858 uu_list_node_init(node, &node->usn_listnode, listpool);
2860 while (node != NULL) {
2862 node = uu_avl_next(avl_tree, node);
2863 uu_avl_remove(avl_tree, rmnode);
2864 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2865 uu_list_insert(list, rmnode, idx2);
2868 for (node = uu_list_first(list); node != NULL;
2869 node = uu_list_next(list, node)) {
2870 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2872 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2873 uu_avl_insert(avl_tree, node, idx);
2876 uu_list_destroy(list);
2877 uu_list_pool_destroy(listpool);
2879 /* Print and free node nvlist memory */
2880 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2883 zfs_free_sort_columns(sortcol);
2885 /* Clean up the AVL tree */
2886 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2889 while ((node = uu_avl_walk_next(walk)) != NULL) {
2890 uu_avl_remove(cb.cb_avl, node);
2894 uu_avl_walk_end(walk);
2895 uu_avl_destroy(avl_tree);
2896 uu_avl_pool_destroy(avl_pool);
2902 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2903 * [-t type[,...]] [filesystem|volume|snapshot] ...
2905 * -H Scripted mode; elide headers and separate columns by tabs.
2906 * -p Display values in parsable (literal) format.
2907 * -r Recurse over all children.
2908 * -d Limit recursion by depth.
2909 * -o Control which fields to display.
2910 * -s Specify sort columns, descending order.
2911 * -S Specify sort columns, ascending order.
2912 * -t Control which object types to display.
2914 * When given no arguments, list all filesystems in the system.
2915 * Otherwise, list the specified datasets, optionally recursing down them if
2916 * '-r' is specified.
2918 typedef struct list_cbdata {
2920 boolean_t cb_literal;
2921 boolean_t cb_scripted;
2922 zprop_list_t *cb_proplist;
2926 * Given a list of columns to display, output appropriate headers for each one.
2929 print_header(list_cbdata_t *cb)
2931 zprop_list_t *pl = cb->cb_proplist;
2932 char headerbuf[ZFS_MAXPROPLEN];
2935 boolean_t first = B_TRUE;
2936 boolean_t right_justify;
2938 for (; pl != NULL; pl = pl->pl_next) {
2945 right_justify = B_FALSE;
2946 if (pl->pl_prop != ZPROP_INVAL) {
2947 header = zfs_prop_column_name(pl->pl_prop);
2948 right_justify = zfs_prop_align_right(pl->pl_prop);
2950 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2951 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2952 headerbuf[i] = '\0';
2956 if (pl->pl_next == NULL && !right_justify)
2957 (void) printf("%s", header);
2958 else if (right_justify)
2959 (void) printf("%*s", pl->pl_width, header);
2961 (void) printf("%-*s", pl->pl_width, header);
2964 (void) printf("\n");
2968 * Given a dataset and a list of fields, print out all the properties according
2969 * to the described layout.
2972 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2974 zprop_list_t *pl = cb->cb_proplist;
2975 boolean_t first = B_TRUE;
2976 char property[ZFS_MAXPROPLEN];
2977 nvlist_t *userprops = zfs_get_user_props(zhp);
2980 boolean_t right_justify;
2982 for (; pl != NULL; pl = pl->pl_next) {
2984 if (cb->cb_scripted)
2985 (void) printf("\t");
2992 if (pl->pl_prop == ZFS_PROP_NAME) {
2993 (void) strlcpy(property, zfs_get_name(zhp),
2996 right_justify = zfs_prop_align_right(pl->pl_prop);
2997 } else if (pl->pl_prop != ZPROP_INVAL) {
2998 if (zfs_prop_get(zhp, pl->pl_prop, property,
2999 sizeof (property), NULL, NULL, 0,
3000 cb->cb_literal) != 0)
3004 right_justify = zfs_prop_align_right(pl->pl_prop);
3005 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3006 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3007 property, sizeof (property), cb->cb_literal) != 0)
3011 right_justify = B_TRUE;
3012 } else if (zfs_prop_written(pl->pl_user_prop)) {
3013 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3014 property, sizeof (property), cb->cb_literal) != 0)
3018 right_justify = B_TRUE;
3020 if (nvlist_lookup_nvlist(userprops,
3021 pl->pl_user_prop, &propval) != 0)
3024 verify(nvlist_lookup_string(propval,
3025 ZPROP_VALUE, &propstr) == 0);
3026 right_justify = B_FALSE;
3030 * If this is being called in scripted mode, or if this is the
3031 * last column and it is left-justified, don't include a width
3034 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3035 (void) printf("%s", propstr);
3036 else if (right_justify)
3037 (void) printf("%*s", pl->pl_width, propstr);
3039 (void) printf("%-*s", pl->pl_width, propstr);
3042 (void) printf("\n");
3046 * Generic callback function to list a dataset or snapshot.
3049 list_callback(zfs_handle_t *zhp, void *data)
3051 list_cbdata_t *cbp = data;
3053 if (cbp->cb_first) {
3054 if (!cbp->cb_scripted)
3056 cbp->cb_first = B_FALSE;
3059 print_dataset(zhp, cbp);
3065 zfs_do_list(int argc, char **argv)
3068 static char default_fields[] =
3069 "name,used,available,referenced,mountpoint";
3070 int types = ZFS_TYPE_DATASET;
3071 boolean_t types_specified = B_FALSE;
3072 char *fields = NULL;
3073 list_cbdata_t cb = { 0 };
3077 zfs_sort_column_t *sortcol = NULL;
3078 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3081 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3087 cb.cb_literal = B_TRUE;
3088 flags |= ZFS_ITER_LITERAL_PROPS;
3091 limit = parse_depth(optarg, &flags);
3094 flags |= ZFS_ITER_RECURSE;
3097 cb.cb_scripted = B_TRUE;
3100 if (zfs_add_sort_column(&sortcol, optarg,
3102 (void) fprintf(stderr,
3103 gettext("invalid property '%s'\n"), optarg);
3108 if (zfs_add_sort_column(&sortcol, optarg,
3110 (void) fprintf(stderr,
3111 gettext("invalid property '%s'\n"), optarg);
3117 types_specified = B_TRUE;
3118 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3119 while (*optarg != '\0') {
3120 static char *type_subopts[] = { "filesystem",
3121 "volume", "snapshot", "snap", "bookmark",
3124 switch (getsubopt(&optarg, type_subopts,
3127 types |= ZFS_TYPE_FILESYSTEM;
3130 types |= ZFS_TYPE_VOLUME;
3134 types |= ZFS_TYPE_SNAPSHOT;
3137 types |= ZFS_TYPE_BOOKMARK;
3140 types = ZFS_TYPE_DATASET |
3144 (void) fprintf(stderr,
3145 gettext("invalid type '%s'\n"),
3152 (void) fprintf(stderr, gettext("missing argument for "
3153 "'%c' option\n"), optopt);
3157 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3167 fields = default_fields;
3170 * If we are only going to list snapshot names and sort by name,
3171 * then we can use faster version.
3173 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3174 flags |= ZFS_ITER_SIMPLE;
3177 * If "-o space" and no types were specified, don't display snapshots.
3179 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3180 types &= ~ZFS_TYPE_SNAPSHOT;
3183 * If the user specifies '-o all', the zprop_get_list() doesn't
3184 * normally include the name of the dataset. For 'zfs list', we always
3185 * want this property to be first.
3187 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3191 cb.cb_first = B_TRUE;
3193 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3194 limit, list_callback, &cb);
3196 zprop_free_list(cb.cb_proplist);
3197 zfs_free_sort_columns(sortcol);
3199 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3200 (void) printf(gettext("no datasets available\n"));
3206 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3207 * zfs rename [-f] -p <fs | vol> <fs | vol>
3208 * zfs rename -r <snap> <snap>
3209 * zfs rename -u [-p] <fs> <fs>
3211 * Renames the given dataset to another of the same type.
3213 * The '-p' flag creates all the non-existing ancestors of the target first.
3217 zfs_do_rename(int argc, char **argv)
3220 renameflags_t flags = { 0 };
3224 boolean_t parents = B_FALSE;
3225 char *snapshot = NULL;
3228 while ((c = getopt(argc, argv, "fpru")) != -1) {
3234 flags.recurse = B_TRUE;
3237 flags.nounmount = B_TRUE;
3240 flags.forceunmount = B_TRUE;
3244 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3253 /* check number of arguments */
3255 (void) fprintf(stderr, gettext("missing source dataset "
3260 (void) fprintf(stderr, gettext("missing target dataset "
3265 (void) fprintf(stderr, gettext("too many arguments\n"));
3269 if (flags.recurse && parents) {
3270 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3275 if (flags.recurse && strchr(argv[0], '@') == 0) {
3276 (void) fprintf(stderr, gettext("source dataset for recursive "
3277 "rename must be a snapshot\n"));
3281 if (flags.nounmount && parents) {
3282 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3287 if (flags.nounmount)
3288 types = ZFS_TYPE_FILESYSTEM;
3290 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3292 types = ZFS_TYPE_DATASET;
3294 if (flags.recurse) {
3296 * When we do recursive rename we are fine when the given
3297 * snapshot for the given dataset doesn't exist - it can
3298 * still exists below.
3301 snapshot = strchr(argv[0], '@');
3302 assert(snapshot != NULL);
3307 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3310 /* If we were asked and the name looks good, try to create ancestors. */
3311 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3312 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3317 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3326 * Promotes the given clone fs to be the parent
3330 zfs_do_promote(int argc, char **argv)
3336 if (argc > 1 && argv[1][0] == '-') {
3337 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3342 /* check number of arguments */
3344 (void) fprintf(stderr, gettext("missing clone filesystem"
3349 (void) fprintf(stderr, gettext("too many arguments\n"));
3353 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3357 ret = (zfs_promote(zhp) != 0);
3365 * zfs rollback [-rRf] <snapshot>
3367 * -r Delete any intervening snapshots before doing rollback
3368 * -R Delete any snapshots and their clones
3369 * -f ignored for backwards compatability
3371 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3372 * since then and making it the active dataset. If more recent snapshots exist,
3373 * the command will complain unless the '-r' flag is given.
3375 typedef struct rollback_cbdata {
3381 boolean_t cb_recurse;
3382 } rollback_cbdata_t;
3385 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3387 rollback_cbdata_t *cbp = data;
3389 if (cbp->cb_first && cbp->cb_recurse) {
3390 (void) fprintf(stderr, gettext("cannot rollback to "
3391 "'%s': clones of previous snapshots exist\n"),
3393 (void) fprintf(stderr, gettext("use '-R' to "
3394 "force deletion of the following clones and "
3400 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3407 * Report any snapshots more recent than the one specified. Used when '-r' is
3408 * not specified. We reuse this same callback for the snapshot dependents - if
3409 * 'cb_dependent' is set, then this is a dependent and we should report it
3410 * without checking the transaction group.
3413 rollback_check(zfs_handle_t *zhp, void *data)
3415 rollback_cbdata_t *cbp = data;
3417 if (cbp->cb_doclones) {
3422 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3423 if (cbp->cb_first && !cbp->cb_recurse) {
3424 (void) fprintf(stderr, gettext("cannot "
3425 "rollback to '%s': more recent snapshots "
3426 "or bookmarks exist\n"),
3428 (void) fprintf(stderr, gettext("use '-r' to "
3429 "force deletion of the following "
3430 "snapshots and bookmarks:\n"));
3435 if (cbp->cb_recurse) {
3436 if (zfs_iter_dependents(zhp, B_TRUE,
3437 rollback_check_dependent, cbp) != 0) {
3442 (void) fprintf(stderr, "%s\n",
3451 zfs_do_rollback(int argc, char **argv)
3455 boolean_t force = B_FALSE;
3456 rollback_cbdata_t cb = { 0 };
3457 zfs_handle_t *zhp, *snap;
3458 char parentname[ZFS_MAXNAMELEN];
3462 while ((c = getopt(argc, argv, "rRf")) != -1) {
3475 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3484 /* check number of arguments */
3486 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3490 (void) fprintf(stderr, gettext("too many arguments\n"));
3494 /* open the snapshot */
3495 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3498 /* open the parent dataset */
3499 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3500 verify((delim = strrchr(parentname, '@')) != NULL);
3502 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3508 * Check for more recent snapshots and/or clones based on the presence
3511 cb.cb_target = argv[0];
3512 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3513 cb.cb_first = B_TRUE;
3515 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3517 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3520 if ((ret = cb.cb_error) != 0)
3524 * Rollback parent to the given snapshot.
3526 ret = zfs_rollback(zhp, snap, force);
3539 * zfs set property=value ... { fs | snap | vol } ...
3541 * Sets the given properties for all datasets specified on the command line.
3545 set_callback(zfs_handle_t *zhp, void *data)
3547 nvlist_t *props = data;
3549 if (zfs_prop_set_list(zhp, props) != 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)
3568 nvlist_t *props = NULL;
3569 int ds_start = -1; /* argv idx of first dataset arg */
3572 /* check for options */
3573 if (argc > 1 && argv[1][0] == '-') {
3574 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3579 /* check number of arguments */
3581 (void) fprintf(stderr, gettext("missing arguments\n"));
3585 if (strchr(argv[1], '=') == NULL) {
3586 (void) fprintf(stderr, gettext("missing property=value "
3589 (void) fprintf(stderr, gettext("missing dataset "
3595 /* validate argument order: prop=val args followed by dataset args */
3596 for (int i = 1; i < argc; i++) {
3597 if (strchr(argv[i], '=') != NULL) {
3599 /* out-of-order prop=val argument */
3600 (void) fprintf(stderr, gettext("invalid "
3601 "argument order\n"), i);
3604 } else if (ds_start < 0) {
3609 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3613 /* Populate a list of property settings */
3614 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3616 for (int i = 1; i < ds_start; i++) {
3617 if ((ret = parseprop(props, argv[i])) != 0)
3621 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3622 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3629 typedef struct snap_cbdata {
3631 boolean_t sd_recursive;
3632 const char *sd_snapname;
3636 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3638 snap_cbdata_t *sd = arg;
3643 if (sd->sd_recursive &&
3644 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3649 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3652 fnvlist_add_boolean(sd->sd_nvl, name);
3655 if (sd->sd_recursive)
3656 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3662 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3664 * Creates a snapshot with the given name. While functionally equivalent to
3665 * 'zfs create', it is a separate command to differentiate intent.
3668 zfs_do_snapshot(int argc, char **argv)
3673 snap_cbdata_t sd = { 0 };
3674 boolean_t multiple_snaps = B_FALSE;
3676 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3678 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3682 while ((c = getopt(argc, argv, "ro:")) != -1) {
3685 if (parseprop(props, optarg) != 0)
3689 sd.sd_recursive = B_TRUE;
3690 multiple_snaps = B_TRUE;
3693 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3702 /* check number of arguments */
3704 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3709 multiple_snaps = B_TRUE;
3710 for (; argc > 0; argc--, argv++) {
3714 atp = strchr(argv[0], '@');
3718 sd.sd_snapname = atp + 1;
3719 zhp = zfs_open(g_zfs, argv[0],
3720 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3723 if (zfs_snapshot_cb(zhp, &sd) != 0)
3727 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3728 nvlist_free(sd.sd_nvl);
3730 if (ret != 0 && multiple_snaps)
3731 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3735 nvlist_free(sd.sd_nvl);
3742 * Send a backup stream to stdout.
3745 zfs_do_send(int argc, char **argv)
3747 char *fromname = NULL;
3748 char *toname = NULL;
3749 char *resume_token = NULL;
3752 sendflags_t flags = { 0 };
3754 nvlist_t *dbgnv = NULL;
3755 boolean_t extraverbose = B_FALSE;
3758 while ((c = getopt(argc, argv, ":i:I:RDpvnPLet:")) != -1) {
3769 flags.doall = B_TRUE;
3772 flags.replicate = B_TRUE;
3775 flags.props = B_TRUE;
3778 flags.parsable = B_TRUE;
3779 flags.verbose = B_TRUE;
3783 extraverbose = B_TRUE;
3784 flags.verbose = B_TRUE;
3785 flags.progress = B_TRUE;
3788 flags.dedup = B_TRUE;
3791 flags.dryrun = B_TRUE;
3794 flags.largeblock = B_TRUE;
3797 flags.embed_data = B_TRUE;
3800 resume_token = optarg;
3803 (void) fprintf(stderr, gettext("missing argument for "
3804 "'%c' option\n"), optopt);
3808 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3817 if (resume_token != NULL) {
3818 if (fromname != NULL || flags.replicate || flags.props ||
3820 (void) fprintf(stderr,
3821 gettext("invalid flags combined with -t\n"));
3825 (void) fprintf(stderr, gettext("no additional "
3826 "arguments are permitted with -t\n"));
3831 (void) fprintf(stderr,
3832 gettext("missing snapshot argument\n"));
3836 (void) fprintf(stderr, gettext("too many arguments\n"));
3841 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3842 (void) fprintf(stderr,
3843 gettext("Error: Stream can not be written to a terminal.\n"
3844 "You must redirect standard output.\n"));
3848 if (resume_token != NULL) {
3849 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3854 * Special case sending a filesystem, or from a bookmark.
3856 if (strchr(argv[0], '@') == NULL ||
3857 (fromname && strchr(fromname, '#') != NULL)) {
3858 char frombuf[ZFS_MAXNAMELEN];
3859 enum lzc_send_flags lzc_flags = 0;
3861 if (flags.replicate || flags.doall || flags.props ||
3862 flags.dedup || flags.dryrun || flags.verbose ||
3864 (void) fprintf(stderr,
3866 "Unsupported flag with filesystem or bookmark.\n"));
3870 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3874 if (flags.largeblock)
3875 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3876 if (flags.embed_data)
3877 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3879 if (fromname != NULL &&
3880 (fromname[0] == '#' || fromname[0] == '@')) {
3882 * Incremental source name begins with # or @.
3883 * Default to same fs as target.
3885 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3886 cp = strchr(frombuf, '@');
3889 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3892 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3897 cp = strchr(argv[0], '@');
3900 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3905 * If they specified the full path to the snapshot, chop off
3906 * everything except the short name of the snapshot, but special
3907 * case if they specify the origin.
3909 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3910 char origin[ZFS_MAXNAMELEN];
3913 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3914 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3916 if (strcmp(origin, fromname) == 0) {
3918 flags.fromorigin = B_TRUE;
3921 if (cp != fromname && strcmp(argv[0], fromname)) {
3922 (void) fprintf(stderr,
3923 gettext("incremental source must be "
3924 "in same filesystem\n"));
3928 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3929 (void) fprintf(stderr,
3930 gettext("invalid incremental source\n"));
3936 if (flags.replicate && fromname == NULL)
3937 flags.doall = B_TRUE;
3939 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3940 extraverbose ? &dbgnv : NULL);
3942 if (extraverbose && dbgnv != NULL) {
3944 * dump_nvlist prints to stdout, but that's been
3945 * redirected to a file. Make it print to stderr
3948 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3949 dump_nvlist(dbgnv, 0);
3958 * Restore a backup stream from stdin.
3961 zfs_do_receive(int argc, char **argv)
3964 recvflags_t flags = { 0 };
3965 boolean_t abort_resumable = B_FALSE;
3968 nvpair_t *nvp = NULL;
3970 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3974 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
3977 if (parseprop(props, optarg) != 0)
3981 flags.isprefix = B_TRUE;
3984 flags.isprefix = B_TRUE;
3985 flags.istail = B_TRUE;
3988 flags.dryrun = B_TRUE;
3991 flags.nomount = B_TRUE;
3994 flags.verbose = B_TRUE;
3997 flags.resumable = B_TRUE;
4000 flags.force = B_TRUE;
4003 abort_resumable = B_TRUE;
4006 (void) fprintf(stderr, gettext("missing argument for "
4007 "'%c' option\n"), optopt);
4011 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4020 /* check number of arguments */
4022 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4026 (void) fprintf(stderr, gettext("too many arguments\n"));
4030 while ((nvp = nvlist_next_nvpair(props, nvp))) {
4031 if (strcmp(nvpair_name(nvp), "origin") != 0) {
4032 (void) fprintf(stderr, gettext("invalid option"));
4037 if (abort_resumable) {
4038 if (flags.isprefix || flags.istail || flags.dryrun ||
4039 flags.resumable || flags.nomount) {
4040 (void) fprintf(stderr, gettext("invalid option"));
4044 char namebuf[ZFS_MAXNAMELEN];
4045 (void) snprintf(namebuf, sizeof (namebuf),
4046 "%s/%%recv", argv[0]);
4048 if (zfs_dataset_exists(g_zfs, namebuf,
4049 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4050 zfs_handle_t *zhp = zfs_open(g_zfs,
4051 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4054 err = zfs_destroy(zhp, B_FALSE);
4056 zfs_handle_t *zhp = zfs_open(g_zfs,
4057 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4060 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4061 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4062 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4063 (void) fprintf(stderr,
4064 gettext("'%s' does not have any "
4065 "resumable receive state to abort\n"),
4069 err = zfs_destroy(zhp, B_FALSE);
4075 if (isatty(STDIN_FILENO)) {
4076 (void) fprintf(stderr,
4077 gettext("Error: Backup stream can not be read "
4078 "from a terminal.\n"
4079 "You must redirect standard input.\n"));
4082 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4088 * allow/unallow stuff
4090 /* copied from zfs/sys/dsl_deleg.h */
4091 #define ZFS_DELEG_PERM_CREATE "create"
4092 #define ZFS_DELEG_PERM_DESTROY "destroy"
4093 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4094 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4095 #define ZFS_DELEG_PERM_CLONE "clone"
4096 #define ZFS_DELEG_PERM_PROMOTE "promote"
4097 #define ZFS_DELEG_PERM_RENAME "rename"
4098 #define ZFS_DELEG_PERM_MOUNT "mount"
4099 #define ZFS_DELEG_PERM_SHARE "share"
4100 #define ZFS_DELEG_PERM_SEND "send"
4101 #define ZFS_DELEG_PERM_RECEIVE "receive"
4102 #define ZFS_DELEG_PERM_ALLOW "allow"
4103 #define ZFS_DELEG_PERM_USERPROP "userprop"
4104 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4105 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4106 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4107 #define ZFS_DELEG_PERM_USERUSED "userused"
4108 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4109 #define ZFS_DELEG_PERM_HOLD "hold"
4110 #define ZFS_DELEG_PERM_RELEASE "release"
4111 #define ZFS_DELEG_PERM_DIFF "diff"
4112 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4114 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4116 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4117 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4118 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4119 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4120 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4121 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4122 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4123 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4124 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4125 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4126 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4127 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4128 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4129 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4130 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4131 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4132 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4134 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4135 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4136 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4137 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4138 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4139 { NULL, ZFS_DELEG_NOTE_NONE }
4142 /* permission structure */
4143 typedef struct deleg_perm {
4144 zfs_deleg_who_type_t dp_who_type;
4145 const char *dp_name;
4147 boolean_t dp_descend;
4151 typedef struct deleg_perm_node {
4152 deleg_perm_t dpn_perm;
4154 uu_avl_node_t dpn_avl_node;
4155 } deleg_perm_node_t;
4157 typedef struct fs_perm fs_perm_t;
4159 /* permissions set */
4160 typedef struct who_perm {
4161 zfs_deleg_who_type_t who_type;
4162 const char *who_name; /* id */
4163 char who_ug_name[256]; /* user/group name */
4164 fs_perm_t *who_fsperm; /* uplink */
4166 uu_avl_t *who_deleg_perm_avl; /* permissions */
4170 typedef struct who_perm_node {
4171 who_perm_t who_perm;
4172 uu_avl_node_t who_avl_node;
4175 typedef struct fs_perm_set fs_perm_set_t;
4176 /* fs permissions */
4178 const char *fsp_name;
4180 uu_avl_t *fsp_sc_avl; /* sets,create */
4181 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4183 fs_perm_set_t *fsp_set; /* uplink */
4187 typedef struct fs_perm_node {
4188 fs_perm_t fspn_fsperm;
4191 uu_list_node_t fspn_list_node;
4194 /* top level structure */
4195 struct fs_perm_set {
4196 uu_list_pool_t *fsps_list_pool;
4197 uu_list_t *fsps_list; /* list of fs_perms */
4199 uu_avl_pool_t *fsps_named_set_avl_pool;
4200 uu_avl_pool_t *fsps_who_perm_avl_pool;
4201 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4204 static inline const char *
4205 deleg_perm_type(zfs_deleg_note_t note)
4211 case ZFS_DELEG_NOTE_GROUPQUOTA:
4212 case ZFS_DELEG_NOTE_GROUPUSED:
4213 case ZFS_DELEG_NOTE_USERPROP:
4214 case ZFS_DELEG_NOTE_USERQUOTA:
4215 case ZFS_DELEG_NOTE_USERUSED:
4217 return (gettext("other"));
4219 return (gettext("subcommand"));
4224 who_type2weight(zfs_deleg_who_type_t who_type)
4228 case ZFS_DELEG_NAMED_SET_SETS:
4229 case ZFS_DELEG_NAMED_SET:
4232 case ZFS_DELEG_CREATE_SETS:
4233 case ZFS_DELEG_CREATE:
4236 case ZFS_DELEG_USER_SETS:
4237 case ZFS_DELEG_USER:
4240 case ZFS_DELEG_GROUP_SETS:
4241 case ZFS_DELEG_GROUP:
4244 case ZFS_DELEG_EVERYONE_SETS:
4245 case ZFS_DELEG_EVERYONE:
4257 who_perm_compare(const void *larg, const void *rarg, void *unused)
4259 const who_perm_node_t *l = larg;
4260 const who_perm_node_t *r = rarg;
4261 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4262 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4263 int lweight = who_type2weight(ltype);
4264 int rweight = who_type2weight(rtype);
4265 int res = lweight - rweight;
4267 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4268 ZFS_MAX_DELEG_NAME-1);
4280 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4282 const deleg_perm_node_t *l = larg;
4283 const deleg_perm_node_t *r = rarg;
4284 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4285 ZFS_MAX_DELEG_NAME-1);
4297 fs_perm_set_init(fs_perm_set_t *fspset)
4299 bzero(fspset, sizeof (fs_perm_set_t));
4301 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4302 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4303 NULL, UU_DEFAULT)) == NULL)
4305 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4306 UU_DEFAULT)) == NULL)
4309 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4310 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4311 who_perm_node_t, who_avl_node), who_perm_compare,
4312 UU_DEFAULT)) == NULL)
4315 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4316 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4317 who_perm_node_t, who_avl_node), who_perm_compare,
4318 UU_DEFAULT)) == NULL)
4321 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4322 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4323 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4328 static inline void fs_perm_fini(fs_perm_t *);
4329 static inline void who_perm_fini(who_perm_t *);
4332 fs_perm_set_fini(fs_perm_set_t *fspset)
4334 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4336 while (node != NULL) {
4337 fs_perm_node_t *next_node =
4338 uu_list_next(fspset->fsps_list, node);
4339 fs_perm_t *fsperm = &node->fspn_fsperm;
4340 fs_perm_fini(fsperm);
4341 uu_list_remove(fspset->fsps_list, node);
4346 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4347 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4348 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4352 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4355 deleg_perm->dp_who_type = type;
4356 deleg_perm->dp_name = name;
4360 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4361 zfs_deleg_who_type_t type, const char *name)
4363 uu_avl_pool_t *pool;
4364 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4366 bzero(who_perm, sizeof (who_perm_t));
4368 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4369 UU_DEFAULT)) == NULL)
4372 who_perm->who_type = type;
4373 who_perm->who_name = name;
4374 who_perm->who_fsperm = fsperm;
4378 who_perm_fini(who_perm_t *who_perm)
4380 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4382 while (node != NULL) {
4383 deleg_perm_node_t *next_node =
4384 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4386 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4391 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4395 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4397 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4398 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4400 bzero(fsperm, sizeof (fs_perm_t));
4402 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4406 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4410 fsperm->fsp_set = fspset;
4411 fsperm->fsp_name = fsname;
4415 fs_perm_fini(fs_perm_t *fsperm)
4417 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4418 while (node != NULL) {
4419 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4421 who_perm_t *who_perm = &node->who_perm;
4422 who_perm_fini(who_perm);
4423 uu_avl_remove(fsperm->fsp_sc_avl, node);
4428 node = uu_avl_first(fsperm->fsp_uge_avl);
4429 while (node != NULL) {
4430 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4432 who_perm_t *who_perm = &node->who_perm;
4433 who_perm_fini(who_perm);
4434 uu_avl_remove(fsperm->fsp_uge_avl, node);
4439 uu_avl_destroy(fsperm->fsp_sc_avl);
4440 uu_avl_destroy(fsperm->fsp_uge_avl);
4444 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4445 zfs_deleg_who_type_t who_type, const char *name, char locality)
4447 uu_avl_index_t idx = 0;
4449 deleg_perm_node_t *found_node = NULL;
4450 deleg_perm_t *deleg_perm = &node->dpn_perm;
4452 deleg_perm_init(deleg_perm, who_type, name);
4454 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4456 uu_avl_insert(avl, node, idx);
4459 deleg_perm = &node->dpn_perm;
4464 case ZFS_DELEG_LOCAL:
4465 deleg_perm->dp_local = B_TRUE;
4467 case ZFS_DELEG_DESCENDENT:
4468 deleg_perm->dp_descend = B_TRUE;
4473 assert(B_FALSE); /* invalid locality */
4478 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4480 nvpair_t *nvp = NULL;
4481 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4482 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4483 zfs_deleg_who_type_t who_type = who_perm->who_type;
4485 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4486 const char *name = nvpair_name(nvp);
4487 data_type_t type = nvpair_type(nvp);
4488 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4489 deleg_perm_node_t *node =
4490 safe_malloc(sizeof (deleg_perm_node_t));
4492 assert(type == DATA_TYPE_BOOLEAN);
4494 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4495 set_deleg_perm_node(avl, node, who_type, name, locality);
4502 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4504 nvpair_t *nvp = NULL;
4505 fs_perm_set_t *fspset = fsperm->fsp_set;
4507 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4508 nvlist_t *nvl2 = NULL;
4509 const char *name = nvpair_name(nvp);
4510 uu_avl_t *avl = NULL;
4511 uu_avl_pool_t *avl_pool;
4512 zfs_deleg_who_type_t perm_type = name[0];
4513 char perm_locality = name[1];
4514 const char *perm_name = name + 3;
4515 boolean_t is_set = B_TRUE;
4516 who_perm_t *who_perm = NULL;
4518 assert('$' == name[2]);
4520 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4523 switch (perm_type) {
4524 case ZFS_DELEG_CREATE:
4525 case ZFS_DELEG_CREATE_SETS:
4526 case ZFS_DELEG_NAMED_SET:
4527 case ZFS_DELEG_NAMED_SET_SETS:
4528 avl_pool = fspset->fsps_named_set_avl_pool;
4529 avl = fsperm->fsp_sc_avl;
4531 case ZFS_DELEG_USER:
4532 case ZFS_DELEG_USER_SETS:
4533 case ZFS_DELEG_GROUP:
4534 case ZFS_DELEG_GROUP_SETS:
4535 case ZFS_DELEG_EVERYONE:
4536 case ZFS_DELEG_EVERYONE_SETS:
4537 avl_pool = fspset->fsps_who_perm_avl_pool;
4538 avl = fsperm->fsp_uge_avl;
4543 who_perm_node_t *found_node = NULL;
4544 who_perm_node_t *node = safe_malloc(
4545 sizeof (who_perm_node_t));
4546 who_perm = &node->who_perm;
4547 uu_avl_index_t idx = 0;
4549 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4550 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4552 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4554 if (avl == fsperm->fsp_uge_avl) {
4556 struct passwd *p = NULL;
4557 struct group *g = NULL;
4558 const char *nice_name = NULL;
4560 switch (perm_type) {
4561 case ZFS_DELEG_USER_SETS:
4562 case ZFS_DELEG_USER:
4563 rid = atoi(perm_name);
4566 nice_name = p->pw_name;
4568 case ZFS_DELEG_GROUP_SETS:
4569 case ZFS_DELEG_GROUP:
4570 rid = atoi(perm_name);
4573 nice_name = g->gr_name;
4577 if (nice_name != NULL)
4579 node->who_perm.who_ug_name,
4583 uu_avl_insert(avl, node, idx);
4586 who_perm = &node->who_perm;
4590 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4597 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4599 nvpair_t *nvp = NULL;
4600 uu_avl_index_t idx = 0;
4602 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4603 nvlist_t *nvl2 = NULL;
4604 const char *fsname = nvpair_name(nvp);
4605 data_type_t type = nvpair_type(nvp);
4606 fs_perm_t *fsperm = NULL;
4607 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4611 fsperm = &node->fspn_fsperm;
4613 assert(DATA_TYPE_NVLIST == type);
4615 uu_list_node_init(node, &node->fspn_list_node,
4616 fspset->fsps_list_pool);
4618 idx = uu_list_numnodes(fspset->fsps_list);
4619 fs_perm_init(fsperm, fspset, fsname);
4621 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4624 (void) parse_fs_perm(fsperm, nvl2);
4626 uu_list_insert(fspset->fsps_list, node, idx);
4632 static inline const char *
4633 deleg_perm_comment(zfs_deleg_note_t note)
4635 const char *str = "";
4640 case ZFS_DELEG_NOTE_ALLOW:
4641 str = gettext("Must also have the permission that is being"
4642 "\n\t\t\t\tallowed");
4644 case ZFS_DELEG_NOTE_CLONE:
4645 str = gettext("Must also have the 'create' ability and 'mount'"
4646 "\n\t\t\t\tability in the origin file system");
4648 case ZFS_DELEG_NOTE_CREATE:
4649 str = gettext("Must also have the 'mount' ability");
4651 case ZFS_DELEG_NOTE_DESTROY:
4652 str = gettext("Must also have the 'mount' ability");
4654 case ZFS_DELEG_NOTE_DIFF:
4655 str = gettext("Allows lookup of paths within a dataset;"
4656 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4657 "\n\t\t\t\tin order to use zfs diff");
4659 case ZFS_DELEG_NOTE_HOLD:
4660 str = gettext("Allows adding a user hold to a snapshot");
4662 case ZFS_DELEG_NOTE_MOUNT:
4663 str = gettext("Allows mount/umount of ZFS datasets");
4665 case ZFS_DELEG_NOTE_PROMOTE:
4666 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4667 " 'promote' ability in the origin file system");
4669 case ZFS_DELEG_NOTE_RECEIVE:
4670 str = gettext("Must also have the 'mount' and 'create'"
4673 case ZFS_DELEG_NOTE_RELEASE:
4674 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4675 "might destroy the snapshot");
4677 case ZFS_DELEG_NOTE_RENAME:
4678 str = gettext("Must also have the 'mount' and 'create'"
4679 "\n\t\t\t\tability in the new parent");
4681 case ZFS_DELEG_NOTE_ROLLBACK:
4684 case ZFS_DELEG_NOTE_SEND:
4687 case ZFS_DELEG_NOTE_SHARE:
4688 str = gettext("Allows sharing file systems over NFS or SMB"
4689 "\n\t\t\t\tprotocols");
4691 case ZFS_DELEG_NOTE_SNAPSHOT:
4695 * case ZFS_DELEG_NOTE_VSCAN:
4696 * str = gettext("");
4700 case ZFS_DELEG_NOTE_GROUPQUOTA:
4701 str = gettext("Allows accessing any groupquota@... property");
4703 case ZFS_DELEG_NOTE_GROUPUSED:
4704 str = gettext("Allows reading any groupused@... property");
4706 case ZFS_DELEG_NOTE_USERPROP:
4707 str = gettext("Allows changing any user property");
4709 case ZFS_DELEG_NOTE_USERQUOTA:
4710 str = gettext("Allows accessing any userquota@... property");
4712 case ZFS_DELEG_NOTE_USERUSED:
4713 str = gettext("Allows reading any userused@... property");
4731 boolean_t recursive; /* unallow only */
4732 boolean_t prt_usage;
4734 boolean_t prt_perms;
4737 const char *dataset;
4741 prop_cmp(const void *a, const void *b)
4743 const char *str1 = *(const char **)a;
4744 const char *str2 = *(const char **)b;
4745 return (strcmp(str1, str2));
4749 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4751 const char *opt_desc[] = {
4752 "-h", gettext("show this help message and exit"),
4753 "-l", gettext("set permission locally"),
4754 "-d", gettext("set permission for descents"),
4755 "-u", gettext("set permission for user"),
4756 "-g", gettext("set permission for group"),
4757 "-e", gettext("set permission for everyone"),
4758 "-c", gettext("set create time permission"),
4759 "-s", gettext("define permission set"),
4761 "-r", gettext("remove permissions recursively"),
4763 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4764 size_t allow_size = unallow_size - 2;
4765 const char *props[ZFS_NUM_PROPS];
4768 FILE *fp = requested ? stdout : stderr;
4769 zprop_desc_t *pdtbl = zfs_prop_get_table();
4770 const char *fmt = gettext("%-16s %-14s\t%s\n");
4772 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4774 (void) fprintf(fp, gettext("Options:\n"));
4775 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4776 const char *opt = opt_desc[i++];
4777 const char *optdsc = opt_desc[i];
4778 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4781 (void) fprintf(fp, gettext("\nThe following permissions are "
4783 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4785 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4786 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4787 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4788 const char *perm_type = deleg_perm_type(perm_note);
4789 const char *perm_comment = deleg_perm_comment(perm_note);
4790 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4793 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4794 zprop_desc_t *pd = &pdtbl[i];
4795 if (pd->pd_visible != B_TRUE)
4798 if (pd->pd_attr == PROP_READONLY)
4801 props[count++] = pd->pd_name;
4803 props[count] = NULL;
4805 qsort(props, count, sizeof (char *), prop_cmp);
4807 for (i = 0; i < count; i++)
4808 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4811 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4813 exit(requested ? 0 : 2);
4816 static inline const char *
4817 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4820 if (un && argc == expected_argc - 1)
4822 else if (argc == expected_argc)
4823 *permsp = argv[argc - 2];
4825 allow_usage(un, B_FALSE,
4826 gettext("wrong number of parameters\n"));
4828 return (argv[argc - 1]);
4832 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4834 int uge_sum = opts->user + opts->group + opts->everyone;
4835 int csuge_sum = opts->create + opts->set + uge_sum;
4836 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4837 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4840 allow_usage(un, B_FALSE,
4841 gettext("-u, -g, and -e are mutually exclusive\n"));
4843 if (opts->prt_usage)
4844 if (argc == 0 && all_sum == 0)
4845 allow_usage(un, B_TRUE, NULL);
4851 allow_usage(un, B_FALSE,
4852 gettext("invalid options combined with -s\n"));
4854 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4855 if (argv[0][0] != '@')
4856 allow_usage(un, B_FALSE,
4857 gettext("invalid set name: missing '@' prefix\n"));
4858 opts->who = argv[0];
4859 } else if (opts->create) {
4860 if (ldcsuge_sum > 1)
4861 allow_usage(un, B_FALSE,
4862 gettext("invalid options combined with -c\n"));
4863 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4864 } else if (opts->everyone) {
4866 allow_usage(un, B_FALSE,
4867 gettext("invalid options combined with -e\n"));
4868 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4869 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4871 opts->everyone = B_TRUE;
4874 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4875 } else if (argc == 1 && !un) {
4876 opts->prt_perms = B_TRUE;
4877 opts->dataset = argv[argc-1];
4879 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4880 opts->who = argv[0];
4883 if (!opts->local && !opts->descend) {
4884 opts->local = B_TRUE;
4885 opts->descend = B_TRUE;
4890 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4891 const char *who, char *perms, nvlist_t *top_nvl)
4894 char ld[2] = { '\0', '\0' };
4895 char who_buf[ZFS_MAXNAMELEN+32];
4898 nvlist_t *base_nvl = NULL;
4899 nvlist_t *set_nvl = NULL;
4902 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4904 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4908 case ZFS_DELEG_NAMED_SET_SETS:
4909 case ZFS_DELEG_NAMED_SET:
4910 set_type = ZFS_DELEG_NAMED_SET_SETS;
4911 base_type = ZFS_DELEG_NAMED_SET;
4912 ld[0] = ZFS_DELEG_NA;
4914 case ZFS_DELEG_CREATE_SETS:
4915 case ZFS_DELEG_CREATE:
4916 set_type = ZFS_DELEG_CREATE_SETS;
4917 base_type = ZFS_DELEG_CREATE;
4918 ld[0] = ZFS_DELEG_NA;
4920 case ZFS_DELEG_USER_SETS:
4921 case ZFS_DELEG_USER:
4922 set_type = ZFS_DELEG_USER_SETS;
4923 base_type = ZFS_DELEG_USER;
4925 ld[0] = ZFS_DELEG_LOCAL;
4927 ld[1] = ZFS_DELEG_DESCENDENT;
4929 case ZFS_DELEG_GROUP_SETS:
4930 case ZFS_DELEG_GROUP:
4931 set_type = ZFS_DELEG_GROUP_SETS;
4932 base_type = ZFS_DELEG_GROUP;
4934 ld[0] = ZFS_DELEG_LOCAL;
4936 ld[1] = ZFS_DELEG_DESCENDENT;
4938 case ZFS_DELEG_EVERYONE_SETS:
4939 case ZFS_DELEG_EVERYONE:
4940 set_type = ZFS_DELEG_EVERYONE_SETS;
4941 base_type = ZFS_DELEG_EVERYONE;
4943 ld[0] = ZFS_DELEG_LOCAL;
4945 ld[1] = ZFS_DELEG_DESCENDENT;
4948 if (perms != NULL) {
4950 char *end = curr + strlen(perms);
4952 while (curr < end) {
4953 char *delim = strchr(curr, ',');
4964 (void) nvlist_add_boolean(nvl, curr);
4970 for (i = 0; i < 2; i++) {
4971 char locality = ld[i];
4975 if (!nvlist_empty(base_nvl)) {
4977 (void) snprintf(who_buf,
4978 sizeof (who_buf), "%c%c$%s",
4979 base_type, locality, who);
4981 (void) snprintf(who_buf,
4982 sizeof (who_buf), "%c%c$",
4983 base_type, locality);
4985 (void) nvlist_add_nvlist(top_nvl, who_buf,
4990 if (!nvlist_empty(set_nvl)) {
4992 (void) snprintf(who_buf,
4993 sizeof (who_buf), "%c%c$%s",
4994 set_type, locality, who);
4996 (void) snprintf(who_buf,
4997 sizeof (who_buf), "%c%c$",
4998 set_type, locality);
5000 (void) nvlist_add_nvlist(top_nvl, who_buf,
5005 for (i = 0; i < 2; i++) {
5006 char locality = ld[i];
5011 (void) snprintf(who_buf, sizeof (who_buf),
5012 "%c%c$%s", base_type, locality, who);
5014 (void) snprintf(who_buf, sizeof (who_buf),
5015 "%c%c$", base_type, locality);
5016 (void) nvlist_add_boolean(top_nvl, who_buf);
5019 (void) snprintf(who_buf, sizeof (who_buf),
5020 "%c%c$%s", set_type, locality, who);
5022 (void) snprintf(who_buf, sizeof (who_buf),
5023 "%c%c$", set_type, locality);
5024 (void) nvlist_add_boolean(top_nvl, who_buf);
5030 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5032 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5036 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5037 opts->descend, opts->who, opts->perms, *nvlp);
5038 } else if (opts->create) {
5039 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5040 opts->descend, NULL, opts->perms, *nvlp);
5041 } else if (opts->everyone) {
5042 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5043 opts->descend, NULL, opts->perms, *nvlp);
5045 char *curr = opts->who;
5046 char *end = curr + strlen(curr);
5048 while (curr < end) {
5050 zfs_deleg_who_type_t who_type;
5052 char *delim = strchr(curr, ',');
5055 struct passwd *p = NULL;
5056 struct group *g = NULL;
5064 rid = (uid_t)strtol(curr, &endch, 0);
5066 who_type = ZFS_DELEG_USER;
5075 (void) snprintf(errbuf, 256, gettext(
5076 "invalid user %s"), curr);
5077 allow_usage(un, B_TRUE, errbuf);
5079 } else if (opts->group) {
5080 who_type = ZFS_DELEG_GROUP;
5089 (void) snprintf(errbuf, 256, gettext(
5090 "invalid group %s"), curr);
5091 allow_usage(un, B_TRUE, errbuf);
5094 if (*endch != '\0') {
5101 if (*endch != '\0') {
5108 who_type = ZFS_DELEG_USER;
5110 } else if (g != NULL) {
5111 who_type = ZFS_DELEG_GROUP;
5114 (void) snprintf(errbuf, 256, gettext(
5115 "invalid user/group %s"), curr);
5116 allow_usage(un, B_TRUE, errbuf);
5120 (void) sprintf(id, "%u", rid);
5123 store_allow_perm(who_type, opts->local,
5124 opts->descend, who, opts->perms, *nvlp);
5133 print_set_creat_perms(uu_avl_t *who_avl)
5135 const char *sc_title[] = {
5136 gettext("Permission sets:\n"),
5137 gettext("Create time permissions:\n"),
5140 const char **title_ptr = sc_title;
5141 who_perm_node_t *who_node = NULL;
5142 int prev_weight = -1;
5144 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5145 who_node = uu_avl_next(who_avl, who_node)) {
5146 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5147 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5148 const char *who_name = who_node->who_perm.who_name;
5149 int weight = who_type2weight(who_type);
5150 boolean_t first = B_TRUE;
5151 deleg_perm_node_t *deleg_node;
5153 if (prev_weight != weight) {
5154 (void) printf(*title_ptr++);
5155 prev_weight = weight;
5158 if (who_name == NULL || strnlen(who_name, 1) == 0)
5159 (void) printf("\t");
5161 (void) printf("\t%s ", who_name);
5163 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5164 deleg_node = uu_avl_next(avl, deleg_node)) {
5167 deleg_node->dpn_perm.dp_name);
5170 (void) printf(",%s",
5171 deleg_node->dpn_perm.dp_name);
5174 (void) printf("\n");
5179 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5182 who_perm_node_t *who_node = NULL;
5183 boolean_t prt_title = B_TRUE;
5184 uu_avl_walk_t *walk;
5186 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5189 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5190 const char *who_name = who_node->who_perm.who_name;
5191 const char *nice_who_name = who_node->who_perm.who_ug_name;
5192 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5193 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5195 deleg_perm_node_t *deleg_node;
5196 boolean_t prt_who = B_TRUE;
5198 for (deleg_node = uu_avl_first(avl);
5200 deleg_node = uu_avl_next(avl, deleg_node)) {
5201 if (local != deleg_node->dpn_perm.dp_local ||
5202 descend != deleg_node->dpn_perm.dp_descend)
5206 const char *who = NULL;
5208 prt_title = B_FALSE;
5209 (void) printf(title);
5213 case ZFS_DELEG_USER_SETS:
5214 case ZFS_DELEG_USER:
5215 who = gettext("user");
5217 who_name = nice_who_name;
5219 case ZFS_DELEG_GROUP_SETS:
5220 case ZFS_DELEG_GROUP:
5221 who = gettext("group");
5223 who_name = nice_who_name;
5225 case ZFS_DELEG_EVERYONE_SETS:
5226 case ZFS_DELEG_EVERYONE:
5227 who = gettext("everyone");
5232 if (who_name == NULL)
5233 (void) printf("\t%s", who);
5235 (void) printf("\t%s %s", who, who_name);
5238 (void) printf("%c%s", delim,
5239 deleg_node->dpn_perm.dp_name);
5244 (void) printf("\n");
5247 uu_avl_walk_end(walk);
5251 print_fs_perms(fs_perm_set_t *fspset)
5253 fs_perm_node_t *node = NULL;
5254 char buf[ZFS_MAXNAMELEN+32];
5255 const char *dsname = buf;
5257 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5258 node = uu_list_next(fspset->fsps_list, node)) {
5259 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5260 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5263 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5264 gettext("---- Permissions on %s "),
5265 node->fspn_fsperm.fsp_name);
5266 (void) printf(dsname);
5267 left = 70 - strlen(buf);
5270 (void) printf("\n");
5272 print_set_creat_perms(sc_avl);
5273 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5274 gettext("Local permissions:\n"));
5275 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5276 gettext("Descendent permissions:\n"));
5277 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5278 gettext("Local+Descendent permissions:\n"));
5282 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5284 struct deleg_perms {
5290 set_deleg_perms(zfs_handle_t *zhp, void *data)
5292 struct deleg_perms *perms = (struct deleg_perms *)data;
5293 zfs_type_t zfs_type = zfs_get_type(zhp);
5295 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5298 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5302 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5305 nvlist_t *perm_nvl = NULL;
5306 nvlist_t *update_perm_nvl = NULL;
5309 struct allow_opts opts = { 0 };
5311 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5314 while ((c = getopt(argc, argv, optstr)) != -1) {
5317 opts.local = B_TRUE;
5320 opts.descend = B_TRUE;
5326 opts.group = B_TRUE;
5329 opts.everyone = B_TRUE;
5335 opts.create = B_TRUE;
5338 opts.recursive = B_TRUE;
5341 (void) fprintf(stderr, gettext("missing argument for "
5342 "'%c' option\n"), optopt);
5346 opts.prt_usage = B_TRUE;
5349 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5358 /* check arguments */
5359 parse_allow_args(argc, argv, un, &opts);
5361 /* try to open the dataset */
5362 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5363 ZFS_TYPE_VOLUME)) == NULL) {
5364 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5369 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5372 fs_perm_set_init(&fs_perm_set);
5373 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5374 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5379 print_fs_perms(&fs_perm_set);
5381 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5382 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5385 if (un && opts.recursive) {
5386 struct deleg_perms data = { un, update_perm_nvl };
5387 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5396 nvlist_free(perm_nvl);
5397 if (update_perm_nvl != NULL)
5398 nvlist_free(update_perm_nvl);
5400 fs_perm_set_fini(&fs_perm_set);
5408 zfs_do_allow(int argc, char **argv)
5410 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5414 zfs_do_unallow(int argc, char **argv)
5416 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5420 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5425 boolean_t recursive = B_FALSE;
5426 const char *opts = holding ? "rt" : "r";
5430 while ((c = getopt(argc, argv, opts)) != -1) {
5436 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5445 /* check number of arguments */
5453 if (holding && tag[0] == '.') {
5454 /* tags starting with '.' are reserved for libzfs */
5455 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5459 for (i = 0; i < argc; ++i) {
5461 char parent[ZFS_MAXNAMELEN];
5463 char *path = argv[i];
5465 delim = strchr(path, '@');
5466 if (delim == NULL) {
5467 (void) fprintf(stderr,
5468 gettext("'%s' is not a snapshot\n"), path);
5472 (void) strncpy(parent, path, delim - path);
5473 parent[delim - path] = '\0';
5475 zhp = zfs_open(g_zfs, parent,
5476 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5482 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5485 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5491 return (errors != 0);
5495 * zfs hold [-r] [-t] <tag> <snap> ...
5497 * -r Recursively hold
5499 * Apply a user-hold with the given tag to the list of snapshots.
5502 zfs_do_hold(int argc, char **argv)
5504 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5508 * zfs release [-r] <tag> <snap> ...
5510 * -r Recursively release
5512 * Release a user-hold with the given tag from the list of snapshots.
5515 zfs_do_release(int argc, char **argv)
5517 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5520 typedef struct holds_cbdata {
5521 boolean_t cb_recursive;
5522 const char *cb_snapname;
5524 size_t cb_max_namelen;
5525 size_t cb_max_taglen;
5528 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5529 #define DATETIME_BUF_LEN (32)
5534 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5537 nvpair_t *nvp = NULL;
5538 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5542 for (i = 0; i < 3; i++) {
5543 col = gettext(hdr_cols[i]);
5545 (void) printf("%-*s ", i ? tagwidth : nwidth,
5548 (void) printf("%s\n", col);
5552 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5553 char *zname = nvpair_name(nvp);
5555 nvpair_t *nvp2 = NULL;
5556 (void) nvpair_value_nvlist(nvp, &nvl2);
5557 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5558 char tsbuf[DATETIME_BUF_LEN];
5559 char *tagname = nvpair_name(nvp2);
5563 char sep = scripted ? '\t' : ' ';
5564 size_t sepnum = scripted ? 1 : 2;
5566 (void) nvpair_value_uint64(nvp2, &val);
5568 (void) localtime_r(&time, &t);
5569 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5570 gettext(STRFTIME_FMT_STR), &t);
5572 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5573 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5579 * Generic callback function to list a dataset or snapshot.
5582 holds_callback(zfs_handle_t *zhp, void *data)
5584 holds_cbdata_t *cbp = data;
5585 nvlist_t *top_nvl = *cbp->cb_nvlp;
5586 nvlist_t *nvl = NULL;
5587 nvpair_t *nvp = NULL;
5588 const char *zname = zfs_get_name(zhp);
5589 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5591 if (cbp->cb_recursive) {
5592 const char *snapname;
5593 char *delim = strchr(zname, '@');
5597 snapname = delim + 1;
5598 if (strcmp(cbp->cb_snapname, snapname))
5602 if (zfs_get_holds(zhp, &nvl) != 0)
5605 if (znamelen > cbp->cb_max_namelen)
5606 cbp->cb_max_namelen = znamelen;
5608 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5609 const char *tag = nvpair_name(nvp);
5610 size_t taglen = strnlen(tag, MAXNAMELEN);
5611 if (taglen > cbp->cb_max_taglen)
5612 cbp->cb_max_taglen = taglen;
5615 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5619 * zfs holds [-r] <snap> ...
5621 * -r Recursively hold
5624 zfs_do_holds(int argc, char **argv)
5629 boolean_t scripted = B_FALSE;
5630 boolean_t recursive = B_FALSE;
5631 const char *opts = "rH";
5634 int types = ZFS_TYPE_SNAPSHOT;
5635 holds_cbdata_t cb = { 0 };
5642 while ((c = getopt(argc, argv, opts)) != -1) {
5651 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5658 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5659 flags |= ZFS_ITER_RECURSE;
5665 /* check number of arguments */
5669 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5672 for (i = 0; i < argc; ++i) {
5673 char *snapshot = argv[i];
5675 const char *snapname;
5677 delim = strchr(snapshot, '@');
5678 if (delim == NULL) {
5679 (void) fprintf(stderr,
5680 gettext("'%s' is not a snapshot\n"), snapshot);
5684 snapname = delim + 1;
5686 snapshot[delim - snapshot] = '\0';
5688 cb.cb_recursive = recursive;
5689 cb.cb_snapname = snapname;
5693 * 1. collect holds data, set format options
5695 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5696 holds_callback, &cb);
5702 * 2. print holds data
5704 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5706 if (nvlist_empty(nvl))
5707 (void) printf(gettext("no datasets available\n"));
5711 return (0 != errors);
5714 #define CHECK_SPINNER 30
5715 #define SPINNER_TIME 3 /* seconds */
5716 #define MOUNT_TIME 5 /* seconds */
5719 get_one_dataset(zfs_handle_t *zhp, void *data)
5721 static char *spin[] = { "-", "\\", "|", "/" };
5722 static int spinval = 0;
5723 static int spincheck = 0;
5724 static time_t last_spin_time = (time_t)0;
5725 get_all_cb_t *cbp = data;
5726 zfs_type_t type = zfs_get_type(zhp);
5728 if (cbp->cb_verbose) {
5729 if (--spincheck < 0) {
5730 time_t now = time(NULL);
5731 if (last_spin_time + SPINNER_TIME < now) {
5732 update_progress(spin[spinval++ % 4]);
5733 last_spin_time = now;
5735 spincheck = CHECK_SPINNER;
5740 * Interate over any nested datasets.
5742 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5748 * Skip any datasets whose type does not match.
5750 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5754 libzfs_add_handle(cbp, zhp);
5755 assert(cbp->cb_used <= cbp->cb_alloc);
5761 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5763 get_all_cb_t cb = { 0 };
5764 cb.cb_verbose = verbose;
5765 cb.cb_getone = get_one_dataset;
5768 set_progress_header(gettext("Reading ZFS config"));
5769 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5771 *dslist = cb.cb_handles;
5772 *count = cb.cb_used;
5775 finish_progress(gettext("done."));
5779 * Generic callback for sharing or mounting filesystems. Because the code is so
5780 * similar, we have a common function with an extra parameter to determine which
5781 * mode we are using.
5783 #define OP_SHARE 0x1
5784 #define OP_MOUNT 0x2
5787 * Share or mount a dataset.
5790 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5791 boolean_t explicit, const char *options)
5793 char mountpoint[ZFS_MAXPROPLEN];
5794 char shareopts[ZFS_MAXPROPLEN];
5795 char smbshareopts[ZFS_MAXPROPLEN];
5796 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5798 uint64_t zoned, canmount;
5799 boolean_t shared_nfs, shared_smb;
5801 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5804 * Check to make sure we can mount/share this dataset. If we
5805 * are in the global zone and the filesystem is exported to a
5806 * local zone, or if we are in a local zone and the
5807 * filesystem is not exported, then it is an error.
5809 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5811 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5815 (void) fprintf(stderr, gettext("cannot %s '%s': "
5816 "dataset is exported to a local zone\n"), cmdname,
5820 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5824 (void) fprintf(stderr, gettext("cannot %s '%s': "
5825 "permission denied\n"), cmdname,
5831 * Ignore any filesystems which don't apply to us. This
5832 * includes those with a legacy mountpoint, or those with
5833 * legacy share options.
5835 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5836 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5837 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5838 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5839 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5840 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5842 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5843 strcmp(smbshareopts, "off") == 0) {
5847 (void) fprintf(stderr, gettext("cannot share '%s': "
5848 "legacy share\n"), zfs_get_name(zhp));
5849 (void) fprintf(stderr, gettext("to "
5850 "share this filesystem set "
5851 "sharenfs property on\n"));
5856 * We cannot share or mount legacy filesystems. If the
5857 * shareopts is non-legacy but the mountpoint is legacy, we
5858 * treat it as a legacy share.
5860 if (strcmp(mountpoint, "legacy") == 0) {
5864 (void) fprintf(stderr, gettext("cannot %s '%s': "
5865 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5866 (void) fprintf(stderr, gettext("use %s(8) to "
5867 "%s this filesystem\n"), cmdname, cmdname);
5871 if (strcmp(mountpoint, "none") == 0) {
5875 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5876 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5881 * canmount explicit outcome
5882 * on no pass through
5883 * on yes pass through
5885 * off yes display error, return 1
5886 * noauto no return 0
5887 * noauto yes pass through
5889 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5890 if (canmount == ZFS_CANMOUNT_OFF) {
5894 (void) fprintf(stderr, gettext("cannot %s '%s': "
5895 "'canmount' property is set to 'off'\n"), cmdname,
5898 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5903 * If this filesystem is inconsistent and has a receive resume
5904 * token, we can not mount it.
5906 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
5907 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
5908 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
5912 (void) fprintf(stderr, gettext("cannot %s '%s': "
5913 "Contains partially-completed state from "
5914 "\"zfs receive -r\", which can be resumed with "
5915 "\"zfs send -t\"\n"),
5916 cmdname, zfs_get_name(zhp));
5921 * At this point, we have verified that the mountpoint and/or
5922 * shareopts are appropriate for auto management. If the
5923 * filesystem is already mounted or shared, return (failing
5924 * for explicit requests); otherwise mount or share the
5930 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5931 shared_smb = zfs_is_shared_smb(zhp, NULL);
5933 if (shared_nfs && shared_smb ||
5934 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5935 strcmp(smbshareopts, "off") == 0) ||
5936 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5937 strcmp(shareopts, "off") == 0)) {
5941 (void) fprintf(stderr, gettext("cannot share "
5942 "'%s': filesystem already shared\n"),
5947 if (!zfs_is_mounted(zhp, NULL) &&
5948 zfs_mount(zhp, NULL, 0) != 0)
5951 if (protocol == NULL) {
5952 if (zfs_shareall(zhp) != 0)
5954 } else if (strcmp(protocol, "nfs") == 0) {
5955 if (zfs_share_nfs(zhp))
5957 } else if (strcmp(protocol, "smb") == 0) {
5958 if (zfs_share_smb(zhp))
5961 (void) fprintf(stderr, gettext("cannot share "
5962 "'%s': invalid share type '%s' "
5964 zfs_get_name(zhp), protocol);
5971 if (options == NULL)
5972 mnt.mnt_mntopts = "";
5974 mnt.mnt_mntopts = (char *)options;
5976 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5977 zfs_is_mounted(zhp, NULL)) {
5981 (void) fprintf(stderr, gettext("cannot mount "
5982 "'%s': filesystem already mounted\n"),
5987 if (zfs_mount(zhp, options, flags) != 0)
5996 * Reports progress in the form "(current/total)". Not thread-safe.
5999 report_mount_progress(int current, int total)
6001 static time_t last_progress_time = 0;
6002 time_t now = time(NULL);
6005 /* report 1..n instead of 0..n-1 */
6008 /* display header if we're here for the first time */
6010 set_progress_header(gettext("Mounting ZFS filesystems"));
6011 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6012 /* too soon to report again */
6016 last_progress_time = now;
6018 (void) sprintf(info, "(%d/%d)", current, total);
6020 if (current == total)
6021 finish_progress(info);
6023 update_progress(info);
6027 append_options(char *mntopts, char *newopts)
6029 int len = strlen(mntopts);
6031 /* original length plus new string to append plus 1 for the comma */
6032 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6033 (void) fprintf(stderr, gettext("the opts argument for "
6034 "'%c' option is too long (more than %d chars)\n"),
6035 "-o", MNT_LINE_MAX);
6040 mntopts[len++] = ',';
6042 (void) strcpy(&mntopts[len], newopts);
6046 share_mount(int op, int argc, char **argv)
6049 boolean_t verbose = B_FALSE;
6051 char *options = NULL;
6055 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6065 if (*optarg == '\0') {
6066 (void) fprintf(stderr, gettext("empty mount "
6067 "options (-o) specified\n"));
6071 if (options == NULL)
6072 options = safe_malloc(MNT_LINE_MAX + 1);
6074 /* option validation is done later */
6075 append_options(options, optarg);
6079 warnx("no overlay mounts support on FreeBSD, ignoring");
6082 (void) fprintf(stderr, gettext("missing argument for "
6083 "'%c' option\n"), optopt);
6087 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6096 /* check number of arguments */
6098 zfs_handle_t **dslist = NULL;
6099 size_t i, count = 0;
6100 char *protocol = NULL;
6102 if (op == OP_SHARE && argc > 0) {
6103 if (strcmp(argv[0], "nfs") != 0 &&
6104 strcmp(argv[0], "smb") != 0) {
6105 (void) fprintf(stderr, gettext("share type "
6106 "must be 'nfs' or 'smb'\n"));
6115 (void) fprintf(stderr, gettext("too many arguments\n"));
6119 start_progress_timer();
6120 get_all_datasets(&dslist, &count, verbose);
6125 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6127 for (i = 0; i < count; i++) {
6129 report_mount_progress(i, count);
6131 if (share_mount_one(dslist[i], op, flags, protocol,
6132 B_FALSE, options) != 0)
6134 zfs_close(dslist[i]);
6138 } else if (argc == 0) {
6139 struct mnttab entry;
6141 if ((op == OP_SHARE) || (options != NULL)) {
6142 (void) fprintf(stderr, gettext("missing filesystem "
6143 "argument (specify -a for all)\n"));
6148 * When mount is given no arguments, go through /etc/mnttab and
6149 * display any active ZFS mounts. We hide any snapshots, since
6150 * they are controlled automatically.
6152 rewind(mnttab_file);
6153 while (getmntent(mnttab_file, &entry) == 0) {
6154 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6155 strchr(entry.mnt_special, '@') != NULL)
6158 (void) printf("%-30s %s\n", entry.mnt_special,
6166 (void) fprintf(stderr,
6167 gettext("too many arguments\n"));
6171 if ((zhp = zfs_open(g_zfs, argv[0],
6172 ZFS_TYPE_FILESYSTEM)) == NULL) {
6175 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6185 * zfs mount -a [nfs]
6186 * zfs mount filesystem
6188 * Mount all filesystems, or mount the given filesystem.
6191 zfs_do_mount(int argc, char **argv)
6193 return (share_mount(OP_MOUNT, argc, argv));
6197 * zfs share -a [nfs | smb]
6198 * zfs share filesystem
6200 * Share all filesystems, or share the given filesystem.
6203 zfs_do_share(int argc, char **argv)
6205 return (share_mount(OP_SHARE, argc, argv));
6208 typedef struct unshare_unmount_node {
6209 zfs_handle_t *un_zhp;
6211 uu_avl_node_t un_avlnode;
6212 } unshare_unmount_node_t;
6216 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6218 const unshare_unmount_node_t *l = larg;
6219 const unshare_unmount_node_t *r = rarg;
6221 return (strcmp(l->un_mountp, r->un_mountp));
6225 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6226 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6227 * and unmount it appropriately.
6230 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6234 struct stat64 statbuf;
6235 struct extmnttab entry;
6236 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6240 * Search for the path in /etc/mnttab. Rather than looking for the
6241 * specific path, which can be fooled by non-standard paths (i.e. ".."
6242 * or "//"), we stat() the path and search for the corresponding
6243 * (major,minor) device pair.
6245 if (stat64(path, &statbuf) != 0) {
6246 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6247 cmdname, path, strerror(errno));
6250 path_inode = statbuf.st_ino;
6253 * Search for the given (major,minor) pair in the mount table.
6256 rewind(mnttab_file);
6257 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6258 if (entry.mnt_major == major(statbuf.st_dev) &&
6259 entry.mnt_minor == minor(statbuf.st_dev))
6266 if (statfs(path, &sfs) != 0) {
6267 (void) fprintf(stderr, "%s: %s\n", path,
6271 statfs2mnttab(&sfs, &entry);
6275 if (op == OP_SHARE) {
6276 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6277 "currently mounted\n"), cmdname, path);
6280 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6282 if ((ret = umount2(path, flags)) != 0)
6283 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6288 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6289 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6290 "filesystem\n"), cmdname, path);
6294 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6295 ZFS_TYPE_FILESYSTEM)) == NULL)
6299 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6300 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6301 cmdname, path, strerror(errno));
6303 } else if (statbuf.st_ino != path_inode) {
6304 (void) fprintf(stderr, gettext("cannot "
6305 "%s '%s': not a mountpoint\n"), cmdname, path);
6309 if (op == OP_SHARE) {
6310 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6311 char smbshare_prop[ZFS_MAXPROPLEN];
6313 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6314 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6315 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6316 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6318 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6319 strcmp(smbshare_prop, "off") == 0) {
6320 (void) fprintf(stderr, gettext("cannot unshare "
6321 "'%s': legacy share\n"), path);
6323 (void) fprintf(stderr, gettext("use "
6324 "unshare(1M) to unshare this filesystem\n"));
6326 } else if (!zfs_is_shared(zhp)) {
6327 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6328 "not currently shared\n"), path);
6330 ret = zfs_unshareall_bypath(zhp, path);
6333 char mtpt_prop[ZFS_MAXPROPLEN];
6335 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6336 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6339 ret = zfs_unmount(zhp, NULL, flags);
6340 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6341 (void) fprintf(stderr, gettext("cannot unmount "
6342 "'%s': legacy mountpoint\n"),
6344 (void) fprintf(stderr, gettext("use umount(8) "
6345 "to unmount this filesystem\n"));
6347 ret = zfs_unmountall(zhp, flags);
6358 * Generic callback for unsharing or unmounting a filesystem.
6361 unshare_unmount(int op, int argc, char **argv)
6368 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6369 char sharesmb[ZFS_MAXPROPLEN];
6372 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6381 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6392 * We could make use of zfs_for_each() to walk all datasets in
6393 * the system, but this would be very inefficient, especially
6394 * since we would have to linearly search /etc/mnttab for each
6395 * one. Instead, do one pass through /etc/mnttab looking for
6396 * zfs entries and call zfs_unmount() for each one.
6398 * Things get a little tricky if the administrator has created
6399 * mountpoints beneath other ZFS filesystems. In this case, we
6400 * have to unmount the deepest filesystems first. To accomplish
6401 * this, we place all the mountpoints in an AVL tree sorted by
6402 * the special type (dataset name), and walk the result in
6403 * reverse to make sure to get any snapshots first.
6405 struct mnttab entry;
6406 uu_avl_pool_t *pool;
6408 unshare_unmount_node_t *node;
6410 uu_avl_walk_t *walk;
6413 (void) fprintf(stderr, gettext("too many arguments\n"));
6417 if (((pool = uu_avl_pool_create("unmount_pool",
6418 sizeof (unshare_unmount_node_t),
6419 offsetof(unshare_unmount_node_t, un_avlnode),
6420 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6421 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6424 rewind(mnttab_file);
6425 while (getmntent(mnttab_file, &entry) == 0) {
6427 /* ignore non-ZFS entries */
6428 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6431 /* ignore snapshots */
6432 if (strchr(entry.mnt_special, '@') != NULL)
6435 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6436 ZFS_TYPE_FILESYSTEM)) == NULL) {
6443 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6445 sizeof (nfs_mnt_prop),
6446 NULL, NULL, 0, B_FALSE) == 0);
6447 if (strcmp(nfs_mnt_prop, "off") != 0)
6449 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6451 sizeof (nfs_mnt_prop),
6452 NULL, NULL, 0, B_FALSE) == 0);
6453 if (strcmp(nfs_mnt_prop, "off") == 0)
6457 /* Ignore legacy mounts */
6458 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6460 sizeof (nfs_mnt_prop),
6461 NULL, NULL, 0, B_FALSE) == 0);
6462 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6464 /* Ignore canmount=noauto mounts */
6465 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6466 ZFS_CANMOUNT_NOAUTO)
6472 node = safe_malloc(sizeof (unshare_unmount_node_t));
6474 node->un_mountp = safe_strdup(entry.mnt_mountp);
6476 uu_avl_node_init(node, &node->un_avlnode, pool);
6478 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6479 uu_avl_insert(tree, node, idx);
6481 zfs_close(node->un_zhp);
6482 free(node->un_mountp);
6488 * Walk the AVL tree in reverse, unmounting each filesystem and
6489 * removing it from the AVL tree in the process.
6491 if ((walk = uu_avl_walk_start(tree,
6492 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6495 while ((node = uu_avl_walk_next(walk)) != NULL) {
6496 uu_avl_remove(tree, node);
6500 if (zfs_unshareall_bypath(node->un_zhp,
6501 node->un_mountp) != 0)
6506 if (zfs_unmount(node->un_zhp,
6507 node->un_mountp, flags) != 0)
6512 zfs_close(node->un_zhp);
6513 free(node->un_mountp);
6517 uu_avl_walk_end(walk);
6518 uu_avl_destroy(tree);
6519 uu_avl_pool_destroy(pool);
6524 (void) fprintf(stderr,
6525 gettext("missing filesystem argument\n"));
6527 (void) fprintf(stderr,
6528 gettext("too many arguments\n"));
6533 * We have an argument, but it may be a full path or a ZFS
6534 * filesystem. Pass full paths off to unmount_path() (shared by
6535 * manual_unmount), otherwise open the filesystem and pass to
6538 if (argv[0][0] == '/')
6539 return (unshare_unmount_path(op, argv[0],
6542 if ((zhp = zfs_open(g_zfs, argv[0],
6543 ZFS_TYPE_FILESYSTEM)) == NULL)
6546 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6547 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6548 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6549 NULL, 0, B_FALSE) == 0);
6553 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6555 sizeof (nfs_mnt_prop),
6556 NULL, NULL, 0, B_FALSE) == 0);
6557 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6558 sharesmb, sizeof (sharesmb), NULL, NULL,
6561 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6562 strcmp(sharesmb, "off") == 0) {
6563 (void) fprintf(stderr, gettext("cannot "
6564 "unshare '%s': legacy share\n"),
6567 (void) fprintf(stderr, gettext("use "
6568 "unshare(1M) to unshare this "
6572 } else if (!zfs_is_shared(zhp)) {
6573 (void) fprintf(stderr, gettext("cannot "
6574 "unshare '%s': not currently "
6575 "shared\n"), zfs_get_name(zhp));
6577 } else if (zfs_unshareall(zhp) != 0) {
6583 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6584 (void) fprintf(stderr, gettext("cannot "
6585 "unmount '%s': legacy "
6586 "mountpoint\n"), zfs_get_name(zhp));
6587 (void) fprintf(stderr, gettext("use "
6588 "umount(8) to unmount this "
6591 } else if (!zfs_is_mounted(zhp, NULL)) {
6592 (void) fprintf(stderr, gettext("cannot "
6593 "unmount '%s': not currently "
6597 } else if (zfs_unmountall(zhp, flags) != 0) {
6611 * zfs unmount filesystem
6613 * Unmount all filesystems, or a specific ZFS filesystem.
6616 zfs_do_unmount(int argc, char **argv)
6618 return (unshare_unmount(OP_MOUNT, argc, argv));
6623 * zfs unshare filesystem
6625 * Unshare all filesystems, or a specific ZFS filesystem.
6628 zfs_do_unshare(int argc, char **argv)
6630 return (unshare_unmount(OP_SHARE, argc, argv));
6634 * Attach/detach the given dataset to/from the given jail
6638 do_jail(int argc, char **argv, int attach)
6643 /* check number of arguments */
6645 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6649 (void) fprintf(stderr, gettext("too many arguments\n"));
6653 jailid = jail_getid(argv[1]);
6655 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6659 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6663 ret = (zfs_jail(zhp, jailid, attach) != 0);
6670 * zfs jail jailid filesystem
6672 * Attach the given dataset to the given jail
6676 zfs_do_jail(int argc, char **argv)
6679 return (do_jail(argc, argv, 1));
6683 * zfs unjail jailid filesystem
6685 * Detach the given dataset from the given jail
6689 zfs_do_unjail(int argc, char **argv)
6692 return (do_jail(argc, argv, 0));
6696 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6697 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6700 manual_mount(int argc, char **argv)
6703 char mountpoint[ZFS_MAXPROPLEN];
6704 char mntopts[MNT_LINE_MAX] = { '\0' };
6708 char *dataset, *path;
6711 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6714 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6717 flags |= MS_OVERLAY;
6720 flags |= MS_NOMNTTAB;
6723 (void) fprintf(stderr, gettext("missing argument for "
6724 "'%c' option\n"), optopt);
6728 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6730 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6739 /* check that we only have two arguments */
6742 (void) fprintf(stderr, gettext("missing dataset "
6745 (void) fprintf(stderr,
6746 gettext("missing mountpoint argument\n"));
6748 (void) fprintf(stderr, gettext("too many arguments\n"));
6749 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6756 /* try to open the dataset */
6757 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6760 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6761 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6763 /* check for legacy mountpoint and complain appropriately */
6765 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6766 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6767 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6768 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6773 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6774 "mounted using 'mount -t zfs'\n"), dataset);
6775 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6776 "instead.\n"), path);
6777 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6778 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6779 (void) fprintf(stderr, gettext("See zfs(8) for more "
6788 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6789 * unmounts of non-legacy filesystems, as this is the dominant administrative
6793 manual_unmount(int argc, char **argv)
6799 while ((c = getopt(argc, argv, "f")) != -1) {
6805 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6807 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6816 /* check arguments */
6819 (void) fprintf(stderr, gettext("missing path "
6822 (void) fprintf(stderr, gettext("too many arguments\n"));
6823 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6827 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6831 find_command_idx(char *command, int *idx)
6835 for (i = 0; i < NCOMMAND; i++) {
6836 if (command_table[i].name == NULL)
6839 if (strcmp(command, command_table[i].name) == 0) {
6848 zfs_do_diff(int argc, char **argv)
6852 char *tosnap = NULL;
6853 char *fromsnap = NULL;
6858 while ((c = getopt(argc, argv, "FHt")) != -1) {
6861 flags |= ZFS_DIFF_CLASSIFY;
6864 flags |= ZFS_DIFF_PARSEABLE;
6867 flags |= ZFS_DIFF_TIMESTAMP;
6870 (void) fprintf(stderr,
6871 gettext("invalid option '%c'\n"), optopt);
6880 (void) fprintf(stderr,
6881 gettext("must provide at least one snapshot name\n"));
6886 (void) fprintf(stderr, gettext("too many arguments\n"));
6891 tosnap = (argc == 2) ? argv[1] : NULL;
6894 if (*fromsnap != '@')
6895 copy = strdup(fromsnap);
6897 copy = strdup(tosnap);
6901 if (atp = strchr(copy, '@'))
6904 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6910 * Ignore SIGPIPE so that the library can give us
6911 * information on any failure
6913 (void) sigignore(SIGPIPE);
6915 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6923 * zfs bookmark <fs@snap> <fs#bmark>
6925 * Creates a bookmark with the given name from the given snapshot.
6928 zfs_do_bookmark(int argc, char **argv)
6930 char snapname[ZFS_MAXNAMELEN];
6937 while ((c = getopt(argc, argv, "")) != -1) {
6940 (void) fprintf(stderr,
6941 gettext("invalid option '%c'\n"), optopt);
6949 /* check number of arguments */
6951 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6955 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6959 if (strchr(argv[1], '#') == NULL) {
6960 (void) fprintf(stderr,
6961 gettext("invalid bookmark name '%s' -- "
6962 "must contain a '#'\n"), argv[1]);
6966 if (argv[0][0] == '@') {
6968 * Snapshot name begins with @.
6969 * Default to same fs as bookmark.
6971 (void) strncpy(snapname, argv[1], sizeof (snapname));
6972 *strchr(snapname, '#') = '\0';
6973 (void) strlcat(snapname, argv[0], sizeof (snapname));
6975 (void) strncpy(snapname, argv[0], sizeof (snapname));
6977 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6983 nvl = fnvlist_alloc();
6984 fnvlist_add_string(nvl, argv[1], snapname);
6985 ret = lzc_bookmark(nvl, NULL);
6989 const char *err_msg;
6992 (void) snprintf(errbuf, sizeof (errbuf),
6993 dgettext(TEXT_DOMAIN,
6994 "cannot create bookmark '%s'"), argv[1]);
6998 err_msg = "bookmark is in a different pool";
7001 err_msg = "bookmark exists";
7004 err_msg = "invalid argument";
7007 err_msg = "bookmark feature not enabled";
7010 err_msg = "out of space";
7013 err_msg = "unknown error";
7016 (void) fprintf(stderr, "%s: %s\n", errbuf,
7017 dgettext(TEXT_DOMAIN, err_msg));
7028 main(int argc, char **argv)
7035 (void) setlocale(LC_ALL, "");
7036 (void) textdomain(TEXT_DOMAIN);
7040 if ((g_zfs = libzfs_init()) == NULL) {
7041 (void) fprintf(stderr, gettext("internal error: failed to "
7042 "initialize ZFS library\n"));
7046 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7048 libzfs_print_on_error(g_zfs, B_TRUE);
7050 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
7051 (void) fprintf(stderr, gettext("internal error: unable to "
7052 "open %s\n"), MNTTAB);
7057 * This command also doubles as the /etc/fs mount and unmount program.
7058 * Determine if we should take this behavior based on argv[0].
7060 progname = basename(argv[0]);
7061 if (strcmp(progname, "mount") == 0) {
7062 ret = manual_mount(argc, argv);
7063 } else if (strcmp(progname, "umount") == 0) {
7064 ret = manual_unmount(argc, argv);
7067 * Make sure the user has specified some command.
7070 (void) fprintf(stderr, gettext("missing command\n"));
7077 * The 'umount' command is an alias for 'unmount'
7079 if (strcmp(cmdname, "umount") == 0)
7080 cmdname = "unmount";
7083 * The 'recv' command is an alias for 'receive'
7085 if (strcmp(cmdname, "recv") == 0)
7086 cmdname = "receive";
7089 * The 'snap' command is an alias for 'snapshot'
7091 if (strcmp(cmdname, "snap") == 0)
7092 cmdname = "snapshot";
7097 if (strcmp(cmdname, "-?") == 0)
7101 * Run the appropriate command.
7103 libzfs_mnttab_cache(g_zfs, B_TRUE);
7104 if (find_command_idx(cmdname, &i) == 0) {
7105 current_command = &command_table[i];
7106 ret = command_table[i].func(argc - 1, argv + 1);
7107 } else if (strchr(cmdname, '=') != NULL) {
7108 verify(find_command_idx("set", &i) == 0);
7109 current_command = &command_table[i];
7110 ret = command_table[i].func(argc, argv);
7112 (void) fprintf(stderr, gettext("unrecognized "
7113 "command '%s'\n"), cmdname);
7116 libzfs_mnttab_cache(g_zfs, B_FALSE);
7119 (void) fclose(mnttab_file);
7121 if (ret == 0 && log_history)
7122 (void) zpool_log_history(g_zfs, history_str);
7127 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7128 * for the purposes of running ::findleaks.
7130 if (getenv("ZFS_ABORT") != NULL) {
7131 (void) printf("dumping core by request\n");