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;
2364 (void) nvlist_lookup_uint64(lnvl, propname,
2366 (void) nvlist_lookup_uint64(rnvl, propname,
2369 rc = (rv64 < lv64) ? 1 : -1;
2371 (void) nvlist_lookup_string(lnvl, propname,
2373 (void) nvlist_lookup_string(rnvl, propname,
2375 rc = strcmp(lvstr, rvstr);
2379 case ZFS_PROP_QUOTA:
2382 if (prop == ZFS_PROP_USED)
2386 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2387 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2389 rc = (rv64 < lv64) ? 1 : -1;
2398 return (reverse ? 1 : -1);
2400 return (reverse ? -1 : 1);
2405 * If entries still seem to be the same, check if they are of the same
2406 * type (smbentity is added only if we are doing SID to POSIX ID
2407 * translation where we can have duplicate type/name combinations).
2409 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2410 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2412 return (lvb < rvb ? -1 : 1);
2417 static inline const char *
2418 us_type2str(unsigned field_type)
2420 switch (field_type) {
2421 case USTYPE_PSX_USR:
2422 return ("POSIX User");
2423 case USTYPE_PSX_GRP:
2424 return ("POSIX Group");
2425 case USTYPE_SMB_USR:
2426 return ("SMB User");
2427 case USTYPE_SMB_GRP:
2428 return ("SMB Group");
2430 return ("Undefined");
2435 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2437 us_cbdata_t *cb = (us_cbdata_t *)arg;
2438 zfs_userquota_prop_t prop = cb->cb_prop;
2443 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2444 uu_avl_t *avl = cb->cb_avl;
2448 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2450 const char *typestr;
2454 int typeidx, nameidx, sizeidx;
2455 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2456 boolean_t smbentity = B_FALSE;
2458 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2460 node = safe_malloc(sizeof (us_node_t));
2461 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2462 node->usn_nvl = props;
2464 if (domain != NULL && domain[0] != '\0') {
2466 char sid[MAXNAMELEN + 32];
2470 int flag = IDMAP_REQ_FLG_USE_CACHE;
2475 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2477 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2478 type = USTYPE_SMB_GRP;
2480 err = sid_to_id(sid, B_FALSE, &id);
2483 type = USTYPE_SMB_USR;
2485 err = sid_to_id(sid, B_TRUE, &id);
2492 if (!cb->cb_sid2posix) {
2493 if (type == USTYPE_SMB_USR) {
2494 (void) idmap_getwinnamebyuid(rid, flag,
2497 (void) idmap_getwinnamebygid(rid, flag,
2507 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2509 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2510 type = USTYPE_PSX_GRP;
2511 if (!cb->cb_numname) {
2514 if ((g = getgrgid(rid)) != NULL)
2518 type = USTYPE_PSX_USR;
2519 if (!cb->cb_numname) {
2522 if ((p = getpwuid(rid)) != NULL)
2529 * Make sure that the type/name combination is unique when doing
2530 * SID to POSIX ID translation (hence changing the type from SMB to
2533 if (cb->cb_sid2posix &&
2534 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2537 /* Calculate/update width of TYPE field */
2538 typestr = us_type2str(type);
2539 typelen = strlen(gettext(typestr));
2540 typeidx = us_field_index("type");
2541 if (typelen > cb->cb_width[typeidx])
2542 cb->cb_width[typeidx] = typelen;
2543 if (nvlist_add_uint32(props, "type", type) != 0)
2546 /* Calculate/update width of NAME field */
2547 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2548 if (nvlist_add_uint64(props, "name", rid) != 0)
2550 namelen = snprintf(NULL, 0, "%u", rid);
2552 if (nvlist_add_string(props, "name", name) != 0)
2554 namelen = strlen(name);
2556 nameidx = us_field_index("name");
2557 if (namelen > cb->cb_width[nameidx])
2558 cb->cb_width[nameidx] = namelen;
2561 * Check if this type/name combination is in the list and update it;
2562 * otherwise add new node to the list.
2564 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2565 uu_avl_insert(avl, node, idx);
2570 props = node->usn_nvl;
2573 /* Calculate/update width of USED/QUOTA fields */
2575 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2577 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2578 sizelen = strlen(sizebuf);
2579 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2581 if (!nvlist_exists(props, "quota"))
2582 (void) nvlist_add_uint64(props, "quota", 0);
2585 if (!nvlist_exists(props, "used"))
2586 (void) nvlist_add_uint64(props, "used", 0);
2588 sizeidx = us_field_index(propname);
2589 if (sizelen > cb->cb_width[sizeidx])
2590 cb->cb_width[sizeidx] = sizelen;
2592 if (nvlist_add_uint64(props, propname, space) != 0)
2599 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2600 size_t *width, us_node_t *node)
2602 nvlist_t *nvl = node->usn_nvl;
2603 char valstr[MAXNAMELEN];
2604 boolean_t first = B_TRUE;
2610 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2611 if (!(ustype & types))
2614 while ((field = fields[cfield]) != USFIELD_LAST) {
2615 nvpair_t *nvp = NULL;
2619 char *strval = NULL;
2621 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2622 if (strcmp(nvpair_name(nvp),
2623 us_field_names[field]) == 0)
2627 type = nvpair_type(nvp);
2629 case DATA_TYPE_UINT32:
2630 (void) nvpair_value_uint32(nvp, &val32);
2632 case DATA_TYPE_UINT64:
2633 (void) nvpair_value_uint64(nvp, &val64);
2635 case DATA_TYPE_STRING:
2636 (void) nvpair_value_string(nvp, &strval);
2639 (void) fprintf(stderr, "invalid data type\n");
2644 strval = (char *)us_type2str(val32);
2647 if (type == DATA_TYPE_UINT64) {
2648 (void) sprintf(valstr, "%llu", val64);
2654 if (type == DATA_TYPE_UINT64) {
2656 (void) sprintf(valstr, "%llu", val64);
2658 zfs_nicenum(val64, valstr,
2661 if (field == USFIELD_QUOTA &&
2662 strcmp(valstr, "0") == 0)
2672 (void) printf("\t");
2677 (void) printf("%s", strval);
2678 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2679 (void) printf("%-*s", width[field], strval);
2681 (void) printf("%*s", width[field], strval);
2687 (void) printf("\n");
2691 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2692 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2700 boolean_t first = B_TRUE;
2702 while ((field = fields[cfield]) != USFIELD_LAST) {
2703 col = gettext(us_field_hdr[field]);
2704 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2705 (void) printf(first ? "%-*s" : " %-*s",
2708 (void) printf(first ? "%*s" : " %*s",
2714 (void) printf("\n");
2717 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2718 print_us_node(scripted, parsable, fields, types, width, node);
2720 nvlist_free(node->usn_nvl);
2725 zfs_do_userspace(int argc, char **argv)
2728 zfs_userquota_prop_t p;
2730 uu_avl_pool_t *avl_pool;
2732 uu_avl_walk_t *walk;
2734 char deffields[] = "type,name,used,quota";
2735 char *ofield = NULL;
2736 char *tfield = NULL;
2740 boolean_t scripted = B_FALSE;
2741 boolean_t prtnum = B_FALSE;
2742 boolean_t parsable = B_FALSE;
2743 boolean_t sid2posix = B_FALSE;
2746 zfs_sort_column_t *sortcol = NULL;
2747 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2751 uu_list_pool_t *listpool;
2753 uu_avl_index_t idx = 0;
2754 uu_list_index_t idx2 = 0;
2759 if (strcmp(argv[0], "groupspace") == 0)
2760 /* Toggle default group types */
2761 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2763 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2779 if (zfs_add_sort_column(&sortcol, optarg,
2780 c == 's' ? B_FALSE : B_TRUE) != 0) {
2781 (void) fprintf(stderr,
2782 gettext("invalid field '%s'\n"), optarg);
2793 (void) fprintf(stderr, gettext("missing argument for "
2794 "'%c' option\n"), optopt);
2798 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2808 (void) fprintf(stderr, gettext("missing dataset name\n"));
2812 (void) fprintf(stderr, gettext("too many arguments\n"));
2816 /* Use default output fields if not specified using -o */
2820 if ((delim = strchr(ofield, ',')) != NULL)
2822 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2823 (void) fprintf(stderr, gettext("invalid type '%s' "
2824 "for -o option\n"), ofield);
2829 } while (delim != NULL);
2830 fields[cfield] = USFIELD_LAST;
2832 /* Override output types (-t option) */
2833 if (tfield != NULL) {
2837 boolean_t found = B_FALSE;
2839 if ((delim = strchr(tfield, ',')) != NULL)
2841 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2843 if (strcmp(tfield, us_type_names[i]) == 0) {
2845 types |= us_type_bits[i];
2850 (void) fprintf(stderr, gettext("invalid type "
2851 "'%s' for -t option\n"), tfield);
2856 } while (delim != NULL);
2859 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2862 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2863 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2865 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2868 /* Always add default sorting columns */
2869 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2870 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2872 cb.cb_sortcol = sortcol;
2873 cb.cb_numname = prtnum;
2874 cb.cb_nicenum = !parsable;
2875 cb.cb_avl_pool = avl_pool;
2876 cb.cb_avl = avl_tree;
2877 cb.cb_sid2posix = sid2posix;
2879 for (i = 0; i < USFIELD_LAST; i++)
2880 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2882 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2883 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2884 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2885 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2886 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2889 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2894 if ((node = uu_avl_first(avl_tree)) == NULL)
2897 us_populated = B_TRUE;
2899 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2900 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2901 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2902 uu_list_node_init(node, &node->usn_listnode, listpool);
2904 while (node != NULL) {
2906 node = uu_avl_next(avl_tree, node);
2907 uu_avl_remove(avl_tree, rmnode);
2908 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2909 uu_list_insert(list, rmnode, idx2);
2912 for (node = uu_list_first(list); node != NULL;
2913 node = uu_list_next(list, node)) {
2914 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2916 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2917 uu_avl_insert(avl_tree, node, idx);
2920 uu_list_destroy(list);
2921 uu_list_pool_destroy(listpool);
2923 /* Print and free node nvlist memory */
2924 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2927 zfs_free_sort_columns(sortcol);
2929 /* Clean up the AVL tree */
2930 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2933 while ((node = uu_avl_walk_next(walk)) != NULL) {
2934 uu_avl_remove(cb.cb_avl, node);
2938 uu_avl_walk_end(walk);
2939 uu_avl_destroy(avl_tree);
2940 uu_avl_pool_destroy(avl_pool);
2946 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2947 * [-t type[,...]] [filesystem|volume|snapshot] ...
2949 * -H Scripted mode; elide headers and separate columns by tabs.
2950 * -p Display values in parsable (literal) format.
2951 * -r Recurse over all children.
2952 * -d Limit recursion by depth.
2953 * -o Control which fields to display.
2954 * -s Specify sort columns, descending order.
2955 * -S Specify sort columns, ascending order.
2956 * -t Control which object types to display.
2958 * When given no arguments, list all filesystems in the system.
2959 * Otherwise, list the specified datasets, optionally recursing down them if
2960 * '-r' is specified.
2962 typedef struct list_cbdata {
2964 boolean_t cb_literal;
2965 boolean_t cb_scripted;
2966 zprop_list_t *cb_proplist;
2970 * Given a list of columns to display, output appropriate headers for each one.
2973 print_header(list_cbdata_t *cb)
2975 zprop_list_t *pl = cb->cb_proplist;
2976 char headerbuf[ZFS_MAXPROPLEN];
2979 boolean_t first = B_TRUE;
2980 boolean_t right_justify;
2982 for (; pl != NULL; pl = pl->pl_next) {
2989 right_justify = B_FALSE;
2990 if (pl->pl_prop != ZPROP_INVAL) {
2991 header = zfs_prop_column_name(pl->pl_prop);
2992 right_justify = zfs_prop_align_right(pl->pl_prop);
2994 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2995 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2996 headerbuf[i] = '\0';
3000 if (pl->pl_next == NULL && !right_justify)
3001 (void) printf("%s", header);
3002 else if (right_justify)
3003 (void) printf("%*s", pl->pl_width, header);
3005 (void) printf("%-*s", pl->pl_width, header);
3008 (void) printf("\n");
3012 * Given a dataset and a list of fields, print out all the properties according
3013 * to the described layout.
3016 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3018 zprop_list_t *pl = cb->cb_proplist;
3019 boolean_t first = B_TRUE;
3020 char property[ZFS_MAXPROPLEN];
3021 nvlist_t *userprops = zfs_get_user_props(zhp);
3024 boolean_t right_justify;
3026 for (; pl != NULL; pl = pl->pl_next) {
3028 if (cb->cb_scripted)
3029 (void) printf("\t");
3036 if (pl->pl_prop == ZFS_PROP_NAME) {
3037 (void) strlcpy(property, zfs_get_name(zhp),
3040 right_justify = zfs_prop_align_right(pl->pl_prop);
3041 } else if (pl->pl_prop != ZPROP_INVAL) {
3042 if (zfs_prop_get(zhp, pl->pl_prop, property,
3043 sizeof (property), NULL, NULL, 0,
3044 cb->cb_literal) != 0)
3048 right_justify = zfs_prop_align_right(pl->pl_prop);
3049 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3050 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3051 property, sizeof (property), cb->cb_literal) != 0)
3055 right_justify = B_TRUE;
3056 } else if (zfs_prop_written(pl->pl_user_prop)) {
3057 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3058 property, sizeof (property), cb->cb_literal) != 0)
3062 right_justify = B_TRUE;
3064 if (nvlist_lookup_nvlist(userprops,
3065 pl->pl_user_prop, &propval) != 0)
3068 verify(nvlist_lookup_string(propval,
3069 ZPROP_VALUE, &propstr) == 0);
3070 right_justify = B_FALSE;
3074 * If this is being called in scripted mode, or if this is the
3075 * last column and it is left-justified, don't include a width
3078 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3079 (void) printf("%s", propstr);
3080 else if (right_justify)
3081 (void) printf("%*s", pl->pl_width, propstr);
3083 (void) printf("%-*s", pl->pl_width, propstr);
3086 (void) printf("\n");
3090 * Generic callback function to list a dataset or snapshot.
3093 list_callback(zfs_handle_t *zhp, void *data)
3095 list_cbdata_t *cbp = data;
3097 if (cbp->cb_first) {
3098 if (!cbp->cb_scripted)
3100 cbp->cb_first = B_FALSE;
3103 print_dataset(zhp, cbp);
3109 zfs_do_list(int argc, char **argv)
3112 static char default_fields[] =
3113 "name,used,available,referenced,mountpoint";
3114 int types = ZFS_TYPE_DATASET;
3115 boolean_t types_specified = B_FALSE;
3116 char *fields = NULL;
3117 list_cbdata_t cb = { 0 };
3121 zfs_sort_column_t *sortcol = NULL;
3122 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3125 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3131 cb.cb_literal = B_TRUE;
3132 flags |= ZFS_ITER_LITERAL_PROPS;
3135 limit = parse_depth(optarg, &flags);
3138 flags |= ZFS_ITER_RECURSE;
3141 cb.cb_scripted = B_TRUE;
3144 if (zfs_add_sort_column(&sortcol, optarg,
3146 (void) fprintf(stderr,
3147 gettext("invalid property '%s'\n"), optarg);
3152 if (zfs_add_sort_column(&sortcol, optarg,
3154 (void) fprintf(stderr,
3155 gettext("invalid property '%s'\n"), optarg);
3161 types_specified = B_TRUE;
3162 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3163 while (*optarg != '\0') {
3164 static char *type_subopts[] = { "filesystem",
3165 "volume", "snapshot", "snap", "bookmark",
3168 switch (getsubopt(&optarg, type_subopts,
3171 types |= ZFS_TYPE_FILESYSTEM;
3174 types |= ZFS_TYPE_VOLUME;
3178 types |= ZFS_TYPE_SNAPSHOT;
3181 types |= ZFS_TYPE_BOOKMARK;
3184 types = ZFS_TYPE_DATASET |
3188 (void) fprintf(stderr,
3189 gettext("invalid type '%s'\n"),
3196 (void) fprintf(stderr, gettext("missing argument for "
3197 "'%c' option\n"), optopt);
3201 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3211 fields = default_fields;
3214 * If we are only going to list snapshot names and sort by name,
3215 * then we can use faster version.
3217 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3218 flags |= ZFS_ITER_SIMPLE;
3221 * If "-o space" and no types were specified, don't display snapshots.
3223 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3224 types &= ~ZFS_TYPE_SNAPSHOT;
3227 * If the user specifies '-o all', the zprop_get_list() doesn't
3228 * normally include the name of the dataset. For 'zfs list', we always
3229 * want this property to be first.
3231 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3235 cb.cb_first = B_TRUE;
3237 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3238 limit, list_callback, &cb);
3240 zprop_free_list(cb.cb_proplist);
3241 zfs_free_sort_columns(sortcol);
3243 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3244 (void) printf(gettext("no datasets available\n"));
3250 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3251 * zfs rename [-f] -p <fs | vol> <fs | vol>
3252 * zfs rename -r <snap> <snap>
3253 * zfs rename -u [-p] <fs> <fs>
3255 * Renames the given dataset to another of the same type.
3257 * The '-p' flag creates all the non-existing ancestors of the target first.
3261 zfs_do_rename(int argc, char **argv)
3264 renameflags_t flags = { 0 };
3268 boolean_t parents = B_FALSE;
3269 char *snapshot = NULL;
3272 while ((c = getopt(argc, argv, "fpru")) != -1) {
3278 flags.recurse = B_TRUE;
3281 flags.nounmount = B_TRUE;
3284 flags.forceunmount = B_TRUE;
3288 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3297 /* check number of arguments */
3299 (void) fprintf(stderr, gettext("missing source dataset "
3304 (void) fprintf(stderr, gettext("missing target dataset "
3309 (void) fprintf(stderr, gettext("too many arguments\n"));
3313 if (flags.recurse && parents) {
3314 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3319 if (flags.recurse && strchr(argv[0], '@') == 0) {
3320 (void) fprintf(stderr, gettext("source dataset for recursive "
3321 "rename must be a snapshot\n"));
3325 if (flags.nounmount && parents) {
3326 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3331 if (flags.nounmount)
3332 types = ZFS_TYPE_FILESYSTEM;
3334 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3336 types = ZFS_TYPE_DATASET;
3338 if (flags.recurse) {
3340 * When we do recursive rename we are fine when the given
3341 * snapshot for the given dataset doesn't exist - it can
3342 * still exists below.
3345 snapshot = strchr(argv[0], '@');
3346 assert(snapshot != NULL);
3351 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3354 /* If we were asked and the name looks good, try to create ancestors. */
3355 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3356 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3361 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3370 * Promotes the given clone fs to be the parent
3374 zfs_do_promote(int argc, char **argv)
3380 if (argc > 1 && argv[1][0] == '-') {
3381 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3386 /* check number of arguments */
3388 (void) fprintf(stderr, gettext("missing clone filesystem"
3393 (void) fprintf(stderr, gettext("too many arguments\n"));
3397 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3401 ret = (zfs_promote(zhp) != 0);
3409 * zfs rollback [-rRf] <snapshot>
3411 * -r Delete any intervening snapshots before doing rollback
3412 * -R Delete any snapshots and their clones
3413 * -f ignored for backwards compatability
3415 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3416 * since then and making it the active dataset. If more recent snapshots exist,
3417 * the command will complain unless the '-r' flag is given.
3419 typedef struct rollback_cbdata {
3425 boolean_t cb_recurse;
3426 } rollback_cbdata_t;
3429 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3431 rollback_cbdata_t *cbp = data;
3433 if (cbp->cb_first && cbp->cb_recurse) {
3434 (void) fprintf(stderr, gettext("cannot rollback to "
3435 "'%s': clones of previous snapshots exist\n"),
3437 (void) fprintf(stderr, gettext("use '-R' to "
3438 "force deletion of the following clones and "
3444 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3451 * Report any snapshots more recent than the one specified. Used when '-r' is
3452 * not specified. We reuse this same callback for the snapshot dependents - if
3453 * 'cb_dependent' is set, then this is a dependent and we should report it
3454 * without checking the transaction group.
3457 rollback_check(zfs_handle_t *zhp, void *data)
3459 rollback_cbdata_t *cbp = data;
3461 if (cbp->cb_doclones) {
3466 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3467 if (cbp->cb_first && !cbp->cb_recurse) {
3468 (void) fprintf(stderr, gettext("cannot "
3469 "rollback to '%s': more recent snapshots "
3470 "or bookmarks exist\n"),
3472 (void) fprintf(stderr, gettext("use '-r' to "
3473 "force deletion of the following "
3474 "snapshots and bookmarks:\n"));
3479 if (cbp->cb_recurse) {
3480 if (zfs_iter_dependents(zhp, B_TRUE,
3481 rollback_check_dependent, cbp) != 0) {
3486 (void) fprintf(stderr, "%s\n",
3495 zfs_do_rollback(int argc, char **argv)
3499 boolean_t force = B_FALSE;
3500 rollback_cbdata_t cb = { 0 };
3501 zfs_handle_t *zhp, *snap;
3502 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3506 while ((c = getopt(argc, argv, "rRf")) != -1) {
3519 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3528 /* check number of arguments */
3530 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3534 (void) fprintf(stderr, gettext("too many arguments\n"));
3538 /* open the snapshot */
3539 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3542 /* open the parent dataset */
3543 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3544 verify((delim = strrchr(parentname, '@')) != NULL);
3546 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3552 * Check for more recent snapshots and/or clones based on the presence
3555 cb.cb_target = argv[0];
3556 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3557 cb.cb_first = B_TRUE;
3559 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3561 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3564 if ((ret = cb.cb_error) != 0)
3568 * Rollback parent to the given snapshot.
3570 ret = zfs_rollback(zhp, snap, force);
3583 * zfs set property=value ... { fs | snap | vol } ...
3585 * Sets the given properties for all datasets specified on the command line.
3589 set_callback(zfs_handle_t *zhp, void *data)
3591 nvlist_t *props = data;
3593 if (zfs_prop_set_list(zhp, props) != 0) {
3594 switch (libzfs_errno(g_zfs)) {
3595 case EZFS_MOUNTFAILED:
3596 (void) fprintf(stderr, gettext("property may be set "
3597 "but unable to remount filesystem\n"));
3599 case EZFS_SHARENFSFAILED:
3600 (void) fprintf(stderr, gettext("property may be set "
3601 "but unable to reshare filesystem\n"));
3610 zfs_do_set(int argc, char **argv)
3612 nvlist_t *props = NULL;
3613 int ds_start = -1; /* argv idx of first dataset arg */
3616 /* check for options */
3617 if (argc > 1 && argv[1][0] == '-') {
3618 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3623 /* check number of arguments */
3625 (void) fprintf(stderr, gettext("missing arguments\n"));
3629 if (strchr(argv[1], '=') == NULL) {
3630 (void) fprintf(stderr, gettext("missing property=value "
3633 (void) fprintf(stderr, gettext("missing dataset "
3639 /* validate argument order: prop=val args followed by dataset args */
3640 for (int i = 1; i < argc; i++) {
3641 if (strchr(argv[i], '=') != NULL) {
3643 /* out-of-order prop=val argument */
3644 (void) fprintf(stderr, gettext("invalid "
3645 "argument order\n"), i);
3648 } else if (ds_start < 0) {
3653 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3657 /* Populate a list of property settings */
3658 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3660 for (int i = 1; i < ds_start; i++) {
3661 if ((ret = parseprop(props, argv[i])) != 0)
3665 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3666 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3673 typedef struct snap_cbdata {
3675 boolean_t sd_recursive;
3676 const char *sd_snapname;
3680 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3682 snap_cbdata_t *sd = arg;
3687 if (sd->sd_recursive &&
3688 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3693 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3696 fnvlist_add_boolean(sd->sd_nvl, name);
3699 if (sd->sd_recursive)
3700 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3706 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3708 * Creates a snapshot with the given name. While functionally equivalent to
3709 * 'zfs create', it is a separate command to differentiate intent.
3712 zfs_do_snapshot(int argc, char **argv)
3717 snap_cbdata_t sd = { 0 };
3718 boolean_t multiple_snaps = B_FALSE;
3720 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3722 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3726 while ((c = getopt(argc, argv, "ro:")) != -1) {
3729 if (parseprop(props, optarg) != 0)
3733 sd.sd_recursive = B_TRUE;
3734 multiple_snaps = B_TRUE;
3737 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3746 /* check number of arguments */
3748 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3753 multiple_snaps = B_TRUE;
3754 for (; argc > 0; argc--, argv++) {
3758 atp = strchr(argv[0], '@');
3762 sd.sd_snapname = atp + 1;
3763 zhp = zfs_open(g_zfs, argv[0],
3764 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3767 if (zfs_snapshot_cb(zhp, &sd) != 0)
3771 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3772 nvlist_free(sd.sd_nvl);
3774 if (ret != 0 && multiple_snaps)
3775 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3779 nvlist_free(sd.sd_nvl);
3786 * Send a backup stream to stdout.
3789 zfs_do_send(int argc, char **argv)
3791 char *fromname = NULL;
3792 char *toname = NULL;
3793 char *resume_token = NULL;
3796 sendflags_t flags = { 0 };
3798 nvlist_t *dbgnv = NULL;
3799 boolean_t extraverbose = B_FALSE;
3801 struct option long_options[] = {
3802 {"replicate", no_argument, NULL, 'R'},
3803 {"props", no_argument, NULL, 'p'},
3804 {"parsable", no_argument, NULL, 'P'},
3805 {"dedup", no_argument, NULL, 'D'},
3806 {"verbose", no_argument, NULL, 'v'},
3807 {"dryrun", no_argument, NULL, 'n'},
3808 {"large-block", no_argument, NULL, 'L'},
3809 {"embed", no_argument, NULL, 'e'},
3810 {"resume", required_argument, NULL, 't'},
3811 {"compressed", no_argument, NULL, 'c'},
3816 while ((c = getopt_long(argc, argv, ":i:I:RbDpvnPLet:c", long_options,
3828 flags.doall = B_TRUE;
3831 flags.replicate = B_TRUE;
3834 flags.props = B_TRUE;
3837 flags.parsable = B_TRUE;
3838 flags.verbose = B_TRUE;
3842 extraverbose = B_TRUE;
3843 flags.verbose = B_TRUE;
3844 flags.progress = B_TRUE;
3847 flags.dedup = B_TRUE;
3850 flags.dryrun = B_TRUE;
3853 flags.largeblock = B_TRUE;
3856 flags.embed_data = B_TRUE;
3859 resume_token = optarg;
3862 flags.compress = B_TRUE;
3865 (void) fprintf(stderr, gettext("missing argument for "
3866 "'%c' option\n"), optopt);
3872 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3881 if (resume_token != NULL) {
3882 if (fromname != NULL || flags.replicate || flags.props ||
3884 (void) fprintf(stderr,
3885 gettext("invalid flags combined with -t\n"));
3889 (void) fprintf(stderr, gettext("no additional "
3890 "arguments are permitted with -t\n"));
3895 (void) fprintf(stderr,
3896 gettext("missing snapshot argument\n"));
3900 (void) fprintf(stderr, gettext("too many arguments\n"));
3905 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3906 (void) fprintf(stderr,
3907 gettext("Error: Stream can not be written to a terminal.\n"
3908 "You must redirect standard output.\n"));
3912 if (resume_token != NULL) {
3913 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3918 * Special case sending a filesystem, or from a bookmark.
3920 if (strchr(argv[0], '@') == NULL ||
3921 (fromname && strchr(fromname, '#') != NULL)) {
3922 char frombuf[ZFS_MAX_DATASET_NAME_LEN];
3923 enum lzc_send_flags lzc_flags = 0;
3925 if (flags.replicate || flags.doall || flags.props ||
3926 flags.dedup || flags.dryrun || flags.verbose ||
3928 (void) fprintf(stderr,
3930 "Unsupported flag with filesystem or bookmark.\n"));
3934 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3938 if (flags.largeblock)
3939 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3940 if (flags.embed_data)
3941 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3943 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
3945 if (fromname != NULL &&
3946 (fromname[0] == '#' || fromname[0] == '@')) {
3948 * Incremental source name begins with # or @.
3949 * Default to same fs as target.
3951 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3952 cp = strchr(frombuf, '@');
3955 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3958 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3963 cp = strchr(argv[0], '@');
3966 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3971 * If they specified the full path to the snapshot, chop off
3972 * everything except the short name of the snapshot, but special
3973 * case if they specify the origin.
3975 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3976 char origin[ZFS_MAX_DATASET_NAME_LEN];
3979 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3980 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3982 if (strcmp(origin, fromname) == 0) {
3984 flags.fromorigin = B_TRUE;
3987 if (cp != fromname && strcmp(argv[0], fromname)) {
3988 (void) fprintf(stderr,
3989 gettext("incremental source must be "
3990 "in same filesystem\n"));
3994 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3995 (void) fprintf(stderr,
3996 gettext("invalid incremental source\n"));
4002 if (flags.replicate && fromname == NULL)
4003 flags.doall = B_TRUE;
4005 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
4006 extraverbose ? &dbgnv : NULL);
4008 if (extraverbose && dbgnv != NULL) {
4010 * dump_nvlist prints to stdout, but that's been
4011 * redirected to a file. Make it print to stderr
4014 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
4015 dump_nvlist(dbgnv, 0);
4024 * Restore a backup stream from stdin.
4027 zfs_do_receive(int argc, char **argv)
4030 recvflags_t flags = { 0 };
4031 boolean_t abort_resumable = B_FALSE;
4034 nvpair_t *nvp = NULL;
4036 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4040 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
4043 if (parseprop(props, optarg) != 0)
4047 flags.isprefix = B_TRUE;
4050 flags.isprefix = B_TRUE;
4051 flags.istail = B_TRUE;
4054 flags.dryrun = B_TRUE;
4057 flags.nomount = B_TRUE;
4060 flags.verbose = B_TRUE;
4063 flags.resumable = B_TRUE;
4066 flags.force = B_TRUE;
4069 abort_resumable = B_TRUE;
4072 (void) fprintf(stderr, gettext("missing argument for "
4073 "'%c' option\n"), optopt);
4077 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4086 /* check number of arguments */
4088 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4092 (void) fprintf(stderr, gettext("too many arguments\n"));
4096 while ((nvp = nvlist_next_nvpair(props, nvp))) {
4097 if (strcmp(nvpair_name(nvp), "origin") != 0) {
4098 (void) fprintf(stderr, gettext("invalid option"));
4103 if (abort_resumable) {
4104 if (flags.isprefix || flags.istail || flags.dryrun ||
4105 flags.resumable || flags.nomount) {
4106 (void) fprintf(stderr, gettext("invalid option"));
4110 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4111 (void) snprintf(namebuf, sizeof (namebuf),
4112 "%s/%%recv", argv[0]);
4114 if (zfs_dataset_exists(g_zfs, namebuf,
4115 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4116 zfs_handle_t *zhp = zfs_open(g_zfs,
4117 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4120 err = zfs_destroy(zhp, B_FALSE);
4122 zfs_handle_t *zhp = zfs_open(g_zfs,
4123 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4126 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4127 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4128 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4129 (void) fprintf(stderr,
4130 gettext("'%s' does not have any "
4131 "resumable receive state to abort\n"),
4135 err = zfs_destroy(zhp, B_FALSE);
4141 if (isatty(STDIN_FILENO)) {
4142 (void) fprintf(stderr,
4143 gettext("Error: Backup stream can not be read "
4144 "from a terminal.\n"
4145 "You must redirect standard input.\n"));
4148 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4154 * allow/unallow stuff
4156 /* copied from zfs/sys/dsl_deleg.h */
4157 #define ZFS_DELEG_PERM_CREATE "create"
4158 #define ZFS_DELEG_PERM_DESTROY "destroy"
4159 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4160 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4161 #define ZFS_DELEG_PERM_CLONE "clone"
4162 #define ZFS_DELEG_PERM_PROMOTE "promote"
4163 #define ZFS_DELEG_PERM_RENAME "rename"
4164 #define ZFS_DELEG_PERM_MOUNT "mount"
4165 #define ZFS_DELEG_PERM_SHARE "share"
4166 #define ZFS_DELEG_PERM_SEND "send"
4167 #define ZFS_DELEG_PERM_RECEIVE "receive"
4168 #define ZFS_DELEG_PERM_ALLOW "allow"
4169 #define ZFS_DELEG_PERM_USERPROP "userprop"
4170 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4171 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4172 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4173 #define ZFS_DELEG_PERM_USERUSED "userused"
4174 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4175 #define ZFS_DELEG_PERM_HOLD "hold"
4176 #define ZFS_DELEG_PERM_RELEASE "release"
4177 #define ZFS_DELEG_PERM_DIFF "diff"
4178 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4179 #define ZFS_DELEG_PERM_REMAP "remap"
4181 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4183 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4184 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4185 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4186 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4187 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4188 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4189 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4190 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4191 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4192 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4193 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4194 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4195 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4196 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4197 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4198 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4199 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4200 { ZFS_DELEG_PERM_REMAP, ZFS_DELEG_NOTE_REMAP },
4202 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4203 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4204 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4205 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4206 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4207 { NULL, ZFS_DELEG_NOTE_NONE }
4210 /* permission structure */
4211 typedef struct deleg_perm {
4212 zfs_deleg_who_type_t dp_who_type;
4213 const char *dp_name;
4215 boolean_t dp_descend;
4219 typedef struct deleg_perm_node {
4220 deleg_perm_t dpn_perm;
4222 uu_avl_node_t dpn_avl_node;
4223 } deleg_perm_node_t;
4225 typedef struct fs_perm fs_perm_t;
4227 /* permissions set */
4228 typedef struct who_perm {
4229 zfs_deleg_who_type_t who_type;
4230 const char *who_name; /* id */
4231 char who_ug_name[256]; /* user/group name */
4232 fs_perm_t *who_fsperm; /* uplink */
4234 uu_avl_t *who_deleg_perm_avl; /* permissions */
4238 typedef struct who_perm_node {
4239 who_perm_t who_perm;
4240 uu_avl_node_t who_avl_node;
4243 typedef struct fs_perm_set fs_perm_set_t;
4244 /* fs permissions */
4246 const char *fsp_name;
4248 uu_avl_t *fsp_sc_avl; /* sets,create */
4249 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4251 fs_perm_set_t *fsp_set; /* uplink */
4255 typedef struct fs_perm_node {
4256 fs_perm_t fspn_fsperm;
4259 uu_list_node_t fspn_list_node;
4262 /* top level structure */
4263 struct fs_perm_set {
4264 uu_list_pool_t *fsps_list_pool;
4265 uu_list_t *fsps_list; /* list of fs_perms */
4267 uu_avl_pool_t *fsps_named_set_avl_pool;
4268 uu_avl_pool_t *fsps_who_perm_avl_pool;
4269 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4272 static inline const char *
4273 deleg_perm_type(zfs_deleg_note_t note)
4279 case ZFS_DELEG_NOTE_GROUPQUOTA:
4280 case ZFS_DELEG_NOTE_GROUPUSED:
4281 case ZFS_DELEG_NOTE_USERPROP:
4282 case ZFS_DELEG_NOTE_USERQUOTA:
4283 case ZFS_DELEG_NOTE_USERUSED:
4285 return (gettext("other"));
4287 return (gettext("subcommand"));
4292 who_type2weight(zfs_deleg_who_type_t who_type)
4296 case ZFS_DELEG_NAMED_SET_SETS:
4297 case ZFS_DELEG_NAMED_SET:
4300 case ZFS_DELEG_CREATE_SETS:
4301 case ZFS_DELEG_CREATE:
4304 case ZFS_DELEG_USER_SETS:
4305 case ZFS_DELEG_USER:
4308 case ZFS_DELEG_GROUP_SETS:
4309 case ZFS_DELEG_GROUP:
4312 case ZFS_DELEG_EVERYONE_SETS:
4313 case ZFS_DELEG_EVERYONE:
4325 who_perm_compare(const void *larg, const void *rarg, void *unused)
4327 const who_perm_node_t *l = larg;
4328 const who_perm_node_t *r = rarg;
4329 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4330 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4331 int lweight = who_type2weight(ltype);
4332 int rweight = who_type2weight(rtype);
4333 int res = lweight - rweight;
4335 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4336 ZFS_MAX_DELEG_NAME-1);
4348 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4350 const deleg_perm_node_t *l = larg;
4351 const deleg_perm_node_t *r = rarg;
4352 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4353 ZFS_MAX_DELEG_NAME-1);
4365 fs_perm_set_init(fs_perm_set_t *fspset)
4367 bzero(fspset, sizeof (fs_perm_set_t));
4369 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4370 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4371 NULL, UU_DEFAULT)) == NULL)
4373 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4374 UU_DEFAULT)) == NULL)
4377 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4378 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4379 who_perm_node_t, who_avl_node), who_perm_compare,
4380 UU_DEFAULT)) == NULL)
4383 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4384 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4385 who_perm_node_t, who_avl_node), who_perm_compare,
4386 UU_DEFAULT)) == NULL)
4389 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4390 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4391 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4396 static inline void fs_perm_fini(fs_perm_t *);
4397 static inline void who_perm_fini(who_perm_t *);
4400 fs_perm_set_fini(fs_perm_set_t *fspset)
4402 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4404 while (node != NULL) {
4405 fs_perm_node_t *next_node =
4406 uu_list_next(fspset->fsps_list, node);
4407 fs_perm_t *fsperm = &node->fspn_fsperm;
4408 fs_perm_fini(fsperm);
4409 uu_list_remove(fspset->fsps_list, node);
4414 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4415 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4416 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4420 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4423 deleg_perm->dp_who_type = type;
4424 deleg_perm->dp_name = name;
4428 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4429 zfs_deleg_who_type_t type, const char *name)
4431 uu_avl_pool_t *pool;
4432 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4434 bzero(who_perm, sizeof (who_perm_t));
4436 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4437 UU_DEFAULT)) == NULL)
4440 who_perm->who_type = type;
4441 who_perm->who_name = name;
4442 who_perm->who_fsperm = fsperm;
4446 who_perm_fini(who_perm_t *who_perm)
4448 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4450 while (node != NULL) {
4451 deleg_perm_node_t *next_node =
4452 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4454 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4459 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4463 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4465 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4466 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4468 bzero(fsperm, sizeof (fs_perm_t));
4470 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4474 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4478 fsperm->fsp_set = fspset;
4479 fsperm->fsp_name = fsname;
4483 fs_perm_fini(fs_perm_t *fsperm)
4485 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4486 while (node != NULL) {
4487 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4489 who_perm_t *who_perm = &node->who_perm;
4490 who_perm_fini(who_perm);
4491 uu_avl_remove(fsperm->fsp_sc_avl, node);
4496 node = uu_avl_first(fsperm->fsp_uge_avl);
4497 while (node != NULL) {
4498 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4500 who_perm_t *who_perm = &node->who_perm;
4501 who_perm_fini(who_perm);
4502 uu_avl_remove(fsperm->fsp_uge_avl, node);
4507 uu_avl_destroy(fsperm->fsp_sc_avl);
4508 uu_avl_destroy(fsperm->fsp_uge_avl);
4512 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4513 zfs_deleg_who_type_t who_type, const char *name, char locality)
4515 uu_avl_index_t idx = 0;
4517 deleg_perm_node_t *found_node = NULL;
4518 deleg_perm_t *deleg_perm = &node->dpn_perm;
4520 deleg_perm_init(deleg_perm, who_type, name);
4522 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4524 uu_avl_insert(avl, node, idx);
4527 deleg_perm = &node->dpn_perm;
4532 case ZFS_DELEG_LOCAL:
4533 deleg_perm->dp_local = B_TRUE;
4535 case ZFS_DELEG_DESCENDENT:
4536 deleg_perm->dp_descend = B_TRUE;
4541 assert(B_FALSE); /* invalid locality */
4546 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4548 nvpair_t *nvp = NULL;
4549 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4550 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4551 zfs_deleg_who_type_t who_type = who_perm->who_type;
4553 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4554 const char *name = nvpair_name(nvp);
4555 data_type_t type = nvpair_type(nvp);
4556 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4557 deleg_perm_node_t *node =
4558 safe_malloc(sizeof (deleg_perm_node_t));
4560 assert(type == DATA_TYPE_BOOLEAN);
4562 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4563 set_deleg_perm_node(avl, node, who_type, name, locality);
4570 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4572 nvpair_t *nvp = NULL;
4573 fs_perm_set_t *fspset = fsperm->fsp_set;
4575 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4576 nvlist_t *nvl2 = NULL;
4577 const char *name = nvpair_name(nvp);
4578 uu_avl_t *avl = NULL;
4579 uu_avl_pool_t *avl_pool = NULL;
4580 zfs_deleg_who_type_t perm_type = name[0];
4581 char perm_locality = name[1];
4582 const char *perm_name = name + 3;
4583 boolean_t is_set = B_TRUE;
4584 who_perm_t *who_perm = NULL;
4586 assert('$' == name[2]);
4588 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4591 switch (perm_type) {
4592 case ZFS_DELEG_CREATE:
4593 case ZFS_DELEG_CREATE_SETS:
4594 case ZFS_DELEG_NAMED_SET:
4595 case ZFS_DELEG_NAMED_SET_SETS:
4596 avl_pool = fspset->fsps_named_set_avl_pool;
4597 avl = fsperm->fsp_sc_avl;
4599 case ZFS_DELEG_USER:
4600 case ZFS_DELEG_USER_SETS:
4601 case ZFS_DELEG_GROUP:
4602 case ZFS_DELEG_GROUP_SETS:
4603 case ZFS_DELEG_EVERYONE:
4604 case ZFS_DELEG_EVERYONE_SETS:
4605 avl_pool = fspset->fsps_who_perm_avl_pool;
4606 avl = fsperm->fsp_uge_avl;
4610 assert(!"unhandled zfs_deleg_who_type_t");
4614 who_perm_node_t *found_node = NULL;
4615 who_perm_node_t *node = safe_malloc(
4616 sizeof (who_perm_node_t));
4617 who_perm = &node->who_perm;
4618 uu_avl_index_t idx = 0;
4620 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4621 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4623 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4625 if (avl == fsperm->fsp_uge_avl) {
4627 struct passwd *p = NULL;
4628 struct group *g = NULL;
4629 const char *nice_name = NULL;
4631 switch (perm_type) {
4632 case ZFS_DELEG_USER_SETS:
4633 case ZFS_DELEG_USER:
4634 rid = atoi(perm_name);
4637 nice_name = p->pw_name;
4639 case ZFS_DELEG_GROUP_SETS:
4640 case ZFS_DELEG_GROUP:
4641 rid = atoi(perm_name);
4644 nice_name = g->gr_name;
4651 if (nice_name != NULL)
4653 node->who_perm.who_ug_name,
4657 uu_avl_insert(avl, node, idx);
4660 who_perm = &node->who_perm;
4664 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4671 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4673 nvpair_t *nvp = NULL;
4674 uu_avl_index_t idx = 0;
4676 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4677 nvlist_t *nvl2 = NULL;
4678 const char *fsname = nvpair_name(nvp);
4679 data_type_t type = nvpair_type(nvp);
4680 fs_perm_t *fsperm = NULL;
4681 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4685 fsperm = &node->fspn_fsperm;
4687 assert(DATA_TYPE_NVLIST == type);
4689 uu_list_node_init(node, &node->fspn_list_node,
4690 fspset->fsps_list_pool);
4692 idx = uu_list_numnodes(fspset->fsps_list);
4693 fs_perm_init(fsperm, fspset, fsname);
4695 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4698 (void) parse_fs_perm(fsperm, nvl2);
4700 uu_list_insert(fspset->fsps_list, node, idx);
4706 static inline const char *
4707 deleg_perm_comment(zfs_deleg_note_t note)
4709 const char *str = "";
4714 case ZFS_DELEG_NOTE_ALLOW:
4715 str = gettext("Must also have the permission that is being"
4716 "\n\t\t\t\tallowed");
4718 case ZFS_DELEG_NOTE_CLONE:
4719 str = gettext("Must also have the 'create' ability and 'mount'"
4720 "\n\t\t\t\tability in the origin file system");
4722 case ZFS_DELEG_NOTE_CREATE:
4723 str = gettext("Must also have the 'mount' ability");
4725 case ZFS_DELEG_NOTE_DESTROY:
4726 str = gettext("Must also have the 'mount' ability");
4728 case ZFS_DELEG_NOTE_DIFF:
4729 str = gettext("Allows lookup of paths within a dataset;"
4730 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4731 "\n\t\t\t\tin order to use zfs diff");
4733 case ZFS_DELEG_NOTE_HOLD:
4734 str = gettext("Allows adding a user hold to a snapshot");
4736 case ZFS_DELEG_NOTE_MOUNT:
4737 str = gettext("Allows mount/umount of ZFS datasets");
4739 case ZFS_DELEG_NOTE_PROMOTE:
4740 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4741 " 'promote' ability in the origin file system");
4743 case ZFS_DELEG_NOTE_RECEIVE:
4744 str = gettext("Must also have the 'mount' and 'create'"
4747 case ZFS_DELEG_NOTE_RELEASE:
4748 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4749 "might destroy the snapshot");
4751 case ZFS_DELEG_NOTE_RENAME:
4752 str = gettext("Must also have the 'mount' and 'create'"
4753 "\n\t\t\t\tability in the new parent");
4755 case ZFS_DELEG_NOTE_ROLLBACK:
4758 case ZFS_DELEG_NOTE_SEND:
4761 case ZFS_DELEG_NOTE_SHARE:
4762 str = gettext("Allows sharing file systems over NFS or SMB"
4763 "\n\t\t\t\tprotocols");
4765 case ZFS_DELEG_NOTE_SNAPSHOT:
4769 * case ZFS_DELEG_NOTE_VSCAN:
4770 * str = gettext("");
4774 case ZFS_DELEG_NOTE_GROUPQUOTA:
4775 str = gettext("Allows accessing any groupquota@... property");
4777 case ZFS_DELEG_NOTE_GROUPUSED:
4778 str = gettext("Allows reading any groupused@... property");
4780 case ZFS_DELEG_NOTE_USERPROP:
4781 str = gettext("Allows changing any user property");
4783 case ZFS_DELEG_NOTE_USERQUOTA:
4784 str = gettext("Allows accessing any userquota@... property");
4786 case ZFS_DELEG_NOTE_USERUSED:
4787 str = gettext("Allows reading any userused@... property");
4805 boolean_t recursive; /* unallow only */
4806 boolean_t prt_usage;
4808 boolean_t prt_perms;
4811 const char *dataset;
4815 prop_cmp(const void *a, const void *b)
4817 const char *str1 = *(const char **)a;
4818 const char *str2 = *(const char **)b;
4819 return (strcmp(str1, str2));
4823 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4825 const char *opt_desc[] = {
4826 "-h", gettext("show this help message and exit"),
4827 "-l", gettext("set permission locally"),
4828 "-d", gettext("set permission for descents"),
4829 "-u", gettext("set permission for user"),
4830 "-g", gettext("set permission for group"),
4831 "-e", gettext("set permission for everyone"),
4832 "-c", gettext("set create time permission"),
4833 "-s", gettext("define permission set"),
4835 "-r", gettext("remove permissions recursively"),
4837 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4838 size_t allow_size = unallow_size - 2;
4839 const char *props[ZFS_NUM_PROPS];
4842 FILE *fp = requested ? stdout : stderr;
4843 zprop_desc_t *pdtbl = zfs_prop_get_table();
4844 const char *fmt = gettext("%-16s %-14s\t%s\n");
4846 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4848 (void) fprintf(fp, gettext("Options:\n"));
4849 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4850 const char *opt = opt_desc[i++];
4851 const char *optdsc = opt_desc[i];
4852 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4855 (void) fprintf(fp, gettext("\nThe following permissions are "
4857 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4859 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4860 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4861 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4862 const char *perm_type = deleg_perm_type(perm_note);
4863 const char *perm_comment = deleg_perm_comment(perm_note);
4864 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4867 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4868 zprop_desc_t *pd = &pdtbl[i];
4869 if (pd->pd_visible != B_TRUE)
4872 if (pd->pd_attr == PROP_READONLY)
4875 props[count++] = pd->pd_name;
4877 props[count] = NULL;
4879 qsort(props, count, sizeof (char *), prop_cmp);
4881 for (i = 0; i < count; i++)
4882 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4885 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4887 exit(requested ? 0 : 2);
4890 static inline const char *
4891 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4894 if (un && argc == expected_argc - 1)
4896 else if (argc == expected_argc)
4897 *permsp = argv[argc - 2];
4899 allow_usage(un, B_FALSE,
4900 gettext("wrong number of parameters\n"));
4902 return (argv[argc - 1]);
4906 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4908 int uge_sum = opts->user + opts->group + opts->everyone;
4909 int csuge_sum = opts->create + opts->set + uge_sum;
4910 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4911 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4914 allow_usage(un, B_FALSE,
4915 gettext("-u, -g, and -e are mutually exclusive\n"));
4917 if (opts->prt_usage) {
4918 if (argc == 0 && all_sum == 0)
4919 allow_usage(un, B_TRUE, NULL);
4926 allow_usage(un, B_FALSE,
4927 gettext("invalid options combined with -s\n"));
4929 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4930 if (argv[0][0] != '@')
4931 allow_usage(un, B_FALSE,
4932 gettext("invalid set name: missing '@' prefix\n"));
4933 opts->who = argv[0];
4934 } else if (opts->create) {
4935 if (ldcsuge_sum > 1)
4936 allow_usage(un, B_FALSE,
4937 gettext("invalid options combined with -c\n"));
4938 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4939 } else if (opts->everyone) {
4941 allow_usage(un, B_FALSE,
4942 gettext("invalid options combined with -e\n"));
4943 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4944 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4946 opts->everyone = B_TRUE;
4949 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4950 } else if (argc == 1 && !un) {
4951 opts->prt_perms = B_TRUE;
4952 opts->dataset = argv[argc-1];
4954 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4955 opts->who = argv[0];
4958 if (!opts->local && !opts->descend) {
4959 opts->local = B_TRUE;
4960 opts->descend = B_TRUE;
4965 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4966 const char *who, char *perms, nvlist_t *top_nvl)
4969 char ld[2] = { '\0', '\0' };
4970 char who_buf[MAXNAMELEN + 32];
4971 char base_type = '\0';
4972 char set_type = '\0';
4973 nvlist_t *base_nvl = NULL;
4974 nvlist_t *set_nvl = NULL;
4977 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4979 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4983 case ZFS_DELEG_NAMED_SET_SETS:
4984 case ZFS_DELEG_NAMED_SET:
4985 set_type = ZFS_DELEG_NAMED_SET_SETS;
4986 base_type = ZFS_DELEG_NAMED_SET;
4987 ld[0] = ZFS_DELEG_NA;
4989 case ZFS_DELEG_CREATE_SETS:
4990 case ZFS_DELEG_CREATE:
4991 set_type = ZFS_DELEG_CREATE_SETS;
4992 base_type = ZFS_DELEG_CREATE;
4993 ld[0] = ZFS_DELEG_NA;
4995 case ZFS_DELEG_USER_SETS:
4996 case ZFS_DELEG_USER:
4997 set_type = ZFS_DELEG_USER_SETS;
4998 base_type = ZFS_DELEG_USER;
5000 ld[0] = ZFS_DELEG_LOCAL;
5002 ld[1] = ZFS_DELEG_DESCENDENT;
5004 case ZFS_DELEG_GROUP_SETS:
5005 case ZFS_DELEG_GROUP:
5006 set_type = ZFS_DELEG_GROUP_SETS;
5007 base_type = ZFS_DELEG_GROUP;
5009 ld[0] = ZFS_DELEG_LOCAL;
5011 ld[1] = ZFS_DELEG_DESCENDENT;
5013 case ZFS_DELEG_EVERYONE_SETS:
5014 case ZFS_DELEG_EVERYONE:
5015 set_type = ZFS_DELEG_EVERYONE_SETS;
5016 base_type = ZFS_DELEG_EVERYONE;
5018 ld[0] = ZFS_DELEG_LOCAL;
5020 ld[1] = ZFS_DELEG_DESCENDENT;
5024 assert(set_type != '\0' && base_type != '\0');
5027 if (perms != NULL) {
5029 char *end = curr + strlen(perms);
5031 while (curr < end) {
5032 char *delim = strchr(curr, ',');
5043 (void) nvlist_add_boolean(nvl, curr);
5049 for (i = 0; i < 2; i++) {
5050 char locality = ld[i];
5054 if (!nvlist_empty(base_nvl)) {
5056 (void) snprintf(who_buf,
5057 sizeof (who_buf), "%c%c$%s",
5058 base_type, locality, who);
5060 (void) snprintf(who_buf,
5061 sizeof (who_buf), "%c%c$",
5062 base_type, locality);
5064 (void) nvlist_add_nvlist(top_nvl, who_buf,
5069 if (!nvlist_empty(set_nvl)) {
5071 (void) snprintf(who_buf,
5072 sizeof (who_buf), "%c%c$%s",
5073 set_type, locality, who);
5075 (void) snprintf(who_buf,
5076 sizeof (who_buf), "%c%c$",
5077 set_type, locality);
5079 (void) nvlist_add_nvlist(top_nvl, who_buf,
5084 for (i = 0; i < 2; i++) {
5085 char locality = ld[i];
5090 (void) snprintf(who_buf, sizeof (who_buf),
5091 "%c%c$%s", base_type, locality, who);
5093 (void) snprintf(who_buf, sizeof (who_buf),
5094 "%c%c$", base_type, locality);
5095 (void) nvlist_add_boolean(top_nvl, who_buf);
5098 (void) snprintf(who_buf, sizeof (who_buf),
5099 "%c%c$%s", set_type, locality, who);
5101 (void) snprintf(who_buf, sizeof (who_buf),
5102 "%c%c$", set_type, locality);
5103 (void) nvlist_add_boolean(top_nvl, who_buf);
5109 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5111 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5115 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5116 opts->descend, opts->who, opts->perms, *nvlp);
5117 } else if (opts->create) {
5118 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5119 opts->descend, NULL, opts->perms, *nvlp);
5120 } else if (opts->everyone) {
5121 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5122 opts->descend, NULL, opts->perms, *nvlp);
5124 char *curr = opts->who;
5125 char *end = curr + strlen(curr);
5127 while (curr < end) {
5129 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5131 char *delim = strchr(curr, ',');
5134 struct passwd *p = NULL;
5135 struct group *g = NULL;
5143 rid = (uid_t)strtol(curr, &endch, 0);
5145 who_type = ZFS_DELEG_USER;
5154 (void) snprintf(errbuf, 256, gettext(
5155 "invalid user %s"), curr);
5156 allow_usage(un, B_TRUE, errbuf);
5158 } else if (opts->group) {
5159 who_type = ZFS_DELEG_GROUP;
5168 (void) snprintf(errbuf, 256, gettext(
5169 "invalid group %s"), curr);
5170 allow_usage(un, B_TRUE, errbuf);
5173 if (*endch != '\0') {
5180 if (*endch != '\0') {
5188 who_type = ZFS_DELEG_USER;
5190 } else if (g != NULL) {
5191 who_type = ZFS_DELEG_GROUP;
5194 (void) snprintf(errbuf, 256, gettext(
5195 "invalid user/group %s"), curr);
5196 allow_usage(un, B_TRUE, errbuf);
5200 (void) sprintf(id, "%u", rid);
5203 store_allow_perm(who_type, opts->local,
5204 opts->descend, who, opts->perms, *nvlp);
5213 print_set_creat_perms(uu_avl_t *who_avl)
5215 const char *sc_title[] = {
5216 gettext("Permission sets:\n"),
5217 gettext("Create time permissions:\n"),
5220 const char **title_ptr = sc_title;
5221 who_perm_node_t *who_node = NULL;
5222 int prev_weight = -1;
5224 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5225 who_node = uu_avl_next(who_avl, who_node)) {
5226 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5227 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5228 const char *who_name = who_node->who_perm.who_name;
5229 int weight = who_type2weight(who_type);
5230 boolean_t first = B_TRUE;
5231 deleg_perm_node_t *deleg_node;
5233 if (prev_weight != weight) {
5234 (void) printf(*title_ptr++);
5235 prev_weight = weight;
5238 if (who_name == NULL || strnlen(who_name, 1) == 0)
5239 (void) printf("\t");
5241 (void) printf("\t%s ", who_name);
5243 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5244 deleg_node = uu_avl_next(avl, deleg_node)) {
5247 deleg_node->dpn_perm.dp_name);
5250 (void) printf(",%s",
5251 deleg_node->dpn_perm.dp_name);
5254 (void) printf("\n");
5259 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5262 who_perm_node_t *who_node = NULL;
5263 boolean_t prt_title = B_TRUE;
5264 uu_avl_walk_t *walk;
5266 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5269 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5270 const char *who_name = who_node->who_perm.who_name;
5271 const char *nice_who_name = who_node->who_perm.who_ug_name;
5272 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5273 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5275 deleg_perm_node_t *deleg_node;
5276 boolean_t prt_who = B_TRUE;
5278 for (deleg_node = uu_avl_first(avl);
5280 deleg_node = uu_avl_next(avl, deleg_node)) {
5281 if (local != deleg_node->dpn_perm.dp_local ||
5282 descend != deleg_node->dpn_perm.dp_descend)
5286 const char *who = NULL;
5288 prt_title = B_FALSE;
5289 (void) printf(title);
5293 case ZFS_DELEG_USER_SETS:
5294 case ZFS_DELEG_USER:
5295 who = gettext("user");
5297 who_name = nice_who_name;
5299 case ZFS_DELEG_GROUP_SETS:
5300 case ZFS_DELEG_GROUP:
5301 who = gettext("group");
5303 who_name = nice_who_name;
5305 case ZFS_DELEG_EVERYONE_SETS:
5306 case ZFS_DELEG_EVERYONE:
5307 who = gettext("everyone");
5312 assert(who != NULL);
5316 if (who_name == NULL)
5317 (void) printf("\t%s", who);
5319 (void) printf("\t%s %s", who, who_name);
5322 (void) printf("%c%s", delim,
5323 deleg_node->dpn_perm.dp_name);
5328 (void) printf("\n");
5331 uu_avl_walk_end(walk);
5335 print_fs_perms(fs_perm_set_t *fspset)
5337 fs_perm_node_t *node = NULL;
5338 char buf[MAXNAMELEN + 32];
5339 const char *dsname = buf;
5341 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5342 node = uu_list_next(fspset->fsps_list, node)) {
5343 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5344 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5347 (void) snprintf(buf, sizeof (buf),
5348 gettext("---- Permissions on %s "),
5349 node->fspn_fsperm.fsp_name);
5350 (void) printf(dsname);
5351 left = 70 - strlen(buf);
5354 (void) printf("\n");
5356 print_set_creat_perms(sc_avl);
5357 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5358 gettext("Local permissions:\n"));
5359 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5360 gettext("Descendent permissions:\n"));
5361 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5362 gettext("Local+Descendent permissions:\n"));
5366 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5368 struct deleg_perms {
5374 set_deleg_perms(zfs_handle_t *zhp, void *data)
5376 struct deleg_perms *perms = (struct deleg_perms *)data;
5377 zfs_type_t zfs_type = zfs_get_type(zhp);
5379 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5382 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5386 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5389 nvlist_t *perm_nvl = NULL;
5390 nvlist_t *update_perm_nvl = NULL;
5393 struct allow_opts opts = { 0 };
5395 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5398 while ((c = getopt(argc, argv, optstr)) != -1) {
5401 opts.local = B_TRUE;
5404 opts.descend = B_TRUE;
5410 opts.group = B_TRUE;
5413 opts.everyone = B_TRUE;
5419 opts.create = B_TRUE;
5422 opts.recursive = B_TRUE;
5425 (void) fprintf(stderr, gettext("missing argument for "
5426 "'%c' option\n"), optopt);
5430 opts.prt_usage = B_TRUE;
5433 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5442 /* check arguments */
5443 parse_allow_args(argc, argv, un, &opts);
5445 /* try to open the dataset */
5446 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5447 ZFS_TYPE_VOLUME)) == NULL) {
5448 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5453 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5456 fs_perm_set_init(&fs_perm_set);
5457 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5458 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5463 print_fs_perms(&fs_perm_set);
5465 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5466 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5469 if (un && opts.recursive) {
5470 struct deleg_perms data = { un, update_perm_nvl };
5471 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5480 nvlist_free(perm_nvl);
5481 nvlist_free(update_perm_nvl);
5483 fs_perm_set_fini(&fs_perm_set);
5491 zfs_do_allow(int argc, char **argv)
5493 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5497 zfs_do_unallow(int argc, char **argv)
5499 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5503 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5508 boolean_t recursive = B_FALSE;
5509 const char *opts = holding ? "rt" : "r";
5513 while ((c = getopt(argc, argv, opts)) != -1) {
5519 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5528 /* check number of arguments */
5536 if (holding && tag[0] == '.') {
5537 /* tags starting with '.' are reserved for libzfs */
5538 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5542 for (i = 0; i < argc; ++i) {
5544 char parent[ZFS_MAX_DATASET_NAME_LEN];
5546 char *path = argv[i];
5548 delim = strchr(path, '@');
5549 if (delim == NULL) {
5550 (void) fprintf(stderr,
5551 gettext("'%s' is not a snapshot\n"), path);
5555 (void) strncpy(parent, path, delim - path);
5556 parent[delim - path] = '\0';
5558 zhp = zfs_open(g_zfs, parent,
5559 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5565 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5568 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5574 return (errors != 0);
5578 * zfs hold [-r] [-t] <tag> <snap> ...
5580 * -r Recursively hold
5582 * Apply a user-hold with the given tag to the list of snapshots.
5585 zfs_do_hold(int argc, char **argv)
5587 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5591 * zfs release [-r] <tag> <snap> ...
5593 * -r Recursively release
5595 * Release a user-hold with the given tag from the list of snapshots.
5598 zfs_do_release(int argc, char **argv)
5600 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5603 typedef struct holds_cbdata {
5604 boolean_t cb_recursive;
5605 const char *cb_snapname;
5607 size_t cb_max_namelen;
5608 size_t cb_max_taglen;
5611 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5612 #define DATETIME_BUF_LEN (32)
5617 print_holds(boolean_t scripted, boolean_t literal, size_t nwidth,
5618 size_t tagwidth, nvlist_t *nvl)
5621 nvpair_t *nvp = NULL;
5622 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5626 for (i = 0; i < 3; i++) {
5627 col = gettext(hdr_cols[i]);
5629 (void) printf("%-*s ", i ? tagwidth : nwidth,
5632 (void) printf("%s\n", col);
5636 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5637 char *zname = nvpair_name(nvp);
5639 nvpair_t *nvp2 = NULL;
5640 (void) nvpair_value_nvlist(nvp, &nvl2);
5641 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5642 char tsbuf[DATETIME_BUF_LEN];
5643 char *tagname = nvpair_name(nvp2);
5648 (void) nvpair_value_uint64(nvp2, &val);
5650 snprintf(tsbuf, DATETIME_BUF_LEN, "%llu", val);
5653 (void) localtime_r(&time, &t);
5654 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5655 gettext(STRFTIME_FMT_STR), &t);
5659 (void) printf("%s\t%s\t%s\n", zname,
5662 (void) printf("%-*s %-*s %s\n", nwidth,
5663 zname, tagwidth, tagname, tsbuf);
5670 * Generic callback function to list a dataset or snapshot.
5673 holds_callback(zfs_handle_t *zhp, void *data)
5675 holds_cbdata_t *cbp = data;
5676 nvlist_t *top_nvl = *cbp->cb_nvlp;
5677 nvlist_t *nvl = NULL;
5678 nvpair_t *nvp = NULL;
5679 const char *zname = zfs_get_name(zhp);
5680 size_t znamelen = strlen(zname);
5682 if (cbp->cb_recursive && cbp->cb_snapname != NULL) {
5683 const char *snapname;
5684 char *delim = strchr(zname, '@');
5688 snapname = delim + 1;
5689 if (strcmp(cbp->cb_snapname, snapname))
5693 if (zfs_get_holds(zhp, &nvl) != 0)
5696 if (znamelen > cbp->cb_max_namelen)
5697 cbp->cb_max_namelen = znamelen;
5699 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5700 const char *tag = nvpair_name(nvp);
5701 size_t taglen = strlen(tag);
5702 if (taglen > cbp->cb_max_taglen)
5703 cbp->cb_max_taglen = taglen;
5706 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5710 * zfs holds [-Hp] [-r | -d max] <dataset|snap> ...
5712 * -H Suppress header output
5713 * -p Output literal values
5714 * -r Recursively search for holds
5715 * -d max Limit depth of recursive search
5718 zfs_do_holds(int argc, char **argv)
5723 boolean_t scripted = B_FALSE;
5724 boolean_t literal = B_FALSE;
5725 boolean_t recursive = B_FALSE;
5726 const char *opts = "d:rHp";
5729 int types = ZFS_TYPE_SNAPSHOT;
5730 holds_cbdata_t cb = { 0 };
5737 while ((c = getopt(argc, argv, opts)) != -1) {
5740 limit = parse_depth(optarg, &flags);
5753 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5760 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5761 flags |= ZFS_ITER_RECURSE;
5767 /* check number of arguments */
5771 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5774 for (i = 0; i < argc; ++i) {
5775 char *snapshot = argv[i];
5777 const char *snapname = NULL;
5779 delim = strchr(snapshot, '@');
5780 if (delim != NULL) {
5781 snapname = delim + 1;
5783 snapshot[delim - snapshot] = '\0';
5786 cb.cb_recursive = recursive;
5787 cb.cb_snapname = snapname;
5791 * 1. collect holds data, set format options
5793 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5794 holds_callback, &cb);
5800 * 2. print holds data
5802 print_holds(scripted, literal, cb.cb_max_namelen, cb.cb_max_taglen,
5805 if (nvlist_empty(nvl))
5806 (void) printf(gettext("no datasets available\n"));
5810 return (0 != errors);
5813 #define CHECK_SPINNER 30
5814 #define SPINNER_TIME 3 /* seconds */
5815 #define MOUNT_TIME 5 /* seconds */
5818 get_one_dataset(zfs_handle_t *zhp, void *data)
5820 static char *spin[] = { "-", "\\", "|", "/" };
5821 static int spinval = 0;
5822 static int spincheck = 0;
5823 static time_t last_spin_time = (time_t)0;
5824 get_all_cb_t *cbp = data;
5825 zfs_type_t type = zfs_get_type(zhp);
5827 if (cbp->cb_verbose) {
5828 if (--spincheck < 0) {
5829 time_t now = time(NULL);
5830 if (last_spin_time + SPINNER_TIME < now) {
5831 update_progress(spin[spinval++ % 4]);
5832 last_spin_time = now;
5834 spincheck = CHECK_SPINNER;
5839 * Interate over any nested datasets.
5841 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5847 * Skip any datasets whose type does not match.
5849 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5853 libzfs_add_handle(cbp, zhp);
5854 assert(cbp->cb_used <= cbp->cb_alloc);
5860 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5862 get_all_cb_t cb = { 0 };
5863 cb.cb_verbose = verbose;
5864 cb.cb_getone = get_one_dataset;
5867 set_progress_header(gettext("Reading ZFS config"));
5868 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5870 *dslist = cb.cb_handles;
5871 *count = cb.cb_used;
5874 finish_progress(gettext("done."));
5878 * Generic callback for sharing or mounting filesystems. Because the code is so
5879 * similar, we have a common function with an extra parameter to determine which
5880 * mode we are using.
5882 #define OP_SHARE 0x1
5883 #define OP_MOUNT 0x2
5886 * Share or mount a dataset.
5889 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5890 boolean_t explicit, const char *options)
5892 char mountpoint[ZFS_MAXPROPLEN];
5893 char shareopts[ZFS_MAXPROPLEN];
5894 char smbshareopts[ZFS_MAXPROPLEN];
5895 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5897 uint64_t zoned, canmount;
5898 boolean_t shared_nfs, shared_smb;
5900 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5903 * Check to make sure we can mount/share this dataset. If we
5904 * are in the global zone and the filesystem is exported to a
5905 * local zone, or if we are in a local zone and the
5906 * filesystem is not exported, then it is an error.
5908 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5910 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5914 (void) fprintf(stderr, gettext("cannot %s '%s': "
5915 "dataset is exported to a local zone\n"), cmdname,
5919 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5923 (void) fprintf(stderr, gettext("cannot %s '%s': "
5924 "permission denied\n"), cmdname,
5930 * Ignore any filesystems which don't apply to us. This
5931 * includes those with a legacy mountpoint, or those with
5932 * legacy share options.
5934 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5935 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5936 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5937 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5938 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5939 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5941 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5942 strcmp(smbshareopts, "off") == 0) {
5946 (void) fprintf(stderr, gettext("cannot share '%s': "
5947 "legacy share\n"), zfs_get_name(zhp));
5948 (void) fprintf(stderr, gettext("to "
5949 "share this filesystem set "
5950 "sharenfs property on\n"));
5955 * We cannot share or mount legacy filesystems. If the
5956 * shareopts is non-legacy but the mountpoint is legacy, we
5957 * treat it as a legacy share.
5959 if (strcmp(mountpoint, "legacy") == 0) {
5963 (void) fprintf(stderr, gettext("cannot %s '%s': "
5964 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5965 (void) fprintf(stderr, gettext("use %s(8) to "
5966 "%s this filesystem\n"), cmdname, cmdname);
5970 if (strcmp(mountpoint, "none") == 0) {
5974 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5975 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5980 * canmount explicit outcome
5981 * on no pass through
5982 * on yes pass through
5984 * off yes display error, return 1
5985 * noauto no return 0
5986 * noauto yes pass through
5988 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5989 if (canmount == ZFS_CANMOUNT_OFF) {
5993 (void) fprintf(stderr, gettext("cannot %s '%s': "
5994 "'canmount' property is set to 'off'\n"), cmdname,
5997 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
6002 * If this filesystem is inconsistent and has a receive resume
6003 * token, we can not mount it.
6005 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
6006 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
6007 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
6011 (void) fprintf(stderr, gettext("cannot %s '%s': "
6012 "Contains partially-completed state from "
6013 "\"zfs receive -r\", which can be resumed with "
6014 "\"zfs send -t\"\n"),
6015 cmdname, zfs_get_name(zhp));
6020 * At this point, we have verified that the mountpoint and/or
6021 * shareopts are appropriate for auto management. If the
6022 * filesystem is already mounted or shared, return (failing
6023 * for explicit requests); otherwise mount or share the
6029 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
6030 shared_smb = zfs_is_shared_smb(zhp, NULL);
6032 if ((shared_nfs && shared_smb) ||
6033 (shared_nfs && strcmp(shareopts, "on") == 0 &&
6034 strcmp(smbshareopts, "off") == 0) ||
6035 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
6036 strcmp(shareopts, "off") == 0)) {
6040 (void) fprintf(stderr, gettext("cannot share "
6041 "'%s': filesystem already shared\n"),
6046 if (!zfs_is_mounted(zhp, NULL) &&
6047 zfs_mount(zhp, NULL, 0) != 0)
6050 if (protocol == NULL) {
6051 if (zfs_shareall(zhp) != 0)
6053 } else if (strcmp(protocol, "nfs") == 0) {
6054 if (zfs_share_nfs(zhp))
6056 } else if (strcmp(protocol, "smb") == 0) {
6057 if (zfs_share_smb(zhp))
6060 (void) fprintf(stderr, gettext("cannot share "
6061 "'%s': invalid share type '%s' "
6063 zfs_get_name(zhp), protocol);
6070 if (options == NULL)
6071 mnt.mnt_mntopts = "";
6073 mnt.mnt_mntopts = (char *)options;
6075 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6076 zfs_is_mounted(zhp, NULL)) {
6080 (void) fprintf(stderr, gettext("cannot mount "
6081 "'%s': filesystem already mounted\n"),
6086 if (zfs_mount(zhp, options, flags) != 0)
6095 * Reports progress in the form "(current/total)". Not thread-safe.
6098 report_mount_progress(int current, int total)
6100 static time_t last_progress_time = 0;
6101 time_t now = time(NULL);
6104 /* report 1..n instead of 0..n-1 */
6107 /* display header if we're here for the first time */
6109 set_progress_header(gettext("Mounting ZFS filesystems"));
6110 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6111 /* too soon to report again */
6115 last_progress_time = now;
6117 (void) sprintf(info, "(%d/%d)", current, total);
6119 if (current == total)
6120 finish_progress(info);
6122 update_progress(info);
6126 append_options(char *mntopts, char *newopts)
6128 int len = strlen(mntopts);
6130 /* original length plus new string to append plus 1 for the comma */
6131 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6132 (void) fprintf(stderr, gettext("the opts argument for "
6133 "'%c' option is too long (more than %d chars)\n"),
6134 "-o", MNT_LINE_MAX);
6139 mntopts[len++] = ',';
6141 (void) strcpy(&mntopts[len], newopts);
6145 share_mount(int op, int argc, char **argv)
6148 boolean_t verbose = B_FALSE;
6150 char *options = NULL;
6154 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6164 if (*optarg == '\0') {
6165 (void) fprintf(stderr, gettext("empty mount "
6166 "options (-o) specified\n"));
6170 if (options == NULL)
6171 options = safe_malloc(MNT_LINE_MAX + 1);
6173 /* option validation is done later */
6174 append_options(options, optarg);
6178 warnx("no overlay mounts support on FreeBSD, ignoring");
6181 (void) fprintf(stderr, gettext("missing argument for "
6182 "'%c' option\n"), optopt);
6186 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6195 /* check number of arguments */
6197 zfs_handle_t **dslist = NULL;
6198 size_t i, count = 0;
6199 char *protocol = NULL;
6201 if (op == OP_SHARE && argc > 0) {
6202 if (strcmp(argv[0], "nfs") != 0 &&
6203 strcmp(argv[0], "smb") != 0) {
6204 (void) fprintf(stderr, gettext("share type "
6205 "must be 'nfs' or 'smb'\n"));
6214 (void) fprintf(stderr, gettext("too many arguments\n"));
6218 start_progress_timer();
6219 get_all_datasets(&dslist, &count, verbose);
6224 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6226 sa_init_selective_arg_t sharearg;
6227 sharearg.zhandle_arr = dslist;
6228 sharearg.zhandle_len = count;
6229 if ((ret = zfs_init_libshare_arg(zfs_get_handle(dslist[0]),
6230 SA_INIT_SHARE_API_SELECTIVE, &sharearg)) != SA_OK) {
6231 (void) fprintf(stderr,
6232 gettext("Could not initialize libshare, %d"), ret);
6237 for (i = 0; i < count; i++) {
6239 report_mount_progress(i, count);
6241 if (share_mount_one(dslist[i], op, flags, protocol,
6242 B_FALSE, options) != 0)
6244 zfs_close(dslist[i]);
6248 } else if (argc == 0) {
6249 struct mnttab entry;
6251 if ((op == OP_SHARE) || (options != NULL)) {
6252 (void) fprintf(stderr, gettext("missing filesystem "
6253 "argument (specify -a for all)\n"));
6258 * When mount is given no arguments, go through /etc/mnttab and
6259 * display any active ZFS mounts. We hide any snapshots, since
6260 * they are controlled automatically.
6262 rewind(mnttab_file);
6263 while (getmntent(mnttab_file, &entry) == 0) {
6264 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6265 strchr(entry.mnt_special, '@') != NULL)
6268 (void) printf("%-30s %s\n", entry.mnt_special,
6276 (void) fprintf(stderr,
6277 gettext("too many arguments\n"));
6281 if ((zhp = zfs_open(g_zfs, argv[0],
6282 ZFS_TYPE_FILESYSTEM)) == NULL) {
6285 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6295 * zfs mount -a [nfs]
6296 * zfs mount filesystem
6298 * Mount all filesystems, or mount the given filesystem.
6301 zfs_do_mount(int argc, char **argv)
6303 return (share_mount(OP_MOUNT, argc, argv));
6307 * zfs share -a [nfs | smb]
6308 * zfs share filesystem
6310 * Share all filesystems, or share the given filesystem.
6313 zfs_do_share(int argc, char **argv)
6315 return (share_mount(OP_SHARE, argc, argv));
6318 typedef struct unshare_unmount_node {
6319 zfs_handle_t *un_zhp;
6321 uu_avl_node_t un_avlnode;
6322 } unshare_unmount_node_t;
6326 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6328 const unshare_unmount_node_t *l = larg;
6329 const unshare_unmount_node_t *r = rarg;
6331 return (strcmp(l->un_mountp, r->un_mountp));
6335 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6336 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6337 * and unmount it appropriately.
6340 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6344 struct stat64 statbuf;
6345 struct extmnttab entry;
6346 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6350 * Search for the path in /etc/mnttab. Rather than looking for the
6351 * specific path, which can be fooled by non-standard paths (i.e. ".."
6352 * or "//"), we stat() the path and search for the corresponding
6353 * (major,minor) device pair.
6355 if (stat64(path, &statbuf) != 0) {
6356 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6357 cmdname, path, strerror(errno));
6360 path_inode = statbuf.st_ino;
6363 * Search for the given (major,minor) pair in the mount table.
6366 rewind(mnttab_file);
6367 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6368 if (entry.mnt_major == major(statbuf.st_dev) &&
6369 entry.mnt_minor == minor(statbuf.st_dev))
6376 if (statfs(path, &sfs) != 0) {
6377 (void) fprintf(stderr, "%s: %s\n", path,
6381 statfs2mnttab(&sfs, &entry);
6385 if (op == OP_SHARE) {
6386 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6387 "currently mounted\n"), cmdname, path);
6390 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6392 if ((ret = umount2(path, flags)) != 0)
6393 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6398 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6399 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6400 "filesystem\n"), cmdname, path);
6404 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6405 ZFS_TYPE_FILESYSTEM)) == NULL)
6409 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6410 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6411 cmdname, path, strerror(errno));
6413 } else if (statbuf.st_ino != path_inode) {
6414 (void) fprintf(stderr, gettext("cannot "
6415 "%s '%s': not a mountpoint\n"), cmdname, path);
6419 if (op == OP_SHARE) {
6420 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6421 char smbshare_prop[ZFS_MAXPROPLEN];
6423 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6424 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6425 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6426 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6428 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6429 strcmp(smbshare_prop, "off") == 0) {
6430 (void) fprintf(stderr, gettext("cannot unshare "
6431 "'%s': legacy share\n"), path);
6433 (void) fprintf(stderr, gettext("use "
6434 "unshare(1M) to unshare this filesystem\n"));
6436 } else if (!zfs_is_shared(zhp)) {
6437 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6438 "not currently shared\n"), path);
6440 ret = zfs_unshareall_bypath(zhp, path);
6443 char mtpt_prop[ZFS_MAXPROPLEN];
6445 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6446 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6449 ret = zfs_unmount(zhp, NULL, flags);
6450 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6451 (void) fprintf(stderr, gettext("cannot unmount "
6452 "'%s': legacy mountpoint\n"),
6454 (void) fprintf(stderr, gettext("use umount(8) "
6455 "to unmount this filesystem\n"));
6457 ret = zfs_unmountall(zhp, flags);
6468 * Generic callback for unsharing or unmounting a filesystem.
6471 unshare_unmount(int op, int argc, char **argv)
6478 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6479 char sharesmb[ZFS_MAXPROPLEN];
6482 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6491 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6502 * We could make use of zfs_for_each() to walk all datasets in
6503 * the system, but this would be very inefficient, especially
6504 * since we would have to linearly search /etc/mnttab for each
6505 * one. Instead, do one pass through /etc/mnttab looking for
6506 * zfs entries and call zfs_unmount() for each one.
6508 * Things get a little tricky if the administrator has created
6509 * mountpoints beneath other ZFS filesystems. In this case, we
6510 * have to unmount the deepest filesystems first. To accomplish
6511 * this, we place all the mountpoints in an AVL tree sorted by
6512 * the special type (dataset name), and walk the result in
6513 * reverse to make sure to get any snapshots first.
6515 struct mnttab entry;
6516 uu_avl_pool_t *pool;
6517 uu_avl_t *tree = NULL;
6518 unshare_unmount_node_t *node;
6520 uu_avl_walk_t *walk;
6523 (void) fprintf(stderr, gettext("too many arguments\n"));
6527 if (((pool = uu_avl_pool_create("unmount_pool",
6528 sizeof (unshare_unmount_node_t),
6529 offsetof(unshare_unmount_node_t, un_avlnode),
6530 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6531 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6534 rewind(mnttab_file);
6535 while (getmntent(mnttab_file, &entry) == 0) {
6537 /* ignore non-ZFS entries */
6538 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6541 /* ignore snapshots */
6542 if (strchr(entry.mnt_special, '@') != NULL)
6545 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6546 ZFS_TYPE_FILESYSTEM)) == NULL) {
6552 * Ignore datasets that are excluded/restricted by
6555 if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
6562 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6564 sizeof (nfs_mnt_prop),
6565 NULL, NULL, 0, B_FALSE) == 0);
6566 if (strcmp(nfs_mnt_prop, "off") != 0)
6568 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6570 sizeof (nfs_mnt_prop),
6571 NULL, NULL, 0, B_FALSE) == 0);
6572 if (strcmp(nfs_mnt_prop, "off") == 0)
6576 /* Ignore legacy mounts */
6577 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6579 sizeof (nfs_mnt_prop),
6580 NULL, NULL, 0, B_FALSE) == 0);
6581 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6583 /* Ignore canmount=noauto mounts */
6584 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6585 ZFS_CANMOUNT_NOAUTO)
6591 node = safe_malloc(sizeof (unshare_unmount_node_t));
6593 node->un_mountp = safe_strdup(entry.mnt_mountp);
6595 uu_avl_node_init(node, &node->un_avlnode, pool);
6597 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6598 uu_avl_insert(tree, node, idx);
6600 zfs_close(node->un_zhp);
6601 free(node->un_mountp);
6607 * Walk the AVL tree in reverse, unmounting each filesystem and
6608 * removing it from the AVL tree in the process.
6610 if ((walk = uu_avl_walk_start(tree,
6611 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6614 while ((node = uu_avl_walk_next(walk)) != NULL) {
6615 uu_avl_remove(tree, node);
6619 if (zfs_unshareall_bypath(node->un_zhp,
6620 node->un_mountp) != 0)
6625 if (zfs_unmount(node->un_zhp,
6626 node->un_mountp, flags) != 0)
6631 zfs_close(node->un_zhp);
6632 free(node->un_mountp);
6636 uu_avl_walk_end(walk);
6637 uu_avl_destroy(tree);
6638 uu_avl_pool_destroy(pool);
6643 (void) fprintf(stderr,
6644 gettext("missing filesystem argument\n"));
6646 (void) fprintf(stderr,
6647 gettext("too many arguments\n"));
6652 * We have an argument, but it may be a full path or a ZFS
6653 * filesystem. Pass full paths off to unmount_path() (shared by
6654 * manual_unmount), otherwise open the filesystem and pass to
6657 if (argv[0][0] == '/')
6658 return (unshare_unmount_path(op, argv[0],
6661 if ((zhp = zfs_open(g_zfs, argv[0],
6662 ZFS_TYPE_FILESYSTEM)) == NULL)
6665 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6666 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6667 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6668 NULL, 0, B_FALSE) == 0);
6672 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6674 sizeof (nfs_mnt_prop),
6675 NULL, NULL, 0, B_FALSE) == 0);
6676 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6677 sharesmb, sizeof (sharesmb), NULL, NULL,
6680 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6681 strcmp(sharesmb, "off") == 0) {
6682 (void) fprintf(stderr, gettext("cannot "
6683 "unshare '%s': legacy share\n"),
6686 (void) fprintf(stderr, gettext("use "
6687 "unshare(1M) to unshare this "
6691 } else if (!zfs_is_shared(zhp)) {
6692 (void) fprintf(stderr, gettext("cannot "
6693 "unshare '%s': not currently "
6694 "shared\n"), zfs_get_name(zhp));
6696 } else if (zfs_unshareall(zhp) != 0) {
6702 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6703 (void) fprintf(stderr, gettext("cannot "
6704 "unmount '%s': legacy "
6705 "mountpoint\n"), zfs_get_name(zhp));
6706 (void) fprintf(stderr, gettext("use "
6707 "umount(8) to unmount this "
6710 } else if (!zfs_is_mounted(zhp, NULL)) {
6711 (void) fprintf(stderr, gettext("cannot "
6712 "unmount '%s': not currently "
6716 } else if (zfs_unmountall(zhp, flags) != 0) {
6730 * zfs unmount filesystem
6732 * Unmount all filesystems, or a specific ZFS filesystem.
6735 zfs_do_unmount(int argc, char **argv)
6737 return (unshare_unmount(OP_MOUNT, argc, argv));
6742 * zfs unshare filesystem
6744 * Unshare all filesystems, or a specific ZFS filesystem.
6747 zfs_do_unshare(int argc, char **argv)
6749 return (unshare_unmount(OP_SHARE, argc, argv));
6753 * Attach/detach the given dataset to/from the given jail
6757 do_jail(int argc, char **argv, int attach)
6762 /* check number of arguments */
6764 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6768 (void) fprintf(stderr, gettext("too many arguments\n"));
6772 jailid = jail_getid(argv[1]);
6774 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6778 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6782 ret = (zfs_jail(zhp, jailid, attach) != 0);
6789 * zfs jail jailid filesystem
6791 * Attach the given dataset to the given jail
6795 zfs_do_jail(int argc, char **argv)
6798 return (do_jail(argc, argv, 1));
6802 * zfs unjail jailid filesystem
6804 * Detach the given dataset from the given jail
6808 zfs_do_unjail(int argc, char **argv)
6811 return (do_jail(argc, argv, 0));
6815 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6816 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6819 manual_mount(int argc, char **argv)
6822 char mountpoint[ZFS_MAXPROPLEN];
6823 char mntopts[MNT_LINE_MAX] = { '\0' };
6827 char *dataset, *path;
6830 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6833 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6836 flags |= MS_OVERLAY;
6839 flags |= MS_NOMNTTAB;
6842 (void) fprintf(stderr, gettext("missing argument for "
6843 "'%c' option\n"), optopt);
6847 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6849 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6858 /* check that we only have two arguments */
6861 (void) fprintf(stderr, gettext("missing dataset "
6864 (void) fprintf(stderr,
6865 gettext("missing mountpoint argument\n"));
6867 (void) fprintf(stderr, gettext("too many arguments\n"));
6868 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6875 /* try to open the dataset */
6876 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6879 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6880 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6882 /* check for legacy mountpoint and complain appropriately */
6884 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6885 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6886 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6887 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6892 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6893 "mounted using 'mount -t zfs'\n"), dataset);
6894 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6895 "instead.\n"), path);
6896 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6897 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6898 (void) fprintf(stderr, gettext("See zfs(8) for more "
6907 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6908 * unmounts of non-legacy filesystems, as this is the dominant administrative
6912 manual_unmount(int argc, char **argv)
6918 while ((c = getopt(argc, argv, "f")) != -1) {
6924 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6926 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6935 /* check arguments */
6938 (void) fprintf(stderr, gettext("missing path "
6941 (void) fprintf(stderr, gettext("too many arguments\n"));
6942 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6946 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6950 find_command_idx(char *command, int *idx)
6954 for (i = 0; i < NCOMMAND; i++) {
6955 if (command_table[i].name == NULL)
6958 if (strcmp(command, command_table[i].name) == 0) {
6967 zfs_do_diff(int argc, char **argv)
6971 char *tosnap = NULL;
6972 char *fromsnap = NULL;
6977 while ((c = getopt(argc, argv, "FHt")) != -1) {
6980 flags |= ZFS_DIFF_CLASSIFY;
6983 flags |= ZFS_DIFF_PARSEABLE;
6986 flags |= ZFS_DIFF_TIMESTAMP;
6989 (void) fprintf(stderr,
6990 gettext("invalid option '%c'\n"), optopt);
6999 (void) fprintf(stderr,
7000 gettext("must provide at least one snapshot name\n"));
7005 (void) fprintf(stderr, gettext("too many arguments\n"));
7010 tosnap = (argc == 2) ? argv[1] : NULL;
7013 if (*fromsnap != '@')
7014 copy = strdup(fromsnap);
7016 copy = strdup(tosnap);
7020 if ((atp = strchr(copy, '@')) != NULL)
7023 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
7029 * Ignore SIGPIPE so that the library can give us
7030 * information on any failure
7032 (void) sigignore(SIGPIPE);
7034 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
7042 * zfs remap <filesystem | volume>
7044 * Remap the indirect blocks in the given fileystem or volume.
7047 zfs_do_remap(int argc, char **argv)
7054 while ((c = getopt(argc, argv, "")) != -1) {
7057 (void) fprintf(stderr,
7058 gettext("invalid option '%c'\n"), optopt);
7064 (void) fprintf(stderr, gettext("wrong number of arguments\n"));
7069 err = zfs_remap_indirects(g_zfs, fsname);
7075 * zfs bookmark <fs@snap> <fs#bmark>
7077 * Creates a bookmark with the given name from the given snapshot.
7080 zfs_do_bookmark(int argc, char **argv)
7082 char snapname[ZFS_MAX_DATASET_NAME_LEN];
7089 while ((c = getopt(argc, argv, "")) != -1) {
7092 (void) fprintf(stderr,
7093 gettext("invalid option '%c'\n"), optopt);
7101 /* check number of arguments */
7103 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
7107 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
7111 if (strchr(argv[1], '#') == NULL) {
7112 (void) fprintf(stderr,
7113 gettext("invalid bookmark name '%s' -- "
7114 "must contain a '#'\n"), argv[1]);
7118 if (argv[0][0] == '@') {
7120 * Snapshot name begins with @.
7121 * Default to same fs as bookmark.
7123 (void) strncpy(snapname, argv[1], sizeof (snapname));
7124 *strchr(snapname, '#') = '\0';
7125 (void) strlcat(snapname, argv[0], sizeof (snapname));
7127 (void) strncpy(snapname, argv[0], sizeof (snapname));
7129 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
7135 nvl = fnvlist_alloc();
7136 fnvlist_add_string(nvl, argv[1], snapname);
7137 ret = lzc_bookmark(nvl, NULL);
7141 const char *err_msg;
7144 (void) snprintf(errbuf, sizeof (errbuf),
7145 dgettext(TEXT_DOMAIN,
7146 "cannot create bookmark '%s'"), argv[1]);
7150 err_msg = "bookmark is in a different pool";
7153 err_msg = "bookmark exists";
7156 err_msg = "invalid argument";
7159 err_msg = "bookmark feature not enabled";
7162 err_msg = "out of space";
7165 err_msg = "unknown error";
7168 (void) fprintf(stderr, "%s: %s\n", errbuf,
7169 dgettext(TEXT_DOMAIN, err_msg));
7180 zfs_do_channel_program(int argc, char **argv)
7184 char *progbuf, *filename, *poolname;
7185 size_t progsize, progread;
7187 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
7188 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
7189 boolean_t sync_flag = B_TRUE;
7190 zpool_handle_t *zhp;
7194 (c = getopt(argc, argv, "nt:(instr-limit)m:(memory-limit)"))) {
7202 arg = strtoull(optarg, &endp, 0);
7203 if (errno != 0 || *endp != '\0') {
7204 (void) fprintf(stderr, gettext(
7206 "'%s': expected integer\n"), optarg);
7211 if (arg > ZCP_MAX_INSTRLIMIT || arg == 0) {
7212 (void) fprintf(stderr, gettext(
7213 "Invalid instruction limit: "
7220 ASSERT3U(c, ==, 'm');
7221 if (arg > ZCP_MAX_MEMLIMIT || arg == 0) {
7222 (void) fprintf(stderr, gettext(
7223 "Invalid memory limit: "
7233 sync_flag = B_FALSE;
7237 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7247 (void) fprintf(stderr,
7248 gettext("invalid number of arguments\n"));
7254 if (strcmp(filename, "-") == 0) {
7256 filename = "standard input";
7257 } else if ((fd = open(filename, O_RDONLY)) < 0) {
7258 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
7259 filename, strerror(errno));
7263 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7264 (void) fprintf(stderr, gettext("cannot open pool '%s'"),
7271 * Read in the channel program, expanding the program buffer as
7276 progbuf = safe_malloc(progsize);
7278 ret = read(fd, progbuf + progread, progsize - progread);
7280 if (progread == progsize && ret > 0) {
7282 progbuf = safe_realloc(progbuf, progsize);
7290 (void) fprintf(stderr,
7291 gettext("cannot read '%s': %s\n"),
7292 filename, strerror(errno));
7295 progbuf[progread] = '\0';
7298 * Any remaining arguments are passed as arguments to the lua script as
7301 * "argv" -> [ "arg 1", ... "arg n" ],
7304 nvlist_t *argnvl = fnvlist_alloc();
7305 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);
7308 ret = lzc_channel_program(poolname, progbuf,
7309 instrlimit, memlimit, argnvl, &outnvl);
7311 ret = lzc_channel_program_nosync(poolname, progbuf,
7312 instrlimit, memlimit, argnvl, &outnvl);
7317 * On error, report the error message handed back by lua if one
7318 * exists. Otherwise, generate an appropriate error message,
7319 * falling back on strerror() for an unexpected return code.
7321 char *errstring = NULL;
7322 if (nvlist_exists(outnvl, ZCP_RET_ERROR)) {
7323 (void) nvlist_lookup_string(outnvl,
7324 ZCP_RET_ERROR, &errstring);
7325 if (errstring == NULL)
7326 errstring = strerror(ret);
7331 "Invalid instruction or memory limit.";
7334 errstring = "Return value too large.";
7337 errstring = "Memory limit exhausted.";
7344 errstring = "Timed out.";
7347 errstring = "Permission denied. Channel "
7348 "programs must be run as root.";
7351 errstring = strerror(ret);
7354 (void) fprintf(stderr,
7355 gettext("Channel program execution failed:\n%s\n"),
7358 (void) printf("Channel program fully executed ");
7359 if (nvlist_empty(outnvl)) {
7360 (void) printf("with no return value.\n");
7362 (void) printf("with return value:\n");
7363 dump_nvlist(outnvl, 4);
7368 fnvlist_free(outnvl);
7369 fnvlist_free(argnvl);
7378 main(int argc, char **argv)
7385 (void) setlocale(LC_ALL, "");
7386 (void) textdomain(TEXT_DOMAIN);
7390 if ((g_zfs = libzfs_init()) == NULL) {
7391 (void) fprintf(stderr, gettext("internal error: failed to "
7392 "initialize ZFS library\n"));
7396 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7398 libzfs_print_on_error(g_zfs, B_TRUE);
7400 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
7401 (void) fprintf(stderr, gettext("internal error: unable to "
7402 "open %s\n"), MNTTAB);
7407 * This command also doubles as the /etc/fs mount and unmount program.
7408 * Determine if we should take this behavior based on argv[0].
7410 progname = basename(argv[0]);
7411 if (strcmp(progname, "mount") == 0) {
7412 ret = manual_mount(argc, argv);
7413 } else if (strcmp(progname, "umount") == 0) {
7414 ret = manual_unmount(argc, argv);
7417 * Make sure the user has specified some command.
7420 (void) fprintf(stderr, gettext("missing command\n"));
7427 * The 'umount' command is an alias for 'unmount'
7429 if (strcmp(cmdname, "umount") == 0)
7430 cmdname = "unmount";
7433 * The 'recv' command is an alias for 'receive'
7435 if (strcmp(cmdname, "recv") == 0)
7436 cmdname = "receive";
7439 * The 'snap' command is an alias for 'snapshot'
7441 if (strcmp(cmdname, "snap") == 0)
7442 cmdname = "snapshot";
7447 if (strcmp(cmdname, "-?") == 0)
7451 * Run the appropriate command.
7453 libzfs_mnttab_cache(g_zfs, B_TRUE);
7454 if (find_command_idx(cmdname, &i) == 0) {
7455 current_command = &command_table[i];
7456 ret = command_table[i].func(argc - 1, argv + 1);
7457 } else if (strchr(cmdname, '=') != NULL) {
7458 verify(find_command_idx("set", &i) == 0);
7459 current_command = &command_table[i];
7460 ret = command_table[i].func(argc, argv);
7462 (void) fprintf(stderr, gettext("unrecognized "
7463 "command '%s'\n"), cmdname);
7466 libzfs_mnttab_cache(g_zfs, B_FALSE);
7469 (void) fclose(mnttab_file);
7471 if (ret == 0 && log_history)
7472 (void) zpool_log_history(g_zfs, history_str);
7477 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7478 * for the purposes of running ::findleaks.
7480 if (getenv("ZFS_ABORT") != NULL) {
7481 (void) printf("dumping core by request\n");