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 nvlist_t *real_props;
849 uint64_t spa_version;
851 zfs_prop_t resv_prop;
855 if (p = strchr(argv[0], '/'))
857 zpool_handle = zpool_open(g_zfs, argv[0]);
860 if (zpool_handle == NULL)
862 spa_version = zpool_get_prop_int(zpool_handle,
863 ZPOOL_PROP_VERSION, NULL);
864 zpool_close(zpool_handle);
865 if (spa_version >= SPA_VERSION_REFRESERVATION)
866 resv_prop = ZFS_PROP_REFRESERVATION;
868 resv_prop = ZFS_PROP_RESERVATION;
870 (void) snprintf(msg, sizeof (msg),
871 gettext("cannot create '%s'"), argv[0]);
872 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
873 props, 0, NULL, msg)) == NULL)
876 volsize = zvol_volsize_to_reservation(volsize, real_props);
877 nvlist_free(real_props);
879 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
881 if (nvlist_add_uint64(props,
882 zfs_prop_to_name(resv_prop), volsize) != 0) {
889 if (parents && zfs_name_valid(argv[0], type)) {
891 * Now create the ancestors of target dataset. If the target
892 * already exists and '-p' option was used we should not
895 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
899 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
904 if (zfs_create(g_zfs, argv[0], type, props) != 0)
907 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
913 * Mount and/or share the new filesystem as appropriate. We provide a
914 * verbose error message to let the user know that their filesystem was
915 * in fact created, even if we failed to mount or share it.
916 * If the user doesn't want the dataset automatically mounted,
917 * then skip the mount/share step altogether.
919 if (!nomount && should_auto_mount(zhp)) {
920 if (zfs_mount(zhp, NULL, 0) != 0) {
921 (void) fprintf(stderr, gettext("filesystem "
922 "successfully created, but not mounted\n"));
924 } else if (zfs_share(zhp) != 0) {
925 (void) fprintf(stderr, gettext("filesystem "
926 "successfully created, but not shared\n"));
943 * zfs destroy [-rRf] <fs, vol>
944 * zfs destroy [-rRd] <snap>
946 * -r Recursively destroy all children
947 * -R Recursively destroy all dependents, including clones
948 * -f Force unmounting of any dependents
949 * -d If we can't destroy now, mark for deferred destruction
951 * Destroys the given dataset. By default, it will unmount any filesystems,
952 * and refuse to destroy a dataset that has any dependents. A dependent can
953 * either be a child, or a clone of a child.
955 typedef struct destroy_cbdata {
958 boolean_t cb_recurse;
960 boolean_t cb_doclones;
961 zfs_handle_t *cb_target;
962 boolean_t cb_defer_destroy;
963 boolean_t cb_verbose;
964 boolean_t cb_parsable;
967 nvlist_t *cb_batchedsnaps;
969 /* first snap in contiguous run */
971 /* previous snap in contiguous run */
979 * Check for any dependents based on the '-r' or '-R' flags.
982 destroy_check_dependent(zfs_handle_t *zhp, void *data)
984 destroy_cbdata_t *cbp = data;
985 const char *tname = zfs_get_name(cbp->cb_target);
986 const char *name = zfs_get_name(zhp);
988 if (strncmp(tname, name, strlen(tname)) == 0 &&
989 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
991 * This is a direct descendant, not a clone somewhere else in
998 (void) fprintf(stderr, gettext("cannot destroy '%s': "
999 "%s has children\n"),
1000 zfs_get_name(cbp->cb_target),
1001 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1002 (void) fprintf(stderr, gettext("use '-r' to destroy "
1003 "the following datasets:\n"));
1004 cbp->cb_first = B_FALSE;
1005 cbp->cb_error = B_TRUE;
1008 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1011 * This is a clone. We only want to report this if the '-r'
1012 * wasn't specified, or the target is a snapshot.
1014 if (!cbp->cb_recurse &&
1015 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1018 if (cbp->cb_first) {
1019 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1020 "%s has dependent clones\n"),
1021 zfs_get_name(cbp->cb_target),
1022 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1023 (void) fprintf(stderr, gettext("use '-R' to destroy "
1024 "the following datasets:\n"));
1025 cbp->cb_first = B_FALSE;
1026 cbp->cb_error = B_TRUE;
1027 cbp->cb_dryrun = B_TRUE;
1030 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1039 destroy_callback(zfs_handle_t *zhp, void *data)
1041 destroy_cbdata_t *cb = data;
1042 const char *name = zfs_get_name(zhp);
1044 if (cb->cb_verbose) {
1045 if (cb->cb_parsable) {
1046 (void) printf("destroy\t%s\n", name);
1047 } else if (cb->cb_dryrun) {
1048 (void) printf(gettext("would destroy %s\n"),
1051 (void) printf(gettext("will destroy %s\n"),
1057 * Ignore pools (which we've already flagged as an error before getting
1060 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1061 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1065 if (cb->cb_dryrun) {
1071 * We batch up all contiguous snapshots (even of different
1072 * filesystems) and destroy them with one ioctl. We can't
1073 * simply do all snap deletions and then all fs deletions,
1074 * because we must delete a clone before its origin.
1076 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1077 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1079 int error = zfs_destroy_snaps_nvl(g_zfs,
1080 cb->cb_batchedsnaps, B_FALSE);
1081 fnvlist_free(cb->cb_batchedsnaps);
1082 cb->cb_batchedsnaps = fnvlist_alloc();
1085 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1086 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1097 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1099 destroy_cbdata_t *cb = arg;
1100 const char *name = zfs_get_name(zhp);
1103 if (nvlist_exists(cb->cb_nvl, name)) {
1104 if (cb->cb_firstsnap == NULL)
1105 cb->cb_firstsnap = strdup(name);
1106 if (cb->cb_prevsnap != NULL)
1107 free(cb->cb_prevsnap);
1108 /* this snap continues the current range */
1109 cb->cb_prevsnap = strdup(name);
1110 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1112 if (cb->cb_verbose) {
1113 if (cb->cb_parsable) {
1114 (void) printf("destroy\t%s\n", name);
1115 } else if (cb->cb_dryrun) {
1116 (void) printf(gettext("would destroy %s\n"),
1119 (void) printf(gettext("will destroy %s\n"),
1123 } else if (cb->cb_firstsnap != NULL) {
1124 /* end of this range */
1126 err = lzc_snaprange_space(cb->cb_firstsnap,
1127 cb->cb_prevsnap, &used);
1128 cb->cb_snapused += used;
1129 free(cb->cb_firstsnap);
1130 cb->cb_firstsnap = NULL;
1131 free(cb->cb_prevsnap);
1132 cb->cb_prevsnap = NULL;
1139 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1142 assert(cb->cb_firstsnap == NULL);
1143 assert(cb->cb_prevsnap == NULL);
1144 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1145 if (cb->cb_firstsnap != NULL) {
1148 err = lzc_snaprange_space(cb->cb_firstsnap,
1149 cb->cb_prevsnap, &used);
1151 cb->cb_snapused += used;
1152 free(cb->cb_firstsnap);
1153 cb->cb_firstsnap = NULL;
1154 free(cb->cb_prevsnap);
1155 cb->cb_prevsnap = NULL;
1161 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1163 destroy_cbdata_t *cb = arg;
1166 /* Check for clones. */
1167 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1168 cb->cb_target = zhp;
1169 cb->cb_first = B_TRUE;
1170 err = zfs_iter_dependents(zhp, B_TRUE,
1171 destroy_check_dependent, cb);
1175 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1183 gather_snapshots(zfs_handle_t *zhp, void *arg)
1185 destroy_cbdata_t *cb = arg;
1188 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1194 if (cb->cb_verbose) {
1195 err = destroy_print_snapshots(zhp, cb);
1201 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1209 destroy_clones(destroy_cbdata_t *cb)
1212 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1214 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1215 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1218 boolean_t defer = cb->cb_defer_destroy;
1222 * We can't defer destroy non-snapshots, so set it to
1223 * false while destroying the clones.
1225 cb->cb_defer_destroy = B_FALSE;
1226 err = zfs_iter_dependents(zhp, B_FALSE,
1227 destroy_callback, cb);
1228 cb->cb_defer_destroy = defer;
1238 zfs_do_destroy(int argc, char **argv)
1240 destroy_cbdata_t cb = { 0 };
1244 zfs_handle_t *zhp = NULL;
1246 zfs_type_t type = ZFS_TYPE_DATASET;
1249 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1252 cb.cb_verbose = B_TRUE;
1255 cb.cb_verbose = B_TRUE;
1256 cb.cb_parsable = B_TRUE;
1259 cb.cb_dryrun = B_TRUE;
1262 cb.cb_defer_destroy = B_TRUE;
1263 type = ZFS_TYPE_SNAPSHOT;
1266 cb.cb_force = B_TRUE;
1269 cb.cb_recurse = B_TRUE;
1272 cb.cb_recurse = B_TRUE;
1273 cb.cb_doclones = B_TRUE;
1277 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1286 /* check number of arguments */
1288 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1292 (void) fprintf(stderr, gettext("too many arguments\n"));
1296 at = strchr(argv[0], '@');
1297 pound = strchr(argv[0], '#');
1300 /* Build the list of snaps to destroy in cb_nvl. */
1301 cb.cb_nvl = fnvlist_alloc();
1304 zhp = zfs_open(g_zfs, argv[0],
1305 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1309 cb.cb_snapspec = at + 1;
1310 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1316 if (nvlist_empty(cb.cb_nvl)) {
1317 (void) fprintf(stderr, gettext("could not find any "
1318 "snapshots to destroy; check snapshot names.\n"));
1323 if (cb.cb_verbose) {
1325 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1326 if (cb.cb_parsable) {
1327 (void) printf("reclaim\t%llu\n",
1329 } else if (cb.cb_dryrun) {
1330 (void) printf(gettext("would reclaim %s\n"),
1333 (void) printf(gettext("will reclaim %s\n"),
1338 if (!cb.cb_dryrun) {
1339 if (cb.cb_doclones) {
1340 cb.cb_batchedsnaps = fnvlist_alloc();
1341 err = destroy_clones(&cb);
1343 err = zfs_destroy_snaps_nvl(g_zfs,
1344 cb.cb_batchedsnaps, B_FALSE);
1352 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1353 cb.cb_defer_destroy);
1359 } else if (pound != NULL) {
1364 (void) fprintf(stderr,
1365 "dryrun is not supported with bookmark\n");
1369 if (cb.cb_defer_destroy) {
1370 (void) fprintf(stderr,
1371 "defer destroy is not supported with bookmark\n");
1375 if (cb.cb_recurse) {
1376 (void) fprintf(stderr,
1377 "recursive is not supported with bookmark\n");
1381 if (!zfs_bookmark_exists(argv[0])) {
1382 (void) fprintf(stderr, gettext("bookmark '%s' "
1383 "does not exist.\n"), argv[0]);
1387 nvl = fnvlist_alloc();
1388 fnvlist_add_boolean(nvl, argv[0]);
1390 err = lzc_destroy_bookmarks(nvl, NULL);
1392 (void) zfs_standard_error(g_zfs, err,
1393 "cannot destroy bookmark");
1396 nvlist_free(cb.cb_nvl);
1400 /* Open the given dataset */
1401 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1407 * Perform an explicit check for pools before going any further.
1409 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1410 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1411 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1412 "operation does not apply to pools\n"),
1414 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1415 "%s' to destroy all datasets in the pool\n"),
1417 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1418 "to destroy the pool itself\n"), zfs_get_name(zhp));
1424 * Check for any dependents and/or clones.
1426 cb.cb_first = B_TRUE;
1427 if (!cb.cb_doclones &&
1428 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1439 cb.cb_batchedsnaps = fnvlist_alloc();
1440 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1447 * Do the real thing. The callback will close the
1448 * handle regardless of whether it succeeds or not.
1450 err = destroy_callback(zhp, &cb);
1453 err = zfs_destroy_snaps_nvl(g_zfs,
1454 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1461 fnvlist_free(cb.cb_batchedsnaps);
1462 fnvlist_free(cb.cb_nvl);
1469 is_recvd_column(zprop_get_cbdata_t *cbp)
1472 zfs_get_column_t col;
1474 for (i = 0; i < ZFS_GET_NCOLS &&
1475 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1476 if (col == GET_COL_RECVD)
1482 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1483 * < all | property[,property]... > < fs | snap | vol > ...
1485 * -r recurse over any child datasets
1486 * -H scripted mode. Headers are stripped, and fields are separated
1487 * by tabs instead of spaces.
1488 * -o Set of fields to display. One of "name,property,value,
1489 * received,source". Default is "name,property,value,source".
1490 * "all" is an alias for all five.
1491 * -s Set of sources to allow. One of
1492 * "local,default,inherited,received,temporary,none". Default is
1494 * -p Display values in parsable (literal) format.
1496 * Prints properties for the given datasets. The user can control which
1497 * columns to display as well as which property types to allow.
1501 * Invoked to display the properties for a single dataset.
1504 get_callback(zfs_handle_t *zhp, void *data)
1506 char buf[ZFS_MAXPROPLEN];
1507 char rbuf[ZFS_MAXPROPLEN];
1508 zprop_source_t sourcetype;
1509 char source[ZFS_MAXNAMELEN];
1510 zprop_get_cbdata_t *cbp = data;
1511 nvlist_t *user_props = zfs_get_user_props(zhp);
1512 zprop_list_t *pl = cbp->cb_proplist;
1516 boolean_t received = is_recvd_column(cbp);
1518 for (; pl != NULL; pl = pl->pl_next) {
1519 char *recvdval = NULL;
1521 * Skip the special fake placeholder. This will also skip over
1522 * the name property when 'all' is specified.
1524 if (pl->pl_prop == ZFS_PROP_NAME &&
1525 pl == cbp->cb_proplist)
1528 if (pl->pl_prop != ZPROP_INVAL) {
1529 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1530 sizeof (buf), &sourcetype, source,
1532 cbp->cb_literal) != 0) {
1535 if (!zfs_prop_valid_for_type(pl->pl_prop,
1536 ZFS_TYPE_DATASET)) {
1537 (void) fprintf(stderr,
1538 gettext("No such property '%s'\n"),
1539 zfs_prop_to_name(pl->pl_prop));
1542 sourcetype = ZPROP_SRC_NONE;
1543 (void) strlcpy(buf, "-", sizeof (buf));
1546 if (received && (zfs_prop_get_recvd(zhp,
1547 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1548 cbp->cb_literal) == 0))
1551 zprop_print_one_property(zfs_get_name(zhp), cbp,
1552 zfs_prop_to_name(pl->pl_prop),
1553 buf, sourcetype, source, recvdval);
1554 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1555 sourcetype = ZPROP_SRC_LOCAL;
1557 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1558 buf, sizeof (buf), cbp->cb_literal) != 0) {
1559 sourcetype = ZPROP_SRC_NONE;
1560 (void) strlcpy(buf, "-", sizeof (buf));
1563 zprop_print_one_property(zfs_get_name(zhp), cbp,
1564 pl->pl_user_prop, buf, sourcetype, source, NULL);
1565 } else if (zfs_prop_written(pl->pl_user_prop)) {
1566 sourcetype = ZPROP_SRC_LOCAL;
1568 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1569 buf, sizeof (buf), cbp->cb_literal) != 0) {
1570 sourcetype = ZPROP_SRC_NONE;
1571 (void) strlcpy(buf, "-", sizeof (buf));
1574 zprop_print_one_property(zfs_get_name(zhp), cbp,
1575 pl->pl_user_prop, buf, sourcetype, source, NULL);
1577 if (nvlist_lookup_nvlist(user_props,
1578 pl->pl_user_prop, &propval) != 0) {
1581 sourcetype = ZPROP_SRC_NONE;
1584 verify(nvlist_lookup_string(propval,
1585 ZPROP_VALUE, &strval) == 0);
1586 verify(nvlist_lookup_string(propval,
1587 ZPROP_SOURCE, &sourceval) == 0);
1589 if (strcmp(sourceval,
1590 zfs_get_name(zhp)) == 0) {
1591 sourcetype = ZPROP_SRC_LOCAL;
1592 } else if (strcmp(sourceval,
1593 ZPROP_SOURCE_VAL_RECVD) == 0) {
1594 sourcetype = ZPROP_SRC_RECEIVED;
1596 sourcetype = ZPROP_SRC_INHERITED;
1597 (void) strlcpy(source,
1598 sourceval, sizeof (source));
1602 if (received && (zfs_prop_get_recvd(zhp,
1603 pl->pl_user_prop, rbuf, sizeof (rbuf),
1604 cbp->cb_literal) == 0))
1607 zprop_print_one_property(zfs_get_name(zhp), cbp,
1608 pl->pl_user_prop, strval, sourcetype,
1617 zfs_do_get(int argc, char **argv)
1619 zprop_get_cbdata_t cb = { 0 };
1620 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1621 int types = ZFS_TYPE_DATASET;
1622 char *value, *fields;
1625 zprop_list_t fake_name = { 0 };
1628 * Set up default columns and sources.
1630 cb.cb_sources = ZPROP_SRC_ALL;
1631 cb.cb_columns[0] = GET_COL_NAME;
1632 cb.cb_columns[1] = GET_COL_PROPERTY;
1633 cb.cb_columns[2] = GET_COL_VALUE;
1634 cb.cb_columns[3] = GET_COL_SOURCE;
1635 cb.cb_type = ZFS_TYPE_DATASET;
1638 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1641 cb.cb_literal = B_TRUE;
1644 limit = parse_depth(optarg, &flags);
1647 flags |= ZFS_ITER_RECURSE;
1650 cb.cb_scripted = B_TRUE;
1653 (void) fprintf(stderr, gettext("missing argument for "
1654 "'%c' option\n"), optopt);
1659 * Process the set of columns to display. We zero out
1660 * the structure to give us a blank slate.
1662 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1664 while (*optarg != '\0') {
1665 static char *col_subopts[] =
1666 { "name", "property", "value", "received",
1667 "source", "all", NULL };
1669 if (i == ZFS_GET_NCOLS) {
1670 (void) fprintf(stderr, gettext("too "
1671 "many fields given to -o "
1676 switch (getsubopt(&optarg, col_subopts,
1679 cb.cb_columns[i++] = GET_COL_NAME;
1682 cb.cb_columns[i++] = GET_COL_PROPERTY;
1685 cb.cb_columns[i++] = GET_COL_VALUE;
1688 cb.cb_columns[i++] = GET_COL_RECVD;
1689 flags |= ZFS_ITER_RECVD_PROPS;
1692 cb.cb_columns[i++] = GET_COL_SOURCE;
1696 (void) fprintf(stderr,
1697 gettext("\"all\" conflicts "
1698 "with specific fields "
1699 "given to -o option\n"));
1702 cb.cb_columns[0] = GET_COL_NAME;
1703 cb.cb_columns[1] = GET_COL_PROPERTY;
1704 cb.cb_columns[2] = GET_COL_VALUE;
1705 cb.cb_columns[3] = GET_COL_RECVD;
1706 cb.cb_columns[4] = GET_COL_SOURCE;
1707 flags |= ZFS_ITER_RECVD_PROPS;
1711 (void) fprintf(stderr,
1712 gettext("invalid column name "
1721 while (*optarg != '\0') {
1722 static char *source_subopts[] = {
1723 "local", "default", "inherited",
1724 "received", "temporary", "none",
1727 switch (getsubopt(&optarg, source_subopts,
1730 cb.cb_sources |= ZPROP_SRC_LOCAL;
1733 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1736 cb.cb_sources |= ZPROP_SRC_INHERITED;
1739 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1742 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1745 cb.cb_sources |= ZPROP_SRC_NONE;
1748 (void) fprintf(stderr,
1749 gettext("invalid source "
1758 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1759 while (*optarg != '\0') {
1760 static char *type_subopts[] = { "filesystem",
1761 "volume", "snapshot", "bookmark",
1764 switch (getsubopt(&optarg, type_subopts,
1767 types |= ZFS_TYPE_FILESYSTEM;
1770 types |= ZFS_TYPE_VOLUME;
1773 types |= ZFS_TYPE_SNAPSHOT;
1776 types |= ZFS_TYPE_BOOKMARK;
1779 types = ZFS_TYPE_DATASET |
1784 (void) fprintf(stderr,
1785 gettext("invalid type '%s'\n"),
1793 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1803 (void) fprintf(stderr, gettext("missing property "
1810 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1818 * As part of zfs_expand_proplist(), we keep track of the maximum column
1819 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1820 * need to know the maximum name length. However, the user likely did
1821 * not specify 'name' as one of the properties to fetch, so we need to
1822 * make sure we always include at least this property for
1823 * print_get_headers() to work properly.
1825 if (cb.cb_proplist != NULL) {
1826 fake_name.pl_prop = ZFS_PROP_NAME;
1827 fake_name.pl_width = strlen(gettext("NAME"));
1828 fake_name.pl_next = cb.cb_proplist;
1829 cb.cb_proplist = &fake_name;
1832 cb.cb_first = B_TRUE;
1834 /* run for each object */
1835 ret = zfs_for_each(argc, argv, flags, types, NULL,
1836 &cb.cb_proplist, limit, get_callback, &cb);
1838 if (cb.cb_proplist == &fake_name)
1839 zprop_free_list(fake_name.pl_next);
1841 zprop_free_list(cb.cb_proplist);
1847 * inherit [-rS] <property> <fs|vol> ...
1849 * -r Recurse over all children
1850 * -S Revert to received value, if any
1852 * For each dataset specified on the command line, inherit the given property
1853 * from its parent. Inheriting a property at the pool level will cause it to
1854 * use the default value. The '-r' flag will recurse over all children, and is
1855 * useful for setting a property on a hierarchy-wide basis, regardless of any
1856 * local modifications for each dataset.
1859 typedef struct inherit_cbdata {
1860 const char *cb_propname;
1861 boolean_t cb_received;
1865 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1867 inherit_cbdata_t *cb = data;
1868 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1871 * If we're doing it recursively, then ignore properties that
1872 * are not valid for this type of dataset.
1874 if (prop != ZPROP_INVAL &&
1875 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1878 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1882 inherit_cb(zfs_handle_t *zhp, void *data)
1884 inherit_cbdata_t *cb = data;
1886 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1890 zfs_do_inherit(int argc, char **argv)
1894 inherit_cbdata_t cb = { 0 };
1898 boolean_t received = B_FALSE;
1901 while ((c = getopt(argc, argv, "rS")) != -1) {
1904 flags |= ZFS_ITER_RECURSE;
1911 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1920 /* check number of arguments */
1922 (void) fprintf(stderr, gettext("missing property argument\n"));
1926 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1934 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1935 if (zfs_prop_readonly(prop)) {
1936 (void) fprintf(stderr, gettext(
1937 "%s property is read-only\n"),
1941 if (!zfs_prop_inheritable(prop) && !received) {
1942 (void) fprintf(stderr, gettext("'%s' property cannot "
1943 "be inherited\n"), propname);
1944 if (prop == ZFS_PROP_QUOTA ||
1945 prop == ZFS_PROP_RESERVATION ||
1946 prop == ZFS_PROP_REFQUOTA ||
1947 prop == ZFS_PROP_REFRESERVATION) {
1948 (void) fprintf(stderr, gettext("use 'zfs set "
1949 "%s=none' to clear\n"), propname);
1950 (void) fprintf(stderr, gettext("use 'zfs "
1951 "inherit -S %s' to revert to received "
1952 "value\n"), propname);
1956 if (received && (prop == ZFS_PROP_VOLSIZE ||
1957 prop == ZFS_PROP_VERSION)) {
1958 (void) fprintf(stderr, gettext("'%s' property cannot "
1959 "be reverted to a received value\n"), propname);
1962 } else if (!zfs_prop_user(propname)) {
1963 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1968 cb.cb_propname = propname;
1969 cb.cb_received = received;
1971 if (flags & ZFS_ITER_RECURSE) {
1972 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1973 NULL, NULL, 0, inherit_recurse_cb, &cb);
1975 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1976 NULL, NULL, 0, inherit_cb, &cb);
1982 typedef struct upgrade_cbdata {
1983 uint64_t cb_numupgraded;
1984 uint64_t cb_numsamegraded;
1985 uint64_t cb_numfailed;
1986 uint64_t cb_version;
1988 boolean_t cb_foundone;
1989 char cb_lastfs[ZFS_MAXNAMELEN];
1993 same_pool(zfs_handle_t *zhp, const char *name)
1995 int len1 = strcspn(name, "/@");
1996 const char *zhname = zfs_get_name(zhp);
1997 int len2 = strcspn(zhname, "/@");
2001 return (strncmp(name, zhname, len1) == 0);
2005 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2007 upgrade_cbdata_t *cb = data;
2008 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2010 /* list if it's old/new */
2011 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2012 (cb->cb_newer && version > ZPL_VERSION)) {
2015 str = gettext("The following filesystems are "
2016 "formatted using a newer software version and\n"
2017 "cannot be accessed on the current system.\n\n");
2019 str = gettext("The following filesystems are "
2020 "out of date, and can be upgraded. After being\n"
2021 "upgraded, these filesystems (and any 'zfs send' "
2022 "streams generated from\n"
2023 "subsequent snapshots) will no longer be "
2024 "accessible by older software versions.\n\n");
2027 if (!cb->cb_foundone) {
2029 (void) printf(gettext("VER FILESYSTEM\n"));
2030 (void) printf(gettext("--- ------------\n"));
2031 cb->cb_foundone = B_TRUE;
2034 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2041 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2043 upgrade_cbdata_t *cb = data;
2044 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2045 int needed_spa_version;
2048 if (zfs_spa_version(zhp, &spa_version) < 0)
2051 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2053 if (needed_spa_version < 0)
2056 if (spa_version < needed_spa_version) {
2058 (void) printf(gettext("%s: can not be "
2059 "upgraded; the pool version needs to first "
2060 "be upgraded\nto version %d\n\n"),
2061 zfs_get_name(zhp), needed_spa_version);
2067 if (version < cb->cb_version) {
2069 (void) snprintf(verstr, sizeof (verstr),
2070 "%llu", cb->cb_version);
2071 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2073 * If they did "zfs upgrade -a", then we could
2074 * be doing ioctls to different pools. We need
2075 * to log this history once to each pool, and bypass
2076 * the normal history logging that happens in main().
2078 (void) zpool_log_history(g_zfs, history_str);
2079 log_history = B_FALSE;
2081 if (zfs_prop_set(zhp, "version", verstr) == 0)
2082 cb->cb_numupgraded++;
2085 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2086 } else if (version > cb->cb_version) {
2087 /* can't downgrade */
2088 (void) printf(gettext("%s: can not be downgraded; "
2089 "it is already at version %u\n"),
2090 zfs_get_name(zhp), version);
2093 cb->cb_numsamegraded++;
2101 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2104 zfs_do_upgrade(int argc, char **argv)
2106 boolean_t all = B_FALSE;
2107 boolean_t showversions = B_FALSE;
2109 upgrade_cbdata_t cb = { 0 };
2111 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2114 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2117 flags |= ZFS_ITER_RECURSE;
2120 showversions = B_TRUE;
2123 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2124 optarg, &cb.cb_version) != 0) {
2125 (void) fprintf(stderr,
2126 gettext("invalid version %s\n"), optarg);
2135 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2144 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2146 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2147 cb.cb_version || argc))
2149 if ((all || argc) && (showversions))
2155 /* Show info on available versions. */
2156 (void) printf(gettext("The following filesystem versions are "
2158 (void) printf(gettext("VER DESCRIPTION\n"));
2159 (void) printf("--- -----------------------------------------"
2160 "---------------\n");
2161 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2162 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2163 (void) printf(gettext(" 3 Case insensitive and filesystem "
2164 "user identifier (FUID)\n"));
2165 (void) printf(gettext(" 4 userquota, groupquota "
2167 (void) printf(gettext(" 5 System attributes\n"));
2168 (void) printf(gettext("\nFor more information on a particular "
2169 "version, including supported releases,\n"));
2170 (void) printf("see the ZFS Administration Guide.\n\n");
2172 } else if (argc || all) {
2173 /* Upgrade filesystems */
2174 if (cb.cb_version == 0)
2175 cb.cb_version = ZPL_VERSION;
2176 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2177 NULL, NULL, 0, upgrade_set_callback, &cb);
2178 (void) printf(gettext("%llu filesystems upgraded\n"),
2180 if (cb.cb_numsamegraded) {
2181 (void) printf(gettext("%llu filesystems already at "
2183 cb.cb_numsamegraded);
2185 if (cb.cb_numfailed != 0)
2188 /* List old-version filesytems */
2190 (void) printf(gettext("This system is currently running "
2191 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2193 flags |= ZFS_ITER_RECURSE;
2194 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2195 NULL, NULL, 0, upgrade_list_callback, &cb);
2197 found = cb.cb_foundone;
2198 cb.cb_foundone = B_FALSE;
2199 cb.cb_newer = B_TRUE;
2201 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2202 NULL, NULL, 0, upgrade_list_callback, &cb);
2204 if (!cb.cb_foundone && !found) {
2205 (void) printf(gettext("All filesystems are "
2206 "formatted with the current version.\n"));
2214 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2215 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2216 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2217 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2219 * -H Scripted mode; elide headers and separate columns by tabs.
2220 * -i Translate SID to POSIX ID.
2221 * -n Print numeric ID instead of user/group name.
2222 * -o Control which fields to display.
2223 * -p Use exact (parsable) numeric output.
2224 * -s Specify sort columns, descending order.
2225 * -S Specify sort columns, ascending order.
2226 * -t Control which object types to display.
2228 * Displays space consumed by, and quotas on, each user in the specified
2229 * filesystem or snapshot.
2232 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2233 enum us_field_types {
2239 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2240 static char *us_field_names[] = { "type", "name", "used", "quota" };
2241 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2243 #define USTYPE_PSX_GRP (1 << 0)
2244 #define USTYPE_PSX_USR (1 << 1)
2245 #define USTYPE_SMB_GRP (1 << 2)
2246 #define USTYPE_SMB_USR (1 << 3)
2247 #define USTYPE_ALL \
2248 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2250 static int us_type_bits[] = {
2257 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2260 typedef struct us_node {
2262 uu_avl_node_t usn_avlnode;
2263 uu_list_node_t usn_listnode;
2266 typedef struct us_cbdata {
2268 uu_avl_pool_t *cb_avl_pool;
2270 boolean_t cb_numname;
2271 boolean_t cb_nicenum;
2272 boolean_t cb_sid2posix;
2273 zfs_userquota_prop_t cb_prop;
2274 zfs_sort_column_t *cb_sortcol;
2275 size_t cb_width[USFIELD_LAST];
2278 static boolean_t us_populated = B_FALSE;
2281 zfs_sort_column_t *si_sortcol;
2282 boolean_t si_numname;
2286 us_field_index(char *field)
2290 for (i = 0; i < USFIELD_LAST; i++) {
2291 if (strcmp(field, us_field_names[i]) == 0)
2299 us_compare(const void *larg, const void *rarg, void *unused)
2301 const us_node_t *l = larg;
2302 const us_node_t *r = rarg;
2303 us_sort_info_t *si = (us_sort_info_t *)unused;
2304 zfs_sort_column_t *sortcol = si->si_sortcol;
2305 boolean_t numname = si->si_numname;
2306 nvlist_t *lnvl = l->usn_nvl;
2307 nvlist_t *rnvl = r->usn_nvl;
2311 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2318 zfs_prop_t prop = sortcol->sc_prop;
2319 const char *propname = NULL;
2320 boolean_t reverse = sortcol->sc_reverse;
2325 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2326 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2328 rc = (rv32 < lv32) ? 1 : -1;
2333 (void) nvlist_lookup_uint64(lnvl, propname,
2335 (void) nvlist_lookup_uint64(rnvl, propname,
2338 rc = (rv64 < lv64) ? 1 : -1;
2340 (void) nvlist_lookup_string(lnvl, propname,
2342 (void) nvlist_lookup_string(rnvl, propname,
2344 rc = strcmp(lvstr, rvstr);
2348 case ZFS_PROP_QUOTA:
2351 if (prop == ZFS_PROP_USED)
2355 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2356 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2358 rc = (rv64 < lv64) ? 1 : -1;
2364 return (reverse ? 1 : -1);
2366 return (reverse ? -1 : 1);
2371 * If entries still seem to be the same, check if they are of the same
2372 * type (smbentity is added only if we are doing SID to POSIX ID
2373 * translation where we can have duplicate type/name combinations).
2375 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2376 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2378 return (lvb < rvb ? -1 : 1);
2383 static inline const char *
2384 us_type2str(unsigned field_type)
2386 switch (field_type) {
2387 case USTYPE_PSX_USR:
2388 return ("POSIX User");
2389 case USTYPE_PSX_GRP:
2390 return ("POSIX Group");
2391 case USTYPE_SMB_USR:
2392 return ("SMB User");
2393 case USTYPE_SMB_GRP:
2394 return ("SMB Group");
2396 return ("Undefined");
2401 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2403 us_cbdata_t *cb = (us_cbdata_t *)arg;
2404 zfs_userquota_prop_t prop = cb->cb_prop;
2409 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2410 uu_avl_t *avl = cb->cb_avl;
2414 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2416 const char *typestr;
2420 int typeidx, nameidx, sizeidx;
2421 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2422 boolean_t smbentity = B_FALSE;
2424 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2426 node = safe_malloc(sizeof (us_node_t));
2427 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2428 node->usn_nvl = props;
2430 if (domain != NULL && domain[0] != '\0') {
2432 char sid[ZFS_MAXNAMELEN + 32];
2436 int flag = IDMAP_REQ_FLG_USE_CACHE;
2441 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2443 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2444 type = USTYPE_SMB_GRP;
2446 err = sid_to_id(sid, B_FALSE, &id);
2449 type = USTYPE_SMB_USR;
2451 err = sid_to_id(sid, B_TRUE, &id);
2458 if (!cb->cb_sid2posix) {
2459 if (type == USTYPE_SMB_USR) {
2460 (void) idmap_getwinnamebyuid(rid, flag,
2463 (void) idmap_getwinnamebygid(rid, flag,
2473 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2475 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2476 type = USTYPE_PSX_GRP;
2477 if (!cb->cb_numname) {
2480 if ((g = getgrgid(rid)) != NULL)
2484 type = USTYPE_PSX_USR;
2485 if (!cb->cb_numname) {
2488 if ((p = getpwuid(rid)) != NULL)
2495 * Make sure that the type/name combination is unique when doing
2496 * SID to POSIX ID translation (hence changing the type from SMB to
2499 if (cb->cb_sid2posix &&
2500 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2503 /* Calculate/update width of TYPE field */
2504 typestr = us_type2str(type);
2505 typelen = strlen(gettext(typestr));
2506 typeidx = us_field_index("type");
2507 if (typelen > cb->cb_width[typeidx])
2508 cb->cb_width[typeidx] = typelen;
2509 if (nvlist_add_uint32(props, "type", type) != 0)
2512 /* Calculate/update width of NAME field */
2513 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2514 if (nvlist_add_uint64(props, "name", rid) != 0)
2516 namelen = snprintf(NULL, 0, "%u", rid);
2518 if (nvlist_add_string(props, "name", name) != 0)
2520 namelen = strlen(name);
2522 nameidx = us_field_index("name");
2523 if (namelen > cb->cb_width[nameidx])
2524 cb->cb_width[nameidx] = namelen;
2527 * Check if this type/name combination is in the list and update it;
2528 * otherwise add new node to the list.
2530 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2531 uu_avl_insert(avl, node, idx);
2536 props = node->usn_nvl;
2539 /* Calculate/update width of USED/QUOTA fields */
2541 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2543 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2544 sizelen = strlen(sizebuf);
2545 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2547 if (!nvlist_exists(props, "quota"))
2548 (void) nvlist_add_uint64(props, "quota", 0);
2551 if (!nvlist_exists(props, "used"))
2552 (void) nvlist_add_uint64(props, "used", 0);
2554 sizeidx = us_field_index(propname);
2555 if (sizelen > cb->cb_width[sizeidx])
2556 cb->cb_width[sizeidx] = sizelen;
2558 if (nvlist_add_uint64(props, propname, space) != 0)
2565 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2566 size_t *width, us_node_t *node)
2568 nvlist_t *nvl = node->usn_nvl;
2569 char valstr[ZFS_MAXNAMELEN];
2570 boolean_t first = B_TRUE;
2576 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2577 if (!(ustype & types))
2580 while ((field = fields[cfield]) != USFIELD_LAST) {
2581 nvpair_t *nvp = NULL;
2585 char *strval = NULL;
2587 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2588 if (strcmp(nvpair_name(nvp),
2589 us_field_names[field]) == 0)
2593 type = nvpair_type(nvp);
2595 case DATA_TYPE_UINT32:
2596 (void) nvpair_value_uint32(nvp, &val32);
2598 case DATA_TYPE_UINT64:
2599 (void) nvpair_value_uint64(nvp, &val64);
2601 case DATA_TYPE_STRING:
2602 (void) nvpair_value_string(nvp, &strval);
2605 (void) fprintf(stderr, "invalid data type\n");
2610 strval = (char *)us_type2str(val32);
2613 if (type == DATA_TYPE_UINT64) {
2614 (void) sprintf(valstr, "%llu", val64);
2620 if (type == DATA_TYPE_UINT64) {
2622 (void) sprintf(valstr, "%llu", val64);
2624 zfs_nicenum(val64, valstr,
2627 if (field == USFIELD_QUOTA &&
2628 strcmp(valstr, "0") == 0)
2638 (void) printf("\t");
2643 (void) printf("%s", strval);
2644 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2645 (void) printf("%-*s", width[field], strval);
2647 (void) printf("%*s", width[field], strval);
2653 (void) printf("\n");
2657 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2658 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2666 boolean_t first = B_TRUE;
2668 while ((field = fields[cfield]) != USFIELD_LAST) {
2669 col = gettext(us_field_hdr[field]);
2670 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2671 (void) printf(first ? "%-*s" : " %-*s",
2674 (void) printf(first ? "%*s" : " %*s",
2680 (void) printf("\n");
2683 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2684 print_us_node(scripted, parsable, fields, types, width, node);
2686 nvlist_free(node->usn_nvl);
2691 zfs_do_userspace(int argc, char **argv)
2694 zfs_userquota_prop_t p;
2696 uu_avl_pool_t *avl_pool;
2698 uu_avl_walk_t *walk;
2700 char deffields[] = "type,name,used,quota";
2701 char *ofield = NULL;
2702 char *tfield = NULL;
2706 boolean_t scripted = B_FALSE;
2707 boolean_t prtnum = B_FALSE;
2708 boolean_t parsable = B_FALSE;
2709 boolean_t sid2posix = B_FALSE;
2712 zfs_sort_column_t *sortcol = NULL;
2713 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2717 uu_list_pool_t *listpool;
2719 uu_avl_index_t idx = 0;
2720 uu_list_index_t idx2 = 0;
2725 if (strcmp(argv[0], "groupspace") == 0)
2726 /* Toggle default group types */
2727 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2729 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2745 if (zfs_add_sort_column(&sortcol, optarg,
2746 c == 's' ? B_FALSE : B_TRUE) != 0) {
2747 (void) fprintf(stderr,
2748 gettext("invalid field '%s'\n"), optarg);
2759 (void) fprintf(stderr, gettext("missing argument for "
2760 "'%c' option\n"), optopt);
2764 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2774 (void) fprintf(stderr, gettext("missing dataset name\n"));
2778 (void) fprintf(stderr, gettext("too many arguments\n"));
2782 /* Use default output fields if not specified using -o */
2786 if ((delim = strchr(ofield, ',')) != NULL)
2788 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2789 (void) fprintf(stderr, gettext("invalid type '%s' "
2790 "for -o option\n"), ofield);
2795 } while (delim != NULL);
2796 fields[cfield] = USFIELD_LAST;
2798 /* Override output types (-t option) */
2799 if (tfield != NULL) {
2803 boolean_t found = B_FALSE;
2805 if ((delim = strchr(tfield, ',')) != NULL)
2807 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2809 if (strcmp(tfield, us_type_names[i]) == 0) {
2811 types |= us_type_bits[i];
2816 (void) fprintf(stderr, gettext("invalid type "
2817 "'%s' for -t option\n"), tfield);
2822 } while (delim != NULL);
2825 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2828 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2829 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2831 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2834 /* Always add default sorting columns */
2835 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2836 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2838 cb.cb_sortcol = sortcol;
2839 cb.cb_numname = prtnum;
2840 cb.cb_nicenum = !parsable;
2841 cb.cb_avl_pool = avl_pool;
2842 cb.cb_avl = avl_tree;
2843 cb.cb_sid2posix = sid2posix;
2845 for (i = 0; i < USFIELD_LAST; i++)
2846 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2848 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2849 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2850 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2851 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2852 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2855 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2860 if ((node = uu_avl_first(avl_tree)) == NULL)
2863 us_populated = B_TRUE;
2865 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2866 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2867 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2868 uu_list_node_init(node, &node->usn_listnode, listpool);
2870 while (node != NULL) {
2872 node = uu_avl_next(avl_tree, node);
2873 uu_avl_remove(avl_tree, rmnode);
2874 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2875 uu_list_insert(list, rmnode, idx2);
2878 for (node = uu_list_first(list); node != NULL;
2879 node = uu_list_next(list, node)) {
2880 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2882 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2883 uu_avl_insert(avl_tree, node, idx);
2886 uu_list_destroy(list);
2887 uu_list_pool_destroy(listpool);
2889 /* Print and free node nvlist memory */
2890 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2893 zfs_free_sort_columns(sortcol);
2895 /* Clean up the AVL tree */
2896 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2899 while ((node = uu_avl_walk_next(walk)) != NULL) {
2900 uu_avl_remove(cb.cb_avl, node);
2904 uu_avl_walk_end(walk);
2905 uu_avl_destroy(avl_tree);
2906 uu_avl_pool_destroy(avl_pool);
2912 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2913 * [-t type[,...]] [filesystem|volume|snapshot] ...
2915 * -H Scripted mode; elide headers and separate columns by tabs.
2916 * -p Display values in parsable (literal) format.
2917 * -r Recurse over all children.
2918 * -d Limit recursion by depth.
2919 * -o Control which fields to display.
2920 * -s Specify sort columns, descending order.
2921 * -S Specify sort columns, ascending order.
2922 * -t Control which object types to display.
2924 * When given no arguments, list all filesystems in the system.
2925 * Otherwise, list the specified datasets, optionally recursing down them if
2926 * '-r' is specified.
2928 typedef struct list_cbdata {
2930 boolean_t cb_literal;
2931 boolean_t cb_scripted;
2932 zprop_list_t *cb_proplist;
2936 * Given a list of columns to display, output appropriate headers for each one.
2939 print_header(list_cbdata_t *cb)
2941 zprop_list_t *pl = cb->cb_proplist;
2942 char headerbuf[ZFS_MAXPROPLEN];
2945 boolean_t first = B_TRUE;
2946 boolean_t right_justify;
2948 for (; pl != NULL; pl = pl->pl_next) {
2955 right_justify = B_FALSE;
2956 if (pl->pl_prop != ZPROP_INVAL) {
2957 header = zfs_prop_column_name(pl->pl_prop);
2958 right_justify = zfs_prop_align_right(pl->pl_prop);
2960 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2961 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2962 headerbuf[i] = '\0';
2966 if (pl->pl_next == NULL && !right_justify)
2967 (void) printf("%s", header);
2968 else if (right_justify)
2969 (void) printf("%*s", pl->pl_width, header);
2971 (void) printf("%-*s", pl->pl_width, header);
2974 (void) printf("\n");
2978 * Given a dataset and a list of fields, print out all the properties according
2979 * to the described layout.
2982 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2984 zprop_list_t *pl = cb->cb_proplist;
2985 boolean_t first = B_TRUE;
2986 char property[ZFS_MAXPROPLEN];
2987 nvlist_t *userprops = zfs_get_user_props(zhp);
2990 boolean_t right_justify;
2992 for (; pl != NULL; pl = pl->pl_next) {
2994 if (cb->cb_scripted)
2995 (void) printf("\t");
3002 if (pl->pl_prop == ZFS_PROP_NAME) {
3003 (void) strlcpy(property, zfs_get_name(zhp),
3006 right_justify = zfs_prop_align_right(pl->pl_prop);
3007 } else if (pl->pl_prop != ZPROP_INVAL) {
3008 if (zfs_prop_get(zhp, pl->pl_prop, property,
3009 sizeof (property), NULL, NULL, 0,
3010 cb->cb_literal) != 0)
3014 right_justify = zfs_prop_align_right(pl->pl_prop);
3015 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3016 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3017 property, sizeof (property), cb->cb_literal) != 0)
3021 right_justify = B_TRUE;
3022 } else if (zfs_prop_written(pl->pl_user_prop)) {
3023 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3024 property, sizeof (property), cb->cb_literal) != 0)
3028 right_justify = B_TRUE;
3030 if (nvlist_lookup_nvlist(userprops,
3031 pl->pl_user_prop, &propval) != 0)
3034 verify(nvlist_lookup_string(propval,
3035 ZPROP_VALUE, &propstr) == 0);
3036 right_justify = B_FALSE;
3040 * If this is being called in scripted mode, or if this is the
3041 * last column and it is left-justified, don't include a width
3044 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3045 (void) printf("%s", propstr);
3046 else if (right_justify)
3047 (void) printf("%*s", pl->pl_width, propstr);
3049 (void) printf("%-*s", pl->pl_width, propstr);
3052 (void) printf("\n");
3056 * Generic callback function to list a dataset or snapshot.
3059 list_callback(zfs_handle_t *zhp, void *data)
3061 list_cbdata_t *cbp = data;
3063 if (cbp->cb_first) {
3064 if (!cbp->cb_scripted)
3066 cbp->cb_first = B_FALSE;
3069 print_dataset(zhp, cbp);
3075 zfs_do_list(int argc, char **argv)
3078 static char default_fields[] =
3079 "name,used,available,referenced,mountpoint";
3080 int types = ZFS_TYPE_DATASET;
3081 boolean_t types_specified = B_FALSE;
3082 char *fields = NULL;
3083 list_cbdata_t cb = { 0 };
3087 zfs_sort_column_t *sortcol = NULL;
3088 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3091 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3097 cb.cb_literal = B_TRUE;
3098 flags |= ZFS_ITER_LITERAL_PROPS;
3101 limit = parse_depth(optarg, &flags);
3104 flags |= ZFS_ITER_RECURSE;
3107 cb.cb_scripted = B_TRUE;
3110 if (zfs_add_sort_column(&sortcol, optarg,
3112 (void) fprintf(stderr,
3113 gettext("invalid property '%s'\n"), optarg);
3118 if (zfs_add_sort_column(&sortcol, optarg,
3120 (void) fprintf(stderr,
3121 gettext("invalid property '%s'\n"), optarg);
3127 types_specified = B_TRUE;
3128 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3129 while (*optarg != '\0') {
3130 static char *type_subopts[] = { "filesystem",
3131 "volume", "snapshot", "snap", "bookmark",
3134 switch (getsubopt(&optarg, type_subopts,
3137 types |= ZFS_TYPE_FILESYSTEM;
3140 types |= ZFS_TYPE_VOLUME;
3144 types |= ZFS_TYPE_SNAPSHOT;
3147 types |= ZFS_TYPE_BOOKMARK;
3150 types = ZFS_TYPE_DATASET |
3154 (void) fprintf(stderr,
3155 gettext("invalid type '%s'\n"),
3162 (void) fprintf(stderr, gettext("missing argument for "
3163 "'%c' option\n"), optopt);
3167 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3177 fields = default_fields;
3180 * If we are only going to list snapshot names and sort by name,
3181 * then we can use faster version.
3183 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3184 flags |= ZFS_ITER_SIMPLE;
3187 * If "-o space" and no types were specified, don't display snapshots.
3189 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3190 types &= ~ZFS_TYPE_SNAPSHOT;
3193 * If the user specifies '-o all', the zprop_get_list() doesn't
3194 * normally include the name of the dataset. For 'zfs list', we always
3195 * want this property to be first.
3197 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3201 cb.cb_first = B_TRUE;
3203 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3204 limit, list_callback, &cb);
3206 zprop_free_list(cb.cb_proplist);
3207 zfs_free_sort_columns(sortcol);
3209 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3210 (void) printf(gettext("no datasets available\n"));
3216 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3217 * zfs rename [-f] -p <fs | vol> <fs | vol>
3218 * zfs rename -r <snap> <snap>
3219 * zfs rename -u [-p] <fs> <fs>
3221 * Renames the given dataset to another of the same type.
3223 * The '-p' flag creates all the non-existing ancestors of the target first.
3227 zfs_do_rename(int argc, char **argv)
3230 renameflags_t flags = { 0 };
3234 boolean_t parents = B_FALSE;
3235 char *snapshot = NULL;
3238 while ((c = getopt(argc, argv, "fpru")) != -1) {
3244 flags.recurse = B_TRUE;
3247 flags.nounmount = B_TRUE;
3250 flags.forceunmount = B_TRUE;
3254 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3263 /* check number of arguments */
3265 (void) fprintf(stderr, gettext("missing source dataset "
3270 (void) fprintf(stderr, gettext("missing target dataset "
3275 (void) fprintf(stderr, gettext("too many arguments\n"));
3279 if (flags.recurse && parents) {
3280 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3285 if (flags.recurse && strchr(argv[0], '@') == 0) {
3286 (void) fprintf(stderr, gettext("source dataset for recursive "
3287 "rename must be a snapshot\n"));
3291 if (flags.nounmount && parents) {
3292 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3297 if (flags.nounmount)
3298 types = ZFS_TYPE_FILESYSTEM;
3300 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3302 types = ZFS_TYPE_DATASET;
3304 if (flags.recurse) {
3306 * When we do recursive rename we are fine when the given
3307 * snapshot for the given dataset doesn't exist - it can
3308 * still exists below.
3311 snapshot = strchr(argv[0], '@');
3312 assert(snapshot != NULL);
3317 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3320 /* If we were asked and the name looks good, try to create ancestors. */
3321 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3322 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3327 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3336 * Promotes the given clone fs to be the parent
3340 zfs_do_promote(int argc, char **argv)
3346 if (argc > 1 && argv[1][0] == '-') {
3347 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3352 /* check number of arguments */
3354 (void) fprintf(stderr, gettext("missing clone filesystem"
3359 (void) fprintf(stderr, gettext("too many arguments\n"));
3363 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3367 ret = (zfs_promote(zhp) != 0);
3375 * zfs rollback [-rRf] <snapshot>
3377 * -r Delete any intervening snapshots before doing rollback
3378 * -R Delete any snapshots and their clones
3379 * -f ignored for backwards compatability
3381 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3382 * since then and making it the active dataset. If more recent snapshots exist,
3383 * the command will complain unless the '-r' flag is given.
3385 typedef struct rollback_cbdata {
3391 boolean_t cb_recurse;
3392 } rollback_cbdata_t;
3395 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3397 rollback_cbdata_t *cbp = data;
3399 if (cbp->cb_first && cbp->cb_recurse) {
3400 (void) fprintf(stderr, gettext("cannot rollback to "
3401 "'%s': clones of previous snapshots exist\n"),
3403 (void) fprintf(stderr, gettext("use '-R' to "
3404 "force deletion of the following clones and "
3410 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3417 * Report any snapshots more recent than the one specified. Used when '-r' is
3418 * not specified. We reuse this same callback for the snapshot dependents - if
3419 * 'cb_dependent' is set, then this is a dependent and we should report it
3420 * without checking the transaction group.
3423 rollback_check(zfs_handle_t *zhp, void *data)
3425 rollback_cbdata_t *cbp = data;
3427 if (cbp->cb_doclones) {
3432 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3433 if (cbp->cb_first && !cbp->cb_recurse) {
3434 (void) fprintf(stderr, gettext("cannot "
3435 "rollback to '%s': more recent snapshots "
3436 "or bookmarks exist\n"),
3438 (void) fprintf(stderr, gettext("use '-r' to "
3439 "force deletion of the following "
3440 "snapshots and bookmarks:\n"));
3445 if (cbp->cb_recurse) {
3446 if (zfs_iter_dependents(zhp, B_TRUE,
3447 rollback_check_dependent, cbp) != 0) {
3452 (void) fprintf(stderr, "%s\n",
3461 zfs_do_rollback(int argc, char **argv)
3465 boolean_t force = B_FALSE;
3466 rollback_cbdata_t cb = { 0 };
3467 zfs_handle_t *zhp, *snap;
3468 char parentname[ZFS_MAXNAMELEN];
3472 while ((c = getopt(argc, argv, "rRf")) != -1) {
3485 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3494 /* check number of arguments */
3496 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3500 (void) fprintf(stderr, gettext("too many arguments\n"));
3504 /* open the snapshot */
3505 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3508 /* open the parent dataset */
3509 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3510 verify((delim = strrchr(parentname, '@')) != NULL);
3512 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3518 * Check for more recent snapshots and/or clones based on the presence
3521 cb.cb_target = argv[0];
3522 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3523 cb.cb_first = B_TRUE;
3525 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3527 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3530 if ((ret = cb.cb_error) != 0)
3534 * Rollback parent to the given snapshot.
3536 ret = zfs_rollback(zhp, snap, force);
3549 * zfs set property=value ... { fs | snap | vol } ...
3551 * Sets the given properties for all datasets specified on the command line.
3555 set_callback(zfs_handle_t *zhp, void *data)
3557 nvlist_t *props = data;
3559 if (zfs_prop_set_list(zhp, props) != 0) {
3560 switch (libzfs_errno(g_zfs)) {
3561 case EZFS_MOUNTFAILED:
3562 (void) fprintf(stderr, gettext("property may be set "
3563 "but unable to remount filesystem\n"));
3565 case EZFS_SHARENFSFAILED:
3566 (void) fprintf(stderr, gettext("property may be set "
3567 "but unable to reshare filesystem\n"));
3576 zfs_do_set(int argc, char **argv)
3578 nvlist_t *props = NULL;
3579 int ds_start = -1; /* argv idx of first dataset arg */
3582 /* check for options */
3583 if (argc > 1 && argv[1][0] == '-') {
3584 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3589 /* check number of arguments */
3591 (void) fprintf(stderr, gettext("missing arguments\n"));
3595 if (strchr(argv[1], '=') == NULL) {
3596 (void) fprintf(stderr, gettext("missing property=value "
3599 (void) fprintf(stderr, gettext("missing dataset "
3605 /* validate argument order: prop=val args followed by dataset args */
3606 for (int i = 1; i < argc; i++) {
3607 if (strchr(argv[i], '=') != NULL) {
3609 /* out-of-order prop=val argument */
3610 (void) fprintf(stderr, gettext("invalid "
3611 "argument order\n"), i);
3614 } else if (ds_start < 0) {
3619 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3623 /* Populate a list of property settings */
3624 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3626 for (int i = 1; i < ds_start; i++) {
3627 if ((ret = parseprop(props, argv[i])) != 0)
3631 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3632 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3639 typedef struct snap_cbdata {
3641 boolean_t sd_recursive;
3642 const char *sd_snapname;
3646 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3648 snap_cbdata_t *sd = arg;
3653 if (sd->sd_recursive &&
3654 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3659 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3662 fnvlist_add_boolean(sd->sd_nvl, name);
3665 if (sd->sd_recursive)
3666 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3672 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3674 * Creates a snapshot with the given name. While functionally equivalent to
3675 * 'zfs create', it is a separate command to differentiate intent.
3678 zfs_do_snapshot(int argc, char **argv)
3683 snap_cbdata_t sd = { 0 };
3684 boolean_t multiple_snaps = B_FALSE;
3686 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3688 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3692 while ((c = getopt(argc, argv, "ro:")) != -1) {
3695 if (parseprop(props, optarg) != 0)
3699 sd.sd_recursive = B_TRUE;
3700 multiple_snaps = B_TRUE;
3703 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3712 /* check number of arguments */
3714 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3719 multiple_snaps = B_TRUE;
3720 for (; argc > 0; argc--, argv++) {
3724 atp = strchr(argv[0], '@');
3728 sd.sd_snapname = atp + 1;
3729 zhp = zfs_open(g_zfs, argv[0],
3730 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3733 if (zfs_snapshot_cb(zhp, &sd) != 0)
3737 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3738 nvlist_free(sd.sd_nvl);
3740 if (ret != 0 && multiple_snaps)
3741 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3745 nvlist_free(sd.sd_nvl);
3752 * Send a backup stream to stdout.
3755 zfs_do_send(int argc, char **argv)
3757 char *fromname = NULL;
3758 char *toname = NULL;
3759 char *resume_token = NULL;
3762 sendflags_t flags = { 0 };
3764 nvlist_t *dbgnv = NULL;
3765 boolean_t extraverbose = B_FALSE;
3768 while ((c = getopt(argc, argv, ":i:I:RDpvnPLet:")) != -1) {
3779 flags.doall = B_TRUE;
3782 flags.replicate = B_TRUE;
3785 flags.props = B_TRUE;
3788 flags.parsable = B_TRUE;
3789 flags.verbose = B_TRUE;
3793 extraverbose = B_TRUE;
3794 flags.verbose = B_TRUE;
3795 flags.progress = B_TRUE;
3798 flags.dedup = B_TRUE;
3801 flags.dryrun = B_TRUE;
3804 flags.largeblock = B_TRUE;
3807 flags.embed_data = B_TRUE;
3810 resume_token = optarg;
3813 (void) fprintf(stderr, gettext("missing argument for "
3814 "'%c' option\n"), optopt);
3818 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3827 if (resume_token != NULL) {
3828 if (fromname != NULL || flags.replicate || flags.props ||
3830 (void) fprintf(stderr,
3831 gettext("invalid flags combined with -t\n"));
3835 (void) fprintf(stderr, gettext("no additional "
3836 "arguments are permitted with -t\n"));
3841 (void) fprintf(stderr,
3842 gettext("missing snapshot argument\n"));
3846 (void) fprintf(stderr, gettext("too many arguments\n"));
3851 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3852 (void) fprintf(stderr,
3853 gettext("Error: Stream can not be written to a terminal.\n"
3854 "You must redirect standard output.\n"));
3858 if (resume_token != NULL) {
3859 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3864 * Special case sending a filesystem, or from a bookmark.
3866 if (strchr(argv[0], '@') == NULL ||
3867 (fromname && strchr(fromname, '#') != NULL)) {
3868 char frombuf[ZFS_MAXNAMELEN];
3869 enum lzc_send_flags lzc_flags = 0;
3871 if (flags.replicate || flags.doall || flags.props ||
3872 flags.dedup || flags.dryrun || flags.verbose ||
3874 (void) fprintf(stderr,
3876 "Unsupported flag with filesystem or bookmark.\n"));
3880 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3884 if (flags.largeblock)
3885 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3886 if (flags.embed_data)
3887 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3889 if (fromname != NULL &&
3890 (fromname[0] == '#' || fromname[0] == '@')) {
3892 * Incremental source name begins with # or @.
3893 * Default to same fs as target.
3895 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3896 cp = strchr(frombuf, '@');
3899 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3902 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3907 cp = strchr(argv[0], '@');
3910 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3915 * If they specified the full path to the snapshot, chop off
3916 * everything except the short name of the snapshot, but special
3917 * case if they specify the origin.
3919 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3920 char origin[ZFS_MAXNAMELEN];
3923 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3924 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3926 if (strcmp(origin, fromname) == 0) {
3928 flags.fromorigin = B_TRUE;
3931 if (cp != fromname && strcmp(argv[0], fromname)) {
3932 (void) fprintf(stderr,
3933 gettext("incremental source must be "
3934 "in same filesystem\n"));
3938 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3939 (void) fprintf(stderr,
3940 gettext("invalid incremental source\n"));
3946 if (flags.replicate && fromname == NULL)
3947 flags.doall = B_TRUE;
3949 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3950 extraverbose ? &dbgnv : NULL);
3952 if (extraverbose && dbgnv != NULL) {
3954 * dump_nvlist prints to stdout, but that's been
3955 * redirected to a file. Make it print to stderr
3958 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3959 dump_nvlist(dbgnv, 0);
3968 * Restore a backup stream from stdin.
3971 zfs_do_receive(int argc, char **argv)
3974 recvflags_t flags = { 0 };
3975 boolean_t abort_resumable = B_FALSE;
3978 nvpair_t *nvp = NULL;
3980 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3984 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
3987 if (parseprop(props, optarg) != 0)
3991 flags.isprefix = B_TRUE;
3994 flags.isprefix = B_TRUE;
3995 flags.istail = B_TRUE;
3998 flags.dryrun = B_TRUE;
4001 flags.nomount = B_TRUE;
4004 flags.verbose = B_TRUE;
4007 flags.resumable = B_TRUE;
4010 flags.force = B_TRUE;
4013 abort_resumable = B_TRUE;
4016 (void) fprintf(stderr, gettext("missing argument for "
4017 "'%c' option\n"), optopt);
4021 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4030 /* check number of arguments */
4032 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4036 (void) fprintf(stderr, gettext("too many arguments\n"));
4040 while ((nvp = nvlist_next_nvpair(props, nvp))) {
4041 if (strcmp(nvpair_name(nvp), "origin") != 0) {
4042 (void) fprintf(stderr, gettext("invalid option"));
4047 if (abort_resumable) {
4048 if (flags.isprefix || flags.istail || flags.dryrun ||
4049 flags.resumable || flags.nomount) {
4050 (void) fprintf(stderr, gettext("invalid option"));
4054 char namebuf[ZFS_MAXNAMELEN];
4055 (void) snprintf(namebuf, sizeof (namebuf),
4056 "%s/%%recv", argv[0]);
4058 if (zfs_dataset_exists(g_zfs, namebuf,
4059 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4060 zfs_handle_t *zhp = zfs_open(g_zfs,
4061 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4064 err = zfs_destroy(zhp, B_FALSE);
4066 zfs_handle_t *zhp = zfs_open(g_zfs,
4067 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4070 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4071 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4072 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4073 (void) fprintf(stderr,
4074 gettext("'%s' does not have any "
4075 "resumable receive state to abort\n"),
4079 err = zfs_destroy(zhp, B_FALSE);
4085 if (isatty(STDIN_FILENO)) {
4086 (void) fprintf(stderr,
4087 gettext("Error: Backup stream can not be read "
4088 "from a terminal.\n"
4089 "You must redirect standard input.\n"));
4092 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4098 * allow/unallow stuff
4100 /* copied from zfs/sys/dsl_deleg.h */
4101 #define ZFS_DELEG_PERM_CREATE "create"
4102 #define ZFS_DELEG_PERM_DESTROY "destroy"
4103 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4104 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4105 #define ZFS_DELEG_PERM_CLONE "clone"
4106 #define ZFS_DELEG_PERM_PROMOTE "promote"
4107 #define ZFS_DELEG_PERM_RENAME "rename"
4108 #define ZFS_DELEG_PERM_MOUNT "mount"
4109 #define ZFS_DELEG_PERM_SHARE "share"
4110 #define ZFS_DELEG_PERM_SEND "send"
4111 #define ZFS_DELEG_PERM_RECEIVE "receive"
4112 #define ZFS_DELEG_PERM_ALLOW "allow"
4113 #define ZFS_DELEG_PERM_USERPROP "userprop"
4114 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4115 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4116 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4117 #define ZFS_DELEG_PERM_USERUSED "userused"
4118 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4119 #define ZFS_DELEG_PERM_HOLD "hold"
4120 #define ZFS_DELEG_PERM_RELEASE "release"
4121 #define ZFS_DELEG_PERM_DIFF "diff"
4122 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4124 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4126 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4127 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4128 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4129 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4130 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4131 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4132 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4133 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4134 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4135 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4136 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4137 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4138 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4139 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4140 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4141 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4142 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4144 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4145 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4146 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4147 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4148 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4149 { NULL, ZFS_DELEG_NOTE_NONE }
4152 /* permission structure */
4153 typedef struct deleg_perm {
4154 zfs_deleg_who_type_t dp_who_type;
4155 const char *dp_name;
4157 boolean_t dp_descend;
4161 typedef struct deleg_perm_node {
4162 deleg_perm_t dpn_perm;
4164 uu_avl_node_t dpn_avl_node;
4165 } deleg_perm_node_t;
4167 typedef struct fs_perm fs_perm_t;
4169 /* permissions set */
4170 typedef struct who_perm {
4171 zfs_deleg_who_type_t who_type;
4172 const char *who_name; /* id */
4173 char who_ug_name[256]; /* user/group name */
4174 fs_perm_t *who_fsperm; /* uplink */
4176 uu_avl_t *who_deleg_perm_avl; /* permissions */
4180 typedef struct who_perm_node {
4181 who_perm_t who_perm;
4182 uu_avl_node_t who_avl_node;
4185 typedef struct fs_perm_set fs_perm_set_t;
4186 /* fs permissions */
4188 const char *fsp_name;
4190 uu_avl_t *fsp_sc_avl; /* sets,create */
4191 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4193 fs_perm_set_t *fsp_set; /* uplink */
4197 typedef struct fs_perm_node {
4198 fs_perm_t fspn_fsperm;
4201 uu_list_node_t fspn_list_node;
4204 /* top level structure */
4205 struct fs_perm_set {
4206 uu_list_pool_t *fsps_list_pool;
4207 uu_list_t *fsps_list; /* list of fs_perms */
4209 uu_avl_pool_t *fsps_named_set_avl_pool;
4210 uu_avl_pool_t *fsps_who_perm_avl_pool;
4211 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4214 static inline const char *
4215 deleg_perm_type(zfs_deleg_note_t note)
4221 case ZFS_DELEG_NOTE_GROUPQUOTA:
4222 case ZFS_DELEG_NOTE_GROUPUSED:
4223 case ZFS_DELEG_NOTE_USERPROP:
4224 case ZFS_DELEG_NOTE_USERQUOTA:
4225 case ZFS_DELEG_NOTE_USERUSED:
4227 return (gettext("other"));
4229 return (gettext("subcommand"));
4234 who_type2weight(zfs_deleg_who_type_t who_type)
4238 case ZFS_DELEG_NAMED_SET_SETS:
4239 case ZFS_DELEG_NAMED_SET:
4242 case ZFS_DELEG_CREATE_SETS:
4243 case ZFS_DELEG_CREATE:
4246 case ZFS_DELEG_USER_SETS:
4247 case ZFS_DELEG_USER:
4250 case ZFS_DELEG_GROUP_SETS:
4251 case ZFS_DELEG_GROUP:
4254 case ZFS_DELEG_EVERYONE_SETS:
4255 case ZFS_DELEG_EVERYONE:
4267 who_perm_compare(const void *larg, const void *rarg, void *unused)
4269 const who_perm_node_t *l = larg;
4270 const who_perm_node_t *r = rarg;
4271 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4272 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4273 int lweight = who_type2weight(ltype);
4274 int rweight = who_type2weight(rtype);
4275 int res = lweight - rweight;
4277 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4278 ZFS_MAX_DELEG_NAME-1);
4290 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4292 const deleg_perm_node_t *l = larg;
4293 const deleg_perm_node_t *r = rarg;
4294 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4295 ZFS_MAX_DELEG_NAME-1);
4307 fs_perm_set_init(fs_perm_set_t *fspset)
4309 bzero(fspset, sizeof (fs_perm_set_t));
4311 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4312 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4313 NULL, UU_DEFAULT)) == NULL)
4315 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4316 UU_DEFAULT)) == NULL)
4319 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4320 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4321 who_perm_node_t, who_avl_node), who_perm_compare,
4322 UU_DEFAULT)) == NULL)
4325 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4326 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4327 who_perm_node_t, who_avl_node), who_perm_compare,
4328 UU_DEFAULT)) == NULL)
4331 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4332 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4333 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4338 static inline void fs_perm_fini(fs_perm_t *);
4339 static inline void who_perm_fini(who_perm_t *);
4342 fs_perm_set_fini(fs_perm_set_t *fspset)
4344 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4346 while (node != NULL) {
4347 fs_perm_node_t *next_node =
4348 uu_list_next(fspset->fsps_list, node);
4349 fs_perm_t *fsperm = &node->fspn_fsperm;
4350 fs_perm_fini(fsperm);
4351 uu_list_remove(fspset->fsps_list, node);
4356 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4357 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4358 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4362 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4365 deleg_perm->dp_who_type = type;
4366 deleg_perm->dp_name = name;
4370 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4371 zfs_deleg_who_type_t type, const char *name)
4373 uu_avl_pool_t *pool;
4374 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4376 bzero(who_perm, sizeof (who_perm_t));
4378 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4379 UU_DEFAULT)) == NULL)
4382 who_perm->who_type = type;
4383 who_perm->who_name = name;
4384 who_perm->who_fsperm = fsperm;
4388 who_perm_fini(who_perm_t *who_perm)
4390 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4392 while (node != NULL) {
4393 deleg_perm_node_t *next_node =
4394 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4396 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4401 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4405 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4407 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4408 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4410 bzero(fsperm, sizeof (fs_perm_t));
4412 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4416 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4420 fsperm->fsp_set = fspset;
4421 fsperm->fsp_name = fsname;
4425 fs_perm_fini(fs_perm_t *fsperm)
4427 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4428 while (node != NULL) {
4429 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4431 who_perm_t *who_perm = &node->who_perm;
4432 who_perm_fini(who_perm);
4433 uu_avl_remove(fsperm->fsp_sc_avl, node);
4438 node = uu_avl_first(fsperm->fsp_uge_avl);
4439 while (node != NULL) {
4440 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4442 who_perm_t *who_perm = &node->who_perm;
4443 who_perm_fini(who_perm);
4444 uu_avl_remove(fsperm->fsp_uge_avl, node);
4449 uu_avl_destroy(fsperm->fsp_sc_avl);
4450 uu_avl_destroy(fsperm->fsp_uge_avl);
4454 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4455 zfs_deleg_who_type_t who_type, const char *name, char locality)
4457 uu_avl_index_t idx = 0;
4459 deleg_perm_node_t *found_node = NULL;
4460 deleg_perm_t *deleg_perm = &node->dpn_perm;
4462 deleg_perm_init(deleg_perm, who_type, name);
4464 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4466 uu_avl_insert(avl, node, idx);
4469 deleg_perm = &node->dpn_perm;
4474 case ZFS_DELEG_LOCAL:
4475 deleg_perm->dp_local = B_TRUE;
4477 case ZFS_DELEG_DESCENDENT:
4478 deleg_perm->dp_descend = B_TRUE;
4483 assert(B_FALSE); /* invalid locality */
4488 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4490 nvpair_t *nvp = NULL;
4491 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4492 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4493 zfs_deleg_who_type_t who_type = who_perm->who_type;
4495 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4496 const char *name = nvpair_name(nvp);
4497 data_type_t type = nvpair_type(nvp);
4498 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4499 deleg_perm_node_t *node =
4500 safe_malloc(sizeof (deleg_perm_node_t));
4502 assert(type == DATA_TYPE_BOOLEAN);
4504 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4505 set_deleg_perm_node(avl, node, who_type, name, locality);
4512 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4514 nvpair_t *nvp = NULL;
4515 fs_perm_set_t *fspset = fsperm->fsp_set;
4517 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4518 nvlist_t *nvl2 = NULL;
4519 const char *name = nvpair_name(nvp);
4520 uu_avl_t *avl = NULL;
4521 uu_avl_pool_t *avl_pool;
4522 zfs_deleg_who_type_t perm_type = name[0];
4523 char perm_locality = name[1];
4524 const char *perm_name = name + 3;
4525 boolean_t is_set = B_TRUE;
4526 who_perm_t *who_perm = NULL;
4528 assert('$' == name[2]);
4530 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4533 switch (perm_type) {
4534 case ZFS_DELEG_CREATE:
4535 case ZFS_DELEG_CREATE_SETS:
4536 case ZFS_DELEG_NAMED_SET:
4537 case ZFS_DELEG_NAMED_SET_SETS:
4538 avl_pool = fspset->fsps_named_set_avl_pool;
4539 avl = fsperm->fsp_sc_avl;
4541 case ZFS_DELEG_USER:
4542 case ZFS_DELEG_USER_SETS:
4543 case ZFS_DELEG_GROUP:
4544 case ZFS_DELEG_GROUP_SETS:
4545 case ZFS_DELEG_EVERYONE:
4546 case ZFS_DELEG_EVERYONE_SETS:
4547 avl_pool = fspset->fsps_who_perm_avl_pool;
4548 avl = fsperm->fsp_uge_avl;
4553 who_perm_node_t *found_node = NULL;
4554 who_perm_node_t *node = safe_malloc(
4555 sizeof (who_perm_node_t));
4556 who_perm = &node->who_perm;
4557 uu_avl_index_t idx = 0;
4559 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4560 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4562 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4564 if (avl == fsperm->fsp_uge_avl) {
4566 struct passwd *p = NULL;
4567 struct group *g = NULL;
4568 const char *nice_name = NULL;
4570 switch (perm_type) {
4571 case ZFS_DELEG_USER_SETS:
4572 case ZFS_DELEG_USER:
4573 rid = atoi(perm_name);
4576 nice_name = p->pw_name;
4578 case ZFS_DELEG_GROUP_SETS:
4579 case ZFS_DELEG_GROUP:
4580 rid = atoi(perm_name);
4583 nice_name = g->gr_name;
4587 if (nice_name != NULL)
4589 node->who_perm.who_ug_name,
4593 uu_avl_insert(avl, node, idx);
4596 who_perm = &node->who_perm;
4600 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4607 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4609 nvpair_t *nvp = NULL;
4610 uu_avl_index_t idx = 0;
4612 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4613 nvlist_t *nvl2 = NULL;
4614 const char *fsname = nvpair_name(nvp);
4615 data_type_t type = nvpair_type(nvp);
4616 fs_perm_t *fsperm = NULL;
4617 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4621 fsperm = &node->fspn_fsperm;
4623 assert(DATA_TYPE_NVLIST == type);
4625 uu_list_node_init(node, &node->fspn_list_node,
4626 fspset->fsps_list_pool);
4628 idx = uu_list_numnodes(fspset->fsps_list);
4629 fs_perm_init(fsperm, fspset, fsname);
4631 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4634 (void) parse_fs_perm(fsperm, nvl2);
4636 uu_list_insert(fspset->fsps_list, node, idx);
4642 static inline const char *
4643 deleg_perm_comment(zfs_deleg_note_t note)
4645 const char *str = "";
4650 case ZFS_DELEG_NOTE_ALLOW:
4651 str = gettext("Must also have the permission that is being"
4652 "\n\t\t\t\tallowed");
4654 case ZFS_DELEG_NOTE_CLONE:
4655 str = gettext("Must also have the 'create' ability and 'mount'"
4656 "\n\t\t\t\tability in the origin file system");
4658 case ZFS_DELEG_NOTE_CREATE:
4659 str = gettext("Must also have the 'mount' ability");
4661 case ZFS_DELEG_NOTE_DESTROY:
4662 str = gettext("Must also have the 'mount' ability");
4664 case ZFS_DELEG_NOTE_DIFF:
4665 str = gettext("Allows lookup of paths within a dataset;"
4666 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4667 "\n\t\t\t\tin order to use zfs diff");
4669 case ZFS_DELEG_NOTE_HOLD:
4670 str = gettext("Allows adding a user hold to a snapshot");
4672 case ZFS_DELEG_NOTE_MOUNT:
4673 str = gettext("Allows mount/umount of ZFS datasets");
4675 case ZFS_DELEG_NOTE_PROMOTE:
4676 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4677 " 'promote' ability in the origin file system");
4679 case ZFS_DELEG_NOTE_RECEIVE:
4680 str = gettext("Must also have the 'mount' and 'create'"
4683 case ZFS_DELEG_NOTE_RELEASE:
4684 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4685 "might destroy the snapshot");
4687 case ZFS_DELEG_NOTE_RENAME:
4688 str = gettext("Must also have the 'mount' and 'create'"
4689 "\n\t\t\t\tability in the new parent");
4691 case ZFS_DELEG_NOTE_ROLLBACK:
4694 case ZFS_DELEG_NOTE_SEND:
4697 case ZFS_DELEG_NOTE_SHARE:
4698 str = gettext("Allows sharing file systems over NFS or SMB"
4699 "\n\t\t\t\tprotocols");
4701 case ZFS_DELEG_NOTE_SNAPSHOT:
4705 * case ZFS_DELEG_NOTE_VSCAN:
4706 * str = gettext("");
4710 case ZFS_DELEG_NOTE_GROUPQUOTA:
4711 str = gettext("Allows accessing any groupquota@... property");
4713 case ZFS_DELEG_NOTE_GROUPUSED:
4714 str = gettext("Allows reading any groupused@... property");
4716 case ZFS_DELEG_NOTE_USERPROP:
4717 str = gettext("Allows changing any user property");
4719 case ZFS_DELEG_NOTE_USERQUOTA:
4720 str = gettext("Allows accessing any userquota@... property");
4722 case ZFS_DELEG_NOTE_USERUSED:
4723 str = gettext("Allows reading any userused@... property");
4741 boolean_t recursive; /* unallow only */
4742 boolean_t prt_usage;
4744 boolean_t prt_perms;
4747 const char *dataset;
4751 prop_cmp(const void *a, const void *b)
4753 const char *str1 = *(const char **)a;
4754 const char *str2 = *(const char **)b;
4755 return (strcmp(str1, str2));
4759 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4761 const char *opt_desc[] = {
4762 "-h", gettext("show this help message and exit"),
4763 "-l", gettext("set permission locally"),
4764 "-d", gettext("set permission for descents"),
4765 "-u", gettext("set permission for user"),
4766 "-g", gettext("set permission for group"),
4767 "-e", gettext("set permission for everyone"),
4768 "-c", gettext("set create time permission"),
4769 "-s", gettext("define permission set"),
4771 "-r", gettext("remove permissions recursively"),
4773 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4774 size_t allow_size = unallow_size - 2;
4775 const char *props[ZFS_NUM_PROPS];
4778 FILE *fp = requested ? stdout : stderr;
4779 zprop_desc_t *pdtbl = zfs_prop_get_table();
4780 const char *fmt = gettext("%-16s %-14s\t%s\n");
4782 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4784 (void) fprintf(fp, gettext("Options:\n"));
4785 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4786 const char *opt = opt_desc[i++];
4787 const char *optdsc = opt_desc[i];
4788 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4791 (void) fprintf(fp, gettext("\nThe following permissions are "
4793 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4795 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4796 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4797 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4798 const char *perm_type = deleg_perm_type(perm_note);
4799 const char *perm_comment = deleg_perm_comment(perm_note);
4800 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4803 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4804 zprop_desc_t *pd = &pdtbl[i];
4805 if (pd->pd_visible != B_TRUE)
4808 if (pd->pd_attr == PROP_READONLY)
4811 props[count++] = pd->pd_name;
4813 props[count] = NULL;
4815 qsort(props, count, sizeof (char *), prop_cmp);
4817 for (i = 0; i < count; i++)
4818 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4821 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4823 exit(requested ? 0 : 2);
4826 static inline const char *
4827 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4830 if (un && argc == expected_argc - 1)
4832 else if (argc == expected_argc)
4833 *permsp = argv[argc - 2];
4835 allow_usage(un, B_FALSE,
4836 gettext("wrong number of parameters\n"));
4838 return (argv[argc - 1]);
4842 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4844 int uge_sum = opts->user + opts->group + opts->everyone;
4845 int csuge_sum = opts->create + opts->set + uge_sum;
4846 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4847 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4850 allow_usage(un, B_FALSE,
4851 gettext("-u, -g, and -e are mutually exclusive\n"));
4853 if (opts->prt_usage)
4854 if (argc == 0 && all_sum == 0)
4855 allow_usage(un, B_TRUE, NULL);
4861 allow_usage(un, B_FALSE,
4862 gettext("invalid options combined with -s\n"));
4864 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4865 if (argv[0][0] != '@')
4866 allow_usage(un, B_FALSE,
4867 gettext("invalid set name: missing '@' prefix\n"));
4868 opts->who = argv[0];
4869 } else if (opts->create) {
4870 if (ldcsuge_sum > 1)
4871 allow_usage(un, B_FALSE,
4872 gettext("invalid options combined with -c\n"));
4873 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4874 } else if (opts->everyone) {
4876 allow_usage(un, B_FALSE,
4877 gettext("invalid options combined with -e\n"));
4878 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4879 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4881 opts->everyone = B_TRUE;
4884 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4885 } else if (argc == 1 && !un) {
4886 opts->prt_perms = B_TRUE;
4887 opts->dataset = argv[argc-1];
4889 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4890 opts->who = argv[0];
4893 if (!opts->local && !opts->descend) {
4894 opts->local = B_TRUE;
4895 opts->descend = B_TRUE;
4900 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4901 const char *who, char *perms, nvlist_t *top_nvl)
4904 char ld[2] = { '\0', '\0' };
4905 char who_buf[ZFS_MAXNAMELEN+32];
4908 nvlist_t *base_nvl = NULL;
4909 nvlist_t *set_nvl = NULL;
4912 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4914 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4918 case ZFS_DELEG_NAMED_SET_SETS:
4919 case ZFS_DELEG_NAMED_SET:
4920 set_type = ZFS_DELEG_NAMED_SET_SETS;
4921 base_type = ZFS_DELEG_NAMED_SET;
4922 ld[0] = ZFS_DELEG_NA;
4924 case ZFS_DELEG_CREATE_SETS:
4925 case ZFS_DELEG_CREATE:
4926 set_type = ZFS_DELEG_CREATE_SETS;
4927 base_type = ZFS_DELEG_CREATE;
4928 ld[0] = ZFS_DELEG_NA;
4930 case ZFS_DELEG_USER_SETS:
4931 case ZFS_DELEG_USER:
4932 set_type = ZFS_DELEG_USER_SETS;
4933 base_type = ZFS_DELEG_USER;
4935 ld[0] = ZFS_DELEG_LOCAL;
4937 ld[1] = ZFS_DELEG_DESCENDENT;
4939 case ZFS_DELEG_GROUP_SETS:
4940 case ZFS_DELEG_GROUP:
4941 set_type = ZFS_DELEG_GROUP_SETS;
4942 base_type = ZFS_DELEG_GROUP;
4944 ld[0] = ZFS_DELEG_LOCAL;
4946 ld[1] = ZFS_DELEG_DESCENDENT;
4948 case ZFS_DELEG_EVERYONE_SETS:
4949 case ZFS_DELEG_EVERYONE:
4950 set_type = ZFS_DELEG_EVERYONE_SETS;
4951 base_type = ZFS_DELEG_EVERYONE;
4953 ld[0] = ZFS_DELEG_LOCAL;
4955 ld[1] = ZFS_DELEG_DESCENDENT;
4958 if (perms != NULL) {
4960 char *end = curr + strlen(perms);
4962 while (curr < end) {
4963 char *delim = strchr(curr, ',');
4974 (void) nvlist_add_boolean(nvl, curr);
4980 for (i = 0; i < 2; i++) {
4981 char locality = ld[i];
4985 if (!nvlist_empty(base_nvl)) {
4987 (void) snprintf(who_buf,
4988 sizeof (who_buf), "%c%c$%s",
4989 base_type, locality, who);
4991 (void) snprintf(who_buf,
4992 sizeof (who_buf), "%c%c$",
4993 base_type, locality);
4995 (void) nvlist_add_nvlist(top_nvl, who_buf,
5000 if (!nvlist_empty(set_nvl)) {
5002 (void) snprintf(who_buf,
5003 sizeof (who_buf), "%c%c$%s",
5004 set_type, locality, who);
5006 (void) snprintf(who_buf,
5007 sizeof (who_buf), "%c%c$",
5008 set_type, locality);
5010 (void) nvlist_add_nvlist(top_nvl, who_buf,
5015 for (i = 0; i < 2; i++) {
5016 char locality = ld[i];
5021 (void) snprintf(who_buf, sizeof (who_buf),
5022 "%c%c$%s", base_type, locality, who);
5024 (void) snprintf(who_buf, sizeof (who_buf),
5025 "%c%c$", base_type, locality);
5026 (void) nvlist_add_boolean(top_nvl, who_buf);
5029 (void) snprintf(who_buf, sizeof (who_buf),
5030 "%c%c$%s", set_type, locality, who);
5032 (void) snprintf(who_buf, sizeof (who_buf),
5033 "%c%c$", set_type, locality);
5034 (void) nvlist_add_boolean(top_nvl, who_buf);
5040 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5042 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5046 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5047 opts->descend, opts->who, opts->perms, *nvlp);
5048 } else if (opts->create) {
5049 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5050 opts->descend, NULL, opts->perms, *nvlp);
5051 } else if (opts->everyone) {
5052 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5053 opts->descend, NULL, opts->perms, *nvlp);
5055 char *curr = opts->who;
5056 char *end = curr + strlen(curr);
5058 while (curr < end) {
5060 zfs_deleg_who_type_t who_type;
5062 char *delim = strchr(curr, ',');
5065 struct passwd *p = NULL;
5066 struct group *g = NULL;
5074 rid = (uid_t)strtol(curr, &endch, 0);
5076 who_type = ZFS_DELEG_USER;
5085 (void) snprintf(errbuf, 256, gettext(
5086 "invalid user %s"), curr);
5087 allow_usage(un, B_TRUE, errbuf);
5089 } else if (opts->group) {
5090 who_type = ZFS_DELEG_GROUP;
5099 (void) snprintf(errbuf, 256, gettext(
5100 "invalid group %s"), curr);
5101 allow_usage(un, B_TRUE, errbuf);
5104 if (*endch != '\0') {
5111 if (*endch != '\0') {
5118 who_type = ZFS_DELEG_USER;
5120 } else if (g != NULL) {
5121 who_type = ZFS_DELEG_GROUP;
5124 (void) snprintf(errbuf, 256, gettext(
5125 "invalid user/group %s"), curr);
5126 allow_usage(un, B_TRUE, errbuf);
5130 (void) sprintf(id, "%u", rid);
5133 store_allow_perm(who_type, opts->local,
5134 opts->descend, who, opts->perms, *nvlp);
5143 print_set_creat_perms(uu_avl_t *who_avl)
5145 const char *sc_title[] = {
5146 gettext("Permission sets:\n"),
5147 gettext("Create time permissions:\n"),
5150 const char **title_ptr = sc_title;
5151 who_perm_node_t *who_node = NULL;
5152 int prev_weight = -1;
5154 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5155 who_node = uu_avl_next(who_avl, who_node)) {
5156 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5157 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5158 const char *who_name = who_node->who_perm.who_name;
5159 int weight = who_type2weight(who_type);
5160 boolean_t first = B_TRUE;
5161 deleg_perm_node_t *deleg_node;
5163 if (prev_weight != weight) {
5164 (void) printf(*title_ptr++);
5165 prev_weight = weight;
5168 if (who_name == NULL || strnlen(who_name, 1) == 0)
5169 (void) printf("\t");
5171 (void) printf("\t%s ", who_name);
5173 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5174 deleg_node = uu_avl_next(avl, deleg_node)) {
5177 deleg_node->dpn_perm.dp_name);
5180 (void) printf(",%s",
5181 deleg_node->dpn_perm.dp_name);
5184 (void) printf("\n");
5189 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5192 who_perm_node_t *who_node = NULL;
5193 boolean_t prt_title = B_TRUE;
5194 uu_avl_walk_t *walk;
5196 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5199 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5200 const char *who_name = who_node->who_perm.who_name;
5201 const char *nice_who_name = who_node->who_perm.who_ug_name;
5202 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5203 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5205 deleg_perm_node_t *deleg_node;
5206 boolean_t prt_who = B_TRUE;
5208 for (deleg_node = uu_avl_first(avl);
5210 deleg_node = uu_avl_next(avl, deleg_node)) {
5211 if (local != deleg_node->dpn_perm.dp_local ||
5212 descend != deleg_node->dpn_perm.dp_descend)
5216 const char *who = NULL;
5218 prt_title = B_FALSE;
5219 (void) printf(title);
5223 case ZFS_DELEG_USER_SETS:
5224 case ZFS_DELEG_USER:
5225 who = gettext("user");
5227 who_name = nice_who_name;
5229 case ZFS_DELEG_GROUP_SETS:
5230 case ZFS_DELEG_GROUP:
5231 who = gettext("group");
5233 who_name = nice_who_name;
5235 case ZFS_DELEG_EVERYONE_SETS:
5236 case ZFS_DELEG_EVERYONE:
5237 who = gettext("everyone");
5242 if (who_name == NULL)
5243 (void) printf("\t%s", who);
5245 (void) printf("\t%s %s", who, who_name);
5248 (void) printf("%c%s", delim,
5249 deleg_node->dpn_perm.dp_name);
5254 (void) printf("\n");
5257 uu_avl_walk_end(walk);
5261 print_fs_perms(fs_perm_set_t *fspset)
5263 fs_perm_node_t *node = NULL;
5264 char buf[ZFS_MAXNAMELEN+32];
5265 const char *dsname = buf;
5267 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5268 node = uu_list_next(fspset->fsps_list, node)) {
5269 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5270 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5273 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5274 gettext("---- Permissions on %s "),
5275 node->fspn_fsperm.fsp_name);
5276 (void) printf(dsname);
5277 left = 70 - strlen(buf);
5280 (void) printf("\n");
5282 print_set_creat_perms(sc_avl);
5283 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5284 gettext("Local permissions:\n"));
5285 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5286 gettext("Descendent permissions:\n"));
5287 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5288 gettext("Local+Descendent permissions:\n"));
5292 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5294 struct deleg_perms {
5300 set_deleg_perms(zfs_handle_t *zhp, void *data)
5302 struct deleg_perms *perms = (struct deleg_perms *)data;
5303 zfs_type_t zfs_type = zfs_get_type(zhp);
5305 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5308 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5312 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5315 nvlist_t *perm_nvl = NULL;
5316 nvlist_t *update_perm_nvl = NULL;
5319 struct allow_opts opts = { 0 };
5321 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5324 while ((c = getopt(argc, argv, optstr)) != -1) {
5327 opts.local = B_TRUE;
5330 opts.descend = B_TRUE;
5336 opts.group = B_TRUE;
5339 opts.everyone = B_TRUE;
5345 opts.create = B_TRUE;
5348 opts.recursive = B_TRUE;
5351 (void) fprintf(stderr, gettext("missing argument for "
5352 "'%c' option\n"), optopt);
5356 opts.prt_usage = B_TRUE;
5359 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5368 /* check arguments */
5369 parse_allow_args(argc, argv, un, &opts);
5371 /* try to open the dataset */
5372 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5373 ZFS_TYPE_VOLUME)) == NULL) {
5374 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5379 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5382 fs_perm_set_init(&fs_perm_set);
5383 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5384 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5389 print_fs_perms(&fs_perm_set);
5391 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5392 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5395 if (un && opts.recursive) {
5396 struct deleg_perms data = { un, update_perm_nvl };
5397 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5406 nvlist_free(perm_nvl);
5407 if (update_perm_nvl != NULL)
5408 nvlist_free(update_perm_nvl);
5410 fs_perm_set_fini(&fs_perm_set);
5418 zfs_do_allow(int argc, char **argv)
5420 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5424 zfs_do_unallow(int argc, char **argv)
5426 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5430 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5435 boolean_t recursive = B_FALSE;
5436 const char *opts = holding ? "rt" : "r";
5440 while ((c = getopt(argc, argv, opts)) != -1) {
5446 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5455 /* check number of arguments */
5463 if (holding && tag[0] == '.') {
5464 /* tags starting with '.' are reserved for libzfs */
5465 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5469 for (i = 0; i < argc; ++i) {
5471 char parent[ZFS_MAXNAMELEN];
5473 char *path = argv[i];
5475 delim = strchr(path, '@');
5476 if (delim == NULL) {
5477 (void) fprintf(stderr,
5478 gettext("'%s' is not a snapshot\n"), path);
5482 (void) strncpy(parent, path, delim - path);
5483 parent[delim - path] = '\0';
5485 zhp = zfs_open(g_zfs, parent,
5486 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5492 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5495 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5501 return (errors != 0);
5505 * zfs hold [-r] [-t] <tag> <snap> ...
5507 * -r Recursively hold
5509 * Apply a user-hold with the given tag to the list of snapshots.
5512 zfs_do_hold(int argc, char **argv)
5514 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5518 * zfs release [-r] <tag> <snap> ...
5520 * -r Recursively release
5522 * Release a user-hold with the given tag from the list of snapshots.
5525 zfs_do_release(int argc, char **argv)
5527 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5530 typedef struct holds_cbdata {
5531 boolean_t cb_recursive;
5532 const char *cb_snapname;
5534 size_t cb_max_namelen;
5535 size_t cb_max_taglen;
5538 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5539 #define DATETIME_BUF_LEN (32)
5544 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5547 nvpair_t *nvp = NULL;
5548 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5552 for (i = 0; i < 3; i++) {
5553 col = gettext(hdr_cols[i]);
5555 (void) printf("%-*s ", i ? tagwidth : nwidth,
5558 (void) printf("%s\n", col);
5562 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5563 char *zname = nvpair_name(nvp);
5565 nvpair_t *nvp2 = NULL;
5566 (void) nvpair_value_nvlist(nvp, &nvl2);
5567 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5568 char tsbuf[DATETIME_BUF_LEN];
5569 char *tagname = nvpair_name(nvp2);
5573 char sep = scripted ? '\t' : ' ';
5574 size_t sepnum = scripted ? 1 : 2;
5576 (void) nvpair_value_uint64(nvp2, &val);
5578 (void) localtime_r(&time, &t);
5579 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5580 gettext(STRFTIME_FMT_STR), &t);
5582 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5583 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5589 * Generic callback function to list a dataset or snapshot.
5592 holds_callback(zfs_handle_t *zhp, void *data)
5594 holds_cbdata_t *cbp = data;
5595 nvlist_t *top_nvl = *cbp->cb_nvlp;
5596 nvlist_t *nvl = NULL;
5597 nvpair_t *nvp = NULL;
5598 const char *zname = zfs_get_name(zhp);
5599 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5601 if (cbp->cb_recursive) {
5602 const char *snapname;
5603 char *delim = strchr(zname, '@');
5607 snapname = delim + 1;
5608 if (strcmp(cbp->cb_snapname, snapname))
5612 if (zfs_get_holds(zhp, &nvl) != 0)
5615 if (znamelen > cbp->cb_max_namelen)
5616 cbp->cb_max_namelen = znamelen;
5618 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5619 const char *tag = nvpair_name(nvp);
5620 size_t taglen = strnlen(tag, MAXNAMELEN);
5621 if (taglen > cbp->cb_max_taglen)
5622 cbp->cb_max_taglen = taglen;
5625 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5629 * zfs holds [-r] <snap> ...
5631 * -r Recursively hold
5634 zfs_do_holds(int argc, char **argv)
5639 boolean_t scripted = B_FALSE;
5640 boolean_t recursive = B_FALSE;
5641 const char *opts = "rH";
5644 int types = ZFS_TYPE_SNAPSHOT;
5645 holds_cbdata_t cb = { 0 };
5652 while ((c = getopt(argc, argv, opts)) != -1) {
5661 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5668 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5669 flags |= ZFS_ITER_RECURSE;
5675 /* check number of arguments */
5679 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5682 for (i = 0; i < argc; ++i) {
5683 char *snapshot = argv[i];
5685 const char *snapname;
5687 delim = strchr(snapshot, '@');
5688 if (delim == NULL) {
5689 (void) fprintf(stderr,
5690 gettext("'%s' is not a snapshot\n"), snapshot);
5694 snapname = delim + 1;
5696 snapshot[delim - snapshot] = '\0';
5698 cb.cb_recursive = recursive;
5699 cb.cb_snapname = snapname;
5703 * 1. collect holds data, set format options
5705 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5706 holds_callback, &cb);
5712 * 2. print holds data
5714 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5716 if (nvlist_empty(nvl))
5717 (void) printf(gettext("no datasets available\n"));
5721 return (0 != errors);
5724 #define CHECK_SPINNER 30
5725 #define SPINNER_TIME 3 /* seconds */
5726 #define MOUNT_TIME 5 /* seconds */
5729 get_one_dataset(zfs_handle_t *zhp, void *data)
5731 static char *spin[] = { "-", "\\", "|", "/" };
5732 static int spinval = 0;
5733 static int spincheck = 0;
5734 static time_t last_spin_time = (time_t)0;
5735 get_all_cb_t *cbp = data;
5736 zfs_type_t type = zfs_get_type(zhp);
5738 if (cbp->cb_verbose) {
5739 if (--spincheck < 0) {
5740 time_t now = time(NULL);
5741 if (last_spin_time + SPINNER_TIME < now) {
5742 update_progress(spin[spinval++ % 4]);
5743 last_spin_time = now;
5745 spincheck = CHECK_SPINNER;
5750 * Interate over any nested datasets.
5752 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5758 * Skip any datasets whose type does not match.
5760 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5764 libzfs_add_handle(cbp, zhp);
5765 assert(cbp->cb_used <= cbp->cb_alloc);
5771 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5773 get_all_cb_t cb = { 0 };
5774 cb.cb_verbose = verbose;
5775 cb.cb_getone = get_one_dataset;
5778 set_progress_header(gettext("Reading ZFS config"));
5779 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5781 *dslist = cb.cb_handles;
5782 *count = cb.cb_used;
5785 finish_progress(gettext("done."));
5789 * Generic callback for sharing or mounting filesystems. Because the code is so
5790 * similar, we have a common function with an extra parameter to determine which
5791 * mode we are using.
5793 #define OP_SHARE 0x1
5794 #define OP_MOUNT 0x2
5797 * Share or mount a dataset.
5800 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5801 boolean_t explicit, const char *options)
5803 char mountpoint[ZFS_MAXPROPLEN];
5804 char shareopts[ZFS_MAXPROPLEN];
5805 char smbshareopts[ZFS_MAXPROPLEN];
5806 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5808 uint64_t zoned, canmount;
5809 boolean_t shared_nfs, shared_smb;
5811 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5814 * Check to make sure we can mount/share this dataset. If we
5815 * are in the global zone and the filesystem is exported to a
5816 * local zone, or if we are in a local zone and the
5817 * filesystem is not exported, then it is an error.
5819 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5821 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5825 (void) fprintf(stderr, gettext("cannot %s '%s': "
5826 "dataset is exported to a local zone\n"), cmdname,
5830 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5834 (void) fprintf(stderr, gettext("cannot %s '%s': "
5835 "permission denied\n"), cmdname,
5841 * Ignore any filesystems which don't apply to us. This
5842 * includes those with a legacy mountpoint, or those with
5843 * legacy share options.
5845 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5846 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5847 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5848 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5849 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5850 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5852 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5853 strcmp(smbshareopts, "off") == 0) {
5857 (void) fprintf(stderr, gettext("cannot share '%s': "
5858 "legacy share\n"), zfs_get_name(zhp));
5859 (void) fprintf(stderr, gettext("to "
5860 "share this filesystem set "
5861 "sharenfs property on\n"));
5866 * We cannot share or mount legacy filesystems. If the
5867 * shareopts is non-legacy but the mountpoint is legacy, we
5868 * treat it as a legacy share.
5870 if (strcmp(mountpoint, "legacy") == 0) {
5874 (void) fprintf(stderr, gettext("cannot %s '%s': "
5875 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5876 (void) fprintf(stderr, gettext("use %s(8) to "
5877 "%s this filesystem\n"), cmdname, cmdname);
5881 if (strcmp(mountpoint, "none") == 0) {
5885 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5886 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5891 * canmount explicit outcome
5892 * on no pass through
5893 * on yes pass through
5895 * off yes display error, return 1
5896 * noauto no return 0
5897 * noauto yes pass through
5899 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5900 if (canmount == ZFS_CANMOUNT_OFF) {
5904 (void) fprintf(stderr, gettext("cannot %s '%s': "
5905 "'canmount' property is set to 'off'\n"), cmdname,
5908 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5913 * If this filesystem is inconsistent and has a receive resume
5914 * token, we can not mount it.
5916 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
5917 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
5918 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
5922 (void) fprintf(stderr, gettext("cannot %s '%s': "
5923 "Contains partially-completed state from "
5924 "\"zfs receive -r\", which can be resumed with "
5925 "\"zfs send -t\"\n"),
5926 cmdname, zfs_get_name(zhp));
5931 * At this point, we have verified that the mountpoint and/or
5932 * shareopts are appropriate for auto management. If the
5933 * filesystem is already mounted or shared, return (failing
5934 * for explicit requests); otherwise mount or share the
5940 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5941 shared_smb = zfs_is_shared_smb(zhp, NULL);
5943 if (shared_nfs && shared_smb ||
5944 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5945 strcmp(smbshareopts, "off") == 0) ||
5946 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5947 strcmp(shareopts, "off") == 0)) {
5951 (void) fprintf(stderr, gettext("cannot share "
5952 "'%s': filesystem already shared\n"),
5957 if (!zfs_is_mounted(zhp, NULL) &&
5958 zfs_mount(zhp, NULL, 0) != 0)
5961 if (protocol == NULL) {
5962 if (zfs_shareall(zhp) != 0)
5964 } else if (strcmp(protocol, "nfs") == 0) {
5965 if (zfs_share_nfs(zhp))
5967 } else if (strcmp(protocol, "smb") == 0) {
5968 if (zfs_share_smb(zhp))
5971 (void) fprintf(stderr, gettext("cannot share "
5972 "'%s': invalid share type '%s' "
5974 zfs_get_name(zhp), protocol);
5981 if (options == NULL)
5982 mnt.mnt_mntopts = "";
5984 mnt.mnt_mntopts = (char *)options;
5986 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5987 zfs_is_mounted(zhp, NULL)) {
5991 (void) fprintf(stderr, gettext("cannot mount "
5992 "'%s': filesystem already mounted\n"),
5997 if (zfs_mount(zhp, options, flags) != 0)
6006 * Reports progress in the form "(current/total)". Not thread-safe.
6009 report_mount_progress(int current, int total)
6011 static time_t last_progress_time = 0;
6012 time_t now = time(NULL);
6015 /* report 1..n instead of 0..n-1 */
6018 /* display header if we're here for the first time */
6020 set_progress_header(gettext("Mounting ZFS filesystems"));
6021 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6022 /* too soon to report again */
6026 last_progress_time = now;
6028 (void) sprintf(info, "(%d/%d)", current, total);
6030 if (current == total)
6031 finish_progress(info);
6033 update_progress(info);
6037 append_options(char *mntopts, char *newopts)
6039 int len = strlen(mntopts);
6041 /* original length plus new string to append plus 1 for the comma */
6042 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6043 (void) fprintf(stderr, gettext("the opts argument for "
6044 "'%c' option is too long (more than %d chars)\n"),
6045 "-o", MNT_LINE_MAX);
6050 mntopts[len++] = ',';
6052 (void) strcpy(&mntopts[len], newopts);
6056 share_mount(int op, int argc, char **argv)
6059 boolean_t verbose = B_FALSE;
6061 char *options = NULL;
6065 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6075 if (*optarg == '\0') {
6076 (void) fprintf(stderr, gettext("empty mount "
6077 "options (-o) specified\n"));
6081 if (options == NULL)
6082 options = safe_malloc(MNT_LINE_MAX + 1);
6084 /* option validation is done later */
6085 append_options(options, optarg);
6089 warnx("no overlay mounts support on FreeBSD, ignoring");
6092 (void) fprintf(stderr, gettext("missing argument for "
6093 "'%c' option\n"), optopt);
6097 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6106 /* check number of arguments */
6108 zfs_handle_t **dslist = NULL;
6109 size_t i, count = 0;
6110 char *protocol = NULL;
6112 if (op == OP_SHARE && argc > 0) {
6113 if (strcmp(argv[0], "nfs") != 0 &&
6114 strcmp(argv[0], "smb") != 0) {
6115 (void) fprintf(stderr, gettext("share type "
6116 "must be 'nfs' or 'smb'\n"));
6125 (void) fprintf(stderr, gettext("too many arguments\n"));
6129 start_progress_timer();
6130 get_all_datasets(&dslist, &count, verbose);
6135 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6137 for (i = 0; i < count; i++) {
6139 report_mount_progress(i, count);
6141 if (share_mount_one(dslist[i], op, flags, protocol,
6142 B_FALSE, options) != 0)
6144 zfs_close(dslist[i]);
6148 } else if (argc == 0) {
6149 struct mnttab entry;
6151 if ((op == OP_SHARE) || (options != NULL)) {
6152 (void) fprintf(stderr, gettext("missing filesystem "
6153 "argument (specify -a for all)\n"));
6158 * When mount is given no arguments, go through /etc/mnttab and
6159 * display any active ZFS mounts. We hide any snapshots, since
6160 * they are controlled automatically.
6162 rewind(mnttab_file);
6163 while (getmntent(mnttab_file, &entry) == 0) {
6164 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6165 strchr(entry.mnt_special, '@') != NULL)
6168 (void) printf("%-30s %s\n", entry.mnt_special,
6176 (void) fprintf(stderr,
6177 gettext("too many arguments\n"));
6181 if ((zhp = zfs_open(g_zfs, argv[0],
6182 ZFS_TYPE_FILESYSTEM)) == NULL) {
6185 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6195 * zfs mount -a [nfs]
6196 * zfs mount filesystem
6198 * Mount all filesystems, or mount the given filesystem.
6201 zfs_do_mount(int argc, char **argv)
6203 return (share_mount(OP_MOUNT, argc, argv));
6207 * zfs share -a [nfs | smb]
6208 * zfs share filesystem
6210 * Share all filesystems, or share the given filesystem.
6213 zfs_do_share(int argc, char **argv)
6215 return (share_mount(OP_SHARE, argc, argv));
6218 typedef struct unshare_unmount_node {
6219 zfs_handle_t *un_zhp;
6221 uu_avl_node_t un_avlnode;
6222 } unshare_unmount_node_t;
6226 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6228 const unshare_unmount_node_t *l = larg;
6229 const unshare_unmount_node_t *r = rarg;
6231 return (strcmp(l->un_mountp, r->un_mountp));
6235 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6236 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6237 * and unmount it appropriately.
6240 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6244 struct stat64 statbuf;
6245 struct extmnttab entry;
6246 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6250 * Search for the path in /etc/mnttab. Rather than looking for the
6251 * specific path, which can be fooled by non-standard paths (i.e. ".."
6252 * or "//"), we stat() the path and search for the corresponding
6253 * (major,minor) device pair.
6255 if (stat64(path, &statbuf) != 0) {
6256 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6257 cmdname, path, strerror(errno));
6260 path_inode = statbuf.st_ino;
6263 * Search for the given (major,minor) pair in the mount table.
6266 rewind(mnttab_file);
6267 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6268 if (entry.mnt_major == major(statbuf.st_dev) &&
6269 entry.mnt_minor == minor(statbuf.st_dev))
6276 if (statfs(path, &sfs) != 0) {
6277 (void) fprintf(stderr, "%s: %s\n", path,
6281 statfs2mnttab(&sfs, &entry);
6285 if (op == OP_SHARE) {
6286 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6287 "currently mounted\n"), cmdname, path);
6290 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6292 if ((ret = umount2(path, flags)) != 0)
6293 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6298 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6299 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6300 "filesystem\n"), cmdname, path);
6304 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6305 ZFS_TYPE_FILESYSTEM)) == NULL)
6309 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6310 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6311 cmdname, path, strerror(errno));
6313 } else if (statbuf.st_ino != path_inode) {
6314 (void) fprintf(stderr, gettext("cannot "
6315 "%s '%s': not a mountpoint\n"), cmdname, path);
6319 if (op == OP_SHARE) {
6320 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6321 char smbshare_prop[ZFS_MAXPROPLEN];
6323 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6324 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6325 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6326 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6328 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6329 strcmp(smbshare_prop, "off") == 0) {
6330 (void) fprintf(stderr, gettext("cannot unshare "
6331 "'%s': legacy share\n"), path);
6333 (void) fprintf(stderr, gettext("use "
6334 "unshare(1M) to unshare this filesystem\n"));
6336 } else if (!zfs_is_shared(zhp)) {
6337 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6338 "not currently shared\n"), path);
6340 ret = zfs_unshareall_bypath(zhp, path);
6343 char mtpt_prop[ZFS_MAXPROPLEN];
6345 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6346 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6349 ret = zfs_unmount(zhp, NULL, flags);
6350 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6351 (void) fprintf(stderr, gettext("cannot unmount "
6352 "'%s': legacy mountpoint\n"),
6354 (void) fprintf(stderr, gettext("use umount(8) "
6355 "to unmount this filesystem\n"));
6357 ret = zfs_unmountall(zhp, flags);
6368 * Generic callback for unsharing or unmounting a filesystem.
6371 unshare_unmount(int op, int argc, char **argv)
6378 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6379 char sharesmb[ZFS_MAXPROPLEN];
6382 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6391 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6402 * We could make use of zfs_for_each() to walk all datasets in
6403 * the system, but this would be very inefficient, especially
6404 * since we would have to linearly search /etc/mnttab for each
6405 * one. Instead, do one pass through /etc/mnttab looking for
6406 * zfs entries and call zfs_unmount() for each one.
6408 * Things get a little tricky if the administrator has created
6409 * mountpoints beneath other ZFS filesystems. In this case, we
6410 * have to unmount the deepest filesystems first. To accomplish
6411 * this, we place all the mountpoints in an AVL tree sorted by
6412 * the special type (dataset name), and walk the result in
6413 * reverse to make sure to get any snapshots first.
6415 struct mnttab entry;
6416 uu_avl_pool_t *pool;
6418 unshare_unmount_node_t *node;
6420 uu_avl_walk_t *walk;
6423 (void) fprintf(stderr, gettext("too many arguments\n"));
6427 if (((pool = uu_avl_pool_create("unmount_pool",
6428 sizeof (unshare_unmount_node_t),
6429 offsetof(unshare_unmount_node_t, un_avlnode),
6430 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6431 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6434 rewind(mnttab_file);
6435 while (getmntent(mnttab_file, &entry) == 0) {
6437 /* ignore non-ZFS entries */
6438 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6441 /* ignore snapshots */
6442 if (strchr(entry.mnt_special, '@') != NULL)
6445 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6446 ZFS_TYPE_FILESYSTEM)) == NULL) {
6453 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6455 sizeof (nfs_mnt_prop),
6456 NULL, NULL, 0, B_FALSE) == 0);
6457 if (strcmp(nfs_mnt_prop, "off") != 0)
6459 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6461 sizeof (nfs_mnt_prop),
6462 NULL, NULL, 0, B_FALSE) == 0);
6463 if (strcmp(nfs_mnt_prop, "off") == 0)
6467 /* Ignore legacy mounts */
6468 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6470 sizeof (nfs_mnt_prop),
6471 NULL, NULL, 0, B_FALSE) == 0);
6472 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6474 /* Ignore canmount=noauto mounts */
6475 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6476 ZFS_CANMOUNT_NOAUTO)
6482 node = safe_malloc(sizeof (unshare_unmount_node_t));
6484 node->un_mountp = safe_strdup(entry.mnt_mountp);
6486 uu_avl_node_init(node, &node->un_avlnode, pool);
6488 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6489 uu_avl_insert(tree, node, idx);
6491 zfs_close(node->un_zhp);
6492 free(node->un_mountp);
6498 * Walk the AVL tree in reverse, unmounting each filesystem and
6499 * removing it from the AVL tree in the process.
6501 if ((walk = uu_avl_walk_start(tree,
6502 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6505 while ((node = uu_avl_walk_next(walk)) != NULL) {
6506 uu_avl_remove(tree, node);
6510 if (zfs_unshareall_bypath(node->un_zhp,
6511 node->un_mountp) != 0)
6516 if (zfs_unmount(node->un_zhp,
6517 node->un_mountp, flags) != 0)
6522 zfs_close(node->un_zhp);
6523 free(node->un_mountp);
6527 uu_avl_walk_end(walk);
6528 uu_avl_destroy(tree);
6529 uu_avl_pool_destroy(pool);
6534 (void) fprintf(stderr,
6535 gettext("missing filesystem argument\n"));
6537 (void) fprintf(stderr,
6538 gettext("too many arguments\n"));
6543 * We have an argument, but it may be a full path or a ZFS
6544 * filesystem. Pass full paths off to unmount_path() (shared by
6545 * manual_unmount), otherwise open the filesystem and pass to
6548 if (argv[0][0] == '/')
6549 return (unshare_unmount_path(op, argv[0],
6552 if ((zhp = zfs_open(g_zfs, argv[0],
6553 ZFS_TYPE_FILESYSTEM)) == NULL)
6556 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6557 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6558 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6559 NULL, 0, B_FALSE) == 0);
6563 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6565 sizeof (nfs_mnt_prop),
6566 NULL, NULL, 0, B_FALSE) == 0);
6567 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6568 sharesmb, sizeof (sharesmb), NULL, NULL,
6571 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6572 strcmp(sharesmb, "off") == 0) {
6573 (void) fprintf(stderr, gettext("cannot "
6574 "unshare '%s': legacy share\n"),
6577 (void) fprintf(stderr, gettext("use "
6578 "unshare(1M) to unshare this "
6582 } else if (!zfs_is_shared(zhp)) {
6583 (void) fprintf(stderr, gettext("cannot "
6584 "unshare '%s': not currently "
6585 "shared\n"), zfs_get_name(zhp));
6587 } else if (zfs_unshareall(zhp) != 0) {
6593 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6594 (void) fprintf(stderr, gettext("cannot "
6595 "unmount '%s': legacy "
6596 "mountpoint\n"), zfs_get_name(zhp));
6597 (void) fprintf(stderr, gettext("use "
6598 "umount(8) to unmount this "
6601 } else if (!zfs_is_mounted(zhp, NULL)) {
6602 (void) fprintf(stderr, gettext("cannot "
6603 "unmount '%s': not currently "
6607 } else if (zfs_unmountall(zhp, flags) != 0) {
6621 * zfs unmount filesystem
6623 * Unmount all filesystems, or a specific ZFS filesystem.
6626 zfs_do_unmount(int argc, char **argv)
6628 return (unshare_unmount(OP_MOUNT, argc, argv));
6633 * zfs unshare filesystem
6635 * Unshare all filesystems, or a specific ZFS filesystem.
6638 zfs_do_unshare(int argc, char **argv)
6640 return (unshare_unmount(OP_SHARE, argc, argv));
6644 * Attach/detach the given dataset to/from the given jail
6648 do_jail(int argc, char **argv, int attach)
6653 /* check number of arguments */
6655 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6659 (void) fprintf(stderr, gettext("too many arguments\n"));
6663 jailid = jail_getid(argv[1]);
6665 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6669 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6673 ret = (zfs_jail(zhp, jailid, attach) != 0);
6680 * zfs jail jailid filesystem
6682 * Attach the given dataset to the given jail
6686 zfs_do_jail(int argc, char **argv)
6689 return (do_jail(argc, argv, 1));
6693 * zfs unjail jailid filesystem
6695 * Detach the given dataset from the given jail
6699 zfs_do_unjail(int argc, char **argv)
6702 return (do_jail(argc, argv, 0));
6706 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6707 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6710 manual_mount(int argc, char **argv)
6713 char mountpoint[ZFS_MAXPROPLEN];
6714 char mntopts[MNT_LINE_MAX] = { '\0' };
6718 char *dataset, *path;
6721 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6724 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6727 flags |= MS_OVERLAY;
6730 flags |= MS_NOMNTTAB;
6733 (void) fprintf(stderr, gettext("missing argument for "
6734 "'%c' option\n"), optopt);
6738 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6740 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6749 /* check that we only have two arguments */
6752 (void) fprintf(stderr, gettext("missing dataset "
6755 (void) fprintf(stderr,
6756 gettext("missing mountpoint argument\n"));
6758 (void) fprintf(stderr, gettext("too many arguments\n"));
6759 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6766 /* try to open the dataset */
6767 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6770 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6771 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6773 /* check for legacy mountpoint and complain appropriately */
6775 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6776 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6777 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6778 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6783 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6784 "mounted using 'mount -t zfs'\n"), dataset);
6785 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6786 "instead.\n"), path);
6787 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6788 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6789 (void) fprintf(stderr, gettext("See zfs(8) for more "
6798 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6799 * unmounts of non-legacy filesystems, as this is the dominant administrative
6803 manual_unmount(int argc, char **argv)
6809 while ((c = getopt(argc, argv, "f")) != -1) {
6815 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6817 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6826 /* check arguments */
6829 (void) fprintf(stderr, gettext("missing path "
6832 (void) fprintf(stderr, gettext("too many arguments\n"));
6833 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6837 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6841 find_command_idx(char *command, int *idx)
6845 for (i = 0; i < NCOMMAND; i++) {
6846 if (command_table[i].name == NULL)
6849 if (strcmp(command, command_table[i].name) == 0) {
6858 zfs_do_diff(int argc, char **argv)
6862 char *tosnap = NULL;
6863 char *fromsnap = NULL;
6868 while ((c = getopt(argc, argv, "FHt")) != -1) {
6871 flags |= ZFS_DIFF_CLASSIFY;
6874 flags |= ZFS_DIFF_PARSEABLE;
6877 flags |= ZFS_DIFF_TIMESTAMP;
6880 (void) fprintf(stderr,
6881 gettext("invalid option '%c'\n"), optopt);
6890 (void) fprintf(stderr,
6891 gettext("must provide at least one snapshot name\n"));
6896 (void) fprintf(stderr, gettext("too many arguments\n"));
6901 tosnap = (argc == 2) ? argv[1] : NULL;
6904 if (*fromsnap != '@')
6905 copy = strdup(fromsnap);
6907 copy = strdup(tosnap);
6911 if (atp = strchr(copy, '@'))
6914 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6920 * Ignore SIGPIPE so that the library can give us
6921 * information on any failure
6923 (void) sigignore(SIGPIPE);
6925 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6933 * zfs bookmark <fs@snap> <fs#bmark>
6935 * Creates a bookmark with the given name from the given snapshot.
6938 zfs_do_bookmark(int argc, char **argv)
6940 char snapname[ZFS_MAXNAMELEN];
6947 while ((c = getopt(argc, argv, "")) != -1) {
6950 (void) fprintf(stderr,
6951 gettext("invalid option '%c'\n"), optopt);
6959 /* check number of arguments */
6961 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6965 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6969 if (strchr(argv[1], '#') == NULL) {
6970 (void) fprintf(stderr,
6971 gettext("invalid bookmark name '%s' -- "
6972 "must contain a '#'\n"), argv[1]);
6976 if (argv[0][0] == '@') {
6978 * Snapshot name begins with @.
6979 * Default to same fs as bookmark.
6981 (void) strncpy(snapname, argv[1], sizeof (snapname));
6982 *strchr(snapname, '#') = '\0';
6983 (void) strlcat(snapname, argv[0], sizeof (snapname));
6985 (void) strncpy(snapname, argv[0], sizeof (snapname));
6987 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6993 nvl = fnvlist_alloc();
6994 fnvlist_add_string(nvl, argv[1], snapname);
6995 ret = lzc_bookmark(nvl, NULL);
6999 const char *err_msg;
7002 (void) snprintf(errbuf, sizeof (errbuf),
7003 dgettext(TEXT_DOMAIN,
7004 "cannot create bookmark '%s'"), argv[1]);
7008 err_msg = "bookmark is in a different pool";
7011 err_msg = "bookmark exists";
7014 err_msg = "invalid argument";
7017 err_msg = "bookmark feature not enabled";
7020 err_msg = "out of space";
7023 err_msg = "unknown error";
7026 (void) fprintf(stderr, "%s: %s\n", errbuf,
7027 dgettext(TEXT_DOMAIN, err_msg));
7038 main(int argc, char **argv)
7045 (void) setlocale(LC_ALL, "");
7046 (void) textdomain(TEXT_DOMAIN);
7050 if ((g_zfs = libzfs_init()) == NULL) {
7051 (void) fprintf(stderr, gettext("internal error: failed to "
7052 "initialize ZFS library\n"));
7056 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7058 libzfs_print_on_error(g_zfs, B_TRUE);
7060 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
7061 (void) fprintf(stderr, gettext("internal error: unable to "
7062 "open %s\n"), MNTTAB);
7067 * This command also doubles as the /etc/fs mount and unmount program.
7068 * Determine if we should take this behavior based on argv[0].
7070 progname = basename(argv[0]);
7071 if (strcmp(progname, "mount") == 0) {
7072 ret = manual_mount(argc, argv);
7073 } else if (strcmp(progname, "umount") == 0) {
7074 ret = manual_unmount(argc, argv);
7077 * Make sure the user has specified some command.
7080 (void) fprintf(stderr, gettext("missing command\n"));
7087 * The 'umount' command is an alias for 'unmount'
7089 if (strcmp(cmdname, "umount") == 0)
7090 cmdname = "unmount";
7093 * The 'recv' command is an alias for 'receive'
7095 if (strcmp(cmdname, "recv") == 0)
7096 cmdname = "receive";
7099 * The 'snap' command is an alias for 'snapshot'
7101 if (strcmp(cmdname, "snap") == 0)
7102 cmdname = "snapshot";
7107 if (strcmp(cmdname, "-?") == 0)
7111 * Run the appropriate command.
7113 libzfs_mnttab_cache(g_zfs, B_TRUE);
7114 if (find_command_idx(cmdname, &i) == 0) {
7115 current_command = &command_table[i];
7116 ret = command_table[i].func(argc - 1, argv + 1);
7117 } else if (strchr(cmdname, '=') != NULL) {
7118 verify(find_command_idx("set", &i) == 0);
7119 current_command = &command_table[i];
7120 ret = command_table[i].func(argc, argv);
7122 (void) fprintf(stderr, gettext("unrecognized "
7123 "command '%s'\n"), cmdname);
7126 libzfs_mnttab_cache(g_zfs, B_FALSE);
7129 (void) fclose(mnttab_file);
7131 if (ret == 0 && log_history)
7132 (void) zpool_log_history(g_zfs, history_str);
7137 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7138 * for the purposes of running ::findleaks.
7140 if (getenv("ZFS_ABORT") != NULL) {
7141 (void) printf("dumping core by request\n");