4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright (c) 2014 Integros [integros.com]
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
32 * Copyright 2016 Nexenta Systems, Inc.
42 #include <libnvpair.h>
54 #include <sys/debug.h>
56 #include <sys/mntent.h>
57 #include <sys/mnttab.h>
58 #include <sys/mount.h>
60 #include <sys/fs/zfs.h>
61 #include <sys/types.h>
67 #include <libzfs_core.h>
69 #include <zfs_deleg.h>
73 #include <directory.h>
79 #include "zfs_comutil.h"
81 libzfs_handle_t *g_zfs;
83 static FILE *mnttab_file;
84 static char history_str[HIS_MAX_RECORD_LEN];
85 static boolean_t log_history = B_TRUE;
87 static int zfs_do_clone(int argc, char **argv);
88 static int zfs_do_create(int argc, char **argv);
89 static int zfs_do_destroy(int argc, char **argv);
90 static int zfs_do_get(int argc, char **argv);
91 static int zfs_do_inherit(int argc, char **argv);
92 static int zfs_do_list(int argc, char **argv);
93 static int zfs_do_mount(int argc, char **argv);
94 static int zfs_do_rename(int argc, char **argv);
95 static int zfs_do_rollback(int argc, char **argv);
96 static int zfs_do_set(int argc, char **argv);
97 static int zfs_do_upgrade(int argc, char **argv);
98 static int zfs_do_snapshot(int argc, char **argv);
99 static int zfs_do_unmount(int argc, char **argv);
100 static int zfs_do_share(int argc, char **argv);
101 static int zfs_do_unshare(int argc, char **argv);
102 static int zfs_do_send(int argc, char **argv);
103 static int zfs_do_receive(int argc, char **argv);
104 static int zfs_do_promote(int argc, char **argv);
105 static int zfs_do_userspace(int argc, char **argv);
106 static int zfs_do_allow(int argc, char **argv);
107 static int zfs_do_unallow(int argc, char **argv);
108 static int zfs_do_hold(int argc, char **argv);
109 static int zfs_do_holds(int argc, char **argv);
110 static int zfs_do_release(int argc, char **argv);
111 static int zfs_do_diff(int argc, char **argv);
112 static int zfs_do_jail(int argc, char **argv);
113 static int zfs_do_unjail(int argc, char **argv);
114 static int zfs_do_bookmark(int argc, char **argv);
115 static int zfs_do_channel_program(int argc, char **argv);
118 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
123 _umem_debug_init(void)
125 return ("default,verbose"); /* $UMEM_DEBUG setting */
129 _umem_logging_init(void)
131 return ("fail,contents"); /* $UMEM_LOGGING setting */
165 HELP_CHANNEL_PROGRAM,
168 typedef struct zfs_command {
170 int (*func)(int argc, char **argv);
175 * Master command table. Each ZFS command has a name, associated function, and
176 * usage message. The usage messages need to be internationalized, so we have
177 * to have a function to return the usage message based on a command index.
179 * These commands are organized according to how they are displayed in the usage
180 * message. An empty command (one with a NULL name) indicates an empty line in
181 * the generic usage message.
183 static zfs_command_t command_table[] = {
184 { "create", zfs_do_create, HELP_CREATE },
185 { "destroy", zfs_do_destroy, HELP_DESTROY },
187 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
188 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
189 { "clone", zfs_do_clone, HELP_CLONE },
190 { "promote", zfs_do_promote, HELP_PROMOTE },
191 { "rename", zfs_do_rename, HELP_RENAME },
192 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
193 { "program", zfs_do_channel_program, HELP_CHANNEL_PROGRAM },
195 { "list", zfs_do_list, HELP_LIST },
197 { "set", zfs_do_set, HELP_SET },
198 { "get", zfs_do_get, HELP_GET },
199 { "inherit", zfs_do_inherit, HELP_INHERIT },
200 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
201 { "userspace", zfs_do_userspace, HELP_USERSPACE },
202 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
204 { "mount", zfs_do_mount, HELP_MOUNT },
205 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
206 { "share", zfs_do_share, HELP_SHARE },
207 { "unshare", zfs_do_unshare, HELP_UNSHARE },
209 { "send", zfs_do_send, HELP_SEND },
210 { "receive", zfs_do_receive, HELP_RECEIVE },
212 { "allow", zfs_do_allow, HELP_ALLOW },
214 { "unallow", zfs_do_unallow, HELP_UNALLOW },
216 { "hold", zfs_do_hold, HELP_HOLD },
217 { "holds", zfs_do_holds, HELP_HOLDS },
218 { "release", zfs_do_release, HELP_RELEASE },
219 { "diff", zfs_do_diff, HELP_DIFF },
221 { "jail", zfs_do_jail, HELP_JAIL },
222 { "unjail", zfs_do_unjail, HELP_UNJAIL },
225 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
227 zfs_command_t *current_command;
230 get_usage(zfs_help_t idx)
234 return (gettext("\tclone [-p] [-o property=value] ... "
235 "<snapshot> <filesystem|volume>\n"));
237 return (gettext("\tcreate [-pu] [-o property=value] ... "
239 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
240 "-V <size> <volume>\n"));
242 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
243 "\tdestroy [-dnpRrv] "
244 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
245 "\tdestroy <filesystem|volume>#<bookmark>\n"));
247 return (gettext("\tget [-rHp] [-d max] "
248 "[-o \"all\" | field[,...]]\n"
249 "\t [-t type[,...]] [-s source[,...]]\n"
250 "\t <\"all\" | property[,...]> "
251 "[filesystem|volume|snapshot|bookmark] ...\n"));
253 return (gettext("\tinherit [-rS] <property> "
254 "<filesystem|volume|snapshot> ...\n"));
256 return (gettext("\tupgrade [-v]\n"
257 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
259 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
261 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
263 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
264 "[-s property]...\n\t [-S property]... [-t type[,...]] "
265 "[filesystem|volume|snapshot] ...\n"));
267 return (gettext("\tmount\n"
268 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
270 return (gettext("\tpromote <clone-filesystem>\n"));
272 return (gettext("\treceive|recv [-vnsFu] <filesystem|volume|"
274 "\treceive|recv [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
276 "\treceive|recv -A <filesystem|volume>\n"));
278 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
279 "<filesystem|volume|snapshot>\n"
280 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
281 "\trename -r <snapshot> <snapshot>\n"
282 "\trename -u [-p] <filesystem> <filesystem>"));
284 return (gettext("\trollback [-rRf] <snapshot>\n"));
286 return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] "
288 "\tsend [-Le] [-i snapshot|bookmark] "
289 "<filesystem|volume|snapshot>\n"
290 "\tsend [-nvPe] -t <receive_resume_token>\n"));
292 return (gettext("\tset <property=value> ... "
293 "<filesystem|volume|snapshot> ...\n"));
295 return (gettext("\tshare <-a | filesystem>\n"));
297 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
298 "<filesystem|volume>@<snap> ...\n"));
300 return (gettext("\tunmount|umount [-f] "
301 "<-a | filesystem|mountpoint>\n"));
303 return (gettext("\tunshare "
304 "<-a | filesystem|mountpoint>\n"));
306 return (gettext("\tallow <filesystem|volume>\n"
308 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
309 "\t <filesystem|volume>\n"
310 "\tallow [-ld] -e <perm|@setname>[,...] "
311 "<filesystem|volume>\n"
312 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
313 "\tallow -s @setname <perm|@setname>[,...] "
314 "<filesystem|volume>\n"));
316 return (gettext("\tunallow [-rldug] "
317 "<\"everyone\"|user|group>[,...]\n"
318 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
319 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
320 "<filesystem|volume>\n"
321 "\tunallow [-r] -c [<perm|@setname>[,...]] "
322 "<filesystem|volume>\n"
323 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
324 "<filesystem|volume>\n"));
326 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
328 "\t [-S field] ... [-t type[,...]] "
329 "<filesystem|snapshot>\n"));
330 case HELP_GROUPSPACE:
331 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
333 "\t [-S field] ... [-t type[,...]] "
334 "<filesystem|snapshot>\n"));
336 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
338 return (gettext("\tholds [-Hp] [-r|-d depth] "
339 "<filesystem|volume|snapshot> ...\n"));
341 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
343 return (gettext("\tdiff [-FHt] <snapshot> "
344 "[snapshot|filesystem]\n"));
346 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
347 case HELP_CHANNEL_PROGRAM:
348 return (gettext("\tprogram [-t <instruction limit>] "
349 "[-m <memory limit (b)>] <pool> <program file> "
360 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
365 * Utility function to guarantee malloc() success.
369 safe_malloc(size_t size)
373 if ((data = calloc(1, size)) == NULL)
380 safe_realloc(void *data, size_t size)
383 if ((newp = realloc(data, size)) == NULL) {
392 safe_strdup(char *str)
394 char *dupstr = strdup(str);
403 * Callback routine that will print out information for each of
407 usage_prop_cb(int prop, void *cb)
411 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
413 if (zfs_prop_readonly(prop))
414 (void) fprintf(fp, " NO ");
416 (void) fprintf(fp, "YES ");
418 if (zfs_prop_inheritable(prop))
419 (void) fprintf(fp, " YES ");
421 (void) fprintf(fp, " NO ");
423 if (zfs_prop_values(prop) == NULL)
424 (void) fprintf(fp, "-\n");
426 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
432 * Display usage message. If we're inside a command, display only the usage for
433 * that command. Otherwise, iterate over the entire command table and display
434 * a complete usage message.
437 usage(boolean_t requested)
440 boolean_t show_properties = B_FALSE;
441 FILE *fp = requested ? stdout : stderr;
443 if (current_command == NULL) {
445 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
447 gettext("where 'command' is one of the following:\n\n"));
449 for (i = 0; i < NCOMMAND; i++) {
450 if (command_table[i].name == NULL)
451 (void) fprintf(fp, "\n");
453 (void) fprintf(fp, "%s",
454 get_usage(command_table[i].usage));
457 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
458 "pool/[dataset/]*dataset[@name]\n"));
460 (void) fprintf(fp, gettext("usage:\n"));
461 (void) fprintf(fp, "%s", get_usage(current_command->usage));
464 if (current_command != NULL &&
465 (strcmp(current_command->name, "set") == 0 ||
466 strcmp(current_command->name, "get") == 0 ||
467 strcmp(current_command->name, "inherit") == 0 ||
468 strcmp(current_command->name, "list") == 0))
469 show_properties = B_TRUE;
471 if (show_properties) {
473 gettext("\nThe following properties are supported:\n"));
475 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
476 "PROPERTY", "EDIT", "INHERIT", "VALUES");
478 /* Iterate over all properties */
479 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
482 (void) fprintf(fp, "\t%-15s ", "userused@...");
483 (void) fprintf(fp, " NO NO <size>\n");
484 (void) fprintf(fp, "\t%-15s ", "groupused@...");
485 (void) fprintf(fp, " NO NO <size>\n");
486 (void) fprintf(fp, "\t%-15s ", "userquota@...");
487 (void) fprintf(fp, "YES NO <size> | none\n");
488 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
489 (void) fprintf(fp, "YES NO <size> | none\n");
490 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
491 (void) fprintf(fp, " NO NO <size>\n");
493 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
494 "with standard units such as K, M, G, etc.\n"));
495 (void) fprintf(fp, gettext("\nUser-defined properties can "
496 "be specified by using a name containing a colon (:).\n"));
497 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
498 "properties must be appended with\n"
499 "a user or group specifier of one of these forms:\n"
500 " POSIX name (eg: \"matt\")\n"
501 " POSIX id (eg: \"126829\")\n"
502 " SMB name@domain (eg: \"matt@sun\")\n"
503 " SMB SID (eg: \"S-1-234-567-89\")\n"));
506 gettext("\nFor the property list, run: %s\n"),
509 gettext("\nFor the delegated permission list, run: %s\n"),
510 "zfs allow|unallow");
514 * See comments at end of main().
516 if (getenv("ZFS_ABORT") != NULL) {
517 (void) printf("dumping core by request\n");
521 exit(requested ? 0 : 2);
525 * Take a property=value argument string and add it to the given nvlist.
526 * Modifies the argument inplace.
529 parseprop(nvlist_t *props, char *propname)
531 char *propval, *strval;
533 if ((propval = strchr(propname, '=')) == NULL) {
534 (void) fprintf(stderr, gettext("missing "
535 "'=' for property=value argument\n"));
540 if (nvlist_lookup_string(props, propname, &strval) == 0) {
541 (void) fprintf(stderr, gettext("property '%s' "
542 "specified multiple times\n"), propname);
545 if (nvlist_add_string(props, propname, propval) != 0)
551 parse_depth(char *opt, int *flags)
556 depth = (int)strtol(opt, &tmp, 0);
558 (void) fprintf(stderr,
559 gettext("%s is not an integer\n"), opt);
563 (void) fprintf(stderr,
564 gettext("Depth can not be negative.\n"));
567 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
571 #define PROGRESS_DELAY 2 /* seconds */
573 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";
574 static time_t pt_begin;
575 static char *pt_header = NULL;
576 static boolean_t pt_shown;
579 start_progress_timer(void)
581 pt_begin = time(NULL) + PROGRESS_DELAY;
586 set_progress_header(char *header)
588 assert(pt_header == NULL);
589 pt_header = safe_strdup(header);
591 (void) printf("%s: ", header);
592 (void) fflush(stdout);
597 update_progress(char *update)
599 if (!pt_shown && time(NULL) > pt_begin) {
600 int len = strlen(update);
602 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
604 (void) fflush(stdout);
606 } else if (pt_shown) {
607 int len = strlen(update);
609 (void) printf("%s%*.*s", update, len, len, pt_reverse);
610 (void) fflush(stdout);
615 finish_progress(char *done)
618 (void) printf("%s\n", done);
619 (void) fflush(stdout);
626 * Check if the dataset is mountable and should be automatically mounted.
629 should_auto_mount(zfs_handle_t *zhp)
631 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp)))
633 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON);
637 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
639 * Given an existing dataset, create a writable copy whose initial contents
640 * are the same as the source. The newly created dataset maintains a
641 * dependency on the original; the original cannot be destroyed so long as
644 * The '-p' flag creates all the non-existing ancestors of the target first.
647 zfs_do_clone(int argc, char **argv)
649 zfs_handle_t *zhp = NULL;
650 boolean_t parents = B_FALSE;
655 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
659 while ((c = getopt(argc, argv, "o:p")) != -1) {
662 if (parseprop(props, optarg) != 0)
669 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
678 /* check number of arguments */
680 (void) fprintf(stderr, gettext("missing source dataset "
685 (void) fprintf(stderr, gettext("missing target dataset "
690 (void) fprintf(stderr, gettext("too many arguments\n"));
694 /* open the source dataset */
695 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
698 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
701 * Now create the ancestors of the target dataset. If the
702 * target already exists and '-p' option was used we should not
705 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
708 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
713 ret = zfs_clone(zhp, argv[1], props);
715 /* create the mountpoint if necessary */
719 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
722 * If the user doesn't want the dataset
723 * automatically mounted, then skip the mount/share
726 if (should_auto_mount(clone)) {
727 if ((ret = zfs_mount(clone, NULL, 0)) != 0) {
728 (void) fprintf(stderr, gettext("clone "
729 "successfully created, "
730 "but not mounted\n"));
731 } else if ((ret = zfs_share(clone)) != 0) {
732 (void) fprintf(stderr, gettext("clone "
733 "successfully created, "
734 "but not shared\n"));
755 * zfs create [-pu] [-o prop=value] ... fs
756 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
758 * Create a new dataset. This command can be used to create filesystems
759 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
760 * For volumes, the user must specify a size to be used.
762 * The '-s' flag applies only to volumes, and indicates that we should not try
763 * to set the reservation for this volume. By default we set a reservation
764 * equal to the size for any volume. For pools with SPA_VERSION >=
765 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
767 * The '-p' flag creates all the non-existing ancestors of the target first.
769 * The '-u' flag prevents mounting of newly created file system.
772 zfs_do_create(int argc, char **argv)
774 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
775 zfs_handle_t *zhp = NULL;
776 uint64_t volsize = 0;
778 boolean_t noreserve = B_FALSE;
779 boolean_t bflag = B_FALSE;
780 boolean_t parents = B_FALSE;
781 boolean_t nomount = B_FALSE;
786 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
790 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) {
793 type = ZFS_TYPE_VOLUME;
794 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
795 (void) fprintf(stderr, gettext("bad volume "
796 "size '%s': %s\n"), optarg,
797 libzfs_error_description(g_zfs));
801 if (nvlist_add_uint64(props,
802 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
811 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
812 (void) fprintf(stderr, gettext("bad volume "
813 "block size '%s': %s\n"), optarg,
814 libzfs_error_description(g_zfs));
818 if (nvlist_add_uint64(props,
819 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
824 if (parseprop(props, optarg) != 0)
834 (void) fprintf(stderr, gettext("missing size "
838 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
844 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
845 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
846 "used when creating a volume\n"));
849 if (nomount && type != ZFS_TYPE_FILESYSTEM) {
850 (void) fprintf(stderr, gettext("'-u' can only be "
851 "used when creating a file system\n"));
858 /* check number of arguments */
860 (void) fprintf(stderr, gettext("missing %s argument\n"),
861 zfs_type_to_name(type));
865 (void) fprintf(stderr, gettext("too many arguments\n"));
869 if (type == ZFS_TYPE_VOLUME && !noreserve) {
870 zpool_handle_t *zpool_handle;
871 nvlist_t *real_props = NULL;
872 uint64_t spa_version;
874 zfs_prop_t resv_prop;
878 if ((p = strchr(argv[0], '/')) != NULL)
880 zpool_handle = zpool_open(g_zfs, argv[0]);
883 if (zpool_handle == NULL)
885 spa_version = zpool_get_prop_int(zpool_handle,
886 ZPOOL_PROP_VERSION, NULL);
887 if (spa_version >= SPA_VERSION_REFRESERVATION)
888 resv_prop = ZFS_PROP_REFRESERVATION;
890 resv_prop = ZFS_PROP_RESERVATION;
892 (void) snprintf(msg, sizeof (msg),
893 gettext("cannot create '%s'"), argv[0]);
894 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
895 props, 0, NULL, zpool_handle, msg)) == NULL) {
896 zpool_close(zpool_handle);
899 zpool_close(zpool_handle);
901 volsize = zvol_volsize_to_reservation(volsize, real_props);
902 nvlist_free(real_props);
904 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
906 if (nvlist_add_uint64(props,
907 zfs_prop_to_name(resv_prop), volsize) != 0) {
914 if (parents && zfs_name_valid(argv[0], type)) {
916 * Now create the ancestors of target dataset. If the target
917 * already exists and '-p' option was used we should not
920 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
924 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
929 if (zfs_create(g_zfs, argv[0], type, props) != 0)
932 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
938 * Mount and/or share the new filesystem as appropriate. We provide a
939 * verbose error message to let the user know that their filesystem was
940 * in fact created, even if we failed to mount or share it.
941 * If the user doesn't want the dataset automatically mounted,
942 * then skip the mount/share step altogether.
944 if (!nomount && should_auto_mount(zhp)) {
945 if (zfs_mount(zhp, NULL, 0) != 0) {
946 (void) fprintf(stderr, gettext("filesystem "
947 "successfully created, but not mounted\n"));
949 } else if (zfs_share(zhp) != 0) {
950 (void) fprintf(stderr, gettext("filesystem "
951 "successfully created, but not shared\n"));
968 * zfs destroy [-rRf] <fs, vol>
969 * zfs destroy [-rRd] <snap>
971 * -r Recursively destroy all children
972 * -R Recursively destroy all dependents, including clones
973 * -f Force unmounting of any dependents
974 * -d If we can't destroy now, mark for deferred destruction
976 * Destroys the given dataset. By default, it will unmount any filesystems,
977 * and refuse to destroy a dataset that has any dependents. A dependent can
978 * either be a child, or a clone of a child.
980 typedef struct destroy_cbdata {
983 boolean_t cb_recurse;
985 boolean_t cb_doclones;
986 zfs_handle_t *cb_target;
987 boolean_t cb_defer_destroy;
988 boolean_t cb_verbose;
989 boolean_t cb_parsable;
992 nvlist_t *cb_batchedsnaps;
994 /* first snap in contiguous run */
996 /* previous snap in contiguous run */
1004 * Check for any dependents based on the '-r' or '-R' flags.
1007 destroy_check_dependent(zfs_handle_t *zhp, void *data)
1009 destroy_cbdata_t *cbp = data;
1010 const char *tname = zfs_get_name(cbp->cb_target);
1011 const char *name = zfs_get_name(zhp);
1013 if (strncmp(tname, name, strlen(tname)) == 0 &&
1014 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
1016 * This is a direct descendant, not a clone somewhere else in
1019 if (cbp->cb_recurse)
1022 if (cbp->cb_first) {
1023 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1024 "%s has children\n"),
1025 zfs_get_name(cbp->cb_target),
1026 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1027 (void) fprintf(stderr, gettext("use '-r' to destroy "
1028 "the following datasets:\n"));
1029 cbp->cb_first = B_FALSE;
1030 cbp->cb_error = B_TRUE;
1033 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1036 * This is a clone. We only want to report this if the '-r'
1037 * wasn't specified, or the target is a snapshot.
1039 if (!cbp->cb_recurse &&
1040 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1043 if (cbp->cb_first) {
1044 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1045 "%s has dependent clones\n"),
1046 zfs_get_name(cbp->cb_target),
1047 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1048 (void) fprintf(stderr, gettext("use '-R' to destroy "
1049 "the following datasets:\n"));
1050 cbp->cb_first = B_FALSE;
1051 cbp->cb_error = B_TRUE;
1052 cbp->cb_dryrun = B_TRUE;
1055 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1064 destroy_callback(zfs_handle_t *zhp, void *data)
1066 destroy_cbdata_t *cb = data;
1067 const char *name = zfs_get_name(zhp);
1069 if (cb->cb_verbose) {
1070 if (cb->cb_parsable) {
1071 (void) printf("destroy\t%s\n", name);
1072 } else if (cb->cb_dryrun) {
1073 (void) printf(gettext("would destroy %s\n"),
1076 (void) printf(gettext("will destroy %s\n"),
1082 * Ignore pools (which we've already flagged as an error before getting
1085 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1086 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1090 if (cb->cb_dryrun) {
1096 * We batch up all contiguous snapshots (even of different
1097 * filesystems) and destroy them with one ioctl. We can't
1098 * simply do all snap deletions and then all fs deletions,
1099 * because we must delete a clone before its origin.
1101 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1102 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1104 int error = zfs_destroy_snaps_nvl(g_zfs,
1105 cb->cb_batchedsnaps, B_FALSE);
1106 fnvlist_free(cb->cb_batchedsnaps);
1107 cb->cb_batchedsnaps = fnvlist_alloc();
1110 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1111 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1122 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1124 destroy_cbdata_t *cb = arg;
1125 const char *name = zfs_get_name(zhp);
1128 if (nvlist_exists(cb->cb_nvl, name)) {
1129 if (cb->cb_firstsnap == NULL)
1130 cb->cb_firstsnap = strdup(name);
1131 if (cb->cb_prevsnap != NULL)
1132 free(cb->cb_prevsnap);
1133 /* this snap continues the current range */
1134 cb->cb_prevsnap = strdup(name);
1135 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1137 if (cb->cb_verbose) {
1138 if (cb->cb_parsable) {
1139 (void) printf("destroy\t%s\n", name);
1140 } else if (cb->cb_dryrun) {
1141 (void) printf(gettext("would destroy %s\n"),
1144 (void) printf(gettext("will destroy %s\n"),
1148 } else if (cb->cb_firstsnap != NULL) {
1149 /* end of this range */
1151 err = lzc_snaprange_space(cb->cb_firstsnap,
1152 cb->cb_prevsnap, &used);
1153 cb->cb_snapused += used;
1154 free(cb->cb_firstsnap);
1155 cb->cb_firstsnap = NULL;
1156 free(cb->cb_prevsnap);
1157 cb->cb_prevsnap = NULL;
1164 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1167 assert(cb->cb_firstsnap == NULL);
1168 assert(cb->cb_prevsnap == NULL);
1169 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1170 if (cb->cb_firstsnap != NULL) {
1173 err = lzc_snaprange_space(cb->cb_firstsnap,
1174 cb->cb_prevsnap, &used);
1176 cb->cb_snapused += used;
1177 free(cb->cb_firstsnap);
1178 cb->cb_firstsnap = NULL;
1179 free(cb->cb_prevsnap);
1180 cb->cb_prevsnap = NULL;
1186 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1188 destroy_cbdata_t *cb = arg;
1191 /* Check for clones. */
1192 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1193 cb->cb_target = zhp;
1194 cb->cb_first = B_TRUE;
1195 err = zfs_iter_dependents(zhp, B_TRUE,
1196 destroy_check_dependent, cb);
1200 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1208 gather_snapshots(zfs_handle_t *zhp, void *arg)
1210 destroy_cbdata_t *cb = arg;
1213 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1219 if (cb->cb_verbose) {
1220 err = destroy_print_snapshots(zhp, cb);
1226 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1234 destroy_clones(destroy_cbdata_t *cb)
1237 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1239 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1240 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1243 boolean_t defer = cb->cb_defer_destroy;
1247 * We can't defer destroy non-snapshots, so set it to
1248 * false while destroying the clones.
1250 cb->cb_defer_destroy = B_FALSE;
1251 err = zfs_iter_dependents(zhp, B_FALSE,
1252 destroy_callback, cb);
1253 cb->cb_defer_destroy = defer;
1263 zfs_do_destroy(int argc, char **argv)
1265 destroy_cbdata_t cb = { 0 };
1269 zfs_handle_t *zhp = NULL;
1271 zfs_type_t type = ZFS_TYPE_DATASET;
1274 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1277 cb.cb_verbose = B_TRUE;
1280 cb.cb_verbose = B_TRUE;
1281 cb.cb_parsable = B_TRUE;
1284 cb.cb_dryrun = B_TRUE;
1287 cb.cb_defer_destroy = B_TRUE;
1288 type = ZFS_TYPE_SNAPSHOT;
1291 cb.cb_force = B_TRUE;
1294 cb.cb_recurse = B_TRUE;
1297 cb.cb_recurse = B_TRUE;
1298 cb.cb_doclones = B_TRUE;
1302 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1311 /* check number of arguments */
1313 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1317 (void) fprintf(stderr, gettext("too many arguments\n"));
1321 at = strchr(argv[0], '@');
1322 pound = strchr(argv[0], '#');
1325 /* Build the list of snaps to destroy in cb_nvl. */
1326 cb.cb_nvl = fnvlist_alloc();
1329 zhp = zfs_open(g_zfs, argv[0],
1330 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1334 cb.cb_snapspec = at + 1;
1335 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1341 if (nvlist_empty(cb.cb_nvl)) {
1342 (void) fprintf(stderr, gettext("could not find any "
1343 "snapshots to destroy; check snapshot names.\n"));
1348 if (cb.cb_verbose) {
1350 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1351 if (cb.cb_parsable) {
1352 (void) printf("reclaim\t%llu\n",
1354 } else if (cb.cb_dryrun) {
1355 (void) printf(gettext("would reclaim %s\n"),
1358 (void) printf(gettext("will reclaim %s\n"),
1363 if (!cb.cb_dryrun) {
1364 if (cb.cb_doclones) {
1365 cb.cb_batchedsnaps = fnvlist_alloc();
1366 err = destroy_clones(&cb);
1368 err = zfs_destroy_snaps_nvl(g_zfs,
1369 cb.cb_batchedsnaps, B_FALSE);
1377 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1378 cb.cb_defer_destroy);
1384 } else if (pound != NULL) {
1389 (void) fprintf(stderr,
1390 "dryrun is not supported with bookmark\n");
1394 if (cb.cb_defer_destroy) {
1395 (void) fprintf(stderr,
1396 "defer destroy is not supported with bookmark\n");
1400 if (cb.cb_recurse) {
1401 (void) fprintf(stderr,
1402 "recursive is not supported with bookmark\n");
1406 if (!zfs_bookmark_exists(argv[0])) {
1407 (void) fprintf(stderr, gettext("bookmark '%s' "
1408 "does not exist.\n"), argv[0]);
1412 nvl = fnvlist_alloc();
1413 fnvlist_add_boolean(nvl, argv[0]);
1415 err = lzc_destroy_bookmarks(nvl, NULL);
1417 (void) zfs_standard_error(g_zfs, err,
1418 "cannot destroy bookmark");
1421 nvlist_free(cb.cb_nvl);
1425 /* Open the given dataset */
1426 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1432 * Perform an explicit check for pools before going any further.
1434 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1435 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1436 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1437 "operation does not apply to pools\n"),
1439 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1440 "%s' to destroy all datasets in the pool\n"),
1442 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1443 "to destroy the pool itself\n"), zfs_get_name(zhp));
1449 * Check for any dependents and/or clones.
1451 cb.cb_first = B_TRUE;
1452 if (!cb.cb_doclones &&
1453 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1464 cb.cb_batchedsnaps = fnvlist_alloc();
1465 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1472 * Do the real thing. The callback will close the
1473 * handle regardless of whether it succeeds or not.
1475 err = destroy_callback(zhp, &cb);
1478 err = zfs_destroy_snaps_nvl(g_zfs,
1479 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1486 fnvlist_free(cb.cb_batchedsnaps);
1487 fnvlist_free(cb.cb_nvl);
1494 is_recvd_column(zprop_get_cbdata_t *cbp)
1497 zfs_get_column_t col;
1499 for (i = 0; i < ZFS_GET_NCOLS &&
1500 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1501 if (col == GET_COL_RECVD)
1507 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1508 * < all | property[,property]... > < fs | snap | vol > ...
1510 * -r recurse over any child datasets
1511 * -H scripted mode. Headers are stripped, and fields are separated
1512 * by tabs instead of spaces.
1513 * -o Set of fields to display. One of "name,property,value,
1514 * received,source". Default is "name,property,value,source".
1515 * "all" is an alias for all five.
1516 * -s Set of sources to allow. One of
1517 * "local,default,inherited,received,temporary,none". Default is
1519 * -p Display values in parsable (literal) format.
1521 * Prints properties for the given datasets. The user can control which
1522 * columns to display as well as which property types to allow.
1526 * Invoked to display the properties for a single dataset.
1529 get_callback(zfs_handle_t *zhp, void *data)
1531 char buf[ZFS_MAXPROPLEN];
1532 char rbuf[ZFS_MAXPROPLEN];
1533 zprop_source_t sourcetype;
1534 char source[ZFS_MAX_DATASET_NAME_LEN];
1535 zprop_get_cbdata_t *cbp = data;
1536 nvlist_t *user_props = zfs_get_user_props(zhp);
1537 zprop_list_t *pl = cbp->cb_proplist;
1541 boolean_t received = is_recvd_column(cbp);
1543 for (; pl != NULL; pl = pl->pl_next) {
1544 char *recvdval = NULL;
1546 * Skip the special fake placeholder. This will also skip over
1547 * the name property when 'all' is specified.
1549 if (pl->pl_prop == ZFS_PROP_NAME &&
1550 pl == cbp->cb_proplist)
1553 if (pl->pl_prop != ZPROP_INVAL) {
1554 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1555 sizeof (buf), &sourcetype, source,
1557 cbp->cb_literal) != 0) {
1560 if (!zfs_prop_valid_for_type(pl->pl_prop,
1561 ZFS_TYPE_DATASET)) {
1562 (void) fprintf(stderr,
1563 gettext("No such property '%s'\n"),
1564 zfs_prop_to_name(pl->pl_prop));
1567 sourcetype = ZPROP_SRC_NONE;
1568 (void) strlcpy(buf, "-", sizeof (buf));
1571 if (received && (zfs_prop_get_recvd(zhp,
1572 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1573 cbp->cb_literal) == 0))
1576 zprop_print_one_property(zfs_get_name(zhp), cbp,
1577 zfs_prop_to_name(pl->pl_prop),
1578 buf, sourcetype, source, recvdval);
1579 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1580 sourcetype = ZPROP_SRC_LOCAL;
1582 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1583 buf, sizeof (buf), cbp->cb_literal) != 0) {
1584 sourcetype = ZPROP_SRC_NONE;
1585 (void) strlcpy(buf, "-", sizeof (buf));
1588 zprop_print_one_property(zfs_get_name(zhp), cbp,
1589 pl->pl_user_prop, buf, sourcetype, source, NULL);
1590 } else if (zfs_prop_written(pl->pl_user_prop)) {
1591 sourcetype = ZPROP_SRC_LOCAL;
1593 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1594 buf, sizeof (buf), cbp->cb_literal) != 0) {
1595 sourcetype = ZPROP_SRC_NONE;
1596 (void) strlcpy(buf, "-", sizeof (buf));
1599 zprop_print_one_property(zfs_get_name(zhp), cbp,
1600 pl->pl_user_prop, buf, sourcetype, source, NULL);
1602 if (nvlist_lookup_nvlist(user_props,
1603 pl->pl_user_prop, &propval) != 0) {
1606 sourcetype = ZPROP_SRC_NONE;
1609 verify(nvlist_lookup_string(propval,
1610 ZPROP_VALUE, &strval) == 0);
1611 verify(nvlist_lookup_string(propval,
1612 ZPROP_SOURCE, &sourceval) == 0);
1614 if (strcmp(sourceval,
1615 zfs_get_name(zhp)) == 0) {
1616 sourcetype = ZPROP_SRC_LOCAL;
1617 } else if (strcmp(sourceval,
1618 ZPROP_SOURCE_VAL_RECVD) == 0) {
1619 sourcetype = ZPROP_SRC_RECEIVED;
1621 sourcetype = ZPROP_SRC_INHERITED;
1622 (void) strlcpy(source,
1623 sourceval, sizeof (source));
1627 if (received && (zfs_prop_get_recvd(zhp,
1628 pl->pl_user_prop, rbuf, sizeof (rbuf),
1629 cbp->cb_literal) == 0))
1632 zprop_print_one_property(zfs_get_name(zhp), cbp,
1633 pl->pl_user_prop, strval, sourcetype,
1642 zfs_do_get(int argc, char **argv)
1644 zprop_get_cbdata_t cb = { 0 };
1645 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1646 int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
1647 char *value, *fields;
1650 zprop_list_t fake_name = { 0 };
1653 * Set up default columns and sources.
1655 cb.cb_sources = ZPROP_SRC_ALL;
1656 cb.cb_columns[0] = GET_COL_NAME;
1657 cb.cb_columns[1] = GET_COL_PROPERTY;
1658 cb.cb_columns[2] = GET_COL_VALUE;
1659 cb.cb_columns[3] = GET_COL_SOURCE;
1660 cb.cb_type = ZFS_TYPE_DATASET;
1663 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1666 cb.cb_literal = B_TRUE;
1669 limit = parse_depth(optarg, &flags);
1672 flags |= ZFS_ITER_RECURSE;
1675 cb.cb_scripted = B_TRUE;
1678 (void) fprintf(stderr, gettext("missing argument for "
1679 "'%c' option\n"), optopt);
1684 * Process the set of columns to display. We zero out
1685 * the structure to give us a blank slate.
1687 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1689 while (*optarg != '\0') {
1690 static char *col_subopts[] =
1691 { "name", "property", "value", "received",
1692 "source", "all", NULL };
1694 if (i == ZFS_GET_NCOLS) {
1695 (void) fprintf(stderr, gettext("too "
1696 "many fields given to -o "
1701 switch (getsubopt(&optarg, col_subopts,
1704 cb.cb_columns[i++] = GET_COL_NAME;
1707 cb.cb_columns[i++] = GET_COL_PROPERTY;
1710 cb.cb_columns[i++] = GET_COL_VALUE;
1713 cb.cb_columns[i++] = GET_COL_RECVD;
1714 flags |= ZFS_ITER_RECVD_PROPS;
1717 cb.cb_columns[i++] = GET_COL_SOURCE;
1721 (void) fprintf(stderr,
1722 gettext("\"all\" conflicts "
1723 "with specific fields "
1724 "given to -o option\n"));
1727 cb.cb_columns[0] = GET_COL_NAME;
1728 cb.cb_columns[1] = GET_COL_PROPERTY;
1729 cb.cb_columns[2] = GET_COL_VALUE;
1730 cb.cb_columns[3] = GET_COL_RECVD;
1731 cb.cb_columns[4] = GET_COL_SOURCE;
1732 flags |= ZFS_ITER_RECVD_PROPS;
1736 (void) fprintf(stderr,
1737 gettext("invalid column name "
1738 "'%s'\n"), suboptarg);
1746 while (*optarg != '\0') {
1747 static char *source_subopts[] = {
1748 "local", "default", "inherited",
1749 "received", "temporary", "none",
1752 switch (getsubopt(&optarg, source_subopts,
1755 cb.cb_sources |= ZPROP_SRC_LOCAL;
1758 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1761 cb.cb_sources |= ZPROP_SRC_INHERITED;
1764 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1767 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1770 cb.cb_sources |= ZPROP_SRC_NONE;
1773 (void) fprintf(stderr,
1774 gettext("invalid source "
1775 "'%s'\n"), suboptarg);
1783 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1784 while (*optarg != '\0') {
1785 static char *type_subopts[] = { "filesystem",
1786 "volume", "snapshot", "bookmark",
1789 switch (getsubopt(&optarg, type_subopts,
1792 types |= ZFS_TYPE_FILESYSTEM;
1795 types |= ZFS_TYPE_VOLUME;
1798 types |= ZFS_TYPE_SNAPSHOT;
1801 types |= ZFS_TYPE_BOOKMARK;
1804 types = ZFS_TYPE_DATASET |
1809 (void) fprintf(stderr,
1810 gettext("invalid type '%s'\n"),
1818 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1828 (void) fprintf(stderr, gettext("missing property "
1835 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1843 * As part of zfs_expand_proplist(), we keep track of the maximum column
1844 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1845 * need to know the maximum name length. However, the user likely did
1846 * not specify 'name' as one of the properties to fetch, so we need to
1847 * make sure we always include at least this property for
1848 * print_get_headers() to work properly.
1850 if (cb.cb_proplist != NULL) {
1851 fake_name.pl_prop = ZFS_PROP_NAME;
1852 fake_name.pl_width = strlen(gettext("NAME"));
1853 fake_name.pl_next = cb.cb_proplist;
1854 cb.cb_proplist = &fake_name;
1857 cb.cb_first = B_TRUE;
1859 /* run for each object */
1860 ret = zfs_for_each(argc, argv, flags, types, NULL,
1861 &cb.cb_proplist, limit, get_callback, &cb);
1863 if (cb.cb_proplist == &fake_name)
1864 zprop_free_list(fake_name.pl_next);
1866 zprop_free_list(cb.cb_proplist);
1872 * inherit [-rS] <property> <fs|vol> ...
1874 * -r Recurse over all children
1875 * -S Revert to received value, if any
1877 * For each dataset specified on the command line, inherit the given property
1878 * from its parent. Inheriting a property at the pool level will cause it to
1879 * use the default value. The '-r' flag will recurse over all children, and is
1880 * useful for setting a property on a hierarchy-wide basis, regardless of any
1881 * local modifications for each dataset.
1884 typedef struct inherit_cbdata {
1885 const char *cb_propname;
1886 boolean_t cb_received;
1890 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1892 inherit_cbdata_t *cb = data;
1893 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1896 * If we're doing it recursively, then ignore properties that
1897 * are not valid for this type of dataset.
1899 if (prop != ZPROP_INVAL &&
1900 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1903 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1907 inherit_cb(zfs_handle_t *zhp, void *data)
1909 inherit_cbdata_t *cb = data;
1911 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1915 zfs_do_inherit(int argc, char **argv)
1919 inherit_cbdata_t cb = { 0 };
1923 boolean_t received = B_FALSE;
1926 while ((c = getopt(argc, argv, "rS")) != -1) {
1929 flags |= ZFS_ITER_RECURSE;
1936 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1945 /* check number of arguments */
1947 (void) fprintf(stderr, gettext("missing property argument\n"));
1951 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1959 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1960 if (zfs_prop_readonly(prop)) {
1961 (void) fprintf(stderr, gettext(
1962 "%s property is read-only\n"),
1966 if (!zfs_prop_inheritable(prop) && !received) {
1967 (void) fprintf(stderr, gettext("'%s' property cannot "
1968 "be inherited\n"), propname);
1969 if (prop == ZFS_PROP_QUOTA ||
1970 prop == ZFS_PROP_RESERVATION ||
1971 prop == ZFS_PROP_REFQUOTA ||
1972 prop == ZFS_PROP_REFRESERVATION) {
1973 (void) fprintf(stderr, gettext("use 'zfs set "
1974 "%s=none' to clear\n"), propname);
1975 (void) fprintf(stderr, gettext("use 'zfs "
1976 "inherit -S %s' to revert to received "
1977 "value\n"), propname);
1981 if (received && (prop == ZFS_PROP_VOLSIZE ||
1982 prop == ZFS_PROP_VERSION)) {
1983 (void) fprintf(stderr, gettext("'%s' property cannot "
1984 "be reverted to a received value\n"), propname);
1987 } else if (!zfs_prop_user(propname)) {
1988 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1993 cb.cb_propname = propname;
1994 cb.cb_received = received;
1996 if (flags & ZFS_ITER_RECURSE) {
1997 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1998 NULL, NULL, 0, inherit_recurse_cb, &cb);
2000 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2001 NULL, NULL, 0, inherit_cb, &cb);
2007 typedef struct upgrade_cbdata {
2008 uint64_t cb_numupgraded;
2009 uint64_t cb_numsamegraded;
2010 uint64_t cb_numfailed;
2011 uint64_t cb_version;
2013 boolean_t cb_foundone;
2014 char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
2018 same_pool(zfs_handle_t *zhp, const char *name)
2020 int len1 = strcspn(name, "/@");
2021 const char *zhname = zfs_get_name(zhp);
2022 int len2 = strcspn(zhname, "/@");
2026 return (strncmp(name, zhname, len1) == 0);
2030 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2032 upgrade_cbdata_t *cb = data;
2033 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2035 /* list if it's old/new */
2036 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2037 (cb->cb_newer && version > ZPL_VERSION)) {
2040 str = gettext("The following filesystems are "
2041 "formatted using a newer software version and\n"
2042 "cannot be accessed on the current system.\n\n");
2044 str = gettext("The following filesystems are "
2045 "out of date, and can be upgraded. After being\n"
2046 "upgraded, these filesystems (and any 'zfs send' "
2047 "streams generated from\n"
2048 "subsequent snapshots) will no longer be "
2049 "accessible by older software versions.\n\n");
2052 if (!cb->cb_foundone) {
2054 (void) printf(gettext("VER FILESYSTEM\n"));
2055 (void) printf(gettext("--- ------------\n"));
2056 cb->cb_foundone = B_TRUE;
2059 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2066 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2068 upgrade_cbdata_t *cb = data;
2069 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2070 int needed_spa_version;
2073 if (zfs_spa_version(zhp, &spa_version) < 0)
2076 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2078 if (needed_spa_version < 0)
2081 if (spa_version < needed_spa_version) {
2083 (void) printf(gettext("%s: can not be "
2084 "upgraded; the pool version needs to first "
2085 "be upgraded\nto version %d\n\n"),
2086 zfs_get_name(zhp), needed_spa_version);
2092 if (version < cb->cb_version) {
2094 (void) snprintf(verstr, sizeof (verstr),
2095 "%llu", cb->cb_version);
2096 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2098 * If they did "zfs upgrade -a", then we could
2099 * be doing ioctls to different pools. We need
2100 * to log this history once to each pool, and bypass
2101 * the normal history logging that happens in main().
2103 (void) zpool_log_history(g_zfs, history_str);
2104 log_history = B_FALSE;
2106 if (zfs_prop_set(zhp, "version", verstr) == 0)
2107 cb->cb_numupgraded++;
2110 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2111 } else if (version > cb->cb_version) {
2112 /* can't downgrade */
2113 (void) printf(gettext("%s: can not be downgraded; "
2114 "it is already at version %u\n"),
2115 zfs_get_name(zhp), version);
2118 cb->cb_numsamegraded++;
2126 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2129 zfs_do_upgrade(int argc, char **argv)
2131 boolean_t all = B_FALSE;
2132 boolean_t showversions = B_FALSE;
2134 upgrade_cbdata_t cb = { 0 };
2136 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2139 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2142 flags |= ZFS_ITER_RECURSE;
2145 showversions = B_TRUE;
2148 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2149 optarg, &cb.cb_version) != 0) {
2150 (void) fprintf(stderr,
2151 gettext("invalid version %s\n"), optarg);
2160 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2169 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2171 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2172 cb.cb_version || argc))
2174 if ((all || argc) && (showversions))
2180 /* Show info on available versions. */
2181 (void) printf(gettext("The following filesystem versions are "
2183 (void) printf(gettext("VER DESCRIPTION\n"));
2184 (void) printf("--- -----------------------------------------"
2185 "---------------\n");
2186 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2187 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2188 (void) printf(gettext(" 3 Case insensitive and filesystem "
2189 "user identifier (FUID)\n"));
2190 (void) printf(gettext(" 4 userquota, groupquota "
2192 (void) printf(gettext(" 5 System attributes\n"));
2193 (void) printf(gettext("\nFor more information on a particular "
2194 "version, including supported releases,\n"));
2195 (void) printf("see the ZFS Administration Guide.\n\n");
2197 } else if (argc || all) {
2198 /* Upgrade filesystems */
2199 if (cb.cb_version == 0)
2200 cb.cb_version = ZPL_VERSION;
2201 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2202 NULL, NULL, 0, upgrade_set_callback, &cb);
2203 (void) printf(gettext("%llu filesystems upgraded\n"),
2205 if (cb.cb_numsamegraded) {
2206 (void) printf(gettext("%llu filesystems already at "
2208 cb.cb_numsamegraded);
2210 if (cb.cb_numfailed != 0)
2213 /* List old-version filesytems */
2215 (void) printf(gettext("This system is currently running "
2216 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2218 flags |= ZFS_ITER_RECURSE;
2219 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2220 NULL, NULL, 0, upgrade_list_callback, &cb);
2222 found = cb.cb_foundone;
2223 cb.cb_foundone = B_FALSE;
2224 cb.cb_newer = B_TRUE;
2226 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2227 NULL, NULL, 0, upgrade_list_callback, &cb);
2229 if (!cb.cb_foundone && !found) {
2230 (void) printf(gettext("All filesystems are "
2231 "formatted with the current version.\n"));
2239 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2240 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2241 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2242 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2244 * -H Scripted mode; elide headers and separate columns by tabs.
2245 * -i Translate SID to POSIX ID.
2246 * -n Print numeric ID instead of user/group name.
2247 * -o Control which fields to display.
2248 * -p Use exact (parsable) numeric output.
2249 * -s Specify sort columns, descending order.
2250 * -S Specify sort columns, ascending order.
2251 * -t Control which object types to display.
2253 * Displays space consumed by, and quotas on, each user in the specified
2254 * filesystem or snapshot.
2257 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2258 enum us_field_types {
2264 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2265 static char *us_field_names[] = { "type", "name", "used", "quota" };
2266 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2268 #define USTYPE_PSX_GRP (1 << 0)
2269 #define USTYPE_PSX_USR (1 << 1)
2270 #define USTYPE_SMB_GRP (1 << 2)
2271 #define USTYPE_SMB_USR (1 << 3)
2272 #define USTYPE_ALL \
2273 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2275 static int us_type_bits[] = {
2282 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2285 typedef struct us_node {
2287 uu_avl_node_t usn_avlnode;
2288 uu_list_node_t usn_listnode;
2291 typedef struct us_cbdata {
2293 uu_avl_pool_t *cb_avl_pool;
2295 boolean_t cb_numname;
2296 boolean_t cb_nicenum;
2297 boolean_t cb_sid2posix;
2298 zfs_userquota_prop_t cb_prop;
2299 zfs_sort_column_t *cb_sortcol;
2300 size_t cb_width[USFIELD_LAST];
2303 static boolean_t us_populated = B_FALSE;
2306 zfs_sort_column_t *si_sortcol;
2307 boolean_t si_numname;
2311 us_field_index(char *field)
2315 for (i = 0; i < USFIELD_LAST; i++) {
2316 if (strcmp(field, us_field_names[i]) == 0)
2324 us_compare(const void *larg, const void *rarg, void *unused)
2326 const us_node_t *l = larg;
2327 const us_node_t *r = rarg;
2328 us_sort_info_t *si = (us_sort_info_t *)unused;
2329 zfs_sort_column_t *sortcol = si->si_sortcol;
2330 boolean_t numname = si->si_numname;
2331 nvlist_t *lnvl = l->usn_nvl;
2332 nvlist_t *rnvl = r->usn_nvl;
2336 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2343 zfs_prop_t prop = sortcol->sc_prop;
2344 const char *propname = NULL;
2345 boolean_t reverse = sortcol->sc_reverse;
2350 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2351 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2353 rc = (rv32 < lv32) ? 1 : -1;
2358 (void) nvlist_lookup_uint64(lnvl, propname,
2360 (void) nvlist_lookup_uint64(rnvl, propname,
2363 rc = (rv64 < lv64) ? 1 : -1;
2365 (void) nvlist_lookup_string(lnvl, propname,
2367 (void) nvlist_lookup_string(rnvl, propname,
2369 rc = strcmp(lvstr, rvstr);
2373 case ZFS_PROP_QUOTA:
2376 if (prop == ZFS_PROP_USED)
2380 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2381 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2383 rc = (rv64 < lv64) ? 1 : -1;
2392 return (reverse ? 1 : -1);
2394 return (reverse ? -1 : 1);
2399 * If entries still seem to be the same, check if they are of the same
2400 * type (smbentity is added only if we are doing SID to POSIX ID
2401 * translation where we can have duplicate type/name combinations).
2403 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2404 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2406 return (lvb < rvb ? -1 : 1);
2411 static inline const char *
2412 us_type2str(unsigned field_type)
2414 switch (field_type) {
2415 case USTYPE_PSX_USR:
2416 return ("POSIX User");
2417 case USTYPE_PSX_GRP:
2418 return ("POSIX Group");
2419 case USTYPE_SMB_USR:
2420 return ("SMB User");
2421 case USTYPE_SMB_GRP:
2422 return ("SMB Group");
2424 return ("Undefined");
2429 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2431 us_cbdata_t *cb = (us_cbdata_t *)arg;
2432 zfs_userquota_prop_t prop = cb->cb_prop;
2437 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2438 uu_avl_t *avl = cb->cb_avl;
2442 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2444 const char *typestr;
2448 int typeidx, nameidx, sizeidx;
2449 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2450 boolean_t smbentity = B_FALSE;
2452 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2454 node = safe_malloc(sizeof (us_node_t));
2455 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2456 node->usn_nvl = props;
2458 if (domain != NULL && domain[0] != '\0') {
2460 char sid[MAXNAMELEN + 32];
2464 int flag = IDMAP_REQ_FLG_USE_CACHE;
2469 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2471 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2472 type = USTYPE_SMB_GRP;
2474 err = sid_to_id(sid, B_FALSE, &id);
2477 type = USTYPE_SMB_USR;
2479 err = sid_to_id(sid, B_TRUE, &id);
2486 if (!cb->cb_sid2posix) {
2487 if (type == USTYPE_SMB_USR) {
2488 (void) idmap_getwinnamebyuid(rid, flag,
2491 (void) idmap_getwinnamebygid(rid, flag,
2501 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2503 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2504 type = USTYPE_PSX_GRP;
2505 if (!cb->cb_numname) {
2508 if ((g = getgrgid(rid)) != NULL)
2512 type = USTYPE_PSX_USR;
2513 if (!cb->cb_numname) {
2516 if ((p = getpwuid(rid)) != NULL)
2523 * Make sure that the type/name combination is unique when doing
2524 * SID to POSIX ID translation (hence changing the type from SMB to
2527 if (cb->cb_sid2posix &&
2528 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2531 /* Calculate/update width of TYPE field */
2532 typestr = us_type2str(type);
2533 typelen = strlen(gettext(typestr));
2534 typeidx = us_field_index("type");
2535 if (typelen > cb->cb_width[typeidx])
2536 cb->cb_width[typeidx] = typelen;
2537 if (nvlist_add_uint32(props, "type", type) != 0)
2540 /* Calculate/update width of NAME field */
2541 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2542 if (nvlist_add_uint64(props, "name", rid) != 0)
2544 namelen = snprintf(NULL, 0, "%u", rid);
2546 if (nvlist_add_string(props, "name", name) != 0)
2548 namelen = strlen(name);
2550 nameidx = us_field_index("name");
2551 if (namelen > cb->cb_width[nameidx])
2552 cb->cb_width[nameidx] = namelen;
2555 * Check if this type/name combination is in the list and update it;
2556 * otherwise add new node to the list.
2558 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2559 uu_avl_insert(avl, node, idx);
2564 props = node->usn_nvl;
2567 /* Calculate/update width of USED/QUOTA fields */
2569 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2571 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2572 sizelen = strlen(sizebuf);
2573 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2575 if (!nvlist_exists(props, "quota"))
2576 (void) nvlist_add_uint64(props, "quota", 0);
2579 if (!nvlist_exists(props, "used"))
2580 (void) nvlist_add_uint64(props, "used", 0);
2582 sizeidx = us_field_index(propname);
2583 if (sizelen > cb->cb_width[sizeidx])
2584 cb->cb_width[sizeidx] = sizelen;
2586 if (nvlist_add_uint64(props, propname, space) != 0)
2593 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2594 size_t *width, us_node_t *node)
2596 nvlist_t *nvl = node->usn_nvl;
2597 char valstr[MAXNAMELEN];
2598 boolean_t first = B_TRUE;
2604 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2605 if (!(ustype & types))
2608 while ((field = fields[cfield]) != USFIELD_LAST) {
2609 nvpair_t *nvp = NULL;
2613 char *strval = NULL;
2615 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2616 if (strcmp(nvpair_name(nvp),
2617 us_field_names[field]) == 0)
2621 type = nvpair_type(nvp);
2623 case DATA_TYPE_UINT32:
2624 (void) nvpair_value_uint32(nvp, &val32);
2626 case DATA_TYPE_UINT64:
2627 (void) nvpair_value_uint64(nvp, &val64);
2629 case DATA_TYPE_STRING:
2630 (void) nvpair_value_string(nvp, &strval);
2633 (void) fprintf(stderr, "invalid data type\n");
2638 strval = (char *)us_type2str(val32);
2641 if (type == DATA_TYPE_UINT64) {
2642 (void) sprintf(valstr, "%llu", val64);
2648 if (type == DATA_TYPE_UINT64) {
2650 (void) sprintf(valstr, "%llu", val64);
2652 zfs_nicenum(val64, valstr,
2655 if (field == USFIELD_QUOTA &&
2656 strcmp(valstr, "0") == 0)
2666 (void) printf("\t");
2671 (void) printf("%s", strval);
2672 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2673 (void) printf("%-*s", width[field], strval);
2675 (void) printf("%*s", width[field], strval);
2681 (void) printf("\n");
2685 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2686 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2694 boolean_t first = B_TRUE;
2696 while ((field = fields[cfield]) != USFIELD_LAST) {
2697 col = gettext(us_field_hdr[field]);
2698 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2699 (void) printf(first ? "%-*s" : " %-*s",
2702 (void) printf(first ? "%*s" : " %*s",
2708 (void) printf("\n");
2711 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2712 print_us_node(scripted, parsable, fields, types, width, node);
2714 nvlist_free(node->usn_nvl);
2719 zfs_do_userspace(int argc, char **argv)
2722 zfs_userquota_prop_t p;
2724 uu_avl_pool_t *avl_pool;
2726 uu_avl_walk_t *walk;
2728 char deffields[] = "type,name,used,quota";
2729 char *ofield = NULL;
2730 char *tfield = NULL;
2734 boolean_t scripted = B_FALSE;
2735 boolean_t prtnum = B_FALSE;
2736 boolean_t parsable = B_FALSE;
2737 boolean_t sid2posix = B_FALSE;
2740 zfs_sort_column_t *sortcol = NULL;
2741 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2745 uu_list_pool_t *listpool;
2747 uu_avl_index_t idx = 0;
2748 uu_list_index_t idx2 = 0;
2753 if (strcmp(argv[0], "groupspace") == 0)
2754 /* Toggle default group types */
2755 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2757 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2773 if (zfs_add_sort_column(&sortcol, optarg,
2774 c == 's' ? B_FALSE : B_TRUE) != 0) {
2775 (void) fprintf(stderr,
2776 gettext("invalid field '%s'\n"), optarg);
2787 (void) fprintf(stderr, gettext("missing argument for "
2788 "'%c' option\n"), optopt);
2792 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2802 (void) fprintf(stderr, gettext("missing dataset name\n"));
2806 (void) fprintf(stderr, gettext("too many arguments\n"));
2810 /* Use default output fields if not specified using -o */
2814 if ((delim = strchr(ofield, ',')) != NULL)
2816 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2817 (void) fprintf(stderr, gettext("invalid type '%s' "
2818 "for -o option\n"), ofield);
2823 } while (delim != NULL);
2824 fields[cfield] = USFIELD_LAST;
2826 /* Override output types (-t option) */
2827 if (tfield != NULL) {
2831 boolean_t found = B_FALSE;
2833 if ((delim = strchr(tfield, ',')) != NULL)
2835 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2837 if (strcmp(tfield, us_type_names[i]) == 0) {
2839 types |= us_type_bits[i];
2844 (void) fprintf(stderr, gettext("invalid type "
2845 "'%s' for -t option\n"), tfield);
2850 } while (delim != NULL);
2853 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2856 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2857 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2859 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2862 /* Always add default sorting columns */
2863 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2864 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2866 cb.cb_sortcol = sortcol;
2867 cb.cb_numname = prtnum;
2868 cb.cb_nicenum = !parsable;
2869 cb.cb_avl_pool = avl_pool;
2870 cb.cb_avl = avl_tree;
2871 cb.cb_sid2posix = sid2posix;
2873 for (i = 0; i < USFIELD_LAST; i++)
2874 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2876 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2877 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2878 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2879 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2880 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2883 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2888 if ((node = uu_avl_first(avl_tree)) == NULL)
2891 us_populated = B_TRUE;
2893 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2894 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2895 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2896 uu_list_node_init(node, &node->usn_listnode, listpool);
2898 while (node != NULL) {
2900 node = uu_avl_next(avl_tree, node);
2901 uu_avl_remove(avl_tree, rmnode);
2902 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2903 uu_list_insert(list, rmnode, idx2);
2906 for (node = uu_list_first(list); node != NULL;
2907 node = uu_list_next(list, node)) {
2908 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2910 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2911 uu_avl_insert(avl_tree, node, idx);
2914 uu_list_destroy(list);
2915 uu_list_pool_destroy(listpool);
2917 /* Print and free node nvlist memory */
2918 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2921 zfs_free_sort_columns(sortcol);
2923 /* Clean up the AVL tree */
2924 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2927 while ((node = uu_avl_walk_next(walk)) != NULL) {
2928 uu_avl_remove(cb.cb_avl, node);
2932 uu_avl_walk_end(walk);
2933 uu_avl_destroy(avl_tree);
2934 uu_avl_pool_destroy(avl_pool);
2940 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2941 * [-t type[,...]] [filesystem|volume|snapshot] ...
2943 * -H Scripted mode; elide headers and separate columns by tabs.
2944 * -p Display values in parsable (literal) format.
2945 * -r Recurse over all children.
2946 * -d Limit recursion by depth.
2947 * -o Control which fields to display.
2948 * -s Specify sort columns, descending order.
2949 * -S Specify sort columns, ascending order.
2950 * -t Control which object types to display.
2952 * When given no arguments, list all filesystems in the system.
2953 * Otherwise, list the specified datasets, optionally recursing down them if
2954 * '-r' is specified.
2956 typedef struct list_cbdata {
2958 boolean_t cb_literal;
2959 boolean_t cb_scripted;
2960 zprop_list_t *cb_proplist;
2964 * Given a list of columns to display, output appropriate headers for each one.
2967 print_header(list_cbdata_t *cb)
2969 zprop_list_t *pl = cb->cb_proplist;
2970 char headerbuf[ZFS_MAXPROPLEN];
2973 boolean_t first = B_TRUE;
2974 boolean_t right_justify;
2976 for (; pl != NULL; pl = pl->pl_next) {
2983 right_justify = B_FALSE;
2984 if (pl->pl_prop != ZPROP_INVAL) {
2985 header = zfs_prop_column_name(pl->pl_prop);
2986 right_justify = zfs_prop_align_right(pl->pl_prop);
2988 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2989 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2990 headerbuf[i] = '\0';
2994 if (pl->pl_next == NULL && !right_justify)
2995 (void) printf("%s", header);
2996 else if (right_justify)
2997 (void) printf("%*s", pl->pl_width, header);
2999 (void) printf("%-*s", pl->pl_width, header);
3002 (void) printf("\n");
3006 * Given a dataset and a list of fields, print out all the properties according
3007 * to the described layout.
3010 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3012 zprop_list_t *pl = cb->cb_proplist;
3013 boolean_t first = B_TRUE;
3014 char property[ZFS_MAXPROPLEN];
3015 nvlist_t *userprops = zfs_get_user_props(zhp);
3018 boolean_t right_justify;
3020 for (; pl != NULL; pl = pl->pl_next) {
3022 if (cb->cb_scripted)
3023 (void) printf("\t");
3030 if (pl->pl_prop == ZFS_PROP_NAME) {
3031 (void) strlcpy(property, zfs_get_name(zhp),
3034 right_justify = zfs_prop_align_right(pl->pl_prop);
3035 } else if (pl->pl_prop != ZPROP_INVAL) {
3036 if (zfs_prop_get(zhp, pl->pl_prop, property,
3037 sizeof (property), NULL, NULL, 0,
3038 cb->cb_literal) != 0)
3042 right_justify = zfs_prop_align_right(pl->pl_prop);
3043 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3044 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3045 property, sizeof (property), cb->cb_literal) != 0)
3049 right_justify = B_TRUE;
3050 } else if (zfs_prop_written(pl->pl_user_prop)) {
3051 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3052 property, sizeof (property), cb->cb_literal) != 0)
3056 right_justify = B_TRUE;
3058 if (nvlist_lookup_nvlist(userprops,
3059 pl->pl_user_prop, &propval) != 0)
3062 verify(nvlist_lookup_string(propval,
3063 ZPROP_VALUE, &propstr) == 0);
3064 right_justify = B_FALSE;
3068 * If this is being called in scripted mode, or if this is the
3069 * last column and it is left-justified, don't include a width
3072 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3073 (void) printf("%s", propstr);
3074 else if (right_justify)
3075 (void) printf("%*s", pl->pl_width, propstr);
3077 (void) printf("%-*s", pl->pl_width, propstr);
3080 (void) printf("\n");
3084 * Generic callback function to list a dataset or snapshot.
3087 list_callback(zfs_handle_t *zhp, void *data)
3089 list_cbdata_t *cbp = data;
3091 if (cbp->cb_first) {
3092 if (!cbp->cb_scripted)
3094 cbp->cb_first = B_FALSE;
3097 print_dataset(zhp, cbp);
3103 zfs_do_list(int argc, char **argv)
3106 static char default_fields[] =
3107 "name,used,available,referenced,mountpoint";
3108 int types = ZFS_TYPE_DATASET;
3109 boolean_t types_specified = B_FALSE;
3110 char *fields = NULL;
3111 list_cbdata_t cb = { 0 };
3115 zfs_sort_column_t *sortcol = NULL;
3116 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3119 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3125 cb.cb_literal = B_TRUE;
3126 flags |= ZFS_ITER_LITERAL_PROPS;
3129 limit = parse_depth(optarg, &flags);
3132 flags |= ZFS_ITER_RECURSE;
3135 cb.cb_scripted = B_TRUE;
3138 if (zfs_add_sort_column(&sortcol, optarg,
3140 (void) fprintf(stderr,
3141 gettext("invalid property '%s'\n"), optarg);
3146 if (zfs_add_sort_column(&sortcol, optarg,
3148 (void) fprintf(stderr,
3149 gettext("invalid property '%s'\n"), optarg);
3155 types_specified = B_TRUE;
3156 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3157 while (*optarg != '\0') {
3158 static char *type_subopts[] = { "filesystem",
3159 "volume", "snapshot", "snap", "bookmark",
3162 switch (getsubopt(&optarg, type_subopts,
3165 types |= ZFS_TYPE_FILESYSTEM;
3168 types |= ZFS_TYPE_VOLUME;
3172 types |= ZFS_TYPE_SNAPSHOT;
3175 types |= ZFS_TYPE_BOOKMARK;
3178 types = ZFS_TYPE_DATASET |
3182 (void) fprintf(stderr,
3183 gettext("invalid type '%s'\n"),
3190 (void) fprintf(stderr, gettext("missing argument for "
3191 "'%c' option\n"), optopt);
3195 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3205 fields = default_fields;
3208 * If we are only going to list snapshot names and sort by name,
3209 * then we can use faster version.
3211 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3212 flags |= ZFS_ITER_SIMPLE;
3215 * If "-o space" and no types were specified, don't display snapshots.
3217 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3218 types &= ~ZFS_TYPE_SNAPSHOT;
3221 * If the user specifies '-o all', the zprop_get_list() doesn't
3222 * normally include the name of the dataset. For 'zfs list', we always
3223 * want this property to be first.
3225 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3229 cb.cb_first = B_TRUE;
3231 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3232 limit, list_callback, &cb);
3234 zprop_free_list(cb.cb_proplist);
3235 zfs_free_sort_columns(sortcol);
3237 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3238 (void) printf(gettext("no datasets available\n"));
3244 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3245 * zfs rename [-f] -p <fs | vol> <fs | vol>
3246 * zfs rename -r <snap> <snap>
3247 * zfs rename -u [-p] <fs> <fs>
3249 * Renames the given dataset to another of the same type.
3251 * The '-p' flag creates all the non-existing ancestors of the target first.
3255 zfs_do_rename(int argc, char **argv)
3258 renameflags_t flags = { 0 };
3262 boolean_t parents = B_FALSE;
3263 char *snapshot = NULL;
3266 while ((c = getopt(argc, argv, "fpru")) != -1) {
3272 flags.recurse = B_TRUE;
3275 flags.nounmount = B_TRUE;
3278 flags.forceunmount = B_TRUE;
3282 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3291 /* check number of arguments */
3293 (void) fprintf(stderr, gettext("missing source dataset "
3298 (void) fprintf(stderr, gettext("missing target dataset "
3303 (void) fprintf(stderr, gettext("too many arguments\n"));
3307 if (flags.recurse && parents) {
3308 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3313 if (flags.recurse && strchr(argv[0], '@') == 0) {
3314 (void) fprintf(stderr, gettext("source dataset for recursive "
3315 "rename must be a snapshot\n"));
3319 if (flags.nounmount && parents) {
3320 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3325 if (flags.nounmount)
3326 types = ZFS_TYPE_FILESYSTEM;
3328 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3330 types = ZFS_TYPE_DATASET;
3332 if (flags.recurse) {
3334 * When we do recursive rename we are fine when the given
3335 * snapshot for the given dataset doesn't exist - it can
3336 * still exists below.
3339 snapshot = strchr(argv[0], '@');
3340 assert(snapshot != NULL);
3345 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3348 /* If we were asked and the name looks good, try to create ancestors. */
3349 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3350 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3355 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3364 * Promotes the given clone fs to be the parent
3368 zfs_do_promote(int argc, char **argv)
3374 if (argc > 1 && argv[1][0] == '-') {
3375 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3380 /* check number of arguments */
3382 (void) fprintf(stderr, gettext("missing clone filesystem"
3387 (void) fprintf(stderr, gettext("too many arguments\n"));
3391 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3395 ret = (zfs_promote(zhp) != 0);
3403 * zfs rollback [-rRf] <snapshot>
3405 * -r Delete any intervening snapshots before doing rollback
3406 * -R Delete any snapshots and their clones
3407 * -f ignored for backwards compatability
3409 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3410 * since then and making it the active dataset. If more recent snapshots exist,
3411 * the command will complain unless the '-r' flag is given.
3413 typedef struct rollback_cbdata {
3419 boolean_t cb_recurse;
3420 } rollback_cbdata_t;
3423 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3425 rollback_cbdata_t *cbp = data;
3427 if (cbp->cb_first && cbp->cb_recurse) {
3428 (void) fprintf(stderr, gettext("cannot rollback to "
3429 "'%s': clones of previous snapshots exist\n"),
3431 (void) fprintf(stderr, gettext("use '-R' to "
3432 "force deletion of the following clones and "
3438 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3445 * Report any snapshots more recent than the one specified. Used when '-r' is
3446 * not specified. We reuse this same callback for the snapshot dependents - if
3447 * 'cb_dependent' is set, then this is a dependent and we should report it
3448 * without checking the transaction group.
3451 rollback_check(zfs_handle_t *zhp, void *data)
3453 rollback_cbdata_t *cbp = data;
3455 if (cbp->cb_doclones) {
3460 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3461 if (cbp->cb_first && !cbp->cb_recurse) {
3462 (void) fprintf(stderr, gettext("cannot "
3463 "rollback to '%s': more recent snapshots "
3464 "or bookmarks exist\n"),
3466 (void) fprintf(stderr, gettext("use '-r' to "
3467 "force deletion of the following "
3468 "snapshots and bookmarks:\n"));
3473 if (cbp->cb_recurse) {
3474 if (zfs_iter_dependents(zhp, B_TRUE,
3475 rollback_check_dependent, cbp) != 0) {
3480 (void) fprintf(stderr, "%s\n",
3489 zfs_do_rollback(int argc, char **argv)
3493 boolean_t force = B_FALSE;
3494 rollback_cbdata_t cb = { 0 };
3495 zfs_handle_t *zhp, *snap;
3496 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3500 while ((c = getopt(argc, argv, "rRf")) != -1) {
3513 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3522 /* check number of arguments */
3524 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3528 (void) fprintf(stderr, gettext("too many arguments\n"));
3532 /* open the snapshot */
3533 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3536 /* open the parent dataset */
3537 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3538 verify((delim = strrchr(parentname, '@')) != NULL);
3540 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3546 * Check for more recent snapshots and/or clones based on the presence
3549 cb.cb_target = argv[0];
3550 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3551 cb.cb_first = B_TRUE;
3553 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3555 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3558 if ((ret = cb.cb_error) != 0)
3562 * Rollback parent to the given snapshot.
3564 ret = zfs_rollback(zhp, snap, force);
3577 * zfs set property=value ... { fs | snap | vol } ...
3579 * Sets the given properties for all datasets specified on the command line.
3583 set_callback(zfs_handle_t *zhp, void *data)
3585 nvlist_t *props = data;
3587 if (zfs_prop_set_list(zhp, props) != 0) {
3588 switch (libzfs_errno(g_zfs)) {
3589 case EZFS_MOUNTFAILED:
3590 (void) fprintf(stderr, gettext("property may be set "
3591 "but unable to remount filesystem\n"));
3593 case EZFS_SHARENFSFAILED:
3594 (void) fprintf(stderr, gettext("property may be set "
3595 "but unable to reshare filesystem\n"));
3604 zfs_do_set(int argc, char **argv)
3606 nvlist_t *props = NULL;
3607 int ds_start = -1; /* argv idx of first dataset arg */
3610 /* check for options */
3611 if (argc > 1 && argv[1][0] == '-') {
3612 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3617 /* check number of arguments */
3619 (void) fprintf(stderr, gettext("missing arguments\n"));
3623 if (strchr(argv[1], '=') == NULL) {
3624 (void) fprintf(stderr, gettext("missing property=value "
3627 (void) fprintf(stderr, gettext("missing dataset "
3633 /* validate argument order: prop=val args followed by dataset args */
3634 for (int i = 1; i < argc; i++) {
3635 if (strchr(argv[i], '=') != NULL) {
3637 /* out-of-order prop=val argument */
3638 (void) fprintf(stderr, gettext("invalid "
3639 "argument order\n"), i);
3642 } else if (ds_start < 0) {
3647 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3651 /* Populate a list of property settings */
3652 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3654 for (int i = 1; i < ds_start; i++) {
3655 if ((ret = parseprop(props, argv[i])) != 0)
3659 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3660 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3667 typedef struct snap_cbdata {
3669 boolean_t sd_recursive;
3670 const char *sd_snapname;
3674 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3676 snap_cbdata_t *sd = arg;
3681 if (sd->sd_recursive &&
3682 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3687 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3690 fnvlist_add_boolean(sd->sd_nvl, name);
3693 if (sd->sd_recursive)
3694 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3700 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3702 * Creates a snapshot with the given name. While functionally equivalent to
3703 * 'zfs create', it is a separate command to differentiate intent.
3706 zfs_do_snapshot(int argc, char **argv)
3711 snap_cbdata_t sd = { 0 };
3712 boolean_t multiple_snaps = B_FALSE;
3714 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3716 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3720 while ((c = getopt(argc, argv, "ro:")) != -1) {
3723 if (parseprop(props, optarg) != 0)
3727 sd.sd_recursive = B_TRUE;
3728 multiple_snaps = B_TRUE;
3731 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3740 /* check number of arguments */
3742 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3747 multiple_snaps = B_TRUE;
3748 for (; argc > 0; argc--, argv++) {
3752 atp = strchr(argv[0], '@');
3756 sd.sd_snapname = atp + 1;
3757 zhp = zfs_open(g_zfs, argv[0],
3758 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3761 if (zfs_snapshot_cb(zhp, &sd) != 0)
3765 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3766 nvlist_free(sd.sd_nvl);
3768 if (ret != 0 && multiple_snaps)
3769 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3773 nvlist_free(sd.sd_nvl);
3780 * Send a backup stream to stdout.
3783 zfs_do_send(int argc, char **argv)
3785 char *fromname = NULL;
3786 char *toname = NULL;
3787 char *resume_token = NULL;
3790 sendflags_t flags = { 0 };
3792 nvlist_t *dbgnv = NULL;
3793 boolean_t extraverbose = B_FALSE;
3795 struct option long_options[] = {
3796 {"replicate", no_argument, NULL, 'R'},
3797 {"props", no_argument, NULL, 'p'},
3798 {"parsable", no_argument, NULL, 'P'},
3799 {"dedup", no_argument, NULL, 'D'},
3800 {"verbose", no_argument, NULL, 'v'},
3801 {"dryrun", no_argument, NULL, 'n'},
3802 {"large-block", no_argument, NULL, 'L'},
3803 {"embed", no_argument, NULL, 'e'},
3804 {"resume", required_argument, NULL, 't'},
3805 {"compressed", no_argument, NULL, 'c'},
3810 while ((c = getopt_long(argc, argv, ":i:I:RbDpvnPLet:c", long_options,
3822 flags.doall = B_TRUE;
3825 flags.replicate = B_TRUE;
3828 flags.props = B_TRUE;
3831 flags.parsable = B_TRUE;
3832 flags.verbose = B_TRUE;
3836 extraverbose = B_TRUE;
3837 flags.verbose = B_TRUE;
3838 flags.progress = B_TRUE;
3841 flags.dedup = B_TRUE;
3844 flags.dryrun = B_TRUE;
3847 flags.largeblock = B_TRUE;
3850 flags.embed_data = B_TRUE;
3853 resume_token = optarg;
3856 flags.compress = B_TRUE;
3859 (void) fprintf(stderr, gettext("missing argument for "
3860 "'%c' option\n"), optopt);
3866 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3875 if (resume_token != NULL) {
3876 if (fromname != NULL || flags.replicate || flags.props ||
3878 (void) fprintf(stderr,
3879 gettext("invalid flags combined with -t\n"));
3883 (void) fprintf(stderr, gettext("no additional "
3884 "arguments are permitted with -t\n"));
3889 (void) fprintf(stderr,
3890 gettext("missing snapshot argument\n"));
3894 (void) fprintf(stderr, gettext("too many arguments\n"));
3899 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3900 (void) fprintf(stderr,
3901 gettext("Error: Stream can not be written to a terminal.\n"
3902 "You must redirect standard output.\n"));
3906 if (resume_token != NULL) {
3907 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3912 * Special case sending a filesystem, or from a bookmark.
3914 if (strchr(argv[0], '@') == NULL ||
3915 (fromname && strchr(fromname, '#') != NULL)) {
3916 char frombuf[ZFS_MAX_DATASET_NAME_LEN];
3917 enum lzc_send_flags lzc_flags = 0;
3919 if (flags.replicate || flags.doall || flags.props ||
3920 flags.dedup || flags.dryrun || flags.verbose ||
3922 (void) fprintf(stderr,
3924 "Unsupported flag with filesystem or bookmark.\n"));
3928 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3932 if (flags.largeblock)
3933 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3934 if (flags.embed_data)
3935 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3937 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
3939 if (fromname != NULL &&
3940 (fromname[0] == '#' || fromname[0] == '@')) {
3942 * Incremental source name begins with # or @.
3943 * Default to same fs as target.
3945 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3946 cp = strchr(frombuf, '@');
3949 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3952 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3957 cp = strchr(argv[0], '@');
3960 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3965 * If they specified the full path to the snapshot, chop off
3966 * everything except the short name of the snapshot, but special
3967 * case if they specify the origin.
3969 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3970 char origin[ZFS_MAX_DATASET_NAME_LEN];
3973 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3974 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3976 if (strcmp(origin, fromname) == 0) {
3978 flags.fromorigin = B_TRUE;
3981 if (cp != fromname && strcmp(argv[0], fromname)) {
3982 (void) fprintf(stderr,
3983 gettext("incremental source must be "
3984 "in same filesystem\n"));
3988 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3989 (void) fprintf(stderr,
3990 gettext("invalid incremental source\n"));
3996 if (flags.replicate && fromname == NULL)
3997 flags.doall = B_TRUE;
3999 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
4000 extraverbose ? &dbgnv : NULL);
4002 if (extraverbose && dbgnv != NULL) {
4004 * dump_nvlist prints to stdout, but that's been
4005 * redirected to a file. Make it print to stderr
4008 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
4009 dump_nvlist(dbgnv, 0);
4018 * Restore a backup stream from stdin.
4021 zfs_do_receive(int argc, char **argv)
4024 recvflags_t flags = { 0 };
4025 boolean_t abort_resumable = B_FALSE;
4028 nvpair_t *nvp = NULL;
4030 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4034 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
4037 if (parseprop(props, optarg) != 0)
4041 flags.isprefix = B_TRUE;
4044 flags.isprefix = B_TRUE;
4045 flags.istail = B_TRUE;
4048 flags.dryrun = B_TRUE;
4051 flags.nomount = B_TRUE;
4054 flags.verbose = B_TRUE;
4057 flags.resumable = B_TRUE;
4060 flags.force = B_TRUE;
4063 abort_resumable = B_TRUE;
4066 (void) fprintf(stderr, gettext("missing argument for "
4067 "'%c' option\n"), optopt);
4071 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4080 /* check number of arguments */
4082 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4086 (void) fprintf(stderr, gettext("too many arguments\n"));
4090 while ((nvp = nvlist_next_nvpair(props, nvp))) {
4091 if (strcmp(nvpair_name(nvp), "origin") != 0) {
4092 (void) fprintf(stderr, gettext("invalid option"));
4097 if (abort_resumable) {
4098 if (flags.isprefix || flags.istail || flags.dryrun ||
4099 flags.resumable || flags.nomount) {
4100 (void) fprintf(stderr, gettext("invalid option"));
4104 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4105 (void) snprintf(namebuf, sizeof (namebuf),
4106 "%s/%%recv", argv[0]);
4108 if (zfs_dataset_exists(g_zfs, namebuf,
4109 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4110 zfs_handle_t *zhp = zfs_open(g_zfs,
4111 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4114 err = zfs_destroy(zhp, B_FALSE);
4116 zfs_handle_t *zhp = zfs_open(g_zfs,
4117 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4120 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4121 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4122 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4123 (void) fprintf(stderr,
4124 gettext("'%s' does not have any "
4125 "resumable receive state to abort\n"),
4129 err = zfs_destroy(zhp, B_FALSE);
4135 if (isatty(STDIN_FILENO)) {
4136 (void) fprintf(stderr,
4137 gettext("Error: Backup stream can not be read "
4138 "from a terminal.\n"
4139 "You must redirect standard input.\n"));
4142 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4148 * allow/unallow stuff
4150 /* copied from zfs/sys/dsl_deleg.h */
4151 #define ZFS_DELEG_PERM_CREATE "create"
4152 #define ZFS_DELEG_PERM_DESTROY "destroy"
4153 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4154 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4155 #define ZFS_DELEG_PERM_CLONE "clone"
4156 #define ZFS_DELEG_PERM_PROMOTE "promote"
4157 #define ZFS_DELEG_PERM_RENAME "rename"
4158 #define ZFS_DELEG_PERM_MOUNT "mount"
4159 #define ZFS_DELEG_PERM_SHARE "share"
4160 #define ZFS_DELEG_PERM_SEND "send"
4161 #define ZFS_DELEG_PERM_RECEIVE "receive"
4162 #define ZFS_DELEG_PERM_ALLOW "allow"
4163 #define ZFS_DELEG_PERM_USERPROP "userprop"
4164 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4165 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4166 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4167 #define ZFS_DELEG_PERM_USERUSED "userused"
4168 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4169 #define ZFS_DELEG_PERM_HOLD "hold"
4170 #define ZFS_DELEG_PERM_RELEASE "release"
4171 #define ZFS_DELEG_PERM_DIFF "diff"
4172 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4174 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4176 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4177 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4178 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4179 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4180 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4181 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4182 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4183 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4184 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4185 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4186 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4187 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4188 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4189 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4190 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4191 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4192 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4194 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4195 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4196 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4197 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4198 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4199 { NULL, ZFS_DELEG_NOTE_NONE }
4202 /* permission structure */
4203 typedef struct deleg_perm {
4204 zfs_deleg_who_type_t dp_who_type;
4205 const char *dp_name;
4207 boolean_t dp_descend;
4211 typedef struct deleg_perm_node {
4212 deleg_perm_t dpn_perm;
4214 uu_avl_node_t dpn_avl_node;
4215 } deleg_perm_node_t;
4217 typedef struct fs_perm fs_perm_t;
4219 /* permissions set */
4220 typedef struct who_perm {
4221 zfs_deleg_who_type_t who_type;
4222 const char *who_name; /* id */
4223 char who_ug_name[256]; /* user/group name */
4224 fs_perm_t *who_fsperm; /* uplink */
4226 uu_avl_t *who_deleg_perm_avl; /* permissions */
4230 typedef struct who_perm_node {
4231 who_perm_t who_perm;
4232 uu_avl_node_t who_avl_node;
4235 typedef struct fs_perm_set fs_perm_set_t;
4236 /* fs permissions */
4238 const char *fsp_name;
4240 uu_avl_t *fsp_sc_avl; /* sets,create */
4241 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4243 fs_perm_set_t *fsp_set; /* uplink */
4247 typedef struct fs_perm_node {
4248 fs_perm_t fspn_fsperm;
4251 uu_list_node_t fspn_list_node;
4254 /* top level structure */
4255 struct fs_perm_set {
4256 uu_list_pool_t *fsps_list_pool;
4257 uu_list_t *fsps_list; /* list of fs_perms */
4259 uu_avl_pool_t *fsps_named_set_avl_pool;
4260 uu_avl_pool_t *fsps_who_perm_avl_pool;
4261 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4264 static inline const char *
4265 deleg_perm_type(zfs_deleg_note_t note)
4271 case ZFS_DELEG_NOTE_GROUPQUOTA:
4272 case ZFS_DELEG_NOTE_GROUPUSED:
4273 case ZFS_DELEG_NOTE_USERPROP:
4274 case ZFS_DELEG_NOTE_USERQUOTA:
4275 case ZFS_DELEG_NOTE_USERUSED:
4277 return (gettext("other"));
4279 return (gettext("subcommand"));
4284 who_type2weight(zfs_deleg_who_type_t who_type)
4288 case ZFS_DELEG_NAMED_SET_SETS:
4289 case ZFS_DELEG_NAMED_SET:
4292 case ZFS_DELEG_CREATE_SETS:
4293 case ZFS_DELEG_CREATE:
4296 case ZFS_DELEG_USER_SETS:
4297 case ZFS_DELEG_USER:
4300 case ZFS_DELEG_GROUP_SETS:
4301 case ZFS_DELEG_GROUP:
4304 case ZFS_DELEG_EVERYONE_SETS:
4305 case ZFS_DELEG_EVERYONE:
4317 who_perm_compare(const void *larg, const void *rarg, void *unused)
4319 const who_perm_node_t *l = larg;
4320 const who_perm_node_t *r = rarg;
4321 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4322 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4323 int lweight = who_type2weight(ltype);
4324 int rweight = who_type2weight(rtype);
4325 int res = lweight - rweight;
4327 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4328 ZFS_MAX_DELEG_NAME-1);
4340 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4342 const deleg_perm_node_t *l = larg;
4343 const deleg_perm_node_t *r = rarg;
4344 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4345 ZFS_MAX_DELEG_NAME-1);
4357 fs_perm_set_init(fs_perm_set_t *fspset)
4359 bzero(fspset, sizeof (fs_perm_set_t));
4361 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4362 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4363 NULL, UU_DEFAULT)) == NULL)
4365 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4366 UU_DEFAULT)) == NULL)
4369 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4370 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4371 who_perm_node_t, who_avl_node), who_perm_compare,
4372 UU_DEFAULT)) == NULL)
4375 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4376 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4377 who_perm_node_t, who_avl_node), who_perm_compare,
4378 UU_DEFAULT)) == NULL)
4381 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4382 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4383 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4388 static inline void fs_perm_fini(fs_perm_t *);
4389 static inline void who_perm_fini(who_perm_t *);
4392 fs_perm_set_fini(fs_perm_set_t *fspset)
4394 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4396 while (node != NULL) {
4397 fs_perm_node_t *next_node =
4398 uu_list_next(fspset->fsps_list, node);
4399 fs_perm_t *fsperm = &node->fspn_fsperm;
4400 fs_perm_fini(fsperm);
4401 uu_list_remove(fspset->fsps_list, node);
4406 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4407 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4408 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4412 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4415 deleg_perm->dp_who_type = type;
4416 deleg_perm->dp_name = name;
4420 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4421 zfs_deleg_who_type_t type, const char *name)
4423 uu_avl_pool_t *pool;
4424 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4426 bzero(who_perm, sizeof (who_perm_t));
4428 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4429 UU_DEFAULT)) == NULL)
4432 who_perm->who_type = type;
4433 who_perm->who_name = name;
4434 who_perm->who_fsperm = fsperm;
4438 who_perm_fini(who_perm_t *who_perm)
4440 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4442 while (node != NULL) {
4443 deleg_perm_node_t *next_node =
4444 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4446 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4451 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4455 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4457 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4458 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4460 bzero(fsperm, sizeof (fs_perm_t));
4462 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4466 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4470 fsperm->fsp_set = fspset;
4471 fsperm->fsp_name = fsname;
4475 fs_perm_fini(fs_perm_t *fsperm)
4477 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4478 while (node != NULL) {
4479 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4481 who_perm_t *who_perm = &node->who_perm;
4482 who_perm_fini(who_perm);
4483 uu_avl_remove(fsperm->fsp_sc_avl, node);
4488 node = uu_avl_first(fsperm->fsp_uge_avl);
4489 while (node != NULL) {
4490 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4492 who_perm_t *who_perm = &node->who_perm;
4493 who_perm_fini(who_perm);
4494 uu_avl_remove(fsperm->fsp_uge_avl, node);
4499 uu_avl_destroy(fsperm->fsp_sc_avl);
4500 uu_avl_destroy(fsperm->fsp_uge_avl);
4504 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4505 zfs_deleg_who_type_t who_type, const char *name, char locality)
4507 uu_avl_index_t idx = 0;
4509 deleg_perm_node_t *found_node = NULL;
4510 deleg_perm_t *deleg_perm = &node->dpn_perm;
4512 deleg_perm_init(deleg_perm, who_type, name);
4514 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4516 uu_avl_insert(avl, node, idx);
4519 deleg_perm = &node->dpn_perm;
4524 case ZFS_DELEG_LOCAL:
4525 deleg_perm->dp_local = B_TRUE;
4527 case ZFS_DELEG_DESCENDENT:
4528 deleg_perm->dp_descend = B_TRUE;
4533 assert(B_FALSE); /* invalid locality */
4538 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4540 nvpair_t *nvp = NULL;
4541 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4542 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4543 zfs_deleg_who_type_t who_type = who_perm->who_type;
4545 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4546 const char *name = nvpair_name(nvp);
4547 data_type_t type = nvpair_type(nvp);
4548 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4549 deleg_perm_node_t *node =
4550 safe_malloc(sizeof (deleg_perm_node_t));
4552 assert(type == DATA_TYPE_BOOLEAN);
4554 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4555 set_deleg_perm_node(avl, node, who_type, name, locality);
4562 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4564 nvpair_t *nvp = NULL;
4565 fs_perm_set_t *fspset = fsperm->fsp_set;
4567 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4568 nvlist_t *nvl2 = NULL;
4569 const char *name = nvpair_name(nvp);
4570 uu_avl_t *avl = NULL;
4571 uu_avl_pool_t *avl_pool = NULL;
4572 zfs_deleg_who_type_t perm_type = name[0];
4573 char perm_locality = name[1];
4574 const char *perm_name = name + 3;
4575 boolean_t is_set = B_TRUE;
4576 who_perm_t *who_perm = NULL;
4578 assert('$' == name[2]);
4580 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4583 switch (perm_type) {
4584 case ZFS_DELEG_CREATE:
4585 case ZFS_DELEG_CREATE_SETS:
4586 case ZFS_DELEG_NAMED_SET:
4587 case ZFS_DELEG_NAMED_SET_SETS:
4588 avl_pool = fspset->fsps_named_set_avl_pool;
4589 avl = fsperm->fsp_sc_avl;
4591 case ZFS_DELEG_USER:
4592 case ZFS_DELEG_USER_SETS:
4593 case ZFS_DELEG_GROUP:
4594 case ZFS_DELEG_GROUP_SETS:
4595 case ZFS_DELEG_EVERYONE:
4596 case ZFS_DELEG_EVERYONE_SETS:
4597 avl_pool = fspset->fsps_who_perm_avl_pool;
4598 avl = fsperm->fsp_uge_avl;
4602 assert(!"unhandled zfs_deleg_who_type_t");
4606 who_perm_node_t *found_node = NULL;
4607 who_perm_node_t *node = safe_malloc(
4608 sizeof (who_perm_node_t));
4609 who_perm = &node->who_perm;
4610 uu_avl_index_t idx = 0;
4612 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4613 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4615 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4617 if (avl == fsperm->fsp_uge_avl) {
4619 struct passwd *p = NULL;
4620 struct group *g = NULL;
4621 const char *nice_name = NULL;
4623 switch (perm_type) {
4624 case ZFS_DELEG_USER_SETS:
4625 case ZFS_DELEG_USER:
4626 rid = atoi(perm_name);
4629 nice_name = p->pw_name;
4631 case ZFS_DELEG_GROUP_SETS:
4632 case ZFS_DELEG_GROUP:
4633 rid = atoi(perm_name);
4636 nice_name = g->gr_name;
4643 if (nice_name != NULL)
4645 node->who_perm.who_ug_name,
4649 uu_avl_insert(avl, node, idx);
4652 who_perm = &node->who_perm;
4656 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4663 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4665 nvpair_t *nvp = NULL;
4666 uu_avl_index_t idx = 0;
4668 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4669 nvlist_t *nvl2 = NULL;
4670 const char *fsname = nvpair_name(nvp);
4671 data_type_t type = nvpair_type(nvp);
4672 fs_perm_t *fsperm = NULL;
4673 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4677 fsperm = &node->fspn_fsperm;
4679 assert(DATA_TYPE_NVLIST == type);
4681 uu_list_node_init(node, &node->fspn_list_node,
4682 fspset->fsps_list_pool);
4684 idx = uu_list_numnodes(fspset->fsps_list);
4685 fs_perm_init(fsperm, fspset, fsname);
4687 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4690 (void) parse_fs_perm(fsperm, nvl2);
4692 uu_list_insert(fspset->fsps_list, node, idx);
4698 static inline const char *
4699 deleg_perm_comment(zfs_deleg_note_t note)
4701 const char *str = "";
4706 case ZFS_DELEG_NOTE_ALLOW:
4707 str = gettext("Must also have the permission that is being"
4708 "\n\t\t\t\tallowed");
4710 case ZFS_DELEG_NOTE_CLONE:
4711 str = gettext("Must also have the 'create' ability and 'mount'"
4712 "\n\t\t\t\tability in the origin file system");
4714 case ZFS_DELEG_NOTE_CREATE:
4715 str = gettext("Must also have the 'mount' ability");
4717 case ZFS_DELEG_NOTE_DESTROY:
4718 str = gettext("Must also have the 'mount' ability");
4720 case ZFS_DELEG_NOTE_DIFF:
4721 str = gettext("Allows lookup of paths within a dataset;"
4722 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4723 "\n\t\t\t\tin order to use zfs diff");
4725 case ZFS_DELEG_NOTE_HOLD:
4726 str = gettext("Allows adding a user hold to a snapshot");
4728 case ZFS_DELEG_NOTE_MOUNT:
4729 str = gettext("Allows mount/umount of ZFS datasets");
4731 case ZFS_DELEG_NOTE_PROMOTE:
4732 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4733 " 'promote' ability in the origin file system");
4735 case ZFS_DELEG_NOTE_RECEIVE:
4736 str = gettext("Must also have the 'mount' and 'create'"
4739 case ZFS_DELEG_NOTE_RELEASE:
4740 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4741 "might destroy the snapshot");
4743 case ZFS_DELEG_NOTE_RENAME:
4744 str = gettext("Must also have the 'mount' and 'create'"
4745 "\n\t\t\t\tability in the new parent");
4747 case ZFS_DELEG_NOTE_ROLLBACK:
4750 case ZFS_DELEG_NOTE_SEND:
4753 case ZFS_DELEG_NOTE_SHARE:
4754 str = gettext("Allows sharing file systems over NFS or SMB"
4755 "\n\t\t\t\tprotocols");
4757 case ZFS_DELEG_NOTE_SNAPSHOT:
4761 * case ZFS_DELEG_NOTE_VSCAN:
4762 * str = gettext("");
4766 case ZFS_DELEG_NOTE_GROUPQUOTA:
4767 str = gettext("Allows accessing any groupquota@... property");
4769 case ZFS_DELEG_NOTE_GROUPUSED:
4770 str = gettext("Allows reading any groupused@... property");
4772 case ZFS_DELEG_NOTE_USERPROP:
4773 str = gettext("Allows changing any user property");
4775 case ZFS_DELEG_NOTE_USERQUOTA:
4776 str = gettext("Allows accessing any userquota@... property");
4778 case ZFS_DELEG_NOTE_USERUSED:
4779 str = gettext("Allows reading any userused@... property");
4797 boolean_t recursive; /* unallow only */
4798 boolean_t prt_usage;
4800 boolean_t prt_perms;
4803 const char *dataset;
4807 prop_cmp(const void *a, const void *b)
4809 const char *str1 = *(const char **)a;
4810 const char *str2 = *(const char **)b;
4811 return (strcmp(str1, str2));
4815 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4817 const char *opt_desc[] = {
4818 "-h", gettext("show this help message and exit"),
4819 "-l", gettext("set permission locally"),
4820 "-d", gettext("set permission for descents"),
4821 "-u", gettext("set permission for user"),
4822 "-g", gettext("set permission for group"),
4823 "-e", gettext("set permission for everyone"),
4824 "-c", gettext("set create time permission"),
4825 "-s", gettext("define permission set"),
4827 "-r", gettext("remove permissions recursively"),
4829 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4830 size_t allow_size = unallow_size - 2;
4831 const char *props[ZFS_NUM_PROPS];
4834 FILE *fp = requested ? stdout : stderr;
4835 zprop_desc_t *pdtbl = zfs_prop_get_table();
4836 const char *fmt = gettext("%-16s %-14s\t%s\n");
4838 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4840 (void) fprintf(fp, gettext("Options:\n"));
4841 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4842 const char *opt = opt_desc[i++];
4843 const char *optdsc = opt_desc[i];
4844 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4847 (void) fprintf(fp, gettext("\nThe following permissions are "
4849 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4851 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4852 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4853 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4854 const char *perm_type = deleg_perm_type(perm_note);
4855 const char *perm_comment = deleg_perm_comment(perm_note);
4856 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4859 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4860 zprop_desc_t *pd = &pdtbl[i];
4861 if (pd->pd_visible != B_TRUE)
4864 if (pd->pd_attr == PROP_READONLY)
4867 props[count++] = pd->pd_name;
4869 props[count] = NULL;
4871 qsort(props, count, sizeof (char *), prop_cmp);
4873 for (i = 0; i < count; i++)
4874 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4877 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4879 exit(requested ? 0 : 2);
4882 static inline const char *
4883 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4886 if (un && argc == expected_argc - 1)
4888 else if (argc == expected_argc)
4889 *permsp = argv[argc - 2];
4891 allow_usage(un, B_FALSE,
4892 gettext("wrong number of parameters\n"));
4894 return (argv[argc - 1]);
4898 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4900 int uge_sum = opts->user + opts->group + opts->everyone;
4901 int csuge_sum = opts->create + opts->set + uge_sum;
4902 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4903 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4906 allow_usage(un, B_FALSE,
4907 gettext("-u, -g, and -e are mutually exclusive\n"));
4909 if (opts->prt_usage) {
4910 if (argc == 0 && all_sum == 0)
4911 allow_usage(un, B_TRUE, NULL);
4918 allow_usage(un, B_FALSE,
4919 gettext("invalid options combined with -s\n"));
4921 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4922 if (argv[0][0] != '@')
4923 allow_usage(un, B_FALSE,
4924 gettext("invalid set name: missing '@' prefix\n"));
4925 opts->who = argv[0];
4926 } else if (opts->create) {
4927 if (ldcsuge_sum > 1)
4928 allow_usage(un, B_FALSE,
4929 gettext("invalid options combined with -c\n"));
4930 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4931 } else if (opts->everyone) {
4933 allow_usage(un, B_FALSE,
4934 gettext("invalid options combined with -e\n"));
4935 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4936 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4938 opts->everyone = B_TRUE;
4941 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4942 } else if (argc == 1 && !un) {
4943 opts->prt_perms = B_TRUE;
4944 opts->dataset = argv[argc-1];
4946 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4947 opts->who = argv[0];
4950 if (!opts->local && !opts->descend) {
4951 opts->local = B_TRUE;
4952 opts->descend = B_TRUE;
4957 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4958 const char *who, char *perms, nvlist_t *top_nvl)
4961 char ld[2] = { '\0', '\0' };
4962 char who_buf[MAXNAMELEN + 32];
4963 char base_type = '\0';
4964 char set_type = '\0';
4965 nvlist_t *base_nvl = NULL;
4966 nvlist_t *set_nvl = NULL;
4969 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4971 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4975 case ZFS_DELEG_NAMED_SET_SETS:
4976 case ZFS_DELEG_NAMED_SET:
4977 set_type = ZFS_DELEG_NAMED_SET_SETS;
4978 base_type = ZFS_DELEG_NAMED_SET;
4979 ld[0] = ZFS_DELEG_NA;
4981 case ZFS_DELEG_CREATE_SETS:
4982 case ZFS_DELEG_CREATE:
4983 set_type = ZFS_DELEG_CREATE_SETS;
4984 base_type = ZFS_DELEG_CREATE;
4985 ld[0] = ZFS_DELEG_NA;
4987 case ZFS_DELEG_USER_SETS:
4988 case ZFS_DELEG_USER:
4989 set_type = ZFS_DELEG_USER_SETS;
4990 base_type = ZFS_DELEG_USER;
4992 ld[0] = ZFS_DELEG_LOCAL;
4994 ld[1] = ZFS_DELEG_DESCENDENT;
4996 case ZFS_DELEG_GROUP_SETS:
4997 case ZFS_DELEG_GROUP:
4998 set_type = ZFS_DELEG_GROUP_SETS;
4999 base_type = ZFS_DELEG_GROUP;
5001 ld[0] = ZFS_DELEG_LOCAL;
5003 ld[1] = ZFS_DELEG_DESCENDENT;
5005 case ZFS_DELEG_EVERYONE_SETS:
5006 case ZFS_DELEG_EVERYONE:
5007 set_type = ZFS_DELEG_EVERYONE_SETS;
5008 base_type = ZFS_DELEG_EVERYONE;
5010 ld[0] = ZFS_DELEG_LOCAL;
5012 ld[1] = ZFS_DELEG_DESCENDENT;
5016 assert(set_type != '\0' && base_type != '\0');
5019 if (perms != NULL) {
5021 char *end = curr + strlen(perms);
5023 while (curr < end) {
5024 char *delim = strchr(curr, ',');
5035 (void) nvlist_add_boolean(nvl, curr);
5041 for (i = 0; i < 2; i++) {
5042 char locality = ld[i];
5046 if (!nvlist_empty(base_nvl)) {
5048 (void) snprintf(who_buf,
5049 sizeof (who_buf), "%c%c$%s",
5050 base_type, locality, who);
5052 (void) snprintf(who_buf,
5053 sizeof (who_buf), "%c%c$",
5054 base_type, locality);
5056 (void) nvlist_add_nvlist(top_nvl, who_buf,
5061 if (!nvlist_empty(set_nvl)) {
5063 (void) snprintf(who_buf,
5064 sizeof (who_buf), "%c%c$%s",
5065 set_type, locality, who);
5067 (void) snprintf(who_buf,
5068 sizeof (who_buf), "%c%c$",
5069 set_type, locality);
5071 (void) nvlist_add_nvlist(top_nvl, who_buf,
5076 for (i = 0; i < 2; i++) {
5077 char locality = ld[i];
5082 (void) snprintf(who_buf, sizeof (who_buf),
5083 "%c%c$%s", base_type, locality, who);
5085 (void) snprintf(who_buf, sizeof (who_buf),
5086 "%c%c$", base_type, locality);
5087 (void) nvlist_add_boolean(top_nvl, who_buf);
5090 (void) snprintf(who_buf, sizeof (who_buf),
5091 "%c%c$%s", set_type, locality, who);
5093 (void) snprintf(who_buf, sizeof (who_buf),
5094 "%c%c$", set_type, locality);
5095 (void) nvlist_add_boolean(top_nvl, who_buf);
5101 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5103 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5107 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5108 opts->descend, opts->who, opts->perms, *nvlp);
5109 } else if (opts->create) {
5110 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5111 opts->descend, NULL, opts->perms, *nvlp);
5112 } else if (opts->everyone) {
5113 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5114 opts->descend, NULL, opts->perms, *nvlp);
5116 char *curr = opts->who;
5117 char *end = curr + strlen(curr);
5119 while (curr < end) {
5121 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5123 char *delim = strchr(curr, ',');
5126 struct passwd *p = NULL;
5127 struct group *g = NULL;
5135 rid = (uid_t)strtol(curr, &endch, 0);
5137 who_type = ZFS_DELEG_USER;
5146 (void) snprintf(errbuf, 256, gettext(
5147 "invalid user %s"), curr);
5148 allow_usage(un, B_TRUE, errbuf);
5150 } else if (opts->group) {
5151 who_type = ZFS_DELEG_GROUP;
5160 (void) snprintf(errbuf, 256, gettext(
5161 "invalid group %s"), curr);
5162 allow_usage(un, B_TRUE, errbuf);
5165 if (*endch != '\0') {
5172 if (*endch != '\0') {
5180 who_type = ZFS_DELEG_USER;
5182 } else if (g != NULL) {
5183 who_type = ZFS_DELEG_GROUP;
5186 (void) snprintf(errbuf, 256, gettext(
5187 "invalid user/group %s"), curr);
5188 allow_usage(un, B_TRUE, errbuf);
5192 (void) sprintf(id, "%u", rid);
5195 store_allow_perm(who_type, opts->local,
5196 opts->descend, who, opts->perms, *nvlp);
5205 print_set_creat_perms(uu_avl_t *who_avl)
5207 const char *sc_title[] = {
5208 gettext("Permission sets:\n"),
5209 gettext("Create time permissions:\n"),
5212 const char **title_ptr = sc_title;
5213 who_perm_node_t *who_node = NULL;
5214 int prev_weight = -1;
5216 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5217 who_node = uu_avl_next(who_avl, who_node)) {
5218 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5219 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5220 const char *who_name = who_node->who_perm.who_name;
5221 int weight = who_type2weight(who_type);
5222 boolean_t first = B_TRUE;
5223 deleg_perm_node_t *deleg_node;
5225 if (prev_weight != weight) {
5226 (void) printf(*title_ptr++);
5227 prev_weight = weight;
5230 if (who_name == NULL || strnlen(who_name, 1) == 0)
5231 (void) printf("\t");
5233 (void) printf("\t%s ", who_name);
5235 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5236 deleg_node = uu_avl_next(avl, deleg_node)) {
5239 deleg_node->dpn_perm.dp_name);
5242 (void) printf(",%s",
5243 deleg_node->dpn_perm.dp_name);
5246 (void) printf("\n");
5251 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5254 who_perm_node_t *who_node = NULL;
5255 boolean_t prt_title = B_TRUE;
5256 uu_avl_walk_t *walk;
5258 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5261 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5262 const char *who_name = who_node->who_perm.who_name;
5263 const char *nice_who_name = who_node->who_perm.who_ug_name;
5264 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5265 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5267 deleg_perm_node_t *deleg_node;
5268 boolean_t prt_who = B_TRUE;
5270 for (deleg_node = uu_avl_first(avl);
5272 deleg_node = uu_avl_next(avl, deleg_node)) {
5273 if (local != deleg_node->dpn_perm.dp_local ||
5274 descend != deleg_node->dpn_perm.dp_descend)
5278 const char *who = NULL;
5280 prt_title = B_FALSE;
5281 (void) printf(title);
5285 case ZFS_DELEG_USER_SETS:
5286 case ZFS_DELEG_USER:
5287 who = gettext("user");
5289 who_name = nice_who_name;
5291 case ZFS_DELEG_GROUP_SETS:
5292 case ZFS_DELEG_GROUP:
5293 who = gettext("group");
5295 who_name = nice_who_name;
5297 case ZFS_DELEG_EVERYONE_SETS:
5298 case ZFS_DELEG_EVERYONE:
5299 who = gettext("everyone");
5304 assert(who != NULL);
5308 if (who_name == NULL)
5309 (void) printf("\t%s", who);
5311 (void) printf("\t%s %s", who, who_name);
5314 (void) printf("%c%s", delim,
5315 deleg_node->dpn_perm.dp_name);
5320 (void) printf("\n");
5323 uu_avl_walk_end(walk);
5327 print_fs_perms(fs_perm_set_t *fspset)
5329 fs_perm_node_t *node = NULL;
5330 char buf[MAXNAMELEN + 32];
5331 const char *dsname = buf;
5333 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5334 node = uu_list_next(fspset->fsps_list, node)) {
5335 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5336 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5339 (void) snprintf(buf, sizeof (buf),
5340 gettext("---- Permissions on %s "),
5341 node->fspn_fsperm.fsp_name);
5342 (void) printf(dsname);
5343 left = 70 - strlen(buf);
5346 (void) printf("\n");
5348 print_set_creat_perms(sc_avl);
5349 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5350 gettext("Local permissions:\n"));
5351 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5352 gettext("Descendent permissions:\n"));
5353 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5354 gettext("Local+Descendent permissions:\n"));
5358 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5360 struct deleg_perms {
5366 set_deleg_perms(zfs_handle_t *zhp, void *data)
5368 struct deleg_perms *perms = (struct deleg_perms *)data;
5369 zfs_type_t zfs_type = zfs_get_type(zhp);
5371 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5374 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5378 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5381 nvlist_t *perm_nvl = NULL;
5382 nvlist_t *update_perm_nvl = NULL;
5385 struct allow_opts opts = { 0 };
5387 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5390 while ((c = getopt(argc, argv, optstr)) != -1) {
5393 opts.local = B_TRUE;
5396 opts.descend = B_TRUE;
5402 opts.group = B_TRUE;
5405 opts.everyone = B_TRUE;
5411 opts.create = B_TRUE;
5414 opts.recursive = B_TRUE;
5417 (void) fprintf(stderr, gettext("missing argument for "
5418 "'%c' option\n"), optopt);
5422 opts.prt_usage = B_TRUE;
5425 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5434 /* check arguments */
5435 parse_allow_args(argc, argv, un, &opts);
5437 /* try to open the dataset */
5438 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5439 ZFS_TYPE_VOLUME)) == NULL) {
5440 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5445 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5448 fs_perm_set_init(&fs_perm_set);
5449 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5450 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5455 print_fs_perms(&fs_perm_set);
5457 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5458 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5461 if (un && opts.recursive) {
5462 struct deleg_perms data = { un, update_perm_nvl };
5463 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5472 nvlist_free(perm_nvl);
5473 nvlist_free(update_perm_nvl);
5475 fs_perm_set_fini(&fs_perm_set);
5483 zfs_do_allow(int argc, char **argv)
5485 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5489 zfs_do_unallow(int argc, char **argv)
5491 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5495 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5500 boolean_t recursive = B_FALSE;
5501 const char *opts = holding ? "rt" : "r";
5505 while ((c = getopt(argc, argv, opts)) != -1) {
5511 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5520 /* check number of arguments */
5528 if (holding && tag[0] == '.') {
5529 /* tags starting with '.' are reserved for libzfs */
5530 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5534 for (i = 0; i < argc; ++i) {
5536 char parent[ZFS_MAX_DATASET_NAME_LEN];
5538 char *path = argv[i];
5540 delim = strchr(path, '@');
5541 if (delim == NULL) {
5542 (void) fprintf(stderr,
5543 gettext("'%s' is not a snapshot\n"), path);
5547 (void) strncpy(parent, path, delim - path);
5548 parent[delim - path] = '\0';
5550 zhp = zfs_open(g_zfs, parent,
5551 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5557 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5560 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5566 return (errors != 0);
5570 * zfs hold [-r] [-t] <tag> <snap> ...
5572 * -r Recursively hold
5574 * Apply a user-hold with the given tag to the list of snapshots.
5577 zfs_do_hold(int argc, char **argv)
5579 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5583 * zfs release [-r] <tag> <snap> ...
5585 * -r Recursively release
5587 * Release a user-hold with the given tag from the list of snapshots.
5590 zfs_do_release(int argc, char **argv)
5592 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5595 typedef struct holds_cbdata {
5596 boolean_t cb_recursive;
5597 const char *cb_snapname;
5599 size_t cb_max_namelen;
5600 size_t cb_max_taglen;
5603 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5604 #define DATETIME_BUF_LEN (32)
5609 print_holds(boolean_t scripted, boolean_t literal, size_t nwidth,
5610 size_t tagwidth, nvlist_t *nvl)
5613 nvpair_t *nvp = NULL;
5614 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5618 for (i = 0; i < 3; i++) {
5619 col = gettext(hdr_cols[i]);
5621 (void) printf("%-*s ", i ? tagwidth : nwidth,
5624 (void) printf("%s\n", col);
5628 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5629 char *zname = nvpair_name(nvp);
5631 nvpair_t *nvp2 = NULL;
5632 (void) nvpair_value_nvlist(nvp, &nvl2);
5633 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5634 char tsbuf[DATETIME_BUF_LEN];
5635 char *tagname = nvpair_name(nvp2);
5639 char sep = scripted ? '\t' : ' ';
5640 size_t sepnum = scripted ? 1 : 2;
5642 (void) nvpair_value_uint64(nvp2, &val);
5644 snprintf(tsbuf, DATETIME_BUF_LEN, "%llu", val);
5647 (void) localtime_r(&time, &t);
5648 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5649 gettext(STRFTIME_FMT_STR), &t);
5652 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5653 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5659 * Generic callback function to list a dataset or snapshot.
5662 holds_callback(zfs_handle_t *zhp, void *data)
5664 holds_cbdata_t *cbp = data;
5665 nvlist_t *top_nvl = *cbp->cb_nvlp;
5666 nvlist_t *nvl = NULL;
5667 nvpair_t *nvp = NULL;
5668 const char *zname = zfs_get_name(zhp);
5669 size_t znamelen = strlen(zname);
5671 if (cbp->cb_recursive && cbp->cb_snapname != NULL) {
5672 const char *snapname;
5673 char *delim = strchr(zname, '@');
5677 snapname = delim + 1;
5678 if (strcmp(cbp->cb_snapname, snapname))
5682 if (zfs_get_holds(zhp, &nvl) != 0)
5685 if (znamelen > cbp->cb_max_namelen)
5686 cbp->cb_max_namelen = znamelen;
5688 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5689 const char *tag = nvpair_name(nvp);
5690 size_t taglen = strlen(tag);
5691 if (taglen > cbp->cb_max_taglen)
5692 cbp->cb_max_taglen = taglen;
5695 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5699 * zfs holds [-Hp] [-r | -d max] <dataset|snap> ...
5701 * -H Suppress header output
5702 * -p Output literal values
5703 * -r Recursively search for holds
5704 * -d max Limit depth of recursive search
5707 zfs_do_holds(int argc, char **argv)
5712 boolean_t scripted = B_FALSE;
5713 boolean_t literal = B_FALSE;
5714 boolean_t recursive = B_FALSE;
5715 const char *opts = "d:rHp";
5718 int types = ZFS_TYPE_SNAPSHOT;
5719 holds_cbdata_t cb = { 0 };
5726 while ((c = getopt(argc, argv, opts)) != -1) {
5729 limit = parse_depth(optarg, &flags);
5742 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5749 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5750 flags |= ZFS_ITER_RECURSE;
5756 /* check number of arguments */
5760 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5763 for (i = 0; i < argc; ++i) {
5764 char *snapshot = argv[i];
5766 const char *snapname = NULL;
5768 delim = strchr(snapshot, '@');
5769 if (delim != NULL) {
5770 snapname = delim + 1;
5772 snapshot[delim - snapshot] = '\0';
5775 cb.cb_recursive = recursive;
5776 cb.cb_snapname = snapname;
5780 * 1. collect holds data, set format options
5782 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5783 holds_callback, &cb);
5789 * 2. print holds data
5791 print_holds(scripted, literal, cb.cb_max_namelen, cb.cb_max_taglen,
5794 if (nvlist_empty(nvl))
5795 (void) printf(gettext("no datasets available\n"));
5799 return (0 != errors);
5802 #define CHECK_SPINNER 30
5803 #define SPINNER_TIME 3 /* seconds */
5804 #define MOUNT_TIME 5 /* seconds */
5807 get_one_dataset(zfs_handle_t *zhp, void *data)
5809 static char *spin[] = { "-", "\\", "|", "/" };
5810 static int spinval = 0;
5811 static int spincheck = 0;
5812 static time_t last_spin_time = (time_t)0;
5813 get_all_cb_t *cbp = data;
5814 zfs_type_t type = zfs_get_type(zhp);
5816 if (cbp->cb_verbose) {
5817 if (--spincheck < 0) {
5818 time_t now = time(NULL);
5819 if (last_spin_time + SPINNER_TIME < now) {
5820 update_progress(spin[spinval++ % 4]);
5821 last_spin_time = now;
5823 spincheck = CHECK_SPINNER;
5828 * Interate over any nested datasets.
5830 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5836 * Skip any datasets whose type does not match.
5838 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5842 libzfs_add_handle(cbp, zhp);
5843 assert(cbp->cb_used <= cbp->cb_alloc);
5849 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5851 get_all_cb_t cb = { 0 };
5852 cb.cb_verbose = verbose;
5853 cb.cb_getone = get_one_dataset;
5856 set_progress_header(gettext("Reading ZFS config"));
5857 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5859 *dslist = cb.cb_handles;
5860 *count = cb.cb_used;
5863 finish_progress(gettext("done."));
5867 * Generic callback for sharing or mounting filesystems. Because the code is so
5868 * similar, we have a common function with an extra parameter to determine which
5869 * mode we are using.
5871 #define OP_SHARE 0x1
5872 #define OP_MOUNT 0x2
5875 * Share or mount a dataset.
5878 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5879 boolean_t explicit, const char *options)
5881 char mountpoint[ZFS_MAXPROPLEN];
5882 char shareopts[ZFS_MAXPROPLEN];
5883 char smbshareopts[ZFS_MAXPROPLEN];
5884 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5886 uint64_t zoned, canmount;
5887 boolean_t shared_nfs, shared_smb;
5889 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5892 * Check to make sure we can mount/share this dataset. If we
5893 * are in the global zone and the filesystem is exported to a
5894 * local zone, or if we are in a local zone and the
5895 * filesystem is not exported, then it is an error.
5897 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5899 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5903 (void) fprintf(stderr, gettext("cannot %s '%s': "
5904 "dataset is exported to a local zone\n"), cmdname,
5908 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5912 (void) fprintf(stderr, gettext("cannot %s '%s': "
5913 "permission denied\n"), cmdname,
5919 * Ignore any filesystems which don't apply to us. This
5920 * includes those with a legacy mountpoint, or those with
5921 * legacy share options.
5923 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5924 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5925 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5926 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5927 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5928 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5930 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5931 strcmp(smbshareopts, "off") == 0) {
5935 (void) fprintf(stderr, gettext("cannot share '%s': "
5936 "legacy share\n"), zfs_get_name(zhp));
5937 (void) fprintf(stderr, gettext("to "
5938 "share this filesystem set "
5939 "sharenfs property on\n"));
5944 * We cannot share or mount legacy filesystems. If the
5945 * shareopts is non-legacy but the mountpoint is legacy, we
5946 * treat it as a legacy share.
5948 if (strcmp(mountpoint, "legacy") == 0) {
5952 (void) fprintf(stderr, gettext("cannot %s '%s': "
5953 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5954 (void) fprintf(stderr, gettext("use %s(8) to "
5955 "%s this filesystem\n"), cmdname, cmdname);
5959 if (strcmp(mountpoint, "none") == 0) {
5963 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5964 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5969 * canmount explicit outcome
5970 * on no pass through
5971 * on yes pass through
5973 * off yes display error, return 1
5974 * noauto no return 0
5975 * noauto yes pass through
5977 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5978 if (canmount == ZFS_CANMOUNT_OFF) {
5982 (void) fprintf(stderr, gettext("cannot %s '%s': "
5983 "'canmount' property is set to 'off'\n"), cmdname,
5986 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5991 * If this filesystem is inconsistent and has a receive resume
5992 * token, we can not mount it.
5994 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
5995 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
5996 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
6000 (void) fprintf(stderr, gettext("cannot %s '%s': "
6001 "Contains partially-completed state from "
6002 "\"zfs receive -r\", which can be resumed with "
6003 "\"zfs send -t\"\n"),
6004 cmdname, zfs_get_name(zhp));
6009 * At this point, we have verified that the mountpoint and/or
6010 * shareopts are appropriate for auto management. If the
6011 * filesystem is already mounted or shared, return (failing
6012 * for explicit requests); otherwise mount or share the
6018 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
6019 shared_smb = zfs_is_shared_smb(zhp, NULL);
6021 if ((shared_nfs && shared_smb) ||
6022 (shared_nfs && strcmp(shareopts, "on") == 0 &&
6023 strcmp(smbshareopts, "off") == 0) ||
6024 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
6025 strcmp(shareopts, "off") == 0)) {
6029 (void) fprintf(stderr, gettext("cannot share "
6030 "'%s': filesystem already shared\n"),
6035 if (!zfs_is_mounted(zhp, NULL) &&
6036 zfs_mount(zhp, NULL, 0) != 0)
6039 if (protocol == NULL) {
6040 if (zfs_shareall(zhp) != 0)
6042 } else if (strcmp(protocol, "nfs") == 0) {
6043 if (zfs_share_nfs(zhp))
6045 } else if (strcmp(protocol, "smb") == 0) {
6046 if (zfs_share_smb(zhp))
6049 (void) fprintf(stderr, gettext("cannot share "
6050 "'%s': invalid share type '%s' "
6052 zfs_get_name(zhp), protocol);
6059 if (options == NULL)
6060 mnt.mnt_mntopts = "";
6062 mnt.mnt_mntopts = (char *)options;
6064 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6065 zfs_is_mounted(zhp, NULL)) {
6069 (void) fprintf(stderr, gettext("cannot mount "
6070 "'%s': filesystem already mounted\n"),
6075 if (zfs_mount(zhp, options, flags) != 0)
6084 * Reports progress in the form "(current/total)". Not thread-safe.
6087 report_mount_progress(int current, int total)
6089 static time_t last_progress_time = 0;
6090 time_t now = time(NULL);
6093 /* report 1..n instead of 0..n-1 */
6096 /* display header if we're here for the first time */
6098 set_progress_header(gettext("Mounting ZFS filesystems"));
6099 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6100 /* too soon to report again */
6104 last_progress_time = now;
6106 (void) sprintf(info, "(%d/%d)", current, total);
6108 if (current == total)
6109 finish_progress(info);
6111 update_progress(info);
6115 append_options(char *mntopts, char *newopts)
6117 int len = strlen(mntopts);
6119 /* original length plus new string to append plus 1 for the comma */
6120 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6121 (void) fprintf(stderr, gettext("the opts argument for "
6122 "'%c' option is too long (more than %d chars)\n"),
6123 "-o", MNT_LINE_MAX);
6128 mntopts[len++] = ',';
6130 (void) strcpy(&mntopts[len], newopts);
6134 share_mount(int op, int argc, char **argv)
6137 boolean_t verbose = B_FALSE;
6139 char *options = NULL;
6143 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6153 if (*optarg == '\0') {
6154 (void) fprintf(stderr, gettext("empty mount "
6155 "options (-o) specified\n"));
6159 if (options == NULL)
6160 options = safe_malloc(MNT_LINE_MAX + 1);
6162 /* option validation is done later */
6163 append_options(options, optarg);
6167 warnx("no overlay mounts support on FreeBSD, ignoring");
6170 (void) fprintf(stderr, gettext("missing argument for "
6171 "'%c' option\n"), optopt);
6175 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6184 /* check number of arguments */
6186 zfs_handle_t **dslist = NULL;
6187 size_t i, count = 0;
6188 char *protocol = NULL;
6190 if (op == OP_SHARE && argc > 0) {
6191 if (strcmp(argv[0], "nfs") != 0 &&
6192 strcmp(argv[0], "smb") != 0) {
6193 (void) fprintf(stderr, gettext("share type "
6194 "must be 'nfs' or 'smb'\n"));
6203 (void) fprintf(stderr, gettext("too many arguments\n"));
6207 start_progress_timer();
6208 get_all_datasets(&dslist, &count, verbose);
6213 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6215 for (i = 0; i < count; i++) {
6217 report_mount_progress(i, count);
6219 if (share_mount_one(dslist[i], op, flags, protocol,
6220 B_FALSE, options) != 0)
6222 zfs_close(dslist[i]);
6226 } else if (argc == 0) {
6227 struct mnttab entry;
6229 if ((op == OP_SHARE) || (options != NULL)) {
6230 (void) fprintf(stderr, gettext("missing filesystem "
6231 "argument (specify -a for all)\n"));
6236 * When mount is given no arguments, go through /etc/mnttab and
6237 * display any active ZFS mounts. We hide any snapshots, since
6238 * they are controlled automatically.
6240 rewind(mnttab_file);
6241 while (getmntent(mnttab_file, &entry) == 0) {
6242 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6243 strchr(entry.mnt_special, '@') != NULL)
6246 (void) printf("%-30s %s\n", entry.mnt_special,
6254 (void) fprintf(stderr,
6255 gettext("too many arguments\n"));
6259 if ((zhp = zfs_open(g_zfs, argv[0],
6260 ZFS_TYPE_FILESYSTEM)) == NULL) {
6263 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6273 * zfs mount -a [nfs]
6274 * zfs mount filesystem
6276 * Mount all filesystems, or mount the given filesystem.
6279 zfs_do_mount(int argc, char **argv)
6281 return (share_mount(OP_MOUNT, argc, argv));
6285 * zfs share -a [nfs | smb]
6286 * zfs share filesystem
6288 * Share all filesystems, or share the given filesystem.
6291 zfs_do_share(int argc, char **argv)
6293 return (share_mount(OP_SHARE, argc, argv));
6296 typedef struct unshare_unmount_node {
6297 zfs_handle_t *un_zhp;
6299 uu_avl_node_t un_avlnode;
6300 } unshare_unmount_node_t;
6304 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6306 const unshare_unmount_node_t *l = larg;
6307 const unshare_unmount_node_t *r = rarg;
6309 return (strcmp(l->un_mountp, r->un_mountp));
6313 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6314 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6315 * and unmount it appropriately.
6318 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6322 struct stat64 statbuf;
6323 struct extmnttab entry;
6324 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6328 * Search for the path in /etc/mnttab. Rather than looking for the
6329 * specific path, which can be fooled by non-standard paths (i.e. ".."
6330 * or "//"), we stat() the path and search for the corresponding
6331 * (major,minor) device pair.
6333 if (stat64(path, &statbuf) != 0) {
6334 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6335 cmdname, path, strerror(errno));
6338 path_inode = statbuf.st_ino;
6341 * Search for the given (major,minor) pair in the mount table.
6344 rewind(mnttab_file);
6345 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6346 if (entry.mnt_major == major(statbuf.st_dev) &&
6347 entry.mnt_minor == minor(statbuf.st_dev))
6354 if (statfs(path, &sfs) != 0) {
6355 (void) fprintf(stderr, "%s: %s\n", path,
6359 statfs2mnttab(&sfs, &entry);
6363 if (op == OP_SHARE) {
6364 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6365 "currently mounted\n"), cmdname, path);
6368 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6370 if ((ret = umount2(path, flags)) != 0)
6371 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6376 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6377 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6378 "filesystem\n"), cmdname, path);
6382 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6383 ZFS_TYPE_FILESYSTEM)) == NULL)
6387 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6388 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6389 cmdname, path, strerror(errno));
6391 } else if (statbuf.st_ino != path_inode) {
6392 (void) fprintf(stderr, gettext("cannot "
6393 "%s '%s': not a mountpoint\n"), cmdname, path);
6397 if (op == OP_SHARE) {
6398 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6399 char smbshare_prop[ZFS_MAXPROPLEN];
6401 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6402 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6403 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6404 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6406 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6407 strcmp(smbshare_prop, "off") == 0) {
6408 (void) fprintf(stderr, gettext("cannot unshare "
6409 "'%s': legacy share\n"), path);
6411 (void) fprintf(stderr, gettext("use "
6412 "unshare(1M) to unshare this filesystem\n"));
6414 } else if (!zfs_is_shared(zhp)) {
6415 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6416 "not currently shared\n"), path);
6418 ret = zfs_unshareall_bypath(zhp, path);
6421 char mtpt_prop[ZFS_MAXPROPLEN];
6423 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6424 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6427 ret = zfs_unmount(zhp, NULL, flags);
6428 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6429 (void) fprintf(stderr, gettext("cannot unmount "
6430 "'%s': legacy mountpoint\n"),
6432 (void) fprintf(stderr, gettext("use umount(8) "
6433 "to unmount this filesystem\n"));
6435 ret = zfs_unmountall(zhp, flags);
6446 * Generic callback for unsharing or unmounting a filesystem.
6449 unshare_unmount(int op, int argc, char **argv)
6456 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6457 char sharesmb[ZFS_MAXPROPLEN];
6460 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6469 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6480 * We could make use of zfs_for_each() to walk all datasets in
6481 * the system, but this would be very inefficient, especially
6482 * since we would have to linearly search /etc/mnttab for each
6483 * one. Instead, do one pass through /etc/mnttab looking for
6484 * zfs entries and call zfs_unmount() for each one.
6486 * Things get a little tricky if the administrator has created
6487 * mountpoints beneath other ZFS filesystems. In this case, we
6488 * have to unmount the deepest filesystems first. To accomplish
6489 * this, we place all the mountpoints in an AVL tree sorted by
6490 * the special type (dataset name), and walk the result in
6491 * reverse to make sure to get any snapshots first.
6493 struct mnttab entry;
6494 uu_avl_pool_t *pool;
6495 uu_avl_t *tree = NULL;
6496 unshare_unmount_node_t *node;
6498 uu_avl_walk_t *walk;
6501 (void) fprintf(stderr, gettext("too many arguments\n"));
6505 if (((pool = uu_avl_pool_create("unmount_pool",
6506 sizeof (unshare_unmount_node_t),
6507 offsetof(unshare_unmount_node_t, un_avlnode),
6508 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6509 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6512 rewind(mnttab_file);
6513 while (getmntent(mnttab_file, &entry) == 0) {
6515 /* ignore non-ZFS entries */
6516 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6519 /* ignore snapshots */
6520 if (strchr(entry.mnt_special, '@') != NULL)
6523 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6524 ZFS_TYPE_FILESYSTEM)) == NULL) {
6530 * Ignore datasets that are excluded/restricted by
6533 if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
6540 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6542 sizeof (nfs_mnt_prop),
6543 NULL, NULL, 0, B_FALSE) == 0);
6544 if (strcmp(nfs_mnt_prop, "off") != 0)
6546 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6548 sizeof (nfs_mnt_prop),
6549 NULL, NULL, 0, B_FALSE) == 0);
6550 if (strcmp(nfs_mnt_prop, "off") == 0)
6554 /* Ignore legacy mounts */
6555 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6557 sizeof (nfs_mnt_prop),
6558 NULL, NULL, 0, B_FALSE) == 0);
6559 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6561 /* Ignore canmount=noauto mounts */
6562 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6563 ZFS_CANMOUNT_NOAUTO)
6569 node = safe_malloc(sizeof (unshare_unmount_node_t));
6571 node->un_mountp = safe_strdup(entry.mnt_mountp);
6573 uu_avl_node_init(node, &node->un_avlnode, pool);
6575 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6576 uu_avl_insert(tree, node, idx);
6578 zfs_close(node->un_zhp);
6579 free(node->un_mountp);
6585 * Walk the AVL tree in reverse, unmounting each filesystem and
6586 * removing it from the AVL tree in the process.
6588 if ((walk = uu_avl_walk_start(tree,
6589 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6592 while ((node = uu_avl_walk_next(walk)) != NULL) {
6593 uu_avl_remove(tree, node);
6597 if (zfs_unshareall_bypath(node->un_zhp,
6598 node->un_mountp) != 0)
6603 if (zfs_unmount(node->un_zhp,
6604 node->un_mountp, flags) != 0)
6609 zfs_close(node->un_zhp);
6610 free(node->un_mountp);
6614 uu_avl_walk_end(walk);
6615 uu_avl_destroy(tree);
6616 uu_avl_pool_destroy(pool);
6621 (void) fprintf(stderr,
6622 gettext("missing filesystem argument\n"));
6624 (void) fprintf(stderr,
6625 gettext("too many arguments\n"));
6630 * We have an argument, but it may be a full path or a ZFS
6631 * filesystem. Pass full paths off to unmount_path() (shared by
6632 * manual_unmount), otherwise open the filesystem and pass to
6635 if (argv[0][0] == '/')
6636 return (unshare_unmount_path(op, argv[0],
6639 if ((zhp = zfs_open(g_zfs, argv[0],
6640 ZFS_TYPE_FILESYSTEM)) == NULL)
6643 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6644 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6645 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6646 NULL, 0, B_FALSE) == 0);
6650 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6652 sizeof (nfs_mnt_prop),
6653 NULL, NULL, 0, B_FALSE) == 0);
6654 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6655 sharesmb, sizeof (sharesmb), NULL, NULL,
6658 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6659 strcmp(sharesmb, "off") == 0) {
6660 (void) fprintf(stderr, gettext("cannot "
6661 "unshare '%s': legacy share\n"),
6664 (void) fprintf(stderr, gettext("use "
6665 "unshare(1M) to unshare this "
6669 } else if (!zfs_is_shared(zhp)) {
6670 (void) fprintf(stderr, gettext("cannot "
6671 "unshare '%s': not currently "
6672 "shared\n"), zfs_get_name(zhp));
6674 } else if (zfs_unshareall(zhp) != 0) {
6680 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6681 (void) fprintf(stderr, gettext("cannot "
6682 "unmount '%s': legacy "
6683 "mountpoint\n"), zfs_get_name(zhp));
6684 (void) fprintf(stderr, gettext("use "
6685 "umount(8) to unmount this "
6688 } else if (!zfs_is_mounted(zhp, NULL)) {
6689 (void) fprintf(stderr, gettext("cannot "
6690 "unmount '%s': not currently "
6694 } else if (zfs_unmountall(zhp, flags) != 0) {
6708 * zfs unmount filesystem
6710 * Unmount all filesystems, or a specific ZFS filesystem.
6713 zfs_do_unmount(int argc, char **argv)
6715 return (unshare_unmount(OP_MOUNT, argc, argv));
6720 * zfs unshare filesystem
6722 * Unshare all filesystems, or a specific ZFS filesystem.
6725 zfs_do_unshare(int argc, char **argv)
6727 return (unshare_unmount(OP_SHARE, argc, argv));
6731 * Attach/detach the given dataset to/from the given jail
6735 do_jail(int argc, char **argv, int attach)
6740 /* check number of arguments */
6742 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6746 (void) fprintf(stderr, gettext("too many arguments\n"));
6750 jailid = jail_getid(argv[1]);
6752 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6756 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6760 ret = (zfs_jail(zhp, jailid, attach) != 0);
6767 * zfs jail jailid filesystem
6769 * Attach the given dataset to the given jail
6773 zfs_do_jail(int argc, char **argv)
6776 return (do_jail(argc, argv, 1));
6780 * zfs unjail jailid filesystem
6782 * Detach the given dataset from the given jail
6786 zfs_do_unjail(int argc, char **argv)
6789 return (do_jail(argc, argv, 0));
6793 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6794 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6797 manual_mount(int argc, char **argv)
6800 char mountpoint[ZFS_MAXPROPLEN];
6801 char mntopts[MNT_LINE_MAX] = { '\0' };
6805 char *dataset, *path;
6808 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6811 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6814 flags |= MS_OVERLAY;
6817 flags |= MS_NOMNTTAB;
6820 (void) fprintf(stderr, gettext("missing argument for "
6821 "'%c' option\n"), optopt);
6825 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6827 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6836 /* check that we only have two arguments */
6839 (void) fprintf(stderr, gettext("missing dataset "
6842 (void) fprintf(stderr,
6843 gettext("missing mountpoint argument\n"));
6845 (void) fprintf(stderr, gettext("too many arguments\n"));
6846 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6853 /* try to open the dataset */
6854 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6857 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6858 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6860 /* check for legacy mountpoint and complain appropriately */
6862 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6863 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6864 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6865 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6870 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6871 "mounted using 'mount -t zfs'\n"), dataset);
6872 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6873 "instead.\n"), path);
6874 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6875 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6876 (void) fprintf(stderr, gettext("See zfs(8) for more "
6885 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6886 * unmounts of non-legacy filesystems, as this is the dominant administrative
6890 manual_unmount(int argc, char **argv)
6896 while ((c = getopt(argc, argv, "f")) != -1) {
6902 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6904 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6913 /* check arguments */
6916 (void) fprintf(stderr, gettext("missing path "
6919 (void) fprintf(stderr, gettext("too many arguments\n"));
6920 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6924 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6928 find_command_idx(char *command, int *idx)
6932 for (i = 0; i < NCOMMAND; i++) {
6933 if (command_table[i].name == NULL)
6936 if (strcmp(command, command_table[i].name) == 0) {
6945 zfs_do_diff(int argc, char **argv)
6949 char *tosnap = NULL;
6950 char *fromsnap = NULL;
6955 while ((c = getopt(argc, argv, "FHt")) != -1) {
6958 flags |= ZFS_DIFF_CLASSIFY;
6961 flags |= ZFS_DIFF_PARSEABLE;
6964 flags |= ZFS_DIFF_TIMESTAMP;
6967 (void) fprintf(stderr,
6968 gettext("invalid option '%c'\n"), optopt);
6977 (void) fprintf(stderr,
6978 gettext("must provide at least one snapshot name\n"));
6983 (void) fprintf(stderr, gettext("too many arguments\n"));
6988 tosnap = (argc == 2) ? argv[1] : NULL;
6991 if (*fromsnap != '@')
6992 copy = strdup(fromsnap);
6994 copy = strdup(tosnap);
6998 if ((atp = strchr(copy, '@')) != NULL)
7001 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
7007 * Ignore SIGPIPE so that the library can give us
7008 * information on any failure
7010 (void) sigignore(SIGPIPE);
7012 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
7020 * zfs bookmark <fs@snap> <fs#bmark>
7022 * Creates a bookmark with the given name from the given snapshot.
7025 zfs_do_bookmark(int argc, char **argv)
7027 char snapname[ZFS_MAX_DATASET_NAME_LEN];
7034 while ((c = getopt(argc, argv, "")) != -1) {
7037 (void) fprintf(stderr,
7038 gettext("invalid option '%c'\n"), optopt);
7046 /* check number of arguments */
7048 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
7052 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
7056 if (strchr(argv[1], '#') == NULL) {
7057 (void) fprintf(stderr,
7058 gettext("invalid bookmark name '%s' -- "
7059 "must contain a '#'\n"), argv[1]);
7063 if (argv[0][0] == '@') {
7065 * Snapshot name begins with @.
7066 * Default to same fs as bookmark.
7068 (void) strncpy(snapname, argv[1], sizeof (snapname));
7069 *strchr(snapname, '#') = '\0';
7070 (void) strlcat(snapname, argv[0], sizeof (snapname));
7072 (void) strncpy(snapname, argv[0], sizeof (snapname));
7074 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
7080 nvl = fnvlist_alloc();
7081 fnvlist_add_string(nvl, argv[1], snapname);
7082 ret = lzc_bookmark(nvl, NULL);
7086 const char *err_msg;
7089 (void) snprintf(errbuf, sizeof (errbuf),
7090 dgettext(TEXT_DOMAIN,
7091 "cannot create bookmark '%s'"), argv[1]);
7095 err_msg = "bookmark is in a different pool";
7098 err_msg = "bookmark exists";
7101 err_msg = "invalid argument";
7104 err_msg = "bookmark feature not enabled";
7107 err_msg = "out of space";
7110 err_msg = "unknown error";
7113 (void) fprintf(stderr, "%s: %s\n", errbuf,
7114 dgettext(TEXT_DOMAIN, err_msg));
7125 zfs_do_channel_program(int argc, char **argv)
7129 char *progbuf, *filename, *poolname;
7130 size_t progsize, progread;
7132 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
7133 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
7134 zpool_handle_t *zhp;
7138 (c = getopt(argc, argv, "t:(instr-limit)m:(memory-limit)"))) {
7146 arg = strtoull(optarg, &endp, 0);
7147 if (errno != 0 || *endp != '\0') {
7148 (void) fprintf(stderr, gettext(
7150 "'%s': expected integer\n"), optarg);
7155 if (arg > ZCP_MAX_INSTRLIMIT || arg == 0) {
7156 (void) fprintf(stderr, gettext(
7157 "Invalid instruction limit: "
7164 ASSERT3U(c, ==, 'm');
7165 if (arg > ZCP_MAX_MEMLIMIT || arg == 0) {
7166 (void) fprintf(stderr, gettext(
7167 "Invalid memory limit: "
7177 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7187 (void) fprintf(stderr,
7188 gettext("invalid number of arguments\n"));
7194 if (strcmp(filename, "-") == 0) {
7196 filename = "standard input";
7197 } else if ((fd = open(filename, O_RDONLY)) < 0) {
7198 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
7199 filename, strerror(errno));
7203 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7204 (void) fprintf(stderr, gettext("cannot open pool '%s'"),
7211 * Read in the channel program, expanding the program buffer as
7216 progbuf = safe_malloc(progsize);
7218 ret = read(fd, progbuf + progread, progsize - progread);
7220 if (progread == progsize && ret > 0) {
7222 progbuf = safe_realloc(progbuf, progsize);
7230 (void) fprintf(stderr,
7231 gettext("cannot read '%s': %s\n"),
7232 filename, strerror(errno));
7235 progbuf[progread] = '\0';
7238 * Any remaining arguments are passed as arguments to the lua script as
7241 * "argv" -> [ "arg 1", ... "arg n" ],
7244 nvlist_t *argnvl = fnvlist_alloc();
7245 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);
7247 ret = lzc_channel_program(poolname, progbuf, instrlimit, memlimit,
7252 * On error, report the error message handed back by lua if one
7253 * exists. Otherwise, generate an appropriate error message,
7254 * falling back on strerror() for an unexpected return code.
7256 char *errstring = NULL;
7257 if (nvlist_exists(outnvl, ZCP_RET_ERROR)) {
7258 (void) nvlist_lookup_string(outnvl,
7259 ZCP_RET_ERROR, &errstring);
7260 if (errstring == NULL)
7261 errstring = strerror(ret);
7266 "Invalid instruction or memory limit.";
7269 errstring = "Return value too large.";
7272 errstring = "Memory limit exhausted.";
7279 errstring = "Timed out.";
7282 errstring = "Permission denied. Channel "
7283 "programs must be run as root.";
7286 errstring = strerror(ret);
7289 (void) fprintf(stderr,
7290 gettext("Channel program execution failed:\n%s\n"),
7293 (void) printf("Channel program fully executed ");
7294 if (nvlist_empty(outnvl)) {
7295 (void) printf("with no return value.\n");
7297 (void) printf("with return value:\n");
7298 dump_nvlist(outnvl, 4);
7303 fnvlist_free(outnvl);
7304 fnvlist_free(argnvl);
7313 main(int argc, char **argv)
7320 (void) setlocale(LC_ALL, "");
7321 (void) textdomain(TEXT_DOMAIN);
7325 if ((g_zfs = libzfs_init()) == NULL) {
7326 (void) fprintf(stderr, gettext("internal error: failed to "
7327 "initialize ZFS library\n"));
7331 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7333 libzfs_print_on_error(g_zfs, B_TRUE);
7335 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
7336 (void) fprintf(stderr, gettext("internal error: unable to "
7337 "open %s\n"), MNTTAB);
7342 * This command also doubles as the /etc/fs mount and unmount program.
7343 * Determine if we should take this behavior based on argv[0].
7345 progname = basename(argv[0]);
7346 if (strcmp(progname, "mount") == 0) {
7347 ret = manual_mount(argc, argv);
7348 } else if (strcmp(progname, "umount") == 0) {
7349 ret = manual_unmount(argc, argv);
7352 * Make sure the user has specified some command.
7355 (void) fprintf(stderr, gettext("missing command\n"));
7362 * The 'umount' command is an alias for 'unmount'
7364 if (strcmp(cmdname, "umount") == 0)
7365 cmdname = "unmount";
7368 * The 'recv' command is an alias for 'receive'
7370 if (strcmp(cmdname, "recv") == 0)
7371 cmdname = "receive";
7374 * The 'snap' command is an alias for 'snapshot'
7376 if (strcmp(cmdname, "snap") == 0)
7377 cmdname = "snapshot";
7382 if (strcmp(cmdname, "-?") == 0)
7386 * Run the appropriate command.
7388 libzfs_mnttab_cache(g_zfs, B_TRUE);
7389 if (find_command_idx(cmdname, &i) == 0) {
7390 current_command = &command_table[i];
7391 ret = command_table[i].func(argc - 1, argv + 1);
7392 } else if (strchr(cmdname, '=') != NULL) {
7393 verify(find_command_idx("set", &i) == 0);
7394 current_command = &command_table[i];
7395 ret = command_table[i].func(argc, argv);
7397 (void) fprintf(stderr, gettext("unrecognized "
7398 "command '%s'\n"), cmdname);
7401 libzfs_mnttab_cache(g_zfs, B_FALSE);
7404 (void) fclose(mnttab_file);
7406 if (ret == 0 && log_history)
7407 (void) zpool_log_history(g_zfs, history_str);
7412 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7413 * for the purposes of running ::findleaks.
7415 if (getenv("ZFS_ABORT") != NULL) {
7416 (void) printf("dumping core by request\n");