4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * All rights reserved.
29 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
30 * Copyright (c) 2013 Steven Hartland. All rights reserved.
31 * Copyright 2013 Nexenta Systems, Inc. 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>
76 #include "zfs_comutil.h"
78 libzfs_handle_t *g_zfs;
80 static FILE *mnttab_file;
81 static char history_str[HIS_MAX_RECORD_LEN];
82 static boolean_t log_history = B_TRUE;
84 static int zfs_do_clone(int argc, char **argv);
85 static int zfs_do_create(int argc, char **argv);
86 static int zfs_do_destroy(int argc, char **argv);
87 static int zfs_do_get(int argc, char **argv);
88 static int zfs_do_inherit(int argc, char **argv);
89 static int zfs_do_list(int argc, char **argv);
90 static int zfs_do_mount(int argc, char **argv);
91 static int zfs_do_rename(int argc, char **argv);
92 static int zfs_do_rollback(int argc, char **argv);
93 static int zfs_do_set(int argc, char **argv);
94 static int zfs_do_upgrade(int argc, char **argv);
95 static int zfs_do_snapshot(int argc, char **argv);
96 static int zfs_do_unmount(int argc, char **argv);
97 static int zfs_do_share(int argc, char **argv);
98 static int zfs_do_unshare(int argc, char **argv);
99 static int zfs_do_send(int argc, char **argv);
100 static int zfs_do_receive(int argc, char **argv);
101 static int zfs_do_promote(int argc, char **argv);
102 static int zfs_do_userspace(int argc, char **argv);
103 static int zfs_do_allow(int argc, char **argv);
104 static int zfs_do_unallow(int argc, char **argv);
105 static int zfs_do_hold(int argc, char **argv);
106 static int zfs_do_holds(int argc, char **argv);
107 static int zfs_do_release(int argc, char **argv);
108 static int zfs_do_diff(int argc, char **argv);
109 static int zfs_do_jail(int argc, char **argv);
110 static int zfs_do_unjail(int argc, char **argv);
111 static int zfs_do_bookmark(int argc, char **argv);
114 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
119 _umem_debug_init(void)
121 return ("default,verbose"); /* $UMEM_DEBUG setting */
125 _umem_logging_init(void)
127 return ("fail,contents"); /* $UMEM_LOGGING setting */
163 typedef struct zfs_command {
165 int (*func)(int argc, char **argv);
170 * Master command table. Each ZFS command has a name, associated function, and
171 * usage message. The usage messages need to be internationalized, so we have
172 * to have a function to return the usage message based on a command index.
174 * These commands are organized according to how they are displayed in the usage
175 * message. An empty command (one with a NULL name) indicates an empty line in
176 * the generic usage message.
178 static zfs_command_t command_table[] = {
179 { "create", zfs_do_create, HELP_CREATE },
180 { "destroy", zfs_do_destroy, HELP_DESTROY },
182 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
183 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
184 { "clone", zfs_do_clone, HELP_CLONE },
185 { "promote", zfs_do_promote, HELP_PROMOTE },
186 { "rename", zfs_do_rename, HELP_RENAME },
187 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
189 { "list", zfs_do_list, HELP_LIST },
191 { "set", zfs_do_set, HELP_SET },
192 { "get", zfs_do_get, HELP_GET },
193 { "inherit", zfs_do_inherit, HELP_INHERIT },
194 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
195 { "userspace", zfs_do_userspace, HELP_USERSPACE },
196 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
198 { "mount", zfs_do_mount, HELP_MOUNT },
199 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
200 { "share", zfs_do_share, HELP_SHARE },
201 { "unshare", zfs_do_unshare, HELP_UNSHARE },
203 { "send", zfs_do_send, HELP_SEND },
204 { "receive", zfs_do_receive, HELP_RECEIVE },
206 { "allow", zfs_do_allow, HELP_ALLOW },
208 { "unallow", zfs_do_unallow, HELP_UNALLOW },
210 { "hold", zfs_do_hold, HELP_HOLD },
211 { "holds", zfs_do_holds, HELP_HOLDS },
212 { "release", zfs_do_release, HELP_RELEASE },
213 { "diff", zfs_do_diff, HELP_DIFF },
215 { "jail", zfs_do_jail, HELP_JAIL },
216 { "unjail", zfs_do_unjail, HELP_UNJAIL },
219 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
221 zfs_command_t *current_command;
224 get_usage(zfs_help_t idx)
228 return (gettext("\tclone [-p] [-o property=value] ... "
229 "<snapshot> <filesystem|volume>\n"));
231 return (gettext("\tcreate [-pu] [-o property=value] ... "
233 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
234 "-V <size> <volume>\n"));
236 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
237 "\tdestroy [-dnpRrv] "
238 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
239 "\tdestroy <filesystem|volume>#<bookmark>\n"));
241 return (gettext("\tget [-rHp] [-d max] "
242 "[-o \"all\" | field[,...]]\n"
243 "\t [-t type[,...]] [-s source[,...]]\n"
244 "\t <\"all\" | property[,...]> "
245 "[filesystem|volume|snapshot] ...\n"));
247 return (gettext("\tinherit [-rS] <property> "
248 "<filesystem|volume|snapshot> ...\n"));
250 return (gettext("\tupgrade [-v]\n"
251 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
253 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
255 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
257 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
258 "[-s property]...\n\t [-S property]... [-t type[,...]] "
259 "[filesystem|volume|snapshot] ...\n"));
261 return (gettext("\tmount\n"
262 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
264 return (gettext("\tpromote <clone-filesystem>\n"));
266 return (gettext("\treceive|recv [-vnsFu] <filesystem|volume|"
268 "\treceive|recv [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
270 "\treceive|recv -A <filesystem|volume>\n"));
272 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
273 "<filesystem|volume|snapshot>\n"
274 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
275 "\trename -r <snapshot> <snapshot>\n"
276 "\trename -u [-p] <filesystem> <filesystem>"));
278 return (gettext("\trollback [-rRf] <snapshot>\n"));
280 return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] "
282 "\tsend [-Le] [-i snapshot|bookmark] "
283 "<filesystem|volume|snapshot>\n"
284 "\tsend [-nvPe] -t <receive_resume_token>\n"));
286 return (gettext("\tset <property=value> ... "
287 "<filesystem|volume|snapshot> ...\n"));
289 return (gettext("\tshare <-a | filesystem>\n"));
291 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
292 "<filesystem|volume>@<snap> ...\n"));
294 return (gettext("\tunmount|umount [-f] "
295 "<-a | filesystem|mountpoint>\n"));
297 return (gettext("\tunshare "
298 "<-a | filesystem|mountpoint>\n"));
300 return (gettext("\tallow <filesystem|volume>\n"
302 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
303 "\t <filesystem|volume>\n"
304 "\tallow [-ld] -e <perm|@setname>[,...] "
305 "<filesystem|volume>\n"
306 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
307 "\tallow -s @setname <perm|@setname>[,...] "
308 "<filesystem|volume>\n"));
310 return (gettext("\tunallow [-rldug] "
311 "<\"everyone\"|user|group>[,...]\n"
312 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
313 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
314 "<filesystem|volume>\n"
315 "\tunallow [-r] -c [<perm|@setname>[,...]] "
316 "<filesystem|volume>\n"
317 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
318 "<filesystem|volume>\n"));
320 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
322 "\t [-S field] ... [-t type[,...]] "
323 "<filesystem|snapshot>\n"));
324 case HELP_GROUPSPACE:
325 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
327 "\t [-S field] ... [-t type[,...]] "
328 "<filesystem|snapshot>\n"));
330 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
332 return (gettext("\tholds [-r] <snapshot> ...\n"));
334 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
336 return (gettext("\tdiff [-FHt] <snapshot> "
337 "[snapshot|filesystem]\n"));
339 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
349 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
354 * Utility function to guarantee malloc() success.
358 safe_malloc(size_t size)
362 if ((data = calloc(1, size)) == NULL)
369 safe_strdup(char *str)
371 char *dupstr = strdup(str);
380 * Callback routine that will print out information for each of
384 usage_prop_cb(int prop, void *cb)
388 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
390 if (zfs_prop_readonly(prop))
391 (void) fprintf(fp, " NO ");
393 (void) fprintf(fp, "YES ");
395 if (zfs_prop_inheritable(prop))
396 (void) fprintf(fp, " YES ");
398 (void) fprintf(fp, " NO ");
400 if (zfs_prop_values(prop) == NULL)
401 (void) fprintf(fp, "-\n");
403 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
409 * Display usage message. If we're inside a command, display only the usage for
410 * that command. Otherwise, iterate over the entire command table and display
411 * a complete usage message.
414 usage(boolean_t requested)
417 boolean_t show_properties = B_FALSE;
418 FILE *fp = requested ? stdout : stderr;
420 if (current_command == NULL) {
422 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
424 gettext("where 'command' is one of the following:\n\n"));
426 for (i = 0; i < NCOMMAND; i++) {
427 if (command_table[i].name == NULL)
428 (void) fprintf(fp, "\n");
430 (void) fprintf(fp, "%s",
431 get_usage(command_table[i].usage));
434 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
435 "pool/[dataset/]*dataset[@name]\n"));
437 (void) fprintf(fp, gettext("usage:\n"));
438 (void) fprintf(fp, "%s", get_usage(current_command->usage));
441 if (current_command != NULL &&
442 (strcmp(current_command->name, "set") == 0 ||
443 strcmp(current_command->name, "get") == 0 ||
444 strcmp(current_command->name, "inherit") == 0 ||
445 strcmp(current_command->name, "list") == 0))
446 show_properties = B_TRUE;
448 if (show_properties) {
450 gettext("\nThe following properties are supported:\n"));
452 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
453 "PROPERTY", "EDIT", "INHERIT", "VALUES");
455 /* Iterate over all properties */
456 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
459 (void) fprintf(fp, "\t%-15s ", "userused@...");
460 (void) fprintf(fp, " NO NO <size>\n");
461 (void) fprintf(fp, "\t%-15s ", "groupused@...");
462 (void) fprintf(fp, " NO NO <size>\n");
463 (void) fprintf(fp, "\t%-15s ", "userquota@...");
464 (void) fprintf(fp, "YES NO <size> | none\n");
465 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
466 (void) fprintf(fp, "YES NO <size> | none\n");
467 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
468 (void) fprintf(fp, " NO NO <size>\n");
470 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
471 "with standard units such as K, M, G, etc.\n"));
472 (void) fprintf(fp, gettext("\nUser-defined properties can "
473 "be specified by using a name containing a colon (:).\n"));
474 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
475 "properties must be appended with\n"
476 "a user or group specifier of one of these forms:\n"
477 " POSIX name (eg: \"matt\")\n"
478 " POSIX id (eg: \"126829\")\n"
479 " SMB name@domain (eg: \"matt@sun\")\n"
480 " SMB SID (eg: \"S-1-234-567-89\")\n"));
483 gettext("\nFor the property list, run: %s\n"),
486 gettext("\nFor the delegated permission list, run: %s\n"),
487 "zfs allow|unallow");
491 * See comments at end of main().
493 if (getenv("ZFS_ABORT") != NULL) {
494 (void) printf("dumping core by request\n");
498 exit(requested ? 0 : 2);
502 * Take a property=value argument string and add it to the given nvlist.
503 * Modifies the argument inplace.
506 parseprop(nvlist_t *props, char *propname)
508 char *propval, *strval;
510 if ((propval = strchr(propname, '=')) == NULL) {
511 (void) fprintf(stderr, gettext("missing "
512 "'=' for property=value argument\n"));
517 if (nvlist_lookup_string(props, propname, &strval) == 0) {
518 (void) fprintf(stderr, gettext("property '%s' "
519 "specified multiple times\n"), propname);
522 if (nvlist_add_string(props, propname, propval) != 0)
528 parse_depth(char *opt, int *flags)
533 depth = (int)strtol(opt, &tmp, 0);
535 (void) fprintf(stderr,
536 gettext("%s is not an integer\n"), opt);
540 (void) fprintf(stderr,
541 gettext("Depth can not be negative.\n"));
544 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
548 #define PROGRESS_DELAY 2 /* seconds */
550 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";
551 static time_t pt_begin;
552 static char *pt_header = NULL;
553 static boolean_t pt_shown;
556 start_progress_timer(void)
558 pt_begin = time(NULL) + PROGRESS_DELAY;
563 set_progress_header(char *header)
565 assert(pt_header == NULL);
566 pt_header = safe_strdup(header);
568 (void) printf("%s: ", header);
569 (void) fflush(stdout);
574 update_progress(char *update)
576 if (!pt_shown && time(NULL) > pt_begin) {
577 int len = strlen(update);
579 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
581 (void) fflush(stdout);
583 } else if (pt_shown) {
584 int len = strlen(update);
586 (void) printf("%s%*.*s", update, len, len, pt_reverse);
587 (void) fflush(stdout);
592 finish_progress(char *done)
595 (void) printf("%s\n", done);
596 (void) fflush(stdout);
603 * Check if the dataset is mountable and should be automatically mounted.
606 should_auto_mount(zfs_handle_t *zhp)
608 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp)))
610 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON);
614 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
616 * Given an existing dataset, create a writable copy whose initial contents
617 * are the same as the source. The newly created dataset maintains a
618 * dependency on the original; the original cannot be destroyed so long as
621 * The '-p' flag creates all the non-existing ancestors of the target first.
624 zfs_do_clone(int argc, char **argv)
626 zfs_handle_t *zhp = NULL;
627 boolean_t parents = B_FALSE;
632 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
636 while ((c = getopt(argc, argv, "o:p")) != -1) {
639 if (parseprop(props, optarg) != 0)
646 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
655 /* check number of arguments */
657 (void) fprintf(stderr, gettext("missing source dataset "
662 (void) fprintf(stderr, gettext("missing target dataset "
667 (void) fprintf(stderr, gettext("too many arguments\n"));
671 /* open the source dataset */
672 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
675 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
678 * Now create the ancestors of the target dataset. If the
679 * target already exists and '-p' option was used we should not
682 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
685 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
690 ret = zfs_clone(zhp, argv[1], props);
692 /* create the mountpoint if necessary */
696 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
699 * If the user doesn't want the dataset
700 * automatically mounted, then skip the mount/share
703 if (should_auto_mount(clone)) {
704 if ((ret = zfs_mount(clone, NULL, 0)) != 0) {
705 (void) fprintf(stderr, gettext("clone "
706 "successfully created, "
707 "but not mounted\n"));
708 } else if ((ret = zfs_share(clone)) != 0) {
709 (void) fprintf(stderr, gettext("clone "
710 "successfully created, "
711 "but not shared\n"));
732 * zfs create [-pu] [-o prop=value] ... fs
733 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
735 * Create a new dataset. This command can be used to create filesystems
736 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
737 * For volumes, the user must specify a size to be used.
739 * The '-s' flag applies only to volumes, and indicates that we should not try
740 * to set the reservation for this volume. By default we set a reservation
741 * equal to the size for any volume. For pools with SPA_VERSION >=
742 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
744 * The '-p' flag creates all the non-existing ancestors of the target first.
746 * The '-u' flag prevents mounting of newly created file system.
749 zfs_do_create(int argc, char **argv)
751 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
752 zfs_handle_t *zhp = NULL;
755 boolean_t noreserve = B_FALSE;
756 boolean_t bflag = B_FALSE;
757 boolean_t parents = B_FALSE;
758 boolean_t nomount = B_FALSE;
763 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
767 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) {
770 type = ZFS_TYPE_VOLUME;
771 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
772 (void) fprintf(stderr, gettext("bad volume "
773 "size '%s': %s\n"), optarg,
774 libzfs_error_description(g_zfs));
778 if (nvlist_add_uint64(props,
779 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
788 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
789 (void) fprintf(stderr, gettext("bad volume "
790 "block size '%s': %s\n"), optarg,
791 libzfs_error_description(g_zfs));
795 if (nvlist_add_uint64(props,
796 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
801 if (parseprop(props, optarg) != 0)
811 (void) fprintf(stderr, gettext("missing size "
815 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
821 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
822 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
823 "used when creating a volume\n"));
826 if (nomount && type != ZFS_TYPE_FILESYSTEM) {
827 (void) fprintf(stderr, gettext("'-u' can only be "
828 "used when creating a file system\n"));
835 /* check number of arguments */
837 (void) fprintf(stderr, gettext("missing %s argument\n"),
838 zfs_type_to_name(type));
842 (void) fprintf(stderr, gettext("too many arguments\n"));
846 if (type == ZFS_TYPE_VOLUME && !noreserve) {
847 zpool_handle_t *zpool_handle;
848 nvlist_t *real_props;
849 uint64_t spa_version;
851 zfs_prop_t resv_prop;
855 if (p = strchr(argv[0], '/'))
857 zpool_handle = zpool_open(g_zfs, argv[0]);
860 if (zpool_handle == NULL)
862 spa_version = zpool_get_prop_int(zpool_handle,
863 ZPOOL_PROP_VERSION, NULL);
864 if (spa_version >= SPA_VERSION_REFRESERVATION)
865 resv_prop = ZFS_PROP_REFRESERVATION;
867 resv_prop = ZFS_PROP_RESERVATION;
869 (void) snprintf(msg, sizeof (msg),
870 gettext("cannot create '%s'"), argv[0]);
871 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
872 props, 0, NULL, zpool_handle, msg)) == NULL) {
873 zpool_close(zpool_handle);
876 zpool_close(zpool_handle);
878 volsize = zvol_volsize_to_reservation(volsize, real_props);
879 nvlist_free(real_props);
881 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
883 if (nvlist_add_uint64(props,
884 zfs_prop_to_name(resv_prop), volsize) != 0) {
891 if (parents && zfs_name_valid(argv[0], type)) {
893 * Now create the ancestors of target dataset. If the target
894 * already exists and '-p' option was used we should not
897 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
901 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
906 if (zfs_create(g_zfs, argv[0], type, props) != 0)
909 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
915 * Mount and/or share the new filesystem as appropriate. We provide a
916 * verbose error message to let the user know that their filesystem was
917 * in fact created, even if we failed to mount or share it.
918 * If the user doesn't want the dataset automatically mounted,
919 * then skip the mount/share step altogether.
921 if (!nomount && should_auto_mount(zhp)) {
922 if (zfs_mount(zhp, NULL, 0) != 0) {
923 (void) fprintf(stderr, gettext("filesystem "
924 "successfully created, but not mounted\n"));
926 } else if (zfs_share(zhp) != 0) {
927 (void) fprintf(stderr, gettext("filesystem "
928 "successfully created, but not shared\n"));
945 * zfs destroy [-rRf] <fs, vol>
946 * zfs destroy [-rRd] <snap>
948 * -r Recursively destroy all children
949 * -R Recursively destroy all dependents, including clones
950 * -f Force unmounting of any dependents
951 * -d If we can't destroy now, mark for deferred destruction
953 * Destroys the given dataset. By default, it will unmount any filesystems,
954 * and refuse to destroy a dataset that has any dependents. A dependent can
955 * either be a child, or a clone of a child.
957 typedef struct destroy_cbdata {
960 boolean_t cb_recurse;
962 boolean_t cb_doclones;
963 zfs_handle_t *cb_target;
964 boolean_t cb_defer_destroy;
965 boolean_t cb_verbose;
966 boolean_t cb_parsable;
969 nvlist_t *cb_batchedsnaps;
971 /* first snap in contiguous run */
973 /* previous snap in contiguous run */
981 * Check for any dependents based on the '-r' or '-R' flags.
984 destroy_check_dependent(zfs_handle_t *zhp, void *data)
986 destroy_cbdata_t *cbp = data;
987 const char *tname = zfs_get_name(cbp->cb_target);
988 const char *name = zfs_get_name(zhp);
990 if (strncmp(tname, name, strlen(tname)) == 0 &&
991 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
993 * This is a direct descendant, not a clone somewhere else in
1000 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1001 "%s has children\n"),
1002 zfs_get_name(cbp->cb_target),
1003 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1004 (void) fprintf(stderr, gettext("use '-r' to destroy "
1005 "the following datasets:\n"));
1006 cbp->cb_first = B_FALSE;
1007 cbp->cb_error = B_TRUE;
1010 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1013 * This is a clone. We only want to report this if the '-r'
1014 * wasn't specified, or the target is a snapshot.
1016 if (!cbp->cb_recurse &&
1017 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1020 if (cbp->cb_first) {
1021 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1022 "%s has dependent clones\n"),
1023 zfs_get_name(cbp->cb_target),
1024 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1025 (void) fprintf(stderr, gettext("use '-R' to destroy "
1026 "the following datasets:\n"));
1027 cbp->cb_first = B_FALSE;
1028 cbp->cb_error = B_TRUE;
1029 cbp->cb_dryrun = B_TRUE;
1032 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1041 destroy_callback(zfs_handle_t *zhp, void *data)
1043 destroy_cbdata_t *cb = data;
1044 const char *name = zfs_get_name(zhp);
1046 if (cb->cb_verbose) {
1047 if (cb->cb_parsable) {
1048 (void) printf("destroy\t%s\n", name);
1049 } else if (cb->cb_dryrun) {
1050 (void) printf(gettext("would destroy %s\n"),
1053 (void) printf(gettext("will destroy %s\n"),
1059 * Ignore pools (which we've already flagged as an error before getting
1062 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1063 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1067 if (cb->cb_dryrun) {
1073 * We batch up all contiguous snapshots (even of different
1074 * filesystems) and destroy them with one ioctl. We can't
1075 * simply do all snap deletions and then all fs deletions,
1076 * because we must delete a clone before its origin.
1078 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1079 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1081 int error = zfs_destroy_snaps_nvl(g_zfs,
1082 cb->cb_batchedsnaps, B_FALSE);
1083 fnvlist_free(cb->cb_batchedsnaps);
1084 cb->cb_batchedsnaps = fnvlist_alloc();
1087 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1088 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1099 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1101 destroy_cbdata_t *cb = arg;
1102 const char *name = zfs_get_name(zhp);
1105 if (nvlist_exists(cb->cb_nvl, name)) {
1106 if (cb->cb_firstsnap == NULL)
1107 cb->cb_firstsnap = strdup(name);
1108 if (cb->cb_prevsnap != NULL)
1109 free(cb->cb_prevsnap);
1110 /* this snap continues the current range */
1111 cb->cb_prevsnap = strdup(name);
1112 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1114 if (cb->cb_verbose) {
1115 if (cb->cb_parsable) {
1116 (void) printf("destroy\t%s\n", name);
1117 } else if (cb->cb_dryrun) {
1118 (void) printf(gettext("would destroy %s\n"),
1121 (void) printf(gettext("will destroy %s\n"),
1125 } else if (cb->cb_firstsnap != NULL) {
1126 /* end of this range */
1128 err = lzc_snaprange_space(cb->cb_firstsnap,
1129 cb->cb_prevsnap, &used);
1130 cb->cb_snapused += used;
1131 free(cb->cb_firstsnap);
1132 cb->cb_firstsnap = NULL;
1133 free(cb->cb_prevsnap);
1134 cb->cb_prevsnap = NULL;
1141 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1144 assert(cb->cb_firstsnap == NULL);
1145 assert(cb->cb_prevsnap == NULL);
1146 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1147 if (cb->cb_firstsnap != NULL) {
1150 err = lzc_snaprange_space(cb->cb_firstsnap,
1151 cb->cb_prevsnap, &used);
1153 cb->cb_snapused += used;
1154 free(cb->cb_firstsnap);
1155 cb->cb_firstsnap = NULL;
1156 free(cb->cb_prevsnap);
1157 cb->cb_prevsnap = NULL;
1163 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1165 destroy_cbdata_t *cb = arg;
1168 /* Check for clones. */
1169 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1170 cb->cb_target = zhp;
1171 cb->cb_first = B_TRUE;
1172 err = zfs_iter_dependents(zhp, B_TRUE,
1173 destroy_check_dependent, cb);
1177 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1185 gather_snapshots(zfs_handle_t *zhp, void *arg)
1187 destroy_cbdata_t *cb = arg;
1190 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1196 if (cb->cb_verbose) {
1197 err = destroy_print_snapshots(zhp, cb);
1203 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1211 destroy_clones(destroy_cbdata_t *cb)
1214 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1216 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1217 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1220 boolean_t defer = cb->cb_defer_destroy;
1224 * We can't defer destroy non-snapshots, so set it to
1225 * false while destroying the clones.
1227 cb->cb_defer_destroy = B_FALSE;
1228 err = zfs_iter_dependents(zhp, B_FALSE,
1229 destroy_callback, cb);
1230 cb->cb_defer_destroy = defer;
1240 zfs_do_destroy(int argc, char **argv)
1242 destroy_cbdata_t cb = { 0 };
1246 zfs_handle_t *zhp = NULL;
1248 zfs_type_t type = ZFS_TYPE_DATASET;
1251 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1254 cb.cb_verbose = B_TRUE;
1257 cb.cb_verbose = B_TRUE;
1258 cb.cb_parsable = B_TRUE;
1261 cb.cb_dryrun = B_TRUE;
1264 cb.cb_defer_destroy = B_TRUE;
1265 type = ZFS_TYPE_SNAPSHOT;
1268 cb.cb_force = B_TRUE;
1271 cb.cb_recurse = B_TRUE;
1274 cb.cb_recurse = B_TRUE;
1275 cb.cb_doclones = B_TRUE;
1279 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1288 /* check number of arguments */
1290 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1294 (void) fprintf(stderr, gettext("too many arguments\n"));
1298 at = strchr(argv[0], '@');
1299 pound = strchr(argv[0], '#');
1302 /* Build the list of snaps to destroy in cb_nvl. */
1303 cb.cb_nvl = fnvlist_alloc();
1306 zhp = zfs_open(g_zfs, argv[0],
1307 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1311 cb.cb_snapspec = at + 1;
1312 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1318 if (nvlist_empty(cb.cb_nvl)) {
1319 (void) fprintf(stderr, gettext("could not find any "
1320 "snapshots to destroy; check snapshot names.\n"));
1325 if (cb.cb_verbose) {
1327 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1328 if (cb.cb_parsable) {
1329 (void) printf("reclaim\t%llu\n",
1331 } else if (cb.cb_dryrun) {
1332 (void) printf(gettext("would reclaim %s\n"),
1335 (void) printf(gettext("will reclaim %s\n"),
1340 if (!cb.cb_dryrun) {
1341 if (cb.cb_doclones) {
1342 cb.cb_batchedsnaps = fnvlist_alloc();
1343 err = destroy_clones(&cb);
1345 err = zfs_destroy_snaps_nvl(g_zfs,
1346 cb.cb_batchedsnaps, B_FALSE);
1354 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1355 cb.cb_defer_destroy);
1361 } else if (pound != NULL) {
1366 (void) fprintf(stderr,
1367 "dryrun is not supported with bookmark\n");
1371 if (cb.cb_defer_destroy) {
1372 (void) fprintf(stderr,
1373 "defer destroy is not supported with bookmark\n");
1377 if (cb.cb_recurse) {
1378 (void) fprintf(stderr,
1379 "recursive is not supported with bookmark\n");
1383 if (!zfs_bookmark_exists(argv[0])) {
1384 (void) fprintf(stderr, gettext("bookmark '%s' "
1385 "does not exist.\n"), argv[0]);
1389 nvl = fnvlist_alloc();
1390 fnvlist_add_boolean(nvl, argv[0]);
1392 err = lzc_destroy_bookmarks(nvl, NULL);
1394 (void) zfs_standard_error(g_zfs, err,
1395 "cannot destroy bookmark");
1398 nvlist_free(cb.cb_nvl);
1402 /* Open the given dataset */
1403 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1409 * Perform an explicit check for pools before going any further.
1411 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1412 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1413 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1414 "operation does not apply to pools\n"),
1416 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1417 "%s' to destroy all datasets in the pool\n"),
1419 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1420 "to destroy the pool itself\n"), zfs_get_name(zhp));
1426 * Check for any dependents and/or clones.
1428 cb.cb_first = B_TRUE;
1429 if (!cb.cb_doclones &&
1430 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1441 cb.cb_batchedsnaps = fnvlist_alloc();
1442 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1449 * Do the real thing. The callback will close the
1450 * handle regardless of whether it succeeds or not.
1452 err = destroy_callback(zhp, &cb);
1455 err = zfs_destroy_snaps_nvl(g_zfs,
1456 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1463 fnvlist_free(cb.cb_batchedsnaps);
1464 fnvlist_free(cb.cb_nvl);
1471 is_recvd_column(zprop_get_cbdata_t *cbp)
1474 zfs_get_column_t col;
1476 for (i = 0; i < ZFS_GET_NCOLS &&
1477 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1478 if (col == GET_COL_RECVD)
1484 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1485 * < all | property[,property]... > < fs | snap | vol > ...
1487 * -r recurse over any child datasets
1488 * -H scripted mode. Headers are stripped, and fields are separated
1489 * by tabs instead of spaces.
1490 * -o Set of fields to display. One of "name,property,value,
1491 * received,source". Default is "name,property,value,source".
1492 * "all" is an alias for all five.
1493 * -s Set of sources to allow. One of
1494 * "local,default,inherited,received,temporary,none". Default is
1496 * -p Display values in parsable (literal) format.
1498 * Prints properties for the given datasets. The user can control which
1499 * columns to display as well as which property types to allow.
1503 * Invoked to display the properties for a single dataset.
1506 get_callback(zfs_handle_t *zhp, void *data)
1508 char buf[ZFS_MAXPROPLEN];
1509 char rbuf[ZFS_MAXPROPLEN];
1510 zprop_source_t sourcetype;
1511 char source[ZFS_MAXNAMELEN];
1512 zprop_get_cbdata_t *cbp = data;
1513 nvlist_t *user_props = zfs_get_user_props(zhp);
1514 zprop_list_t *pl = cbp->cb_proplist;
1518 boolean_t received = is_recvd_column(cbp);
1520 for (; pl != NULL; pl = pl->pl_next) {
1521 char *recvdval = NULL;
1523 * Skip the special fake placeholder. This will also skip over
1524 * the name property when 'all' is specified.
1526 if (pl->pl_prop == ZFS_PROP_NAME &&
1527 pl == cbp->cb_proplist)
1530 if (pl->pl_prop != ZPROP_INVAL) {
1531 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1532 sizeof (buf), &sourcetype, source,
1534 cbp->cb_literal) != 0) {
1537 if (!zfs_prop_valid_for_type(pl->pl_prop,
1538 ZFS_TYPE_DATASET)) {
1539 (void) fprintf(stderr,
1540 gettext("No such property '%s'\n"),
1541 zfs_prop_to_name(pl->pl_prop));
1544 sourcetype = ZPROP_SRC_NONE;
1545 (void) strlcpy(buf, "-", sizeof (buf));
1548 if (received && (zfs_prop_get_recvd(zhp,
1549 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1550 cbp->cb_literal) == 0))
1553 zprop_print_one_property(zfs_get_name(zhp), cbp,
1554 zfs_prop_to_name(pl->pl_prop),
1555 buf, sourcetype, source, recvdval);
1556 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1557 sourcetype = ZPROP_SRC_LOCAL;
1559 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1560 buf, sizeof (buf), cbp->cb_literal) != 0) {
1561 sourcetype = ZPROP_SRC_NONE;
1562 (void) strlcpy(buf, "-", sizeof (buf));
1565 zprop_print_one_property(zfs_get_name(zhp), cbp,
1566 pl->pl_user_prop, buf, sourcetype, source, NULL);
1567 } else if (zfs_prop_written(pl->pl_user_prop)) {
1568 sourcetype = ZPROP_SRC_LOCAL;
1570 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1571 buf, sizeof (buf), cbp->cb_literal) != 0) {
1572 sourcetype = ZPROP_SRC_NONE;
1573 (void) strlcpy(buf, "-", sizeof (buf));
1576 zprop_print_one_property(zfs_get_name(zhp), cbp,
1577 pl->pl_user_prop, buf, sourcetype, source, NULL);
1579 if (nvlist_lookup_nvlist(user_props,
1580 pl->pl_user_prop, &propval) != 0) {
1583 sourcetype = ZPROP_SRC_NONE;
1586 verify(nvlist_lookup_string(propval,
1587 ZPROP_VALUE, &strval) == 0);
1588 verify(nvlist_lookup_string(propval,
1589 ZPROP_SOURCE, &sourceval) == 0);
1591 if (strcmp(sourceval,
1592 zfs_get_name(zhp)) == 0) {
1593 sourcetype = ZPROP_SRC_LOCAL;
1594 } else if (strcmp(sourceval,
1595 ZPROP_SOURCE_VAL_RECVD) == 0) {
1596 sourcetype = ZPROP_SRC_RECEIVED;
1598 sourcetype = ZPROP_SRC_INHERITED;
1599 (void) strlcpy(source,
1600 sourceval, sizeof (source));
1604 if (received && (zfs_prop_get_recvd(zhp,
1605 pl->pl_user_prop, rbuf, sizeof (rbuf),
1606 cbp->cb_literal) == 0))
1609 zprop_print_one_property(zfs_get_name(zhp), cbp,
1610 pl->pl_user_prop, strval, sourcetype,
1619 zfs_do_get(int argc, char **argv)
1621 zprop_get_cbdata_t cb = { 0 };
1622 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1623 int types = ZFS_TYPE_DATASET;
1624 char *value, *fields;
1627 zprop_list_t fake_name = { 0 };
1630 * Set up default columns and sources.
1632 cb.cb_sources = ZPROP_SRC_ALL;
1633 cb.cb_columns[0] = GET_COL_NAME;
1634 cb.cb_columns[1] = GET_COL_PROPERTY;
1635 cb.cb_columns[2] = GET_COL_VALUE;
1636 cb.cb_columns[3] = GET_COL_SOURCE;
1637 cb.cb_type = ZFS_TYPE_DATASET;
1640 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1643 cb.cb_literal = B_TRUE;
1646 limit = parse_depth(optarg, &flags);
1649 flags |= ZFS_ITER_RECURSE;
1652 cb.cb_scripted = B_TRUE;
1655 (void) fprintf(stderr, gettext("missing argument for "
1656 "'%c' option\n"), optopt);
1661 * Process the set of columns to display. We zero out
1662 * the structure to give us a blank slate.
1664 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1666 while (*optarg != '\0') {
1667 static char *col_subopts[] =
1668 { "name", "property", "value", "received",
1669 "source", "all", NULL };
1671 if (i == ZFS_GET_NCOLS) {
1672 (void) fprintf(stderr, gettext("too "
1673 "many fields given to -o "
1678 switch (getsubopt(&optarg, col_subopts,
1681 cb.cb_columns[i++] = GET_COL_NAME;
1684 cb.cb_columns[i++] = GET_COL_PROPERTY;
1687 cb.cb_columns[i++] = GET_COL_VALUE;
1690 cb.cb_columns[i++] = GET_COL_RECVD;
1691 flags |= ZFS_ITER_RECVD_PROPS;
1694 cb.cb_columns[i++] = GET_COL_SOURCE;
1698 (void) fprintf(stderr,
1699 gettext("\"all\" conflicts "
1700 "with specific fields "
1701 "given to -o option\n"));
1704 cb.cb_columns[0] = GET_COL_NAME;
1705 cb.cb_columns[1] = GET_COL_PROPERTY;
1706 cb.cb_columns[2] = GET_COL_VALUE;
1707 cb.cb_columns[3] = GET_COL_RECVD;
1708 cb.cb_columns[4] = GET_COL_SOURCE;
1709 flags |= ZFS_ITER_RECVD_PROPS;
1713 (void) fprintf(stderr,
1714 gettext("invalid column name "
1715 "'%s'\n"), suboptarg);
1723 while (*optarg != '\0') {
1724 static char *source_subopts[] = {
1725 "local", "default", "inherited",
1726 "received", "temporary", "none",
1729 switch (getsubopt(&optarg, source_subopts,
1732 cb.cb_sources |= ZPROP_SRC_LOCAL;
1735 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1738 cb.cb_sources |= ZPROP_SRC_INHERITED;
1741 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1744 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1747 cb.cb_sources |= ZPROP_SRC_NONE;
1750 (void) fprintf(stderr,
1751 gettext("invalid source "
1752 "'%s'\n"), suboptarg);
1760 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1761 while (*optarg != '\0') {
1762 static char *type_subopts[] = { "filesystem",
1763 "volume", "snapshot", "bookmark",
1766 switch (getsubopt(&optarg, type_subopts,
1769 types |= ZFS_TYPE_FILESYSTEM;
1772 types |= ZFS_TYPE_VOLUME;
1775 types |= ZFS_TYPE_SNAPSHOT;
1778 types |= ZFS_TYPE_BOOKMARK;
1781 types = ZFS_TYPE_DATASET |
1786 (void) fprintf(stderr,
1787 gettext("invalid type '%s'\n"),
1795 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1805 (void) fprintf(stderr, gettext("missing property "
1812 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1820 * As part of zfs_expand_proplist(), we keep track of the maximum column
1821 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1822 * need to know the maximum name length. However, the user likely did
1823 * not specify 'name' as one of the properties to fetch, so we need to
1824 * make sure we always include at least this property for
1825 * print_get_headers() to work properly.
1827 if (cb.cb_proplist != NULL) {
1828 fake_name.pl_prop = ZFS_PROP_NAME;
1829 fake_name.pl_width = strlen(gettext("NAME"));
1830 fake_name.pl_next = cb.cb_proplist;
1831 cb.cb_proplist = &fake_name;
1834 cb.cb_first = B_TRUE;
1836 /* run for each object */
1837 ret = zfs_for_each(argc, argv, flags, types, NULL,
1838 &cb.cb_proplist, limit, get_callback, &cb);
1840 if (cb.cb_proplist == &fake_name)
1841 zprop_free_list(fake_name.pl_next);
1843 zprop_free_list(cb.cb_proplist);
1849 * inherit [-rS] <property> <fs|vol> ...
1851 * -r Recurse over all children
1852 * -S Revert to received value, if any
1854 * For each dataset specified on the command line, inherit the given property
1855 * from its parent. Inheriting a property at the pool level will cause it to
1856 * use the default value. The '-r' flag will recurse over all children, and is
1857 * useful for setting a property on a hierarchy-wide basis, regardless of any
1858 * local modifications for each dataset.
1861 typedef struct inherit_cbdata {
1862 const char *cb_propname;
1863 boolean_t cb_received;
1867 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1869 inherit_cbdata_t *cb = data;
1870 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1873 * If we're doing it recursively, then ignore properties that
1874 * are not valid for this type of dataset.
1876 if (prop != ZPROP_INVAL &&
1877 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1880 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1884 inherit_cb(zfs_handle_t *zhp, void *data)
1886 inherit_cbdata_t *cb = data;
1888 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1892 zfs_do_inherit(int argc, char **argv)
1896 inherit_cbdata_t cb = { 0 };
1900 boolean_t received = B_FALSE;
1903 while ((c = getopt(argc, argv, "rS")) != -1) {
1906 flags |= ZFS_ITER_RECURSE;
1913 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1922 /* check number of arguments */
1924 (void) fprintf(stderr, gettext("missing property argument\n"));
1928 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1936 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1937 if (zfs_prop_readonly(prop)) {
1938 (void) fprintf(stderr, gettext(
1939 "%s property is read-only\n"),
1943 if (!zfs_prop_inheritable(prop) && !received) {
1944 (void) fprintf(stderr, gettext("'%s' property cannot "
1945 "be inherited\n"), propname);
1946 if (prop == ZFS_PROP_QUOTA ||
1947 prop == ZFS_PROP_RESERVATION ||
1948 prop == ZFS_PROP_REFQUOTA ||
1949 prop == ZFS_PROP_REFRESERVATION) {
1950 (void) fprintf(stderr, gettext("use 'zfs set "
1951 "%s=none' to clear\n"), propname);
1952 (void) fprintf(stderr, gettext("use 'zfs "
1953 "inherit -S %s' to revert to received "
1954 "value\n"), propname);
1958 if (received && (prop == ZFS_PROP_VOLSIZE ||
1959 prop == ZFS_PROP_VERSION)) {
1960 (void) fprintf(stderr, gettext("'%s' property cannot "
1961 "be reverted to a received value\n"), propname);
1964 } else if (!zfs_prop_user(propname)) {
1965 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1970 cb.cb_propname = propname;
1971 cb.cb_received = received;
1973 if (flags & ZFS_ITER_RECURSE) {
1974 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1975 NULL, NULL, 0, inherit_recurse_cb, &cb);
1977 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1978 NULL, NULL, 0, inherit_cb, &cb);
1984 typedef struct upgrade_cbdata {
1985 uint64_t cb_numupgraded;
1986 uint64_t cb_numsamegraded;
1987 uint64_t cb_numfailed;
1988 uint64_t cb_version;
1990 boolean_t cb_foundone;
1991 char cb_lastfs[ZFS_MAXNAMELEN];
1995 same_pool(zfs_handle_t *zhp, const char *name)
1997 int len1 = strcspn(name, "/@");
1998 const char *zhname = zfs_get_name(zhp);
1999 int len2 = strcspn(zhname, "/@");
2003 return (strncmp(name, zhname, len1) == 0);
2007 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2009 upgrade_cbdata_t *cb = data;
2010 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2012 /* list if it's old/new */
2013 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2014 (cb->cb_newer && version > ZPL_VERSION)) {
2017 str = gettext("The following filesystems are "
2018 "formatted using a newer software version and\n"
2019 "cannot be accessed on the current system.\n\n");
2021 str = gettext("The following filesystems are "
2022 "out of date, and can be upgraded. After being\n"
2023 "upgraded, these filesystems (and any 'zfs send' "
2024 "streams generated from\n"
2025 "subsequent snapshots) will no longer be "
2026 "accessible by older software versions.\n\n");
2029 if (!cb->cb_foundone) {
2031 (void) printf(gettext("VER FILESYSTEM\n"));
2032 (void) printf(gettext("--- ------------\n"));
2033 cb->cb_foundone = B_TRUE;
2036 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2043 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2045 upgrade_cbdata_t *cb = data;
2046 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2047 int needed_spa_version;
2050 if (zfs_spa_version(zhp, &spa_version) < 0)
2053 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2055 if (needed_spa_version < 0)
2058 if (spa_version < needed_spa_version) {
2060 (void) printf(gettext("%s: can not be "
2061 "upgraded; the pool version needs to first "
2062 "be upgraded\nto version %d\n\n"),
2063 zfs_get_name(zhp), needed_spa_version);
2069 if (version < cb->cb_version) {
2071 (void) snprintf(verstr, sizeof (verstr),
2072 "%llu", cb->cb_version);
2073 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2075 * If they did "zfs upgrade -a", then we could
2076 * be doing ioctls to different pools. We need
2077 * to log this history once to each pool, and bypass
2078 * the normal history logging that happens in main().
2080 (void) zpool_log_history(g_zfs, history_str);
2081 log_history = B_FALSE;
2083 if (zfs_prop_set(zhp, "version", verstr) == 0)
2084 cb->cb_numupgraded++;
2087 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2088 } else if (version > cb->cb_version) {
2089 /* can't downgrade */
2090 (void) printf(gettext("%s: can not be downgraded; "
2091 "it is already at version %u\n"),
2092 zfs_get_name(zhp), version);
2095 cb->cb_numsamegraded++;
2103 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2106 zfs_do_upgrade(int argc, char **argv)
2108 boolean_t all = B_FALSE;
2109 boolean_t showversions = B_FALSE;
2111 upgrade_cbdata_t cb = { 0 };
2113 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2116 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2119 flags |= ZFS_ITER_RECURSE;
2122 showversions = B_TRUE;
2125 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2126 optarg, &cb.cb_version) != 0) {
2127 (void) fprintf(stderr,
2128 gettext("invalid version %s\n"), optarg);
2137 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2146 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2148 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2149 cb.cb_version || argc))
2151 if ((all || argc) && (showversions))
2157 /* Show info on available versions. */
2158 (void) printf(gettext("The following filesystem versions are "
2160 (void) printf(gettext("VER DESCRIPTION\n"));
2161 (void) printf("--- -----------------------------------------"
2162 "---------------\n");
2163 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2164 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2165 (void) printf(gettext(" 3 Case insensitive and filesystem "
2166 "user identifier (FUID)\n"));
2167 (void) printf(gettext(" 4 userquota, groupquota "
2169 (void) printf(gettext(" 5 System attributes\n"));
2170 (void) printf(gettext("\nFor more information on a particular "
2171 "version, including supported releases,\n"));
2172 (void) printf("see the ZFS Administration Guide.\n\n");
2174 } else if (argc || all) {
2175 /* Upgrade filesystems */
2176 if (cb.cb_version == 0)
2177 cb.cb_version = ZPL_VERSION;
2178 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2179 NULL, NULL, 0, upgrade_set_callback, &cb);
2180 (void) printf(gettext("%llu filesystems upgraded\n"),
2182 if (cb.cb_numsamegraded) {
2183 (void) printf(gettext("%llu filesystems already at "
2185 cb.cb_numsamegraded);
2187 if (cb.cb_numfailed != 0)
2190 /* List old-version filesytems */
2192 (void) printf(gettext("This system is currently running "
2193 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2195 flags |= ZFS_ITER_RECURSE;
2196 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2197 NULL, NULL, 0, upgrade_list_callback, &cb);
2199 found = cb.cb_foundone;
2200 cb.cb_foundone = B_FALSE;
2201 cb.cb_newer = B_TRUE;
2203 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2204 NULL, NULL, 0, upgrade_list_callback, &cb);
2206 if (!cb.cb_foundone && !found) {
2207 (void) printf(gettext("All filesystems are "
2208 "formatted with the current version.\n"));
2216 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2217 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2218 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2219 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2221 * -H Scripted mode; elide headers and separate columns by tabs.
2222 * -i Translate SID to POSIX ID.
2223 * -n Print numeric ID instead of user/group name.
2224 * -o Control which fields to display.
2225 * -p Use exact (parsable) numeric output.
2226 * -s Specify sort columns, descending order.
2227 * -S Specify sort columns, ascending order.
2228 * -t Control which object types to display.
2230 * Displays space consumed by, and quotas on, each user in the specified
2231 * filesystem or snapshot.
2234 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2235 enum us_field_types {
2241 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2242 static char *us_field_names[] = { "type", "name", "used", "quota" };
2243 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2245 #define USTYPE_PSX_GRP (1 << 0)
2246 #define USTYPE_PSX_USR (1 << 1)
2247 #define USTYPE_SMB_GRP (1 << 2)
2248 #define USTYPE_SMB_USR (1 << 3)
2249 #define USTYPE_ALL \
2250 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2252 static int us_type_bits[] = {
2259 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2262 typedef struct us_node {
2264 uu_avl_node_t usn_avlnode;
2265 uu_list_node_t usn_listnode;
2268 typedef struct us_cbdata {
2270 uu_avl_pool_t *cb_avl_pool;
2272 boolean_t cb_numname;
2273 boolean_t cb_nicenum;
2274 boolean_t cb_sid2posix;
2275 zfs_userquota_prop_t cb_prop;
2276 zfs_sort_column_t *cb_sortcol;
2277 size_t cb_width[USFIELD_LAST];
2280 static boolean_t us_populated = B_FALSE;
2283 zfs_sort_column_t *si_sortcol;
2284 boolean_t si_numname;
2288 us_field_index(char *field)
2292 for (i = 0; i < USFIELD_LAST; i++) {
2293 if (strcmp(field, us_field_names[i]) == 0)
2301 us_compare(const void *larg, const void *rarg, void *unused)
2303 const us_node_t *l = larg;
2304 const us_node_t *r = rarg;
2305 us_sort_info_t *si = (us_sort_info_t *)unused;
2306 zfs_sort_column_t *sortcol = si->si_sortcol;
2307 boolean_t numname = si->si_numname;
2308 nvlist_t *lnvl = l->usn_nvl;
2309 nvlist_t *rnvl = r->usn_nvl;
2313 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2320 zfs_prop_t prop = sortcol->sc_prop;
2321 const char *propname = NULL;
2322 boolean_t reverse = sortcol->sc_reverse;
2327 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2328 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2330 rc = (rv32 < lv32) ? 1 : -1;
2335 (void) nvlist_lookup_uint64(lnvl, propname,
2337 (void) nvlist_lookup_uint64(rnvl, propname,
2340 rc = (rv64 < lv64) ? 1 : -1;
2342 (void) nvlist_lookup_string(lnvl, propname,
2344 (void) nvlist_lookup_string(rnvl, propname,
2346 rc = strcmp(lvstr, rvstr);
2350 case ZFS_PROP_QUOTA:
2353 if (prop == ZFS_PROP_USED)
2357 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2358 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2360 rc = (rv64 < lv64) ? 1 : -1;
2366 return (reverse ? 1 : -1);
2368 return (reverse ? -1 : 1);
2373 * If entries still seem to be the same, check if they are of the same
2374 * type (smbentity is added only if we are doing SID to POSIX ID
2375 * translation where we can have duplicate type/name combinations).
2377 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2378 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2380 return (lvb < rvb ? -1 : 1);
2385 static inline const char *
2386 us_type2str(unsigned field_type)
2388 switch (field_type) {
2389 case USTYPE_PSX_USR:
2390 return ("POSIX User");
2391 case USTYPE_PSX_GRP:
2392 return ("POSIX Group");
2393 case USTYPE_SMB_USR:
2394 return ("SMB User");
2395 case USTYPE_SMB_GRP:
2396 return ("SMB Group");
2398 return ("Undefined");
2403 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2405 us_cbdata_t *cb = (us_cbdata_t *)arg;
2406 zfs_userquota_prop_t prop = cb->cb_prop;
2411 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2412 uu_avl_t *avl = cb->cb_avl;
2416 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2418 const char *typestr;
2422 int typeidx, nameidx, sizeidx;
2423 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2424 boolean_t smbentity = B_FALSE;
2426 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2428 node = safe_malloc(sizeof (us_node_t));
2429 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2430 node->usn_nvl = props;
2432 if (domain != NULL && domain[0] != '\0') {
2434 char sid[ZFS_MAXNAMELEN + 32];
2438 int flag = IDMAP_REQ_FLG_USE_CACHE;
2443 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2445 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2446 type = USTYPE_SMB_GRP;
2448 err = sid_to_id(sid, B_FALSE, &id);
2451 type = USTYPE_SMB_USR;
2453 err = sid_to_id(sid, B_TRUE, &id);
2460 if (!cb->cb_sid2posix) {
2461 if (type == USTYPE_SMB_USR) {
2462 (void) idmap_getwinnamebyuid(rid, flag,
2465 (void) idmap_getwinnamebygid(rid, flag,
2475 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2477 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2478 type = USTYPE_PSX_GRP;
2479 if (!cb->cb_numname) {
2482 if ((g = getgrgid(rid)) != NULL)
2486 type = USTYPE_PSX_USR;
2487 if (!cb->cb_numname) {
2490 if ((p = getpwuid(rid)) != NULL)
2497 * Make sure that the type/name combination is unique when doing
2498 * SID to POSIX ID translation (hence changing the type from SMB to
2501 if (cb->cb_sid2posix &&
2502 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2505 /* Calculate/update width of TYPE field */
2506 typestr = us_type2str(type);
2507 typelen = strlen(gettext(typestr));
2508 typeidx = us_field_index("type");
2509 if (typelen > cb->cb_width[typeidx])
2510 cb->cb_width[typeidx] = typelen;
2511 if (nvlist_add_uint32(props, "type", type) != 0)
2514 /* Calculate/update width of NAME field */
2515 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2516 if (nvlist_add_uint64(props, "name", rid) != 0)
2518 namelen = snprintf(NULL, 0, "%u", rid);
2520 if (nvlist_add_string(props, "name", name) != 0)
2522 namelen = strlen(name);
2524 nameidx = us_field_index("name");
2525 if (namelen > cb->cb_width[nameidx])
2526 cb->cb_width[nameidx] = namelen;
2529 * Check if this type/name combination is in the list and update it;
2530 * otherwise add new node to the list.
2532 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2533 uu_avl_insert(avl, node, idx);
2538 props = node->usn_nvl;
2541 /* Calculate/update width of USED/QUOTA fields */
2543 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2545 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2546 sizelen = strlen(sizebuf);
2547 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2549 if (!nvlist_exists(props, "quota"))
2550 (void) nvlist_add_uint64(props, "quota", 0);
2553 if (!nvlist_exists(props, "used"))
2554 (void) nvlist_add_uint64(props, "used", 0);
2556 sizeidx = us_field_index(propname);
2557 if (sizelen > cb->cb_width[sizeidx])
2558 cb->cb_width[sizeidx] = sizelen;
2560 if (nvlist_add_uint64(props, propname, space) != 0)
2567 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2568 size_t *width, us_node_t *node)
2570 nvlist_t *nvl = node->usn_nvl;
2571 char valstr[ZFS_MAXNAMELEN];
2572 boolean_t first = B_TRUE;
2578 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2579 if (!(ustype & types))
2582 while ((field = fields[cfield]) != USFIELD_LAST) {
2583 nvpair_t *nvp = NULL;
2587 char *strval = NULL;
2589 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2590 if (strcmp(nvpair_name(nvp),
2591 us_field_names[field]) == 0)
2595 type = nvpair_type(nvp);
2597 case DATA_TYPE_UINT32:
2598 (void) nvpair_value_uint32(nvp, &val32);
2600 case DATA_TYPE_UINT64:
2601 (void) nvpair_value_uint64(nvp, &val64);
2603 case DATA_TYPE_STRING:
2604 (void) nvpair_value_string(nvp, &strval);
2607 (void) fprintf(stderr, "invalid data type\n");
2612 strval = (char *)us_type2str(val32);
2615 if (type == DATA_TYPE_UINT64) {
2616 (void) sprintf(valstr, "%llu", val64);
2622 if (type == DATA_TYPE_UINT64) {
2624 (void) sprintf(valstr, "%llu", val64);
2626 zfs_nicenum(val64, valstr,
2629 if (field == USFIELD_QUOTA &&
2630 strcmp(valstr, "0") == 0)
2640 (void) printf("\t");
2645 (void) printf("%s", strval);
2646 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2647 (void) printf("%-*s", width[field], strval);
2649 (void) printf("%*s", width[field], strval);
2655 (void) printf("\n");
2659 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2660 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2668 boolean_t first = B_TRUE;
2670 while ((field = fields[cfield]) != USFIELD_LAST) {
2671 col = gettext(us_field_hdr[field]);
2672 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2673 (void) printf(first ? "%-*s" : " %-*s",
2676 (void) printf(first ? "%*s" : " %*s",
2682 (void) printf("\n");
2685 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2686 print_us_node(scripted, parsable, fields, types, width, node);
2688 nvlist_free(node->usn_nvl);
2693 zfs_do_userspace(int argc, char **argv)
2696 zfs_userquota_prop_t p;
2698 uu_avl_pool_t *avl_pool;
2700 uu_avl_walk_t *walk;
2702 char deffields[] = "type,name,used,quota";
2703 char *ofield = NULL;
2704 char *tfield = NULL;
2708 boolean_t scripted = B_FALSE;
2709 boolean_t prtnum = B_FALSE;
2710 boolean_t parsable = B_FALSE;
2711 boolean_t sid2posix = B_FALSE;
2714 zfs_sort_column_t *sortcol = NULL;
2715 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2719 uu_list_pool_t *listpool;
2721 uu_avl_index_t idx = 0;
2722 uu_list_index_t idx2 = 0;
2727 if (strcmp(argv[0], "groupspace") == 0)
2728 /* Toggle default group types */
2729 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2731 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2747 if (zfs_add_sort_column(&sortcol, optarg,
2748 c == 's' ? B_FALSE : B_TRUE) != 0) {
2749 (void) fprintf(stderr,
2750 gettext("invalid field '%s'\n"), optarg);
2761 (void) fprintf(stderr, gettext("missing argument for "
2762 "'%c' option\n"), optopt);
2766 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2776 (void) fprintf(stderr, gettext("missing dataset name\n"));
2780 (void) fprintf(stderr, gettext("too many arguments\n"));
2784 /* Use default output fields if not specified using -o */
2788 if ((delim = strchr(ofield, ',')) != NULL)
2790 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2791 (void) fprintf(stderr, gettext("invalid type '%s' "
2792 "for -o option\n"), ofield);
2797 } while (delim != NULL);
2798 fields[cfield] = USFIELD_LAST;
2800 /* Override output types (-t option) */
2801 if (tfield != NULL) {
2805 boolean_t found = B_FALSE;
2807 if ((delim = strchr(tfield, ',')) != NULL)
2809 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2811 if (strcmp(tfield, us_type_names[i]) == 0) {
2813 types |= us_type_bits[i];
2818 (void) fprintf(stderr, gettext("invalid type "
2819 "'%s' for -t option\n"), tfield);
2824 } while (delim != NULL);
2827 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2830 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2831 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2833 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2836 /* Always add default sorting columns */
2837 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2838 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2840 cb.cb_sortcol = sortcol;
2841 cb.cb_numname = prtnum;
2842 cb.cb_nicenum = !parsable;
2843 cb.cb_avl_pool = avl_pool;
2844 cb.cb_avl = avl_tree;
2845 cb.cb_sid2posix = sid2posix;
2847 for (i = 0; i < USFIELD_LAST; i++)
2848 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2850 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2851 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2852 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2853 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2854 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2857 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2862 if ((node = uu_avl_first(avl_tree)) == NULL)
2865 us_populated = B_TRUE;
2867 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2868 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2869 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2870 uu_list_node_init(node, &node->usn_listnode, listpool);
2872 while (node != NULL) {
2874 node = uu_avl_next(avl_tree, node);
2875 uu_avl_remove(avl_tree, rmnode);
2876 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2877 uu_list_insert(list, rmnode, idx2);
2880 for (node = uu_list_first(list); node != NULL;
2881 node = uu_list_next(list, node)) {
2882 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2884 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2885 uu_avl_insert(avl_tree, node, idx);
2888 uu_list_destroy(list);
2889 uu_list_pool_destroy(listpool);
2891 /* Print and free node nvlist memory */
2892 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2895 zfs_free_sort_columns(sortcol);
2897 /* Clean up the AVL tree */
2898 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2901 while ((node = uu_avl_walk_next(walk)) != NULL) {
2902 uu_avl_remove(cb.cb_avl, node);
2906 uu_avl_walk_end(walk);
2907 uu_avl_destroy(avl_tree);
2908 uu_avl_pool_destroy(avl_pool);
2914 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2915 * [-t type[,...]] [filesystem|volume|snapshot] ...
2917 * -H Scripted mode; elide headers and separate columns by tabs.
2918 * -p Display values in parsable (literal) format.
2919 * -r Recurse over all children.
2920 * -d Limit recursion by depth.
2921 * -o Control which fields to display.
2922 * -s Specify sort columns, descending order.
2923 * -S Specify sort columns, ascending order.
2924 * -t Control which object types to display.
2926 * When given no arguments, list all filesystems in the system.
2927 * Otherwise, list the specified datasets, optionally recursing down them if
2928 * '-r' is specified.
2930 typedef struct list_cbdata {
2932 boolean_t cb_literal;
2933 boolean_t cb_scripted;
2934 zprop_list_t *cb_proplist;
2938 * Given a list of columns to display, output appropriate headers for each one.
2941 print_header(list_cbdata_t *cb)
2943 zprop_list_t *pl = cb->cb_proplist;
2944 char headerbuf[ZFS_MAXPROPLEN];
2947 boolean_t first = B_TRUE;
2948 boolean_t right_justify;
2950 for (; pl != NULL; pl = pl->pl_next) {
2957 right_justify = B_FALSE;
2958 if (pl->pl_prop != ZPROP_INVAL) {
2959 header = zfs_prop_column_name(pl->pl_prop);
2960 right_justify = zfs_prop_align_right(pl->pl_prop);
2962 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2963 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2964 headerbuf[i] = '\0';
2968 if (pl->pl_next == NULL && !right_justify)
2969 (void) printf("%s", header);
2970 else if (right_justify)
2971 (void) printf("%*s", pl->pl_width, header);
2973 (void) printf("%-*s", pl->pl_width, header);
2976 (void) printf("\n");
2980 * Given a dataset and a list of fields, print out all the properties according
2981 * to the described layout.
2984 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2986 zprop_list_t *pl = cb->cb_proplist;
2987 boolean_t first = B_TRUE;
2988 char property[ZFS_MAXPROPLEN];
2989 nvlist_t *userprops = zfs_get_user_props(zhp);
2992 boolean_t right_justify;
2994 for (; pl != NULL; pl = pl->pl_next) {
2996 if (cb->cb_scripted)
2997 (void) printf("\t");
3004 if (pl->pl_prop == ZFS_PROP_NAME) {
3005 (void) strlcpy(property, zfs_get_name(zhp),
3008 right_justify = zfs_prop_align_right(pl->pl_prop);
3009 } else if (pl->pl_prop != ZPROP_INVAL) {
3010 if (zfs_prop_get(zhp, pl->pl_prop, property,
3011 sizeof (property), NULL, NULL, 0,
3012 cb->cb_literal) != 0)
3016 right_justify = zfs_prop_align_right(pl->pl_prop);
3017 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3018 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3019 property, sizeof (property), cb->cb_literal) != 0)
3023 right_justify = B_TRUE;
3024 } else if (zfs_prop_written(pl->pl_user_prop)) {
3025 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3026 property, sizeof (property), cb->cb_literal) != 0)
3030 right_justify = B_TRUE;
3032 if (nvlist_lookup_nvlist(userprops,
3033 pl->pl_user_prop, &propval) != 0)
3036 verify(nvlist_lookup_string(propval,
3037 ZPROP_VALUE, &propstr) == 0);
3038 right_justify = B_FALSE;
3042 * If this is being called in scripted mode, or if this is the
3043 * last column and it is left-justified, don't include a width
3046 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3047 (void) printf("%s", propstr);
3048 else if (right_justify)
3049 (void) printf("%*s", pl->pl_width, propstr);
3051 (void) printf("%-*s", pl->pl_width, propstr);
3054 (void) printf("\n");
3058 * Generic callback function to list a dataset or snapshot.
3061 list_callback(zfs_handle_t *zhp, void *data)
3063 list_cbdata_t *cbp = data;
3065 if (cbp->cb_first) {
3066 if (!cbp->cb_scripted)
3068 cbp->cb_first = B_FALSE;
3071 print_dataset(zhp, cbp);
3077 zfs_do_list(int argc, char **argv)
3080 static char default_fields[] =
3081 "name,used,available,referenced,mountpoint";
3082 int types = ZFS_TYPE_DATASET;
3083 boolean_t types_specified = B_FALSE;
3084 char *fields = NULL;
3085 list_cbdata_t cb = { 0 };
3089 zfs_sort_column_t *sortcol = NULL;
3090 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3093 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3099 cb.cb_literal = B_TRUE;
3100 flags |= ZFS_ITER_LITERAL_PROPS;
3103 limit = parse_depth(optarg, &flags);
3106 flags |= ZFS_ITER_RECURSE;
3109 cb.cb_scripted = B_TRUE;
3112 if (zfs_add_sort_column(&sortcol, optarg,
3114 (void) fprintf(stderr,
3115 gettext("invalid property '%s'\n"), optarg);
3120 if (zfs_add_sort_column(&sortcol, optarg,
3122 (void) fprintf(stderr,
3123 gettext("invalid property '%s'\n"), optarg);
3129 types_specified = B_TRUE;
3130 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3131 while (*optarg != '\0') {
3132 static char *type_subopts[] = { "filesystem",
3133 "volume", "snapshot", "snap", "bookmark",
3136 switch (getsubopt(&optarg, type_subopts,
3139 types |= ZFS_TYPE_FILESYSTEM;
3142 types |= ZFS_TYPE_VOLUME;
3146 types |= ZFS_TYPE_SNAPSHOT;
3149 types |= ZFS_TYPE_BOOKMARK;
3152 types = ZFS_TYPE_DATASET |
3156 (void) fprintf(stderr,
3157 gettext("invalid type '%s'\n"),
3164 (void) fprintf(stderr, gettext("missing argument for "
3165 "'%c' option\n"), optopt);
3169 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3179 fields = default_fields;
3182 * If we are only going to list snapshot names and sort by name,
3183 * then we can use faster version.
3185 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3186 flags |= ZFS_ITER_SIMPLE;
3189 * If "-o space" and no types were specified, don't display snapshots.
3191 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3192 types &= ~ZFS_TYPE_SNAPSHOT;
3195 * If the user specifies '-o all', the zprop_get_list() doesn't
3196 * normally include the name of the dataset. For 'zfs list', we always
3197 * want this property to be first.
3199 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3203 cb.cb_first = B_TRUE;
3205 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3206 limit, list_callback, &cb);
3208 zprop_free_list(cb.cb_proplist);
3209 zfs_free_sort_columns(sortcol);
3211 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3212 (void) printf(gettext("no datasets available\n"));
3218 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3219 * zfs rename [-f] -p <fs | vol> <fs | vol>
3220 * zfs rename -r <snap> <snap>
3221 * zfs rename -u [-p] <fs> <fs>
3223 * Renames the given dataset to another of the same type.
3225 * The '-p' flag creates all the non-existing ancestors of the target first.
3229 zfs_do_rename(int argc, char **argv)
3232 renameflags_t flags = { 0 };
3236 boolean_t parents = B_FALSE;
3237 char *snapshot = NULL;
3240 while ((c = getopt(argc, argv, "fpru")) != -1) {
3246 flags.recurse = B_TRUE;
3249 flags.nounmount = B_TRUE;
3252 flags.forceunmount = B_TRUE;
3256 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3265 /* check number of arguments */
3267 (void) fprintf(stderr, gettext("missing source dataset "
3272 (void) fprintf(stderr, gettext("missing target dataset "
3277 (void) fprintf(stderr, gettext("too many arguments\n"));
3281 if (flags.recurse && parents) {
3282 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3287 if (flags.recurse && strchr(argv[0], '@') == 0) {
3288 (void) fprintf(stderr, gettext("source dataset for recursive "
3289 "rename must be a snapshot\n"));
3293 if (flags.nounmount && parents) {
3294 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3299 if (flags.nounmount)
3300 types = ZFS_TYPE_FILESYSTEM;
3302 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3304 types = ZFS_TYPE_DATASET;
3306 if (flags.recurse) {
3308 * When we do recursive rename we are fine when the given
3309 * snapshot for the given dataset doesn't exist - it can
3310 * still exists below.
3313 snapshot = strchr(argv[0], '@');
3314 assert(snapshot != NULL);
3319 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3322 /* If we were asked and the name looks good, try to create ancestors. */
3323 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3324 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3329 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3338 * Promotes the given clone fs to be the parent
3342 zfs_do_promote(int argc, char **argv)
3348 if (argc > 1 && argv[1][0] == '-') {
3349 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3354 /* check number of arguments */
3356 (void) fprintf(stderr, gettext("missing clone filesystem"
3361 (void) fprintf(stderr, gettext("too many arguments\n"));
3365 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3369 ret = (zfs_promote(zhp) != 0);
3377 * zfs rollback [-rRf] <snapshot>
3379 * -r Delete any intervening snapshots before doing rollback
3380 * -R Delete any snapshots and their clones
3381 * -f ignored for backwards compatability
3383 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3384 * since then and making it the active dataset. If more recent snapshots exist,
3385 * the command will complain unless the '-r' flag is given.
3387 typedef struct rollback_cbdata {
3393 boolean_t cb_recurse;
3394 } rollback_cbdata_t;
3397 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3399 rollback_cbdata_t *cbp = data;
3401 if (cbp->cb_first && cbp->cb_recurse) {
3402 (void) fprintf(stderr, gettext("cannot rollback to "
3403 "'%s': clones of previous snapshots exist\n"),
3405 (void) fprintf(stderr, gettext("use '-R' to "
3406 "force deletion of the following clones and "
3412 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3419 * Report any snapshots more recent than the one specified. Used when '-r' is
3420 * not specified. We reuse this same callback for the snapshot dependents - if
3421 * 'cb_dependent' is set, then this is a dependent and we should report it
3422 * without checking the transaction group.
3425 rollback_check(zfs_handle_t *zhp, void *data)
3427 rollback_cbdata_t *cbp = data;
3429 if (cbp->cb_doclones) {
3434 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3435 if (cbp->cb_first && !cbp->cb_recurse) {
3436 (void) fprintf(stderr, gettext("cannot "
3437 "rollback to '%s': more recent snapshots "
3438 "or bookmarks exist\n"),
3440 (void) fprintf(stderr, gettext("use '-r' to "
3441 "force deletion of the following "
3442 "snapshots and bookmarks:\n"));
3447 if (cbp->cb_recurse) {
3448 if (zfs_iter_dependents(zhp, B_TRUE,
3449 rollback_check_dependent, cbp) != 0) {
3454 (void) fprintf(stderr, "%s\n",
3463 zfs_do_rollback(int argc, char **argv)
3467 boolean_t force = B_FALSE;
3468 rollback_cbdata_t cb = { 0 };
3469 zfs_handle_t *zhp, *snap;
3470 char parentname[ZFS_MAXNAMELEN];
3474 while ((c = getopt(argc, argv, "rRf")) != -1) {
3487 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3496 /* check number of arguments */
3498 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3502 (void) fprintf(stderr, gettext("too many arguments\n"));
3506 /* open the snapshot */
3507 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3510 /* open the parent dataset */
3511 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3512 verify((delim = strrchr(parentname, '@')) != NULL);
3514 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3520 * Check for more recent snapshots and/or clones based on the presence
3523 cb.cb_target = argv[0];
3524 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3525 cb.cb_first = B_TRUE;
3527 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3529 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3532 if ((ret = cb.cb_error) != 0)
3536 * Rollback parent to the given snapshot.
3538 ret = zfs_rollback(zhp, snap, force);
3551 * zfs set property=value ... { fs | snap | vol } ...
3553 * Sets the given properties for all datasets specified on the command line.
3557 set_callback(zfs_handle_t *zhp, void *data)
3559 nvlist_t *props = data;
3561 if (zfs_prop_set_list(zhp, props) != 0) {
3562 switch (libzfs_errno(g_zfs)) {
3563 case EZFS_MOUNTFAILED:
3564 (void) fprintf(stderr, gettext("property may be set "
3565 "but unable to remount filesystem\n"));
3567 case EZFS_SHARENFSFAILED:
3568 (void) fprintf(stderr, gettext("property may be set "
3569 "but unable to reshare filesystem\n"));
3578 zfs_do_set(int argc, char **argv)
3580 nvlist_t *props = NULL;
3581 int ds_start = -1; /* argv idx of first dataset arg */
3584 /* check for options */
3585 if (argc > 1 && argv[1][0] == '-') {
3586 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3591 /* check number of arguments */
3593 (void) fprintf(stderr, gettext("missing arguments\n"));
3597 if (strchr(argv[1], '=') == NULL) {
3598 (void) fprintf(stderr, gettext("missing property=value "
3601 (void) fprintf(stderr, gettext("missing dataset "
3607 /* validate argument order: prop=val args followed by dataset args */
3608 for (i = 1; i < argc; i++) {
3609 if (strchr(argv[i], '=') != NULL) {
3611 /* out-of-order prop=val argument */
3612 (void) fprintf(stderr, gettext("invalid "
3613 "argument order\n"), i);
3616 } else if (ds_start < 0) {
3621 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3625 /* Populate a list of property settings */
3626 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3628 for (i = 1; i < ds_start; i++) {
3629 if ((ret = parseprop(props, argv[i])) != 0)
3633 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3634 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3641 typedef struct snap_cbdata {
3643 boolean_t sd_recursive;
3644 const char *sd_snapname;
3648 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3650 snap_cbdata_t *sd = arg;
3655 if (sd->sd_recursive &&
3656 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3661 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3664 fnvlist_add_boolean(sd->sd_nvl, name);
3667 if (sd->sd_recursive)
3668 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3674 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3676 * Creates a snapshot with the given name. While functionally equivalent to
3677 * 'zfs create', it is a separate command to differentiate intent.
3680 zfs_do_snapshot(int argc, char **argv)
3685 snap_cbdata_t sd = { 0 };
3686 boolean_t multiple_snaps = B_FALSE;
3688 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3690 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3694 while ((c = getopt(argc, argv, "ro:")) != -1) {
3697 if (parseprop(props, optarg) != 0)
3701 sd.sd_recursive = B_TRUE;
3702 multiple_snaps = B_TRUE;
3705 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3714 /* check number of arguments */
3716 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3721 multiple_snaps = B_TRUE;
3722 for (; argc > 0; argc--, argv++) {
3726 atp = strchr(argv[0], '@');
3730 sd.sd_snapname = atp + 1;
3731 zhp = zfs_open(g_zfs, argv[0],
3732 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3735 if (zfs_snapshot_cb(zhp, &sd) != 0)
3739 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3740 nvlist_free(sd.sd_nvl);
3742 if (ret != 0 && multiple_snaps)
3743 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3747 nvlist_free(sd.sd_nvl);
3754 * Send a backup stream to stdout.
3757 zfs_do_send(int argc, char **argv)
3759 char *fromname = NULL;
3760 char *toname = NULL;
3761 char *resume_token = NULL;
3764 sendflags_t flags = { 0 };
3766 nvlist_t *dbgnv = NULL;
3767 boolean_t extraverbose = B_FALSE;
3770 while ((c = getopt(argc, argv, ":i:I:RDpvnPLet:")) != -1) {
3781 flags.doall = B_TRUE;
3784 flags.replicate = B_TRUE;
3787 flags.props = B_TRUE;
3790 flags.parsable = B_TRUE;
3791 flags.verbose = B_TRUE;
3795 extraverbose = B_TRUE;
3796 flags.verbose = B_TRUE;
3797 flags.progress = B_TRUE;
3800 flags.dedup = B_TRUE;
3803 flags.dryrun = B_TRUE;
3806 flags.largeblock = B_TRUE;
3809 flags.embed_data = B_TRUE;
3812 resume_token = optarg;
3815 (void) fprintf(stderr, gettext("missing argument for "
3816 "'%c' option\n"), optopt);
3820 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3829 if (resume_token != NULL) {
3830 if (fromname != NULL || flags.replicate || flags.props ||
3832 (void) fprintf(stderr,
3833 gettext("invalid flags combined with -t\n"));
3837 (void) fprintf(stderr, gettext("no additional "
3838 "arguments are permitted with -t\n"));
3843 (void) fprintf(stderr,
3844 gettext("missing snapshot argument\n"));
3848 (void) fprintf(stderr, gettext("too many arguments\n"));
3853 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3854 (void) fprintf(stderr,
3855 gettext("Error: Stream can not be written to a terminal.\n"
3856 "You must redirect standard output.\n"));
3860 if (resume_token != NULL) {
3861 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3866 * Special case sending a filesystem, or from a bookmark.
3868 if (strchr(argv[0], '@') == NULL ||
3869 (fromname && strchr(fromname, '#') != NULL)) {
3870 char frombuf[ZFS_MAXNAMELEN];
3871 enum lzc_send_flags lzc_flags = 0;
3873 if (flags.replicate || flags.doall || flags.props ||
3874 flags.dedup || flags.dryrun || flags.verbose ||
3876 (void) fprintf(stderr,
3878 "Unsupported flag with filesystem or bookmark.\n"));
3882 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3886 if (flags.largeblock)
3887 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3888 if (flags.embed_data)
3889 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3891 if (fromname != NULL &&
3892 (fromname[0] == '#' || fromname[0] == '@')) {
3894 * Incremental source name begins with # or @.
3895 * Default to same fs as target.
3897 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3898 cp = strchr(frombuf, '@');
3901 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3904 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3909 cp = strchr(argv[0], '@');
3912 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3917 * If they specified the full path to the snapshot, chop off
3918 * everything except the short name of the snapshot, but special
3919 * case if they specify the origin.
3921 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3922 char origin[ZFS_MAXNAMELEN];
3925 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3926 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3928 if (strcmp(origin, fromname) == 0) {
3930 flags.fromorigin = B_TRUE;
3933 if (cp != fromname && strcmp(argv[0], fromname)) {
3934 (void) fprintf(stderr,
3935 gettext("incremental source must be "
3936 "in same filesystem\n"));
3940 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3941 (void) fprintf(stderr,
3942 gettext("invalid incremental source\n"));
3948 if (flags.replicate && fromname == NULL)
3949 flags.doall = B_TRUE;
3951 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3952 extraverbose ? &dbgnv : NULL);
3954 if (extraverbose && dbgnv != NULL) {
3956 * dump_nvlist prints to stdout, but that's been
3957 * redirected to a file. Make it print to stderr
3960 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3961 dump_nvlist(dbgnv, 0);
3970 * Restore a backup stream from stdin.
3973 zfs_do_receive(int argc, char **argv)
3976 recvflags_t flags = { 0 };
3977 boolean_t abort_resumable = B_FALSE;
3980 nvpair_t *nvp = NULL;
3982 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3986 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
3989 if (parseprop(props, optarg) != 0)
3993 flags.isprefix = B_TRUE;
3996 flags.isprefix = B_TRUE;
3997 flags.istail = B_TRUE;
4000 flags.dryrun = B_TRUE;
4003 flags.nomount = B_TRUE;
4006 flags.verbose = B_TRUE;
4009 flags.resumable = B_TRUE;
4012 flags.force = B_TRUE;
4015 abort_resumable = B_TRUE;
4018 (void) fprintf(stderr, gettext("missing argument for "
4019 "'%c' option\n"), optopt);
4023 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4032 /* check number of arguments */
4034 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4038 (void) fprintf(stderr, gettext("too many arguments\n"));
4042 while ((nvp = nvlist_next_nvpair(props, nvp))) {
4043 if (strcmp(nvpair_name(nvp), "origin") != 0) {
4044 (void) fprintf(stderr, gettext("invalid option"));
4049 if (abort_resumable) {
4050 if (flags.isprefix || flags.istail || flags.dryrun ||
4051 flags.resumable || flags.nomount) {
4052 (void) fprintf(stderr, gettext("invalid option"));
4056 char namebuf[ZFS_MAXNAMELEN];
4057 (void) snprintf(namebuf, sizeof (namebuf),
4058 "%s/%%recv", argv[0]);
4060 if (zfs_dataset_exists(g_zfs, namebuf,
4061 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4062 zfs_handle_t *zhp = zfs_open(g_zfs,
4063 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4066 err = zfs_destroy(zhp, B_FALSE);
4068 zfs_handle_t *zhp = zfs_open(g_zfs,
4069 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4072 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4073 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4074 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4075 (void) fprintf(stderr,
4076 gettext("'%s' does not have any "
4077 "resumable receive state to abort\n"),
4081 err = zfs_destroy(zhp, B_FALSE);
4087 if (isatty(STDIN_FILENO)) {
4088 (void) fprintf(stderr,
4089 gettext("Error: Backup stream can not be read "
4090 "from a terminal.\n"
4091 "You must redirect standard input.\n"));
4094 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4100 * allow/unallow stuff
4102 /* copied from zfs/sys/dsl_deleg.h */
4103 #define ZFS_DELEG_PERM_CREATE "create"
4104 #define ZFS_DELEG_PERM_DESTROY "destroy"
4105 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4106 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4107 #define ZFS_DELEG_PERM_CLONE "clone"
4108 #define ZFS_DELEG_PERM_PROMOTE "promote"
4109 #define ZFS_DELEG_PERM_RENAME "rename"
4110 #define ZFS_DELEG_PERM_MOUNT "mount"
4111 #define ZFS_DELEG_PERM_SHARE "share"
4112 #define ZFS_DELEG_PERM_SEND "send"
4113 #define ZFS_DELEG_PERM_RECEIVE "receive"
4114 #define ZFS_DELEG_PERM_ALLOW "allow"
4115 #define ZFS_DELEG_PERM_USERPROP "userprop"
4116 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4117 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4118 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4119 #define ZFS_DELEG_PERM_USERUSED "userused"
4120 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4121 #define ZFS_DELEG_PERM_HOLD "hold"
4122 #define ZFS_DELEG_PERM_RELEASE "release"
4123 #define ZFS_DELEG_PERM_DIFF "diff"
4124 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4126 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4128 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4129 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4130 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4131 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4132 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4133 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4134 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4135 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4136 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4137 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4138 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4139 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4140 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4141 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4142 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4143 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4144 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4146 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4147 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4148 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4149 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4150 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4151 { NULL, ZFS_DELEG_NOTE_NONE }
4154 /* permission structure */
4155 typedef struct deleg_perm {
4156 zfs_deleg_who_type_t dp_who_type;
4157 const char *dp_name;
4159 boolean_t dp_descend;
4163 typedef struct deleg_perm_node {
4164 deleg_perm_t dpn_perm;
4166 uu_avl_node_t dpn_avl_node;
4167 } deleg_perm_node_t;
4169 typedef struct fs_perm fs_perm_t;
4171 /* permissions set */
4172 typedef struct who_perm {
4173 zfs_deleg_who_type_t who_type;
4174 const char *who_name; /* id */
4175 char who_ug_name[256]; /* user/group name */
4176 fs_perm_t *who_fsperm; /* uplink */
4178 uu_avl_t *who_deleg_perm_avl; /* permissions */
4182 typedef struct who_perm_node {
4183 who_perm_t who_perm;
4184 uu_avl_node_t who_avl_node;
4187 typedef struct fs_perm_set fs_perm_set_t;
4188 /* fs permissions */
4190 const char *fsp_name;
4192 uu_avl_t *fsp_sc_avl; /* sets,create */
4193 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4195 fs_perm_set_t *fsp_set; /* uplink */
4199 typedef struct fs_perm_node {
4200 fs_perm_t fspn_fsperm;
4203 uu_list_node_t fspn_list_node;
4206 /* top level structure */
4207 struct fs_perm_set {
4208 uu_list_pool_t *fsps_list_pool;
4209 uu_list_t *fsps_list; /* list of fs_perms */
4211 uu_avl_pool_t *fsps_named_set_avl_pool;
4212 uu_avl_pool_t *fsps_who_perm_avl_pool;
4213 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4216 static inline const char *
4217 deleg_perm_type(zfs_deleg_note_t note)
4223 case ZFS_DELEG_NOTE_GROUPQUOTA:
4224 case ZFS_DELEG_NOTE_GROUPUSED:
4225 case ZFS_DELEG_NOTE_USERPROP:
4226 case ZFS_DELEG_NOTE_USERQUOTA:
4227 case ZFS_DELEG_NOTE_USERUSED:
4229 return (gettext("other"));
4231 return (gettext("subcommand"));
4236 who_type2weight(zfs_deleg_who_type_t who_type)
4240 case ZFS_DELEG_NAMED_SET_SETS:
4241 case ZFS_DELEG_NAMED_SET:
4244 case ZFS_DELEG_CREATE_SETS:
4245 case ZFS_DELEG_CREATE:
4248 case ZFS_DELEG_USER_SETS:
4249 case ZFS_DELEG_USER:
4252 case ZFS_DELEG_GROUP_SETS:
4253 case ZFS_DELEG_GROUP:
4256 case ZFS_DELEG_EVERYONE_SETS:
4257 case ZFS_DELEG_EVERYONE:
4269 who_perm_compare(const void *larg, const void *rarg, void *unused)
4271 const who_perm_node_t *l = larg;
4272 const who_perm_node_t *r = rarg;
4273 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4274 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4275 int lweight = who_type2weight(ltype);
4276 int rweight = who_type2weight(rtype);
4277 int res = lweight - rweight;
4279 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4280 ZFS_MAX_DELEG_NAME-1);
4292 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4294 const deleg_perm_node_t *l = larg;
4295 const deleg_perm_node_t *r = rarg;
4296 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4297 ZFS_MAX_DELEG_NAME-1);
4309 fs_perm_set_init(fs_perm_set_t *fspset)
4311 bzero(fspset, sizeof (fs_perm_set_t));
4313 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4314 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4315 NULL, UU_DEFAULT)) == NULL)
4317 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4318 UU_DEFAULT)) == NULL)
4321 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4322 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4323 who_perm_node_t, who_avl_node), who_perm_compare,
4324 UU_DEFAULT)) == NULL)
4327 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4328 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4329 who_perm_node_t, who_avl_node), who_perm_compare,
4330 UU_DEFAULT)) == NULL)
4333 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4334 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4335 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4340 static inline void fs_perm_fini(fs_perm_t *);
4341 static inline void who_perm_fini(who_perm_t *);
4344 fs_perm_set_fini(fs_perm_set_t *fspset)
4346 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4348 while (node != NULL) {
4349 fs_perm_node_t *next_node =
4350 uu_list_next(fspset->fsps_list, node);
4351 fs_perm_t *fsperm = &node->fspn_fsperm;
4352 fs_perm_fini(fsperm);
4353 uu_list_remove(fspset->fsps_list, node);
4358 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4359 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4360 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4364 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4367 deleg_perm->dp_who_type = type;
4368 deleg_perm->dp_name = name;
4372 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4373 zfs_deleg_who_type_t type, const char *name)
4375 uu_avl_pool_t *pool;
4376 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4378 bzero(who_perm, sizeof (who_perm_t));
4380 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4381 UU_DEFAULT)) == NULL)
4384 who_perm->who_type = type;
4385 who_perm->who_name = name;
4386 who_perm->who_fsperm = fsperm;
4390 who_perm_fini(who_perm_t *who_perm)
4392 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4394 while (node != NULL) {
4395 deleg_perm_node_t *next_node =
4396 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4398 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4403 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4407 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4409 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4410 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4412 bzero(fsperm, sizeof (fs_perm_t));
4414 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4418 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4422 fsperm->fsp_set = fspset;
4423 fsperm->fsp_name = fsname;
4427 fs_perm_fini(fs_perm_t *fsperm)
4429 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4430 while (node != NULL) {
4431 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4433 who_perm_t *who_perm = &node->who_perm;
4434 who_perm_fini(who_perm);
4435 uu_avl_remove(fsperm->fsp_sc_avl, node);
4440 node = uu_avl_first(fsperm->fsp_uge_avl);
4441 while (node != NULL) {
4442 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4444 who_perm_t *who_perm = &node->who_perm;
4445 who_perm_fini(who_perm);
4446 uu_avl_remove(fsperm->fsp_uge_avl, node);
4451 uu_avl_destroy(fsperm->fsp_sc_avl);
4452 uu_avl_destroy(fsperm->fsp_uge_avl);
4456 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4457 zfs_deleg_who_type_t who_type, const char *name, char locality)
4459 uu_avl_index_t idx = 0;
4461 deleg_perm_node_t *found_node = NULL;
4462 deleg_perm_t *deleg_perm = &node->dpn_perm;
4464 deleg_perm_init(deleg_perm, who_type, name);
4466 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4468 uu_avl_insert(avl, node, idx);
4471 deleg_perm = &node->dpn_perm;
4476 case ZFS_DELEG_LOCAL:
4477 deleg_perm->dp_local = B_TRUE;
4479 case ZFS_DELEG_DESCENDENT:
4480 deleg_perm->dp_descend = B_TRUE;
4485 assert(B_FALSE); /* invalid locality */
4490 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4492 nvpair_t *nvp = NULL;
4493 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4494 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4495 zfs_deleg_who_type_t who_type = who_perm->who_type;
4497 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4498 const char *name = nvpair_name(nvp);
4499 data_type_t type = nvpair_type(nvp);
4500 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4501 deleg_perm_node_t *node =
4502 safe_malloc(sizeof (deleg_perm_node_t));
4504 assert(type == DATA_TYPE_BOOLEAN);
4506 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4507 set_deleg_perm_node(avl, node, who_type, name, locality);
4514 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4516 nvpair_t *nvp = NULL;
4517 fs_perm_set_t *fspset = fsperm->fsp_set;
4519 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4520 nvlist_t *nvl2 = NULL;
4521 const char *name = nvpair_name(nvp);
4522 uu_avl_t *avl = NULL;
4523 uu_avl_pool_t *avl_pool;
4524 zfs_deleg_who_type_t perm_type = name[0];
4525 char perm_locality = name[1];
4526 const char *perm_name = name + 3;
4527 boolean_t is_set = B_TRUE;
4528 who_perm_t *who_perm = NULL;
4530 assert('$' == name[2]);
4532 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4535 switch (perm_type) {
4536 case ZFS_DELEG_CREATE:
4537 case ZFS_DELEG_CREATE_SETS:
4538 case ZFS_DELEG_NAMED_SET:
4539 case ZFS_DELEG_NAMED_SET_SETS:
4540 avl_pool = fspset->fsps_named_set_avl_pool;
4541 avl = fsperm->fsp_sc_avl;
4543 case ZFS_DELEG_USER:
4544 case ZFS_DELEG_USER_SETS:
4545 case ZFS_DELEG_GROUP:
4546 case ZFS_DELEG_GROUP_SETS:
4547 case ZFS_DELEG_EVERYONE:
4548 case ZFS_DELEG_EVERYONE_SETS:
4549 avl_pool = fspset->fsps_who_perm_avl_pool;
4550 avl = fsperm->fsp_uge_avl;
4555 who_perm_node_t *found_node = NULL;
4556 who_perm_node_t *node = safe_malloc(
4557 sizeof (who_perm_node_t));
4558 who_perm = &node->who_perm;
4559 uu_avl_index_t idx = 0;
4561 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4562 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4564 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4566 if (avl == fsperm->fsp_uge_avl) {
4568 struct passwd *p = NULL;
4569 struct group *g = NULL;
4570 const char *nice_name = NULL;
4572 switch (perm_type) {
4573 case ZFS_DELEG_USER_SETS:
4574 case ZFS_DELEG_USER:
4575 rid = atoi(perm_name);
4578 nice_name = p->pw_name;
4580 case ZFS_DELEG_GROUP_SETS:
4581 case ZFS_DELEG_GROUP:
4582 rid = atoi(perm_name);
4585 nice_name = g->gr_name;
4589 if (nice_name != NULL)
4591 node->who_perm.who_ug_name,
4595 uu_avl_insert(avl, node, idx);
4598 who_perm = &node->who_perm;
4602 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4609 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4611 nvpair_t *nvp = NULL;
4612 uu_avl_index_t idx = 0;
4614 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4615 nvlist_t *nvl2 = NULL;
4616 const char *fsname = nvpair_name(nvp);
4617 data_type_t type = nvpair_type(nvp);
4618 fs_perm_t *fsperm = NULL;
4619 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4623 fsperm = &node->fspn_fsperm;
4625 assert(DATA_TYPE_NVLIST == type);
4627 uu_list_node_init(node, &node->fspn_list_node,
4628 fspset->fsps_list_pool);
4630 idx = uu_list_numnodes(fspset->fsps_list);
4631 fs_perm_init(fsperm, fspset, fsname);
4633 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4636 (void) parse_fs_perm(fsperm, nvl2);
4638 uu_list_insert(fspset->fsps_list, node, idx);
4644 static inline const char *
4645 deleg_perm_comment(zfs_deleg_note_t note)
4647 const char *str = "";
4652 case ZFS_DELEG_NOTE_ALLOW:
4653 str = gettext("Must also have the permission that is being"
4654 "\n\t\t\t\tallowed");
4656 case ZFS_DELEG_NOTE_CLONE:
4657 str = gettext("Must also have the 'create' ability and 'mount'"
4658 "\n\t\t\t\tability in the origin file system");
4660 case ZFS_DELEG_NOTE_CREATE:
4661 str = gettext("Must also have the 'mount' ability");
4663 case ZFS_DELEG_NOTE_DESTROY:
4664 str = gettext("Must also have the 'mount' ability");
4666 case ZFS_DELEG_NOTE_DIFF:
4667 str = gettext("Allows lookup of paths within a dataset;"
4668 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4669 "\n\t\t\t\tin order to use zfs diff");
4671 case ZFS_DELEG_NOTE_HOLD:
4672 str = gettext("Allows adding a user hold to a snapshot");
4674 case ZFS_DELEG_NOTE_MOUNT:
4675 str = gettext("Allows mount/umount of ZFS datasets");
4677 case ZFS_DELEG_NOTE_PROMOTE:
4678 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4679 " 'promote' ability in the origin file system");
4681 case ZFS_DELEG_NOTE_RECEIVE:
4682 str = gettext("Must also have the 'mount' and 'create'"
4685 case ZFS_DELEG_NOTE_RELEASE:
4686 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4687 "might destroy the snapshot");
4689 case ZFS_DELEG_NOTE_RENAME:
4690 str = gettext("Must also have the 'mount' and 'create'"
4691 "\n\t\t\t\tability in the new parent");
4693 case ZFS_DELEG_NOTE_ROLLBACK:
4696 case ZFS_DELEG_NOTE_SEND:
4699 case ZFS_DELEG_NOTE_SHARE:
4700 str = gettext("Allows sharing file systems over NFS or SMB"
4701 "\n\t\t\t\tprotocols");
4703 case ZFS_DELEG_NOTE_SNAPSHOT:
4707 * case ZFS_DELEG_NOTE_VSCAN:
4708 * str = gettext("");
4712 case ZFS_DELEG_NOTE_GROUPQUOTA:
4713 str = gettext("Allows accessing any groupquota@... property");
4715 case ZFS_DELEG_NOTE_GROUPUSED:
4716 str = gettext("Allows reading any groupused@... property");
4718 case ZFS_DELEG_NOTE_USERPROP:
4719 str = gettext("Allows changing any user property");
4721 case ZFS_DELEG_NOTE_USERQUOTA:
4722 str = gettext("Allows accessing any userquota@... property");
4724 case ZFS_DELEG_NOTE_USERUSED:
4725 str = gettext("Allows reading any userused@... property");
4743 boolean_t recursive; /* unallow only */
4744 boolean_t prt_usage;
4746 boolean_t prt_perms;
4749 const char *dataset;
4753 prop_cmp(const void *a, const void *b)
4755 const char *str1 = *(const char **)a;
4756 const char *str2 = *(const char **)b;
4757 return (strcmp(str1, str2));
4761 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4763 const char *opt_desc[] = {
4764 "-h", gettext("show this help message and exit"),
4765 "-l", gettext("set permission locally"),
4766 "-d", gettext("set permission for descents"),
4767 "-u", gettext("set permission for user"),
4768 "-g", gettext("set permission for group"),
4769 "-e", gettext("set permission for everyone"),
4770 "-c", gettext("set create time permission"),
4771 "-s", gettext("define permission set"),
4773 "-r", gettext("remove permissions recursively"),
4775 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4776 size_t allow_size = unallow_size - 2;
4777 const char *props[ZFS_NUM_PROPS];
4780 FILE *fp = requested ? stdout : stderr;
4781 zprop_desc_t *pdtbl = zfs_prop_get_table();
4782 const char *fmt = gettext("%-16s %-14s\t%s\n");
4784 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4786 (void) fprintf(fp, gettext("Options:\n"));
4787 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4788 const char *opt = opt_desc[i++];
4789 const char *optdsc = opt_desc[i];
4790 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4793 (void) fprintf(fp, gettext("\nThe following permissions are "
4795 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4797 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4798 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4799 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4800 const char *perm_type = deleg_perm_type(perm_note);
4801 const char *perm_comment = deleg_perm_comment(perm_note);
4802 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4805 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4806 zprop_desc_t *pd = &pdtbl[i];
4807 if (pd->pd_visible != B_TRUE)
4810 if (pd->pd_attr == PROP_READONLY)
4813 props[count++] = pd->pd_name;
4815 props[count] = NULL;
4817 qsort(props, count, sizeof (char *), prop_cmp);
4819 for (i = 0; i < count; i++)
4820 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4823 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4825 exit(requested ? 0 : 2);
4828 static inline const char *
4829 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4832 if (un && argc == expected_argc - 1)
4834 else if (argc == expected_argc)
4835 *permsp = argv[argc - 2];
4837 allow_usage(un, B_FALSE,
4838 gettext("wrong number of parameters\n"));
4840 return (argv[argc - 1]);
4844 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4846 int uge_sum = opts->user + opts->group + opts->everyone;
4847 int csuge_sum = opts->create + opts->set + uge_sum;
4848 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4849 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4852 allow_usage(un, B_FALSE,
4853 gettext("-u, -g, and -e are mutually exclusive\n"));
4855 if (opts->prt_usage)
4856 if (argc == 0 && all_sum == 0)
4857 allow_usage(un, B_TRUE, NULL);
4863 allow_usage(un, B_FALSE,
4864 gettext("invalid options combined with -s\n"));
4866 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4867 if (argv[0][0] != '@')
4868 allow_usage(un, B_FALSE,
4869 gettext("invalid set name: missing '@' prefix\n"));
4870 opts->who = argv[0];
4871 } else if (opts->create) {
4872 if (ldcsuge_sum > 1)
4873 allow_usage(un, B_FALSE,
4874 gettext("invalid options combined with -c\n"));
4875 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4876 } else if (opts->everyone) {
4878 allow_usage(un, B_FALSE,
4879 gettext("invalid options combined with -e\n"));
4880 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4881 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4883 opts->everyone = B_TRUE;
4886 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4887 } else if (argc == 1 && !un) {
4888 opts->prt_perms = B_TRUE;
4889 opts->dataset = argv[argc-1];
4891 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4892 opts->who = argv[0];
4895 if (!opts->local && !opts->descend) {
4896 opts->local = B_TRUE;
4897 opts->descend = B_TRUE;
4902 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4903 const char *who, char *perms, nvlist_t *top_nvl)
4906 char ld[2] = { '\0', '\0' };
4907 char who_buf[ZFS_MAXNAMELEN+32];
4910 nvlist_t *base_nvl = NULL;
4911 nvlist_t *set_nvl = NULL;
4914 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4916 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4920 case ZFS_DELEG_NAMED_SET_SETS:
4921 case ZFS_DELEG_NAMED_SET:
4922 set_type = ZFS_DELEG_NAMED_SET_SETS;
4923 base_type = ZFS_DELEG_NAMED_SET;
4924 ld[0] = ZFS_DELEG_NA;
4926 case ZFS_DELEG_CREATE_SETS:
4927 case ZFS_DELEG_CREATE:
4928 set_type = ZFS_DELEG_CREATE_SETS;
4929 base_type = ZFS_DELEG_CREATE;
4930 ld[0] = ZFS_DELEG_NA;
4932 case ZFS_DELEG_USER_SETS:
4933 case ZFS_DELEG_USER:
4934 set_type = ZFS_DELEG_USER_SETS;
4935 base_type = ZFS_DELEG_USER;
4937 ld[0] = ZFS_DELEG_LOCAL;
4939 ld[1] = ZFS_DELEG_DESCENDENT;
4941 case ZFS_DELEG_GROUP_SETS:
4942 case ZFS_DELEG_GROUP:
4943 set_type = ZFS_DELEG_GROUP_SETS;
4944 base_type = ZFS_DELEG_GROUP;
4946 ld[0] = ZFS_DELEG_LOCAL;
4948 ld[1] = ZFS_DELEG_DESCENDENT;
4950 case ZFS_DELEG_EVERYONE_SETS:
4951 case ZFS_DELEG_EVERYONE:
4952 set_type = ZFS_DELEG_EVERYONE_SETS;
4953 base_type = ZFS_DELEG_EVERYONE;
4955 ld[0] = ZFS_DELEG_LOCAL;
4957 ld[1] = ZFS_DELEG_DESCENDENT;
4960 if (perms != NULL) {
4962 char *end = curr + strlen(perms);
4964 while (curr < end) {
4965 char *delim = strchr(curr, ',');
4976 (void) nvlist_add_boolean(nvl, curr);
4982 for (i = 0; i < 2; i++) {
4983 char locality = ld[i];
4987 if (!nvlist_empty(base_nvl)) {
4989 (void) snprintf(who_buf,
4990 sizeof (who_buf), "%c%c$%s",
4991 base_type, locality, who);
4993 (void) snprintf(who_buf,
4994 sizeof (who_buf), "%c%c$",
4995 base_type, locality);
4997 (void) nvlist_add_nvlist(top_nvl, who_buf,
5002 if (!nvlist_empty(set_nvl)) {
5004 (void) snprintf(who_buf,
5005 sizeof (who_buf), "%c%c$%s",
5006 set_type, locality, who);
5008 (void) snprintf(who_buf,
5009 sizeof (who_buf), "%c%c$",
5010 set_type, locality);
5012 (void) nvlist_add_nvlist(top_nvl, who_buf,
5017 for (i = 0; i < 2; i++) {
5018 char locality = ld[i];
5023 (void) snprintf(who_buf, sizeof (who_buf),
5024 "%c%c$%s", base_type, locality, who);
5026 (void) snprintf(who_buf, sizeof (who_buf),
5027 "%c%c$", base_type, locality);
5028 (void) nvlist_add_boolean(top_nvl, who_buf);
5031 (void) snprintf(who_buf, sizeof (who_buf),
5032 "%c%c$%s", set_type, locality, who);
5034 (void) snprintf(who_buf, sizeof (who_buf),
5035 "%c%c$", set_type, locality);
5036 (void) nvlist_add_boolean(top_nvl, who_buf);
5042 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5044 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5048 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5049 opts->descend, opts->who, opts->perms, *nvlp);
5050 } else if (opts->create) {
5051 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5052 opts->descend, NULL, opts->perms, *nvlp);
5053 } else if (opts->everyone) {
5054 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5055 opts->descend, NULL, opts->perms, *nvlp);
5057 char *curr = opts->who;
5058 char *end = curr + strlen(curr);
5060 while (curr < end) {
5062 zfs_deleg_who_type_t who_type;
5064 char *delim = strchr(curr, ',');
5067 struct passwd *p = NULL;
5068 struct group *g = NULL;
5076 rid = (uid_t)strtol(curr, &endch, 0);
5078 who_type = ZFS_DELEG_USER;
5087 (void) snprintf(errbuf, 256, gettext(
5088 "invalid user %s"), curr);
5089 allow_usage(un, B_TRUE, errbuf);
5091 } else if (opts->group) {
5092 who_type = ZFS_DELEG_GROUP;
5101 (void) snprintf(errbuf, 256, gettext(
5102 "invalid group %s"), curr);
5103 allow_usage(un, B_TRUE, errbuf);
5106 if (*endch != '\0') {
5113 if (*endch != '\0') {
5120 who_type = ZFS_DELEG_USER;
5122 } else if (g != NULL) {
5123 who_type = ZFS_DELEG_GROUP;
5126 (void) snprintf(errbuf, 256, gettext(
5127 "invalid user/group %s"), curr);
5128 allow_usage(un, B_TRUE, errbuf);
5132 (void) sprintf(id, "%u", rid);
5135 store_allow_perm(who_type, opts->local,
5136 opts->descend, who, opts->perms, *nvlp);
5145 print_set_creat_perms(uu_avl_t *who_avl)
5147 const char *sc_title[] = {
5148 gettext("Permission sets:\n"),
5149 gettext("Create time permissions:\n"),
5152 const char **title_ptr = sc_title;
5153 who_perm_node_t *who_node = NULL;
5154 int prev_weight = -1;
5156 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5157 who_node = uu_avl_next(who_avl, who_node)) {
5158 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5159 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5160 const char *who_name = who_node->who_perm.who_name;
5161 int weight = who_type2weight(who_type);
5162 boolean_t first = B_TRUE;
5163 deleg_perm_node_t *deleg_node;
5165 if (prev_weight != weight) {
5166 (void) printf(*title_ptr++);
5167 prev_weight = weight;
5170 if (who_name == NULL || strnlen(who_name, 1) == 0)
5171 (void) printf("\t");
5173 (void) printf("\t%s ", who_name);
5175 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5176 deleg_node = uu_avl_next(avl, deleg_node)) {
5179 deleg_node->dpn_perm.dp_name);
5182 (void) printf(",%s",
5183 deleg_node->dpn_perm.dp_name);
5186 (void) printf("\n");
5191 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5194 who_perm_node_t *who_node = NULL;
5195 boolean_t prt_title = B_TRUE;
5196 uu_avl_walk_t *walk;
5198 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5201 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5202 const char *who_name = who_node->who_perm.who_name;
5203 const char *nice_who_name = who_node->who_perm.who_ug_name;
5204 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5205 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5207 deleg_perm_node_t *deleg_node;
5208 boolean_t prt_who = B_TRUE;
5210 for (deleg_node = uu_avl_first(avl);
5212 deleg_node = uu_avl_next(avl, deleg_node)) {
5213 if (local != deleg_node->dpn_perm.dp_local ||
5214 descend != deleg_node->dpn_perm.dp_descend)
5218 const char *who = NULL;
5220 prt_title = B_FALSE;
5221 (void) printf(title);
5225 case ZFS_DELEG_USER_SETS:
5226 case ZFS_DELEG_USER:
5227 who = gettext("user");
5229 who_name = nice_who_name;
5231 case ZFS_DELEG_GROUP_SETS:
5232 case ZFS_DELEG_GROUP:
5233 who = gettext("group");
5235 who_name = nice_who_name;
5237 case ZFS_DELEG_EVERYONE_SETS:
5238 case ZFS_DELEG_EVERYONE:
5239 who = gettext("everyone");
5244 if (who_name == NULL)
5245 (void) printf("\t%s", who);
5247 (void) printf("\t%s %s", who, who_name);
5250 (void) printf("%c%s", delim,
5251 deleg_node->dpn_perm.dp_name);
5256 (void) printf("\n");
5259 uu_avl_walk_end(walk);
5263 print_fs_perms(fs_perm_set_t *fspset)
5265 fs_perm_node_t *node = NULL;
5266 char buf[ZFS_MAXNAMELEN+32];
5267 const char *dsname = buf;
5269 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5270 node = uu_list_next(fspset->fsps_list, node)) {
5271 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5272 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5275 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5276 gettext("---- Permissions on %s "),
5277 node->fspn_fsperm.fsp_name);
5278 (void) printf(dsname);
5279 left = 70 - strlen(buf);
5282 (void) printf("\n");
5284 print_set_creat_perms(sc_avl);
5285 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5286 gettext("Local permissions:\n"));
5287 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5288 gettext("Descendent permissions:\n"));
5289 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5290 gettext("Local+Descendent permissions:\n"));
5294 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5296 struct deleg_perms {
5302 set_deleg_perms(zfs_handle_t *zhp, void *data)
5304 struct deleg_perms *perms = (struct deleg_perms *)data;
5305 zfs_type_t zfs_type = zfs_get_type(zhp);
5307 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5310 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5314 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5317 nvlist_t *perm_nvl = NULL;
5318 nvlist_t *update_perm_nvl = NULL;
5321 struct allow_opts opts = { 0 };
5323 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5326 while ((c = getopt(argc, argv, optstr)) != -1) {
5329 opts.local = B_TRUE;
5332 opts.descend = B_TRUE;
5338 opts.group = B_TRUE;
5341 opts.everyone = B_TRUE;
5347 opts.create = B_TRUE;
5350 opts.recursive = B_TRUE;
5353 (void) fprintf(stderr, gettext("missing argument for "
5354 "'%c' option\n"), optopt);
5358 opts.prt_usage = B_TRUE;
5361 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5370 /* check arguments */
5371 parse_allow_args(argc, argv, un, &opts);
5373 /* try to open the dataset */
5374 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5375 ZFS_TYPE_VOLUME)) == NULL) {
5376 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5381 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5384 fs_perm_set_init(&fs_perm_set);
5385 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5386 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5391 print_fs_perms(&fs_perm_set);
5393 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5394 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5397 if (un && opts.recursive) {
5398 struct deleg_perms data = { un, update_perm_nvl };
5399 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5408 nvlist_free(perm_nvl);
5409 if (update_perm_nvl != NULL)
5410 nvlist_free(update_perm_nvl);
5412 fs_perm_set_fini(&fs_perm_set);
5420 zfs_do_allow(int argc, char **argv)
5422 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5426 zfs_do_unallow(int argc, char **argv)
5428 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5432 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5437 boolean_t recursive = B_FALSE;
5438 const char *opts = holding ? "rt" : "r";
5442 while ((c = getopt(argc, argv, opts)) != -1) {
5448 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5457 /* check number of arguments */
5465 if (holding && tag[0] == '.') {
5466 /* tags starting with '.' are reserved for libzfs */
5467 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5471 for (i = 0; i < argc; ++i) {
5473 char parent[ZFS_MAXNAMELEN];
5475 char *path = argv[i];
5477 delim = strchr(path, '@');
5478 if (delim == NULL) {
5479 (void) fprintf(stderr,
5480 gettext("'%s' is not a snapshot\n"), path);
5484 (void) strncpy(parent, path, delim - path);
5485 parent[delim - path] = '\0';
5487 zhp = zfs_open(g_zfs, parent,
5488 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5494 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5497 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5503 return (errors != 0);
5507 * zfs hold [-r] [-t] <tag> <snap> ...
5509 * -r Recursively hold
5511 * Apply a user-hold with the given tag to the list of snapshots.
5514 zfs_do_hold(int argc, char **argv)
5516 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5520 * zfs release [-r] <tag> <snap> ...
5522 * -r Recursively release
5524 * Release a user-hold with the given tag from the list of snapshots.
5527 zfs_do_release(int argc, char **argv)
5529 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5532 typedef struct holds_cbdata {
5533 boolean_t cb_recursive;
5534 const char *cb_snapname;
5536 size_t cb_max_namelen;
5537 size_t cb_max_taglen;
5540 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5541 #define DATETIME_BUF_LEN (32)
5546 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5549 nvpair_t *nvp = NULL;
5550 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5554 for (i = 0; i < 3; i++) {
5555 col = gettext(hdr_cols[i]);
5557 (void) printf("%-*s ", i ? tagwidth : nwidth,
5560 (void) printf("%s\n", col);
5564 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5565 char *zname = nvpair_name(nvp);
5567 nvpair_t *nvp2 = NULL;
5568 (void) nvpair_value_nvlist(nvp, &nvl2);
5569 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5570 char tsbuf[DATETIME_BUF_LEN];
5571 char *tagname = nvpair_name(nvp2);
5575 char sep = scripted ? '\t' : ' ';
5576 size_t sepnum = scripted ? 1 : 2;
5578 (void) nvpair_value_uint64(nvp2, &val);
5580 (void) localtime_r(&time, &t);
5581 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5582 gettext(STRFTIME_FMT_STR), &t);
5584 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5585 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5591 * Generic callback function to list a dataset or snapshot.
5594 holds_callback(zfs_handle_t *zhp, void *data)
5596 holds_cbdata_t *cbp = data;
5597 nvlist_t *top_nvl = *cbp->cb_nvlp;
5598 nvlist_t *nvl = NULL;
5599 nvpair_t *nvp = NULL;
5600 const char *zname = zfs_get_name(zhp);
5601 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5603 if (cbp->cb_recursive) {
5604 const char *snapname;
5605 char *delim = strchr(zname, '@');
5609 snapname = delim + 1;
5610 if (strcmp(cbp->cb_snapname, snapname))
5614 if (zfs_get_holds(zhp, &nvl) != 0)
5617 if (znamelen > cbp->cb_max_namelen)
5618 cbp->cb_max_namelen = znamelen;
5620 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5621 const char *tag = nvpair_name(nvp);
5622 size_t taglen = strnlen(tag, MAXNAMELEN);
5623 if (taglen > cbp->cb_max_taglen)
5624 cbp->cb_max_taglen = taglen;
5627 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5631 * zfs holds [-r] <snap> ...
5633 * -r Recursively hold
5636 zfs_do_holds(int argc, char **argv)
5641 boolean_t scripted = B_FALSE;
5642 boolean_t recursive = B_FALSE;
5643 const char *opts = "rH";
5646 int types = ZFS_TYPE_SNAPSHOT;
5647 holds_cbdata_t cb = { 0 };
5654 while ((c = getopt(argc, argv, opts)) != -1) {
5663 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5670 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5671 flags |= ZFS_ITER_RECURSE;
5677 /* check number of arguments */
5681 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5684 for (i = 0; i < argc; ++i) {
5685 char *snapshot = argv[i];
5687 const char *snapname;
5689 delim = strchr(snapshot, '@');
5690 if (delim == NULL) {
5691 (void) fprintf(stderr,
5692 gettext("'%s' is not a snapshot\n"), snapshot);
5696 snapname = delim + 1;
5698 snapshot[delim - snapshot] = '\0';
5700 cb.cb_recursive = recursive;
5701 cb.cb_snapname = snapname;
5705 * 1. collect holds data, set format options
5707 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5708 holds_callback, &cb);
5714 * 2. print holds data
5716 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5718 if (nvlist_empty(nvl))
5719 (void) printf(gettext("no datasets available\n"));
5723 return (0 != errors);
5726 #define CHECK_SPINNER 30
5727 #define SPINNER_TIME 3 /* seconds */
5728 #define MOUNT_TIME 5 /* seconds */
5731 get_one_dataset(zfs_handle_t *zhp, void *data)
5733 static char *spin[] = { "-", "\\", "|", "/" };
5734 static int spinval = 0;
5735 static int spincheck = 0;
5736 static time_t last_spin_time = (time_t)0;
5737 get_all_cb_t *cbp = data;
5738 zfs_type_t type = zfs_get_type(zhp);
5740 if (cbp->cb_verbose) {
5741 if (--spincheck < 0) {
5742 time_t now = time(NULL);
5743 if (last_spin_time + SPINNER_TIME < now) {
5744 update_progress(spin[spinval++ % 4]);
5745 last_spin_time = now;
5747 spincheck = CHECK_SPINNER;
5752 * Interate over any nested datasets.
5754 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5760 * Skip any datasets whose type does not match.
5762 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5766 libzfs_add_handle(cbp, zhp);
5767 assert(cbp->cb_used <= cbp->cb_alloc);
5773 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5775 get_all_cb_t cb = { 0 };
5776 cb.cb_verbose = verbose;
5777 cb.cb_getone = get_one_dataset;
5780 set_progress_header(gettext("Reading ZFS config"));
5781 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5783 *dslist = cb.cb_handles;
5784 *count = cb.cb_used;
5787 finish_progress(gettext("done."));
5791 * Generic callback for sharing or mounting filesystems. Because the code is so
5792 * similar, we have a common function with an extra parameter to determine which
5793 * mode we are using.
5795 #define OP_SHARE 0x1
5796 #define OP_MOUNT 0x2
5799 * Share or mount a dataset.
5802 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5803 boolean_t explicit, const char *options)
5805 char mountpoint[ZFS_MAXPROPLEN];
5806 char shareopts[ZFS_MAXPROPLEN];
5807 char smbshareopts[ZFS_MAXPROPLEN];
5808 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5810 uint64_t zoned, canmount;
5811 boolean_t shared_nfs, shared_smb;
5813 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5816 * Check to make sure we can mount/share this dataset. If we
5817 * are in the global zone and the filesystem is exported to a
5818 * local zone, or if we are in a local zone and the
5819 * filesystem is not exported, then it is an error.
5821 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5823 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5827 (void) fprintf(stderr, gettext("cannot %s '%s': "
5828 "dataset is exported to a local zone\n"), cmdname,
5832 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5836 (void) fprintf(stderr, gettext("cannot %s '%s': "
5837 "permission denied\n"), cmdname,
5843 * Ignore any filesystems which don't apply to us. This
5844 * includes those with a legacy mountpoint, or those with
5845 * legacy share options.
5847 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5848 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5849 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5850 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5851 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5852 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5854 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5855 strcmp(smbshareopts, "off") == 0) {
5859 (void) fprintf(stderr, gettext("cannot share '%s': "
5860 "legacy share\n"), zfs_get_name(zhp));
5861 (void) fprintf(stderr, gettext("to "
5862 "share this filesystem set "
5863 "sharenfs property on\n"));
5868 * We cannot share or mount legacy filesystems. If the
5869 * shareopts is non-legacy but the mountpoint is legacy, we
5870 * treat it as a legacy share.
5872 if (strcmp(mountpoint, "legacy") == 0) {
5876 (void) fprintf(stderr, gettext("cannot %s '%s': "
5877 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5878 (void) fprintf(stderr, gettext("use %s(8) to "
5879 "%s this filesystem\n"), cmdname, cmdname);
5883 if (strcmp(mountpoint, "none") == 0) {
5887 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5888 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5893 * canmount explicit outcome
5894 * on no pass through
5895 * on yes pass through
5897 * off yes display error, return 1
5898 * noauto no return 0
5899 * noauto yes pass through
5901 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5902 if (canmount == ZFS_CANMOUNT_OFF) {
5906 (void) fprintf(stderr, gettext("cannot %s '%s': "
5907 "'canmount' property is set to 'off'\n"), cmdname,
5910 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5915 * If this filesystem is inconsistent and has a receive resume
5916 * token, we can not mount it.
5918 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
5919 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
5920 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
5924 (void) fprintf(stderr, gettext("cannot %s '%s': "
5925 "Contains partially-completed state from "
5926 "\"zfs receive -r\", which can be resumed with "
5927 "\"zfs send -t\"\n"),
5928 cmdname, zfs_get_name(zhp));
5933 * At this point, we have verified that the mountpoint and/or
5934 * shareopts are appropriate for auto management. If the
5935 * filesystem is already mounted or shared, return (failing
5936 * for explicit requests); otherwise mount or share the
5942 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5943 shared_smb = zfs_is_shared_smb(zhp, NULL);
5945 if (shared_nfs && shared_smb ||
5946 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5947 strcmp(smbshareopts, "off") == 0) ||
5948 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5949 strcmp(shareopts, "off") == 0)) {
5953 (void) fprintf(stderr, gettext("cannot share "
5954 "'%s': filesystem already shared\n"),
5959 if (!zfs_is_mounted(zhp, NULL) &&
5960 zfs_mount(zhp, NULL, 0) != 0)
5963 if (protocol == NULL) {
5964 if (zfs_shareall(zhp) != 0)
5966 } else if (strcmp(protocol, "nfs") == 0) {
5967 if (zfs_share_nfs(zhp))
5969 } else if (strcmp(protocol, "smb") == 0) {
5970 if (zfs_share_smb(zhp))
5973 (void) fprintf(stderr, gettext("cannot share "
5974 "'%s': invalid share type '%s' "
5976 zfs_get_name(zhp), protocol);
5983 if (options == NULL)
5984 mnt.mnt_mntopts = "";
5986 mnt.mnt_mntopts = (char *)options;
5988 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
5989 zfs_is_mounted(zhp, NULL)) {
5993 (void) fprintf(stderr, gettext("cannot mount "
5994 "'%s': filesystem already mounted\n"),
5999 if (zfs_mount(zhp, options, flags) != 0)
6008 * Reports progress in the form "(current/total)". Not thread-safe.
6011 report_mount_progress(int current, int total)
6013 static time_t last_progress_time = 0;
6014 time_t now = time(NULL);
6017 /* report 1..n instead of 0..n-1 */
6020 /* display header if we're here for the first time */
6022 set_progress_header(gettext("Mounting ZFS filesystems"));
6023 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6024 /* too soon to report again */
6028 last_progress_time = now;
6030 (void) sprintf(info, "(%d/%d)", current, total);
6032 if (current == total)
6033 finish_progress(info);
6035 update_progress(info);
6039 append_options(char *mntopts, char *newopts)
6041 int len = strlen(mntopts);
6043 /* original length plus new string to append plus 1 for the comma */
6044 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6045 (void) fprintf(stderr, gettext("the opts argument for "
6046 "'%c' option is too long (more than %d chars)\n"),
6047 "-o", MNT_LINE_MAX);
6052 mntopts[len++] = ',';
6054 (void) strcpy(&mntopts[len], newopts);
6058 share_mount(int op, int argc, char **argv)
6061 boolean_t verbose = B_FALSE;
6063 char *options = NULL;
6067 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6077 if (*optarg == '\0') {
6078 (void) fprintf(stderr, gettext("empty mount "
6079 "options (-o) specified\n"));
6083 if (options == NULL)
6084 options = safe_malloc(MNT_LINE_MAX + 1);
6086 /* option validation is done later */
6087 append_options(options, optarg);
6091 warnx("no overlay mounts support on FreeBSD, ignoring");
6094 (void) fprintf(stderr, gettext("missing argument for "
6095 "'%c' option\n"), optopt);
6099 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6108 /* check number of arguments */
6110 zfs_handle_t **dslist = NULL;
6111 size_t i, count = 0;
6112 char *protocol = NULL;
6114 if (op == OP_SHARE && argc > 0) {
6115 if (strcmp(argv[0], "nfs") != 0 &&
6116 strcmp(argv[0], "smb") != 0) {
6117 (void) fprintf(stderr, gettext("share type "
6118 "must be 'nfs' or 'smb'\n"));
6127 (void) fprintf(stderr, gettext("too many arguments\n"));
6131 start_progress_timer();
6132 get_all_datasets(&dslist, &count, verbose);
6137 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6139 for (i = 0; i < count; i++) {
6141 report_mount_progress(i, count);
6143 if (share_mount_one(dslist[i], op, flags, protocol,
6144 B_FALSE, options) != 0)
6146 zfs_close(dslist[i]);
6150 } else if (argc == 0) {
6151 struct mnttab entry;
6153 if ((op == OP_SHARE) || (options != NULL)) {
6154 (void) fprintf(stderr, gettext("missing filesystem "
6155 "argument (specify -a for all)\n"));
6160 * When mount is given no arguments, go through /etc/mnttab and
6161 * display any active ZFS mounts. We hide any snapshots, since
6162 * they are controlled automatically.
6164 rewind(mnttab_file);
6165 while (getmntent(mnttab_file, &entry) == 0) {
6166 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6167 strchr(entry.mnt_special, '@') != NULL)
6170 (void) printf("%-30s %s\n", entry.mnt_special,
6178 (void) fprintf(stderr,
6179 gettext("too many arguments\n"));
6183 if ((zhp = zfs_open(g_zfs, argv[0],
6184 ZFS_TYPE_FILESYSTEM)) == NULL) {
6187 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6197 * zfs mount -a [nfs]
6198 * zfs mount filesystem
6200 * Mount all filesystems, or mount the given filesystem.
6203 zfs_do_mount(int argc, char **argv)
6205 return (share_mount(OP_MOUNT, argc, argv));
6209 * zfs share -a [nfs | smb]
6210 * zfs share filesystem
6212 * Share all filesystems, or share the given filesystem.
6215 zfs_do_share(int argc, char **argv)
6217 return (share_mount(OP_SHARE, argc, argv));
6220 typedef struct unshare_unmount_node {
6221 zfs_handle_t *un_zhp;
6223 uu_avl_node_t un_avlnode;
6224 } unshare_unmount_node_t;
6228 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6230 const unshare_unmount_node_t *l = larg;
6231 const unshare_unmount_node_t *r = rarg;
6233 return (strcmp(l->un_mountp, r->un_mountp));
6237 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6238 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6239 * and unmount it appropriately.
6242 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6246 struct stat64 statbuf;
6247 struct extmnttab entry;
6248 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6252 * Search for the path in /etc/mnttab. Rather than looking for the
6253 * specific path, which can be fooled by non-standard paths (i.e. ".."
6254 * or "//"), we stat() the path and search for the corresponding
6255 * (major,minor) device pair.
6257 if (stat64(path, &statbuf) != 0) {
6258 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6259 cmdname, path, strerror(errno));
6262 path_inode = statbuf.st_ino;
6265 * Search for the given (major,minor) pair in the mount table.
6268 rewind(mnttab_file);
6269 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6270 if (entry.mnt_major == major(statbuf.st_dev) &&
6271 entry.mnt_minor == minor(statbuf.st_dev))
6278 if (statfs(path, &sfs) != 0) {
6279 (void) fprintf(stderr, "%s: %s\n", path,
6283 statfs2mnttab(&sfs, &entry);
6287 if (op == OP_SHARE) {
6288 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6289 "currently mounted\n"), cmdname, path);
6292 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6294 if ((ret = umount2(path, flags)) != 0)
6295 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6300 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6301 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6302 "filesystem\n"), cmdname, path);
6306 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6307 ZFS_TYPE_FILESYSTEM)) == NULL)
6311 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6312 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6313 cmdname, path, strerror(errno));
6315 } else if (statbuf.st_ino != path_inode) {
6316 (void) fprintf(stderr, gettext("cannot "
6317 "%s '%s': not a mountpoint\n"), cmdname, path);
6321 if (op == OP_SHARE) {
6322 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6323 char smbshare_prop[ZFS_MAXPROPLEN];
6325 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6326 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6327 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6328 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6330 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6331 strcmp(smbshare_prop, "off") == 0) {
6332 (void) fprintf(stderr, gettext("cannot unshare "
6333 "'%s': legacy share\n"), path);
6335 (void) fprintf(stderr, gettext("use "
6336 "unshare(1M) to unshare this filesystem\n"));
6338 } else if (!zfs_is_shared(zhp)) {
6339 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6340 "not currently shared\n"), path);
6342 ret = zfs_unshareall_bypath(zhp, path);
6345 char mtpt_prop[ZFS_MAXPROPLEN];
6347 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6348 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6351 ret = zfs_unmount(zhp, NULL, flags);
6352 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6353 (void) fprintf(stderr, gettext("cannot unmount "
6354 "'%s': legacy mountpoint\n"),
6356 (void) fprintf(stderr, gettext("use umount(8) "
6357 "to unmount this filesystem\n"));
6359 ret = zfs_unmountall(zhp, flags);
6370 * Generic callback for unsharing or unmounting a filesystem.
6373 unshare_unmount(int op, int argc, char **argv)
6380 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6381 char sharesmb[ZFS_MAXPROPLEN];
6384 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6393 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6404 * We could make use of zfs_for_each() to walk all datasets in
6405 * the system, but this would be very inefficient, especially
6406 * since we would have to linearly search /etc/mnttab for each
6407 * one. Instead, do one pass through /etc/mnttab looking for
6408 * zfs entries and call zfs_unmount() for each one.
6410 * Things get a little tricky if the administrator has created
6411 * mountpoints beneath other ZFS filesystems. In this case, we
6412 * have to unmount the deepest filesystems first. To accomplish
6413 * this, we place all the mountpoints in an AVL tree sorted by
6414 * the special type (dataset name), and walk the result in
6415 * reverse to make sure to get any snapshots first.
6417 struct mnttab entry;
6418 uu_avl_pool_t *pool;
6420 unshare_unmount_node_t *node;
6422 uu_avl_walk_t *walk;
6425 (void) fprintf(stderr, gettext("too many arguments\n"));
6429 if (((pool = uu_avl_pool_create("unmount_pool",
6430 sizeof (unshare_unmount_node_t),
6431 offsetof(unshare_unmount_node_t, un_avlnode),
6432 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6433 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6436 rewind(mnttab_file);
6437 while (getmntent(mnttab_file, &entry) == 0) {
6439 /* ignore non-ZFS entries */
6440 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6443 /* ignore snapshots */
6444 if (strchr(entry.mnt_special, '@') != NULL)
6447 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6448 ZFS_TYPE_FILESYSTEM)) == NULL) {
6455 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6457 sizeof (nfs_mnt_prop),
6458 NULL, NULL, 0, B_FALSE) == 0);
6459 if (strcmp(nfs_mnt_prop, "off") != 0)
6461 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6463 sizeof (nfs_mnt_prop),
6464 NULL, NULL, 0, B_FALSE) == 0);
6465 if (strcmp(nfs_mnt_prop, "off") == 0)
6469 /* Ignore legacy mounts */
6470 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6472 sizeof (nfs_mnt_prop),
6473 NULL, NULL, 0, B_FALSE) == 0);
6474 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6476 /* Ignore canmount=noauto mounts */
6477 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6478 ZFS_CANMOUNT_NOAUTO)
6484 node = safe_malloc(sizeof (unshare_unmount_node_t));
6486 node->un_mountp = safe_strdup(entry.mnt_mountp);
6488 uu_avl_node_init(node, &node->un_avlnode, pool);
6490 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6491 uu_avl_insert(tree, node, idx);
6493 zfs_close(node->un_zhp);
6494 free(node->un_mountp);
6500 * Walk the AVL tree in reverse, unmounting each filesystem and
6501 * removing it from the AVL tree in the process.
6503 if ((walk = uu_avl_walk_start(tree,
6504 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6507 while ((node = uu_avl_walk_next(walk)) != NULL) {
6508 uu_avl_remove(tree, node);
6512 if (zfs_unshareall_bypath(node->un_zhp,
6513 node->un_mountp) != 0)
6518 if (zfs_unmount(node->un_zhp,
6519 node->un_mountp, flags) != 0)
6524 zfs_close(node->un_zhp);
6525 free(node->un_mountp);
6529 uu_avl_walk_end(walk);
6530 uu_avl_destroy(tree);
6531 uu_avl_pool_destroy(pool);
6536 (void) fprintf(stderr,
6537 gettext("missing filesystem argument\n"));
6539 (void) fprintf(stderr,
6540 gettext("too many arguments\n"));
6545 * We have an argument, but it may be a full path or a ZFS
6546 * filesystem. Pass full paths off to unmount_path() (shared by
6547 * manual_unmount), otherwise open the filesystem and pass to
6550 if (argv[0][0] == '/')
6551 return (unshare_unmount_path(op, argv[0],
6554 if ((zhp = zfs_open(g_zfs, argv[0],
6555 ZFS_TYPE_FILESYSTEM)) == NULL)
6558 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6559 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6560 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6561 NULL, 0, B_FALSE) == 0);
6565 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6567 sizeof (nfs_mnt_prop),
6568 NULL, NULL, 0, B_FALSE) == 0);
6569 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6570 sharesmb, sizeof (sharesmb), NULL, NULL,
6573 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6574 strcmp(sharesmb, "off") == 0) {
6575 (void) fprintf(stderr, gettext("cannot "
6576 "unshare '%s': legacy share\n"),
6579 (void) fprintf(stderr, gettext("use "
6580 "unshare(1M) to unshare this "
6584 } else if (!zfs_is_shared(zhp)) {
6585 (void) fprintf(stderr, gettext("cannot "
6586 "unshare '%s': not currently "
6587 "shared\n"), zfs_get_name(zhp));
6589 } else if (zfs_unshareall(zhp) != 0) {
6595 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6596 (void) fprintf(stderr, gettext("cannot "
6597 "unmount '%s': legacy "
6598 "mountpoint\n"), zfs_get_name(zhp));
6599 (void) fprintf(stderr, gettext("use "
6600 "umount(8) to unmount this "
6603 } else if (!zfs_is_mounted(zhp, NULL)) {
6604 (void) fprintf(stderr, gettext("cannot "
6605 "unmount '%s': not currently "
6609 } else if (zfs_unmountall(zhp, flags) != 0) {
6623 * zfs unmount filesystem
6625 * Unmount all filesystems, or a specific ZFS filesystem.
6628 zfs_do_unmount(int argc, char **argv)
6630 return (unshare_unmount(OP_MOUNT, argc, argv));
6635 * zfs unshare filesystem
6637 * Unshare all filesystems, or a specific ZFS filesystem.
6640 zfs_do_unshare(int argc, char **argv)
6642 return (unshare_unmount(OP_SHARE, argc, argv));
6646 * Attach/detach the given dataset to/from the given jail
6650 do_jail(int argc, char **argv, int attach)
6655 /* check number of arguments */
6657 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6661 (void) fprintf(stderr, gettext("too many arguments\n"));
6665 jailid = jail_getid(argv[1]);
6667 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6671 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6675 ret = (zfs_jail(zhp, jailid, attach) != 0);
6682 * zfs jail jailid filesystem
6684 * Attach the given dataset to the given jail
6688 zfs_do_jail(int argc, char **argv)
6691 return (do_jail(argc, argv, 1));
6695 * zfs unjail jailid filesystem
6697 * Detach the given dataset from the given jail
6701 zfs_do_unjail(int argc, char **argv)
6704 return (do_jail(argc, argv, 0));
6708 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6709 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6712 manual_mount(int argc, char **argv)
6715 char mountpoint[ZFS_MAXPROPLEN];
6716 char mntopts[MNT_LINE_MAX] = { '\0' };
6720 char *dataset, *path;
6723 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6726 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6729 flags |= MS_OVERLAY;
6732 flags |= MS_NOMNTTAB;
6735 (void) fprintf(stderr, gettext("missing argument for "
6736 "'%c' option\n"), optopt);
6740 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6742 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6751 /* check that we only have two arguments */
6754 (void) fprintf(stderr, gettext("missing dataset "
6757 (void) fprintf(stderr,
6758 gettext("missing mountpoint argument\n"));
6760 (void) fprintf(stderr, gettext("too many arguments\n"));
6761 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6768 /* try to open the dataset */
6769 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6772 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6773 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6775 /* check for legacy mountpoint and complain appropriately */
6777 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6778 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6779 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6780 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6785 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6786 "mounted using 'mount -t zfs'\n"), dataset);
6787 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6788 "instead.\n"), path);
6789 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6790 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6791 (void) fprintf(stderr, gettext("See zfs(8) for more "
6800 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6801 * unmounts of non-legacy filesystems, as this is the dominant administrative
6805 manual_unmount(int argc, char **argv)
6811 while ((c = getopt(argc, argv, "f")) != -1) {
6817 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6819 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6828 /* check arguments */
6831 (void) fprintf(stderr, gettext("missing path "
6834 (void) fprintf(stderr, gettext("too many arguments\n"));
6835 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6839 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6843 find_command_idx(char *command, int *idx)
6847 for (i = 0; i < NCOMMAND; i++) {
6848 if (command_table[i].name == NULL)
6851 if (strcmp(command, command_table[i].name) == 0) {
6860 zfs_do_diff(int argc, char **argv)
6864 char *tosnap = NULL;
6865 char *fromsnap = NULL;
6870 while ((c = getopt(argc, argv, "FHt")) != -1) {
6873 flags |= ZFS_DIFF_CLASSIFY;
6876 flags |= ZFS_DIFF_PARSEABLE;
6879 flags |= ZFS_DIFF_TIMESTAMP;
6882 (void) fprintf(stderr,
6883 gettext("invalid option '%c'\n"), optopt);
6892 (void) fprintf(stderr,
6893 gettext("must provide at least one snapshot name\n"));
6898 (void) fprintf(stderr, gettext("too many arguments\n"));
6903 tosnap = (argc == 2) ? argv[1] : NULL;
6906 if (*fromsnap != '@')
6907 copy = strdup(fromsnap);
6909 copy = strdup(tosnap);
6913 if (atp = strchr(copy, '@'))
6916 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6922 * Ignore SIGPIPE so that the library can give us
6923 * information on any failure
6925 (void) sigignore(SIGPIPE);
6927 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6935 * zfs bookmark <fs@snap> <fs#bmark>
6937 * Creates a bookmark with the given name from the given snapshot.
6940 zfs_do_bookmark(int argc, char **argv)
6942 char snapname[ZFS_MAXNAMELEN];
6949 while ((c = getopt(argc, argv, "")) != -1) {
6952 (void) fprintf(stderr,
6953 gettext("invalid option '%c'\n"), optopt);
6961 /* check number of arguments */
6963 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6967 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6971 if (strchr(argv[1], '#') == NULL) {
6972 (void) fprintf(stderr,
6973 gettext("invalid bookmark name '%s' -- "
6974 "must contain a '#'\n"), argv[1]);
6978 if (argv[0][0] == '@') {
6980 * Snapshot name begins with @.
6981 * Default to same fs as bookmark.
6983 (void) strncpy(snapname, argv[1], sizeof (snapname));
6984 *strchr(snapname, '#') = '\0';
6985 (void) strlcat(snapname, argv[0], sizeof (snapname));
6987 (void) strncpy(snapname, argv[0], sizeof (snapname));
6989 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
6995 nvl = fnvlist_alloc();
6996 fnvlist_add_string(nvl, argv[1], snapname);
6997 ret = lzc_bookmark(nvl, NULL);
7001 const char *err_msg;
7004 (void) snprintf(errbuf, sizeof (errbuf),
7005 dgettext(TEXT_DOMAIN,
7006 "cannot create bookmark '%s'"), argv[1]);
7010 err_msg = "bookmark is in a different pool";
7013 err_msg = "bookmark exists";
7016 err_msg = "invalid argument";
7019 err_msg = "bookmark feature not enabled";
7022 err_msg = "out of space";
7025 err_msg = "unknown error";
7028 (void) fprintf(stderr, "%s: %s\n", errbuf,
7029 dgettext(TEXT_DOMAIN, err_msg));
7040 main(int argc, char **argv)
7047 (void) setlocale(LC_ALL, "");
7048 (void) textdomain(TEXT_DOMAIN);
7052 if ((g_zfs = libzfs_init()) == NULL) {
7053 (void) fprintf(stderr, gettext("internal error: failed to "
7054 "initialize ZFS library\n"));
7058 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7060 libzfs_print_on_error(g_zfs, B_TRUE);
7062 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
7063 (void) fprintf(stderr, gettext("internal error: unable to "
7064 "open %s\n"), MNTTAB);
7069 * This command also doubles as the /etc/fs mount and unmount program.
7070 * Determine if we should take this behavior based on argv[0].
7072 progname = basename(argv[0]);
7073 if (strcmp(progname, "mount") == 0) {
7074 ret = manual_mount(argc, argv);
7075 } else if (strcmp(progname, "umount") == 0) {
7076 ret = manual_unmount(argc, argv);
7079 * Make sure the user has specified some command.
7082 (void) fprintf(stderr, gettext("missing command\n"));
7089 * The 'umount' command is an alias for 'unmount'
7091 if (strcmp(cmdname, "umount") == 0)
7092 cmdname = "unmount";
7095 * The 'recv' command is an alias for 'receive'
7097 if (strcmp(cmdname, "recv") == 0)
7098 cmdname = "receive";
7101 * The 'snap' command is an alias for 'snapshot'
7103 if (strcmp(cmdname, "snap") == 0)
7104 cmdname = "snapshot";
7109 if (strcmp(cmdname, "-?") == 0)
7113 * Run the appropriate command.
7115 libzfs_mnttab_cache(g_zfs, B_TRUE);
7116 if (find_command_idx(cmdname, &i) == 0) {
7117 current_command = &command_table[i];
7118 ret = command_table[i].func(argc - 1, argv + 1);
7119 } else if (strchr(cmdname, '=') != NULL) {
7120 verify(find_command_idx("set", &i) == 0);
7121 current_command = &command_table[i];
7122 ret = command_table[i].func(argc, argv);
7124 (void) fprintf(stderr, gettext("unrecognized "
7125 "command '%s'\n"), cmdname);
7128 libzfs_mnttab_cache(g_zfs, B_FALSE);
7131 (void) fclose(mnttab_file);
7133 if (ret == 0 && log_history)
7134 (void) zpool_log_history(g_zfs, history_str);
7139 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7140 * for the purposes of running ::findleaks.
7142 if (getenv("ZFS_ABORT") != NULL) {
7143 (void) printf("dumping core by request\n");