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, 2015 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) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
37 #include <libnvpair.h>
50 #include <sys/mkdev.h>
51 #include <sys/mntent.h>
52 #include <sys/mnttab.h>
53 #include <sys/mount.h>
55 #include <sys/fs/zfs.h>
56 #include <sys/types.h>
60 #include <libzfs_core.h>
62 #include <zfs_deleg.h>
66 #include <directory.h>
67 #endif /* HAVE_IDMAP */
71 #include "zfs_comutil.h"
72 #include "libzfs_impl.h"
74 libzfs_handle_t *g_zfs;
76 static FILE *mnttab_file;
77 static char history_str[HIS_MAX_RECORD_LEN];
78 static boolean_t log_history = B_TRUE;
80 static int zfs_do_clone(int argc, char **argv);
81 static int zfs_do_create(int argc, char **argv);
82 static int zfs_do_destroy(int argc, char **argv);
83 static int zfs_do_get(int argc, char **argv);
84 static int zfs_do_inherit(int argc, char **argv);
85 static int zfs_do_list(int argc, char **argv);
86 static int zfs_do_mount(int argc, char **argv);
87 static int zfs_do_rename(int argc, char **argv);
88 static int zfs_do_rollback(int argc, char **argv);
89 static int zfs_do_set(int argc, char **argv);
90 static int zfs_do_upgrade(int argc, char **argv);
91 static int zfs_do_snapshot(int argc, char **argv);
92 static int zfs_do_unmount(int argc, char **argv);
93 static int zfs_do_share(int argc, char **argv);
94 static int zfs_do_unshare(int argc, char **argv);
95 static int zfs_do_send(int argc, char **argv);
96 static int zfs_do_receive(int argc, char **argv);
97 static int zfs_do_promote(int argc, char **argv);
98 static int zfs_do_userspace(int argc, char **argv);
99 static int zfs_do_allow(int argc, char **argv);
100 static int zfs_do_unallow(int argc, char **argv);
101 static int zfs_do_hold(int argc, char **argv);
102 static int zfs_do_holds(int argc, char **argv);
103 static int zfs_do_release(int argc, char **argv);
104 static int zfs_do_diff(int argc, char **argv);
105 static int zfs_do_bookmark(int argc, char **argv);
108 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
113 _umem_debug_init(void)
115 return ("default,verbose"); /* $UMEM_DEBUG setting */
119 _umem_logging_init(void)
121 return ("fail,contents"); /* $UMEM_LOGGING setting */
155 typedef struct zfs_command {
157 int (*func)(int argc, char **argv);
162 * Master command table. Each ZFS command has a name, associated function, and
163 * usage message. The usage messages need to be internationalized, so we have
164 * to have a function to return the usage message based on a command index.
166 * These commands are organized according to how they are displayed in the usage
167 * message. An empty command (one with a NULL name) indicates an empty line in
168 * the generic usage message.
170 static zfs_command_t command_table[] = {
171 { "create", zfs_do_create, HELP_CREATE },
172 { "destroy", zfs_do_destroy, HELP_DESTROY },
174 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
175 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
176 { "clone", zfs_do_clone, HELP_CLONE },
177 { "promote", zfs_do_promote, HELP_PROMOTE },
178 { "rename", zfs_do_rename, HELP_RENAME },
179 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
181 { "list", zfs_do_list, HELP_LIST },
183 { "set", zfs_do_set, HELP_SET },
184 { "get", zfs_do_get, HELP_GET },
185 { "inherit", zfs_do_inherit, HELP_INHERIT },
186 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
187 { "userspace", zfs_do_userspace, HELP_USERSPACE },
188 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
190 { "mount", zfs_do_mount, HELP_MOUNT },
191 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
192 { "share", zfs_do_share, HELP_SHARE },
193 { "unshare", zfs_do_unshare, HELP_UNSHARE },
195 { "send", zfs_do_send, HELP_SEND },
196 { "receive", zfs_do_receive, HELP_RECEIVE },
198 { "allow", zfs_do_allow, HELP_ALLOW },
200 { "unallow", zfs_do_unallow, HELP_UNALLOW },
202 { "hold", zfs_do_hold, HELP_HOLD },
203 { "holds", zfs_do_holds, HELP_HOLDS },
204 { "release", zfs_do_release, HELP_RELEASE },
205 { "diff", zfs_do_diff, HELP_DIFF },
208 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
210 zfs_command_t *current_command;
213 get_usage(zfs_help_t idx)
217 return (gettext("\tclone [-p] [-o property=value] ... "
218 "<snapshot> <filesystem|volume>\n"));
220 return (gettext("\tcreate [-p] [-o property=value] ... "
222 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
223 "-V <size> <volume>\n"));
225 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
226 "\tdestroy [-dnpRrv] "
227 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
228 "\tdestroy <filesystem|volume>#<bookmark>\n"));
230 return (gettext("\tget [-rHp] [-d max] "
231 "[-o \"all\" | field[,...]]\n"
232 "\t [-t type[,...]] [-s source[,...]]\n"
233 "\t <\"all\" | property[,...]> "
234 "[filesystem|volume|snapshot] ...\n"));
236 return (gettext("\tinherit [-rS] <property> "
237 "<filesystem|volume|snapshot> ...\n"));
239 return (gettext("\tupgrade [-v]\n"
240 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
242 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
243 "[-s property]...\n\t [-S property]... [-t type[,...]] "
244 "[filesystem|volume|snapshot] ...\n"));
246 return (gettext("\tmount\n"
247 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
249 return (gettext("\tpromote <clone-filesystem>\n"));
251 return (gettext("\treceive [-vnsFu] <filesystem|volume|"
253 "\treceive [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
255 "\treceive -A <filesystem|volume>\n"));
257 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
258 "<filesystem|volume|snapshot>\n"
259 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
260 "\trename -r <snapshot> <snapshot>\n"));
262 return (gettext("\trollback [-rRf] <snapshot>\n"));
264 return (gettext("\tsend [-DnPpRvLec] [-[iI] snapshot] "
266 "\tsend [-Le] [-i snapshot|bookmark] "
267 "<filesystem|volume|snapshot>\n"
268 "\tsend [-nvPe] -t <receive_resume_token>\n"));
270 return (gettext("\tset <property=value> ... "
271 "<filesystem|volume|snapshot> ...\n"));
273 return (gettext("\tshare <-a [nfs|smb] | filesystem>\n"));
275 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
276 "<filesystem|volume>@<snap> ...\n"));
278 return (gettext("\tunmount [-f] "
279 "<-a | filesystem|mountpoint>\n"));
281 return (gettext("\tunshare "
282 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
284 return (gettext("\tallow <filesystem|volume>\n"
286 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
287 "\t <filesystem|volume>\n"
288 "\tallow [-ld] -e <perm|@setname>[,...] "
289 "<filesystem|volume>\n"
290 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
291 "\tallow -s @setname <perm|@setname>[,...] "
292 "<filesystem|volume>\n"));
294 return (gettext("\tunallow [-rldug] "
295 "<\"everyone\"|user|group>[,...]\n"
296 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
297 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
298 "<filesystem|volume>\n"
299 "\tunallow [-r] -c [<perm|@setname>[,...]] "
300 "<filesystem|volume>\n"
301 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
302 "<filesystem|volume>\n"));
304 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
306 "\t [-S field] ... [-t type[,...]] "
307 "<filesystem|snapshot>\n"));
308 case HELP_GROUPSPACE:
309 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
311 "\t [-S field] ... [-t type[,...]] "
312 "<filesystem|snapshot>\n"));
314 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
316 return (gettext("\tholds [-r] <snapshot> ...\n"));
318 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
320 return (gettext("\tdiff [-FHt] <snapshot> "
321 "[snapshot|filesystem]\n"));
323 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
333 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
338 * Utility function to guarantee malloc() success.
342 safe_malloc(size_t size)
346 if ((data = calloc(1, size)) == NULL)
353 safe_strdup(char *str)
355 char *dupstr = strdup(str);
364 * Callback routine that will print out information for each of
368 usage_prop_cb(int prop, void *cb)
372 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
374 if (zfs_prop_readonly(prop))
375 (void) fprintf(fp, " NO ");
377 (void) fprintf(fp, "YES ");
379 if (zfs_prop_inheritable(prop))
380 (void) fprintf(fp, " YES ");
382 (void) fprintf(fp, " NO ");
384 if (zfs_prop_values(prop) == NULL)
385 (void) fprintf(fp, "-\n");
387 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
393 * Display usage message. If we're inside a command, display only the usage for
394 * that command. Otherwise, iterate over the entire command table and display
395 * a complete usage message.
398 usage(boolean_t requested)
401 boolean_t show_properties = B_FALSE;
402 FILE *fp = requested ? stdout : stderr;
404 if (current_command == NULL) {
406 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
408 gettext("where 'command' is one of the following:\n\n"));
410 for (i = 0; i < NCOMMAND; i++) {
411 if (command_table[i].name == NULL)
412 (void) fprintf(fp, "\n");
414 (void) fprintf(fp, "%s",
415 get_usage(command_table[i].usage));
418 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
419 "pool/[dataset/]*dataset[@name]\n"));
421 (void) fprintf(fp, gettext("usage:\n"));
422 (void) fprintf(fp, "%s", get_usage(current_command->usage));
425 if (current_command != NULL &&
426 (strcmp(current_command->name, "set") == 0 ||
427 strcmp(current_command->name, "get") == 0 ||
428 strcmp(current_command->name, "inherit") == 0 ||
429 strcmp(current_command->name, "list") == 0))
430 show_properties = B_TRUE;
432 if (show_properties) {
434 gettext("\nThe following properties are supported:\n"));
436 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
437 "PROPERTY", "EDIT", "INHERIT", "VALUES");
439 /* Iterate over all properties */
440 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
443 (void) fprintf(fp, "\t%-15s ", "userused@...");
444 (void) fprintf(fp, " NO NO <size>\n");
445 (void) fprintf(fp, "\t%-15s ", "groupused@...");
446 (void) fprintf(fp, " NO NO <size>\n");
447 (void) fprintf(fp, "\t%-15s ", "userquota@...");
448 (void) fprintf(fp, "YES NO <size> | none\n");
449 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
450 (void) fprintf(fp, "YES NO <size> | none\n");
451 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
452 (void) fprintf(fp, " NO NO <size>\n");
454 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
455 "with standard units such as K, M, G, etc.\n"));
456 (void) fprintf(fp, gettext("\nUser-defined properties can "
457 "be specified by using a name containing a colon (:).\n"));
458 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
459 "properties must be appended with\n"
460 "a user or group specifier of one of these forms:\n"
461 " POSIX name (eg: \"matt\")\n"
462 " POSIX id (eg: \"126829\")\n"
463 " SMB name@domain (eg: \"matt@sun\")\n"
464 " SMB SID (eg: \"S-1-234-567-89\")\n"));
467 gettext("\nFor the property list, run: %s\n"),
470 gettext("\nFor the delegated permission list, run: %s\n"),
471 "zfs allow|unallow");
475 * See comments at end of main().
477 if (getenv("ZFS_ABORT") != NULL) {
478 (void) printf("dumping core by request\n");
482 exit(requested ? 0 : 2);
486 * Take a property=value argument string and add it to the given nvlist.
487 * Modifies the argument inplace.
490 parseprop(nvlist_t *props, char *propname)
492 char *propval, *strval;
494 if ((propval = strchr(propname, '=')) == NULL) {
495 (void) fprintf(stderr, gettext("missing "
496 "'=' for property=value argument\n"));
501 if (nvlist_lookup_string(props, propname, &strval) == 0) {
502 (void) fprintf(stderr, gettext("property '%s' "
503 "specified multiple times\n"), propname);
506 if (nvlist_add_string(props, propname, propval) != 0)
512 parse_depth(char *opt, int *flags)
517 depth = (int)strtol(opt, &tmp, 0);
519 (void) fprintf(stderr,
520 gettext("%s is not an integer\n"), optarg);
524 (void) fprintf(stderr,
525 gettext("Depth can not be negative.\n"));
528 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
532 #define PROGRESS_DELAY 2 /* seconds */
534 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";
535 static time_t pt_begin;
536 static char *pt_header = NULL;
537 static boolean_t pt_shown;
540 start_progress_timer(void)
542 pt_begin = time(NULL) + PROGRESS_DELAY;
547 set_progress_header(char *header)
549 assert(pt_header == NULL);
550 pt_header = safe_strdup(header);
552 (void) printf("%s: ", header);
553 (void) fflush(stdout);
558 update_progress(char *update)
560 if (!pt_shown && time(NULL) > pt_begin) {
561 int len = strlen(update);
563 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
565 (void) fflush(stdout);
567 } else if (pt_shown) {
568 int len = strlen(update);
570 (void) printf("%s%*.*s", update, len, len, pt_reverse);
571 (void) fflush(stdout);
576 finish_progress(char *done)
579 (void) printf("%s\n", done);
580 (void) fflush(stdout);
587 zfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type)
589 zfs_handle_t *zhp = NULL;
592 zhp = zfs_open(hdl, dataset, type);
597 * Volumes may neither be mounted or shared. Potentially in the
598 * future filesystems detected on these volumes could be mounted.
600 if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
606 * Mount and/or share the new filesystem as appropriate. We provide a
607 * verbose error message to let the user know that their filesystem was
608 * in fact created, even if we failed to mount or share it.
610 * If the user doesn't want the dataset automatically mounted, then
611 * skip the mount/share step
613 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type, B_FALSE) &&
614 zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON) {
615 if (geteuid() != 0) {
616 (void) fprintf(stderr, gettext("filesystem "
617 "successfully created, but it may only be "
618 "mounted by root\n"));
620 } else if (zfs_mount(zhp, NULL, 0) != 0) {
621 (void) fprintf(stderr, gettext("filesystem "
622 "successfully created, but not mounted\n"));
624 } else if (zfs_share(zhp) != 0) {
625 (void) fprintf(stderr, gettext("filesystem "
626 "successfully created, but not shared\n"));
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) {
671 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
680 /* check number of arguments */
682 (void) fprintf(stderr, gettext("missing source dataset "
687 (void) fprintf(stderr, gettext("missing target dataset "
692 (void) fprintf(stderr, gettext("too many arguments\n"));
696 /* open the source dataset */
697 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) {
702 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
705 * Now create the ancestors of the target dataset. If the
706 * target already exists and '-p' option was used we should not
709 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
715 if (zfs_create_ancestors(g_zfs, argv[1]) != 0) {
723 ret = zfs_clone(zhp, argv[1], props);
725 /* create the mountpoint if necessary */
728 (void) zpool_log_history(g_zfs, history_str);
729 log_history = B_FALSE;
732 ret = zfs_mount_and_share(g_zfs, argv[1], ZFS_TYPE_DATASET);
749 * zfs create [-p] [-o prop=value] ... fs
750 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
752 * Create a new dataset. This command can be used to create filesystems
753 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
754 * For volumes, the user must specify a size to be used.
756 * The '-s' flag applies only to volumes, and indicates that we should not try
757 * to set the reservation for this volume. By default we set a reservation
758 * equal to the size for any volume. For pools with SPA_VERSION >=
759 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
761 * The '-p' flag creates all the non-existing ancestors of the target first.
764 zfs_do_create(int argc, char **argv)
766 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
767 uint64_t volsize = 0;
769 boolean_t noreserve = B_FALSE;
770 boolean_t bflag = B_FALSE;
771 boolean_t parents = B_FALSE;
776 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
780 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
783 type = ZFS_TYPE_VOLUME;
784 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
785 (void) fprintf(stderr, gettext("bad volume "
786 "size '%s': %s\n"), optarg,
787 libzfs_error_description(g_zfs));
791 if (nvlist_add_uint64(props,
792 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
801 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
802 (void) fprintf(stderr, gettext("bad volume "
803 "block size '%s': %s\n"), optarg,
804 libzfs_error_description(g_zfs));
808 if (nvlist_add_uint64(props,
809 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
814 if (parseprop(props, optarg) != 0)
821 (void) fprintf(stderr, gettext("missing size "
825 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
831 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
832 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
833 "used when creating a volume\n"));
840 /* check number of arguments */
842 (void) fprintf(stderr, gettext("missing %s argument\n"),
843 zfs_type_to_name(type));
847 (void) fprintf(stderr, gettext("too many arguments\n"));
851 if (type == ZFS_TYPE_VOLUME && !noreserve) {
852 zpool_handle_t *zpool_handle;
853 nvlist_t *real_props = NULL;
854 uint64_t spa_version;
856 zfs_prop_t resv_prop;
860 if ((p = strchr(argv[0], '/')))
862 zpool_handle = zpool_open(g_zfs, argv[0]);
865 if (zpool_handle == NULL)
867 spa_version = zpool_get_prop_int(zpool_handle,
868 ZPOOL_PROP_VERSION, NULL);
869 if (spa_version >= SPA_VERSION_REFRESERVATION)
870 resv_prop = ZFS_PROP_REFRESERVATION;
872 resv_prop = ZFS_PROP_RESERVATION;
874 (void) snprintf(msg, sizeof (msg),
875 gettext("cannot create '%s'"), argv[0]);
876 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
877 props, 0, NULL, zpool_handle, msg)) == NULL) {
878 zpool_close(zpool_handle);
881 zpool_close(zpool_handle);
883 volsize = zvol_volsize_to_reservation(volsize, real_props);
884 nvlist_free(real_props);
886 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
888 if (nvlist_add_uint64(props,
889 zfs_prop_to_name(resv_prop), volsize) != 0) {
896 if (parents && zfs_name_valid(argv[0], type)) {
898 * Now create the ancestors of target dataset. If the target
899 * already exists and '-p' option was used we should not
902 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
906 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
911 if (zfs_create(g_zfs, argv[0], type, props) != 0)
915 (void) zpool_log_history(g_zfs, history_str);
916 log_history = B_FALSE;
919 ret = zfs_mount_and_share(g_zfs, argv[0], ZFS_TYPE_DATASET);
930 * zfs destroy [-rRf] <fs, vol>
931 * zfs destroy [-rRd] <snap>
933 * -r Recursively destroy all children
934 * -R Recursively destroy all dependents, including clones
935 * -f Force unmounting of any dependents
936 * -d If we can't destroy now, mark for deferred destruction
938 * Destroys the given dataset. By default, it will unmount any filesystems,
939 * and refuse to destroy a dataset that has any dependents. A dependent can
940 * either be a child, or a clone of a child.
942 typedef struct destroy_cbdata {
945 boolean_t cb_recurse;
947 boolean_t cb_doclones;
948 zfs_handle_t *cb_target;
949 boolean_t cb_defer_destroy;
950 boolean_t cb_verbose;
951 boolean_t cb_parsable;
954 nvlist_t *cb_batchedsnaps;
956 /* first snap in contiguous run */
958 /* previous snap in contiguous run */
966 * Check for any dependents based on the '-r' or '-R' flags.
969 destroy_check_dependent(zfs_handle_t *zhp, void *data)
971 destroy_cbdata_t *cbp = data;
972 const char *tname = zfs_get_name(cbp->cb_target);
973 const char *name = zfs_get_name(zhp);
975 if (strncmp(tname, name, strlen(tname)) == 0 &&
976 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
978 * This is a direct descendant, not a clone somewhere else in
985 (void) fprintf(stderr, gettext("cannot destroy '%s': "
986 "%s has children\n"),
987 zfs_get_name(cbp->cb_target),
988 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
989 (void) fprintf(stderr, gettext("use '-r' to destroy "
990 "the following datasets:\n"));
991 cbp->cb_first = B_FALSE;
992 cbp->cb_error = B_TRUE;
995 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
998 * This is a clone. We only want to report this if the '-r'
999 * wasn't specified, or the target is a snapshot.
1001 if (!cbp->cb_recurse &&
1002 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1005 if (cbp->cb_first) {
1006 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1007 "%s has dependent clones\n"),
1008 zfs_get_name(cbp->cb_target),
1009 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1010 (void) fprintf(stderr, gettext("use '-R' to destroy "
1011 "the following datasets:\n"));
1012 cbp->cb_first = B_FALSE;
1013 cbp->cb_error = B_TRUE;
1014 cbp->cb_dryrun = B_TRUE;
1017 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1026 destroy_callback(zfs_handle_t *zhp, void *data)
1028 destroy_cbdata_t *cb = data;
1029 const char *name = zfs_get_name(zhp);
1031 if (cb->cb_verbose) {
1032 if (cb->cb_parsable) {
1033 (void) printf("destroy\t%s\n", name);
1034 } else if (cb->cb_dryrun) {
1035 (void) printf(gettext("would destroy %s\n"),
1038 (void) printf(gettext("will destroy %s\n"),
1044 * Ignore pools (which we've already flagged as an error before getting
1047 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1048 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1052 if (cb->cb_dryrun) {
1058 * We batch up all contiguous snapshots (even of different
1059 * filesystems) and destroy them with one ioctl. We can't
1060 * simply do all snap deletions and then all fs deletions,
1061 * because we must delete a clone before its origin.
1063 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1064 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1066 int error = zfs_destroy_snaps_nvl(g_zfs,
1067 cb->cb_batchedsnaps, B_FALSE);
1068 fnvlist_free(cb->cb_batchedsnaps);
1069 cb->cb_batchedsnaps = fnvlist_alloc();
1072 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1073 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1084 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1086 destroy_cbdata_t *cb = arg;
1087 const char *name = zfs_get_name(zhp);
1090 if (nvlist_exists(cb->cb_nvl, name)) {
1091 if (cb->cb_firstsnap == NULL)
1092 cb->cb_firstsnap = strdup(name);
1093 if (cb->cb_prevsnap != NULL)
1094 free(cb->cb_prevsnap);
1095 /* this snap continues the current range */
1096 cb->cb_prevsnap = strdup(name);
1097 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1099 if (cb->cb_verbose) {
1100 if (cb->cb_parsable) {
1101 (void) printf("destroy\t%s\n", name);
1102 } else if (cb->cb_dryrun) {
1103 (void) printf(gettext("would destroy %s\n"),
1106 (void) printf(gettext("will destroy %s\n"),
1110 } else if (cb->cb_firstsnap != NULL) {
1111 /* end of this range */
1113 err = lzc_snaprange_space(cb->cb_firstsnap,
1114 cb->cb_prevsnap, &used);
1115 cb->cb_snapused += used;
1116 free(cb->cb_firstsnap);
1117 cb->cb_firstsnap = NULL;
1118 free(cb->cb_prevsnap);
1119 cb->cb_prevsnap = NULL;
1126 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1129 assert(cb->cb_firstsnap == NULL);
1130 assert(cb->cb_prevsnap == NULL);
1131 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1132 if (cb->cb_firstsnap != NULL) {
1135 err = lzc_snaprange_space(cb->cb_firstsnap,
1136 cb->cb_prevsnap, &used);
1138 cb->cb_snapused += used;
1139 free(cb->cb_firstsnap);
1140 cb->cb_firstsnap = NULL;
1141 free(cb->cb_prevsnap);
1142 cb->cb_prevsnap = NULL;
1148 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1150 destroy_cbdata_t *cb = arg;
1153 /* Check for clones. */
1154 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1155 cb->cb_target = zhp;
1156 cb->cb_first = B_TRUE;
1157 err = zfs_iter_dependents(zhp, B_TRUE,
1158 destroy_check_dependent, cb);
1162 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1170 gather_snapshots(zfs_handle_t *zhp, void *arg)
1172 destroy_cbdata_t *cb = arg;
1175 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1181 if (cb->cb_verbose) {
1182 err = destroy_print_snapshots(zhp, cb);
1188 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1196 destroy_clones(destroy_cbdata_t *cb)
1199 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1201 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1202 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1205 boolean_t defer = cb->cb_defer_destroy;
1209 * We can't defer destroy non-snapshots, so set it to
1210 * false while destroying the clones.
1212 cb->cb_defer_destroy = B_FALSE;
1213 err = zfs_iter_dependents(zhp, B_FALSE,
1214 destroy_callback, cb);
1215 cb->cb_defer_destroy = defer;
1225 zfs_do_destroy(int argc, char **argv)
1227 destroy_cbdata_t cb = { 0 };
1231 zfs_handle_t *zhp = NULL;
1233 zfs_type_t type = ZFS_TYPE_DATASET;
1236 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1239 cb.cb_verbose = B_TRUE;
1242 cb.cb_verbose = B_TRUE;
1243 cb.cb_parsable = B_TRUE;
1246 cb.cb_dryrun = B_TRUE;
1249 cb.cb_defer_destroy = B_TRUE;
1250 type = ZFS_TYPE_SNAPSHOT;
1253 cb.cb_force = B_TRUE;
1256 cb.cb_recurse = B_TRUE;
1259 cb.cb_recurse = B_TRUE;
1260 cb.cb_doclones = B_TRUE;
1264 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1273 /* check number of arguments */
1275 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1279 (void) fprintf(stderr, gettext("too many arguments\n"));
1283 at = strchr(argv[0], '@');
1284 pound = strchr(argv[0], '#');
1287 /* Build the list of snaps to destroy in cb_nvl. */
1288 cb.cb_nvl = fnvlist_alloc();
1291 zhp = zfs_open(g_zfs, argv[0],
1292 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1294 nvlist_free(cb.cb_nvl);
1298 cb.cb_snapspec = at + 1;
1299 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1305 if (nvlist_empty(cb.cb_nvl)) {
1306 (void) fprintf(stderr, gettext("could not find any "
1307 "snapshots to destroy; check snapshot names.\n"));
1312 if (cb.cb_verbose) {
1314 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1315 if (cb.cb_parsable) {
1316 (void) printf("reclaim\t%llu\n",
1317 (u_longlong_t)cb.cb_snapused);
1318 } else if (cb.cb_dryrun) {
1319 (void) printf(gettext("would reclaim %s\n"),
1322 (void) printf(gettext("will reclaim %s\n"),
1327 if (!cb.cb_dryrun) {
1328 if (cb.cb_doclones) {
1329 cb.cb_batchedsnaps = fnvlist_alloc();
1330 err = destroy_clones(&cb);
1332 err = zfs_destroy_snaps_nvl(g_zfs,
1333 cb.cb_batchedsnaps, B_FALSE);
1341 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1342 cb.cb_defer_destroy);
1348 } else if (pound != NULL) {
1353 (void) fprintf(stderr,
1354 "dryrun is not supported with bookmark\n");
1358 if (cb.cb_defer_destroy) {
1359 (void) fprintf(stderr,
1360 "defer destroy is not supported with bookmark\n");
1364 if (cb.cb_recurse) {
1365 (void) fprintf(stderr,
1366 "recursive is not supported with bookmark\n");
1370 if (!zfs_bookmark_exists(argv[0])) {
1371 (void) fprintf(stderr, gettext("bookmark '%s' "
1372 "does not exist.\n"), argv[0]);
1376 nvl = fnvlist_alloc();
1377 fnvlist_add_boolean(nvl, argv[0]);
1379 err = lzc_destroy_bookmarks(nvl, NULL);
1381 (void) zfs_standard_error(g_zfs, err,
1382 "cannot destroy bookmark");
1389 /* Open the given dataset */
1390 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1396 * Perform an explicit check for pools before going any further.
1398 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1399 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1400 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1401 "operation does not apply to pools\n"),
1403 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1404 "%s' to destroy all datasets in the pool\n"),
1406 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1407 "to destroy the pool itself\n"), zfs_get_name(zhp));
1413 * Check for any dependents and/or clones.
1415 cb.cb_first = B_TRUE;
1416 if (!cb.cb_doclones &&
1417 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1428 cb.cb_batchedsnaps = fnvlist_alloc();
1429 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1436 * Do the real thing. The callback will close the
1437 * handle regardless of whether it succeeds or not.
1439 err = destroy_callback(zhp, &cb);
1442 err = zfs_destroy_snaps_nvl(g_zfs,
1443 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1450 fnvlist_free(cb.cb_batchedsnaps);
1451 fnvlist_free(cb.cb_nvl);
1458 is_recvd_column(zprop_get_cbdata_t *cbp)
1461 zfs_get_column_t col;
1463 for (i = 0; i < ZFS_GET_NCOLS &&
1464 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1465 if (col == GET_COL_RECVD)
1471 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1472 * < all | property[,property]... > < fs | snap | vol > ...
1474 * -r recurse over any child datasets
1475 * -H scripted mode. Headers are stripped, and fields are separated
1476 * by tabs instead of spaces.
1477 * -o Set of fields to display. One of "name,property,value,
1478 * received,source". Default is "name,property,value,source".
1479 * "all" is an alias for all five.
1480 * -s Set of sources to allow. One of
1481 * "local,default,inherited,received,temporary,none". Default is
1483 * -p Display values in parsable (literal) format.
1485 * Prints properties for the given datasets. The user can control which
1486 * columns to display as well as which property types to allow.
1490 * Invoked to display the properties for a single dataset.
1493 get_callback(zfs_handle_t *zhp, void *data)
1495 char buf[ZFS_MAXPROPLEN];
1496 char rbuf[ZFS_MAXPROPLEN];
1497 zprop_source_t sourcetype;
1498 char source[ZFS_MAX_DATASET_NAME_LEN];
1499 zprop_get_cbdata_t *cbp = data;
1500 nvlist_t *user_props = zfs_get_user_props(zhp);
1501 zprop_list_t *pl = cbp->cb_proplist;
1505 boolean_t received = is_recvd_column(cbp);
1507 for (; pl != NULL; pl = pl->pl_next) {
1508 char *recvdval = NULL;
1510 * Skip the special fake placeholder. This will also skip over
1511 * the name property when 'all' is specified.
1513 if (pl->pl_prop == ZFS_PROP_NAME &&
1514 pl == cbp->cb_proplist)
1517 if (pl->pl_prop != ZPROP_INVAL) {
1518 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1519 sizeof (buf), &sourcetype, source,
1521 cbp->cb_literal) != 0) {
1524 if (!zfs_prop_valid_for_type(pl->pl_prop,
1525 ZFS_TYPE_DATASET, B_FALSE)) {
1526 (void) fprintf(stderr,
1527 gettext("No such property '%s'\n"),
1528 zfs_prop_to_name(pl->pl_prop));
1531 sourcetype = ZPROP_SRC_NONE;
1532 (void) strlcpy(buf, "-", sizeof (buf));
1535 if (received && (zfs_prop_get_recvd(zhp,
1536 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1537 cbp->cb_literal) == 0))
1540 zprop_print_one_property(zfs_get_name(zhp), cbp,
1541 zfs_prop_to_name(pl->pl_prop),
1542 buf, sourcetype, source, recvdval);
1543 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1544 sourcetype = ZPROP_SRC_LOCAL;
1546 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1547 buf, sizeof (buf), cbp->cb_literal) != 0) {
1548 sourcetype = ZPROP_SRC_NONE;
1549 (void) strlcpy(buf, "-", sizeof (buf));
1552 zprop_print_one_property(zfs_get_name(zhp), cbp,
1553 pl->pl_user_prop, buf, sourcetype, source, NULL);
1554 } else if (zfs_prop_written(pl->pl_user_prop)) {
1555 sourcetype = ZPROP_SRC_LOCAL;
1557 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1558 buf, sizeof (buf), cbp->cb_literal) != 0) {
1559 sourcetype = ZPROP_SRC_NONE;
1560 (void) strlcpy(buf, "-", sizeof (buf));
1563 zprop_print_one_property(zfs_get_name(zhp), cbp,
1564 pl->pl_user_prop, buf, sourcetype, source, NULL);
1566 if (nvlist_lookup_nvlist(user_props,
1567 pl->pl_user_prop, &propval) != 0) {
1570 sourcetype = ZPROP_SRC_NONE;
1573 verify(nvlist_lookup_string(propval,
1574 ZPROP_VALUE, &strval) == 0);
1575 verify(nvlist_lookup_string(propval,
1576 ZPROP_SOURCE, &sourceval) == 0);
1578 if (strcmp(sourceval,
1579 zfs_get_name(zhp)) == 0) {
1580 sourcetype = ZPROP_SRC_LOCAL;
1581 } else if (strcmp(sourceval,
1582 ZPROP_SOURCE_VAL_RECVD) == 0) {
1583 sourcetype = ZPROP_SRC_RECEIVED;
1585 sourcetype = ZPROP_SRC_INHERITED;
1586 (void) strlcpy(source,
1587 sourceval, sizeof (source));
1591 if (received && (zfs_prop_get_recvd(zhp,
1592 pl->pl_user_prop, rbuf, sizeof (rbuf),
1593 cbp->cb_literal) == 0))
1596 zprop_print_one_property(zfs_get_name(zhp), cbp,
1597 pl->pl_user_prop, strval, sourcetype,
1606 zfs_do_get(int argc, char **argv)
1608 zprop_get_cbdata_t cb = { 0 };
1609 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1610 int types = ZFS_TYPE_DATASET;
1611 char *value, *fields;
1614 zprop_list_t fake_name = { 0 };
1617 * Set up default columns and sources.
1619 cb.cb_sources = ZPROP_SRC_ALL;
1620 cb.cb_columns[0] = GET_COL_NAME;
1621 cb.cb_columns[1] = GET_COL_PROPERTY;
1622 cb.cb_columns[2] = GET_COL_VALUE;
1623 cb.cb_columns[3] = GET_COL_SOURCE;
1624 cb.cb_type = ZFS_TYPE_DATASET;
1627 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1630 cb.cb_literal = B_TRUE;
1633 limit = parse_depth(optarg, &flags);
1636 flags |= ZFS_ITER_RECURSE;
1639 cb.cb_scripted = B_TRUE;
1642 (void) fprintf(stderr, gettext("missing argument for "
1643 "'%c' option\n"), optopt);
1648 * Process the set of columns to display. We zero out
1649 * the structure to give us a blank slate.
1651 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1653 while (*optarg != '\0') {
1654 static char *col_subopts[] =
1655 { "name", "property", "value", "received",
1656 "source", "all", NULL };
1658 if (i == ZFS_GET_NCOLS) {
1659 (void) fprintf(stderr, gettext("too "
1660 "many fields given to -o "
1665 switch (getsubopt(&optarg, col_subopts,
1668 cb.cb_columns[i++] = GET_COL_NAME;
1671 cb.cb_columns[i++] = GET_COL_PROPERTY;
1674 cb.cb_columns[i++] = GET_COL_VALUE;
1677 cb.cb_columns[i++] = GET_COL_RECVD;
1678 flags |= ZFS_ITER_RECVD_PROPS;
1681 cb.cb_columns[i++] = GET_COL_SOURCE;
1685 (void) fprintf(stderr,
1686 gettext("\"all\" conflicts "
1687 "with specific fields "
1688 "given to -o option\n"));
1691 cb.cb_columns[0] = GET_COL_NAME;
1692 cb.cb_columns[1] = GET_COL_PROPERTY;
1693 cb.cb_columns[2] = GET_COL_VALUE;
1694 cb.cb_columns[3] = GET_COL_RECVD;
1695 cb.cb_columns[4] = GET_COL_SOURCE;
1696 flags |= ZFS_ITER_RECVD_PROPS;
1700 (void) fprintf(stderr,
1701 gettext("invalid column name "
1710 while (*optarg != '\0') {
1711 static char *source_subopts[] = {
1712 "local", "default", "inherited",
1713 "received", "temporary", "none",
1716 switch (getsubopt(&optarg, source_subopts,
1719 cb.cb_sources |= ZPROP_SRC_LOCAL;
1722 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1725 cb.cb_sources |= ZPROP_SRC_INHERITED;
1728 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1731 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1734 cb.cb_sources |= ZPROP_SRC_NONE;
1737 (void) fprintf(stderr,
1738 gettext("invalid source "
1747 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1748 while (*optarg != '\0') {
1749 static char *type_subopts[] = { "filesystem",
1750 "volume", "snapshot", "bookmark",
1753 switch (getsubopt(&optarg, type_subopts,
1756 types |= ZFS_TYPE_FILESYSTEM;
1759 types |= ZFS_TYPE_VOLUME;
1762 types |= ZFS_TYPE_SNAPSHOT;
1765 types |= ZFS_TYPE_BOOKMARK;
1768 types = ZFS_TYPE_DATASET |
1773 (void) fprintf(stderr,
1774 gettext("invalid type '%s'\n"),
1782 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1792 (void) fprintf(stderr, gettext("missing property "
1799 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1807 * As part of zfs_expand_proplist(), we keep track of the maximum column
1808 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1809 * need to know the maximum name length. However, the user likely did
1810 * not specify 'name' as one of the properties to fetch, so we need to
1811 * make sure we always include at least this property for
1812 * print_get_headers() to work properly.
1814 if (cb.cb_proplist != NULL) {
1815 fake_name.pl_prop = ZFS_PROP_NAME;
1816 fake_name.pl_width = strlen(gettext("NAME"));
1817 fake_name.pl_next = cb.cb_proplist;
1818 cb.cb_proplist = &fake_name;
1821 cb.cb_first = B_TRUE;
1823 /* run for each object */
1824 ret = zfs_for_each(argc, argv, flags, types, NULL,
1825 &cb.cb_proplist, limit, get_callback, &cb);
1827 if (cb.cb_proplist == &fake_name)
1828 zprop_free_list(fake_name.pl_next);
1830 zprop_free_list(cb.cb_proplist);
1836 * inherit [-rS] <property> <fs|vol> ...
1838 * -r Recurse over all children
1839 * -S Revert to received value, if any
1841 * For each dataset specified on the command line, inherit the given property
1842 * from its parent. Inheriting a property at the pool level will cause it to
1843 * use the default value. The '-r' flag will recurse over all children, and is
1844 * useful for setting a property on a hierarchy-wide basis, regardless of any
1845 * local modifications for each dataset.
1848 typedef struct inherit_cbdata {
1849 const char *cb_propname;
1850 boolean_t cb_received;
1854 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1856 inherit_cbdata_t *cb = data;
1857 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1860 * If we're doing it recursively, then ignore properties that
1861 * are not valid for this type of dataset.
1863 if (prop != ZPROP_INVAL &&
1864 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp), B_FALSE))
1867 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1871 inherit_cb(zfs_handle_t *zhp, void *data)
1873 inherit_cbdata_t *cb = data;
1875 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1879 zfs_do_inherit(int argc, char **argv)
1883 inherit_cbdata_t cb = { 0 };
1887 boolean_t received = B_FALSE;
1890 while ((c = getopt(argc, argv, "rS")) != -1) {
1893 flags |= ZFS_ITER_RECURSE;
1900 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1909 /* check number of arguments */
1911 (void) fprintf(stderr, gettext("missing property argument\n"));
1915 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1923 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1924 if (zfs_prop_readonly(prop)) {
1925 (void) fprintf(stderr, gettext(
1926 "%s property is read-only\n"),
1930 if (!zfs_prop_inheritable(prop) && !received) {
1931 (void) fprintf(stderr, gettext("'%s' property cannot "
1932 "be inherited\n"), propname);
1933 if (prop == ZFS_PROP_QUOTA ||
1934 prop == ZFS_PROP_RESERVATION ||
1935 prop == ZFS_PROP_REFQUOTA ||
1936 prop == ZFS_PROP_REFRESERVATION) {
1937 (void) fprintf(stderr, gettext("use 'zfs set "
1938 "%s=none' to clear\n"), propname);
1939 (void) fprintf(stderr, gettext("use 'zfs "
1940 "inherit -S %s' to revert to received "
1941 "value\n"), propname);
1945 if (received && (prop == ZFS_PROP_VOLSIZE ||
1946 prop == ZFS_PROP_VERSION)) {
1947 (void) fprintf(stderr, gettext("'%s' property cannot "
1948 "be reverted to a received value\n"), propname);
1951 } else if (!zfs_prop_user(propname)) {
1952 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1957 cb.cb_propname = propname;
1958 cb.cb_received = received;
1960 if (flags & ZFS_ITER_RECURSE) {
1961 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1962 NULL, NULL, 0, inherit_recurse_cb, &cb);
1964 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1965 NULL, NULL, 0, inherit_cb, &cb);
1971 typedef struct upgrade_cbdata {
1972 uint64_t cb_numupgraded;
1973 uint64_t cb_numsamegraded;
1974 uint64_t cb_numfailed;
1975 uint64_t cb_version;
1977 boolean_t cb_foundone;
1978 char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
1982 same_pool(zfs_handle_t *zhp, const char *name)
1984 int len1 = strcspn(name, "/@");
1985 const char *zhname = zfs_get_name(zhp);
1986 int len2 = strcspn(zhname, "/@");
1990 return (strncmp(name, zhname, len1) == 0);
1994 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1996 upgrade_cbdata_t *cb = data;
1997 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1999 /* list if it's old/new */
2000 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2001 (cb->cb_newer && version > ZPL_VERSION)) {
2004 str = gettext("The following filesystems are "
2005 "formatted using a newer software version and\n"
2006 "cannot be accessed on the current system.\n\n");
2008 str = gettext("The following filesystems are "
2009 "out of date, and can be upgraded. After being\n"
2010 "upgraded, these filesystems (and any 'zfs send' "
2011 "streams generated from\n"
2012 "subsequent snapshots) will no longer be "
2013 "accessible by older software versions.\n\n");
2016 if (!cb->cb_foundone) {
2018 (void) printf(gettext("VER FILESYSTEM\n"));
2019 (void) printf(gettext("--- ------------\n"));
2020 cb->cb_foundone = B_TRUE;
2023 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2030 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2032 upgrade_cbdata_t *cb = data;
2033 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2034 int needed_spa_version;
2037 if (zfs_spa_version(zhp, &spa_version) < 0)
2040 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2042 if (needed_spa_version < 0)
2045 if (spa_version < needed_spa_version) {
2047 (void) printf(gettext("%s: can not be "
2048 "upgraded; the pool version needs to first "
2049 "be upgraded\nto version %d\n\n"),
2050 zfs_get_name(zhp), needed_spa_version);
2056 if (version < cb->cb_version) {
2058 (void) snprintf(verstr, sizeof (verstr),
2059 "%llu", (u_longlong_t)cb->cb_version);
2060 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2062 * If they did "zfs upgrade -a", then we could
2063 * be doing ioctls to different pools. We need
2064 * to log this history once to each pool, and bypass
2065 * the normal history logging that happens in main().
2067 (void) zpool_log_history(g_zfs, history_str);
2068 log_history = B_FALSE;
2070 if (zfs_prop_set(zhp, "version", verstr) == 0)
2071 cb->cb_numupgraded++;
2074 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2075 } else if (version > cb->cb_version) {
2076 /* can't downgrade */
2077 (void) printf(gettext("%s: can not be downgraded; "
2078 "it is already at version %u\n"),
2079 zfs_get_name(zhp), version);
2082 cb->cb_numsamegraded++;
2090 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2093 zfs_do_upgrade(int argc, char **argv)
2095 boolean_t all = B_FALSE;
2096 boolean_t showversions = B_FALSE;
2098 upgrade_cbdata_t cb = { 0 };
2100 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2103 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2106 flags |= ZFS_ITER_RECURSE;
2109 showversions = B_TRUE;
2112 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2113 optarg, &cb.cb_version) != 0) {
2114 (void) fprintf(stderr,
2115 gettext("invalid version %s\n"), optarg);
2124 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2133 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2135 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2136 cb.cb_version || argc))
2138 if ((all || argc) && (showversions))
2144 /* Show info on available versions. */
2145 (void) printf(gettext("The following filesystem versions are "
2147 (void) printf(gettext("VER DESCRIPTION\n"));
2148 (void) printf("--- -----------------------------------------"
2149 "---------------\n");
2150 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2151 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2152 (void) printf(gettext(" 3 Case insensitive and filesystem "
2153 "user identifier (FUID)\n"));
2154 (void) printf(gettext(" 4 userquota, groupquota "
2156 (void) printf(gettext(" 5 System attributes\n"));
2157 (void) printf(gettext("\nFor more information on a particular "
2158 "version, including supported releases,\n"));
2159 (void) printf("see the ZFS Administration Guide.\n\n");
2161 } else if (argc || all) {
2162 /* Upgrade filesystems */
2163 if (cb.cb_version == 0)
2164 cb.cb_version = ZPL_VERSION;
2165 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2166 NULL, NULL, 0, upgrade_set_callback, &cb);
2167 (void) printf(gettext("%llu filesystems upgraded\n"),
2168 (u_longlong_t)cb.cb_numupgraded);
2169 if (cb.cb_numsamegraded) {
2170 (void) printf(gettext("%llu filesystems already at "
2172 (u_longlong_t)cb.cb_numsamegraded);
2174 if (cb.cb_numfailed != 0)
2177 /* List old-version filesytems */
2179 (void) printf(gettext("This system is currently running "
2180 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2182 flags |= ZFS_ITER_RECURSE;
2183 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2184 NULL, NULL, 0, upgrade_list_callback, &cb);
2186 found = cb.cb_foundone;
2187 cb.cb_foundone = B_FALSE;
2188 cb.cb_newer = B_TRUE;
2190 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2191 NULL, NULL, 0, upgrade_list_callback, &cb);
2193 if (!cb.cb_foundone && !found) {
2194 (void) printf(gettext("All filesystems are "
2195 "formatted with the current version.\n"));
2203 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2204 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2205 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2206 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2208 * -H Scripted mode; elide headers and separate columns by tabs.
2209 * -i Translate SID to POSIX ID.
2210 * -n Print numeric ID instead of user/group name.
2211 * -o Control which fields to display.
2212 * -p Use exact (parsable) numeric output.
2213 * -s Specify sort columns, descending order.
2214 * -S Specify sort columns, ascending order.
2215 * -t Control which object types to display.
2217 * Displays space consumed by, and quotas on, each user in the specified
2218 * filesystem or snapshot.
2221 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2222 enum us_field_types {
2230 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA",
2231 "OBJUSED", "OBJQUOTA" };
2232 static char *us_field_names[] = { "type", "name", "used", "quota",
2233 "objused", "objquota" };
2234 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2236 #define USTYPE_PSX_GRP (1 << 0)
2237 #define USTYPE_PSX_USR (1 << 1)
2238 #define USTYPE_SMB_GRP (1 << 2)
2239 #define USTYPE_SMB_USR (1 << 3)
2240 #define USTYPE_ALL \
2241 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2243 static int us_type_bits[] = {
2250 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2253 typedef struct us_node {
2255 uu_avl_node_t usn_avlnode;
2256 uu_list_node_t usn_listnode;
2259 typedef struct us_cbdata {
2261 uu_avl_pool_t *cb_avl_pool;
2263 boolean_t cb_numname;
2264 boolean_t cb_nicenum;
2265 boolean_t cb_sid2posix;
2266 zfs_userquota_prop_t cb_prop;
2267 zfs_sort_column_t *cb_sortcol;
2268 size_t cb_width[USFIELD_LAST];
2271 static boolean_t us_populated = B_FALSE;
2274 zfs_sort_column_t *si_sortcol;
2275 boolean_t si_numname;
2279 us_field_index(char *field)
2283 for (i = 0; i < USFIELD_LAST; i++) {
2284 if (strcmp(field, us_field_names[i]) == 0)
2292 us_compare(const void *larg, const void *rarg, void *unused)
2294 const us_node_t *l = larg;
2295 const us_node_t *r = rarg;
2296 us_sort_info_t *si = (us_sort_info_t *)unused;
2297 zfs_sort_column_t *sortcol = si->si_sortcol;
2298 boolean_t numname = si->si_numname;
2299 nvlist_t *lnvl = l->usn_nvl;
2300 nvlist_t *rnvl = r->usn_nvl;
2304 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2311 zfs_prop_t prop = sortcol->sc_prop;
2312 const char *propname = NULL;
2313 boolean_t reverse = sortcol->sc_reverse;
2318 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2319 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2321 rc = (rv32 < lv32) ? 1 : -1;
2327 (void) nvlist_lookup_uint64(lnvl, propname,
2329 (void) nvlist_lookup_uint64(rnvl, propname,
2332 rc = (rv64 < lv64) ? 1 : -1;
2334 if ((nvlist_lookup_string(lnvl, propname,
2335 &lvstr) == ENOENT) ||
2336 (nvlist_lookup_string(rnvl, propname,
2337 &rvstr) == ENOENT)) {
2340 rc = strcmp(lvstr, rvstr);
2344 case ZFS_PROP_QUOTA:
2347 if (prop == ZFS_PROP_USED)
2351 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2352 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2354 rc = (rv64 < lv64) ? 1 : -1;
2362 return (reverse ? 1 : -1);
2364 return (reverse ? -1 : 1);
2369 * If entries still seem to be the same, check if they are of the same
2370 * type (smbentity is added only if we are doing SID to POSIX ID
2371 * translation where we can have duplicate type/name combinations).
2373 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2374 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2376 return (lvb < rvb ? -1 : 1);
2382 zfs_prop_is_user(unsigned p)
2384 return (p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA ||
2385 p == ZFS_PROP_USEROBJUSED || p == ZFS_PROP_USEROBJQUOTA);
2389 zfs_prop_is_group(unsigned p)
2391 return (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA ||
2392 p == ZFS_PROP_GROUPOBJUSED || p == ZFS_PROP_GROUPOBJQUOTA);
2395 static inline const char *
2396 us_type2str(unsigned field_type)
2398 switch (field_type) {
2399 case USTYPE_PSX_USR:
2400 return ("POSIX User");
2401 case USTYPE_PSX_GRP:
2402 return ("POSIX Group");
2403 case USTYPE_SMB_USR:
2404 return ("SMB User");
2405 case USTYPE_SMB_GRP:
2406 return ("SMB Group");
2408 return ("Undefined");
2413 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2415 us_cbdata_t *cb = (us_cbdata_t *)arg;
2416 zfs_userquota_prop_t prop = cb->cb_prop;
2421 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2422 uu_avl_t *avl = cb->cb_avl;
2426 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2428 const char *typestr;
2432 int typeidx, nameidx, sizeidx;
2433 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2434 boolean_t smbentity = B_FALSE;
2436 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2438 node = safe_malloc(sizeof (us_node_t));
2439 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2440 node->usn_nvl = props;
2442 if (domain != NULL && domain[0] != '\0') {
2445 char sid[MAXNAMELEN + 32];
2449 directory_error_t e;
2453 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2455 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2456 type = USTYPE_SMB_GRP;
2457 err = sid_to_id(sid, B_FALSE, &id);
2459 type = USTYPE_SMB_USR;
2460 err = sid_to_id(sid, B_TRUE, &id);
2465 if (!cb->cb_sid2posix) {
2466 e = directory_name_from_sid(NULL, sid, &name,
2469 directory_error_free(e);
2479 #endif /* HAVE_IDMAP */
2482 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2484 if (zfs_prop_is_group(prop)) {
2485 type = USTYPE_PSX_GRP;
2486 if (!cb->cb_numname) {
2489 if ((g = getgrgid(rid)) != NULL)
2493 type = USTYPE_PSX_USR;
2494 if (!cb->cb_numname) {
2497 if ((p = getpwuid(rid)) != NULL)
2504 * Make sure that the type/name combination is unique when doing
2505 * SID to POSIX ID translation (hence changing the type from SMB to
2508 if (cb->cb_sid2posix &&
2509 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2512 /* Calculate/update width of TYPE field */
2513 typestr = us_type2str(type);
2514 typelen = strlen(gettext(typestr));
2515 typeidx = us_field_index("type");
2516 if (typelen > cb->cb_width[typeidx])
2517 cb->cb_width[typeidx] = typelen;
2518 if (nvlist_add_uint32(props, "type", type) != 0)
2521 /* Calculate/update width of NAME field */
2522 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2523 if (nvlist_add_uint64(props, "name", rid) != 0)
2525 namelen = snprintf(NULL, 0, "%u", rid);
2527 if (nvlist_add_string(props, "name", name) != 0)
2529 namelen = strlen(name);
2531 nameidx = us_field_index("name");
2532 if (nameidx >= 0 && namelen > cb->cb_width[nameidx])
2533 cb->cb_width[nameidx] = namelen;
2536 * Check if this type/name combination is in the list and update it;
2537 * otherwise add new node to the list.
2539 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2540 uu_avl_insert(avl, node, idx);
2545 props = node->usn_nvl;
2548 /* Calculate/update width of USED/QUOTA fields */
2550 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2552 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
2553 (u_longlong_t)space);
2554 sizelen = strlen(sizebuf);
2555 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2557 if (!nvlist_exists(props, "quota"))
2558 (void) nvlist_add_uint64(props, "quota", 0);
2559 } else if (prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA) {
2561 if (!nvlist_exists(props, "used"))
2562 (void) nvlist_add_uint64(props, "used", 0);
2563 } else if (prop == ZFS_PROP_USEROBJUSED ||
2564 prop == ZFS_PROP_GROUPOBJUSED) {
2565 propname = "objused";
2566 if (!nvlist_exists(props, "objquota"))
2567 (void) nvlist_add_uint64(props, "objquota", 0);
2568 } else if (prop == ZFS_PROP_USEROBJQUOTA ||
2569 prop == ZFS_PROP_GROUPOBJQUOTA) {
2570 propname = "objquota";
2571 if (!nvlist_exists(props, "objused"))
2572 (void) nvlist_add_uint64(props, "objused", 0);
2576 sizeidx = us_field_index(propname);
2577 if (sizeidx >= 0 && sizelen > cb->cb_width[sizeidx])
2578 cb->cb_width[sizeidx] = sizelen;
2580 if (nvlist_add_uint64(props, propname, space) != 0)
2587 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2588 size_t *width, us_node_t *node)
2590 nvlist_t *nvl = node->usn_nvl;
2591 char valstr[MAXNAMELEN];
2592 boolean_t first = B_TRUE;
2598 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2599 if (!(ustype & types))
2602 while ((field = fields[cfield]) != USFIELD_LAST) {
2603 nvpair_t *nvp = NULL;
2609 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2610 if (strcmp(nvpair_name(nvp),
2611 us_field_names[field]) == 0)
2615 type = nvp == NULL ? DATA_TYPE_UNKNOWN : nvpair_type(nvp);
2617 case DATA_TYPE_UINT32:
2618 (void) nvpair_value_uint32(nvp, &val32);
2620 case DATA_TYPE_UINT64:
2621 (void) nvpair_value_uint64(nvp, &val64);
2623 case DATA_TYPE_STRING:
2624 (void) nvpair_value_string(nvp, &strval);
2626 case DATA_TYPE_UNKNOWN:
2629 (void) fprintf(stderr, "invalid data type\n");
2634 if (type == DATA_TYPE_UINT32)
2635 strval = (char *)us_type2str(val32);
2638 if (type == DATA_TYPE_UINT64) {
2639 (void) sprintf(valstr, "%llu",
2640 (u_longlong_t)val64);
2646 case USFIELD_OBJUSED:
2647 case USFIELD_OBJQUOTA:
2648 if (type == DATA_TYPE_UINT64) {
2650 (void) sprintf(valstr, "%llu",
2651 (u_longlong_t)val64);
2653 zfs_nicenum(val64, valstr,
2656 if ((field == USFIELD_QUOTA ||
2657 field == USFIELD_OBJQUOTA) &&
2658 strcmp(valstr, "0") == 0)
2668 (void) printf("\t");
2673 (void) printf("%s", strval);
2674 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2675 (void) printf("%-*s", (int)width[field], strval);
2677 (void) printf("%*s", (int)width[field], strval);
2683 (void) printf("\n");
2687 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2688 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2696 boolean_t first = B_TRUE;
2698 while ((field = fields[cfield]) != USFIELD_LAST) {
2699 col = gettext(us_field_hdr[field]);
2700 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2701 (void) printf(first ? "%-*s" : " %-*s",
2702 (int)width[field], col);
2704 (void) printf(first ? "%*s" : " %*s",
2705 (int)width[field], col);
2710 (void) printf("\n");
2713 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2714 print_us_node(scripted, parsable, fields, types, width, node);
2716 nvlist_free(node->usn_nvl);
2721 zfs_do_userspace(int argc, char **argv)
2724 zfs_userquota_prop_t p;
2725 uu_avl_pool_t *avl_pool;
2727 uu_avl_walk_t *walk;
2729 char deffields[] = "type,name,used,quota,objused,objquota";
2730 char *ofield = NULL;
2731 char *tfield = NULL;
2735 boolean_t scripted = B_FALSE;
2736 boolean_t prtnum = B_FALSE;
2737 boolean_t parsable = B_FALSE;
2738 boolean_t sid2posix = B_FALSE;
2741 zfs_sort_column_t *sortcol = NULL;
2742 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2746 uu_list_pool_t *listpool;
2748 uu_avl_index_t idx = 0;
2749 uu_list_index_t idx2 = 0;
2754 if (strcmp(argv[0], "groupspace") == 0)
2755 /* Toggle default group types */
2756 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2758 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2774 if (zfs_add_sort_column(&sortcol, optarg,
2775 c == 's' ? B_FALSE : B_TRUE) != 0) {
2776 (void) fprintf(stderr,
2777 gettext("invalid field '%s'\n"), optarg);
2788 (void) fprintf(stderr, gettext("missing argument for "
2789 "'%c' option\n"), optopt);
2793 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2803 (void) fprintf(stderr, gettext("missing dataset name\n"));
2807 (void) fprintf(stderr, gettext("too many arguments\n"));
2811 /* Use default output fields if not specified using -o */
2815 if ((delim = strchr(ofield, ',')) != NULL)
2817 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2818 (void) fprintf(stderr, gettext("invalid type '%s' "
2819 "for -o option\n"), ofield);
2824 } while (delim != NULL);
2825 fields[cfield] = USFIELD_LAST;
2827 /* Override output types (-t option) */
2828 if (tfield != NULL) {
2832 boolean_t found = B_FALSE;
2834 if ((delim = strchr(tfield, ',')) != NULL)
2836 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2838 if (strcmp(tfield, us_type_names[i]) == 0) {
2840 types |= us_type_bits[i];
2845 (void) fprintf(stderr, gettext("invalid type "
2846 "'%s' for -t option\n"), tfield);
2851 } while (delim != NULL);
2854 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2857 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2858 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2860 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2863 /* Always add default sorting columns */
2864 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2865 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2867 cb.cb_sortcol = sortcol;
2868 cb.cb_numname = prtnum;
2869 cb.cb_nicenum = !parsable;
2870 cb.cb_avl_pool = avl_pool;
2871 cb.cb_avl = avl_tree;
2872 cb.cb_sid2posix = sid2posix;
2874 for (i = 0; i < USFIELD_LAST; i++)
2875 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2877 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2878 if ((zfs_prop_is_user(p) &&
2879 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2880 (zfs_prop_is_group(p) &&
2881 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2885 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2890 if ((node = uu_avl_first(avl_tree)) == NULL)
2893 us_populated = B_TRUE;
2895 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2896 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2897 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2898 uu_list_node_init(node, &node->usn_listnode, listpool);
2900 while (node != NULL) {
2902 node = uu_avl_next(avl_tree, node);
2903 uu_avl_remove(avl_tree, rmnode);
2904 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2905 uu_list_insert(list, rmnode, idx2);
2908 for (node = uu_list_first(list); node != NULL;
2909 node = uu_list_next(list, node)) {
2910 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2912 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2913 uu_avl_insert(avl_tree, node, idx);
2916 uu_list_destroy(list);
2917 uu_list_pool_destroy(listpool);
2919 /* Print and free node nvlist memory */
2920 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2923 zfs_free_sort_columns(sortcol);
2925 /* Clean up the AVL tree */
2926 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2929 while ((node = uu_avl_walk_next(walk)) != NULL) {
2930 uu_avl_remove(cb.cb_avl, node);
2934 uu_avl_walk_end(walk);
2935 uu_avl_destroy(avl_tree);
2936 uu_avl_pool_destroy(avl_pool);
2942 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
2943 * [-t type[,...]] [filesystem|volume|snapshot] ...
2945 * -H Scripted mode; elide headers and separate columns by tabs
2946 * -p Display values in parsable (literal) format.
2947 * -r Recurse over all children
2948 * -d Limit recursion by depth.
2949 * -o Control which fields to display.
2950 * -s Specify sort columns, descending order.
2951 * -S Specify sort columns, ascending order.
2952 * -t Control which object types to display.
2954 * When given no arguments, list all filesystems in the system.
2955 * Otherwise, list the specified datasets, optionally recursing down them if
2956 * '-r' is specified.
2958 typedef struct list_cbdata {
2960 boolean_t cb_literal;
2961 boolean_t cb_scripted;
2962 zprop_list_t *cb_proplist;
2966 * Given a list of columns to display, output appropriate headers for each one.
2969 print_header(list_cbdata_t *cb)
2971 zprop_list_t *pl = cb->cb_proplist;
2972 char headerbuf[ZFS_MAXPROPLEN];
2975 boolean_t first = B_TRUE;
2976 boolean_t right_justify;
2978 for (; pl != NULL; pl = pl->pl_next) {
2985 right_justify = B_FALSE;
2986 if (pl->pl_prop != ZPROP_INVAL) {
2987 header = zfs_prop_column_name(pl->pl_prop);
2988 right_justify = zfs_prop_align_right(pl->pl_prop);
2990 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2991 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2992 headerbuf[i] = '\0';
2996 if (pl->pl_next == NULL && !right_justify)
2997 (void) printf("%s", header);
2998 else if (right_justify)
2999 (void) printf("%*s", (int)pl->pl_width, header);
3001 (void) printf("%-*s", (int)pl->pl_width, header);
3004 (void) printf("\n");
3008 * Given a dataset and a list of fields, print out all the properties according
3009 * to the described layout.
3012 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3014 zprop_list_t *pl = cb->cb_proplist;
3015 boolean_t first = B_TRUE;
3016 char property[ZFS_MAXPROPLEN];
3017 nvlist_t *userprops = zfs_get_user_props(zhp);
3020 boolean_t right_justify;
3022 for (; pl != NULL; pl = pl->pl_next) {
3024 if (cb->cb_scripted)
3025 (void) printf("\t");
3032 if (pl->pl_prop == ZFS_PROP_NAME) {
3033 (void) strlcpy(property, zfs_get_name(zhp),
3036 right_justify = zfs_prop_align_right(pl->pl_prop);
3037 } else if (pl->pl_prop != ZPROP_INVAL) {
3038 if (zfs_prop_get(zhp, pl->pl_prop, property,
3039 sizeof (property), NULL, NULL, 0,
3040 cb->cb_literal) != 0)
3044 right_justify = zfs_prop_align_right(pl->pl_prop);
3045 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3046 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3047 property, sizeof (property), cb->cb_literal) != 0)
3051 right_justify = B_TRUE;
3052 } else if (zfs_prop_written(pl->pl_user_prop)) {
3053 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3054 property, sizeof (property), cb->cb_literal) != 0)
3058 right_justify = B_TRUE;
3060 if (nvlist_lookup_nvlist(userprops,
3061 pl->pl_user_prop, &propval) != 0)
3064 verify(nvlist_lookup_string(propval,
3065 ZPROP_VALUE, &propstr) == 0);
3066 right_justify = B_FALSE;
3070 * If this is being called in scripted mode, or if this is the
3071 * last column and it is left-justified, don't include a width
3074 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3075 (void) printf("%s", propstr);
3076 else if (right_justify)
3077 (void) printf("%*s", (int)pl->pl_width, propstr);
3079 (void) printf("%-*s", (int)pl->pl_width, propstr);
3082 (void) printf("\n");
3086 * Generic callback function to list a dataset or snapshot.
3089 list_callback(zfs_handle_t *zhp, void *data)
3091 list_cbdata_t *cbp = data;
3093 if (cbp->cb_first) {
3094 if (!cbp->cb_scripted)
3096 cbp->cb_first = B_FALSE;
3099 print_dataset(zhp, cbp);
3105 zfs_do_list(int argc, char **argv)
3108 static char default_fields[] =
3109 "name,used,available,referenced,mountpoint";
3110 int types = ZFS_TYPE_DATASET;
3111 boolean_t types_specified = B_FALSE;
3112 char *fields = NULL;
3113 list_cbdata_t cb = { 0 };
3117 zfs_sort_column_t *sortcol = NULL;
3118 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3121 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3127 cb.cb_literal = B_TRUE;
3128 flags |= ZFS_ITER_LITERAL_PROPS;
3131 limit = parse_depth(optarg, &flags);
3134 flags |= ZFS_ITER_RECURSE;
3137 cb.cb_scripted = B_TRUE;
3140 if (zfs_add_sort_column(&sortcol, optarg,
3142 (void) fprintf(stderr,
3143 gettext("invalid property '%s'\n"), optarg);
3148 if (zfs_add_sort_column(&sortcol, optarg,
3150 (void) fprintf(stderr,
3151 gettext("invalid property '%s'\n"), optarg);
3157 types_specified = B_TRUE;
3158 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3159 while (*optarg != '\0') {
3160 static char *type_subopts[] = { "filesystem",
3161 "volume", "snapshot", "snap", "bookmark",
3164 switch (getsubopt(&optarg, type_subopts,
3167 types |= ZFS_TYPE_FILESYSTEM;
3170 types |= ZFS_TYPE_VOLUME;
3174 types |= ZFS_TYPE_SNAPSHOT;
3177 types |= ZFS_TYPE_BOOKMARK;
3180 types = ZFS_TYPE_DATASET |
3184 (void) fprintf(stderr,
3185 gettext("invalid type '%s'\n"),
3192 (void) fprintf(stderr, gettext("missing argument for "
3193 "'%c' option\n"), optopt);
3197 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3207 fields = default_fields;
3210 * If we are only going to list snapshot names and sort by name,
3211 * then we can use faster version.
3213 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3214 flags |= ZFS_ITER_SIMPLE;
3217 * If "-o space" and no types were specified, don't display snapshots.
3219 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3220 types &= ~ZFS_TYPE_SNAPSHOT;
3223 * If the user specifies '-o all', the zprop_get_list() doesn't
3224 * normally include the name of the dataset. For 'zfs list', we always
3225 * want this property to be first.
3227 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3231 cb.cb_first = B_TRUE;
3233 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3234 limit, list_callback, &cb);
3236 zprop_free_list(cb.cb_proplist);
3237 zfs_free_sort_columns(sortcol);
3239 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3240 (void) fprintf(stderr, gettext("no datasets available\n"));
3246 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3247 * zfs rename [-f] -p <fs | vol> <fs | vol>
3248 * zfs rename -r <snap> <snap>
3250 * Renames the given dataset to another of the same type.
3252 * The '-p' flag creates all the non-existing ancestors of the target first.
3256 zfs_do_rename(int argc, char **argv)
3261 boolean_t recurse = B_FALSE;
3262 boolean_t parents = B_FALSE;
3263 boolean_t force_unmount = B_FALSE;
3266 while ((c = getopt(argc, argv, "prf")) != -1) {
3275 force_unmount = B_TRUE;
3279 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3288 /* check number of arguments */
3290 (void) fprintf(stderr, gettext("missing source dataset "
3295 (void) fprintf(stderr, gettext("missing target dataset "
3300 (void) fprintf(stderr, gettext("too many arguments\n"));
3304 if (recurse && parents) {
3305 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3310 if (recurse && strchr(argv[0], '@') == 0) {
3311 (void) fprintf(stderr, gettext("source dataset for recursive "
3312 "rename must be a snapshot\n"));
3316 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3317 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3320 /* If we were asked and the name looks good, try to create ancestors. */
3321 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3322 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3327 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3336 * Promotes the given clone fs to be the parent
3340 zfs_do_promote(int argc, char **argv)
3346 if (argc > 1 && argv[1][0] == '-') {
3347 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3352 /* check number of arguments */
3354 (void) fprintf(stderr, gettext("missing clone filesystem"
3359 (void) fprintf(stderr, gettext("too many arguments\n"));
3363 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3367 ret = (zfs_promote(zhp) != 0);
3375 * zfs rollback [-rRf] <snapshot>
3377 * -r Delete any intervening snapshots before doing rollback
3378 * -R Delete any snapshots and their clones
3379 * -f ignored for backwards compatability
3381 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3382 * since then and making it the active dataset. If more recent snapshots exist,
3383 * the command will complain unless the '-r' flag is given.
3385 typedef struct rollback_cbdata {
3391 boolean_t cb_recurse;
3392 } rollback_cbdata_t;
3395 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3397 rollback_cbdata_t *cbp = data;
3399 if (cbp->cb_first && cbp->cb_recurse) {
3400 (void) fprintf(stderr, gettext("cannot rollback to "
3401 "'%s': clones of previous snapshots exist\n"),
3403 (void) fprintf(stderr, gettext("use '-R' to "
3404 "force deletion of the following clones and "
3410 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3418 * Report any snapshots more recent than the one specified. Used when '-r' is
3419 * not specified. We reuse this same callback for the snapshot dependents - if
3420 * 'cb_dependent' is set, then this is a dependent and we should report it
3421 * without checking the transaction group.
3424 rollback_check(zfs_handle_t *zhp, void *data)
3426 rollback_cbdata_t *cbp = data;
3428 if (cbp->cb_doclones) {
3433 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3434 if (cbp->cb_first && !cbp->cb_recurse) {
3435 (void) fprintf(stderr, gettext("cannot "
3436 "rollback to '%s': more recent snapshots "
3437 "or bookmarks exist\n"),
3439 (void) fprintf(stderr, gettext("use '-r' to "
3440 "force deletion of the following "
3441 "snapshots and bookmarks:\n"));
3446 if (cbp->cb_recurse) {
3447 if (zfs_iter_dependents(zhp, B_TRUE,
3448 rollback_check_dependent, cbp) != 0) {
3453 (void) fprintf(stderr, "%s\n",
3462 zfs_do_rollback(int argc, char **argv)
3466 boolean_t force = B_FALSE;
3467 rollback_cbdata_t cb = { 0 };
3468 zfs_handle_t *zhp, *snap;
3469 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3473 while ((c = getopt(argc, argv, "rRf")) != -1) {
3486 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3495 /* check number of arguments */
3497 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3501 (void) fprintf(stderr, gettext("too many arguments\n"));
3505 /* open the snapshot */
3506 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3509 /* open the parent dataset */
3510 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3511 verify((delim = strrchr(parentname, '@')) != NULL);
3513 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3519 * Check for more recent snapshots and/or clones based on the presence
3522 cb.cb_target = argv[0];
3523 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3524 cb.cb_first = B_TRUE;
3526 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3528 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3531 if ((ret = cb.cb_error) != 0)
3535 * Rollback parent to the given snapshot.
3537 ret = zfs_rollback(zhp, snap, force);
3550 * zfs set property=value ... { fs | snap | vol } ...
3552 * Sets the given properties for all datasets specified on the command line.
3556 set_callback(zfs_handle_t *zhp, void *data)
3558 nvlist_t *props = data;
3560 if (zfs_prop_set_list(zhp, props) != 0) {
3561 switch (libzfs_errno(g_zfs)) {
3562 case EZFS_MOUNTFAILED:
3563 (void) fprintf(stderr, gettext("property may be set "
3564 "but unable to remount filesystem\n"));
3566 case EZFS_SHARENFSFAILED:
3567 (void) fprintf(stderr, gettext("property may be set "
3568 "but unable to reshare filesystem\n"));
3577 zfs_do_set(int argc, char **argv)
3579 nvlist_t *props = NULL;
3580 int ds_start = -1; /* argv idx of first dataset arg */
3584 /* check for options */
3585 if (argc > 1 && argv[1][0] == '-') {
3586 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3591 /* check number of arguments */
3593 (void) fprintf(stderr, gettext("missing arguments\n"));
3597 if (strchr(argv[1], '=') == NULL) {
3598 (void) fprintf(stderr, gettext("missing property=value "
3601 (void) fprintf(stderr, gettext("missing dataset "
3607 /* validate argument order: prop=val args followed by dataset args */
3608 for (i = 1; i < argc; i++) {
3609 if (strchr(argv[i], '=') != NULL) {
3611 /* out-of-order prop=val argument */
3612 (void) fprintf(stderr, gettext("invalid "
3613 "argument order\n"));
3616 } else if (ds_start < 0) {
3621 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3625 /* Populate a list of property settings */
3626 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3628 for (i = 1; i < ds_start; i++) {
3629 if ((ret = parseprop(props, argv[i])) != 0)
3633 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3634 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3641 typedef struct snap_cbdata {
3643 boolean_t sd_recursive;
3644 const char *sd_snapname;
3648 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3650 snap_cbdata_t *sd = arg;
3655 if (sd->sd_recursive &&
3656 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3661 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3664 fnvlist_add_boolean(sd->sd_nvl, name);
3667 if (sd->sd_recursive)
3668 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3674 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3676 * Creates a snapshot with the given name. While functionally equivalent to
3677 * 'zfs create', it is a separate command to differentiate intent.
3680 zfs_do_snapshot(int argc, char **argv)
3685 snap_cbdata_t sd = { 0 };
3686 boolean_t multiple_snaps = B_FALSE;
3688 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3690 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3694 while ((c = getopt(argc, argv, "ro:")) != -1) {
3697 if (parseprop(props, optarg) != 0) {
3698 nvlist_free(sd.sd_nvl);
3704 sd.sd_recursive = B_TRUE;
3705 multiple_snaps = B_TRUE;
3708 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3717 /* check number of arguments */
3719 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3724 multiple_snaps = B_TRUE;
3725 for (; argc > 0; argc--, argv++) {
3729 atp = strchr(argv[0], '@');
3733 sd.sd_snapname = atp + 1;
3734 zhp = zfs_open(g_zfs, argv[0],
3735 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3738 if (zfs_snapshot_cb(zhp, &sd) != 0)
3742 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3743 nvlist_free(sd.sd_nvl);
3745 if (ret != 0 && multiple_snaps)
3746 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3750 nvlist_free(sd.sd_nvl);
3757 * Send a backup stream to stdout.
3760 zfs_do_send(int argc, char **argv)
3762 char *fromname = NULL;
3763 char *toname = NULL;
3764 char *resume_token = NULL;
3767 sendflags_t flags = { 0 };
3769 nvlist_t *dbgnv = NULL;
3770 boolean_t extraverbose = B_FALSE;
3773 while ((c = getopt(argc, argv, ":i:I:RDpvnPLet:c")) != -1) {
3784 flags.doall = B_TRUE;
3787 flags.replicate = B_TRUE;
3790 flags.props = B_TRUE;
3793 flags.parsable = B_TRUE;
3794 flags.verbose = B_TRUE;
3798 extraverbose = B_TRUE;
3799 flags.verbose = B_TRUE;
3800 flags.progress = B_TRUE;
3803 flags.dedup = B_TRUE;
3806 flags.dryrun = B_TRUE;
3809 flags.largeblock = B_TRUE;
3812 flags.embed_data = B_TRUE;
3815 resume_token = optarg;
3818 flags.compress = B_TRUE;
3821 (void) fprintf(stderr, gettext("missing argument for "
3822 "'%c' option\n"), optopt);
3826 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3835 if (resume_token != NULL) {
3836 if (fromname != NULL || flags.replicate || flags.props ||
3838 (void) fprintf(stderr,
3839 gettext("invalid flags combined with -t\n"));
3843 (void) fprintf(stderr, gettext("no additional "
3844 "arguments are permitted with -t\n"));
3849 (void) fprintf(stderr,
3850 gettext("missing snapshot argument\n"));
3854 (void) fprintf(stderr, gettext("too many arguments\n"));
3859 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3860 (void) fprintf(stderr,
3861 gettext("Error: Stream can not be written to a terminal.\n"
3862 "You must redirect standard output.\n"));
3866 if (resume_token != NULL) {
3867 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3872 * Special case sending a filesystem, or from a bookmark.
3874 if (strchr(argv[0], '@') == NULL ||
3875 (fromname && strchr(fromname, '#') != NULL)) {
3876 char frombuf[ZFS_MAX_DATASET_NAME_LEN];
3877 enum lzc_send_flags lzc_flags = 0;
3879 if (flags.replicate || flags.doall || flags.props ||
3880 flags.dedup || flags.dryrun || flags.verbose ||
3882 (void) fprintf(stderr,
3884 "Unsupported flag with filesystem or bookmark.\n"));
3888 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3892 if (flags.largeblock)
3893 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3894 if (flags.embed_data)
3895 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3897 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
3899 if (fromname != NULL &&
3900 (fromname[0] == '#' || fromname[0] == '@')) {
3902 * Incremental source name begins with # or @.
3903 * Default to same fs as target.
3905 (void) strlcpy(frombuf, argv[0], sizeof (frombuf));
3906 cp = strchr(frombuf, '@');
3909 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3912 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3917 cp = strchr(argv[0], '@');
3920 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3925 * If they specified the full path to the snapshot, chop off
3926 * everything except the short name of the snapshot, but special
3927 * case if they specify the origin.
3929 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3930 char origin[ZFS_MAX_DATASET_NAME_LEN];
3933 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3934 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3936 if (strcmp(origin, fromname) == 0) {
3938 flags.fromorigin = B_TRUE;
3941 if (cp != fromname && strcmp(argv[0], fromname)) {
3942 (void) fprintf(stderr,
3943 gettext("incremental source must be "
3944 "in same filesystem\n"));
3948 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3949 (void) fprintf(stderr,
3950 gettext("invalid incremental source\n"));
3956 if (flags.replicate && fromname == NULL)
3957 flags.doall = B_TRUE;
3959 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3960 extraverbose ? &dbgnv : NULL);
3962 if (extraverbose && dbgnv != NULL) {
3964 * dump_nvlist prints to stdout, but that's been
3965 * redirected to a file. Make it print to stderr
3968 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3969 dump_nvlist(dbgnv, 0);
3978 * Restore a backup stream from stdin.
3981 zfs_do_receive(int argc, char **argv)
3984 recvflags_t flags = { 0 };
3985 boolean_t abort_resumable = B_FALSE;
3988 nvpair_t *nvp = NULL;
3990 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3994 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
3997 if (parseprop(props, optarg) != 0) {
4003 flags.isprefix = B_TRUE;
4006 flags.isprefix = B_TRUE;
4007 flags.istail = B_TRUE;
4010 flags.dryrun = B_TRUE;
4013 flags.nomount = B_TRUE;
4016 flags.verbose = B_TRUE;
4019 flags.resumable = B_TRUE;
4022 flags.force = B_TRUE;
4025 abort_resumable = B_TRUE;
4028 (void) fprintf(stderr, gettext("missing argument for "
4029 "'%c' option\n"), optopt);
4033 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4042 /* check number of arguments */
4044 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4048 (void) fprintf(stderr, gettext("too many arguments\n"));
4052 while ((nvp = nvlist_next_nvpair(props, nvp))) {
4053 if (strcmp(nvpair_name(nvp), "origin") != 0) {
4054 (void) fprintf(stderr, gettext("invalid option"));
4059 if (abort_resumable) {
4060 if (flags.isprefix || flags.istail || flags.dryrun ||
4061 flags.resumable || flags.nomount) {
4062 (void) fprintf(stderr, gettext("invalid option"));
4066 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4067 (void) snprintf(namebuf, sizeof (namebuf),
4068 "%s/%%recv", argv[0]);
4070 if (zfs_dataset_exists(g_zfs, namebuf,
4071 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4072 zfs_handle_t *zhp = zfs_open(g_zfs,
4073 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4078 err = zfs_destroy(zhp, B_FALSE);
4081 zfs_handle_t *zhp = zfs_open(g_zfs,
4082 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4085 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4086 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4087 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4088 (void) fprintf(stderr,
4089 gettext("'%s' does not have any "
4090 "resumable receive state to abort\n"),
4096 err = zfs_destroy(zhp, B_FALSE);
4103 if (isatty(STDIN_FILENO)) {
4104 (void) fprintf(stderr,
4105 gettext("Error: Backup stream can not be read "
4106 "from a terminal.\n"
4107 "You must redirect standard input.\n"));
4111 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4118 * allow/unallow stuff
4120 /* copied from zfs/sys/dsl_deleg.h */
4121 #define ZFS_DELEG_PERM_CREATE "create"
4122 #define ZFS_DELEG_PERM_DESTROY "destroy"
4123 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4124 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4125 #define ZFS_DELEG_PERM_CLONE "clone"
4126 #define ZFS_DELEG_PERM_PROMOTE "promote"
4127 #define ZFS_DELEG_PERM_RENAME "rename"
4128 #define ZFS_DELEG_PERM_MOUNT "mount"
4129 #define ZFS_DELEG_PERM_SHARE "share"
4130 #define ZFS_DELEG_PERM_SEND "send"
4131 #define ZFS_DELEG_PERM_RECEIVE "receive"
4132 #define ZFS_DELEG_PERM_ALLOW "allow"
4133 #define ZFS_DELEG_PERM_USERPROP "userprop"
4134 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4135 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4136 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4137 #define ZFS_DELEG_PERM_USERUSED "userused"
4138 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4139 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4140 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4141 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4142 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4144 #define ZFS_DELEG_PERM_HOLD "hold"
4145 #define ZFS_DELEG_PERM_RELEASE "release"
4146 #define ZFS_DELEG_PERM_DIFF "diff"
4147 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4149 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4151 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4152 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4153 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4154 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4155 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4156 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4157 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4158 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4159 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4160 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4161 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4162 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4163 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4164 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4165 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4166 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4167 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4169 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4170 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4171 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4172 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4173 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4174 { ZFS_DELEG_PERM_USEROBJQUOTA, ZFS_DELEG_NOTE_USEROBJQUOTA },
4175 { ZFS_DELEG_PERM_USEROBJUSED, ZFS_DELEG_NOTE_USEROBJUSED },
4176 { ZFS_DELEG_PERM_GROUPOBJQUOTA, ZFS_DELEG_NOTE_GROUPOBJQUOTA },
4177 { ZFS_DELEG_PERM_GROUPOBJUSED, ZFS_DELEG_NOTE_GROUPOBJUSED },
4178 { NULL, ZFS_DELEG_NOTE_NONE }
4181 /* permission structure */
4182 typedef struct deleg_perm {
4183 zfs_deleg_who_type_t dp_who_type;
4184 const char *dp_name;
4186 boolean_t dp_descend;
4190 typedef struct deleg_perm_node {
4191 deleg_perm_t dpn_perm;
4193 uu_avl_node_t dpn_avl_node;
4194 } deleg_perm_node_t;
4196 typedef struct fs_perm fs_perm_t;
4198 /* permissions set */
4199 typedef struct who_perm {
4200 zfs_deleg_who_type_t who_type;
4201 const char *who_name; /* id */
4202 char who_ug_name[256]; /* user/group name */
4203 fs_perm_t *who_fsperm; /* uplink */
4205 uu_avl_t *who_deleg_perm_avl; /* permissions */
4209 typedef struct who_perm_node {
4210 who_perm_t who_perm;
4211 uu_avl_node_t who_avl_node;
4214 typedef struct fs_perm_set fs_perm_set_t;
4215 /* fs permissions */
4217 const char *fsp_name;
4219 uu_avl_t *fsp_sc_avl; /* sets,create */
4220 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4222 fs_perm_set_t *fsp_set; /* uplink */
4226 typedef struct fs_perm_node {
4227 fs_perm_t fspn_fsperm;
4230 uu_list_node_t fspn_list_node;
4233 /* top level structure */
4234 struct fs_perm_set {
4235 uu_list_pool_t *fsps_list_pool;
4236 uu_list_t *fsps_list; /* list of fs_perms */
4238 uu_avl_pool_t *fsps_named_set_avl_pool;
4239 uu_avl_pool_t *fsps_who_perm_avl_pool;
4240 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4243 static inline const char *
4244 deleg_perm_type(zfs_deleg_note_t note)
4250 case ZFS_DELEG_NOTE_GROUPQUOTA:
4251 case ZFS_DELEG_NOTE_GROUPUSED:
4252 case ZFS_DELEG_NOTE_USERPROP:
4253 case ZFS_DELEG_NOTE_USERQUOTA:
4254 case ZFS_DELEG_NOTE_USERUSED:
4255 case ZFS_DELEG_NOTE_USEROBJQUOTA:
4256 case ZFS_DELEG_NOTE_USEROBJUSED:
4257 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
4258 case ZFS_DELEG_NOTE_GROUPOBJUSED:
4260 return (gettext("other"));
4262 return (gettext("subcommand"));
4267 who_type2weight(zfs_deleg_who_type_t who_type)
4271 case ZFS_DELEG_NAMED_SET_SETS:
4272 case ZFS_DELEG_NAMED_SET:
4275 case ZFS_DELEG_CREATE_SETS:
4276 case ZFS_DELEG_CREATE:
4279 case ZFS_DELEG_USER_SETS:
4280 case ZFS_DELEG_USER:
4283 case ZFS_DELEG_GROUP_SETS:
4284 case ZFS_DELEG_GROUP:
4287 case ZFS_DELEG_EVERYONE_SETS:
4288 case ZFS_DELEG_EVERYONE:
4300 who_perm_compare(const void *larg, const void *rarg, void *unused)
4302 const who_perm_node_t *l = larg;
4303 const who_perm_node_t *r = rarg;
4304 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4305 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4306 int lweight = who_type2weight(ltype);
4307 int rweight = who_type2weight(rtype);
4308 int res = lweight - rweight;
4310 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4311 ZFS_MAX_DELEG_NAME-1);
4323 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4325 const deleg_perm_node_t *l = larg;
4326 const deleg_perm_node_t *r = rarg;
4327 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4328 ZFS_MAX_DELEG_NAME-1);
4340 fs_perm_set_init(fs_perm_set_t *fspset)
4342 bzero(fspset, sizeof (fs_perm_set_t));
4344 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4345 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4346 NULL, UU_DEFAULT)) == NULL)
4348 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4349 UU_DEFAULT)) == NULL)
4352 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4353 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4354 who_perm_node_t, who_avl_node), who_perm_compare,
4355 UU_DEFAULT)) == NULL)
4358 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4359 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4360 who_perm_node_t, who_avl_node), who_perm_compare,
4361 UU_DEFAULT)) == NULL)
4364 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4365 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4366 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4371 static inline void fs_perm_fini(fs_perm_t *);
4372 static inline void who_perm_fini(who_perm_t *);
4375 fs_perm_set_fini(fs_perm_set_t *fspset)
4377 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4379 while (node != NULL) {
4380 fs_perm_node_t *next_node =
4381 uu_list_next(fspset->fsps_list, node);
4382 fs_perm_t *fsperm = &node->fspn_fsperm;
4383 fs_perm_fini(fsperm);
4384 uu_list_remove(fspset->fsps_list, node);
4389 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4390 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4391 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4395 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4398 deleg_perm->dp_who_type = type;
4399 deleg_perm->dp_name = name;
4403 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4404 zfs_deleg_who_type_t type, const char *name)
4406 uu_avl_pool_t *pool;
4407 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4409 bzero(who_perm, sizeof (who_perm_t));
4411 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4412 UU_DEFAULT)) == NULL)
4415 who_perm->who_type = type;
4416 who_perm->who_name = name;
4417 who_perm->who_fsperm = fsperm;
4421 who_perm_fini(who_perm_t *who_perm)
4423 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4425 while (node != NULL) {
4426 deleg_perm_node_t *next_node =
4427 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4429 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4434 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4438 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4440 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4441 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4443 bzero(fsperm, sizeof (fs_perm_t));
4445 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4449 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4453 fsperm->fsp_set = fspset;
4454 fsperm->fsp_name = fsname;
4458 fs_perm_fini(fs_perm_t *fsperm)
4460 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4461 while (node != NULL) {
4462 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4464 who_perm_t *who_perm = &node->who_perm;
4465 who_perm_fini(who_perm);
4466 uu_avl_remove(fsperm->fsp_sc_avl, node);
4471 node = uu_avl_first(fsperm->fsp_uge_avl);
4472 while (node != NULL) {
4473 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4475 who_perm_t *who_perm = &node->who_perm;
4476 who_perm_fini(who_perm);
4477 uu_avl_remove(fsperm->fsp_uge_avl, node);
4482 uu_avl_destroy(fsperm->fsp_sc_avl);
4483 uu_avl_destroy(fsperm->fsp_uge_avl);
4487 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4488 zfs_deleg_who_type_t who_type, const char *name, char locality)
4490 uu_avl_index_t idx = 0;
4492 deleg_perm_node_t *found_node = NULL;
4493 deleg_perm_t *deleg_perm = &node->dpn_perm;
4495 deleg_perm_init(deleg_perm, who_type, name);
4497 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4499 uu_avl_insert(avl, node, idx);
4502 deleg_perm = &node->dpn_perm;
4507 case ZFS_DELEG_LOCAL:
4508 deleg_perm->dp_local = B_TRUE;
4510 case ZFS_DELEG_DESCENDENT:
4511 deleg_perm->dp_descend = B_TRUE;
4516 assert(B_FALSE); /* invalid locality */
4521 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4523 nvpair_t *nvp = NULL;
4524 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4525 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4526 zfs_deleg_who_type_t who_type = who_perm->who_type;
4528 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4529 const char *name = nvpair_name(nvp);
4530 data_type_t type = nvpair_type(nvp);
4531 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4532 deleg_perm_node_t *node =
4533 safe_malloc(sizeof (deleg_perm_node_t));
4535 VERIFY(type == DATA_TYPE_BOOLEAN);
4537 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4538 set_deleg_perm_node(avl, node, who_type, name, locality);
4545 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4547 nvpair_t *nvp = NULL;
4548 fs_perm_set_t *fspset = fsperm->fsp_set;
4550 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4551 nvlist_t *nvl2 = NULL;
4552 const char *name = nvpair_name(nvp);
4553 uu_avl_t *avl = NULL;
4554 uu_avl_pool_t *avl_pool = NULL;
4555 zfs_deleg_who_type_t perm_type = name[0];
4556 char perm_locality = name[1];
4557 const char *perm_name = name + 3;
4558 boolean_t is_set = B_TRUE;
4559 who_perm_t *who_perm = NULL;
4561 assert('$' == name[2]);
4563 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4566 switch (perm_type) {
4567 case ZFS_DELEG_CREATE:
4568 case ZFS_DELEG_CREATE_SETS:
4569 case ZFS_DELEG_NAMED_SET:
4570 case ZFS_DELEG_NAMED_SET_SETS:
4571 avl_pool = fspset->fsps_named_set_avl_pool;
4572 avl = fsperm->fsp_sc_avl;
4574 case ZFS_DELEG_USER:
4575 case ZFS_DELEG_USER_SETS:
4576 case ZFS_DELEG_GROUP:
4577 case ZFS_DELEG_GROUP_SETS:
4578 case ZFS_DELEG_EVERYONE:
4579 case ZFS_DELEG_EVERYONE_SETS:
4580 avl_pool = fspset->fsps_who_perm_avl_pool;
4581 avl = fsperm->fsp_uge_avl;
4588 who_perm_node_t *found_node = NULL;
4589 who_perm_node_t *node = safe_malloc(
4590 sizeof (who_perm_node_t));
4591 who_perm = &node->who_perm;
4592 uu_avl_index_t idx = 0;
4594 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4595 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4597 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4599 if (avl == fsperm->fsp_uge_avl) {
4601 struct passwd *p = NULL;
4602 struct group *g = NULL;
4603 const char *nice_name = NULL;
4605 switch (perm_type) {
4606 case ZFS_DELEG_USER_SETS:
4607 case ZFS_DELEG_USER:
4608 rid = atoi(perm_name);
4611 nice_name = p->pw_name;
4613 case ZFS_DELEG_GROUP_SETS:
4614 case ZFS_DELEG_GROUP:
4615 rid = atoi(perm_name);
4618 nice_name = g->gr_name;
4624 if (nice_name != NULL)
4626 node->who_perm.who_ug_name,
4630 uu_avl_insert(avl, node, idx);
4633 who_perm = &node->who_perm;
4636 VERIFY3P(who_perm, !=, NULL);
4637 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4644 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4646 nvpair_t *nvp = NULL;
4647 uu_avl_index_t idx = 0;
4649 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4650 nvlist_t *nvl2 = NULL;
4651 const char *fsname = nvpair_name(nvp);
4652 data_type_t type = nvpair_type(nvp);
4653 fs_perm_t *fsperm = NULL;
4654 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4658 fsperm = &node->fspn_fsperm;
4660 VERIFY(DATA_TYPE_NVLIST == type);
4662 uu_list_node_init(node, &node->fspn_list_node,
4663 fspset->fsps_list_pool);
4665 idx = uu_list_numnodes(fspset->fsps_list);
4666 fs_perm_init(fsperm, fspset, fsname);
4668 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4671 (void) parse_fs_perm(fsperm, nvl2);
4673 uu_list_insert(fspset->fsps_list, node, idx);
4679 static inline const char *
4680 deleg_perm_comment(zfs_deleg_note_t note)
4682 const char *str = "";
4687 case ZFS_DELEG_NOTE_ALLOW:
4688 str = gettext("Must also have the permission that is being"
4689 "\n\t\t\t\tallowed");
4691 case ZFS_DELEG_NOTE_CLONE:
4692 str = gettext("Must also have the 'create' ability and 'mount'"
4693 "\n\t\t\t\tability in the origin file system");
4695 case ZFS_DELEG_NOTE_CREATE:
4696 str = gettext("Must also have the 'mount' ability");
4698 case ZFS_DELEG_NOTE_DESTROY:
4699 str = gettext("Must also have the 'mount' ability");
4701 case ZFS_DELEG_NOTE_DIFF:
4702 str = gettext("Allows lookup of paths within a dataset;"
4703 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4704 "\n\t\t\t\tin order to use zfs diff");
4706 case ZFS_DELEG_NOTE_HOLD:
4707 str = gettext("Allows adding a user hold to a snapshot");
4709 case ZFS_DELEG_NOTE_MOUNT:
4710 str = gettext("Allows mount/umount of ZFS datasets");
4712 case ZFS_DELEG_NOTE_PROMOTE:
4713 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4714 " 'promote' ability in the origin file system");
4716 case ZFS_DELEG_NOTE_RECEIVE:
4717 str = gettext("Must also have the 'mount' and 'create'"
4720 case ZFS_DELEG_NOTE_RELEASE:
4721 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4722 "might destroy the snapshot");
4724 case ZFS_DELEG_NOTE_RENAME:
4725 str = gettext("Must also have the 'mount' and 'create'"
4726 "\n\t\t\t\tability in the new parent");
4728 case ZFS_DELEG_NOTE_ROLLBACK:
4731 case ZFS_DELEG_NOTE_SEND:
4734 case ZFS_DELEG_NOTE_SHARE:
4735 str = gettext("Allows sharing file systems over NFS or SMB"
4736 "\n\t\t\t\tprotocols");
4738 case ZFS_DELEG_NOTE_SNAPSHOT:
4742 * case ZFS_DELEG_NOTE_VSCAN:
4743 * str = gettext("");
4747 case ZFS_DELEG_NOTE_GROUPQUOTA:
4748 str = gettext("Allows accessing any groupquota@... property");
4750 case ZFS_DELEG_NOTE_GROUPUSED:
4751 str = gettext("Allows reading any groupused@... property");
4753 case ZFS_DELEG_NOTE_USERPROP:
4754 str = gettext("Allows changing any user property");
4756 case ZFS_DELEG_NOTE_USERQUOTA:
4757 str = gettext("Allows accessing any userquota@... property");
4759 case ZFS_DELEG_NOTE_USERUSED:
4760 str = gettext("Allows reading any userused@... property");
4762 case ZFS_DELEG_NOTE_USEROBJQUOTA:
4763 str = gettext("Allows accessing any userobjquota@... property");
4765 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
4766 str = gettext("Allows accessing any \n\t\t\t\t"
4767 "groupobjquota@... property");
4769 case ZFS_DELEG_NOTE_GROUPOBJUSED:
4770 str = gettext("Allows reading any groupobjused@... property");
4772 case ZFS_DELEG_NOTE_USEROBJUSED:
4773 str = gettext("Allows reading any userobjused@... property");
4791 boolean_t recursive; /* unallow only */
4792 boolean_t prt_usage;
4794 boolean_t prt_perms;
4797 const char *dataset;
4801 prop_cmp(const void *a, const void *b)
4803 const char *str1 = *(const char **)a;
4804 const char *str2 = *(const char **)b;
4805 return (strcmp(str1, str2));
4809 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4811 const char *opt_desc[] = {
4812 "-h", gettext("show this help message and exit"),
4813 "-l", gettext("set permission locally"),
4814 "-d", gettext("set permission for descents"),
4815 "-u", gettext("set permission for user"),
4816 "-g", gettext("set permission for group"),
4817 "-e", gettext("set permission for everyone"),
4818 "-c", gettext("set create time permission"),
4819 "-s", gettext("define permission set"),
4821 "-r", gettext("remove permissions recursively"),
4823 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4824 size_t allow_size = unallow_size - 2;
4825 const char *props[ZFS_NUM_PROPS];
4828 FILE *fp = requested ? stdout : stderr;
4829 zprop_desc_t *pdtbl = zfs_prop_get_table();
4830 const char *fmt = gettext("%-16s %-14s\t%s\n");
4832 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4834 (void) fprintf(fp, gettext("Options:\n"));
4835 for (i = 0; i < (un ? unallow_size : allow_size); i += 2) {
4836 const char *opt = opt_desc[i];
4837 const char *optdsc = opt_desc[i + 1];
4838 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4841 (void) fprintf(fp, gettext("\nThe following permissions are "
4843 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4845 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4846 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4847 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4848 const char *perm_type = deleg_perm_type(perm_note);
4849 const char *perm_comment = deleg_perm_comment(perm_note);
4850 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4853 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4854 zprop_desc_t *pd = &pdtbl[i];
4855 if (pd->pd_visible != B_TRUE)
4858 if (pd->pd_attr == PROP_READONLY)
4861 props[count++] = pd->pd_name;
4863 props[count] = NULL;
4865 qsort(props, count, sizeof (char *), prop_cmp);
4867 for (i = 0; i < count; i++)
4868 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4871 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4873 exit(requested ? 0 : 2);
4876 static inline const char *
4877 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4880 if (un && argc == expected_argc - 1)
4882 else if (argc == expected_argc)
4883 *permsp = argv[argc - 2];
4885 allow_usage(un, B_FALSE,
4886 gettext("wrong number of parameters\n"));
4888 return (argv[argc - 1]);
4892 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4894 int uge_sum = opts->user + opts->group + opts->everyone;
4895 int csuge_sum = opts->create + opts->set + uge_sum;
4896 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4897 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4900 allow_usage(un, B_FALSE,
4901 gettext("-u, -g, and -e are mutually exclusive\n"));
4903 if (opts->prt_usage) {
4904 if (argc == 0 && all_sum == 0)
4905 allow_usage(un, B_TRUE, NULL);
4912 allow_usage(un, B_FALSE,
4913 gettext("invalid options combined with -s\n"));
4915 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4916 if (argv[0][0] != '@')
4917 allow_usage(un, B_FALSE,
4918 gettext("invalid set name: missing '@' prefix\n"));
4919 opts->who = argv[0];
4920 } else if (opts->create) {
4921 if (ldcsuge_sum > 1)
4922 allow_usage(un, B_FALSE,
4923 gettext("invalid options combined with -c\n"));
4924 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4925 } else if (opts->everyone) {
4927 allow_usage(un, B_FALSE,
4928 gettext("invalid options combined with -e\n"));
4929 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4930 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4932 opts->everyone = B_TRUE;
4935 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4936 } else if (argc == 1 && !un) {
4937 opts->prt_perms = B_TRUE;
4938 opts->dataset = argv[argc-1];
4940 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4941 opts->who = argv[0];
4944 if (!opts->local && !opts->descend) {
4945 opts->local = B_TRUE;
4946 opts->descend = B_TRUE;
4951 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4952 const char *who, char *perms, nvlist_t *top_nvl)
4955 char ld[2] = { '\0', '\0' };
4956 char who_buf[MAXNAMELEN + 32];
4957 char base_type = ZFS_DELEG_WHO_UNKNOWN;
4958 char set_type = ZFS_DELEG_WHO_UNKNOWN;
4959 nvlist_t *base_nvl = NULL;
4960 nvlist_t *set_nvl = NULL;
4963 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4965 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4969 case ZFS_DELEG_NAMED_SET_SETS:
4970 case ZFS_DELEG_NAMED_SET:
4971 set_type = ZFS_DELEG_NAMED_SET_SETS;
4972 base_type = ZFS_DELEG_NAMED_SET;
4973 ld[0] = ZFS_DELEG_NA;
4975 case ZFS_DELEG_CREATE_SETS:
4976 case ZFS_DELEG_CREATE:
4977 set_type = ZFS_DELEG_CREATE_SETS;
4978 base_type = ZFS_DELEG_CREATE;
4979 ld[0] = ZFS_DELEG_NA;
4981 case ZFS_DELEG_USER_SETS:
4982 case ZFS_DELEG_USER:
4983 set_type = ZFS_DELEG_USER_SETS;
4984 base_type = ZFS_DELEG_USER;
4986 ld[0] = ZFS_DELEG_LOCAL;
4988 ld[1] = ZFS_DELEG_DESCENDENT;
4990 case ZFS_DELEG_GROUP_SETS:
4991 case ZFS_DELEG_GROUP:
4992 set_type = ZFS_DELEG_GROUP_SETS;
4993 base_type = ZFS_DELEG_GROUP;
4995 ld[0] = ZFS_DELEG_LOCAL;
4997 ld[1] = ZFS_DELEG_DESCENDENT;
4999 case ZFS_DELEG_EVERYONE_SETS:
5000 case ZFS_DELEG_EVERYONE:
5001 set_type = ZFS_DELEG_EVERYONE_SETS;
5002 base_type = ZFS_DELEG_EVERYONE;
5004 ld[0] = ZFS_DELEG_LOCAL;
5006 ld[1] = ZFS_DELEG_DESCENDENT;
5011 if (perms != NULL) {
5013 char *end = curr + strlen(perms);
5015 while (curr < end) {
5016 char *delim = strchr(curr, ',');
5027 (void) nvlist_add_boolean(nvl, curr);
5033 for (i = 0; i < 2; i++) {
5034 char locality = ld[i];
5038 if (!nvlist_empty(base_nvl)) {
5040 (void) snprintf(who_buf,
5041 sizeof (who_buf), "%c%c$%s",
5042 base_type, locality, who);
5044 (void) snprintf(who_buf,
5045 sizeof (who_buf), "%c%c$",
5046 base_type, locality);
5048 (void) nvlist_add_nvlist(top_nvl, who_buf,
5053 if (!nvlist_empty(set_nvl)) {
5055 (void) snprintf(who_buf,
5056 sizeof (who_buf), "%c%c$%s",
5057 set_type, locality, who);
5059 (void) snprintf(who_buf,
5060 sizeof (who_buf), "%c%c$",
5061 set_type, locality);
5063 (void) nvlist_add_nvlist(top_nvl, who_buf,
5068 for (i = 0; i < 2; i++) {
5069 char locality = ld[i];
5074 (void) snprintf(who_buf, sizeof (who_buf),
5075 "%c%c$%s", base_type, locality, who);
5077 (void) snprintf(who_buf, sizeof (who_buf),
5078 "%c%c$", base_type, locality);
5079 (void) nvlist_add_boolean(top_nvl, who_buf);
5082 (void) snprintf(who_buf, sizeof (who_buf),
5083 "%c%c$%s", set_type, locality, who);
5085 (void) snprintf(who_buf, sizeof (who_buf),
5086 "%c%c$", set_type, locality);
5087 (void) nvlist_add_boolean(top_nvl, who_buf);
5093 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5095 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5099 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5100 opts->descend, opts->who, opts->perms, *nvlp);
5101 } else if (opts->create) {
5102 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5103 opts->descend, NULL, opts->perms, *nvlp);
5104 } else if (opts->everyone) {
5105 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5106 opts->descend, NULL, opts->perms, *nvlp);
5108 char *curr = opts->who;
5109 char *end = curr + strlen(curr);
5111 while (curr < end) {
5113 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5115 char *delim = strchr(curr, ',');
5118 struct passwd *p = NULL;
5119 struct group *g = NULL;
5127 rid = (uid_t)strtol(curr, &endch, 0);
5129 who_type = ZFS_DELEG_USER;
5138 (void) snprintf(errbuf, 256, gettext(
5139 "invalid user %s"), curr);
5140 allow_usage(un, B_TRUE, errbuf);
5142 } else if (opts->group) {
5143 who_type = ZFS_DELEG_GROUP;
5152 (void) snprintf(errbuf, 256, gettext(
5153 "invalid group %s"), curr);
5154 allow_usage(un, B_TRUE, errbuf);
5157 if (*endch != '\0') {
5164 if (*endch != '\0') {
5172 who_type = ZFS_DELEG_USER;
5174 } else if (g != NULL) {
5175 who_type = ZFS_DELEG_GROUP;
5178 (void) snprintf(errbuf, 256, gettext(
5179 "invalid user/group %s"), curr);
5180 allow_usage(un, B_TRUE, errbuf);
5184 (void) sprintf(id, "%u", rid);
5187 store_allow_perm(who_type, opts->local,
5188 opts->descend, who, opts->perms, *nvlp);
5197 print_set_creat_perms(uu_avl_t *who_avl)
5199 const char *sc_title[] = {
5200 gettext("Permission sets:\n"),
5201 gettext("Create time permissions:\n"),
5204 const char **title_ptr = sc_title;
5205 who_perm_node_t *who_node = NULL;
5206 int prev_weight = -1;
5208 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5209 who_node = uu_avl_next(who_avl, who_node)) {
5210 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5211 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5212 const char *who_name = who_node->who_perm.who_name;
5213 int weight = who_type2weight(who_type);
5214 boolean_t first = B_TRUE;
5215 deleg_perm_node_t *deleg_node;
5217 if (prev_weight != weight) {
5218 (void) printf("%s", *title_ptr++);
5219 prev_weight = weight;
5222 if (who_name == NULL || strnlen(who_name, 1) == 0)
5223 (void) printf("\t");
5225 (void) printf("\t%s ", who_name);
5227 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5228 deleg_node = uu_avl_next(avl, deleg_node)) {
5231 deleg_node->dpn_perm.dp_name);
5234 (void) printf(",%s",
5235 deleg_node->dpn_perm.dp_name);
5238 (void) printf("\n");
5243 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5246 who_perm_node_t *who_node = NULL;
5247 boolean_t prt_title = B_TRUE;
5248 uu_avl_walk_t *walk;
5250 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5253 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5254 const char *who_name = who_node->who_perm.who_name;
5255 const char *nice_who_name = who_node->who_perm.who_ug_name;
5256 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5257 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5259 deleg_perm_node_t *deleg_node;
5260 boolean_t prt_who = B_TRUE;
5262 for (deleg_node = uu_avl_first(avl);
5264 deleg_node = uu_avl_next(avl, deleg_node)) {
5265 if (local != deleg_node->dpn_perm.dp_local ||
5266 descend != deleg_node->dpn_perm.dp_descend)
5270 const char *who = NULL;
5272 prt_title = B_FALSE;
5273 (void) printf("%s", title);
5277 case ZFS_DELEG_USER_SETS:
5278 case ZFS_DELEG_USER:
5279 who = gettext("user");
5281 who_name = nice_who_name;
5283 case ZFS_DELEG_GROUP_SETS:
5284 case ZFS_DELEG_GROUP:
5285 who = gettext("group");
5287 who_name = nice_who_name;
5289 case ZFS_DELEG_EVERYONE_SETS:
5290 case ZFS_DELEG_EVERYONE:
5291 who = gettext("everyone");
5298 if (who_name == NULL)
5299 (void) printf("\t%s", who);
5301 (void) printf("\t%s %s", who, who_name);
5304 (void) printf("%c%s", delim,
5305 deleg_node->dpn_perm.dp_name);
5310 (void) printf("\n");
5313 uu_avl_walk_end(walk);
5317 print_fs_perms(fs_perm_set_t *fspset)
5319 fs_perm_node_t *node = NULL;
5320 char buf[MAXNAMELEN + 32];
5321 const char *dsname = buf;
5323 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5324 node = uu_list_next(fspset->fsps_list, node)) {
5325 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5326 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5329 (void) snprintf(buf, sizeof (buf),
5330 gettext("---- Permissions on %s "),
5331 node->fspn_fsperm.fsp_name);
5332 (void) printf("%s", dsname);
5333 left = 70 - strlen(buf);
5336 (void) printf("\n");
5338 print_set_creat_perms(sc_avl);
5339 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5340 gettext("Local permissions:\n"));
5341 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5342 gettext("Descendent permissions:\n"));
5343 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5344 gettext("Local+Descendent permissions:\n"));
5348 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5350 struct deleg_perms {
5356 set_deleg_perms(zfs_handle_t *zhp, void *data)
5358 struct deleg_perms *perms = (struct deleg_perms *)data;
5359 zfs_type_t zfs_type = zfs_get_type(zhp);
5361 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5364 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5368 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5371 nvlist_t *perm_nvl = NULL;
5372 nvlist_t *update_perm_nvl = NULL;
5375 struct allow_opts opts = { 0 };
5377 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5380 while ((c = getopt(argc, argv, optstr)) != -1) {
5383 opts.local = B_TRUE;
5386 opts.descend = B_TRUE;
5392 opts.group = B_TRUE;
5395 opts.everyone = B_TRUE;
5401 opts.create = B_TRUE;
5404 opts.recursive = B_TRUE;
5407 (void) fprintf(stderr, gettext("missing argument for "
5408 "'%c' option\n"), optopt);
5412 opts.prt_usage = B_TRUE;
5415 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5424 /* check arguments */
5425 parse_allow_args(argc, argv, un, &opts);
5427 /* try to open the dataset */
5428 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5429 ZFS_TYPE_VOLUME)) == NULL) {
5430 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5435 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5438 fs_perm_set_init(&fs_perm_set);
5439 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5440 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5445 print_fs_perms(&fs_perm_set);
5447 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5448 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5451 if (un && opts.recursive) {
5452 struct deleg_perms data = { un, update_perm_nvl };
5453 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5462 nvlist_free(perm_nvl);
5463 nvlist_free(update_perm_nvl);
5465 fs_perm_set_fini(&fs_perm_set);
5473 zfs_do_allow(int argc, char **argv)
5475 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5479 zfs_do_unallow(int argc, char **argv)
5481 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5485 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5490 boolean_t recursive = B_FALSE;
5491 const char *opts = holding ? "rt" : "r";
5495 while ((c = getopt(argc, argv, opts)) != -1) {
5501 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5510 /* check number of arguments */
5518 if (holding && tag[0] == '.') {
5519 /* tags starting with '.' are reserved for libzfs */
5520 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5524 for (i = 0; i < argc; ++i) {
5526 char parent[ZFS_MAX_DATASET_NAME_LEN];
5528 char *path = argv[i];
5530 delim = strchr(path, '@');
5531 if (delim == NULL) {
5532 (void) fprintf(stderr,
5533 gettext("'%s' is not a snapshot\n"), path);
5537 (void) strncpy(parent, path, delim - path);
5538 parent[delim - path] = '\0';
5540 zhp = zfs_open(g_zfs, parent,
5541 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5547 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5550 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5556 return (errors != 0);
5560 * zfs hold [-r] [-t] <tag> <snap> ...
5562 * -r Recursively hold
5564 * Apply a user-hold with the given tag to the list of snapshots.
5567 zfs_do_hold(int argc, char **argv)
5569 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5573 * zfs release [-r] <tag> <snap> ...
5575 * -r Recursively release
5577 * Release a user-hold with the given tag from the list of snapshots.
5580 zfs_do_release(int argc, char **argv)
5582 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5585 typedef struct holds_cbdata {
5586 boolean_t cb_recursive;
5587 const char *cb_snapname;
5589 size_t cb_max_namelen;
5590 size_t cb_max_taglen;
5593 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5594 #define DATETIME_BUF_LEN (32)
5599 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
5602 nvpair_t *nvp = NULL;
5603 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5607 for (i = 0; i < 3; i++) {
5608 col = gettext(hdr_cols[i]);
5610 (void) printf("%-*s ", i ? tagwidth : nwidth,
5613 (void) printf("%s\n", col);
5617 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5618 char *zname = nvpair_name(nvp);
5620 nvpair_t *nvp2 = NULL;
5621 (void) nvpair_value_nvlist(nvp, &nvl2);
5622 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5623 char tsbuf[DATETIME_BUF_LEN];
5624 char *tagname = nvpair_name(nvp2);
5628 char sep = scripted ? '\t' : ' ';
5629 int sepnum = scripted ? 1 : 2;
5631 (void) nvpair_value_uint64(nvp2, &val);
5633 (void) localtime_r(&time, &t);
5634 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5635 gettext(STRFTIME_FMT_STR), &t);
5637 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5638 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5644 * Generic callback function to list a dataset or snapshot.
5647 holds_callback(zfs_handle_t *zhp, void *data)
5649 holds_cbdata_t *cbp = data;
5650 nvlist_t *top_nvl = *cbp->cb_nvlp;
5651 nvlist_t *nvl = NULL;
5652 nvpair_t *nvp = NULL;
5653 const char *zname = zfs_get_name(zhp);
5654 size_t znamelen = strlen(zname);
5656 if (cbp->cb_recursive) {
5657 const char *snapname;
5658 char *delim = strchr(zname, '@');
5662 snapname = delim + 1;
5663 if (strcmp(cbp->cb_snapname, snapname))
5667 if (zfs_get_holds(zhp, &nvl) != 0)
5670 if (znamelen > cbp->cb_max_namelen)
5671 cbp->cb_max_namelen = znamelen;
5673 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5674 const char *tag = nvpair_name(nvp);
5675 size_t taglen = strlen(tag);
5676 if (taglen > cbp->cb_max_taglen)
5677 cbp->cb_max_taglen = taglen;
5680 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5684 * zfs holds [-r] <snap> ...
5686 * -r Recursively hold
5689 zfs_do_holds(int argc, char **argv)
5694 boolean_t scripted = B_FALSE;
5695 boolean_t recursive = B_FALSE;
5696 const char *opts = "rH";
5699 int types = ZFS_TYPE_SNAPSHOT;
5700 holds_cbdata_t cb = { 0 };
5707 while ((c = getopt(argc, argv, opts)) != -1) {
5716 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5723 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5724 flags |= ZFS_ITER_RECURSE;
5730 /* check number of arguments */
5734 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5737 for (i = 0; i < argc; ++i) {
5738 char *snapshot = argv[i];
5740 const char *snapname;
5742 delim = strchr(snapshot, '@');
5743 if (delim == NULL) {
5744 (void) fprintf(stderr,
5745 gettext("'%s' is not a snapshot\n"), snapshot);
5749 snapname = delim + 1;
5751 snapshot[delim - snapshot] = '\0';
5753 cb.cb_recursive = recursive;
5754 cb.cb_snapname = snapname;
5758 * 1. collect holds data, set format options
5760 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5761 holds_callback, &cb);
5767 * 2. print holds data
5769 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5771 if (nvlist_empty(nvl))
5772 (void) fprintf(stderr, gettext("no datasets available\n"));
5776 return (0 != errors);
5779 #define CHECK_SPINNER 30
5780 #define SPINNER_TIME 3 /* seconds */
5781 #define MOUNT_TIME 5 /* seconds */
5784 get_one_dataset(zfs_handle_t *zhp, void *data)
5786 static char *spin[] = { "-", "\\", "|", "/" };
5787 static int spinval = 0;
5788 static int spincheck = 0;
5789 static time_t last_spin_time = (time_t)0;
5790 get_all_cb_t *cbp = data;
5791 zfs_type_t type = zfs_get_type(zhp);
5793 if (cbp->cb_verbose) {
5794 if (--spincheck < 0) {
5795 time_t now = time(NULL);
5796 if (last_spin_time + SPINNER_TIME < now) {
5797 update_progress(spin[spinval++ % 4]);
5798 last_spin_time = now;
5800 spincheck = CHECK_SPINNER;
5805 * Iterate over any nested datasets.
5807 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5813 * Skip any datasets whose type does not match.
5815 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5819 libzfs_add_handle(cbp, zhp);
5820 assert(cbp->cb_used <= cbp->cb_alloc);
5826 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5828 get_all_cb_t cb = { 0 };
5829 cb.cb_verbose = verbose;
5830 cb.cb_getone = get_one_dataset;
5833 set_progress_header(gettext("Reading ZFS config"));
5834 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5836 *dslist = cb.cb_handles;
5837 *count = cb.cb_used;
5840 finish_progress(gettext("done."));
5844 * Generic callback for sharing or mounting filesystems. Because the code is so
5845 * similar, we have a common function with an extra parameter to determine which
5846 * mode we are using.
5848 #define OP_SHARE 0x1
5849 #define OP_MOUNT 0x2
5852 * Share or mount a dataset.
5855 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5856 boolean_t explicit, const char *options)
5858 char mountpoint[ZFS_MAXPROPLEN];
5859 char shareopts[ZFS_MAXPROPLEN];
5860 char smbshareopts[ZFS_MAXPROPLEN];
5861 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5863 uint64_t zoned, canmount;
5864 boolean_t shared_nfs, shared_smb;
5866 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5869 * Check to make sure we can mount/share this dataset. If we
5870 * are in the global zone and the filesystem is exported to a
5871 * local zone, or if we are in a local zone and the
5872 * filesystem is not exported, then it is an error.
5874 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5876 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5880 (void) fprintf(stderr, gettext("cannot %s '%s': "
5881 "dataset is exported to a local zone\n"), cmdname,
5885 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5889 (void) fprintf(stderr, gettext("cannot %s '%s': "
5890 "permission denied\n"), cmdname,
5896 * Ignore any filesystems which don't apply to us. This
5897 * includes those with a legacy mountpoint, or those with
5898 * legacy share options.
5900 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5901 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5902 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5903 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5904 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5905 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5907 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5908 strcmp(smbshareopts, "off") == 0) {
5912 (void) fprintf(stderr, gettext("cannot share '%s': "
5913 "legacy share\n"), zfs_get_name(zhp));
5914 (void) fprintf(stderr, gettext("use share(1M) to "
5915 "share this filesystem, or set "
5916 "sharenfs property on\n"));
5921 * We cannot share or mount legacy filesystems. If the
5922 * shareopts is non-legacy but the mountpoint is legacy, we
5923 * treat it as a legacy share.
5925 if (strcmp(mountpoint, "legacy") == 0) {
5929 (void) fprintf(stderr, gettext("cannot %s '%s': "
5930 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5931 (void) fprintf(stderr, gettext("use %s(1M) to "
5932 "%s this filesystem\n"), cmdname, cmdname);
5936 if (strcmp(mountpoint, "none") == 0) {
5940 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5941 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5946 * canmount explicit outcome
5947 * on no pass through
5948 * on yes pass through
5950 * off yes display error, return 1
5951 * noauto no return 0
5952 * noauto yes pass through
5954 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5955 if (canmount == ZFS_CANMOUNT_OFF) {
5959 (void) fprintf(stderr, gettext("cannot %s '%s': "
5960 "'canmount' property is set to 'off'\n"), cmdname,
5963 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5968 * If this filesystem is inconsistent and has a receive resume
5969 * token, we can not mount it.
5971 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
5972 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
5973 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
5977 (void) fprintf(stderr, gettext("cannot %s '%s': "
5978 "Contains partially-completed state from "
5979 "\"zfs receive -r\", which can be resumed with "
5980 "\"zfs send -t\"\n"),
5981 cmdname, zfs_get_name(zhp));
5986 * At this point, we have verified that the mountpoint and/or
5987 * shareopts are appropriate for auto management. If the
5988 * filesystem is already mounted or shared, return (failing
5989 * for explicit requests); otherwise mount or share the
5995 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5996 shared_smb = zfs_is_shared_smb(zhp, NULL);
5998 if ((shared_nfs && shared_smb) ||
5999 ((shared_nfs && strcmp(shareopts, "on") == 0) &&
6000 (strcmp(smbshareopts, "off") == 0)) ||
6001 ((shared_smb && strcmp(smbshareopts, "on") == 0) &&
6002 (strcmp(shareopts, "off") == 0))) {
6006 (void) fprintf(stderr, gettext("cannot share "
6007 "'%s': filesystem already shared\n"),
6012 if (!zfs_is_mounted(zhp, NULL) &&
6013 zfs_mount(zhp, NULL, 0) != 0)
6016 if (protocol == NULL) {
6017 if (zfs_shareall(zhp) != 0)
6019 } else if (strcmp(protocol, "nfs") == 0) {
6020 if (zfs_share_nfs(zhp))
6022 } else if (strcmp(protocol, "smb") == 0) {
6023 if (zfs_share_smb(zhp))
6026 (void) fprintf(stderr, gettext("cannot share "
6027 "'%s': invalid share type '%s' "
6029 zfs_get_name(zhp), protocol);
6036 if (options == NULL)
6037 mnt.mnt_mntopts = "";
6039 mnt.mnt_mntopts = (char *)options;
6041 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6042 zfs_is_mounted(zhp, NULL)) {
6046 (void) fprintf(stderr, gettext("cannot mount "
6047 "'%s': filesystem already mounted\n"),
6052 if (zfs_mount(zhp, options, flags) != 0)
6061 * Reports progress in the form "(current/total)". Not thread-safe.
6064 report_mount_progress(int current, int total)
6066 static time_t last_progress_time = 0;
6067 time_t now = time(NULL);
6070 /* report 1..n instead of 0..n-1 */
6073 /* display header if we're here for the first time */
6075 set_progress_header(gettext("Mounting ZFS filesystems"));
6076 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6077 /* too soon to report again */
6081 last_progress_time = now;
6083 (void) sprintf(info, "(%d/%d)", current, total);
6085 if (current == total)
6086 finish_progress(info);
6088 update_progress(info);
6092 append_options(char *mntopts, char *newopts)
6094 int len = strlen(mntopts);
6096 /* original length plus new string to append plus 1 for the comma */
6097 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6098 (void) fprintf(stderr, gettext("the opts argument for "
6099 "'%s' option is too long (more than %d chars)\n"),
6100 "-o", MNT_LINE_MAX);
6105 mntopts[len++] = ',';
6107 (void) strcpy(&mntopts[len], newopts);
6111 share_mount(int op, int argc, char **argv)
6114 boolean_t verbose = B_FALSE;
6116 char *options = NULL;
6120 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6130 if (*optarg == '\0') {
6131 (void) fprintf(stderr, gettext("empty mount "
6132 "options (-o) specified\n"));
6136 if (options == NULL)
6137 options = safe_malloc(MNT_LINE_MAX + 1);
6139 /* option validation is done later */
6140 append_options(options, optarg);
6143 flags |= MS_OVERLAY;
6146 (void) fprintf(stderr, gettext("missing argument for "
6147 "'%c' option\n"), optopt);
6151 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6160 /* check number of arguments */
6162 zfs_handle_t **dslist = NULL;
6163 size_t i, count = 0;
6164 char *protocol = NULL;
6166 if (op == OP_SHARE && argc > 0) {
6167 if (strcmp(argv[0], "nfs") != 0 &&
6168 strcmp(argv[0], "smb") != 0) {
6169 (void) fprintf(stderr, gettext("share type "
6170 "must be 'nfs' or 'smb'\n"));
6179 (void) fprintf(stderr, gettext("too many arguments\n"));
6183 start_progress_timer();
6184 get_all_datasets(&dslist, &count, verbose);
6187 if (options != NULL)
6192 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6194 for (i = 0; i < count; i++) {
6196 report_mount_progress(i, count);
6198 if (share_mount_one(dslist[i], op, flags, protocol,
6199 B_FALSE, options) != 0)
6201 zfs_close(dslist[i]);
6205 } else if (argc == 0) {
6206 struct mnttab entry;
6208 if ((op == OP_SHARE) || (options != NULL)) {
6209 (void) fprintf(stderr, gettext("missing filesystem "
6210 "argument (specify -a for all)\n"));
6215 * When mount is given no arguments, go through
6216 * /proc/self/mounts and display any active ZFS mounts.
6217 * We hide any snapshots, since they are controlled
6221 /* Reopen MNTTAB to prevent reading stale data from open file */
6222 if (freopen(MNTTAB, "r", mnttab_file) == NULL) {
6223 if (options != NULL)
6228 while (getmntent(mnttab_file, &entry) == 0) {
6229 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6230 strchr(entry.mnt_special, '@') != NULL)
6233 (void) printf("%-30s %s\n", entry.mnt_special,
6241 (void) fprintf(stderr,
6242 gettext("too many arguments\n"));
6246 if ((zhp = zfs_open(g_zfs, argv[0],
6247 ZFS_TYPE_FILESYSTEM)) == NULL) {
6250 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6256 if (options != NULL)
6263 * zfs mount -a [nfs]
6264 * zfs mount filesystem
6266 * Mount all filesystems, or mount the given filesystem.
6269 zfs_do_mount(int argc, char **argv)
6271 return (share_mount(OP_MOUNT, argc, argv));
6275 * zfs share -a [nfs | smb]
6276 * zfs share filesystem
6278 * Share all filesystems, or share the given filesystem.
6281 zfs_do_share(int argc, char **argv)
6283 return (share_mount(OP_SHARE, argc, argv));
6286 typedef struct unshare_unmount_node {
6287 zfs_handle_t *un_zhp;
6289 uu_avl_node_t un_avlnode;
6290 } unshare_unmount_node_t;
6294 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6296 const unshare_unmount_node_t *l = larg;
6297 const unshare_unmount_node_t *r = rarg;
6299 return (strcmp(l->un_mountp, r->un_mountp));
6303 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6304 * absolute path, find the entry /proc/self/mounts, verify that its a
6305 * ZFS filesystems, and unmount it appropriately.
6308 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6312 struct stat64 statbuf;
6313 struct extmnttab entry;
6314 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6318 * Search for the path in /proc/self/mounts. Rather than looking for the
6319 * specific path, which can be fooled by non-standard paths (i.e. ".."
6320 * or "//"), we stat() the path and search for the corresponding
6321 * (major,minor) device pair.
6323 if (stat64(path, &statbuf) != 0) {
6324 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6325 cmdname, path, strerror(errno));
6328 path_inode = statbuf.st_ino;
6331 * Search for the given (major,minor) pair in the mount table.
6334 /* Reopen MNTTAB to prevent reading stale data from open file */
6335 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6338 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6339 if (entry.mnt_major == major(statbuf.st_dev) &&
6340 entry.mnt_minor == minor(statbuf.st_dev))
6344 if (op == OP_SHARE) {
6345 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6346 "currently mounted\n"), cmdname, path);
6349 (void) fprintf(stderr, gettext("warning: %s not in"
6350 "/proc/self/mounts\n"), path);
6351 if ((ret = umount2(path, flags)) != 0)
6352 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6357 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6358 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6359 "filesystem\n"), cmdname, path);
6363 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6364 ZFS_TYPE_FILESYSTEM)) == NULL)
6368 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6369 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6370 cmdname, path, strerror(errno));
6372 } else if (statbuf.st_ino != path_inode) {
6373 (void) fprintf(stderr, gettext("cannot "
6374 "%s '%s': not a mountpoint\n"), cmdname, path);
6378 if (op == OP_SHARE) {
6379 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6380 char smbshare_prop[ZFS_MAXPROPLEN];
6382 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6383 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6384 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6385 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6387 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6388 strcmp(smbshare_prop, "off") == 0) {
6389 (void) fprintf(stderr, gettext("cannot unshare "
6390 "'%s': legacy share\n"), path);
6391 (void) fprintf(stderr, gettext("use exportfs(8) "
6392 "or smbcontrol(1) to unshare this filesystem\n"));
6393 } else if (!zfs_is_shared(zhp)) {
6394 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6395 "not currently shared\n"), path);
6397 ret = zfs_unshareall_bypath(zhp, path);
6400 char mtpt_prop[ZFS_MAXPROPLEN];
6402 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6403 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6406 ret = zfs_unmount(zhp, NULL, flags);
6407 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6408 (void) fprintf(stderr, gettext("cannot unmount "
6409 "'%s': legacy mountpoint\n"),
6411 (void) fprintf(stderr, gettext("use umount(8) "
6412 "to unmount this filesystem\n"));
6414 ret = zfs_unmountall(zhp, flags);
6425 * Generic callback for unsharing or unmounting a filesystem.
6428 unshare_unmount(int op, int argc, char **argv)
6435 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6436 char sharesmb[ZFS_MAXPROPLEN];
6439 while ((c = getopt(argc, argv, op == OP_SHARE ? ":a" : "af")) != -1) {
6448 (void) fprintf(stderr, gettext("missing argument for "
6449 "'%c' option\n"), optopt);
6453 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6464 * We could make use of zfs_for_each() to walk all datasets in
6465 * the system, but this would be very inefficient, especially
6466 * since we would have to linearly search /proc/self/mounts for
6467 * each one. Instead, do one pass through /proc/self/mounts
6468 * looking for zfs entries and call zfs_unmount() for each one.
6470 * Things get a little tricky if the administrator has created
6471 * mountpoints beneath other ZFS filesystems. In this case, we
6472 * have to unmount the deepest filesystems first. To accomplish
6473 * this, we place all the mountpoints in an AVL tree sorted by
6474 * the special type (dataset name), and walk the result in
6475 * reverse to make sure to get any snapshots first.
6477 struct mnttab entry;
6478 uu_avl_pool_t *pool;
6479 uu_avl_t *tree = NULL;
6480 unshare_unmount_node_t *node;
6482 uu_avl_walk_t *walk;
6483 char *protocol = NULL;
6485 if (op == OP_SHARE && argc > 0) {
6486 if (strcmp(argv[0], "nfs") != 0 &&
6487 strcmp(argv[0], "smb") != 0) {
6488 (void) fprintf(stderr, gettext("share type "
6489 "must be 'nfs' or 'smb'\n"));
6498 (void) fprintf(stderr, gettext("too many arguments\n"));
6502 if (((pool = uu_avl_pool_create("unmount_pool",
6503 sizeof (unshare_unmount_node_t),
6504 offsetof(unshare_unmount_node_t, un_avlnode),
6505 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6506 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6509 /* Reopen MNTTAB to prevent reading stale data from open file */
6510 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
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) {
6531 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6533 sizeof (nfs_mnt_prop),
6534 NULL, NULL, 0, B_FALSE) == 0);
6535 if (strcmp(nfs_mnt_prop, "off") != 0)
6537 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6539 sizeof (nfs_mnt_prop),
6540 NULL, NULL, 0, B_FALSE) == 0);
6541 if (strcmp(nfs_mnt_prop, "off") == 0)
6545 /* Ignore legacy mounts */
6546 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6548 sizeof (nfs_mnt_prop),
6549 NULL, NULL, 0, B_FALSE) == 0);
6550 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6552 /* Ignore canmount=noauto mounts */
6553 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6554 ZFS_CANMOUNT_NOAUTO)
6560 node = safe_malloc(sizeof (unshare_unmount_node_t));
6562 node->un_mountp = safe_strdup(entry.mnt_mountp);
6564 uu_avl_node_init(node, &node->un_avlnode, pool);
6566 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6567 uu_avl_insert(tree, node, idx);
6569 zfs_close(node->un_zhp);
6570 free(node->un_mountp);
6576 * Walk the AVL tree in reverse, unmounting each filesystem and
6577 * removing it from the AVL tree in the process.
6579 if ((walk = uu_avl_walk_start(tree,
6580 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6583 while ((node = uu_avl_walk_next(walk)) != NULL) {
6584 uu_avl_remove(tree, node);
6588 if (zfs_unshareall_bytype(node->un_zhp,
6589 node->un_mountp, protocol) != 0)
6594 if (zfs_unmount(node->un_zhp,
6595 node->un_mountp, flags) != 0)
6600 zfs_close(node->un_zhp);
6601 free(node->un_mountp);
6605 uu_avl_walk_end(walk);
6606 uu_avl_destroy(tree);
6607 uu_avl_pool_destroy(pool);
6612 (void) fprintf(stderr,
6613 gettext("missing filesystem argument\n"));
6615 (void) fprintf(stderr,
6616 gettext("too many arguments\n"));
6621 * We have an argument, but it may be a full path or a ZFS
6622 * filesystem. Pass full paths off to unmount_path() (shared by
6623 * manual_unmount), otherwise open the filesystem and pass to
6626 if (argv[0][0] == '/')
6627 return (unshare_unmount_path(op, argv[0],
6630 if ((zhp = zfs_open(g_zfs, argv[0],
6631 ZFS_TYPE_FILESYSTEM)) == NULL)
6634 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6635 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6636 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6637 NULL, 0, B_FALSE) == 0);
6641 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6643 sizeof (nfs_mnt_prop),
6644 NULL, NULL, 0, B_FALSE) == 0);
6645 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6646 sharesmb, sizeof (sharesmb), NULL, NULL,
6649 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6650 strcmp(sharesmb, "off") == 0) {
6651 (void) fprintf(stderr, gettext("cannot "
6652 "unshare '%s': legacy share\n"),
6654 (void) fprintf(stderr, gettext("use "
6655 "unshare(1M) to unshare this "
6658 } else if (!zfs_is_shared(zhp)) {
6659 (void) fprintf(stderr, gettext("cannot "
6660 "unshare '%s': not currently "
6661 "shared\n"), zfs_get_name(zhp));
6663 } else if (zfs_unshareall(zhp) != 0) {
6669 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6670 (void) fprintf(stderr, gettext("cannot "
6671 "unmount '%s': legacy "
6672 "mountpoint\n"), zfs_get_name(zhp));
6673 (void) fprintf(stderr, gettext("use "
6674 "umount(1M) to unmount this "
6677 } else if (!zfs_is_mounted(zhp, NULL)) {
6678 (void) fprintf(stderr, gettext("cannot "
6679 "unmount '%s': not currently "
6683 } else if (zfs_unmountall(zhp, flags) != 0) {
6697 * zfs unmount filesystem
6699 * Unmount all filesystems, or a specific ZFS filesystem.
6702 zfs_do_unmount(int argc, char **argv)
6704 return (unshare_unmount(OP_MOUNT, argc, argv));
6709 * zfs unshare filesystem
6711 * Unshare all filesystems, or a specific ZFS filesystem.
6714 zfs_do_unshare(int argc, char **argv)
6716 return (unshare_unmount(OP_SHARE, argc, argv));
6720 find_command_idx(char *command, int *idx)
6724 for (i = 0; i < NCOMMAND; i++) {
6725 if (command_table[i].name == NULL)
6728 if (strcmp(command, command_table[i].name) == 0) {
6737 zfs_do_diff(int argc, char **argv)
6741 char *tosnap = NULL;
6742 char *fromsnap = NULL;
6747 while ((c = getopt(argc, argv, "FHt")) != -1) {
6750 flags |= ZFS_DIFF_CLASSIFY;
6753 flags |= ZFS_DIFF_PARSEABLE;
6756 flags |= ZFS_DIFF_TIMESTAMP;
6759 (void) fprintf(stderr,
6760 gettext("invalid option '%c'\n"), optopt);
6769 (void) fprintf(stderr,
6770 gettext("must provide at least one snapshot name\n"));
6775 (void) fprintf(stderr, gettext("too many arguments\n"));
6780 tosnap = (argc == 2) ? argv[1] : NULL;
6783 if (*fromsnap != '@')
6784 copy = strdup(fromsnap);
6786 copy = strdup(tosnap);
6790 if ((atp = strchr(copy, '@')))
6793 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) {
6800 * Ignore SIGPIPE so that the library can give us
6801 * information on any failure
6803 (void) sigignore(SIGPIPE);
6805 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6813 * zfs bookmark <fs@snap> <fs#bmark>
6815 * Creates a bookmark with the given name from the given snapshot.
6818 zfs_do_bookmark(int argc, char **argv)
6820 char snapname[ZFS_MAX_DATASET_NAME_LEN];
6827 while ((c = getopt(argc, argv, "")) != -1) {
6830 (void) fprintf(stderr,
6831 gettext("invalid option '%c'\n"), optopt);
6839 /* check number of arguments */
6841 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6845 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6849 if (strchr(argv[1], '#') == NULL) {
6850 (void) fprintf(stderr,
6851 gettext("invalid bookmark name '%s' -- "
6852 "must contain a '#'\n"), argv[1]);
6856 if (argv[0][0] == '@') {
6858 * Snapshot name begins with @.
6859 * Default to same fs as bookmark.
6861 (void) strlcpy(snapname, argv[1], sizeof (snapname));
6862 *strchr(snapname, '#') = '\0';
6863 (void) strlcat(snapname, argv[0], sizeof (snapname));
6865 (void) strlcpy(snapname, argv[0], sizeof (snapname));
6867 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6873 nvl = fnvlist_alloc();
6874 fnvlist_add_string(nvl, argv[1], snapname);
6875 ret = lzc_bookmark(nvl, NULL);
6879 const char *err_msg;
6882 (void) snprintf(errbuf, sizeof (errbuf),
6883 dgettext(TEXT_DOMAIN,
6884 "cannot create bookmark '%s'"), argv[1]);
6888 err_msg = "bookmark is in a different pool";
6891 err_msg = "bookmark exists";
6894 err_msg = "invalid argument";
6897 err_msg = "bookmark feature not enabled";
6900 err_msg = "out of space";
6903 err_msg = "unknown error";
6906 (void) fprintf(stderr, "%s: %s\n", errbuf,
6907 dgettext(TEXT_DOMAIN, err_msg));
6918 main(int argc, char **argv)
6924 (void) setlocale(LC_ALL, "");
6925 (void) textdomain(TEXT_DOMAIN);
6927 dprintf_setup(&argc, argv);
6932 * Make sure the user has specified some command.
6935 (void) fprintf(stderr, gettext("missing command\n"));
6942 * The 'umount' command is an alias for 'unmount'
6944 if (strcmp(cmdname, "umount") == 0)
6945 cmdname = "unmount";
6948 * The 'recv' command is an alias for 'receive'
6950 if (strcmp(cmdname, "recv") == 0)
6951 cmdname = "receive";
6954 * The 'snap' command is an alias for 'snapshot'
6956 if (strcmp(cmdname, "snap") == 0)
6957 cmdname = "snapshot";
6962 if ((strcmp(cmdname, "-?") == 0) ||
6963 (strcmp(cmdname, "--help") == 0))
6966 if ((g_zfs = libzfs_init()) == NULL) {
6967 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
6971 mnttab_file = g_zfs->libzfs_mnttab;
6973 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6975 libzfs_print_on_error(g_zfs, B_TRUE);
6978 * Run the appropriate command.
6980 libzfs_mnttab_cache(g_zfs, B_TRUE);
6981 if (find_command_idx(cmdname, &i) == 0) {
6982 current_command = &command_table[i];
6983 ret = command_table[i].func(argc - 1, argv + 1);
6984 } else if (strchr(cmdname, '=') != NULL) {
6985 verify(find_command_idx("set", &i) == 0);
6986 current_command = &command_table[i];
6987 ret = command_table[i].func(argc, argv);
6989 (void) fprintf(stderr, gettext("unrecognized "
6990 "command '%s'\n"), cmdname);
6995 if (ret == 0 && log_history)
6996 (void) zpool_log_history(g_zfs, history_str);
7001 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7002 * for the purposes of running ::findleaks.
7004 if (getenv("ZFS_ABORT") != NULL) {
7005 (void) printf("dumping core by request\n");