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.
31 * Copyright (c) 2013 Steven Hartland. All rights reserved.
40 #include <libnvpair.h>
53 #include <sys/mntent.h>
54 #include <sys/mnttab.h>
55 #include <sys/mount.h>
57 #include <sys/fs/zfs.h>
58 #include <sys/types.h>
64 #include <libzfs_core.h>
66 #include <zfs_deleg.h>
70 #include <directory.h>
75 #include "zfs_comutil.h"
77 libzfs_handle_t *g_zfs;
79 static FILE *mnttab_file;
80 static char history_str[HIS_MAX_RECORD_LEN];
81 static boolean_t log_history = B_TRUE;
83 static int zfs_do_clone(int argc, char **argv);
84 static int zfs_do_create(int argc, char **argv);
85 static int zfs_do_destroy(int argc, char **argv);
86 static int zfs_do_get(int argc, char **argv);
87 static int zfs_do_inherit(int argc, char **argv);
88 static int zfs_do_list(int argc, char **argv);
89 static int zfs_do_mount(int argc, char **argv);
90 static int zfs_do_rename(int argc, char **argv);
91 static int zfs_do_rollback(int argc, char **argv);
92 static int zfs_do_set(int argc, char **argv);
93 static int zfs_do_upgrade(int argc, char **argv);
94 static int zfs_do_snapshot(int argc, char **argv);
95 static int zfs_do_unmount(int argc, char **argv);
96 static int zfs_do_share(int argc, char **argv);
97 static int zfs_do_unshare(int argc, char **argv);
98 static int zfs_do_send(int argc, char **argv);
99 static int zfs_do_receive(int argc, char **argv);
100 static int zfs_do_promote(int argc, char **argv);
101 static int zfs_do_userspace(int argc, char **argv);
102 static int zfs_do_allow(int argc, char **argv);
103 static int zfs_do_unallow(int argc, char **argv);
104 static int zfs_do_hold(int argc, char **argv);
105 static int zfs_do_holds(int argc, char **argv);
106 static int zfs_do_release(int argc, char **argv);
107 static int zfs_do_diff(int argc, char **argv);
108 static int zfs_do_jail(int argc, char **argv);
109 static int zfs_do_unjail(int argc, char **argv);
112 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
117 _umem_debug_init(void)
119 return ("default,verbose"); /* $UMEM_DEBUG setting */
123 _umem_logging_init(void)
125 return ("fail,contents"); /* $UMEM_LOGGING setting */
160 typedef struct zfs_command {
162 int (*func)(int argc, char **argv);
167 * Master command table. Each ZFS command has a name, associated function, and
168 * usage message. The usage messages need to be internationalized, so we have
169 * to have a function to return the usage message based on a command index.
171 * These commands are organized according to how they are displayed in the usage
172 * message. An empty command (one with a NULL name) indicates an empty line in
173 * the generic usage message.
175 static zfs_command_t command_table[] = {
176 { "create", zfs_do_create, HELP_CREATE },
177 { "destroy", zfs_do_destroy, HELP_DESTROY },
179 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
180 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
181 { "clone", zfs_do_clone, HELP_CLONE },
182 { "promote", zfs_do_promote, HELP_PROMOTE },
183 { "rename", zfs_do_rename, HELP_RENAME },
185 { "list", zfs_do_list, HELP_LIST },
187 { "set", zfs_do_set, HELP_SET },
188 { "get", zfs_do_get, HELP_GET },
189 { "inherit", zfs_do_inherit, HELP_INHERIT },
190 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
191 { "userspace", zfs_do_userspace, HELP_USERSPACE },
192 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
194 { "mount", zfs_do_mount, HELP_MOUNT },
195 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
196 { "share", zfs_do_share, HELP_SHARE },
197 { "unshare", zfs_do_unshare, HELP_UNSHARE },
199 { "send", zfs_do_send, HELP_SEND },
200 { "receive", zfs_do_receive, HELP_RECEIVE },
202 { "allow", zfs_do_allow, HELP_ALLOW },
204 { "unallow", zfs_do_unallow, HELP_UNALLOW },
206 { "hold", zfs_do_hold, HELP_HOLD },
207 { "holds", zfs_do_holds, HELP_HOLDS },
208 { "release", zfs_do_release, HELP_RELEASE },
209 { "diff", zfs_do_diff, HELP_DIFF },
211 { "jail", zfs_do_jail, HELP_JAIL },
212 { "unjail", zfs_do_unjail, HELP_UNJAIL },
215 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
217 zfs_command_t *current_command;
220 get_usage(zfs_help_t idx)
224 return (gettext("\tclone [-p] [-o property=value] ... "
225 "<snapshot> <filesystem|volume>\n"));
227 return (gettext("\tcreate [-pu] [-o property=value] ... "
229 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
230 "-V <size> <volume>\n"));
232 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
233 "\tdestroy [-dnpRrv] "
234 "<snapshot>[%<snapname>][,...]\n"));
236 return (gettext("\tget [-rHp] [-d max] "
237 "[-o \"all\" | field[,...]] [-t type[,...]] "
238 "[-s source[,...]]\n"
239 "\t <\"all\" | property[,...]> "
240 "[filesystem|volume|snapshot] ...\n"));
242 return (gettext("\tinherit [-rS] <property> "
243 "<filesystem|volume|snapshot> ...\n"));
245 return (gettext("\tupgrade [-v]\n"
246 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
248 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
250 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
252 return (gettext("\tlist [-rH][-d max] "
253 "[-o property[,...]] [-t type[,...]] [-s property] ...\n"
254 "\t [-S property] ... "
255 "[filesystem|volume|snapshot] ...\n"));
257 return (gettext("\tmount\n"
258 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
260 return (gettext("\tpromote <clone-filesystem>\n"));
262 return (gettext("\treceive [-vnFu] <filesystem|volume|"
264 "\treceive [-vnFu] [-d | -e] <filesystem>\n"));
266 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
267 "<filesystem|volume|snapshot>\n"
268 "\trename [-f] -p <filesystem|volume> "
269 "<filesystem|volume>\n"
270 "\trename -r <snapshot> <snapshot>\n"
271 "\trename -u [-p] <filesystem> <filesystem>"));
273 return (gettext("\trollback [-rRf] <snapshot>\n"));
275 return (gettext("\tsend [-DnPpRv] "
276 "[-i snapshot | -I snapshot] <snapshot>\n"));
278 return (gettext("\tset <property=value> "
279 "<filesystem|volume|snapshot> ...\n"));
281 return (gettext("\tshare <-a | filesystem>\n"));
283 return (gettext("\tsnapshot [-r] [-o property=value] ... "
284 "<filesystem@snapname|volume@snapname> ...\n"));
286 return (gettext("\tunmount [-f] "
287 "<-a | filesystem|mountpoint>\n"));
289 return (gettext("\tunshare "
290 "<-a | filesystem|mountpoint>\n"));
292 return (gettext("\tallow <filesystem|volume>\n"
294 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
295 "\t <filesystem|volume>\n"
296 "\tallow [-ld] -e <perm|@setname>[,...] "
297 "<filesystem|volume>\n"
298 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
299 "\tallow -s @setname <perm|@setname>[,...] "
300 "<filesystem|volume>\n"));
302 return (gettext("\tunallow [-rldug] "
303 "<\"everyone\"|user|group>[,...]\n"
304 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
305 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
306 "<filesystem|volume>\n"
307 "\tunallow [-r] -c [<perm|@setname>[,...]] "
308 "<filesystem|volume>\n"
309 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
310 "<filesystem|volume>\n"));
312 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
313 "[-s field] ...\n\t[-S field] ... "
314 "[-t type[,...]] <filesystem|snapshot>\n"));
315 case HELP_GROUPSPACE:
316 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
317 "[-s field] ...\n\t[-S field] ... "
318 "[-t type[,...]] <filesystem|snapshot>\n"));
320 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
322 return (gettext("\tholds [-r] <snapshot> ...\n"));
324 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
326 return (gettext("\tdiff [-FHt] <snapshot> "
327 "[snapshot|filesystem]\n"));
337 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
342 * Utility function to guarantee malloc() success.
346 safe_malloc(size_t size)
350 if ((data = calloc(1, size)) == NULL)
357 safe_strdup(char *str)
359 char *dupstr = strdup(str);
368 * Callback routine that will print out information for each of
372 usage_prop_cb(int prop, void *cb)
376 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
378 if (zfs_prop_readonly(prop))
379 (void) fprintf(fp, " NO ");
381 (void) fprintf(fp, "YES ");
383 if (zfs_prop_inheritable(prop))
384 (void) fprintf(fp, " YES ");
386 (void) fprintf(fp, " NO ");
388 if (zfs_prop_values(prop) == NULL)
389 (void) fprintf(fp, "-\n");
391 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
397 * Display usage message. If we're inside a command, display only the usage for
398 * that command. Otherwise, iterate over the entire command table and display
399 * a complete usage message.
402 usage(boolean_t requested)
405 boolean_t show_properties = B_FALSE;
406 FILE *fp = requested ? stdout : stderr;
408 if (current_command == NULL) {
410 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
412 gettext("where 'command' is one of the following:\n\n"));
414 for (i = 0; i < NCOMMAND; i++) {
415 if (command_table[i].name == NULL)
416 (void) fprintf(fp, "\n");
418 (void) fprintf(fp, "%s",
419 get_usage(command_table[i].usage));
422 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
423 "pool/[dataset/]*dataset[@name]\n"));
425 (void) fprintf(fp, gettext("usage:\n"));
426 (void) fprintf(fp, "%s", get_usage(current_command->usage));
429 if (current_command != NULL &&
430 (strcmp(current_command->name, "set") == 0 ||
431 strcmp(current_command->name, "get") == 0 ||
432 strcmp(current_command->name, "inherit") == 0 ||
433 strcmp(current_command->name, "list") == 0))
434 show_properties = B_TRUE;
436 if (show_properties) {
438 gettext("\nThe following properties are supported:\n"));
440 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
441 "PROPERTY", "EDIT", "INHERIT", "VALUES");
443 /* Iterate over all properties */
444 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
447 (void) fprintf(fp, "\t%-15s ", "userused@...");
448 (void) fprintf(fp, " NO NO <size>\n");
449 (void) fprintf(fp, "\t%-15s ", "groupused@...");
450 (void) fprintf(fp, " NO NO <size>\n");
451 (void) fprintf(fp, "\t%-15s ", "userquota@...");
452 (void) fprintf(fp, "YES NO <size> | none\n");
453 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
454 (void) fprintf(fp, "YES NO <size> | none\n");
455 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
456 (void) fprintf(fp, " NO NO <size>\n");
458 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
459 "with standard units such as K, M, G, etc.\n"));
460 (void) fprintf(fp, gettext("\nUser-defined properties can "
461 "be specified by using a name containing a colon (:).\n"));
462 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
463 "properties must be appended with\n"
464 "a user or group specifier of one of these forms:\n"
465 " POSIX name (eg: \"matt\")\n"
466 " POSIX id (eg: \"126829\")\n"
467 " SMB name@domain (eg: \"matt@sun\")\n"
468 " SMB SID (eg: \"S-1-234-567-89\")\n"));
471 gettext("\nFor the property list, run: %s\n"),
474 gettext("\nFor the delegated permission list, run: %s\n"),
475 "zfs allow|unallow");
479 * See comments at end of main().
481 if (getenv("ZFS_ABORT") != NULL) {
482 (void) printf("dumping core by request\n");
486 exit(requested ? 0 : 2);
490 parseprop(nvlist_t *props)
492 char *propname = optarg;
493 char *propval, *strval;
495 if ((propval = strchr(propname, '=')) == NULL) {
496 (void) fprintf(stderr, gettext("missing "
497 "'=' for -o option\n"));
502 if (nvlist_lookup_string(props, propname, &strval) == 0) {
503 (void) fprintf(stderr, gettext("property '%s' "
504 "specified multiple times\n"), propname);
507 if (nvlist_add_string(props, propname, propval) != 0)
513 parse_depth(char *opt, int *flags)
518 depth = (int)strtol(opt, &tmp, 0);
520 (void) fprintf(stderr,
521 gettext("%s is not an integer\n"), optarg);
525 (void) fprintf(stderr,
526 gettext("Depth can not be negative.\n"));
529 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
533 #define PROGRESS_DELAY 2 /* seconds */
535 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";
536 static time_t pt_begin;
537 static char *pt_header = NULL;
538 static boolean_t pt_shown;
541 start_progress_timer(void)
543 pt_begin = time(NULL) + PROGRESS_DELAY;
548 set_progress_header(char *header)
550 assert(pt_header == NULL);
551 pt_header = safe_strdup(header);
553 (void) printf("%s: ", header);
554 (void) fflush(stdout);
559 update_progress(char *update)
561 if (!pt_shown && time(NULL) > pt_begin) {
562 int len = strlen(update);
564 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
566 (void) fflush(stdout);
568 } else if (pt_shown) {
569 int len = strlen(update);
571 (void) printf("%s%*.*s", update, len, len, pt_reverse);
572 (void) fflush(stdout);
577 finish_progress(char *done)
580 (void) printf("%s\n", done);
581 (void) fflush(stdout);
587 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
589 * Given an existing dataset, create a writable copy whose initial contents
590 * are the same as the source. The newly created dataset maintains a
591 * dependency on the original; the original cannot be destroyed so long as
594 * The '-p' flag creates all the non-existing ancestors of the target first.
597 zfs_do_clone(int argc, char **argv)
599 zfs_handle_t *zhp = NULL;
600 boolean_t parents = B_FALSE;
605 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
609 while ((c = getopt(argc, argv, "o:p")) != -1) {
612 if (parseprop(props))
619 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
628 /* check number of arguments */
630 (void) fprintf(stderr, gettext("missing source dataset "
635 (void) fprintf(stderr, gettext("missing target dataset "
640 (void) fprintf(stderr, gettext("too many arguments\n"));
644 /* open the source dataset */
645 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
648 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
651 * Now create the ancestors of the target dataset. If the
652 * target already exists and '-p' option was used we should not
655 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
658 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
663 ret = zfs_clone(zhp, argv[1], props);
665 /* create the mountpoint if necessary */
669 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
671 if (zfs_get_type(clone) != ZFS_TYPE_VOLUME)
672 if ((ret = zfs_mount(clone, NULL, 0)) == 0)
673 ret = zfs_share(clone);
692 * zfs create [-pu] [-o prop=value] ... fs
693 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
695 * Create a new dataset. This command can be used to create filesystems
696 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
697 * For volumes, the user must specify a size to be used.
699 * The '-s' flag applies only to volumes, and indicates that we should not try
700 * to set the reservation for this volume. By default we set a reservation
701 * equal to the size for any volume. For pools with SPA_VERSION >=
702 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
704 * The '-p' flag creates all the non-existing ancestors of the target first.
706 * The '-u' flag prevents mounting of newly created file system.
709 zfs_do_create(int argc, char **argv)
711 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
712 zfs_handle_t *zhp = NULL;
715 boolean_t noreserve = B_FALSE;
716 boolean_t bflag = B_FALSE;
717 boolean_t parents = B_FALSE;
718 boolean_t nomount = B_FALSE;
722 int canmount = ZFS_CANMOUNT_OFF;
724 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
728 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) {
731 type = ZFS_TYPE_VOLUME;
732 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
733 (void) fprintf(stderr, gettext("bad volume "
734 "size '%s': %s\n"), optarg,
735 libzfs_error_description(g_zfs));
739 if (nvlist_add_uint64(props,
740 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
749 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
750 (void) fprintf(stderr, gettext("bad volume "
751 "block size '%s': %s\n"), optarg,
752 libzfs_error_description(g_zfs));
756 if (nvlist_add_uint64(props,
757 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
762 if (parseprop(props))
772 (void) fprintf(stderr, gettext("missing size "
776 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
782 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
783 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
784 "used when creating a volume\n"));
787 if (nomount && type != ZFS_TYPE_FILESYSTEM) {
788 (void) fprintf(stderr, gettext("'-u' can only be "
789 "used when creating a file system\n"));
796 /* check number of arguments */
798 (void) fprintf(stderr, gettext("missing %s argument\n"),
799 zfs_type_to_name(type));
803 (void) fprintf(stderr, gettext("too many arguments\n"));
807 if (type == ZFS_TYPE_VOLUME && !noreserve) {
808 zpool_handle_t *zpool_handle;
809 uint64_t spa_version;
811 zfs_prop_t resv_prop;
814 if (p = strchr(argv[0], '/'))
816 zpool_handle = zpool_open(g_zfs, argv[0]);
819 if (zpool_handle == NULL)
821 spa_version = zpool_get_prop_int(zpool_handle,
822 ZPOOL_PROP_VERSION, NULL);
823 zpool_close(zpool_handle);
824 if (spa_version >= SPA_VERSION_REFRESERVATION)
825 resv_prop = ZFS_PROP_REFRESERVATION;
827 resv_prop = ZFS_PROP_RESERVATION;
828 volsize = zvol_volsize_to_reservation(volsize, props);
830 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
832 if (nvlist_add_uint64(props,
833 zfs_prop_to_name(resv_prop), volsize) != 0) {
840 if (parents && zfs_name_valid(argv[0], type)) {
842 * Now create the ancestors of target dataset. If the target
843 * already exists and '-p' option was used we should not
846 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
850 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
855 if (zfs_create(g_zfs, argv[0], type, props) != 0)
858 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
863 * if the user doesn't want the dataset automatically mounted,
864 * then skip the mount/share step
866 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type))
867 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
870 * Mount and/or share the new filesystem as appropriate. We provide a
871 * verbose error message to let the user know that their filesystem was
872 * in fact created, even if we failed to mount or share it.
874 if (!nomount && canmount == ZFS_CANMOUNT_ON) {
875 if (zfs_mount(zhp, NULL, 0) != 0) {
876 (void) fprintf(stderr, gettext("filesystem "
877 "successfully created, but not mounted\n"));
879 } else if (zfs_share(zhp) != 0) {
880 (void) fprintf(stderr, gettext("filesystem "
881 "successfully created, but not shared\n"));
898 * zfs destroy [-rRf] <fs, vol>
899 * zfs destroy [-rRd] <snap>
901 * -r Recursively destroy all children
902 * -R Recursively destroy all dependents, including clones
903 * -f Force unmounting of any dependents
904 * -d If we can't destroy now, mark for deferred destruction
906 * Destroys the given dataset. By default, it will unmount any filesystems,
907 * and refuse to destroy a dataset that has any dependents. A dependent can
908 * either be a child, or a clone of a child.
910 typedef struct destroy_cbdata {
913 boolean_t cb_recurse;
915 boolean_t cb_doclones;
916 zfs_handle_t *cb_target;
917 boolean_t cb_defer_destroy;
918 boolean_t cb_verbose;
919 boolean_t cb_parsable;
922 nvlist_t *cb_batchedsnaps;
924 /* first snap in contiguous run */
926 /* previous snap in contiguous run */
933 * Check for any dependents based on the '-r' or '-R' flags.
936 destroy_check_dependent(zfs_handle_t *zhp, void *data)
938 destroy_cbdata_t *cbp = data;
939 const char *tname = zfs_get_name(cbp->cb_target);
940 const char *name = zfs_get_name(zhp);
942 if (strncmp(tname, name, strlen(tname)) == 0 &&
943 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
945 * This is a direct descendant, not a clone somewhere else in
952 (void) fprintf(stderr, gettext("cannot destroy '%s': "
953 "%s has children\n"),
954 zfs_get_name(cbp->cb_target),
955 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
956 (void) fprintf(stderr, gettext("use '-r' to destroy "
957 "the following datasets:\n"));
958 cbp->cb_first = B_FALSE;
959 cbp->cb_error = B_TRUE;
962 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
965 * This is a clone. We only want to report this if the '-r'
966 * wasn't specified, or the target is a snapshot.
968 if (!cbp->cb_recurse &&
969 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
973 (void) fprintf(stderr, gettext("cannot destroy '%s': "
974 "%s has dependent clones\n"),
975 zfs_get_name(cbp->cb_target),
976 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
977 (void) fprintf(stderr, gettext("use '-R' to destroy "
978 "the following datasets:\n"));
979 cbp->cb_first = B_FALSE;
980 cbp->cb_error = B_TRUE;
981 cbp->cb_dryrun = B_TRUE;
984 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
993 destroy_callback(zfs_handle_t *zhp, void *data)
995 destroy_cbdata_t *cb = data;
996 const char *name = zfs_get_name(zhp);
998 if (cb->cb_verbose) {
999 if (cb->cb_parsable) {
1000 (void) printf("destroy\t%s\n", name);
1001 } else if (cb->cb_dryrun) {
1002 (void) printf(gettext("would destroy %s\n"),
1005 (void) printf(gettext("will destroy %s\n"),
1011 * Ignore pools (which we've already flagged as an error before getting
1014 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1015 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1019 if (cb->cb_dryrun) {
1025 * We batch up all contiguous snapshots (even of different
1026 * filesystems) and destroy them with one ioctl. We can't
1027 * simply do all snap deletions and then all fs deletions,
1028 * because we must delete a clone before its origin.
1030 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1031 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1033 int error = zfs_destroy_snaps_nvl(g_zfs,
1034 cb->cb_batchedsnaps, B_FALSE);
1035 fnvlist_free(cb->cb_batchedsnaps);
1036 cb->cb_batchedsnaps = fnvlist_alloc();
1039 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1040 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1051 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1053 destroy_cbdata_t *cb = arg;
1054 const char *name = zfs_get_name(zhp);
1057 if (nvlist_exists(cb->cb_nvl, name)) {
1058 if (cb->cb_firstsnap == NULL)
1059 cb->cb_firstsnap = strdup(name);
1060 if (cb->cb_prevsnap != NULL)
1061 free(cb->cb_prevsnap);
1062 /* this snap continues the current range */
1063 cb->cb_prevsnap = strdup(name);
1064 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1066 if (cb->cb_verbose) {
1067 if (cb->cb_parsable) {
1068 (void) printf("destroy\t%s\n", name);
1069 } else if (cb->cb_dryrun) {
1070 (void) printf(gettext("would destroy %s\n"),
1073 (void) printf(gettext("will destroy %s\n"),
1077 } else if (cb->cb_firstsnap != NULL) {
1078 /* end of this range */
1080 err = lzc_snaprange_space(cb->cb_firstsnap,
1081 cb->cb_prevsnap, &used);
1082 cb->cb_snapused += used;
1083 free(cb->cb_firstsnap);
1084 cb->cb_firstsnap = NULL;
1085 free(cb->cb_prevsnap);
1086 cb->cb_prevsnap = NULL;
1093 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1096 assert(cb->cb_firstsnap == NULL);
1097 assert(cb->cb_prevsnap == NULL);
1098 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1099 if (cb->cb_firstsnap != NULL) {
1102 err = lzc_snaprange_space(cb->cb_firstsnap,
1103 cb->cb_prevsnap, &used);
1105 cb->cb_snapused += used;
1106 free(cb->cb_firstsnap);
1107 cb->cb_firstsnap = NULL;
1108 free(cb->cb_prevsnap);
1109 cb->cb_prevsnap = NULL;
1115 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1117 destroy_cbdata_t *cb = arg;
1120 /* Check for clones. */
1121 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1122 cb->cb_target = zhp;
1123 cb->cb_first = B_TRUE;
1124 err = zfs_iter_dependents(zhp, B_TRUE,
1125 destroy_check_dependent, cb);
1129 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1137 gather_snapshots(zfs_handle_t *zhp, void *arg)
1139 destroy_cbdata_t *cb = arg;
1142 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1148 if (cb->cb_verbose) {
1149 err = destroy_print_snapshots(zhp, cb);
1155 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1163 destroy_clones(destroy_cbdata_t *cb)
1166 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1168 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1169 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1172 boolean_t defer = cb->cb_defer_destroy;
1176 * We can't defer destroy non-snapshots, so set it to
1177 * false while destroying the clones.
1179 cb->cb_defer_destroy = B_FALSE;
1180 err = zfs_iter_dependents(zhp, B_FALSE,
1181 destroy_callback, cb);
1182 cb->cb_defer_destroy = defer;
1192 zfs_do_destroy(int argc, char **argv)
1194 destroy_cbdata_t cb = { 0 };
1198 zfs_handle_t *zhp = NULL;
1200 zfs_type_t type = ZFS_TYPE_DATASET;
1203 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1206 cb.cb_verbose = B_TRUE;
1209 cb.cb_verbose = B_TRUE;
1210 cb.cb_parsable = B_TRUE;
1213 cb.cb_dryrun = B_TRUE;
1216 cb.cb_defer_destroy = B_TRUE;
1217 type = ZFS_TYPE_SNAPSHOT;
1220 cb.cb_force = B_TRUE;
1223 cb.cb_recurse = B_TRUE;
1226 cb.cb_recurse = B_TRUE;
1227 cb.cb_doclones = B_TRUE;
1231 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1240 /* check number of arguments */
1242 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1246 (void) fprintf(stderr, gettext("too many arguments\n"));
1250 at = strchr(argv[0], '@');
1253 /* Build the list of snaps to destroy in cb_nvl. */
1254 cb.cb_nvl = fnvlist_alloc();
1257 zhp = zfs_open(g_zfs, argv[0],
1258 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1262 cb.cb_snapspec = at + 1;
1263 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1269 if (nvlist_empty(cb.cb_nvl)) {
1270 (void) fprintf(stderr, gettext("could not find any "
1271 "snapshots to destroy; check snapshot names.\n"));
1276 if (cb.cb_verbose) {
1278 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1279 if (cb.cb_parsable) {
1280 (void) printf("reclaim\t%llu\n",
1282 } else if (cb.cb_dryrun) {
1283 (void) printf(gettext("would reclaim %s\n"),
1286 (void) printf(gettext("will reclaim %s\n"),
1291 if (!cb.cb_dryrun) {
1292 if (cb.cb_doclones) {
1293 cb.cb_batchedsnaps = fnvlist_alloc();
1294 err = destroy_clones(&cb);
1296 err = zfs_destroy_snaps_nvl(g_zfs,
1297 cb.cb_batchedsnaps, B_FALSE);
1305 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1306 cb.cb_defer_destroy);
1313 /* Open the given dataset */
1314 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1320 * Perform an explicit check for pools before going any further.
1322 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1323 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1324 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1325 "operation does not apply to pools\n"),
1327 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1328 "%s' to destroy all datasets in the pool\n"),
1330 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1331 "to destroy the pool itself\n"), zfs_get_name(zhp));
1337 * Check for any dependents and/or clones.
1339 cb.cb_first = B_TRUE;
1340 if (!cb.cb_doclones &&
1341 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1352 cb.cb_batchedsnaps = fnvlist_alloc();
1353 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1360 * Do the real thing. The callback will close the
1361 * handle regardless of whether it succeeds or not.
1363 err = destroy_callback(zhp, &cb);
1366 err = zfs_destroy_snaps_nvl(g_zfs,
1367 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1374 fnvlist_free(cb.cb_batchedsnaps);
1375 fnvlist_free(cb.cb_nvl);
1382 is_recvd_column(zprop_get_cbdata_t *cbp)
1385 zfs_get_column_t col;
1387 for (i = 0; i < ZFS_GET_NCOLS &&
1388 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1389 if (col == GET_COL_RECVD)
1395 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1396 * < all | property[,property]... > < fs | snap | vol > ...
1398 * -r recurse over any child datasets
1399 * -H scripted mode. Headers are stripped, and fields are separated
1400 * by tabs instead of spaces.
1401 * -o Set of fields to display. One of "name,property,value,
1402 * received,source". Default is "name,property,value,source".
1403 * "all" is an alias for all five.
1404 * -s Set of sources to allow. One of
1405 * "local,default,inherited,received,temporary,none". Default is
1407 * -p Display values in parsable (literal) format.
1409 * Prints properties for the given datasets. The user can control which
1410 * columns to display as well as which property types to allow.
1414 * Invoked to display the properties for a single dataset.
1417 get_callback(zfs_handle_t *zhp, void *data)
1419 char buf[ZFS_MAXPROPLEN];
1420 char rbuf[ZFS_MAXPROPLEN];
1421 zprop_source_t sourcetype;
1422 char source[ZFS_MAXNAMELEN];
1423 zprop_get_cbdata_t *cbp = data;
1424 nvlist_t *user_props = zfs_get_user_props(zhp);
1425 zprop_list_t *pl = cbp->cb_proplist;
1429 boolean_t received = is_recvd_column(cbp);
1431 for (; pl != NULL; pl = pl->pl_next) {
1432 char *recvdval = NULL;
1434 * Skip the special fake placeholder. This will also skip over
1435 * the name property when 'all' is specified.
1437 if (pl->pl_prop == ZFS_PROP_NAME &&
1438 pl == cbp->cb_proplist)
1441 if (pl->pl_prop != ZPROP_INVAL) {
1442 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1443 sizeof (buf), &sourcetype, source,
1445 cbp->cb_literal) != 0) {
1448 if (!zfs_prop_valid_for_type(pl->pl_prop,
1449 ZFS_TYPE_DATASET)) {
1450 (void) fprintf(stderr,
1451 gettext("No such property '%s'\n"),
1452 zfs_prop_to_name(pl->pl_prop));
1455 sourcetype = ZPROP_SRC_NONE;
1456 (void) strlcpy(buf, "-", sizeof (buf));
1459 if (received && (zfs_prop_get_recvd(zhp,
1460 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1461 cbp->cb_literal) == 0))
1464 zprop_print_one_property(zfs_get_name(zhp), cbp,
1465 zfs_prop_to_name(pl->pl_prop),
1466 buf, sourcetype, source, recvdval);
1467 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1468 sourcetype = ZPROP_SRC_LOCAL;
1470 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1471 buf, sizeof (buf), cbp->cb_literal) != 0) {
1472 sourcetype = ZPROP_SRC_NONE;
1473 (void) strlcpy(buf, "-", sizeof (buf));
1476 zprop_print_one_property(zfs_get_name(zhp), cbp,
1477 pl->pl_user_prop, buf, sourcetype, source, NULL);
1478 } else if (zfs_prop_written(pl->pl_user_prop)) {
1479 sourcetype = ZPROP_SRC_LOCAL;
1481 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1482 buf, sizeof (buf), cbp->cb_literal) != 0) {
1483 sourcetype = ZPROP_SRC_NONE;
1484 (void) strlcpy(buf, "-", sizeof (buf));
1487 zprop_print_one_property(zfs_get_name(zhp), cbp,
1488 pl->pl_user_prop, buf, sourcetype, source, NULL);
1490 if (nvlist_lookup_nvlist(user_props,
1491 pl->pl_user_prop, &propval) != 0) {
1494 sourcetype = ZPROP_SRC_NONE;
1497 verify(nvlist_lookup_string(propval,
1498 ZPROP_VALUE, &strval) == 0);
1499 verify(nvlist_lookup_string(propval,
1500 ZPROP_SOURCE, &sourceval) == 0);
1502 if (strcmp(sourceval,
1503 zfs_get_name(zhp)) == 0) {
1504 sourcetype = ZPROP_SRC_LOCAL;
1505 } else if (strcmp(sourceval,
1506 ZPROP_SOURCE_VAL_RECVD) == 0) {
1507 sourcetype = ZPROP_SRC_RECEIVED;
1509 sourcetype = ZPROP_SRC_INHERITED;
1510 (void) strlcpy(source,
1511 sourceval, sizeof (source));
1515 if (received && (zfs_prop_get_recvd(zhp,
1516 pl->pl_user_prop, rbuf, sizeof (rbuf),
1517 cbp->cb_literal) == 0))
1520 zprop_print_one_property(zfs_get_name(zhp), cbp,
1521 pl->pl_user_prop, strval, sourcetype,
1530 zfs_do_get(int argc, char **argv)
1532 zprop_get_cbdata_t cb = { 0 };
1533 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1534 int types = ZFS_TYPE_DATASET;
1535 char *value, *fields;
1538 zprop_list_t fake_name = { 0 };
1541 * Set up default columns and sources.
1543 cb.cb_sources = ZPROP_SRC_ALL;
1544 cb.cb_columns[0] = GET_COL_NAME;
1545 cb.cb_columns[1] = GET_COL_PROPERTY;
1546 cb.cb_columns[2] = GET_COL_VALUE;
1547 cb.cb_columns[3] = GET_COL_SOURCE;
1548 cb.cb_type = ZFS_TYPE_DATASET;
1551 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1554 cb.cb_literal = B_TRUE;
1557 limit = parse_depth(optarg, &flags);
1560 flags |= ZFS_ITER_RECURSE;
1563 cb.cb_scripted = B_TRUE;
1566 (void) fprintf(stderr, gettext("missing argument for "
1567 "'%c' option\n"), optopt);
1572 * Process the set of columns to display. We zero out
1573 * the structure to give us a blank slate.
1575 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1577 while (*optarg != '\0') {
1578 static char *col_subopts[] =
1579 { "name", "property", "value", "received",
1580 "source", "all", NULL };
1582 if (i == ZFS_GET_NCOLS) {
1583 (void) fprintf(stderr, gettext("too "
1584 "many fields given to -o "
1589 switch (getsubopt(&optarg, col_subopts,
1592 cb.cb_columns[i++] = GET_COL_NAME;
1595 cb.cb_columns[i++] = GET_COL_PROPERTY;
1598 cb.cb_columns[i++] = GET_COL_VALUE;
1601 cb.cb_columns[i++] = GET_COL_RECVD;
1602 flags |= ZFS_ITER_RECVD_PROPS;
1605 cb.cb_columns[i++] = GET_COL_SOURCE;
1609 (void) fprintf(stderr,
1610 gettext("\"all\" conflicts "
1611 "with specific fields "
1612 "given to -o option\n"));
1615 cb.cb_columns[0] = GET_COL_NAME;
1616 cb.cb_columns[1] = GET_COL_PROPERTY;
1617 cb.cb_columns[2] = GET_COL_VALUE;
1618 cb.cb_columns[3] = GET_COL_RECVD;
1619 cb.cb_columns[4] = GET_COL_SOURCE;
1620 flags |= ZFS_ITER_RECVD_PROPS;
1624 (void) fprintf(stderr,
1625 gettext("invalid column name "
1634 while (*optarg != '\0') {
1635 static char *source_subopts[] = {
1636 "local", "default", "inherited",
1637 "received", "temporary", "none",
1640 switch (getsubopt(&optarg, source_subopts,
1643 cb.cb_sources |= ZPROP_SRC_LOCAL;
1646 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1649 cb.cb_sources |= ZPROP_SRC_INHERITED;
1652 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1655 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1658 cb.cb_sources |= ZPROP_SRC_NONE;
1661 (void) fprintf(stderr,
1662 gettext("invalid source "
1671 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1672 while (*optarg != '\0') {
1673 static char *type_subopts[] = { "filesystem",
1674 "volume", "snapshot", "all", NULL };
1676 switch (getsubopt(&optarg, type_subopts,
1679 types |= ZFS_TYPE_FILESYSTEM;
1682 types |= ZFS_TYPE_VOLUME;
1685 types |= ZFS_TYPE_SNAPSHOT;
1688 types = ZFS_TYPE_DATASET;
1692 (void) fprintf(stderr,
1693 gettext("invalid type '%s'\n"),
1701 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1711 (void) fprintf(stderr, gettext("missing property "
1718 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1726 * As part of zfs_expand_proplist(), we keep track of the maximum column
1727 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1728 * need to know the maximum name length. However, the user likely did
1729 * not specify 'name' as one of the properties to fetch, so we need to
1730 * make sure we always include at least this property for
1731 * print_get_headers() to work properly.
1733 if (cb.cb_proplist != NULL) {
1734 fake_name.pl_prop = ZFS_PROP_NAME;
1735 fake_name.pl_width = strlen(gettext("NAME"));
1736 fake_name.pl_next = cb.cb_proplist;
1737 cb.cb_proplist = &fake_name;
1740 cb.cb_first = B_TRUE;
1742 /* run for each object */
1743 ret = zfs_for_each(argc, argv, flags, types, NULL,
1744 &cb.cb_proplist, limit, get_callback, &cb);
1746 if (cb.cb_proplist == &fake_name)
1747 zprop_free_list(fake_name.pl_next);
1749 zprop_free_list(cb.cb_proplist);
1755 * inherit [-rS] <property> <fs|vol> ...
1757 * -r Recurse over all children
1758 * -S Revert to received value, if any
1760 * For each dataset specified on the command line, inherit the given property
1761 * from its parent. Inheriting a property at the pool level will cause it to
1762 * use the default value. The '-r' flag will recurse over all children, and is
1763 * useful for setting a property on a hierarchy-wide basis, regardless of any
1764 * local modifications for each dataset.
1767 typedef struct inherit_cbdata {
1768 const char *cb_propname;
1769 boolean_t cb_received;
1773 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1775 inherit_cbdata_t *cb = data;
1776 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1779 * If we're doing it recursively, then ignore properties that
1780 * are not valid for this type of dataset.
1782 if (prop != ZPROP_INVAL &&
1783 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1786 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1790 inherit_cb(zfs_handle_t *zhp, void *data)
1792 inherit_cbdata_t *cb = data;
1794 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1798 zfs_do_inherit(int argc, char **argv)
1802 inherit_cbdata_t cb = { 0 };
1806 boolean_t received = B_FALSE;
1809 while ((c = getopt(argc, argv, "rS")) != -1) {
1812 flags |= ZFS_ITER_RECURSE;
1819 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1828 /* check number of arguments */
1830 (void) fprintf(stderr, gettext("missing property argument\n"));
1834 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1842 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1843 if (zfs_prop_readonly(prop)) {
1844 (void) fprintf(stderr, gettext(
1845 "%s property is read-only\n"),
1849 if (!zfs_prop_inheritable(prop) && !received) {
1850 (void) fprintf(stderr, gettext("'%s' property cannot "
1851 "be inherited\n"), propname);
1852 if (prop == ZFS_PROP_QUOTA ||
1853 prop == ZFS_PROP_RESERVATION ||
1854 prop == ZFS_PROP_REFQUOTA ||
1855 prop == ZFS_PROP_REFRESERVATION)
1856 (void) fprintf(stderr, gettext("use 'zfs set "
1857 "%s=none' to clear\n"), propname);
1860 if (received && (prop == ZFS_PROP_VOLSIZE ||
1861 prop == ZFS_PROP_VERSION)) {
1862 (void) fprintf(stderr, gettext("'%s' property cannot "
1863 "be reverted to a received value\n"), propname);
1866 } else if (!zfs_prop_user(propname)) {
1867 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1872 cb.cb_propname = propname;
1873 cb.cb_received = received;
1875 if (flags & ZFS_ITER_RECURSE) {
1876 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1877 NULL, NULL, 0, inherit_recurse_cb, &cb);
1879 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1880 NULL, NULL, 0, inherit_cb, &cb);
1886 typedef struct upgrade_cbdata {
1887 uint64_t cb_numupgraded;
1888 uint64_t cb_numsamegraded;
1889 uint64_t cb_numfailed;
1890 uint64_t cb_version;
1892 boolean_t cb_foundone;
1893 char cb_lastfs[ZFS_MAXNAMELEN];
1897 same_pool(zfs_handle_t *zhp, const char *name)
1899 int len1 = strcspn(name, "/@");
1900 const char *zhname = zfs_get_name(zhp);
1901 int len2 = strcspn(zhname, "/@");
1905 return (strncmp(name, zhname, len1) == 0);
1909 upgrade_list_callback(zfs_handle_t *zhp, void *data)
1911 upgrade_cbdata_t *cb = data;
1912 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1914 /* list if it's old/new */
1915 if ((!cb->cb_newer && version < ZPL_VERSION) ||
1916 (cb->cb_newer && version > ZPL_VERSION)) {
1919 str = gettext("The following filesystems are "
1920 "formatted using a newer software version and\n"
1921 "cannot be accessed on the current system.\n\n");
1923 str = gettext("The following filesystems are "
1924 "out of date, and can be upgraded. After being\n"
1925 "upgraded, these filesystems (and any 'zfs send' "
1926 "streams generated from\n"
1927 "subsequent snapshots) will no longer be "
1928 "accessible by older software versions.\n\n");
1931 if (!cb->cb_foundone) {
1933 (void) printf(gettext("VER FILESYSTEM\n"));
1934 (void) printf(gettext("--- ------------\n"));
1935 cb->cb_foundone = B_TRUE;
1938 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
1945 upgrade_set_callback(zfs_handle_t *zhp, void *data)
1947 upgrade_cbdata_t *cb = data;
1948 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1949 int needed_spa_version;
1952 if (zfs_spa_version(zhp, &spa_version) < 0)
1955 needed_spa_version = zfs_spa_version_map(cb->cb_version);
1957 if (needed_spa_version < 0)
1960 if (spa_version < needed_spa_version) {
1962 (void) printf(gettext("%s: can not be "
1963 "upgraded; the pool version needs to first "
1964 "be upgraded\nto version %d\n\n"),
1965 zfs_get_name(zhp), needed_spa_version);
1971 if (version < cb->cb_version) {
1973 (void) snprintf(verstr, sizeof (verstr),
1974 "%llu", cb->cb_version);
1975 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
1977 * If they did "zfs upgrade -a", then we could
1978 * be doing ioctls to different pools. We need
1979 * to log this history once to each pool, and bypass
1980 * the normal history logging that happens in main().
1982 (void) zpool_log_history(g_zfs, history_str);
1983 log_history = B_FALSE;
1985 if (zfs_prop_set(zhp, "version", verstr) == 0)
1986 cb->cb_numupgraded++;
1989 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
1990 } else if (version > cb->cb_version) {
1991 /* can't downgrade */
1992 (void) printf(gettext("%s: can not be downgraded; "
1993 "it is already at version %u\n"),
1994 zfs_get_name(zhp), version);
1997 cb->cb_numsamegraded++;
2005 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2008 zfs_do_upgrade(int argc, char **argv)
2010 boolean_t all = B_FALSE;
2011 boolean_t showversions = B_FALSE;
2013 upgrade_cbdata_t cb = { 0 };
2015 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2018 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2021 flags |= ZFS_ITER_RECURSE;
2024 showversions = B_TRUE;
2027 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2028 optarg, &cb.cb_version) != 0) {
2029 (void) fprintf(stderr,
2030 gettext("invalid version %s\n"), optarg);
2039 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2048 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2050 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2051 cb.cb_version || argc))
2053 if ((all || argc) && (showversions))
2059 /* Show info on available versions. */
2060 (void) printf(gettext("The following filesystem versions are "
2062 (void) printf(gettext("VER DESCRIPTION\n"));
2063 (void) printf("--- -----------------------------------------"
2064 "---------------\n");
2065 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2066 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2067 (void) printf(gettext(" 3 Case insensitive and filesystem "
2068 "user identifier (FUID)\n"));
2069 (void) printf(gettext(" 4 userquota, groupquota "
2071 (void) printf(gettext(" 5 System attributes\n"));
2072 (void) printf(gettext("\nFor more information on a particular "
2073 "version, including supported releases,\n"));
2074 (void) printf("see the ZFS Administration Guide.\n\n");
2076 } else if (argc || all) {
2077 /* Upgrade filesystems */
2078 if (cb.cb_version == 0)
2079 cb.cb_version = ZPL_VERSION;
2080 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2081 NULL, NULL, 0, upgrade_set_callback, &cb);
2082 (void) printf(gettext("%llu filesystems upgraded\n"),
2084 if (cb.cb_numsamegraded) {
2085 (void) printf(gettext("%llu filesystems already at "
2087 cb.cb_numsamegraded);
2089 if (cb.cb_numfailed != 0)
2092 /* List old-version filesytems */
2094 (void) printf(gettext("This system is currently running "
2095 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2097 flags |= ZFS_ITER_RECURSE;
2098 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2099 NULL, NULL, 0, upgrade_list_callback, &cb);
2101 found = cb.cb_foundone;
2102 cb.cb_foundone = B_FALSE;
2103 cb.cb_newer = B_TRUE;
2105 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2106 NULL, NULL, 0, upgrade_list_callback, &cb);
2108 if (!cb.cb_foundone && !found) {
2109 (void) printf(gettext("All filesystems are "
2110 "formatted with the current version.\n"));
2118 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2119 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2120 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2121 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2123 * -H Scripted mode; elide headers and separate columns by tabs.
2124 * -i Translate SID to POSIX ID.
2125 * -n Print numeric ID instead of user/group name.
2126 * -o Control which fields to display.
2127 * -p Use exact (parseable) numeric output.
2128 * -s Specify sort columns, descending order.
2129 * -S Specify sort columns, ascending order.
2130 * -t Control which object types to display.
2132 * Displays space consumed by, and quotas on, each user in the specified
2133 * filesystem or snapshot.
2136 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2137 enum us_field_types {
2143 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2144 static char *us_field_names[] = { "type", "name", "used", "quota" };
2145 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2147 #define USTYPE_PSX_GRP (1 << 0)
2148 #define USTYPE_PSX_USR (1 << 1)
2149 #define USTYPE_SMB_GRP (1 << 2)
2150 #define USTYPE_SMB_USR (1 << 3)
2151 #define USTYPE_ALL \
2152 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2154 static int us_type_bits[] = {
2161 static char *us_type_names[] = { "posixgroup", "posxiuser", "smbgroup",
2164 typedef struct us_node {
2166 uu_avl_node_t usn_avlnode;
2167 uu_list_node_t usn_listnode;
2170 typedef struct us_cbdata {
2172 uu_avl_pool_t *cb_avl_pool;
2174 boolean_t cb_numname;
2175 boolean_t cb_nicenum;
2176 boolean_t cb_sid2posix;
2177 zfs_userquota_prop_t cb_prop;
2178 zfs_sort_column_t *cb_sortcol;
2179 size_t cb_width[USFIELD_LAST];
2182 static boolean_t us_populated = B_FALSE;
2185 zfs_sort_column_t *si_sortcol;
2186 boolean_t si_numname;
2190 us_field_index(char *field)
2194 for (i = 0; i < USFIELD_LAST; i++) {
2195 if (strcmp(field, us_field_names[i]) == 0)
2203 us_compare(const void *larg, const void *rarg, void *unused)
2205 const us_node_t *l = larg;
2206 const us_node_t *r = rarg;
2207 us_sort_info_t *si = (us_sort_info_t *)unused;
2208 zfs_sort_column_t *sortcol = si->si_sortcol;
2209 boolean_t numname = si->si_numname;
2210 nvlist_t *lnvl = l->usn_nvl;
2211 nvlist_t *rnvl = r->usn_nvl;
2215 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2222 zfs_prop_t prop = sortcol->sc_prop;
2223 const char *propname = NULL;
2224 boolean_t reverse = sortcol->sc_reverse;
2229 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2230 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2232 rc = (rv32 < lv32) ? 1 : -1;
2237 (void) nvlist_lookup_uint64(lnvl, propname,
2239 (void) nvlist_lookup_uint64(rnvl, propname,
2242 rc = (rv64 < lv64) ? 1 : -1;
2244 (void) nvlist_lookup_string(lnvl, propname,
2246 (void) nvlist_lookup_string(rnvl, propname,
2248 rc = strcmp(lvstr, rvstr);
2252 case ZFS_PROP_QUOTA:
2255 if (prop == ZFS_PROP_USED)
2259 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2260 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2262 rc = (rv64 < lv64) ? 1 : -1;
2268 return (reverse ? 1 : -1);
2270 return (reverse ? -1 : 1);
2275 * If entries still seem to be the same, check if they are of the same
2276 * type (smbentity is added only if we are doing SID to POSIX ID
2277 * translation where we can have duplicate type/name combinations).
2279 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2280 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2282 return (lvb < rvb ? -1 : 1);
2287 static inline const char *
2288 us_type2str(unsigned field_type)
2290 switch (field_type) {
2291 case USTYPE_PSX_USR:
2292 return ("POSIX User");
2293 case USTYPE_PSX_GRP:
2294 return ("POSIX Group");
2295 case USTYPE_SMB_USR:
2296 return ("SMB User");
2297 case USTYPE_SMB_GRP:
2298 return ("SMB Group");
2300 return ("Undefined");
2305 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2307 us_cbdata_t *cb = (us_cbdata_t *)arg;
2308 zfs_userquota_prop_t prop = cb->cb_prop;
2313 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2314 uu_avl_t *avl = cb->cb_avl;
2318 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2320 const char *typestr;
2324 int typeidx, nameidx, sizeidx;
2325 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2326 boolean_t smbentity = B_FALSE;
2328 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2330 node = safe_malloc(sizeof (us_node_t));
2331 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2332 node->usn_nvl = props;
2334 if (domain != NULL && domain[0] != '\0') {
2336 char sid[ZFS_MAXNAMELEN + 32];
2341 directory_error_t e;
2346 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2348 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2349 type = USTYPE_SMB_GRP;
2351 err = sid_to_id(sid, B_FALSE, &id);
2354 type = USTYPE_SMB_USR;
2356 err = sid_to_id(sid, B_TRUE, &id);
2363 if (!cb->cb_sid2posix) {
2364 e = directory_name_from_sid(NULL, sid, &name,
2367 directory_error_free(e);
2375 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2377 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2378 type = USTYPE_PSX_GRP;
2379 if (!cb->cb_numname) {
2382 if ((g = getgrgid(rid)) != NULL)
2386 type = USTYPE_PSX_USR;
2387 if (!cb->cb_numname) {
2390 if ((p = getpwuid(rid)) != NULL)
2397 * Make sure that the type/name combination is unique when doing
2398 * SID to POSIX ID translation (hence changing the type from SMB to
2401 if (cb->cb_sid2posix &&
2402 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2405 /* Calculate/update width of TYPE field */
2406 typestr = us_type2str(type);
2407 typelen = strlen(gettext(typestr));
2408 typeidx = us_field_index("type");
2409 if (typelen > cb->cb_width[typeidx])
2410 cb->cb_width[typeidx] = typelen;
2411 if (nvlist_add_uint32(props, "type", type) != 0)
2414 /* Calculate/update width of NAME field */
2415 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2416 if (nvlist_add_uint64(props, "name", rid) != 0)
2418 namelen = snprintf(NULL, 0, "%u", rid);
2420 if (nvlist_add_string(props, "name", name) != 0)
2422 namelen = strlen(name);
2424 nameidx = us_field_index("name");
2425 if (namelen > cb->cb_width[nameidx])
2426 cb->cb_width[nameidx] = namelen;
2429 * Check if this type/name combination is in the list and update it;
2430 * otherwise add new node to the list.
2432 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2433 uu_avl_insert(avl, node, idx);
2438 props = node->usn_nvl;
2441 /* Calculate/update width of USED/QUOTA fields */
2443 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2445 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2446 sizelen = strlen(sizebuf);
2447 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2449 if (!nvlist_exists(props, "quota"))
2450 (void) nvlist_add_uint64(props, "quota", 0);
2453 if (!nvlist_exists(props, "used"))
2454 (void) nvlist_add_uint64(props, "used", 0);
2456 sizeidx = us_field_index(propname);
2457 if (sizelen > cb->cb_width[sizeidx])
2458 cb->cb_width[sizeidx] = sizelen;
2460 if (nvlist_add_uint64(props, propname, space) != 0)
2467 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2468 size_t *width, us_node_t *node)
2470 nvlist_t *nvl = node->usn_nvl;
2471 char valstr[ZFS_MAXNAMELEN];
2472 boolean_t first = B_TRUE;
2478 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2479 if (!(ustype & types))
2482 while ((field = fields[cfield]) != USFIELD_LAST) {
2483 nvpair_t *nvp = NULL;
2487 char *strval = NULL;
2489 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2490 if (strcmp(nvpair_name(nvp),
2491 us_field_names[field]) == 0)
2495 type = nvpair_type(nvp);
2497 case DATA_TYPE_UINT32:
2498 (void) nvpair_value_uint32(nvp, &val32);
2500 case DATA_TYPE_UINT64:
2501 (void) nvpair_value_uint64(nvp, &val64);
2503 case DATA_TYPE_STRING:
2504 (void) nvpair_value_string(nvp, &strval);
2507 (void) fprintf(stderr, "invalid data type\n");
2512 strval = (char *)us_type2str(val32);
2515 if (type == DATA_TYPE_UINT64) {
2516 (void) sprintf(valstr, "%llu", val64);
2522 if (type == DATA_TYPE_UINT64) {
2524 (void) sprintf(valstr, "%llu", val64);
2526 zfs_nicenum(val64, valstr,
2529 if (field == USFIELD_QUOTA &&
2530 strcmp(valstr, "0") == 0)
2540 (void) printf("\t");
2545 (void) printf("%s", strval);
2546 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2547 (void) printf("%-*s", width[field], strval);
2549 (void) printf("%*s", width[field], strval);
2555 (void) printf("\n");
2559 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2560 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2568 boolean_t first = B_TRUE;
2570 while ((field = fields[cfield]) != USFIELD_LAST) {
2571 col = gettext(us_field_hdr[field]);
2572 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2573 (void) printf(first ? "%-*s" : " %-*s",
2576 (void) printf(first ? "%*s" : " %*s",
2582 (void) printf("\n");
2585 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2586 print_us_node(scripted, parsable, fields, types, width, node);
2588 nvlist_free(node->usn_nvl);
2593 zfs_do_userspace(int argc, char **argv)
2596 zfs_userquota_prop_t p;
2598 uu_avl_pool_t *avl_pool;
2600 uu_avl_walk_t *walk;
2602 char deffields[] = "type,name,used,quota";
2603 char *ofield = NULL;
2604 char *tfield = NULL;
2608 boolean_t scripted = B_FALSE;
2609 boolean_t prtnum = B_FALSE;
2610 boolean_t parsable = B_FALSE;
2611 boolean_t sid2posix = B_FALSE;
2614 zfs_sort_column_t *sortcol = NULL;
2615 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2619 uu_list_pool_t *listpool;
2621 uu_avl_index_t idx = 0;
2622 uu_list_index_t idx2 = 0;
2627 if (strcmp(argv[0], "groupspace") == 0)
2628 /* Toggle default group types */
2629 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2631 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2647 if (zfs_add_sort_column(&sortcol, optarg,
2648 c == 's' ? B_FALSE : B_TRUE) != 0) {
2649 (void) fprintf(stderr,
2650 gettext("invalid field '%s'\n"), optarg);
2661 (void) fprintf(stderr, gettext("missing argument for "
2662 "'%c' option\n"), optopt);
2666 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2676 (void) fprintf(stderr, gettext("missing dataset name\n"));
2680 (void) fprintf(stderr, gettext("too many arguments\n"));
2684 /* Use default output fields if not specified using -o */
2688 if ((delim = strchr(ofield, ',')) != NULL)
2690 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2691 (void) fprintf(stderr, gettext("invalid type '%s' "
2692 "for -o option\n"), ofield);
2697 } while (delim != NULL);
2698 fields[cfield] = USFIELD_LAST;
2700 /* Override output types (-t option) */
2701 if (tfield != NULL) {
2705 boolean_t found = B_FALSE;
2707 if ((delim = strchr(tfield, ',')) != NULL)
2709 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2711 if (strcmp(tfield, us_type_names[i]) == 0) {
2713 types |= us_type_bits[i];
2718 (void) fprintf(stderr, gettext("invalid type "
2719 "'%s' for -t option\n"), tfield);
2724 } while (delim != NULL);
2727 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2730 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2731 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2733 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2736 /* Always add default sorting columns */
2737 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2738 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2740 cb.cb_sortcol = sortcol;
2741 cb.cb_numname = prtnum;
2742 cb.cb_nicenum = !parsable;
2743 cb.cb_avl_pool = avl_pool;
2744 cb.cb_avl = avl_tree;
2745 cb.cb_sid2posix = sid2posix;
2747 for (i = 0; i < USFIELD_LAST; i++)
2748 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2750 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2751 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2752 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2753 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2754 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2757 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2762 if ((node = uu_avl_first(avl_tree)) == NULL)
2765 us_populated = B_TRUE;
2767 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2768 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2769 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2770 uu_list_node_init(node, &node->usn_listnode, listpool);
2772 while (node != NULL) {
2774 node = uu_avl_next(avl_tree, node);
2775 uu_avl_remove(avl_tree, rmnode);
2776 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2777 uu_list_insert(list, rmnode, idx2);
2780 for (node = uu_list_first(list); node != NULL;
2781 node = uu_list_next(list, node)) {
2782 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2784 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2785 uu_avl_insert(avl_tree, node, idx);
2788 uu_list_destroy(list);
2789 uu_list_pool_destroy(listpool);
2791 /* Print and free node nvlist memory */
2792 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2795 zfs_free_sort_columns(sortcol);
2797 /* Clean up the AVL tree */
2798 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2801 while ((node = uu_avl_walk_next(walk)) != NULL) {
2802 uu_avl_remove(cb.cb_avl, node);
2806 uu_avl_walk_end(walk);
2807 uu_avl_destroy(avl_tree);
2808 uu_avl_pool_destroy(avl_pool);
2814 * list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
2815 * [-s property [-s property]...] [-S property [-S property]...]
2818 * -r Recurse over all children
2819 * -d Limit recursion by depth.
2820 * -H Scripted mode; elide headers and separate columns by tabs
2821 * -o Control which fields to display.
2822 * -t Control which object types to display.
2823 * -s Specify sort columns, descending order.
2824 * -S Specify sort columns, ascending order.
2826 * When given no arguments, lists all filesystems in the system.
2827 * Otherwise, list the specified datasets, optionally recursing down them if
2828 * '-r' is specified.
2830 typedef struct list_cbdata {
2832 boolean_t cb_scripted;
2833 zprop_list_t *cb_proplist;
2837 * Given a list of columns to display, output appropriate headers for each one.
2840 print_header(zprop_list_t *pl)
2842 char headerbuf[ZFS_MAXPROPLEN];
2845 boolean_t first = B_TRUE;
2846 boolean_t right_justify;
2848 for (; pl != NULL; pl = pl->pl_next) {
2855 right_justify = B_FALSE;
2856 if (pl->pl_prop != ZPROP_INVAL) {
2857 header = zfs_prop_column_name(pl->pl_prop);
2858 right_justify = zfs_prop_align_right(pl->pl_prop);
2860 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2861 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2862 headerbuf[i] = '\0';
2866 if (pl->pl_next == NULL && !right_justify)
2867 (void) printf("%s", header);
2868 else if (right_justify)
2869 (void) printf("%*s", pl->pl_width, header);
2871 (void) printf("%-*s", pl->pl_width, header);
2874 (void) printf("\n");
2878 * Given a dataset and a list of fields, print out all the properties according
2879 * to the described layout.
2882 print_dataset(zfs_handle_t *zhp, zprop_list_t *pl, boolean_t scripted)
2884 boolean_t first = B_TRUE;
2885 char property[ZFS_MAXPROPLEN];
2886 nvlist_t *userprops = zfs_get_user_props(zhp);
2889 boolean_t right_justify;
2892 for (; pl != NULL; pl = pl->pl_next) {
2895 (void) printf("\t");
2902 if (pl->pl_prop == ZFS_PROP_NAME) {
2903 (void) strlcpy(property, zfs_get_name(zhp),
2906 right_justify = zfs_prop_align_right(pl->pl_prop);
2907 } else if (pl->pl_prop != ZPROP_INVAL) {
2908 if (zfs_prop_get(zhp, pl->pl_prop, property,
2909 sizeof (property), NULL, NULL, 0, B_FALSE) != 0)
2914 right_justify = zfs_prop_align_right(pl->pl_prop);
2915 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
2916 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
2917 property, sizeof (property), B_FALSE) != 0)
2921 right_justify = B_TRUE;
2922 } else if (zfs_prop_written(pl->pl_user_prop)) {
2923 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
2924 property, sizeof (property), B_FALSE) != 0)
2928 right_justify = B_TRUE;
2930 if (nvlist_lookup_nvlist(userprops,
2931 pl->pl_user_prop, &propval) != 0)
2934 verify(nvlist_lookup_string(propval,
2935 ZPROP_VALUE, &propstr) == 0);
2936 right_justify = B_FALSE;
2939 width = pl->pl_width;
2942 * If this is being called in scripted mode, or if this is the
2943 * last column and it is left-justified, don't include a width
2946 if (scripted || (pl->pl_next == NULL && !right_justify))
2947 (void) printf("%s", propstr);
2948 else if (right_justify)
2949 (void) printf("%*s", width, propstr);
2951 (void) printf("%-*s", width, propstr);
2954 (void) printf("\n");
2958 * Generic callback function to list a dataset or snapshot.
2961 list_callback(zfs_handle_t *zhp, void *data)
2963 list_cbdata_t *cbp = data;
2965 if (cbp->cb_first) {
2966 if (!cbp->cb_scripted)
2967 print_header(cbp->cb_proplist);
2968 cbp->cb_first = B_FALSE;
2971 print_dataset(zhp, cbp->cb_proplist, cbp->cb_scripted);
2977 zfs_do_list(int argc, char **argv)
2980 boolean_t scripted = B_FALSE;
2981 static char default_fields[] =
2982 "name,used,available,referenced,mountpoint";
2983 int types = ZFS_TYPE_DATASET;
2984 boolean_t types_specified = B_FALSE;
2985 char *fields = NULL;
2986 list_cbdata_t cb = { 0 };
2990 zfs_sort_column_t *sortcol = NULL;
2991 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
2994 while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
3000 limit = parse_depth(optarg, &flags);
3003 flags |= ZFS_ITER_RECURSE;
3009 if (zfs_add_sort_column(&sortcol, optarg,
3011 (void) fprintf(stderr,
3012 gettext("invalid property '%s'\n"), optarg);
3017 if (zfs_add_sort_column(&sortcol, optarg,
3019 (void) fprintf(stderr,
3020 gettext("invalid property '%s'\n"), optarg);
3026 types_specified = B_TRUE;
3027 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3028 while (*optarg != '\0') {
3029 static char *type_subopts[] = { "filesystem",
3030 "volume", "snapshot", "all", NULL };
3032 switch (getsubopt(&optarg, type_subopts,
3035 types |= ZFS_TYPE_FILESYSTEM;
3038 types |= ZFS_TYPE_VOLUME;
3041 types |= ZFS_TYPE_SNAPSHOT;
3044 types = ZFS_TYPE_DATASET;
3048 (void) fprintf(stderr,
3049 gettext("invalid type '%s'\n"),
3056 (void) fprintf(stderr, gettext("missing argument for "
3057 "'%c' option\n"), optopt);
3061 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3071 fields = default_fields;
3074 * If we are only going to list snapshot names and sort by name,
3075 * then we can use faster version.
3077 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3078 flags |= ZFS_ITER_SIMPLE;
3081 * If "-o space" and no types were specified, don't display snapshots.
3083 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3084 types &= ~ZFS_TYPE_SNAPSHOT;
3087 * If the user specifies '-o all', the zprop_get_list() doesn't
3088 * normally include the name of the dataset. For 'zfs list', we always
3089 * want this property to be first.
3091 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3095 cb.cb_scripted = scripted;
3096 cb.cb_first = B_TRUE;
3098 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3099 limit, list_callback, &cb);
3101 zprop_free_list(cb.cb_proplist);
3102 zfs_free_sort_columns(sortcol);
3104 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3105 (void) printf(gettext("no datasets available\n"));
3111 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3112 * zfs rename [-f] -p <fs | vol> <fs | vol>
3113 * zfs rename -r <snap> <snap>
3114 * zfs rename -u [-p] <fs> <fs>
3116 * Renames the given dataset to another of the same type.
3118 * The '-p' flag creates all the non-existing ancestors of the target first.
3122 zfs_do_rename(int argc, char **argv)
3125 renameflags_t flags = { 0 };
3129 boolean_t parents = B_FALSE;
3130 char *snapshot = NULL;
3133 while ((c = getopt(argc, argv, "fpru")) != -1) {
3139 flags.recurse = B_TRUE;
3142 flags.nounmount = B_TRUE;
3145 flags.forceunmount = B_TRUE;
3149 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3158 /* check number of arguments */
3160 (void) fprintf(stderr, gettext("missing source dataset "
3165 (void) fprintf(stderr, gettext("missing target dataset "
3170 (void) fprintf(stderr, gettext("too many arguments\n"));
3174 if (flags.recurse && parents) {
3175 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3180 if (flags.recurse && strchr(argv[0], '@') == 0) {
3181 (void) fprintf(stderr, gettext("source dataset for recursive "
3182 "rename must be a snapshot\n"));
3186 if (flags.nounmount && parents) {
3187 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3192 if (flags.nounmount)
3193 types = ZFS_TYPE_FILESYSTEM;
3195 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3197 types = ZFS_TYPE_DATASET;
3199 if (flags.recurse) {
3201 * When we do recursive rename we are fine when the given
3202 * snapshot for the given dataset doesn't exist - it can
3203 * still exists below.
3206 snapshot = strchr(argv[0], '@');
3207 assert(snapshot != NULL);
3212 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3215 /* If we were asked and the name looks good, try to create ancestors. */
3216 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3217 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3222 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3231 * Promotes the given clone fs to be the parent
3235 zfs_do_promote(int argc, char **argv)
3241 if (argc > 1 && argv[1][0] == '-') {
3242 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3247 /* check number of arguments */
3249 (void) fprintf(stderr, gettext("missing clone filesystem"
3254 (void) fprintf(stderr, gettext("too many arguments\n"));
3258 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3262 ret = (zfs_promote(zhp) != 0);
3270 * zfs rollback [-rRf] <snapshot>
3272 * -r Delete any intervening snapshots before doing rollback
3273 * -R Delete any snapshots and their clones
3274 * -f ignored for backwards compatability
3276 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3277 * since then and making it the active dataset. If more recent snapshots exist,
3278 * the command will complain unless the '-r' flag is given.
3280 typedef struct rollback_cbdata {
3286 boolean_t cb_recurse;
3287 boolean_t cb_dependent;
3288 } rollback_cbdata_t;
3291 * Report any snapshots more recent than the one specified. Used when '-r' is
3292 * not specified. We reuse this same callback for the snapshot dependents - if
3293 * 'cb_dependent' is set, then this is a dependent and we should report it
3294 * without checking the transaction group.
3297 rollback_check(zfs_handle_t *zhp, void *data)
3299 rollback_cbdata_t *cbp = data;
3301 if (cbp->cb_doclones) {
3306 if (!cbp->cb_dependent) {
3307 if (strcmp(zfs_get_name(zhp), cbp->cb_target) != 0 &&
3308 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3309 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3312 if (cbp->cb_first && !cbp->cb_recurse) {
3313 (void) fprintf(stderr, gettext("cannot "
3314 "rollback to '%s': more recent snapshots "
3317 (void) fprintf(stderr, gettext("use '-r' to "
3318 "force deletion of the following "
3324 if (cbp->cb_recurse) {
3325 cbp->cb_dependent = B_TRUE;
3326 if (zfs_iter_dependents(zhp, B_TRUE,
3327 rollback_check, cbp) != 0) {
3331 cbp->cb_dependent = B_FALSE;
3333 (void) fprintf(stderr, "%s\n",
3338 if (cbp->cb_first && cbp->cb_recurse) {
3339 (void) fprintf(stderr, gettext("cannot rollback to "
3340 "'%s': clones of previous snapshots exist\n"),
3342 (void) fprintf(stderr, gettext("use '-R' to "
3343 "force deletion of the following clones and "
3349 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3357 zfs_do_rollback(int argc, char **argv)
3361 boolean_t force = B_FALSE;
3362 rollback_cbdata_t cb = { 0 };
3363 zfs_handle_t *zhp, *snap;
3364 char parentname[ZFS_MAXNAMELEN];
3368 while ((c = getopt(argc, argv, "rRf")) != -1) {
3381 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3390 /* check number of arguments */
3392 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3396 (void) fprintf(stderr, gettext("too many arguments\n"));
3400 /* open the snapshot */
3401 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3404 /* open the parent dataset */
3405 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3406 verify((delim = strrchr(parentname, '@')) != NULL);
3408 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3414 * Check for more recent snapshots and/or clones based on the presence
3417 cb.cb_target = argv[0];
3418 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3419 cb.cb_first = B_TRUE;
3421 if ((ret = zfs_iter_children(zhp, rollback_check, &cb)) != 0)
3424 if ((ret = cb.cb_error) != 0)
3428 * Rollback parent to the given snapshot.
3430 ret = zfs_rollback(zhp, snap, force);
3443 * zfs set property=value { fs | snap | vol } ...
3445 * Sets the given property for all datasets specified on the command line.
3447 typedef struct set_cbdata {
3453 set_callback(zfs_handle_t *zhp, void *data)
3455 set_cbdata_t *cbp = data;
3457 if (zfs_prop_set(zhp, cbp->cb_propname, cbp->cb_value) != 0) {
3458 switch (libzfs_errno(g_zfs)) {
3459 case EZFS_MOUNTFAILED:
3460 (void) fprintf(stderr, gettext("property may be set "
3461 "but unable to remount filesystem\n"));
3463 case EZFS_SHARENFSFAILED:
3464 (void) fprintf(stderr, gettext("property may be set "
3465 "but unable to reshare filesystem\n"));
3474 zfs_do_set(int argc, char **argv)
3479 /* check for options */
3480 if (argc > 1 && argv[1][0] == '-') {
3481 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3486 /* check number of arguments */
3488 (void) fprintf(stderr, gettext("missing property=value "
3493 (void) fprintf(stderr, gettext("missing dataset name\n"));
3497 /* validate property=value argument */
3498 cb.cb_propname = argv[1];
3499 if (((cb.cb_value = strchr(cb.cb_propname, '=')) == NULL) ||
3500 (cb.cb_value[1] == '\0')) {
3501 (void) fprintf(stderr, gettext("missing value in "
3502 "property=value argument\n"));
3506 *cb.cb_value = '\0';
3509 if (*cb.cb_propname == '\0') {
3510 (void) fprintf(stderr,
3511 gettext("missing property in property=value argument\n"));
3515 ret = zfs_for_each(argc - 2, argv + 2, 0,
3516 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
3521 typedef struct snap_cbdata {
3523 boolean_t sd_recursive;
3524 const char *sd_snapname;
3528 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3530 snap_cbdata_t *sd = arg;
3535 if (sd->sd_recursive &&
3536 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3541 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3544 fnvlist_add_boolean(sd->sd_nvl, name);
3547 if (sd->sd_recursive)
3548 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3554 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3556 * Creates a snapshot with the given name. While functionally equivalent to
3557 * 'zfs create', it is a separate command to differentiate intent.
3560 zfs_do_snapshot(int argc, char **argv)
3565 snap_cbdata_t sd = { 0 };
3566 boolean_t multiple_snaps = B_FALSE;
3568 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3570 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3574 while ((c = getopt(argc, argv, "ro:")) != -1) {
3577 if (parseprop(props))
3581 sd.sd_recursive = B_TRUE;
3582 multiple_snaps = B_TRUE;
3585 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3594 /* check number of arguments */
3596 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3601 multiple_snaps = B_TRUE;
3602 for (; argc > 0; argc--, argv++) {
3606 atp = strchr(argv[0], '@');
3610 sd.sd_snapname = atp + 1;
3611 zhp = zfs_open(g_zfs, argv[0],
3612 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3615 if (zfs_snapshot_cb(zhp, &sd) != 0)
3619 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3620 nvlist_free(sd.sd_nvl);
3622 if (ret != 0 && multiple_snaps)
3623 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3627 nvlist_free(sd.sd_nvl);
3634 * Send a backup stream to stdout.
3637 zfs_do_send(int argc, char **argv)
3639 char *fromname = NULL;
3640 char *toname = NULL;
3643 sendflags_t flags = { 0 };
3645 nvlist_t *dbgnv = NULL;
3646 boolean_t extraverbose = B_FALSE;
3649 while ((c = getopt(argc, argv, ":i:I:RDpvnP")) != -1) {
3660 flags.doall = B_TRUE;
3663 flags.replicate = B_TRUE;
3666 flags.props = B_TRUE;
3669 flags.parsable = B_TRUE;
3670 flags.verbose = B_TRUE;
3674 extraverbose = B_TRUE;
3675 flags.verbose = B_TRUE;
3676 flags.progress = B_TRUE;
3679 flags.dedup = B_TRUE;
3682 flags.dryrun = B_TRUE;
3685 (void) fprintf(stderr, gettext("missing argument for "
3686 "'%c' option\n"), optopt);
3690 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3699 /* check number of arguments */
3701 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3705 (void) fprintf(stderr, gettext("too many arguments\n"));
3709 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3710 (void) fprintf(stderr,
3711 gettext("Error: Stream can not be written to a terminal.\n"
3712 "You must redirect standard output.\n"));
3716 cp = strchr(argv[0], '@');
3718 (void) fprintf(stderr,
3719 gettext("argument must be a snapshot\n"));
3724 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3729 * If they specified the full path to the snapshot, chop off
3730 * everything except the short name of the snapshot, but special
3731 * case if they specify the origin.
3733 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3734 char origin[ZFS_MAXNAMELEN];
3737 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3738 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3740 if (strcmp(origin, fromname) == 0) {
3742 flags.fromorigin = B_TRUE;
3745 if (cp != fromname && strcmp(argv[0], fromname)) {
3746 (void) fprintf(stderr,
3747 gettext("incremental source must be "
3748 "in same filesystem\n"));
3752 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3753 (void) fprintf(stderr,
3754 gettext("invalid incremental source\n"));
3760 if (flags.replicate && fromname == NULL)
3761 flags.doall = B_TRUE;
3763 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3764 extraverbose ? &dbgnv : NULL);
3766 if (extraverbose && dbgnv != NULL) {
3768 * dump_nvlist prints to stdout, but that's been
3769 * redirected to a file. Make it print to stderr
3772 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3773 dump_nvlist(dbgnv, 0);
3782 * zfs receive [-vnFu] [-d | -e] <fs@snap>
3784 * Restore a backup stream from stdin.
3787 zfs_do_receive(int argc, char **argv)
3790 recvflags_t flags = { 0 };
3793 while ((c = getopt(argc, argv, ":denuvF")) != -1) {
3796 flags.isprefix = B_TRUE;
3799 flags.isprefix = B_TRUE;
3800 flags.istail = B_TRUE;
3803 flags.dryrun = B_TRUE;
3806 flags.nomount = B_TRUE;
3809 flags.verbose = B_TRUE;
3812 flags.force = B_TRUE;
3815 (void) fprintf(stderr, gettext("missing argument for "
3816 "'%c' option\n"), optopt);
3820 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3829 /* check number of arguments */
3831 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3835 (void) fprintf(stderr, gettext("too many arguments\n"));
3839 if (isatty(STDIN_FILENO)) {
3840 (void) fprintf(stderr,
3841 gettext("Error: Backup stream can not be read "
3842 "from a terminal.\n"
3843 "You must redirect standard input.\n"));
3847 err = zfs_receive(g_zfs, argv[0], &flags, STDIN_FILENO, NULL);
3853 * allow/unallow stuff
3855 /* copied from zfs/sys/dsl_deleg.h */
3856 #define ZFS_DELEG_PERM_CREATE "create"
3857 #define ZFS_DELEG_PERM_DESTROY "destroy"
3858 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
3859 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
3860 #define ZFS_DELEG_PERM_CLONE "clone"
3861 #define ZFS_DELEG_PERM_PROMOTE "promote"
3862 #define ZFS_DELEG_PERM_RENAME "rename"
3863 #define ZFS_DELEG_PERM_MOUNT "mount"
3864 #define ZFS_DELEG_PERM_SHARE "share"
3865 #define ZFS_DELEG_PERM_SEND "send"
3866 #define ZFS_DELEG_PERM_RECEIVE "receive"
3867 #define ZFS_DELEG_PERM_ALLOW "allow"
3868 #define ZFS_DELEG_PERM_USERPROP "userprop"
3869 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
3870 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
3871 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
3872 #define ZFS_DELEG_PERM_USERUSED "userused"
3873 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
3874 #define ZFS_DELEG_PERM_HOLD "hold"
3875 #define ZFS_DELEG_PERM_RELEASE "release"
3876 #define ZFS_DELEG_PERM_DIFF "diff"
3878 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
3880 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
3881 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
3882 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
3883 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
3884 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
3885 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
3886 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
3887 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
3888 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
3889 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
3890 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
3891 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
3892 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
3893 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
3894 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
3895 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
3897 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
3898 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
3899 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
3900 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
3901 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
3902 { NULL, ZFS_DELEG_NOTE_NONE }
3905 /* permission structure */
3906 typedef struct deleg_perm {
3907 zfs_deleg_who_type_t dp_who_type;
3908 const char *dp_name;
3910 boolean_t dp_descend;
3914 typedef struct deleg_perm_node {
3915 deleg_perm_t dpn_perm;
3917 uu_avl_node_t dpn_avl_node;
3918 } deleg_perm_node_t;
3920 typedef struct fs_perm fs_perm_t;
3922 /* permissions set */
3923 typedef struct who_perm {
3924 zfs_deleg_who_type_t who_type;
3925 const char *who_name; /* id */
3926 char who_ug_name[256]; /* user/group name */
3927 fs_perm_t *who_fsperm; /* uplink */
3929 uu_avl_t *who_deleg_perm_avl; /* permissions */
3933 typedef struct who_perm_node {
3934 who_perm_t who_perm;
3935 uu_avl_node_t who_avl_node;
3938 typedef struct fs_perm_set fs_perm_set_t;
3939 /* fs permissions */
3941 const char *fsp_name;
3943 uu_avl_t *fsp_sc_avl; /* sets,create */
3944 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
3946 fs_perm_set_t *fsp_set; /* uplink */
3950 typedef struct fs_perm_node {
3951 fs_perm_t fspn_fsperm;
3954 uu_list_node_t fspn_list_node;
3957 /* top level structure */
3958 struct fs_perm_set {
3959 uu_list_pool_t *fsps_list_pool;
3960 uu_list_t *fsps_list; /* list of fs_perms */
3962 uu_avl_pool_t *fsps_named_set_avl_pool;
3963 uu_avl_pool_t *fsps_who_perm_avl_pool;
3964 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
3967 static inline const char *
3968 deleg_perm_type(zfs_deleg_note_t note)
3974 case ZFS_DELEG_NOTE_GROUPQUOTA:
3975 case ZFS_DELEG_NOTE_GROUPUSED:
3976 case ZFS_DELEG_NOTE_USERPROP:
3977 case ZFS_DELEG_NOTE_USERQUOTA:
3978 case ZFS_DELEG_NOTE_USERUSED:
3980 return (gettext("other"));
3982 return (gettext("subcommand"));
3987 who_type2weight(zfs_deleg_who_type_t who_type)
3991 case ZFS_DELEG_NAMED_SET_SETS:
3992 case ZFS_DELEG_NAMED_SET:
3995 case ZFS_DELEG_CREATE_SETS:
3996 case ZFS_DELEG_CREATE:
3999 case ZFS_DELEG_USER_SETS:
4000 case ZFS_DELEG_USER:
4003 case ZFS_DELEG_GROUP_SETS:
4004 case ZFS_DELEG_GROUP:
4007 case ZFS_DELEG_EVERYONE_SETS:
4008 case ZFS_DELEG_EVERYONE:
4020 who_perm_compare(const void *larg, const void *rarg, void *unused)
4022 const who_perm_node_t *l = larg;
4023 const who_perm_node_t *r = rarg;
4024 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4025 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4026 int lweight = who_type2weight(ltype);
4027 int rweight = who_type2weight(rtype);
4028 int res = lweight - rweight;
4030 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4031 ZFS_MAX_DELEG_NAME-1);
4043 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4045 const deleg_perm_node_t *l = larg;
4046 const deleg_perm_node_t *r = rarg;
4047 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4048 ZFS_MAX_DELEG_NAME-1);
4060 fs_perm_set_init(fs_perm_set_t *fspset)
4062 bzero(fspset, sizeof (fs_perm_set_t));
4064 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4065 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4066 NULL, UU_DEFAULT)) == NULL)
4068 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4069 UU_DEFAULT)) == NULL)
4072 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4073 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4074 who_perm_node_t, who_avl_node), who_perm_compare,
4075 UU_DEFAULT)) == NULL)
4078 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4079 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4080 who_perm_node_t, who_avl_node), who_perm_compare,
4081 UU_DEFAULT)) == NULL)
4084 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4085 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4086 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4091 static inline void fs_perm_fini(fs_perm_t *);
4092 static inline void who_perm_fini(who_perm_t *);
4095 fs_perm_set_fini(fs_perm_set_t *fspset)
4097 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4099 while (node != NULL) {
4100 fs_perm_node_t *next_node =
4101 uu_list_next(fspset->fsps_list, node);
4102 fs_perm_t *fsperm = &node->fspn_fsperm;
4103 fs_perm_fini(fsperm);
4104 uu_list_remove(fspset->fsps_list, node);
4109 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4110 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4111 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4115 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4118 deleg_perm->dp_who_type = type;
4119 deleg_perm->dp_name = name;
4123 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4124 zfs_deleg_who_type_t type, const char *name)
4126 uu_avl_pool_t *pool;
4127 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4129 bzero(who_perm, sizeof (who_perm_t));
4131 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4132 UU_DEFAULT)) == NULL)
4135 who_perm->who_type = type;
4136 who_perm->who_name = name;
4137 who_perm->who_fsperm = fsperm;
4141 who_perm_fini(who_perm_t *who_perm)
4143 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4145 while (node != NULL) {
4146 deleg_perm_node_t *next_node =
4147 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4149 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4154 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4158 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4160 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4161 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4163 bzero(fsperm, sizeof (fs_perm_t));
4165 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4169 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4173 fsperm->fsp_set = fspset;
4174 fsperm->fsp_name = fsname;
4178 fs_perm_fini(fs_perm_t *fsperm)
4180 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4181 while (node != NULL) {
4182 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4184 who_perm_t *who_perm = &node->who_perm;
4185 who_perm_fini(who_perm);
4186 uu_avl_remove(fsperm->fsp_sc_avl, node);
4191 node = uu_avl_first(fsperm->fsp_uge_avl);
4192 while (node != NULL) {
4193 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4195 who_perm_t *who_perm = &node->who_perm;
4196 who_perm_fini(who_perm);
4197 uu_avl_remove(fsperm->fsp_uge_avl, node);
4202 uu_avl_destroy(fsperm->fsp_sc_avl);
4203 uu_avl_destroy(fsperm->fsp_uge_avl);
4207 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4208 zfs_deleg_who_type_t who_type, const char *name, char locality)
4210 uu_avl_index_t idx = 0;
4212 deleg_perm_node_t *found_node = NULL;
4213 deleg_perm_t *deleg_perm = &node->dpn_perm;
4215 deleg_perm_init(deleg_perm, who_type, name);
4217 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4219 uu_avl_insert(avl, node, idx);
4222 deleg_perm = &node->dpn_perm;
4227 case ZFS_DELEG_LOCAL:
4228 deleg_perm->dp_local = B_TRUE;
4230 case ZFS_DELEG_DESCENDENT:
4231 deleg_perm->dp_descend = B_TRUE;
4236 assert(B_FALSE); /* invalid locality */
4241 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4243 nvpair_t *nvp = NULL;
4244 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4245 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4246 zfs_deleg_who_type_t who_type = who_perm->who_type;
4248 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4249 const char *name = nvpair_name(nvp);
4250 data_type_t type = nvpair_type(nvp);
4251 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4252 deleg_perm_node_t *node =
4253 safe_malloc(sizeof (deleg_perm_node_t));
4255 assert(type == DATA_TYPE_BOOLEAN);
4257 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4258 set_deleg_perm_node(avl, node, who_type, name, locality);
4265 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4267 nvpair_t *nvp = NULL;
4268 fs_perm_set_t *fspset = fsperm->fsp_set;
4270 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4271 nvlist_t *nvl2 = NULL;
4272 const char *name = nvpair_name(nvp);
4273 uu_avl_t *avl = NULL;
4274 uu_avl_pool_t *avl_pool;
4275 zfs_deleg_who_type_t perm_type = name[0];
4276 char perm_locality = name[1];
4277 const char *perm_name = name + 3;
4278 boolean_t is_set = B_TRUE;
4279 who_perm_t *who_perm = NULL;
4281 assert('$' == name[2]);
4283 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4286 switch (perm_type) {
4287 case ZFS_DELEG_CREATE:
4288 case ZFS_DELEG_CREATE_SETS:
4289 case ZFS_DELEG_NAMED_SET:
4290 case ZFS_DELEG_NAMED_SET_SETS:
4291 avl_pool = fspset->fsps_named_set_avl_pool;
4292 avl = fsperm->fsp_sc_avl;
4294 case ZFS_DELEG_USER:
4295 case ZFS_DELEG_USER_SETS:
4296 case ZFS_DELEG_GROUP:
4297 case ZFS_DELEG_GROUP_SETS:
4298 case ZFS_DELEG_EVERYONE:
4299 case ZFS_DELEG_EVERYONE_SETS:
4300 avl_pool = fspset->fsps_who_perm_avl_pool;
4301 avl = fsperm->fsp_uge_avl;
4306 who_perm_node_t *found_node = NULL;
4307 who_perm_node_t *node = safe_malloc(
4308 sizeof (who_perm_node_t));
4309 who_perm = &node->who_perm;
4310 uu_avl_index_t idx = 0;
4312 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4313 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4315 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4317 if (avl == fsperm->fsp_uge_avl) {
4319 struct passwd *p = NULL;
4320 struct group *g = NULL;
4321 const char *nice_name = NULL;
4323 switch (perm_type) {
4324 case ZFS_DELEG_USER_SETS:
4325 case ZFS_DELEG_USER:
4326 rid = atoi(perm_name);
4329 nice_name = p->pw_name;
4331 case ZFS_DELEG_GROUP_SETS:
4332 case ZFS_DELEG_GROUP:
4333 rid = atoi(perm_name);
4336 nice_name = g->gr_name;
4340 if (nice_name != NULL)
4342 node->who_perm.who_ug_name,
4346 uu_avl_insert(avl, node, idx);
4349 who_perm = &node->who_perm;
4353 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4360 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4362 nvpair_t *nvp = NULL;
4363 uu_avl_index_t idx = 0;
4365 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4366 nvlist_t *nvl2 = NULL;
4367 const char *fsname = nvpair_name(nvp);
4368 data_type_t type = nvpair_type(nvp);
4369 fs_perm_t *fsperm = NULL;
4370 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4374 fsperm = &node->fspn_fsperm;
4376 assert(DATA_TYPE_NVLIST == type);
4378 uu_list_node_init(node, &node->fspn_list_node,
4379 fspset->fsps_list_pool);
4381 idx = uu_list_numnodes(fspset->fsps_list);
4382 fs_perm_init(fsperm, fspset, fsname);
4384 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4387 (void) parse_fs_perm(fsperm, nvl2);
4389 uu_list_insert(fspset->fsps_list, node, idx);
4395 static inline const char *
4396 deleg_perm_comment(zfs_deleg_note_t note)
4398 const char *str = "";
4403 case ZFS_DELEG_NOTE_ALLOW:
4404 str = gettext("Must also have the permission that is being"
4405 "\n\t\t\t\tallowed");
4407 case ZFS_DELEG_NOTE_CLONE:
4408 str = gettext("Must also have the 'create' ability and 'mount'"
4409 "\n\t\t\t\tability in the origin file system");
4411 case ZFS_DELEG_NOTE_CREATE:
4412 str = gettext("Must also have the 'mount' ability");
4414 case ZFS_DELEG_NOTE_DESTROY:
4415 str = gettext("Must also have the 'mount' ability");
4417 case ZFS_DELEG_NOTE_DIFF:
4418 str = gettext("Allows lookup of paths within a dataset;"
4419 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4420 "\n\t\t\t\tin order to use zfs diff");
4422 case ZFS_DELEG_NOTE_HOLD:
4423 str = gettext("Allows adding a user hold to a snapshot");
4425 case ZFS_DELEG_NOTE_MOUNT:
4426 str = gettext("Allows mount/umount of ZFS datasets");
4428 case ZFS_DELEG_NOTE_PROMOTE:
4429 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4430 " 'promote' ability in the origin file system");
4432 case ZFS_DELEG_NOTE_RECEIVE:
4433 str = gettext("Must also have the 'mount' and 'create'"
4436 case ZFS_DELEG_NOTE_RELEASE:
4437 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4438 "might destroy the snapshot");
4440 case ZFS_DELEG_NOTE_RENAME:
4441 str = gettext("Must also have the 'mount' and 'create'"
4442 "\n\t\t\t\tability in the new parent");
4444 case ZFS_DELEG_NOTE_ROLLBACK:
4447 case ZFS_DELEG_NOTE_SEND:
4450 case ZFS_DELEG_NOTE_SHARE:
4451 str = gettext("Allows sharing file systems over NFS or SMB"
4452 "\n\t\t\t\tprotocols");
4454 case ZFS_DELEG_NOTE_SNAPSHOT:
4458 * case ZFS_DELEG_NOTE_VSCAN:
4459 * str = gettext("");
4463 case ZFS_DELEG_NOTE_GROUPQUOTA:
4464 str = gettext("Allows accessing any groupquota@... property");
4466 case ZFS_DELEG_NOTE_GROUPUSED:
4467 str = gettext("Allows reading any groupused@... property");
4469 case ZFS_DELEG_NOTE_USERPROP:
4470 str = gettext("Allows changing any user property");
4472 case ZFS_DELEG_NOTE_USERQUOTA:
4473 str = gettext("Allows accessing any userquota@... property");
4475 case ZFS_DELEG_NOTE_USERUSED:
4476 str = gettext("Allows reading any userused@... property");
4494 boolean_t recursive; /* unallow only */
4495 boolean_t prt_usage;
4497 boolean_t prt_perms;
4500 const char *dataset;
4504 prop_cmp(const void *a, const void *b)
4506 const char *str1 = *(const char **)a;
4507 const char *str2 = *(const char **)b;
4508 return (strcmp(str1, str2));
4512 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4514 const char *opt_desc[] = {
4515 "-h", gettext("show this help message and exit"),
4516 "-l", gettext("set permission locally"),
4517 "-d", gettext("set permission for descents"),
4518 "-u", gettext("set permission for user"),
4519 "-g", gettext("set permission for group"),
4520 "-e", gettext("set permission for everyone"),
4521 "-c", gettext("set create time permission"),
4522 "-s", gettext("define permission set"),
4524 "-r", gettext("remove permissions recursively"),
4526 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4527 size_t allow_size = unallow_size - 2;
4528 const char *props[ZFS_NUM_PROPS];
4531 FILE *fp = requested ? stdout : stderr;
4532 zprop_desc_t *pdtbl = zfs_prop_get_table();
4533 const char *fmt = gettext("%-16s %-14s\t%s\n");
4535 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4537 (void) fprintf(fp, gettext("Options:\n"));
4538 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4539 const char *opt = opt_desc[i++];
4540 const char *optdsc = opt_desc[i];
4541 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4544 (void) fprintf(fp, gettext("\nThe following permissions are "
4546 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4548 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4549 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4550 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4551 const char *perm_type = deleg_perm_type(perm_note);
4552 const char *perm_comment = deleg_perm_comment(perm_note);
4553 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4556 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4557 zprop_desc_t *pd = &pdtbl[i];
4558 if (pd->pd_visible != B_TRUE)
4561 if (pd->pd_attr == PROP_READONLY)
4564 props[count++] = pd->pd_name;
4566 props[count] = NULL;
4568 qsort(props, count, sizeof (char *), prop_cmp);
4570 for (i = 0; i < count; i++)
4571 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4574 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4576 exit(requested ? 0 : 2);
4579 static inline const char *
4580 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4583 if (un && argc == expected_argc - 1)
4585 else if (argc == expected_argc)
4586 *permsp = argv[argc - 2];
4588 allow_usage(un, B_FALSE,
4589 gettext("wrong number of parameters\n"));
4591 return (argv[argc - 1]);
4595 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4597 int uge_sum = opts->user + opts->group + opts->everyone;
4598 int csuge_sum = opts->create + opts->set + uge_sum;
4599 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4600 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4603 allow_usage(un, B_FALSE,
4604 gettext("-u, -g, and -e are mutually exclusive\n"));
4606 if (opts->prt_usage)
4607 if (argc == 0 && all_sum == 0)
4608 allow_usage(un, B_TRUE, NULL);
4614 allow_usage(un, B_FALSE,
4615 gettext("invalid options combined with -s\n"));
4617 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4618 if (argv[0][0] != '@')
4619 allow_usage(un, B_FALSE,
4620 gettext("invalid set name: missing '@' prefix\n"));
4621 opts->who = argv[0];
4622 } else if (opts->create) {
4623 if (ldcsuge_sum > 1)
4624 allow_usage(un, B_FALSE,
4625 gettext("invalid options combined with -c\n"));
4626 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4627 } else if (opts->everyone) {
4629 allow_usage(un, B_FALSE,
4630 gettext("invalid options combined with -e\n"));
4631 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4632 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4634 opts->everyone = B_TRUE;
4637 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4638 } else if (argc == 1 && !un) {
4639 opts->prt_perms = B_TRUE;
4640 opts->dataset = argv[argc-1];
4642 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4643 opts->who = argv[0];
4646 if (!opts->local && !opts->descend) {
4647 opts->local = B_TRUE;
4648 opts->descend = B_TRUE;
4653 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4654 const char *who, char *perms, nvlist_t *top_nvl)
4657 char ld[2] = { '\0', '\0' };
4658 char who_buf[ZFS_MAXNAMELEN+32];
4661 nvlist_t *base_nvl = NULL;
4662 nvlist_t *set_nvl = NULL;
4665 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4667 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4671 case ZFS_DELEG_NAMED_SET_SETS:
4672 case ZFS_DELEG_NAMED_SET:
4673 set_type = ZFS_DELEG_NAMED_SET_SETS;
4674 base_type = ZFS_DELEG_NAMED_SET;
4675 ld[0] = ZFS_DELEG_NA;
4677 case ZFS_DELEG_CREATE_SETS:
4678 case ZFS_DELEG_CREATE:
4679 set_type = ZFS_DELEG_CREATE_SETS;
4680 base_type = ZFS_DELEG_CREATE;
4681 ld[0] = ZFS_DELEG_NA;
4683 case ZFS_DELEG_USER_SETS:
4684 case ZFS_DELEG_USER:
4685 set_type = ZFS_DELEG_USER_SETS;
4686 base_type = ZFS_DELEG_USER;
4688 ld[0] = ZFS_DELEG_LOCAL;
4690 ld[1] = ZFS_DELEG_DESCENDENT;
4692 case ZFS_DELEG_GROUP_SETS:
4693 case ZFS_DELEG_GROUP:
4694 set_type = ZFS_DELEG_GROUP_SETS;
4695 base_type = ZFS_DELEG_GROUP;
4697 ld[0] = ZFS_DELEG_LOCAL;
4699 ld[1] = ZFS_DELEG_DESCENDENT;
4701 case ZFS_DELEG_EVERYONE_SETS:
4702 case ZFS_DELEG_EVERYONE:
4703 set_type = ZFS_DELEG_EVERYONE_SETS;
4704 base_type = ZFS_DELEG_EVERYONE;
4706 ld[0] = ZFS_DELEG_LOCAL;
4708 ld[1] = ZFS_DELEG_DESCENDENT;
4711 if (perms != NULL) {
4713 char *end = curr + strlen(perms);
4715 while (curr < end) {
4716 char *delim = strchr(curr, ',');
4727 (void) nvlist_add_boolean(nvl, curr);
4733 for (i = 0; i < 2; i++) {
4734 char locality = ld[i];
4738 if (!nvlist_empty(base_nvl)) {
4740 (void) snprintf(who_buf,
4741 sizeof (who_buf), "%c%c$%s",
4742 base_type, locality, who);
4744 (void) snprintf(who_buf,
4745 sizeof (who_buf), "%c%c$",
4746 base_type, locality);
4748 (void) nvlist_add_nvlist(top_nvl, who_buf,
4753 if (!nvlist_empty(set_nvl)) {
4755 (void) snprintf(who_buf,
4756 sizeof (who_buf), "%c%c$%s",
4757 set_type, locality, who);
4759 (void) snprintf(who_buf,
4760 sizeof (who_buf), "%c%c$",
4761 set_type, locality);
4763 (void) nvlist_add_nvlist(top_nvl, who_buf,
4768 for (i = 0; i < 2; i++) {
4769 char locality = ld[i];
4774 (void) snprintf(who_buf, sizeof (who_buf),
4775 "%c%c$%s", base_type, locality, who);
4777 (void) snprintf(who_buf, sizeof (who_buf),
4778 "%c%c$", base_type, locality);
4779 (void) nvlist_add_boolean(top_nvl, who_buf);
4782 (void) snprintf(who_buf, sizeof (who_buf),
4783 "%c%c$%s", set_type, locality, who);
4785 (void) snprintf(who_buf, sizeof (who_buf),
4786 "%c%c$", set_type, locality);
4787 (void) nvlist_add_boolean(top_nvl, who_buf);
4793 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
4795 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
4799 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
4800 opts->descend, opts->who, opts->perms, *nvlp);
4801 } else if (opts->create) {
4802 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
4803 opts->descend, NULL, opts->perms, *nvlp);
4804 } else if (opts->everyone) {
4805 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
4806 opts->descend, NULL, opts->perms, *nvlp);
4808 char *curr = opts->who;
4809 char *end = curr + strlen(curr);
4811 while (curr < end) {
4813 zfs_deleg_who_type_t who_type;
4815 char *delim = strchr(curr, ',');
4818 struct passwd *p = NULL;
4819 struct group *g = NULL;
4827 rid = (uid_t)strtol(curr, &endch, 0);
4829 who_type = ZFS_DELEG_USER;
4838 (void) snprintf(errbuf, 256, gettext(
4839 "invalid user %s"), curr);
4840 allow_usage(un, B_TRUE, errbuf);
4842 } else if (opts->group) {
4843 who_type = ZFS_DELEG_GROUP;
4852 (void) snprintf(errbuf, 256, gettext(
4853 "invalid group %s"), curr);
4854 allow_usage(un, B_TRUE, errbuf);
4857 if (*endch != '\0') {
4864 if (*endch != '\0') {
4871 who_type = ZFS_DELEG_USER;
4873 } else if (g != NULL) {
4874 who_type = ZFS_DELEG_GROUP;
4877 (void) snprintf(errbuf, 256, gettext(
4878 "invalid user/group %s"), curr);
4879 allow_usage(un, B_TRUE, errbuf);
4883 (void) sprintf(id, "%u", rid);
4886 store_allow_perm(who_type, opts->local,
4887 opts->descend, who, opts->perms, *nvlp);
4896 print_set_creat_perms(uu_avl_t *who_avl)
4898 const char *sc_title[] = {
4899 gettext("Permission sets:\n"),
4900 gettext("Create time permissions:\n"),
4903 const char **title_ptr = sc_title;
4904 who_perm_node_t *who_node = NULL;
4905 int prev_weight = -1;
4907 for (who_node = uu_avl_first(who_avl); who_node != NULL;
4908 who_node = uu_avl_next(who_avl, who_node)) {
4909 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4910 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4911 const char *who_name = who_node->who_perm.who_name;
4912 int weight = who_type2weight(who_type);
4913 boolean_t first = B_TRUE;
4914 deleg_perm_node_t *deleg_node;
4916 if (prev_weight != weight) {
4917 (void) printf(*title_ptr++);
4918 prev_weight = weight;
4921 if (who_name == NULL || strnlen(who_name, 1) == 0)
4922 (void) printf("\t");
4924 (void) printf("\t%s ", who_name);
4926 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
4927 deleg_node = uu_avl_next(avl, deleg_node)) {
4930 deleg_node->dpn_perm.dp_name);
4933 (void) printf(",%s",
4934 deleg_node->dpn_perm.dp_name);
4937 (void) printf("\n");
4942 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
4945 who_perm_node_t *who_node = NULL;
4946 boolean_t prt_title = B_TRUE;
4947 uu_avl_walk_t *walk;
4949 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
4952 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
4953 const char *who_name = who_node->who_perm.who_name;
4954 const char *nice_who_name = who_node->who_perm.who_ug_name;
4955 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
4956 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
4958 deleg_perm_node_t *deleg_node;
4959 boolean_t prt_who = B_TRUE;
4961 for (deleg_node = uu_avl_first(avl);
4963 deleg_node = uu_avl_next(avl, deleg_node)) {
4964 if (local != deleg_node->dpn_perm.dp_local ||
4965 descend != deleg_node->dpn_perm.dp_descend)
4969 const char *who = NULL;
4971 prt_title = B_FALSE;
4972 (void) printf(title);
4976 case ZFS_DELEG_USER_SETS:
4977 case ZFS_DELEG_USER:
4978 who = gettext("user");
4980 who_name = nice_who_name;
4982 case ZFS_DELEG_GROUP_SETS:
4983 case ZFS_DELEG_GROUP:
4984 who = gettext("group");
4986 who_name = nice_who_name;
4988 case ZFS_DELEG_EVERYONE_SETS:
4989 case ZFS_DELEG_EVERYONE:
4990 who = gettext("everyone");
4995 if (who_name == NULL)
4996 (void) printf("\t%s", who);
4998 (void) printf("\t%s %s", who, who_name);
5001 (void) printf("%c%s", delim,
5002 deleg_node->dpn_perm.dp_name);
5007 (void) printf("\n");
5010 uu_avl_walk_end(walk);
5014 print_fs_perms(fs_perm_set_t *fspset)
5016 fs_perm_node_t *node = NULL;
5017 char buf[ZFS_MAXNAMELEN+32];
5018 const char *dsname = buf;
5020 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5021 node = uu_list_next(fspset->fsps_list, node)) {
5022 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5023 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5026 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5027 gettext("---- Permissions on %s "),
5028 node->fspn_fsperm.fsp_name);
5029 (void) printf(dsname);
5030 left = 70 - strlen(buf);
5033 (void) printf("\n");
5035 print_set_creat_perms(sc_avl);
5036 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5037 gettext("Local permissions:\n"));
5038 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5039 gettext("Descendent permissions:\n"));
5040 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5041 gettext("Local+Descendent permissions:\n"));
5045 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5047 struct deleg_perms {
5053 set_deleg_perms(zfs_handle_t *zhp, void *data)
5055 struct deleg_perms *perms = (struct deleg_perms *)data;
5056 zfs_type_t zfs_type = zfs_get_type(zhp);
5058 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5061 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5065 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5068 nvlist_t *perm_nvl = NULL;
5069 nvlist_t *update_perm_nvl = NULL;
5072 struct allow_opts opts = { 0 };
5074 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5077 while ((c = getopt(argc, argv, optstr)) != -1) {
5080 opts.local = B_TRUE;
5083 opts.descend = B_TRUE;
5089 opts.group = B_TRUE;
5092 opts.everyone = B_TRUE;
5098 opts.create = B_TRUE;
5101 opts.recursive = B_TRUE;
5104 (void) fprintf(stderr, gettext("missing argument for "
5105 "'%c' option\n"), optopt);
5109 opts.prt_usage = B_TRUE;
5112 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5121 /* check arguments */
5122 parse_allow_args(argc, argv, un, &opts);
5124 /* try to open the dataset */
5125 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5126 ZFS_TYPE_VOLUME)) == NULL) {
5127 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5132 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5135 fs_perm_set_init(&fs_perm_set);
5136 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5137 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5142 print_fs_perms(&fs_perm_set);
5144 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5145 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5148 if (un && opts.recursive) {
5149 struct deleg_perms data = { un, update_perm_nvl };
5150 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5159 nvlist_free(perm_nvl);
5160 if (update_perm_nvl != NULL)
5161 nvlist_free(update_perm_nvl);
5163 fs_perm_set_fini(&fs_perm_set);
5171 zfs_do_allow(int argc, char **argv)
5173 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5177 zfs_do_unallow(int argc, char **argv)
5179 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5183 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5188 boolean_t recursive = B_FALSE;
5189 const char *opts = holding ? "rt" : "r";
5193 while ((c = getopt(argc, argv, opts)) != -1) {
5199 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5208 /* check number of arguments */
5216 if (holding && tag[0] == '.') {
5217 /* tags starting with '.' are reserved for libzfs */
5218 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5222 for (i = 0; i < argc; ++i) {
5224 char parent[ZFS_MAXNAMELEN];
5226 char *path = argv[i];
5228 delim = strchr(path, '@');
5229 if (delim == NULL) {
5230 (void) fprintf(stderr,
5231 gettext("'%s' is not a snapshot\n"), path);
5235 (void) strncpy(parent, path, delim - path);
5236 parent[delim - path] = '\0';
5238 zhp = zfs_open(g_zfs, parent,
5239 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5245 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5248 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5254 return (errors != 0);
5258 * zfs hold [-r] [-t] <tag> <snap> ...
5260 * -r Recursively hold
5262 * Apply a user-hold with the given tag to the list of snapshots.
5265 zfs_do_hold(int argc, char **argv)
5267 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5271 * zfs release [-r] <tag> <snap> ...
5273 * -r Recursively release
5275 * Release a user-hold with the given tag from the list of snapshots.
5278 zfs_do_release(int argc, char **argv)
5280 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5283 typedef struct holds_cbdata {
5284 boolean_t cb_recursive;
5285 const char *cb_snapname;
5287 size_t cb_max_namelen;
5288 size_t cb_max_taglen;
5291 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5292 #define DATETIME_BUF_LEN (32)
5297 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5300 nvpair_t *nvp = NULL;
5301 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5305 for (i = 0; i < 3; i++) {
5306 col = gettext(hdr_cols[i]);
5308 (void) printf("%-*s ", i ? tagwidth : nwidth,
5311 (void) printf("%s\n", col);
5315 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5316 char *zname = nvpair_name(nvp);
5318 nvpair_t *nvp2 = NULL;
5319 (void) nvpair_value_nvlist(nvp, &nvl2);
5320 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5321 char tsbuf[DATETIME_BUF_LEN];
5322 char *tagname = nvpair_name(nvp2);
5326 char sep = scripted ? '\t' : ' ';
5327 size_t sepnum = scripted ? 1 : 2;
5329 (void) nvpair_value_uint64(nvp2, &val);
5331 (void) localtime_r(&time, &t);
5332 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5333 gettext(STRFTIME_FMT_STR), &t);
5335 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5336 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5342 * Generic callback function to list a dataset or snapshot.
5345 holds_callback(zfs_handle_t *zhp, void *data)
5347 holds_cbdata_t *cbp = data;
5348 nvlist_t *top_nvl = *cbp->cb_nvlp;
5349 nvlist_t *nvl = NULL;
5350 nvpair_t *nvp = NULL;
5351 const char *zname = zfs_get_name(zhp);
5352 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5354 if (cbp->cb_recursive) {
5355 const char *snapname;
5356 char *delim = strchr(zname, '@');
5360 snapname = delim + 1;
5361 if (strcmp(cbp->cb_snapname, snapname))
5365 if (zfs_get_holds(zhp, &nvl) != 0)
5368 if (znamelen > cbp->cb_max_namelen)
5369 cbp->cb_max_namelen = znamelen;
5371 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5372 const char *tag = nvpair_name(nvp);
5373 size_t taglen = strnlen(tag, MAXNAMELEN);
5374 if (taglen > cbp->cb_max_taglen)
5375 cbp->cb_max_taglen = taglen;
5378 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5382 * zfs holds [-r] <snap> ...
5384 * -r Recursively hold
5387 zfs_do_holds(int argc, char **argv)
5392 boolean_t scripted = B_FALSE;
5393 boolean_t recursive = B_FALSE;
5394 const char *opts = "rH";
5397 int types = ZFS_TYPE_SNAPSHOT;
5398 holds_cbdata_t cb = { 0 };
5405 while ((c = getopt(argc, argv, opts)) != -1) {
5414 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5421 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5422 flags |= ZFS_ITER_RECURSE;
5428 /* check number of arguments */
5432 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5435 for (i = 0; i < argc; ++i) {
5436 char *snapshot = argv[i];
5438 const char *snapname;
5440 delim = strchr(snapshot, '@');
5441 if (delim == NULL) {
5442 (void) fprintf(stderr,
5443 gettext("'%s' is not a snapshot\n"), snapshot);
5447 snapname = delim + 1;
5449 snapshot[delim - snapshot] = '\0';
5451 cb.cb_recursive = recursive;
5452 cb.cb_snapname = snapname;
5456 * 1. collect holds data, set format options
5458 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5459 holds_callback, &cb);
5465 * 2. print holds data
5467 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5469 if (nvlist_empty(nvl))
5470 (void) printf(gettext("no datasets available\n"));
5474 return (0 != errors);
5477 #define CHECK_SPINNER 30
5478 #define SPINNER_TIME 3 /* seconds */
5479 #define MOUNT_TIME 5 /* seconds */
5482 get_one_dataset(zfs_handle_t *zhp, void *data)
5484 static char *spin[] = { "-", "\\", "|", "/" };
5485 static int spinval = 0;
5486 static int spincheck = 0;
5487 static time_t last_spin_time = (time_t)0;
5488 get_all_cb_t *cbp = data;
5489 zfs_type_t type = zfs_get_type(zhp);
5491 if (cbp->cb_verbose) {
5492 if (--spincheck < 0) {
5493 time_t now = time(NULL);
5494 if (last_spin_time + SPINNER_TIME < now) {
5495 update_progress(spin[spinval++ % 4]);
5496 last_spin_time = now;
5498 spincheck = CHECK_SPINNER;
5503 * Interate over any nested datasets.
5505 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5511 * Skip any datasets whose type does not match.
5513 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5517 libzfs_add_handle(cbp, zhp);
5518 assert(cbp->cb_used <= cbp->cb_alloc);
5524 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5526 get_all_cb_t cb = { 0 };
5527 cb.cb_verbose = verbose;
5528 cb.cb_getone = get_one_dataset;
5531 set_progress_header(gettext("Reading ZFS config"));
5532 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5534 *dslist = cb.cb_handles;
5535 *count = cb.cb_used;
5538 finish_progress(gettext("done."));
5542 * Generic callback for sharing or mounting filesystems. Because the code is so
5543 * similar, we have a common function with an extra parameter to determine which
5544 * mode we are using.
5546 #define OP_SHARE 0x1
5547 #define OP_MOUNT 0x2
5550 * Share or mount a dataset.
5553 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5554 boolean_t explicit, const char *options)
5556 char mountpoint[ZFS_MAXPROPLEN];
5557 char shareopts[ZFS_MAXPROPLEN];
5558 char smbshareopts[ZFS_MAXPROPLEN];
5559 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5561 uint64_t zoned, canmount;
5562 boolean_t shared_nfs, shared_smb;
5564 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5567 * Check to make sure we can mount/share this dataset. If we
5568 * are in the global zone and the filesystem is exported to a
5569 * local zone, or if we are in a local zone and the
5570 * filesystem is not exported, then it is an error.
5572 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5574 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5578 (void) fprintf(stderr, gettext("cannot %s '%s': "
5579 "dataset is exported to a local zone\n"), cmdname,
5583 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5587 (void) fprintf(stderr, gettext("cannot %s '%s': "
5588 "permission denied\n"), cmdname,
5594 * Ignore any filesystems which don't apply to us. This
5595 * includes those with a legacy mountpoint, or those with
5596 * legacy share options.
5598 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5599 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5600 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5601 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5602 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5603 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5605 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5606 strcmp(smbshareopts, "off") == 0) {
5610 (void) fprintf(stderr, gettext("cannot share '%s': "
5611 "legacy share\n"), zfs_get_name(zhp));
5612 (void) fprintf(stderr, gettext("to "
5613 "share this filesystem set "
5614 "sharenfs property on\n"));
5619 * We cannot share or mount legacy filesystems. If the
5620 * shareopts is non-legacy but the mountpoint is legacy, we
5621 * treat it as a legacy share.
5623 if (strcmp(mountpoint, "legacy") == 0) {
5627 (void) fprintf(stderr, gettext("cannot %s '%s': "
5628 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5629 (void) fprintf(stderr, gettext("use %s(8) to "
5630 "%s this filesystem\n"), cmdname, cmdname);
5634 if (strcmp(mountpoint, "none") == 0) {
5638 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5639 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5644 * canmount explicit outcome
5645 * on no pass through
5646 * on yes pass through
5648 * off yes display error, return 1
5649 * noauto no return 0
5650 * noauto yes pass through
5652 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5653 if (canmount == ZFS_CANMOUNT_OFF) {
5657 (void) fprintf(stderr, gettext("cannot %s '%s': "
5658 "'canmount' property is set to 'off'\n"), cmdname,
5661 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5666 * At this point, we have verified that the mountpoint and/or
5667 * shareopts are appropriate for auto management. If the
5668 * filesystem is already mounted or shared, return (failing
5669 * for explicit requests); otherwise mount or share the
5675 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5676 shared_smb = zfs_is_shared_smb(zhp, NULL);
5678 if (shared_nfs && shared_smb ||
5679 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5680 strcmp(smbshareopts, "off") == 0) ||
5681 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5682 strcmp(shareopts, "off") == 0)) {
5686 (void) fprintf(stderr, gettext("cannot share "
5687 "'%s': filesystem already shared\n"),
5692 if (!zfs_is_mounted(zhp, NULL) &&
5693 zfs_mount(zhp, NULL, 0) != 0)
5696 if (protocol == NULL) {
5697 if (zfs_shareall(zhp) != 0)
5699 } else if (strcmp(protocol, "nfs") == 0) {
5700 if (zfs_share_nfs(zhp))
5702 } else if (strcmp(protocol, "smb") == 0) {
5703 if (zfs_share_smb(zhp))
5706 (void) fprintf(stderr, gettext("cannot share "
5707 "'%s': invalid share type '%s' "
5709 zfs_get_name(zhp), protocol);
5716 if (options == NULL)
5717 mnt.mnt_mntopts = "";
5719 mnt.mnt_mntopts = (char *)options;
5721 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5722 zfs_is_mounted(zhp, NULL)) {
5726 (void) fprintf(stderr, gettext("cannot mount "
5727 "'%s': filesystem already mounted\n"),
5732 if (zfs_mount(zhp, options, flags) != 0)
5741 * Reports progress in the form "(current/total)". Not thread-safe.
5744 report_mount_progress(int current, int total)
5746 static time_t last_progress_time = 0;
5747 time_t now = time(NULL);
5750 /* report 1..n instead of 0..n-1 */
5753 /* display header if we're here for the first time */
5755 set_progress_header(gettext("Mounting ZFS filesystems"));
5756 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
5757 /* too soon to report again */
5761 last_progress_time = now;
5763 (void) sprintf(info, "(%d/%d)", current, total);
5765 if (current == total)
5766 finish_progress(info);
5768 update_progress(info);
5772 append_options(char *mntopts, char *newopts)
5774 int len = strlen(mntopts);
5776 /* original length plus new string to append plus 1 for the comma */
5777 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
5778 (void) fprintf(stderr, gettext("the opts argument for "
5779 "'%c' option is too long (more than %d chars)\n"),
5780 "-o", MNT_LINE_MAX);
5785 mntopts[len++] = ',';
5787 (void) strcpy(&mntopts[len], newopts);
5791 share_mount(int op, int argc, char **argv)
5794 boolean_t verbose = B_FALSE;
5796 char *options = NULL;
5800 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
5810 if (*optarg == '\0') {
5811 (void) fprintf(stderr, gettext("empty mount "
5812 "options (-o) specified\n"));
5816 if (options == NULL)
5817 options = safe_malloc(MNT_LINE_MAX + 1);
5819 /* option validation is done later */
5820 append_options(options, optarg);
5824 warnx("no overlay mounts support on FreeBSD, ignoring");
5827 (void) fprintf(stderr, gettext("missing argument for "
5828 "'%c' option\n"), optopt);
5832 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5841 /* check number of arguments */
5843 zfs_handle_t **dslist = NULL;
5844 size_t i, count = 0;
5845 char *protocol = NULL;
5847 if (op == OP_SHARE && argc > 0) {
5848 if (strcmp(argv[0], "nfs") != 0 &&
5849 strcmp(argv[0], "smb") != 0) {
5850 (void) fprintf(stderr, gettext("share type "
5851 "must be 'nfs' or 'smb'\n"));
5860 (void) fprintf(stderr, gettext("too many arguments\n"));
5864 start_progress_timer();
5865 get_all_datasets(&dslist, &count, verbose);
5870 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
5872 for (i = 0; i < count; i++) {
5874 report_mount_progress(i, count);
5876 if (share_mount_one(dslist[i], op, flags, protocol,
5877 B_FALSE, options) != 0)
5879 zfs_close(dslist[i]);
5883 } else if (argc == 0) {
5884 struct mnttab entry;
5886 if ((op == OP_SHARE) || (options != NULL)) {
5887 (void) fprintf(stderr, gettext("missing filesystem "
5888 "argument (specify -a for all)\n"));
5893 * When mount is given no arguments, go through /etc/mnttab and
5894 * display any active ZFS mounts. We hide any snapshots, since
5895 * they are controlled automatically.
5897 rewind(mnttab_file);
5898 while (getmntent(mnttab_file, &entry) == 0) {
5899 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
5900 strchr(entry.mnt_special, '@') != NULL)
5903 (void) printf("%-30s %s\n", entry.mnt_special,
5911 (void) fprintf(stderr,
5912 gettext("too many arguments\n"));
5916 if ((zhp = zfs_open(g_zfs, argv[0],
5917 ZFS_TYPE_FILESYSTEM)) == NULL) {
5920 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
5930 * zfs mount -a [nfs]
5931 * zfs mount filesystem
5933 * Mount all filesystems, or mount the given filesystem.
5936 zfs_do_mount(int argc, char **argv)
5938 return (share_mount(OP_MOUNT, argc, argv));
5942 * zfs share -a [nfs | smb]
5943 * zfs share filesystem
5945 * Share all filesystems, or share the given filesystem.
5948 zfs_do_share(int argc, char **argv)
5950 return (share_mount(OP_SHARE, argc, argv));
5953 typedef struct unshare_unmount_node {
5954 zfs_handle_t *un_zhp;
5956 uu_avl_node_t un_avlnode;
5957 } unshare_unmount_node_t;
5961 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
5963 const unshare_unmount_node_t *l = larg;
5964 const unshare_unmount_node_t *r = rarg;
5966 return (strcmp(l->un_mountp, r->un_mountp));
5970 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
5971 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
5972 * and unmount it appropriately.
5975 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
5979 struct stat64 statbuf;
5980 struct extmnttab entry;
5981 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
5985 * Search for the path in /etc/mnttab. Rather than looking for the
5986 * specific path, which can be fooled by non-standard paths (i.e. ".."
5987 * or "//"), we stat() the path and search for the corresponding
5988 * (major,minor) device pair.
5990 if (stat64(path, &statbuf) != 0) {
5991 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
5992 cmdname, path, strerror(errno));
5995 path_inode = statbuf.st_ino;
5998 * Search for the given (major,minor) pair in the mount table.
6001 rewind(mnttab_file);
6002 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6003 if (entry.mnt_major == major(statbuf.st_dev) &&
6004 entry.mnt_minor == minor(statbuf.st_dev))
6011 if (statfs(path, &sfs) != 0) {
6012 (void) fprintf(stderr, "%s: %s\n", path,
6016 statfs2mnttab(&sfs, &entry);
6020 if (op == OP_SHARE) {
6021 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6022 "currently mounted\n"), cmdname, path);
6025 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6027 if ((ret = umount2(path, flags)) != 0)
6028 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6033 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6034 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6035 "filesystem\n"), cmdname, path);
6039 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6040 ZFS_TYPE_FILESYSTEM)) == NULL)
6044 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6045 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6046 cmdname, path, strerror(errno));
6048 } else if (statbuf.st_ino != path_inode) {
6049 (void) fprintf(stderr, gettext("cannot "
6050 "%s '%s': not a mountpoint\n"), cmdname, path);
6054 if (op == OP_SHARE) {
6055 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6056 char smbshare_prop[ZFS_MAXPROPLEN];
6058 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6059 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6060 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6061 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6063 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6064 strcmp(smbshare_prop, "off") == 0) {
6065 (void) fprintf(stderr, gettext("cannot unshare "
6066 "'%s': legacy share\n"), path);
6068 (void) fprintf(stderr, gettext("use "
6069 "unshare(1M) to unshare this filesystem\n"));
6071 } else if (!zfs_is_shared(zhp)) {
6072 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6073 "not currently shared\n"), path);
6075 ret = zfs_unshareall_bypath(zhp, path);
6078 char mtpt_prop[ZFS_MAXPROPLEN];
6080 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6081 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6084 ret = zfs_unmount(zhp, NULL, flags);
6085 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6086 (void) fprintf(stderr, gettext("cannot unmount "
6087 "'%s': legacy mountpoint\n"),
6089 (void) fprintf(stderr, gettext("use umount(8) "
6090 "to unmount this filesystem\n"));
6092 ret = zfs_unmountall(zhp, flags);
6103 * Generic callback for unsharing or unmounting a filesystem.
6106 unshare_unmount(int op, int argc, char **argv)
6113 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6114 char sharesmb[ZFS_MAXPROPLEN];
6117 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6126 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6137 * We could make use of zfs_for_each() to walk all datasets in
6138 * the system, but this would be very inefficient, especially
6139 * since we would have to linearly search /etc/mnttab for each
6140 * one. Instead, do one pass through /etc/mnttab looking for
6141 * zfs entries and call zfs_unmount() for each one.
6143 * Things get a little tricky if the administrator has created
6144 * mountpoints beneath other ZFS filesystems. In this case, we
6145 * have to unmount the deepest filesystems first. To accomplish
6146 * this, we place all the mountpoints in an AVL tree sorted by
6147 * the special type (dataset name), and walk the result in
6148 * reverse to make sure to get any snapshots first.
6150 struct mnttab entry;
6151 uu_avl_pool_t *pool;
6153 unshare_unmount_node_t *node;
6155 uu_avl_walk_t *walk;
6158 (void) fprintf(stderr, gettext("too many arguments\n"));
6162 if (((pool = uu_avl_pool_create("unmount_pool",
6163 sizeof (unshare_unmount_node_t),
6164 offsetof(unshare_unmount_node_t, un_avlnode),
6165 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6166 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6169 rewind(mnttab_file);
6170 while (getmntent(mnttab_file, &entry) == 0) {
6172 /* ignore non-ZFS entries */
6173 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6176 /* ignore snapshots */
6177 if (strchr(entry.mnt_special, '@') != NULL)
6180 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6181 ZFS_TYPE_FILESYSTEM)) == NULL) {
6188 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6190 sizeof (nfs_mnt_prop),
6191 NULL, NULL, 0, B_FALSE) == 0);
6192 if (strcmp(nfs_mnt_prop, "off") != 0)
6194 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6196 sizeof (nfs_mnt_prop),
6197 NULL, NULL, 0, B_FALSE) == 0);
6198 if (strcmp(nfs_mnt_prop, "off") == 0)
6202 /* Ignore legacy mounts */
6203 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6205 sizeof (nfs_mnt_prop),
6206 NULL, NULL, 0, B_FALSE) == 0);
6207 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6209 /* Ignore canmount=noauto mounts */
6210 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6211 ZFS_CANMOUNT_NOAUTO)
6217 node = safe_malloc(sizeof (unshare_unmount_node_t));
6219 node->un_mountp = safe_strdup(entry.mnt_mountp);
6221 uu_avl_node_init(node, &node->un_avlnode, pool);
6223 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6224 uu_avl_insert(tree, node, idx);
6226 zfs_close(node->un_zhp);
6227 free(node->un_mountp);
6233 * Walk the AVL tree in reverse, unmounting each filesystem and
6234 * removing it from the AVL tree in the process.
6236 if ((walk = uu_avl_walk_start(tree,
6237 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6240 while ((node = uu_avl_walk_next(walk)) != NULL) {
6241 uu_avl_remove(tree, node);
6245 if (zfs_unshareall_bypath(node->un_zhp,
6246 node->un_mountp) != 0)
6251 if (zfs_unmount(node->un_zhp,
6252 node->un_mountp, flags) != 0)
6257 zfs_close(node->un_zhp);
6258 free(node->un_mountp);
6262 uu_avl_walk_end(walk);
6263 uu_avl_destroy(tree);
6264 uu_avl_pool_destroy(pool);
6269 (void) fprintf(stderr,
6270 gettext("missing filesystem argument\n"));
6272 (void) fprintf(stderr,
6273 gettext("too many arguments\n"));
6278 * We have an argument, but it may be a full path or a ZFS
6279 * filesystem. Pass full paths off to unmount_path() (shared by
6280 * manual_unmount), otherwise open the filesystem and pass to
6283 if (argv[0][0] == '/')
6284 return (unshare_unmount_path(op, argv[0],
6287 if ((zhp = zfs_open(g_zfs, argv[0],
6288 ZFS_TYPE_FILESYSTEM)) == NULL)
6291 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6292 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6293 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6294 NULL, 0, B_FALSE) == 0);
6298 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6300 sizeof (nfs_mnt_prop),
6301 NULL, NULL, 0, B_FALSE) == 0);
6302 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6303 sharesmb, sizeof (sharesmb), NULL, NULL,
6306 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6307 strcmp(sharesmb, "off") == 0) {
6308 (void) fprintf(stderr, gettext("cannot "
6309 "unshare '%s': legacy share\n"),
6312 (void) fprintf(stderr, gettext("use "
6313 "unshare(1M) to unshare this "
6317 } else if (!zfs_is_shared(zhp)) {
6318 (void) fprintf(stderr, gettext("cannot "
6319 "unshare '%s': not currently "
6320 "shared\n"), zfs_get_name(zhp));
6322 } else if (zfs_unshareall(zhp) != 0) {
6328 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6329 (void) fprintf(stderr, gettext("cannot "
6330 "unmount '%s': legacy "
6331 "mountpoint\n"), zfs_get_name(zhp));
6332 (void) fprintf(stderr, gettext("use "
6333 "umount(8) to unmount this "
6336 } else if (!zfs_is_mounted(zhp, NULL)) {
6337 (void) fprintf(stderr, gettext("cannot "
6338 "unmount '%s': not currently "
6342 } else if (zfs_unmountall(zhp, flags) != 0) {
6356 * zfs unmount filesystem
6358 * Unmount all filesystems, or a specific ZFS filesystem.
6361 zfs_do_unmount(int argc, char **argv)
6363 return (unshare_unmount(OP_MOUNT, argc, argv));
6368 * zfs unshare filesystem
6370 * Unshare all filesystems, or a specific ZFS filesystem.
6373 zfs_do_unshare(int argc, char **argv)
6375 return (unshare_unmount(OP_SHARE, argc, argv));
6379 * Attach/detach the given dataset to/from the given jail
6383 do_jail(int argc, char **argv, int attach)
6388 /* check number of arguments */
6390 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6394 (void) fprintf(stderr, gettext("too many arguments\n"));
6398 jailid = jail_getid(argv[1]);
6400 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6404 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6408 ret = (zfs_jail(zhp, jailid, attach) != 0);
6415 * zfs jail jailid filesystem
6417 * Attach the given dataset to the given jail
6421 zfs_do_jail(int argc, char **argv)
6424 return (do_jail(argc, argv, 1));
6428 * zfs unjail jailid filesystem
6430 * Detach the given dataset from the given jail
6434 zfs_do_unjail(int argc, char **argv)
6437 return (do_jail(argc, argv, 0));
6441 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6442 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6445 manual_mount(int argc, char **argv)
6448 char mountpoint[ZFS_MAXPROPLEN];
6449 char mntopts[MNT_LINE_MAX] = { '\0' };
6453 char *dataset, *path;
6456 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6459 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6462 flags |= MS_OVERLAY;
6465 flags |= MS_NOMNTTAB;
6468 (void) fprintf(stderr, gettext("missing argument for "
6469 "'%c' option\n"), optopt);
6473 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6475 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6484 /* check that we only have two arguments */
6487 (void) fprintf(stderr, gettext("missing dataset "
6490 (void) fprintf(stderr,
6491 gettext("missing mountpoint argument\n"));
6493 (void) fprintf(stderr, gettext("too many arguments\n"));
6494 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6501 /* try to open the dataset */
6502 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6505 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6506 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6508 /* check for legacy mountpoint and complain appropriately */
6510 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6511 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6512 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6513 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6518 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6519 "mounted using 'mount -t zfs'\n"), dataset);
6520 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6521 "instead.\n"), path);
6522 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6523 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6524 (void) fprintf(stderr, gettext("See zfs(8) for more "
6533 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6534 * unmounts of non-legacy filesystems, as this is the dominant administrative
6538 manual_unmount(int argc, char **argv)
6544 while ((c = getopt(argc, argv, "f")) != -1) {
6550 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6552 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6561 /* check arguments */
6564 (void) fprintf(stderr, gettext("missing path "
6567 (void) fprintf(stderr, gettext("too many arguments\n"));
6568 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6572 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6576 find_command_idx(char *command, int *idx)
6580 for (i = 0; i < NCOMMAND; i++) {
6581 if (command_table[i].name == NULL)
6584 if (strcmp(command, command_table[i].name) == 0) {
6593 zfs_do_diff(int argc, char **argv)
6597 char *tosnap = NULL;
6598 char *fromsnap = NULL;
6603 while ((c = getopt(argc, argv, "FHt")) != -1) {
6606 flags |= ZFS_DIFF_CLASSIFY;
6609 flags |= ZFS_DIFF_PARSEABLE;
6612 flags |= ZFS_DIFF_TIMESTAMP;
6615 (void) fprintf(stderr,
6616 gettext("invalid option '%c'\n"), optopt);
6625 (void) fprintf(stderr,
6626 gettext("must provide at least one snapshot name\n"));
6631 (void) fprintf(stderr, gettext("too many arguments\n"));
6636 tosnap = (argc == 2) ? argv[1] : NULL;
6639 if (*fromsnap != '@')
6640 copy = strdup(fromsnap);
6642 copy = strdup(tosnap);
6646 if (atp = strchr(copy, '@'))
6649 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6655 * Ignore SIGPIPE so that the library can give us
6656 * information on any failure
6658 (void) sigignore(SIGPIPE);
6660 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6668 main(int argc, char **argv)
6675 (void) setlocale(LC_ALL, "");
6676 (void) textdomain(TEXT_DOMAIN);
6680 if ((g_zfs = libzfs_init()) == NULL) {
6681 (void) fprintf(stderr, gettext("internal error: failed to "
6682 "initialize ZFS library\n"));
6686 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
6688 libzfs_print_on_error(g_zfs, B_TRUE);
6690 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
6691 (void) fprintf(stderr, gettext("internal error: unable to "
6692 "open %s\n"), MNTTAB);
6697 * This command also doubles as the /etc/fs mount and unmount program.
6698 * Determine if we should take this behavior based on argv[0].
6700 progname = basename(argv[0]);
6701 if (strcmp(progname, "mount") == 0) {
6702 ret = manual_mount(argc, argv);
6703 } else if (strcmp(progname, "umount") == 0) {
6704 ret = manual_unmount(argc, argv);
6707 * Make sure the user has specified some command.
6710 (void) fprintf(stderr, gettext("missing command\n"));
6717 * The 'umount' command is an alias for 'unmount'
6719 if (strcmp(cmdname, "umount") == 0)
6720 cmdname = "unmount";
6723 * The 'recv' command is an alias for 'receive'
6725 if (strcmp(cmdname, "recv") == 0)
6726 cmdname = "receive";
6731 if (strcmp(cmdname, "-?") == 0)
6735 * Run the appropriate command.
6737 libzfs_mnttab_cache(g_zfs, B_TRUE);
6738 if (find_command_idx(cmdname, &i) == 0) {
6739 current_command = &command_table[i];
6740 ret = command_table[i].func(argc - 1, argv + 1);
6741 } else if (strchr(cmdname, '=') != NULL) {
6742 verify(find_command_idx("set", &i) == 0);
6743 current_command = &command_table[i];
6744 ret = command_table[i].func(argc, argv);
6746 (void) fprintf(stderr, gettext("unrecognized "
6747 "command '%s'\n"), cmdname);
6750 libzfs_mnttab_cache(g_zfs, B_FALSE);
6753 (void) fclose(mnttab_file);
6755 if (ret == 0 && log_history)
6756 (void) zpool_log_history(g_zfs, history_str);
6761 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
6762 * for the purposes of running ::findleaks.
6764 if (getenv("ZFS_ABORT") != NULL) {
6765 (void) printf("dumping core by request\n");