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 [-vnFu] <filesystem|volume|"
253 "\treceive [-vnFu] [-o origin=<snapshot>] [-d | -e] "
256 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
257 "<filesystem|volume|snapshot>\n"
258 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
259 "\trename -r <snapshot> <snapshot>\n"));
261 return (gettext("\trollback [-rRf] <snapshot>\n"));
263 return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] "
265 "\tsend [-Le] [-i snapshot|bookmark] "
266 "<filesystem|volume|snapshot>\n"));
268 return (gettext("\tset <property=value> ... "
269 "<filesystem|volume|snapshot> ...\n"));
271 return (gettext("\tshare <-a | filesystem>\n"));
273 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
274 "<filesystem|volume>@<snap> ...\n"));
276 return (gettext("\tunmount [-f] "
277 "<-a | filesystem|mountpoint>\n"));
279 return (gettext("\tunshare "
280 "<-a | filesystem|mountpoint>\n"));
282 return (gettext("\tallow <filesystem|volume>\n"
284 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
285 "\t <filesystem|volume>\n"
286 "\tallow [-ld] -e <perm|@setname>[,...] "
287 "<filesystem|volume>\n"
288 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
289 "\tallow -s @setname <perm|@setname>[,...] "
290 "<filesystem|volume>\n"));
292 return (gettext("\tunallow [-rldug] "
293 "<\"everyone\"|user|group>[,...]\n"
294 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
295 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
296 "<filesystem|volume>\n"
297 "\tunallow [-r] -c [<perm|@setname>[,...]] "
298 "<filesystem|volume>\n"
299 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
300 "<filesystem|volume>\n"));
302 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
304 "\t [-S field] ... [-t type[,...]] "
305 "<filesystem|snapshot>\n"));
306 case HELP_GROUPSPACE:
307 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
309 "\t [-S field] ... [-t type[,...]] "
310 "<filesystem|snapshot>\n"));
312 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
314 return (gettext("\tholds [-r] <snapshot> ...\n"));
316 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
318 return (gettext("\tdiff [-FHt] <snapshot> "
319 "[snapshot|filesystem]\n"));
321 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
331 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
336 * Utility function to guarantee malloc() success.
340 safe_malloc(size_t size)
344 if ((data = calloc(1, size)) == NULL)
351 safe_strdup(char *str)
353 char *dupstr = strdup(str);
362 * Callback routine that will print out information for each of
366 usage_prop_cb(int prop, void *cb)
370 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
372 if (zfs_prop_readonly(prop))
373 (void) fprintf(fp, " NO ");
375 (void) fprintf(fp, "YES ");
377 if (zfs_prop_inheritable(prop))
378 (void) fprintf(fp, " YES ");
380 (void) fprintf(fp, " NO ");
382 if (zfs_prop_values(prop) == NULL)
383 (void) fprintf(fp, "-\n");
385 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
391 * Display usage message. If we're inside a command, display only the usage for
392 * that command. Otherwise, iterate over the entire command table and display
393 * a complete usage message.
396 usage(boolean_t requested)
399 boolean_t show_properties = B_FALSE;
400 FILE *fp = requested ? stdout : stderr;
402 if (current_command == NULL) {
404 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
406 gettext("where 'command' is one of the following:\n\n"));
408 for (i = 0; i < NCOMMAND; i++) {
409 if (command_table[i].name == NULL)
410 (void) fprintf(fp, "\n");
412 (void) fprintf(fp, "%s",
413 get_usage(command_table[i].usage));
416 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
417 "pool/[dataset/]*dataset[@name]\n"));
419 (void) fprintf(fp, gettext("usage:\n"));
420 (void) fprintf(fp, "%s", get_usage(current_command->usage));
423 if (current_command != NULL &&
424 (strcmp(current_command->name, "set") == 0 ||
425 strcmp(current_command->name, "get") == 0 ||
426 strcmp(current_command->name, "inherit") == 0 ||
427 strcmp(current_command->name, "list") == 0))
428 show_properties = B_TRUE;
430 if (show_properties) {
432 gettext("\nThe following properties are supported:\n"));
434 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
435 "PROPERTY", "EDIT", "INHERIT", "VALUES");
437 /* Iterate over all properties */
438 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
441 (void) fprintf(fp, "\t%-15s ", "userused@...");
442 (void) fprintf(fp, " NO NO <size>\n");
443 (void) fprintf(fp, "\t%-15s ", "groupused@...");
444 (void) fprintf(fp, " NO NO <size>\n");
445 (void) fprintf(fp, "\t%-15s ", "userquota@...");
446 (void) fprintf(fp, "YES NO <size> | none\n");
447 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
448 (void) fprintf(fp, "YES NO <size> | none\n");
449 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
450 (void) fprintf(fp, " NO NO <size>\n");
452 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
453 "with standard units such as K, M, G, etc.\n"));
454 (void) fprintf(fp, gettext("\nUser-defined properties can "
455 "be specified by using a name containing a colon (:).\n"));
456 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
457 "properties must be appended with\n"
458 "a user or group specifier of one of these forms:\n"
459 " POSIX name (eg: \"matt\")\n"
460 " POSIX id (eg: \"126829\")\n"
461 " SMB name@domain (eg: \"matt@sun\")\n"
462 " SMB SID (eg: \"S-1-234-567-89\")\n"));
465 gettext("\nFor the property list, run: %s\n"),
468 gettext("\nFor the delegated permission list, run: %s\n"),
469 "zfs allow|unallow");
473 * See comments at end of main().
475 if (getenv("ZFS_ABORT") != NULL) {
476 (void) printf("dumping core by request\n");
480 exit(requested ? 0 : 2);
484 * Take a property=value argument string and add it to the given nvlist.
485 * Modifies the argument inplace.
488 parseprop(nvlist_t *props, char *propname)
490 char *propval, *strval;
492 if ((propval = strchr(propname, '=')) == NULL) {
493 (void) fprintf(stderr, gettext("missing "
494 "'=' for property=value argument\n"));
499 if (nvlist_lookup_string(props, propname, &strval) == 0) {
500 (void) fprintf(stderr, gettext("property '%s' "
501 "specified multiple times\n"), propname);
504 if (nvlist_add_string(props, propname, propval) != 0)
510 parse_depth(char *opt, int *flags)
515 depth = (int)strtol(opt, &tmp, 0);
517 (void) fprintf(stderr,
518 gettext("%s is not an integer\n"), optarg);
522 (void) fprintf(stderr,
523 gettext("Depth can not be negative.\n"));
526 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
530 #define PROGRESS_DELAY 2 /* seconds */
532 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";
533 static time_t pt_begin;
534 static char *pt_header = NULL;
535 static boolean_t pt_shown;
538 start_progress_timer(void)
540 pt_begin = time(NULL) + PROGRESS_DELAY;
545 set_progress_header(char *header)
547 assert(pt_header == NULL);
548 pt_header = safe_strdup(header);
550 (void) printf("%s: ", header);
551 (void) fflush(stdout);
556 update_progress(char *update)
558 if (!pt_shown && time(NULL) > pt_begin) {
559 int len = strlen(update);
561 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
563 (void) fflush(stdout);
565 } else if (pt_shown) {
566 int len = strlen(update);
568 (void) printf("%s%*.*s", update, len, len, pt_reverse);
569 (void) fflush(stdout);
574 finish_progress(char *done)
577 (void) printf("%s\n", done);
578 (void) fflush(stdout);
585 zfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type)
587 zfs_handle_t *zhp = NULL;
590 zhp = zfs_open(hdl, dataset, type);
595 * Volumes may neither be mounted or shared. Potentially in the
596 * future filesystems detected on these volumes could be mounted.
598 if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
604 * Mount and/or share the new filesystem as appropriate. We provide a
605 * verbose error message to let the user know that their filesystem was
606 * in fact created, even if we failed to mount or share it.
608 * If the user doesn't want the dataset automatically mounted, then
609 * skip the mount/share step
611 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type, B_FALSE) &&
612 zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON) {
613 if (zfs_mount(zhp, NULL, 0) != 0) {
614 (void) fprintf(stderr, gettext("filesystem "
615 "successfully created, but not mounted\n"));
617 } else if (zfs_share(zhp) != 0) {
618 (void) fprintf(stderr, gettext("filesystem "
619 "successfully created, but not shared\n"));
630 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
632 * Given an existing dataset, create a writable copy whose initial contents
633 * are the same as the source. The newly created dataset maintains a
634 * dependency on the original; the original cannot be destroyed so long as
637 * The '-p' flag creates all the non-existing ancestors of the target first.
640 zfs_do_clone(int argc, char **argv)
642 zfs_handle_t *zhp = NULL;
643 boolean_t parents = B_FALSE;
648 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
652 while ((c = getopt(argc, argv, "o:p")) != -1) {
655 if (parseprop(props, optarg) != 0)
662 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
671 /* check number of arguments */
673 (void) fprintf(stderr, gettext("missing source dataset "
678 (void) fprintf(stderr, gettext("missing target dataset "
683 (void) fprintf(stderr, gettext("too many arguments\n"));
687 /* open the source dataset */
688 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
691 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
694 * Now create the ancestors of the target dataset. If the
695 * target already exists and '-p' option was used we should not
698 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
701 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
706 ret = zfs_clone(zhp, argv[1], props);
708 /* create the mountpoint if necessary */
711 (void) zpool_log_history(g_zfs, history_str);
712 log_history = B_FALSE;
715 ret = zfs_mount_and_share(g_zfs, argv[1], ZFS_TYPE_DATASET);
732 * zfs create [-p] [-o prop=value] ... fs
733 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
735 * Create a new dataset. This command can be used to create filesystems
736 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
737 * For volumes, the user must specify a size to be used.
739 * The '-s' flag applies only to volumes, and indicates that we should not try
740 * to set the reservation for this volume. By default we set a reservation
741 * equal to the size for any volume. For pools with SPA_VERSION >=
742 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
744 * The '-p' flag creates all the non-existing ancestors of the target first.
747 zfs_do_create(int argc, char **argv)
749 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
750 uint64_t volsize = 0;
752 boolean_t noreserve = B_FALSE;
753 boolean_t bflag = B_FALSE;
754 boolean_t parents = B_FALSE;
759 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
763 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
766 type = ZFS_TYPE_VOLUME;
767 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
768 (void) fprintf(stderr, gettext("bad volume "
769 "size '%s': %s\n"), optarg,
770 libzfs_error_description(g_zfs));
774 if (nvlist_add_uint64(props,
775 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
784 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
785 (void) fprintf(stderr, gettext("bad volume "
786 "block size '%s': %s\n"), optarg,
787 libzfs_error_description(g_zfs));
791 if (nvlist_add_uint64(props,
792 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
797 if (parseprop(props, optarg) != 0)
804 (void) fprintf(stderr, gettext("missing size "
808 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
814 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
815 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
816 "used when creating a volume\n"));
823 /* check number of arguments */
825 (void) fprintf(stderr, gettext("missing %s argument\n"),
826 zfs_type_to_name(type));
830 (void) fprintf(stderr, gettext("too many arguments\n"));
834 if (type == ZFS_TYPE_VOLUME && !noreserve) {
835 zpool_handle_t *zpool_handle;
836 uint64_t spa_version;
838 zfs_prop_t resv_prop;
841 if ((p = strchr(argv[0], '/')))
843 zpool_handle = zpool_open(g_zfs, argv[0]);
846 if (zpool_handle == NULL)
848 spa_version = zpool_get_prop_int(zpool_handle,
849 ZPOOL_PROP_VERSION, NULL);
850 zpool_close(zpool_handle);
851 if (spa_version >= SPA_VERSION_REFRESERVATION)
852 resv_prop = ZFS_PROP_REFRESERVATION;
854 resv_prop = ZFS_PROP_RESERVATION;
855 volsize = zvol_volsize_to_reservation(volsize, props);
857 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
859 if (nvlist_add_uint64(props,
860 zfs_prop_to_name(resv_prop), volsize) != 0) {
867 if (parents && zfs_name_valid(argv[0], type)) {
869 * Now create the ancestors of target dataset. If the target
870 * already exists and '-p' option was used we should not
873 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
877 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
882 if (zfs_create(g_zfs, argv[0], type, props) != 0)
886 (void) zpool_log_history(g_zfs, history_str);
887 log_history = B_FALSE;
890 ret = zfs_mount_and_share(g_zfs, argv[0], ZFS_TYPE_DATASET);
901 * zfs destroy [-rRf] <fs, vol>
902 * zfs destroy [-rRd] <snap>
904 * -r Recursively destroy all children
905 * -R Recursively destroy all dependents, including clones
906 * -f Force unmounting of any dependents
907 * -d If we can't destroy now, mark for deferred destruction
909 * Destroys the given dataset. By default, it will unmount any filesystems,
910 * and refuse to destroy a dataset that has any dependents. A dependent can
911 * either be a child, or a clone of a child.
913 typedef struct destroy_cbdata {
916 boolean_t cb_recurse;
918 boolean_t cb_doclones;
919 zfs_handle_t *cb_target;
920 boolean_t cb_defer_destroy;
921 boolean_t cb_verbose;
922 boolean_t cb_parsable;
925 nvlist_t *cb_batchedsnaps;
927 /* first snap in contiguous run */
929 /* previous snap in contiguous run */
937 * Check for any dependents based on the '-r' or '-R' flags.
940 destroy_check_dependent(zfs_handle_t *zhp, void *data)
942 destroy_cbdata_t *cbp = data;
943 const char *tname = zfs_get_name(cbp->cb_target);
944 const char *name = zfs_get_name(zhp);
946 if (strncmp(tname, name, strlen(tname)) == 0 &&
947 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
949 * This is a direct descendant, not a clone somewhere else in
956 (void) fprintf(stderr, gettext("cannot destroy '%s': "
957 "%s has children\n"),
958 zfs_get_name(cbp->cb_target),
959 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
960 (void) fprintf(stderr, gettext("use '-r' to destroy "
961 "the following datasets:\n"));
962 cbp->cb_first = B_FALSE;
963 cbp->cb_error = B_TRUE;
966 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
969 * This is a clone. We only want to report this if the '-r'
970 * wasn't specified, or the target is a snapshot.
972 if (!cbp->cb_recurse &&
973 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
977 (void) fprintf(stderr, gettext("cannot destroy '%s': "
978 "%s has dependent clones\n"),
979 zfs_get_name(cbp->cb_target),
980 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
981 (void) fprintf(stderr, gettext("use '-R' to destroy "
982 "the following datasets:\n"));
983 cbp->cb_first = B_FALSE;
984 cbp->cb_error = B_TRUE;
985 cbp->cb_dryrun = B_TRUE;
988 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
997 destroy_callback(zfs_handle_t *zhp, void *data)
999 destroy_cbdata_t *cb = data;
1000 const char *name = zfs_get_name(zhp);
1002 if (cb->cb_verbose) {
1003 if (cb->cb_parsable) {
1004 (void) printf("destroy\t%s\n", name);
1005 } else if (cb->cb_dryrun) {
1006 (void) printf(gettext("would destroy %s\n"),
1009 (void) printf(gettext("will destroy %s\n"),
1015 * Ignore pools (which we've already flagged as an error before getting
1018 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1019 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1023 if (cb->cb_dryrun) {
1029 * We batch up all contiguous snapshots (even of different
1030 * filesystems) and destroy them with one ioctl. We can't
1031 * simply do all snap deletions and then all fs deletions,
1032 * because we must delete a clone before its origin.
1034 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1035 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1037 int error = zfs_destroy_snaps_nvl(g_zfs,
1038 cb->cb_batchedsnaps, B_FALSE);
1039 fnvlist_free(cb->cb_batchedsnaps);
1040 cb->cb_batchedsnaps = fnvlist_alloc();
1043 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1044 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1055 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1057 destroy_cbdata_t *cb = arg;
1058 const char *name = zfs_get_name(zhp);
1061 if (nvlist_exists(cb->cb_nvl, name)) {
1062 if (cb->cb_firstsnap == NULL)
1063 cb->cb_firstsnap = strdup(name);
1064 if (cb->cb_prevsnap != NULL)
1065 free(cb->cb_prevsnap);
1066 /* this snap continues the current range */
1067 cb->cb_prevsnap = strdup(name);
1068 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1070 if (cb->cb_verbose) {
1071 if (cb->cb_parsable) {
1072 (void) printf("destroy\t%s\n", name);
1073 } else if (cb->cb_dryrun) {
1074 (void) printf(gettext("would destroy %s\n"),
1077 (void) printf(gettext("will destroy %s\n"),
1081 } else if (cb->cb_firstsnap != NULL) {
1082 /* end of this range */
1084 err = lzc_snaprange_space(cb->cb_firstsnap,
1085 cb->cb_prevsnap, &used);
1086 cb->cb_snapused += used;
1087 free(cb->cb_firstsnap);
1088 cb->cb_firstsnap = NULL;
1089 free(cb->cb_prevsnap);
1090 cb->cb_prevsnap = NULL;
1097 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1100 assert(cb->cb_firstsnap == NULL);
1101 assert(cb->cb_prevsnap == NULL);
1102 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1103 if (cb->cb_firstsnap != NULL) {
1106 err = lzc_snaprange_space(cb->cb_firstsnap,
1107 cb->cb_prevsnap, &used);
1109 cb->cb_snapused += used;
1110 free(cb->cb_firstsnap);
1111 cb->cb_firstsnap = NULL;
1112 free(cb->cb_prevsnap);
1113 cb->cb_prevsnap = NULL;
1119 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1121 destroy_cbdata_t *cb = arg;
1124 /* Check for clones. */
1125 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1126 cb->cb_target = zhp;
1127 cb->cb_first = B_TRUE;
1128 err = zfs_iter_dependents(zhp, B_TRUE,
1129 destroy_check_dependent, cb);
1133 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1141 gather_snapshots(zfs_handle_t *zhp, void *arg)
1143 destroy_cbdata_t *cb = arg;
1146 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1152 if (cb->cb_verbose) {
1153 err = destroy_print_snapshots(zhp, cb);
1159 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1167 destroy_clones(destroy_cbdata_t *cb)
1170 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1172 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1173 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1176 boolean_t defer = cb->cb_defer_destroy;
1180 * We can't defer destroy non-snapshots, so set it to
1181 * false while destroying the clones.
1183 cb->cb_defer_destroy = B_FALSE;
1184 err = zfs_iter_dependents(zhp, B_FALSE,
1185 destroy_callback, cb);
1186 cb->cb_defer_destroy = defer;
1196 zfs_do_destroy(int argc, char **argv)
1198 destroy_cbdata_t cb = { 0 };
1202 zfs_handle_t *zhp = NULL;
1204 zfs_type_t type = ZFS_TYPE_DATASET;
1207 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1210 cb.cb_verbose = B_TRUE;
1213 cb.cb_verbose = B_TRUE;
1214 cb.cb_parsable = B_TRUE;
1217 cb.cb_dryrun = B_TRUE;
1220 cb.cb_defer_destroy = B_TRUE;
1221 type = ZFS_TYPE_SNAPSHOT;
1224 cb.cb_force = B_TRUE;
1227 cb.cb_recurse = B_TRUE;
1230 cb.cb_recurse = B_TRUE;
1231 cb.cb_doclones = B_TRUE;
1235 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1244 /* check number of arguments */
1246 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1250 (void) fprintf(stderr, gettext("too many arguments\n"));
1254 at = strchr(argv[0], '@');
1255 pound = strchr(argv[0], '#');
1258 /* Build the list of snaps to destroy in cb_nvl. */
1259 cb.cb_nvl = fnvlist_alloc();
1262 zhp = zfs_open(g_zfs, argv[0],
1263 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1267 cb.cb_snapspec = at + 1;
1268 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1274 if (nvlist_empty(cb.cb_nvl)) {
1275 (void) fprintf(stderr, gettext("could not find any "
1276 "snapshots to destroy; check snapshot names.\n"));
1281 if (cb.cb_verbose) {
1283 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1284 if (cb.cb_parsable) {
1285 (void) printf("reclaim\t%llu\n",
1286 (u_longlong_t)cb.cb_snapused);
1287 } else if (cb.cb_dryrun) {
1288 (void) printf(gettext("would reclaim %s\n"),
1291 (void) printf(gettext("will reclaim %s\n"),
1296 if (!cb.cb_dryrun) {
1297 if (cb.cb_doclones) {
1298 cb.cb_batchedsnaps = fnvlist_alloc();
1299 err = destroy_clones(&cb);
1301 err = zfs_destroy_snaps_nvl(g_zfs,
1302 cb.cb_batchedsnaps, B_FALSE);
1310 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1311 cb.cb_defer_destroy);
1317 } else if (pound != NULL) {
1322 (void) fprintf(stderr,
1323 "dryrun is not supported with bookmark\n");
1327 if (cb.cb_defer_destroy) {
1328 (void) fprintf(stderr,
1329 "defer destroy is not supported with bookmark\n");
1333 if (cb.cb_recurse) {
1334 (void) fprintf(stderr,
1335 "recursive is not supported with bookmark\n");
1339 if (!zfs_bookmark_exists(argv[0])) {
1340 (void) fprintf(stderr, gettext("bookmark '%s' "
1341 "does not exist.\n"), argv[0]);
1345 nvl = fnvlist_alloc();
1346 fnvlist_add_boolean(nvl, argv[0]);
1348 err = lzc_destroy_bookmarks(nvl, NULL);
1350 (void) zfs_standard_error(g_zfs, err,
1351 "cannot destroy bookmark");
1354 nvlist_free(cb.cb_nvl);
1358 /* Open the given dataset */
1359 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1365 * Perform an explicit check for pools before going any further.
1367 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1368 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1369 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1370 "operation does not apply to pools\n"),
1372 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1373 "%s' to destroy all datasets in the pool\n"),
1375 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1376 "to destroy the pool itself\n"), zfs_get_name(zhp));
1382 * Check for any dependents and/or clones.
1384 cb.cb_first = B_TRUE;
1385 if (!cb.cb_doclones &&
1386 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1397 cb.cb_batchedsnaps = fnvlist_alloc();
1398 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1405 * Do the real thing. The callback will close the
1406 * handle regardless of whether it succeeds or not.
1408 err = destroy_callback(zhp, &cb);
1411 err = zfs_destroy_snaps_nvl(g_zfs,
1412 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1419 fnvlist_free(cb.cb_batchedsnaps);
1420 fnvlist_free(cb.cb_nvl);
1427 is_recvd_column(zprop_get_cbdata_t *cbp)
1430 zfs_get_column_t col;
1432 for (i = 0; i < ZFS_GET_NCOLS &&
1433 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1434 if (col == GET_COL_RECVD)
1440 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1441 * < all | property[,property]... > < fs | snap | vol > ...
1443 * -r recurse over any child datasets
1444 * -H scripted mode. Headers are stripped, and fields are separated
1445 * by tabs instead of spaces.
1446 * -o Set of fields to display. One of "name,property,value,
1447 * received,source". Default is "name,property,value,source".
1448 * "all" is an alias for all five.
1449 * -s Set of sources to allow. One of
1450 * "local,default,inherited,received,temporary,none". Default is
1452 * -p Display values in parsable (literal) format.
1454 * Prints properties for the given datasets. The user can control which
1455 * columns to display as well as which property types to allow.
1459 * Invoked to display the properties for a single dataset.
1462 get_callback(zfs_handle_t *zhp, void *data)
1464 char buf[ZFS_MAXPROPLEN];
1465 char rbuf[ZFS_MAXPROPLEN];
1466 zprop_source_t sourcetype;
1467 char source[ZFS_MAXNAMELEN];
1468 zprop_get_cbdata_t *cbp = data;
1469 nvlist_t *user_props = zfs_get_user_props(zhp);
1470 zprop_list_t *pl = cbp->cb_proplist;
1474 boolean_t received = is_recvd_column(cbp);
1476 for (; pl != NULL; pl = pl->pl_next) {
1477 char *recvdval = NULL;
1479 * Skip the special fake placeholder. This will also skip over
1480 * the name property when 'all' is specified.
1482 if (pl->pl_prop == ZFS_PROP_NAME &&
1483 pl == cbp->cb_proplist)
1486 if (pl->pl_prop != ZPROP_INVAL) {
1487 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1488 sizeof (buf), &sourcetype, source,
1490 cbp->cb_literal) != 0) {
1493 if (!zfs_prop_valid_for_type(pl->pl_prop,
1494 ZFS_TYPE_DATASET, B_FALSE)) {
1495 (void) fprintf(stderr,
1496 gettext("No such property '%s'\n"),
1497 zfs_prop_to_name(pl->pl_prop));
1500 sourcetype = ZPROP_SRC_NONE;
1501 (void) strlcpy(buf, "-", sizeof (buf));
1504 if (received && (zfs_prop_get_recvd(zhp,
1505 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1506 cbp->cb_literal) == 0))
1509 zprop_print_one_property(zfs_get_name(zhp), cbp,
1510 zfs_prop_to_name(pl->pl_prop),
1511 buf, sourcetype, source, recvdval);
1512 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1513 sourcetype = ZPROP_SRC_LOCAL;
1515 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1516 buf, sizeof (buf), cbp->cb_literal) != 0) {
1517 sourcetype = ZPROP_SRC_NONE;
1518 (void) strlcpy(buf, "-", sizeof (buf));
1521 zprop_print_one_property(zfs_get_name(zhp), cbp,
1522 pl->pl_user_prop, buf, sourcetype, source, NULL);
1523 } else if (zfs_prop_written(pl->pl_user_prop)) {
1524 sourcetype = ZPROP_SRC_LOCAL;
1526 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1527 buf, sizeof (buf), cbp->cb_literal) != 0) {
1528 sourcetype = ZPROP_SRC_NONE;
1529 (void) strlcpy(buf, "-", sizeof (buf));
1532 zprop_print_one_property(zfs_get_name(zhp), cbp,
1533 pl->pl_user_prop, buf, sourcetype, source, NULL);
1535 if (nvlist_lookup_nvlist(user_props,
1536 pl->pl_user_prop, &propval) != 0) {
1539 sourcetype = ZPROP_SRC_NONE;
1542 verify(nvlist_lookup_string(propval,
1543 ZPROP_VALUE, &strval) == 0);
1544 verify(nvlist_lookup_string(propval,
1545 ZPROP_SOURCE, &sourceval) == 0);
1547 if (strcmp(sourceval,
1548 zfs_get_name(zhp)) == 0) {
1549 sourcetype = ZPROP_SRC_LOCAL;
1550 } else if (strcmp(sourceval,
1551 ZPROP_SOURCE_VAL_RECVD) == 0) {
1552 sourcetype = ZPROP_SRC_RECEIVED;
1554 sourcetype = ZPROP_SRC_INHERITED;
1555 (void) strlcpy(source,
1556 sourceval, sizeof (source));
1560 if (received && (zfs_prop_get_recvd(zhp,
1561 pl->pl_user_prop, rbuf, sizeof (rbuf),
1562 cbp->cb_literal) == 0))
1565 zprop_print_one_property(zfs_get_name(zhp), cbp,
1566 pl->pl_user_prop, strval, sourcetype,
1575 zfs_do_get(int argc, char **argv)
1577 zprop_get_cbdata_t cb = { 0 };
1578 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1579 int types = ZFS_TYPE_DATASET;
1580 char *value, *fields;
1583 zprop_list_t fake_name = { 0 };
1586 * Set up default columns and sources.
1588 cb.cb_sources = ZPROP_SRC_ALL;
1589 cb.cb_columns[0] = GET_COL_NAME;
1590 cb.cb_columns[1] = GET_COL_PROPERTY;
1591 cb.cb_columns[2] = GET_COL_VALUE;
1592 cb.cb_columns[3] = GET_COL_SOURCE;
1593 cb.cb_type = ZFS_TYPE_DATASET;
1596 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1599 cb.cb_literal = B_TRUE;
1602 limit = parse_depth(optarg, &flags);
1605 flags |= ZFS_ITER_RECURSE;
1608 cb.cb_scripted = B_TRUE;
1611 (void) fprintf(stderr, gettext("missing argument for "
1612 "'%c' option\n"), optopt);
1617 * Process the set of columns to display. We zero out
1618 * the structure to give us a blank slate.
1620 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1622 while (*optarg != '\0') {
1623 static char *col_subopts[] =
1624 { "name", "property", "value", "received",
1625 "source", "all", NULL };
1627 if (i == ZFS_GET_NCOLS) {
1628 (void) fprintf(stderr, gettext("too "
1629 "many fields given to -o "
1634 switch (getsubopt(&optarg, col_subopts,
1637 cb.cb_columns[i++] = GET_COL_NAME;
1640 cb.cb_columns[i++] = GET_COL_PROPERTY;
1643 cb.cb_columns[i++] = GET_COL_VALUE;
1646 cb.cb_columns[i++] = GET_COL_RECVD;
1647 flags |= ZFS_ITER_RECVD_PROPS;
1650 cb.cb_columns[i++] = GET_COL_SOURCE;
1654 (void) fprintf(stderr,
1655 gettext("\"all\" conflicts "
1656 "with specific fields "
1657 "given to -o option\n"));
1660 cb.cb_columns[0] = GET_COL_NAME;
1661 cb.cb_columns[1] = GET_COL_PROPERTY;
1662 cb.cb_columns[2] = GET_COL_VALUE;
1663 cb.cb_columns[3] = GET_COL_RECVD;
1664 cb.cb_columns[4] = GET_COL_SOURCE;
1665 flags |= ZFS_ITER_RECVD_PROPS;
1669 (void) fprintf(stderr,
1670 gettext("invalid column name "
1679 while (*optarg != '\0') {
1680 static char *source_subopts[] = {
1681 "local", "default", "inherited",
1682 "received", "temporary", "none",
1685 switch (getsubopt(&optarg, source_subopts,
1688 cb.cb_sources |= ZPROP_SRC_LOCAL;
1691 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1694 cb.cb_sources |= ZPROP_SRC_INHERITED;
1697 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1700 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1703 cb.cb_sources |= ZPROP_SRC_NONE;
1706 (void) fprintf(stderr,
1707 gettext("invalid source "
1716 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1717 while (*optarg != '\0') {
1718 static char *type_subopts[] = { "filesystem",
1719 "volume", "snapshot", "bookmark",
1722 switch (getsubopt(&optarg, type_subopts,
1725 types |= ZFS_TYPE_FILESYSTEM;
1728 types |= ZFS_TYPE_VOLUME;
1731 types |= ZFS_TYPE_SNAPSHOT;
1734 types |= ZFS_TYPE_BOOKMARK;
1737 types = ZFS_TYPE_DATASET |
1742 (void) fprintf(stderr,
1743 gettext("invalid type '%s'\n"),
1751 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1761 (void) fprintf(stderr, gettext("missing property "
1768 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1776 * As part of zfs_expand_proplist(), we keep track of the maximum column
1777 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1778 * need to know the maximum name length. However, the user likely did
1779 * not specify 'name' as one of the properties to fetch, so we need to
1780 * make sure we always include at least this property for
1781 * print_get_headers() to work properly.
1783 if (cb.cb_proplist != NULL) {
1784 fake_name.pl_prop = ZFS_PROP_NAME;
1785 fake_name.pl_width = strlen(gettext("NAME"));
1786 fake_name.pl_next = cb.cb_proplist;
1787 cb.cb_proplist = &fake_name;
1790 cb.cb_first = B_TRUE;
1792 /* run for each object */
1793 ret = zfs_for_each(argc, argv, flags, types, NULL,
1794 &cb.cb_proplist, limit, get_callback, &cb);
1796 if (cb.cb_proplist == &fake_name)
1797 zprop_free_list(fake_name.pl_next);
1799 zprop_free_list(cb.cb_proplist);
1805 * inherit [-rS] <property> <fs|vol> ...
1807 * -r Recurse over all children
1808 * -S Revert to received value, if any
1810 * For each dataset specified on the command line, inherit the given property
1811 * from its parent. Inheriting a property at the pool level will cause it to
1812 * use the default value. The '-r' flag will recurse over all children, and is
1813 * useful for setting a property on a hierarchy-wide basis, regardless of any
1814 * local modifications for each dataset.
1817 typedef struct inherit_cbdata {
1818 const char *cb_propname;
1819 boolean_t cb_received;
1823 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1825 inherit_cbdata_t *cb = data;
1826 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1829 * If we're doing it recursively, then ignore properties that
1830 * are not valid for this type of dataset.
1832 if (prop != ZPROP_INVAL &&
1833 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp), B_FALSE))
1836 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1840 inherit_cb(zfs_handle_t *zhp, void *data)
1842 inherit_cbdata_t *cb = data;
1844 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1848 zfs_do_inherit(int argc, char **argv)
1852 inherit_cbdata_t cb = { 0 };
1856 boolean_t received = B_FALSE;
1859 while ((c = getopt(argc, argv, "rS")) != -1) {
1862 flags |= ZFS_ITER_RECURSE;
1869 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1878 /* check number of arguments */
1880 (void) fprintf(stderr, gettext("missing property argument\n"));
1884 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1892 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1893 if (zfs_prop_readonly(prop)) {
1894 (void) fprintf(stderr, gettext(
1895 "%s property is read-only\n"),
1899 if (!zfs_prop_inheritable(prop) && !received) {
1900 (void) fprintf(stderr, gettext("'%s' property cannot "
1901 "be inherited\n"), propname);
1902 if (prop == ZFS_PROP_QUOTA ||
1903 prop == ZFS_PROP_RESERVATION ||
1904 prop == ZFS_PROP_REFQUOTA ||
1905 prop == ZFS_PROP_REFRESERVATION) {
1906 (void) fprintf(stderr, gettext("use 'zfs set "
1907 "%s=none' to clear\n"), propname);
1908 (void) fprintf(stderr, gettext("use 'zfs "
1909 "inherit -S %s' to revert to received "
1910 "value\n"), propname);
1914 if (received && (prop == ZFS_PROP_VOLSIZE ||
1915 prop == ZFS_PROP_VERSION)) {
1916 (void) fprintf(stderr, gettext("'%s' property cannot "
1917 "be reverted to a received value\n"), propname);
1920 } else if (!zfs_prop_user(propname)) {
1921 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1926 cb.cb_propname = propname;
1927 cb.cb_received = received;
1929 if (flags & ZFS_ITER_RECURSE) {
1930 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1931 NULL, NULL, 0, inherit_recurse_cb, &cb);
1933 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1934 NULL, NULL, 0, inherit_cb, &cb);
1940 typedef struct upgrade_cbdata {
1941 uint64_t cb_numupgraded;
1942 uint64_t cb_numsamegraded;
1943 uint64_t cb_numfailed;
1944 uint64_t cb_version;
1946 boolean_t cb_foundone;
1947 char cb_lastfs[ZFS_MAXNAMELEN];
1951 same_pool(zfs_handle_t *zhp, const char *name)
1953 int len1 = strcspn(name, "/@");
1954 const char *zhname = zfs_get_name(zhp);
1955 int len2 = strcspn(zhname, "/@");
1959 return (strncmp(name, zhname, len1) == 0);
1963 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1965 upgrade_cbdata_t *cb = data;
1966 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1968 /* list if it's old/new */
1969 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1970 (cb->cb_newer && version > ZPL_VERSION)) {
1973 str = gettext("The following filesystems are "
1974 "formatted using a newer software version and\n"
1975 "cannot be accessed on the current system.\n\n");
1977 str = gettext("The following filesystems are "
1978 "out of date, and can be upgraded. After being\n"
1979 "upgraded, these filesystems (and any 'zfs send' "
1980 "streams generated from\n"
1981 "subsequent snapshots) will no longer be "
1982 "accessible by older software versions.\n\n");
1985 if (!cb->cb_foundone) {
1987 (void) printf(gettext("VER FILESYSTEM\n"));
1988 (void) printf(gettext("--- ------------\n"));
1989 cb->cb_foundone = B_TRUE;
1992 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1999 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2001 upgrade_cbdata_t *cb = data;
2002 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2003 int needed_spa_version;
2006 if (zfs_spa_version(zhp, &spa_version) < 0)
2009 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2011 if (needed_spa_version < 0)
2014 if (spa_version < needed_spa_version) {
2016 (void) printf(gettext("%s: can not be "
2017 "upgraded; the pool version needs to first "
2018 "be upgraded\nto version %d\n\n"),
2019 zfs_get_name(zhp), needed_spa_version);
2025 if (version < cb->cb_version) {
2027 (void) snprintf(verstr, sizeof (verstr),
2028 "%llu", (u_longlong_t)cb->cb_version);
2029 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2031 * If they did "zfs upgrade -a", then we could
2032 * be doing ioctls to different pools. We need
2033 * to log this history once to each pool, and bypass
2034 * the normal history logging that happens in main().
2036 (void) zpool_log_history(g_zfs, history_str);
2037 log_history = B_FALSE;
2039 if (zfs_prop_set(zhp, "version", verstr) == 0)
2040 cb->cb_numupgraded++;
2043 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2044 } else if (version > cb->cb_version) {
2045 /* can't downgrade */
2046 (void) printf(gettext("%s: can not be downgraded; "
2047 "it is already at version %u\n"),
2048 zfs_get_name(zhp), version);
2051 cb->cb_numsamegraded++;
2059 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2062 zfs_do_upgrade(int argc, char **argv)
2064 boolean_t all = B_FALSE;
2065 boolean_t showversions = B_FALSE;
2067 upgrade_cbdata_t cb = { 0 };
2069 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2072 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2075 flags |= ZFS_ITER_RECURSE;
2078 showversions = B_TRUE;
2081 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2082 optarg, &cb.cb_version) != 0) {
2083 (void) fprintf(stderr,
2084 gettext("invalid version %s\n"), optarg);
2093 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2102 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2104 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2105 cb.cb_version || argc))
2107 if ((all || argc) && (showversions))
2113 /* Show info on available versions. */
2114 (void) printf(gettext("The following filesystem versions are "
2116 (void) printf(gettext("VER DESCRIPTION\n"));
2117 (void) printf("--- -----------------------------------------"
2118 "---------------\n");
2119 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2120 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2121 (void) printf(gettext(" 3 Case insensitive and filesystem "
2122 "user identifier (FUID)\n"));
2123 (void) printf(gettext(" 4 userquota, groupquota "
2125 (void) printf(gettext(" 5 System attributes\n"));
2126 (void) printf(gettext("\nFor more information on a particular "
2127 "version, including supported releases,\n"));
2128 (void) printf("see the ZFS Administration Guide.\n\n");
2130 } else if (argc || all) {
2131 /* Upgrade filesystems */
2132 if (cb.cb_version == 0)
2133 cb.cb_version = ZPL_VERSION;
2134 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2135 NULL, NULL, 0, upgrade_set_callback, &cb);
2136 (void) printf(gettext("%llu filesystems upgraded\n"),
2137 (u_longlong_t)cb.cb_numupgraded);
2138 if (cb.cb_numsamegraded) {
2139 (void) printf(gettext("%llu filesystems already at "
2141 (u_longlong_t)cb.cb_numsamegraded);
2143 if (cb.cb_numfailed != 0)
2146 /* List old-version filesytems */
2148 (void) printf(gettext("This system is currently running "
2149 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2151 flags |= ZFS_ITER_RECURSE;
2152 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2153 NULL, NULL, 0, upgrade_list_callback, &cb);
2155 found = cb.cb_foundone;
2156 cb.cb_foundone = B_FALSE;
2157 cb.cb_newer = B_TRUE;
2159 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2160 NULL, NULL, 0, upgrade_list_callback, &cb);
2162 if (!cb.cb_foundone && !found) {
2163 (void) printf(gettext("All filesystems are "
2164 "formatted with the current version.\n"));
2172 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2173 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2174 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2175 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2177 * -H Scripted mode; elide headers and separate columns by tabs.
2178 * -i Translate SID to POSIX ID.
2179 * -n Print numeric ID instead of user/group name.
2180 * -o Control which fields to display.
2181 * -p Use exact (parsable) numeric output.
2182 * -s Specify sort columns, descending order.
2183 * -S Specify sort columns, ascending order.
2184 * -t Control which object types to display.
2186 * Displays space consumed by, and quotas on, each user in the specified
2187 * filesystem or snapshot.
2190 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2191 enum us_field_types {
2197 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2198 static char *us_field_names[] = { "type", "name", "used", "quota" };
2199 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2201 #define USTYPE_PSX_GRP (1 << 0)
2202 #define USTYPE_PSX_USR (1 << 1)
2203 #define USTYPE_SMB_GRP (1 << 2)
2204 #define USTYPE_SMB_USR (1 << 3)
2205 #define USTYPE_ALL \
2206 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2208 static int us_type_bits[] = {
2215 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2218 typedef struct us_node {
2220 uu_avl_node_t usn_avlnode;
2221 uu_list_node_t usn_listnode;
2224 typedef struct us_cbdata {
2226 uu_avl_pool_t *cb_avl_pool;
2228 boolean_t cb_numname;
2229 boolean_t cb_nicenum;
2230 boolean_t cb_sid2posix;
2231 zfs_userquota_prop_t cb_prop;
2232 zfs_sort_column_t *cb_sortcol;
2233 size_t cb_width[USFIELD_LAST];
2236 static boolean_t us_populated = B_FALSE;
2239 zfs_sort_column_t *si_sortcol;
2240 boolean_t si_numname;
2244 us_field_index(char *field)
2248 for (i = 0; i < USFIELD_LAST; i++) {
2249 if (strcmp(field, us_field_names[i]) == 0)
2257 us_compare(const void *larg, const void *rarg, void *unused)
2259 const us_node_t *l = larg;
2260 const us_node_t *r = rarg;
2261 us_sort_info_t *si = (us_sort_info_t *)unused;
2262 zfs_sort_column_t *sortcol = si->si_sortcol;
2263 boolean_t numname = si->si_numname;
2264 nvlist_t *lnvl = l->usn_nvl;
2265 nvlist_t *rnvl = r->usn_nvl;
2269 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2276 zfs_prop_t prop = sortcol->sc_prop;
2277 const char *propname = NULL;
2278 boolean_t reverse = sortcol->sc_reverse;
2283 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2284 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2286 rc = (rv32 < lv32) ? 1 : -1;
2291 (void) nvlist_lookup_uint64(lnvl, propname,
2293 (void) nvlist_lookup_uint64(rnvl, propname,
2296 rc = (rv64 < lv64) ? 1 : -1;
2298 (void) nvlist_lookup_string(lnvl, propname,
2300 (void) nvlist_lookup_string(rnvl, propname,
2302 rc = strcmp(lvstr, rvstr);
2306 case ZFS_PROP_QUOTA:
2309 if (prop == ZFS_PROP_USED)
2313 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2314 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2316 rc = (rv64 < lv64) ? 1 : -1;
2324 return (reverse ? 1 : -1);
2326 return (reverse ? -1 : 1);
2331 * If entries still seem to be the same, check if they are of the same
2332 * type (smbentity is added only if we are doing SID to POSIX ID
2333 * translation where we can have duplicate type/name combinations).
2335 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2336 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2338 return (lvb < rvb ? -1 : 1);
2343 static inline const char *
2344 us_type2str(unsigned field_type)
2346 switch (field_type) {
2347 case USTYPE_PSX_USR:
2348 return ("POSIX User");
2349 case USTYPE_PSX_GRP:
2350 return ("POSIX Group");
2351 case USTYPE_SMB_USR:
2352 return ("SMB User");
2353 case USTYPE_SMB_GRP:
2354 return ("SMB Group");
2356 return ("Undefined");
2361 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2363 us_cbdata_t *cb = (us_cbdata_t *)arg;
2364 zfs_userquota_prop_t prop = cb->cb_prop;
2369 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2370 uu_avl_t *avl = cb->cb_avl;
2374 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2376 const char *typestr;
2380 int typeidx, nameidx, sizeidx;
2381 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2382 boolean_t smbentity = B_FALSE;
2384 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2386 node = safe_malloc(sizeof (us_node_t));
2387 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2388 node->usn_nvl = props;
2390 if (domain != NULL && domain[0] != '\0') {
2393 char sid[ZFS_MAXNAMELEN + 32];
2397 directory_error_t e;
2401 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2403 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2404 type = USTYPE_SMB_GRP;
2405 err = sid_to_id(sid, B_FALSE, &id);
2407 type = USTYPE_SMB_USR;
2408 err = sid_to_id(sid, B_TRUE, &id);
2413 if (!cb->cb_sid2posix) {
2414 e = directory_name_from_sid(NULL, sid, &name,
2417 directory_error_free(e);
2427 #endif /* HAVE_IDMAP */
2430 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2432 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2433 type = USTYPE_PSX_GRP;
2434 if (!cb->cb_numname) {
2437 if ((g = getgrgid(rid)) != NULL)
2441 type = USTYPE_PSX_USR;
2442 if (!cb->cb_numname) {
2445 if ((p = getpwuid(rid)) != NULL)
2452 * Make sure that the type/name combination is unique when doing
2453 * SID to POSIX ID translation (hence changing the type from SMB to
2456 if (cb->cb_sid2posix &&
2457 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2460 /* Calculate/update width of TYPE field */
2461 typestr = us_type2str(type);
2462 typelen = strlen(gettext(typestr));
2463 typeidx = us_field_index("type");
2464 if (typelen > cb->cb_width[typeidx])
2465 cb->cb_width[typeidx] = typelen;
2466 if (nvlist_add_uint32(props, "type", type) != 0)
2469 /* Calculate/update width of NAME field */
2470 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2471 if (nvlist_add_uint64(props, "name", rid) != 0)
2473 namelen = snprintf(NULL, 0, "%u", rid);
2475 if (nvlist_add_string(props, "name", name) != 0)
2477 namelen = strlen(name);
2479 nameidx = us_field_index("name");
2480 if (namelen > cb->cb_width[nameidx])
2481 cb->cb_width[nameidx] = namelen;
2484 * Check if this type/name combination is in the list and update it;
2485 * otherwise add new node to the list.
2487 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2488 uu_avl_insert(avl, node, idx);
2493 props = node->usn_nvl;
2496 /* Calculate/update width of USED/QUOTA fields */
2498 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2500 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
2501 (u_longlong_t)space);
2502 sizelen = strlen(sizebuf);
2503 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2505 if (!nvlist_exists(props, "quota"))
2506 (void) nvlist_add_uint64(props, "quota", 0);
2509 if (!nvlist_exists(props, "used"))
2510 (void) nvlist_add_uint64(props, "used", 0);
2512 sizeidx = us_field_index(propname);
2513 if (sizelen > cb->cb_width[sizeidx])
2514 cb->cb_width[sizeidx] = sizelen;
2516 if (nvlist_add_uint64(props, propname, space) != 0)
2523 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2524 size_t *width, us_node_t *node)
2526 nvlist_t *nvl = node->usn_nvl;
2527 char valstr[ZFS_MAXNAMELEN];
2528 boolean_t first = B_TRUE;
2534 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2535 if (!(ustype & types))
2538 while ((field = fields[cfield]) != USFIELD_LAST) {
2539 nvpair_t *nvp = NULL;
2543 char *strval = NULL;
2545 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2546 if (strcmp(nvpair_name(nvp),
2547 us_field_names[field]) == 0)
2551 type = nvpair_type(nvp);
2553 case DATA_TYPE_UINT32:
2554 (void) nvpair_value_uint32(nvp, &val32);
2556 case DATA_TYPE_UINT64:
2557 (void) nvpair_value_uint64(nvp, &val64);
2559 case DATA_TYPE_STRING:
2560 (void) nvpair_value_string(nvp, &strval);
2563 (void) fprintf(stderr, "invalid data type\n");
2568 strval = (char *)us_type2str(val32);
2571 if (type == DATA_TYPE_UINT64) {
2572 (void) sprintf(valstr, "%llu",
2573 (u_longlong_t) val64);
2579 if (type == DATA_TYPE_UINT64) {
2581 (void) sprintf(valstr, "%llu",
2582 (u_longlong_t) val64);
2584 zfs_nicenum(val64, valstr,
2587 if (field == USFIELD_QUOTA &&
2588 strcmp(valstr, "0") == 0)
2598 (void) printf("\t");
2603 (void) printf("%s", strval);
2604 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2605 (void) printf("%-*s", (int) width[field], strval);
2607 (void) printf("%*s", (int) width[field], strval);
2613 (void) printf("\n");
2617 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2618 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2626 boolean_t first = B_TRUE;
2628 while ((field = fields[cfield]) != USFIELD_LAST) {
2629 col = gettext(us_field_hdr[field]);
2630 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2631 (void) printf(first ? "%-*s" : " %-*s",
2632 (int) width[field], col);
2634 (void) printf(first ? "%*s" : " %*s",
2635 (int) width[field], col);
2640 (void) printf("\n");
2643 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2644 print_us_node(scripted, parsable, fields, types, width, node);
2646 nvlist_free(node->usn_nvl);
2651 zfs_do_userspace(int argc, char **argv)
2654 zfs_userquota_prop_t p;
2655 uu_avl_pool_t *avl_pool;
2657 uu_avl_walk_t *walk;
2659 char deffields[] = "type,name,used,quota";
2660 char *ofield = NULL;
2661 char *tfield = NULL;
2665 boolean_t scripted = B_FALSE;
2666 boolean_t prtnum = B_FALSE;
2667 boolean_t parsable = B_FALSE;
2668 boolean_t sid2posix = B_FALSE;
2671 zfs_sort_column_t *sortcol = NULL;
2672 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2676 uu_list_pool_t *listpool;
2678 uu_avl_index_t idx = 0;
2679 uu_list_index_t idx2 = 0;
2684 if (strcmp(argv[0], "groupspace") == 0)
2685 /* Toggle default group types */
2686 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2688 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2704 if (zfs_add_sort_column(&sortcol, optarg,
2705 c == 's' ? B_FALSE : B_TRUE) != 0) {
2706 (void) fprintf(stderr,
2707 gettext("invalid field '%s'\n"), optarg);
2718 (void) fprintf(stderr, gettext("missing argument for "
2719 "'%c' option\n"), optopt);
2723 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2733 (void) fprintf(stderr, gettext("missing dataset name\n"));
2737 (void) fprintf(stderr, gettext("too many arguments\n"));
2741 /* Use default output fields if not specified using -o */
2745 if ((delim = strchr(ofield, ',')) != NULL)
2747 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2748 (void) fprintf(stderr, gettext("invalid type '%s' "
2749 "for -o option\n"), ofield);
2754 } while (delim != NULL);
2755 fields[cfield] = USFIELD_LAST;
2757 /* Override output types (-t option) */
2758 if (tfield != NULL) {
2762 boolean_t found = B_FALSE;
2764 if ((delim = strchr(tfield, ',')) != NULL)
2766 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2768 if (strcmp(tfield, us_type_names[i]) == 0) {
2770 types |= us_type_bits[i];
2775 (void) fprintf(stderr, gettext("invalid type "
2776 "'%s' for -t option\n"), tfield);
2781 } while (delim != NULL);
2784 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2787 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2788 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2790 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2793 /* Always add default sorting columns */
2794 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2795 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2797 cb.cb_sortcol = sortcol;
2798 cb.cb_numname = prtnum;
2799 cb.cb_nicenum = !parsable;
2800 cb.cb_avl_pool = avl_pool;
2801 cb.cb_avl = avl_tree;
2802 cb.cb_sid2posix = sid2posix;
2804 for (i = 0; i < USFIELD_LAST; i++)
2805 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2807 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2808 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2809 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2810 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2811 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2814 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2819 if ((node = uu_avl_first(avl_tree)) == NULL)
2822 us_populated = B_TRUE;
2824 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2825 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2826 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2827 uu_list_node_init(node, &node->usn_listnode, listpool);
2829 while (node != NULL) {
2831 node = uu_avl_next(avl_tree, node);
2832 uu_avl_remove(avl_tree, rmnode);
2833 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2834 uu_list_insert(list, rmnode, idx2);
2837 for (node = uu_list_first(list); node != NULL;
2838 node = uu_list_next(list, node)) {
2839 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2841 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2842 uu_avl_insert(avl_tree, node, idx);
2845 uu_list_destroy(list);
2846 uu_list_pool_destroy(listpool);
2848 /* Print and free node nvlist memory */
2849 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2852 zfs_free_sort_columns(sortcol);
2854 /* Clean up the AVL tree */
2855 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2858 while ((node = uu_avl_walk_next(walk)) != NULL) {
2859 uu_avl_remove(cb.cb_avl, node);
2863 uu_avl_walk_end(walk);
2864 uu_avl_destroy(avl_tree);
2865 uu_avl_pool_destroy(avl_pool);
2871 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
2872 * [-t type[,...]] [filesystem|volume|snapshot] ...
2874 * -H Scripted mode; elide headers and separate columns by tabs
2875 * -p Display values in parsable (literal) format.
2876 * -r Recurse over all children
2877 * -d Limit recursion by depth.
2878 * -o Control which fields to display.
2879 * -s Specify sort columns, descending order.
2880 * -S Specify sort columns, ascending order.
2881 * -t Control which object types to display.
2883 * When given no arguments, list all filesystems in the system.
2884 * Otherwise, list the specified datasets, optionally recursing down them if
2885 * '-r' is specified.
2887 typedef struct list_cbdata {
2889 boolean_t cb_literal;
2890 boolean_t cb_scripted;
2891 zprop_list_t *cb_proplist;
2895 * Given a list of columns to display, output appropriate headers for each one.
2898 print_header(list_cbdata_t *cb)
2900 zprop_list_t *pl = cb->cb_proplist;
2901 char headerbuf[ZFS_MAXPROPLEN];
2904 boolean_t first = B_TRUE;
2905 boolean_t right_justify;
2907 for (; pl != NULL; pl = pl->pl_next) {
2914 right_justify = B_FALSE;
2915 if (pl->pl_prop != ZPROP_INVAL) {
2916 header = zfs_prop_column_name(pl->pl_prop);
2917 right_justify = zfs_prop_align_right(pl->pl_prop);
2919 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2920 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2921 headerbuf[i] = '\0';
2925 if (pl->pl_next == NULL && !right_justify)
2926 (void) printf("%s", header);
2927 else if (right_justify)
2928 (void) printf("%*s", (int)pl->pl_width, header);
2930 (void) printf("%-*s", (int)pl->pl_width, header);
2933 (void) printf("\n");
2937 * Given a dataset and a list of fields, print out all the properties according
2938 * to the described layout.
2941 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2943 zprop_list_t *pl = cb->cb_proplist;
2944 boolean_t first = B_TRUE;
2945 char property[ZFS_MAXPROPLEN];
2946 nvlist_t *userprops = zfs_get_user_props(zhp);
2949 boolean_t right_justify;
2951 for (; pl != NULL; pl = pl->pl_next) {
2953 if (cb->cb_scripted)
2954 (void) printf("\t");
2961 if (pl->pl_prop == ZFS_PROP_NAME) {
2962 (void) strlcpy(property, zfs_get_name(zhp),
2965 right_justify = zfs_prop_align_right(pl->pl_prop);
2966 } else if (pl->pl_prop != ZPROP_INVAL) {
2967 if (zfs_prop_get(zhp, pl->pl_prop, property,
2968 sizeof (property), NULL, NULL, 0,
2969 cb->cb_literal) != 0)
2973 right_justify = zfs_prop_align_right(pl->pl_prop);
2974 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2975 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2976 property, sizeof (property), cb->cb_literal) != 0)
2980 right_justify = B_TRUE;
2981 } else if (zfs_prop_written(pl->pl_user_prop)) {
2982 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2983 property, sizeof (property), cb->cb_literal) != 0)
2987 right_justify = B_TRUE;
2989 if (nvlist_lookup_nvlist(userprops,
2990 pl->pl_user_prop, &propval) != 0)
2993 verify(nvlist_lookup_string(propval,
2994 ZPROP_VALUE, &propstr) == 0);
2995 right_justify = B_FALSE;
2999 * If this is being called in scripted mode, or if this is the
3000 * last column and it is left-justified, don't include a width
3003 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3004 (void) printf("%s", propstr);
3005 else if (right_justify)
3006 (void) printf("%*s", (int)pl->pl_width, propstr);
3008 (void) printf("%-*s", (int)pl->pl_width, propstr);
3011 (void) printf("\n");
3015 * Generic callback function to list a dataset or snapshot.
3018 list_callback(zfs_handle_t *zhp, void *data)
3020 list_cbdata_t *cbp = data;
3022 if (cbp->cb_first) {
3023 if (!cbp->cb_scripted)
3025 cbp->cb_first = B_FALSE;
3028 print_dataset(zhp, cbp);
3034 zfs_do_list(int argc, char **argv)
3037 static char default_fields[] =
3038 "name,used,available,referenced,mountpoint";
3039 int types = ZFS_TYPE_DATASET;
3040 boolean_t types_specified = B_FALSE;
3041 char *fields = NULL;
3042 list_cbdata_t cb = { 0 };
3046 zfs_sort_column_t *sortcol = NULL;
3047 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3050 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3056 cb.cb_literal = B_TRUE;
3057 flags |= ZFS_ITER_LITERAL_PROPS;
3060 limit = parse_depth(optarg, &flags);
3063 flags |= ZFS_ITER_RECURSE;
3066 cb.cb_scripted = B_TRUE;
3069 if (zfs_add_sort_column(&sortcol, optarg,
3071 (void) fprintf(stderr,
3072 gettext("invalid property '%s'\n"), optarg);
3077 if (zfs_add_sort_column(&sortcol, optarg,
3079 (void) fprintf(stderr,
3080 gettext("invalid property '%s'\n"), optarg);
3086 types_specified = B_TRUE;
3087 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3088 while (*optarg != '\0') {
3089 static char *type_subopts[] = { "filesystem",
3090 "volume", "snapshot", "snap", "bookmark",
3093 switch (getsubopt(&optarg, type_subopts,
3096 types |= ZFS_TYPE_FILESYSTEM;
3099 types |= ZFS_TYPE_VOLUME;
3103 types |= ZFS_TYPE_SNAPSHOT;
3106 types |= ZFS_TYPE_BOOKMARK;
3109 types = ZFS_TYPE_DATASET |
3113 (void) fprintf(stderr,
3114 gettext("invalid type '%s'\n"),
3121 (void) fprintf(stderr, gettext("missing argument for "
3122 "'%c' option\n"), optopt);
3126 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3136 fields = default_fields;
3139 * If we are only going to list snapshot names and sort by name,
3140 * then we can use faster version.
3142 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3143 flags |= ZFS_ITER_SIMPLE;
3146 * If "-o space" and no types were specified, don't display snapshots.
3148 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3149 types &= ~ZFS_TYPE_SNAPSHOT;
3152 * If the user specifies '-o all', the zprop_get_list() doesn't
3153 * normally include the name of the dataset. For 'zfs list', we always
3154 * want this property to be first.
3156 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3160 cb.cb_first = B_TRUE;
3162 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3163 limit, list_callback, &cb);
3165 zprop_free_list(cb.cb_proplist);
3166 zfs_free_sort_columns(sortcol);
3168 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3169 (void) fprintf(stderr, gettext("no datasets available\n"));
3175 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3176 * zfs rename [-f] -p <fs | vol> <fs | vol>
3177 * zfs rename -r <snap> <snap>
3179 * Renames the given dataset to another of the same type.
3181 * The '-p' flag creates all the non-existing ancestors of the target first.
3185 zfs_do_rename(int argc, char **argv)
3190 boolean_t recurse = B_FALSE;
3191 boolean_t parents = B_FALSE;
3192 boolean_t force_unmount = B_FALSE;
3195 while ((c = getopt(argc, argv, "prf")) != -1) {
3204 force_unmount = B_TRUE;
3208 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3217 /* check number of arguments */
3219 (void) fprintf(stderr, gettext("missing source dataset "
3224 (void) fprintf(stderr, gettext("missing target dataset "
3229 (void) fprintf(stderr, gettext("too many arguments\n"));
3233 if (recurse && parents) {
3234 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3239 if (recurse && strchr(argv[0], '@') == 0) {
3240 (void) fprintf(stderr, gettext("source dataset for recursive "
3241 "rename must be a snapshot\n"));
3245 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3246 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3249 /* If we were asked and the name looks good, try to create ancestors. */
3250 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3251 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3256 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3265 * Promotes the given clone fs to be the parent
3269 zfs_do_promote(int argc, char **argv)
3275 if (argc > 1 && argv[1][0] == '-') {
3276 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3281 /* check number of arguments */
3283 (void) fprintf(stderr, gettext("missing clone filesystem"
3288 (void) fprintf(stderr, gettext("too many arguments\n"));
3292 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3296 ret = (zfs_promote(zhp) != 0);
3304 * zfs rollback [-rRf] <snapshot>
3306 * -r Delete any intervening snapshots before doing rollback
3307 * -R Delete any snapshots and their clones
3308 * -f ignored for backwards compatability
3310 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3311 * since then and making it the active dataset. If more recent snapshots exist,
3312 * the command will complain unless the '-r' flag is given.
3314 typedef struct rollback_cbdata {
3320 boolean_t cb_recurse;
3321 } rollback_cbdata_t;
3324 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3326 rollback_cbdata_t *cbp = data;
3328 if (cbp->cb_first && cbp->cb_recurse) {
3329 (void) fprintf(stderr, gettext("cannot rollback to "
3330 "'%s': clones of previous snapshots exist\n"),
3332 (void) fprintf(stderr, gettext("use '-R' to "
3333 "force deletion of the following clones and "
3339 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3347 * Report any snapshots more recent than the one specified. Used when '-r' is
3348 * not specified. We reuse this same callback for the snapshot dependents - if
3349 * 'cb_dependent' is set, then this is a dependent and we should report it
3350 * without checking the transaction group.
3353 rollback_check(zfs_handle_t *zhp, void *data)
3355 rollback_cbdata_t *cbp = data;
3357 if (cbp->cb_doclones) {
3362 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3363 if (cbp->cb_first && !cbp->cb_recurse) {
3364 (void) fprintf(stderr, gettext("cannot "
3365 "rollback to '%s': more recent snapshots "
3366 "or bookmarks exist\n"),
3368 (void) fprintf(stderr, gettext("use '-r' to "
3369 "force deletion of the following "
3370 "snapshots and bookmarks:\n"));
3375 if (cbp->cb_recurse) {
3376 if (zfs_iter_dependents(zhp, B_TRUE,
3377 rollback_check_dependent, cbp) != 0) {
3382 (void) fprintf(stderr, "%s\n",
3391 zfs_do_rollback(int argc, char **argv)
3395 boolean_t force = B_FALSE;
3396 rollback_cbdata_t cb = { 0 };
3397 zfs_handle_t *zhp, *snap;
3398 char parentname[ZFS_MAXNAMELEN];
3402 while ((c = getopt(argc, argv, "rRf")) != -1) {
3415 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3424 /* check number of arguments */
3426 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3430 (void) fprintf(stderr, gettext("too many arguments\n"));
3434 /* open the snapshot */
3435 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3438 /* open the parent dataset */
3439 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3440 verify((delim = strrchr(parentname, '@')) != NULL);
3442 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3448 * Check for more recent snapshots and/or clones based on the presence
3451 cb.cb_target = argv[0];
3452 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3453 cb.cb_first = B_TRUE;
3455 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3457 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3460 if ((ret = cb.cb_error) != 0)
3464 * Rollback parent to the given snapshot.
3466 ret = zfs_rollback(zhp, snap, force);
3479 * zfs set property=value ... { fs | snap | vol } ...
3481 * Sets the given properties for all datasets specified on the command line.
3485 set_callback(zfs_handle_t *zhp, void *data)
3487 nvlist_t *props = data;
3489 if (zfs_prop_set_list(zhp, props) != 0) {
3490 switch (libzfs_errno(g_zfs)) {
3491 case EZFS_MOUNTFAILED:
3492 (void) fprintf(stderr, gettext("property may be set "
3493 "but unable to remount filesystem\n"));
3495 case EZFS_SHARENFSFAILED:
3496 (void) fprintf(stderr, gettext("property may be set "
3497 "but unable to reshare filesystem\n"));
3506 zfs_do_set(int argc, char **argv)
3508 nvlist_t *props = NULL;
3509 int ds_start = -1; /* argv idx of first dataset arg */
3513 /* check for options */
3514 if (argc > 1 && argv[1][0] == '-') {
3515 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3520 /* check number of arguments */
3522 (void) fprintf(stderr, gettext("missing arguments\n"));
3526 if (strchr(argv[1], '=') == NULL) {
3527 (void) fprintf(stderr, gettext("missing property=value "
3530 (void) fprintf(stderr, gettext("missing dataset "
3536 /* validate argument order: prop=val args followed by dataset args */
3537 for (i = 1; i < argc; i++) {
3538 if (strchr(argv[i], '=') != NULL) {
3540 /* out-of-order prop=val argument */
3541 (void) fprintf(stderr, gettext("invalid "
3542 "argument order\n"));
3545 } else if (ds_start < 0) {
3550 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3554 /* Populate a list of property settings */
3555 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3557 for (i = 1; i < ds_start; i++) {
3558 if ((ret = parseprop(props, argv[i])) != 0)
3562 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3563 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3570 typedef struct snap_cbdata {
3572 boolean_t sd_recursive;
3573 const char *sd_snapname;
3577 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3579 snap_cbdata_t *sd = arg;
3584 if (sd->sd_recursive &&
3585 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3590 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3593 fnvlist_add_boolean(sd->sd_nvl, name);
3596 if (sd->sd_recursive)
3597 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3603 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3605 * Creates a snapshot with the given name. While functionally equivalent to
3606 * 'zfs create', it is a separate command to differentiate intent.
3609 zfs_do_snapshot(int argc, char **argv)
3614 snap_cbdata_t sd = { 0 };
3615 boolean_t multiple_snaps = B_FALSE;
3617 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3619 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3623 while ((c = getopt(argc, argv, "ro:")) != -1) {
3626 if (parseprop(props, optarg) != 0)
3630 sd.sd_recursive = B_TRUE;
3631 multiple_snaps = B_TRUE;
3634 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3643 /* check number of arguments */
3645 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3650 multiple_snaps = B_TRUE;
3651 for (; argc > 0; argc--, argv++) {
3655 atp = strchr(argv[0], '@');
3659 sd.sd_snapname = atp + 1;
3660 zhp = zfs_open(g_zfs, argv[0],
3661 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3664 if (zfs_snapshot_cb(zhp, &sd) != 0)
3668 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3669 nvlist_free(sd.sd_nvl);
3671 if (ret != 0 && multiple_snaps)
3672 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3676 nvlist_free(sd.sd_nvl);
3683 * Send a backup stream to stdout.
3686 zfs_do_send(int argc, char **argv)
3688 char *fromname = NULL;
3689 char *toname = NULL;
3692 sendflags_t flags = { 0 };
3694 nvlist_t *dbgnv = NULL;
3695 boolean_t extraverbose = B_FALSE;
3698 while ((c = getopt(argc, argv, ":i:I:RDpvnPLe")) != -1) {
3709 flags.doall = B_TRUE;
3712 flags.replicate = B_TRUE;
3715 flags.props = B_TRUE;
3718 flags.parsable = B_TRUE;
3719 flags.verbose = B_TRUE;
3723 extraverbose = B_TRUE;
3724 flags.verbose = B_TRUE;
3725 flags.progress = B_TRUE;
3728 flags.dedup = B_TRUE;
3731 flags.dryrun = B_TRUE;
3734 flags.largeblock = B_TRUE;
3737 flags.embed_data = B_TRUE;
3740 (void) fprintf(stderr, gettext("missing argument for "
3741 "'%c' option\n"), optopt);
3745 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3754 /* check number of arguments */
3756 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3760 (void) fprintf(stderr, gettext("too many arguments\n"));
3764 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3765 (void) fprintf(stderr,
3766 gettext("Error: Stream can not be written to a terminal.\n"
3767 "You must redirect standard output.\n"));
3772 * Special case sending a filesystem, or from a bookmark.
3774 if (strchr(argv[0], '@') == NULL ||
3775 (fromname && strchr(fromname, '#') != NULL)) {
3776 char frombuf[ZFS_MAXNAMELEN];
3777 enum lzc_send_flags lzc_flags = 0;
3779 if (flags.replicate || flags.doall || flags.props ||
3780 flags.dedup || flags.dryrun || flags.verbose ||
3782 (void) fprintf(stderr,
3784 "Unsupported flag with filesystem or bookmark.\n"));
3788 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3792 if (flags.largeblock)
3793 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3794 if (flags.embed_data)
3795 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3797 if (fromname != NULL &&
3798 (fromname[0] == '#' || fromname[0] == '@')) {
3800 * Incremental source name begins with # or @.
3801 * Default to same fs as target.
3803 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3804 cp = strchr(frombuf, '@');
3807 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3810 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3815 cp = strchr(argv[0], '@');
3818 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3823 * If they specified the full path to the snapshot, chop off
3824 * everything except the short name of the snapshot, but special
3825 * case if they specify the origin.
3827 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3828 char origin[ZFS_MAXNAMELEN];
3831 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3832 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3834 if (strcmp(origin, fromname) == 0) {
3836 flags.fromorigin = B_TRUE;
3839 if (cp != fromname && strcmp(argv[0], fromname)) {
3840 (void) fprintf(stderr,
3841 gettext("incremental source must be "
3842 "in same filesystem\n"));
3846 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3847 (void) fprintf(stderr,
3848 gettext("invalid incremental source\n"));
3854 if (flags.replicate && fromname == NULL)
3855 flags.doall = B_TRUE;
3857 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3858 extraverbose ? &dbgnv : NULL);
3860 if (extraverbose && dbgnv != NULL) {
3862 * dump_nvlist prints to stdout, but that's been
3863 * redirected to a file. Make it print to stderr
3866 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3867 dump_nvlist(dbgnv, 0);
3876 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3878 * Restore a backup stream from stdin.
3881 zfs_do_receive(int argc, char **argv)
3884 recvflags_t flags = { 0 };
3886 nvpair_t *nvp = NULL;
3888 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3892 while ((c = getopt(argc, argv, ":o:denuvF")) != -1) {
3895 if (parseprop(props, optarg) != 0)
3899 flags.isprefix = B_TRUE;
3902 flags.isprefix = B_TRUE;
3903 flags.istail = B_TRUE;
3906 flags.dryrun = B_TRUE;
3909 flags.nomount = B_TRUE;
3912 flags.verbose = B_TRUE;
3915 flags.force = B_TRUE;
3918 (void) fprintf(stderr, gettext("missing argument for "
3919 "'%c' option\n"), optopt);
3923 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3932 /* check number of arguments */
3934 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3938 (void) fprintf(stderr, gettext("too many arguments\n"));
3942 while ((nvp = nvlist_next_nvpair(props, nvp))) {
3943 if (strcmp(nvpair_name(nvp), "origin") != 0) {
3944 (void) fprintf(stderr, gettext("invalid option"));
3949 if (isatty(STDIN_FILENO)) {
3950 (void) fprintf(stderr,
3951 gettext("Error: Backup stream can not be read "
3952 "from a terminal.\n"
3953 "You must redirect standard input.\n"));
3957 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
3963 * allow/unallow stuff
3965 /* copied from zfs/sys/dsl_deleg.h */
3966 #define ZFS_DELEG_PERM_CREATE "create"
3967 #define ZFS_DELEG_PERM_DESTROY "destroy"
3968 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3969 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3970 #define ZFS_DELEG_PERM_CLONE "clone"
3971 #define ZFS_DELEG_PERM_PROMOTE "promote"
3972 #define ZFS_DELEG_PERM_RENAME "rename"
3973 #define ZFS_DELEG_PERM_MOUNT "mount"
3974 #define ZFS_DELEG_PERM_SHARE "share"
3975 #define ZFS_DELEG_PERM_SEND "send"
3976 #define ZFS_DELEG_PERM_RECEIVE "receive"
3977 #define ZFS_DELEG_PERM_ALLOW "allow"
3978 #define ZFS_DELEG_PERM_USERPROP "userprop"
3979 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3980 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3981 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3982 #define ZFS_DELEG_PERM_USERUSED "userused"
3983 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3984 #define ZFS_DELEG_PERM_HOLD "hold"
3985 #define ZFS_DELEG_PERM_RELEASE "release"
3986 #define ZFS_DELEG_PERM_DIFF "diff"
3987 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
3989 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3991 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3992 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3993 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3994 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3995 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3996 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3997 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3998 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3999 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4000 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4001 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4002 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4003 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4004 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4005 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4006 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4007 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4009 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4010 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4011 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4012 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4013 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4014 { NULL, ZFS_DELEG_NOTE_NONE }
4017 /* permission structure */
4018 typedef struct deleg_perm {
4019 zfs_deleg_who_type_t dp_who_type;
4020 const char *dp_name;
4022 boolean_t dp_descend;
4026 typedef struct deleg_perm_node {
4027 deleg_perm_t dpn_perm;
4029 uu_avl_node_t dpn_avl_node;
4030 } deleg_perm_node_t;
4032 typedef struct fs_perm fs_perm_t;
4034 /* permissions set */
4035 typedef struct who_perm {
4036 zfs_deleg_who_type_t who_type;
4037 const char *who_name; /* id */
4038 char who_ug_name[256]; /* user/group name */
4039 fs_perm_t *who_fsperm; /* uplink */
4041 uu_avl_t *who_deleg_perm_avl; /* permissions */
4045 typedef struct who_perm_node {
4046 who_perm_t who_perm;
4047 uu_avl_node_t who_avl_node;
4050 typedef struct fs_perm_set fs_perm_set_t;
4051 /* fs permissions */
4053 const char *fsp_name;
4055 uu_avl_t *fsp_sc_avl; /* sets,create */
4056 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4058 fs_perm_set_t *fsp_set; /* uplink */
4062 typedef struct fs_perm_node {
4063 fs_perm_t fspn_fsperm;
4066 uu_list_node_t fspn_list_node;
4069 /* top level structure */
4070 struct fs_perm_set {
4071 uu_list_pool_t *fsps_list_pool;
4072 uu_list_t *fsps_list; /* list of fs_perms */
4074 uu_avl_pool_t *fsps_named_set_avl_pool;
4075 uu_avl_pool_t *fsps_who_perm_avl_pool;
4076 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4079 static inline const char *
4080 deleg_perm_type(zfs_deleg_note_t note)
4086 case ZFS_DELEG_NOTE_GROUPQUOTA:
4087 case ZFS_DELEG_NOTE_GROUPUSED:
4088 case ZFS_DELEG_NOTE_USERPROP:
4089 case ZFS_DELEG_NOTE_USERQUOTA:
4090 case ZFS_DELEG_NOTE_USERUSED:
4092 return (gettext("other"));
4094 return (gettext("subcommand"));
4099 who_type2weight(zfs_deleg_who_type_t who_type)
4103 case ZFS_DELEG_NAMED_SET_SETS:
4104 case ZFS_DELEG_NAMED_SET:
4107 case ZFS_DELEG_CREATE_SETS:
4108 case ZFS_DELEG_CREATE:
4111 case ZFS_DELEG_USER_SETS:
4112 case ZFS_DELEG_USER:
4115 case ZFS_DELEG_GROUP_SETS:
4116 case ZFS_DELEG_GROUP:
4119 case ZFS_DELEG_EVERYONE_SETS:
4120 case ZFS_DELEG_EVERYONE:
4132 who_perm_compare(const void *larg, const void *rarg, void *unused)
4134 const who_perm_node_t *l = larg;
4135 const who_perm_node_t *r = rarg;
4136 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4137 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4138 int lweight = who_type2weight(ltype);
4139 int rweight = who_type2weight(rtype);
4140 int res = lweight - rweight;
4142 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4143 ZFS_MAX_DELEG_NAME-1);
4155 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4157 const deleg_perm_node_t *l = larg;
4158 const deleg_perm_node_t *r = rarg;
4159 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4160 ZFS_MAX_DELEG_NAME-1);
4172 fs_perm_set_init(fs_perm_set_t *fspset)
4174 bzero(fspset, sizeof (fs_perm_set_t));
4176 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4177 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4178 NULL, UU_DEFAULT)) == NULL)
4180 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4181 UU_DEFAULT)) == NULL)
4184 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4185 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4186 who_perm_node_t, who_avl_node), who_perm_compare,
4187 UU_DEFAULT)) == NULL)
4190 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4191 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4192 who_perm_node_t, who_avl_node), who_perm_compare,
4193 UU_DEFAULT)) == NULL)
4196 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4197 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4198 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4203 static inline void fs_perm_fini(fs_perm_t *);
4204 static inline void who_perm_fini(who_perm_t *);
4207 fs_perm_set_fini(fs_perm_set_t *fspset)
4209 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4211 while (node != NULL) {
4212 fs_perm_node_t *next_node =
4213 uu_list_next(fspset->fsps_list, node);
4214 fs_perm_t *fsperm = &node->fspn_fsperm;
4215 fs_perm_fini(fsperm);
4216 uu_list_remove(fspset->fsps_list, node);
4221 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4222 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4223 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4227 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4230 deleg_perm->dp_who_type = type;
4231 deleg_perm->dp_name = name;
4235 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4236 zfs_deleg_who_type_t type, const char *name)
4238 uu_avl_pool_t *pool;
4239 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4241 bzero(who_perm, sizeof (who_perm_t));
4243 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4244 UU_DEFAULT)) == NULL)
4247 who_perm->who_type = type;
4248 who_perm->who_name = name;
4249 who_perm->who_fsperm = fsperm;
4253 who_perm_fini(who_perm_t *who_perm)
4255 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4257 while (node != NULL) {
4258 deleg_perm_node_t *next_node =
4259 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4261 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4266 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4270 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4272 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4273 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4275 bzero(fsperm, sizeof (fs_perm_t));
4277 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4281 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4285 fsperm->fsp_set = fspset;
4286 fsperm->fsp_name = fsname;
4290 fs_perm_fini(fs_perm_t *fsperm)
4292 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4293 while (node != NULL) {
4294 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4296 who_perm_t *who_perm = &node->who_perm;
4297 who_perm_fini(who_perm);
4298 uu_avl_remove(fsperm->fsp_sc_avl, node);
4303 node = uu_avl_first(fsperm->fsp_uge_avl);
4304 while (node != NULL) {
4305 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4307 who_perm_t *who_perm = &node->who_perm;
4308 who_perm_fini(who_perm);
4309 uu_avl_remove(fsperm->fsp_uge_avl, node);
4314 uu_avl_destroy(fsperm->fsp_sc_avl);
4315 uu_avl_destroy(fsperm->fsp_uge_avl);
4319 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4320 zfs_deleg_who_type_t who_type, const char *name, char locality)
4322 uu_avl_index_t idx = 0;
4324 deleg_perm_node_t *found_node = NULL;
4325 deleg_perm_t *deleg_perm = &node->dpn_perm;
4327 deleg_perm_init(deleg_perm, who_type, name);
4329 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4331 uu_avl_insert(avl, node, idx);
4334 deleg_perm = &node->dpn_perm;
4339 case ZFS_DELEG_LOCAL:
4340 deleg_perm->dp_local = B_TRUE;
4342 case ZFS_DELEG_DESCENDENT:
4343 deleg_perm->dp_descend = B_TRUE;
4348 assert(B_FALSE); /* invalid locality */
4353 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4355 nvpair_t *nvp = NULL;
4356 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4357 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4358 zfs_deleg_who_type_t who_type = who_perm->who_type;
4360 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4361 const char *name = nvpair_name(nvp);
4362 data_type_t type = nvpair_type(nvp);
4363 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4364 deleg_perm_node_t *node =
4365 safe_malloc(sizeof (deleg_perm_node_t));
4367 VERIFY(type == DATA_TYPE_BOOLEAN);
4369 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4370 set_deleg_perm_node(avl, node, who_type, name, locality);
4377 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4379 nvpair_t *nvp = NULL;
4380 fs_perm_set_t *fspset = fsperm->fsp_set;
4382 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4383 nvlist_t *nvl2 = NULL;
4384 const char *name = nvpair_name(nvp);
4385 uu_avl_t *avl = NULL;
4386 uu_avl_pool_t *avl_pool = NULL;
4387 zfs_deleg_who_type_t perm_type = name[0];
4388 char perm_locality = name[1];
4389 const char *perm_name = name + 3;
4390 boolean_t is_set = B_TRUE;
4391 who_perm_t *who_perm = NULL;
4393 assert('$' == name[2]);
4395 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4398 switch (perm_type) {
4399 case ZFS_DELEG_CREATE:
4400 case ZFS_DELEG_CREATE_SETS:
4401 case ZFS_DELEG_NAMED_SET:
4402 case ZFS_DELEG_NAMED_SET_SETS:
4403 avl_pool = fspset->fsps_named_set_avl_pool;
4404 avl = fsperm->fsp_sc_avl;
4406 case ZFS_DELEG_USER:
4407 case ZFS_DELEG_USER_SETS:
4408 case ZFS_DELEG_GROUP:
4409 case ZFS_DELEG_GROUP_SETS:
4410 case ZFS_DELEG_EVERYONE:
4411 case ZFS_DELEG_EVERYONE_SETS:
4412 avl_pool = fspset->fsps_who_perm_avl_pool;
4413 avl = fsperm->fsp_uge_avl;
4420 who_perm_node_t *found_node = NULL;
4421 who_perm_node_t *node = safe_malloc(
4422 sizeof (who_perm_node_t));
4423 who_perm = &node->who_perm;
4424 uu_avl_index_t idx = 0;
4426 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4427 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4429 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4431 if (avl == fsperm->fsp_uge_avl) {
4433 struct passwd *p = NULL;
4434 struct group *g = NULL;
4435 const char *nice_name = NULL;
4437 switch (perm_type) {
4438 case ZFS_DELEG_USER_SETS:
4439 case ZFS_DELEG_USER:
4440 rid = atoi(perm_name);
4443 nice_name = p->pw_name;
4445 case ZFS_DELEG_GROUP_SETS:
4446 case ZFS_DELEG_GROUP:
4447 rid = atoi(perm_name);
4450 nice_name = g->gr_name;
4456 if (nice_name != NULL)
4458 node->who_perm.who_ug_name,
4462 uu_avl_insert(avl, node, idx);
4465 who_perm = &node->who_perm;
4469 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4476 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4478 nvpair_t *nvp = NULL;
4479 uu_avl_index_t idx = 0;
4481 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4482 nvlist_t *nvl2 = NULL;
4483 const char *fsname = nvpair_name(nvp);
4484 data_type_t type = nvpair_type(nvp);
4485 fs_perm_t *fsperm = NULL;
4486 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4490 fsperm = &node->fspn_fsperm;
4492 VERIFY(DATA_TYPE_NVLIST == type);
4494 uu_list_node_init(node, &node->fspn_list_node,
4495 fspset->fsps_list_pool);
4497 idx = uu_list_numnodes(fspset->fsps_list);
4498 fs_perm_init(fsperm, fspset, fsname);
4500 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4503 (void) parse_fs_perm(fsperm, nvl2);
4505 uu_list_insert(fspset->fsps_list, node, idx);
4511 static inline const char *
4512 deleg_perm_comment(zfs_deleg_note_t note)
4514 const char *str = "";
4519 case ZFS_DELEG_NOTE_ALLOW:
4520 str = gettext("Must also have the permission that is being"
4521 "\n\t\t\t\tallowed");
4523 case ZFS_DELEG_NOTE_CLONE:
4524 str = gettext("Must also have the 'create' ability and 'mount'"
4525 "\n\t\t\t\tability in the origin file system");
4527 case ZFS_DELEG_NOTE_CREATE:
4528 str = gettext("Must also have the 'mount' ability");
4530 case ZFS_DELEG_NOTE_DESTROY:
4531 str = gettext("Must also have the 'mount' ability");
4533 case ZFS_DELEG_NOTE_DIFF:
4534 str = gettext("Allows lookup of paths within a dataset;"
4535 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4536 "\n\t\t\t\tin order to use zfs diff");
4538 case ZFS_DELEG_NOTE_HOLD:
4539 str = gettext("Allows adding a user hold to a snapshot");
4541 case ZFS_DELEG_NOTE_MOUNT:
4542 str = gettext("Allows mount/umount of ZFS datasets");
4544 case ZFS_DELEG_NOTE_PROMOTE:
4545 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4546 " 'promote' ability in the origin file system");
4548 case ZFS_DELEG_NOTE_RECEIVE:
4549 str = gettext("Must also have the 'mount' and 'create'"
4552 case ZFS_DELEG_NOTE_RELEASE:
4553 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4554 "might destroy the snapshot");
4556 case ZFS_DELEG_NOTE_RENAME:
4557 str = gettext("Must also have the 'mount' and 'create'"
4558 "\n\t\t\t\tability in the new parent");
4560 case ZFS_DELEG_NOTE_ROLLBACK:
4563 case ZFS_DELEG_NOTE_SEND:
4566 case ZFS_DELEG_NOTE_SHARE:
4567 str = gettext("Allows sharing file systems over NFS or SMB"
4568 "\n\t\t\t\tprotocols");
4570 case ZFS_DELEG_NOTE_SNAPSHOT:
4574 * case ZFS_DELEG_NOTE_VSCAN:
4575 * str = gettext("");
4579 case ZFS_DELEG_NOTE_GROUPQUOTA:
4580 str = gettext("Allows accessing any groupquota@... property");
4582 case ZFS_DELEG_NOTE_GROUPUSED:
4583 str = gettext("Allows reading any groupused@... property");
4585 case ZFS_DELEG_NOTE_USERPROP:
4586 str = gettext("Allows changing any user property");
4588 case ZFS_DELEG_NOTE_USERQUOTA:
4589 str = gettext("Allows accessing any userquota@... property");
4591 case ZFS_DELEG_NOTE_USERUSED:
4592 str = gettext("Allows reading any userused@... property");
4610 boolean_t recursive; /* unallow only */
4611 boolean_t prt_usage;
4613 boolean_t prt_perms;
4616 const char *dataset;
4620 prop_cmp(const void *a, const void *b)
4622 const char *str1 = *(const char **)a;
4623 const char *str2 = *(const char **)b;
4624 return (strcmp(str1, str2));
4628 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4630 const char *opt_desc[] = {
4631 "-h", gettext("show this help message and exit"),
4632 "-l", gettext("set permission locally"),
4633 "-d", gettext("set permission for descents"),
4634 "-u", gettext("set permission for user"),
4635 "-g", gettext("set permission for group"),
4636 "-e", gettext("set permission for everyone"),
4637 "-c", gettext("set create time permission"),
4638 "-s", gettext("define permission set"),
4640 "-r", gettext("remove permissions recursively"),
4642 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4643 size_t allow_size = unallow_size - 2;
4644 const char *props[ZFS_NUM_PROPS];
4647 FILE *fp = requested ? stdout : stderr;
4648 zprop_desc_t *pdtbl = zfs_prop_get_table();
4649 const char *fmt = gettext("%-16s %-14s\t%s\n");
4651 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4653 (void) fprintf(fp, gettext("Options:\n"));
4654 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4655 const char *opt = opt_desc[i++];
4656 const char *optdsc = opt_desc[i];
4657 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4660 (void) fprintf(fp, gettext("\nThe following permissions are "
4662 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4664 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4665 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4666 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4667 const char *perm_type = deleg_perm_type(perm_note);
4668 const char *perm_comment = deleg_perm_comment(perm_note);
4669 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4672 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4673 zprop_desc_t *pd = &pdtbl[i];
4674 if (pd->pd_visible != B_TRUE)
4677 if (pd->pd_attr == PROP_READONLY)
4680 props[count++] = pd->pd_name;
4682 props[count] = NULL;
4684 qsort(props, count, sizeof (char *), prop_cmp);
4686 for (i = 0; i < count; i++)
4687 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4690 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4692 exit(requested ? 0 : 2);
4695 static inline const char *
4696 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4699 if (un && argc == expected_argc - 1)
4701 else if (argc == expected_argc)
4702 *permsp = argv[argc - 2];
4704 allow_usage(un, B_FALSE,
4705 gettext("wrong number of parameters\n"));
4707 return (argv[argc - 1]);
4711 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4713 int uge_sum = opts->user + opts->group + opts->everyone;
4714 int csuge_sum = opts->create + opts->set + uge_sum;
4715 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4716 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4719 allow_usage(un, B_FALSE,
4720 gettext("-u, -g, and -e are mutually exclusive\n"));
4722 if (opts->prt_usage) {
4723 if (argc == 0 && all_sum == 0)
4724 allow_usage(un, B_TRUE, NULL);
4731 allow_usage(un, B_FALSE,
4732 gettext("invalid options combined with -s\n"));
4734 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4735 if (argv[0][0] != '@')
4736 allow_usage(un, B_FALSE,
4737 gettext("invalid set name: missing '@' prefix\n"));
4738 opts->who = argv[0];
4739 } else if (opts->create) {
4740 if (ldcsuge_sum > 1)
4741 allow_usage(un, B_FALSE,
4742 gettext("invalid options combined with -c\n"));
4743 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4744 } else if (opts->everyone) {
4746 allow_usage(un, B_FALSE,
4747 gettext("invalid options combined with -e\n"));
4748 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4749 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4751 opts->everyone = B_TRUE;
4754 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4755 } else if (argc == 1 && !un) {
4756 opts->prt_perms = B_TRUE;
4757 opts->dataset = argv[argc-1];
4759 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4760 opts->who = argv[0];
4763 if (!opts->local && !opts->descend) {
4764 opts->local = B_TRUE;
4765 opts->descend = B_TRUE;
4770 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4771 const char *who, char *perms, nvlist_t *top_nvl)
4774 char ld[2] = { '\0', '\0' };
4775 char who_buf[ZFS_MAXNAMELEN+32];
4776 char base_type = ZFS_DELEG_WHO_UNKNOWN;
4777 char set_type = ZFS_DELEG_WHO_UNKNOWN;
4778 nvlist_t *base_nvl = NULL;
4779 nvlist_t *set_nvl = NULL;
4782 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4784 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4788 case ZFS_DELEG_NAMED_SET_SETS:
4789 case ZFS_DELEG_NAMED_SET:
4790 set_type = ZFS_DELEG_NAMED_SET_SETS;
4791 base_type = ZFS_DELEG_NAMED_SET;
4792 ld[0] = ZFS_DELEG_NA;
4794 case ZFS_DELEG_CREATE_SETS:
4795 case ZFS_DELEG_CREATE:
4796 set_type = ZFS_DELEG_CREATE_SETS;
4797 base_type = ZFS_DELEG_CREATE;
4798 ld[0] = ZFS_DELEG_NA;
4800 case ZFS_DELEG_USER_SETS:
4801 case ZFS_DELEG_USER:
4802 set_type = ZFS_DELEG_USER_SETS;
4803 base_type = ZFS_DELEG_USER;
4805 ld[0] = ZFS_DELEG_LOCAL;
4807 ld[1] = ZFS_DELEG_DESCENDENT;
4809 case ZFS_DELEG_GROUP_SETS:
4810 case ZFS_DELEG_GROUP:
4811 set_type = ZFS_DELEG_GROUP_SETS;
4812 base_type = ZFS_DELEG_GROUP;
4814 ld[0] = ZFS_DELEG_LOCAL;
4816 ld[1] = ZFS_DELEG_DESCENDENT;
4818 case ZFS_DELEG_EVERYONE_SETS:
4819 case ZFS_DELEG_EVERYONE:
4820 set_type = ZFS_DELEG_EVERYONE_SETS;
4821 base_type = ZFS_DELEG_EVERYONE;
4823 ld[0] = ZFS_DELEG_LOCAL;
4825 ld[1] = ZFS_DELEG_DESCENDENT;
4830 if (perms != NULL) {
4832 char *end = curr + strlen(perms);
4834 while (curr < end) {
4835 char *delim = strchr(curr, ',');
4846 (void) nvlist_add_boolean(nvl, curr);
4852 for (i = 0; i < 2; i++) {
4853 char locality = ld[i];
4857 if (!nvlist_empty(base_nvl)) {
4859 (void) snprintf(who_buf,
4860 sizeof (who_buf), "%c%c$%s",
4861 base_type, locality, who);
4863 (void) snprintf(who_buf,
4864 sizeof (who_buf), "%c%c$",
4865 base_type, locality);
4867 (void) nvlist_add_nvlist(top_nvl, who_buf,
4872 if (!nvlist_empty(set_nvl)) {
4874 (void) snprintf(who_buf,
4875 sizeof (who_buf), "%c%c$%s",
4876 set_type, locality, who);
4878 (void) snprintf(who_buf,
4879 sizeof (who_buf), "%c%c$",
4880 set_type, locality);
4882 (void) nvlist_add_nvlist(top_nvl, who_buf,
4887 for (i = 0; i < 2; i++) {
4888 char locality = ld[i];
4893 (void) snprintf(who_buf, sizeof (who_buf),
4894 "%c%c$%s", base_type, locality, who);
4896 (void) snprintf(who_buf, sizeof (who_buf),
4897 "%c%c$", base_type, locality);
4898 (void) nvlist_add_boolean(top_nvl, who_buf);
4901 (void) snprintf(who_buf, sizeof (who_buf),
4902 "%c%c$%s", set_type, locality, who);
4904 (void) snprintf(who_buf, sizeof (who_buf),
4905 "%c%c$", set_type, locality);
4906 (void) nvlist_add_boolean(top_nvl, who_buf);
4912 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4914 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4918 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4919 opts->descend, opts->who, opts->perms, *nvlp);
4920 } else if (opts->create) {
4921 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4922 opts->descend, NULL, opts->perms, *nvlp);
4923 } else if (opts->everyone) {
4924 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4925 opts->descend, NULL, opts->perms, *nvlp);
4927 char *curr = opts->who;
4928 char *end = curr + strlen(curr);
4930 while (curr < end) {
4932 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
4934 char *delim = strchr(curr, ',');
4937 struct passwd *p = NULL;
4938 struct group *g = NULL;
4946 rid = (uid_t)strtol(curr, &endch, 0);
4948 who_type = ZFS_DELEG_USER;
4957 (void) snprintf(errbuf, 256, gettext(
4958 "invalid user %s"), curr);
4959 allow_usage(un, B_TRUE, errbuf);
4961 } else if (opts->group) {
4962 who_type = ZFS_DELEG_GROUP;
4971 (void) snprintf(errbuf, 256, gettext(
4972 "invalid group %s"), curr);
4973 allow_usage(un, B_TRUE, errbuf);
4976 if (*endch != '\0') {
4983 if (*endch != '\0') {
4991 who_type = ZFS_DELEG_USER;
4993 } else if (g != NULL) {
4994 who_type = ZFS_DELEG_GROUP;
4997 (void) snprintf(errbuf, 256, gettext(
4998 "invalid user/group %s"), curr);
4999 allow_usage(un, B_TRUE, errbuf);
5003 (void) sprintf(id, "%u", rid);
5006 store_allow_perm(who_type, opts->local,
5007 opts->descend, who, opts->perms, *nvlp);
5016 print_set_creat_perms(uu_avl_t *who_avl)
5018 const char *sc_title[] = {
5019 gettext("Permission sets:\n"),
5020 gettext("Create time permissions:\n"),
5023 const char **title_ptr = sc_title;
5024 who_perm_node_t *who_node = NULL;
5025 int prev_weight = -1;
5027 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5028 who_node = uu_avl_next(who_avl, who_node)) {
5029 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5030 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5031 const char *who_name = who_node->who_perm.who_name;
5032 int weight = who_type2weight(who_type);
5033 boolean_t first = B_TRUE;
5034 deleg_perm_node_t *deleg_node;
5036 if (prev_weight != weight) {
5037 (void) printf("%s", *title_ptr++);
5038 prev_weight = weight;
5041 if (who_name == NULL || strnlen(who_name, 1) == 0)
5042 (void) printf("\t");
5044 (void) printf("\t%s ", who_name);
5046 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5047 deleg_node = uu_avl_next(avl, deleg_node)) {
5050 deleg_node->dpn_perm.dp_name);
5053 (void) printf(",%s",
5054 deleg_node->dpn_perm.dp_name);
5057 (void) printf("\n");
5062 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5065 who_perm_node_t *who_node = NULL;
5066 boolean_t prt_title = B_TRUE;
5067 uu_avl_walk_t *walk;
5069 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5072 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5073 const char *who_name = who_node->who_perm.who_name;
5074 const char *nice_who_name = who_node->who_perm.who_ug_name;
5075 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5076 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5078 deleg_perm_node_t *deleg_node;
5079 boolean_t prt_who = B_TRUE;
5081 for (deleg_node = uu_avl_first(avl);
5083 deleg_node = uu_avl_next(avl, deleg_node)) {
5084 if (local != deleg_node->dpn_perm.dp_local ||
5085 descend != deleg_node->dpn_perm.dp_descend)
5089 const char *who = NULL;
5091 prt_title = B_FALSE;
5092 (void) printf("%s", title);
5096 case ZFS_DELEG_USER_SETS:
5097 case ZFS_DELEG_USER:
5098 who = gettext("user");
5100 who_name = nice_who_name;
5102 case ZFS_DELEG_GROUP_SETS:
5103 case ZFS_DELEG_GROUP:
5104 who = gettext("group");
5106 who_name = nice_who_name;
5108 case ZFS_DELEG_EVERYONE_SETS:
5109 case ZFS_DELEG_EVERYONE:
5110 who = gettext("everyone");
5117 if (who_name == NULL)
5118 (void) printf("\t%s", who);
5120 (void) printf("\t%s %s", who, who_name);
5123 (void) printf("%c%s", delim,
5124 deleg_node->dpn_perm.dp_name);
5129 (void) printf("\n");
5132 uu_avl_walk_end(walk);
5136 print_fs_perms(fs_perm_set_t *fspset)
5138 fs_perm_node_t *node = NULL;
5139 char buf[ZFS_MAXNAMELEN+32];
5140 const char *dsname = buf;
5142 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5143 node = uu_list_next(fspset->fsps_list, node)) {
5144 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5145 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5148 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5149 gettext("---- Permissions on %s "),
5150 node->fspn_fsperm.fsp_name);
5151 (void) printf("%s", dsname);
5152 left = 70 - strlen(buf);
5155 (void) printf("\n");
5157 print_set_creat_perms(sc_avl);
5158 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5159 gettext("Local permissions:\n"));
5160 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5161 gettext("Descendent permissions:\n"));
5162 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5163 gettext("Local+Descendent permissions:\n"));
5167 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5169 struct deleg_perms {
5175 set_deleg_perms(zfs_handle_t *zhp, void *data)
5177 struct deleg_perms *perms = (struct deleg_perms *)data;
5178 zfs_type_t zfs_type = zfs_get_type(zhp);
5180 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5183 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5187 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5190 nvlist_t *perm_nvl = NULL;
5191 nvlist_t *update_perm_nvl = NULL;
5194 struct allow_opts opts = { 0 };
5196 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5199 while ((c = getopt(argc, argv, optstr)) != -1) {
5202 opts.local = B_TRUE;
5205 opts.descend = B_TRUE;
5211 opts.group = B_TRUE;
5214 opts.everyone = B_TRUE;
5220 opts.create = B_TRUE;
5223 opts.recursive = B_TRUE;
5226 (void) fprintf(stderr, gettext("missing argument for "
5227 "'%c' option\n"), optopt);
5231 opts.prt_usage = B_TRUE;
5234 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5243 /* check arguments */
5244 parse_allow_args(argc, argv, un, &opts);
5246 /* try to open the dataset */
5247 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5248 ZFS_TYPE_VOLUME)) == NULL) {
5249 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5254 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5257 fs_perm_set_init(&fs_perm_set);
5258 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5259 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5264 print_fs_perms(&fs_perm_set);
5266 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5267 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5270 if (un && opts.recursive) {
5271 struct deleg_perms data = { un, update_perm_nvl };
5272 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5281 nvlist_free(perm_nvl);
5282 if (update_perm_nvl != NULL)
5283 nvlist_free(update_perm_nvl);
5285 fs_perm_set_fini(&fs_perm_set);
5293 zfs_do_allow(int argc, char **argv)
5295 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5299 zfs_do_unallow(int argc, char **argv)
5301 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5305 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5310 boolean_t recursive = B_FALSE;
5311 const char *opts = holding ? "rt" : "r";
5315 while ((c = getopt(argc, argv, opts)) != -1) {
5321 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5330 /* check number of arguments */
5338 if (holding && tag[0] == '.') {
5339 /* tags starting with '.' are reserved for libzfs */
5340 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5344 for (i = 0; i < argc; ++i) {
5346 char parent[ZFS_MAXNAMELEN];
5348 char *path = argv[i];
5350 delim = strchr(path, '@');
5351 if (delim == NULL) {
5352 (void) fprintf(stderr,
5353 gettext("'%s' is not a snapshot\n"), path);
5357 (void) strncpy(parent, path, delim - path);
5358 parent[delim - path] = '\0';
5360 zhp = zfs_open(g_zfs, parent,
5361 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5367 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5370 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5376 return (errors != 0);
5380 * zfs hold [-r] [-t] <tag> <snap> ...
5382 * -r Recursively hold
5384 * Apply a user-hold with the given tag to the list of snapshots.
5387 zfs_do_hold(int argc, char **argv)
5389 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5393 * zfs release [-r] <tag> <snap> ...
5395 * -r Recursively release
5397 * Release a user-hold with the given tag from the list of snapshots.
5400 zfs_do_release(int argc, char **argv)
5402 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5405 typedef struct holds_cbdata {
5406 boolean_t cb_recursive;
5407 const char *cb_snapname;
5409 size_t cb_max_namelen;
5410 size_t cb_max_taglen;
5413 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5414 #define DATETIME_BUF_LEN (32)
5419 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
5422 nvpair_t *nvp = NULL;
5423 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5427 for (i = 0; i < 3; i++) {
5428 col = gettext(hdr_cols[i]);
5430 (void) printf("%-*s ", i ? tagwidth : nwidth,
5433 (void) printf("%s\n", col);
5437 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5438 char *zname = nvpair_name(nvp);
5440 nvpair_t *nvp2 = NULL;
5441 (void) nvpair_value_nvlist(nvp, &nvl2);
5442 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5443 char tsbuf[DATETIME_BUF_LEN];
5444 char *tagname = nvpair_name(nvp2);
5448 char sep = scripted ? '\t' : ' ';
5449 int sepnum = scripted ? 1 : 2;
5451 (void) nvpair_value_uint64(nvp2, &val);
5453 (void) localtime_r(&time, &t);
5454 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5455 gettext(STRFTIME_FMT_STR), &t);
5457 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5458 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5464 * Generic callback function to list a dataset or snapshot.
5467 holds_callback(zfs_handle_t *zhp, void *data)
5469 holds_cbdata_t *cbp = data;
5470 nvlist_t *top_nvl = *cbp->cb_nvlp;
5471 nvlist_t *nvl = NULL;
5472 nvpair_t *nvp = NULL;
5473 const char *zname = zfs_get_name(zhp);
5474 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5476 if (cbp->cb_recursive) {
5477 const char *snapname;
5478 char *delim = strchr(zname, '@');
5482 snapname = delim + 1;
5483 if (strcmp(cbp->cb_snapname, snapname))
5487 if (zfs_get_holds(zhp, &nvl) != 0)
5490 if (znamelen > cbp->cb_max_namelen)
5491 cbp->cb_max_namelen = znamelen;
5493 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5494 const char *tag = nvpair_name(nvp);
5495 size_t taglen = strnlen(tag, MAXNAMELEN);
5496 if (taglen > cbp->cb_max_taglen)
5497 cbp->cb_max_taglen = taglen;
5500 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5504 * zfs holds [-r] <snap> ...
5506 * -r Recursively hold
5509 zfs_do_holds(int argc, char **argv)
5514 boolean_t scripted = B_FALSE;
5515 boolean_t recursive = B_FALSE;
5516 const char *opts = "rH";
5519 int types = ZFS_TYPE_SNAPSHOT;
5520 holds_cbdata_t cb = { 0 };
5527 while ((c = getopt(argc, argv, opts)) != -1) {
5536 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5543 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5544 flags |= ZFS_ITER_RECURSE;
5550 /* check number of arguments */
5554 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5557 for (i = 0; i < argc; ++i) {
5558 char *snapshot = argv[i];
5560 const char *snapname;
5562 delim = strchr(snapshot, '@');
5563 if (delim == NULL) {
5564 (void) fprintf(stderr,
5565 gettext("'%s' is not a snapshot\n"), snapshot);
5569 snapname = delim + 1;
5571 snapshot[delim - snapshot] = '\0';
5573 cb.cb_recursive = recursive;
5574 cb.cb_snapname = snapname;
5578 * 1. collect holds data, set format options
5580 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5581 holds_callback, &cb);
5587 * 2. print holds data
5589 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5591 if (nvlist_empty(nvl))
5592 (void) fprintf(stderr, gettext("no datasets available\n"));
5596 return (0 != errors);
5599 #define CHECK_SPINNER 30
5600 #define SPINNER_TIME 3 /* seconds */
5601 #define MOUNT_TIME 5 /* seconds */
5604 get_one_dataset(zfs_handle_t *zhp, void *data)
5606 static char *spin[] = { "-", "\\", "|", "/" };
5607 static int spinval = 0;
5608 static int spincheck = 0;
5609 static time_t last_spin_time = (time_t)0;
5610 get_all_cb_t *cbp = data;
5611 zfs_type_t type = zfs_get_type(zhp);
5613 if (cbp->cb_verbose) {
5614 if (--spincheck < 0) {
5615 time_t now = time(NULL);
5616 if (last_spin_time + SPINNER_TIME < now) {
5617 update_progress(spin[spinval++ % 4]);
5618 last_spin_time = now;
5620 spincheck = CHECK_SPINNER;
5625 * Iterate over any nested datasets.
5627 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5633 * Skip any datasets whose type does not match.
5635 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5639 libzfs_add_handle(cbp, zhp);
5640 assert(cbp->cb_used <= cbp->cb_alloc);
5646 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5648 get_all_cb_t cb = { 0 };
5649 cb.cb_verbose = verbose;
5650 cb.cb_getone = get_one_dataset;
5653 set_progress_header(gettext("Reading ZFS config"));
5654 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5656 *dslist = cb.cb_handles;
5657 *count = cb.cb_used;
5660 finish_progress(gettext("done."));
5664 * Generic callback for sharing or mounting filesystems. Because the code is so
5665 * similar, we have a common function with an extra parameter to determine which
5666 * mode we are using.
5668 #define OP_SHARE 0x1
5669 #define OP_MOUNT 0x2
5672 * Share or mount a dataset.
5675 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5676 boolean_t explicit, const char *options)
5678 char mountpoint[ZFS_MAXPROPLEN];
5679 char shareopts[ZFS_MAXPROPLEN];
5680 char smbshareopts[ZFS_MAXPROPLEN];
5681 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5683 uint64_t zoned, canmount;
5684 boolean_t shared_nfs, shared_smb;
5686 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5689 * Check to make sure we can mount/share this dataset. If we
5690 * are in the global zone and the filesystem is exported to a
5691 * local zone, or if we are in a local zone and the
5692 * filesystem is not exported, then it is an error.
5694 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5696 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5700 (void) fprintf(stderr, gettext("cannot %s '%s': "
5701 "dataset is exported to a local zone\n"), cmdname,
5705 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5709 (void) fprintf(stderr, gettext("cannot %s '%s': "
5710 "permission denied\n"), cmdname,
5716 * Ignore any filesystems which don't apply to us. This
5717 * includes those with a legacy mountpoint, or those with
5718 * legacy share options.
5720 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5721 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5722 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5723 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5724 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5725 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5727 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5728 strcmp(smbshareopts, "off") == 0) {
5732 (void) fprintf(stderr, gettext("cannot share '%s': "
5733 "legacy share\n"), zfs_get_name(zhp));
5734 (void) fprintf(stderr, gettext("use share(1M) to "
5735 "share this filesystem, or set "
5736 "sharenfs property on\n"));
5741 * We cannot share or mount legacy filesystems. If the
5742 * shareopts is non-legacy but the mountpoint is legacy, we
5743 * treat it as a legacy share.
5745 if (strcmp(mountpoint, "legacy") == 0) {
5749 (void) fprintf(stderr, gettext("cannot %s '%s': "
5750 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5751 (void) fprintf(stderr, gettext("use %s(1M) to "
5752 "%s this filesystem\n"), cmdname, cmdname);
5756 if (strcmp(mountpoint, "none") == 0) {
5760 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5761 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5766 * canmount explicit outcome
5767 * on no pass through
5768 * on yes pass through
5770 * off yes display error, return 1
5771 * noauto no return 0
5772 * noauto yes pass through
5774 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5775 if (canmount == ZFS_CANMOUNT_OFF) {
5779 (void) fprintf(stderr, gettext("cannot %s '%s': "
5780 "'canmount' property is set to 'off'\n"), cmdname,
5783 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5788 * At this point, we have verified that the mountpoint and/or
5789 * shareopts are appropriate for auto management. If the
5790 * filesystem is already mounted or shared, return (failing
5791 * for explicit requests); otherwise mount or share the
5797 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5798 shared_smb = zfs_is_shared_smb(zhp, NULL);
5800 if ((shared_nfs && shared_smb) ||
5801 ((shared_nfs && strcmp(shareopts, "on") == 0) &&
5802 (strcmp(smbshareopts, "off") == 0)) ||
5803 ((shared_smb && strcmp(smbshareopts, "on") == 0) &&
5804 (strcmp(shareopts, "off") == 0))) {
5808 (void) fprintf(stderr, gettext("cannot share "
5809 "'%s': filesystem already shared\n"),
5814 if (!zfs_is_mounted(zhp, NULL) &&
5815 zfs_mount(zhp, NULL, 0) != 0)
5818 if (protocol == NULL) {
5819 if (zfs_shareall(zhp) != 0)
5821 } else if (strcmp(protocol, "nfs") == 0) {
5822 if (zfs_share_nfs(zhp))
5824 } else if (strcmp(protocol, "smb") == 0) {
5825 if (zfs_share_smb(zhp))
5828 (void) fprintf(stderr, gettext("cannot share "
5829 "'%s': invalid share type '%s' "
5831 zfs_get_name(zhp), protocol);
5838 if (options == NULL)
5839 mnt.mnt_mntopts = "";
5841 mnt.mnt_mntopts = (char *)options;
5843 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5844 zfs_is_mounted(zhp, NULL)) {
5848 (void) fprintf(stderr, gettext("cannot mount "
5849 "'%s': filesystem already mounted\n"),
5854 if (zfs_mount(zhp, options, flags) != 0)
5863 * Reports progress in the form "(current/total)". Not thread-safe.
5866 report_mount_progress(int current, int total)
5868 static time_t last_progress_time = 0;
5869 time_t now = time(NULL);
5872 /* report 1..n instead of 0..n-1 */
5875 /* display header if we're here for the first time */
5877 set_progress_header(gettext("Mounting ZFS filesystems"));
5878 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5879 /* too soon to report again */
5883 last_progress_time = now;
5885 (void) sprintf(info, "(%d/%d)", current, total);
5887 if (current == total)
5888 finish_progress(info);
5890 update_progress(info);
5894 append_options(char *mntopts, char *newopts)
5896 int len = strlen(mntopts);
5898 /* original length plus new string to append plus 1 for the comma */
5899 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5900 (void) fprintf(stderr, gettext("the opts argument for "
5901 "'%s' option is too long (more than %d chars)\n"),
5902 "-o", MNT_LINE_MAX);
5907 mntopts[len++] = ',';
5909 (void) strcpy(&mntopts[len], newopts);
5913 share_mount(int op, int argc, char **argv)
5916 boolean_t verbose = B_FALSE;
5918 char *options = NULL;
5922 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5932 if (*optarg == '\0') {
5933 (void) fprintf(stderr, gettext("empty mount "
5934 "options (-o) specified\n"));
5938 if (options == NULL)
5939 options = safe_malloc(MNT_LINE_MAX + 1);
5941 /* option validation is done later */
5942 append_options(options, optarg);
5945 flags |= MS_OVERLAY;
5948 (void) fprintf(stderr, gettext("missing argument for "
5949 "'%c' option\n"), optopt);
5953 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5962 /* check number of arguments */
5964 zfs_handle_t **dslist = NULL;
5965 size_t i, count = 0;
5966 char *protocol = NULL;
5968 if (op == OP_SHARE && argc > 0) {
5969 if (strcmp(argv[0], "nfs") != 0 &&
5970 strcmp(argv[0], "smb") != 0) {
5971 (void) fprintf(stderr, gettext("share type "
5972 "must be 'nfs' or 'smb'\n"));
5981 (void) fprintf(stderr, gettext("too many arguments\n"));
5985 start_progress_timer();
5986 get_all_datasets(&dslist, &count, verbose);
5991 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5993 for (i = 0; i < count; i++) {
5995 report_mount_progress(i, count);
5997 if (share_mount_one(dslist[i], op, flags, protocol,
5998 B_FALSE, options) != 0)
6000 zfs_close(dslist[i]);
6004 } else if (argc == 0) {
6005 struct mnttab entry;
6007 if ((op == OP_SHARE) || (options != NULL)) {
6008 (void) fprintf(stderr, gettext("missing filesystem "
6009 "argument (specify -a for all)\n"));
6014 * When mount is given no arguments, go through /etc/mtab and
6015 * display any active ZFS mounts. We hide any snapshots, since
6016 * they are controlled automatically.
6019 /* Reopen MNTTAB to prevent reading stale data from open file */
6020 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6023 while (getmntent(mnttab_file, &entry) == 0) {
6024 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6025 strchr(entry.mnt_special, '@') != NULL)
6028 (void) printf("%-30s %s\n", entry.mnt_special,
6036 (void) fprintf(stderr,
6037 gettext("too many arguments\n"));
6041 if ((zhp = zfs_open(g_zfs, argv[0],
6042 ZFS_TYPE_FILESYSTEM)) == NULL) {
6045 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6055 * zfs mount -a [nfs]
6056 * zfs mount filesystem
6058 * Mount all filesystems, or mount the given filesystem.
6061 zfs_do_mount(int argc, char **argv)
6063 return (share_mount(OP_MOUNT, argc, argv));
6067 * zfs share -a [nfs | smb]
6068 * zfs share filesystem
6070 * Share all filesystems, or share the given filesystem.
6073 zfs_do_share(int argc, char **argv)
6075 return (share_mount(OP_SHARE, argc, argv));
6078 typedef struct unshare_unmount_node {
6079 zfs_handle_t *un_zhp;
6081 uu_avl_node_t un_avlnode;
6082 } unshare_unmount_node_t;
6086 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6088 const unshare_unmount_node_t *l = larg;
6089 const unshare_unmount_node_t *r = rarg;
6091 return (strcmp(l->un_mountp, r->un_mountp));
6095 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6096 * absolute path, find the entry /etc/mtab, verify that its a ZFS filesystem,
6097 * and unmount it appropriately.
6100 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6104 struct stat64 statbuf;
6105 struct extmnttab entry;
6106 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6110 * Search for the path in /etc/mtab. Rather than looking for the
6111 * specific path, which can be fooled by non-standard paths (i.e. ".."
6112 * or "//"), we stat() the path and search for the corresponding
6113 * (major,minor) device pair.
6115 if (stat64(path, &statbuf) != 0) {
6116 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6117 cmdname, path, strerror(errno));
6120 path_inode = statbuf.st_ino;
6123 * Search for the given (major,minor) pair in the mount table.
6126 /* Reopen MNTTAB to prevent reading stale data from open file */
6127 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6130 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6131 if (entry.mnt_major == major(statbuf.st_dev) &&
6132 entry.mnt_minor == minor(statbuf.st_dev))
6136 if (op == OP_SHARE) {
6137 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6138 "currently mounted\n"), cmdname, path);
6141 (void) fprintf(stderr, gettext("warning: %s not in mtab\n"),
6143 if ((ret = umount2(path, flags)) != 0)
6144 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6149 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6150 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6151 "filesystem\n"), cmdname, path);
6155 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6156 ZFS_TYPE_FILESYSTEM)) == NULL)
6160 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6161 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6162 cmdname, path, strerror(errno));
6164 } else if (statbuf.st_ino != path_inode) {
6165 (void) fprintf(stderr, gettext("cannot "
6166 "%s '%s': not a mountpoint\n"), cmdname, path);
6170 if (op == OP_SHARE) {
6171 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6172 char smbshare_prop[ZFS_MAXPROPLEN];
6174 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6175 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6176 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6177 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6179 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6180 strcmp(smbshare_prop, "off") == 0) {
6181 (void) fprintf(stderr, gettext("cannot unshare "
6182 "'%s': legacy share\n"), path);
6183 (void) fprintf(stderr, gettext("use exportfs(8) "
6184 "or smbcontrol(1) to unshare this filesystem\n"));
6185 } else if (!zfs_is_shared(zhp)) {
6186 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6187 "not currently shared\n"), path);
6189 ret = zfs_unshareall_bypath(zhp, path);
6192 char mtpt_prop[ZFS_MAXPROPLEN];
6194 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6195 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6198 ret = zfs_unmount(zhp, NULL, flags);
6199 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6200 (void) fprintf(stderr, gettext("cannot unmount "
6201 "'%s': legacy mountpoint\n"),
6203 (void) fprintf(stderr, gettext("use umount(8) "
6204 "to unmount this filesystem\n"));
6206 ret = zfs_unmountall(zhp, flags);
6217 * Generic callback for unsharing or unmounting a filesystem.
6220 unshare_unmount(int op, int argc, char **argv)
6227 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6228 char sharesmb[ZFS_MAXPROPLEN];
6231 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6240 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6251 * We could make use of zfs_for_each() to walk all datasets in
6252 * the system, but this would be very inefficient, especially
6253 * since we would have to linearly search /etc/mtab for each
6254 * one. Instead, do one pass through /etc/mtab looking for
6255 * zfs entries and call zfs_unmount() for each one.
6257 * Things get a little tricky if the administrator has created
6258 * mountpoints beneath other ZFS filesystems. In this case, we
6259 * have to unmount the deepest filesystems first. To accomplish
6260 * this, we place all the mountpoints in an AVL tree sorted by
6261 * the special type (dataset name), and walk the result in
6262 * reverse to make sure to get any snapshots first.
6264 struct mnttab entry;
6265 uu_avl_pool_t *pool;
6266 uu_avl_t *tree = NULL;
6267 unshare_unmount_node_t *node;
6269 uu_avl_walk_t *walk;
6272 (void) fprintf(stderr, gettext("too many arguments\n"));
6276 if (((pool = uu_avl_pool_create("unmount_pool",
6277 sizeof (unshare_unmount_node_t),
6278 offsetof(unshare_unmount_node_t, un_avlnode),
6279 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6280 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6283 /* Reopen MNTTAB to prevent reading stale data from open file */
6284 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6287 while (getmntent(mnttab_file, &entry) == 0) {
6289 /* ignore non-ZFS entries */
6290 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6293 /* ignore snapshots */
6294 if (strchr(entry.mnt_special, '@') != NULL)
6297 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6298 ZFS_TYPE_FILESYSTEM)) == NULL) {
6305 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6307 sizeof (nfs_mnt_prop),
6308 NULL, NULL, 0, B_FALSE) == 0);
6309 if (strcmp(nfs_mnt_prop, "off") != 0)
6311 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6313 sizeof (nfs_mnt_prop),
6314 NULL, NULL, 0, B_FALSE) == 0);
6315 if (strcmp(nfs_mnt_prop, "off") == 0)
6319 /* Ignore legacy mounts */
6320 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6322 sizeof (nfs_mnt_prop),
6323 NULL, NULL, 0, B_FALSE) == 0);
6324 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6326 /* Ignore canmount=noauto mounts */
6327 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6328 ZFS_CANMOUNT_NOAUTO)
6334 node = safe_malloc(sizeof (unshare_unmount_node_t));
6336 node->un_mountp = safe_strdup(entry.mnt_mountp);
6338 uu_avl_node_init(node, &node->un_avlnode, pool);
6340 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6341 uu_avl_insert(tree, node, idx);
6343 zfs_close(node->un_zhp);
6344 free(node->un_mountp);
6350 * Walk the AVL tree in reverse, unmounting each filesystem and
6351 * removing it from the AVL tree in the process.
6353 if ((walk = uu_avl_walk_start(tree,
6354 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6357 while ((node = uu_avl_walk_next(walk)) != NULL) {
6358 uu_avl_remove(tree, node);
6362 if (zfs_unshareall_bypath(node->un_zhp,
6363 node->un_mountp) != 0)
6368 if (zfs_unmount(node->un_zhp,
6369 node->un_mountp, flags) != 0)
6374 zfs_close(node->un_zhp);
6375 free(node->un_mountp);
6379 uu_avl_walk_end(walk);
6380 uu_avl_destroy(tree);
6381 uu_avl_pool_destroy(pool);
6386 (void) fprintf(stderr,
6387 gettext("missing filesystem argument\n"));
6389 (void) fprintf(stderr,
6390 gettext("too many arguments\n"));
6395 * We have an argument, but it may be a full path or a ZFS
6396 * filesystem. Pass full paths off to unmount_path() (shared by
6397 * manual_unmount), otherwise open the filesystem and pass to
6400 if (argv[0][0] == '/')
6401 return (unshare_unmount_path(op, argv[0],
6404 if ((zhp = zfs_open(g_zfs, argv[0],
6405 ZFS_TYPE_FILESYSTEM)) == NULL)
6408 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6409 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6410 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6411 NULL, 0, B_FALSE) == 0);
6415 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6417 sizeof (nfs_mnt_prop),
6418 NULL, NULL, 0, B_FALSE) == 0);
6419 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6420 sharesmb, sizeof (sharesmb), NULL, NULL,
6423 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6424 strcmp(sharesmb, "off") == 0) {
6425 (void) fprintf(stderr, gettext("cannot "
6426 "unshare '%s': legacy share\n"),
6428 (void) fprintf(stderr, gettext("use "
6429 "unshare(1M) to unshare this "
6432 } else if (!zfs_is_shared(zhp)) {
6433 (void) fprintf(stderr, gettext("cannot "
6434 "unshare '%s': not currently "
6435 "shared\n"), zfs_get_name(zhp));
6437 } else if (zfs_unshareall(zhp) != 0) {
6443 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6444 (void) fprintf(stderr, gettext("cannot "
6445 "unmount '%s': legacy "
6446 "mountpoint\n"), zfs_get_name(zhp));
6447 (void) fprintf(stderr, gettext("use "
6448 "umount(1M) to unmount this "
6451 } else if (!zfs_is_mounted(zhp, NULL)) {
6452 (void) fprintf(stderr, gettext("cannot "
6453 "unmount '%s': not currently "
6457 } else if (zfs_unmountall(zhp, flags) != 0) {
6471 * zfs unmount filesystem
6473 * Unmount all filesystems, or a specific ZFS filesystem.
6476 zfs_do_unmount(int argc, char **argv)
6478 return (unshare_unmount(OP_MOUNT, argc, argv));
6483 * zfs unshare filesystem
6485 * Unshare all filesystems, or a specific ZFS filesystem.
6488 zfs_do_unshare(int argc, char **argv)
6490 return (unshare_unmount(OP_SHARE, argc, argv));
6494 find_command_idx(char *command, int *idx)
6498 for (i = 0; i < NCOMMAND; i++) {
6499 if (command_table[i].name == NULL)
6502 if (strcmp(command, command_table[i].name) == 0) {
6511 zfs_do_diff(int argc, char **argv)
6515 char *tosnap = NULL;
6516 char *fromsnap = NULL;
6521 while ((c = getopt(argc, argv, "FHt")) != -1) {
6524 flags |= ZFS_DIFF_CLASSIFY;
6527 flags |= ZFS_DIFF_PARSEABLE;
6530 flags |= ZFS_DIFF_TIMESTAMP;
6533 (void) fprintf(stderr,
6534 gettext("invalid option '%c'\n"), optopt);
6543 (void) fprintf(stderr,
6544 gettext("must provide at least one snapshot name\n"));
6549 (void) fprintf(stderr, gettext("too many arguments\n"));
6554 tosnap = (argc == 2) ? argv[1] : NULL;
6557 if (*fromsnap != '@')
6558 copy = strdup(fromsnap);
6560 copy = strdup(tosnap);
6564 if ((atp = strchr(copy, '@')))
6567 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6573 * Ignore SIGPIPE so that the library can give us
6574 * information on any failure
6576 (void) sigignore(SIGPIPE);
6578 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6586 * zfs bookmark <fs@snap> <fs#bmark>
6588 * Creates a bookmark with the given name from the given snapshot.
6591 zfs_do_bookmark(int argc, char **argv)
6593 char snapname[ZFS_MAXNAMELEN];
6600 while ((c = getopt(argc, argv, "")) != -1) {
6603 (void) fprintf(stderr,
6604 gettext("invalid option '%c'\n"), optopt);
6612 /* check number of arguments */
6614 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6618 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6622 if (strchr(argv[1], '#') == NULL) {
6623 (void) fprintf(stderr,
6624 gettext("invalid bookmark name '%s' -- "
6625 "must contain a '#'\n"), argv[1]);
6629 if (argv[0][0] == '@') {
6631 * Snapshot name begins with @.
6632 * Default to same fs as bookmark.
6634 (void) strncpy(snapname, argv[1], sizeof (snapname));
6635 *strchr(snapname, '#') = '\0';
6636 (void) strlcat(snapname, argv[0], sizeof (snapname));
6638 (void) strncpy(snapname, argv[0], sizeof (snapname));
6640 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6646 nvl = fnvlist_alloc();
6647 fnvlist_add_string(nvl, argv[1], snapname);
6648 ret = lzc_bookmark(nvl, NULL);
6652 const char *err_msg;
6655 (void) snprintf(errbuf, sizeof (errbuf),
6656 dgettext(TEXT_DOMAIN,
6657 "cannot create bookmark '%s'"), argv[1]);
6661 err_msg = "bookmark is in a different pool";
6664 err_msg = "bookmark exists";
6667 err_msg = "invalid argument";
6670 err_msg = "bookmark feature not enabled";
6673 err_msg = "out of space";
6676 err_msg = "unknown error";
6679 (void) fprintf(stderr, "%s: %s\n", errbuf,
6680 dgettext(TEXT_DOMAIN, err_msg));
6691 main(int argc, char **argv)
6697 (void) setlocale(LC_ALL, "");
6698 (void) textdomain(TEXT_DOMAIN);
6700 dprintf_setup(&argc, argv);
6705 * Make sure the user has specified some command.
6708 (void) fprintf(stderr, gettext("missing command\n"));
6715 * The 'umount' command is an alias for 'unmount'
6717 if (strcmp(cmdname, "umount") == 0)
6718 cmdname = "unmount";
6721 * The 'recv' command is an alias for 'receive'
6723 if (strcmp(cmdname, "recv") == 0)
6724 cmdname = "receive";
6727 * The 'snap' command is an alias for 'snapshot'
6729 if (strcmp(cmdname, "snap") == 0)
6730 cmdname = "snapshot";
6735 if ((strcmp(cmdname, "-?") == 0) ||
6736 (strcmp(cmdname, "--help") == 0))
6739 if ((g_zfs = libzfs_init()) == NULL) {
6740 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
6744 mnttab_file = g_zfs->libzfs_mnttab;
6746 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6748 libzfs_print_on_error(g_zfs, B_TRUE);
6751 * Run the appropriate command.
6753 libzfs_mnttab_cache(g_zfs, B_TRUE);
6754 if (find_command_idx(cmdname, &i) == 0) {
6755 current_command = &command_table[i];
6756 ret = command_table[i].func(argc - 1, argv + 1);
6757 } else if (strchr(cmdname, '=') != NULL) {
6758 verify(find_command_idx("set", &i) == 0);
6759 current_command = &command_table[i];
6760 ret = command_table[i].func(argc, argv);
6762 (void) fprintf(stderr, gettext("unrecognized "
6763 "command '%s'\n"), cmdname);
6768 if (ret == 0 && log_history)
6769 (void) zpool_log_history(g_zfs, history_str);
6774 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6775 * for the purposes of running ::findleaks.
6777 if (getenv("ZFS_ABORT") != NULL) {
6778 (void) printf("dumping core by request\n");