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>
80 #include "zfs_comutil.h"
82 libzfs_handle_t *g_zfs;
84 static FILE *mnttab_file;
85 static char history_str[HIS_MAX_RECORD_LEN];
86 static boolean_t log_history = B_TRUE;
88 static int zfs_do_clone(int argc, char **argv);
89 static int zfs_do_create(int argc, char **argv);
90 static int zfs_do_destroy(int argc, char **argv);
91 static int zfs_do_get(int argc, char **argv);
92 static int zfs_do_inherit(int argc, char **argv);
93 static int zfs_do_list(int argc, char **argv);
94 static int zfs_do_mount(int argc, char **argv);
95 static int zfs_do_rename(int argc, char **argv);
96 static int zfs_do_rollback(int argc, char **argv);
97 static int zfs_do_set(int argc, char **argv);
98 static int zfs_do_upgrade(int argc, char **argv);
99 static int zfs_do_snapshot(int argc, char **argv);
100 static int zfs_do_unmount(int argc, char **argv);
101 static int zfs_do_share(int argc, char **argv);
102 static int zfs_do_unshare(int argc, char **argv);
103 static int zfs_do_send(int argc, char **argv);
104 static int zfs_do_receive(int argc, char **argv);
105 static int zfs_do_promote(int argc, char **argv);
106 static int zfs_do_userspace(int argc, char **argv);
107 static int zfs_do_allow(int argc, char **argv);
108 static int zfs_do_unallow(int argc, char **argv);
109 static int zfs_do_hold(int argc, char **argv);
110 static int zfs_do_holds(int argc, char **argv);
111 static int zfs_do_release(int argc, char **argv);
112 static int zfs_do_diff(int argc, char **argv);
113 static int zfs_do_jail(int argc, char **argv);
114 static int zfs_do_unjail(int argc, char **argv);
115 static int zfs_do_bookmark(int argc, char **argv);
116 static int zfs_do_remap(int argc, char **argv);
117 static int zfs_do_channel_program(int argc, char **argv);
120 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
125 _umem_debug_init(void)
127 return ("default,verbose"); /* $UMEM_DEBUG setting */
131 _umem_logging_init(void)
133 return ("fail,contents"); /* $UMEM_LOGGING setting */
168 HELP_CHANNEL_PROGRAM,
171 typedef struct zfs_command {
173 int (*func)(int argc, char **argv);
178 * Master command table. Each ZFS command has a name, associated function, and
179 * usage message. The usage messages need to be internationalized, so we have
180 * to have a function to return the usage message based on a command index.
182 * These commands are organized according to how they are displayed in the usage
183 * message. An empty command (one with a NULL name) indicates an empty line in
184 * the generic usage message.
186 static zfs_command_t command_table[] = {
187 { "create", zfs_do_create, HELP_CREATE },
188 { "destroy", zfs_do_destroy, HELP_DESTROY },
190 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
191 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
192 { "clone", zfs_do_clone, HELP_CLONE },
193 { "promote", zfs_do_promote, HELP_PROMOTE },
194 { "rename", zfs_do_rename, HELP_RENAME },
195 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
196 { "program", zfs_do_channel_program, HELP_CHANNEL_PROGRAM },
198 { "list", zfs_do_list, HELP_LIST },
200 { "set", zfs_do_set, HELP_SET },
201 { "get", zfs_do_get, HELP_GET },
202 { "inherit", zfs_do_inherit, HELP_INHERIT },
203 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
204 { "userspace", zfs_do_userspace, HELP_USERSPACE },
205 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
207 { "mount", zfs_do_mount, HELP_MOUNT },
208 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
209 { "share", zfs_do_share, HELP_SHARE },
210 { "unshare", zfs_do_unshare, HELP_UNSHARE },
212 { "send", zfs_do_send, HELP_SEND },
213 { "receive", zfs_do_receive, HELP_RECEIVE },
215 { "allow", zfs_do_allow, HELP_ALLOW },
217 { "unallow", zfs_do_unallow, HELP_UNALLOW },
219 { "hold", zfs_do_hold, HELP_HOLD },
220 { "holds", zfs_do_holds, HELP_HOLDS },
221 { "release", zfs_do_release, HELP_RELEASE },
222 { "diff", zfs_do_diff, HELP_DIFF },
224 { "jail", zfs_do_jail, HELP_JAIL },
225 { "unjail", zfs_do_unjail, HELP_UNJAIL },
226 { "remap", zfs_do_remap, HELP_REMAP },
229 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
231 zfs_command_t *current_command;
234 get_usage(zfs_help_t idx)
238 return (gettext("\tclone [-p] [-o property=value] ... "
239 "<snapshot> <filesystem|volume>\n"));
241 return (gettext("\tcreate [-pu] [-o property=value] ... "
243 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
244 "-V <size> <volume>\n"));
246 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
247 "\tdestroy [-dnpRrv] "
248 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
249 "\tdestroy <filesystem|volume>#<bookmark>\n"));
251 return (gettext("\tget [-rHp] [-d max] "
252 "[-o \"all\" | field[,...]]\n"
253 "\t [-t type[,...]] [-s source[,...]]\n"
254 "\t <\"all\" | property[,...]> "
255 "[filesystem|volume|snapshot|bookmark] ...\n"));
257 return (gettext("\tinherit [-rS] <property> "
258 "<filesystem|volume|snapshot> ...\n"));
260 return (gettext("\tupgrade [-v]\n"
261 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
263 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
265 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
267 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
268 "[-s property]...\n\t [-S property]... [-t type[,...]] "
269 "[filesystem|volume|snapshot] ...\n"));
271 return (gettext("\tmount\n"
272 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
274 return (gettext("\tpromote <clone-filesystem>\n"));
276 return (gettext("\treceive|recv [-vnsFu] <filesystem|volume|"
278 "\treceive|recv [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
280 "\treceive|recv -A <filesystem|volume>\n"));
282 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
283 "<filesystem|volume|snapshot>\n"
284 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
285 "\trename -r <snapshot> <snapshot>\n"
286 "\trename -u [-p] <filesystem> <filesystem>"));
288 return (gettext("\trollback [-rRf] <snapshot>\n"));
290 return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] "
292 "\tsend [-Le] [-i snapshot|bookmark] "
293 "<filesystem|volume|snapshot>\n"
294 "\tsend [-nvPe] -t <receive_resume_token>\n"));
296 return (gettext("\tset <property=value> ... "
297 "<filesystem|volume|snapshot> ...\n"));
299 return (gettext("\tshare <-a | filesystem>\n"));
301 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
302 "<filesystem|volume>@<snap> ...\n"));
304 return (gettext("\tunmount|umount [-f] "
305 "<-a | filesystem|mountpoint>\n"));
307 return (gettext("\tunshare "
308 "<-a | filesystem|mountpoint>\n"));
310 return (gettext("\tallow <filesystem|volume>\n"
312 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
313 "\t <filesystem|volume>\n"
314 "\tallow [-ld] -e <perm|@setname>[,...] "
315 "<filesystem|volume>\n"
316 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
317 "\tallow -s @setname <perm|@setname>[,...] "
318 "<filesystem|volume>\n"));
320 return (gettext("\tunallow [-rldug] "
321 "<\"everyone\"|user|group>[,...]\n"
322 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
323 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
324 "<filesystem|volume>\n"
325 "\tunallow [-r] -c [<perm|@setname>[,...]] "
326 "<filesystem|volume>\n"
327 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
328 "<filesystem|volume>\n"));
330 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
332 "\t [-S field] ... [-t type[,...]] "
333 "<filesystem|snapshot>\n"));
334 case HELP_GROUPSPACE:
335 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
337 "\t [-S field] ... [-t type[,...]] "
338 "<filesystem|snapshot>\n"));
340 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
342 return (gettext("\tholds [-Hp] [-r|-d depth] "
343 "<filesystem|volume|snapshot> ...\n"));
345 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
347 return (gettext("\tdiff [-FHt] <snapshot> "
348 "[snapshot|filesystem]\n"));
350 return (gettext("\tremap <filesystem | volume>\n"));
352 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
353 case HELP_CHANNEL_PROGRAM:
354 return (gettext("\tprogram [-n] [-t <instruction limit>] "
355 "[-m <memory limit (b)>] <pool> <program file> "
366 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
371 * Utility function to guarantee malloc() success.
375 safe_malloc(size_t size)
379 if ((data = calloc(1, size)) == NULL)
386 safe_realloc(void *data, size_t size)
389 if ((newp = realloc(data, size)) == NULL) {
398 safe_strdup(char *str)
400 char *dupstr = strdup(str);
409 * Callback routine that will print out information for each of
413 usage_prop_cb(int prop, void *cb)
417 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
419 if (zfs_prop_readonly(prop))
420 (void) fprintf(fp, " NO ");
422 (void) fprintf(fp, "YES ");
424 if (zfs_prop_inheritable(prop))
425 (void) fprintf(fp, " YES ");
427 (void) fprintf(fp, " NO ");
429 if (zfs_prop_values(prop) == NULL)
430 (void) fprintf(fp, "-\n");
432 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
438 * Display usage message. If we're inside a command, display only the usage for
439 * that command. Otherwise, iterate over the entire command table and display
440 * a complete usage message.
443 usage(boolean_t requested)
446 boolean_t show_properties = B_FALSE;
447 FILE *fp = requested ? stdout : stderr;
449 if (current_command == NULL) {
451 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
453 gettext("where 'command' is one of the following:\n\n"));
455 for (i = 0; i < NCOMMAND; i++) {
456 if (command_table[i].name == NULL)
457 (void) fprintf(fp, "\n");
459 (void) fprintf(fp, "%s",
460 get_usage(command_table[i].usage));
463 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
464 "pool/[dataset/]*dataset[@name]\n"));
466 (void) fprintf(fp, gettext("usage:\n"));
467 (void) fprintf(fp, "%s", get_usage(current_command->usage));
470 if (current_command != NULL &&
471 (strcmp(current_command->name, "set") == 0 ||
472 strcmp(current_command->name, "get") == 0 ||
473 strcmp(current_command->name, "inherit") == 0 ||
474 strcmp(current_command->name, "list") == 0))
475 show_properties = B_TRUE;
477 if (show_properties) {
479 gettext("\nThe following properties are supported:\n"));
481 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
482 "PROPERTY", "EDIT", "INHERIT", "VALUES");
484 /* Iterate over all properties */
485 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
488 (void) fprintf(fp, "\t%-15s ", "userused@...");
489 (void) fprintf(fp, " NO NO <size>\n");
490 (void) fprintf(fp, "\t%-15s ", "groupused@...");
491 (void) fprintf(fp, " NO NO <size>\n");
492 (void) fprintf(fp, "\t%-15s ", "userquota@...");
493 (void) fprintf(fp, "YES NO <size> | none\n");
494 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
495 (void) fprintf(fp, "YES NO <size> | none\n");
496 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
497 (void) fprintf(fp, " NO NO <size>\n");
499 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
500 "with standard units such as K, M, G, etc.\n"));
501 (void) fprintf(fp, gettext("\nUser-defined properties can "
502 "be specified by using a name containing a colon (:).\n"));
503 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
504 "properties must be appended with\n"
505 "a user or group specifier of one of these forms:\n"
506 " POSIX name (eg: \"matt\")\n"
507 " POSIX id (eg: \"126829\")\n"
508 " SMB name@domain (eg: \"matt@sun\")\n"
509 " SMB SID (eg: \"S-1-234-567-89\")\n"));
512 gettext("\nFor the property list, run: %s\n"),
515 gettext("\nFor the delegated permission list, run: %s\n"),
516 "zfs allow|unallow");
520 * See comments at end of main().
522 if (getenv("ZFS_ABORT") != NULL) {
523 (void) printf("dumping core by request\n");
527 exit(requested ? 0 : 2);
531 * Take a property=value argument string and add it to the given nvlist.
532 * Modifies the argument inplace.
535 parseprop(nvlist_t *props, char *propname)
537 char *propval, *strval;
539 if ((propval = strchr(propname, '=')) == NULL) {
540 (void) fprintf(stderr, gettext("missing "
541 "'=' for property=value argument\n"));
546 if (nvlist_lookup_string(props, propname, &strval) == 0) {
547 (void) fprintf(stderr, gettext("property '%s' "
548 "specified multiple times\n"), propname);
551 if (nvlist_add_string(props, propname, propval) != 0)
557 parse_depth(char *opt, int *flags)
562 depth = (int)strtol(opt, &tmp, 0);
564 (void) fprintf(stderr,
565 gettext("%s is not an integer\n"), opt);
569 (void) fprintf(stderr,
570 gettext("Depth can not be negative.\n"));
573 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
577 #define PROGRESS_DELAY 2 /* seconds */
579 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";
580 static time_t pt_begin;
581 static char *pt_header = NULL;
582 static boolean_t pt_shown;
585 start_progress_timer(void)
587 pt_begin = time(NULL) + PROGRESS_DELAY;
592 set_progress_header(char *header)
594 assert(pt_header == NULL);
595 pt_header = safe_strdup(header);
597 (void) printf("%s: ", header);
598 (void) fflush(stdout);
603 update_progress(char *update)
605 if (!pt_shown && time(NULL) > pt_begin) {
606 int len = strlen(update);
608 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
610 (void) fflush(stdout);
612 } else if (pt_shown) {
613 int len = strlen(update);
615 (void) printf("%s%*.*s", update, len, len, pt_reverse);
616 (void) fflush(stdout);
621 finish_progress(char *done)
624 (void) printf("%s\n", done);
625 (void) fflush(stdout);
632 * Check if the dataset is mountable and should be automatically mounted.
635 should_auto_mount(zfs_handle_t *zhp)
637 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp)))
639 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON);
643 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
645 * Given an existing dataset, create a writable copy whose initial contents
646 * are the same as the source. The newly created dataset maintains a
647 * dependency on the original; the original cannot be destroyed so long as
650 * The '-p' flag creates all the non-existing ancestors of the target first.
653 zfs_do_clone(int argc, char **argv)
655 zfs_handle_t *zhp = NULL;
656 boolean_t parents = B_FALSE;
661 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
665 while ((c = getopt(argc, argv, "o:p")) != -1) {
668 if (parseprop(props, optarg) != 0)
675 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
684 /* check number of arguments */
686 (void) fprintf(stderr, gettext("missing source dataset "
691 (void) fprintf(stderr, gettext("missing target dataset "
696 (void) fprintf(stderr, gettext("too many arguments\n"));
700 /* open the source dataset */
701 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
704 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
707 * Now create the ancestors of the target dataset. If the
708 * target already exists and '-p' option was used we should not
711 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
714 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
719 ret = zfs_clone(zhp, argv[1], props);
721 /* create the mountpoint if necessary */
725 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
728 * If the user doesn't want the dataset
729 * automatically mounted, then skip the mount/share
732 if (should_auto_mount(clone)) {
733 if ((ret = zfs_mount(clone, NULL, 0)) != 0) {
734 (void) fprintf(stderr, gettext("clone "
735 "successfully created, "
736 "but not mounted\n"));
737 } else if ((ret = zfs_share(clone)) != 0) {
738 (void) fprintf(stderr, gettext("clone "
739 "successfully created, "
740 "but not shared\n"));
761 * zfs create [-pu] [-o prop=value] ... fs
762 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
764 * Create a new dataset. This command can be used to create filesystems
765 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
766 * For volumes, the user must specify a size to be used.
768 * The '-s' flag applies only to volumes, and indicates that we should not try
769 * to set the reservation for this volume. By default we set a reservation
770 * equal to the size for any volume. For pools with SPA_VERSION >=
771 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
773 * The '-p' flag creates all the non-existing ancestors of the target first.
775 * The '-u' flag prevents mounting of newly created file system.
778 zfs_do_create(int argc, char **argv)
780 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
781 zfs_handle_t *zhp = NULL;
782 uint64_t volsize = 0;
784 boolean_t noreserve = B_FALSE;
785 boolean_t bflag = B_FALSE;
786 boolean_t parents = B_FALSE;
787 boolean_t nomount = B_FALSE;
792 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
796 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) {
799 type = ZFS_TYPE_VOLUME;
800 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
801 (void) fprintf(stderr, gettext("bad volume "
802 "size '%s': %s\n"), optarg,
803 libzfs_error_description(g_zfs));
807 if (nvlist_add_uint64(props,
808 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
817 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
818 (void) fprintf(stderr, gettext("bad volume "
819 "block size '%s': %s\n"), optarg,
820 libzfs_error_description(g_zfs));
824 if (nvlist_add_uint64(props,
825 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
830 if (parseprop(props, optarg) != 0)
840 (void) fprintf(stderr, gettext("missing size "
844 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
850 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
851 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
852 "used when creating a volume\n"));
855 if (nomount && type != ZFS_TYPE_FILESYSTEM) {
856 (void) fprintf(stderr, gettext("'-u' can only be "
857 "used when creating a file system\n"));
864 /* check number of arguments */
866 (void) fprintf(stderr, gettext("missing %s argument\n"),
867 zfs_type_to_name(type));
871 (void) fprintf(stderr, gettext("too many arguments\n"));
875 if (type == ZFS_TYPE_VOLUME && !noreserve) {
876 zpool_handle_t *zpool_handle;
877 nvlist_t *real_props = NULL;
878 uint64_t spa_version;
880 zfs_prop_t resv_prop;
884 if ((p = strchr(argv[0], '/')) != NULL)
886 zpool_handle = zpool_open(g_zfs, argv[0]);
889 if (zpool_handle == NULL)
891 spa_version = zpool_get_prop_int(zpool_handle,
892 ZPOOL_PROP_VERSION, NULL);
893 if (spa_version >= SPA_VERSION_REFRESERVATION)
894 resv_prop = ZFS_PROP_REFRESERVATION;
896 resv_prop = ZFS_PROP_RESERVATION;
898 (void) snprintf(msg, sizeof (msg),
899 gettext("cannot create '%s'"), argv[0]);
900 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
901 props, 0, NULL, zpool_handle, msg)) == NULL) {
902 zpool_close(zpool_handle);
905 zpool_close(zpool_handle);
907 volsize = zvol_volsize_to_reservation(volsize, real_props);
908 nvlist_free(real_props);
910 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
912 if (nvlist_add_uint64(props,
913 zfs_prop_to_name(resv_prop), volsize) != 0) {
920 if (parents && zfs_name_valid(argv[0], type)) {
922 * Now create the ancestors of target dataset. If the target
923 * already exists and '-p' option was used we should not
926 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
930 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
935 if (zfs_create(g_zfs, argv[0], type, props) != 0)
938 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
944 * Mount and/or share the new filesystem as appropriate. We provide a
945 * verbose error message to let the user know that their filesystem was
946 * in fact created, even if we failed to mount or share it.
947 * If the user doesn't want the dataset automatically mounted,
948 * then skip the mount/share step altogether.
950 if (!nomount && should_auto_mount(zhp)) {
951 if (zfs_mount(zhp, NULL, 0) != 0) {
952 (void) fprintf(stderr, gettext("filesystem "
953 "successfully created, but not mounted\n"));
955 } else if (zfs_share(zhp) != 0) {
956 (void) fprintf(stderr, gettext("filesystem "
957 "successfully created, but not shared\n"));
974 * zfs destroy [-rRf] <fs, vol>
975 * zfs destroy [-rRd] <snap>
977 * -r Recursively destroy all children
978 * -R Recursively destroy all dependents, including clones
979 * -f Force unmounting of any dependents
980 * -d If we can't destroy now, mark for deferred destruction
982 * Destroys the given dataset. By default, it will unmount any filesystems,
983 * and refuse to destroy a dataset that has any dependents. A dependent can
984 * either be a child, or a clone of a child.
986 typedef struct destroy_cbdata {
989 boolean_t cb_recurse;
991 boolean_t cb_doclones;
992 zfs_handle_t *cb_target;
993 boolean_t cb_defer_destroy;
994 boolean_t cb_verbose;
995 boolean_t cb_parsable;
998 nvlist_t *cb_batchedsnaps;
1000 /* first snap in contiguous run */
1002 /* previous snap in contiguous run */
1004 int64_t cb_snapused;
1010 * Check for any dependents based on the '-r' or '-R' flags.
1013 destroy_check_dependent(zfs_handle_t *zhp, void *data)
1015 destroy_cbdata_t *cbp = data;
1016 const char *tname = zfs_get_name(cbp->cb_target);
1017 const char *name = zfs_get_name(zhp);
1019 if (strncmp(tname, name, strlen(tname)) == 0 &&
1020 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
1022 * This is a direct descendant, not a clone somewhere else in
1025 if (cbp->cb_recurse)
1028 if (cbp->cb_first) {
1029 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1030 "%s has children\n"),
1031 zfs_get_name(cbp->cb_target),
1032 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1033 (void) fprintf(stderr, gettext("use '-r' to destroy "
1034 "the following datasets:\n"));
1035 cbp->cb_first = B_FALSE;
1036 cbp->cb_error = B_TRUE;
1039 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1042 * This is a clone. We only want to report this if the '-r'
1043 * wasn't specified, or the target is a snapshot.
1045 if (!cbp->cb_recurse &&
1046 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1049 if (cbp->cb_first) {
1050 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1051 "%s has dependent clones\n"),
1052 zfs_get_name(cbp->cb_target),
1053 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1054 (void) fprintf(stderr, gettext("use '-R' to destroy "
1055 "the following datasets:\n"));
1056 cbp->cb_first = B_FALSE;
1057 cbp->cb_error = B_TRUE;
1058 cbp->cb_dryrun = B_TRUE;
1061 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1070 destroy_callback(zfs_handle_t *zhp, void *data)
1072 destroy_cbdata_t *cb = data;
1073 const char *name = zfs_get_name(zhp);
1075 if (cb->cb_verbose) {
1076 if (cb->cb_parsable) {
1077 (void) printf("destroy\t%s\n", name);
1078 } else if (cb->cb_dryrun) {
1079 (void) printf(gettext("would destroy %s\n"),
1082 (void) printf(gettext("will destroy %s\n"),
1088 * Ignore pools (which we've already flagged as an error before getting
1091 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1092 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1096 if (cb->cb_dryrun) {
1102 * We batch up all contiguous snapshots (even of different
1103 * filesystems) and destroy them with one ioctl. We can't
1104 * simply do all snap deletions and then all fs deletions,
1105 * because we must delete a clone before its origin.
1107 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1108 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1110 int error = zfs_destroy_snaps_nvl(g_zfs,
1111 cb->cb_batchedsnaps, B_FALSE);
1112 fnvlist_free(cb->cb_batchedsnaps);
1113 cb->cb_batchedsnaps = fnvlist_alloc();
1116 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1117 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1128 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1130 destroy_cbdata_t *cb = arg;
1131 const char *name = zfs_get_name(zhp);
1134 if (nvlist_exists(cb->cb_nvl, name)) {
1135 if (cb->cb_firstsnap == NULL)
1136 cb->cb_firstsnap = strdup(name);
1137 if (cb->cb_prevsnap != NULL)
1138 free(cb->cb_prevsnap);
1139 /* this snap continues the current range */
1140 cb->cb_prevsnap = strdup(name);
1141 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1143 if (cb->cb_verbose) {
1144 if (cb->cb_parsable) {
1145 (void) printf("destroy\t%s\n", name);
1146 } else if (cb->cb_dryrun) {
1147 (void) printf(gettext("would destroy %s\n"),
1150 (void) printf(gettext("will destroy %s\n"),
1154 } else if (cb->cb_firstsnap != NULL) {
1155 /* end of this range */
1157 err = lzc_snaprange_space(cb->cb_firstsnap,
1158 cb->cb_prevsnap, &used);
1159 cb->cb_snapused += used;
1160 free(cb->cb_firstsnap);
1161 cb->cb_firstsnap = NULL;
1162 free(cb->cb_prevsnap);
1163 cb->cb_prevsnap = NULL;
1170 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1173 assert(cb->cb_firstsnap == NULL);
1174 assert(cb->cb_prevsnap == NULL);
1175 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1176 if (cb->cb_firstsnap != NULL) {
1179 err = lzc_snaprange_space(cb->cb_firstsnap,
1180 cb->cb_prevsnap, &used);
1182 cb->cb_snapused += used;
1183 free(cb->cb_firstsnap);
1184 cb->cb_firstsnap = NULL;
1185 free(cb->cb_prevsnap);
1186 cb->cb_prevsnap = NULL;
1192 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1194 destroy_cbdata_t *cb = arg;
1197 /* Check for clones. */
1198 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1199 cb->cb_target = zhp;
1200 cb->cb_first = B_TRUE;
1201 err = zfs_iter_dependents(zhp, B_TRUE,
1202 destroy_check_dependent, cb);
1206 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1214 gather_snapshots(zfs_handle_t *zhp, void *arg)
1216 destroy_cbdata_t *cb = arg;
1219 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1225 if (cb->cb_verbose) {
1226 err = destroy_print_snapshots(zhp, cb);
1232 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1240 destroy_clones(destroy_cbdata_t *cb)
1243 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1245 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1246 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1249 boolean_t defer = cb->cb_defer_destroy;
1253 * We can't defer destroy non-snapshots, so set it to
1254 * false while destroying the clones.
1256 cb->cb_defer_destroy = B_FALSE;
1257 err = zfs_iter_dependents(zhp, B_FALSE,
1258 destroy_callback, cb);
1259 cb->cb_defer_destroy = defer;
1269 zfs_do_destroy(int argc, char **argv)
1271 destroy_cbdata_t cb = { 0 };
1275 zfs_handle_t *zhp = NULL;
1277 zfs_type_t type = ZFS_TYPE_DATASET;
1280 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1283 cb.cb_verbose = B_TRUE;
1286 cb.cb_verbose = B_TRUE;
1287 cb.cb_parsable = B_TRUE;
1290 cb.cb_dryrun = B_TRUE;
1293 cb.cb_defer_destroy = B_TRUE;
1294 type = ZFS_TYPE_SNAPSHOT;
1297 cb.cb_force = B_TRUE;
1300 cb.cb_recurse = B_TRUE;
1303 cb.cb_recurse = B_TRUE;
1304 cb.cb_doclones = B_TRUE;
1308 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1317 /* check number of arguments */
1319 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1323 (void) fprintf(stderr, gettext("too many arguments\n"));
1327 at = strchr(argv[0], '@');
1328 pound = strchr(argv[0], '#');
1331 /* Build the list of snaps to destroy in cb_nvl. */
1332 cb.cb_nvl = fnvlist_alloc();
1335 zhp = zfs_open(g_zfs, argv[0],
1336 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1340 cb.cb_snapspec = at + 1;
1341 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1347 if (nvlist_empty(cb.cb_nvl)) {
1348 (void) fprintf(stderr, gettext("could not find any "
1349 "snapshots to destroy; check snapshot names.\n"));
1354 if (cb.cb_verbose) {
1356 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1357 if (cb.cb_parsable) {
1358 (void) printf("reclaim\t%llu\n",
1360 } else if (cb.cb_dryrun) {
1361 (void) printf(gettext("would reclaim %s\n"),
1364 (void) printf(gettext("will reclaim %s\n"),
1369 if (!cb.cb_dryrun) {
1370 if (cb.cb_doclones) {
1371 cb.cb_batchedsnaps = fnvlist_alloc();
1372 err = destroy_clones(&cb);
1374 err = zfs_destroy_snaps_nvl(g_zfs,
1375 cb.cb_batchedsnaps, B_FALSE);
1383 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1384 cb.cb_defer_destroy);
1390 } else if (pound != NULL) {
1395 (void) fprintf(stderr,
1396 "dryrun is not supported with bookmark\n");
1400 if (cb.cb_defer_destroy) {
1401 (void) fprintf(stderr,
1402 "defer destroy is not supported with bookmark\n");
1406 if (cb.cb_recurse) {
1407 (void) fprintf(stderr,
1408 "recursive is not supported with bookmark\n");
1412 if (!zfs_bookmark_exists(argv[0])) {
1413 (void) fprintf(stderr, gettext("bookmark '%s' "
1414 "does not exist.\n"), argv[0]);
1418 nvl = fnvlist_alloc();
1419 fnvlist_add_boolean(nvl, argv[0]);
1421 err = lzc_destroy_bookmarks(nvl, NULL);
1423 (void) zfs_standard_error(g_zfs, err,
1424 "cannot destroy bookmark");
1427 nvlist_free(cb.cb_nvl);
1431 /* Open the given dataset */
1432 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1438 * Perform an explicit check for pools before going any further.
1440 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1441 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1442 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1443 "operation does not apply to pools\n"),
1445 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1446 "%s' to destroy all datasets in the pool\n"),
1448 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1449 "to destroy the pool itself\n"), zfs_get_name(zhp));
1455 * Check for any dependents and/or clones.
1457 cb.cb_first = B_TRUE;
1458 if (!cb.cb_doclones &&
1459 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1470 cb.cb_batchedsnaps = fnvlist_alloc();
1471 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1478 * Do the real thing. The callback will close the
1479 * handle regardless of whether it succeeds or not.
1481 err = destroy_callback(zhp, &cb);
1484 err = zfs_destroy_snaps_nvl(g_zfs,
1485 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1492 fnvlist_free(cb.cb_batchedsnaps);
1493 fnvlist_free(cb.cb_nvl);
1500 is_recvd_column(zprop_get_cbdata_t *cbp)
1503 zfs_get_column_t col;
1505 for (i = 0; i < ZFS_GET_NCOLS &&
1506 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1507 if (col == GET_COL_RECVD)
1513 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1514 * < all | property[,property]... > < fs | snap | vol > ...
1516 * -r recurse over any child datasets
1517 * -H scripted mode. Headers are stripped, and fields are separated
1518 * by tabs instead of spaces.
1519 * -o Set of fields to display. One of "name,property,value,
1520 * received,source". Default is "name,property,value,source".
1521 * "all" is an alias for all five.
1522 * -s Set of sources to allow. One of
1523 * "local,default,inherited,received,temporary,none". Default is
1525 * -p Display values in parsable (literal) format.
1527 * Prints properties for the given datasets. The user can control which
1528 * columns to display as well as which property types to allow.
1532 * Invoked to display the properties for a single dataset.
1535 get_callback(zfs_handle_t *zhp, void *data)
1537 char buf[ZFS_MAXPROPLEN];
1538 char rbuf[ZFS_MAXPROPLEN];
1539 zprop_source_t sourcetype;
1540 char source[ZFS_MAX_DATASET_NAME_LEN];
1541 zprop_get_cbdata_t *cbp = data;
1542 nvlist_t *user_props = zfs_get_user_props(zhp);
1543 zprop_list_t *pl = cbp->cb_proplist;
1547 boolean_t received = is_recvd_column(cbp);
1549 for (; pl != NULL; pl = pl->pl_next) {
1550 char *recvdval = NULL;
1552 * Skip the special fake placeholder. This will also skip over
1553 * the name property when 'all' is specified.
1555 if (pl->pl_prop == ZFS_PROP_NAME &&
1556 pl == cbp->cb_proplist)
1559 if (pl->pl_prop != ZPROP_INVAL) {
1560 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1561 sizeof (buf), &sourcetype, source,
1563 cbp->cb_literal) != 0) {
1566 if (!zfs_prop_valid_for_type(pl->pl_prop,
1567 ZFS_TYPE_DATASET)) {
1568 (void) fprintf(stderr,
1569 gettext("No such property '%s'\n"),
1570 zfs_prop_to_name(pl->pl_prop));
1573 sourcetype = ZPROP_SRC_NONE;
1574 (void) strlcpy(buf, "-", sizeof (buf));
1577 if (received && (zfs_prop_get_recvd(zhp,
1578 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1579 cbp->cb_literal) == 0))
1582 zprop_print_one_property(zfs_get_name(zhp), cbp,
1583 zfs_prop_to_name(pl->pl_prop),
1584 buf, sourcetype, source, recvdval);
1585 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1586 sourcetype = ZPROP_SRC_LOCAL;
1588 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1589 buf, sizeof (buf), cbp->cb_literal) != 0) {
1590 sourcetype = ZPROP_SRC_NONE;
1591 (void) strlcpy(buf, "-", sizeof (buf));
1594 zprop_print_one_property(zfs_get_name(zhp), cbp,
1595 pl->pl_user_prop, buf, sourcetype, source, NULL);
1596 } else if (zfs_prop_written(pl->pl_user_prop)) {
1597 sourcetype = ZPROP_SRC_LOCAL;
1599 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1600 buf, sizeof (buf), cbp->cb_literal) != 0) {
1601 sourcetype = ZPROP_SRC_NONE;
1602 (void) strlcpy(buf, "-", sizeof (buf));
1605 zprop_print_one_property(zfs_get_name(zhp), cbp,
1606 pl->pl_user_prop, buf, sourcetype, source, NULL);
1608 if (nvlist_lookup_nvlist(user_props,
1609 pl->pl_user_prop, &propval) != 0) {
1612 sourcetype = ZPROP_SRC_NONE;
1615 verify(nvlist_lookup_string(propval,
1616 ZPROP_VALUE, &strval) == 0);
1617 verify(nvlist_lookup_string(propval,
1618 ZPROP_SOURCE, &sourceval) == 0);
1620 if (strcmp(sourceval,
1621 zfs_get_name(zhp)) == 0) {
1622 sourcetype = ZPROP_SRC_LOCAL;
1623 } else if (strcmp(sourceval,
1624 ZPROP_SOURCE_VAL_RECVD) == 0) {
1625 sourcetype = ZPROP_SRC_RECEIVED;
1627 sourcetype = ZPROP_SRC_INHERITED;
1628 (void) strlcpy(source,
1629 sourceval, sizeof (source));
1633 if (received && (zfs_prop_get_recvd(zhp,
1634 pl->pl_user_prop, rbuf, sizeof (rbuf),
1635 cbp->cb_literal) == 0))
1638 zprop_print_one_property(zfs_get_name(zhp), cbp,
1639 pl->pl_user_prop, strval, sourcetype,
1648 zfs_do_get(int argc, char **argv)
1650 zprop_get_cbdata_t cb = { 0 };
1651 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1652 int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
1653 char *value, *fields;
1656 zprop_list_t fake_name = { 0 };
1659 * Set up default columns and sources.
1661 cb.cb_sources = ZPROP_SRC_ALL;
1662 cb.cb_columns[0] = GET_COL_NAME;
1663 cb.cb_columns[1] = GET_COL_PROPERTY;
1664 cb.cb_columns[2] = GET_COL_VALUE;
1665 cb.cb_columns[3] = GET_COL_SOURCE;
1666 cb.cb_type = ZFS_TYPE_DATASET;
1669 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1672 cb.cb_literal = B_TRUE;
1675 limit = parse_depth(optarg, &flags);
1678 flags |= ZFS_ITER_RECURSE;
1681 cb.cb_scripted = B_TRUE;
1684 (void) fprintf(stderr, gettext("missing argument for "
1685 "'%c' option\n"), optopt);
1690 * Process the set of columns to display. We zero out
1691 * the structure to give us a blank slate.
1693 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1695 while (*optarg != '\0') {
1696 static char *col_subopts[] =
1697 { "name", "property", "value", "received",
1698 "source", "all", NULL };
1700 if (i == ZFS_GET_NCOLS) {
1701 (void) fprintf(stderr, gettext("too "
1702 "many fields given to -o "
1707 switch (getsubopt(&optarg, col_subopts,
1710 cb.cb_columns[i++] = GET_COL_NAME;
1713 cb.cb_columns[i++] = GET_COL_PROPERTY;
1716 cb.cb_columns[i++] = GET_COL_VALUE;
1719 cb.cb_columns[i++] = GET_COL_RECVD;
1720 flags |= ZFS_ITER_RECVD_PROPS;
1723 cb.cb_columns[i++] = GET_COL_SOURCE;
1727 (void) fprintf(stderr,
1728 gettext("\"all\" conflicts "
1729 "with specific fields "
1730 "given to -o option\n"));
1733 cb.cb_columns[0] = GET_COL_NAME;
1734 cb.cb_columns[1] = GET_COL_PROPERTY;
1735 cb.cb_columns[2] = GET_COL_VALUE;
1736 cb.cb_columns[3] = GET_COL_RECVD;
1737 cb.cb_columns[4] = GET_COL_SOURCE;
1738 flags |= ZFS_ITER_RECVD_PROPS;
1742 (void) fprintf(stderr,
1743 gettext("invalid column name "
1744 "'%s'\n"), suboptarg);
1752 while (*optarg != '\0') {
1753 static char *source_subopts[] = {
1754 "local", "default", "inherited",
1755 "received", "temporary", "none",
1758 switch (getsubopt(&optarg, source_subopts,
1761 cb.cb_sources |= ZPROP_SRC_LOCAL;
1764 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1767 cb.cb_sources |= ZPROP_SRC_INHERITED;
1770 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1773 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1776 cb.cb_sources |= ZPROP_SRC_NONE;
1779 (void) fprintf(stderr,
1780 gettext("invalid source "
1781 "'%s'\n"), suboptarg);
1789 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1790 while (*optarg != '\0') {
1791 static char *type_subopts[] = { "filesystem",
1792 "volume", "snapshot", "bookmark",
1795 switch (getsubopt(&optarg, type_subopts,
1798 types |= ZFS_TYPE_FILESYSTEM;
1801 types |= ZFS_TYPE_VOLUME;
1804 types |= ZFS_TYPE_SNAPSHOT;
1807 types |= ZFS_TYPE_BOOKMARK;
1810 types = ZFS_TYPE_DATASET |
1815 (void) fprintf(stderr,
1816 gettext("invalid type '%s'\n"),
1824 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1834 (void) fprintf(stderr, gettext("missing property "
1841 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1849 * As part of zfs_expand_proplist(), we keep track of the maximum column
1850 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1851 * need to know the maximum name length. However, the user likely did
1852 * not specify 'name' as one of the properties to fetch, so we need to
1853 * make sure we always include at least this property for
1854 * print_get_headers() to work properly.
1856 if (cb.cb_proplist != NULL) {
1857 fake_name.pl_prop = ZFS_PROP_NAME;
1858 fake_name.pl_width = strlen(gettext("NAME"));
1859 fake_name.pl_next = cb.cb_proplist;
1860 cb.cb_proplist = &fake_name;
1863 cb.cb_first = B_TRUE;
1865 /* run for each object */
1866 ret = zfs_for_each(argc, argv, flags, types, NULL,
1867 &cb.cb_proplist, limit, get_callback, &cb);
1869 if (cb.cb_proplist == &fake_name)
1870 zprop_free_list(fake_name.pl_next);
1872 zprop_free_list(cb.cb_proplist);
1878 * inherit [-rS] <property> <fs|vol> ...
1880 * -r Recurse over all children
1881 * -S Revert to received value, if any
1883 * For each dataset specified on the command line, inherit the given property
1884 * from its parent. Inheriting a property at the pool level will cause it to
1885 * use the default value. The '-r' flag will recurse over all children, and is
1886 * useful for setting a property on a hierarchy-wide basis, regardless of any
1887 * local modifications for each dataset.
1890 typedef struct inherit_cbdata {
1891 const char *cb_propname;
1892 boolean_t cb_received;
1896 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1898 inherit_cbdata_t *cb = data;
1899 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1902 * If we're doing it recursively, then ignore properties that
1903 * are not valid for this type of dataset.
1905 if (prop != ZPROP_INVAL &&
1906 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1909 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1913 inherit_cb(zfs_handle_t *zhp, void *data)
1915 inherit_cbdata_t *cb = data;
1917 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1921 zfs_do_inherit(int argc, char **argv)
1925 inherit_cbdata_t cb = { 0 };
1929 boolean_t received = B_FALSE;
1932 while ((c = getopt(argc, argv, "rS")) != -1) {
1935 flags |= ZFS_ITER_RECURSE;
1942 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1951 /* check number of arguments */
1953 (void) fprintf(stderr, gettext("missing property argument\n"));
1957 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1965 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1966 if (zfs_prop_readonly(prop)) {
1967 (void) fprintf(stderr, gettext(
1968 "%s property is read-only\n"),
1972 if (!zfs_prop_inheritable(prop) && !received) {
1973 (void) fprintf(stderr, gettext("'%s' property cannot "
1974 "be inherited\n"), propname);
1975 if (prop == ZFS_PROP_QUOTA ||
1976 prop == ZFS_PROP_RESERVATION ||
1977 prop == ZFS_PROP_REFQUOTA ||
1978 prop == ZFS_PROP_REFRESERVATION) {
1979 (void) fprintf(stderr, gettext("use 'zfs set "
1980 "%s=none' to clear\n"), propname);
1981 (void) fprintf(stderr, gettext("use 'zfs "
1982 "inherit -S %s' to revert to received "
1983 "value\n"), propname);
1987 if (received && (prop == ZFS_PROP_VOLSIZE ||
1988 prop == ZFS_PROP_VERSION)) {
1989 (void) fprintf(stderr, gettext("'%s' property cannot "
1990 "be reverted to a received value\n"), propname);
1993 } else if (!zfs_prop_user(propname)) {
1994 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1999 cb.cb_propname = propname;
2000 cb.cb_received = received;
2002 if (flags & ZFS_ITER_RECURSE) {
2003 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2004 NULL, NULL, 0, inherit_recurse_cb, &cb);
2006 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2007 NULL, NULL, 0, inherit_cb, &cb);
2013 typedef struct upgrade_cbdata {
2014 uint64_t cb_numupgraded;
2015 uint64_t cb_numsamegraded;
2016 uint64_t cb_numfailed;
2017 uint64_t cb_version;
2019 boolean_t cb_foundone;
2020 char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
2024 same_pool(zfs_handle_t *zhp, const char *name)
2026 int len1 = strcspn(name, "/@");
2027 const char *zhname = zfs_get_name(zhp);
2028 int len2 = strcspn(zhname, "/@");
2032 return (strncmp(name, zhname, len1) == 0);
2036 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2038 upgrade_cbdata_t *cb = data;
2039 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2041 /* list if it's old/new */
2042 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2043 (cb->cb_newer && version > ZPL_VERSION)) {
2046 str = gettext("The following filesystems are "
2047 "formatted using a newer software version and\n"
2048 "cannot be accessed on the current system.\n\n");
2050 str = gettext("The following filesystems are "
2051 "out of date, and can be upgraded. After being\n"
2052 "upgraded, these filesystems (and any 'zfs send' "
2053 "streams generated from\n"
2054 "subsequent snapshots) will no longer be "
2055 "accessible by older software versions.\n\n");
2058 if (!cb->cb_foundone) {
2060 (void) printf(gettext("VER FILESYSTEM\n"));
2061 (void) printf(gettext("--- ------------\n"));
2062 cb->cb_foundone = B_TRUE;
2065 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2072 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2074 upgrade_cbdata_t *cb = data;
2075 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2076 int needed_spa_version;
2079 if (zfs_spa_version(zhp, &spa_version) < 0)
2082 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2084 if (needed_spa_version < 0)
2087 if (spa_version < needed_spa_version) {
2089 (void) printf(gettext("%s: can not be "
2090 "upgraded; the pool version needs to first "
2091 "be upgraded\nto version %d\n\n"),
2092 zfs_get_name(zhp), needed_spa_version);
2098 if (version < cb->cb_version) {
2100 (void) snprintf(verstr, sizeof (verstr),
2101 "%llu", cb->cb_version);
2102 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2104 * If they did "zfs upgrade -a", then we could
2105 * be doing ioctls to different pools. We need
2106 * to log this history once to each pool, and bypass
2107 * the normal history logging that happens in main().
2109 (void) zpool_log_history(g_zfs, history_str);
2110 log_history = B_FALSE;
2112 if (zfs_prop_set(zhp, "version", verstr) == 0)
2113 cb->cb_numupgraded++;
2116 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2117 } else if (version > cb->cb_version) {
2118 /* can't downgrade */
2119 (void) printf(gettext("%s: can not be downgraded; "
2120 "it is already at version %u\n"),
2121 zfs_get_name(zhp), version);
2124 cb->cb_numsamegraded++;
2132 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2135 zfs_do_upgrade(int argc, char **argv)
2137 boolean_t all = B_FALSE;
2138 boolean_t showversions = B_FALSE;
2140 upgrade_cbdata_t cb = { 0 };
2142 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2145 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2148 flags |= ZFS_ITER_RECURSE;
2151 showversions = B_TRUE;
2154 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2155 optarg, &cb.cb_version) != 0) {
2156 (void) fprintf(stderr,
2157 gettext("invalid version %s\n"), optarg);
2166 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2175 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2177 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2178 cb.cb_version || argc))
2180 if ((all || argc) && (showversions))
2186 /* Show info on available versions. */
2187 (void) printf(gettext("The following filesystem versions are "
2189 (void) printf(gettext("VER DESCRIPTION\n"));
2190 (void) printf("--- -----------------------------------------"
2191 "---------------\n");
2192 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2193 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2194 (void) printf(gettext(" 3 Case insensitive and filesystem "
2195 "user identifier (FUID)\n"));
2196 (void) printf(gettext(" 4 userquota, groupquota "
2198 (void) printf(gettext(" 5 System attributes\n"));
2199 (void) printf(gettext("\nFor more information on a particular "
2200 "version, including supported releases,\n"));
2201 (void) printf("see the ZFS Administration Guide.\n\n");
2203 } else if (argc || all) {
2204 /* Upgrade filesystems */
2205 if (cb.cb_version == 0)
2206 cb.cb_version = ZPL_VERSION;
2207 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2208 NULL, NULL, 0, upgrade_set_callback, &cb);
2209 (void) printf(gettext("%llu filesystems upgraded\n"),
2211 if (cb.cb_numsamegraded) {
2212 (void) printf(gettext("%llu filesystems already at "
2214 cb.cb_numsamegraded);
2216 if (cb.cb_numfailed != 0)
2219 /* List old-version filesystems */
2221 (void) printf(gettext("This system is currently running "
2222 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2224 flags |= ZFS_ITER_RECURSE;
2225 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2226 NULL, NULL, 0, upgrade_list_callback, &cb);
2228 found = cb.cb_foundone;
2229 cb.cb_foundone = B_FALSE;
2230 cb.cb_newer = B_TRUE;
2232 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2233 NULL, NULL, 0, upgrade_list_callback, &cb);
2235 if (!cb.cb_foundone && !found) {
2236 (void) printf(gettext("All filesystems are "
2237 "formatted with the current version.\n"));
2245 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2246 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2247 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2248 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2250 * -H Scripted mode; elide headers and separate columns by tabs.
2251 * -i Translate SID to POSIX ID.
2252 * -n Print numeric ID instead of user/group name.
2253 * -o Control which fields to display.
2254 * -p Use exact (parsable) numeric output.
2255 * -s Specify sort columns, descending order.
2256 * -S Specify sort columns, ascending order.
2257 * -t Control which object types to display.
2259 * Displays space consumed by, and quotas on, each user in the specified
2260 * filesystem or snapshot.
2263 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2264 enum us_field_types {
2270 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2271 static char *us_field_names[] = { "type", "name", "used", "quota" };
2272 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2274 #define USTYPE_PSX_GRP (1 << 0)
2275 #define USTYPE_PSX_USR (1 << 1)
2276 #define USTYPE_SMB_GRP (1 << 2)
2277 #define USTYPE_SMB_USR (1 << 3)
2278 #define USTYPE_ALL \
2279 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2281 static int us_type_bits[] = {
2288 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2291 typedef struct us_node {
2293 uu_avl_node_t usn_avlnode;
2294 uu_list_node_t usn_listnode;
2297 typedef struct us_cbdata {
2299 uu_avl_pool_t *cb_avl_pool;
2301 boolean_t cb_numname;
2302 boolean_t cb_nicenum;
2303 boolean_t cb_sid2posix;
2304 zfs_userquota_prop_t cb_prop;
2305 zfs_sort_column_t *cb_sortcol;
2306 size_t cb_width[USFIELD_LAST];
2309 static boolean_t us_populated = B_FALSE;
2312 zfs_sort_column_t *si_sortcol;
2313 boolean_t si_numname;
2317 us_field_index(char *field)
2321 for (i = 0; i < USFIELD_LAST; i++) {
2322 if (strcmp(field, us_field_names[i]) == 0)
2330 us_compare(const void *larg, const void *rarg, void *unused)
2332 const us_node_t *l = larg;
2333 const us_node_t *r = rarg;
2334 us_sort_info_t *si = (us_sort_info_t *)unused;
2335 zfs_sort_column_t *sortcol = si->si_sortcol;
2336 boolean_t numname = si->si_numname;
2337 nvlist_t *lnvl = l->usn_nvl;
2338 nvlist_t *rnvl = r->usn_nvl;
2342 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2349 zfs_prop_t prop = sortcol->sc_prop;
2350 const char *propname = NULL;
2351 boolean_t reverse = sortcol->sc_reverse;
2356 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2357 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2359 rc = (rv32 < lv32) ? 1 : -1;
2365 (void) nvlist_lookup_uint64(lnvl, propname,
2367 (void) nvlist_lookup_uint64(rnvl, propname,
2370 rc = (rv64 < lv64) ? 1 : -1;
2372 if ((nvlist_lookup_string(lnvl, propname,
2373 &lvstr) == ENOENT) ||
2374 (nvlist_lookup_string(rnvl, propname,
2375 &rvstr) == ENOENT)) {
2378 rc = strcmp(lvstr, rvstr);
2382 case ZFS_PROP_QUOTA:
2385 if (prop == ZFS_PROP_USED)
2389 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2390 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2392 rc = (rv64 < lv64) ? 1 : -1;
2401 return (reverse ? 1 : -1);
2403 return (reverse ? -1 : 1);
2408 * If entries still seem to be the same, check if they are of the same
2409 * type (smbentity is added only if we are doing SID to POSIX ID
2410 * translation where we can have duplicate type/name combinations).
2412 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2413 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2415 return (lvb < rvb ? -1 : 1);
2420 static inline const char *
2421 us_type2str(unsigned field_type)
2423 switch (field_type) {
2424 case USTYPE_PSX_USR:
2425 return ("POSIX User");
2426 case USTYPE_PSX_GRP:
2427 return ("POSIX Group");
2428 case USTYPE_SMB_USR:
2429 return ("SMB User");
2430 case USTYPE_SMB_GRP:
2431 return ("SMB Group");
2433 return ("Undefined");
2438 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2440 us_cbdata_t *cb = (us_cbdata_t *)arg;
2441 zfs_userquota_prop_t prop = cb->cb_prop;
2446 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2447 uu_avl_t *avl = cb->cb_avl;
2451 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2453 const char *typestr;
2457 int typeidx, nameidx, sizeidx;
2458 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2459 boolean_t smbentity = B_FALSE;
2461 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2463 node = safe_malloc(sizeof (us_node_t));
2464 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2465 node->usn_nvl = props;
2467 if (domain != NULL && domain[0] != '\0') {
2469 char sid[MAXNAMELEN + 32];
2473 int flag = IDMAP_REQ_FLG_USE_CACHE;
2478 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2480 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2481 type = USTYPE_SMB_GRP;
2483 err = sid_to_id(sid, B_FALSE, &id);
2486 type = USTYPE_SMB_USR;
2488 err = sid_to_id(sid, B_TRUE, &id);
2495 if (!cb->cb_sid2posix) {
2496 if (type == USTYPE_SMB_USR) {
2497 (void) idmap_getwinnamebyuid(rid, flag,
2500 (void) idmap_getwinnamebygid(rid, flag,
2510 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2512 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2513 type = USTYPE_PSX_GRP;
2514 if (!cb->cb_numname) {
2517 if ((g = getgrgid(rid)) != NULL)
2521 type = USTYPE_PSX_USR;
2522 if (!cb->cb_numname) {
2525 if ((p = getpwuid(rid)) != NULL)
2532 * Make sure that the type/name combination is unique when doing
2533 * SID to POSIX ID translation (hence changing the type from SMB to
2536 if (cb->cb_sid2posix &&
2537 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2540 /* Calculate/update width of TYPE field */
2541 typestr = us_type2str(type);
2542 typelen = strlen(gettext(typestr));
2543 typeidx = us_field_index("type");
2544 if (typelen > cb->cb_width[typeidx])
2545 cb->cb_width[typeidx] = typelen;
2546 if (nvlist_add_uint32(props, "type", type) != 0)
2549 /* Calculate/update width of NAME field */
2550 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2551 if (nvlist_add_uint64(props, "name", rid) != 0)
2553 namelen = snprintf(NULL, 0, "%u", rid);
2555 if (nvlist_add_string(props, "name", name) != 0)
2557 namelen = strlen(name);
2559 nameidx = us_field_index("name");
2560 if (namelen > cb->cb_width[nameidx])
2561 cb->cb_width[nameidx] = namelen;
2564 * Check if this type/name combination is in the list and update it;
2565 * otherwise add new node to the list.
2567 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2568 uu_avl_insert(avl, node, idx);
2573 props = node->usn_nvl;
2576 /* Calculate/update width of USED/QUOTA fields */
2578 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2580 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2581 sizelen = strlen(sizebuf);
2582 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2584 if (!nvlist_exists(props, "quota"))
2585 (void) nvlist_add_uint64(props, "quota", 0);
2588 if (!nvlist_exists(props, "used"))
2589 (void) nvlist_add_uint64(props, "used", 0);
2591 sizeidx = us_field_index(propname);
2592 if (sizelen > cb->cb_width[sizeidx])
2593 cb->cb_width[sizeidx] = sizelen;
2595 if (nvlist_add_uint64(props, propname, space) != 0)
2602 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2603 size_t *width, us_node_t *node)
2605 nvlist_t *nvl = node->usn_nvl;
2606 char valstr[MAXNAMELEN];
2607 boolean_t first = B_TRUE;
2613 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2614 if (!(ustype & types))
2617 while ((field = fields[cfield]) != USFIELD_LAST) {
2618 nvpair_t *nvp = NULL;
2622 char *strval = NULL;
2624 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2625 if (strcmp(nvpair_name(nvp),
2626 us_field_names[field]) == 0)
2630 type = nvpair_type(nvp);
2632 case DATA_TYPE_UINT32:
2633 (void) nvpair_value_uint32(nvp, &val32);
2635 case DATA_TYPE_UINT64:
2636 (void) nvpair_value_uint64(nvp, &val64);
2638 case DATA_TYPE_STRING:
2639 (void) nvpair_value_string(nvp, &strval);
2642 (void) fprintf(stderr, "invalid data type\n");
2647 strval = (char *)us_type2str(val32);
2650 if (type == DATA_TYPE_UINT64) {
2651 (void) sprintf(valstr, "%llu", val64);
2657 if (type == DATA_TYPE_UINT64) {
2659 (void) sprintf(valstr, "%llu", val64);
2661 zfs_nicenum(val64, valstr,
2664 if (field == USFIELD_QUOTA &&
2665 strcmp(valstr, "0") == 0)
2675 (void) printf("\t");
2680 (void) printf("%s", strval);
2681 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2682 (void) printf("%-*s", width[field], strval);
2684 (void) printf("%*s", width[field], strval);
2690 (void) printf("\n");
2694 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2695 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2703 boolean_t first = B_TRUE;
2705 while ((field = fields[cfield]) != USFIELD_LAST) {
2706 col = gettext(us_field_hdr[field]);
2707 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2708 (void) printf(first ? "%-*s" : " %-*s",
2711 (void) printf(first ? "%*s" : " %*s",
2717 (void) printf("\n");
2720 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2721 print_us_node(scripted, parsable, fields, types, width, node);
2723 nvlist_free(node->usn_nvl);
2728 zfs_do_userspace(int argc, char **argv)
2731 zfs_userquota_prop_t p;
2733 uu_avl_pool_t *avl_pool;
2735 uu_avl_walk_t *walk;
2737 char deffields[] = "type,name,used,quota";
2738 char *ofield = NULL;
2739 char *tfield = NULL;
2743 boolean_t scripted = B_FALSE;
2744 boolean_t prtnum = B_FALSE;
2745 boolean_t parsable = B_FALSE;
2746 boolean_t sid2posix = B_FALSE;
2749 zfs_sort_column_t *sortcol = NULL;
2750 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2754 uu_list_pool_t *listpool;
2756 uu_avl_index_t idx = 0;
2757 uu_list_index_t idx2 = 0;
2762 if (strcmp(argv[0], "groupspace") == 0)
2763 /* Toggle default group types */
2764 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2766 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2782 if (zfs_add_sort_column(&sortcol, optarg,
2783 c == 's' ? B_FALSE : B_TRUE) != 0) {
2784 (void) fprintf(stderr,
2785 gettext("invalid field '%s'\n"), optarg);
2796 (void) fprintf(stderr, gettext("missing argument for "
2797 "'%c' option\n"), optopt);
2801 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2811 (void) fprintf(stderr, gettext("missing dataset name\n"));
2815 (void) fprintf(stderr, gettext("too many arguments\n"));
2819 /* Use default output fields if not specified using -o */
2823 if ((delim = strchr(ofield, ',')) != NULL)
2825 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2826 (void) fprintf(stderr, gettext("invalid type '%s' "
2827 "for -o option\n"), ofield);
2832 } while (delim != NULL);
2833 fields[cfield] = USFIELD_LAST;
2835 /* Override output types (-t option) */
2836 if (tfield != NULL) {
2840 boolean_t found = B_FALSE;
2842 if ((delim = strchr(tfield, ',')) != NULL)
2844 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2846 if (strcmp(tfield, us_type_names[i]) == 0) {
2848 types |= us_type_bits[i];
2853 (void) fprintf(stderr, gettext("invalid type "
2854 "'%s' for -t option\n"), tfield);
2859 } while (delim != NULL);
2862 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2865 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2866 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2868 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2871 /* Always add default sorting columns */
2872 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2873 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2875 cb.cb_sortcol = sortcol;
2876 cb.cb_numname = prtnum;
2877 cb.cb_nicenum = !parsable;
2878 cb.cb_avl_pool = avl_pool;
2879 cb.cb_avl = avl_tree;
2880 cb.cb_sid2posix = sid2posix;
2882 for (i = 0; i < USFIELD_LAST; i++)
2883 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2885 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2886 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2887 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2888 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2889 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2892 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2897 if ((node = uu_avl_first(avl_tree)) == NULL)
2900 us_populated = B_TRUE;
2902 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2903 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2904 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2905 uu_list_node_init(node, &node->usn_listnode, listpool);
2907 while (node != NULL) {
2909 node = uu_avl_next(avl_tree, node);
2910 uu_avl_remove(avl_tree, rmnode);
2911 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2912 uu_list_insert(list, rmnode, idx2);
2915 for (node = uu_list_first(list); node != NULL;
2916 node = uu_list_next(list, node)) {
2917 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2919 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2920 uu_avl_insert(avl_tree, node, idx);
2923 uu_list_destroy(list);
2924 uu_list_pool_destroy(listpool);
2926 /* Print and free node nvlist memory */
2927 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2930 zfs_free_sort_columns(sortcol);
2932 /* Clean up the AVL tree */
2933 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2936 while ((node = uu_avl_walk_next(walk)) != NULL) {
2937 uu_avl_remove(cb.cb_avl, node);
2941 uu_avl_walk_end(walk);
2942 uu_avl_destroy(avl_tree);
2943 uu_avl_pool_destroy(avl_pool);
2949 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2950 * [-t type[,...]] [filesystem|volume|snapshot] ...
2952 * -H Scripted mode; elide headers and separate columns by tabs.
2953 * -p Display values in parsable (literal) format.
2954 * -r Recurse over all children.
2955 * -d Limit recursion by depth.
2956 * -o Control which fields to display.
2957 * -s Specify sort columns, descending order.
2958 * -S Specify sort columns, ascending order.
2959 * -t Control which object types to display.
2961 * When given no arguments, list all filesystems in the system.
2962 * Otherwise, list the specified datasets, optionally recursing down them if
2963 * '-r' is specified.
2965 typedef struct list_cbdata {
2967 boolean_t cb_literal;
2968 boolean_t cb_scripted;
2969 zprop_list_t *cb_proplist;
2973 * Given a list of columns to display, output appropriate headers for each one.
2976 print_header(list_cbdata_t *cb)
2978 zprop_list_t *pl = cb->cb_proplist;
2979 char headerbuf[ZFS_MAXPROPLEN];
2982 boolean_t first = B_TRUE;
2983 boolean_t right_justify;
2985 for (; pl != NULL; pl = pl->pl_next) {
2992 right_justify = B_FALSE;
2993 if (pl->pl_prop != ZPROP_INVAL) {
2994 header = zfs_prop_column_name(pl->pl_prop);
2995 right_justify = zfs_prop_align_right(pl->pl_prop);
2997 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2998 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2999 headerbuf[i] = '\0';
3003 if (pl->pl_next == NULL && !right_justify)
3004 (void) printf("%s", header);
3005 else if (right_justify)
3006 (void) printf("%*s", pl->pl_width, header);
3008 (void) printf("%-*s", pl->pl_width, header);
3011 (void) printf("\n");
3015 * Given a dataset and a list of fields, print out all the properties according
3016 * to the described layout.
3019 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3021 zprop_list_t *pl = cb->cb_proplist;
3022 boolean_t first = B_TRUE;
3023 char property[ZFS_MAXPROPLEN];
3024 nvlist_t *userprops = zfs_get_user_props(zhp);
3027 boolean_t right_justify;
3029 for (; pl != NULL; pl = pl->pl_next) {
3031 if (cb->cb_scripted)
3032 (void) printf("\t");
3039 if (pl->pl_prop == ZFS_PROP_NAME) {
3040 (void) strlcpy(property, zfs_get_name(zhp),
3043 right_justify = zfs_prop_align_right(pl->pl_prop);
3044 } else if (pl->pl_prop != ZPROP_INVAL) {
3045 if (zfs_prop_get(zhp, pl->pl_prop, property,
3046 sizeof (property), NULL, NULL, 0,
3047 cb->cb_literal) != 0)
3051 right_justify = zfs_prop_align_right(pl->pl_prop);
3052 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3053 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3054 property, sizeof (property), cb->cb_literal) != 0)
3058 right_justify = B_TRUE;
3059 } else if (zfs_prop_written(pl->pl_user_prop)) {
3060 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3061 property, sizeof (property), cb->cb_literal) != 0)
3065 right_justify = B_TRUE;
3067 if (nvlist_lookup_nvlist(userprops,
3068 pl->pl_user_prop, &propval) != 0)
3071 verify(nvlist_lookup_string(propval,
3072 ZPROP_VALUE, &propstr) == 0);
3073 right_justify = B_FALSE;
3077 * If this is being called in scripted mode, or if this is the
3078 * last column and it is left-justified, don't include a width
3081 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3082 (void) printf("%s", propstr);
3083 else if (right_justify)
3084 (void) printf("%*s", pl->pl_width, propstr);
3086 (void) printf("%-*s", pl->pl_width, propstr);
3089 (void) printf("\n");
3093 * Generic callback function to list a dataset or snapshot.
3096 list_callback(zfs_handle_t *zhp, void *data)
3098 list_cbdata_t *cbp = data;
3100 if (cbp->cb_first) {
3101 if (!cbp->cb_scripted)
3103 cbp->cb_first = B_FALSE;
3106 print_dataset(zhp, cbp);
3112 zfs_do_list(int argc, char **argv)
3115 static char default_fields[] =
3116 "name,used,available,referenced,mountpoint";
3117 int types = ZFS_TYPE_DATASET;
3118 boolean_t types_specified = B_FALSE;
3119 char *fields = NULL;
3120 list_cbdata_t cb = { 0 };
3124 zfs_sort_column_t *sortcol = NULL;
3125 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3128 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3134 cb.cb_literal = B_TRUE;
3135 flags |= ZFS_ITER_LITERAL_PROPS;
3138 limit = parse_depth(optarg, &flags);
3141 flags |= ZFS_ITER_RECURSE;
3144 cb.cb_scripted = B_TRUE;
3147 if (zfs_add_sort_column(&sortcol, optarg,
3149 (void) fprintf(stderr,
3150 gettext("invalid property '%s'\n"), optarg);
3155 if (zfs_add_sort_column(&sortcol, optarg,
3157 (void) fprintf(stderr,
3158 gettext("invalid property '%s'\n"), optarg);
3164 types_specified = B_TRUE;
3165 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3166 while (*optarg != '\0') {
3167 static char *type_subopts[] = { "filesystem",
3168 "volume", "snapshot", "snap", "bookmark",
3171 switch (getsubopt(&optarg, type_subopts,
3174 types |= ZFS_TYPE_FILESYSTEM;
3177 types |= ZFS_TYPE_VOLUME;
3181 types |= ZFS_TYPE_SNAPSHOT;
3184 types |= ZFS_TYPE_BOOKMARK;
3187 types = ZFS_TYPE_DATASET |
3191 (void) fprintf(stderr,
3192 gettext("invalid type '%s'\n"),
3199 (void) fprintf(stderr, gettext("missing argument for "
3200 "'%c' option\n"), optopt);
3204 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3214 fields = default_fields;
3217 * If we are only going to list snapshot names and sort by name,
3218 * then we can use faster version.
3220 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3221 flags |= ZFS_ITER_SIMPLE;
3224 * If "-o space" and no types were specified, don't display snapshots.
3226 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3227 types &= ~ZFS_TYPE_SNAPSHOT;
3230 * If the user specifies '-o all', the zprop_get_list() doesn't
3231 * normally include the name of the dataset. For 'zfs list', we always
3232 * want this property to be first.
3234 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3238 cb.cb_first = B_TRUE;
3240 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3241 limit, list_callback, &cb);
3243 zprop_free_list(cb.cb_proplist);
3244 zfs_free_sort_columns(sortcol);
3246 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3247 (void) printf(gettext("no datasets available\n"));
3253 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3254 * zfs rename [-f] -p <fs | vol> <fs | vol>
3255 * zfs rename -r <snap> <snap>
3256 * zfs rename -u [-p] <fs> <fs>
3258 * Renames the given dataset to another of the same type.
3260 * The '-p' flag creates all the non-existing ancestors of the target first.
3264 zfs_do_rename(int argc, char **argv)
3267 renameflags_t flags = { 0 };
3271 boolean_t parents = B_FALSE;
3272 char *snapshot = NULL;
3275 while ((c = getopt(argc, argv, "fpru")) != -1) {
3281 flags.recurse = B_TRUE;
3284 flags.nounmount = B_TRUE;
3287 flags.forceunmount = B_TRUE;
3291 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3300 /* check number of arguments */
3302 (void) fprintf(stderr, gettext("missing source dataset "
3307 (void) fprintf(stderr, gettext("missing target dataset "
3312 (void) fprintf(stderr, gettext("too many arguments\n"));
3316 if (flags.recurse && parents) {
3317 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3322 if (flags.recurse && strchr(argv[0], '@') == 0) {
3323 (void) fprintf(stderr, gettext("source dataset for recursive "
3324 "rename must be a snapshot\n"));
3328 if (flags.nounmount && parents) {
3329 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3334 if (flags.nounmount)
3335 types = ZFS_TYPE_FILESYSTEM;
3337 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3339 types = ZFS_TYPE_DATASET;
3341 if (flags.recurse) {
3343 * When we do recursive rename we are fine when the given
3344 * snapshot for the given dataset doesn't exist - it can
3345 * still exists below.
3348 snapshot = strchr(argv[0], '@');
3349 assert(snapshot != NULL);
3354 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3357 /* If we were asked and the name looks good, try to create ancestors. */
3358 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3359 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3364 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3373 * Promotes the given clone fs to be the parent
3377 zfs_do_promote(int argc, char **argv)
3383 if (argc > 1 && argv[1][0] == '-') {
3384 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3389 /* check number of arguments */
3391 (void) fprintf(stderr, gettext("missing clone filesystem"
3396 (void) fprintf(stderr, gettext("too many arguments\n"));
3400 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3404 ret = (zfs_promote(zhp) != 0);
3412 * zfs rollback [-rRf] <snapshot>
3414 * -r Delete any intervening snapshots before doing rollback
3415 * -R Delete any snapshots and their clones
3416 * -f ignored for backwards compatability
3418 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3419 * since then and making it the active dataset. If more recent snapshots exist,
3420 * the command will complain unless the '-r' flag is given.
3422 typedef struct rollback_cbdata {
3428 boolean_t cb_recurse;
3429 } rollback_cbdata_t;
3432 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3434 rollback_cbdata_t *cbp = data;
3436 if (cbp->cb_first && cbp->cb_recurse) {
3437 (void) fprintf(stderr, gettext("cannot rollback to "
3438 "'%s': clones of previous snapshots exist\n"),
3440 (void) fprintf(stderr, gettext("use '-R' to "
3441 "force deletion of the following clones and "
3447 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3454 * Report any snapshots more recent than the one specified. Used when '-r' is
3455 * not specified. We reuse this same callback for the snapshot dependents - if
3456 * 'cb_dependent' is set, then this is a dependent and we should report it
3457 * without checking the transaction group.
3460 rollback_check(zfs_handle_t *zhp, void *data)
3462 rollback_cbdata_t *cbp = data;
3464 if (cbp->cb_doclones) {
3469 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3470 if (cbp->cb_first && !cbp->cb_recurse) {
3471 (void) fprintf(stderr, gettext("cannot "
3472 "rollback to '%s': more recent snapshots "
3473 "or bookmarks exist\n"),
3475 (void) fprintf(stderr, gettext("use '-r' to "
3476 "force deletion of the following "
3477 "snapshots and bookmarks:\n"));
3482 if (cbp->cb_recurse) {
3483 if (zfs_iter_dependents(zhp, B_TRUE,
3484 rollback_check_dependent, cbp) != 0) {
3489 (void) fprintf(stderr, "%s\n",
3498 zfs_do_rollback(int argc, char **argv)
3502 boolean_t force = B_FALSE;
3503 rollback_cbdata_t cb = { 0 };
3504 zfs_handle_t *zhp, *snap;
3505 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3509 while ((c = getopt(argc, argv, "rRf")) != -1) {
3522 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3531 /* check number of arguments */
3533 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3537 (void) fprintf(stderr, gettext("too many arguments\n"));
3541 /* open the snapshot */
3542 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3545 /* open the parent dataset */
3546 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3547 verify((delim = strrchr(parentname, '@')) != NULL);
3549 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3555 * Check for more recent snapshots and/or clones based on the presence
3558 cb.cb_target = argv[0];
3559 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3560 cb.cb_first = B_TRUE;
3562 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3564 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3567 if ((ret = cb.cb_error) != 0)
3571 * Rollback parent to the given snapshot.
3573 ret = zfs_rollback(zhp, snap, force);
3586 * zfs set property=value ... { fs | snap | vol } ...
3588 * Sets the given properties for all datasets specified on the command line.
3592 set_callback(zfs_handle_t *zhp, void *data)
3594 nvlist_t *props = data;
3596 if (zfs_prop_set_list(zhp, props) != 0) {
3597 switch (libzfs_errno(g_zfs)) {
3598 case EZFS_MOUNTFAILED:
3599 (void) fprintf(stderr, gettext("property may be set "
3600 "but unable to remount filesystem\n"));
3602 case EZFS_SHARENFSFAILED:
3603 (void) fprintf(stderr, gettext("property may be set "
3604 "but unable to reshare filesystem\n"));
3613 zfs_do_set(int argc, char **argv)
3615 nvlist_t *props = NULL;
3616 int ds_start = -1; /* argv idx of first dataset arg */
3619 /* check for options */
3620 if (argc > 1 && argv[1][0] == '-') {
3621 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3626 /* check number of arguments */
3628 (void) fprintf(stderr, gettext("missing arguments\n"));
3632 if (strchr(argv[1], '=') == NULL) {
3633 (void) fprintf(stderr, gettext("missing property=value "
3636 (void) fprintf(stderr, gettext("missing dataset "
3642 /* validate argument order: prop=val args followed by dataset args */
3643 for (int i = 1; i < argc; i++) {
3644 if (strchr(argv[i], '=') != NULL) {
3646 /* out-of-order prop=val argument */
3647 (void) fprintf(stderr, gettext("invalid "
3648 "argument order\n"), i);
3651 } else if (ds_start < 0) {
3656 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3660 /* Populate a list of property settings */
3661 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3663 for (int i = 1; i < ds_start; i++) {
3664 if ((ret = parseprop(props, argv[i])) != 0)
3668 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3669 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3676 typedef struct snap_cbdata {
3678 boolean_t sd_recursive;
3679 const char *sd_snapname;
3683 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3685 snap_cbdata_t *sd = arg;
3690 if (sd->sd_recursive &&
3691 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3696 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3699 fnvlist_add_boolean(sd->sd_nvl, name);
3702 if (sd->sd_recursive)
3703 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3709 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3711 * Creates a snapshot with the given name. While functionally equivalent to
3712 * 'zfs create', it is a separate command to differentiate intent.
3715 zfs_do_snapshot(int argc, char **argv)
3720 snap_cbdata_t sd = { 0 };
3721 boolean_t multiple_snaps = B_FALSE;
3723 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3725 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3729 while ((c = getopt(argc, argv, "ro:")) != -1) {
3732 if (parseprop(props, optarg) != 0)
3736 sd.sd_recursive = B_TRUE;
3737 multiple_snaps = B_TRUE;
3740 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3749 /* check number of arguments */
3751 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3756 multiple_snaps = B_TRUE;
3757 for (; argc > 0; argc--, argv++) {
3761 atp = strchr(argv[0], '@');
3765 sd.sd_snapname = atp + 1;
3766 zhp = zfs_open(g_zfs, argv[0],
3767 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3770 if (zfs_snapshot_cb(zhp, &sd) != 0)
3774 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3775 nvlist_free(sd.sd_nvl);
3777 if (ret != 0 && multiple_snaps)
3778 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3782 nvlist_free(sd.sd_nvl);
3789 * Send a backup stream to stdout.
3792 zfs_do_send(int argc, char **argv)
3794 char *fromname = NULL;
3795 char *toname = NULL;
3796 char *resume_token = NULL;
3799 sendflags_t flags = { 0 };
3801 nvlist_t *dbgnv = NULL;
3802 boolean_t extraverbose = B_FALSE;
3804 struct option long_options[] = {
3805 {"replicate", no_argument, NULL, 'R'},
3806 {"props", no_argument, NULL, 'p'},
3807 {"parsable", no_argument, NULL, 'P'},
3808 {"dedup", no_argument, NULL, 'D'},
3809 {"verbose", no_argument, NULL, 'v'},
3810 {"dryrun", no_argument, NULL, 'n'},
3811 {"large-block", no_argument, NULL, 'L'},
3812 {"embed", no_argument, NULL, 'e'},
3813 {"resume", required_argument, NULL, 't'},
3814 {"compressed", no_argument, NULL, 'c'},
3819 while ((c = getopt_long(argc, argv, ":i:I:RbDpVvnPLet:c", long_options,
3831 flags.doall = B_TRUE;
3834 flags.replicate = B_TRUE;
3837 flags.props = B_TRUE;
3840 flags.parsable = B_TRUE;
3841 flags.verbose = B_TRUE;
3844 flags.progress = B_TRUE;
3845 flags.progressastitle = B_TRUE;
3849 extraverbose = B_TRUE;
3850 flags.verbose = B_TRUE;
3851 flags.progress = B_TRUE;
3854 flags.dedup = B_TRUE;
3857 flags.dryrun = B_TRUE;
3860 flags.largeblock = B_TRUE;
3863 flags.embed_data = B_TRUE;
3866 resume_token = optarg;
3869 flags.compress = B_TRUE;
3872 (void) fprintf(stderr, gettext("missing argument for "
3873 "'%c' option\n"), optopt);
3879 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3888 if (resume_token != NULL) {
3889 if (fromname != NULL || flags.replicate || flags.props ||
3891 (void) fprintf(stderr,
3892 gettext("invalid flags combined with -t\n"));
3896 (void) fprintf(stderr, gettext("no additional "
3897 "arguments are permitted with -t\n"));
3902 (void) fprintf(stderr,
3903 gettext("missing snapshot argument\n"));
3907 (void) fprintf(stderr, gettext("too many arguments\n"));
3912 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3913 (void) fprintf(stderr,
3914 gettext("Error: Stream can not be written to a terminal.\n"
3915 "You must redirect standard output.\n"));
3919 if (resume_token != NULL) {
3920 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3925 * Special case sending a filesystem, or from a bookmark.
3927 if (strchr(argv[0], '@') == NULL ||
3928 (fromname && strchr(fromname, '#') != NULL)) {
3929 char frombuf[ZFS_MAX_DATASET_NAME_LEN];
3930 enum lzc_send_flags lzc_flags = 0;
3932 if (flags.replicate || flags.doall || flags.props ||
3933 flags.dedup || flags.dryrun || flags.verbose ||
3935 (void) fprintf(stderr,
3937 "Unsupported flag with filesystem or bookmark.\n"));
3941 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3945 if (flags.largeblock)
3946 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3947 if (flags.embed_data)
3948 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3950 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
3952 if (fromname != NULL &&
3953 (fromname[0] == '#' || fromname[0] == '@')) {
3955 * Incremental source name begins with # or @.
3956 * Default to same fs as target.
3958 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3959 cp = strchr(frombuf, '@');
3962 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3965 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3970 cp = strchr(argv[0], '@');
3973 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3978 * If they specified the full path to the snapshot, chop off
3979 * everything except the short name of the snapshot, but special
3980 * case if they specify the origin.
3982 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3983 char origin[ZFS_MAX_DATASET_NAME_LEN];
3986 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3987 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3989 if (strcmp(origin, fromname) == 0) {
3991 flags.fromorigin = B_TRUE;
3994 if (cp != fromname && strcmp(argv[0], fromname)) {
3995 (void) fprintf(stderr,
3996 gettext("incremental source must be "
3997 "in same filesystem\n"));
4001 if (strchr(fromname, '@') || strchr(fromname, '/')) {
4002 (void) fprintf(stderr,
4003 gettext("invalid incremental source\n"));
4009 if (flags.replicate && fromname == NULL)
4010 flags.doall = B_TRUE;
4012 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
4013 extraverbose ? &dbgnv : NULL);
4015 if (extraverbose && dbgnv != NULL) {
4017 * dump_nvlist prints to stdout, but that's been
4018 * redirected to a file. Make it print to stderr
4021 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
4022 dump_nvlist(dbgnv, 0);
4031 * Restore a backup stream from stdin.
4034 zfs_do_receive(int argc, char **argv)
4037 recvflags_t flags = { 0 };
4038 boolean_t abort_resumable = B_FALSE;
4041 nvpair_t *nvp = NULL;
4043 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4047 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
4050 if (parseprop(props, optarg) != 0)
4054 flags.isprefix = B_TRUE;
4057 flags.isprefix = B_TRUE;
4058 flags.istail = B_TRUE;
4061 flags.dryrun = B_TRUE;
4064 flags.nomount = B_TRUE;
4067 flags.verbose = B_TRUE;
4070 flags.resumable = B_TRUE;
4073 flags.force = B_TRUE;
4076 abort_resumable = B_TRUE;
4079 (void) fprintf(stderr, gettext("missing argument for "
4080 "'%c' option\n"), optopt);
4084 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4093 /* check number of arguments */
4095 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4099 (void) fprintf(stderr, gettext("too many arguments\n"));
4103 while ((nvp = nvlist_next_nvpair(props, nvp))) {
4104 if (strcmp(nvpair_name(nvp), "origin") != 0) {
4105 (void) fprintf(stderr, gettext("invalid option"));
4110 if (abort_resumable) {
4111 if (flags.isprefix || flags.istail || flags.dryrun ||
4112 flags.resumable || flags.nomount) {
4113 (void) fprintf(stderr, gettext("invalid option"));
4117 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4118 (void) snprintf(namebuf, sizeof (namebuf),
4119 "%s/%%recv", argv[0]);
4121 if (zfs_dataset_exists(g_zfs, namebuf,
4122 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4123 zfs_handle_t *zhp = zfs_open(g_zfs,
4124 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4127 err = zfs_destroy(zhp, B_FALSE);
4129 zfs_handle_t *zhp = zfs_open(g_zfs,
4130 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4133 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4134 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4135 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4136 (void) fprintf(stderr,
4137 gettext("'%s' does not have any "
4138 "resumable receive state to abort\n"),
4142 err = zfs_destroy(zhp, B_FALSE);
4148 if (isatty(STDIN_FILENO)) {
4149 (void) fprintf(stderr,
4150 gettext("Error: Backup stream can not be read "
4151 "from a terminal.\n"
4152 "You must redirect standard input.\n"));
4155 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4161 * allow/unallow stuff
4163 /* copied from zfs/sys/dsl_deleg.h */
4164 #define ZFS_DELEG_PERM_CREATE "create"
4165 #define ZFS_DELEG_PERM_DESTROY "destroy"
4166 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4167 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4168 #define ZFS_DELEG_PERM_CLONE "clone"
4169 #define ZFS_DELEG_PERM_PROMOTE "promote"
4170 #define ZFS_DELEG_PERM_RENAME "rename"
4171 #define ZFS_DELEG_PERM_MOUNT "mount"
4172 #define ZFS_DELEG_PERM_SHARE "share"
4173 #define ZFS_DELEG_PERM_SEND "send"
4174 #define ZFS_DELEG_PERM_RECEIVE "receive"
4175 #define ZFS_DELEG_PERM_ALLOW "allow"
4176 #define ZFS_DELEG_PERM_USERPROP "userprop"
4177 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4178 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4179 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4180 #define ZFS_DELEG_PERM_USERUSED "userused"
4181 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4182 #define ZFS_DELEG_PERM_HOLD "hold"
4183 #define ZFS_DELEG_PERM_RELEASE "release"
4184 #define ZFS_DELEG_PERM_DIFF "diff"
4185 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4186 #define ZFS_DELEG_PERM_REMAP "remap"
4188 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4190 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4191 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4192 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4193 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4194 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4195 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4196 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4197 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4198 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4199 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4200 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4201 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4202 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4203 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4204 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4205 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4206 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4207 { ZFS_DELEG_PERM_REMAP, ZFS_DELEG_NOTE_REMAP },
4209 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4210 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4211 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4212 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4213 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4214 { NULL, ZFS_DELEG_NOTE_NONE }
4217 /* permission structure */
4218 typedef struct deleg_perm {
4219 zfs_deleg_who_type_t dp_who_type;
4220 const char *dp_name;
4222 boolean_t dp_descend;
4226 typedef struct deleg_perm_node {
4227 deleg_perm_t dpn_perm;
4229 uu_avl_node_t dpn_avl_node;
4230 } deleg_perm_node_t;
4232 typedef struct fs_perm fs_perm_t;
4234 /* permissions set */
4235 typedef struct who_perm {
4236 zfs_deleg_who_type_t who_type;
4237 const char *who_name; /* id */
4238 char who_ug_name[256]; /* user/group name */
4239 fs_perm_t *who_fsperm; /* uplink */
4241 uu_avl_t *who_deleg_perm_avl; /* permissions */
4245 typedef struct who_perm_node {
4246 who_perm_t who_perm;
4247 uu_avl_node_t who_avl_node;
4250 typedef struct fs_perm_set fs_perm_set_t;
4251 /* fs permissions */
4253 const char *fsp_name;
4255 uu_avl_t *fsp_sc_avl; /* sets,create */
4256 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4258 fs_perm_set_t *fsp_set; /* uplink */
4262 typedef struct fs_perm_node {
4263 fs_perm_t fspn_fsperm;
4266 uu_list_node_t fspn_list_node;
4269 /* top level structure */
4270 struct fs_perm_set {
4271 uu_list_pool_t *fsps_list_pool;
4272 uu_list_t *fsps_list; /* list of fs_perms */
4274 uu_avl_pool_t *fsps_named_set_avl_pool;
4275 uu_avl_pool_t *fsps_who_perm_avl_pool;
4276 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4279 static inline const char *
4280 deleg_perm_type(zfs_deleg_note_t note)
4286 case ZFS_DELEG_NOTE_GROUPQUOTA:
4287 case ZFS_DELEG_NOTE_GROUPUSED:
4288 case ZFS_DELEG_NOTE_USERPROP:
4289 case ZFS_DELEG_NOTE_USERQUOTA:
4290 case ZFS_DELEG_NOTE_USERUSED:
4292 return (gettext("other"));
4294 return (gettext("subcommand"));
4299 who_type2weight(zfs_deleg_who_type_t who_type)
4303 case ZFS_DELEG_NAMED_SET_SETS:
4304 case ZFS_DELEG_NAMED_SET:
4307 case ZFS_DELEG_CREATE_SETS:
4308 case ZFS_DELEG_CREATE:
4311 case ZFS_DELEG_USER_SETS:
4312 case ZFS_DELEG_USER:
4315 case ZFS_DELEG_GROUP_SETS:
4316 case ZFS_DELEG_GROUP:
4319 case ZFS_DELEG_EVERYONE_SETS:
4320 case ZFS_DELEG_EVERYONE:
4332 who_perm_compare(const void *larg, const void *rarg, void *unused)
4334 const who_perm_node_t *l = larg;
4335 const who_perm_node_t *r = rarg;
4336 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4337 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4338 int lweight = who_type2weight(ltype);
4339 int rweight = who_type2weight(rtype);
4340 int res = lweight - rweight;
4342 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4343 ZFS_MAX_DELEG_NAME-1);
4355 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4357 const deleg_perm_node_t *l = larg;
4358 const deleg_perm_node_t *r = rarg;
4359 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4360 ZFS_MAX_DELEG_NAME-1);
4372 fs_perm_set_init(fs_perm_set_t *fspset)
4374 bzero(fspset, sizeof (fs_perm_set_t));
4376 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4377 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4378 NULL, UU_DEFAULT)) == NULL)
4380 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4381 UU_DEFAULT)) == NULL)
4384 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4385 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4386 who_perm_node_t, who_avl_node), who_perm_compare,
4387 UU_DEFAULT)) == NULL)
4390 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4391 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4392 who_perm_node_t, who_avl_node), who_perm_compare,
4393 UU_DEFAULT)) == NULL)
4396 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4397 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4398 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4403 static inline void fs_perm_fini(fs_perm_t *);
4404 static inline void who_perm_fini(who_perm_t *);
4407 fs_perm_set_fini(fs_perm_set_t *fspset)
4409 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4411 while (node != NULL) {
4412 fs_perm_node_t *next_node =
4413 uu_list_next(fspset->fsps_list, node);
4414 fs_perm_t *fsperm = &node->fspn_fsperm;
4415 fs_perm_fini(fsperm);
4416 uu_list_remove(fspset->fsps_list, node);
4421 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4422 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4423 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4427 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4430 deleg_perm->dp_who_type = type;
4431 deleg_perm->dp_name = name;
4435 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4436 zfs_deleg_who_type_t type, const char *name)
4438 uu_avl_pool_t *pool;
4439 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4441 bzero(who_perm, sizeof (who_perm_t));
4443 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4444 UU_DEFAULT)) == NULL)
4447 who_perm->who_type = type;
4448 who_perm->who_name = name;
4449 who_perm->who_fsperm = fsperm;
4453 who_perm_fini(who_perm_t *who_perm)
4455 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4457 while (node != NULL) {
4458 deleg_perm_node_t *next_node =
4459 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4461 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4466 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4470 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4472 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4473 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4475 bzero(fsperm, sizeof (fs_perm_t));
4477 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4481 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4485 fsperm->fsp_set = fspset;
4486 fsperm->fsp_name = fsname;
4490 fs_perm_fini(fs_perm_t *fsperm)
4492 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4493 while (node != NULL) {
4494 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4496 who_perm_t *who_perm = &node->who_perm;
4497 who_perm_fini(who_perm);
4498 uu_avl_remove(fsperm->fsp_sc_avl, node);
4503 node = uu_avl_first(fsperm->fsp_uge_avl);
4504 while (node != NULL) {
4505 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4507 who_perm_t *who_perm = &node->who_perm;
4508 who_perm_fini(who_perm);
4509 uu_avl_remove(fsperm->fsp_uge_avl, node);
4514 uu_avl_destroy(fsperm->fsp_sc_avl);
4515 uu_avl_destroy(fsperm->fsp_uge_avl);
4519 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4520 zfs_deleg_who_type_t who_type, const char *name, char locality)
4522 uu_avl_index_t idx = 0;
4524 deleg_perm_node_t *found_node = NULL;
4525 deleg_perm_t *deleg_perm = &node->dpn_perm;
4527 deleg_perm_init(deleg_perm, who_type, name);
4529 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4531 uu_avl_insert(avl, node, idx);
4534 deleg_perm = &node->dpn_perm;
4539 case ZFS_DELEG_LOCAL:
4540 deleg_perm->dp_local = B_TRUE;
4542 case ZFS_DELEG_DESCENDENT:
4543 deleg_perm->dp_descend = B_TRUE;
4548 assert(B_FALSE); /* invalid locality */
4553 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4555 nvpair_t *nvp = NULL;
4556 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4557 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4558 zfs_deleg_who_type_t who_type = who_perm->who_type;
4560 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4561 const char *name = nvpair_name(nvp);
4562 data_type_t type = nvpair_type(nvp);
4563 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4564 deleg_perm_node_t *node =
4565 safe_malloc(sizeof (deleg_perm_node_t));
4567 assert(type == DATA_TYPE_BOOLEAN);
4569 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4570 set_deleg_perm_node(avl, node, who_type, name, locality);
4577 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4579 nvpair_t *nvp = NULL;
4580 fs_perm_set_t *fspset = fsperm->fsp_set;
4582 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4583 nvlist_t *nvl2 = NULL;
4584 const char *name = nvpair_name(nvp);
4585 uu_avl_t *avl = NULL;
4586 uu_avl_pool_t *avl_pool = NULL;
4587 zfs_deleg_who_type_t perm_type = name[0];
4588 char perm_locality = name[1];
4589 const char *perm_name = name + 3;
4590 boolean_t is_set = B_TRUE;
4591 who_perm_t *who_perm = NULL;
4593 assert('$' == name[2]);
4595 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4598 switch (perm_type) {
4599 case ZFS_DELEG_CREATE:
4600 case ZFS_DELEG_CREATE_SETS:
4601 case ZFS_DELEG_NAMED_SET:
4602 case ZFS_DELEG_NAMED_SET_SETS:
4603 avl_pool = fspset->fsps_named_set_avl_pool;
4604 avl = fsperm->fsp_sc_avl;
4606 case ZFS_DELEG_USER:
4607 case ZFS_DELEG_USER_SETS:
4608 case ZFS_DELEG_GROUP:
4609 case ZFS_DELEG_GROUP_SETS:
4610 case ZFS_DELEG_EVERYONE:
4611 case ZFS_DELEG_EVERYONE_SETS:
4612 avl_pool = fspset->fsps_who_perm_avl_pool;
4613 avl = fsperm->fsp_uge_avl;
4617 assert(!"unhandled zfs_deleg_who_type_t");
4621 who_perm_node_t *found_node = NULL;
4622 who_perm_node_t *node = safe_malloc(
4623 sizeof (who_perm_node_t));
4624 who_perm = &node->who_perm;
4625 uu_avl_index_t idx = 0;
4627 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4628 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4630 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4632 if (avl == fsperm->fsp_uge_avl) {
4634 struct passwd *p = NULL;
4635 struct group *g = NULL;
4636 const char *nice_name = NULL;
4638 switch (perm_type) {
4639 case ZFS_DELEG_USER_SETS:
4640 case ZFS_DELEG_USER:
4641 rid = atoi(perm_name);
4644 nice_name = p->pw_name;
4646 case ZFS_DELEG_GROUP_SETS:
4647 case ZFS_DELEG_GROUP:
4648 rid = atoi(perm_name);
4651 nice_name = g->gr_name;
4658 if (nice_name != NULL)
4660 node->who_perm.who_ug_name,
4664 uu_avl_insert(avl, node, idx);
4667 who_perm = &node->who_perm;
4671 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4678 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4680 nvpair_t *nvp = NULL;
4681 uu_avl_index_t idx = 0;
4683 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4684 nvlist_t *nvl2 = NULL;
4685 const char *fsname = nvpair_name(nvp);
4686 data_type_t type = nvpair_type(nvp);
4687 fs_perm_t *fsperm = NULL;
4688 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4692 fsperm = &node->fspn_fsperm;
4694 assert(DATA_TYPE_NVLIST == type);
4696 uu_list_node_init(node, &node->fspn_list_node,
4697 fspset->fsps_list_pool);
4699 idx = uu_list_numnodes(fspset->fsps_list);
4700 fs_perm_init(fsperm, fspset, fsname);
4702 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4705 (void) parse_fs_perm(fsperm, nvl2);
4707 uu_list_insert(fspset->fsps_list, node, idx);
4713 static inline const char *
4714 deleg_perm_comment(zfs_deleg_note_t note)
4716 const char *str = "";
4721 case ZFS_DELEG_NOTE_ALLOW:
4722 str = gettext("Must also have the permission that is being"
4723 "\n\t\t\t\tallowed");
4725 case ZFS_DELEG_NOTE_CLONE:
4726 str = gettext("Must also have the 'create' ability and 'mount'"
4727 "\n\t\t\t\tability in the origin file system");
4729 case ZFS_DELEG_NOTE_CREATE:
4730 str = gettext("Must also have the 'mount' ability");
4732 case ZFS_DELEG_NOTE_DESTROY:
4733 str = gettext("Must also have the 'mount' ability");
4735 case ZFS_DELEG_NOTE_DIFF:
4736 str = gettext("Allows lookup of paths within a dataset;"
4737 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4738 "\n\t\t\t\tin order to use zfs diff");
4740 case ZFS_DELEG_NOTE_HOLD:
4741 str = gettext("Allows adding a user hold to a snapshot");
4743 case ZFS_DELEG_NOTE_MOUNT:
4744 str = gettext("Allows mount/umount of ZFS datasets");
4746 case ZFS_DELEG_NOTE_PROMOTE:
4747 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4748 " 'promote' ability in the origin file system");
4750 case ZFS_DELEG_NOTE_RECEIVE:
4751 str = gettext("Must also have the 'mount' and 'create'"
4754 case ZFS_DELEG_NOTE_RELEASE:
4755 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4756 "might destroy the snapshot");
4758 case ZFS_DELEG_NOTE_RENAME:
4759 str = gettext("Must also have the 'mount' and 'create'"
4760 "\n\t\t\t\tability in the new parent");
4762 case ZFS_DELEG_NOTE_ROLLBACK:
4765 case ZFS_DELEG_NOTE_SEND:
4768 case ZFS_DELEG_NOTE_SHARE:
4769 str = gettext("Allows sharing file systems over NFS or SMB"
4770 "\n\t\t\t\tprotocols");
4772 case ZFS_DELEG_NOTE_SNAPSHOT:
4776 * case ZFS_DELEG_NOTE_VSCAN:
4777 * str = gettext("");
4781 case ZFS_DELEG_NOTE_GROUPQUOTA:
4782 str = gettext("Allows accessing any groupquota@... property");
4784 case ZFS_DELEG_NOTE_GROUPUSED:
4785 str = gettext("Allows reading any groupused@... property");
4787 case ZFS_DELEG_NOTE_USERPROP:
4788 str = gettext("Allows changing any user property");
4790 case ZFS_DELEG_NOTE_USERQUOTA:
4791 str = gettext("Allows accessing any userquota@... property");
4793 case ZFS_DELEG_NOTE_USERUSED:
4794 str = gettext("Allows reading any userused@... property");
4812 boolean_t recursive; /* unallow only */
4813 boolean_t prt_usage;
4815 boolean_t prt_perms;
4818 const char *dataset;
4822 prop_cmp(const void *a, const void *b)
4824 const char *str1 = *(const char **)a;
4825 const char *str2 = *(const char **)b;
4826 return (strcmp(str1, str2));
4830 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4832 const char *opt_desc[] = {
4833 "-h", gettext("show this help message and exit"),
4834 "-l", gettext("set permission locally"),
4835 "-d", gettext("set permission for descents"),
4836 "-u", gettext("set permission for user"),
4837 "-g", gettext("set permission for group"),
4838 "-e", gettext("set permission for everyone"),
4839 "-c", gettext("set create time permission"),
4840 "-s", gettext("define permission set"),
4842 "-r", gettext("remove permissions recursively"),
4844 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4845 size_t allow_size = unallow_size - 2;
4846 const char *props[ZFS_NUM_PROPS];
4849 FILE *fp = requested ? stdout : stderr;
4850 zprop_desc_t *pdtbl = zfs_prop_get_table();
4851 const char *fmt = gettext("%-16s %-14s\t%s\n");
4853 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4855 (void) fprintf(fp, gettext("Options:\n"));
4856 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4857 const char *opt = opt_desc[i++];
4858 const char *optdsc = opt_desc[i];
4859 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4862 (void) fprintf(fp, gettext("\nThe following permissions are "
4864 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4866 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4867 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4868 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4869 const char *perm_type = deleg_perm_type(perm_note);
4870 const char *perm_comment = deleg_perm_comment(perm_note);
4871 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4874 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4875 zprop_desc_t *pd = &pdtbl[i];
4876 if (pd->pd_visible != B_TRUE)
4879 if (pd->pd_attr == PROP_READONLY)
4882 props[count++] = pd->pd_name;
4884 props[count] = NULL;
4886 qsort(props, count, sizeof (char *), prop_cmp);
4888 for (i = 0; i < count; i++)
4889 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4892 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4894 exit(requested ? 0 : 2);
4897 static inline const char *
4898 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4901 if (un && argc == expected_argc - 1)
4903 else if (argc == expected_argc)
4904 *permsp = argv[argc - 2];
4906 allow_usage(un, B_FALSE,
4907 gettext("wrong number of parameters\n"));
4909 return (argv[argc - 1]);
4913 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4915 int uge_sum = opts->user + opts->group + opts->everyone;
4916 int csuge_sum = opts->create + opts->set + uge_sum;
4917 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4918 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4921 allow_usage(un, B_FALSE,
4922 gettext("-u, -g, and -e are mutually exclusive\n"));
4924 if (opts->prt_usage) {
4925 if (argc == 0 && all_sum == 0)
4926 allow_usage(un, B_TRUE, NULL);
4933 allow_usage(un, B_FALSE,
4934 gettext("invalid options combined with -s\n"));
4936 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4937 if (argv[0][0] != '@')
4938 allow_usage(un, B_FALSE,
4939 gettext("invalid set name: missing '@' prefix\n"));
4940 opts->who = argv[0];
4941 } else if (opts->create) {
4942 if (ldcsuge_sum > 1)
4943 allow_usage(un, B_FALSE,
4944 gettext("invalid options combined with -c\n"));
4945 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4946 } else if (opts->everyone) {
4948 allow_usage(un, B_FALSE,
4949 gettext("invalid options combined with -e\n"));
4950 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4951 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4953 opts->everyone = B_TRUE;
4956 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4957 } else if (argc == 1 && !un) {
4958 opts->prt_perms = B_TRUE;
4959 opts->dataset = argv[argc-1];
4961 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4962 opts->who = argv[0];
4965 if (!opts->local && !opts->descend) {
4966 opts->local = B_TRUE;
4967 opts->descend = B_TRUE;
4972 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4973 const char *who, char *perms, nvlist_t *top_nvl)
4976 char ld[2] = { '\0', '\0' };
4977 char who_buf[MAXNAMELEN + 32];
4978 char base_type = '\0';
4979 char set_type = '\0';
4980 nvlist_t *base_nvl = NULL;
4981 nvlist_t *set_nvl = NULL;
4984 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4986 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4990 case ZFS_DELEG_NAMED_SET_SETS:
4991 case ZFS_DELEG_NAMED_SET:
4992 set_type = ZFS_DELEG_NAMED_SET_SETS;
4993 base_type = ZFS_DELEG_NAMED_SET;
4994 ld[0] = ZFS_DELEG_NA;
4996 case ZFS_DELEG_CREATE_SETS:
4997 case ZFS_DELEG_CREATE:
4998 set_type = ZFS_DELEG_CREATE_SETS;
4999 base_type = ZFS_DELEG_CREATE;
5000 ld[0] = ZFS_DELEG_NA;
5002 case ZFS_DELEG_USER_SETS:
5003 case ZFS_DELEG_USER:
5004 set_type = ZFS_DELEG_USER_SETS;
5005 base_type = ZFS_DELEG_USER;
5007 ld[0] = ZFS_DELEG_LOCAL;
5009 ld[1] = ZFS_DELEG_DESCENDENT;
5011 case ZFS_DELEG_GROUP_SETS:
5012 case ZFS_DELEG_GROUP:
5013 set_type = ZFS_DELEG_GROUP_SETS;
5014 base_type = ZFS_DELEG_GROUP;
5016 ld[0] = ZFS_DELEG_LOCAL;
5018 ld[1] = ZFS_DELEG_DESCENDENT;
5020 case ZFS_DELEG_EVERYONE_SETS:
5021 case ZFS_DELEG_EVERYONE:
5022 set_type = ZFS_DELEG_EVERYONE_SETS;
5023 base_type = ZFS_DELEG_EVERYONE;
5025 ld[0] = ZFS_DELEG_LOCAL;
5027 ld[1] = ZFS_DELEG_DESCENDENT;
5031 assert(set_type != '\0' && base_type != '\0');
5034 if (perms != NULL) {
5036 char *end = curr + strlen(perms);
5038 while (curr < end) {
5039 char *delim = strchr(curr, ',');
5050 (void) nvlist_add_boolean(nvl, curr);
5056 for (i = 0; i < 2; i++) {
5057 char locality = ld[i];
5061 if (!nvlist_empty(base_nvl)) {
5063 (void) snprintf(who_buf,
5064 sizeof (who_buf), "%c%c$%s",
5065 base_type, locality, who);
5067 (void) snprintf(who_buf,
5068 sizeof (who_buf), "%c%c$",
5069 base_type, locality);
5071 (void) nvlist_add_nvlist(top_nvl, who_buf,
5076 if (!nvlist_empty(set_nvl)) {
5078 (void) snprintf(who_buf,
5079 sizeof (who_buf), "%c%c$%s",
5080 set_type, locality, who);
5082 (void) snprintf(who_buf,
5083 sizeof (who_buf), "%c%c$",
5084 set_type, locality);
5086 (void) nvlist_add_nvlist(top_nvl, who_buf,
5091 for (i = 0; i < 2; i++) {
5092 char locality = ld[i];
5097 (void) snprintf(who_buf, sizeof (who_buf),
5098 "%c%c$%s", base_type, locality, who);
5100 (void) snprintf(who_buf, sizeof (who_buf),
5101 "%c%c$", base_type, locality);
5102 (void) nvlist_add_boolean(top_nvl, who_buf);
5105 (void) snprintf(who_buf, sizeof (who_buf),
5106 "%c%c$%s", set_type, locality, who);
5108 (void) snprintf(who_buf, sizeof (who_buf),
5109 "%c%c$", set_type, locality);
5110 (void) nvlist_add_boolean(top_nvl, who_buf);
5116 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5118 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5122 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5123 opts->descend, opts->who, opts->perms, *nvlp);
5124 } else if (opts->create) {
5125 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5126 opts->descend, NULL, opts->perms, *nvlp);
5127 } else if (opts->everyone) {
5128 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5129 opts->descend, NULL, opts->perms, *nvlp);
5131 char *curr = opts->who;
5132 char *end = curr + strlen(curr);
5134 while (curr < end) {
5136 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5138 char *delim = strchr(curr, ',');
5141 struct passwd *p = NULL;
5142 struct group *g = NULL;
5150 rid = (uid_t)strtol(curr, &endch, 0);
5152 who_type = ZFS_DELEG_USER;
5161 (void) snprintf(errbuf, 256, gettext(
5162 "invalid user %s"), curr);
5163 allow_usage(un, B_TRUE, errbuf);
5165 } else if (opts->group) {
5166 who_type = ZFS_DELEG_GROUP;
5175 (void) snprintf(errbuf, 256, gettext(
5176 "invalid group %s"), curr);
5177 allow_usage(un, B_TRUE, errbuf);
5180 if (*endch != '\0') {
5187 if (*endch != '\0') {
5195 who_type = ZFS_DELEG_USER;
5197 } else if (g != NULL) {
5198 who_type = ZFS_DELEG_GROUP;
5201 (void) snprintf(errbuf, 256, gettext(
5202 "invalid user/group %s"), curr);
5203 allow_usage(un, B_TRUE, errbuf);
5207 (void) sprintf(id, "%u", rid);
5210 store_allow_perm(who_type, opts->local,
5211 opts->descend, who, opts->perms, *nvlp);
5220 print_set_creat_perms(uu_avl_t *who_avl)
5222 const char *sc_title[] = {
5223 gettext("Permission sets:\n"),
5224 gettext("Create time permissions:\n"),
5227 const char **title_ptr = sc_title;
5228 who_perm_node_t *who_node = NULL;
5229 int prev_weight = -1;
5231 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5232 who_node = uu_avl_next(who_avl, who_node)) {
5233 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5234 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5235 const char *who_name = who_node->who_perm.who_name;
5236 int weight = who_type2weight(who_type);
5237 boolean_t first = B_TRUE;
5238 deleg_perm_node_t *deleg_node;
5240 if (prev_weight != weight) {
5241 (void) printf(*title_ptr++);
5242 prev_weight = weight;
5245 if (who_name == NULL || strnlen(who_name, 1) == 0)
5246 (void) printf("\t");
5248 (void) printf("\t%s ", who_name);
5250 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5251 deleg_node = uu_avl_next(avl, deleg_node)) {
5254 deleg_node->dpn_perm.dp_name);
5257 (void) printf(",%s",
5258 deleg_node->dpn_perm.dp_name);
5261 (void) printf("\n");
5266 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5269 who_perm_node_t *who_node = NULL;
5270 boolean_t prt_title = B_TRUE;
5271 uu_avl_walk_t *walk;
5273 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5276 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5277 const char *who_name = who_node->who_perm.who_name;
5278 const char *nice_who_name = who_node->who_perm.who_ug_name;
5279 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5280 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5282 deleg_perm_node_t *deleg_node;
5283 boolean_t prt_who = B_TRUE;
5285 for (deleg_node = uu_avl_first(avl);
5287 deleg_node = uu_avl_next(avl, deleg_node)) {
5288 if (local != deleg_node->dpn_perm.dp_local ||
5289 descend != deleg_node->dpn_perm.dp_descend)
5293 const char *who = NULL;
5295 prt_title = B_FALSE;
5296 (void) printf(title);
5300 case ZFS_DELEG_USER_SETS:
5301 case ZFS_DELEG_USER:
5302 who = gettext("user");
5304 who_name = nice_who_name;
5306 case ZFS_DELEG_GROUP_SETS:
5307 case ZFS_DELEG_GROUP:
5308 who = gettext("group");
5310 who_name = nice_who_name;
5312 case ZFS_DELEG_EVERYONE_SETS:
5313 case ZFS_DELEG_EVERYONE:
5314 who = gettext("everyone");
5319 assert(who != NULL);
5323 if (who_name == NULL)
5324 (void) printf("\t%s", who);
5326 (void) printf("\t%s %s", who, who_name);
5329 (void) printf("%c%s", delim,
5330 deleg_node->dpn_perm.dp_name);
5335 (void) printf("\n");
5338 uu_avl_walk_end(walk);
5342 print_fs_perms(fs_perm_set_t *fspset)
5344 fs_perm_node_t *node = NULL;
5345 char buf[MAXNAMELEN + 32];
5346 const char *dsname = buf;
5348 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5349 node = uu_list_next(fspset->fsps_list, node)) {
5350 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5351 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5354 (void) snprintf(buf, sizeof (buf),
5355 gettext("---- Permissions on %s "),
5356 node->fspn_fsperm.fsp_name);
5357 (void) printf(dsname);
5358 left = 70 - strlen(buf);
5361 (void) printf("\n");
5363 print_set_creat_perms(sc_avl);
5364 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5365 gettext("Local permissions:\n"));
5366 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5367 gettext("Descendent permissions:\n"));
5368 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5369 gettext("Local+Descendent permissions:\n"));
5373 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5375 struct deleg_perms {
5381 set_deleg_perms(zfs_handle_t *zhp, void *data)
5383 struct deleg_perms *perms = (struct deleg_perms *)data;
5384 zfs_type_t zfs_type = zfs_get_type(zhp);
5386 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5389 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5393 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5396 nvlist_t *perm_nvl = NULL;
5397 nvlist_t *update_perm_nvl = NULL;
5400 struct allow_opts opts = { 0 };
5402 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5405 while ((c = getopt(argc, argv, optstr)) != -1) {
5408 opts.local = B_TRUE;
5411 opts.descend = B_TRUE;
5417 opts.group = B_TRUE;
5420 opts.everyone = B_TRUE;
5426 opts.create = B_TRUE;
5429 opts.recursive = B_TRUE;
5432 (void) fprintf(stderr, gettext("missing argument for "
5433 "'%c' option\n"), optopt);
5437 opts.prt_usage = B_TRUE;
5440 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5449 /* check arguments */
5450 parse_allow_args(argc, argv, un, &opts);
5452 /* try to open the dataset */
5453 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5454 ZFS_TYPE_VOLUME)) == NULL) {
5455 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5460 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5463 fs_perm_set_init(&fs_perm_set);
5464 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5465 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5470 print_fs_perms(&fs_perm_set);
5472 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5473 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5476 if (un && opts.recursive) {
5477 struct deleg_perms data = { un, update_perm_nvl };
5478 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5487 nvlist_free(perm_nvl);
5488 nvlist_free(update_perm_nvl);
5490 fs_perm_set_fini(&fs_perm_set);
5498 zfs_do_allow(int argc, char **argv)
5500 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5504 zfs_do_unallow(int argc, char **argv)
5506 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5510 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5515 boolean_t recursive = B_FALSE;
5516 const char *opts = holding ? "rt" : "r";
5520 while ((c = getopt(argc, argv, opts)) != -1) {
5526 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5535 /* check number of arguments */
5543 if (holding && tag[0] == '.') {
5544 /* tags starting with '.' are reserved for libzfs */
5545 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5549 for (i = 0; i < argc; ++i) {
5551 char parent[ZFS_MAX_DATASET_NAME_LEN];
5553 char *path = argv[i];
5555 delim = strchr(path, '@');
5556 if (delim == NULL) {
5557 (void) fprintf(stderr,
5558 gettext("'%s' is not a snapshot\n"), path);
5562 (void) strncpy(parent, path, delim - path);
5563 parent[delim - path] = '\0';
5565 zhp = zfs_open(g_zfs, parent,
5566 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5572 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5575 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5581 return (errors != 0);
5585 * zfs hold [-r] [-t] <tag> <snap> ...
5587 * -r Recursively hold
5589 * Apply a user-hold with the given tag to the list of snapshots.
5592 zfs_do_hold(int argc, char **argv)
5594 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5598 * zfs release [-r] <tag> <snap> ...
5600 * -r Recursively release
5602 * Release a user-hold with the given tag from the list of snapshots.
5605 zfs_do_release(int argc, char **argv)
5607 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5610 typedef struct holds_cbdata {
5611 boolean_t cb_recursive;
5612 const char *cb_snapname;
5614 size_t cb_max_namelen;
5615 size_t cb_max_taglen;
5618 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5619 #define DATETIME_BUF_LEN (32)
5624 print_holds(boolean_t scripted, boolean_t literal, size_t nwidth,
5625 size_t tagwidth, nvlist_t *nvl)
5628 nvpair_t *nvp = NULL;
5629 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5633 for (i = 0; i < 3; i++) {
5634 col = gettext(hdr_cols[i]);
5636 (void) printf("%-*s ", i ? tagwidth : nwidth,
5639 (void) printf("%s\n", col);
5643 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5644 char *zname = nvpair_name(nvp);
5646 nvpair_t *nvp2 = NULL;
5647 (void) nvpair_value_nvlist(nvp, &nvl2);
5648 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5649 char tsbuf[DATETIME_BUF_LEN];
5650 char *tagname = nvpair_name(nvp2);
5655 (void) nvpair_value_uint64(nvp2, &val);
5657 snprintf(tsbuf, DATETIME_BUF_LEN, "%llu", val);
5660 (void) localtime_r(&time, &t);
5661 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5662 gettext(STRFTIME_FMT_STR), &t);
5666 (void) printf("%s\t%s\t%s\n", zname,
5669 (void) printf("%-*s %-*s %s\n", nwidth,
5670 zname, tagwidth, tagname, tsbuf);
5677 * Generic callback function to list a dataset or snapshot.
5680 holds_callback(zfs_handle_t *zhp, void *data)
5682 holds_cbdata_t *cbp = data;
5683 nvlist_t *top_nvl = *cbp->cb_nvlp;
5684 nvlist_t *nvl = NULL;
5685 nvpair_t *nvp = NULL;
5686 const char *zname = zfs_get_name(zhp);
5687 size_t znamelen = strlen(zname);
5689 if (cbp->cb_recursive && cbp->cb_snapname != NULL) {
5690 const char *snapname;
5691 char *delim = strchr(zname, '@');
5695 snapname = delim + 1;
5696 if (strcmp(cbp->cb_snapname, snapname))
5700 if (zfs_get_holds(zhp, &nvl) != 0)
5703 if (znamelen > cbp->cb_max_namelen)
5704 cbp->cb_max_namelen = znamelen;
5706 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5707 const char *tag = nvpair_name(nvp);
5708 size_t taglen = strlen(tag);
5709 if (taglen > cbp->cb_max_taglen)
5710 cbp->cb_max_taglen = taglen;
5713 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5717 * zfs holds [-Hp] [-r | -d max] <dataset|snap> ...
5719 * -H Suppress header output
5720 * -p Output literal values
5721 * -r Recursively search for holds
5722 * -d max Limit depth of recursive search
5725 zfs_do_holds(int argc, char **argv)
5730 boolean_t scripted = B_FALSE;
5731 boolean_t literal = B_FALSE;
5732 boolean_t recursive = B_FALSE;
5733 const char *opts = "d:rHp";
5736 int types = ZFS_TYPE_SNAPSHOT;
5737 holds_cbdata_t cb = { 0 };
5744 while ((c = getopt(argc, argv, opts)) != -1) {
5747 limit = parse_depth(optarg, &flags);
5760 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5767 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5768 flags |= ZFS_ITER_RECURSE;
5774 /* check number of arguments */
5778 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5781 for (i = 0; i < argc; ++i) {
5782 char *snapshot = argv[i];
5784 const char *snapname = NULL;
5786 delim = strchr(snapshot, '@');
5787 if (delim != NULL) {
5788 snapname = delim + 1;
5790 snapshot[delim - snapshot] = '\0';
5793 cb.cb_recursive = recursive;
5794 cb.cb_snapname = snapname;
5798 * 1. collect holds data, set format options
5800 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5801 holds_callback, &cb);
5807 * 2. print holds data
5809 print_holds(scripted, literal, cb.cb_max_namelen, cb.cb_max_taglen,
5812 if (nvlist_empty(nvl))
5813 (void) printf(gettext("no datasets available\n"));
5817 return (0 != errors);
5820 #define CHECK_SPINNER 30
5821 #define SPINNER_TIME 3 /* seconds */
5822 #define MOUNT_TIME 1 /* seconds */
5824 typedef struct get_all_state {
5825 boolean_t ga_verbose;
5826 get_all_cb_t *ga_cbp;
5830 get_one_dataset(zfs_handle_t *zhp, void *data)
5832 static char *spin[] = { "-", "\\", "|", "/" };
5833 static int spinval = 0;
5834 static int spincheck = 0;
5835 static time_t last_spin_time = (time_t)0;
5836 get_all_state_t *state = data;
5837 zfs_type_t type = zfs_get_type(zhp);
5839 if (state->ga_verbose) {
5840 if (--spincheck < 0) {
5841 time_t now = time(NULL);
5842 if (last_spin_time + SPINNER_TIME < now) {
5843 update_progress(spin[spinval++ % 4]);
5844 last_spin_time = now;
5846 spincheck = CHECK_SPINNER;
5851 * Interate over any nested datasets.
5853 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5859 * Skip any datasets whose type does not match.
5861 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5865 libzfs_add_handle(state->ga_cbp, zhp);
5866 assert(state->ga_cbp->cb_used <= state->ga_cbp->cb_alloc);
5872 get_all_datasets(get_all_cb_t *cbp, boolean_t verbose)
5874 get_all_state_t state = {
5875 .ga_verbose = verbose,
5880 set_progress_header(gettext("Reading ZFS config"));
5881 (void) zfs_iter_root(g_zfs, get_one_dataset, &state);
5884 finish_progress(gettext("done."));
5888 * Generic callback for sharing or mounting filesystems. Because the code is so
5889 * similar, we have a common function with an extra parameter to determine which
5890 * mode we are using.
5892 typedef enum { OP_SHARE, OP_MOUNT } share_mount_op_t;
5894 typedef struct share_mount_state {
5895 share_mount_op_t sm_op;
5896 boolean_t sm_verbose;
5899 char *sm_proto; /* only valid for OP_SHARE */
5900 pthread_mutex_t sm_lock; /* protects the remaining fields */
5901 uint_t sm_total; /* number of filesystems to process */
5902 uint_t sm_done; /* number of filesystems processed */
5903 int sm_status; /* -1 if any of the share/mount operations failed */
5904 } share_mount_state_t;
5907 * Share or mount a dataset.
5910 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5911 boolean_t explicit, const char *options)
5913 char mountpoint[ZFS_MAXPROPLEN];
5914 char shareopts[ZFS_MAXPROPLEN];
5915 char smbshareopts[ZFS_MAXPROPLEN];
5916 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5918 uint64_t zoned, canmount;
5919 boolean_t shared_nfs, shared_smb;
5921 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5924 * Check to make sure we can mount/share this dataset. If we
5925 * are in the global zone and the filesystem is exported to a
5926 * local zone, or if we are in a local zone and the
5927 * filesystem is not exported, then it is an error.
5929 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5931 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5935 (void) fprintf(stderr, gettext("cannot %s '%s': "
5936 "dataset is exported to a local zone\n"), cmdname,
5940 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5944 (void) fprintf(stderr, gettext("cannot %s '%s': "
5945 "permission denied\n"), cmdname,
5951 * Ignore any filesystems which don't apply to us. This
5952 * includes those with a legacy mountpoint, or those with
5953 * legacy share options.
5955 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5956 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5957 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5958 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5959 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5960 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5962 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5963 strcmp(smbshareopts, "off") == 0) {
5967 (void) fprintf(stderr, gettext("cannot share '%s': "
5968 "legacy share\n"), zfs_get_name(zhp));
5969 (void) fprintf(stderr, gettext("to "
5970 "share this filesystem set "
5971 "sharenfs property on\n"));
5976 * We cannot share or mount legacy filesystems. If the
5977 * shareopts is non-legacy but the mountpoint is legacy, we
5978 * treat it as a legacy share.
5980 if (strcmp(mountpoint, "legacy") == 0) {
5984 (void) fprintf(stderr, gettext("cannot %s '%s': "
5985 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5986 (void) fprintf(stderr, gettext("use %s(8) to "
5987 "%s this filesystem\n"), cmdname, cmdname);
5991 if (strcmp(mountpoint, "none") == 0) {
5995 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5996 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
6001 * canmount explicit outcome
6002 * on no pass through
6003 * on yes pass through
6005 * off yes display error, return 1
6006 * noauto no return 0
6007 * noauto yes pass through
6009 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
6010 if (canmount == ZFS_CANMOUNT_OFF) {
6014 (void) fprintf(stderr, gettext("cannot %s '%s': "
6015 "'canmount' property is set to 'off'\n"), cmdname,
6018 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
6023 * If this filesystem is inconsistent and has a receive resume
6024 * token, we can not mount it.
6026 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
6027 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
6028 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
6032 (void) fprintf(stderr, gettext("cannot %s '%s': "
6033 "Contains partially-completed state from "
6034 "\"zfs receive -r\", which can be resumed with "
6035 "\"zfs send -t\"\n"),
6036 cmdname, zfs_get_name(zhp));
6041 * At this point, we have verified that the mountpoint and/or
6042 * shareopts are appropriate for auto management. If the
6043 * filesystem is already mounted or shared, return (failing
6044 * for explicit requests); otherwise mount or share the
6050 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
6051 shared_smb = zfs_is_shared_smb(zhp, NULL);
6053 if ((shared_nfs && shared_smb) ||
6054 (shared_nfs && strcmp(shareopts, "on") == 0 &&
6055 strcmp(smbshareopts, "off") == 0) ||
6056 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
6057 strcmp(shareopts, "off") == 0)) {
6061 (void) fprintf(stderr, gettext("cannot share "
6062 "'%s': filesystem already shared\n"),
6067 if (!zfs_is_mounted(zhp, NULL) &&
6068 zfs_mount(zhp, NULL, 0) != 0)
6071 if (protocol == NULL) {
6072 if (zfs_shareall(zhp) != 0)
6074 } else if (strcmp(protocol, "nfs") == 0) {
6075 if (zfs_share_nfs(zhp))
6077 } else if (strcmp(protocol, "smb") == 0) {
6078 if (zfs_share_smb(zhp))
6081 (void) fprintf(stderr, gettext("cannot share "
6082 "'%s': invalid share type '%s' "
6084 zfs_get_name(zhp), protocol);
6091 if (options == NULL)
6092 mnt.mnt_mntopts = "";
6094 mnt.mnt_mntopts = (char *)options;
6096 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6097 zfs_is_mounted(zhp, NULL)) {
6101 (void) fprintf(stderr, gettext("cannot mount "
6102 "'%s': filesystem already mounted\n"),
6107 if (zfs_mount(zhp, options, flags) != 0)
6116 * Reports progress in the form "(current/total)". Not thread-safe.
6119 report_mount_progress(int current, int total)
6121 static time_t last_progress_time = 0;
6122 time_t now = time(NULL);
6125 /* display header if we're here for the first time */
6127 set_progress_header(gettext("Mounting ZFS filesystems"));
6128 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6129 /* too soon to report again */
6133 last_progress_time = now;
6135 (void) sprintf(info, "(%d/%d)", current, total);
6137 if (current == total)
6138 finish_progress(info);
6140 update_progress(info);
6144 * zfs_foreach_mountpoint() callback that mounts or shares on filesystem and
6145 * updates the progress meter
6148 share_mount_one_cb(zfs_handle_t *zhp, void *arg)
6150 share_mount_state_t *sms = arg;
6153 ret = share_mount_one(zhp, sms->sm_op, sms->sm_flags, sms->sm_proto,
6154 B_FALSE, sms->sm_options);
6156 pthread_mutex_lock(&sms->sm_lock);
6158 sms->sm_status = ret;
6160 if (sms->sm_verbose)
6161 report_mount_progress(sms->sm_done, sms->sm_total);
6162 pthread_mutex_unlock(&sms->sm_lock);
6167 append_options(char *mntopts, char *newopts)
6169 int len = strlen(mntopts);
6171 /* original length plus new string to append plus 1 for the comma */
6172 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6173 (void) fprintf(stderr, gettext("the opts argument for "
6174 "'%c' option is too long (more than %d chars)\n"),
6175 "-o", MNT_LINE_MAX);
6180 mntopts[len++] = ',';
6182 (void) strcpy(&mntopts[len], newopts);
6186 share_mount(int op, int argc, char **argv)
6189 boolean_t verbose = B_FALSE;
6191 char *options = NULL;
6195 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6205 if (*optarg == '\0') {
6206 (void) fprintf(stderr, gettext("empty mount "
6207 "options (-o) specified\n"));
6211 if (options == NULL)
6212 options = safe_malloc(MNT_LINE_MAX + 1);
6214 /* option validation is done later */
6215 append_options(options, optarg);
6219 warnx("no overlay mounts support on FreeBSD, ignoring");
6222 (void) fprintf(stderr, gettext("missing argument for "
6223 "'%c' option\n"), optopt);
6227 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6236 /* check number of arguments */
6238 char *protocol = NULL;
6240 if (op == OP_SHARE && argc > 0) {
6241 if (strcmp(argv[0], "nfs") != 0 &&
6242 strcmp(argv[0], "smb") != 0) {
6243 (void) fprintf(stderr, gettext("share type "
6244 "must be 'nfs' or 'smb'\n"));
6253 (void) fprintf(stderr, gettext("too many arguments\n"));
6257 start_progress_timer();
6258 get_all_cb_t cb = { 0 };
6259 get_all_datasets(&cb, verbose);
6261 if (cb.cb_used == 0) {
6262 if (options != NULL)
6268 if (op == OP_SHARE) {
6269 sa_init_selective_arg_t sharearg;
6270 sharearg.zhandle_arr = cb.cb_handles;
6271 sharearg.zhandle_len = cb.cb_used;
6272 if ((ret = zfs_init_libshare_arg(g_zfs,
6273 SA_INIT_SHARE_API_SELECTIVE, &sharearg)) != SA_OK) {
6274 (void) fprintf(stderr, gettext(
6275 "Could not initialize libshare, %d"), ret);
6280 share_mount_state_t share_mount_state = { 0 };
6281 share_mount_state.sm_op = op;
6282 share_mount_state.sm_verbose = verbose;
6283 share_mount_state.sm_flags = flags;
6284 share_mount_state.sm_options = options;
6285 share_mount_state.sm_proto = protocol;
6286 share_mount_state.sm_total = cb.cb_used;
6287 pthread_mutex_init(&share_mount_state.sm_lock, NULL);
6290 * libshare isn't mt-safe, so only do the operation in parallel
6291 * if we're mounting.
6293 zfs_foreach_mountpoint(g_zfs, cb.cb_handles, cb.cb_used,
6294 share_mount_one_cb, &share_mount_state, op == OP_MOUNT);
6295 ret = share_mount_state.sm_status;
6297 for (int i = 0; i < cb.cb_used; i++)
6298 zfs_close(cb.cb_handles[i]);
6299 free(cb.cb_handles);
6300 } else if (argc == 0) {
6301 struct mnttab entry;
6303 if ((op == OP_SHARE) || (options != NULL)) {
6304 (void) fprintf(stderr, gettext("missing filesystem "
6305 "argument (specify -a for all)\n"));
6310 * When mount is given no arguments, go through /etc/mnttab and
6311 * display any active ZFS mounts. We hide any snapshots, since
6312 * they are controlled automatically.
6314 rewind(mnttab_file);
6315 while (getmntent(mnttab_file, &entry) == 0) {
6316 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6317 strchr(entry.mnt_special, '@') != NULL)
6320 (void) printf("%-30s %s\n", entry.mnt_special,
6328 (void) fprintf(stderr,
6329 gettext("too many arguments\n"));
6333 if ((zhp = zfs_open(g_zfs, argv[0],
6334 ZFS_TYPE_FILESYSTEM)) == NULL) {
6337 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6347 * zfs mount -a [nfs]
6348 * zfs mount filesystem
6350 * Mount all filesystems, or mount the given filesystem.
6353 zfs_do_mount(int argc, char **argv)
6355 return (share_mount(OP_MOUNT, argc, argv));
6359 * zfs share -a [nfs | smb]
6360 * zfs share filesystem
6362 * Share all filesystems, or share the given filesystem.
6365 zfs_do_share(int argc, char **argv)
6367 return (share_mount(OP_SHARE, argc, argv));
6370 typedef struct unshare_unmount_node {
6371 zfs_handle_t *un_zhp;
6373 uu_avl_node_t un_avlnode;
6374 } unshare_unmount_node_t;
6378 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6380 const unshare_unmount_node_t *l = larg;
6381 const unshare_unmount_node_t *r = rarg;
6383 return (strcmp(l->un_mountp, r->un_mountp));
6387 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6388 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6389 * and unmount it appropriately.
6392 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6396 struct stat64 statbuf;
6397 struct extmnttab entry;
6398 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6402 * Search for the path in /etc/mnttab. Rather than looking for the
6403 * specific path, which can be fooled by non-standard paths (i.e. ".."
6404 * or "//"), we stat() the path and search for the corresponding
6405 * (major,minor) device pair.
6407 if (stat64(path, &statbuf) != 0) {
6408 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6409 cmdname, path, strerror(errno));
6412 path_inode = statbuf.st_ino;
6415 * Search for the given (major,minor) pair in the mount table.
6418 rewind(mnttab_file);
6419 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6420 if (entry.mnt_major == major(statbuf.st_dev) &&
6421 entry.mnt_minor == minor(statbuf.st_dev))
6428 if (statfs(path, &sfs) != 0) {
6429 (void) fprintf(stderr, "%s: %s\n", path,
6433 statfs2mnttab(&sfs, &entry);
6437 if (op == OP_SHARE) {
6438 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6439 "currently mounted\n"), cmdname, path);
6442 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6444 if ((ret = umount2(path, flags)) != 0)
6445 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6450 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6451 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6452 "filesystem\n"), cmdname, path);
6456 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6457 ZFS_TYPE_FILESYSTEM)) == NULL)
6461 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6462 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6463 cmdname, path, strerror(errno));
6465 } else if (statbuf.st_ino != path_inode) {
6466 (void) fprintf(stderr, gettext("cannot "
6467 "%s '%s': not a mountpoint\n"), cmdname, path);
6471 if (op == OP_SHARE) {
6472 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6473 char smbshare_prop[ZFS_MAXPROPLEN];
6475 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6476 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6477 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6478 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6480 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6481 strcmp(smbshare_prop, "off") == 0) {
6482 (void) fprintf(stderr, gettext("cannot unshare "
6483 "'%s': legacy share\n"), path);
6485 (void) fprintf(stderr, gettext("use "
6486 "unshare(1M) to unshare this filesystem\n"));
6488 } else if (!zfs_is_shared(zhp)) {
6489 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6490 "not currently shared\n"), path);
6492 ret = zfs_unshareall_bypath(zhp, path);
6495 char mtpt_prop[ZFS_MAXPROPLEN];
6497 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6498 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6501 ret = zfs_unmount(zhp, NULL, flags);
6502 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6503 (void) fprintf(stderr, gettext("cannot unmount "
6504 "'%s': legacy mountpoint\n"),
6506 (void) fprintf(stderr, gettext("use umount(8) "
6507 "to unmount this filesystem\n"));
6509 ret = zfs_unmountall(zhp, flags);
6520 * Generic callback for unsharing or unmounting a filesystem.
6523 unshare_unmount(int op, int argc, char **argv)
6530 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6531 char sharesmb[ZFS_MAXPROPLEN];
6534 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6543 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6554 * We could make use of zfs_for_each() to walk all datasets in
6555 * the system, but this would be very inefficient, especially
6556 * since we would have to linearly search /etc/mnttab for each
6557 * one. Instead, do one pass through /etc/mnttab looking for
6558 * zfs entries and call zfs_unmount() for each one.
6560 * Things get a little tricky if the administrator has created
6561 * mountpoints beneath other ZFS filesystems. In this case, we
6562 * have to unmount the deepest filesystems first. To accomplish
6563 * this, we place all the mountpoints in an AVL tree sorted by
6564 * the special type (dataset name), and walk the result in
6565 * reverse to make sure to get any snapshots first.
6567 struct mnttab entry;
6568 uu_avl_pool_t *pool;
6569 uu_avl_t *tree = NULL;
6570 unshare_unmount_node_t *node;
6572 uu_avl_walk_t *walk;
6575 (void) fprintf(stderr, gettext("too many arguments\n"));
6579 if (((pool = uu_avl_pool_create("unmount_pool",
6580 sizeof (unshare_unmount_node_t),
6581 offsetof(unshare_unmount_node_t, un_avlnode),
6582 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6583 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6586 rewind(mnttab_file);
6587 while (getmntent(mnttab_file, &entry) == 0) {
6589 /* ignore non-ZFS entries */
6590 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6593 /* ignore snapshots */
6594 if (strchr(entry.mnt_special, '@') != NULL)
6597 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6598 ZFS_TYPE_FILESYSTEM)) == NULL) {
6604 * Ignore datasets that are excluded/restricted by
6607 if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
6614 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6616 sizeof (nfs_mnt_prop),
6617 NULL, NULL, 0, B_FALSE) == 0);
6618 if (strcmp(nfs_mnt_prop, "off") != 0)
6620 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6622 sizeof (nfs_mnt_prop),
6623 NULL, NULL, 0, B_FALSE) == 0);
6624 if (strcmp(nfs_mnt_prop, "off") == 0)
6628 /* Ignore legacy mounts */
6629 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6631 sizeof (nfs_mnt_prop),
6632 NULL, NULL, 0, B_FALSE) == 0);
6633 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6635 /* Ignore canmount=noauto mounts */
6636 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6637 ZFS_CANMOUNT_NOAUTO)
6643 node = safe_malloc(sizeof (unshare_unmount_node_t));
6645 node->un_mountp = safe_strdup(entry.mnt_mountp);
6647 uu_avl_node_init(node, &node->un_avlnode, pool);
6649 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6650 uu_avl_insert(tree, node, idx);
6652 zfs_close(node->un_zhp);
6653 free(node->un_mountp);
6659 * Walk the AVL tree in reverse, unmounting each filesystem and
6660 * removing it from the AVL tree in the process.
6662 if ((walk = uu_avl_walk_start(tree,
6663 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6666 while ((node = uu_avl_walk_next(walk)) != NULL) {
6667 uu_avl_remove(tree, node);
6671 if (zfs_unshareall_bypath(node->un_zhp,
6672 node->un_mountp) != 0)
6677 if (zfs_unmount(node->un_zhp,
6678 node->un_mountp, flags) != 0)
6683 zfs_close(node->un_zhp);
6684 free(node->un_mountp);
6688 uu_avl_walk_end(walk);
6689 uu_avl_destroy(tree);
6690 uu_avl_pool_destroy(pool);
6695 (void) fprintf(stderr,
6696 gettext("missing filesystem argument\n"));
6698 (void) fprintf(stderr,
6699 gettext("too many arguments\n"));
6704 * We have an argument, but it may be a full path or a ZFS
6705 * filesystem. Pass full paths off to unmount_path() (shared by
6706 * manual_unmount), otherwise open the filesystem and pass to
6709 if (argv[0][0] == '/')
6710 return (unshare_unmount_path(op, argv[0],
6713 if ((zhp = zfs_open(g_zfs, argv[0],
6714 ZFS_TYPE_FILESYSTEM)) == NULL)
6717 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6718 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6719 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6720 NULL, 0, B_FALSE) == 0);
6724 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6726 sizeof (nfs_mnt_prop),
6727 NULL, NULL, 0, B_FALSE) == 0);
6728 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6729 sharesmb, sizeof (sharesmb), NULL, NULL,
6732 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6733 strcmp(sharesmb, "off") == 0) {
6734 (void) fprintf(stderr, gettext("cannot "
6735 "unshare '%s': legacy share\n"),
6738 (void) fprintf(stderr, gettext("use "
6739 "unshare(1M) to unshare this "
6743 } else if (!zfs_is_shared(zhp)) {
6744 (void) fprintf(stderr, gettext("cannot "
6745 "unshare '%s': not currently "
6746 "shared\n"), zfs_get_name(zhp));
6748 } else if (zfs_unshareall(zhp) != 0) {
6754 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6755 (void) fprintf(stderr, gettext("cannot "
6756 "unmount '%s': legacy "
6757 "mountpoint\n"), zfs_get_name(zhp));
6758 (void) fprintf(stderr, gettext("use "
6759 "umount(8) to unmount this "
6762 } else if (!zfs_is_mounted(zhp, NULL)) {
6763 (void) fprintf(stderr, gettext("cannot "
6764 "unmount '%s': not currently "
6768 } else if (zfs_unmountall(zhp, flags) != 0) {
6782 * zfs unmount filesystem
6784 * Unmount all filesystems, or a specific ZFS filesystem.
6787 zfs_do_unmount(int argc, char **argv)
6789 return (unshare_unmount(OP_MOUNT, argc, argv));
6794 * zfs unshare filesystem
6796 * Unshare all filesystems, or a specific ZFS filesystem.
6799 zfs_do_unshare(int argc, char **argv)
6801 return (unshare_unmount(OP_SHARE, argc, argv));
6805 * Attach/detach the given dataset to/from the given jail
6809 do_jail(int argc, char **argv, int attach)
6814 /* check number of arguments */
6816 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6820 (void) fprintf(stderr, gettext("too many arguments\n"));
6824 jailid = jail_getid(argv[1]);
6826 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6830 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6834 ret = (zfs_jail(zhp, jailid, attach) != 0);
6841 * zfs jail jailid filesystem
6843 * Attach the given dataset to the given jail
6847 zfs_do_jail(int argc, char **argv)
6850 return (do_jail(argc, argv, 1));
6854 * zfs unjail jailid filesystem
6856 * Detach the given dataset from the given jail
6860 zfs_do_unjail(int argc, char **argv)
6863 return (do_jail(argc, argv, 0));
6867 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6868 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6871 manual_mount(int argc, char **argv)
6874 char mountpoint[ZFS_MAXPROPLEN];
6875 char mntopts[MNT_LINE_MAX] = { '\0' };
6879 char *dataset, *path;
6882 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6885 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6888 flags |= MS_OVERLAY;
6891 flags |= MS_NOMNTTAB;
6894 (void) fprintf(stderr, gettext("missing argument for "
6895 "'%c' option\n"), optopt);
6899 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6901 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6910 /* check that we only have two arguments */
6913 (void) fprintf(stderr, gettext("missing dataset "
6916 (void) fprintf(stderr,
6917 gettext("missing mountpoint argument\n"));
6919 (void) fprintf(stderr, gettext("too many arguments\n"));
6920 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6927 /* try to open the dataset */
6928 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6931 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6932 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6934 /* check for legacy mountpoint and complain appropriately */
6936 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6937 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6938 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6939 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6944 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6945 "mounted using 'mount -t zfs'\n"), dataset);
6946 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6947 "instead.\n"), path);
6948 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6949 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6950 (void) fprintf(stderr, gettext("See zfs(8) for more "
6959 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6960 * unmounts of non-legacy filesystems, as this is the dominant administrative
6964 manual_unmount(int argc, char **argv)
6970 while ((c = getopt(argc, argv, "f")) != -1) {
6976 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6978 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6987 /* check arguments */
6990 (void) fprintf(stderr, gettext("missing path "
6993 (void) fprintf(stderr, gettext("too many arguments\n"));
6994 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6998 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
7002 find_command_idx(char *command, int *idx)
7006 for (i = 0; i < NCOMMAND; i++) {
7007 if (command_table[i].name == NULL)
7010 if (strcmp(command, command_table[i].name) == 0) {
7019 zfs_do_diff(int argc, char **argv)
7023 char *tosnap = NULL;
7024 char *fromsnap = NULL;
7029 while ((c = getopt(argc, argv, "FHt")) != -1) {
7032 flags |= ZFS_DIFF_CLASSIFY;
7035 flags |= ZFS_DIFF_PARSEABLE;
7038 flags |= ZFS_DIFF_TIMESTAMP;
7041 (void) fprintf(stderr,
7042 gettext("invalid option '%c'\n"), optopt);
7051 (void) fprintf(stderr,
7052 gettext("must provide at least one snapshot name\n"));
7057 (void) fprintf(stderr, gettext("too many arguments\n"));
7062 tosnap = (argc == 2) ? argv[1] : NULL;
7065 if (*fromsnap != '@')
7066 copy = strdup(fromsnap);
7068 copy = strdup(tosnap);
7072 if ((atp = strchr(copy, '@')) != NULL)
7075 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
7081 * Ignore SIGPIPE so that the library can give us
7082 * information on any failure
7084 (void) sigignore(SIGPIPE);
7086 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
7094 * zfs remap <filesystem | volume>
7096 * Remap the indirect blocks in the given fileystem or volume.
7099 zfs_do_remap(int argc, char **argv)
7106 while ((c = getopt(argc, argv, "")) != -1) {
7109 (void) fprintf(stderr,
7110 gettext("invalid option '%c'\n"), optopt);
7116 (void) fprintf(stderr, gettext("wrong number of arguments\n"));
7121 err = zfs_remap_indirects(g_zfs, fsname);
7127 * zfs bookmark <fs@snap> <fs#bmark>
7129 * Creates a bookmark with the given name from the given snapshot.
7132 zfs_do_bookmark(int argc, char **argv)
7134 char snapname[ZFS_MAX_DATASET_NAME_LEN];
7141 while ((c = getopt(argc, argv, "")) != -1) {
7144 (void) fprintf(stderr,
7145 gettext("invalid option '%c'\n"), optopt);
7153 /* check number of arguments */
7155 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
7159 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
7163 if (strchr(argv[1], '#') == NULL) {
7164 (void) fprintf(stderr,
7165 gettext("invalid bookmark name '%s' -- "
7166 "must contain a '#'\n"), argv[1]);
7170 if (argv[0][0] == '@') {
7172 * Snapshot name begins with @.
7173 * Default to same fs as bookmark.
7175 (void) strncpy(snapname, argv[1], sizeof (snapname));
7176 *strchr(snapname, '#') = '\0';
7177 (void) strlcat(snapname, argv[0], sizeof (snapname));
7179 (void) strncpy(snapname, argv[0], sizeof (snapname));
7181 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
7187 nvl = fnvlist_alloc();
7188 fnvlist_add_string(nvl, argv[1], snapname);
7189 ret = lzc_bookmark(nvl, NULL);
7193 const char *err_msg;
7196 (void) snprintf(errbuf, sizeof (errbuf),
7197 dgettext(TEXT_DOMAIN,
7198 "cannot create bookmark '%s'"), argv[1]);
7202 err_msg = "bookmark is in a different pool";
7205 err_msg = "bookmark exists";
7208 err_msg = "invalid argument";
7211 err_msg = "bookmark feature not enabled";
7214 err_msg = "out of space";
7217 err_msg = "unknown error";
7220 (void) fprintf(stderr, "%s: %s\n", errbuf,
7221 dgettext(TEXT_DOMAIN, err_msg));
7232 zfs_do_channel_program(int argc, char **argv)
7236 char *progbuf, *filename, *poolname;
7237 size_t progsize, progread;
7239 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
7240 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
7241 boolean_t sync_flag = B_TRUE;
7242 zpool_handle_t *zhp;
7246 (c = getopt(argc, argv, "nt:(instr-limit)m:(memory-limit)"))) {
7254 arg = strtoull(optarg, &endp, 0);
7255 if (errno != 0 || *endp != '\0') {
7256 (void) fprintf(stderr, gettext(
7258 "'%s': expected integer\n"), optarg);
7263 if (arg > ZCP_MAX_INSTRLIMIT || arg == 0) {
7264 (void) fprintf(stderr, gettext(
7265 "Invalid instruction limit: "
7272 ASSERT3U(c, ==, 'm');
7273 if (arg > ZCP_MAX_MEMLIMIT || arg == 0) {
7274 (void) fprintf(stderr, gettext(
7275 "Invalid memory limit: "
7285 sync_flag = B_FALSE;
7289 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7299 (void) fprintf(stderr,
7300 gettext("invalid number of arguments\n"));
7306 if (strcmp(filename, "-") == 0) {
7308 filename = "standard input";
7309 } else if ((fd = open(filename, O_RDONLY)) < 0) {
7310 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
7311 filename, strerror(errno));
7315 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7316 (void) fprintf(stderr, gettext("cannot open pool '%s'"),
7323 * Read in the channel program, expanding the program buffer as
7328 progbuf = safe_malloc(progsize);
7330 ret = read(fd, progbuf + progread, progsize - progread);
7332 if (progread == progsize && ret > 0) {
7334 progbuf = safe_realloc(progbuf, progsize);
7342 (void) fprintf(stderr,
7343 gettext("cannot read '%s': %s\n"),
7344 filename, strerror(errno));
7347 progbuf[progread] = '\0';
7350 * Any remaining arguments are passed as arguments to the lua script as
7353 * "argv" -> [ "arg 1", ... "arg n" ],
7356 nvlist_t *argnvl = fnvlist_alloc();
7357 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);
7360 ret = lzc_channel_program(poolname, progbuf,
7361 instrlimit, memlimit, argnvl, &outnvl);
7363 ret = lzc_channel_program_nosync(poolname, progbuf,
7364 instrlimit, memlimit, argnvl, &outnvl);
7369 * On error, report the error message handed back by lua if one
7370 * exists. Otherwise, generate an appropriate error message,
7371 * falling back on strerror() for an unexpected return code.
7373 char *errstring = NULL;
7374 if (nvlist_exists(outnvl, ZCP_RET_ERROR)) {
7375 (void) nvlist_lookup_string(outnvl,
7376 ZCP_RET_ERROR, &errstring);
7377 if (errstring == NULL)
7378 errstring = strerror(ret);
7383 "Invalid instruction or memory limit.";
7386 errstring = "Return value too large.";
7389 errstring = "Memory limit exhausted.";
7396 errstring = "Timed out.";
7399 errstring = "Permission denied. Channel "
7400 "programs must be run as root.";
7403 errstring = strerror(ret);
7406 (void) fprintf(stderr,
7407 gettext("Channel program execution failed:\n%s\n"),
7410 (void) printf("Channel program fully executed ");
7411 if (nvlist_empty(outnvl)) {
7412 (void) printf("with no return value.\n");
7414 (void) printf("with return value:\n");
7415 dump_nvlist(outnvl, 4);
7420 fnvlist_free(outnvl);
7421 fnvlist_free(argnvl);
7430 main(int argc, char **argv)
7437 (void) setlocale(LC_ALL, "");
7438 (void) textdomain(TEXT_DOMAIN);
7442 if ((g_zfs = libzfs_init()) == NULL) {
7443 (void) fprintf(stderr, gettext("internal error: failed to "
7444 "initialize ZFS library\n"));
7448 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7450 libzfs_print_on_error(g_zfs, B_TRUE);
7452 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
7453 (void) fprintf(stderr, gettext("internal error: unable to "
7454 "open %s\n"), MNTTAB);
7459 * This command also doubles as the /etc/fs mount and unmount program.
7460 * Determine if we should take this behavior based on argv[0].
7462 progname = basename(argv[0]);
7463 if (strcmp(progname, "mount") == 0) {
7464 ret = manual_mount(argc, argv);
7465 } else if (strcmp(progname, "umount") == 0) {
7466 ret = manual_unmount(argc, argv);
7469 * Make sure the user has specified some command.
7472 (void) fprintf(stderr, gettext("missing command\n"));
7479 * The 'umount' command is an alias for 'unmount'
7481 if (strcmp(cmdname, "umount") == 0)
7482 cmdname = "unmount";
7485 * The 'recv' command is an alias for 'receive'
7487 if (strcmp(cmdname, "recv") == 0)
7488 cmdname = "receive";
7491 * The 'snap' command is an alias for 'snapshot'
7493 if (strcmp(cmdname, "snap") == 0)
7494 cmdname = "snapshot";
7499 if (strcmp(cmdname, "-?") == 0)
7503 * Run the appropriate command.
7505 libzfs_mnttab_cache(g_zfs, B_TRUE);
7506 if (find_command_idx(cmdname, &i) == 0) {
7507 current_command = &command_table[i];
7508 ret = command_table[i].func(argc - 1, argv + 1);
7509 } else if (strchr(cmdname, '=') != NULL) {
7510 verify(find_command_idx("set", &i) == 0);
7511 current_command = &command_table[i];
7512 ret = command_table[i].func(argc, argv);
7514 (void) fprintf(stderr, gettext("unrecognized "
7515 "command '%s'\n"), cmdname);
7518 libzfs_mnttab_cache(g_zfs, B_FALSE);
7521 (void) fclose(mnttab_file);
7523 if (ret == 0 && log_history)
7524 (void) zpool_log_history(g_zfs, history_str);
7529 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7530 * for the purposes of running ::findleaks.
7532 if (getenv("ZFS_ABORT") != NULL) {
7533 (void) printf("dumping core by request\n");