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.
32 * Copyright (c) 2014 Integros [integros.com]
33 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
42 #include <libnvpair.h>
55 #include <sys/mntent.h>
56 #include <sys/mnttab.h>
57 #include <sys/mount.h>
59 #include <sys/fs/zfs.h>
60 #include <sys/types.h>
66 #include <libzfs_core.h>
68 #include <zfs_deleg.h>
72 #include <directory.h>
78 #include "zfs_comutil.h"
80 libzfs_handle_t *g_zfs;
82 static FILE *mnttab_file;
83 static char history_str[HIS_MAX_RECORD_LEN];
84 static boolean_t log_history = B_TRUE;
86 static int zfs_do_clone(int argc, char **argv);
87 static int zfs_do_create(int argc, char **argv);
88 static int zfs_do_destroy(int argc, char **argv);
89 static int zfs_do_get(int argc, char **argv);
90 static int zfs_do_inherit(int argc, char **argv);
91 static int zfs_do_list(int argc, char **argv);
92 static int zfs_do_mount(int argc, char **argv);
93 static int zfs_do_rename(int argc, char **argv);
94 static int zfs_do_rollback(int argc, char **argv);
95 static int zfs_do_set(int argc, char **argv);
96 static int zfs_do_upgrade(int argc, char **argv);
97 static int zfs_do_snapshot(int argc, char **argv);
98 static int zfs_do_unmount(int argc, char **argv);
99 static int zfs_do_share(int argc, char **argv);
100 static int zfs_do_unshare(int argc, char **argv);
101 static int zfs_do_send(int argc, char **argv);
102 static int zfs_do_receive(int argc, char **argv);
103 static int zfs_do_promote(int argc, char **argv);
104 static int zfs_do_userspace(int argc, char **argv);
105 static int zfs_do_allow(int argc, char **argv);
106 static int zfs_do_unallow(int argc, char **argv);
107 static int zfs_do_hold(int argc, char **argv);
108 static int zfs_do_holds(int argc, char **argv);
109 static int zfs_do_release(int argc, char **argv);
110 static int zfs_do_diff(int argc, char **argv);
111 static int zfs_do_jail(int argc, char **argv);
112 static int zfs_do_unjail(int argc, char **argv);
113 static int zfs_do_bookmark(int argc, char **argv);
116 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
121 _umem_debug_init(void)
123 return ("default,verbose"); /* $UMEM_DEBUG setting */
127 _umem_logging_init(void)
129 return ("fail,contents"); /* $UMEM_LOGGING setting */
165 typedef struct zfs_command {
167 int (*func)(int argc, char **argv);
172 * Master command table. Each ZFS command has a name, associated function, and
173 * usage message. The usage messages need to be internationalized, so we have
174 * to have a function to return the usage message based on a command index.
176 * These commands are organized according to how they are displayed in the usage
177 * message. An empty command (one with a NULL name) indicates an empty line in
178 * the generic usage message.
180 static zfs_command_t command_table[] = {
181 { "create", zfs_do_create, HELP_CREATE },
182 { "destroy", zfs_do_destroy, HELP_DESTROY },
184 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
185 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
186 { "clone", zfs_do_clone, HELP_CLONE },
187 { "promote", zfs_do_promote, HELP_PROMOTE },
188 { "rename", zfs_do_rename, HELP_RENAME },
189 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
191 { "list", zfs_do_list, HELP_LIST },
193 { "set", zfs_do_set, HELP_SET },
194 { "get", zfs_do_get, HELP_GET },
195 { "inherit", zfs_do_inherit, HELP_INHERIT },
196 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
197 { "userspace", zfs_do_userspace, HELP_USERSPACE },
198 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
200 { "mount", zfs_do_mount, HELP_MOUNT },
201 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
202 { "share", zfs_do_share, HELP_SHARE },
203 { "unshare", zfs_do_unshare, HELP_UNSHARE },
205 { "send", zfs_do_send, HELP_SEND },
206 { "receive", zfs_do_receive, HELP_RECEIVE },
208 { "allow", zfs_do_allow, HELP_ALLOW },
210 { "unallow", zfs_do_unallow, HELP_UNALLOW },
212 { "hold", zfs_do_hold, HELP_HOLD },
213 { "holds", zfs_do_holds, HELP_HOLDS },
214 { "release", zfs_do_release, HELP_RELEASE },
215 { "diff", zfs_do_diff, HELP_DIFF },
217 { "jail", zfs_do_jail, HELP_JAIL },
218 { "unjail", zfs_do_unjail, HELP_UNJAIL },
221 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
223 zfs_command_t *current_command;
226 get_usage(zfs_help_t idx)
230 return (gettext("\tclone [-p] [-o property=value] ... "
231 "<snapshot> <filesystem|volume>\n"));
233 return (gettext("\tcreate [-pu] [-o property=value] ... "
235 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
236 "-V <size> <volume>\n"));
238 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
239 "\tdestroy [-dnpRrv] "
240 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
241 "\tdestroy <filesystem|volume>#<bookmark>\n"));
243 return (gettext("\tget [-rHp] [-d max] "
244 "[-o \"all\" | field[,...]]\n"
245 "\t [-t type[,...]] [-s source[,...]]\n"
246 "\t <\"all\" | property[,...]> "
247 "[filesystem|volume|snapshot] ...\n"));
249 return (gettext("\tinherit [-rS] <property> "
250 "<filesystem|volume|snapshot> ...\n"));
252 return (gettext("\tupgrade [-v]\n"
253 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
255 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
257 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
259 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
260 "[-s property]...\n\t [-S property]... [-t type[,...]] "
261 "[filesystem|volume|snapshot] ...\n"));
263 return (gettext("\tmount\n"
264 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
266 return (gettext("\tpromote <clone-filesystem>\n"));
268 return (gettext("\treceive|recv [-vnsFu] <filesystem|volume|"
270 "\treceive|recv [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
272 "\treceive|recv -A <filesystem|volume>\n"));
274 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
275 "<filesystem|volume|snapshot>\n"
276 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
277 "\trename -r <snapshot> <snapshot>\n"
278 "\trename -u [-p] <filesystem> <filesystem>"));
280 return (gettext("\trollback [-rRf] <snapshot>\n"));
282 return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] "
284 "\tsend [-Le] [-i snapshot|bookmark] "
285 "<filesystem|volume|snapshot>\n"
286 "\tsend [-nvPe] -t <receive_resume_token>\n"));
288 return (gettext("\tset <property=value> ... "
289 "<filesystem|volume|snapshot> ...\n"));
291 return (gettext("\tshare <-a | filesystem>\n"));
293 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
294 "<filesystem|volume>@<snap> ...\n"));
296 return (gettext("\tunmount|umount [-f] "
297 "<-a | filesystem|mountpoint>\n"));
299 return (gettext("\tunshare "
300 "<-a | filesystem|mountpoint>\n"));
302 return (gettext("\tallow <filesystem|volume>\n"
304 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
305 "\t <filesystem|volume>\n"
306 "\tallow [-ld] -e <perm|@setname>[,...] "
307 "<filesystem|volume>\n"
308 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
309 "\tallow -s @setname <perm|@setname>[,...] "
310 "<filesystem|volume>\n"));
312 return (gettext("\tunallow [-rldug] "
313 "<\"everyone\"|user|group>[,...]\n"
314 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
315 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
316 "<filesystem|volume>\n"
317 "\tunallow [-r] -c [<perm|@setname>[,...]] "
318 "<filesystem|volume>\n"
319 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
320 "<filesystem|volume>\n"));
322 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
324 "\t [-S field] ... [-t type[,...]] "
325 "<filesystem|snapshot>\n"));
326 case HELP_GROUPSPACE:
327 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
329 "\t [-S field] ... [-t type[,...]] "
330 "<filesystem|snapshot>\n"));
332 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
334 return (gettext("\tholds [-r] <snapshot> ...\n"));
336 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
338 return (gettext("\tdiff [-FHt] <snapshot> "
339 "[snapshot|filesystem]\n"));
341 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
351 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
356 * Utility function to guarantee malloc() success.
360 safe_malloc(size_t size)
364 if ((data = calloc(1, size)) == NULL)
371 safe_strdup(char *str)
373 char *dupstr = strdup(str);
382 * Callback routine that will print out information for each of
386 usage_prop_cb(int prop, void *cb)
390 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
392 if (zfs_prop_readonly(prop))
393 (void) fprintf(fp, " NO ");
395 (void) fprintf(fp, "YES ");
397 if (zfs_prop_inheritable(prop))
398 (void) fprintf(fp, " YES ");
400 (void) fprintf(fp, " NO ");
402 if (zfs_prop_values(prop) == NULL)
403 (void) fprintf(fp, "-\n");
405 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
411 * Display usage message. If we're inside a command, display only the usage for
412 * that command. Otherwise, iterate over the entire command table and display
413 * a complete usage message.
416 usage(boolean_t requested)
419 boolean_t show_properties = B_FALSE;
420 FILE *fp = requested ? stdout : stderr;
422 if (current_command == NULL) {
424 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
426 gettext("where 'command' is one of the following:\n\n"));
428 for (i = 0; i < NCOMMAND; i++) {
429 if (command_table[i].name == NULL)
430 (void) fprintf(fp, "\n");
432 (void) fprintf(fp, "%s",
433 get_usage(command_table[i].usage));
436 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
437 "pool/[dataset/]*dataset[@name]\n"));
439 (void) fprintf(fp, gettext("usage:\n"));
440 (void) fprintf(fp, "%s", get_usage(current_command->usage));
443 if (current_command != NULL &&
444 (strcmp(current_command->name, "set") == 0 ||
445 strcmp(current_command->name, "get") == 0 ||
446 strcmp(current_command->name, "inherit") == 0 ||
447 strcmp(current_command->name, "list") == 0))
448 show_properties = B_TRUE;
450 if (show_properties) {
452 gettext("\nThe following properties are supported:\n"));
454 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
455 "PROPERTY", "EDIT", "INHERIT", "VALUES");
457 /* Iterate over all properties */
458 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
461 (void) fprintf(fp, "\t%-15s ", "userused@...");
462 (void) fprintf(fp, " NO NO <size>\n");
463 (void) fprintf(fp, "\t%-15s ", "groupused@...");
464 (void) fprintf(fp, " NO NO <size>\n");
465 (void) fprintf(fp, "\t%-15s ", "userquota@...");
466 (void) fprintf(fp, "YES NO <size> | none\n");
467 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
468 (void) fprintf(fp, "YES NO <size> | none\n");
469 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
470 (void) fprintf(fp, " NO NO <size>\n");
472 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
473 "with standard units such as K, M, G, etc.\n"));
474 (void) fprintf(fp, gettext("\nUser-defined properties can "
475 "be specified by using a name containing a colon (:).\n"));
476 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
477 "properties must be appended with\n"
478 "a user or group specifier of one of these forms:\n"
479 " POSIX name (eg: \"matt\")\n"
480 " POSIX id (eg: \"126829\")\n"
481 " SMB name@domain (eg: \"matt@sun\")\n"
482 " SMB SID (eg: \"S-1-234-567-89\")\n"));
485 gettext("\nFor the property list, run: %s\n"),
488 gettext("\nFor the delegated permission list, run: %s\n"),
489 "zfs allow|unallow");
493 * See comments at end of main().
495 if (getenv("ZFS_ABORT") != NULL) {
496 (void) printf("dumping core by request\n");
500 exit(requested ? 0 : 2);
504 * Take a property=value argument string and add it to the given nvlist.
505 * Modifies the argument inplace.
508 parseprop(nvlist_t *props, char *propname)
510 char *propval, *strval;
512 if ((propval = strchr(propname, '=')) == NULL) {
513 (void) fprintf(stderr, gettext("missing "
514 "'=' for property=value argument\n"));
519 if (nvlist_lookup_string(props, propname, &strval) == 0) {
520 (void) fprintf(stderr, gettext("property '%s' "
521 "specified multiple times\n"), propname);
524 if (nvlist_add_string(props, propname, propval) != 0)
530 parse_depth(char *opt, int *flags)
535 depth = (int)strtol(opt, &tmp, 0);
537 (void) fprintf(stderr,
538 gettext("%s is not an integer\n"), opt);
542 (void) fprintf(stderr,
543 gettext("Depth can not be negative.\n"));
546 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
550 #define PROGRESS_DELAY 2 /* seconds */
552 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";
553 static time_t pt_begin;
554 static char *pt_header = NULL;
555 static boolean_t pt_shown;
558 start_progress_timer(void)
560 pt_begin = time(NULL) + PROGRESS_DELAY;
565 set_progress_header(char *header)
567 assert(pt_header == NULL);
568 pt_header = safe_strdup(header);
570 (void) printf("%s: ", header);
571 (void) fflush(stdout);
576 update_progress(char *update)
578 if (!pt_shown && time(NULL) > pt_begin) {
579 int len = strlen(update);
581 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
583 (void) fflush(stdout);
585 } else if (pt_shown) {
586 int len = strlen(update);
588 (void) printf("%s%*.*s", update, len, len, pt_reverse);
589 (void) fflush(stdout);
594 finish_progress(char *done)
597 (void) printf("%s\n", done);
598 (void) fflush(stdout);
605 * Check if the dataset is mountable and should be automatically mounted.
608 should_auto_mount(zfs_handle_t *zhp)
610 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp)))
612 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON);
616 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
618 * Given an existing dataset, create a writable copy whose initial contents
619 * are the same as the source. The newly created dataset maintains a
620 * dependency on the original; the original cannot be destroyed so long as
623 * The '-p' flag creates all the non-existing ancestors of the target first.
626 zfs_do_clone(int argc, char **argv)
628 zfs_handle_t *zhp = NULL;
629 boolean_t parents = B_FALSE;
634 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
638 while ((c = getopt(argc, argv, "o:p")) != -1) {
641 if (parseprop(props, optarg) != 0)
648 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
657 /* check number of arguments */
659 (void) fprintf(stderr, gettext("missing source dataset "
664 (void) fprintf(stderr, gettext("missing target dataset "
669 (void) fprintf(stderr, gettext("too many arguments\n"));
673 /* open the source dataset */
674 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
677 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
680 * Now create the ancestors of the target dataset. If the
681 * target already exists and '-p' option was used we should not
684 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
687 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
692 ret = zfs_clone(zhp, argv[1], props);
694 /* create the mountpoint if necessary */
698 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
701 * If the user doesn't want the dataset
702 * automatically mounted, then skip the mount/share
705 if (should_auto_mount(clone)) {
706 if ((ret = zfs_mount(clone, NULL, 0)) != 0) {
707 (void) fprintf(stderr, gettext("clone "
708 "successfully created, "
709 "but not mounted\n"));
710 } else if ((ret = zfs_share(clone)) != 0) {
711 (void) fprintf(stderr, gettext("clone "
712 "successfully created, "
713 "but not shared\n"));
734 * zfs create [-pu] [-o prop=value] ... fs
735 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
737 * Create a new dataset. This command can be used to create filesystems
738 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
739 * For volumes, the user must specify a size to be used.
741 * The '-s' flag applies only to volumes, and indicates that we should not try
742 * to set the reservation for this volume. By default we set a reservation
743 * equal to the size for any volume. For pools with SPA_VERSION >=
744 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
746 * The '-p' flag creates all the non-existing ancestors of the target first.
748 * The '-u' flag prevents mounting of newly created file system.
751 zfs_do_create(int argc, char **argv)
753 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
754 zfs_handle_t *zhp = NULL;
755 uint64_t volsize = 0;
757 boolean_t noreserve = B_FALSE;
758 boolean_t bflag = B_FALSE;
759 boolean_t parents = B_FALSE;
760 boolean_t nomount = B_FALSE;
765 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
769 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) {
772 type = ZFS_TYPE_VOLUME;
773 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
774 (void) fprintf(stderr, gettext("bad volume "
775 "size '%s': %s\n"), optarg,
776 libzfs_error_description(g_zfs));
780 if (nvlist_add_uint64(props,
781 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
790 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
791 (void) fprintf(stderr, gettext("bad volume "
792 "block size '%s': %s\n"), optarg,
793 libzfs_error_description(g_zfs));
797 if (nvlist_add_uint64(props,
798 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
803 if (parseprop(props, optarg) != 0)
813 (void) fprintf(stderr, gettext("missing size "
817 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
823 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
824 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
825 "used when creating a volume\n"));
828 if (nomount && type != ZFS_TYPE_FILESYSTEM) {
829 (void) fprintf(stderr, gettext("'-u' can only be "
830 "used when creating a file system\n"));
837 /* check number of arguments */
839 (void) fprintf(stderr, gettext("missing %s argument\n"),
840 zfs_type_to_name(type));
844 (void) fprintf(stderr, gettext("too many arguments\n"));
848 if (type == ZFS_TYPE_VOLUME && !noreserve) {
849 zpool_handle_t *zpool_handle;
850 nvlist_t *real_props = NULL;
851 uint64_t spa_version;
853 zfs_prop_t resv_prop;
857 if ((p = strchr(argv[0], '/')) != NULL)
859 zpool_handle = zpool_open(g_zfs, argv[0]);
862 if (zpool_handle == NULL)
864 spa_version = zpool_get_prop_int(zpool_handle,
865 ZPOOL_PROP_VERSION, NULL);
866 if (spa_version >= SPA_VERSION_REFRESERVATION)
867 resv_prop = ZFS_PROP_REFRESERVATION;
869 resv_prop = ZFS_PROP_RESERVATION;
871 (void) snprintf(msg, sizeof (msg),
872 gettext("cannot create '%s'"), argv[0]);
873 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
874 props, 0, NULL, zpool_handle, msg)) == NULL) {
875 zpool_close(zpool_handle);
878 zpool_close(zpool_handle);
880 volsize = zvol_volsize_to_reservation(volsize, real_props);
881 nvlist_free(real_props);
883 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
885 if (nvlist_add_uint64(props,
886 zfs_prop_to_name(resv_prop), volsize) != 0) {
893 if (parents && zfs_name_valid(argv[0], type)) {
895 * Now create the ancestors of target dataset. If the target
896 * already exists and '-p' option was used we should not
899 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
903 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
908 if (zfs_create(g_zfs, argv[0], type, props) != 0)
911 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
917 * Mount and/or share the new filesystem as appropriate. We provide a
918 * verbose error message to let the user know that their filesystem was
919 * in fact created, even if we failed to mount or share it.
920 * If the user doesn't want the dataset automatically mounted,
921 * then skip the mount/share step altogether.
923 if (!nomount && should_auto_mount(zhp)) {
924 if (zfs_mount(zhp, NULL, 0) != 0) {
925 (void) fprintf(stderr, gettext("filesystem "
926 "successfully created, but not mounted\n"));
928 } else if (zfs_share(zhp) != 0) {
929 (void) fprintf(stderr, gettext("filesystem "
930 "successfully created, but not shared\n"));
947 * zfs destroy [-rRf] <fs, vol>
948 * zfs destroy [-rRd] <snap>
950 * -r Recursively destroy all children
951 * -R Recursively destroy all dependents, including clones
952 * -f Force unmounting of any dependents
953 * -d If we can't destroy now, mark for deferred destruction
955 * Destroys the given dataset. By default, it will unmount any filesystems,
956 * and refuse to destroy a dataset that has any dependents. A dependent can
957 * either be a child, or a clone of a child.
959 typedef struct destroy_cbdata {
962 boolean_t cb_recurse;
964 boolean_t cb_doclones;
965 zfs_handle_t *cb_target;
966 boolean_t cb_defer_destroy;
967 boolean_t cb_verbose;
968 boolean_t cb_parsable;
971 nvlist_t *cb_batchedsnaps;
973 /* first snap in contiguous run */
975 /* previous snap in contiguous run */
983 * Check for any dependents based on the '-r' or '-R' flags.
986 destroy_check_dependent(zfs_handle_t *zhp, void *data)
988 destroy_cbdata_t *cbp = data;
989 const char *tname = zfs_get_name(cbp->cb_target);
990 const char *name = zfs_get_name(zhp);
992 if (strncmp(tname, name, strlen(tname)) == 0 &&
993 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
995 * This is a direct descendant, not a clone somewhere else in
1001 if (cbp->cb_first) {
1002 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1003 "%s has children\n"),
1004 zfs_get_name(cbp->cb_target),
1005 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1006 (void) fprintf(stderr, gettext("use '-r' to destroy "
1007 "the following datasets:\n"));
1008 cbp->cb_first = B_FALSE;
1009 cbp->cb_error = B_TRUE;
1012 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1015 * This is a clone. We only want to report this if the '-r'
1016 * wasn't specified, or the target is a snapshot.
1018 if (!cbp->cb_recurse &&
1019 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1022 if (cbp->cb_first) {
1023 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1024 "%s has dependent clones\n"),
1025 zfs_get_name(cbp->cb_target),
1026 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1027 (void) fprintf(stderr, gettext("use '-R' to destroy "
1028 "the following datasets:\n"));
1029 cbp->cb_first = B_FALSE;
1030 cbp->cb_error = B_TRUE;
1031 cbp->cb_dryrun = B_TRUE;
1034 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1043 destroy_callback(zfs_handle_t *zhp, void *data)
1045 destroy_cbdata_t *cb = data;
1046 const char *name = zfs_get_name(zhp);
1048 if (cb->cb_verbose) {
1049 if (cb->cb_parsable) {
1050 (void) printf("destroy\t%s\n", name);
1051 } else if (cb->cb_dryrun) {
1052 (void) printf(gettext("would destroy %s\n"),
1055 (void) printf(gettext("will destroy %s\n"),
1061 * Ignore pools (which we've already flagged as an error before getting
1064 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1065 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1069 if (cb->cb_dryrun) {
1075 * We batch up all contiguous snapshots (even of different
1076 * filesystems) and destroy them with one ioctl. We can't
1077 * simply do all snap deletions and then all fs deletions,
1078 * because we must delete a clone before its origin.
1080 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1081 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1083 int error = zfs_destroy_snaps_nvl(g_zfs,
1084 cb->cb_batchedsnaps, B_FALSE);
1085 fnvlist_free(cb->cb_batchedsnaps);
1086 cb->cb_batchedsnaps = fnvlist_alloc();
1089 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1090 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1101 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1103 destroy_cbdata_t *cb = arg;
1104 const char *name = zfs_get_name(zhp);
1107 if (nvlist_exists(cb->cb_nvl, name)) {
1108 if (cb->cb_firstsnap == NULL)
1109 cb->cb_firstsnap = strdup(name);
1110 if (cb->cb_prevsnap != NULL)
1111 free(cb->cb_prevsnap);
1112 /* this snap continues the current range */
1113 cb->cb_prevsnap = strdup(name);
1114 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1116 if (cb->cb_verbose) {
1117 if (cb->cb_parsable) {
1118 (void) printf("destroy\t%s\n", name);
1119 } else if (cb->cb_dryrun) {
1120 (void) printf(gettext("would destroy %s\n"),
1123 (void) printf(gettext("will destroy %s\n"),
1127 } else if (cb->cb_firstsnap != NULL) {
1128 /* end of this range */
1130 err = lzc_snaprange_space(cb->cb_firstsnap,
1131 cb->cb_prevsnap, &used);
1132 cb->cb_snapused += used;
1133 free(cb->cb_firstsnap);
1134 cb->cb_firstsnap = NULL;
1135 free(cb->cb_prevsnap);
1136 cb->cb_prevsnap = NULL;
1143 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1146 assert(cb->cb_firstsnap == NULL);
1147 assert(cb->cb_prevsnap == NULL);
1148 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1149 if (cb->cb_firstsnap != NULL) {
1152 err = lzc_snaprange_space(cb->cb_firstsnap,
1153 cb->cb_prevsnap, &used);
1155 cb->cb_snapused += used;
1156 free(cb->cb_firstsnap);
1157 cb->cb_firstsnap = NULL;
1158 free(cb->cb_prevsnap);
1159 cb->cb_prevsnap = NULL;
1165 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1167 destroy_cbdata_t *cb = arg;
1170 /* Check for clones. */
1171 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1172 cb->cb_target = zhp;
1173 cb->cb_first = B_TRUE;
1174 err = zfs_iter_dependents(zhp, B_TRUE,
1175 destroy_check_dependent, cb);
1179 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1187 gather_snapshots(zfs_handle_t *zhp, void *arg)
1189 destroy_cbdata_t *cb = arg;
1192 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1198 if (cb->cb_verbose) {
1199 err = destroy_print_snapshots(zhp, cb);
1205 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1213 destroy_clones(destroy_cbdata_t *cb)
1216 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1218 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1219 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1222 boolean_t defer = cb->cb_defer_destroy;
1226 * We can't defer destroy non-snapshots, so set it to
1227 * false while destroying the clones.
1229 cb->cb_defer_destroy = B_FALSE;
1230 err = zfs_iter_dependents(zhp, B_FALSE,
1231 destroy_callback, cb);
1232 cb->cb_defer_destroy = defer;
1242 zfs_do_destroy(int argc, char **argv)
1244 destroy_cbdata_t cb = { 0 };
1248 zfs_handle_t *zhp = NULL;
1250 zfs_type_t type = ZFS_TYPE_DATASET;
1253 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1256 cb.cb_verbose = B_TRUE;
1259 cb.cb_verbose = B_TRUE;
1260 cb.cb_parsable = B_TRUE;
1263 cb.cb_dryrun = B_TRUE;
1266 cb.cb_defer_destroy = B_TRUE;
1267 type = ZFS_TYPE_SNAPSHOT;
1270 cb.cb_force = B_TRUE;
1273 cb.cb_recurse = B_TRUE;
1276 cb.cb_recurse = B_TRUE;
1277 cb.cb_doclones = B_TRUE;
1281 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1290 /* check number of arguments */
1292 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1296 (void) fprintf(stderr, gettext("too many arguments\n"));
1300 at = strchr(argv[0], '@');
1301 pound = strchr(argv[0], '#');
1304 /* Build the list of snaps to destroy in cb_nvl. */
1305 cb.cb_nvl = fnvlist_alloc();
1308 zhp = zfs_open(g_zfs, argv[0],
1309 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1313 cb.cb_snapspec = at + 1;
1314 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1320 if (nvlist_empty(cb.cb_nvl)) {
1321 (void) fprintf(stderr, gettext("could not find any "
1322 "snapshots to destroy; check snapshot names.\n"));
1327 if (cb.cb_verbose) {
1329 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1330 if (cb.cb_parsable) {
1331 (void) printf("reclaim\t%llu\n",
1333 } else if (cb.cb_dryrun) {
1334 (void) printf(gettext("would reclaim %s\n"),
1337 (void) printf(gettext("will reclaim %s\n"),
1342 if (!cb.cb_dryrun) {
1343 if (cb.cb_doclones) {
1344 cb.cb_batchedsnaps = fnvlist_alloc();
1345 err = destroy_clones(&cb);
1347 err = zfs_destroy_snaps_nvl(g_zfs,
1348 cb.cb_batchedsnaps, B_FALSE);
1356 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1357 cb.cb_defer_destroy);
1363 } else if (pound != NULL) {
1368 (void) fprintf(stderr,
1369 "dryrun is not supported with bookmark\n");
1373 if (cb.cb_defer_destroy) {
1374 (void) fprintf(stderr,
1375 "defer destroy is not supported with bookmark\n");
1379 if (cb.cb_recurse) {
1380 (void) fprintf(stderr,
1381 "recursive is not supported with bookmark\n");
1385 if (!zfs_bookmark_exists(argv[0])) {
1386 (void) fprintf(stderr, gettext("bookmark '%s' "
1387 "does not exist.\n"), argv[0]);
1391 nvl = fnvlist_alloc();
1392 fnvlist_add_boolean(nvl, argv[0]);
1394 err = lzc_destroy_bookmarks(nvl, NULL);
1396 (void) zfs_standard_error(g_zfs, err,
1397 "cannot destroy bookmark");
1400 nvlist_free(cb.cb_nvl);
1404 /* Open the given dataset */
1405 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1411 * Perform an explicit check for pools before going any further.
1413 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1414 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1415 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1416 "operation does not apply to pools\n"),
1418 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1419 "%s' to destroy all datasets in the pool\n"),
1421 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1422 "to destroy the pool itself\n"), zfs_get_name(zhp));
1428 * Check for any dependents and/or clones.
1430 cb.cb_first = B_TRUE;
1431 if (!cb.cb_doclones &&
1432 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1443 cb.cb_batchedsnaps = fnvlist_alloc();
1444 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1451 * Do the real thing. The callback will close the
1452 * handle regardless of whether it succeeds or not.
1454 err = destroy_callback(zhp, &cb);
1457 err = zfs_destroy_snaps_nvl(g_zfs,
1458 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1465 fnvlist_free(cb.cb_batchedsnaps);
1466 fnvlist_free(cb.cb_nvl);
1473 is_recvd_column(zprop_get_cbdata_t *cbp)
1476 zfs_get_column_t col;
1478 for (i = 0; i < ZFS_GET_NCOLS &&
1479 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1480 if (col == GET_COL_RECVD)
1486 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1487 * < all | property[,property]... > < fs | snap | vol > ...
1489 * -r recurse over any child datasets
1490 * -H scripted mode. Headers are stripped, and fields are separated
1491 * by tabs instead of spaces.
1492 * -o Set of fields to display. One of "name,property,value,
1493 * received,source". Default is "name,property,value,source".
1494 * "all" is an alias for all five.
1495 * -s Set of sources to allow. One of
1496 * "local,default,inherited,received,temporary,none". Default is
1498 * -p Display values in parsable (literal) format.
1500 * Prints properties for the given datasets. The user can control which
1501 * columns to display as well as which property types to allow.
1505 * Invoked to display the properties for a single dataset.
1508 get_callback(zfs_handle_t *zhp, void *data)
1510 char buf[ZFS_MAXPROPLEN];
1511 char rbuf[ZFS_MAXPROPLEN];
1512 zprop_source_t sourcetype;
1513 char source[ZFS_MAXNAMELEN];
1514 zprop_get_cbdata_t *cbp = data;
1515 nvlist_t *user_props = zfs_get_user_props(zhp);
1516 zprop_list_t *pl = cbp->cb_proplist;
1520 boolean_t received = is_recvd_column(cbp);
1522 for (; pl != NULL; pl = pl->pl_next) {
1523 char *recvdval = NULL;
1525 * Skip the special fake placeholder. This will also skip over
1526 * the name property when 'all' is specified.
1528 if (pl->pl_prop == ZFS_PROP_NAME &&
1529 pl == cbp->cb_proplist)
1532 if (pl->pl_prop != ZPROP_INVAL) {
1533 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1534 sizeof (buf), &sourcetype, source,
1536 cbp->cb_literal) != 0) {
1539 if (!zfs_prop_valid_for_type(pl->pl_prop,
1540 ZFS_TYPE_DATASET)) {
1541 (void) fprintf(stderr,
1542 gettext("No such property '%s'\n"),
1543 zfs_prop_to_name(pl->pl_prop));
1546 sourcetype = ZPROP_SRC_NONE;
1547 (void) strlcpy(buf, "-", sizeof (buf));
1550 if (received && (zfs_prop_get_recvd(zhp,
1551 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1552 cbp->cb_literal) == 0))
1555 zprop_print_one_property(zfs_get_name(zhp), cbp,
1556 zfs_prop_to_name(pl->pl_prop),
1557 buf, sourcetype, source, recvdval);
1558 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1559 sourcetype = ZPROP_SRC_LOCAL;
1561 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1562 buf, sizeof (buf), cbp->cb_literal) != 0) {
1563 sourcetype = ZPROP_SRC_NONE;
1564 (void) strlcpy(buf, "-", sizeof (buf));
1567 zprop_print_one_property(zfs_get_name(zhp), cbp,
1568 pl->pl_user_prop, buf, sourcetype, source, NULL);
1569 } else if (zfs_prop_written(pl->pl_user_prop)) {
1570 sourcetype = ZPROP_SRC_LOCAL;
1572 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1573 buf, sizeof (buf), cbp->cb_literal) != 0) {
1574 sourcetype = ZPROP_SRC_NONE;
1575 (void) strlcpy(buf, "-", sizeof (buf));
1578 zprop_print_one_property(zfs_get_name(zhp), cbp,
1579 pl->pl_user_prop, buf, sourcetype, source, NULL);
1581 if (nvlist_lookup_nvlist(user_props,
1582 pl->pl_user_prop, &propval) != 0) {
1585 sourcetype = ZPROP_SRC_NONE;
1588 verify(nvlist_lookup_string(propval,
1589 ZPROP_VALUE, &strval) == 0);
1590 verify(nvlist_lookup_string(propval,
1591 ZPROP_SOURCE, &sourceval) == 0);
1593 if (strcmp(sourceval,
1594 zfs_get_name(zhp)) == 0) {
1595 sourcetype = ZPROP_SRC_LOCAL;
1596 } else if (strcmp(sourceval,
1597 ZPROP_SOURCE_VAL_RECVD) == 0) {
1598 sourcetype = ZPROP_SRC_RECEIVED;
1600 sourcetype = ZPROP_SRC_INHERITED;
1601 (void) strlcpy(source,
1602 sourceval, sizeof (source));
1606 if (received && (zfs_prop_get_recvd(zhp,
1607 pl->pl_user_prop, rbuf, sizeof (rbuf),
1608 cbp->cb_literal) == 0))
1611 zprop_print_one_property(zfs_get_name(zhp), cbp,
1612 pl->pl_user_prop, strval, sourcetype,
1621 zfs_do_get(int argc, char **argv)
1623 zprop_get_cbdata_t cb = { 0 };
1624 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1625 int types = ZFS_TYPE_DATASET;
1626 char *value, *fields;
1629 zprop_list_t fake_name = { 0 };
1632 * Set up default columns and sources.
1634 cb.cb_sources = ZPROP_SRC_ALL;
1635 cb.cb_columns[0] = GET_COL_NAME;
1636 cb.cb_columns[1] = GET_COL_PROPERTY;
1637 cb.cb_columns[2] = GET_COL_VALUE;
1638 cb.cb_columns[3] = GET_COL_SOURCE;
1639 cb.cb_type = ZFS_TYPE_DATASET;
1642 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1645 cb.cb_literal = B_TRUE;
1648 limit = parse_depth(optarg, &flags);
1651 flags |= ZFS_ITER_RECURSE;
1654 cb.cb_scripted = B_TRUE;
1657 (void) fprintf(stderr, gettext("missing argument for "
1658 "'%c' option\n"), optopt);
1663 * Process the set of columns to display. We zero out
1664 * the structure to give us a blank slate.
1666 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1668 while (*optarg != '\0') {
1669 static char *col_subopts[] =
1670 { "name", "property", "value", "received",
1671 "source", "all", NULL };
1673 if (i == ZFS_GET_NCOLS) {
1674 (void) fprintf(stderr, gettext("too "
1675 "many fields given to -o "
1680 switch (getsubopt(&optarg, col_subopts,
1683 cb.cb_columns[i++] = GET_COL_NAME;
1686 cb.cb_columns[i++] = GET_COL_PROPERTY;
1689 cb.cb_columns[i++] = GET_COL_VALUE;
1692 cb.cb_columns[i++] = GET_COL_RECVD;
1693 flags |= ZFS_ITER_RECVD_PROPS;
1696 cb.cb_columns[i++] = GET_COL_SOURCE;
1700 (void) fprintf(stderr,
1701 gettext("\"all\" conflicts "
1702 "with specific fields "
1703 "given to -o option\n"));
1706 cb.cb_columns[0] = GET_COL_NAME;
1707 cb.cb_columns[1] = GET_COL_PROPERTY;
1708 cb.cb_columns[2] = GET_COL_VALUE;
1709 cb.cb_columns[3] = GET_COL_RECVD;
1710 cb.cb_columns[4] = GET_COL_SOURCE;
1711 flags |= ZFS_ITER_RECVD_PROPS;
1715 (void) fprintf(stderr,
1716 gettext("invalid column name "
1717 "'%s'\n"), suboptarg);
1725 while (*optarg != '\0') {
1726 static char *source_subopts[] = {
1727 "local", "default", "inherited",
1728 "received", "temporary", "none",
1731 switch (getsubopt(&optarg, source_subopts,
1734 cb.cb_sources |= ZPROP_SRC_LOCAL;
1737 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1740 cb.cb_sources |= ZPROP_SRC_INHERITED;
1743 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1746 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1749 cb.cb_sources |= ZPROP_SRC_NONE;
1752 (void) fprintf(stderr,
1753 gettext("invalid source "
1754 "'%s'\n"), suboptarg);
1762 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1763 while (*optarg != '\0') {
1764 static char *type_subopts[] = { "filesystem",
1765 "volume", "snapshot", "bookmark",
1768 switch (getsubopt(&optarg, type_subopts,
1771 types |= ZFS_TYPE_FILESYSTEM;
1774 types |= ZFS_TYPE_VOLUME;
1777 types |= ZFS_TYPE_SNAPSHOT;
1780 types |= ZFS_TYPE_BOOKMARK;
1783 types = ZFS_TYPE_DATASET |
1788 (void) fprintf(stderr,
1789 gettext("invalid type '%s'\n"),
1797 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1807 (void) fprintf(stderr, gettext("missing property "
1814 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1822 * As part of zfs_expand_proplist(), we keep track of the maximum column
1823 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1824 * need to know the maximum name length. However, the user likely did
1825 * not specify 'name' as one of the properties to fetch, so we need to
1826 * make sure we always include at least this property for
1827 * print_get_headers() to work properly.
1829 if (cb.cb_proplist != NULL) {
1830 fake_name.pl_prop = ZFS_PROP_NAME;
1831 fake_name.pl_width = strlen(gettext("NAME"));
1832 fake_name.pl_next = cb.cb_proplist;
1833 cb.cb_proplist = &fake_name;
1836 cb.cb_first = B_TRUE;
1838 /* run for each object */
1839 ret = zfs_for_each(argc, argv, flags, types, NULL,
1840 &cb.cb_proplist, limit, get_callback, &cb);
1842 if (cb.cb_proplist == &fake_name)
1843 zprop_free_list(fake_name.pl_next);
1845 zprop_free_list(cb.cb_proplist);
1851 * inherit [-rS] <property> <fs|vol> ...
1853 * -r Recurse over all children
1854 * -S Revert to received value, if any
1856 * For each dataset specified on the command line, inherit the given property
1857 * from its parent. Inheriting a property at the pool level will cause it to
1858 * use the default value. The '-r' flag will recurse over all children, and is
1859 * useful for setting a property on a hierarchy-wide basis, regardless of any
1860 * local modifications for each dataset.
1863 typedef struct inherit_cbdata {
1864 const char *cb_propname;
1865 boolean_t cb_received;
1869 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1871 inherit_cbdata_t *cb = data;
1872 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1875 * If we're doing it recursively, then ignore properties that
1876 * are not valid for this type of dataset.
1878 if (prop != ZPROP_INVAL &&
1879 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1882 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1886 inherit_cb(zfs_handle_t *zhp, void *data)
1888 inherit_cbdata_t *cb = data;
1890 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1894 zfs_do_inherit(int argc, char **argv)
1898 inherit_cbdata_t cb = { 0 };
1902 boolean_t received = B_FALSE;
1905 while ((c = getopt(argc, argv, "rS")) != -1) {
1908 flags |= ZFS_ITER_RECURSE;
1915 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1924 /* check number of arguments */
1926 (void) fprintf(stderr, gettext("missing property argument\n"));
1930 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1938 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1939 if (zfs_prop_readonly(prop)) {
1940 (void) fprintf(stderr, gettext(
1941 "%s property is read-only\n"),
1945 if (!zfs_prop_inheritable(prop) && !received) {
1946 (void) fprintf(stderr, gettext("'%s' property cannot "
1947 "be inherited\n"), propname);
1948 if (prop == ZFS_PROP_QUOTA ||
1949 prop == ZFS_PROP_RESERVATION ||
1950 prop == ZFS_PROP_REFQUOTA ||
1951 prop == ZFS_PROP_REFRESERVATION) {
1952 (void) fprintf(stderr, gettext("use 'zfs set "
1953 "%s=none' to clear\n"), propname);
1954 (void) fprintf(stderr, gettext("use 'zfs "
1955 "inherit -S %s' to revert to received "
1956 "value\n"), propname);
1960 if (received && (prop == ZFS_PROP_VOLSIZE ||
1961 prop == ZFS_PROP_VERSION)) {
1962 (void) fprintf(stderr, gettext("'%s' property cannot "
1963 "be reverted to a received value\n"), propname);
1966 } else if (!zfs_prop_user(propname)) {
1967 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1972 cb.cb_propname = propname;
1973 cb.cb_received = received;
1975 if (flags & ZFS_ITER_RECURSE) {
1976 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1977 NULL, NULL, 0, inherit_recurse_cb, &cb);
1979 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1980 NULL, NULL, 0, inherit_cb, &cb);
1986 typedef struct upgrade_cbdata {
1987 uint64_t cb_numupgraded;
1988 uint64_t cb_numsamegraded;
1989 uint64_t cb_numfailed;
1990 uint64_t cb_version;
1992 boolean_t cb_foundone;
1993 char cb_lastfs[ZFS_MAXNAMELEN];
1997 same_pool(zfs_handle_t *zhp, const char *name)
1999 int len1 = strcspn(name, "/@");
2000 const char *zhname = zfs_get_name(zhp);
2001 int len2 = strcspn(zhname, "/@");
2005 return (strncmp(name, zhname, len1) == 0);
2009 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2011 upgrade_cbdata_t *cb = data;
2012 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2014 /* list if it's old/new */
2015 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2016 (cb->cb_newer && version > ZPL_VERSION)) {
2019 str = gettext("The following filesystems are "
2020 "formatted using a newer software version and\n"
2021 "cannot be accessed on the current system.\n\n");
2023 str = gettext("The following filesystems are "
2024 "out of date, and can be upgraded. After being\n"
2025 "upgraded, these filesystems (and any 'zfs send' "
2026 "streams generated from\n"
2027 "subsequent snapshots) will no longer be "
2028 "accessible by older software versions.\n\n");
2031 if (!cb->cb_foundone) {
2033 (void) printf(gettext("VER FILESYSTEM\n"));
2034 (void) printf(gettext("--- ------------\n"));
2035 cb->cb_foundone = B_TRUE;
2038 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2045 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2047 upgrade_cbdata_t *cb = data;
2048 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2049 int needed_spa_version;
2052 if (zfs_spa_version(zhp, &spa_version) < 0)
2055 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2057 if (needed_spa_version < 0)
2060 if (spa_version < needed_spa_version) {
2062 (void) printf(gettext("%s: can not be "
2063 "upgraded; the pool version needs to first "
2064 "be upgraded\nto version %d\n\n"),
2065 zfs_get_name(zhp), needed_spa_version);
2071 if (version < cb->cb_version) {
2073 (void) snprintf(verstr, sizeof (verstr),
2074 "%llu", cb->cb_version);
2075 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2077 * If they did "zfs upgrade -a", then we could
2078 * be doing ioctls to different pools. We need
2079 * to log this history once to each pool, and bypass
2080 * the normal history logging that happens in main().
2082 (void) zpool_log_history(g_zfs, history_str);
2083 log_history = B_FALSE;
2085 if (zfs_prop_set(zhp, "version", verstr) == 0)
2086 cb->cb_numupgraded++;
2089 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2090 } else if (version > cb->cb_version) {
2091 /* can't downgrade */
2092 (void) printf(gettext("%s: can not be downgraded; "
2093 "it is already at version %u\n"),
2094 zfs_get_name(zhp), version);
2097 cb->cb_numsamegraded++;
2105 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2108 zfs_do_upgrade(int argc, char **argv)
2110 boolean_t all = B_FALSE;
2111 boolean_t showversions = B_FALSE;
2113 upgrade_cbdata_t cb = { 0 };
2115 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2118 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2121 flags |= ZFS_ITER_RECURSE;
2124 showversions = B_TRUE;
2127 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2128 optarg, &cb.cb_version) != 0) {
2129 (void) fprintf(stderr,
2130 gettext("invalid version %s\n"), optarg);
2139 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2148 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2150 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2151 cb.cb_version || argc))
2153 if ((all || argc) && (showversions))
2159 /* Show info on available versions. */
2160 (void) printf(gettext("The following filesystem versions are "
2162 (void) printf(gettext("VER DESCRIPTION\n"));
2163 (void) printf("--- -----------------------------------------"
2164 "---------------\n");
2165 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2166 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2167 (void) printf(gettext(" 3 Case insensitive and filesystem "
2168 "user identifier (FUID)\n"));
2169 (void) printf(gettext(" 4 userquota, groupquota "
2171 (void) printf(gettext(" 5 System attributes\n"));
2172 (void) printf(gettext("\nFor more information on a particular "
2173 "version, including supported releases,\n"));
2174 (void) printf("see the ZFS Administration Guide.\n\n");
2176 } else if (argc || all) {
2177 /* Upgrade filesystems */
2178 if (cb.cb_version == 0)
2179 cb.cb_version = ZPL_VERSION;
2180 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2181 NULL, NULL, 0, upgrade_set_callback, &cb);
2182 (void) printf(gettext("%llu filesystems upgraded\n"),
2184 if (cb.cb_numsamegraded) {
2185 (void) printf(gettext("%llu filesystems already at "
2187 cb.cb_numsamegraded);
2189 if (cb.cb_numfailed != 0)
2192 /* List old-version filesytems */
2194 (void) printf(gettext("This system is currently running "
2195 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2197 flags |= ZFS_ITER_RECURSE;
2198 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2199 NULL, NULL, 0, upgrade_list_callback, &cb);
2201 found = cb.cb_foundone;
2202 cb.cb_foundone = B_FALSE;
2203 cb.cb_newer = B_TRUE;
2205 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2206 NULL, NULL, 0, upgrade_list_callback, &cb);
2208 if (!cb.cb_foundone && !found) {
2209 (void) printf(gettext("All filesystems are "
2210 "formatted with the current version.\n"));
2218 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2219 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2220 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2221 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2223 * -H Scripted mode; elide headers and separate columns by tabs.
2224 * -i Translate SID to POSIX ID.
2225 * -n Print numeric ID instead of user/group name.
2226 * -o Control which fields to display.
2227 * -p Use exact (parsable) numeric output.
2228 * -s Specify sort columns, descending order.
2229 * -S Specify sort columns, ascending order.
2230 * -t Control which object types to display.
2232 * Displays space consumed by, and quotas on, each user in the specified
2233 * filesystem or snapshot.
2236 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2237 enum us_field_types {
2243 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2244 static char *us_field_names[] = { "type", "name", "used", "quota" };
2245 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2247 #define USTYPE_PSX_GRP (1 << 0)
2248 #define USTYPE_PSX_USR (1 << 1)
2249 #define USTYPE_SMB_GRP (1 << 2)
2250 #define USTYPE_SMB_USR (1 << 3)
2251 #define USTYPE_ALL \
2252 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2254 static int us_type_bits[] = {
2261 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2264 typedef struct us_node {
2266 uu_avl_node_t usn_avlnode;
2267 uu_list_node_t usn_listnode;
2270 typedef struct us_cbdata {
2272 uu_avl_pool_t *cb_avl_pool;
2274 boolean_t cb_numname;
2275 boolean_t cb_nicenum;
2276 boolean_t cb_sid2posix;
2277 zfs_userquota_prop_t cb_prop;
2278 zfs_sort_column_t *cb_sortcol;
2279 size_t cb_width[USFIELD_LAST];
2282 static boolean_t us_populated = B_FALSE;
2285 zfs_sort_column_t *si_sortcol;
2286 boolean_t si_numname;
2290 us_field_index(char *field)
2294 for (i = 0; i < USFIELD_LAST; i++) {
2295 if (strcmp(field, us_field_names[i]) == 0)
2303 us_compare(const void *larg, const void *rarg, void *unused)
2305 const us_node_t *l = larg;
2306 const us_node_t *r = rarg;
2307 us_sort_info_t *si = (us_sort_info_t *)unused;
2308 zfs_sort_column_t *sortcol = si->si_sortcol;
2309 boolean_t numname = si->si_numname;
2310 nvlist_t *lnvl = l->usn_nvl;
2311 nvlist_t *rnvl = r->usn_nvl;
2315 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2322 zfs_prop_t prop = sortcol->sc_prop;
2323 const char *propname = NULL;
2324 boolean_t reverse = sortcol->sc_reverse;
2329 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2330 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2332 rc = (rv32 < lv32) ? 1 : -1;
2337 (void) nvlist_lookup_uint64(lnvl, propname,
2339 (void) nvlist_lookup_uint64(rnvl, propname,
2342 rc = (rv64 < lv64) ? 1 : -1;
2344 (void) nvlist_lookup_string(lnvl, propname,
2346 (void) nvlist_lookup_string(rnvl, propname,
2348 rc = strcmp(lvstr, rvstr);
2352 case ZFS_PROP_QUOTA:
2355 if (prop == ZFS_PROP_USED)
2359 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2360 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2362 rc = (rv64 < lv64) ? 1 : -1;
2371 return (reverse ? 1 : -1);
2373 return (reverse ? -1 : 1);
2378 * If entries still seem to be the same, check if they are of the same
2379 * type (smbentity is added only if we are doing SID to POSIX ID
2380 * translation where we can have duplicate type/name combinations).
2382 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2383 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2385 return (lvb < rvb ? -1 : 1);
2390 static inline const char *
2391 us_type2str(unsigned field_type)
2393 switch (field_type) {
2394 case USTYPE_PSX_USR:
2395 return ("POSIX User");
2396 case USTYPE_PSX_GRP:
2397 return ("POSIX Group");
2398 case USTYPE_SMB_USR:
2399 return ("SMB User");
2400 case USTYPE_SMB_GRP:
2401 return ("SMB Group");
2403 return ("Undefined");
2408 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2410 us_cbdata_t *cb = (us_cbdata_t *)arg;
2411 zfs_userquota_prop_t prop = cb->cb_prop;
2416 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2417 uu_avl_t *avl = cb->cb_avl;
2421 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2423 const char *typestr;
2427 int typeidx, nameidx, sizeidx;
2428 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2429 boolean_t smbentity = B_FALSE;
2431 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2433 node = safe_malloc(sizeof (us_node_t));
2434 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2435 node->usn_nvl = props;
2437 if (domain != NULL && domain[0] != '\0') {
2439 char sid[ZFS_MAXNAMELEN + 32];
2443 int flag = IDMAP_REQ_FLG_USE_CACHE;
2448 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2450 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2451 type = USTYPE_SMB_GRP;
2453 err = sid_to_id(sid, B_FALSE, &id);
2456 type = USTYPE_SMB_USR;
2458 err = sid_to_id(sid, B_TRUE, &id);
2465 if (!cb->cb_sid2posix) {
2466 if (type == USTYPE_SMB_USR) {
2467 (void) idmap_getwinnamebyuid(rid, flag,
2470 (void) idmap_getwinnamebygid(rid, flag,
2480 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2482 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2483 type = USTYPE_PSX_GRP;
2484 if (!cb->cb_numname) {
2487 if ((g = getgrgid(rid)) != NULL)
2491 type = USTYPE_PSX_USR;
2492 if (!cb->cb_numname) {
2495 if ((p = getpwuid(rid)) != NULL)
2502 * Make sure that the type/name combination is unique when doing
2503 * SID to POSIX ID translation (hence changing the type from SMB to
2506 if (cb->cb_sid2posix &&
2507 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2510 /* Calculate/update width of TYPE field */
2511 typestr = us_type2str(type);
2512 typelen = strlen(gettext(typestr));
2513 typeidx = us_field_index("type");
2514 if (typelen > cb->cb_width[typeidx])
2515 cb->cb_width[typeidx] = typelen;
2516 if (nvlist_add_uint32(props, "type", type) != 0)
2519 /* Calculate/update width of NAME field */
2520 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2521 if (nvlist_add_uint64(props, "name", rid) != 0)
2523 namelen = snprintf(NULL, 0, "%u", rid);
2525 if (nvlist_add_string(props, "name", name) != 0)
2527 namelen = strlen(name);
2529 nameidx = us_field_index("name");
2530 if (namelen > cb->cb_width[nameidx])
2531 cb->cb_width[nameidx] = namelen;
2534 * Check if this type/name combination is in the list and update it;
2535 * otherwise add new node to the list.
2537 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2538 uu_avl_insert(avl, node, idx);
2543 props = node->usn_nvl;
2546 /* Calculate/update width of USED/QUOTA fields */
2548 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2550 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2551 sizelen = strlen(sizebuf);
2552 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2554 if (!nvlist_exists(props, "quota"))
2555 (void) nvlist_add_uint64(props, "quota", 0);
2558 if (!nvlist_exists(props, "used"))
2559 (void) nvlist_add_uint64(props, "used", 0);
2561 sizeidx = us_field_index(propname);
2562 if (sizelen > cb->cb_width[sizeidx])
2563 cb->cb_width[sizeidx] = sizelen;
2565 if (nvlist_add_uint64(props, propname, space) != 0)
2572 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2573 size_t *width, us_node_t *node)
2575 nvlist_t *nvl = node->usn_nvl;
2576 char valstr[ZFS_MAXNAMELEN];
2577 boolean_t first = B_TRUE;
2583 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2584 if (!(ustype & types))
2587 while ((field = fields[cfield]) != USFIELD_LAST) {
2588 nvpair_t *nvp = NULL;
2592 char *strval = NULL;
2594 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2595 if (strcmp(nvpair_name(nvp),
2596 us_field_names[field]) == 0)
2600 type = nvpair_type(nvp);
2602 case DATA_TYPE_UINT32:
2603 (void) nvpair_value_uint32(nvp, &val32);
2605 case DATA_TYPE_UINT64:
2606 (void) nvpair_value_uint64(nvp, &val64);
2608 case DATA_TYPE_STRING:
2609 (void) nvpair_value_string(nvp, &strval);
2612 (void) fprintf(stderr, "invalid data type\n");
2617 strval = (char *)us_type2str(val32);
2620 if (type == DATA_TYPE_UINT64) {
2621 (void) sprintf(valstr, "%llu", val64);
2627 if (type == DATA_TYPE_UINT64) {
2629 (void) sprintf(valstr, "%llu", val64);
2631 zfs_nicenum(val64, valstr,
2634 if (field == USFIELD_QUOTA &&
2635 strcmp(valstr, "0") == 0)
2645 (void) printf("\t");
2650 (void) printf("%s", strval);
2651 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2652 (void) printf("%-*s", width[field], strval);
2654 (void) printf("%*s", width[field], strval);
2660 (void) printf("\n");
2664 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2665 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2673 boolean_t first = B_TRUE;
2675 while ((field = fields[cfield]) != USFIELD_LAST) {
2676 col = gettext(us_field_hdr[field]);
2677 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2678 (void) printf(first ? "%-*s" : " %-*s",
2681 (void) printf(first ? "%*s" : " %*s",
2687 (void) printf("\n");
2690 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2691 print_us_node(scripted, parsable, fields, types, width, node);
2693 nvlist_free(node->usn_nvl);
2698 zfs_do_userspace(int argc, char **argv)
2701 zfs_userquota_prop_t p;
2703 uu_avl_pool_t *avl_pool;
2705 uu_avl_walk_t *walk;
2707 char deffields[] = "type,name,used,quota";
2708 char *ofield = NULL;
2709 char *tfield = NULL;
2713 boolean_t scripted = B_FALSE;
2714 boolean_t prtnum = B_FALSE;
2715 boolean_t parsable = B_FALSE;
2716 boolean_t sid2posix = B_FALSE;
2719 zfs_sort_column_t *sortcol = NULL;
2720 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2724 uu_list_pool_t *listpool;
2726 uu_avl_index_t idx = 0;
2727 uu_list_index_t idx2 = 0;
2732 if (strcmp(argv[0], "groupspace") == 0)
2733 /* Toggle default group types */
2734 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2736 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2752 if (zfs_add_sort_column(&sortcol, optarg,
2753 c == 's' ? B_FALSE : B_TRUE) != 0) {
2754 (void) fprintf(stderr,
2755 gettext("invalid field '%s'\n"), optarg);
2766 (void) fprintf(stderr, gettext("missing argument for "
2767 "'%c' option\n"), optopt);
2771 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2781 (void) fprintf(stderr, gettext("missing dataset name\n"));
2785 (void) fprintf(stderr, gettext("too many arguments\n"));
2789 /* Use default output fields if not specified using -o */
2793 if ((delim = strchr(ofield, ',')) != NULL)
2795 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2796 (void) fprintf(stderr, gettext("invalid type '%s' "
2797 "for -o option\n"), ofield);
2802 } while (delim != NULL);
2803 fields[cfield] = USFIELD_LAST;
2805 /* Override output types (-t option) */
2806 if (tfield != NULL) {
2810 boolean_t found = B_FALSE;
2812 if ((delim = strchr(tfield, ',')) != NULL)
2814 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2816 if (strcmp(tfield, us_type_names[i]) == 0) {
2818 types |= us_type_bits[i];
2823 (void) fprintf(stderr, gettext("invalid type "
2824 "'%s' for -t option\n"), tfield);
2829 } while (delim != NULL);
2832 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2835 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2836 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2838 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2841 /* Always add default sorting columns */
2842 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2843 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2845 cb.cb_sortcol = sortcol;
2846 cb.cb_numname = prtnum;
2847 cb.cb_nicenum = !parsable;
2848 cb.cb_avl_pool = avl_pool;
2849 cb.cb_avl = avl_tree;
2850 cb.cb_sid2posix = sid2posix;
2852 for (i = 0; i < USFIELD_LAST; i++)
2853 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2855 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2856 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2857 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2858 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2859 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2862 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2867 if ((node = uu_avl_first(avl_tree)) == NULL)
2870 us_populated = B_TRUE;
2872 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2873 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2874 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2875 uu_list_node_init(node, &node->usn_listnode, listpool);
2877 while (node != NULL) {
2879 node = uu_avl_next(avl_tree, node);
2880 uu_avl_remove(avl_tree, rmnode);
2881 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2882 uu_list_insert(list, rmnode, idx2);
2885 for (node = uu_list_first(list); node != NULL;
2886 node = uu_list_next(list, node)) {
2887 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2889 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2890 uu_avl_insert(avl_tree, node, idx);
2893 uu_list_destroy(list);
2894 uu_list_pool_destroy(listpool);
2896 /* Print and free node nvlist memory */
2897 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2900 zfs_free_sort_columns(sortcol);
2902 /* Clean up the AVL tree */
2903 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2906 while ((node = uu_avl_walk_next(walk)) != NULL) {
2907 uu_avl_remove(cb.cb_avl, node);
2911 uu_avl_walk_end(walk);
2912 uu_avl_destroy(avl_tree);
2913 uu_avl_pool_destroy(avl_pool);
2919 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2920 * [-t type[,...]] [filesystem|volume|snapshot] ...
2922 * -H Scripted mode; elide headers and separate columns by tabs.
2923 * -p Display values in parsable (literal) format.
2924 * -r Recurse over all children.
2925 * -d Limit recursion by depth.
2926 * -o Control which fields to display.
2927 * -s Specify sort columns, descending order.
2928 * -S Specify sort columns, ascending order.
2929 * -t Control which object types to display.
2931 * When given no arguments, list all filesystems in the system.
2932 * Otherwise, list the specified datasets, optionally recursing down them if
2933 * '-r' is specified.
2935 typedef struct list_cbdata {
2937 boolean_t cb_literal;
2938 boolean_t cb_scripted;
2939 zprop_list_t *cb_proplist;
2943 * Given a list of columns to display, output appropriate headers for each one.
2946 print_header(list_cbdata_t *cb)
2948 zprop_list_t *pl = cb->cb_proplist;
2949 char headerbuf[ZFS_MAXPROPLEN];
2952 boolean_t first = B_TRUE;
2953 boolean_t right_justify;
2955 for (; pl != NULL; pl = pl->pl_next) {
2962 right_justify = B_FALSE;
2963 if (pl->pl_prop != ZPROP_INVAL) {
2964 header = zfs_prop_column_name(pl->pl_prop);
2965 right_justify = zfs_prop_align_right(pl->pl_prop);
2967 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2968 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2969 headerbuf[i] = '\0';
2973 if (pl->pl_next == NULL && !right_justify)
2974 (void) printf("%s", header);
2975 else if (right_justify)
2976 (void) printf("%*s", pl->pl_width, header);
2978 (void) printf("%-*s", pl->pl_width, header);
2981 (void) printf("\n");
2985 * Given a dataset and a list of fields, print out all the properties according
2986 * to the described layout.
2989 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2991 zprop_list_t *pl = cb->cb_proplist;
2992 boolean_t first = B_TRUE;
2993 char property[ZFS_MAXPROPLEN];
2994 nvlist_t *userprops = zfs_get_user_props(zhp);
2997 boolean_t right_justify;
2999 for (; pl != NULL; pl = pl->pl_next) {
3001 if (cb->cb_scripted)
3002 (void) printf("\t");
3009 if (pl->pl_prop == ZFS_PROP_NAME) {
3010 (void) strlcpy(property, zfs_get_name(zhp),
3013 right_justify = zfs_prop_align_right(pl->pl_prop);
3014 } else if (pl->pl_prop != ZPROP_INVAL) {
3015 if (zfs_prop_get(zhp, pl->pl_prop, property,
3016 sizeof (property), NULL, NULL, 0,
3017 cb->cb_literal) != 0)
3021 right_justify = zfs_prop_align_right(pl->pl_prop);
3022 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3023 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3024 property, sizeof (property), cb->cb_literal) != 0)
3028 right_justify = B_TRUE;
3029 } else if (zfs_prop_written(pl->pl_user_prop)) {
3030 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3031 property, sizeof (property), cb->cb_literal) != 0)
3035 right_justify = B_TRUE;
3037 if (nvlist_lookup_nvlist(userprops,
3038 pl->pl_user_prop, &propval) != 0)
3041 verify(nvlist_lookup_string(propval,
3042 ZPROP_VALUE, &propstr) == 0);
3043 right_justify = B_FALSE;
3047 * If this is being called in scripted mode, or if this is the
3048 * last column and it is left-justified, don't include a width
3051 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3052 (void) printf("%s", propstr);
3053 else if (right_justify)
3054 (void) printf("%*s", pl->pl_width, propstr);
3056 (void) printf("%-*s", pl->pl_width, propstr);
3059 (void) printf("\n");
3063 * Generic callback function to list a dataset or snapshot.
3066 list_callback(zfs_handle_t *zhp, void *data)
3068 list_cbdata_t *cbp = data;
3070 if (cbp->cb_first) {
3071 if (!cbp->cb_scripted)
3073 cbp->cb_first = B_FALSE;
3076 print_dataset(zhp, cbp);
3082 zfs_do_list(int argc, char **argv)
3085 static char default_fields[] =
3086 "name,used,available,referenced,mountpoint";
3087 int types = ZFS_TYPE_DATASET;
3088 boolean_t types_specified = B_FALSE;
3089 char *fields = NULL;
3090 list_cbdata_t cb = { 0 };
3094 zfs_sort_column_t *sortcol = NULL;
3095 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3098 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3104 cb.cb_literal = B_TRUE;
3105 flags |= ZFS_ITER_LITERAL_PROPS;
3108 limit = parse_depth(optarg, &flags);
3111 flags |= ZFS_ITER_RECURSE;
3114 cb.cb_scripted = B_TRUE;
3117 if (zfs_add_sort_column(&sortcol, optarg,
3119 (void) fprintf(stderr,
3120 gettext("invalid property '%s'\n"), optarg);
3125 if (zfs_add_sort_column(&sortcol, optarg,
3127 (void) fprintf(stderr,
3128 gettext("invalid property '%s'\n"), optarg);
3134 types_specified = B_TRUE;
3135 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3136 while (*optarg != '\0') {
3137 static char *type_subopts[] = { "filesystem",
3138 "volume", "snapshot", "snap", "bookmark",
3141 switch (getsubopt(&optarg, type_subopts,
3144 types |= ZFS_TYPE_FILESYSTEM;
3147 types |= ZFS_TYPE_VOLUME;
3151 types |= ZFS_TYPE_SNAPSHOT;
3154 types |= ZFS_TYPE_BOOKMARK;
3157 types = ZFS_TYPE_DATASET |
3161 (void) fprintf(stderr,
3162 gettext("invalid type '%s'\n"),
3169 (void) fprintf(stderr, gettext("missing argument for "
3170 "'%c' option\n"), optopt);
3174 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3184 fields = default_fields;
3187 * If we are only going to list snapshot names and sort by name,
3188 * then we can use faster version.
3190 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3191 flags |= ZFS_ITER_SIMPLE;
3194 * If "-o space" and no types were specified, don't display snapshots.
3196 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3197 types &= ~ZFS_TYPE_SNAPSHOT;
3200 * If the user specifies '-o all', the zprop_get_list() doesn't
3201 * normally include the name of the dataset. For 'zfs list', we always
3202 * want this property to be first.
3204 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3208 cb.cb_first = B_TRUE;
3210 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3211 limit, list_callback, &cb);
3213 zprop_free_list(cb.cb_proplist);
3214 zfs_free_sort_columns(sortcol);
3216 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3217 (void) printf(gettext("no datasets available\n"));
3223 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3224 * zfs rename [-f] -p <fs | vol> <fs | vol>
3225 * zfs rename -r <snap> <snap>
3226 * zfs rename -u [-p] <fs> <fs>
3228 * Renames the given dataset to another of the same type.
3230 * The '-p' flag creates all the non-existing ancestors of the target first.
3234 zfs_do_rename(int argc, char **argv)
3237 renameflags_t flags = { 0 };
3241 boolean_t parents = B_FALSE;
3242 char *snapshot = NULL;
3245 while ((c = getopt(argc, argv, "fpru")) != -1) {
3251 flags.recurse = B_TRUE;
3254 flags.nounmount = B_TRUE;
3257 flags.forceunmount = B_TRUE;
3261 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3270 /* check number of arguments */
3272 (void) fprintf(stderr, gettext("missing source dataset "
3277 (void) fprintf(stderr, gettext("missing target dataset "
3282 (void) fprintf(stderr, gettext("too many arguments\n"));
3286 if (flags.recurse && parents) {
3287 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3292 if (flags.recurse && strchr(argv[0], '@') == 0) {
3293 (void) fprintf(stderr, gettext("source dataset for recursive "
3294 "rename must be a snapshot\n"));
3298 if (flags.nounmount && parents) {
3299 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3304 if (flags.nounmount)
3305 types = ZFS_TYPE_FILESYSTEM;
3307 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3309 types = ZFS_TYPE_DATASET;
3311 if (flags.recurse) {
3313 * When we do recursive rename we are fine when the given
3314 * snapshot for the given dataset doesn't exist - it can
3315 * still exists below.
3318 snapshot = strchr(argv[0], '@');
3319 assert(snapshot != NULL);
3324 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3327 /* If we were asked and the name looks good, try to create ancestors. */
3328 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3329 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3334 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3343 * Promotes the given clone fs to be the parent
3347 zfs_do_promote(int argc, char **argv)
3353 if (argc > 1 && argv[1][0] == '-') {
3354 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3359 /* check number of arguments */
3361 (void) fprintf(stderr, gettext("missing clone filesystem"
3366 (void) fprintf(stderr, gettext("too many arguments\n"));
3370 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3374 ret = (zfs_promote(zhp) != 0);
3382 * zfs rollback [-rRf] <snapshot>
3384 * -r Delete any intervening snapshots before doing rollback
3385 * -R Delete any snapshots and their clones
3386 * -f ignored for backwards compatability
3388 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3389 * since then and making it the active dataset. If more recent snapshots exist,
3390 * the command will complain unless the '-r' flag is given.
3392 typedef struct rollback_cbdata {
3398 boolean_t cb_recurse;
3399 } rollback_cbdata_t;
3402 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3404 rollback_cbdata_t *cbp = data;
3406 if (cbp->cb_first && cbp->cb_recurse) {
3407 (void) fprintf(stderr, gettext("cannot rollback to "
3408 "'%s': clones of previous snapshots exist\n"),
3410 (void) fprintf(stderr, gettext("use '-R' to "
3411 "force deletion of the following clones and "
3417 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3424 * Report any snapshots more recent than the one specified. Used when '-r' is
3425 * not specified. We reuse this same callback for the snapshot dependents - if
3426 * 'cb_dependent' is set, then this is a dependent and we should report it
3427 * without checking the transaction group.
3430 rollback_check(zfs_handle_t *zhp, void *data)
3432 rollback_cbdata_t *cbp = data;
3434 if (cbp->cb_doclones) {
3439 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3440 if (cbp->cb_first && !cbp->cb_recurse) {
3441 (void) fprintf(stderr, gettext("cannot "
3442 "rollback to '%s': more recent snapshots "
3443 "or bookmarks exist\n"),
3445 (void) fprintf(stderr, gettext("use '-r' to "
3446 "force deletion of the following "
3447 "snapshots and bookmarks:\n"));
3452 if (cbp->cb_recurse) {
3453 if (zfs_iter_dependents(zhp, B_TRUE,
3454 rollback_check_dependent, cbp) != 0) {
3459 (void) fprintf(stderr, "%s\n",
3468 zfs_do_rollback(int argc, char **argv)
3472 boolean_t force = B_FALSE;
3473 rollback_cbdata_t cb = { 0 };
3474 zfs_handle_t *zhp, *snap;
3475 char parentname[ZFS_MAXNAMELEN];
3479 while ((c = getopt(argc, argv, "rRf")) != -1) {
3492 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3501 /* check number of arguments */
3503 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3507 (void) fprintf(stderr, gettext("too many arguments\n"));
3511 /* open the snapshot */
3512 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3515 /* open the parent dataset */
3516 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3517 verify((delim = strrchr(parentname, '@')) != NULL);
3519 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3525 * Check for more recent snapshots and/or clones based on the presence
3528 cb.cb_target = argv[0];
3529 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3530 cb.cb_first = B_TRUE;
3532 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3534 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3537 if ((ret = cb.cb_error) != 0)
3541 * Rollback parent to the given snapshot.
3543 ret = zfs_rollback(zhp, snap, force);
3556 * zfs set property=value ... { fs | snap | vol } ...
3558 * Sets the given properties for all datasets specified on the command line.
3562 set_callback(zfs_handle_t *zhp, void *data)
3564 nvlist_t *props = data;
3566 if (zfs_prop_set_list(zhp, props) != 0) {
3567 switch (libzfs_errno(g_zfs)) {
3568 case EZFS_MOUNTFAILED:
3569 (void) fprintf(stderr, gettext("property may be set "
3570 "but unable to remount filesystem\n"));
3572 case EZFS_SHARENFSFAILED:
3573 (void) fprintf(stderr, gettext("property may be set "
3574 "but unable to reshare filesystem\n"));
3583 zfs_do_set(int argc, char **argv)
3585 nvlist_t *props = NULL;
3586 int ds_start = -1; /* argv idx of first dataset arg */
3589 /* check for options */
3590 if (argc > 1 && argv[1][0] == '-') {
3591 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3596 /* check number of arguments */
3598 (void) fprintf(stderr, gettext("missing arguments\n"));
3602 if (strchr(argv[1], '=') == NULL) {
3603 (void) fprintf(stderr, gettext("missing property=value "
3606 (void) fprintf(stderr, gettext("missing dataset "
3612 /* validate argument order: prop=val args followed by dataset args */
3613 for (i = 1; i < argc; i++) {
3614 if (strchr(argv[i], '=') != NULL) {
3616 /* out-of-order prop=val argument */
3617 (void) fprintf(stderr, gettext("invalid "
3618 "argument order\n"), i);
3621 } else if (ds_start < 0) {
3626 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3630 /* Populate a list of property settings */
3631 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3633 for (i = 1; i < ds_start; i++) {
3634 if ((ret = parseprop(props, argv[i])) != 0)
3638 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3639 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3646 typedef struct snap_cbdata {
3648 boolean_t sd_recursive;
3649 const char *sd_snapname;
3653 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3655 snap_cbdata_t *sd = arg;
3660 if (sd->sd_recursive &&
3661 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3666 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3669 fnvlist_add_boolean(sd->sd_nvl, name);
3672 if (sd->sd_recursive)
3673 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3679 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3681 * Creates a snapshot with the given name. While functionally equivalent to
3682 * 'zfs create', it is a separate command to differentiate intent.
3685 zfs_do_snapshot(int argc, char **argv)
3690 snap_cbdata_t sd = { 0 };
3691 boolean_t multiple_snaps = B_FALSE;
3693 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3695 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3699 while ((c = getopt(argc, argv, "ro:")) != -1) {
3702 if (parseprop(props, optarg) != 0)
3706 sd.sd_recursive = B_TRUE;
3707 multiple_snaps = B_TRUE;
3710 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3719 /* check number of arguments */
3721 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3726 multiple_snaps = B_TRUE;
3727 for (; argc > 0; argc--, argv++) {
3731 atp = strchr(argv[0], '@');
3735 sd.sd_snapname = atp + 1;
3736 zhp = zfs_open(g_zfs, argv[0],
3737 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3740 if (zfs_snapshot_cb(zhp, &sd) != 0)
3744 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3745 nvlist_free(sd.sd_nvl);
3747 if (ret != 0 && multiple_snaps)
3748 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3752 nvlist_free(sd.sd_nvl);
3759 * Send a backup stream to stdout.
3762 zfs_do_send(int argc, char **argv)
3764 char *fromname = NULL;
3765 char *toname = NULL;
3766 char *resume_token = NULL;
3769 sendflags_t flags = { 0 };
3771 nvlist_t *dbgnv = NULL;
3772 boolean_t extraverbose = B_FALSE;
3775 while ((c = getopt(argc, argv, ":i:I:RDpvnPLet:")) != -1) {
3786 flags.doall = B_TRUE;
3789 flags.replicate = B_TRUE;
3792 flags.props = B_TRUE;
3795 flags.parsable = B_TRUE;
3796 flags.verbose = B_TRUE;
3800 extraverbose = B_TRUE;
3801 flags.verbose = B_TRUE;
3802 flags.progress = B_TRUE;
3805 flags.dedup = B_TRUE;
3808 flags.dryrun = B_TRUE;
3811 flags.largeblock = B_TRUE;
3814 flags.embed_data = B_TRUE;
3817 resume_token = optarg;
3820 (void) fprintf(stderr, gettext("missing argument for "
3821 "'%c' option\n"), optopt);
3825 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3834 if (resume_token != NULL) {
3835 if (fromname != NULL || flags.replicate || flags.props ||
3837 (void) fprintf(stderr,
3838 gettext("invalid flags combined with -t\n"));
3842 (void) fprintf(stderr, gettext("no additional "
3843 "arguments are permitted with -t\n"));
3848 (void) fprintf(stderr,
3849 gettext("missing snapshot argument\n"));
3853 (void) fprintf(stderr, gettext("too many arguments\n"));
3858 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3859 (void) fprintf(stderr,
3860 gettext("Error: Stream can not be written to a terminal.\n"
3861 "You must redirect standard output.\n"));
3865 if (resume_token != NULL) {
3866 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3871 * Special case sending a filesystem, or from a bookmark.
3873 if (strchr(argv[0], '@') == NULL ||
3874 (fromname && strchr(fromname, '#') != NULL)) {
3875 char frombuf[ZFS_MAXNAMELEN];
3876 enum lzc_send_flags lzc_flags = 0;
3878 if (flags.replicate || flags.doall || flags.props ||
3879 flags.dedup || flags.dryrun || flags.verbose ||
3881 (void) fprintf(stderr,
3883 "Unsupported flag with filesystem or bookmark.\n"));
3887 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3891 if (flags.largeblock)
3892 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3893 if (flags.embed_data)
3894 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3896 if (fromname != NULL &&
3897 (fromname[0] == '#' || fromname[0] == '@')) {
3899 * Incremental source name begins with # or @.
3900 * Default to same fs as target.
3902 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3903 cp = strchr(frombuf, '@');
3906 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3909 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3914 cp = strchr(argv[0], '@');
3917 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3922 * If they specified the full path to the snapshot, chop off
3923 * everything except the short name of the snapshot, but special
3924 * case if they specify the origin.
3926 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3927 char origin[ZFS_MAXNAMELEN];
3930 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3931 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3933 if (strcmp(origin, fromname) == 0) {
3935 flags.fromorigin = B_TRUE;
3938 if (cp != fromname && strcmp(argv[0], fromname)) {
3939 (void) fprintf(stderr,
3940 gettext("incremental source must be "
3941 "in same filesystem\n"));
3945 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3946 (void) fprintf(stderr,
3947 gettext("invalid incremental source\n"));
3953 if (flags.replicate && fromname == NULL)
3954 flags.doall = B_TRUE;
3956 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3957 extraverbose ? &dbgnv : NULL);
3959 if (extraverbose && dbgnv != NULL) {
3961 * dump_nvlist prints to stdout, but that's been
3962 * redirected to a file. Make it print to stderr
3965 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3966 dump_nvlist(dbgnv, 0);
3975 * Restore a backup stream from stdin.
3978 zfs_do_receive(int argc, char **argv)
3981 recvflags_t flags = { 0 };
3982 boolean_t abort_resumable = B_FALSE;
3985 nvpair_t *nvp = NULL;
3987 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3991 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
3994 if (parseprop(props, optarg) != 0)
3998 flags.isprefix = B_TRUE;
4001 flags.isprefix = B_TRUE;
4002 flags.istail = B_TRUE;
4005 flags.dryrun = B_TRUE;
4008 flags.nomount = B_TRUE;
4011 flags.verbose = B_TRUE;
4014 flags.resumable = B_TRUE;
4017 flags.force = B_TRUE;
4020 abort_resumable = B_TRUE;
4023 (void) fprintf(stderr, gettext("missing argument for "
4024 "'%c' option\n"), optopt);
4028 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4037 /* check number of arguments */
4039 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4043 (void) fprintf(stderr, gettext("too many arguments\n"));
4047 while ((nvp = nvlist_next_nvpair(props, nvp))) {
4048 if (strcmp(nvpair_name(nvp), "origin") != 0) {
4049 (void) fprintf(stderr, gettext("invalid option"));
4054 if (abort_resumable) {
4055 if (flags.isprefix || flags.istail || flags.dryrun ||
4056 flags.resumable || flags.nomount) {
4057 (void) fprintf(stderr, gettext("invalid option"));
4061 char namebuf[ZFS_MAXNAMELEN];
4062 (void) snprintf(namebuf, sizeof (namebuf),
4063 "%s/%%recv", argv[0]);
4065 if (zfs_dataset_exists(g_zfs, namebuf,
4066 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4067 zfs_handle_t *zhp = zfs_open(g_zfs,
4068 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4071 err = zfs_destroy(zhp, B_FALSE);
4073 zfs_handle_t *zhp = zfs_open(g_zfs,
4074 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4077 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4078 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4079 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4080 (void) fprintf(stderr,
4081 gettext("'%s' does not have any "
4082 "resumable receive state to abort\n"),
4086 err = zfs_destroy(zhp, B_FALSE);
4092 if (isatty(STDIN_FILENO)) {
4093 (void) fprintf(stderr,
4094 gettext("Error: Backup stream can not be read "
4095 "from a terminal.\n"
4096 "You must redirect standard input.\n"));
4099 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4105 * allow/unallow stuff
4107 /* copied from zfs/sys/dsl_deleg.h */
4108 #define ZFS_DELEG_PERM_CREATE "create"
4109 #define ZFS_DELEG_PERM_DESTROY "destroy"
4110 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4111 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4112 #define ZFS_DELEG_PERM_CLONE "clone"
4113 #define ZFS_DELEG_PERM_PROMOTE "promote"
4114 #define ZFS_DELEG_PERM_RENAME "rename"
4115 #define ZFS_DELEG_PERM_MOUNT "mount"
4116 #define ZFS_DELEG_PERM_SHARE "share"
4117 #define ZFS_DELEG_PERM_SEND "send"
4118 #define ZFS_DELEG_PERM_RECEIVE "receive"
4119 #define ZFS_DELEG_PERM_ALLOW "allow"
4120 #define ZFS_DELEG_PERM_USERPROP "userprop"
4121 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4122 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4123 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4124 #define ZFS_DELEG_PERM_USERUSED "userused"
4125 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4126 #define ZFS_DELEG_PERM_HOLD "hold"
4127 #define ZFS_DELEG_PERM_RELEASE "release"
4128 #define ZFS_DELEG_PERM_DIFF "diff"
4129 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4131 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4133 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4134 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4135 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4136 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4137 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4138 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4139 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4140 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4141 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4142 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4143 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4144 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4145 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4146 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4147 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4148 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4149 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4151 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4152 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4153 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4154 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4155 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4156 { NULL, ZFS_DELEG_NOTE_NONE }
4159 /* permission structure */
4160 typedef struct deleg_perm {
4161 zfs_deleg_who_type_t dp_who_type;
4162 const char *dp_name;
4164 boolean_t dp_descend;
4168 typedef struct deleg_perm_node {
4169 deleg_perm_t dpn_perm;
4171 uu_avl_node_t dpn_avl_node;
4172 } deleg_perm_node_t;
4174 typedef struct fs_perm fs_perm_t;
4176 /* permissions set */
4177 typedef struct who_perm {
4178 zfs_deleg_who_type_t who_type;
4179 const char *who_name; /* id */
4180 char who_ug_name[256]; /* user/group name */
4181 fs_perm_t *who_fsperm; /* uplink */
4183 uu_avl_t *who_deleg_perm_avl; /* permissions */
4187 typedef struct who_perm_node {
4188 who_perm_t who_perm;
4189 uu_avl_node_t who_avl_node;
4192 typedef struct fs_perm_set fs_perm_set_t;
4193 /* fs permissions */
4195 const char *fsp_name;
4197 uu_avl_t *fsp_sc_avl; /* sets,create */
4198 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4200 fs_perm_set_t *fsp_set; /* uplink */
4204 typedef struct fs_perm_node {
4205 fs_perm_t fspn_fsperm;
4208 uu_list_node_t fspn_list_node;
4211 /* top level structure */
4212 struct fs_perm_set {
4213 uu_list_pool_t *fsps_list_pool;
4214 uu_list_t *fsps_list; /* list of fs_perms */
4216 uu_avl_pool_t *fsps_named_set_avl_pool;
4217 uu_avl_pool_t *fsps_who_perm_avl_pool;
4218 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4221 static inline const char *
4222 deleg_perm_type(zfs_deleg_note_t note)
4228 case ZFS_DELEG_NOTE_GROUPQUOTA:
4229 case ZFS_DELEG_NOTE_GROUPUSED:
4230 case ZFS_DELEG_NOTE_USERPROP:
4231 case ZFS_DELEG_NOTE_USERQUOTA:
4232 case ZFS_DELEG_NOTE_USERUSED:
4234 return (gettext("other"));
4236 return (gettext("subcommand"));
4241 who_type2weight(zfs_deleg_who_type_t who_type)
4245 case ZFS_DELEG_NAMED_SET_SETS:
4246 case ZFS_DELEG_NAMED_SET:
4249 case ZFS_DELEG_CREATE_SETS:
4250 case ZFS_DELEG_CREATE:
4253 case ZFS_DELEG_USER_SETS:
4254 case ZFS_DELEG_USER:
4257 case ZFS_DELEG_GROUP_SETS:
4258 case ZFS_DELEG_GROUP:
4261 case ZFS_DELEG_EVERYONE_SETS:
4262 case ZFS_DELEG_EVERYONE:
4274 who_perm_compare(const void *larg, const void *rarg, void *unused)
4276 const who_perm_node_t *l = larg;
4277 const who_perm_node_t *r = rarg;
4278 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4279 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4280 int lweight = who_type2weight(ltype);
4281 int rweight = who_type2weight(rtype);
4282 int res = lweight - rweight;
4284 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4285 ZFS_MAX_DELEG_NAME-1);
4297 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4299 const deleg_perm_node_t *l = larg;
4300 const deleg_perm_node_t *r = rarg;
4301 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4302 ZFS_MAX_DELEG_NAME-1);
4314 fs_perm_set_init(fs_perm_set_t *fspset)
4316 bzero(fspset, sizeof (fs_perm_set_t));
4318 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4319 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4320 NULL, UU_DEFAULT)) == NULL)
4322 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4323 UU_DEFAULT)) == NULL)
4326 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4327 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4328 who_perm_node_t, who_avl_node), who_perm_compare,
4329 UU_DEFAULT)) == NULL)
4332 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4333 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4334 who_perm_node_t, who_avl_node), who_perm_compare,
4335 UU_DEFAULT)) == NULL)
4338 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4339 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4340 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4345 static inline void fs_perm_fini(fs_perm_t *);
4346 static inline void who_perm_fini(who_perm_t *);
4349 fs_perm_set_fini(fs_perm_set_t *fspset)
4351 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4353 while (node != NULL) {
4354 fs_perm_node_t *next_node =
4355 uu_list_next(fspset->fsps_list, node);
4356 fs_perm_t *fsperm = &node->fspn_fsperm;
4357 fs_perm_fini(fsperm);
4358 uu_list_remove(fspset->fsps_list, node);
4363 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4364 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4365 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4369 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4372 deleg_perm->dp_who_type = type;
4373 deleg_perm->dp_name = name;
4377 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4378 zfs_deleg_who_type_t type, const char *name)
4380 uu_avl_pool_t *pool;
4381 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4383 bzero(who_perm, sizeof (who_perm_t));
4385 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4386 UU_DEFAULT)) == NULL)
4389 who_perm->who_type = type;
4390 who_perm->who_name = name;
4391 who_perm->who_fsperm = fsperm;
4395 who_perm_fini(who_perm_t *who_perm)
4397 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4399 while (node != NULL) {
4400 deleg_perm_node_t *next_node =
4401 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4403 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4408 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4412 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4414 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4415 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4417 bzero(fsperm, sizeof (fs_perm_t));
4419 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4423 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4427 fsperm->fsp_set = fspset;
4428 fsperm->fsp_name = fsname;
4432 fs_perm_fini(fs_perm_t *fsperm)
4434 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4435 while (node != NULL) {
4436 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4438 who_perm_t *who_perm = &node->who_perm;
4439 who_perm_fini(who_perm);
4440 uu_avl_remove(fsperm->fsp_sc_avl, node);
4445 node = uu_avl_first(fsperm->fsp_uge_avl);
4446 while (node != NULL) {
4447 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4449 who_perm_t *who_perm = &node->who_perm;
4450 who_perm_fini(who_perm);
4451 uu_avl_remove(fsperm->fsp_uge_avl, node);
4456 uu_avl_destroy(fsperm->fsp_sc_avl);
4457 uu_avl_destroy(fsperm->fsp_uge_avl);
4461 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4462 zfs_deleg_who_type_t who_type, const char *name, char locality)
4464 uu_avl_index_t idx = 0;
4466 deleg_perm_node_t *found_node = NULL;
4467 deleg_perm_t *deleg_perm = &node->dpn_perm;
4469 deleg_perm_init(deleg_perm, who_type, name);
4471 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4473 uu_avl_insert(avl, node, idx);
4476 deleg_perm = &node->dpn_perm;
4481 case ZFS_DELEG_LOCAL:
4482 deleg_perm->dp_local = B_TRUE;
4484 case ZFS_DELEG_DESCENDENT:
4485 deleg_perm->dp_descend = B_TRUE;
4490 assert(B_FALSE); /* invalid locality */
4495 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4497 nvpair_t *nvp = NULL;
4498 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4499 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4500 zfs_deleg_who_type_t who_type = who_perm->who_type;
4502 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4503 const char *name = nvpair_name(nvp);
4504 data_type_t type = nvpair_type(nvp);
4505 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4506 deleg_perm_node_t *node =
4507 safe_malloc(sizeof (deleg_perm_node_t));
4509 assert(type == DATA_TYPE_BOOLEAN);
4511 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4512 set_deleg_perm_node(avl, node, who_type, name, locality);
4519 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4521 nvpair_t *nvp = NULL;
4522 fs_perm_set_t *fspset = fsperm->fsp_set;
4524 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4525 nvlist_t *nvl2 = NULL;
4526 const char *name = nvpair_name(nvp);
4527 uu_avl_t *avl = NULL;
4528 uu_avl_pool_t *avl_pool = NULL;
4529 zfs_deleg_who_type_t perm_type = name[0];
4530 char perm_locality = name[1];
4531 const char *perm_name = name + 3;
4532 boolean_t is_set = B_TRUE;
4533 who_perm_t *who_perm = NULL;
4535 assert('$' == name[2]);
4537 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4540 switch (perm_type) {
4541 case ZFS_DELEG_CREATE:
4542 case ZFS_DELEG_CREATE_SETS:
4543 case ZFS_DELEG_NAMED_SET:
4544 case ZFS_DELEG_NAMED_SET_SETS:
4545 avl_pool = fspset->fsps_named_set_avl_pool;
4546 avl = fsperm->fsp_sc_avl;
4548 case ZFS_DELEG_USER:
4549 case ZFS_DELEG_USER_SETS:
4550 case ZFS_DELEG_GROUP:
4551 case ZFS_DELEG_GROUP_SETS:
4552 case ZFS_DELEG_EVERYONE:
4553 case ZFS_DELEG_EVERYONE_SETS:
4554 avl_pool = fspset->fsps_who_perm_avl_pool;
4555 avl = fsperm->fsp_uge_avl;
4559 assert(!"unhandled zfs_deleg_who_type_t");
4563 who_perm_node_t *found_node = NULL;
4564 who_perm_node_t *node = safe_malloc(
4565 sizeof (who_perm_node_t));
4566 who_perm = &node->who_perm;
4567 uu_avl_index_t idx = 0;
4569 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4570 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4572 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4574 if (avl == fsperm->fsp_uge_avl) {
4576 struct passwd *p = NULL;
4577 struct group *g = NULL;
4578 const char *nice_name = NULL;
4580 switch (perm_type) {
4581 case ZFS_DELEG_USER_SETS:
4582 case ZFS_DELEG_USER:
4583 rid = atoi(perm_name);
4586 nice_name = p->pw_name;
4588 case ZFS_DELEG_GROUP_SETS:
4589 case ZFS_DELEG_GROUP:
4590 rid = atoi(perm_name);
4593 nice_name = g->gr_name;
4600 if (nice_name != NULL)
4602 node->who_perm.who_ug_name,
4606 uu_avl_insert(avl, node, idx);
4609 who_perm = &node->who_perm;
4613 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4620 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4622 nvpair_t *nvp = NULL;
4623 uu_avl_index_t idx = 0;
4625 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4626 nvlist_t *nvl2 = NULL;
4627 const char *fsname = nvpair_name(nvp);
4628 data_type_t type = nvpair_type(nvp);
4629 fs_perm_t *fsperm = NULL;
4630 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4634 fsperm = &node->fspn_fsperm;
4636 assert(DATA_TYPE_NVLIST == type);
4638 uu_list_node_init(node, &node->fspn_list_node,
4639 fspset->fsps_list_pool);
4641 idx = uu_list_numnodes(fspset->fsps_list);
4642 fs_perm_init(fsperm, fspset, fsname);
4644 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4647 (void) parse_fs_perm(fsperm, nvl2);
4649 uu_list_insert(fspset->fsps_list, node, idx);
4655 static inline const char *
4656 deleg_perm_comment(zfs_deleg_note_t note)
4658 const char *str = "";
4663 case ZFS_DELEG_NOTE_ALLOW:
4664 str = gettext("Must also have the permission that is being"
4665 "\n\t\t\t\tallowed");
4667 case ZFS_DELEG_NOTE_CLONE:
4668 str = gettext("Must also have the 'create' ability and 'mount'"
4669 "\n\t\t\t\tability in the origin file system");
4671 case ZFS_DELEG_NOTE_CREATE:
4672 str = gettext("Must also have the 'mount' ability");
4674 case ZFS_DELEG_NOTE_DESTROY:
4675 str = gettext("Must also have the 'mount' ability");
4677 case ZFS_DELEG_NOTE_DIFF:
4678 str = gettext("Allows lookup of paths within a dataset;"
4679 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4680 "\n\t\t\t\tin order to use zfs diff");
4682 case ZFS_DELEG_NOTE_HOLD:
4683 str = gettext("Allows adding a user hold to a snapshot");
4685 case ZFS_DELEG_NOTE_MOUNT:
4686 str = gettext("Allows mount/umount of ZFS datasets");
4688 case ZFS_DELEG_NOTE_PROMOTE:
4689 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4690 " 'promote' ability in the origin file system");
4692 case ZFS_DELEG_NOTE_RECEIVE:
4693 str = gettext("Must also have the 'mount' and 'create'"
4696 case ZFS_DELEG_NOTE_RELEASE:
4697 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4698 "might destroy the snapshot");
4700 case ZFS_DELEG_NOTE_RENAME:
4701 str = gettext("Must also have the 'mount' and 'create'"
4702 "\n\t\t\t\tability in the new parent");
4704 case ZFS_DELEG_NOTE_ROLLBACK:
4707 case ZFS_DELEG_NOTE_SEND:
4710 case ZFS_DELEG_NOTE_SHARE:
4711 str = gettext("Allows sharing file systems over NFS or SMB"
4712 "\n\t\t\t\tprotocols");
4714 case ZFS_DELEG_NOTE_SNAPSHOT:
4718 * case ZFS_DELEG_NOTE_VSCAN:
4719 * str = gettext("");
4723 case ZFS_DELEG_NOTE_GROUPQUOTA:
4724 str = gettext("Allows accessing any groupquota@... property");
4726 case ZFS_DELEG_NOTE_GROUPUSED:
4727 str = gettext("Allows reading any groupused@... property");
4729 case ZFS_DELEG_NOTE_USERPROP:
4730 str = gettext("Allows changing any user property");
4732 case ZFS_DELEG_NOTE_USERQUOTA:
4733 str = gettext("Allows accessing any userquota@... property");
4735 case ZFS_DELEG_NOTE_USERUSED:
4736 str = gettext("Allows reading any userused@... property");
4754 boolean_t recursive; /* unallow only */
4755 boolean_t prt_usage;
4757 boolean_t prt_perms;
4760 const char *dataset;
4764 prop_cmp(const void *a, const void *b)
4766 const char *str1 = *(const char **)a;
4767 const char *str2 = *(const char **)b;
4768 return (strcmp(str1, str2));
4772 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4774 const char *opt_desc[] = {
4775 "-h", gettext("show this help message and exit"),
4776 "-l", gettext("set permission locally"),
4777 "-d", gettext("set permission for descents"),
4778 "-u", gettext("set permission for user"),
4779 "-g", gettext("set permission for group"),
4780 "-e", gettext("set permission for everyone"),
4781 "-c", gettext("set create time permission"),
4782 "-s", gettext("define permission set"),
4784 "-r", gettext("remove permissions recursively"),
4786 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4787 size_t allow_size = unallow_size - 2;
4788 const char *props[ZFS_NUM_PROPS];
4791 FILE *fp = requested ? stdout : stderr;
4792 zprop_desc_t *pdtbl = zfs_prop_get_table();
4793 const char *fmt = gettext("%-16s %-14s\t%s\n");
4795 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4797 (void) fprintf(fp, gettext("Options:\n"));
4798 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4799 const char *opt = opt_desc[i++];
4800 const char *optdsc = opt_desc[i];
4801 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4804 (void) fprintf(fp, gettext("\nThe following permissions are "
4806 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4808 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4809 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4810 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4811 const char *perm_type = deleg_perm_type(perm_note);
4812 const char *perm_comment = deleg_perm_comment(perm_note);
4813 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4816 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4817 zprop_desc_t *pd = &pdtbl[i];
4818 if (pd->pd_visible != B_TRUE)
4821 if (pd->pd_attr == PROP_READONLY)
4824 props[count++] = pd->pd_name;
4826 props[count] = NULL;
4828 qsort(props, count, sizeof (char *), prop_cmp);
4830 for (i = 0; i < count; i++)
4831 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4834 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4836 exit(requested ? 0 : 2);
4839 static inline const char *
4840 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4843 if (un && argc == expected_argc - 1)
4845 else if (argc == expected_argc)
4846 *permsp = argv[argc - 2];
4848 allow_usage(un, B_FALSE,
4849 gettext("wrong number of parameters\n"));
4851 return (argv[argc - 1]);
4855 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4857 int uge_sum = opts->user + opts->group + opts->everyone;
4858 int csuge_sum = opts->create + opts->set + uge_sum;
4859 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4860 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4863 allow_usage(un, B_FALSE,
4864 gettext("-u, -g, and -e are mutually exclusive\n"));
4866 if (opts->prt_usage) {
4867 if (argc == 0 && all_sum == 0)
4868 allow_usage(un, B_TRUE, NULL);
4875 allow_usage(un, B_FALSE,
4876 gettext("invalid options combined with -s\n"));
4878 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4879 if (argv[0][0] != '@')
4880 allow_usage(un, B_FALSE,
4881 gettext("invalid set name: missing '@' prefix\n"));
4882 opts->who = argv[0];
4883 } else if (opts->create) {
4884 if (ldcsuge_sum > 1)
4885 allow_usage(un, B_FALSE,
4886 gettext("invalid options combined with -c\n"));
4887 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4888 } else if (opts->everyone) {
4890 allow_usage(un, B_FALSE,
4891 gettext("invalid options combined with -e\n"));
4892 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4893 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4895 opts->everyone = B_TRUE;
4898 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4899 } else if (argc == 1 && !un) {
4900 opts->prt_perms = B_TRUE;
4901 opts->dataset = argv[argc-1];
4903 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4904 opts->who = argv[0];
4907 if (!opts->local && !opts->descend) {
4908 opts->local = B_TRUE;
4909 opts->descend = B_TRUE;
4914 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4915 const char *who, char *perms, nvlist_t *top_nvl)
4918 char ld[2] = { '\0', '\0' };
4919 char who_buf[ZFS_MAXNAMELEN+32];
4920 char base_type = '\0';
4921 char set_type = '\0';
4922 nvlist_t *base_nvl = NULL;
4923 nvlist_t *set_nvl = NULL;
4926 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4928 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4932 case ZFS_DELEG_NAMED_SET_SETS:
4933 case ZFS_DELEG_NAMED_SET:
4934 set_type = ZFS_DELEG_NAMED_SET_SETS;
4935 base_type = ZFS_DELEG_NAMED_SET;
4936 ld[0] = ZFS_DELEG_NA;
4938 case ZFS_DELEG_CREATE_SETS:
4939 case ZFS_DELEG_CREATE:
4940 set_type = ZFS_DELEG_CREATE_SETS;
4941 base_type = ZFS_DELEG_CREATE;
4942 ld[0] = ZFS_DELEG_NA;
4944 case ZFS_DELEG_USER_SETS:
4945 case ZFS_DELEG_USER:
4946 set_type = ZFS_DELEG_USER_SETS;
4947 base_type = ZFS_DELEG_USER;
4949 ld[0] = ZFS_DELEG_LOCAL;
4951 ld[1] = ZFS_DELEG_DESCENDENT;
4953 case ZFS_DELEG_GROUP_SETS:
4954 case ZFS_DELEG_GROUP:
4955 set_type = ZFS_DELEG_GROUP_SETS;
4956 base_type = ZFS_DELEG_GROUP;
4958 ld[0] = ZFS_DELEG_LOCAL;
4960 ld[1] = ZFS_DELEG_DESCENDENT;
4962 case ZFS_DELEG_EVERYONE_SETS:
4963 case ZFS_DELEG_EVERYONE:
4964 set_type = ZFS_DELEG_EVERYONE_SETS;
4965 base_type = ZFS_DELEG_EVERYONE;
4967 ld[0] = ZFS_DELEG_LOCAL;
4969 ld[1] = ZFS_DELEG_DESCENDENT;
4973 assert(set_type != '\0' && base_type != '\0');
4976 if (perms != NULL) {
4978 char *end = curr + strlen(perms);
4980 while (curr < end) {
4981 char *delim = strchr(curr, ',');
4992 (void) nvlist_add_boolean(nvl, curr);
4998 for (i = 0; i < 2; i++) {
4999 char locality = ld[i];
5003 if (!nvlist_empty(base_nvl)) {
5005 (void) snprintf(who_buf,
5006 sizeof (who_buf), "%c%c$%s",
5007 base_type, locality, who);
5009 (void) snprintf(who_buf,
5010 sizeof (who_buf), "%c%c$",
5011 base_type, locality);
5013 (void) nvlist_add_nvlist(top_nvl, who_buf,
5018 if (!nvlist_empty(set_nvl)) {
5020 (void) snprintf(who_buf,
5021 sizeof (who_buf), "%c%c$%s",
5022 set_type, locality, who);
5024 (void) snprintf(who_buf,
5025 sizeof (who_buf), "%c%c$",
5026 set_type, locality);
5028 (void) nvlist_add_nvlist(top_nvl, who_buf,
5033 for (i = 0; i < 2; i++) {
5034 char locality = ld[i];
5039 (void) snprintf(who_buf, sizeof (who_buf),
5040 "%c%c$%s", base_type, locality, who);
5042 (void) snprintf(who_buf, sizeof (who_buf),
5043 "%c%c$", base_type, locality);
5044 (void) nvlist_add_boolean(top_nvl, who_buf);
5047 (void) snprintf(who_buf, sizeof (who_buf),
5048 "%c%c$%s", set_type, locality, who);
5050 (void) snprintf(who_buf, sizeof (who_buf),
5051 "%c%c$", set_type, locality);
5052 (void) nvlist_add_boolean(top_nvl, who_buf);
5058 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5060 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5064 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5065 opts->descend, opts->who, opts->perms, *nvlp);
5066 } else if (opts->create) {
5067 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5068 opts->descend, NULL, opts->perms, *nvlp);
5069 } else if (opts->everyone) {
5070 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5071 opts->descend, NULL, opts->perms, *nvlp);
5073 char *curr = opts->who;
5074 char *end = curr + strlen(curr);
5076 while (curr < end) {
5078 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5080 char *delim = strchr(curr, ',');
5083 struct passwd *p = NULL;
5084 struct group *g = NULL;
5092 rid = (uid_t)strtol(curr, &endch, 0);
5094 who_type = ZFS_DELEG_USER;
5103 (void) snprintf(errbuf, 256, gettext(
5104 "invalid user %s"), curr);
5105 allow_usage(un, B_TRUE, errbuf);
5107 } else if (opts->group) {
5108 who_type = ZFS_DELEG_GROUP;
5117 (void) snprintf(errbuf, 256, gettext(
5118 "invalid group %s"), curr);
5119 allow_usage(un, B_TRUE, errbuf);
5122 if (*endch != '\0') {
5129 if (*endch != '\0') {
5137 who_type = ZFS_DELEG_USER;
5139 } else if (g != NULL) {
5140 who_type = ZFS_DELEG_GROUP;
5143 (void) snprintf(errbuf, 256, gettext(
5144 "invalid user/group %s"), curr);
5145 allow_usage(un, B_TRUE, errbuf);
5149 (void) sprintf(id, "%u", rid);
5152 store_allow_perm(who_type, opts->local,
5153 opts->descend, who, opts->perms, *nvlp);
5162 print_set_creat_perms(uu_avl_t *who_avl)
5164 const char *sc_title[] = {
5165 gettext("Permission sets:\n"),
5166 gettext("Create time permissions:\n"),
5169 const char **title_ptr = sc_title;
5170 who_perm_node_t *who_node = NULL;
5171 int prev_weight = -1;
5173 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5174 who_node = uu_avl_next(who_avl, who_node)) {
5175 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5176 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5177 const char *who_name = who_node->who_perm.who_name;
5178 int weight = who_type2weight(who_type);
5179 boolean_t first = B_TRUE;
5180 deleg_perm_node_t *deleg_node;
5182 if (prev_weight != weight) {
5183 (void) printf(*title_ptr++);
5184 prev_weight = weight;
5187 if (who_name == NULL || strnlen(who_name, 1) == 0)
5188 (void) printf("\t");
5190 (void) printf("\t%s ", who_name);
5192 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5193 deleg_node = uu_avl_next(avl, deleg_node)) {
5196 deleg_node->dpn_perm.dp_name);
5199 (void) printf(",%s",
5200 deleg_node->dpn_perm.dp_name);
5203 (void) printf("\n");
5208 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5211 who_perm_node_t *who_node = NULL;
5212 boolean_t prt_title = B_TRUE;
5213 uu_avl_walk_t *walk;
5215 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5218 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5219 const char *who_name = who_node->who_perm.who_name;
5220 const char *nice_who_name = who_node->who_perm.who_ug_name;
5221 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5222 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5224 deleg_perm_node_t *deleg_node;
5225 boolean_t prt_who = B_TRUE;
5227 for (deleg_node = uu_avl_first(avl);
5229 deleg_node = uu_avl_next(avl, deleg_node)) {
5230 if (local != deleg_node->dpn_perm.dp_local ||
5231 descend != deleg_node->dpn_perm.dp_descend)
5235 const char *who = NULL;
5237 prt_title = B_FALSE;
5238 (void) printf(title);
5242 case ZFS_DELEG_USER_SETS:
5243 case ZFS_DELEG_USER:
5244 who = gettext("user");
5246 who_name = nice_who_name;
5248 case ZFS_DELEG_GROUP_SETS:
5249 case ZFS_DELEG_GROUP:
5250 who = gettext("group");
5252 who_name = nice_who_name;
5254 case ZFS_DELEG_EVERYONE_SETS:
5255 case ZFS_DELEG_EVERYONE:
5256 who = gettext("everyone");
5261 assert(who != NULL);
5265 if (who_name == NULL)
5266 (void) printf("\t%s", who);
5268 (void) printf("\t%s %s", who, who_name);
5271 (void) printf("%c%s", delim,
5272 deleg_node->dpn_perm.dp_name);
5277 (void) printf("\n");
5280 uu_avl_walk_end(walk);
5284 print_fs_perms(fs_perm_set_t *fspset)
5286 fs_perm_node_t *node = NULL;
5287 char buf[ZFS_MAXNAMELEN+32];
5288 const char *dsname = buf;
5290 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5291 node = uu_list_next(fspset->fsps_list, node)) {
5292 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5293 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5296 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5297 gettext("---- Permissions on %s "),
5298 node->fspn_fsperm.fsp_name);
5299 (void) printf(dsname);
5300 left = 70 - strlen(buf);
5303 (void) printf("\n");
5305 print_set_creat_perms(sc_avl);
5306 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5307 gettext("Local permissions:\n"));
5308 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5309 gettext("Descendent permissions:\n"));
5310 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5311 gettext("Local+Descendent permissions:\n"));
5315 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5317 struct deleg_perms {
5323 set_deleg_perms(zfs_handle_t *zhp, void *data)
5325 struct deleg_perms *perms = (struct deleg_perms *)data;
5326 zfs_type_t zfs_type = zfs_get_type(zhp);
5328 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5331 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5335 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5338 nvlist_t *perm_nvl = NULL;
5339 nvlist_t *update_perm_nvl = NULL;
5342 struct allow_opts opts = { 0 };
5344 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5347 while ((c = getopt(argc, argv, optstr)) != -1) {
5350 opts.local = B_TRUE;
5353 opts.descend = B_TRUE;
5359 opts.group = B_TRUE;
5362 opts.everyone = B_TRUE;
5368 opts.create = B_TRUE;
5371 opts.recursive = B_TRUE;
5374 (void) fprintf(stderr, gettext("missing argument for "
5375 "'%c' option\n"), optopt);
5379 opts.prt_usage = B_TRUE;
5382 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5391 /* check arguments */
5392 parse_allow_args(argc, argv, un, &opts);
5394 /* try to open the dataset */
5395 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5396 ZFS_TYPE_VOLUME)) == NULL) {
5397 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5402 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5405 fs_perm_set_init(&fs_perm_set);
5406 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5407 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5412 print_fs_perms(&fs_perm_set);
5414 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5415 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5418 if (un && opts.recursive) {
5419 struct deleg_perms data = { un, update_perm_nvl };
5420 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5429 nvlist_free(perm_nvl);
5430 nvlist_free(update_perm_nvl);
5432 fs_perm_set_fini(&fs_perm_set);
5440 zfs_do_allow(int argc, char **argv)
5442 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5446 zfs_do_unallow(int argc, char **argv)
5448 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5452 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5457 boolean_t recursive = B_FALSE;
5458 const char *opts = holding ? "rt" : "r";
5462 while ((c = getopt(argc, argv, opts)) != -1) {
5468 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5477 /* check number of arguments */
5485 if (holding && tag[0] == '.') {
5486 /* tags starting with '.' are reserved for libzfs */
5487 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5491 for (i = 0; i < argc; ++i) {
5493 char parent[ZFS_MAXNAMELEN];
5495 char *path = argv[i];
5497 delim = strchr(path, '@');
5498 if (delim == NULL) {
5499 (void) fprintf(stderr,
5500 gettext("'%s' is not a snapshot\n"), path);
5504 (void) strncpy(parent, path, delim - path);
5505 parent[delim - path] = '\0';
5507 zhp = zfs_open(g_zfs, parent,
5508 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5514 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5517 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5523 return (errors != 0);
5527 * zfs hold [-r] [-t] <tag> <snap> ...
5529 * -r Recursively hold
5531 * Apply a user-hold with the given tag to the list of snapshots.
5534 zfs_do_hold(int argc, char **argv)
5536 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5540 * zfs release [-r] <tag> <snap> ...
5542 * -r Recursively release
5544 * Release a user-hold with the given tag from the list of snapshots.
5547 zfs_do_release(int argc, char **argv)
5549 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5552 typedef struct holds_cbdata {
5553 boolean_t cb_recursive;
5554 const char *cb_snapname;
5556 size_t cb_max_namelen;
5557 size_t cb_max_taglen;
5560 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5561 #define DATETIME_BUF_LEN (32)
5566 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5569 nvpair_t *nvp = NULL;
5570 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5574 for (i = 0; i < 3; i++) {
5575 col = gettext(hdr_cols[i]);
5577 (void) printf("%-*s ", i ? tagwidth : nwidth,
5580 (void) printf("%s\n", col);
5584 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5585 char *zname = nvpair_name(nvp);
5587 nvpair_t *nvp2 = NULL;
5588 (void) nvpair_value_nvlist(nvp, &nvl2);
5589 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5590 char tsbuf[DATETIME_BUF_LEN];
5591 char *tagname = nvpair_name(nvp2);
5595 char sep = scripted ? '\t' : ' ';
5596 size_t sepnum = scripted ? 1 : 2;
5598 (void) nvpair_value_uint64(nvp2, &val);
5600 (void) localtime_r(&time, &t);
5601 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5602 gettext(STRFTIME_FMT_STR), &t);
5604 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5605 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5611 * Generic callback function to list a dataset or snapshot.
5614 holds_callback(zfs_handle_t *zhp, void *data)
5616 holds_cbdata_t *cbp = data;
5617 nvlist_t *top_nvl = *cbp->cb_nvlp;
5618 nvlist_t *nvl = NULL;
5619 nvpair_t *nvp = NULL;
5620 const char *zname = zfs_get_name(zhp);
5621 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5623 if (cbp->cb_recursive) {
5624 const char *snapname;
5625 char *delim = strchr(zname, '@');
5629 snapname = delim + 1;
5630 if (strcmp(cbp->cb_snapname, snapname))
5634 if (zfs_get_holds(zhp, &nvl) != 0)
5637 if (znamelen > cbp->cb_max_namelen)
5638 cbp->cb_max_namelen = znamelen;
5640 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5641 const char *tag = nvpair_name(nvp);
5642 size_t taglen = strnlen(tag, MAXNAMELEN);
5643 if (taglen > cbp->cb_max_taglen)
5644 cbp->cb_max_taglen = taglen;
5647 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5651 * zfs holds [-r] <snap> ...
5653 * -r Recursively hold
5656 zfs_do_holds(int argc, char **argv)
5661 boolean_t scripted = B_FALSE;
5662 boolean_t recursive = B_FALSE;
5663 const char *opts = "rH";
5666 int types = ZFS_TYPE_SNAPSHOT;
5667 holds_cbdata_t cb = { 0 };
5674 while ((c = getopt(argc, argv, opts)) != -1) {
5683 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5690 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5691 flags |= ZFS_ITER_RECURSE;
5697 /* check number of arguments */
5701 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5704 for (i = 0; i < argc; ++i) {
5705 char *snapshot = argv[i];
5707 const char *snapname;
5709 delim = strchr(snapshot, '@');
5710 if (delim == NULL) {
5711 (void) fprintf(stderr,
5712 gettext("'%s' is not a snapshot\n"), snapshot);
5716 snapname = delim + 1;
5718 snapshot[delim - snapshot] = '\0';
5720 cb.cb_recursive = recursive;
5721 cb.cb_snapname = snapname;
5725 * 1. collect holds data, set format options
5727 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5728 holds_callback, &cb);
5734 * 2. print holds data
5736 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5738 if (nvlist_empty(nvl))
5739 (void) printf(gettext("no datasets available\n"));
5743 return (0 != errors);
5746 #define CHECK_SPINNER 30
5747 #define SPINNER_TIME 3 /* seconds */
5748 #define MOUNT_TIME 5 /* seconds */
5751 get_one_dataset(zfs_handle_t *zhp, void *data)
5753 static char *spin[] = { "-", "\\", "|", "/" };
5754 static int spinval = 0;
5755 static int spincheck = 0;
5756 static time_t last_spin_time = (time_t)0;
5757 get_all_cb_t *cbp = data;
5758 zfs_type_t type = zfs_get_type(zhp);
5760 if (cbp->cb_verbose) {
5761 if (--spincheck < 0) {
5762 time_t now = time(NULL);
5763 if (last_spin_time + SPINNER_TIME < now) {
5764 update_progress(spin[spinval++ % 4]);
5765 last_spin_time = now;
5767 spincheck = CHECK_SPINNER;
5772 * Interate over any nested datasets.
5774 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5780 * Skip any datasets whose type does not match.
5782 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5786 libzfs_add_handle(cbp, zhp);
5787 assert(cbp->cb_used <= cbp->cb_alloc);
5793 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5795 get_all_cb_t cb = { 0 };
5796 cb.cb_verbose = verbose;
5797 cb.cb_getone = get_one_dataset;
5800 set_progress_header(gettext("Reading ZFS config"));
5801 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5803 *dslist = cb.cb_handles;
5804 *count = cb.cb_used;
5807 finish_progress(gettext("done."));
5811 * Generic callback for sharing or mounting filesystems. Because the code is so
5812 * similar, we have a common function with an extra parameter to determine which
5813 * mode we are using.
5815 #define OP_SHARE 0x1
5816 #define OP_MOUNT 0x2
5819 * Share or mount a dataset.
5822 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5823 boolean_t explicit, const char *options)
5825 char mountpoint[ZFS_MAXPROPLEN];
5826 char shareopts[ZFS_MAXPROPLEN];
5827 char smbshareopts[ZFS_MAXPROPLEN];
5828 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5830 uint64_t zoned, canmount;
5831 boolean_t shared_nfs, shared_smb;
5833 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5836 * Check to make sure we can mount/share this dataset. If we
5837 * are in the global zone and the filesystem is exported to a
5838 * local zone, or if we are in a local zone and the
5839 * filesystem is not exported, then it is an error.
5841 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5843 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5847 (void) fprintf(stderr, gettext("cannot %s '%s': "
5848 "dataset is exported to a local zone\n"), cmdname,
5852 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5856 (void) fprintf(stderr, gettext("cannot %s '%s': "
5857 "permission denied\n"), cmdname,
5863 * Ignore any filesystems which don't apply to us. This
5864 * includes those with a legacy mountpoint, or those with
5865 * legacy share options.
5867 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5868 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5869 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5870 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5871 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5872 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5874 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5875 strcmp(smbshareopts, "off") == 0) {
5879 (void) fprintf(stderr, gettext("cannot share '%s': "
5880 "legacy share\n"), zfs_get_name(zhp));
5881 (void) fprintf(stderr, gettext("to "
5882 "share this filesystem set "
5883 "sharenfs property on\n"));
5888 * We cannot share or mount legacy filesystems. If the
5889 * shareopts is non-legacy but the mountpoint is legacy, we
5890 * treat it as a legacy share.
5892 if (strcmp(mountpoint, "legacy") == 0) {
5896 (void) fprintf(stderr, gettext("cannot %s '%s': "
5897 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5898 (void) fprintf(stderr, gettext("use %s(8) to "
5899 "%s this filesystem\n"), cmdname, cmdname);
5903 if (strcmp(mountpoint, "none") == 0) {
5907 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5908 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5913 * canmount explicit outcome
5914 * on no pass through
5915 * on yes pass through
5917 * off yes display error, return 1
5918 * noauto no return 0
5919 * noauto yes pass through
5921 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5922 if (canmount == ZFS_CANMOUNT_OFF) {
5926 (void) fprintf(stderr, gettext("cannot %s '%s': "
5927 "'canmount' property is set to 'off'\n"), cmdname,
5930 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5935 * If this filesystem is inconsistent and has a receive resume
5936 * token, we can not mount it.
5938 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
5939 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
5940 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
5944 (void) fprintf(stderr, gettext("cannot %s '%s': "
5945 "Contains partially-completed state from "
5946 "\"zfs receive -r\", which can be resumed with "
5947 "\"zfs send -t\"\n"),
5948 cmdname, zfs_get_name(zhp));
5953 * At this point, we have verified that the mountpoint and/or
5954 * shareopts are appropriate for auto management. If the
5955 * filesystem is already mounted or shared, return (failing
5956 * for explicit requests); otherwise mount or share the
5962 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5963 shared_smb = zfs_is_shared_smb(zhp, NULL);
5965 if ((shared_nfs && shared_smb) ||
5966 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5967 strcmp(smbshareopts, "off") == 0) ||
5968 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5969 strcmp(shareopts, "off") == 0)) {
5973 (void) fprintf(stderr, gettext("cannot share "
5974 "'%s': filesystem already shared\n"),
5979 if (!zfs_is_mounted(zhp, NULL) &&
5980 zfs_mount(zhp, NULL, 0) != 0)
5983 if (protocol == NULL) {
5984 if (zfs_shareall(zhp) != 0)
5986 } else if (strcmp(protocol, "nfs") == 0) {
5987 if (zfs_share_nfs(zhp))
5989 } else if (strcmp(protocol, "smb") == 0) {
5990 if (zfs_share_smb(zhp))
5993 (void) fprintf(stderr, gettext("cannot share "
5994 "'%s': invalid share type '%s' "
5996 zfs_get_name(zhp), protocol);
6003 if (options == NULL)
6004 mnt.mnt_mntopts = "";
6006 mnt.mnt_mntopts = (char *)options;
6008 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6009 zfs_is_mounted(zhp, NULL)) {
6013 (void) fprintf(stderr, gettext("cannot mount "
6014 "'%s': filesystem already mounted\n"),
6019 if (zfs_mount(zhp, options, flags) != 0)
6028 * Reports progress in the form "(current/total)". Not thread-safe.
6031 report_mount_progress(int current, int total)
6033 static time_t last_progress_time = 0;
6034 time_t now = time(NULL);
6037 /* report 1..n instead of 0..n-1 */
6040 /* display header if we're here for the first time */
6042 set_progress_header(gettext("Mounting ZFS filesystems"));
6043 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6044 /* too soon to report again */
6048 last_progress_time = now;
6050 (void) sprintf(info, "(%d/%d)", current, total);
6052 if (current == total)
6053 finish_progress(info);
6055 update_progress(info);
6059 append_options(char *mntopts, char *newopts)
6061 int len = strlen(mntopts);
6063 /* original length plus new string to append plus 1 for the comma */
6064 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6065 (void) fprintf(stderr, gettext("the opts argument for "
6066 "'%c' option is too long (more than %d chars)\n"),
6067 "-o", MNT_LINE_MAX);
6072 mntopts[len++] = ',';
6074 (void) strcpy(&mntopts[len], newopts);
6078 share_mount(int op, int argc, char **argv)
6081 boolean_t verbose = B_FALSE;
6083 char *options = NULL;
6087 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6097 if (*optarg == '\0') {
6098 (void) fprintf(stderr, gettext("empty mount "
6099 "options (-o) specified\n"));
6103 if (options == NULL)
6104 options = safe_malloc(MNT_LINE_MAX + 1);
6106 /* option validation is done later */
6107 append_options(options, optarg);
6111 warnx("no overlay mounts support on FreeBSD, ignoring");
6114 (void) fprintf(stderr, gettext("missing argument for "
6115 "'%c' option\n"), optopt);
6119 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6128 /* check number of arguments */
6130 zfs_handle_t **dslist = NULL;
6131 size_t i, count = 0;
6132 char *protocol = NULL;
6134 if (op == OP_SHARE && argc > 0) {
6135 if (strcmp(argv[0], "nfs") != 0 &&
6136 strcmp(argv[0], "smb") != 0) {
6137 (void) fprintf(stderr, gettext("share type "
6138 "must be 'nfs' or 'smb'\n"));
6147 (void) fprintf(stderr, gettext("too many arguments\n"));
6151 start_progress_timer();
6152 get_all_datasets(&dslist, &count, verbose);
6157 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6159 for (i = 0; i < count; i++) {
6161 report_mount_progress(i, count);
6163 if (share_mount_one(dslist[i], op, flags, protocol,
6164 B_FALSE, options) != 0)
6166 zfs_close(dslist[i]);
6170 } else if (argc == 0) {
6171 struct mnttab entry;
6173 if ((op == OP_SHARE) || (options != NULL)) {
6174 (void) fprintf(stderr, gettext("missing filesystem "
6175 "argument (specify -a for all)\n"));
6180 * When mount is given no arguments, go through /etc/mnttab and
6181 * display any active ZFS mounts. We hide any snapshots, since
6182 * they are controlled automatically.
6184 rewind(mnttab_file);
6185 while (getmntent(mnttab_file, &entry) == 0) {
6186 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6187 strchr(entry.mnt_special, '@') != NULL)
6190 (void) printf("%-30s %s\n", entry.mnt_special,
6198 (void) fprintf(stderr,
6199 gettext("too many arguments\n"));
6203 if ((zhp = zfs_open(g_zfs, argv[0],
6204 ZFS_TYPE_FILESYSTEM)) == NULL) {
6207 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6217 * zfs mount -a [nfs]
6218 * zfs mount filesystem
6220 * Mount all filesystems, or mount the given filesystem.
6223 zfs_do_mount(int argc, char **argv)
6225 return (share_mount(OP_MOUNT, argc, argv));
6229 * zfs share -a [nfs | smb]
6230 * zfs share filesystem
6232 * Share all filesystems, or share the given filesystem.
6235 zfs_do_share(int argc, char **argv)
6237 return (share_mount(OP_SHARE, argc, argv));
6240 typedef struct unshare_unmount_node {
6241 zfs_handle_t *un_zhp;
6243 uu_avl_node_t un_avlnode;
6244 } unshare_unmount_node_t;
6248 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6250 const unshare_unmount_node_t *l = larg;
6251 const unshare_unmount_node_t *r = rarg;
6253 return (strcmp(l->un_mountp, r->un_mountp));
6257 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6258 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6259 * and unmount it appropriately.
6262 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6266 struct stat64 statbuf;
6267 struct extmnttab entry;
6268 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6272 * Search for the path in /etc/mnttab. Rather than looking for the
6273 * specific path, which can be fooled by non-standard paths (i.e. ".."
6274 * or "//"), we stat() the path and search for the corresponding
6275 * (major,minor) device pair.
6277 if (stat64(path, &statbuf) != 0) {
6278 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6279 cmdname, path, strerror(errno));
6282 path_inode = statbuf.st_ino;
6285 * Search for the given (major,minor) pair in the mount table.
6288 rewind(mnttab_file);
6289 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6290 if (entry.mnt_major == major(statbuf.st_dev) &&
6291 entry.mnt_minor == minor(statbuf.st_dev))
6298 if (statfs(path, &sfs) != 0) {
6299 (void) fprintf(stderr, "%s: %s\n", path,
6303 statfs2mnttab(&sfs, &entry);
6307 if (op == OP_SHARE) {
6308 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6309 "currently mounted\n"), cmdname, path);
6312 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6314 if ((ret = umount2(path, flags)) != 0)
6315 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6320 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6321 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6322 "filesystem\n"), cmdname, path);
6326 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6327 ZFS_TYPE_FILESYSTEM)) == NULL)
6331 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6332 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6333 cmdname, path, strerror(errno));
6335 } else if (statbuf.st_ino != path_inode) {
6336 (void) fprintf(stderr, gettext("cannot "
6337 "%s '%s': not a mountpoint\n"), cmdname, path);
6341 if (op == OP_SHARE) {
6342 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6343 char smbshare_prop[ZFS_MAXPROPLEN];
6345 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6346 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6347 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6348 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6350 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6351 strcmp(smbshare_prop, "off") == 0) {
6352 (void) fprintf(stderr, gettext("cannot unshare "
6353 "'%s': legacy share\n"), path);
6355 (void) fprintf(stderr, gettext("use "
6356 "unshare(1M) to unshare this filesystem\n"));
6358 } else if (!zfs_is_shared(zhp)) {
6359 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6360 "not currently shared\n"), path);
6362 ret = zfs_unshareall_bypath(zhp, path);
6365 char mtpt_prop[ZFS_MAXPROPLEN];
6367 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6368 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6371 ret = zfs_unmount(zhp, NULL, flags);
6372 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6373 (void) fprintf(stderr, gettext("cannot unmount "
6374 "'%s': legacy mountpoint\n"),
6376 (void) fprintf(stderr, gettext("use umount(8) "
6377 "to unmount this filesystem\n"));
6379 ret = zfs_unmountall(zhp, flags);
6390 * Generic callback for unsharing or unmounting a filesystem.
6393 unshare_unmount(int op, int argc, char **argv)
6400 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6401 char sharesmb[ZFS_MAXPROPLEN];
6404 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6413 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6424 * We could make use of zfs_for_each() to walk all datasets in
6425 * the system, but this would be very inefficient, especially
6426 * since we would have to linearly search /etc/mnttab for each
6427 * one. Instead, do one pass through /etc/mnttab looking for
6428 * zfs entries and call zfs_unmount() for each one.
6430 * Things get a little tricky if the administrator has created
6431 * mountpoints beneath other ZFS filesystems. In this case, we
6432 * have to unmount the deepest filesystems first. To accomplish
6433 * this, we place all the mountpoints in an AVL tree sorted by
6434 * the special type (dataset name), and walk the result in
6435 * reverse to make sure to get any snapshots first.
6437 struct mnttab entry;
6438 uu_avl_pool_t *pool;
6439 uu_avl_t *tree = NULL;
6440 unshare_unmount_node_t *node;
6442 uu_avl_walk_t *walk;
6445 (void) fprintf(stderr, gettext("too many arguments\n"));
6449 if (((pool = uu_avl_pool_create("unmount_pool",
6450 sizeof (unshare_unmount_node_t),
6451 offsetof(unshare_unmount_node_t, un_avlnode),
6452 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6453 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6456 rewind(mnttab_file);
6457 while (getmntent(mnttab_file, &entry) == 0) {
6459 /* ignore non-ZFS entries */
6460 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6463 /* ignore snapshots */
6464 if (strchr(entry.mnt_special, '@') != NULL)
6467 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6468 ZFS_TYPE_FILESYSTEM)) == NULL) {
6475 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6477 sizeof (nfs_mnt_prop),
6478 NULL, NULL, 0, B_FALSE) == 0);
6479 if (strcmp(nfs_mnt_prop, "off") != 0)
6481 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6483 sizeof (nfs_mnt_prop),
6484 NULL, NULL, 0, B_FALSE) == 0);
6485 if (strcmp(nfs_mnt_prop, "off") == 0)
6489 /* Ignore legacy mounts */
6490 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6492 sizeof (nfs_mnt_prop),
6493 NULL, NULL, 0, B_FALSE) == 0);
6494 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6496 /* Ignore canmount=noauto mounts */
6497 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6498 ZFS_CANMOUNT_NOAUTO)
6504 node = safe_malloc(sizeof (unshare_unmount_node_t));
6506 node->un_mountp = safe_strdup(entry.mnt_mountp);
6508 uu_avl_node_init(node, &node->un_avlnode, pool);
6510 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6511 uu_avl_insert(tree, node, idx);
6513 zfs_close(node->un_zhp);
6514 free(node->un_mountp);
6520 * Walk the AVL tree in reverse, unmounting each filesystem and
6521 * removing it from the AVL tree in the process.
6523 if ((walk = uu_avl_walk_start(tree,
6524 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6527 while ((node = uu_avl_walk_next(walk)) != NULL) {
6528 uu_avl_remove(tree, node);
6532 if (zfs_unshareall_bypath(node->un_zhp,
6533 node->un_mountp) != 0)
6538 if (zfs_unmount(node->un_zhp,
6539 node->un_mountp, flags) != 0)
6544 zfs_close(node->un_zhp);
6545 free(node->un_mountp);
6549 uu_avl_walk_end(walk);
6550 uu_avl_destroy(tree);
6551 uu_avl_pool_destroy(pool);
6556 (void) fprintf(stderr,
6557 gettext("missing filesystem argument\n"));
6559 (void) fprintf(stderr,
6560 gettext("too many arguments\n"));
6565 * We have an argument, but it may be a full path or a ZFS
6566 * filesystem. Pass full paths off to unmount_path() (shared by
6567 * manual_unmount), otherwise open the filesystem and pass to
6570 if (argv[0][0] == '/')
6571 return (unshare_unmount_path(op, argv[0],
6574 if ((zhp = zfs_open(g_zfs, argv[0],
6575 ZFS_TYPE_FILESYSTEM)) == NULL)
6578 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6579 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6580 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6581 NULL, 0, B_FALSE) == 0);
6585 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6587 sizeof (nfs_mnt_prop),
6588 NULL, NULL, 0, B_FALSE) == 0);
6589 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6590 sharesmb, sizeof (sharesmb), NULL, NULL,
6593 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6594 strcmp(sharesmb, "off") == 0) {
6595 (void) fprintf(stderr, gettext("cannot "
6596 "unshare '%s': legacy share\n"),
6599 (void) fprintf(stderr, gettext("use "
6600 "unshare(1M) to unshare this "
6604 } else if (!zfs_is_shared(zhp)) {
6605 (void) fprintf(stderr, gettext("cannot "
6606 "unshare '%s': not currently "
6607 "shared\n"), zfs_get_name(zhp));
6609 } else if (zfs_unshareall(zhp) != 0) {
6615 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6616 (void) fprintf(stderr, gettext("cannot "
6617 "unmount '%s': legacy "
6618 "mountpoint\n"), zfs_get_name(zhp));
6619 (void) fprintf(stderr, gettext("use "
6620 "umount(8) to unmount this "
6623 } else if (!zfs_is_mounted(zhp, NULL)) {
6624 (void) fprintf(stderr, gettext("cannot "
6625 "unmount '%s': not currently "
6629 } else if (zfs_unmountall(zhp, flags) != 0) {
6643 * zfs unmount filesystem
6645 * Unmount all filesystems, or a specific ZFS filesystem.
6648 zfs_do_unmount(int argc, char **argv)
6650 return (unshare_unmount(OP_MOUNT, argc, argv));
6655 * zfs unshare filesystem
6657 * Unshare all filesystems, or a specific ZFS filesystem.
6660 zfs_do_unshare(int argc, char **argv)
6662 return (unshare_unmount(OP_SHARE, argc, argv));
6666 * Attach/detach the given dataset to/from the given jail
6670 do_jail(int argc, char **argv, int attach)
6675 /* check number of arguments */
6677 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6681 (void) fprintf(stderr, gettext("too many arguments\n"));
6685 jailid = jail_getid(argv[1]);
6687 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6691 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6695 ret = (zfs_jail(zhp, jailid, attach) != 0);
6702 * zfs jail jailid filesystem
6704 * Attach the given dataset to the given jail
6708 zfs_do_jail(int argc, char **argv)
6711 return (do_jail(argc, argv, 1));
6715 * zfs unjail jailid filesystem
6717 * Detach the given dataset from the given jail
6721 zfs_do_unjail(int argc, char **argv)
6724 return (do_jail(argc, argv, 0));
6728 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6729 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6732 manual_mount(int argc, char **argv)
6735 char mountpoint[ZFS_MAXPROPLEN];
6736 char mntopts[MNT_LINE_MAX] = { '\0' };
6740 char *dataset, *path;
6743 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6746 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6749 flags |= MS_OVERLAY;
6752 flags |= MS_NOMNTTAB;
6755 (void) fprintf(stderr, gettext("missing argument for "
6756 "'%c' option\n"), optopt);
6760 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6762 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6771 /* check that we only have two arguments */
6774 (void) fprintf(stderr, gettext("missing dataset "
6777 (void) fprintf(stderr,
6778 gettext("missing mountpoint argument\n"));
6780 (void) fprintf(stderr, gettext("too many arguments\n"));
6781 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6788 /* try to open the dataset */
6789 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6792 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6793 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6795 /* check for legacy mountpoint and complain appropriately */
6797 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6798 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6799 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6800 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6805 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6806 "mounted using 'mount -t zfs'\n"), dataset);
6807 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6808 "instead.\n"), path);
6809 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6810 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6811 (void) fprintf(stderr, gettext("See zfs(8) for more "
6820 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6821 * unmounts of non-legacy filesystems, as this is the dominant administrative
6825 manual_unmount(int argc, char **argv)
6831 while ((c = getopt(argc, argv, "f")) != -1) {
6837 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6839 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6848 /* check arguments */
6851 (void) fprintf(stderr, gettext("missing path "
6854 (void) fprintf(stderr, gettext("too many arguments\n"));
6855 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6859 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6863 find_command_idx(char *command, int *idx)
6867 for (i = 0; i < NCOMMAND; i++) {
6868 if (command_table[i].name == NULL)
6871 if (strcmp(command, command_table[i].name) == 0) {
6880 zfs_do_diff(int argc, char **argv)
6884 char *tosnap = NULL;
6885 char *fromsnap = NULL;
6890 while ((c = getopt(argc, argv, "FHt")) != -1) {
6893 flags |= ZFS_DIFF_CLASSIFY;
6896 flags |= ZFS_DIFF_PARSEABLE;
6899 flags |= ZFS_DIFF_TIMESTAMP;
6902 (void) fprintf(stderr,
6903 gettext("invalid option '%c'\n"), optopt);
6912 (void) fprintf(stderr,
6913 gettext("must provide at least one snapshot name\n"));
6918 (void) fprintf(stderr, gettext("too many arguments\n"));
6923 tosnap = (argc == 2) ? argv[1] : NULL;
6926 if (*fromsnap != '@')
6927 copy = strdup(fromsnap);
6929 copy = strdup(tosnap);
6933 if ((atp = strchr(copy, '@')) != NULL)
6936 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6942 * Ignore SIGPIPE so that the library can give us
6943 * information on any failure
6945 (void) sigignore(SIGPIPE);
6947 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6955 * zfs bookmark <fs@snap> <fs#bmark>
6957 * Creates a bookmark with the given name from the given snapshot.
6960 zfs_do_bookmark(int argc, char **argv)
6962 char snapname[ZFS_MAXNAMELEN];
6969 while ((c = getopt(argc, argv, "")) != -1) {
6972 (void) fprintf(stderr,
6973 gettext("invalid option '%c'\n"), optopt);
6981 /* check number of arguments */
6983 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6987 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6991 if (strchr(argv[1], '#') == NULL) {
6992 (void) fprintf(stderr,
6993 gettext("invalid bookmark name '%s' -- "
6994 "must contain a '#'\n"), argv[1]);
6998 if (argv[0][0] == '@') {
7000 * Snapshot name begins with @.
7001 * Default to same fs as bookmark.
7003 (void) strncpy(snapname, argv[1], sizeof (snapname));
7004 *strchr(snapname, '#') = '\0';
7005 (void) strlcat(snapname, argv[0], sizeof (snapname));
7007 (void) strncpy(snapname, argv[0], sizeof (snapname));
7009 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
7015 nvl = fnvlist_alloc();
7016 fnvlist_add_string(nvl, argv[1], snapname);
7017 ret = lzc_bookmark(nvl, NULL);
7021 const char *err_msg;
7024 (void) snprintf(errbuf, sizeof (errbuf),
7025 dgettext(TEXT_DOMAIN,
7026 "cannot create bookmark '%s'"), argv[1]);
7030 err_msg = "bookmark is in a different pool";
7033 err_msg = "bookmark exists";
7036 err_msg = "invalid argument";
7039 err_msg = "bookmark feature not enabled";
7042 err_msg = "out of space";
7045 err_msg = "unknown error";
7048 (void) fprintf(stderr, "%s: %s\n", errbuf,
7049 dgettext(TEXT_DOMAIN, err_msg));
7060 main(int argc, char **argv)
7067 (void) setlocale(LC_ALL, "");
7068 (void) textdomain(TEXT_DOMAIN);
7072 if ((g_zfs = libzfs_init()) == NULL) {
7073 (void) fprintf(stderr, gettext("internal error: failed to "
7074 "initialize ZFS library\n"));
7078 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7080 libzfs_print_on_error(g_zfs, B_TRUE);
7082 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
7083 (void) fprintf(stderr, gettext("internal error: unable to "
7084 "open %s\n"), MNTTAB);
7089 * This command also doubles as the /etc/fs mount and unmount program.
7090 * Determine if we should take this behavior based on argv[0].
7092 progname = basename(argv[0]);
7093 if (strcmp(progname, "mount") == 0) {
7094 ret = manual_mount(argc, argv);
7095 } else if (strcmp(progname, "umount") == 0) {
7096 ret = manual_unmount(argc, argv);
7099 * Make sure the user has specified some command.
7102 (void) fprintf(stderr, gettext("missing command\n"));
7109 * The 'umount' command is an alias for 'unmount'
7111 if (strcmp(cmdname, "umount") == 0)
7112 cmdname = "unmount";
7115 * The 'recv' command is an alias for 'receive'
7117 if (strcmp(cmdname, "recv") == 0)
7118 cmdname = "receive";
7121 * The 'snap' command is an alias for 'snapshot'
7123 if (strcmp(cmdname, "snap") == 0)
7124 cmdname = "snapshot";
7129 if (strcmp(cmdname, "-?") == 0)
7133 * Run the appropriate command.
7135 libzfs_mnttab_cache(g_zfs, B_TRUE);
7136 if (find_command_idx(cmdname, &i) == 0) {
7137 current_command = &command_table[i];
7138 ret = command_table[i].func(argc - 1, argv + 1);
7139 } else if (strchr(cmdname, '=') != NULL) {
7140 verify(find_command_idx("set", &i) == 0);
7141 current_command = &command_table[i];
7142 ret = command_table[i].func(argc, argv);
7144 (void) fprintf(stderr, gettext("unrecognized "
7145 "command '%s'\n"), cmdname);
7148 libzfs_mnttab_cache(g_zfs, B_FALSE);
7151 (void) fclose(mnttab_file);
7153 if (ret == 0 && log_history)
7154 (void) zpool_log_history(g_zfs, history_str);
7159 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7160 * for the purposes of running ::findleaks.
7162 if (getenv("ZFS_ABORT") != NULL) {
7163 (void) printf("dumping core by request\n");