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, 2016 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. All rights reserved.
28 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright (c) 2014 Integros [integros.com]
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
32 * Copyright 2016 Nexenta Systems, Inc.
42 #include <libnvpair.h>
54 #include <sys/debug.h>
56 #include <sys/mntent.h>
57 #include <sys/mnttab.h>
58 #include <sys/mount.h>
60 #include <sys/fs/zfs.h>
61 #include <sys/types.h>
67 #include <libzfs_core.h>
69 #include <zfs_deleg.h>
73 #include <directory.h>
79 #include "zfs_comutil.h"
81 libzfs_handle_t *g_zfs;
83 static FILE *mnttab_file;
84 static char history_str[HIS_MAX_RECORD_LEN];
85 static boolean_t log_history = B_TRUE;
87 static int zfs_do_clone(int argc, char **argv);
88 static int zfs_do_create(int argc, char **argv);
89 static int zfs_do_destroy(int argc, char **argv);
90 static int zfs_do_get(int argc, char **argv);
91 static int zfs_do_inherit(int argc, char **argv);
92 static int zfs_do_list(int argc, char **argv);
93 static int zfs_do_mount(int argc, char **argv);
94 static int zfs_do_rename(int argc, char **argv);
95 static int zfs_do_rollback(int argc, char **argv);
96 static int zfs_do_set(int argc, char **argv);
97 static int zfs_do_upgrade(int argc, char **argv);
98 static int zfs_do_snapshot(int argc, char **argv);
99 static int zfs_do_unmount(int argc, char **argv);
100 static int zfs_do_share(int argc, char **argv);
101 static int zfs_do_unshare(int argc, char **argv);
102 static int zfs_do_send(int argc, char **argv);
103 static int zfs_do_receive(int argc, char **argv);
104 static int zfs_do_promote(int argc, char **argv);
105 static int zfs_do_userspace(int argc, char **argv);
106 static int zfs_do_allow(int argc, char **argv);
107 static int zfs_do_unallow(int argc, char **argv);
108 static int zfs_do_hold(int argc, char **argv);
109 static int zfs_do_holds(int argc, char **argv);
110 static int zfs_do_release(int argc, char **argv);
111 static int zfs_do_diff(int argc, char **argv);
112 static int zfs_do_jail(int argc, char **argv);
113 static int zfs_do_unjail(int argc, char **argv);
114 static int zfs_do_bookmark(int argc, char **argv);
115 static int zfs_do_remap(int argc, char **argv);
116 static int zfs_do_channel_program(int argc, char **argv);
119 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
124 _umem_debug_init(void)
126 return ("default,verbose"); /* $UMEM_DEBUG setting */
130 _umem_logging_init(void)
132 return ("fail,contents"); /* $UMEM_LOGGING setting */
167 HELP_CHANNEL_PROGRAM,
170 typedef struct zfs_command {
172 int (*func)(int argc, char **argv);
177 * Master command table. Each ZFS command has a name, associated function, and
178 * usage message. The usage messages need to be internationalized, so we have
179 * to have a function to return the usage message based on a command index.
181 * These commands are organized according to how they are displayed in the usage
182 * message. An empty command (one with a NULL name) indicates an empty line in
183 * the generic usage message.
185 static zfs_command_t command_table[] = {
186 { "create", zfs_do_create, HELP_CREATE },
187 { "destroy", zfs_do_destroy, HELP_DESTROY },
189 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
190 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
191 { "clone", zfs_do_clone, HELP_CLONE },
192 { "promote", zfs_do_promote, HELP_PROMOTE },
193 { "rename", zfs_do_rename, HELP_RENAME },
194 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
195 { "program", zfs_do_channel_program, HELP_CHANNEL_PROGRAM },
197 { "list", zfs_do_list, HELP_LIST },
199 { "set", zfs_do_set, HELP_SET },
200 { "get", zfs_do_get, HELP_GET },
201 { "inherit", zfs_do_inherit, HELP_INHERIT },
202 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
203 { "userspace", zfs_do_userspace, HELP_USERSPACE },
204 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
206 { "mount", zfs_do_mount, HELP_MOUNT },
207 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
208 { "share", zfs_do_share, HELP_SHARE },
209 { "unshare", zfs_do_unshare, HELP_UNSHARE },
211 { "send", zfs_do_send, HELP_SEND },
212 { "receive", zfs_do_receive, HELP_RECEIVE },
214 { "allow", zfs_do_allow, HELP_ALLOW },
216 { "unallow", zfs_do_unallow, HELP_UNALLOW },
218 { "hold", zfs_do_hold, HELP_HOLD },
219 { "holds", zfs_do_holds, HELP_HOLDS },
220 { "release", zfs_do_release, HELP_RELEASE },
221 { "diff", zfs_do_diff, HELP_DIFF },
223 { "jail", zfs_do_jail, HELP_JAIL },
224 { "unjail", zfs_do_unjail, HELP_UNJAIL },
225 { "remap", zfs_do_remap, HELP_REMAP },
228 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
230 zfs_command_t *current_command;
233 get_usage(zfs_help_t idx)
237 return (gettext("\tclone [-p] [-o property=value] ... "
238 "<snapshot> <filesystem|volume>\n"));
240 return (gettext("\tcreate [-pu] [-o property=value] ... "
242 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
243 "-V <size> <volume>\n"));
245 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
246 "\tdestroy [-dnpRrv] "
247 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
248 "\tdestroy <filesystem|volume>#<bookmark>\n"));
250 return (gettext("\tget [-rHp] [-d max] "
251 "[-o \"all\" | field[,...]]\n"
252 "\t [-t type[,...]] [-s source[,...]]\n"
253 "\t <\"all\" | property[,...]> "
254 "[filesystem|volume|snapshot|bookmark] ...\n"));
256 return (gettext("\tinherit [-rS] <property> "
257 "<filesystem|volume|snapshot> ...\n"));
259 return (gettext("\tupgrade [-v]\n"
260 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
262 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
264 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
266 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
267 "[-s property]...\n\t [-S property]... [-t type[,...]] "
268 "[filesystem|volume|snapshot] ...\n"));
270 return (gettext("\tmount\n"
271 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
273 return (gettext("\tpromote <clone-filesystem>\n"));
275 return (gettext("\treceive|recv [-vnsFu] <filesystem|volume|"
277 "\treceive|recv [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
279 "\treceive|recv -A <filesystem|volume>\n"));
281 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
282 "<filesystem|volume|snapshot>\n"
283 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
284 "\trename -r <snapshot> <snapshot>\n"
285 "\trename -u [-p] <filesystem> <filesystem>"));
287 return (gettext("\trollback [-rRf] <snapshot>\n"));
289 return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] "
291 "\tsend [-Le] [-i snapshot|bookmark] "
292 "<filesystem|volume|snapshot>\n"
293 "\tsend [-nvPe] -t <receive_resume_token>\n"));
295 return (gettext("\tset <property=value> ... "
296 "<filesystem|volume|snapshot> ...\n"));
298 return (gettext("\tshare <-a | filesystem>\n"));
300 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
301 "<filesystem|volume>@<snap> ...\n"));
303 return (gettext("\tunmount|umount [-f] "
304 "<-a | filesystem|mountpoint>\n"));
306 return (gettext("\tunshare "
307 "<-a | filesystem|mountpoint>\n"));
309 return (gettext("\tallow <filesystem|volume>\n"
311 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
312 "\t <filesystem|volume>\n"
313 "\tallow [-ld] -e <perm|@setname>[,...] "
314 "<filesystem|volume>\n"
315 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
316 "\tallow -s @setname <perm|@setname>[,...] "
317 "<filesystem|volume>\n"));
319 return (gettext("\tunallow [-rldug] "
320 "<\"everyone\"|user|group>[,...]\n"
321 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
322 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
323 "<filesystem|volume>\n"
324 "\tunallow [-r] -c [<perm|@setname>[,...]] "
325 "<filesystem|volume>\n"
326 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
327 "<filesystem|volume>\n"));
329 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
331 "\t [-S field] ... [-t type[,...]] "
332 "<filesystem|snapshot>\n"));
333 case HELP_GROUPSPACE:
334 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
336 "\t [-S field] ... [-t type[,...]] "
337 "<filesystem|snapshot>\n"));
339 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
341 return (gettext("\tholds [-Hp] [-r|-d depth] "
342 "<filesystem|volume|snapshot> ...\n"));
344 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
346 return (gettext("\tdiff [-FHt] <snapshot> "
347 "[snapshot|filesystem]\n"));
349 return (gettext("\tremap <filesystem | volume>\n"));
351 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
352 case HELP_CHANNEL_PROGRAM:
353 return (gettext("\tprogram [-n] [-t <instruction limit>] "
354 "[-m <memory limit (b)>] <pool> <program file> "
365 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
370 * Utility function to guarantee malloc() success.
374 safe_malloc(size_t size)
378 if ((data = calloc(1, size)) == NULL)
385 safe_realloc(void *data, size_t size)
388 if ((newp = realloc(data, size)) == NULL) {
397 safe_strdup(char *str)
399 char *dupstr = strdup(str);
408 * Callback routine that will print out information for each of
412 usage_prop_cb(int prop, void *cb)
416 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
418 if (zfs_prop_readonly(prop))
419 (void) fprintf(fp, " NO ");
421 (void) fprintf(fp, "YES ");
423 if (zfs_prop_inheritable(prop))
424 (void) fprintf(fp, " YES ");
426 (void) fprintf(fp, " NO ");
428 if (zfs_prop_values(prop) == NULL)
429 (void) fprintf(fp, "-\n");
431 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
437 * Display usage message. If we're inside a command, display only the usage for
438 * that command. Otherwise, iterate over the entire command table and display
439 * a complete usage message.
442 usage(boolean_t requested)
445 boolean_t show_properties = B_FALSE;
446 FILE *fp = requested ? stdout : stderr;
448 if (current_command == NULL) {
450 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
452 gettext("where 'command' is one of the following:\n\n"));
454 for (i = 0; i < NCOMMAND; i++) {
455 if (command_table[i].name == NULL)
456 (void) fprintf(fp, "\n");
458 (void) fprintf(fp, "%s",
459 get_usage(command_table[i].usage));
462 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
463 "pool/[dataset/]*dataset[@name]\n"));
465 (void) fprintf(fp, gettext("usage:\n"));
466 (void) fprintf(fp, "%s", get_usage(current_command->usage));
469 if (current_command != NULL &&
470 (strcmp(current_command->name, "set") == 0 ||
471 strcmp(current_command->name, "get") == 0 ||
472 strcmp(current_command->name, "inherit") == 0 ||
473 strcmp(current_command->name, "list") == 0))
474 show_properties = B_TRUE;
476 if (show_properties) {
478 gettext("\nThe following properties are supported:\n"));
480 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
481 "PROPERTY", "EDIT", "INHERIT", "VALUES");
483 /* Iterate over all properties */
484 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
487 (void) fprintf(fp, "\t%-15s ", "userused@...");
488 (void) fprintf(fp, " NO NO <size>\n");
489 (void) fprintf(fp, "\t%-15s ", "groupused@...");
490 (void) fprintf(fp, " NO NO <size>\n");
491 (void) fprintf(fp, "\t%-15s ", "userquota@...");
492 (void) fprintf(fp, "YES NO <size> | none\n");
493 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
494 (void) fprintf(fp, "YES NO <size> | none\n");
495 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
496 (void) fprintf(fp, " NO NO <size>\n");
498 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
499 "with standard units such as K, M, G, etc.\n"));
500 (void) fprintf(fp, gettext("\nUser-defined properties can "
501 "be specified by using a name containing a colon (:).\n"));
502 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
503 "properties must be appended with\n"
504 "a user or group specifier of one of these forms:\n"
505 " POSIX name (eg: \"matt\")\n"
506 " POSIX id (eg: \"126829\")\n"
507 " SMB name@domain (eg: \"matt@sun\")\n"
508 " SMB SID (eg: \"S-1-234-567-89\")\n"));
511 gettext("\nFor the property list, run: %s\n"),
514 gettext("\nFor the delegated permission list, run: %s\n"),
515 "zfs allow|unallow");
519 * See comments at end of main().
521 if (getenv("ZFS_ABORT") != NULL) {
522 (void) printf("dumping core by request\n");
526 exit(requested ? 0 : 2);
530 * Take a property=value argument string and add it to the given nvlist.
531 * Modifies the argument inplace.
534 parseprop(nvlist_t *props, char *propname)
536 char *propval, *strval;
538 if ((propval = strchr(propname, '=')) == NULL) {
539 (void) fprintf(stderr, gettext("missing "
540 "'=' for property=value argument\n"));
545 if (nvlist_lookup_string(props, propname, &strval) == 0) {
546 (void) fprintf(stderr, gettext("property '%s' "
547 "specified multiple times\n"), propname);
550 if (nvlist_add_string(props, propname, propval) != 0)
556 parse_depth(char *opt, int *flags)
561 depth = (int)strtol(opt, &tmp, 0);
563 (void) fprintf(stderr,
564 gettext("%s is not an integer\n"), opt);
568 (void) fprintf(stderr,
569 gettext("Depth can not be negative.\n"));
572 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
576 #define PROGRESS_DELAY 2 /* seconds */
578 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";
579 static time_t pt_begin;
580 static char *pt_header = NULL;
581 static boolean_t pt_shown;
584 start_progress_timer(void)
586 pt_begin = time(NULL) + PROGRESS_DELAY;
591 set_progress_header(char *header)
593 assert(pt_header == NULL);
594 pt_header = safe_strdup(header);
596 (void) printf("%s: ", header);
597 (void) fflush(stdout);
602 update_progress(char *update)
604 if (!pt_shown && time(NULL) > pt_begin) {
605 int len = strlen(update);
607 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
609 (void) fflush(stdout);
611 } else if (pt_shown) {
612 int len = strlen(update);
614 (void) printf("%s%*.*s", update, len, len, pt_reverse);
615 (void) fflush(stdout);
620 finish_progress(char *done)
623 (void) printf("%s\n", done);
624 (void) fflush(stdout);
631 * Check if the dataset is mountable and should be automatically mounted.
634 should_auto_mount(zfs_handle_t *zhp)
636 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp)))
638 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON);
642 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
644 * Given an existing dataset, create a writable copy whose initial contents
645 * are the same as the source. The newly created dataset maintains a
646 * dependency on the original; the original cannot be destroyed so long as
649 * The '-p' flag creates all the non-existing ancestors of the target first.
652 zfs_do_clone(int argc, char **argv)
654 zfs_handle_t *zhp = NULL;
655 boolean_t parents = B_FALSE;
660 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
664 while ((c = getopt(argc, argv, "o:p")) != -1) {
667 if (parseprop(props, optarg) != 0)
674 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
683 /* check number of arguments */
685 (void) fprintf(stderr, gettext("missing source dataset "
690 (void) fprintf(stderr, gettext("missing target dataset "
695 (void) fprintf(stderr, gettext("too many arguments\n"));
699 /* open the source dataset */
700 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
703 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
706 * Now create the ancestors of the target dataset. If the
707 * target already exists and '-p' option was used we should not
710 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
713 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
718 ret = zfs_clone(zhp, argv[1], props);
720 /* create the mountpoint if necessary */
724 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
727 * If the user doesn't want the dataset
728 * automatically mounted, then skip the mount/share
731 if (should_auto_mount(clone)) {
732 if ((ret = zfs_mount(clone, NULL, 0)) != 0) {
733 (void) fprintf(stderr, gettext("clone "
734 "successfully created, "
735 "but not mounted\n"));
736 } else if ((ret = zfs_share(clone)) != 0) {
737 (void) fprintf(stderr, gettext("clone "
738 "successfully created, "
739 "but not shared\n"));
760 * zfs create [-pu] [-o prop=value] ... fs
761 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
763 * Create a new dataset. This command can be used to create filesystems
764 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
765 * For volumes, the user must specify a size to be used.
767 * The '-s' flag applies only to volumes, and indicates that we should not try
768 * to set the reservation for this volume. By default we set a reservation
769 * equal to the size for any volume. For pools with SPA_VERSION >=
770 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
772 * The '-p' flag creates all the non-existing ancestors of the target first.
774 * The '-u' flag prevents mounting of newly created file system.
777 zfs_do_create(int argc, char **argv)
779 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
780 zfs_handle_t *zhp = NULL;
781 uint64_t volsize = 0;
783 boolean_t noreserve = B_FALSE;
784 boolean_t bflag = B_FALSE;
785 boolean_t parents = B_FALSE;
786 boolean_t nomount = B_FALSE;
791 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
795 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) {
798 type = ZFS_TYPE_VOLUME;
799 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
800 (void) fprintf(stderr, gettext("bad volume "
801 "size '%s': %s\n"), optarg,
802 libzfs_error_description(g_zfs));
806 if (nvlist_add_uint64(props,
807 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
816 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
817 (void) fprintf(stderr, gettext("bad volume "
818 "block size '%s': %s\n"), optarg,
819 libzfs_error_description(g_zfs));
823 if (nvlist_add_uint64(props,
824 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
829 if (parseprop(props, optarg) != 0)
839 (void) fprintf(stderr, gettext("missing size "
843 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
849 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
850 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
851 "used when creating a volume\n"));
854 if (nomount && type != ZFS_TYPE_FILESYSTEM) {
855 (void) fprintf(stderr, gettext("'-u' can only be "
856 "used when creating a file system\n"));
863 /* check number of arguments */
865 (void) fprintf(stderr, gettext("missing %s argument\n"),
866 zfs_type_to_name(type));
870 (void) fprintf(stderr, gettext("too many arguments\n"));
874 if (type == ZFS_TYPE_VOLUME && !noreserve) {
875 zpool_handle_t *zpool_handle;
876 nvlist_t *real_props = NULL;
877 uint64_t spa_version;
879 zfs_prop_t resv_prop;
883 if ((p = strchr(argv[0], '/')) != NULL)
885 zpool_handle = zpool_open(g_zfs, argv[0]);
888 if (zpool_handle == NULL)
890 spa_version = zpool_get_prop_int(zpool_handle,
891 ZPOOL_PROP_VERSION, NULL);
892 if (spa_version >= SPA_VERSION_REFRESERVATION)
893 resv_prop = ZFS_PROP_REFRESERVATION;
895 resv_prop = ZFS_PROP_RESERVATION;
897 (void) snprintf(msg, sizeof (msg),
898 gettext("cannot create '%s'"), argv[0]);
899 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
900 props, 0, NULL, zpool_handle, msg)) == NULL) {
901 zpool_close(zpool_handle);
904 zpool_close(zpool_handle);
906 volsize = zvol_volsize_to_reservation(volsize, real_props);
907 nvlist_free(real_props);
909 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
911 if (nvlist_add_uint64(props,
912 zfs_prop_to_name(resv_prop), volsize) != 0) {
919 if (parents && zfs_name_valid(argv[0], type)) {
921 * Now create the ancestors of target dataset. If the target
922 * already exists and '-p' option was used we should not
925 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
929 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
934 if (zfs_create(g_zfs, argv[0], type, props) != 0)
937 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
943 * Mount and/or share the new filesystem as appropriate. We provide a
944 * verbose error message to let the user know that their filesystem was
945 * in fact created, even if we failed to mount or share it.
946 * If the user doesn't want the dataset automatically mounted,
947 * then skip the mount/share step altogether.
949 if (!nomount && should_auto_mount(zhp)) {
950 if (zfs_mount(zhp, NULL, 0) != 0) {
951 (void) fprintf(stderr, gettext("filesystem "
952 "successfully created, but not mounted\n"));
954 } else if (zfs_share(zhp) != 0) {
955 (void) fprintf(stderr, gettext("filesystem "
956 "successfully created, but not shared\n"));
973 * zfs destroy [-rRf] <fs, vol>
974 * zfs destroy [-rRd] <snap>
976 * -r Recursively destroy all children
977 * -R Recursively destroy all dependents, including clones
978 * -f Force unmounting of any dependents
979 * -d If we can't destroy now, mark for deferred destruction
981 * Destroys the given dataset. By default, it will unmount any filesystems,
982 * and refuse to destroy a dataset that has any dependents. A dependent can
983 * either be a child, or a clone of a child.
985 typedef struct destroy_cbdata {
988 boolean_t cb_recurse;
990 boolean_t cb_doclones;
991 zfs_handle_t *cb_target;
992 boolean_t cb_defer_destroy;
993 boolean_t cb_verbose;
994 boolean_t cb_parsable;
997 nvlist_t *cb_batchedsnaps;
999 /* first snap in contiguous run */
1001 /* previous snap in contiguous run */
1003 int64_t cb_snapused;
1009 * Check for any dependents based on the '-r' or '-R' flags.
1012 destroy_check_dependent(zfs_handle_t *zhp, void *data)
1014 destroy_cbdata_t *cbp = data;
1015 const char *tname = zfs_get_name(cbp->cb_target);
1016 const char *name = zfs_get_name(zhp);
1018 if (strncmp(tname, name, strlen(tname)) == 0 &&
1019 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
1021 * This is a direct descendant, not a clone somewhere else in
1024 if (cbp->cb_recurse)
1027 if (cbp->cb_first) {
1028 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1029 "%s has children\n"),
1030 zfs_get_name(cbp->cb_target),
1031 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1032 (void) fprintf(stderr, gettext("use '-r' to destroy "
1033 "the following datasets:\n"));
1034 cbp->cb_first = B_FALSE;
1035 cbp->cb_error = B_TRUE;
1038 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1041 * This is a clone. We only want to report this if the '-r'
1042 * wasn't specified, or the target is a snapshot.
1044 if (!cbp->cb_recurse &&
1045 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1048 if (cbp->cb_first) {
1049 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1050 "%s has dependent clones\n"),
1051 zfs_get_name(cbp->cb_target),
1052 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1053 (void) fprintf(stderr, gettext("use '-R' to destroy "
1054 "the following datasets:\n"));
1055 cbp->cb_first = B_FALSE;
1056 cbp->cb_error = B_TRUE;
1057 cbp->cb_dryrun = B_TRUE;
1060 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1069 destroy_callback(zfs_handle_t *zhp, void *data)
1071 destroy_cbdata_t *cb = data;
1072 const char *name = zfs_get_name(zhp);
1074 if (cb->cb_verbose) {
1075 if (cb->cb_parsable) {
1076 (void) printf("destroy\t%s\n", name);
1077 } else if (cb->cb_dryrun) {
1078 (void) printf(gettext("would destroy %s\n"),
1081 (void) printf(gettext("will destroy %s\n"),
1087 * Ignore pools (which we've already flagged as an error before getting
1090 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1091 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1095 if (cb->cb_dryrun) {
1101 * We batch up all contiguous snapshots (even of different
1102 * filesystems) and destroy them with one ioctl. We can't
1103 * simply do all snap deletions and then all fs deletions,
1104 * because we must delete a clone before its origin.
1106 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1107 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1109 int error = zfs_destroy_snaps_nvl(g_zfs,
1110 cb->cb_batchedsnaps, B_FALSE);
1111 fnvlist_free(cb->cb_batchedsnaps);
1112 cb->cb_batchedsnaps = fnvlist_alloc();
1115 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1116 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1127 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1129 destroy_cbdata_t *cb = arg;
1130 const char *name = zfs_get_name(zhp);
1133 if (nvlist_exists(cb->cb_nvl, name)) {
1134 if (cb->cb_firstsnap == NULL)
1135 cb->cb_firstsnap = strdup(name);
1136 if (cb->cb_prevsnap != NULL)
1137 free(cb->cb_prevsnap);
1138 /* this snap continues the current range */
1139 cb->cb_prevsnap = strdup(name);
1140 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1142 if (cb->cb_verbose) {
1143 if (cb->cb_parsable) {
1144 (void) printf("destroy\t%s\n", name);
1145 } else if (cb->cb_dryrun) {
1146 (void) printf(gettext("would destroy %s\n"),
1149 (void) printf(gettext("will destroy %s\n"),
1153 } else if (cb->cb_firstsnap != NULL) {
1154 /* end of this range */
1156 err = lzc_snaprange_space(cb->cb_firstsnap,
1157 cb->cb_prevsnap, &used);
1158 cb->cb_snapused += used;
1159 free(cb->cb_firstsnap);
1160 cb->cb_firstsnap = NULL;
1161 free(cb->cb_prevsnap);
1162 cb->cb_prevsnap = NULL;
1169 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1172 assert(cb->cb_firstsnap == NULL);
1173 assert(cb->cb_prevsnap == NULL);
1174 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1175 if (cb->cb_firstsnap != NULL) {
1178 err = lzc_snaprange_space(cb->cb_firstsnap,
1179 cb->cb_prevsnap, &used);
1181 cb->cb_snapused += used;
1182 free(cb->cb_firstsnap);
1183 cb->cb_firstsnap = NULL;
1184 free(cb->cb_prevsnap);
1185 cb->cb_prevsnap = NULL;
1191 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1193 destroy_cbdata_t *cb = arg;
1196 /* Check for clones. */
1197 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1198 cb->cb_target = zhp;
1199 cb->cb_first = B_TRUE;
1200 err = zfs_iter_dependents(zhp, B_TRUE,
1201 destroy_check_dependent, cb);
1205 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1213 gather_snapshots(zfs_handle_t *zhp, void *arg)
1215 destroy_cbdata_t *cb = arg;
1218 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1224 if (cb->cb_verbose) {
1225 err = destroy_print_snapshots(zhp, cb);
1231 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1239 destroy_clones(destroy_cbdata_t *cb)
1242 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1244 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1245 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1248 boolean_t defer = cb->cb_defer_destroy;
1252 * We can't defer destroy non-snapshots, so set it to
1253 * false while destroying the clones.
1255 cb->cb_defer_destroy = B_FALSE;
1256 err = zfs_iter_dependents(zhp, B_FALSE,
1257 destroy_callback, cb);
1258 cb->cb_defer_destroy = defer;
1268 zfs_do_destroy(int argc, char **argv)
1270 destroy_cbdata_t cb = { 0 };
1274 zfs_handle_t *zhp = NULL;
1276 zfs_type_t type = ZFS_TYPE_DATASET;
1279 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1282 cb.cb_verbose = B_TRUE;
1285 cb.cb_verbose = B_TRUE;
1286 cb.cb_parsable = B_TRUE;
1289 cb.cb_dryrun = B_TRUE;
1292 cb.cb_defer_destroy = B_TRUE;
1293 type = ZFS_TYPE_SNAPSHOT;
1296 cb.cb_force = B_TRUE;
1299 cb.cb_recurse = B_TRUE;
1302 cb.cb_recurse = B_TRUE;
1303 cb.cb_doclones = B_TRUE;
1307 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1316 /* check number of arguments */
1318 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1322 (void) fprintf(stderr, gettext("too many arguments\n"));
1326 at = strchr(argv[0], '@');
1327 pound = strchr(argv[0], '#');
1330 /* Build the list of snaps to destroy in cb_nvl. */
1331 cb.cb_nvl = fnvlist_alloc();
1334 zhp = zfs_open(g_zfs, argv[0],
1335 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1339 cb.cb_snapspec = at + 1;
1340 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1346 if (nvlist_empty(cb.cb_nvl)) {
1347 (void) fprintf(stderr, gettext("could not find any "
1348 "snapshots to destroy; check snapshot names.\n"));
1353 if (cb.cb_verbose) {
1355 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1356 if (cb.cb_parsable) {
1357 (void) printf("reclaim\t%llu\n",
1359 } else if (cb.cb_dryrun) {
1360 (void) printf(gettext("would reclaim %s\n"),
1363 (void) printf(gettext("will reclaim %s\n"),
1368 if (!cb.cb_dryrun) {
1369 if (cb.cb_doclones) {
1370 cb.cb_batchedsnaps = fnvlist_alloc();
1371 err = destroy_clones(&cb);
1373 err = zfs_destroy_snaps_nvl(g_zfs,
1374 cb.cb_batchedsnaps, B_FALSE);
1382 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1383 cb.cb_defer_destroy);
1389 } else if (pound != NULL) {
1394 (void) fprintf(stderr,
1395 "dryrun is not supported with bookmark\n");
1399 if (cb.cb_defer_destroy) {
1400 (void) fprintf(stderr,
1401 "defer destroy is not supported with bookmark\n");
1405 if (cb.cb_recurse) {
1406 (void) fprintf(stderr,
1407 "recursive is not supported with bookmark\n");
1411 if (!zfs_bookmark_exists(argv[0])) {
1412 (void) fprintf(stderr, gettext("bookmark '%s' "
1413 "does not exist.\n"), argv[0]);
1417 nvl = fnvlist_alloc();
1418 fnvlist_add_boolean(nvl, argv[0]);
1420 err = lzc_destroy_bookmarks(nvl, NULL);
1422 (void) zfs_standard_error(g_zfs, err,
1423 "cannot destroy bookmark");
1426 nvlist_free(cb.cb_nvl);
1430 /* Open the given dataset */
1431 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1437 * Perform an explicit check for pools before going any further.
1439 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1440 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1441 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1442 "operation does not apply to pools\n"),
1444 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1445 "%s' to destroy all datasets in the pool\n"),
1447 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1448 "to destroy the pool itself\n"), zfs_get_name(zhp));
1454 * Check for any dependents and/or clones.
1456 cb.cb_first = B_TRUE;
1457 if (!cb.cb_doclones &&
1458 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1469 cb.cb_batchedsnaps = fnvlist_alloc();
1470 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1477 * Do the real thing. The callback will close the
1478 * handle regardless of whether it succeeds or not.
1480 err = destroy_callback(zhp, &cb);
1483 err = zfs_destroy_snaps_nvl(g_zfs,
1484 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1491 fnvlist_free(cb.cb_batchedsnaps);
1492 fnvlist_free(cb.cb_nvl);
1499 is_recvd_column(zprop_get_cbdata_t *cbp)
1502 zfs_get_column_t col;
1504 for (i = 0; i < ZFS_GET_NCOLS &&
1505 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1506 if (col == GET_COL_RECVD)
1512 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1513 * < all | property[,property]... > < fs | snap | vol > ...
1515 * -r recurse over any child datasets
1516 * -H scripted mode. Headers are stripped, and fields are separated
1517 * by tabs instead of spaces.
1518 * -o Set of fields to display. One of "name,property,value,
1519 * received,source". Default is "name,property,value,source".
1520 * "all" is an alias for all five.
1521 * -s Set of sources to allow. One of
1522 * "local,default,inherited,received,temporary,none". Default is
1524 * -p Display values in parsable (literal) format.
1526 * Prints properties for the given datasets. The user can control which
1527 * columns to display as well as which property types to allow.
1531 * Invoked to display the properties for a single dataset.
1534 get_callback(zfs_handle_t *zhp, void *data)
1536 char buf[ZFS_MAXPROPLEN];
1537 char rbuf[ZFS_MAXPROPLEN];
1538 zprop_source_t sourcetype;
1539 char source[ZFS_MAX_DATASET_NAME_LEN];
1540 zprop_get_cbdata_t *cbp = data;
1541 nvlist_t *user_props = zfs_get_user_props(zhp);
1542 zprop_list_t *pl = cbp->cb_proplist;
1546 boolean_t received = is_recvd_column(cbp);
1548 for (; pl != NULL; pl = pl->pl_next) {
1549 char *recvdval = NULL;
1551 * Skip the special fake placeholder. This will also skip over
1552 * the name property when 'all' is specified.
1554 if (pl->pl_prop == ZFS_PROP_NAME &&
1555 pl == cbp->cb_proplist)
1558 if (pl->pl_prop != ZPROP_INVAL) {
1559 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1560 sizeof (buf), &sourcetype, source,
1562 cbp->cb_literal) != 0) {
1565 if (!zfs_prop_valid_for_type(pl->pl_prop,
1566 ZFS_TYPE_DATASET)) {
1567 (void) fprintf(stderr,
1568 gettext("No such property '%s'\n"),
1569 zfs_prop_to_name(pl->pl_prop));
1572 sourcetype = ZPROP_SRC_NONE;
1573 (void) strlcpy(buf, "-", sizeof (buf));
1576 if (received && (zfs_prop_get_recvd(zhp,
1577 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1578 cbp->cb_literal) == 0))
1581 zprop_print_one_property(zfs_get_name(zhp), cbp,
1582 zfs_prop_to_name(pl->pl_prop),
1583 buf, sourcetype, source, recvdval);
1584 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1585 sourcetype = ZPROP_SRC_LOCAL;
1587 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1588 buf, sizeof (buf), cbp->cb_literal) != 0) {
1589 sourcetype = ZPROP_SRC_NONE;
1590 (void) strlcpy(buf, "-", sizeof (buf));
1593 zprop_print_one_property(zfs_get_name(zhp), cbp,
1594 pl->pl_user_prop, buf, sourcetype, source, NULL);
1595 } else if (zfs_prop_written(pl->pl_user_prop)) {
1596 sourcetype = ZPROP_SRC_LOCAL;
1598 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1599 buf, sizeof (buf), cbp->cb_literal) != 0) {
1600 sourcetype = ZPROP_SRC_NONE;
1601 (void) strlcpy(buf, "-", sizeof (buf));
1604 zprop_print_one_property(zfs_get_name(zhp), cbp,
1605 pl->pl_user_prop, buf, sourcetype, source, NULL);
1607 if (nvlist_lookup_nvlist(user_props,
1608 pl->pl_user_prop, &propval) != 0) {
1611 sourcetype = ZPROP_SRC_NONE;
1614 verify(nvlist_lookup_string(propval,
1615 ZPROP_VALUE, &strval) == 0);
1616 verify(nvlist_lookup_string(propval,
1617 ZPROP_SOURCE, &sourceval) == 0);
1619 if (strcmp(sourceval,
1620 zfs_get_name(zhp)) == 0) {
1621 sourcetype = ZPROP_SRC_LOCAL;
1622 } else if (strcmp(sourceval,
1623 ZPROP_SOURCE_VAL_RECVD) == 0) {
1624 sourcetype = ZPROP_SRC_RECEIVED;
1626 sourcetype = ZPROP_SRC_INHERITED;
1627 (void) strlcpy(source,
1628 sourceval, sizeof (source));
1632 if (received && (zfs_prop_get_recvd(zhp,
1633 pl->pl_user_prop, rbuf, sizeof (rbuf),
1634 cbp->cb_literal) == 0))
1637 zprop_print_one_property(zfs_get_name(zhp), cbp,
1638 pl->pl_user_prop, strval, sourcetype,
1647 zfs_do_get(int argc, char **argv)
1649 zprop_get_cbdata_t cb = { 0 };
1650 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1651 int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
1652 char *value, *fields;
1655 zprop_list_t fake_name = { 0 };
1658 * Set up default columns and sources.
1660 cb.cb_sources = ZPROP_SRC_ALL;
1661 cb.cb_columns[0] = GET_COL_NAME;
1662 cb.cb_columns[1] = GET_COL_PROPERTY;
1663 cb.cb_columns[2] = GET_COL_VALUE;
1664 cb.cb_columns[3] = GET_COL_SOURCE;
1665 cb.cb_type = ZFS_TYPE_DATASET;
1668 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1671 cb.cb_literal = B_TRUE;
1674 limit = parse_depth(optarg, &flags);
1677 flags |= ZFS_ITER_RECURSE;
1680 cb.cb_scripted = B_TRUE;
1683 (void) fprintf(stderr, gettext("missing argument for "
1684 "'%c' option\n"), optopt);
1689 * Process the set of columns to display. We zero out
1690 * the structure to give us a blank slate.
1692 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1694 while (*optarg != '\0') {
1695 static char *col_subopts[] =
1696 { "name", "property", "value", "received",
1697 "source", "all", NULL };
1699 if (i == ZFS_GET_NCOLS) {
1700 (void) fprintf(stderr, gettext("too "
1701 "many fields given to -o "
1706 switch (getsubopt(&optarg, col_subopts,
1709 cb.cb_columns[i++] = GET_COL_NAME;
1712 cb.cb_columns[i++] = GET_COL_PROPERTY;
1715 cb.cb_columns[i++] = GET_COL_VALUE;
1718 cb.cb_columns[i++] = GET_COL_RECVD;
1719 flags |= ZFS_ITER_RECVD_PROPS;
1722 cb.cb_columns[i++] = GET_COL_SOURCE;
1726 (void) fprintf(stderr,
1727 gettext("\"all\" conflicts "
1728 "with specific fields "
1729 "given to -o option\n"));
1732 cb.cb_columns[0] = GET_COL_NAME;
1733 cb.cb_columns[1] = GET_COL_PROPERTY;
1734 cb.cb_columns[2] = GET_COL_VALUE;
1735 cb.cb_columns[3] = GET_COL_RECVD;
1736 cb.cb_columns[4] = GET_COL_SOURCE;
1737 flags |= ZFS_ITER_RECVD_PROPS;
1741 (void) fprintf(stderr,
1742 gettext("invalid column name "
1743 "'%s'\n"), suboptarg);
1751 while (*optarg != '\0') {
1752 static char *source_subopts[] = {
1753 "local", "default", "inherited",
1754 "received", "temporary", "none",
1757 switch (getsubopt(&optarg, source_subopts,
1760 cb.cb_sources |= ZPROP_SRC_LOCAL;
1763 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1766 cb.cb_sources |= ZPROP_SRC_INHERITED;
1769 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1772 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1775 cb.cb_sources |= ZPROP_SRC_NONE;
1778 (void) fprintf(stderr,
1779 gettext("invalid source "
1780 "'%s'\n"), suboptarg);
1788 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1789 while (*optarg != '\0') {
1790 static char *type_subopts[] = { "filesystem",
1791 "volume", "snapshot", "bookmark",
1794 switch (getsubopt(&optarg, type_subopts,
1797 types |= ZFS_TYPE_FILESYSTEM;
1800 types |= ZFS_TYPE_VOLUME;
1803 types |= ZFS_TYPE_SNAPSHOT;
1806 types |= ZFS_TYPE_BOOKMARK;
1809 types = ZFS_TYPE_DATASET |
1814 (void) fprintf(stderr,
1815 gettext("invalid type '%s'\n"),
1823 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1833 (void) fprintf(stderr, gettext("missing property "
1840 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1848 * As part of zfs_expand_proplist(), we keep track of the maximum column
1849 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1850 * need to know the maximum name length. However, the user likely did
1851 * not specify 'name' as one of the properties to fetch, so we need to
1852 * make sure we always include at least this property for
1853 * print_get_headers() to work properly.
1855 if (cb.cb_proplist != NULL) {
1856 fake_name.pl_prop = ZFS_PROP_NAME;
1857 fake_name.pl_width = strlen(gettext("NAME"));
1858 fake_name.pl_next = cb.cb_proplist;
1859 cb.cb_proplist = &fake_name;
1862 cb.cb_first = B_TRUE;
1864 /* run for each object */
1865 ret = zfs_for_each(argc, argv, flags, types, NULL,
1866 &cb.cb_proplist, limit, get_callback, &cb);
1868 if (cb.cb_proplist == &fake_name)
1869 zprop_free_list(fake_name.pl_next);
1871 zprop_free_list(cb.cb_proplist);
1877 * inherit [-rS] <property> <fs|vol> ...
1879 * -r Recurse over all children
1880 * -S Revert to received value, if any
1882 * For each dataset specified on the command line, inherit the given property
1883 * from its parent. Inheriting a property at the pool level will cause it to
1884 * use the default value. The '-r' flag will recurse over all children, and is
1885 * useful for setting a property on a hierarchy-wide basis, regardless of any
1886 * local modifications for each dataset.
1889 typedef struct inherit_cbdata {
1890 const char *cb_propname;
1891 boolean_t cb_received;
1895 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1897 inherit_cbdata_t *cb = data;
1898 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1901 * If we're doing it recursively, then ignore properties that
1902 * are not valid for this type of dataset.
1904 if (prop != ZPROP_INVAL &&
1905 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1908 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1912 inherit_cb(zfs_handle_t *zhp, void *data)
1914 inherit_cbdata_t *cb = data;
1916 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1920 zfs_do_inherit(int argc, char **argv)
1924 inherit_cbdata_t cb = { 0 };
1928 boolean_t received = B_FALSE;
1931 while ((c = getopt(argc, argv, "rS")) != -1) {
1934 flags |= ZFS_ITER_RECURSE;
1941 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1950 /* check number of arguments */
1952 (void) fprintf(stderr, gettext("missing property argument\n"));
1956 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1964 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1965 if (zfs_prop_readonly(prop)) {
1966 (void) fprintf(stderr, gettext(
1967 "%s property is read-only\n"),
1971 if (!zfs_prop_inheritable(prop) && !received) {
1972 (void) fprintf(stderr, gettext("'%s' property cannot "
1973 "be inherited\n"), propname);
1974 if (prop == ZFS_PROP_QUOTA ||
1975 prop == ZFS_PROP_RESERVATION ||
1976 prop == ZFS_PROP_REFQUOTA ||
1977 prop == ZFS_PROP_REFRESERVATION) {
1978 (void) fprintf(stderr, gettext("use 'zfs set "
1979 "%s=none' to clear\n"), propname);
1980 (void) fprintf(stderr, gettext("use 'zfs "
1981 "inherit -S %s' to revert to received "
1982 "value\n"), propname);
1986 if (received && (prop == ZFS_PROP_VOLSIZE ||
1987 prop == ZFS_PROP_VERSION)) {
1988 (void) fprintf(stderr, gettext("'%s' property cannot "
1989 "be reverted to a received value\n"), propname);
1992 } else if (!zfs_prop_user(propname)) {
1993 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1998 cb.cb_propname = propname;
1999 cb.cb_received = received;
2001 if (flags & ZFS_ITER_RECURSE) {
2002 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2003 NULL, NULL, 0, inherit_recurse_cb, &cb);
2005 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2006 NULL, NULL, 0, inherit_cb, &cb);
2012 typedef struct upgrade_cbdata {
2013 uint64_t cb_numupgraded;
2014 uint64_t cb_numsamegraded;
2015 uint64_t cb_numfailed;
2016 uint64_t cb_version;
2018 boolean_t cb_foundone;
2019 char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
2023 same_pool(zfs_handle_t *zhp, const char *name)
2025 int len1 = strcspn(name, "/@");
2026 const char *zhname = zfs_get_name(zhp);
2027 int len2 = strcspn(zhname, "/@");
2031 return (strncmp(name, zhname, len1) == 0);
2035 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2037 upgrade_cbdata_t *cb = data;
2038 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2040 /* list if it's old/new */
2041 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2042 (cb->cb_newer && version > ZPL_VERSION)) {
2045 str = gettext("The following filesystems are "
2046 "formatted using a newer software version and\n"
2047 "cannot be accessed on the current system.\n\n");
2049 str = gettext("The following filesystems are "
2050 "out of date, and can be upgraded. After being\n"
2051 "upgraded, these filesystems (and any 'zfs send' "
2052 "streams generated from\n"
2053 "subsequent snapshots) will no longer be "
2054 "accessible by older software versions.\n\n");
2057 if (!cb->cb_foundone) {
2059 (void) printf(gettext("VER FILESYSTEM\n"));
2060 (void) printf(gettext("--- ------------\n"));
2061 cb->cb_foundone = B_TRUE;
2064 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2071 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2073 upgrade_cbdata_t *cb = data;
2074 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2075 int needed_spa_version;
2078 if (zfs_spa_version(zhp, &spa_version) < 0)
2081 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2083 if (needed_spa_version < 0)
2086 if (spa_version < needed_spa_version) {
2088 (void) printf(gettext("%s: can not be "
2089 "upgraded; the pool version needs to first "
2090 "be upgraded\nto version %d\n\n"),
2091 zfs_get_name(zhp), needed_spa_version);
2097 if (version < cb->cb_version) {
2099 (void) snprintf(verstr, sizeof (verstr),
2100 "%llu", cb->cb_version);
2101 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2103 * If they did "zfs upgrade -a", then we could
2104 * be doing ioctls to different pools. We need
2105 * to log this history once to each pool, and bypass
2106 * the normal history logging that happens in main().
2108 (void) zpool_log_history(g_zfs, history_str);
2109 log_history = B_FALSE;
2111 if (zfs_prop_set(zhp, "version", verstr) == 0)
2112 cb->cb_numupgraded++;
2115 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2116 } else if (version > cb->cb_version) {
2117 /* can't downgrade */
2118 (void) printf(gettext("%s: can not be downgraded; "
2119 "it is already at version %u\n"),
2120 zfs_get_name(zhp), version);
2123 cb->cb_numsamegraded++;
2131 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2134 zfs_do_upgrade(int argc, char **argv)
2136 boolean_t all = B_FALSE;
2137 boolean_t showversions = B_FALSE;
2139 upgrade_cbdata_t cb = { 0 };
2141 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2144 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2147 flags |= ZFS_ITER_RECURSE;
2150 showversions = B_TRUE;
2153 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2154 optarg, &cb.cb_version) != 0) {
2155 (void) fprintf(stderr,
2156 gettext("invalid version %s\n"), optarg);
2165 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2174 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2176 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2177 cb.cb_version || argc))
2179 if ((all || argc) && (showversions))
2185 /* Show info on available versions. */
2186 (void) printf(gettext("The following filesystem versions are "
2188 (void) printf(gettext("VER DESCRIPTION\n"));
2189 (void) printf("--- -----------------------------------------"
2190 "---------------\n");
2191 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2192 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2193 (void) printf(gettext(" 3 Case insensitive and filesystem "
2194 "user identifier (FUID)\n"));
2195 (void) printf(gettext(" 4 userquota, groupquota "
2197 (void) printf(gettext(" 5 System attributes\n"));
2198 (void) printf(gettext("\nFor more information on a particular "
2199 "version, including supported releases,\n"));
2200 (void) printf("see the ZFS Administration Guide.\n\n");
2202 } else if (argc || all) {
2203 /* Upgrade filesystems */
2204 if (cb.cb_version == 0)
2205 cb.cb_version = ZPL_VERSION;
2206 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2207 NULL, NULL, 0, upgrade_set_callback, &cb);
2208 (void) printf(gettext("%llu filesystems upgraded\n"),
2210 if (cb.cb_numsamegraded) {
2211 (void) printf(gettext("%llu filesystems already at "
2213 cb.cb_numsamegraded);
2215 if (cb.cb_numfailed != 0)
2218 /* List old-version filesystems */
2220 (void) printf(gettext("This system is currently running "
2221 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2223 flags |= ZFS_ITER_RECURSE;
2224 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2225 NULL, NULL, 0, upgrade_list_callback, &cb);
2227 found = cb.cb_foundone;
2228 cb.cb_foundone = B_FALSE;
2229 cb.cb_newer = B_TRUE;
2231 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2232 NULL, NULL, 0, upgrade_list_callback, &cb);
2234 if (!cb.cb_foundone && !found) {
2235 (void) printf(gettext("All filesystems are "
2236 "formatted with the current version.\n"));
2244 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2245 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2246 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2247 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2249 * -H Scripted mode; elide headers and separate columns by tabs.
2250 * -i Translate SID to POSIX ID.
2251 * -n Print numeric ID instead of user/group name.
2252 * -o Control which fields to display.
2253 * -p Use exact (parsable) numeric output.
2254 * -s Specify sort columns, descending order.
2255 * -S Specify sort columns, ascending order.
2256 * -t Control which object types to display.
2258 * Displays space consumed by, and quotas on, each user in the specified
2259 * filesystem or snapshot.
2262 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2263 enum us_field_types {
2269 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2270 static char *us_field_names[] = { "type", "name", "used", "quota" };
2271 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2273 #define USTYPE_PSX_GRP (1 << 0)
2274 #define USTYPE_PSX_USR (1 << 1)
2275 #define USTYPE_SMB_GRP (1 << 2)
2276 #define USTYPE_SMB_USR (1 << 3)
2277 #define USTYPE_ALL \
2278 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2280 static int us_type_bits[] = {
2287 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2290 typedef struct us_node {
2292 uu_avl_node_t usn_avlnode;
2293 uu_list_node_t usn_listnode;
2296 typedef struct us_cbdata {
2298 uu_avl_pool_t *cb_avl_pool;
2300 boolean_t cb_numname;
2301 boolean_t cb_nicenum;
2302 boolean_t cb_sid2posix;
2303 zfs_userquota_prop_t cb_prop;
2304 zfs_sort_column_t *cb_sortcol;
2305 size_t cb_width[USFIELD_LAST];
2308 static boolean_t us_populated = B_FALSE;
2311 zfs_sort_column_t *si_sortcol;
2312 boolean_t si_numname;
2316 us_field_index(char *field)
2320 for (i = 0; i < USFIELD_LAST; i++) {
2321 if (strcmp(field, us_field_names[i]) == 0)
2329 us_compare(const void *larg, const void *rarg, void *unused)
2331 const us_node_t *l = larg;
2332 const us_node_t *r = rarg;
2333 us_sort_info_t *si = (us_sort_info_t *)unused;
2334 zfs_sort_column_t *sortcol = si->si_sortcol;
2335 boolean_t numname = si->si_numname;
2336 nvlist_t *lnvl = l->usn_nvl;
2337 nvlist_t *rnvl = r->usn_nvl;
2341 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2348 zfs_prop_t prop = sortcol->sc_prop;
2349 const char *propname = NULL;
2350 boolean_t reverse = sortcol->sc_reverse;
2355 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2356 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2358 rc = (rv32 < lv32) ? 1 : -1;
2363 (void) nvlist_lookup_uint64(lnvl, propname,
2365 (void) nvlist_lookup_uint64(rnvl, propname,
2368 rc = (rv64 < lv64) ? 1 : -1;
2370 (void) nvlist_lookup_string(lnvl, propname,
2372 (void) nvlist_lookup_string(rnvl, propname,
2374 rc = strcmp(lvstr, rvstr);
2378 case ZFS_PROP_QUOTA:
2381 if (prop == ZFS_PROP_USED)
2385 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2386 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2388 rc = (rv64 < lv64) ? 1 : -1;
2397 return (reverse ? 1 : -1);
2399 return (reverse ? -1 : 1);
2404 * If entries still seem to be the same, check if they are of the same
2405 * type (smbentity is added only if we are doing SID to POSIX ID
2406 * translation where we can have duplicate type/name combinations).
2408 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2409 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2411 return (lvb < rvb ? -1 : 1);
2416 static inline const char *
2417 us_type2str(unsigned field_type)
2419 switch (field_type) {
2420 case USTYPE_PSX_USR:
2421 return ("POSIX User");
2422 case USTYPE_PSX_GRP:
2423 return ("POSIX Group");
2424 case USTYPE_SMB_USR:
2425 return ("SMB User");
2426 case USTYPE_SMB_GRP:
2427 return ("SMB Group");
2429 return ("Undefined");
2434 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2436 us_cbdata_t *cb = (us_cbdata_t *)arg;
2437 zfs_userquota_prop_t prop = cb->cb_prop;
2442 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2443 uu_avl_t *avl = cb->cb_avl;
2447 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2449 const char *typestr;
2453 int typeidx, nameidx, sizeidx;
2454 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2455 boolean_t smbentity = B_FALSE;
2457 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2459 node = safe_malloc(sizeof (us_node_t));
2460 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2461 node->usn_nvl = props;
2463 if (domain != NULL && domain[0] != '\0') {
2465 char sid[MAXNAMELEN + 32];
2469 int flag = IDMAP_REQ_FLG_USE_CACHE;
2474 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2476 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2477 type = USTYPE_SMB_GRP;
2479 err = sid_to_id(sid, B_FALSE, &id);
2482 type = USTYPE_SMB_USR;
2484 err = sid_to_id(sid, B_TRUE, &id);
2491 if (!cb->cb_sid2posix) {
2492 if (type == USTYPE_SMB_USR) {
2493 (void) idmap_getwinnamebyuid(rid, flag,
2496 (void) idmap_getwinnamebygid(rid, flag,
2506 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2508 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2509 type = USTYPE_PSX_GRP;
2510 if (!cb->cb_numname) {
2513 if ((g = getgrgid(rid)) != NULL)
2517 type = USTYPE_PSX_USR;
2518 if (!cb->cb_numname) {
2521 if ((p = getpwuid(rid)) != NULL)
2528 * Make sure that the type/name combination is unique when doing
2529 * SID to POSIX ID translation (hence changing the type from SMB to
2532 if (cb->cb_sid2posix &&
2533 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2536 /* Calculate/update width of TYPE field */
2537 typestr = us_type2str(type);
2538 typelen = strlen(gettext(typestr));
2539 typeidx = us_field_index("type");
2540 if (typelen > cb->cb_width[typeidx])
2541 cb->cb_width[typeidx] = typelen;
2542 if (nvlist_add_uint32(props, "type", type) != 0)
2545 /* Calculate/update width of NAME field */
2546 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2547 if (nvlist_add_uint64(props, "name", rid) != 0)
2549 namelen = snprintf(NULL, 0, "%u", rid);
2551 if (nvlist_add_string(props, "name", name) != 0)
2553 namelen = strlen(name);
2555 nameidx = us_field_index("name");
2556 if (namelen > cb->cb_width[nameidx])
2557 cb->cb_width[nameidx] = namelen;
2560 * Check if this type/name combination is in the list and update it;
2561 * otherwise add new node to the list.
2563 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2564 uu_avl_insert(avl, node, idx);
2569 props = node->usn_nvl;
2572 /* Calculate/update width of USED/QUOTA fields */
2574 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2576 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2577 sizelen = strlen(sizebuf);
2578 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2580 if (!nvlist_exists(props, "quota"))
2581 (void) nvlist_add_uint64(props, "quota", 0);
2584 if (!nvlist_exists(props, "used"))
2585 (void) nvlist_add_uint64(props, "used", 0);
2587 sizeidx = us_field_index(propname);
2588 if (sizelen > cb->cb_width[sizeidx])
2589 cb->cb_width[sizeidx] = sizelen;
2591 if (nvlist_add_uint64(props, propname, space) != 0)
2598 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2599 size_t *width, us_node_t *node)
2601 nvlist_t *nvl = node->usn_nvl;
2602 char valstr[MAXNAMELEN];
2603 boolean_t first = B_TRUE;
2609 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2610 if (!(ustype & types))
2613 while ((field = fields[cfield]) != USFIELD_LAST) {
2614 nvpair_t *nvp = NULL;
2618 char *strval = NULL;
2620 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2621 if (strcmp(nvpair_name(nvp),
2622 us_field_names[field]) == 0)
2626 type = nvpair_type(nvp);
2628 case DATA_TYPE_UINT32:
2629 (void) nvpair_value_uint32(nvp, &val32);
2631 case DATA_TYPE_UINT64:
2632 (void) nvpair_value_uint64(nvp, &val64);
2634 case DATA_TYPE_STRING:
2635 (void) nvpair_value_string(nvp, &strval);
2638 (void) fprintf(stderr, "invalid data type\n");
2643 strval = (char *)us_type2str(val32);
2646 if (type == DATA_TYPE_UINT64) {
2647 (void) sprintf(valstr, "%llu", val64);
2653 if (type == DATA_TYPE_UINT64) {
2655 (void) sprintf(valstr, "%llu", val64);
2657 zfs_nicenum(val64, valstr,
2660 if (field == USFIELD_QUOTA &&
2661 strcmp(valstr, "0") == 0)
2671 (void) printf("\t");
2676 (void) printf("%s", strval);
2677 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2678 (void) printf("%-*s", width[field], strval);
2680 (void) printf("%*s", width[field], strval);
2686 (void) printf("\n");
2690 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2691 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2699 boolean_t first = B_TRUE;
2701 while ((field = fields[cfield]) != USFIELD_LAST) {
2702 col = gettext(us_field_hdr[field]);
2703 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2704 (void) printf(first ? "%-*s" : " %-*s",
2707 (void) printf(first ? "%*s" : " %*s",
2713 (void) printf("\n");
2716 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2717 print_us_node(scripted, parsable, fields, types, width, node);
2719 nvlist_free(node->usn_nvl);
2724 zfs_do_userspace(int argc, char **argv)
2727 zfs_userquota_prop_t p;
2729 uu_avl_pool_t *avl_pool;
2731 uu_avl_walk_t *walk;
2733 char deffields[] = "type,name,used,quota";
2734 char *ofield = NULL;
2735 char *tfield = NULL;
2739 boolean_t scripted = B_FALSE;
2740 boolean_t prtnum = B_FALSE;
2741 boolean_t parsable = B_FALSE;
2742 boolean_t sid2posix = B_FALSE;
2745 zfs_sort_column_t *sortcol = NULL;
2746 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2750 uu_list_pool_t *listpool;
2752 uu_avl_index_t idx = 0;
2753 uu_list_index_t idx2 = 0;
2758 if (strcmp(argv[0], "groupspace") == 0)
2759 /* Toggle default group types */
2760 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2762 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2778 if (zfs_add_sort_column(&sortcol, optarg,
2779 c == 's' ? B_FALSE : B_TRUE) != 0) {
2780 (void) fprintf(stderr,
2781 gettext("invalid field '%s'\n"), optarg);
2792 (void) fprintf(stderr, gettext("missing argument for "
2793 "'%c' option\n"), optopt);
2797 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2807 (void) fprintf(stderr, gettext("missing dataset name\n"));
2811 (void) fprintf(stderr, gettext("too many arguments\n"));
2815 /* Use default output fields if not specified using -o */
2819 if ((delim = strchr(ofield, ',')) != NULL)
2821 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2822 (void) fprintf(stderr, gettext("invalid type '%s' "
2823 "for -o option\n"), ofield);
2828 } while (delim != NULL);
2829 fields[cfield] = USFIELD_LAST;
2831 /* Override output types (-t option) */
2832 if (tfield != NULL) {
2836 boolean_t found = B_FALSE;
2838 if ((delim = strchr(tfield, ',')) != NULL)
2840 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2842 if (strcmp(tfield, us_type_names[i]) == 0) {
2844 types |= us_type_bits[i];
2849 (void) fprintf(stderr, gettext("invalid type "
2850 "'%s' for -t option\n"), tfield);
2855 } while (delim != NULL);
2858 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2861 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2862 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2864 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2867 /* Always add default sorting columns */
2868 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2869 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2871 cb.cb_sortcol = sortcol;
2872 cb.cb_numname = prtnum;
2873 cb.cb_nicenum = !parsable;
2874 cb.cb_avl_pool = avl_pool;
2875 cb.cb_avl = avl_tree;
2876 cb.cb_sid2posix = sid2posix;
2878 for (i = 0; i < USFIELD_LAST; i++)
2879 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2881 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2882 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2883 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2884 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2885 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2888 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2893 if ((node = uu_avl_first(avl_tree)) == NULL)
2896 us_populated = B_TRUE;
2898 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2899 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2900 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2901 uu_list_node_init(node, &node->usn_listnode, listpool);
2903 while (node != NULL) {
2905 node = uu_avl_next(avl_tree, node);
2906 uu_avl_remove(avl_tree, rmnode);
2907 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2908 uu_list_insert(list, rmnode, idx2);
2911 for (node = uu_list_first(list); node != NULL;
2912 node = uu_list_next(list, node)) {
2913 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2915 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2916 uu_avl_insert(avl_tree, node, idx);
2919 uu_list_destroy(list);
2920 uu_list_pool_destroy(listpool);
2922 /* Print and free node nvlist memory */
2923 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2926 zfs_free_sort_columns(sortcol);
2928 /* Clean up the AVL tree */
2929 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2932 while ((node = uu_avl_walk_next(walk)) != NULL) {
2933 uu_avl_remove(cb.cb_avl, node);
2937 uu_avl_walk_end(walk);
2938 uu_avl_destroy(avl_tree);
2939 uu_avl_pool_destroy(avl_pool);
2945 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2946 * [-t type[,...]] [filesystem|volume|snapshot] ...
2948 * -H Scripted mode; elide headers and separate columns by tabs.
2949 * -p Display values in parsable (literal) format.
2950 * -r Recurse over all children.
2951 * -d Limit recursion by depth.
2952 * -o Control which fields to display.
2953 * -s Specify sort columns, descending order.
2954 * -S Specify sort columns, ascending order.
2955 * -t Control which object types to display.
2957 * When given no arguments, list all filesystems in the system.
2958 * Otherwise, list the specified datasets, optionally recursing down them if
2959 * '-r' is specified.
2961 typedef struct list_cbdata {
2963 boolean_t cb_literal;
2964 boolean_t cb_scripted;
2965 zprop_list_t *cb_proplist;
2969 * Given a list of columns to display, output appropriate headers for each one.
2972 print_header(list_cbdata_t *cb)
2974 zprop_list_t *pl = cb->cb_proplist;
2975 char headerbuf[ZFS_MAXPROPLEN];
2978 boolean_t first = B_TRUE;
2979 boolean_t right_justify;
2981 for (; pl != NULL; pl = pl->pl_next) {
2988 right_justify = B_FALSE;
2989 if (pl->pl_prop != ZPROP_INVAL) {
2990 header = zfs_prop_column_name(pl->pl_prop);
2991 right_justify = zfs_prop_align_right(pl->pl_prop);
2993 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2994 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2995 headerbuf[i] = '\0';
2999 if (pl->pl_next == NULL && !right_justify)
3000 (void) printf("%s", header);
3001 else if (right_justify)
3002 (void) printf("%*s", pl->pl_width, header);
3004 (void) printf("%-*s", pl->pl_width, header);
3007 (void) printf("\n");
3011 * Given a dataset and a list of fields, print out all the properties according
3012 * to the described layout.
3015 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3017 zprop_list_t *pl = cb->cb_proplist;
3018 boolean_t first = B_TRUE;
3019 char property[ZFS_MAXPROPLEN];
3020 nvlist_t *userprops = zfs_get_user_props(zhp);
3023 boolean_t right_justify;
3025 for (; pl != NULL; pl = pl->pl_next) {
3027 if (cb->cb_scripted)
3028 (void) printf("\t");
3035 if (pl->pl_prop == ZFS_PROP_NAME) {
3036 (void) strlcpy(property, zfs_get_name(zhp),
3039 right_justify = zfs_prop_align_right(pl->pl_prop);
3040 } else if (pl->pl_prop != ZPROP_INVAL) {
3041 if (zfs_prop_get(zhp, pl->pl_prop, property,
3042 sizeof (property), NULL, NULL, 0,
3043 cb->cb_literal) != 0)
3047 right_justify = zfs_prop_align_right(pl->pl_prop);
3048 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3049 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3050 property, sizeof (property), cb->cb_literal) != 0)
3054 right_justify = B_TRUE;
3055 } else if (zfs_prop_written(pl->pl_user_prop)) {
3056 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3057 property, sizeof (property), cb->cb_literal) != 0)
3061 right_justify = B_TRUE;
3063 if (nvlist_lookup_nvlist(userprops,
3064 pl->pl_user_prop, &propval) != 0)
3067 verify(nvlist_lookup_string(propval,
3068 ZPROP_VALUE, &propstr) == 0);
3069 right_justify = B_FALSE;
3073 * If this is being called in scripted mode, or if this is the
3074 * last column and it is left-justified, don't include a width
3077 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3078 (void) printf("%s", propstr);
3079 else if (right_justify)
3080 (void) printf("%*s", pl->pl_width, propstr);
3082 (void) printf("%-*s", pl->pl_width, propstr);
3085 (void) printf("\n");
3089 * Generic callback function to list a dataset or snapshot.
3092 list_callback(zfs_handle_t *zhp, void *data)
3094 list_cbdata_t *cbp = data;
3096 if (cbp->cb_first) {
3097 if (!cbp->cb_scripted)
3099 cbp->cb_first = B_FALSE;
3102 print_dataset(zhp, cbp);
3108 zfs_do_list(int argc, char **argv)
3111 static char default_fields[] =
3112 "name,used,available,referenced,mountpoint";
3113 int types = ZFS_TYPE_DATASET;
3114 boolean_t types_specified = B_FALSE;
3115 char *fields = NULL;
3116 list_cbdata_t cb = { 0 };
3120 zfs_sort_column_t *sortcol = NULL;
3121 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3124 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3130 cb.cb_literal = B_TRUE;
3131 flags |= ZFS_ITER_LITERAL_PROPS;
3134 limit = parse_depth(optarg, &flags);
3137 flags |= ZFS_ITER_RECURSE;
3140 cb.cb_scripted = B_TRUE;
3143 if (zfs_add_sort_column(&sortcol, optarg,
3145 (void) fprintf(stderr,
3146 gettext("invalid property '%s'\n"), optarg);
3151 if (zfs_add_sort_column(&sortcol, optarg,
3153 (void) fprintf(stderr,
3154 gettext("invalid property '%s'\n"), optarg);
3160 types_specified = B_TRUE;
3161 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3162 while (*optarg != '\0') {
3163 static char *type_subopts[] = { "filesystem",
3164 "volume", "snapshot", "snap", "bookmark",
3167 switch (getsubopt(&optarg, type_subopts,
3170 types |= ZFS_TYPE_FILESYSTEM;
3173 types |= ZFS_TYPE_VOLUME;
3177 types |= ZFS_TYPE_SNAPSHOT;
3180 types |= ZFS_TYPE_BOOKMARK;
3183 types = ZFS_TYPE_DATASET |
3187 (void) fprintf(stderr,
3188 gettext("invalid type '%s'\n"),
3195 (void) fprintf(stderr, gettext("missing argument for "
3196 "'%c' option\n"), optopt);
3200 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3210 fields = default_fields;
3213 * If we are only going to list snapshot names and sort by name,
3214 * then we can use faster version.
3216 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3217 flags |= ZFS_ITER_SIMPLE;
3220 * If "-o space" and no types were specified, don't display snapshots.
3222 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3223 types &= ~ZFS_TYPE_SNAPSHOT;
3226 * If the user specifies '-o all', the zprop_get_list() doesn't
3227 * normally include the name of the dataset. For 'zfs list', we always
3228 * want this property to be first.
3230 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3234 cb.cb_first = B_TRUE;
3236 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3237 limit, list_callback, &cb);
3239 zprop_free_list(cb.cb_proplist);
3240 zfs_free_sort_columns(sortcol);
3242 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3243 (void) printf(gettext("no datasets available\n"));
3249 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3250 * zfs rename [-f] -p <fs | vol> <fs | vol>
3251 * zfs rename -r <snap> <snap>
3252 * zfs rename -u [-p] <fs> <fs>
3254 * Renames the given dataset to another of the same type.
3256 * The '-p' flag creates all the non-existing ancestors of the target first.
3260 zfs_do_rename(int argc, char **argv)
3263 renameflags_t flags = { 0 };
3267 boolean_t parents = B_FALSE;
3268 char *snapshot = NULL;
3271 while ((c = getopt(argc, argv, "fpru")) != -1) {
3277 flags.recurse = B_TRUE;
3280 flags.nounmount = B_TRUE;
3283 flags.forceunmount = B_TRUE;
3287 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3296 /* check number of arguments */
3298 (void) fprintf(stderr, gettext("missing source dataset "
3303 (void) fprintf(stderr, gettext("missing target dataset "
3308 (void) fprintf(stderr, gettext("too many arguments\n"));
3312 if (flags.recurse && parents) {
3313 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3318 if (flags.recurse && strchr(argv[0], '@') == 0) {
3319 (void) fprintf(stderr, gettext("source dataset for recursive "
3320 "rename must be a snapshot\n"));
3324 if (flags.nounmount && parents) {
3325 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3330 if (flags.nounmount)
3331 types = ZFS_TYPE_FILESYSTEM;
3333 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3335 types = ZFS_TYPE_DATASET;
3337 if (flags.recurse) {
3339 * When we do recursive rename we are fine when the given
3340 * snapshot for the given dataset doesn't exist - it can
3341 * still exists below.
3344 snapshot = strchr(argv[0], '@');
3345 assert(snapshot != NULL);
3350 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3353 /* If we were asked and the name looks good, try to create ancestors. */
3354 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3355 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3360 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3369 * Promotes the given clone fs to be the parent
3373 zfs_do_promote(int argc, char **argv)
3379 if (argc > 1 && argv[1][0] == '-') {
3380 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3385 /* check number of arguments */
3387 (void) fprintf(stderr, gettext("missing clone filesystem"
3392 (void) fprintf(stderr, gettext("too many arguments\n"));
3396 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3400 ret = (zfs_promote(zhp) != 0);
3408 * zfs rollback [-rRf] <snapshot>
3410 * -r Delete any intervening snapshots before doing rollback
3411 * -R Delete any snapshots and their clones
3412 * -f ignored for backwards compatability
3414 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3415 * since then and making it the active dataset. If more recent snapshots exist,
3416 * the command will complain unless the '-r' flag is given.
3418 typedef struct rollback_cbdata {
3424 boolean_t cb_recurse;
3425 } rollback_cbdata_t;
3428 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3430 rollback_cbdata_t *cbp = data;
3432 if (cbp->cb_first && cbp->cb_recurse) {
3433 (void) fprintf(stderr, gettext("cannot rollback to "
3434 "'%s': clones of previous snapshots exist\n"),
3436 (void) fprintf(stderr, gettext("use '-R' to "
3437 "force deletion of the following clones and "
3443 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3450 * Report any snapshots more recent than the one specified. Used when '-r' is
3451 * not specified. We reuse this same callback for the snapshot dependents - if
3452 * 'cb_dependent' is set, then this is a dependent and we should report it
3453 * without checking the transaction group.
3456 rollback_check(zfs_handle_t *zhp, void *data)
3458 rollback_cbdata_t *cbp = data;
3460 if (cbp->cb_doclones) {
3465 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3466 if (cbp->cb_first && !cbp->cb_recurse) {
3467 (void) fprintf(stderr, gettext("cannot "
3468 "rollback to '%s': more recent snapshots "
3469 "or bookmarks exist\n"),
3471 (void) fprintf(stderr, gettext("use '-r' to "
3472 "force deletion of the following "
3473 "snapshots and bookmarks:\n"));
3478 if (cbp->cb_recurse) {
3479 if (zfs_iter_dependents(zhp, B_TRUE,
3480 rollback_check_dependent, cbp) != 0) {
3485 (void) fprintf(stderr, "%s\n",
3494 zfs_do_rollback(int argc, char **argv)
3498 boolean_t force = B_FALSE;
3499 rollback_cbdata_t cb = { 0 };
3500 zfs_handle_t *zhp, *snap;
3501 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3505 while ((c = getopt(argc, argv, "rRf")) != -1) {
3518 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3527 /* check number of arguments */
3529 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3533 (void) fprintf(stderr, gettext("too many arguments\n"));
3537 /* open the snapshot */
3538 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3541 /* open the parent dataset */
3542 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3543 verify((delim = strrchr(parentname, '@')) != NULL);
3545 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3551 * Check for more recent snapshots and/or clones based on the presence
3554 cb.cb_target = argv[0];
3555 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3556 cb.cb_first = B_TRUE;
3558 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3560 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3563 if ((ret = cb.cb_error) != 0)
3567 * Rollback parent to the given snapshot.
3569 ret = zfs_rollback(zhp, snap, force);
3582 * zfs set property=value ... { fs | snap | vol } ...
3584 * Sets the given properties for all datasets specified on the command line.
3588 set_callback(zfs_handle_t *zhp, void *data)
3590 nvlist_t *props = data;
3592 if (zfs_prop_set_list(zhp, props) != 0) {
3593 switch (libzfs_errno(g_zfs)) {
3594 case EZFS_MOUNTFAILED:
3595 (void) fprintf(stderr, gettext("property may be set "
3596 "but unable to remount filesystem\n"));
3598 case EZFS_SHARENFSFAILED:
3599 (void) fprintf(stderr, gettext("property may be set "
3600 "but unable to reshare filesystem\n"));
3609 zfs_do_set(int argc, char **argv)
3611 nvlist_t *props = NULL;
3612 int ds_start = -1; /* argv idx of first dataset arg */
3615 /* check for options */
3616 if (argc > 1 && argv[1][0] == '-') {
3617 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3622 /* check number of arguments */
3624 (void) fprintf(stderr, gettext("missing arguments\n"));
3628 if (strchr(argv[1], '=') == NULL) {
3629 (void) fprintf(stderr, gettext("missing property=value "
3632 (void) fprintf(stderr, gettext("missing dataset "
3638 /* validate argument order: prop=val args followed by dataset args */
3639 for (int i = 1; i < argc; i++) {
3640 if (strchr(argv[i], '=') != NULL) {
3642 /* out-of-order prop=val argument */
3643 (void) fprintf(stderr, gettext("invalid "
3644 "argument order\n"), i);
3647 } else if (ds_start < 0) {
3652 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3656 /* Populate a list of property settings */
3657 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3659 for (int i = 1; i < ds_start; i++) {
3660 if ((ret = parseprop(props, argv[i])) != 0)
3664 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3665 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3672 typedef struct snap_cbdata {
3674 boolean_t sd_recursive;
3675 const char *sd_snapname;
3679 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3681 snap_cbdata_t *sd = arg;
3686 if (sd->sd_recursive &&
3687 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3692 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3695 fnvlist_add_boolean(sd->sd_nvl, name);
3698 if (sd->sd_recursive)
3699 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3705 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3707 * Creates a snapshot with the given name. While functionally equivalent to
3708 * 'zfs create', it is a separate command to differentiate intent.
3711 zfs_do_snapshot(int argc, char **argv)
3716 snap_cbdata_t sd = { 0 };
3717 boolean_t multiple_snaps = B_FALSE;
3719 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3721 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3725 while ((c = getopt(argc, argv, "ro:")) != -1) {
3728 if (parseprop(props, optarg) != 0)
3732 sd.sd_recursive = B_TRUE;
3733 multiple_snaps = B_TRUE;
3736 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3745 /* check number of arguments */
3747 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3752 multiple_snaps = B_TRUE;
3753 for (; argc > 0; argc--, argv++) {
3757 atp = strchr(argv[0], '@');
3761 sd.sd_snapname = atp + 1;
3762 zhp = zfs_open(g_zfs, argv[0],
3763 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3766 if (zfs_snapshot_cb(zhp, &sd) != 0)
3770 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3771 nvlist_free(sd.sd_nvl);
3773 if (ret != 0 && multiple_snaps)
3774 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3778 nvlist_free(sd.sd_nvl);
3785 * Send a backup stream to stdout.
3788 zfs_do_send(int argc, char **argv)
3790 char *fromname = NULL;
3791 char *toname = NULL;
3792 char *resume_token = NULL;
3795 sendflags_t flags = { 0 };
3797 nvlist_t *dbgnv = NULL;
3798 boolean_t extraverbose = B_FALSE;
3800 struct option long_options[] = {
3801 {"replicate", no_argument, NULL, 'R'},
3802 {"props", no_argument, NULL, 'p'},
3803 {"parsable", no_argument, NULL, 'P'},
3804 {"dedup", no_argument, NULL, 'D'},
3805 {"verbose", no_argument, NULL, 'v'},
3806 {"dryrun", no_argument, NULL, 'n'},
3807 {"large-block", no_argument, NULL, 'L'},
3808 {"embed", no_argument, NULL, 'e'},
3809 {"resume", required_argument, NULL, 't'},
3810 {"compressed", no_argument, NULL, 'c'},
3815 while ((c = getopt_long(argc, argv, ":i:I:RbDpvnPLet:c", long_options,
3827 flags.doall = B_TRUE;
3830 flags.replicate = B_TRUE;
3833 flags.props = B_TRUE;
3836 flags.parsable = B_TRUE;
3837 flags.verbose = B_TRUE;
3841 extraverbose = B_TRUE;
3842 flags.verbose = B_TRUE;
3843 flags.progress = B_TRUE;
3846 flags.dedup = B_TRUE;
3849 flags.dryrun = B_TRUE;
3852 flags.largeblock = B_TRUE;
3855 flags.embed_data = B_TRUE;
3858 resume_token = optarg;
3861 flags.compress = B_TRUE;
3864 (void) fprintf(stderr, gettext("missing argument for "
3865 "'%c' option\n"), optopt);
3871 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3880 if (resume_token != NULL) {
3881 if (fromname != NULL || flags.replicate || flags.props ||
3883 (void) fprintf(stderr,
3884 gettext("invalid flags combined with -t\n"));
3888 (void) fprintf(stderr, gettext("no additional "
3889 "arguments are permitted with -t\n"));
3894 (void) fprintf(stderr,
3895 gettext("missing snapshot argument\n"));
3899 (void) fprintf(stderr, gettext("too many arguments\n"));
3904 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3905 (void) fprintf(stderr,
3906 gettext("Error: Stream can not be written to a terminal.\n"
3907 "You must redirect standard output.\n"));
3911 if (resume_token != NULL) {
3912 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3917 * Special case sending a filesystem, or from a bookmark.
3919 if (strchr(argv[0], '@') == NULL ||
3920 (fromname && strchr(fromname, '#') != NULL)) {
3921 char frombuf[ZFS_MAX_DATASET_NAME_LEN];
3922 enum lzc_send_flags lzc_flags = 0;
3924 if (flags.replicate || flags.doall || flags.props ||
3925 flags.dedup || flags.dryrun || flags.verbose ||
3927 (void) fprintf(stderr,
3929 "Unsupported flag with filesystem or bookmark.\n"));
3933 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3937 if (flags.largeblock)
3938 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3939 if (flags.embed_data)
3940 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3942 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
3944 if (fromname != NULL &&
3945 (fromname[0] == '#' || fromname[0] == '@')) {
3947 * Incremental source name begins with # or @.
3948 * Default to same fs as target.
3950 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3951 cp = strchr(frombuf, '@');
3954 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3957 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3962 cp = strchr(argv[0], '@');
3965 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3970 * If they specified the full path to the snapshot, chop off
3971 * everything except the short name of the snapshot, but special
3972 * case if they specify the origin.
3974 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3975 char origin[ZFS_MAX_DATASET_NAME_LEN];
3978 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3979 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3981 if (strcmp(origin, fromname) == 0) {
3983 flags.fromorigin = B_TRUE;
3986 if (cp != fromname && strcmp(argv[0], fromname)) {
3987 (void) fprintf(stderr,
3988 gettext("incremental source must be "
3989 "in same filesystem\n"));
3993 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3994 (void) fprintf(stderr,
3995 gettext("invalid incremental source\n"));
4001 if (flags.replicate && fromname == NULL)
4002 flags.doall = B_TRUE;
4004 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
4005 extraverbose ? &dbgnv : NULL);
4007 if (extraverbose && dbgnv != NULL) {
4009 * dump_nvlist prints to stdout, but that's been
4010 * redirected to a file. Make it print to stderr
4013 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
4014 dump_nvlist(dbgnv, 0);
4023 * Restore a backup stream from stdin.
4026 zfs_do_receive(int argc, char **argv)
4029 recvflags_t flags = { 0 };
4030 boolean_t abort_resumable = B_FALSE;
4033 nvpair_t *nvp = NULL;
4035 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4039 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
4042 if (parseprop(props, optarg) != 0)
4046 flags.isprefix = B_TRUE;
4049 flags.isprefix = B_TRUE;
4050 flags.istail = B_TRUE;
4053 flags.dryrun = B_TRUE;
4056 flags.nomount = B_TRUE;
4059 flags.verbose = B_TRUE;
4062 flags.resumable = B_TRUE;
4065 flags.force = B_TRUE;
4068 abort_resumable = B_TRUE;
4071 (void) fprintf(stderr, gettext("missing argument for "
4072 "'%c' option\n"), optopt);
4076 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4085 /* check number of arguments */
4087 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4091 (void) fprintf(stderr, gettext("too many arguments\n"));
4095 while ((nvp = nvlist_next_nvpair(props, nvp))) {
4096 if (strcmp(nvpair_name(nvp), "origin") != 0) {
4097 (void) fprintf(stderr, gettext("invalid option"));
4102 if (abort_resumable) {
4103 if (flags.isprefix || flags.istail || flags.dryrun ||
4104 flags.resumable || flags.nomount) {
4105 (void) fprintf(stderr, gettext("invalid option"));
4109 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4110 (void) snprintf(namebuf, sizeof (namebuf),
4111 "%s/%%recv", argv[0]);
4113 if (zfs_dataset_exists(g_zfs, namebuf,
4114 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4115 zfs_handle_t *zhp = zfs_open(g_zfs,
4116 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4119 err = zfs_destroy(zhp, B_FALSE);
4121 zfs_handle_t *zhp = zfs_open(g_zfs,
4122 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4125 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4126 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4127 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4128 (void) fprintf(stderr,
4129 gettext("'%s' does not have any "
4130 "resumable receive state to abort\n"),
4134 err = zfs_destroy(zhp, B_FALSE);
4140 if (isatty(STDIN_FILENO)) {
4141 (void) fprintf(stderr,
4142 gettext("Error: Backup stream can not be read "
4143 "from a terminal.\n"
4144 "You must redirect standard input.\n"));
4147 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4153 * allow/unallow stuff
4155 /* copied from zfs/sys/dsl_deleg.h */
4156 #define ZFS_DELEG_PERM_CREATE "create"
4157 #define ZFS_DELEG_PERM_DESTROY "destroy"
4158 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4159 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4160 #define ZFS_DELEG_PERM_CLONE "clone"
4161 #define ZFS_DELEG_PERM_PROMOTE "promote"
4162 #define ZFS_DELEG_PERM_RENAME "rename"
4163 #define ZFS_DELEG_PERM_MOUNT "mount"
4164 #define ZFS_DELEG_PERM_SHARE "share"
4165 #define ZFS_DELEG_PERM_SEND "send"
4166 #define ZFS_DELEG_PERM_RECEIVE "receive"
4167 #define ZFS_DELEG_PERM_ALLOW "allow"
4168 #define ZFS_DELEG_PERM_USERPROP "userprop"
4169 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4170 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4171 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4172 #define ZFS_DELEG_PERM_USERUSED "userused"
4173 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4174 #define ZFS_DELEG_PERM_HOLD "hold"
4175 #define ZFS_DELEG_PERM_RELEASE "release"
4176 #define ZFS_DELEG_PERM_DIFF "diff"
4177 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4178 #define ZFS_DELEG_PERM_REMAP "remap"
4180 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4182 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4183 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4184 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4185 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4186 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4187 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4188 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4189 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4190 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4191 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4192 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4193 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4194 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4195 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4196 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4197 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4198 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4199 { ZFS_DELEG_PERM_REMAP, ZFS_DELEG_NOTE_REMAP },
4201 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4202 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4203 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4204 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4205 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4206 { NULL, ZFS_DELEG_NOTE_NONE }
4209 /* permission structure */
4210 typedef struct deleg_perm {
4211 zfs_deleg_who_type_t dp_who_type;
4212 const char *dp_name;
4214 boolean_t dp_descend;
4218 typedef struct deleg_perm_node {
4219 deleg_perm_t dpn_perm;
4221 uu_avl_node_t dpn_avl_node;
4222 } deleg_perm_node_t;
4224 typedef struct fs_perm fs_perm_t;
4226 /* permissions set */
4227 typedef struct who_perm {
4228 zfs_deleg_who_type_t who_type;
4229 const char *who_name; /* id */
4230 char who_ug_name[256]; /* user/group name */
4231 fs_perm_t *who_fsperm; /* uplink */
4233 uu_avl_t *who_deleg_perm_avl; /* permissions */
4237 typedef struct who_perm_node {
4238 who_perm_t who_perm;
4239 uu_avl_node_t who_avl_node;
4242 typedef struct fs_perm_set fs_perm_set_t;
4243 /* fs permissions */
4245 const char *fsp_name;
4247 uu_avl_t *fsp_sc_avl; /* sets,create */
4248 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4250 fs_perm_set_t *fsp_set; /* uplink */
4254 typedef struct fs_perm_node {
4255 fs_perm_t fspn_fsperm;
4258 uu_list_node_t fspn_list_node;
4261 /* top level structure */
4262 struct fs_perm_set {
4263 uu_list_pool_t *fsps_list_pool;
4264 uu_list_t *fsps_list; /* list of fs_perms */
4266 uu_avl_pool_t *fsps_named_set_avl_pool;
4267 uu_avl_pool_t *fsps_who_perm_avl_pool;
4268 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4271 static inline const char *
4272 deleg_perm_type(zfs_deleg_note_t note)
4278 case ZFS_DELEG_NOTE_GROUPQUOTA:
4279 case ZFS_DELEG_NOTE_GROUPUSED:
4280 case ZFS_DELEG_NOTE_USERPROP:
4281 case ZFS_DELEG_NOTE_USERQUOTA:
4282 case ZFS_DELEG_NOTE_USERUSED:
4284 return (gettext("other"));
4286 return (gettext("subcommand"));
4291 who_type2weight(zfs_deleg_who_type_t who_type)
4295 case ZFS_DELEG_NAMED_SET_SETS:
4296 case ZFS_DELEG_NAMED_SET:
4299 case ZFS_DELEG_CREATE_SETS:
4300 case ZFS_DELEG_CREATE:
4303 case ZFS_DELEG_USER_SETS:
4304 case ZFS_DELEG_USER:
4307 case ZFS_DELEG_GROUP_SETS:
4308 case ZFS_DELEG_GROUP:
4311 case ZFS_DELEG_EVERYONE_SETS:
4312 case ZFS_DELEG_EVERYONE:
4324 who_perm_compare(const void *larg, const void *rarg, void *unused)
4326 const who_perm_node_t *l = larg;
4327 const who_perm_node_t *r = rarg;
4328 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4329 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4330 int lweight = who_type2weight(ltype);
4331 int rweight = who_type2weight(rtype);
4332 int res = lweight - rweight;
4334 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4335 ZFS_MAX_DELEG_NAME-1);
4347 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4349 const deleg_perm_node_t *l = larg;
4350 const deleg_perm_node_t *r = rarg;
4351 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4352 ZFS_MAX_DELEG_NAME-1);
4364 fs_perm_set_init(fs_perm_set_t *fspset)
4366 bzero(fspset, sizeof (fs_perm_set_t));
4368 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4369 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4370 NULL, UU_DEFAULT)) == NULL)
4372 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4373 UU_DEFAULT)) == NULL)
4376 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4377 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4378 who_perm_node_t, who_avl_node), who_perm_compare,
4379 UU_DEFAULT)) == NULL)
4382 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4383 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4384 who_perm_node_t, who_avl_node), who_perm_compare,
4385 UU_DEFAULT)) == NULL)
4388 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4389 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4390 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4395 static inline void fs_perm_fini(fs_perm_t *);
4396 static inline void who_perm_fini(who_perm_t *);
4399 fs_perm_set_fini(fs_perm_set_t *fspset)
4401 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4403 while (node != NULL) {
4404 fs_perm_node_t *next_node =
4405 uu_list_next(fspset->fsps_list, node);
4406 fs_perm_t *fsperm = &node->fspn_fsperm;
4407 fs_perm_fini(fsperm);
4408 uu_list_remove(fspset->fsps_list, node);
4413 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4414 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4415 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4419 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4422 deleg_perm->dp_who_type = type;
4423 deleg_perm->dp_name = name;
4427 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4428 zfs_deleg_who_type_t type, const char *name)
4430 uu_avl_pool_t *pool;
4431 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4433 bzero(who_perm, sizeof (who_perm_t));
4435 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4436 UU_DEFAULT)) == NULL)
4439 who_perm->who_type = type;
4440 who_perm->who_name = name;
4441 who_perm->who_fsperm = fsperm;
4445 who_perm_fini(who_perm_t *who_perm)
4447 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4449 while (node != NULL) {
4450 deleg_perm_node_t *next_node =
4451 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4453 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4458 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4462 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4464 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4465 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4467 bzero(fsperm, sizeof (fs_perm_t));
4469 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4473 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4477 fsperm->fsp_set = fspset;
4478 fsperm->fsp_name = fsname;
4482 fs_perm_fini(fs_perm_t *fsperm)
4484 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4485 while (node != NULL) {
4486 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4488 who_perm_t *who_perm = &node->who_perm;
4489 who_perm_fini(who_perm);
4490 uu_avl_remove(fsperm->fsp_sc_avl, node);
4495 node = uu_avl_first(fsperm->fsp_uge_avl);
4496 while (node != NULL) {
4497 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4499 who_perm_t *who_perm = &node->who_perm;
4500 who_perm_fini(who_perm);
4501 uu_avl_remove(fsperm->fsp_uge_avl, node);
4506 uu_avl_destroy(fsperm->fsp_sc_avl);
4507 uu_avl_destroy(fsperm->fsp_uge_avl);
4511 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4512 zfs_deleg_who_type_t who_type, const char *name, char locality)
4514 uu_avl_index_t idx = 0;
4516 deleg_perm_node_t *found_node = NULL;
4517 deleg_perm_t *deleg_perm = &node->dpn_perm;
4519 deleg_perm_init(deleg_perm, who_type, name);
4521 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4523 uu_avl_insert(avl, node, idx);
4526 deleg_perm = &node->dpn_perm;
4531 case ZFS_DELEG_LOCAL:
4532 deleg_perm->dp_local = B_TRUE;
4534 case ZFS_DELEG_DESCENDENT:
4535 deleg_perm->dp_descend = B_TRUE;
4540 assert(B_FALSE); /* invalid locality */
4545 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4547 nvpair_t *nvp = NULL;
4548 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4549 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4550 zfs_deleg_who_type_t who_type = who_perm->who_type;
4552 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4553 const char *name = nvpair_name(nvp);
4554 data_type_t type = nvpair_type(nvp);
4555 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4556 deleg_perm_node_t *node =
4557 safe_malloc(sizeof (deleg_perm_node_t));
4559 assert(type == DATA_TYPE_BOOLEAN);
4561 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4562 set_deleg_perm_node(avl, node, who_type, name, locality);
4569 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4571 nvpair_t *nvp = NULL;
4572 fs_perm_set_t *fspset = fsperm->fsp_set;
4574 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4575 nvlist_t *nvl2 = NULL;
4576 const char *name = nvpair_name(nvp);
4577 uu_avl_t *avl = NULL;
4578 uu_avl_pool_t *avl_pool = NULL;
4579 zfs_deleg_who_type_t perm_type = name[0];
4580 char perm_locality = name[1];
4581 const char *perm_name = name + 3;
4582 boolean_t is_set = B_TRUE;
4583 who_perm_t *who_perm = NULL;
4585 assert('$' == name[2]);
4587 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4590 switch (perm_type) {
4591 case ZFS_DELEG_CREATE:
4592 case ZFS_DELEG_CREATE_SETS:
4593 case ZFS_DELEG_NAMED_SET:
4594 case ZFS_DELEG_NAMED_SET_SETS:
4595 avl_pool = fspset->fsps_named_set_avl_pool;
4596 avl = fsperm->fsp_sc_avl;
4598 case ZFS_DELEG_USER:
4599 case ZFS_DELEG_USER_SETS:
4600 case ZFS_DELEG_GROUP:
4601 case ZFS_DELEG_GROUP_SETS:
4602 case ZFS_DELEG_EVERYONE:
4603 case ZFS_DELEG_EVERYONE_SETS:
4604 avl_pool = fspset->fsps_who_perm_avl_pool;
4605 avl = fsperm->fsp_uge_avl;
4609 assert(!"unhandled zfs_deleg_who_type_t");
4613 who_perm_node_t *found_node = NULL;
4614 who_perm_node_t *node = safe_malloc(
4615 sizeof (who_perm_node_t));
4616 who_perm = &node->who_perm;
4617 uu_avl_index_t idx = 0;
4619 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4620 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4622 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4624 if (avl == fsperm->fsp_uge_avl) {
4626 struct passwd *p = NULL;
4627 struct group *g = NULL;
4628 const char *nice_name = NULL;
4630 switch (perm_type) {
4631 case ZFS_DELEG_USER_SETS:
4632 case ZFS_DELEG_USER:
4633 rid = atoi(perm_name);
4636 nice_name = p->pw_name;
4638 case ZFS_DELEG_GROUP_SETS:
4639 case ZFS_DELEG_GROUP:
4640 rid = atoi(perm_name);
4643 nice_name = g->gr_name;
4650 if (nice_name != NULL)
4652 node->who_perm.who_ug_name,
4656 uu_avl_insert(avl, node, idx);
4659 who_perm = &node->who_perm;
4663 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4670 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4672 nvpair_t *nvp = NULL;
4673 uu_avl_index_t idx = 0;
4675 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4676 nvlist_t *nvl2 = NULL;
4677 const char *fsname = nvpair_name(nvp);
4678 data_type_t type = nvpair_type(nvp);
4679 fs_perm_t *fsperm = NULL;
4680 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4684 fsperm = &node->fspn_fsperm;
4686 assert(DATA_TYPE_NVLIST == type);
4688 uu_list_node_init(node, &node->fspn_list_node,
4689 fspset->fsps_list_pool);
4691 idx = uu_list_numnodes(fspset->fsps_list);
4692 fs_perm_init(fsperm, fspset, fsname);
4694 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4697 (void) parse_fs_perm(fsperm, nvl2);
4699 uu_list_insert(fspset->fsps_list, node, idx);
4705 static inline const char *
4706 deleg_perm_comment(zfs_deleg_note_t note)
4708 const char *str = "";
4713 case ZFS_DELEG_NOTE_ALLOW:
4714 str = gettext("Must also have the permission that is being"
4715 "\n\t\t\t\tallowed");
4717 case ZFS_DELEG_NOTE_CLONE:
4718 str = gettext("Must also have the 'create' ability and 'mount'"
4719 "\n\t\t\t\tability in the origin file system");
4721 case ZFS_DELEG_NOTE_CREATE:
4722 str = gettext("Must also have the 'mount' ability");
4724 case ZFS_DELEG_NOTE_DESTROY:
4725 str = gettext("Must also have the 'mount' ability");
4727 case ZFS_DELEG_NOTE_DIFF:
4728 str = gettext("Allows lookup of paths within a dataset;"
4729 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4730 "\n\t\t\t\tin order to use zfs diff");
4732 case ZFS_DELEG_NOTE_HOLD:
4733 str = gettext("Allows adding a user hold to a snapshot");
4735 case ZFS_DELEG_NOTE_MOUNT:
4736 str = gettext("Allows mount/umount of ZFS datasets");
4738 case ZFS_DELEG_NOTE_PROMOTE:
4739 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4740 " 'promote' ability in the origin file system");
4742 case ZFS_DELEG_NOTE_RECEIVE:
4743 str = gettext("Must also have the 'mount' and 'create'"
4746 case ZFS_DELEG_NOTE_RELEASE:
4747 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4748 "might destroy the snapshot");
4750 case ZFS_DELEG_NOTE_RENAME:
4751 str = gettext("Must also have the 'mount' and 'create'"
4752 "\n\t\t\t\tability in the new parent");
4754 case ZFS_DELEG_NOTE_ROLLBACK:
4757 case ZFS_DELEG_NOTE_SEND:
4760 case ZFS_DELEG_NOTE_SHARE:
4761 str = gettext("Allows sharing file systems over NFS or SMB"
4762 "\n\t\t\t\tprotocols");
4764 case ZFS_DELEG_NOTE_SNAPSHOT:
4768 * case ZFS_DELEG_NOTE_VSCAN:
4769 * str = gettext("");
4773 case ZFS_DELEG_NOTE_GROUPQUOTA:
4774 str = gettext("Allows accessing any groupquota@... property");
4776 case ZFS_DELEG_NOTE_GROUPUSED:
4777 str = gettext("Allows reading any groupused@... property");
4779 case ZFS_DELEG_NOTE_USERPROP:
4780 str = gettext("Allows changing any user property");
4782 case ZFS_DELEG_NOTE_USERQUOTA:
4783 str = gettext("Allows accessing any userquota@... property");
4785 case ZFS_DELEG_NOTE_USERUSED:
4786 str = gettext("Allows reading any userused@... property");
4804 boolean_t recursive; /* unallow only */
4805 boolean_t prt_usage;
4807 boolean_t prt_perms;
4810 const char *dataset;
4814 prop_cmp(const void *a, const void *b)
4816 const char *str1 = *(const char **)a;
4817 const char *str2 = *(const char **)b;
4818 return (strcmp(str1, str2));
4822 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4824 const char *opt_desc[] = {
4825 "-h", gettext("show this help message and exit"),
4826 "-l", gettext("set permission locally"),
4827 "-d", gettext("set permission for descents"),
4828 "-u", gettext("set permission for user"),
4829 "-g", gettext("set permission for group"),
4830 "-e", gettext("set permission for everyone"),
4831 "-c", gettext("set create time permission"),
4832 "-s", gettext("define permission set"),
4834 "-r", gettext("remove permissions recursively"),
4836 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4837 size_t allow_size = unallow_size - 2;
4838 const char *props[ZFS_NUM_PROPS];
4841 FILE *fp = requested ? stdout : stderr;
4842 zprop_desc_t *pdtbl = zfs_prop_get_table();
4843 const char *fmt = gettext("%-16s %-14s\t%s\n");
4845 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4847 (void) fprintf(fp, gettext("Options:\n"));
4848 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4849 const char *opt = opt_desc[i++];
4850 const char *optdsc = opt_desc[i];
4851 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4854 (void) fprintf(fp, gettext("\nThe following permissions are "
4856 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4858 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4859 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4860 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4861 const char *perm_type = deleg_perm_type(perm_note);
4862 const char *perm_comment = deleg_perm_comment(perm_note);
4863 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4866 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4867 zprop_desc_t *pd = &pdtbl[i];
4868 if (pd->pd_visible != B_TRUE)
4871 if (pd->pd_attr == PROP_READONLY)
4874 props[count++] = pd->pd_name;
4876 props[count] = NULL;
4878 qsort(props, count, sizeof (char *), prop_cmp);
4880 for (i = 0; i < count; i++)
4881 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4884 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4886 exit(requested ? 0 : 2);
4889 static inline const char *
4890 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4893 if (un && argc == expected_argc - 1)
4895 else if (argc == expected_argc)
4896 *permsp = argv[argc - 2];
4898 allow_usage(un, B_FALSE,
4899 gettext("wrong number of parameters\n"));
4901 return (argv[argc - 1]);
4905 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4907 int uge_sum = opts->user + opts->group + opts->everyone;
4908 int csuge_sum = opts->create + opts->set + uge_sum;
4909 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4910 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4913 allow_usage(un, B_FALSE,
4914 gettext("-u, -g, and -e are mutually exclusive\n"));
4916 if (opts->prt_usage) {
4917 if (argc == 0 && all_sum == 0)
4918 allow_usage(un, B_TRUE, NULL);
4925 allow_usage(un, B_FALSE,
4926 gettext("invalid options combined with -s\n"));
4928 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4929 if (argv[0][0] != '@')
4930 allow_usage(un, B_FALSE,
4931 gettext("invalid set name: missing '@' prefix\n"));
4932 opts->who = argv[0];
4933 } else if (opts->create) {
4934 if (ldcsuge_sum > 1)
4935 allow_usage(un, B_FALSE,
4936 gettext("invalid options combined with -c\n"));
4937 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4938 } else if (opts->everyone) {
4940 allow_usage(un, B_FALSE,
4941 gettext("invalid options combined with -e\n"));
4942 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4943 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4945 opts->everyone = B_TRUE;
4948 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4949 } else if (argc == 1 && !un) {
4950 opts->prt_perms = B_TRUE;
4951 opts->dataset = argv[argc-1];
4953 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4954 opts->who = argv[0];
4957 if (!opts->local && !opts->descend) {
4958 opts->local = B_TRUE;
4959 opts->descend = B_TRUE;
4964 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4965 const char *who, char *perms, nvlist_t *top_nvl)
4968 char ld[2] = { '\0', '\0' };
4969 char who_buf[MAXNAMELEN + 32];
4970 char base_type = '\0';
4971 char set_type = '\0';
4972 nvlist_t *base_nvl = NULL;
4973 nvlist_t *set_nvl = NULL;
4976 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4978 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4982 case ZFS_DELEG_NAMED_SET_SETS:
4983 case ZFS_DELEG_NAMED_SET:
4984 set_type = ZFS_DELEG_NAMED_SET_SETS;
4985 base_type = ZFS_DELEG_NAMED_SET;
4986 ld[0] = ZFS_DELEG_NA;
4988 case ZFS_DELEG_CREATE_SETS:
4989 case ZFS_DELEG_CREATE:
4990 set_type = ZFS_DELEG_CREATE_SETS;
4991 base_type = ZFS_DELEG_CREATE;
4992 ld[0] = ZFS_DELEG_NA;
4994 case ZFS_DELEG_USER_SETS:
4995 case ZFS_DELEG_USER:
4996 set_type = ZFS_DELEG_USER_SETS;
4997 base_type = ZFS_DELEG_USER;
4999 ld[0] = ZFS_DELEG_LOCAL;
5001 ld[1] = ZFS_DELEG_DESCENDENT;
5003 case ZFS_DELEG_GROUP_SETS:
5004 case ZFS_DELEG_GROUP:
5005 set_type = ZFS_DELEG_GROUP_SETS;
5006 base_type = ZFS_DELEG_GROUP;
5008 ld[0] = ZFS_DELEG_LOCAL;
5010 ld[1] = ZFS_DELEG_DESCENDENT;
5012 case ZFS_DELEG_EVERYONE_SETS:
5013 case ZFS_DELEG_EVERYONE:
5014 set_type = ZFS_DELEG_EVERYONE_SETS;
5015 base_type = ZFS_DELEG_EVERYONE;
5017 ld[0] = ZFS_DELEG_LOCAL;
5019 ld[1] = ZFS_DELEG_DESCENDENT;
5023 assert(set_type != '\0' && base_type != '\0');
5026 if (perms != NULL) {
5028 char *end = curr + strlen(perms);
5030 while (curr < end) {
5031 char *delim = strchr(curr, ',');
5042 (void) nvlist_add_boolean(nvl, curr);
5048 for (i = 0; i < 2; i++) {
5049 char locality = ld[i];
5053 if (!nvlist_empty(base_nvl)) {
5055 (void) snprintf(who_buf,
5056 sizeof (who_buf), "%c%c$%s",
5057 base_type, locality, who);
5059 (void) snprintf(who_buf,
5060 sizeof (who_buf), "%c%c$",
5061 base_type, locality);
5063 (void) nvlist_add_nvlist(top_nvl, who_buf,
5068 if (!nvlist_empty(set_nvl)) {
5070 (void) snprintf(who_buf,
5071 sizeof (who_buf), "%c%c$%s",
5072 set_type, locality, who);
5074 (void) snprintf(who_buf,
5075 sizeof (who_buf), "%c%c$",
5076 set_type, locality);
5078 (void) nvlist_add_nvlist(top_nvl, who_buf,
5083 for (i = 0; i < 2; i++) {
5084 char locality = ld[i];
5089 (void) snprintf(who_buf, sizeof (who_buf),
5090 "%c%c$%s", base_type, locality, who);
5092 (void) snprintf(who_buf, sizeof (who_buf),
5093 "%c%c$", base_type, locality);
5094 (void) nvlist_add_boolean(top_nvl, who_buf);
5097 (void) snprintf(who_buf, sizeof (who_buf),
5098 "%c%c$%s", set_type, locality, who);
5100 (void) snprintf(who_buf, sizeof (who_buf),
5101 "%c%c$", set_type, locality);
5102 (void) nvlist_add_boolean(top_nvl, who_buf);
5108 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5110 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5114 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5115 opts->descend, opts->who, opts->perms, *nvlp);
5116 } else if (opts->create) {
5117 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5118 opts->descend, NULL, opts->perms, *nvlp);
5119 } else if (opts->everyone) {
5120 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5121 opts->descend, NULL, opts->perms, *nvlp);
5123 char *curr = opts->who;
5124 char *end = curr + strlen(curr);
5126 while (curr < end) {
5128 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5130 char *delim = strchr(curr, ',');
5133 struct passwd *p = NULL;
5134 struct group *g = NULL;
5142 rid = (uid_t)strtol(curr, &endch, 0);
5144 who_type = ZFS_DELEG_USER;
5153 (void) snprintf(errbuf, 256, gettext(
5154 "invalid user %s"), curr);
5155 allow_usage(un, B_TRUE, errbuf);
5157 } else if (opts->group) {
5158 who_type = ZFS_DELEG_GROUP;
5167 (void) snprintf(errbuf, 256, gettext(
5168 "invalid group %s"), curr);
5169 allow_usage(un, B_TRUE, errbuf);
5172 if (*endch != '\0') {
5179 if (*endch != '\0') {
5187 who_type = ZFS_DELEG_USER;
5189 } else if (g != NULL) {
5190 who_type = ZFS_DELEG_GROUP;
5193 (void) snprintf(errbuf, 256, gettext(
5194 "invalid user/group %s"), curr);
5195 allow_usage(un, B_TRUE, errbuf);
5199 (void) sprintf(id, "%u", rid);
5202 store_allow_perm(who_type, opts->local,
5203 opts->descend, who, opts->perms, *nvlp);
5212 print_set_creat_perms(uu_avl_t *who_avl)
5214 const char *sc_title[] = {
5215 gettext("Permission sets:\n"),
5216 gettext("Create time permissions:\n"),
5219 const char **title_ptr = sc_title;
5220 who_perm_node_t *who_node = NULL;
5221 int prev_weight = -1;
5223 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5224 who_node = uu_avl_next(who_avl, who_node)) {
5225 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5226 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5227 const char *who_name = who_node->who_perm.who_name;
5228 int weight = who_type2weight(who_type);
5229 boolean_t first = B_TRUE;
5230 deleg_perm_node_t *deleg_node;
5232 if (prev_weight != weight) {
5233 (void) printf(*title_ptr++);
5234 prev_weight = weight;
5237 if (who_name == NULL || strnlen(who_name, 1) == 0)
5238 (void) printf("\t");
5240 (void) printf("\t%s ", who_name);
5242 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5243 deleg_node = uu_avl_next(avl, deleg_node)) {
5246 deleg_node->dpn_perm.dp_name);
5249 (void) printf(",%s",
5250 deleg_node->dpn_perm.dp_name);
5253 (void) printf("\n");
5258 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5261 who_perm_node_t *who_node = NULL;
5262 boolean_t prt_title = B_TRUE;
5263 uu_avl_walk_t *walk;
5265 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5268 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5269 const char *who_name = who_node->who_perm.who_name;
5270 const char *nice_who_name = who_node->who_perm.who_ug_name;
5271 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5272 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5274 deleg_perm_node_t *deleg_node;
5275 boolean_t prt_who = B_TRUE;
5277 for (deleg_node = uu_avl_first(avl);
5279 deleg_node = uu_avl_next(avl, deleg_node)) {
5280 if (local != deleg_node->dpn_perm.dp_local ||
5281 descend != deleg_node->dpn_perm.dp_descend)
5285 const char *who = NULL;
5287 prt_title = B_FALSE;
5288 (void) printf(title);
5292 case ZFS_DELEG_USER_SETS:
5293 case ZFS_DELEG_USER:
5294 who = gettext("user");
5296 who_name = nice_who_name;
5298 case ZFS_DELEG_GROUP_SETS:
5299 case ZFS_DELEG_GROUP:
5300 who = gettext("group");
5302 who_name = nice_who_name;
5304 case ZFS_DELEG_EVERYONE_SETS:
5305 case ZFS_DELEG_EVERYONE:
5306 who = gettext("everyone");
5311 assert(who != NULL);
5315 if (who_name == NULL)
5316 (void) printf("\t%s", who);
5318 (void) printf("\t%s %s", who, who_name);
5321 (void) printf("%c%s", delim,
5322 deleg_node->dpn_perm.dp_name);
5327 (void) printf("\n");
5330 uu_avl_walk_end(walk);
5334 print_fs_perms(fs_perm_set_t *fspset)
5336 fs_perm_node_t *node = NULL;
5337 char buf[MAXNAMELEN + 32];
5338 const char *dsname = buf;
5340 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5341 node = uu_list_next(fspset->fsps_list, node)) {
5342 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5343 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5346 (void) snprintf(buf, sizeof (buf),
5347 gettext("---- Permissions on %s "),
5348 node->fspn_fsperm.fsp_name);
5349 (void) printf(dsname);
5350 left = 70 - strlen(buf);
5353 (void) printf("\n");
5355 print_set_creat_perms(sc_avl);
5356 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5357 gettext("Local permissions:\n"));
5358 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5359 gettext("Descendent permissions:\n"));
5360 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5361 gettext("Local+Descendent permissions:\n"));
5365 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5367 struct deleg_perms {
5373 set_deleg_perms(zfs_handle_t *zhp, void *data)
5375 struct deleg_perms *perms = (struct deleg_perms *)data;
5376 zfs_type_t zfs_type = zfs_get_type(zhp);
5378 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5381 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5385 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5388 nvlist_t *perm_nvl = NULL;
5389 nvlist_t *update_perm_nvl = NULL;
5392 struct allow_opts opts = { 0 };
5394 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5397 while ((c = getopt(argc, argv, optstr)) != -1) {
5400 opts.local = B_TRUE;
5403 opts.descend = B_TRUE;
5409 opts.group = B_TRUE;
5412 opts.everyone = B_TRUE;
5418 opts.create = B_TRUE;
5421 opts.recursive = B_TRUE;
5424 (void) fprintf(stderr, gettext("missing argument for "
5425 "'%c' option\n"), optopt);
5429 opts.prt_usage = B_TRUE;
5432 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5441 /* check arguments */
5442 parse_allow_args(argc, argv, un, &opts);
5444 /* try to open the dataset */
5445 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5446 ZFS_TYPE_VOLUME)) == NULL) {
5447 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5452 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5455 fs_perm_set_init(&fs_perm_set);
5456 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5457 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5462 print_fs_perms(&fs_perm_set);
5464 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5465 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5468 if (un && opts.recursive) {
5469 struct deleg_perms data = { un, update_perm_nvl };
5470 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5479 nvlist_free(perm_nvl);
5480 nvlist_free(update_perm_nvl);
5482 fs_perm_set_fini(&fs_perm_set);
5490 zfs_do_allow(int argc, char **argv)
5492 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5496 zfs_do_unallow(int argc, char **argv)
5498 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5502 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5507 boolean_t recursive = B_FALSE;
5508 const char *opts = holding ? "rt" : "r";
5512 while ((c = getopt(argc, argv, opts)) != -1) {
5518 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5527 /* check number of arguments */
5535 if (holding && tag[0] == '.') {
5536 /* tags starting with '.' are reserved for libzfs */
5537 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5541 for (i = 0; i < argc; ++i) {
5543 char parent[ZFS_MAX_DATASET_NAME_LEN];
5545 char *path = argv[i];
5547 delim = strchr(path, '@');
5548 if (delim == NULL) {
5549 (void) fprintf(stderr,
5550 gettext("'%s' is not a snapshot\n"), path);
5554 (void) strncpy(parent, path, delim - path);
5555 parent[delim - path] = '\0';
5557 zhp = zfs_open(g_zfs, parent,
5558 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5564 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5567 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5573 return (errors != 0);
5577 * zfs hold [-r] [-t] <tag> <snap> ...
5579 * -r Recursively hold
5581 * Apply a user-hold with the given tag to the list of snapshots.
5584 zfs_do_hold(int argc, char **argv)
5586 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5590 * zfs release [-r] <tag> <snap> ...
5592 * -r Recursively release
5594 * Release a user-hold with the given tag from the list of snapshots.
5597 zfs_do_release(int argc, char **argv)
5599 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5602 typedef struct holds_cbdata {
5603 boolean_t cb_recursive;
5604 const char *cb_snapname;
5606 size_t cb_max_namelen;
5607 size_t cb_max_taglen;
5610 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5611 #define DATETIME_BUF_LEN (32)
5616 print_holds(boolean_t scripted, boolean_t literal, size_t nwidth,
5617 size_t tagwidth, nvlist_t *nvl)
5620 nvpair_t *nvp = NULL;
5621 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5625 for (i = 0; i < 3; i++) {
5626 col = gettext(hdr_cols[i]);
5628 (void) printf("%-*s ", i ? tagwidth : nwidth,
5631 (void) printf("%s\n", col);
5635 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5636 char *zname = nvpair_name(nvp);
5638 nvpair_t *nvp2 = NULL;
5639 (void) nvpair_value_nvlist(nvp, &nvl2);
5640 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5641 char tsbuf[DATETIME_BUF_LEN];
5642 char *tagname = nvpair_name(nvp2);
5647 (void) nvpair_value_uint64(nvp2, &val);
5649 snprintf(tsbuf, DATETIME_BUF_LEN, "%llu", val);
5652 (void) localtime_r(&time, &t);
5653 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5654 gettext(STRFTIME_FMT_STR), &t);
5658 (void) printf("%s\t%s\t%s\n", zname,
5661 (void) printf("%-*s %-*s %s\n", nwidth,
5662 zname, tagwidth, tagname, tsbuf);
5669 * Generic callback function to list a dataset or snapshot.
5672 holds_callback(zfs_handle_t *zhp, void *data)
5674 holds_cbdata_t *cbp = data;
5675 nvlist_t *top_nvl = *cbp->cb_nvlp;
5676 nvlist_t *nvl = NULL;
5677 nvpair_t *nvp = NULL;
5678 const char *zname = zfs_get_name(zhp);
5679 size_t znamelen = strlen(zname);
5681 if (cbp->cb_recursive && cbp->cb_snapname != NULL) {
5682 const char *snapname;
5683 char *delim = strchr(zname, '@');
5687 snapname = delim + 1;
5688 if (strcmp(cbp->cb_snapname, snapname))
5692 if (zfs_get_holds(zhp, &nvl) != 0)
5695 if (znamelen > cbp->cb_max_namelen)
5696 cbp->cb_max_namelen = znamelen;
5698 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5699 const char *tag = nvpair_name(nvp);
5700 size_t taglen = strlen(tag);
5701 if (taglen > cbp->cb_max_taglen)
5702 cbp->cb_max_taglen = taglen;
5705 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5709 * zfs holds [-Hp] [-r | -d max] <dataset|snap> ...
5711 * -H Suppress header output
5712 * -p Output literal values
5713 * -r Recursively search for holds
5714 * -d max Limit depth of recursive search
5717 zfs_do_holds(int argc, char **argv)
5722 boolean_t scripted = B_FALSE;
5723 boolean_t literal = B_FALSE;
5724 boolean_t recursive = B_FALSE;
5725 const char *opts = "d:rHp";
5728 int types = ZFS_TYPE_SNAPSHOT;
5729 holds_cbdata_t cb = { 0 };
5736 while ((c = getopt(argc, argv, opts)) != -1) {
5739 limit = parse_depth(optarg, &flags);
5752 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5759 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5760 flags |= ZFS_ITER_RECURSE;
5766 /* check number of arguments */
5770 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5773 for (i = 0; i < argc; ++i) {
5774 char *snapshot = argv[i];
5776 const char *snapname = NULL;
5778 delim = strchr(snapshot, '@');
5779 if (delim != NULL) {
5780 snapname = delim + 1;
5782 snapshot[delim - snapshot] = '\0';
5785 cb.cb_recursive = recursive;
5786 cb.cb_snapname = snapname;
5790 * 1. collect holds data, set format options
5792 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5793 holds_callback, &cb);
5799 * 2. print holds data
5801 print_holds(scripted, literal, cb.cb_max_namelen, cb.cb_max_taglen,
5804 if (nvlist_empty(nvl))
5805 (void) printf(gettext("no datasets available\n"));
5809 return (0 != errors);
5812 #define CHECK_SPINNER 30
5813 #define SPINNER_TIME 3 /* seconds */
5814 #define MOUNT_TIME 5 /* seconds */
5817 get_one_dataset(zfs_handle_t *zhp, void *data)
5819 static char *spin[] = { "-", "\\", "|", "/" };
5820 static int spinval = 0;
5821 static int spincheck = 0;
5822 static time_t last_spin_time = (time_t)0;
5823 get_all_cb_t *cbp = data;
5824 zfs_type_t type = zfs_get_type(zhp);
5826 if (cbp->cb_verbose) {
5827 if (--spincheck < 0) {
5828 time_t now = time(NULL);
5829 if (last_spin_time + SPINNER_TIME < now) {
5830 update_progress(spin[spinval++ % 4]);
5831 last_spin_time = now;
5833 spincheck = CHECK_SPINNER;
5838 * Interate over any nested datasets.
5840 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5846 * Skip any datasets whose type does not match.
5848 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5852 libzfs_add_handle(cbp, zhp);
5853 assert(cbp->cb_used <= cbp->cb_alloc);
5859 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5861 get_all_cb_t cb = { 0 };
5862 cb.cb_verbose = verbose;
5863 cb.cb_getone = get_one_dataset;
5866 set_progress_header(gettext("Reading ZFS config"));
5867 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5869 *dslist = cb.cb_handles;
5870 *count = cb.cb_used;
5873 finish_progress(gettext("done."));
5877 * Generic callback for sharing or mounting filesystems. Because the code is so
5878 * similar, we have a common function with an extra parameter to determine which
5879 * mode we are using.
5881 #define OP_SHARE 0x1
5882 #define OP_MOUNT 0x2
5885 * Share or mount a dataset.
5888 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5889 boolean_t explicit, const char *options)
5891 char mountpoint[ZFS_MAXPROPLEN];
5892 char shareopts[ZFS_MAXPROPLEN];
5893 char smbshareopts[ZFS_MAXPROPLEN];
5894 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5896 uint64_t zoned, canmount;
5897 boolean_t shared_nfs, shared_smb;
5899 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5902 * Check to make sure we can mount/share this dataset. If we
5903 * are in the global zone and the filesystem is exported to a
5904 * local zone, or if we are in a local zone and the
5905 * filesystem is not exported, then it is an error.
5907 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5909 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5913 (void) fprintf(stderr, gettext("cannot %s '%s': "
5914 "dataset is exported to a local zone\n"), cmdname,
5918 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5922 (void) fprintf(stderr, gettext("cannot %s '%s': "
5923 "permission denied\n"), cmdname,
5929 * Ignore any filesystems which don't apply to us. This
5930 * includes those with a legacy mountpoint, or those with
5931 * legacy share options.
5933 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5934 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5935 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5936 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5937 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5938 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5940 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5941 strcmp(smbshareopts, "off") == 0) {
5945 (void) fprintf(stderr, gettext("cannot share '%s': "
5946 "legacy share\n"), zfs_get_name(zhp));
5947 (void) fprintf(stderr, gettext("to "
5948 "share this filesystem set "
5949 "sharenfs property on\n"));
5954 * We cannot share or mount legacy filesystems. If the
5955 * shareopts is non-legacy but the mountpoint is legacy, we
5956 * treat it as a legacy share.
5958 if (strcmp(mountpoint, "legacy") == 0) {
5962 (void) fprintf(stderr, gettext("cannot %s '%s': "
5963 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5964 (void) fprintf(stderr, gettext("use %s(8) to "
5965 "%s this filesystem\n"), cmdname, cmdname);
5969 if (strcmp(mountpoint, "none") == 0) {
5973 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5974 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5979 * canmount explicit outcome
5980 * on no pass through
5981 * on yes pass through
5983 * off yes display error, return 1
5984 * noauto no return 0
5985 * noauto yes pass through
5987 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5988 if (canmount == ZFS_CANMOUNT_OFF) {
5992 (void) fprintf(stderr, gettext("cannot %s '%s': "
5993 "'canmount' property is set to 'off'\n"), cmdname,
5996 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
6001 * If this filesystem is inconsistent and has a receive resume
6002 * token, we can not mount it.
6004 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
6005 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
6006 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
6010 (void) fprintf(stderr, gettext("cannot %s '%s': "
6011 "Contains partially-completed state from "
6012 "\"zfs receive -r\", which can be resumed with "
6013 "\"zfs send -t\"\n"),
6014 cmdname, zfs_get_name(zhp));
6019 * At this point, we have verified that the mountpoint and/or
6020 * shareopts are appropriate for auto management. If the
6021 * filesystem is already mounted or shared, return (failing
6022 * for explicit requests); otherwise mount or share the
6028 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
6029 shared_smb = zfs_is_shared_smb(zhp, NULL);
6031 if ((shared_nfs && shared_smb) ||
6032 (shared_nfs && strcmp(shareopts, "on") == 0 &&
6033 strcmp(smbshareopts, "off") == 0) ||
6034 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
6035 strcmp(shareopts, "off") == 0)) {
6039 (void) fprintf(stderr, gettext("cannot share "
6040 "'%s': filesystem already shared\n"),
6045 if (!zfs_is_mounted(zhp, NULL) &&
6046 zfs_mount(zhp, NULL, 0) != 0)
6049 if (protocol == NULL) {
6050 if (zfs_shareall(zhp) != 0)
6052 } else if (strcmp(protocol, "nfs") == 0) {
6053 if (zfs_share_nfs(zhp))
6055 } else if (strcmp(protocol, "smb") == 0) {
6056 if (zfs_share_smb(zhp))
6059 (void) fprintf(stderr, gettext("cannot share "
6060 "'%s': invalid share type '%s' "
6062 zfs_get_name(zhp), protocol);
6069 if (options == NULL)
6070 mnt.mnt_mntopts = "";
6072 mnt.mnt_mntopts = (char *)options;
6074 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6075 zfs_is_mounted(zhp, NULL)) {
6079 (void) fprintf(stderr, gettext("cannot mount "
6080 "'%s': filesystem already mounted\n"),
6085 if (zfs_mount(zhp, options, flags) != 0)
6094 * Reports progress in the form "(current/total)". Not thread-safe.
6097 report_mount_progress(int current, int total)
6099 static time_t last_progress_time = 0;
6100 time_t now = time(NULL);
6103 /* report 1..n instead of 0..n-1 */
6106 /* display header if we're here for the first time */
6108 set_progress_header(gettext("Mounting ZFS filesystems"));
6109 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6110 /* too soon to report again */
6114 last_progress_time = now;
6116 (void) sprintf(info, "(%d/%d)", current, total);
6118 if (current == total)
6119 finish_progress(info);
6121 update_progress(info);
6125 append_options(char *mntopts, char *newopts)
6127 int len = strlen(mntopts);
6129 /* original length plus new string to append plus 1 for the comma */
6130 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6131 (void) fprintf(stderr, gettext("the opts argument for "
6132 "'%c' option is too long (more than %d chars)\n"),
6133 "-o", MNT_LINE_MAX);
6138 mntopts[len++] = ',';
6140 (void) strcpy(&mntopts[len], newopts);
6144 share_mount(int op, int argc, char **argv)
6147 boolean_t verbose = B_FALSE;
6149 char *options = NULL;
6153 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6163 if (*optarg == '\0') {
6164 (void) fprintf(stderr, gettext("empty mount "
6165 "options (-o) specified\n"));
6169 if (options == NULL)
6170 options = safe_malloc(MNT_LINE_MAX + 1);
6172 /* option validation is done later */
6173 append_options(options, optarg);
6177 warnx("no overlay mounts support on FreeBSD, ignoring");
6180 (void) fprintf(stderr, gettext("missing argument for "
6181 "'%c' option\n"), optopt);
6185 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6194 /* check number of arguments */
6196 zfs_handle_t **dslist = NULL;
6197 size_t i, count = 0;
6198 char *protocol = NULL;
6200 if (op == OP_SHARE && argc > 0) {
6201 if (strcmp(argv[0], "nfs") != 0 &&
6202 strcmp(argv[0], "smb") != 0) {
6203 (void) fprintf(stderr, gettext("share type "
6204 "must be 'nfs' or 'smb'\n"));
6213 (void) fprintf(stderr, gettext("too many arguments\n"));
6217 start_progress_timer();
6218 get_all_datasets(&dslist, &count, verbose);
6223 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6225 for (i = 0; i < count; i++) {
6227 report_mount_progress(i, count);
6229 if (share_mount_one(dslist[i], op, flags, protocol,
6230 B_FALSE, options) != 0)
6232 zfs_close(dslist[i]);
6236 } else if (argc == 0) {
6237 struct mnttab entry;
6239 if ((op == OP_SHARE) || (options != NULL)) {
6240 (void) fprintf(stderr, gettext("missing filesystem "
6241 "argument (specify -a for all)\n"));
6246 * When mount is given no arguments, go through /etc/mnttab and
6247 * display any active ZFS mounts. We hide any snapshots, since
6248 * they are controlled automatically.
6250 rewind(mnttab_file);
6251 while (getmntent(mnttab_file, &entry) == 0) {
6252 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6253 strchr(entry.mnt_special, '@') != NULL)
6256 (void) printf("%-30s %s\n", entry.mnt_special,
6264 (void) fprintf(stderr,
6265 gettext("too many arguments\n"));
6269 if ((zhp = zfs_open(g_zfs, argv[0],
6270 ZFS_TYPE_FILESYSTEM)) == NULL) {
6273 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6283 * zfs mount -a [nfs]
6284 * zfs mount filesystem
6286 * Mount all filesystems, or mount the given filesystem.
6289 zfs_do_mount(int argc, char **argv)
6291 return (share_mount(OP_MOUNT, argc, argv));
6295 * zfs share -a [nfs | smb]
6296 * zfs share filesystem
6298 * Share all filesystems, or share the given filesystem.
6301 zfs_do_share(int argc, char **argv)
6303 return (share_mount(OP_SHARE, argc, argv));
6306 typedef struct unshare_unmount_node {
6307 zfs_handle_t *un_zhp;
6309 uu_avl_node_t un_avlnode;
6310 } unshare_unmount_node_t;
6314 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6316 const unshare_unmount_node_t *l = larg;
6317 const unshare_unmount_node_t *r = rarg;
6319 return (strcmp(l->un_mountp, r->un_mountp));
6323 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6324 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6325 * and unmount it appropriately.
6328 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6332 struct stat64 statbuf;
6333 struct extmnttab entry;
6334 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6338 * Search for the path in /etc/mnttab. Rather than looking for the
6339 * specific path, which can be fooled by non-standard paths (i.e. ".."
6340 * or "//"), we stat() the path and search for the corresponding
6341 * (major,minor) device pair.
6343 if (stat64(path, &statbuf) != 0) {
6344 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6345 cmdname, path, strerror(errno));
6348 path_inode = statbuf.st_ino;
6351 * Search for the given (major,minor) pair in the mount table.
6354 rewind(mnttab_file);
6355 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6356 if (entry.mnt_major == major(statbuf.st_dev) &&
6357 entry.mnt_minor == minor(statbuf.st_dev))
6364 if (statfs(path, &sfs) != 0) {
6365 (void) fprintf(stderr, "%s: %s\n", path,
6369 statfs2mnttab(&sfs, &entry);
6373 if (op == OP_SHARE) {
6374 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6375 "currently mounted\n"), cmdname, path);
6378 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6380 if ((ret = umount2(path, flags)) != 0)
6381 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6386 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6387 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6388 "filesystem\n"), cmdname, path);
6392 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6393 ZFS_TYPE_FILESYSTEM)) == NULL)
6397 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6398 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6399 cmdname, path, strerror(errno));
6401 } else if (statbuf.st_ino != path_inode) {
6402 (void) fprintf(stderr, gettext("cannot "
6403 "%s '%s': not a mountpoint\n"), cmdname, path);
6407 if (op == OP_SHARE) {
6408 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6409 char smbshare_prop[ZFS_MAXPROPLEN];
6411 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6412 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6413 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6414 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6416 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6417 strcmp(smbshare_prop, "off") == 0) {
6418 (void) fprintf(stderr, gettext("cannot unshare "
6419 "'%s': legacy share\n"), path);
6421 (void) fprintf(stderr, gettext("use "
6422 "unshare(1M) to unshare this filesystem\n"));
6424 } else if (!zfs_is_shared(zhp)) {
6425 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6426 "not currently shared\n"), path);
6428 ret = zfs_unshareall_bypath(zhp, path);
6431 char mtpt_prop[ZFS_MAXPROPLEN];
6433 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6434 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6437 ret = zfs_unmount(zhp, NULL, flags);
6438 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6439 (void) fprintf(stderr, gettext("cannot unmount "
6440 "'%s': legacy mountpoint\n"),
6442 (void) fprintf(stderr, gettext("use umount(8) "
6443 "to unmount this filesystem\n"));
6445 ret = zfs_unmountall(zhp, flags);
6456 * Generic callback for unsharing or unmounting a filesystem.
6459 unshare_unmount(int op, int argc, char **argv)
6466 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6467 char sharesmb[ZFS_MAXPROPLEN];
6470 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6479 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6490 * We could make use of zfs_for_each() to walk all datasets in
6491 * the system, but this would be very inefficient, especially
6492 * since we would have to linearly search /etc/mnttab for each
6493 * one. Instead, do one pass through /etc/mnttab looking for
6494 * zfs entries and call zfs_unmount() for each one.
6496 * Things get a little tricky if the administrator has created
6497 * mountpoints beneath other ZFS filesystems. In this case, we
6498 * have to unmount the deepest filesystems first. To accomplish
6499 * this, we place all the mountpoints in an AVL tree sorted by
6500 * the special type (dataset name), and walk the result in
6501 * reverse to make sure to get any snapshots first.
6503 struct mnttab entry;
6504 uu_avl_pool_t *pool;
6505 uu_avl_t *tree = NULL;
6506 unshare_unmount_node_t *node;
6508 uu_avl_walk_t *walk;
6511 (void) fprintf(stderr, gettext("too many arguments\n"));
6515 if (((pool = uu_avl_pool_create("unmount_pool",
6516 sizeof (unshare_unmount_node_t),
6517 offsetof(unshare_unmount_node_t, un_avlnode),
6518 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6519 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6522 rewind(mnttab_file);
6523 while (getmntent(mnttab_file, &entry) == 0) {
6525 /* ignore non-ZFS entries */
6526 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6529 /* ignore snapshots */
6530 if (strchr(entry.mnt_special, '@') != NULL)
6533 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6534 ZFS_TYPE_FILESYSTEM)) == NULL) {
6540 * Ignore datasets that are excluded/restricted by
6543 if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
6550 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6552 sizeof (nfs_mnt_prop),
6553 NULL, NULL, 0, B_FALSE) == 0);
6554 if (strcmp(nfs_mnt_prop, "off") != 0)
6556 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6558 sizeof (nfs_mnt_prop),
6559 NULL, NULL, 0, B_FALSE) == 0);
6560 if (strcmp(nfs_mnt_prop, "off") == 0)
6564 /* Ignore legacy mounts */
6565 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6567 sizeof (nfs_mnt_prop),
6568 NULL, NULL, 0, B_FALSE) == 0);
6569 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6571 /* Ignore canmount=noauto mounts */
6572 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6573 ZFS_CANMOUNT_NOAUTO)
6579 node = safe_malloc(sizeof (unshare_unmount_node_t));
6581 node->un_mountp = safe_strdup(entry.mnt_mountp);
6583 uu_avl_node_init(node, &node->un_avlnode, pool);
6585 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6586 uu_avl_insert(tree, node, idx);
6588 zfs_close(node->un_zhp);
6589 free(node->un_mountp);
6595 * Walk the AVL tree in reverse, unmounting each filesystem and
6596 * removing it from the AVL tree in the process.
6598 if ((walk = uu_avl_walk_start(tree,
6599 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6602 while ((node = uu_avl_walk_next(walk)) != NULL) {
6603 uu_avl_remove(tree, node);
6607 if (zfs_unshareall_bypath(node->un_zhp,
6608 node->un_mountp) != 0)
6613 if (zfs_unmount(node->un_zhp,
6614 node->un_mountp, flags) != 0)
6619 zfs_close(node->un_zhp);
6620 free(node->un_mountp);
6624 uu_avl_walk_end(walk);
6625 uu_avl_destroy(tree);
6626 uu_avl_pool_destroy(pool);
6631 (void) fprintf(stderr,
6632 gettext("missing filesystem argument\n"));
6634 (void) fprintf(stderr,
6635 gettext("too many arguments\n"));
6640 * We have an argument, but it may be a full path or a ZFS
6641 * filesystem. Pass full paths off to unmount_path() (shared by
6642 * manual_unmount), otherwise open the filesystem and pass to
6645 if (argv[0][0] == '/')
6646 return (unshare_unmount_path(op, argv[0],
6649 if ((zhp = zfs_open(g_zfs, argv[0],
6650 ZFS_TYPE_FILESYSTEM)) == NULL)
6653 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6654 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6655 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6656 NULL, 0, B_FALSE) == 0);
6660 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6662 sizeof (nfs_mnt_prop),
6663 NULL, NULL, 0, B_FALSE) == 0);
6664 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6665 sharesmb, sizeof (sharesmb), NULL, NULL,
6668 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6669 strcmp(sharesmb, "off") == 0) {
6670 (void) fprintf(stderr, gettext("cannot "
6671 "unshare '%s': legacy share\n"),
6674 (void) fprintf(stderr, gettext("use "
6675 "unshare(1M) to unshare this "
6679 } else if (!zfs_is_shared(zhp)) {
6680 (void) fprintf(stderr, gettext("cannot "
6681 "unshare '%s': not currently "
6682 "shared\n"), zfs_get_name(zhp));
6684 } else if (zfs_unshareall(zhp) != 0) {
6690 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6691 (void) fprintf(stderr, gettext("cannot "
6692 "unmount '%s': legacy "
6693 "mountpoint\n"), zfs_get_name(zhp));
6694 (void) fprintf(stderr, gettext("use "
6695 "umount(8) to unmount this "
6698 } else if (!zfs_is_mounted(zhp, NULL)) {
6699 (void) fprintf(stderr, gettext("cannot "
6700 "unmount '%s': not currently "
6704 } else if (zfs_unmountall(zhp, flags) != 0) {
6718 * zfs unmount filesystem
6720 * Unmount all filesystems, or a specific ZFS filesystem.
6723 zfs_do_unmount(int argc, char **argv)
6725 return (unshare_unmount(OP_MOUNT, argc, argv));
6730 * zfs unshare filesystem
6732 * Unshare all filesystems, or a specific ZFS filesystem.
6735 zfs_do_unshare(int argc, char **argv)
6737 return (unshare_unmount(OP_SHARE, argc, argv));
6741 * Attach/detach the given dataset to/from the given jail
6745 do_jail(int argc, char **argv, int attach)
6750 /* check number of arguments */
6752 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6756 (void) fprintf(stderr, gettext("too many arguments\n"));
6760 jailid = jail_getid(argv[1]);
6762 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6766 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6770 ret = (zfs_jail(zhp, jailid, attach) != 0);
6777 * zfs jail jailid filesystem
6779 * Attach the given dataset to the given jail
6783 zfs_do_jail(int argc, char **argv)
6786 return (do_jail(argc, argv, 1));
6790 * zfs unjail jailid filesystem
6792 * Detach the given dataset from the given jail
6796 zfs_do_unjail(int argc, char **argv)
6799 return (do_jail(argc, argv, 0));
6803 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6804 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6807 manual_mount(int argc, char **argv)
6810 char mountpoint[ZFS_MAXPROPLEN];
6811 char mntopts[MNT_LINE_MAX] = { '\0' };
6815 char *dataset, *path;
6818 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6821 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6824 flags |= MS_OVERLAY;
6827 flags |= MS_NOMNTTAB;
6830 (void) fprintf(stderr, gettext("missing argument for "
6831 "'%c' option\n"), optopt);
6835 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6837 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6846 /* check that we only have two arguments */
6849 (void) fprintf(stderr, gettext("missing dataset "
6852 (void) fprintf(stderr,
6853 gettext("missing mountpoint argument\n"));
6855 (void) fprintf(stderr, gettext("too many arguments\n"));
6856 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6863 /* try to open the dataset */
6864 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6867 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6868 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6870 /* check for legacy mountpoint and complain appropriately */
6872 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6873 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6874 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6875 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6880 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6881 "mounted using 'mount -t zfs'\n"), dataset);
6882 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6883 "instead.\n"), path);
6884 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6885 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6886 (void) fprintf(stderr, gettext("See zfs(8) for more "
6895 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6896 * unmounts of non-legacy filesystems, as this is the dominant administrative
6900 manual_unmount(int argc, char **argv)
6906 while ((c = getopt(argc, argv, "f")) != -1) {
6912 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6914 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6923 /* check arguments */
6926 (void) fprintf(stderr, gettext("missing path "
6929 (void) fprintf(stderr, gettext("too many arguments\n"));
6930 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6934 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6938 find_command_idx(char *command, int *idx)
6942 for (i = 0; i < NCOMMAND; i++) {
6943 if (command_table[i].name == NULL)
6946 if (strcmp(command, command_table[i].name) == 0) {
6955 zfs_do_diff(int argc, char **argv)
6959 char *tosnap = NULL;
6960 char *fromsnap = NULL;
6965 while ((c = getopt(argc, argv, "FHt")) != -1) {
6968 flags |= ZFS_DIFF_CLASSIFY;
6971 flags |= ZFS_DIFF_PARSEABLE;
6974 flags |= ZFS_DIFF_TIMESTAMP;
6977 (void) fprintf(stderr,
6978 gettext("invalid option '%c'\n"), optopt);
6987 (void) fprintf(stderr,
6988 gettext("must provide at least one snapshot name\n"));
6993 (void) fprintf(stderr, gettext("too many arguments\n"));
6998 tosnap = (argc == 2) ? argv[1] : NULL;
7001 if (*fromsnap != '@')
7002 copy = strdup(fromsnap);
7004 copy = strdup(tosnap);
7008 if ((atp = strchr(copy, '@')) != NULL)
7011 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
7017 * Ignore SIGPIPE so that the library can give us
7018 * information on any failure
7020 (void) sigignore(SIGPIPE);
7022 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
7030 zfs_do_remap(int argc, char **argv)
7035 (void) fprintf(stderr, gettext("wrong number of arguments\n"));
7040 err = zfs_remap_indirects(g_zfs, fsname);
7046 * zfs bookmark <fs@snap> <fs#bmark>
7048 * Creates a bookmark with the given name from the given snapshot.
7051 zfs_do_bookmark(int argc, char **argv)
7053 char snapname[ZFS_MAX_DATASET_NAME_LEN];
7060 while ((c = getopt(argc, argv, "")) != -1) {
7063 (void) fprintf(stderr,
7064 gettext("invalid option '%c'\n"), optopt);
7072 /* check number of arguments */
7074 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
7078 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
7082 if (strchr(argv[1], '#') == NULL) {
7083 (void) fprintf(stderr,
7084 gettext("invalid bookmark name '%s' -- "
7085 "must contain a '#'\n"), argv[1]);
7089 if (argv[0][0] == '@') {
7091 * Snapshot name begins with @.
7092 * Default to same fs as bookmark.
7094 (void) strncpy(snapname, argv[1], sizeof (snapname));
7095 *strchr(snapname, '#') = '\0';
7096 (void) strlcat(snapname, argv[0], sizeof (snapname));
7098 (void) strncpy(snapname, argv[0], sizeof (snapname));
7100 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
7106 nvl = fnvlist_alloc();
7107 fnvlist_add_string(nvl, argv[1], snapname);
7108 ret = lzc_bookmark(nvl, NULL);
7112 const char *err_msg;
7115 (void) snprintf(errbuf, sizeof (errbuf),
7116 dgettext(TEXT_DOMAIN,
7117 "cannot create bookmark '%s'"), argv[1]);
7121 err_msg = "bookmark is in a different pool";
7124 err_msg = "bookmark exists";
7127 err_msg = "invalid argument";
7130 err_msg = "bookmark feature not enabled";
7133 err_msg = "out of space";
7136 err_msg = "unknown error";
7139 (void) fprintf(stderr, "%s: %s\n", errbuf,
7140 dgettext(TEXT_DOMAIN, err_msg));
7151 zfs_do_channel_program(int argc, char **argv)
7155 char *progbuf, *filename, *poolname;
7156 size_t progsize, progread;
7158 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
7159 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
7160 boolean_t sync_flag = B_TRUE;
7161 zpool_handle_t *zhp;
7165 (c = getopt(argc, argv, "nt:(instr-limit)m:(memory-limit)"))) {
7173 arg = strtoull(optarg, &endp, 0);
7174 if (errno != 0 || *endp != '\0') {
7175 (void) fprintf(stderr, gettext(
7177 "'%s': expected integer\n"), optarg);
7182 if (arg > ZCP_MAX_INSTRLIMIT || arg == 0) {
7183 (void) fprintf(stderr, gettext(
7184 "Invalid instruction limit: "
7191 ASSERT3U(c, ==, 'm');
7192 if (arg > ZCP_MAX_MEMLIMIT || arg == 0) {
7193 (void) fprintf(stderr, gettext(
7194 "Invalid memory limit: "
7204 sync_flag = B_FALSE;
7208 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7218 (void) fprintf(stderr,
7219 gettext("invalid number of arguments\n"));
7225 if (strcmp(filename, "-") == 0) {
7227 filename = "standard input";
7228 } else if ((fd = open(filename, O_RDONLY)) < 0) {
7229 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
7230 filename, strerror(errno));
7234 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7235 (void) fprintf(stderr, gettext("cannot open pool '%s'"),
7242 * Read in the channel program, expanding the program buffer as
7247 progbuf = safe_malloc(progsize);
7249 ret = read(fd, progbuf + progread, progsize - progread);
7251 if (progread == progsize && ret > 0) {
7253 progbuf = safe_realloc(progbuf, progsize);
7261 (void) fprintf(stderr,
7262 gettext("cannot read '%s': %s\n"),
7263 filename, strerror(errno));
7266 progbuf[progread] = '\0';
7269 * Any remaining arguments are passed as arguments to the lua script as
7272 * "argv" -> [ "arg 1", ... "arg n" ],
7275 nvlist_t *argnvl = fnvlist_alloc();
7276 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);
7279 ret = lzc_channel_program(poolname, progbuf,
7280 instrlimit, memlimit, argnvl, &outnvl);
7282 ret = lzc_channel_program_nosync(poolname, progbuf,
7283 instrlimit, memlimit, argnvl, &outnvl);
7288 * On error, report the error message handed back by lua if one
7289 * exists. Otherwise, generate an appropriate error message,
7290 * falling back on strerror() for an unexpected return code.
7292 char *errstring = NULL;
7293 if (nvlist_exists(outnvl, ZCP_RET_ERROR)) {
7294 (void) nvlist_lookup_string(outnvl,
7295 ZCP_RET_ERROR, &errstring);
7296 if (errstring == NULL)
7297 errstring = strerror(ret);
7302 "Invalid instruction or memory limit.";
7305 errstring = "Return value too large.";
7308 errstring = "Memory limit exhausted.";
7315 errstring = "Timed out.";
7318 errstring = "Permission denied. Channel "
7319 "programs must be run as root.";
7322 errstring = strerror(ret);
7325 (void) fprintf(stderr,
7326 gettext("Channel program execution failed:\n%s\n"),
7329 (void) printf("Channel program fully executed ");
7330 if (nvlist_empty(outnvl)) {
7331 (void) printf("with no return value.\n");
7333 (void) printf("with return value:\n");
7334 dump_nvlist(outnvl, 4);
7339 fnvlist_free(outnvl);
7340 fnvlist_free(argnvl);
7349 main(int argc, char **argv)
7356 (void) setlocale(LC_ALL, "");
7357 (void) textdomain(TEXT_DOMAIN);
7361 if ((g_zfs = libzfs_init()) == NULL) {
7362 (void) fprintf(stderr, gettext("internal error: failed to "
7363 "initialize ZFS library\n"));
7367 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7369 libzfs_print_on_error(g_zfs, B_TRUE);
7371 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
7372 (void) fprintf(stderr, gettext("internal error: unable to "
7373 "open %s\n"), MNTTAB);
7378 * This command also doubles as the /etc/fs mount and unmount program.
7379 * Determine if we should take this behavior based on argv[0].
7381 progname = basename(argv[0]);
7382 if (strcmp(progname, "mount") == 0) {
7383 ret = manual_mount(argc, argv);
7384 } else if (strcmp(progname, "umount") == 0) {
7385 ret = manual_unmount(argc, argv);
7388 * Make sure the user has specified some command.
7391 (void) fprintf(stderr, gettext("missing command\n"));
7398 * The 'umount' command is an alias for 'unmount'
7400 if (strcmp(cmdname, "umount") == 0)
7401 cmdname = "unmount";
7404 * The 'recv' command is an alias for 'receive'
7406 if (strcmp(cmdname, "recv") == 0)
7407 cmdname = "receive";
7410 * The 'snap' command is an alias for 'snapshot'
7412 if (strcmp(cmdname, "snap") == 0)
7413 cmdname = "snapshot";
7418 if (strcmp(cmdname, "-?") == 0)
7422 * Run the appropriate command.
7424 libzfs_mnttab_cache(g_zfs, B_TRUE);
7425 if (find_command_idx(cmdname, &i) == 0) {
7426 current_command = &command_table[i];
7427 ret = command_table[i].func(argc - 1, argv + 1);
7428 } else if (strchr(cmdname, '=') != NULL) {
7429 verify(find_command_idx("set", &i) == 0);
7430 current_command = &command_table[i];
7431 ret = command_table[i].func(argc, argv);
7433 (void) fprintf(stderr, gettext("unrecognized "
7434 "command '%s'\n"), cmdname);
7437 libzfs_mnttab_cache(g_zfs, B_FALSE);
7440 (void) fclose(mnttab_file);
7442 if (ret == 0 && log_history)
7443 (void) zpool_log_history(g_zfs, history_str);
7448 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7449 * for the purposes of running ::findleaks.
7451 if (getenv("ZFS_ABORT") != NULL) {
7452 (void) printf("dumping core by request\n");