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 2012 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright 2012 Milan Jurik. All rights reserved.
27 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
29 * All rights reserved.
30 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
39 #include <libnvpair.h>
52 #include <sys/mntent.h>
53 #include <sys/mnttab.h>
54 #include <sys/mount.h>
56 #include <sys/fs/zfs.h>
57 #include <sys/types.h>
61 #include <libzfs_core.h>
63 #include <zfs_deleg.h>
67 #include <directory.h>
72 #include "zfs_comutil.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_jail(int argc, char **argv);
106 static int zfs_do_unjail(int argc, char **argv);
109 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
114 _umem_debug_init(void)
116 return ("default,verbose"); /* $UMEM_DEBUG setting */
120 _umem_logging_init(void)
122 return ("fail,contents"); /* $UMEM_LOGGING setting */
157 typedef struct zfs_command {
159 int (*func)(int argc, char **argv);
164 * Master command table. Each ZFS command has a name, associated function, and
165 * usage message. The usage messages need to be internationalized, so we have
166 * to have a function to return the usage message based on a command index.
168 * These commands are organized according to how they are displayed in the usage
169 * message. An empty command (one with a NULL name) indicates an empty line in
170 * the generic usage message.
172 static zfs_command_t command_table[] = {
173 { "create", zfs_do_create, HELP_CREATE },
174 { "destroy", zfs_do_destroy, HELP_DESTROY },
176 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
177 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
178 { "clone", zfs_do_clone, HELP_CLONE },
179 { "promote", zfs_do_promote, HELP_PROMOTE },
180 { "rename", zfs_do_rename, HELP_RENAME },
182 { "list", zfs_do_list, HELP_LIST },
184 { "set", zfs_do_set, HELP_SET },
185 { "get", zfs_do_get, HELP_GET },
186 { "inherit", zfs_do_inherit, HELP_INHERIT },
187 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
188 { "userspace", zfs_do_userspace, HELP_USERSPACE },
189 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
191 { "mount", zfs_do_mount, HELP_MOUNT },
192 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
193 { "share", zfs_do_share, HELP_SHARE },
194 { "unshare", zfs_do_unshare, HELP_UNSHARE },
196 { "send", zfs_do_send, HELP_SEND },
197 { "receive", zfs_do_receive, HELP_RECEIVE },
199 { "allow", zfs_do_allow, HELP_ALLOW },
201 { "unallow", zfs_do_unallow, HELP_UNALLOW },
203 { "hold", zfs_do_hold, HELP_HOLD },
204 { "holds", zfs_do_holds, HELP_HOLDS },
205 { "release", zfs_do_release, HELP_RELEASE },
206 { "diff", zfs_do_diff, HELP_DIFF },
208 { "jail", zfs_do_jail, HELP_JAIL },
209 { "unjail", zfs_do_unjail, HELP_UNJAIL },
212 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
214 zfs_command_t *current_command;
217 get_usage(zfs_help_t idx)
221 return (gettext("\tclone [-p] [-o property=value] ... "
222 "<snapshot> <filesystem|volume>\n"));
224 return (gettext("\tcreate [-p] [-o property=value] ... "
226 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
227 "-V <size> <volume>\n"));
229 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
230 "\tdestroy [-dnpRrv] "
231 "<snapshot>[%<snapname>][,...]\n"));
233 return (gettext("\tget [-rHp] [-d max] "
234 "[-o \"all\" | field[,...]] [-t type[,...]] "
235 "[-s source[,...]]\n"
236 "\t <\"all\" | property[,...]> "
237 "[filesystem|volume|snapshot] ...\n"));
239 return (gettext("\tinherit [-rS] <property> "
240 "<filesystem|volume|snapshot> ...\n"));
242 return (gettext("\tupgrade [-v]\n"
243 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
245 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
247 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
249 return (gettext("\tlist [-rH][-d max] "
250 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
251 "\t [-S property] ... "
252 "[filesystem|volume|snapshot] ...\n"));
254 return (gettext("\tmount\n"
255 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
257 return (gettext("\tpromote <clone-filesystem>\n"));
259 return (gettext("\treceive [-vnFu] <filesystem|volume|"
261 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
263 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
264 "<filesystem|volume|snapshot>\n"
265 "\trename [-f] -p <filesystem|volume> "
266 "<filesystem|volume>\n"
267 "\trename -r <snapshot> <snapshot>\n"
268 "\trename -u [-p] <filesystem> <filesystem>"));
270 return (gettext("\trollback [-rRf] <snapshot>\n"));
272 return (gettext("\tsend [-DnPpRv] "
273 "[-i snapshot | -I snapshot] <snapshot>\n"));
275 return (gettext("\tset <property=value> "
276 "<filesystem|volume|snapshot> ...\n"));
278 return (gettext("\tshare <-a | filesystem>\n"));
280 return (gettext("\tsnapshot [-r] [-o property=value] ... "
281 "<filesystem@snapname|volume@snapname> ...\n"));
283 return (gettext("\tunmount [-f] "
284 "<-a | filesystem|mountpoint>\n"));
286 return (gettext("\tunshare "
287 "<-a | filesystem|mountpoint>\n"));
289 return (gettext("\tallow <filesystem|volume>\n"
291 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
292 "\t <filesystem|volume>\n"
293 "\tallow [-ld] -e <perm|@setname>[,...] "
294 "<filesystem|volume>\n"
295 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
296 "\tallow -s @setname <perm|@setname>[,...] "
297 "<filesystem|volume>\n"));
299 return (gettext("\tunallow [-rldug] "
300 "<\"everyone\"|user|group>[,...]\n"
301 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
302 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
303 "<filesystem|volume>\n"
304 "\tunallow [-r] -c [<perm|@setname>[,...]] "
305 "<filesystem|volume>\n"
306 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
307 "<filesystem|volume>\n"));
309 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
310 "[-s field] ...\n\t[-S field] ... "
311 "[-t type[,...]] <filesystem|snapshot>\n"));
312 case HELP_GROUPSPACE:
313 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
314 "[-s field] ...\n\t[-S field] ... "
315 "[-t type[,...]] <filesystem|snapshot>\n"));
317 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
319 return (gettext("\tholds [-r] <snapshot> ...\n"));
321 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
323 return (gettext("\tdiff [-FHt] <snapshot> "
324 "[snapshot|filesystem]\n"));
334 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
339 * Utility function to guarantee malloc() success.
343 safe_malloc(size_t size)
347 if ((data = calloc(1, size)) == NULL)
354 safe_strdup(char *str)
356 char *dupstr = strdup(str);
365 * Callback routine that will print out information for each of
369 usage_prop_cb(int prop, void *cb)
373 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
375 if (zfs_prop_readonly(prop))
376 (void) fprintf(fp, " NO ");
378 (void) fprintf(fp, "YES ");
380 if (zfs_prop_inheritable(prop))
381 (void) fprintf(fp, " YES ");
383 (void) fprintf(fp, " NO ");
385 if (zfs_prop_values(prop) == NULL)
386 (void) fprintf(fp, "-\n");
388 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
394 * Display usage message. If we're inside a command, display only the usage for
395 * that command. Otherwise, iterate over the entire command table and display
396 * a complete usage message.
399 usage(boolean_t requested)
402 boolean_t show_properties = B_FALSE;
403 FILE *fp = requested ? stdout : stderr;
405 if (current_command == NULL) {
407 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
409 gettext("where 'command' is one of the following:\n\n"));
411 for (i = 0; i < NCOMMAND; i++) {
412 if (command_table[i].name == NULL)
413 (void) fprintf(fp, "\n");
415 (void) fprintf(fp, "%s",
416 get_usage(command_table[i].usage));
419 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
420 "pool/[dataset/]*dataset[@name]\n"));
422 (void) fprintf(fp, gettext("usage:\n"));
423 (void) fprintf(fp, "%s", get_usage(current_command->usage));
426 if (current_command != NULL &&
427 (strcmp(current_command->name, "set") == 0 ||
428 strcmp(current_command->name, "get") == 0 ||
429 strcmp(current_command->name, "inherit") == 0 ||
430 strcmp(current_command->name, "list") == 0))
431 show_properties = B_TRUE;
433 if (show_properties) {
435 gettext("\nThe following properties are supported:\n"));
437 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
438 "PROPERTY", "EDIT", "INHERIT", "VALUES");
440 /* Iterate over all properties */
441 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
444 (void) fprintf(fp, "\t%-15s ", "userused@...");
445 (void) fprintf(fp, " NO NO <size>\n");
446 (void) fprintf(fp, "\t%-15s ", "groupused@...");
447 (void) fprintf(fp, " NO NO <size>\n");
448 (void) fprintf(fp, "\t%-15s ", "userquota@...");
449 (void) fprintf(fp, "YES NO <size> | none\n");
450 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
451 (void) fprintf(fp, "YES NO <size> | none\n");
452 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
453 (void) fprintf(fp, " NO NO <size>\n");
455 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
456 "with standard units such as K, M, G, etc.\n"));
457 (void) fprintf(fp, gettext("\nUser-defined properties can "
458 "be specified by using a name containing a colon (:).\n"));
459 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
460 "properties must be appended with\n"
461 "a user or group specifier of one of these forms:\n"
462 " POSIX name (eg: \"matt\")\n"
463 " POSIX id (eg: \"126829\")\n"
464 " SMB name@domain (eg: \"matt@sun\")\n"
465 " SMB SID (eg: \"S-1-234-567-89\")\n"));
468 gettext("\nFor the property list, run: %s\n"),
471 gettext("\nFor the delegated permission list, run: %s\n"),
472 "zfs allow|unallow");
476 * See comments at end of main().
478 if (getenv("ZFS_ABORT") != NULL) {
479 (void) printf("dumping core by request\n");
483 exit(requested ? 0 : 2);
487 parseprop(nvlist_t *props)
489 char *propname = optarg;
490 char *propval, *strval;
492 if ((propval = strchr(propname, '=')) == NULL) {
493 (void) fprintf(stderr, gettext("missing "
494 "'=' for -o option\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);
584 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
586 * Given an existing dataset, create a writable copy whose initial contents
587 * are the same as the source. The newly created dataset maintains a
588 * dependency on the original; the original cannot be destroyed so long as
591 * The '-p' flag creates all the non-existing ancestors of the target first.
594 zfs_do_clone(int argc, char **argv)
596 zfs_handle_t *zhp = NULL;
597 boolean_t parents = B_FALSE;
602 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
606 while ((c = getopt(argc, argv, "o:p")) != -1) {
609 if (parseprop(props))
616 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
625 /* check number of arguments */
627 (void) fprintf(stderr, gettext("missing source dataset "
632 (void) fprintf(stderr, gettext("missing target dataset "
637 (void) fprintf(stderr, gettext("too many arguments\n"));
641 /* open the source dataset */
642 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
645 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
648 * Now create the ancestors of the target dataset. If the
649 * target already exists and '-p' option was used we should not
652 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
655 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
660 ret = zfs_clone(zhp, argv[1], props);
662 /* create the mountpoint if necessary */
666 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
668 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
669 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
670 ret = zfs_share(clone);
689 * zfs create [-p] [-o prop=value] ... fs
690 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
692 * Create a new dataset. This command can be used to create filesystems
693 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
694 * For volumes, the user must specify a size to be used.
696 * The '-s' flag applies only to volumes, and indicates that we should not try
697 * to set the reservation for this volume. By default we set a reservation
698 * equal to the size for any volume. For pools with SPA_VERSION >=
699 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
701 * The '-p' flag creates all the non-existing ancestors of the target first.
704 zfs_do_create(int argc, char **argv)
706 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
707 zfs_handle_t *zhp = NULL;
710 boolean_t noreserve = B_FALSE;
711 boolean_t bflag = B_FALSE;
712 boolean_t parents = B_FALSE;
716 int canmount = ZFS_CANMOUNT_OFF;
718 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
722 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
725 type = ZFS_TYPE_VOLUME;
726 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
727 (void) fprintf(stderr, gettext("bad volume "
728 "size '%s': %s\n"), optarg,
729 libzfs_error_description(g_zfs));
733 if (nvlist_add_uint64(props,
734 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
743 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
744 (void) fprintf(stderr, gettext("bad volume "
745 "block size '%s': %s\n"), optarg,
746 libzfs_error_description(g_zfs));
750 if (nvlist_add_uint64(props,
751 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
756 if (parseprop(props))
763 (void) fprintf(stderr, gettext("missing size "
767 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
773 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
774 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
775 "used when creating a volume\n"));
782 /* check number of arguments */
784 (void) fprintf(stderr, gettext("missing %s argument\n"),
785 zfs_type_to_name(type));
789 (void) fprintf(stderr, gettext("too many arguments\n"));
793 if (type == ZFS_TYPE_VOLUME && !noreserve) {
794 zpool_handle_t *zpool_handle;
795 uint64_t spa_version;
797 zfs_prop_t resv_prop;
800 if (p = strchr(argv[0], '/'))
802 zpool_handle = zpool_open(g_zfs, argv[0]);
805 if (zpool_handle == NULL)
807 spa_version = zpool_get_prop_int(zpool_handle,
808 ZPOOL_PROP_VERSION, NULL);
809 zpool_close(zpool_handle);
810 if (spa_version >= SPA_VERSION_REFRESERVATION)
811 resv_prop = ZFS_PROP_REFRESERVATION;
813 resv_prop = ZFS_PROP_RESERVATION;
814 volsize = zvol_volsize_to_reservation(volsize, props);
816 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
818 if (nvlist_add_uint64(props,
819 zfs_prop_to_name(resv_prop), volsize) != 0) {
826 if (parents && zfs_name_valid(argv[0], type)) {
828 * Now create the ancestors of target dataset. If the target
829 * already exists and '-p' option was used we should not
832 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
836 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
841 if (zfs_create(g_zfs, argv[0], type, props) != 0)
844 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
849 * if the user doesn't want the dataset automatically mounted,
850 * then skip the mount/share step
852 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
853 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
856 * Mount and/or share the new filesystem as appropriate. We provide a
857 * verbose error message to let the user know that their filesystem was
858 * in fact created, even if we failed to mount or share it.
860 if (canmount == ZFS_CANMOUNT_ON) {
861 if (zfs_mount(zhp, NULL, 0) != 0) {
862 (void) fprintf(stderr, gettext("filesystem "
863 "successfully created, but not mounted\n"));
865 } else if (zfs_share(zhp) != 0) {
866 (void) fprintf(stderr, gettext("filesystem "
867 "successfully created, but not shared\n"));
884 * zfs destroy [-rRf] <fs, vol>
885 * zfs destroy [-rRd] <snap>
887 * -r Recursively destroy all children
888 * -R Recursively destroy all dependents, including clones
889 * -f Force unmounting of any dependents
890 * -d If we can't destroy now, mark for deferred destruction
892 * Destroys the given dataset. By default, it will unmount any filesystems,
893 * and refuse to destroy a dataset that has any dependents. A dependent can
894 * either be a child, or a clone of a child.
896 typedef struct destroy_cbdata {
899 boolean_t cb_recurse;
901 boolean_t cb_doclones;
902 zfs_handle_t *cb_target;
903 boolean_t cb_defer_destroy;
904 boolean_t cb_verbose;
905 boolean_t cb_parsable;
908 nvlist_t *cb_batchedsnaps;
910 /* first snap in contiguous run */
912 /* previous snap in contiguous run */
919 * Check for any dependents based on the '-r' or '-R' flags.
922 destroy_check_dependent(zfs_handle_t *zhp, void *data)
924 destroy_cbdata_t *cbp = data;
925 const char *tname = zfs_get_name(cbp->cb_target);
926 const char *name = zfs_get_name(zhp);
928 if (strncmp(tname, name, strlen(tname)) == 0 &&
929 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
931 * This is a direct descendant, not a clone somewhere else in
938 (void) fprintf(stderr, gettext("cannot destroy '%s': "
939 "%s has children\n"),
940 zfs_get_name(cbp->cb_target),
941 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
942 (void) fprintf(stderr, gettext("use '-r' to destroy "
943 "the following datasets:\n"));
944 cbp->cb_first = B_FALSE;
945 cbp->cb_error = B_TRUE;
948 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
951 * This is a clone. We only want to report this if the '-r'
952 * wasn't specified, or the target is a snapshot.
954 if (!cbp->cb_recurse &&
955 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
959 (void) fprintf(stderr, gettext("cannot destroy '%s': "
960 "%s has dependent clones\n"),
961 zfs_get_name(cbp->cb_target),
962 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
963 (void) fprintf(stderr, gettext("use '-R' to destroy "
964 "the following datasets:\n"));
965 cbp->cb_first = B_FALSE;
966 cbp->cb_error = B_TRUE;
967 cbp->cb_dryrun = B_TRUE;
970 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
979 destroy_callback(zfs_handle_t *zhp, void *data)
981 destroy_cbdata_t *cb = data;
982 const char *name = zfs_get_name(zhp);
984 if (cb->cb_verbose) {
985 if (cb->cb_parsable) {
986 (void) printf("destroy\t%s\n", name);
987 } else if (cb->cb_dryrun) {
988 (void) printf(gettext("would destroy %s\n"),
991 (void) printf(gettext("will destroy %s\n"),
997 * Ignore pools (which we've already flagged as an error before getting
1000 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1001 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1005 if (cb->cb_dryrun) {
1011 * We batch up all contiguous snapshots (even of different
1012 * filesystems) and destroy them with one ioctl. We can't
1013 * simply do all snap deletions and then all fs deletions,
1014 * because we must delete a clone before its origin.
1016 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1017 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1019 int error = zfs_destroy_snaps_nvl(g_zfs,
1020 cb->cb_batchedsnaps, B_FALSE);
1021 fnvlist_free(cb->cb_batchedsnaps);
1022 cb->cb_batchedsnaps = fnvlist_alloc();
1025 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1026 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1037 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1039 destroy_cbdata_t *cb = arg;
1040 const char *name = zfs_get_name(zhp);
1043 if (nvlist_exists(cb->cb_nvl, name)) {
1044 if (cb->cb_firstsnap == NULL)
1045 cb->cb_firstsnap = strdup(name);
1046 if (cb->cb_prevsnap != NULL)
1047 free(cb->cb_prevsnap);
1048 /* this snap continues the current range */
1049 cb->cb_prevsnap = strdup(name);
1050 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1052 if (cb->cb_verbose) {
1053 if (cb->cb_parsable) {
1054 (void) printf("destroy\t%s\n", name);
1055 } else if (cb->cb_dryrun) {
1056 (void) printf(gettext("would destroy %s\n"),
1059 (void) printf(gettext("will destroy %s\n"),
1063 } else if (cb->cb_firstsnap != NULL) {
1064 /* end of this range */
1066 err = lzc_snaprange_space(cb->cb_firstsnap,
1067 cb->cb_prevsnap, &used);
1068 cb->cb_snapused += used;
1069 free(cb->cb_firstsnap);
1070 cb->cb_firstsnap = NULL;
1071 free(cb->cb_prevsnap);
1072 cb->cb_prevsnap = NULL;
1079 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1082 assert(cb->cb_firstsnap == NULL);
1083 assert(cb->cb_prevsnap == NULL);
1084 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1085 if (cb->cb_firstsnap != NULL) {
1088 err = lzc_snaprange_space(cb->cb_firstsnap,
1089 cb->cb_prevsnap, &used);
1091 cb->cb_snapused += used;
1092 free(cb->cb_firstsnap);
1093 cb->cb_firstsnap = NULL;
1094 free(cb->cb_prevsnap);
1095 cb->cb_prevsnap = NULL;
1101 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1103 destroy_cbdata_t *cb = arg;
1106 /* Check for clones. */
1107 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1108 cb->cb_target = zhp;
1109 cb->cb_first = B_TRUE;
1110 err = zfs_iter_dependents(zhp, B_TRUE,
1111 destroy_check_dependent, cb);
1115 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1123 gather_snapshots(zfs_handle_t *zhp, void *arg)
1125 destroy_cbdata_t *cb = arg;
1128 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1134 if (cb->cb_verbose) {
1135 err = destroy_print_snapshots(zhp, cb);
1141 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1149 destroy_clones(destroy_cbdata_t *cb)
1152 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1154 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1155 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1158 boolean_t defer = cb->cb_defer_destroy;
1162 * We can't defer destroy non-snapshots, so set it to
1163 * false while destroying the clones.
1165 cb->cb_defer_destroy = B_FALSE;
1166 err = zfs_iter_dependents(zhp, B_FALSE,
1167 destroy_callback, cb);
1168 cb->cb_defer_destroy = defer;
1178 zfs_do_destroy(int argc, char **argv)
1180 destroy_cbdata_t cb = { 0 };
1184 zfs_handle_t *zhp = NULL;
1186 zfs_type_t type = ZFS_TYPE_DATASET;
1189 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1192 cb.cb_verbose = B_TRUE;
1195 cb.cb_verbose = B_TRUE;
1196 cb.cb_parsable = B_TRUE;
1199 cb.cb_dryrun = B_TRUE;
1202 cb.cb_defer_destroy = B_TRUE;
1203 type = ZFS_TYPE_SNAPSHOT;
1206 cb.cb_force = B_TRUE;
1209 cb.cb_recurse = B_TRUE;
1212 cb.cb_recurse = B_TRUE;
1213 cb.cb_doclones = B_TRUE;
1217 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1226 /* check number of arguments */
1228 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1232 (void) fprintf(stderr, gettext("too many arguments\n"));
1236 at = strchr(argv[0], '@');
1239 /* Build the list of snaps to destroy in cb_nvl. */
1240 cb.cb_nvl = fnvlist_alloc();
1243 zhp = zfs_open(g_zfs, argv[0],
1244 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1248 cb.cb_snapspec = at + 1;
1249 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1255 if (nvlist_empty(cb.cb_nvl)) {
1256 (void) fprintf(stderr, gettext("could not find any "
1257 "snapshots to destroy; check snapshot names.\n"));
1262 if (cb.cb_verbose) {
1264 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1265 if (cb.cb_parsable) {
1266 (void) printf("reclaim\t%llu\n",
1268 } else if (cb.cb_dryrun) {
1269 (void) printf(gettext("would reclaim %s\n"),
1272 (void) printf(gettext("will reclaim %s\n"),
1277 if (!cb.cb_dryrun) {
1278 if (cb.cb_doclones) {
1279 cb.cb_batchedsnaps = fnvlist_alloc();
1280 err = destroy_clones(&cb);
1282 err = zfs_destroy_snaps_nvl(g_zfs,
1283 cb.cb_batchedsnaps, B_FALSE);
1291 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1292 cb.cb_defer_destroy);
1299 /* Open the given dataset */
1300 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1306 * Perform an explicit check for pools before going any further.
1308 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1309 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1310 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1311 "operation does not apply to pools\n"),
1313 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1314 "%s' to destroy all datasets in the pool\n"),
1316 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1317 "to destroy the pool itself\n"), zfs_get_name(zhp));
1323 * Check for any dependents and/or clones.
1325 cb.cb_first = B_TRUE;
1326 if (!cb.cb_doclones &&
1327 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1338 cb.cb_batchedsnaps = fnvlist_alloc();
1339 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1346 * Do the real thing. The callback will close the
1347 * handle regardless of whether it succeeds or not.
1349 err = destroy_callback(zhp, &cb);
1352 err = zfs_destroy_snaps_nvl(g_zfs,
1353 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1360 fnvlist_free(cb.cb_batchedsnaps);
1361 fnvlist_free(cb.cb_nvl);
1368 is_recvd_column(zprop_get_cbdata_t *cbp)
1371 zfs_get_column_t col;
1373 for (i = 0; i < ZFS_GET_NCOLS &&
1374 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1375 if (col == GET_COL_RECVD)
1381 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1382 * < all | property[,property]... > < fs | snap | vol > ...
1384 * -r recurse over any child datasets
1385 * -H scripted mode. Headers are stripped, and fields are separated
1386 * by tabs instead of spaces.
1387 * -o Set of fields to display. One of "name,property,value,
1388 * received,source". Default is "name,property,value,source".
1389 * "all" is an alias for all five.
1390 * -s Set of sources to allow. One of
1391 * "local,default,inherited,received,temporary,none". Default is
1393 * -p Display values in parsable (literal) format.
1395 * Prints properties for the given datasets. The user can control which
1396 * columns to display as well as which property types to allow.
1400 * Invoked to display the properties for a single dataset.
1403 get_callback(zfs_handle_t *zhp, void *data)
1405 char buf[ZFS_MAXPROPLEN];
1406 char rbuf[ZFS_MAXPROPLEN];
1407 zprop_source_t sourcetype;
1408 char source[ZFS_MAXNAMELEN];
1409 zprop_get_cbdata_t *cbp = data;
1410 nvlist_t *user_props = zfs_get_user_props(zhp);
1411 zprop_list_t *pl = cbp->cb_proplist;
1415 boolean_t received = is_recvd_column(cbp);
1417 for (; pl != NULL; pl = pl->pl_next) {
1418 char *recvdval = NULL;
1420 * Skip the special fake placeholder. This will also skip over
1421 * the name property when 'all' is specified.
1423 if (pl->pl_prop == ZFS_PROP_NAME &&
1424 pl == cbp->cb_proplist)
1427 if (pl->pl_prop != ZPROP_INVAL) {
1428 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1429 sizeof (buf), &sourcetype, source,
1431 cbp->cb_literal) != 0) {
1434 if (!zfs_prop_valid_for_type(pl->pl_prop,
1435 ZFS_TYPE_DATASET)) {
1436 (void) fprintf(stderr,
1437 gettext("No such property '%s'\n"),
1438 zfs_prop_to_name(pl->pl_prop));
1441 sourcetype = ZPROP_SRC_NONE;
1442 (void) strlcpy(buf, "-", sizeof (buf));
1445 if (received && (zfs_prop_get_recvd(zhp,
1446 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1447 cbp->cb_literal) == 0))
1450 zprop_print_one_property(zfs_get_name(zhp), cbp,
1451 zfs_prop_to_name(pl->pl_prop),
1452 buf, sourcetype, source, recvdval);
1453 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1454 sourcetype = ZPROP_SRC_LOCAL;
1456 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1457 buf, sizeof (buf), cbp->cb_literal) != 0) {
1458 sourcetype = ZPROP_SRC_NONE;
1459 (void) strlcpy(buf, "-", sizeof (buf));
1462 zprop_print_one_property(zfs_get_name(zhp), cbp,
1463 pl->pl_user_prop, buf, sourcetype, source, NULL);
1464 } else if (zfs_prop_written(pl->pl_user_prop)) {
1465 sourcetype = ZPROP_SRC_LOCAL;
1467 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1468 buf, sizeof (buf), cbp->cb_literal) != 0) {
1469 sourcetype = ZPROP_SRC_NONE;
1470 (void) strlcpy(buf, "-", sizeof (buf));
1473 zprop_print_one_property(zfs_get_name(zhp), cbp,
1474 pl->pl_user_prop, buf, sourcetype, source, NULL);
1476 if (nvlist_lookup_nvlist(user_props,
1477 pl->pl_user_prop, &propval) != 0) {
1480 sourcetype = ZPROP_SRC_NONE;
1483 verify(nvlist_lookup_string(propval,
1484 ZPROP_VALUE, &strval) == 0);
1485 verify(nvlist_lookup_string(propval,
1486 ZPROP_SOURCE, &sourceval) == 0);
1488 if (strcmp(sourceval,
1489 zfs_get_name(zhp)) == 0) {
1490 sourcetype = ZPROP_SRC_LOCAL;
1491 } else if (strcmp(sourceval,
1492 ZPROP_SOURCE_VAL_RECVD) == 0) {
1493 sourcetype = ZPROP_SRC_RECEIVED;
1495 sourcetype = ZPROP_SRC_INHERITED;
1496 (void) strlcpy(source,
1497 sourceval, sizeof (source));
1501 if (received && (zfs_prop_get_recvd(zhp,
1502 pl->pl_user_prop, rbuf, sizeof (rbuf),
1503 cbp->cb_literal) == 0))
1506 zprop_print_one_property(zfs_get_name(zhp), cbp,
1507 pl->pl_user_prop, strval, sourcetype,
1516 zfs_do_get(int argc, char **argv)
1518 zprop_get_cbdata_t cb = { 0 };
1519 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1520 int types = ZFS_TYPE_DATASET;
1521 char *value, *fields;
1524 zprop_list_t fake_name = { 0 };
1527 * Set up default columns and sources.
1529 cb.cb_sources = ZPROP_SRC_ALL;
1530 cb.cb_columns[0] = GET_COL_NAME;
1531 cb.cb_columns[1] = GET_COL_PROPERTY;
1532 cb.cb_columns[2] = GET_COL_VALUE;
1533 cb.cb_columns[3] = GET_COL_SOURCE;
1534 cb.cb_type = ZFS_TYPE_DATASET;
1537 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1540 cb.cb_literal = B_TRUE;
1543 limit = parse_depth(optarg, &flags);
1546 flags |= ZFS_ITER_RECURSE;
1549 cb.cb_scripted = B_TRUE;
1552 (void) fprintf(stderr, gettext("missing argument for "
1553 "'%c' option\n"), optopt);
1558 * Process the set of columns to display. We zero out
1559 * the structure to give us a blank slate.
1561 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1563 while (*optarg != '\0') {
1564 static char *col_subopts[] =
1565 { "name", "property", "value", "received",
1566 "source", "all", NULL };
1568 if (i == ZFS_GET_NCOLS) {
1569 (void) fprintf(stderr, gettext("too "
1570 "many fields given to -o "
1575 switch (getsubopt(&optarg, col_subopts,
1578 cb.cb_columns[i++] = GET_COL_NAME;
1581 cb.cb_columns[i++] = GET_COL_PROPERTY;
1584 cb.cb_columns[i++] = GET_COL_VALUE;
1587 cb.cb_columns[i++] = GET_COL_RECVD;
1588 flags |= ZFS_ITER_RECVD_PROPS;
1591 cb.cb_columns[i++] = GET_COL_SOURCE;
1595 (void) fprintf(stderr,
1596 gettext("\"all\" conflicts "
1597 "with specific fields "
1598 "given to -o option\n"));
1601 cb.cb_columns[0] = GET_COL_NAME;
1602 cb.cb_columns[1] = GET_COL_PROPERTY;
1603 cb.cb_columns[2] = GET_COL_VALUE;
1604 cb.cb_columns[3] = GET_COL_RECVD;
1605 cb.cb_columns[4] = GET_COL_SOURCE;
1606 flags |= ZFS_ITER_RECVD_PROPS;
1610 (void) fprintf(stderr,
1611 gettext("invalid column name "
1620 while (*optarg != '\0') {
1621 static char *source_subopts[] = {
1622 "local", "default", "inherited",
1623 "received", "temporary", "none",
1626 switch (getsubopt(&optarg, source_subopts,
1629 cb.cb_sources |= ZPROP_SRC_LOCAL;
1632 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1635 cb.cb_sources |= ZPROP_SRC_INHERITED;
1638 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1641 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1644 cb.cb_sources |= ZPROP_SRC_NONE;
1647 (void) fprintf(stderr,
1648 gettext("invalid source "
1657 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1658 while (*optarg != '\0') {
1659 static char *type_subopts[] = { "filesystem",
1660 "volume", "snapshot", "all", NULL };
1662 switch (getsubopt(&optarg, type_subopts,
1665 types |= ZFS_TYPE_FILESYSTEM;
1668 types |= ZFS_TYPE_VOLUME;
1671 types |= ZFS_TYPE_SNAPSHOT;
1674 types = ZFS_TYPE_DATASET;
1678 (void) fprintf(stderr,
1679 gettext("invalid type '%s'\n"),
1687 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1697 (void) fprintf(stderr, gettext("missing property "
1704 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1712 * As part of zfs_expand_proplist(), we keep track of the maximum column
1713 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1714 * need to know the maximum name length. However, the user likely did
1715 * not specify 'name' as one of the properties to fetch, so we need to
1716 * make sure we always include at least this property for
1717 * print_get_headers() to work properly.
1719 if (cb.cb_proplist != NULL) {
1720 fake_name.pl_prop = ZFS_PROP_NAME;
1721 fake_name.pl_width = strlen(gettext("NAME"));
1722 fake_name.pl_next = cb.cb_proplist;
1723 cb.cb_proplist = &fake_name;
1726 cb.cb_first = B_TRUE;
1728 /* run for each object */
1729 ret = zfs_for_each(argc, argv, flags, types, NULL,
1730 &cb.cb_proplist, limit, get_callback, &cb);
1732 if (cb.cb_proplist == &fake_name)
1733 zprop_free_list(fake_name.pl_next);
1735 zprop_free_list(cb.cb_proplist);
1741 * inherit [-rS] <property> <fs|vol> ...
1743 * -r Recurse over all children
1744 * -S Revert to received value, if any
1746 * For each dataset specified on the command line, inherit the given property
1747 * from its parent. Inheriting a property at the pool level will cause it to
1748 * use the default value. The '-r' flag will recurse over all children, and is
1749 * useful for setting a property on a hierarchy-wide basis, regardless of any
1750 * local modifications for each dataset.
1753 typedef struct inherit_cbdata {
1754 const char *cb_propname;
1755 boolean_t cb_received;
1759 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1761 inherit_cbdata_t *cb = data;
1762 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1765 * If we're doing it recursively, then ignore properties that
1766 * are not valid for this type of dataset.
1768 if (prop != ZPROP_INVAL &&
1769 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1772 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1776 inherit_cb(zfs_handle_t *zhp, void *data)
1778 inherit_cbdata_t *cb = data;
1780 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1784 zfs_do_inherit(int argc, char **argv)
1788 inherit_cbdata_t cb = { 0 };
1792 boolean_t received = B_FALSE;
1795 while ((c = getopt(argc, argv, "rS")) != -1) {
1798 flags |= ZFS_ITER_RECURSE;
1805 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1814 /* check number of arguments */
1816 (void) fprintf(stderr, gettext("missing property argument\n"));
1820 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1828 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1829 if (zfs_prop_readonly(prop)) {
1830 (void) fprintf(stderr, gettext(
1831 "%s property is read-only\n"),
1835 if (!zfs_prop_inheritable(prop) && !received) {
1836 (void) fprintf(stderr, gettext("'%s' property cannot "
1837 "be inherited\n"), propname);
1838 if (prop == ZFS_PROP_QUOTA ||
1839 prop == ZFS_PROP_RESERVATION ||
1840 prop == ZFS_PROP_REFQUOTA ||
1841 prop == ZFS_PROP_REFRESERVATION)
1842 (void) fprintf(stderr, gettext("use 'zfs set "
1843 "%s=none' to clear\n"), propname);
1846 if (received && (prop == ZFS_PROP_VOLSIZE ||
1847 prop == ZFS_PROP_VERSION)) {
1848 (void) fprintf(stderr, gettext("'%s' property cannot "
1849 "be reverted to a received value\n"), propname);
1852 } else if (!zfs_prop_user(propname)) {
1853 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1858 cb.cb_propname = propname;
1859 cb.cb_received = received;
1861 if (flags & ZFS_ITER_RECURSE) {
1862 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1863 NULL, NULL, 0, inherit_recurse_cb, &cb);
1865 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1866 NULL, NULL, 0, inherit_cb, &cb);
1872 typedef struct upgrade_cbdata {
1873 uint64_t cb_numupgraded;
1874 uint64_t cb_numsamegraded;
1875 uint64_t cb_numfailed;
1876 uint64_t cb_version;
1878 boolean_t cb_foundone;
1879 char cb_lastfs[ZFS_MAXNAMELEN];
1883 same_pool(zfs_handle_t *zhp, const char *name)
1885 int len1 = strcspn(name, "/@");
1886 const char *zhname = zfs_get_name(zhp);
1887 int len2 = strcspn(zhname, "/@");
1891 return (strncmp(name, zhname, len1) == 0);
1895 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1897 upgrade_cbdata_t *cb = data;
1898 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1900 /* list if it's old/new */
1901 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1902 (cb->cb_newer && version > ZPL_VERSION)) {
1905 str = gettext("The following filesystems are "
1906 "formatted using a newer software version and\n"
1907 "cannot be accessed on the current system.\n\n");
1909 str = gettext("The following filesystems are "
1910 "out of date, and can be upgraded. After being\n"
1911 "upgraded, these filesystems (and any 'zfs send' "
1912 "streams generated from\n"
1913 "subsequent snapshots) will no longer be "
1914 "accessible by older software versions.\n\n");
1917 if (!cb->cb_foundone) {
1919 (void) printf(gettext("VER FILESYSTEM\n"));
1920 (void) printf(gettext("--- ------------\n"));
1921 cb->cb_foundone = B_TRUE;
1924 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1931 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1933 upgrade_cbdata_t *cb = data;
1934 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1935 int needed_spa_version;
1938 if (zfs_spa_version(zhp, &spa_version) < 0)
1941 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1943 if (needed_spa_version < 0)
1946 if (spa_version < needed_spa_version) {
1948 (void) printf(gettext("%s: can not be "
1949 "upgraded; the pool version needs to first "
1950 "be upgraded\nto version %d\n\n"),
1951 zfs_get_name(zhp), needed_spa_version);
1957 if (version < cb->cb_version) {
1959 (void) snprintf(verstr, sizeof (verstr),
1960 "%llu", cb->cb_version);
1961 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1963 * If they did "zfs upgrade -a", then we could
1964 * be doing ioctls to different pools. We need
1965 * to log this history once to each pool, and bypass
1966 * the normal history logging that happens in main().
1968 (void) zpool_log_history(g_zfs, history_str);
1969 log_history = B_FALSE;
1971 if (zfs_prop_set(zhp, "version", verstr) == 0)
1972 cb->cb_numupgraded++;
1975 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1976 } else if (version > cb->cb_version) {
1977 /* can't downgrade */
1978 (void) printf(gettext("%s: can not be downgraded; "
1979 "it is already at version %u\n"),
1980 zfs_get_name(zhp), version);
1983 cb->cb_numsamegraded++;
1991 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
1994 zfs_do_upgrade(int argc, char **argv)
1996 boolean_t all = B_FALSE;
1997 boolean_t showversions = B_FALSE;
1999 upgrade_cbdata_t cb = { 0 };
2001 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2004 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2007 flags |= ZFS_ITER_RECURSE;
2010 showversions = B_TRUE;
2013 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2014 optarg, &cb.cb_version) != 0) {
2015 (void) fprintf(stderr,
2016 gettext("invalid version %s\n"), optarg);
2025 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2034 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2036 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2037 cb.cb_version || argc))
2039 if ((all || argc) && (showversions))
2045 /* Show info on available versions. */
2046 (void) printf(gettext("The following filesystem versions are "
2048 (void) printf(gettext("VER DESCRIPTION\n"));
2049 (void) printf("--- -----------------------------------------"
2050 "---------------\n");
2051 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2052 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2053 (void) printf(gettext(" 3 Case insensitive and filesystem "
2054 "user identifier (FUID)\n"));
2055 (void) printf(gettext(" 4 userquota, groupquota "
2057 (void) printf(gettext(" 5 System attributes\n"));
2058 (void) printf(gettext("\nFor more information on a particular "
2059 "version, including supported releases,\n"));
2060 (void) printf("see the ZFS Administration Guide.\n\n");
2062 } else if (argc || all) {
2063 /* Upgrade filesystems */
2064 if (cb.cb_version == 0)
2065 cb.cb_version = ZPL_VERSION;
2066 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2067 NULL, NULL, 0, upgrade_set_callback, &cb);
2068 (void) printf(gettext("%llu filesystems upgraded\n"),
2070 if (cb.cb_numsamegraded) {
2071 (void) printf(gettext("%llu filesystems already at "
2073 cb.cb_numsamegraded);
2075 if (cb.cb_numfailed != 0)
2078 /* List old-version filesytems */
2080 (void) printf(gettext("This system is currently running "
2081 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2083 flags |= ZFS_ITER_RECURSE;
2084 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2085 NULL, NULL, 0, upgrade_list_callback, &cb);
2087 found = cb.cb_foundone;
2088 cb.cb_foundone = B_FALSE;
2089 cb.cb_newer = B_TRUE;
2091 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2092 NULL, NULL, 0, upgrade_list_callback, &cb);
2094 if (!cb.cb_foundone && !found) {
2095 (void) printf(gettext("All filesystems are "
2096 "formatted with the current version.\n"));
2104 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2105 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2106 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2107 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2109 * -H Scripted mode; elide headers and separate columns by tabs.
2110 * -i Translate SID to POSIX ID.
2111 * -n Print numeric ID instead of user/group name.
2112 * -o Control which fields to display.
2113 * -p Use exact (parseable) numeric output.
2114 * -s Specify sort columns, descending order.
2115 * -S Specify sort columns, ascending order.
2116 * -t Control which object types to display.
2118 * Displays space consumed by, and quotas on, each user in the specified
2119 * filesystem or snapshot.
2122 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2123 enum us_field_types {
2129 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2130 static char *us_field_names[] = { "type", "name", "used", "quota" };
2131 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2133 #define USTYPE_PSX_GRP (1 << 0)
2134 #define USTYPE_PSX_USR (1 << 1)
2135 #define USTYPE_SMB_GRP (1 << 2)
2136 #define USTYPE_SMB_USR (1 << 3)
2137 #define USTYPE_ALL \
2138 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2140 static int us_type_bits[] = {
2147 static char *us_type_names[] = { "posixgroup", "posxiuser", "smbgroup",
2150 typedef struct us_node {
2152 uu_avl_node_t usn_avlnode;
2153 uu_list_node_t usn_listnode;
2156 typedef struct us_cbdata {
2158 uu_avl_pool_t *cb_avl_pool;
2160 boolean_t cb_numname;
2161 boolean_t cb_nicenum;
2162 boolean_t cb_sid2posix;
2163 zfs_userquota_prop_t cb_prop;
2164 zfs_sort_column_t *cb_sortcol;
2165 size_t cb_width[USFIELD_LAST];
2168 static boolean_t us_populated = B_FALSE;
2171 zfs_sort_column_t *si_sortcol;
2172 boolean_t si_numname;
2176 us_field_index(char *field)
2180 for (i = 0; i < USFIELD_LAST; i++) {
2181 if (strcmp(field, us_field_names[i]) == 0)
2189 us_compare(const void *larg, const void *rarg, void *unused)
2191 const us_node_t *l = larg;
2192 const us_node_t *r = rarg;
2193 us_sort_info_t *si = (us_sort_info_t *)unused;
2194 zfs_sort_column_t *sortcol = si->si_sortcol;
2195 boolean_t numname = si->si_numname;
2196 nvlist_t *lnvl = l->usn_nvl;
2197 nvlist_t *rnvl = r->usn_nvl;
2201 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2208 zfs_prop_t prop = sortcol->sc_prop;
2209 const char *propname = NULL;
2210 boolean_t reverse = sortcol->sc_reverse;
2215 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2216 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2218 rc = (rv32 < lv32) ? 1 : -1;
2223 (void) nvlist_lookup_uint64(lnvl, propname,
2225 (void) nvlist_lookup_uint64(rnvl, propname,
2228 rc = (rv64 < lv64) ? 1 : -1;
2230 (void) nvlist_lookup_string(lnvl, propname,
2232 (void) nvlist_lookup_string(rnvl, propname,
2234 rc = strcmp(lvstr, rvstr);
2238 case ZFS_PROP_QUOTA:
2241 if (prop == ZFS_PROP_USED)
2245 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2246 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2248 rc = (rv64 < lv64) ? 1 : -1;
2254 return (reverse ? 1 : -1);
2256 return (reverse ? -1 : 1);
2261 * If entries still seem to be the same, check if they are of the same
2262 * type (smbentity is added only if we are doing SID to POSIX ID
2263 * translation where we can have duplicate type/name combinations).
2265 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2266 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2268 return (lvb < rvb ? -1 : 1);
2273 static inline const char *
2274 us_type2str(unsigned field_type)
2276 switch (field_type) {
2277 case USTYPE_PSX_USR:
2278 return ("POSIX User");
2279 case USTYPE_PSX_GRP:
2280 return ("POSIX Group");
2281 case USTYPE_SMB_USR:
2282 return ("SMB User");
2283 case USTYPE_SMB_GRP:
2284 return ("SMB Group");
2286 return ("Undefined");
2291 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2293 us_cbdata_t *cb = (us_cbdata_t *)arg;
2294 zfs_userquota_prop_t prop = cb->cb_prop;
2299 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2300 uu_avl_t *avl = cb->cb_avl;
2304 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2306 const char *typestr;
2310 int typeidx, nameidx, sizeidx;
2311 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2312 boolean_t smbentity = B_FALSE;
2314 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2316 node = safe_malloc(sizeof (us_node_t));
2317 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2318 node->usn_nvl = props;
2320 if (domain != NULL && domain[0] != '\0') {
2322 char sid[ZFS_MAXNAMELEN + 32];
2327 directory_error_t e;
2332 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2334 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2335 type = USTYPE_SMB_GRP;
2337 err = sid_to_id(sid, B_FALSE, &id);
2340 type = USTYPE_SMB_USR;
2342 err = sid_to_id(sid, B_TRUE, &id);
2349 if (!cb->cb_sid2posix) {
2350 e = directory_name_from_sid(NULL, sid, &name,
2353 directory_error_free(e);
2361 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2363 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2364 type = USTYPE_PSX_GRP;
2365 if (!cb->cb_numname) {
2368 if ((g = getgrgid(rid)) != NULL)
2372 type = USTYPE_PSX_USR;
2373 if (!cb->cb_numname) {
2376 if ((p = getpwuid(rid)) != NULL)
2383 * Make sure that the type/name combination is unique when doing
2384 * SID to POSIX ID translation (hence changing the type from SMB to
2387 if (cb->cb_sid2posix &&
2388 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2391 /* Calculate/update width of TYPE field */
2392 typestr = us_type2str(type);
2393 typelen = strlen(gettext(typestr));
2394 typeidx = us_field_index("type");
2395 if (typelen > cb->cb_width[typeidx])
2396 cb->cb_width[typeidx] = typelen;
2397 if (nvlist_add_uint32(props, "type", type) != 0)
2400 /* Calculate/update width of NAME field */
2401 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2402 if (nvlist_add_uint64(props, "name", rid) != 0)
2404 namelen = snprintf(NULL, 0, "%u", rid);
2406 if (nvlist_add_string(props, "name", name) != 0)
2408 namelen = strlen(name);
2410 nameidx = us_field_index("name");
2411 if (namelen > cb->cb_width[nameidx])
2412 cb->cb_width[nameidx] = namelen;
2415 * Check if this type/name combination is in the list and update it;
2416 * otherwise add new node to the list.
2418 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2419 uu_avl_insert(avl, node, idx);
2424 props = node->usn_nvl;
2427 /* Calculate/update width of USED/QUOTA fields */
2429 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2431 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2432 sizelen = strlen(sizebuf);
2433 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2435 if (!nvlist_exists(props, "quota"))
2436 (void) nvlist_add_uint64(props, "quota", 0);
2439 if (!nvlist_exists(props, "used"))
2440 (void) nvlist_add_uint64(props, "used", 0);
2442 sizeidx = us_field_index(propname);
2443 if (sizelen > cb->cb_width[sizeidx])
2444 cb->cb_width[sizeidx] = sizelen;
2446 if (nvlist_add_uint64(props, propname, space) != 0)
2453 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2454 size_t *width, us_node_t *node)
2456 nvlist_t *nvl = node->usn_nvl;
2457 char valstr[ZFS_MAXNAMELEN];
2458 boolean_t first = B_TRUE;
2464 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2465 if (!(ustype & types))
2468 while ((field = fields[cfield]) != USFIELD_LAST) {
2469 nvpair_t *nvp = NULL;
2473 char *strval = NULL;
2475 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2476 if (strcmp(nvpair_name(nvp),
2477 us_field_names[field]) == 0)
2481 type = nvpair_type(nvp);
2483 case DATA_TYPE_UINT32:
2484 (void) nvpair_value_uint32(nvp, &val32);
2486 case DATA_TYPE_UINT64:
2487 (void) nvpair_value_uint64(nvp, &val64);
2489 case DATA_TYPE_STRING:
2490 (void) nvpair_value_string(nvp, &strval);
2493 (void) fprintf(stderr, "invalid data type\n");
2498 strval = (char *)us_type2str(val32);
2501 if (type == DATA_TYPE_UINT64) {
2502 (void) sprintf(valstr, "%llu", val64);
2508 if (type == DATA_TYPE_UINT64) {
2510 (void) sprintf(valstr, "%llu", val64);
2512 zfs_nicenum(val64, valstr,
2515 if (field == USFIELD_QUOTA &&
2516 strcmp(valstr, "0") == 0)
2526 (void) printf("\t");
2531 (void) printf("%s", strval);
2532 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2533 (void) printf("%-*s", width[field], strval);
2535 (void) printf("%*s", width[field], strval);
2541 (void) printf("\n");
2545 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2546 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2554 boolean_t first = B_TRUE;
2556 while ((field = fields[cfield]) != USFIELD_LAST) {
2557 col = gettext(us_field_hdr[field]);
2558 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2559 (void) printf(first ? "%-*s" : " %-*s",
2562 (void) printf(first ? "%*s" : " %*s",
2568 (void) printf("\n");
2571 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2572 print_us_node(scripted, parsable, fields, types, width, node);
2574 nvlist_free(node->usn_nvl);
2579 zfs_do_userspace(int argc, char **argv)
2582 zfs_userquota_prop_t p;
2584 uu_avl_pool_t *avl_pool;
2586 uu_avl_walk_t *walk;
2588 char deffields[] = "type,name,used,quota";
2589 char *ofield = NULL;
2590 char *tfield = NULL;
2594 boolean_t scripted = B_FALSE;
2595 boolean_t prtnum = B_FALSE;
2596 boolean_t parsable = B_FALSE;
2597 boolean_t sid2posix = B_FALSE;
2600 zfs_sort_column_t *sortcol = NULL;
2601 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2605 uu_list_pool_t *listpool;
2607 uu_avl_index_t idx = 0;
2608 uu_list_index_t idx2 = 0;
2613 if (strcmp(argv[0], "groupspace") == 0)
2614 /* Toggle default group types */
2615 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2617 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2633 if (zfs_add_sort_column(&sortcol, optarg,
2634 c == 's' ? B_FALSE : B_TRUE) != 0) {
2635 (void) fprintf(stderr,
2636 gettext("invalid field '%s'\n"), optarg);
2647 (void) fprintf(stderr, gettext("missing argument for "
2648 "'%c' option\n"), optopt);
2652 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2662 (void) fprintf(stderr, gettext("missing dataset name\n"));
2666 (void) fprintf(stderr, gettext("too many arguments\n"));
2670 /* Use default output fields if not specified using -o */
2674 if ((delim = strchr(ofield, ',')) != NULL)
2676 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2677 (void) fprintf(stderr, gettext("invalid type '%s' "
2678 "for -o option\n"), ofield);
2683 } while (delim != NULL);
2684 fields[cfield] = USFIELD_LAST;
2686 /* Override output types (-t option) */
2687 if (tfield != NULL) {
2691 boolean_t found = B_FALSE;
2693 if ((delim = strchr(tfield, ',')) != NULL)
2695 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2697 if (strcmp(tfield, us_type_names[i]) == 0) {
2699 types |= us_type_bits[i];
2704 (void) fprintf(stderr, gettext("invalid type "
2705 "'%s' for -t option\n"), tfield);
2710 } while (delim != NULL);
2713 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2716 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2717 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2719 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2722 /* Always add default sorting columns */
2723 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2724 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2726 cb.cb_sortcol = sortcol;
2727 cb.cb_numname = prtnum;
2728 cb.cb_nicenum = !parsable;
2729 cb.cb_avl_pool = avl_pool;
2730 cb.cb_avl = avl_tree;
2731 cb.cb_sid2posix = sid2posix;
2733 for (i = 0; i < USFIELD_LAST; i++)
2734 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2736 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2737 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2738 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2739 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2740 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2743 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2748 if ((node = uu_avl_first(avl_tree)) == NULL)
2751 us_populated = B_TRUE;
2753 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2754 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2755 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2756 uu_list_node_init(node, &node->usn_listnode, listpool);
2758 while (node != NULL) {
2760 node = uu_avl_next(avl_tree, node);
2761 uu_avl_remove(avl_tree, rmnode);
2762 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2763 uu_list_insert(list, rmnode, idx2);
2766 for (node = uu_list_first(list); node != NULL;
2767 node = uu_list_next(list, node)) {
2768 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2770 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2771 uu_avl_insert(avl_tree, node, idx);
2774 uu_list_destroy(list);
2775 uu_list_pool_destroy(listpool);
2777 /* Print and free node nvlist memory */
2778 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2781 zfs_free_sort_columns(sortcol);
2783 /* Clean up the AVL tree */
2784 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2787 while ((node = uu_avl_walk_next(walk)) != NULL) {
2788 uu_avl_remove(cb.cb_avl, node);
2792 uu_avl_walk_end(walk);
2793 uu_avl_destroy(avl_tree);
2794 uu_avl_pool_destroy(avl_pool);
2800 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2801 * [-s property [-s property]...] [-S property [-S property]...]
2804 * -r Recurse over all children
2805 * -d Limit recursion by depth.
2806 * -H Scripted mode; elide headers and separate columns by tabs
2807 * -o Control which fields to display.
2808 * -t Control which object types to display.
2809 * -s Specify sort columns, descending order.
2810 * -S Specify sort columns, ascending order.
2812 * When given no arguments, lists all filesystems in the system.
2813 * Otherwise, list the specified datasets, optionally recursing down them if
2814 * '-r' is specified.
2816 typedef struct list_cbdata {
2818 boolean_t cb_scripted;
2819 zprop_list_t *cb_proplist;
2823 * Given a list of columns to display, output appropriate headers for each one.
2826 print_header(zprop_list_t *pl)
2828 char headerbuf[ZFS_MAXPROPLEN];
2831 boolean_t first = B_TRUE;
2832 boolean_t right_justify;
2834 for (; pl != NULL; pl = pl->pl_next) {
2841 right_justify = B_FALSE;
2842 if (pl->pl_prop != ZPROP_INVAL) {
2843 header = zfs_prop_column_name(pl->pl_prop);
2844 right_justify = zfs_prop_align_right(pl->pl_prop);
2846 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2847 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2848 headerbuf[i] = '\0';
2852 if (pl->pl_next == NULL && !right_justify)
2853 (void) printf("%s", header);
2854 else if (right_justify)
2855 (void) printf("%*s", pl->pl_width, header);
2857 (void) printf("%-*s", pl->pl_width, header);
2860 (void) printf("\n");
2864 * Given a dataset and a list of fields, print out all the properties according
2865 * to the described layout.
2868 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
2870 boolean_t first = B_TRUE;
2871 char property[ZFS_MAXPROPLEN];
2872 nvlist_t *userprops = zfs_get_user_props(zhp);
2875 boolean_t right_justify;
2878 for (; pl != NULL; pl = pl->pl_next) {
2881 (void) printf("\t");
2888 if (pl->pl_prop == ZFS_PROP_NAME) {
2889 (void) strlcpy(property, zfs_get_name(zhp),
2892 right_justify = zfs_prop_align_right(pl->pl_prop);
2893 } else if (pl->pl_prop != ZPROP_INVAL) {
2894 if (zfs_prop_get(zhp, pl->pl_prop, property,
2895 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2900 right_justify = zfs_prop_align_right(pl->pl_prop);
2901 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2902 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2903 property, sizeof (property), B_FALSE) != 0)
2907 right_justify = B_TRUE;
2908 } else if (zfs_prop_written(pl->pl_user_prop)) {
2909 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2910 property, sizeof (property), B_FALSE) != 0)
2914 right_justify = B_TRUE;
2916 if (nvlist_lookup_nvlist(userprops,
2917 pl->pl_user_prop, &propval) != 0)
2920 verify(nvlist_lookup_string(propval,
2921 ZPROP_VALUE, &propstr) == 0);
2922 right_justify = B_FALSE;
2925 width = pl->pl_width;
2928 * If this is being called in scripted mode, or if this is the
2929 * last column and it is left-justified, don't include a width
2932 if (scripted || (pl->pl_next == NULL && !right_justify))
2933 (void) printf("%s", propstr);
2934 else if (right_justify)
2935 (void) printf("%*s", width, propstr);
2937 (void) printf("%-*s", width, propstr);
2940 (void) printf("\n");
2944 * Generic callback function to list a dataset or snapshot.
2947 list_callback(zfs_handle_t *zhp, void *data)
2949 list_cbdata_t *cbp = data;
2951 if (cbp->cb_first) {
2952 if (!cbp->cb_scripted)
2953 print_header(cbp->cb_proplist);
2954 cbp->cb_first = B_FALSE;
2957 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2963 zfs_do_list(int argc, char **argv)
2966 boolean_t scripted = B_FALSE;
2967 static char default_fields[] =
2968 "name,used,available,referenced,mountpoint";
2969 int types = ZFS_TYPE_DATASET;
2970 boolean_t types_specified = B_FALSE;
2971 char *fields = NULL;
2972 list_cbdata_t cb = { 0 };
2976 zfs_sort_column_t *sortcol = NULL;
2977 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2980 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
2986 limit = parse_depth(optarg, &flags);
2989 flags |= ZFS_ITER_RECURSE;
2995 if (zfs_add_sort_column(&sortcol, optarg,
2997 (void) fprintf(stderr,
2998 gettext("invalid property '%s'\n"), optarg);
3003 if (zfs_add_sort_column(&sortcol, optarg,
3005 (void) fprintf(stderr,
3006 gettext("invalid property '%s'\n"), optarg);
3012 types_specified = B_TRUE;
3013 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3014 while (*optarg != '\0') {
3015 static char *type_subopts[] = { "filesystem",
3016 "volume", "snapshot", "all", NULL };
3018 switch (getsubopt(&optarg, type_subopts,
3021 types |= ZFS_TYPE_FILESYSTEM;
3024 types |= ZFS_TYPE_VOLUME;
3027 types |= ZFS_TYPE_SNAPSHOT;
3030 types = ZFS_TYPE_DATASET;
3034 (void) fprintf(stderr,
3035 gettext("invalid type '%s'\n"),
3042 (void) fprintf(stderr, gettext("missing argument for "
3043 "'%c' option\n"), optopt);
3047 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3057 fields = default_fields;
3060 * If we are only going to list snapshot names and sort by name,
3061 * then we can use faster version.
3063 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3064 flags |= ZFS_ITER_SIMPLE;
3067 * If "-o space" and no types were specified, don't display snapshots.
3069 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3070 types &= ~ZFS_TYPE_SNAPSHOT;
3073 * If the user specifies '-o all', the zprop_get_list() doesn't
3074 * normally include the name of the dataset. For 'zfs list', we always
3075 * want this property to be first.
3077 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3081 cb.cb_scripted = scripted;
3082 cb.cb_first = B_TRUE;
3084 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3085 limit, list_callback, &cb);
3087 zprop_free_list(cb.cb_proplist);
3088 zfs_free_sort_columns(sortcol);
3090 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3091 (void) printf(gettext("no datasets available\n"));
3097 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3098 * zfs rename [-f] -p <fs | vol> <fs | vol>
3099 * zfs rename -r <snap> <snap>
3100 * zfs rename -u [-p] <fs> <fs>
3102 * Renames the given dataset to another of the same type.
3104 * The '-p' flag creates all the non-existing ancestors of the target first.
3108 zfs_do_rename(int argc, char **argv)
3111 renameflags_t flags = { 0 };
3115 boolean_t parents = B_FALSE;
3116 char *snapshot = NULL;
3119 while ((c = getopt(argc, argv, "fpru")) != -1) {
3125 flags.recurse = B_TRUE;
3128 flags.nounmount = B_TRUE;
3131 flags.forceunmount = B_TRUE;
3135 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3144 /* check number of arguments */
3146 (void) fprintf(stderr, gettext("missing source dataset "
3151 (void) fprintf(stderr, gettext("missing target dataset "
3156 (void) fprintf(stderr, gettext("too many arguments\n"));
3160 if (flags.recurse && parents) {
3161 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3166 if (flags.recurse && strchr(argv[0], '@') == 0) {
3167 (void) fprintf(stderr, gettext("source dataset for recursive "
3168 "rename must be a snapshot\n"));
3172 if (flags.nounmount && parents) {
3173 (void) fprintf(stderr, gettext("-u and -r options are mutually "
3178 if (flags.nounmount)
3179 types = ZFS_TYPE_FILESYSTEM;
3181 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3183 types = ZFS_TYPE_DATASET;
3185 if (flags.recurse) {
3187 * When we do recursive rename we are fine when the given
3188 * snapshot for the given dataset doesn't exist - it can
3189 * still exists below.
3192 snapshot = strchr(argv[0], '@');
3193 assert(snapshot != NULL);
3198 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3201 /* If we were asked and the name looks good, try to create ancestors. */
3202 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3203 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3208 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3217 * Promotes the given clone fs to be the parent
3221 zfs_do_promote(int argc, char **argv)
3227 if (argc > 1 && argv[1][0] == '-') {
3228 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3233 /* check number of arguments */
3235 (void) fprintf(stderr, gettext("missing clone filesystem"
3240 (void) fprintf(stderr, gettext("too many arguments\n"));
3244 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3248 ret = (zfs_promote(zhp) != 0);
3256 * zfs rollback [-rRf] <snapshot>
3258 * -r Delete any intervening snapshots before doing rollback
3259 * -R Delete any snapshots and their clones
3260 * -f ignored for backwards compatability
3262 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3263 * since then and making it the active dataset. If more recent snapshots exist,
3264 * the command will complain unless the '-r' flag is given.
3266 typedef struct rollback_cbdata {
3272 boolean_t cb_recurse;
3273 boolean_t cb_dependent;
3274 } rollback_cbdata_t;
3277 * Report any snapshots more recent than the one specified. Used when '-r' is
3278 * not specified. We reuse this same callback for the snapshot dependents - if
3279 * 'cb_dependent' is set, then this is a dependent and we should report it
3280 * without checking the transaction group.
3283 rollback_check(zfs_handle_t *zhp, void *data)
3285 rollback_cbdata_t *cbp = data;
3287 if (cbp->cb_doclones) {
3292 if (!cbp->cb_dependent) {
3293 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
3294 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3295 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3298 if (cbp->cb_first && !cbp->cb_recurse) {
3299 (void) fprintf(stderr, gettext("cannot "
3300 "rollback to '%s': more recent snapshots "
3303 (void) fprintf(stderr, gettext("use '-r' to "
3304 "force deletion of the following "
3310 if (cbp->cb_recurse) {
3311 cbp->cb_dependent = B_TRUE;
3312 if (zfs_iter_dependents(zhp, B_TRUE,
3313 rollback_check, cbp) != 0) {
3317 cbp->cb_dependent = B_FALSE;
3319 (void) fprintf(stderr, "%s\n",
3324 if (cbp->cb_first && cbp->cb_recurse) {
3325 (void) fprintf(stderr, gettext("cannot rollback to "
3326 "'%s': clones of previous snapshots exist\n"),
3328 (void) fprintf(stderr, gettext("use '-R' to "
3329 "force deletion of the following clones and "
3335 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3343 zfs_do_rollback(int argc, char **argv)
3347 boolean_t force = B_FALSE;
3348 rollback_cbdata_t cb = { 0 };
3349 zfs_handle_t *zhp, *snap;
3350 char parentname[ZFS_MAXNAMELEN];
3354 while ((c = getopt(argc, argv, "rRf")) != -1) {
3367 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3376 /* check number of arguments */
3378 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3382 (void) fprintf(stderr, gettext("too many arguments\n"));
3386 /* open the snapshot */
3387 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3390 /* open the parent dataset */
3391 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3392 verify((delim = strrchr(parentname, '@')) != NULL);
3394 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3400 * Check for more recent snapshots and/or clones based on the presence
3403 cb.cb_target = argv[0];
3404 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3405 cb.cb_first = B_TRUE;
3407 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
3410 if ((ret = cb.cb_error) != 0)
3414 * Rollback parent to the given snapshot.
3416 ret = zfs_rollback(zhp, snap, force);
3429 * zfs set property=value { fs | snap | vol } ...
3431 * Sets the given property for all datasets specified on the command line.
3433 typedef struct set_cbdata {
3439 set_callback(zfs_handle_t *zhp, void *data)
3441 set_cbdata_t *cbp = data;
3443 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3444 switch (libzfs_errno(g_zfs)) {
3445 case EZFS_MOUNTFAILED:
3446 (void) fprintf(stderr, gettext("property may be set "
3447 "but unable to remount filesystem\n"));
3449 case EZFS_SHARENFSFAILED:
3450 (void) fprintf(stderr, gettext("property may be set "
3451 "but unable to reshare filesystem\n"));
3460 zfs_do_set(int argc, char **argv)
3465 /* check for options */
3466 if (argc > 1 && argv[1][0] == '-') {
3467 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3472 /* check number of arguments */
3474 (void) fprintf(stderr, gettext("missing property=value "
3479 (void) fprintf(stderr, gettext("missing dataset name\n"));
3483 /* validate property=value argument */
3484 cb.cb_propname = argv[1];
3485 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3486 (cb.cb_value[1] == '\0')) {
3487 (void) fprintf(stderr, gettext("missing value in "
3488 "property=value argument\n"));
3492 *cb.cb_value = '\0';
3495 if (*cb.cb_propname == '\0') {
3496 (void) fprintf(stderr,
3497 gettext("missing property in property=value argument\n"));
3501 ret = zfs_for_each(argc - 2, argv + 2, 0,
3502 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3507 typedef struct snap_cbdata {
3509 boolean_t sd_recursive;
3510 const char *sd_snapname;
3514 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3516 snap_cbdata_t *sd = arg;
3521 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3524 fnvlist_add_boolean(sd->sd_nvl, name);
3527 if (sd->sd_recursive)
3528 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3534 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3536 * Creates a snapshot with the given name. While functionally equivalent to
3537 * 'zfs create', it is a separate command to differentiate intent.
3540 zfs_do_snapshot(int argc, char **argv)
3545 snap_cbdata_t sd = { 0 };
3546 boolean_t multiple_snaps = B_FALSE;
3548 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3550 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3554 while ((c = getopt(argc, argv, "ro:")) != -1) {
3557 if (parseprop(props))
3561 sd.sd_recursive = B_TRUE;
3562 multiple_snaps = B_TRUE;
3565 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3574 /* check number of arguments */
3576 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3581 multiple_snaps = B_TRUE;
3582 for (; argc > 0; argc--, argv++) {
3586 atp = strchr(argv[0], '@');
3590 sd.sd_snapname = atp + 1;
3591 zhp = zfs_open(g_zfs, argv[0],
3592 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3595 if (zfs_snapshot_cb(zhp, &sd) != 0)
3599 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3600 nvlist_free(sd.sd_nvl);
3602 if (ret != 0 && multiple_snaps)
3603 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3607 nvlist_free(sd.sd_nvl);
3614 * Send a backup stream to stdout.
3617 zfs_do_send(int argc, char **argv)
3619 char *fromname = NULL;
3620 char *toname = NULL;
3623 sendflags_t flags = { 0 };
3625 nvlist_t *dbgnv = NULL;
3626 boolean_t extraverbose = B_FALSE;
3629 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3640 flags.doall = B_TRUE;
3643 flags.replicate = B_TRUE;
3646 flags.props = B_TRUE;
3649 flags.parsable = B_TRUE;
3650 flags.verbose = B_TRUE;
3654 extraverbose = B_TRUE;
3655 flags.verbose = B_TRUE;
3656 flags.progress = B_TRUE;
3659 flags.dedup = B_TRUE;
3662 flags.dryrun = B_TRUE;
3665 (void) fprintf(stderr, gettext("missing argument for "
3666 "'%c' option\n"), optopt);
3670 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3679 /* check number of arguments */
3681 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3685 (void) fprintf(stderr, gettext("too many arguments\n"));
3689 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3690 (void) fprintf(stderr,
3691 gettext("Error: Stream can not be written to a terminal.\n"
3692 "You must redirect standard output.\n"));
3696 cp = strchr(argv[0], '@');
3698 (void) fprintf(stderr,
3699 gettext("argument must be a snapshot\n"));
3704 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3709 * If they specified the full path to the snapshot, chop off
3710 * everything except the short name of the snapshot, but special
3711 * case if they specify the origin.
3713 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3714 char origin[ZFS_MAXNAMELEN];
3717 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3718 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3720 if (strcmp(origin, fromname) == 0) {
3722 flags.fromorigin = B_TRUE;
3725 if (cp != fromname && strcmp(argv[0], fromname)) {
3726 (void) fprintf(stderr,
3727 gettext("incremental source must be "
3728 "in same filesystem\n"));
3732 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3733 (void) fprintf(stderr,
3734 gettext("invalid incremental source\n"));
3740 if (flags.replicate && fromname == NULL)
3741 flags.doall = B_TRUE;
3743 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3744 extraverbose ? &dbgnv : NULL);
3746 if (extraverbose && dbgnv != NULL) {
3748 * dump_nvlist prints to stdout, but that's been
3749 * redirected to a file. Make it print to stderr
3752 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3753 dump_nvlist(dbgnv, 0);
3762 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3764 * Restore a backup stream from stdin.
3767 zfs_do_receive(int argc, char **argv)
3770 recvflags_t flags = { 0 };
3773 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3776 flags.isprefix = B_TRUE;
3779 flags.isprefix = B_TRUE;
3780 flags.istail = B_TRUE;
3783 flags.dryrun = B_TRUE;
3786 flags.nomount = B_TRUE;
3789 flags.verbose = B_TRUE;
3792 flags.force = B_TRUE;
3795 (void) fprintf(stderr, gettext("missing argument for "
3796 "'%c' option\n"), optopt);
3800 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3809 /* check number of arguments */
3811 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3815 (void) fprintf(stderr, gettext("too many arguments\n"));
3819 if (isatty(STDIN_FILENO)) {
3820 (void) fprintf(stderr,
3821 gettext("Error: Backup stream can not be read "
3822 "from a terminal.\n"
3823 "You must redirect standard input.\n"));
3827 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3833 * allow/unallow stuff
3835 /* copied from zfs/sys/dsl_deleg.h */
3836 #define ZFS_DELEG_PERM_CREATE "create"
3837 #define ZFS_DELEG_PERM_DESTROY "destroy"
3838 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3839 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3840 #define ZFS_DELEG_PERM_CLONE "clone"
3841 #define ZFS_DELEG_PERM_PROMOTE "promote"
3842 #define ZFS_DELEG_PERM_RENAME "rename"
3843 #define ZFS_DELEG_PERM_MOUNT "mount"
3844 #define ZFS_DELEG_PERM_SHARE "share"
3845 #define ZFS_DELEG_PERM_SEND "send"
3846 #define ZFS_DELEG_PERM_RECEIVE "receive"
3847 #define ZFS_DELEG_PERM_ALLOW "allow"
3848 #define ZFS_DELEG_PERM_USERPROP "userprop"
3849 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3850 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3851 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3852 #define ZFS_DELEG_PERM_USERUSED "userused"
3853 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3854 #define ZFS_DELEG_PERM_HOLD "hold"
3855 #define ZFS_DELEG_PERM_RELEASE "release"
3856 #define ZFS_DELEG_PERM_DIFF "diff"
3858 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3860 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3861 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3862 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3863 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3864 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3865 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3866 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3867 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3868 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3869 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3870 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3871 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3872 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3873 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3874 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3875 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3877 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3878 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3879 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3880 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3881 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3882 { NULL, ZFS_DELEG_NOTE_NONE }
3885 /* permission structure */
3886 typedef struct deleg_perm {
3887 zfs_deleg_who_type_t dp_who_type;
3888 const char *dp_name;
3890 boolean_t dp_descend;
3894 typedef struct deleg_perm_node {
3895 deleg_perm_t dpn_perm;
3897 uu_avl_node_t dpn_avl_node;
3898 } deleg_perm_node_t;
3900 typedef struct fs_perm fs_perm_t;
3902 /* permissions set */
3903 typedef struct who_perm {
3904 zfs_deleg_who_type_t who_type;
3905 const char *who_name; /* id */
3906 char who_ug_name[256]; /* user/group name */
3907 fs_perm_t *who_fsperm; /* uplink */
3909 uu_avl_t *who_deleg_perm_avl; /* permissions */
3913 typedef struct who_perm_node {
3914 who_perm_t who_perm;
3915 uu_avl_node_t who_avl_node;
3918 typedef struct fs_perm_set fs_perm_set_t;
3919 /* fs permissions */
3921 const char *fsp_name;
3923 uu_avl_t *fsp_sc_avl; /* sets,create */
3924 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3926 fs_perm_set_t *fsp_set; /* uplink */
3930 typedef struct fs_perm_node {
3931 fs_perm_t fspn_fsperm;
3934 uu_list_node_t fspn_list_node;
3937 /* top level structure */
3938 struct fs_perm_set {
3939 uu_list_pool_t *fsps_list_pool;
3940 uu_list_t *fsps_list; /* list of fs_perms */
3942 uu_avl_pool_t *fsps_named_set_avl_pool;
3943 uu_avl_pool_t *fsps_who_perm_avl_pool;
3944 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
3947 static inline const char *
3948 deleg_perm_type(zfs_deleg_note_t note)
3954 case ZFS_DELEG_NOTE_GROUPQUOTA:
3955 case ZFS_DELEG_NOTE_GROUPUSED:
3956 case ZFS_DELEG_NOTE_USERPROP:
3957 case ZFS_DELEG_NOTE_USERQUOTA:
3958 case ZFS_DELEG_NOTE_USERUSED:
3960 return (gettext("other"));
3962 return (gettext("subcommand"));
3967 who_type2weight(zfs_deleg_who_type_t who_type)
3971 case ZFS_DELEG_NAMED_SET_SETS:
3972 case ZFS_DELEG_NAMED_SET:
3975 case ZFS_DELEG_CREATE_SETS:
3976 case ZFS_DELEG_CREATE:
3979 case ZFS_DELEG_USER_SETS:
3980 case ZFS_DELEG_USER:
3983 case ZFS_DELEG_GROUP_SETS:
3984 case ZFS_DELEG_GROUP:
3987 case ZFS_DELEG_EVERYONE_SETS:
3988 case ZFS_DELEG_EVERYONE:
4000 who_perm_compare(const void *larg, const void *rarg, void *unused)
4002 const who_perm_node_t *l = larg;
4003 const who_perm_node_t *r = rarg;
4004 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4005 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4006 int lweight = who_type2weight(ltype);
4007 int rweight = who_type2weight(rtype);
4008 int res = lweight - rweight;
4010 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4011 ZFS_MAX_DELEG_NAME-1);
4023 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4025 const deleg_perm_node_t *l = larg;
4026 const deleg_perm_node_t *r = rarg;
4027 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4028 ZFS_MAX_DELEG_NAME-1);
4040 fs_perm_set_init(fs_perm_set_t *fspset)
4042 bzero(fspset, sizeof (fs_perm_set_t));
4044 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4045 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4046 NULL, UU_DEFAULT)) == NULL)
4048 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4049 UU_DEFAULT)) == NULL)
4052 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4053 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4054 who_perm_node_t, who_avl_node), who_perm_compare,
4055 UU_DEFAULT)) == NULL)
4058 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4059 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4060 who_perm_node_t, who_avl_node), who_perm_compare,
4061 UU_DEFAULT)) == NULL)
4064 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4065 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4066 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4071 static inline void fs_perm_fini(fs_perm_t *);
4072 static inline void who_perm_fini(who_perm_t *);
4075 fs_perm_set_fini(fs_perm_set_t *fspset)
4077 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4079 while (node != NULL) {
4080 fs_perm_node_t *next_node =
4081 uu_list_next(fspset->fsps_list, node);
4082 fs_perm_t *fsperm = &node->fspn_fsperm;
4083 fs_perm_fini(fsperm);
4084 uu_list_remove(fspset->fsps_list, node);
4089 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4090 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4091 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4095 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4098 deleg_perm->dp_who_type = type;
4099 deleg_perm->dp_name = name;
4103 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4104 zfs_deleg_who_type_t type, const char *name)
4106 uu_avl_pool_t *pool;
4107 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4109 bzero(who_perm, sizeof (who_perm_t));
4111 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4112 UU_DEFAULT)) == NULL)
4115 who_perm->who_type = type;
4116 who_perm->who_name = name;
4117 who_perm->who_fsperm = fsperm;
4121 who_perm_fini(who_perm_t *who_perm)
4123 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4125 while (node != NULL) {
4126 deleg_perm_node_t *next_node =
4127 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4129 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4134 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4138 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4140 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4141 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4143 bzero(fsperm, sizeof (fs_perm_t));
4145 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4149 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4153 fsperm->fsp_set = fspset;
4154 fsperm->fsp_name = fsname;
4158 fs_perm_fini(fs_perm_t *fsperm)
4160 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4161 while (node != NULL) {
4162 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4164 who_perm_t *who_perm = &node->who_perm;
4165 who_perm_fini(who_perm);
4166 uu_avl_remove(fsperm->fsp_sc_avl, node);
4171 node = uu_avl_first(fsperm->fsp_uge_avl);
4172 while (node != NULL) {
4173 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4175 who_perm_t *who_perm = &node->who_perm;
4176 who_perm_fini(who_perm);
4177 uu_avl_remove(fsperm->fsp_uge_avl, node);
4182 uu_avl_destroy(fsperm->fsp_sc_avl);
4183 uu_avl_destroy(fsperm->fsp_uge_avl);
4187 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4188 zfs_deleg_who_type_t who_type, const char *name, char locality)
4190 uu_avl_index_t idx = 0;
4192 deleg_perm_node_t *found_node = NULL;
4193 deleg_perm_t *deleg_perm = &node->dpn_perm;
4195 deleg_perm_init(deleg_perm, who_type, name);
4197 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4199 uu_avl_insert(avl, node, idx);
4202 deleg_perm = &node->dpn_perm;
4207 case ZFS_DELEG_LOCAL:
4208 deleg_perm->dp_local = B_TRUE;
4210 case ZFS_DELEG_DESCENDENT:
4211 deleg_perm->dp_descend = B_TRUE;
4216 assert(B_FALSE); /* invalid locality */
4221 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4223 nvpair_t *nvp = NULL;
4224 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4225 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4226 zfs_deleg_who_type_t who_type = who_perm->who_type;
4228 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4229 const char *name = nvpair_name(nvp);
4230 data_type_t type = nvpair_type(nvp);
4231 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4232 deleg_perm_node_t *node =
4233 safe_malloc(sizeof (deleg_perm_node_t));
4235 assert(type == DATA_TYPE_BOOLEAN);
4237 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4238 set_deleg_perm_node(avl, node, who_type, name, locality);
4245 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4247 nvpair_t *nvp = NULL;
4248 fs_perm_set_t *fspset = fsperm->fsp_set;
4250 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4251 nvlist_t *nvl2 = NULL;
4252 const char *name = nvpair_name(nvp);
4253 uu_avl_t *avl = NULL;
4254 uu_avl_pool_t *avl_pool;
4255 zfs_deleg_who_type_t perm_type = name[0];
4256 char perm_locality = name[1];
4257 const char *perm_name = name + 3;
4258 boolean_t is_set = B_TRUE;
4259 who_perm_t *who_perm = NULL;
4261 assert('$' == name[2]);
4263 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4266 switch (perm_type) {
4267 case ZFS_DELEG_CREATE:
4268 case ZFS_DELEG_CREATE_SETS:
4269 case ZFS_DELEG_NAMED_SET:
4270 case ZFS_DELEG_NAMED_SET_SETS:
4271 avl_pool = fspset->fsps_named_set_avl_pool;
4272 avl = fsperm->fsp_sc_avl;
4274 case ZFS_DELEG_USER:
4275 case ZFS_DELEG_USER_SETS:
4276 case ZFS_DELEG_GROUP:
4277 case ZFS_DELEG_GROUP_SETS:
4278 case ZFS_DELEG_EVERYONE:
4279 case ZFS_DELEG_EVERYONE_SETS:
4280 avl_pool = fspset->fsps_who_perm_avl_pool;
4281 avl = fsperm->fsp_uge_avl;
4286 who_perm_node_t *found_node = NULL;
4287 who_perm_node_t *node = safe_malloc(
4288 sizeof (who_perm_node_t));
4289 who_perm = &node->who_perm;
4290 uu_avl_index_t idx = 0;
4292 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4293 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4295 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4297 if (avl == fsperm->fsp_uge_avl) {
4299 struct passwd *p = NULL;
4300 struct group *g = NULL;
4301 const char *nice_name = NULL;
4303 switch (perm_type) {
4304 case ZFS_DELEG_USER_SETS:
4305 case ZFS_DELEG_USER:
4306 rid = atoi(perm_name);
4309 nice_name = p->pw_name;
4311 case ZFS_DELEG_GROUP_SETS:
4312 case ZFS_DELEG_GROUP:
4313 rid = atoi(perm_name);
4316 nice_name = g->gr_name;
4320 if (nice_name != NULL)
4322 node->who_perm.who_ug_name,
4326 uu_avl_insert(avl, node, idx);
4329 who_perm = &node->who_perm;
4333 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4340 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4342 nvpair_t *nvp = NULL;
4343 uu_avl_index_t idx = 0;
4345 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4346 nvlist_t *nvl2 = NULL;
4347 const char *fsname = nvpair_name(nvp);
4348 data_type_t type = nvpair_type(nvp);
4349 fs_perm_t *fsperm = NULL;
4350 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4354 fsperm = &node->fspn_fsperm;
4356 assert(DATA_TYPE_NVLIST == type);
4358 uu_list_node_init(node, &node->fspn_list_node,
4359 fspset->fsps_list_pool);
4361 idx = uu_list_numnodes(fspset->fsps_list);
4362 fs_perm_init(fsperm, fspset, fsname);
4364 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4367 (void) parse_fs_perm(fsperm, nvl2);
4369 uu_list_insert(fspset->fsps_list, node, idx);
4375 static inline const char *
4376 deleg_perm_comment(zfs_deleg_note_t note)
4378 const char *str = "";
4383 case ZFS_DELEG_NOTE_ALLOW:
4384 str = gettext("Must also have the permission that is being"
4385 "\n\t\t\t\tallowed");
4387 case ZFS_DELEG_NOTE_CLONE:
4388 str = gettext("Must also have the 'create' ability and 'mount'"
4389 "\n\t\t\t\tability in the origin file system");
4391 case ZFS_DELEG_NOTE_CREATE:
4392 str = gettext("Must also have the 'mount' ability");
4394 case ZFS_DELEG_NOTE_DESTROY:
4395 str = gettext("Must also have the 'mount' ability");
4397 case ZFS_DELEG_NOTE_DIFF:
4398 str = gettext("Allows lookup of paths within a dataset;"
4399 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4400 "\n\t\t\t\tin order to use zfs diff");
4402 case ZFS_DELEG_NOTE_HOLD:
4403 str = gettext("Allows adding a user hold to a snapshot");
4405 case ZFS_DELEG_NOTE_MOUNT:
4406 str = gettext("Allows mount/umount of ZFS datasets");
4408 case ZFS_DELEG_NOTE_PROMOTE:
4409 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4410 " 'promote' ability in the origin file system");
4412 case ZFS_DELEG_NOTE_RECEIVE:
4413 str = gettext("Must also have the 'mount' and 'create'"
4416 case ZFS_DELEG_NOTE_RELEASE:
4417 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4418 "might destroy the snapshot");
4420 case ZFS_DELEG_NOTE_RENAME:
4421 str = gettext("Must also have the 'mount' and 'create'"
4422 "\n\t\t\t\tability in the new parent");
4424 case ZFS_DELEG_NOTE_ROLLBACK:
4427 case ZFS_DELEG_NOTE_SEND:
4430 case ZFS_DELEG_NOTE_SHARE:
4431 str = gettext("Allows sharing file systems over NFS or SMB"
4432 "\n\t\t\t\tprotocols");
4434 case ZFS_DELEG_NOTE_SNAPSHOT:
4438 * case ZFS_DELEG_NOTE_VSCAN:
4439 * str = gettext("");
4443 case ZFS_DELEG_NOTE_GROUPQUOTA:
4444 str = gettext("Allows accessing any groupquota@... property");
4446 case ZFS_DELEG_NOTE_GROUPUSED:
4447 str = gettext("Allows reading any groupused@... property");
4449 case ZFS_DELEG_NOTE_USERPROP:
4450 str = gettext("Allows changing any user property");
4452 case ZFS_DELEG_NOTE_USERQUOTA:
4453 str = gettext("Allows accessing any userquota@... property");
4455 case ZFS_DELEG_NOTE_USERUSED:
4456 str = gettext("Allows reading any userused@... property");
4474 boolean_t recursive; /* unallow only */
4475 boolean_t prt_usage;
4477 boolean_t prt_perms;
4480 const char *dataset;
4484 prop_cmp(const void *a, const void *b)
4486 const char *str1 = *(const char **)a;
4487 const char *str2 = *(const char **)b;
4488 return (strcmp(str1, str2));
4492 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4494 const char *opt_desc[] = {
4495 "-h", gettext("show this help message and exit"),
4496 "-l", gettext("set permission locally"),
4497 "-d", gettext("set permission for descents"),
4498 "-u", gettext("set permission for user"),
4499 "-g", gettext("set permission for group"),
4500 "-e", gettext("set permission for everyone"),
4501 "-c", gettext("set create time permission"),
4502 "-s", gettext("define permission set"),
4504 "-r", gettext("remove permissions recursively"),
4506 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4507 size_t allow_size = unallow_size - 2;
4508 const char *props[ZFS_NUM_PROPS];
4511 FILE *fp = requested ? stdout : stderr;
4512 zprop_desc_t *pdtbl = zfs_prop_get_table();
4513 const char *fmt = gettext("%-16s %-14s\t%s\n");
4515 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4517 (void) fprintf(fp, gettext("Options:\n"));
4518 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4519 const char *opt = opt_desc[i++];
4520 const char *optdsc = opt_desc[i];
4521 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4524 (void) fprintf(fp, gettext("\nThe following permissions are "
4526 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4528 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4529 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4530 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4531 const char *perm_type = deleg_perm_type(perm_note);
4532 const char *perm_comment = deleg_perm_comment(perm_note);
4533 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4536 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4537 zprop_desc_t *pd = &pdtbl[i];
4538 if (pd->pd_visible != B_TRUE)
4541 if (pd->pd_attr == PROP_READONLY)
4544 props[count++] = pd->pd_name;
4546 props[count] = NULL;
4548 qsort(props, count, sizeof (char *), prop_cmp);
4550 for (i = 0; i < count; i++)
4551 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4554 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4556 exit(requested ? 0 : 2);
4559 static inline const char *
4560 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4563 if (un && argc == expected_argc - 1)
4565 else if (argc == expected_argc)
4566 *permsp = argv[argc - 2];
4568 allow_usage(un, B_FALSE,
4569 gettext("wrong number of parameters\n"));
4571 return (argv[argc - 1]);
4575 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4577 int uge_sum = opts->user + opts->group + opts->everyone;
4578 int csuge_sum = opts->create + opts->set + uge_sum;
4579 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4580 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4583 allow_usage(un, B_FALSE,
4584 gettext("-u, -g, and -e are mutually exclusive\n"));
4586 if (opts->prt_usage)
4587 if (argc == 0 && all_sum == 0)
4588 allow_usage(un, B_TRUE, NULL);
4594 allow_usage(un, B_FALSE,
4595 gettext("invalid options combined with -s\n"));
4597 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4598 if (argv[0][0] != '@')
4599 allow_usage(un, B_FALSE,
4600 gettext("invalid set name: missing '@' prefix\n"));
4601 opts->who = argv[0];
4602 } else if (opts->create) {
4603 if (ldcsuge_sum > 1)
4604 allow_usage(un, B_FALSE,
4605 gettext("invalid options combined with -c\n"));
4606 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4607 } else if (opts->everyone) {
4609 allow_usage(un, B_FALSE,
4610 gettext("invalid options combined with -e\n"));
4611 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4612 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4614 opts->everyone = B_TRUE;
4617 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4618 } else if (argc == 1 && !un) {
4619 opts->prt_perms = B_TRUE;
4620 opts->dataset = argv[argc-1];
4622 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4623 opts->who = argv[0];
4626 if (!opts->local && !opts->descend) {
4627 opts->local = B_TRUE;
4628 opts->descend = B_TRUE;
4633 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4634 const char *who, char *perms, nvlist_t *top_nvl)
4637 char ld[2] = { '\0', '\0' };
4638 char who_buf[ZFS_MAXNAMELEN+32];
4641 nvlist_t *base_nvl = NULL;
4642 nvlist_t *set_nvl = NULL;
4645 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4647 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4651 case ZFS_DELEG_NAMED_SET_SETS:
4652 case ZFS_DELEG_NAMED_SET:
4653 set_type = ZFS_DELEG_NAMED_SET_SETS;
4654 base_type = ZFS_DELEG_NAMED_SET;
4655 ld[0] = ZFS_DELEG_NA;
4657 case ZFS_DELEG_CREATE_SETS:
4658 case ZFS_DELEG_CREATE:
4659 set_type = ZFS_DELEG_CREATE_SETS;
4660 base_type = ZFS_DELEG_CREATE;
4661 ld[0] = ZFS_DELEG_NA;
4663 case ZFS_DELEG_USER_SETS:
4664 case ZFS_DELEG_USER:
4665 set_type = ZFS_DELEG_USER_SETS;
4666 base_type = ZFS_DELEG_USER;
4668 ld[0] = ZFS_DELEG_LOCAL;
4670 ld[1] = ZFS_DELEG_DESCENDENT;
4672 case ZFS_DELEG_GROUP_SETS:
4673 case ZFS_DELEG_GROUP:
4674 set_type = ZFS_DELEG_GROUP_SETS;
4675 base_type = ZFS_DELEG_GROUP;
4677 ld[0] = ZFS_DELEG_LOCAL;
4679 ld[1] = ZFS_DELEG_DESCENDENT;
4681 case ZFS_DELEG_EVERYONE_SETS:
4682 case ZFS_DELEG_EVERYONE:
4683 set_type = ZFS_DELEG_EVERYONE_SETS;
4684 base_type = ZFS_DELEG_EVERYONE;
4686 ld[0] = ZFS_DELEG_LOCAL;
4688 ld[1] = ZFS_DELEG_DESCENDENT;
4691 if (perms != NULL) {
4693 char *end = curr + strlen(perms);
4695 while (curr < end) {
4696 char *delim = strchr(curr, ',');
4707 (void) nvlist_add_boolean(nvl, curr);
4713 for (i = 0; i < 2; i++) {
4714 char locality = ld[i];
4718 if (!nvlist_empty(base_nvl)) {
4720 (void) snprintf(who_buf,
4721 sizeof (who_buf), "%c%c$%s",
4722 base_type, locality, who);
4724 (void) snprintf(who_buf,
4725 sizeof (who_buf), "%c%c$",
4726 base_type, locality);
4728 (void) nvlist_add_nvlist(top_nvl, who_buf,
4733 if (!nvlist_empty(set_nvl)) {
4735 (void) snprintf(who_buf,
4736 sizeof (who_buf), "%c%c$%s",
4737 set_type, locality, who);
4739 (void) snprintf(who_buf,
4740 sizeof (who_buf), "%c%c$",
4741 set_type, locality);
4743 (void) nvlist_add_nvlist(top_nvl, who_buf,
4748 for (i = 0; i < 2; i++) {
4749 char locality = ld[i];
4754 (void) snprintf(who_buf, sizeof (who_buf),
4755 "%c%c$%s", base_type, locality, who);
4757 (void) snprintf(who_buf, sizeof (who_buf),
4758 "%c%c$", base_type, locality);
4759 (void) nvlist_add_boolean(top_nvl, who_buf);
4762 (void) snprintf(who_buf, sizeof (who_buf),
4763 "%c%c$%s", set_type, locality, who);
4765 (void) snprintf(who_buf, sizeof (who_buf),
4766 "%c%c$", set_type, locality);
4767 (void) nvlist_add_boolean(top_nvl, who_buf);
4773 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4775 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4779 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4780 opts->descend, opts->who, opts->perms, *nvlp);
4781 } else if (opts->create) {
4782 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4783 opts->descend, NULL, opts->perms, *nvlp);
4784 } else if (opts->everyone) {
4785 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4786 opts->descend, NULL, opts->perms, *nvlp);
4788 char *curr = opts->who;
4789 char *end = curr + strlen(curr);
4791 while (curr < end) {
4793 zfs_deleg_who_type_t who_type;
4795 char *delim = strchr(curr, ',');
4798 struct passwd *p = NULL;
4799 struct group *g = NULL;
4807 rid = (uid_t)strtol(curr, &endch, 0);
4809 who_type = ZFS_DELEG_USER;
4818 (void) snprintf(errbuf, 256, gettext(
4819 "invalid user %s"), curr);
4820 allow_usage(un, B_TRUE, errbuf);
4822 } else if (opts->group) {
4823 who_type = ZFS_DELEG_GROUP;
4832 (void) snprintf(errbuf, 256, gettext(
4833 "invalid group %s"), curr);
4834 allow_usage(un, B_TRUE, errbuf);
4837 if (*endch != '\0') {
4844 if (*endch != '\0') {
4851 who_type = ZFS_DELEG_USER;
4853 } else if (g != NULL) {
4854 who_type = ZFS_DELEG_GROUP;
4857 (void) snprintf(errbuf, 256, gettext(
4858 "invalid user/group %s"), curr);
4859 allow_usage(un, B_TRUE, errbuf);
4863 (void) sprintf(id, "%u", rid);
4866 store_allow_perm(who_type, opts->local,
4867 opts->descend, who, opts->perms, *nvlp);
4876 print_set_creat_perms(uu_avl_t *who_avl)
4878 const char *sc_title[] = {
4879 gettext("Permission sets:\n"),
4880 gettext("Create time permissions:\n"),
4883 const char **title_ptr = sc_title;
4884 who_perm_node_t *who_node = NULL;
4885 int prev_weight = -1;
4887 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4888 who_node = uu_avl_next(who_avl, who_node)) {
4889 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4890 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4891 const char *who_name = who_node->who_perm.who_name;
4892 int weight = who_type2weight(who_type);
4893 boolean_t first = B_TRUE;
4894 deleg_perm_node_t *deleg_node;
4896 if (prev_weight != weight) {
4897 (void) printf(*title_ptr++);
4898 prev_weight = weight;
4901 if (who_name == NULL || strnlen(who_name, 1) == 0)
4902 (void) printf("\t");
4904 (void) printf("\t%s ", who_name);
4906 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4907 deleg_node = uu_avl_next(avl, deleg_node)) {
4910 deleg_node->dpn_perm.dp_name);
4913 (void) printf(",%s",
4914 deleg_node->dpn_perm.dp_name);
4917 (void) printf("\n");
4922 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4925 who_perm_node_t *who_node = NULL;
4926 boolean_t prt_title = B_TRUE;
4927 uu_avl_walk_t *walk;
4929 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4932 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4933 const char *who_name = who_node->who_perm.who_name;
4934 const char *nice_who_name = who_node->who_perm.who_ug_name;
4935 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4936 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4938 deleg_perm_node_t *deleg_node;
4939 boolean_t prt_who = B_TRUE;
4941 for (deleg_node = uu_avl_first(avl);
4943 deleg_node = uu_avl_next(avl, deleg_node)) {
4944 if (local != deleg_node->dpn_perm.dp_local ||
4945 descend != deleg_node->dpn_perm.dp_descend)
4949 const char *who = NULL;
4951 prt_title = B_FALSE;
4952 (void) printf(title);
4956 case ZFS_DELEG_USER_SETS:
4957 case ZFS_DELEG_USER:
4958 who = gettext("user");
4960 who_name = nice_who_name;
4962 case ZFS_DELEG_GROUP_SETS:
4963 case ZFS_DELEG_GROUP:
4964 who = gettext("group");
4966 who_name = nice_who_name;
4968 case ZFS_DELEG_EVERYONE_SETS:
4969 case ZFS_DELEG_EVERYONE:
4970 who = gettext("everyone");
4975 if (who_name == NULL)
4976 (void) printf("\t%s", who);
4978 (void) printf("\t%s %s", who, who_name);
4981 (void) printf("%c%s", delim,
4982 deleg_node->dpn_perm.dp_name);
4987 (void) printf("\n");
4990 uu_avl_walk_end(walk);
4994 print_fs_perms(fs_perm_set_t *fspset)
4996 fs_perm_node_t *node = NULL;
4997 char buf[ZFS_MAXNAMELEN+32];
4998 const char *dsname = buf;
5000 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5001 node = uu_list_next(fspset->fsps_list, node)) {
5002 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5003 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5006 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5007 gettext("---- Permissions on %s "),
5008 node->fspn_fsperm.fsp_name);
5009 (void) printf(dsname);
5010 left = 70 - strlen(buf);
5013 (void) printf("\n");
5015 print_set_creat_perms(sc_avl);
5016 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5017 gettext("Local permissions:\n"));
5018 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5019 gettext("Descendent permissions:\n"));
5020 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5021 gettext("Local+Descendent permissions:\n"));
5025 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5027 struct deleg_perms {
5033 set_deleg_perms(zfs_handle_t *zhp, void *data)
5035 struct deleg_perms *perms = (struct deleg_perms *)data;
5036 zfs_type_t zfs_type = zfs_get_type(zhp);
5038 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5041 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5045 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5048 nvlist_t *perm_nvl = NULL;
5049 nvlist_t *update_perm_nvl = NULL;
5052 struct allow_opts opts = { 0 };
5054 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5057 while ((c = getopt(argc, argv, optstr)) != -1) {
5060 opts.local = B_TRUE;
5063 opts.descend = B_TRUE;
5069 opts.group = B_TRUE;
5072 opts.everyone = B_TRUE;
5078 opts.create = B_TRUE;
5081 opts.recursive = B_TRUE;
5084 (void) fprintf(stderr, gettext("missing argument for "
5085 "'%c' option\n"), optopt);
5089 opts.prt_usage = B_TRUE;
5092 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5101 /* check arguments */
5102 parse_allow_args(argc, argv, un, &opts);
5104 /* try to open the dataset */
5105 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5106 ZFS_TYPE_VOLUME)) == NULL) {
5107 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5112 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5115 fs_perm_set_init(&fs_perm_set);
5116 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5117 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5122 print_fs_perms(&fs_perm_set);
5124 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5125 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5128 if (un && opts.recursive) {
5129 struct deleg_perms data = { un, update_perm_nvl };
5130 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5139 nvlist_free(perm_nvl);
5140 if (update_perm_nvl != NULL)
5141 nvlist_free(update_perm_nvl);
5143 fs_perm_set_fini(&fs_perm_set);
5151 zfs_do_allow(int argc, char **argv)
5153 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5157 zfs_do_unallow(int argc, char **argv)
5159 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5163 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5168 boolean_t recursive = B_FALSE;
5169 const char *opts = holding ? "rt" : "r";
5173 while ((c = getopt(argc, argv, opts)) != -1) {
5179 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5188 /* check number of arguments */
5196 if (holding && tag[0] == '.') {
5197 /* tags starting with '.' are reserved for libzfs */
5198 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5202 for (i = 0; i < argc; ++i) {
5204 char parent[ZFS_MAXNAMELEN];
5206 char *path = argv[i];
5208 delim = strchr(path, '@');
5209 if (delim == NULL) {
5210 (void) fprintf(stderr,
5211 gettext("'%s' is not a snapshot\n"), path);
5215 (void) strncpy(parent, path, delim - path);
5216 parent[delim - path] = '\0';
5218 zhp = zfs_open(g_zfs, parent,
5219 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5225 if (zfs_hold(zhp, delim+1, tag, recursive,
5229 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5235 return (errors != 0);
5239 * zfs hold [-r] [-t] <tag> <snap> ...
5241 * -r Recursively hold
5243 * Apply a user-hold with the given tag to the list of snapshots.
5246 zfs_do_hold(int argc, char **argv)
5248 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5252 * zfs release [-r] <tag> <snap> ...
5254 * -r Recursively release
5256 * Release a user-hold with the given tag from the list of snapshots.
5259 zfs_do_release(int argc, char **argv)
5261 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5264 typedef struct holds_cbdata {
5265 boolean_t cb_recursive;
5266 const char *cb_snapname;
5268 size_t cb_max_namelen;
5269 size_t cb_max_taglen;
5272 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5273 #define DATETIME_BUF_LEN (32)
5278 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5281 nvpair_t *nvp = NULL;
5282 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5286 for (i = 0; i < 3; i++) {
5287 col = gettext(hdr_cols[i]);
5289 (void) printf("%-*s ", i ? tagwidth : nwidth,
5292 (void) printf("%s\n", col);
5296 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5297 char *zname = nvpair_name(nvp);
5299 nvpair_t *nvp2 = NULL;
5300 (void) nvpair_value_nvlist(nvp, &nvl2);
5301 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5302 char tsbuf[DATETIME_BUF_LEN];
5303 char *tagname = nvpair_name(nvp2);
5307 char sep = scripted ? '\t' : ' ';
5308 size_t sepnum = scripted ? 1 : 2;
5310 (void) nvpair_value_uint64(nvp2, &val);
5312 (void) localtime_r(&time, &t);
5313 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5314 gettext(STRFTIME_FMT_STR), &t);
5316 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5317 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5323 * Generic callback function to list a dataset or snapshot.
5326 holds_callback(zfs_handle_t *zhp, void *data)
5328 holds_cbdata_t *cbp = data;
5329 nvlist_t *top_nvl = *cbp->cb_nvlp;
5330 nvlist_t *nvl = NULL;
5331 nvpair_t *nvp = NULL;
5332 const char *zname = zfs_get_name(zhp);
5333 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5335 if (cbp->cb_recursive) {
5336 const char *snapname;
5337 char *delim = strchr(zname, '@');
5341 snapname = delim + 1;
5342 if (strcmp(cbp->cb_snapname, snapname))
5346 if (zfs_get_holds(zhp, &nvl) != 0)
5349 if (znamelen > cbp->cb_max_namelen)
5350 cbp->cb_max_namelen = znamelen;
5352 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5353 const char *tag = nvpair_name(nvp);
5354 size_t taglen = strnlen(tag, MAXNAMELEN);
5355 if (taglen > cbp->cb_max_taglen)
5356 cbp->cb_max_taglen = taglen;
5359 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5363 * zfs holds [-r] <snap> ...
5365 * -r Recursively hold
5368 zfs_do_holds(int argc, char **argv)
5373 boolean_t scripted = B_FALSE;
5374 boolean_t recursive = B_FALSE;
5375 const char *opts = "rH";
5378 int types = ZFS_TYPE_SNAPSHOT;
5379 holds_cbdata_t cb = { 0 };
5386 while ((c = getopt(argc, argv, opts)) != -1) {
5395 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5402 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5403 flags |= ZFS_ITER_RECURSE;
5409 /* check number of arguments */
5413 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5416 for (i = 0; i < argc; ++i) {
5417 char *snapshot = argv[i];
5419 const char *snapname;
5421 delim = strchr(snapshot, '@');
5422 if (delim == NULL) {
5423 (void) fprintf(stderr,
5424 gettext("'%s' is not a snapshot\n"), snapshot);
5428 snapname = delim + 1;
5430 snapshot[delim - snapshot] = '\0';
5432 cb.cb_recursive = recursive;
5433 cb.cb_snapname = snapname;
5437 * 1. collect holds data, set format options
5439 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5440 holds_callback, &cb);
5446 * 2. print holds data
5448 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5450 if (nvlist_empty(nvl))
5451 (void) printf(gettext("no datasets available\n"));
5455 return (0 != errors);
5458 #define CHECK_SPINNER 30
5459 #define SPINNER_TIME 3 /* seconds */
5460 #define MOUNT_TIME 5 /* seconds */
5463 get_one_dataset(zfs_handle_t *zhp, void *data)
5465 static char *spin[] = { "-", "\\", "|", "/" };
5466 static int spinval = 0;
5467 static int spincheck = 0;
5468 static time_t last_spin_time = (time_t)0;
5469 get_all_cb_t *cbp = data;
5470 zfs_type_t type = zfs_get_type(zhp);
5472 if (cbp->cb_verbose) {
5473 if (--spincheck < 0) {
5474 time_t now = time(NULL);
5475 if (last_spin_time + SPINNER_TIME < now) {
5476 update_progress(spin[spinval++ % 4]);
5477 last_spin_time = now;
5479 spincheck = CHECK_SPINNER;
5484 * Interate over any nested datasets.
5486 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5492 * Skip any datasets whose type does not match.
5494 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5498 libzfs_add_handle(cbp, zhp);
5499 assert(cbp->cb_used <= cbp->cb_alloc);
5505 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5507 get_all_cb_t cb = { 0 };
5508 cb.cb_verbose = verbose;
5509 cb.cb_getone = get_one_dataset;
5512 set_progress_header(gettext("Reading ZFS config"));
5513 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5515 *dslist = cb.cb_handles;
5516 *count = cb.cb_used;
5519 finish_progress(gettext("done."));
5523 * Generic callback for sharing or mounting filesystems. Because the code is so
5524 * similar, we have a common function with an extra parameter to determine which
5525 * mode we are using.
5527 #define OP_SHARE 0x1
5528 #define OP_MOUNT 0x2
5531 * Share or mount a dataset.
5534 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5535 boolean_t explicit, const char *options)
5537 char mountpoint[ZFS_MAXPROPLEN];
5538 char shareopts[ZFS_MAXPROPLEN];
5539 char smbshareopts[ZFS_MAXPROPLEN];
5540 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5542 uint64_t zoned, canmount;
5543 boolean_t shared_nfs, shared_smb;
5545 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5548 * Check to make sure we can mount/share this dataset. If we
5549 * are in the global zone and the filesystem is exported to a
5550 * local zone, or if we are in a local zone and the
5551 * filesystem is not exported, then it is an error.
5553 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5555 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5559 (void) fprintf(stderr, gettext("cannot %s '%s': "
5560 "dataset is exported to a local zone\n"), cmdname,
5564 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5568 (void) fprintf(stderr, gettext("cannot %s '%s': "
5569 "permission denied\n"), cmdname,
5575 * Ignore any filesystems which don't apply to us. This
5576 * includes those with a legacy mountpoint, or those with
5577 * legacy share options.
5579 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5580 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5581 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5582 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5583 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5584 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5586 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5587 strcmp(smbshareopts, "off") == 0) {
5591 (void) fprintf(stderr, gettext("cannot share '%s': "
5592 "legacy share\n"), zfs_get_name(zhp));
5593 (void) fprintf(stderr, gettext("use share(1M) to "
5594 "share this filesystem, or set "
5595 "sharenfs property on\n"));
5600 * We cannot share or mount legacy filesystems. If the
5601 * shareopts is non-legacy but the mountpoint is legacy, we
5602 * treat it as a legacy share.
5604 if (strcmp(mountpoint, "legacy") == 0) {
5608 (void) fprintf(stderr, gettext("cannot %s '%s': "
5609 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5610 (void) fprintf(stderr, gettext("use %s(1M) to "
5611 "%s this filesystem\n"), cmdname, cmdname);
5615 if (strcmp(mountpoint, "none") == 0) {
5619 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5620 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5625 * canmount explicit outcome
5626 * on no pass through
5627 * on yes pass through
5629 * off yes display error, return 1
5630 * noauto no return 0
5631 * noauto yes pass through
5633 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5634 if (canmount == ZFS_CANMOUNT_OFF) {
5638 (void) fprintf(stderr, gettext("cannot %s '%s': "
5639 "'canmount' property is set to 'off'\n"), cmdname,
5642 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5647 * At this point, we have verified that the mountpoint and/or
5648 * shareopts are appropriate for auto management. If the
5649 * filesystem is already mounted or shared, return (failing
5650 * for explicit requests); otherwise mount or share the
5656 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5657 shared_smb = zfs_is_shared_smb(zhp, NULL);
5659 if (shared_nfs && shared_smb ||
5660 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5661 strcmp(smbshareopts, "off") == 0) ||
5662 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5663 strcmp(shareopts, "off") == 0)) {
5667 (void) fprintf(stderr, gettext("cannot share "
5668 "'%s': filesystem already shared\n"),
5673 if (!zfs_is_mounted(zhp, NULL) &&
5674 zfs_mount(zhp, NULL, 0) != 0)
5677 if (protocol == NULL) {
5678 if (zfs_shareall(zhp) != 0)
5680 } else if (strcmp(protocol, "nfs") == 0) {
5681 if (zfs_share_nfs(zhp))
5683 } else if (strcmp(protocol, "smb") == 0) {
5684 if (zfs_share_smb(zhp))
5687 (void) fprintf(stderr, gettext("cannot share "
5688 "'%s': invalid share type '%s' "
5690 zfs_get_name(zhp), protocol);
5697 if (options == NULL)
5698 mnt.mnt_mntopts = "";
5700 mnt.mnt_mntopts = (char *)options;
5702 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5703 zfs_is_mounted(zhp, NULL)) {
5707 (void) fprintf(stderr, gettext("cannot mount "
5708 "'%s': filesystem already mounted\n"),
5713 if (zfs_mount(zhp, options, flags) != 0)
5722 * Reports progress in the form "(current/total)". Not thread-safe.
5725 report_mount_progress(int current, int total)
5727 static time_t last_progress_time = 0;
5728 time_t now = time(NULL);
5731 /* report 1..n instead of 0..n-1 */
5734 /* display header if we're here for the first time */
5736 set_progress_header(gettext("Mounting ZFS filesystems"));
5737 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5738 /* too soon to report again */
5742 last_progress_time = now;
5744 (void) sprintf(info, "(%d/%d)", current, total);
5746 if (current == total)
5747 finish_progress(info);
5749 update_progress(info);
5753 append_options(char *mntopts, char *newopts)
5755 int len = strlen(mntopts);
5757 /* original length plus new string to append plus 1 for the comma */
5758 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5759 (void) fprintf(stderr, gettext("the opts argument for "
5760 "'%c' option is too long (more than %d chars)\n"),
5761 "-o", MNT_LINE_MAX);
5766 mntopts[len++] = ',';
5768 (void) strcpy(&mntopts[len], newopts);
5772 share_mount(int op, int argc, char **argv)
5775 boolean_t verbose = B_FALSE;
5777 char *options = NULL;
5781 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5791 if (*optarg == '\0') {
5792 (void) fprintf(stderr, gettext("empty mount "
5793 "options (-o) specified\n"));
5797 if (options == NULL)
5798 options = safe_malloc(MNT_LINE_MAX + 1);
5800 /* option validation is done later */
5801 append_options(options, optarg);
5805 warnx("no overlay mounts support on FreeBSD, ignoring");
5808 (void) fprintf(stderr, gettext("missing argument for "
5809 "'%c' option\n"), optopt);
5813 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5822 /* check number of arguments */
5824 zfs_handle_t **dslist = NULL;
5825 size_t i, count = 0;
5826 char *protocol = NULL;
5828 if (op == OP_SHARE && argc > 0) {
5829 if (strcmp(argv[0], "nfs") != 0 &&
5830 strcmp(argv[0], "smb") != 0) {
5831 (void) fprintf(stderr, gettext("share type "
5832 "must be 'nfs' or 'smb'\n"));
5841 (void) fprintf(stderr, gettext("too many arguments\n"));
5845 start_progress_timer();
5846 get_all_datasets(&dslist, &count, verbose);
5851 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5853 for (i = 0; i < count; i++) {
5855 report_mount_progress(i, count);
5857 if (share_mount_one(dslist[i], op, flags, protocol,
5858 B_FALSE, options) != 0)
5860 zfs_close(dslist[i]);
5864 } else if (argc == 0) {
5865 struct mnttab entry;
5867 if ((op == OP_SHARE) || (options != NULL)) {
5868 (void) fprintf(stderr, gettext("missing filesystem "
5869 "argument (specify -a for all)\n"));
5874 * When mount is given no arguments, go through /etc/mnttab and
5875 * display any active ZFS mounts. We hide any snapshots, since
5876 * they are controlled automatically.
5878 rewind(mnttab_file);
5879 while (getmntent(mnttab_file, &entry) == 0) {
5880 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5881 strchr(entry.mnt_special, '@') != NULL)
5884 (void) printf("%-30s %s\n", entry.mnt_special,
5892 (void) fprintf(stderr,
5893 gettext("too many arguments\n"));
5897 if ((zhp = zfs_open(g_zfs, argv[0],
5898 ZFS_TYPE_FILESYSTEM)) == NULL) {
5901 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5911 * zfs mount -a [nfs]
5912 * zfs mount filesystem
5914 * Mount all filesystems, or mount the given filesystem.
5917 zfs_do_mount(int argc, char **argv)
5919 return (share_mount(OP_MOUNT, argc, argv));
5923 * zfs share -a [nfs | smb]
5924 * zfs share filesystem
5926 * Share all filesystems, or share the given filesystem.
5929 zfs_do_share(int argc, char **argv)
5931 return (share_mount(OP_SHARE, argc, argv));
5934 typedef struct unshare_unmount_node {
5935 zfs_handle_t *un_zhp;
5937 uu_avl_node_t un_avlnode;
5938 } unshare_unmount_node_t;
5942 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
5944 const unshare_unmount_node_t *l = larg;
5945 const unshare_unmount_node_t *r = rarg;
5947 return (strcmp(l->un_mountp, r->un_mountp));
5951 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5952 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
5953 * and unmount it appropriately.
5956 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
5960 struct stat64 statbuf;
5961 struct extmnttab entry;
5962 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5966 * Search for the path in /etc/mnttab. Rather than looking for the
5967 * specific path, which can be fooled by non-standard paths (i.e. ".."
5968 * or "//"), we stat() the path and search for the corresponding
5969 * (major,minor) device pair.
5971 if (stat64(path, &statbuf) != 0) {
5972 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5973 cmdname, path, strerror(errno));
5976 path_inode = statbuf.st_ino;
5979 * Search for the given (major,minor) pair in the mount table.
5982 rewind(mnttab_file);
5983 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
5984 if (entry.mnt_major == major(statbuf.st_dev) &&
5985 entry.mnt_minor == minor(statbuf.st_dev))
5992 if (statfs(path, &sfs) != 0) {
5993 (void) fprintf(stderr, "%s: %s\n", path,
5997 statfs2mnttab(&sfs, &entry);
6001 if (op == OP_SHARE) {
6002 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6003 "currently mounted\n"), cmdname, path);
6006 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6008 if ((ret = umount2(path, flags)) != 0)
6009 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6014 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6015 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6016 "filesystem\n"), cmdname, path);
6020 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6021 ZFS_TYPE_FILESYSTEM)) == NULL)
6025 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6026 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6027 cmdname, path, strerror(errno));
6029 } else if (statbuf.st_ino != path_inode) {
6030 (void) fprintf(stderr, gettext("cannot "
6031 "%s '%s': not a mountpoint\n"), cmdname, path);
6035 if (op == OP_SHARE) {
6036 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6037 char smbshare_prop[ZFS_MAXPROPLEN];
6039 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6040 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6041 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6042 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6044 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6045 strcmp(smbshare_prop, "off") == 0) {
6046 (void) fprintf(stderr, gettext("cannot unshare "
6047 "'%s': legacy share\n"), path);
6048 (void) fprintf(stderr, gettext("use "
6049 "unshare(1M) to unshare this filesystem\n"));
6050 } else if (!zfs_is_shared(zhp)) {
6051 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6052 "not currently shared\n"), path);
6054 ret = zfs_unshareall_bypath(zhp, path);
6057 char mtpt_prop[ZFS_MAXPROPLEN];
6059 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6060 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6063 ret = zfs_unmount(zhp, NULL, flags);
6064 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6065 (void) fprintf(stderr, gettext("cannot unmount "
6066 "'%s': legacy mountpoint\n"),
6068 (void) fprintf(stderr, gettext("use umount(1M) "
6069 "to unmount this filesystem\n"));
6071 ret = zfs_unmountall(zhp, flags);
6082 * Generic callback for unsharing or unmounting a filesystem.
6085 unshare_unmount(int op, int argc, char **argv)
6092 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6093 char sharesmb[ZFS_MAXPROPLEN];
6096 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6105 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6116 * We could make use of zfs_for_each() to walk all datasets in
6117 * the system, but this would be very inefficient, especially
6118 * since we would have to linearly search /etc/mnttab for each
6119 * one. Instead, do one pass through /etc/mnttab looking for
6120 * zfs entries and call zfs_unmount() for each one.
6122 * Things get a little tricky if the administrator has created
6123 * mountpoints beneath other ZFS filesystems. In this case, we
6124 * have to unmount the deepest filesystems first. To accomplish
6125 * this, we place all the mountpoints in an AVL tree sorted by
6126 * the special type (dataset name), and walk the result in
6127 * reverse to make sure to get any snapshots first.
6129 struct mnttab entry;
6130 uu_avl_pool_t *pool;
6132 unshare_unmount_node_t *node;
6134 uu_avl_walk_t *walk;
6137 (void) fprintf(stderr, gettext("too many arguments\n"));
6141 if (((pool = uu_avl_pool_create("unmount_pool",
6142 sizeof (unshare_unmount_node_t),
6143 offsetof(unshare_unmount_node_t, un_avlnode),
6144 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6145 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6148 rewind(mnttab_file);
6149 while (getmntent(mnttab_file, &entry) == 0) {
6151 /* ignore non-ZFS entries */
6152 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6155 /* ignore snapshots */
6156 if (strchr(entry.mnt_special, '@') != NULL)
6159 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6160 ZFS_TYPE_FILESYSTEM)) == NULL) {
6167 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6169 sizeof (nfs_mnt_prop),
6170 NULL, NULL, 0, B_FALSE) == 0);
6171 if (strcmp(nfs_mnt_prop, "off") != 0)
6173 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6175 sizeof (nfs_mnt_prop),
6176 NULL, NULL, 0, B_FALSE) == 0);
6177 if (strcmp(nfs_mnt_prop, "off") == 0)
6181 /* Ignore legacy mounts */
6182 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6184 sizeof (nfs_mnt_prop),
6185 NULL, NULL, 0, B_FALSE) == 0);
6186 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6188 /* Ignore canmount=noauto mounts */
6189 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6190 ZFS_CANMOUNT_NOAUTO)
6196 node = safe_malloc(sizeof (unshare_unmount_node_t));
6198 node->un_mountp = safe_strdup(entry.mnt_mountp);
6200 uu_avl_node_init(node, &node->un_avlnode, pool);
6202 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6203 uu_avl_insert(tree, node, idx);
6205 zfs_close(node->un_zhp);
6206 free(node->un_mountp);
6212 * Walk the AVL tree in reverse, unmounting each filesystem and
6213 * removing it from the AVL tree in the process.
6215 if ((walk = uu_avl_walk_start(tree,
6216 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6219 while ((node = uu_avl_walk_next(walk)) != NULL) {
6220 uu_avl_remove(tree, node);
6224 if (zfs_unshareall_bypath(node->un_zhp,
6225 node->un_mountp) != 0)
6230 if (zfs_unmount(node->un_zhp,
6231 node->un_mountp, flags) != 0)
6236 zfs_close(node->un_zhp);
6237 free(node->un_mountp);
6241 uu_avl_walk_end(walk);
6242 uu_avl_destroy(tree);
6243 uu_avl_pool_destroy(pool);
6248 (void) fprintf(stderr,
6249 gettext("missing filesystem argument\n"));
6251 (void) fprintf(stderr,
6252 gettext("too many arguments\n"));
6257 * We have an argument, but it may be a full path or a ZFS
6258 * filesystem. Pass full paths off to unmount_path() (shared by
6259 * manual_unmount), otherwise open the filesystem and pass to
6262 if (argv[0][0] == '/')
6263 return (unshare_unmount_path(op, argv[0],
6266 if ((zhp = zfs_open(g_zfs, argv[0],
6267 ZFS_TYPE_FILESYSTEM)) == NULL)
6270 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6271 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6272 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6273 NULL, 0, B_FALSE) == 0);
6277 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6279 sizeof (nfs_mnt_prop),
6280 NULL, NULL, 0, B_FALSE) == 0);
6281 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6282 sharesmb, sizeof (sharesmb), NULL, NULL,
6285 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6286 strcmp(sharesmb, "off") == 0) {
6287 (void) fprintf(stderr, gettext("cannot "
6288 "unshare '%s': legacy share\n"),
6290 (void) fprintf(stderr, gettext("use "
6291 "unshare(1M) to unshare this "
6294 } else if (!zfs_is_shared(zhp)) {
6295 (void) fprintf(stderr, gettext("cannot "
6296 "unshare '%s': not currently "
6297 "shared\n"), zfs_get_name(zhp));
6299 } else if (zfs_unshareall(zhp) != 0) {
6305 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6306 (void) fprintf(stderr, gettext("cannot "
6307 "unmount '%s': legacy "
6308 "mountpoint\n"), zfs_get_name(zhp));
6309 (void) fprintf(stderr, gettext("use "
6310 "umount(1M) to unmount this "
6313 } else if (!zfs_is_mounted(zhp, NULL)) {
6314 (void) fprintf(stderr, gettext("cannot "
6315 "unmount '%s': not currently "
6319 } else if (zfs_unmountall(zhp, flags) != 0) {
6333 * zfs unmount filesystem
6335 * Unmount all filesystems, or a specific ZFS filesystem.
6338 zfs_do_unmount(int argc, char **argv)
6340 return (unshare_unmount(OP_MOUNT, argc, argv));
6345 * zfs unshare filesystem
6347 * Unshare all filesystems, or a specific ZFS filesystem.
6350 zfs_do_unshare(int argc, char **argv)
6352 return (unshare_unmount(OP_SHARE, argc, argv));
6356 * Attach/detach the given dataset to/from the given jail
6360 do_jail(int argc, char **argv, int attach)
6365 /* check number of arguments */
6367 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6371 (void) fprintf(stderr, gettext("too many arguments\n"));
6375 jailid = jail_getid(argv[1]);
6377 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6381 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6385 ret = (zfs_jail(zhp, jailid, attach) != 0);
6392 * zfs jail jailid filesystem
6394 * Attach the given dataset to the given jail
6398 zfs_do_jail(int argc, char **argv)
6401 return (do_jail(argc, argv, 1));
6405 * zfs unjail jailid filesystem
6407 * Detach the given dataset from the given jail
6411 zfs_do_unjail(int argc, char **argv)
6414 return (do_jail(argc, argv, 0));
6418 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6419 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6422 manual_mount(int argc, char **argv)
6425 char mountpoint[ZFS_MAXPROPLEN];
6426 char mntopts[MNT_LINE_MAX] = { '\0' };
6430 char *dataset, *path;
6433 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6436 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6439 flags |= MS_OVERLAY;
6442 flags |= MS_NOMNTTAB;
6445 (void) fprintf(stderr, gettext("missing argument for "
6446 "'%c' option\n"), optopt);
6450 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6452 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6461 /* check that we only have two arguments */
6464 (void) fprintf(stderr, gettext("missing dataset "
6467 (void) fprintf(stderr,
6468 gettext("missing mountpoint argument\n"));
6470 (void) fprintf(stderr, gettext("too many arguments\n"));
6471 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6478 /* try to open the dataset */
6479 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6482 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6483 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6485 /* check for legacy mountpoint and complain appropriately */
6487 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6488 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6489 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6490 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6495 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6496 "mounted using 'mount -F zfs'\n"), dataset);
6497 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6498 "instead.\n"), path);
6499 (void) fprintf(stderr, gettext("If you must use 'mount -F zfs' "
6500 "or /etc/vfstab, use 'zfs set mountpoint=legacy'.\n"));
6501 (void) fprintf(stderr, gettext("See zfs(1M) for more "
6510 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6511 * unmounts of non-legacy filesystems, as this is the dominant administrative
6515 manual_unmount(int argc, char **argv)
6521 while ((c = getopt(argc, argv, "f")) != -1) {
6527 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6529 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6538 /* check arguments */
6541 (void) fprintf(stderr, gettext("missing path "
6544 (void) fprintf(stderr, gettext("too many arguments\n"));
6545 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6549 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6553 find_command_idx(char *command, int *idx)
6557 for (i = 0; i < NCOMMAND; i++) {
6558 if (command_table[i].name == NULL)
6561 if (strcmp(command, command_table[i].name) == 0) {
6570 zfs_do_diff(int argc, char **argv)
6574 char *tosnap = NULL;
6575 char *fromsnap = NULL;
6580 while ((c = getopt(argc, argv, "FHt")) != -1) {
6583 flags |= ZFS_DIFF_CLASSIFY;
6586 flags |= ZFS_DIFF_PARSEABLE;
6589 flags |= ZFS_DIFF_TIMESTAMP;
6592 (void) fprintf(stderr,
6593 gettext("invalid option '%c'\n"), optopt);
6602 (void) fprintf(stderr,
6603 gettext("must provide at least one snapshot name\n"));
6608 (void) fprintf(stderr, gettext("too many arguments\n"));
6613 tosnap = (argc == 2) ? argv[1] : NULL;
6616 if (*fromsnap != '@')
6617 copy = strdup(fromsnap);
6619 copy = strdup(tosnap);
6623 if (atp = strchr(copy, '@'))
6626 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6632 * Ignore SIGPIPE so that the library can give us
6633 * information on any failure
6635 (void) sigignore(SIGPIPE);
6637 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6645 main(int argc, char **argv)
6652 (void) setlocale(LC_ALL, "");
6653 (void) textdomain(TEXT_DOMAIN);
6657 if ((g_zfs = libzfs_init()) == NULL) {
6658 (void) fprintf(stderr, gettext("internal error: failed to "
6659 "initialize ZFS library\n"));
6663 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6665 libzfs_print_on_error(g_zfs, B_TRUE);
6667 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6668 (void) fprintf(stderr, gettext("internal error: unable to "
6669 "open %s\n"), MNTTAB);
6674 * This command also doubles as the /etc/fs mount and unmount program.
6675 * Determine if we should take this behavior based on argv[0].
6677 progname = basename(argv[0]);
6678 if (strcmp(progname, "mount") == 0) {
6679 ret = manual_mount(argc, argv);
6680 } else if (strcmp(progname, "umount") == 0) {
6681 ret = manual_unmount(argc, argv);
6684 * Make sure the user has specified some command.
6687 (void) fprintf(stderr, gettext("missing command\n"));
6694 * The 'umount' command is an alias for 'unmount'
6696 if (strcmp(cmdname, "umount") == 0)
6697 cmdname = "unmount";
6700 * The 'recv' command is an alias for 'receive'
6702 if (strcmp(cmdname, "recv") == 0)
6703 cmdname = "receive";
6708 if (strcmp(cmdname, "-?") == 0)
6712 * Run the appropriate command.
6714 libzfs_mnttab_cache(g_zfs, B_TRUE);
6715 if (find_command_idx(cmdname, &i) == 0) {
6716 current_command = &command_table[i];
6717 ret = command_table[i].func(argc - 1, argv + 1);
6718 } else if (strchr(cmdname, '=') != NULL) {
6719 verify(find_command_idx("set", &i) == 0);
6720 current_command = &command_table[i];
6721 ret = command_table[i].func(argc, argv);
6723 (void) fprintf(stderr, gettext("unrecognized "
6724 "command '%s'\n"), cmdname);
6727 libzfs_mnttab_cache(g_zfs, B_FALSE);
6730 (void) fclose(mnttab_file);
6732 if (ret == 0 && log_history)
6733 (void) zpool_log_history(g_zfs, history_str);
6738 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6739 * for the purposes of running ::findleaks.
6741 if (getenv("ZFS_ABORT") != NULL) {
6742 (void) printf("dumping core by request\n");