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. All rights reserved.
28 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
31 * Copyright (c) 2014 Integros [integros.com]
32 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
41 #include <libnvpair.h>
54 #include <sys/mntent.h>
55 #include <sys/mnttab.h>
56 #include <sys/mount.h>
58 #include <sys/fs/zfs.h>
59 #include <sys/types.h>
65 #include <libzfs_core.h>
67 #include <zfs_deleg.h>
71 #include <directory.h>
77 #include "zfs_comutil.h"
79 libzfs_handle_t *g_zfs;
81 static FILE *mnttab_file;
82 static char history_str[HIS_MAX_RECORD_LEN];
83 static boolean_t log_history = B_TRUE;
85 static int zfs_do_clone(int argc, char **argv);
86 static int zfs_do_create(int argc, char **argv);
87 static int zfs_do_destroy(int argc, char **argv);
88 static int zfs_do_get(int argc, char **argv);
89 static int zfs_do_inherit(int argc, char **argv);
90 static int zfs_do_list(int argc, char **argv);
91 static int zfs_do_mount(int argc, char **argv);
92 static int zfs_do_rename(int argc, char **argv);
93 static int zfs_do_rollback(int argc, char **argv);
94 static int zfs_do_set(int argc, char **argv);
95 static int zfs_do_upgrade(int argc, char **argv);
96 static int zfs_do_snapshot(int argc, char **argv);
97 static int zfs_do_unmount(int argc, char **argv);
98 static int zfs_do_share(int argc, char **argv);
99 static int zfs_do_unshare(int argc, char **argv);
100 static int zfs_do_send(int argc, char **argv);
101 static int zfs_do_receive(int argc, char **argv);
102 static int zfs_do_promote(int argc, char **argv);
103 static int zfs_do_userspace(int argc, char **argv);
104 static int zfs_do_allow(int argc, char **argv);
105 static int zfs_do_unallow(int argc, char **argv);
106 static int zfs_do_hold(int argc, char **argv);
107 static int zfs_do_holds(int argc, char **argv);
108 static int zfs_do_release(int argc, char **argv);
109 static int zfs_do_diff(int argc, char **argv);
110 static int zfs_do_jail(int argc, char **argv);
111 static int zfs_do_unjail(int argc, char **argv);
112 static int zfs_do_bookmark(int argc, char **argv);
115 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
120 _umem_debug_init(void)
122 return ("default,verbose"); /* $UMEM_DEBUG setting */
126 _umem_logging_init(void)
128 return ("fail,contents"); /* $UMEM_LOGGING setting */
164 typedef struct zfs_command {
166 int (*func)(int argc, char **argv);
171 * Master command table. Each ZFS command has a name, associated function, and
172 * usage message. The usage messages need to be internationalized, so we have
173 * to have a function to return the usage message based on a command index.
175 * These commands are organized according to how they are displayed in the usage
176 * message. An empty command (one with a NULL name) indicates an empty line in
177 * the generic usage message.
179 static zfs_command_t command_table[] = {
180 { "create", zfs_do_create, HELP_CREATE },
181 { "destroy", zfs_do_destroy, HELP_DESTROY },
183 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
184 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
185 { "clone", zfs_do_clone, HELP_CLONE },
186 { "promote", zfs_do_promote, HELP_PROMOTE },
187 { "rename", zfs_do_rename, HELP_RENAME },
188 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
190 { "list", zfs_do_list, HELP_LIST },
192 { "set", zfs_do_set, HELP_SET },
193 { "get", zfs_do_get, HELP_GET },
194 { "inherit", zfs_do_inherit, HELP_INHERIT },
195 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
196 { "userspace", zfs_do_userspace, HELP_USERSPACE },
197 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
199 { "mount", zfs_do_mount, HELP_MOUNT },
200 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
201 { "share", zfs_do_share, HELP_SHARE },
202 { "unshare", zfs_do_unshare, HELP_UNSHARE },
204 { "send", zfs_do_send, HELP_SEND },
205 { "receive", zfs_do_receive, HELP_RECEIVE },
207 { "allow", zfs_do_allow, HELP_ALLOW },
209 { "unallow", zfs_do_unallow, HELP_UNALLOW },
211 { "hold", zfs_do_hold, HELP_HOLD },
212 { "holds", zfs_do_holds, HELP_HOLDS },
213 { "release", zfs_do_release, HELP_RELEASE },
214 { "diff", zfs_do_diff, HELP_DIFF },
216 { "jail", zfs_do_jail, HELP_JAIL },
217 { "unjail", zfs_do_unjail, HELP_UNJAIL },
220 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
222 zfs_command_t *current_command;
225 get_usage(zfs_help_t idx)
229 return (gettext("\tclone [-p] [-o property=value] ... "
230 "<snapshot> <filesystem|volume>\n"));
232 return (gettext("\tcreate [-pu] [-o property=value] ... "
234 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
235 "-V <size> <volume>\n"));
237 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
238 "\tdestroy [-dnpRrv] "
239 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
240 "\tdestroy <filesystem|volume>#<bookmark>\n"));
242 return (gettext("\tget [-rHp] [-d max] "
243 "[-o \"all\" | field[,...]]\n"
244 "\t [-t type[,...]] [-s source[,...]]\n"
245 "\t <\"all\" | property[,...]> "
246 "[filesystem|volume|snapshot] ...\n"));
248 return (gettext("\tinherit [-rS] <property> "
249 "<filesystem|volume|snapshot> ...\n"));
251 return (gettext("\tupgrade [-v]\n"
252 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
254 return (gettext("\tjail <jailid|jailname> <filesystem>\n"));
256 return (gettext("\tunjail <jailid|jailname> <filesystem>\n"));
258 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
259 "[-s property]...\n\t [-S property]... [-t type[,...]] "
260 "[filesystem|volume|snapshot] ...\n"));
262 return (gettext("\tmount\n"
263 "\tmount [-vO] [-o opts] <-a | filesystem>\n"));
265 return (gettext("\tpromote <clone-filesystem>\n"));
267 return (gettext("\treceive|recv [-vnsFu] <filesystem|volume|"
269 "\treceive|recv [-vnsFu] [-o origin=<snapshot>] [-d | -e] "
271 "\treceive|recv -A <filesystem|volume>\n"));
273 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
274 "<filesystem|volume|snapshot>\n"
275 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
276 "\trename -r <snapshot> <snapshot>\n"
277 "\trename -u [-p] <filesystem> <filesystem>"));
279 return (gettext("\trollback [-rRf] <snapshot>\n"));
281 return (gettext("\tsend [-DnPpRvLe] [-[iI] snapshot] "
283 "\tsend [-Le] [-i snapshot|bookmark] "
284 "<filesystem|volume|snapshot>\n"
285 "\tsend [-nvPe] -t <receive_resume_token>\n"));
287 return (gettext("\tset <property=value> ... "
288 "<filesystem|volume|snapshot> ...\n"));
290 return (gettext("\tshare <-a | filesystem>\n"));
292 return (gettext("\tsnapshot|snap [-r] [-o property=value] ... "
293 "<filesystem|volume>@<snap> ...\n"));
295 return (gettext("\tunmount|umount [-f] "
296 "<-a | filesystem|mountpoint>\n"));
298 return (gettext("\tunshare "
299 "<-a | filesystem|mountpoint>\n"));
301 return (gettext("\tallow <filesystem|volume>\n"
303 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
304 "\t <filesystem|volume>\n"
305 "\tallow [-ld] -e <perm|@setname>[,...] "
306 "<filesystem|volume>\n"
307 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
308 "\tallow -s @setname <perm|@setname>[,...] "
309 "<filesystem|volume>\n"));
311 return (gettext("\tunallow [-rldug] "
312 "<\"everyone\"|user|group>[,...]\n"
313 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
314 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
315 "<filesystem|volume>\n"
316 "\tunallow [-r] -c [<perm|@setname>[,...]] "
317 "<filesystem|volume>\n"
318 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
319 "<filesystem|volume>\n"));
321 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
323 "\t [-S field] ... [-t type[,...]] "
324 "<filesystem|snapshot>\n"));
325 case HELP_GROUPSPACE:
326 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
328 "\t [-S field] ... [-t type[,...]] "
329 "<filesystem|snapshot>\n"));
331 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
333 return (gettext("\tholds [-r] <snapshot> ...\n"));
335 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
337 return (gettext("\tdiff [-FHt] <snapshot> "
338 "[snapshot|filesystem]\n"));
340 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
350 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
355 * Utility function to guarantee malloc() success.
359 safe_malloc(size_t size)
363 if ((data = calloc(1, size)) == NULL)
370 safe_strdup(char *str)
372 char *dupstr = strdup(str);
381 * Callback routine that will print out information for each of
385 usage_prop_cb(int prop, void *cb)
389 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
391 if (zfs_prop_readonly(prop))
392 (void) fprintf(fp, " NO ");
394 (void) fprintf(fp, "YES ");
396 if (zfs_prop_inheritable(prop))
397 (void) fprintf(fp, " YES ");
399 (void) fprintf(fp, " NO ");
401 if (zfs_prop_values(prop) == NULL)
402 (void) fprintf(fp, "-\n");
404 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
410 * Display usage message. If we're inside a command, display only the usage for
411 * that command. Otherwise, iterate over the entire command table and display
412 * a complete usage message.
415 usage(boolean_t requested)
418 boolean_t show_properties = B_FALSE;
419 FILE *fp = requested ? stdout : stderr;
421 if (current_command == NULL) {
423 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
425 gettext("where 'command' is one of the following:\n\n"));
427 for (i = 0; i < NCOMMAND; i++) {
428 if (command_table[i].name == NULL)
429 (void) fprintf(fp, "\n");
431 (void) fprintf(fp, "%s",
432 get_usage(command_table[i].usage));
435 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
436 "pool/[dataset/]*dataset[@name]\n"));
438 (void) fprintf(fp, gettext("usage:\n"));
439 (void) fprintf(fp, "%s", get_usage(current_command->usage));
442 if (current_command != NULL &&
443 (strcmp(current_command->name, "set") == 0 ||
444 strcmp(current_command->name, "get") == 0 ||
445 strcmp(current_command->name, "inherit") == 0 ||
446 strcmp(current_command->name, "list") == 0))
447 show_properties = B_TRUE;
449 if (show_properties) {
451 gettext("\nThe following properties are supported:\n"));
453 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
454 "PROPERTY", "EDIT", "INHERIT", "VALUES");
456 /* Iterate over all properties */
457 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
460 (void) fprintf(fp, "\t%-15s ", "userused@...");
461 (void) fprintf(fp, " NO NO <size>\n");
462 (void) fprintf(fp, "\t%-15s ", "groupused@...");
463 (void) fprintf(fp, " NO NO <size>\n");
464 (void) fprintf(fp, "\t%-15s ", "userquota@...");
465 (void) fprintf(fp, "YES NO <size> | none\n");
466 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
467 (void) fprintf(fp, "YES NO <size> | none\n");
468 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
469 (void) fprintf(fp, " NO NO <size>\n");
471 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
472 "with standard units such as K, M, G, etc.\n"));
473 (void) fprintf(fp, gettext("\nUser-defined properties can "
474 "be specified by using a name containing a colon (:).\n"));
475 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
476 "properties must be appended with\n"
477 "a user or group specifier of one of these forms:\n"
478 " POSIX name (eg: \"matt\")\n"
479 " POSIX id (eg: \"126829\")\n"
480 " SMB name@domain (eg: \"matt@sun\")\n"
481 " SMB SID (eg: \"S-1-234-567-89\")\n"));
484 gettext("\nFor the property list, run: %s\n"),
487 gettext("\nFor the delegated permission list, run: %s\n"),
488 "zfs allow|unallow");
492 * See comments at end of main().
494 if (getenv("ZFS_ABORT") != NULL) {
495 (void) printf("dumping core by request\n");
499 exit(requested ? 0 : 2);
503 * Take a property=value argument string and add it to the given nvlist.
504 * Modifies the argument inplace.
507 parseprop(nvlist_t *props, char *propname)
509 char *propval, *strval;
511 if ((propval = strchr(propname, '=')) == NULL) {
512 (void) fprintf(stderr, gettext("missing "
513 "'=' for property=value argument\n"));
518 if (nvlist_lookup_string(props, propname, &strval) == 0) {
519 (void) fprintf(stderr, gettext("property '%s' "
520 "specified multiple times\n"), propname);
523 if (nvlist_add_string(props, propname, propval) != 0)
529 parse_depth(char *opt, int *flags)
534 depth = (int)strtol(opt, &tmp, 0);
536 (void) fprintf(stderr,
537 gettext("%s is not an integer\n"), opt);
541 (void) fprintf(stderr,
542 gettext("Depth can not be negative.\n"));
545 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
549 #define PROGRESS_DELAY 2 /* seconds */
551 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";
552 static time_t pt_begin;
553 static char *pt_header = NULL;
554 static boolean_t pt_shown;
557 start_progress_timer(void)
559 pt_begin = time(NULL) + PROGRESS_DELAY;
564 set_progress_header(char *header)
566 assert(pt_header == NULL);
567 pt_header = safe_strdup(header);
569 (void) printf("%s: ", header);
570 (void) fflush(stdout);
575 update_progress(char *update)
577 if (!pt_shown && time(NULL) > pt_begin) {
578 int len = strlen(update);
580 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
582 (void) fflush(stdout);
584 } else if (pt_shown) {
585 int len = strlen(update);
587 (void) printf("%s%*.*s", update, len, len, pt_reverse);
588 (void) fflush(stdout);
593 finish_progress(char *done)
596 (void) printf("%s\n", done);
597 (void) fflush(stdout);
604 * Check if the dataset is mountable and should be automatically mounted.
607 should_auto_mount(zfs_handle_t *zhp)
609 if (!zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, zfs_get_type(zhp)))
611 return (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON);
615 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
617 * Given an existing dataset, create a writable copy whose initial contents
618 * are the same as the source. The newly created dataset maintains a
619 * dependency on the original; the original cannot be destroyed so long as
622 * The '-p' flag creates all the non-existing ancestors of the target first.
625 zfs_do_clone(int argc, char **argv)
627 zfs_handle_t *zhp = NULL;
628 boolean_t parents = B_FALSE;
633 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
637 while ((c = getopt(argc, argv, "o:p")) != -1) {
640 if (parseprop(props, optarg) != 0)
647 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
656 /* check number of arguments */
658 (void) fprintf(stderr, gettext("missing source dataset "
663 (void) fprintf(stderr, gettext("missing target dataset "
668 (void) fprintf(stderr, gettext("too many arguments\n"));
672 /* open the source dataset */
673 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
676 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
679 * Now create the ancestors of the target dataset. If the
680 * target already exists and '-p' option was used we should not
683 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
686 if (zfs_create_ancestors(g_zfs, argv[1]) != 0)
691 ret = zfs_clone(zhp, argv[1], props);
693 /* create the mountpoint if necessary */
697 clone = zfs_open(g_zfs, argv[1], ZFS_TYPE_DATASET);
700 * If the user doesn't want the dataset
701 * automatically mounted, then skip the mount/share
704 if (should_auto_mount(clone)) {
705 if ((ret = zfs_mount(clone, NULL, 0)) != 0) {
706 (void) fprintf(stderr, gettext("clone "
707 "successfully created, "
708 "but not mounted\n"));
709 } else if ((ret = zfs_share(clone)) != 0) {
710 (void) fprintf(stderr, gettext("clone "
711 "successfully created, "
712 "but not shared\n"));
733 * zfs create [-pu] [-o prop=value] ... fs
734 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
736 * Create a new dataset. This command can be used to create filesystems
737 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
738 * For volumes, the user must specify a size to be used.
740 * The '-s' flag applies only to volumes, and indicates that we should not try
741 * to set the reservation for this volume. By default we set a reservation
742 * equal to the size for any volume. For pools with SPA_VERSION >=
743 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
745 * The '-p' flag creates all the non-existing ancestors of the target first.
747 * The '-u' flag prevents mounting of newly created file system.
750 zfs_do_create(int argc, char **argv)
752 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
753 zfs_handle_t *zhp = NULL;
754 uint64_t volsize = 0;
756 boolean_t noreserve = B_FALSE;
757 boolean_t bflag = B_FALSE;
758 boolean_t parents = B_FALSE;
759 boolean_t nomount = B_FALSE;
764 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
768 while ((c = getopt(argc, argv, ":V:b:so:pu")) != -1) {
771 type = ZFS_TYPE_VOLUME;
772 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
773 (void) fprintf(stderr, gettext("bad volume "
774 "size '%s': %s\n"), optarg,
775 libzfs_error_description(g_zfs));
779 if (nvlist_add_uint64(props,
780 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
789 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
790 (void) fprintf(stderr, gettext("bad volume "
791 "block size '%s': %s\n"), optarg,
792 libzfs_error_description(g_zfs));
796 if (nvlist_add_uint64(props,
797 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
802 if (parseprop(props, optarg) != 0)
812 (void) fprintf(stderr, gettext("missing size "
816 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
822 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
823 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
824 "used when creating a volume\n"));
827 if (nomount && type != ZFS_TYPE_FILESYSTEM) {
828 (void) fprintf(stderr, gettext("'-u' can only be "
829 "used when creating a file system\n"));
836 /* check number of arguments */
838 (void) fprintf(stderr, gettext("missing %s argument\n"),
839 zfs_type_to_name(type));
843 (void) fprintf(stderr, gettext("too many arguments\n"));
847 if (type == ZFS_TYPE_VOLUME && !noreserve) {
848 zpool_handle_t *zpool_handle;
849 nvlist_t *real_props = NULL;
850 uint64_t spa_version;
852 zfs_prop_t resv_prop;
856 if ((p = strchr(argv[0], '/')) != NULL)
858 zpool_handle = zpool_open(g_zfs, argv[0]);
861 if (zpool_handle == NULL)
863 spa_version = zpool_get_prop_int(zpool_handle,
864 ZPOOL_PROP_VERSION, NULL);
865 if (spa_version >= SPA_VERSION_REFRESERVATION)
866 resv_prop = ZFS_PROP_REFRESERVATION;
868 resv_prop = ZFS_PROP_RESERVATION;
870 (void) snprintf(msg, sizeof (msg),
871 gettext("cannot create '%s'"), argv[0]);
872 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
873 props, 0, NULL, zpool_handle, msg)) == NULL) {
874 zpool_close(zpool_handle);
877 zpool_close(zpool_handle);
879 volsize = zvol_volsize_to_reservation(volsize, real_props);
880 nvlist_free(real_props);
882 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
884 if (nvlist_add_uint64(props,
885 zfs_prop_to_name(resv_prop), volsize) != 0) {
892 if (parents && zfs_name_valid(argv[0], type)) {
894 * Now create the ancestors of target dataset. If the target
895 * already exists and '-p' option was used we should not
898 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
902 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
907 if (zfs_create(g_zfs, argv[0], type, props) != 0)
910 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
916 * Mount and/or share the new filesystem as appropriate. We provide a
917 * verbose error message to let the user know that their filesystem was
918 * in fact created, even if we failed to mount or share it.
919 * If the user doesn't want the dataset automatically mounted,
920 * then skip the mount/share step altogether.
922 if (!nomount && should_auto_mount(zhp)) {
923 if (zfs_mount(zhp, NULL, 0) != 0) {
924 (void) fprintf(stderr, gettext("filesystem "
925 "successfully created, but not mounted\n"));
927 } else if (zfs_share(zhp) != 0) {
928 (void) fprintf(stderr, gettext("filesystem "
929 "successfully created, but not shared\n"));
946 * zfs destroy [-rRf] <fs, vol>
947 * zfs destroy [-rRd] <snap>
949 * -r Recursively destroy all children
950 * -R Recursively destroy all dependents, including clones
951 * -f Force unmounting of any dependents
952 * -d If we can't destroy now, mark for deferred destruction
954 * Destroys the given dataset. By default, it will unmount any filesystems,
955 * and refuse to destroy a dataset that has any dependents. A dependent can
956 * either be a child, or a clone of a child.
958 typedef struct destroy_cbdata {
961 boolean_t cb_recurse;
963 boolean_t cb_doclones;
964 zfs_handle_t *cb_target;
965 boolean_t cb_defer_destroy;
966 boolean_t cb_verbose;
967 boolean_t cb_parsable;
970 nvlist_t *cb_batchedsnaps;
972 /* first snap in contiguous run */
974 /* previous snap in contiguous run */
982 * Check for any dependents based on the '-r' or '-R' flags.
985 destroy_check_dependent(zfs_handle_t *zhp, void *data)
987 destroy_cbdata_t *cbp = data;
988 const char *tname = zfs_get_name(cbp->cb_target);
989 const char *name = zfs_get_name(zhp);
991 if (strncmp(tname, name, strlen(tname)) == 0 &&
992 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
994 * This is a direct descendant, not a clone somewhere else in
1000 if (cbp->cb_first) {
1001 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1002 "%s has children\n"),
1003 zfs_get_name(cbp->cb_target),
1004 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1005 (void) fprintf(stderr, gettext("use '-r' to destroy "
1006 "the following datasets:\n"));
1007 cbp->cb_first = B_FALSE;
1008 cbp->cb_error = B_TRUE;
1011 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1014 * This is a clone. We only want to report this if the '-r'
1015 * wasn't specified, or the target is a snapshot.
1017 if (!cbp->cb_recurse &&
1018 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1021 if (cbp->cb_first) {
1022 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1023 "%s has dependent clones\n"),
1024 zfs_get_name(cbp->cb_target),
1025 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1026 (void) fprintf(stderr, gettext("use '-R' to destroy "
1027 "the following datasets:\n"));
1028 cbp->cb_first = B_FALSE;
1029 cbp->cb_error = B_TRUE;
1030 cbp->cb_dryrun = B_TRUE;
1033 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1042 destroy_callback(zfs_handle_t *zhp, void *data)
1044 destroy_cbdata_t *cb = data;
1045 const char *name = zfs_get_name(zhp);
1047 if (cb->cb_verbose) {
1048 if (cb->cb_parsable) {
1049 (void) printf("destroy\t%s\n", name);
1050 } else if (cb->cb_dryrun) {
1051 (void) printf(gettext("would destroy %s\n"),
1054 (void) printf(gettext("will destroy %s\n"),
1060 * Ignore pools (which we've already flagged as an error before getting
1063 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1064 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1068 if (cb->cb_dryrun) {
1074 * We batch up all contiguous snapshots (even of different
1075 * filesystems) and destroy them with one ioctl. We can't
1076 * simply do all snap deletions and then all fs deletions,
1077 * because we must delete a clone before its origin.
1079 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1080 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1082 int error = zfs_destroy_snaps_nvl(g_zfs,
1083 cb->cb_batchedsnaps, B_FALSE);
1084 fnvlist_free(cb->cb_batchedsnaps);
1085 cb->cb_batchedsnaps = fnvlist_alloc();
1088 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1089 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1100 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1102 destroy_cbdata_t *cb = arg;
1103 const char *name = zfs_get_name(zhp);
1106 if (nvlist_exists(cb->cb_nvl, name)) {
1107 if (cb->cb_firstsnap == NULL)
1108 cb->cb_firstsnap = strdup(name);
1109 if (cb->cb_prevsnap != NULL)
1110 free(cb->cb_prevsnap);
1111 /* this snap continues the current range */
1112 cb->cb_prevsnap = strdup(name);
1113 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1115 if (cb->cb_verbose) {
1116 if (cb->cb_parsable) {
1117 (void) printf("destroy\t%s\n", name);
1118 } else if (cb->cb_dryrun) {
1119 (void) printf(gettext("would destroy %s\n"),
1122 (void) printf(gettext("will destroy %s\n"),
1126 } else if (cb->cb_firstsnap != NULL) {
1127 /* end of this range */
1129 err = lzc_snaprange_space(cb->cb_firstsnap,
1130 cb->cb_prevsnap, &used);
1131 cb->cb_snapused += used;
1132 free(cb->cb_firstsnap);
1133 cb->cb_firstsnap = NULL;
1134 free(cb->cb_prevsnap);
1135 cb->cb_prevsnap = NULL;
1142 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1145 assert(cb->cb_firstsnap == NULL);
1146 assert(cb->cb_prevsnap == NULL);
1147 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1148 if (cb->cb_firstsnap != NULL) {
1151 err = lzc_snaprange_space(cb->cb_firstsnap,
1152 cb->cb_prevsnap, &used);
1154 cb->cb_snapused += used;
1155 free(cb->cb_firstsnap);
1156 cb->cb_firstsnap = NULL;
1157 free(cb->cb_prevsnap);
1158 cb->cb_prevsnap = NULL;
1164 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1166 destroy_cbdata_t *cb = arg;
1169 /* Check for clones. */
1170 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1171 cb->cb_target = zhp;
1172 cb->cb_first = B_TRUE;
1173 err = zfs_iter_dependents(zhp, B_TRUE,
1174 destroy_check_dependent, cb);
1178 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1186 gather_snapshots(zfs_handle_t *zhp, void *arg)
1188 destroy_cbdata_t *cb = arg;
1191 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1197 if (cb->cb_verbose) {
1198 err = destroy_print_snapshots(zhp, cb);
1204 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1212 destroy_clones(destroy_cbdata_t *cb)
1215 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1217 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1218 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1221 boolean_t defer = cb->cb_defer_destroy;
1225 * We can't defer destroy non-snapshots, so set it to
1226 * false while destroying the clones.
1228 cb->cb_defer_destroy = B_FALSE;
1229 err = zfs_iter_dependents(zhp, B_FALSE,
1230 destroy_callback, cb);
1231 cb->cb_defer_destroy = defer;
1241 zfs_do_destroy(int argc, char **argv)
1243 destroy_cbdata_t cb = { 0 };
1247 zfs_handle_t *zhp = NULL;
1249 zfs_type_t type = ZFS_TYPE_DATASET;
1252 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1255 cb.cb_verbose = B_TRUE;
1258 cb.cb_verbose = B_TRUE;
1259 cb.cb_parsable = B_TRUE;
1262 cb.cb_dryrun = B_TRUE;
1265 cb.cb_defer_destroy = B_TRUE;
1266 type = ZFS_TYPE_SNAPSHOT;
1269 cb.cb_force = B_TRUE;
1272 cb.cb_recurse = B_TRUE;
1275 cb.cb_recurse = B_TRUE;
1276 cb.cb_doclones = B_TRUE;
1280 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1289 /* check number of arguments */
1291 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1295 (void) fprintf(stderr, gettext("too many arguments\n"));
1299 at = strchr(argv[0], '@');
1300 pound = strchr(argv[0], '#');
1303 /* Build the list of snaps to destroy in cb_nvl. */
1304 cb.cb_nvl = fnvlist_alloc();
1307 zhp = zfs_open(g_zfs, argv[0],
1308 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1312 cb.cb_snapspec = at + 1;
1313 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1319 if (nvlist_empty(cb.cb_nvl)) {
1320 (void) fprintf(stderr, gettext("could not find any "
1321 "snapshots to destroy; check snapshot names.\n"));
1326 if (cb.cb_verbose) {
1328 zfs_nicenum(cb.cb_snapused, buf, sizeof (buf));
1329 if (cb.cb_parsable) {
1330 (void) printf("reclaim\t%llu\n",
1332 } else if (cb.cb_dryrun) {
1333 (void) printf(gettext("would reclaim %s\n"),
1336 (void) printf(gettext("will reclaim %s\n"),
1341 if (!cb.cb_dryrun) {
1342 if (cb.cb_doclones) {
1343 cb.cb_batchedsnaps = fnvlist_alloc();
1344 err = destroy_clones(&cb);
1346 err = zfs_destroy_snaps_nvl(g_zfs,
1347 cb.cb_batchedsnaps, B_FALSE);
1355 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1356 cb.cb_defer_destroy);
1362 } else if (pound != NULL) {
1367 (void) fprintf(stderr,
1368 "dryrun is not supported with bookmark\n");
1372 if (cb.cb_defer_destroy) {
1373 (void) fprintf(stderr,
1374 "defer destroy is not supported with bookmark\n");
1378 if (cb.cb_recurse) {
1379 (void) fprintf(stderr,
1380 "recursive is not supported with bookmark\n");
1384 if (!zfs_bookmark_exists(argv[0])) {
1385 (void) fprintf(stderr, gettext("bookmark '%s' "
1386 "does not exist.\n"), argv[0]);
1390 nvl = fnvlist_alloc();
1391 fnvlist_add_boolean(nvl, argv[0]);
1393 err = lzc_destroy_bookmarks(nvl, NULL);
1395 (void) zfs_standard_error(g_zfs, err,
1396 "cannot destroy bookmark");
1399 nvlist_free(cb.cb_nvl);
1403 /* Open the given dataset */
1404 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1410 * Perform an explicit check for pools before going any further.
1412 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1413 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1414 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1415 "operation does not apply to pools\n"),
1417 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1418 "%s' to destroy all datasets in the pool\n"),
1420 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1421 "to destroy the pool itself\n"), zfs_get_name(zhp));
1427 * Check for any dependents and/or clones.
1429 cb.cb_first = B_TRUE;
1430 if (!cb.cb_doclones &&
1431 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1442 cb.cb_batchedsnaps = fnvlist_alloc();
1443 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1450 * Do the real thing. The callback will close the
1451 * handle regardless of whether it succeeds or not.
1453 err = destroy_callback(zhp, &cb);
1456 err = zfs_destroy_snaps_nvl(g_zfs,
1457 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1464 fnvlist_free(cb.cb_batchedsnaps);
1465 fnvlist_free(cb.cb_nvl);
1472 is_recvd_column(zprop_get_cbdata_t *cbp)
1475 zfs_get_column_t col;
1477 for (i = 0; i < ZFS_GET_NCOLS &&
1478 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1479 if (col == GET_COL_RECVD)
1485 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1486 * < all | property[,property]... > < fs | snap | vol > ...
1488 * -r recurse over any child datasets
1489 * -H scripted mode. Headers are stripped, and fields are separated
1490 * by tabs instead of spaces.
1491 * -o Set of fields to display. One of "name,property,value,
1492 * received,source". Default is "name,property,value,source".
1493 * "all" is an alias for all five.
1494 * -s Set of sources to allow. One of
1495 * "local,default,inherited,received,temporary,none". Default is
1497 * -p Display values in parsable (literal) format.
1499 * Prints properties for the given datasets. The user can control which
1500 * columns to display as well as which property types to allow.
1504 * Invoked to display the properties for a single dataset.
1507 get_callback(zfs_handle_t *zhp, void *data)
1509 char buf[ZFS_MAXPROPLEN];
1510 char rbuf[ZFS_MAXPROPLEN];
1511 zprop_source_t sourcetype;
1512 char source[ZFS_MAXNAMELEN];
1513 zprop_get_cbdata_t *cbp = data;
1514 nvlist_t *user_props = zfs_get_user_props(zhp);
1515 zprop_list_t *pl = cbp->cb_proplist;
1519 boolean_t received = is_recvd_column(cbp);
1521 for (; pl != NULL; pl = pl->pl_next) {
1522 char *recvdval = NULL;
1524 * Skip the special fake placeholder. This will also skip over
1525 * the name property when 'all' is specified.
1527 if (pl->pl_prop == ZFS_PROP_NAME &&
1528 pl == cbp->cb_proplist)
1531 if (pl->pl_prop != ZPROP_INVAL) {
1532 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1533 sizeof (buf), &sourcetype, source,
1535 cbp->cb_literal) != 0) {
1538 if (!zfs_prop_valid_for_type(pl->pl_prop,
1539 ZFS_TYPE_DATASET)) {
1540 (void) fprintf(stderr,
1541 gettext("No such property '%s'\n"),
1542 zfs_prop_to_name(pl->pl_prop));
1545 sourcetype = ZPROP_SRC_NONE;
1546 (void) strlcpy(buf, "-", sizeof (buf));
1549 if (received && (zfs_prop_get_recvd(zhp,
1550 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1551 cbp->cb_literal) == 0))
1554 zprop_print_one_property(zfs_get_name(zhp), cbp,
1555 zfs_prop_to_name(pl->pl_prop),
1556 buf, sourcetype, source, recvdval);
1557 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1558 sourcetype = ZPROP_SRC_LOCAL;
1560 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1561 buf, sizeof (buf), cbp->cb_literal) != 0) {
1562 sourcetype = ZPROP_SRC_NONE;
1563 (void) strlcpy(buf, "-", sizeof (buf));
1566 zprop_print_one_property(zfs_get_name(zhp), cbp,
1567 pl->pl_user_prop, buf, sourcetype, source, NULL);
1568 } else if (zfs_prop_written(pl->pl_user_prop)) {
1569 sourcetype = ZPROP_SRC_LOCAL;
1571 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1572 buf, sizeof (buf), cbp->cb_literal) != 0) {
1573 sourcetype = ZPROP_SRC_NONE;
1574 (void) strlcpy(buf, "-", sizeof (buf));
1577 zprop_print_one_property(zfs_get_name(zhp), cbp,
1578 pl->pl_user_prop, buf, sourcetype, source, NULL);
1580 if (nvlist_lookup_nvlist(user_props,
1581 pl->pl_user_prop, &propval) != 0) {
1584 sourcetype = ZPROP_SRC_NONE;
1587 verify(nvlist_lookup_string(propval,
1588 ZPROP_VALUE, &strval) == 0);
1589 verify(nvlist_lookup_string(propval,
1590 ZPROP_SOURCE, &sourceval) == 0);
1592 if (strcmp(sourceval,
1593 zfs_get_name(zhp)) == 0) {
1594 sourcetype = ZPROP_SRC_LOCAL;
1595 } else if (strcmp(sourceval,
1596 ZPROP_SOURCE_VAL_RECVD) == 0) {
1597 sourcetype = ZPROP_SRC_RECEIVED;
1599 sourcetype = ZPROP_SRC_INHERITED;
1600 (void) strlcpy(source,
1601 sourceval, sizeof (source));
1605 if (received && (zfs_prop_get_recvd(zhp,
1606 pl->pl_user_prop, rbuf, sizeof (rbuf),
1607 cbp->cb_literal) == 0))
1610 zprop_print_one_property(zfs_get_name(zhp), cbp,
1611 pl->pl_user_prop, strval, sourcetype,
1620 zfs_do_get(int argc, char **argv)
1622 zprop_get_cbdata_t cb = { 0 };
1623 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1624 int types = ZFS_TYPE_DATASET;
1625 char *value, *fields;
1628 zprop_list_t fake_name = { 0 };
1631 * Set up default columns and sources.
1633 cb.cb_sources = ZPROP_SRC_ALL;
1634 cb.cb_columns[0] = GET_COL_NAME;
1635 cb.cb_columns[1] = GET_COL_PROPERTY;
1636 cb.cb_columns[2] = GET_COL_VALUE;
1637 cb.cb_columns[3] = GET_COL_SOURCE;
1638 cb.cb_type = ZFS_TYPE_DATASET;
1641 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1644 cb.cb_literal = B_TRUE;
1647 limit = parse_depth(optarg, &flags);
1650 flags |= ZFS_ITER_RECURSE;
1653 cb.cb_scripted = B_TRUE;
1656 (void) fprintf(stderr, gettext("missing argument for "
1657 "'%c' option\n"), optopt);
1662 * Process the set of columns to display. We zero out
1663 * the structure to give us a blank slate.
1665 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1667 while (*optarg != '\0') {
1668 static char *col_subopts[] =
1669 { "name", "property", "value", "received",
1670 "source", "all", NULL };
1672 if (i == ZFS_GET_NCOLS) {
1673 (void) fprintf(stderr, gettext("too "
1674 "many fields given to -o "
1679 switch (getsubopt(&optarg, col_subopts,
1682 cb.cb_columns[i++] = GET_COL_NAME;
1685 cb.cb_columns[i++] = GET_COL_PROPERTY;
1688 cb.cb_columns[i++] = GET_COL_VALUE;
1691 cb.cb_columns[i++] = GET_COL_RECVD;
1692 flags |= ZFS_ITER_RECVD_PROPS;
1695 cb.cb_columns[i++] = GET_COL_SOURCE;
1699 (void) fprintf(stderr,
1700 gettext("\"all\" conflicts "
1701 "with specific fields "
1702 "given to -o option\n"));
1705 cb.cb_columns[0] = GET_COL_NAME;
1706 cb.cb_columns[1] = GET_COL_PROPERTY;
1707 cb.cb_columns[2] = GET_COL_VALUE;
1708 cb.cb_columns[3] = GET_COL_RECVD;
1709 cb.cb_columns[4] = GET_COL_SOURCE;
1710 flags |= ZFS_ITER_RECVD_PROPS;
1714 (void) fprintf(stderr,
1715 gettext("invalid column name "
1716 "'%s'\n"), suboptarg);
1724 while (*optarg != '\0') {
1725 static char *source_subopts[] = {
1726 "local", "default", "inherited",
1727 "received", "temporary", "none",
1730 switch (getsubopt(&optarg, source_subopts,
1733 cb.cb_sources |= ZPROP_SRC_LOCAL;
1736 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1739 cb.cb_sources |= ZPROP_SRC_INHERITED;
1742 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1745 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1748 cb.cb_sources |= ZPROP_SRC_NONE;
1751 (void) fprintf(stderr,
1752 gettext("invalid source "
1753 "'%s'\n"), suboptarg);
1761 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1762 while (*optarg != '\0') {
1763 static char *type_subopts[] = { "filesystem",
1764 "volume", "snapshot", "bookmark",
1767 switch (getsubopt(&optarg, type_subopts,
1770 types |= ZFS_TYPE_FILESYSTEM;
1773 types |= ZFS_TYPE_VOLUME;
1776 types |= ZFS_TYPE_SNAPSHOT;
1779 types |= ZFS_TYPE_BOOKMARK;
1782 types = ZFS_TYPE_DATASET |
1787 (void) fprintf(stderr,
1788 gettext("invalid type '%s'\n"),
1796 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1806 (void) fprintf(stderr, gettext("missing property "
1813 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1821 * As part of zfs_expand_proplist(), we keep track of the maximum column
1822 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1823 * need to know the maximum name length. However, the user likely did
1824 * not specify 'name' as one of the properties to fetch, so we need to
1825 * make sure we always include at least this property for
1826 * print_get_headers() to work properly.
1828 if (cb.cb_proplist != NULL) {
1829 fake_name.pl_prop = ZFS_PROP_NAME;
1830 fake_name.pl_width = strlen(gettext("NAME"));
1831 fake_name.pl_next = cb.cb_proplist;
1832 cb.cb_proplist = &fake_name;
1835 cb.cb_first = B_TRUE;
1837 /* run for each object */
1838 ret = zfs_for_each(argc, argv, flags, types, NULL,
1839 &cb.cb_proplist, limit, get_callback, &cb);
1841 if (cb.cb_proplist == &fake_name)
1842 zprop_free_list(fake_name.pl_next);
1844 zprop_free_list(cb.cb_proplist);
1850 * inherit [-rS] <property> <fs|vol> ...
1852 * -r Recurse over all children
1853 * -S Revert to received value, if any
1855 * For each dataset specified on the command line, inherit the given property
1856 * from its parent. Inheriting a property at the pool level will cause it to
1857 * use the default value. The '-r' flag will recurse over all children, and is
1858 * useful for setting a property on a hierarchy-wide basis, regardless of any
1859 * local modifications for each dataset.
1862 typedef struct inherit_cbdata {
1863 const char *cb_propname;
1864 boolean_t cb_received;
1868 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1870 inherit_cbdata_t *cb = data;
1871 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1874 * If we're doing it recursively, then ignore properties that
1875 * are not valid for this type of dataset.
1877 if (prop != ZPROP_INVAL &&
1878 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp)))
1881 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1885 inherit_cb(zfs_handle_t *zhp, void *data)
1887 inherit_cbdata_t *cb = data;
1889 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1893 zfs_do_inherit(int argc, char **argv)
1897 inherit_cbdata_t cb = { 0 };
1901 boolean_t received = B_FALSE;
1904 while ((c = getopt(argc, argv, "rS")) != -1) {
1907 flags |= ZFS_ITER_RECURSE;
1914 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1923 /* check number of arguments */
1925 (void) fprintf(stderr, gettext("missing property argument\n"));
1929 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1937 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1938 if (zfs_prop_readonly(prop)) {
1939 (void) fprintf(stderr, gettext(
1940 "%s property is read-only\n"),
1944 if (!zfs_prop_inheritable(prop) && !received) {
1945 (void) fprintf(stderr, gettext("'%s' property cannot "
1946 "be inherited\n"), propname);
1947 if (prop == ZFS_PROP_QUOTA ||
1948 prop == ZFS_PROP_RESERVATION ||
1949 prop == ZFS_PROP_REFQUOTA ||
1950 prop == ZFS_PROP_REFRESERVATION) {
1951 (void) fprintf(stderr, gettext("use 'zfs set "
1952 "%s=none' to clear\n"), propname);
1953 (void) fprintf(stderr, gettext("use 'zfs "
1954 "inherit -S %s' to revert to received "
1955 "value\n"), propname);
1959 if (received && (prop == ZFS_PROP_VOLSIZE ||
1960 prop == ZFS_PROP_VERSION)) {
1961 (void) fprintf(stderr, gettext("'%s' property cannot "
1962 "be reverted to a received value\n"), propname);
1965 } else if (!zfs_prop_user(propname)) {
1966 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
1971 cb.cb_propname = propname;
1972 cb.cb_received = received;
1974 if (flags & ZFS_ITER_RECURSE) {
1975 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1976 NULL, NULL, 0, inherit_recurse_cb, &cb);
1978 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
1979 NULL, NULL, 0, inherit_cb, &cb);
1985 typedef struct upgrade_cbdata {
1986 uint64_t cb_numupgraded;
1987 uint64_t cb_numsamegraded;
1988 uint64_t cb_numfailed;
1989 uint64_t cb_version;
1991 boolean_t cb_foundone;
1992 char cb_lastfs[ZFS_MAXNAMELEN];
1996 same_pool(zfs_handle_t *zhp, const char *name)
1998 int len1 = strcspn(name, "/@");
1999 const char *zhname = zfs_get_name(zhp);
2000 int len2 = strcspn(zhname, "/@");
2004 return (strncmp(name, zhname, len1) == 0);
2008 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2010 upgrade_cbdata_t *cb = data;
2011 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2013 /* list if it's old/new */
2014 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2015 (cb->cb_newer && version > ZPL_VERSION)) {
2018 str = gettext("The following filesystems are "
2019 "formatted using a newer software version and\n"
2020 "cannot be accessed on the current system.\n\n");
2022 str = gettext("The following filesystems are "
2023 "out of date, and can be upgraded. After being\n"
2024 "upgraded, these filesystems (and any 'zfs send' "
2025 "streams generated from\n"
2026 "subsequent snapshots) will no longer be "
2027 "accessible by older software versions.\n\n");
2030 if (!cb->cb_foundone) {
2032 (void) printf(gettext("VER FILESYSTEM\n"));
2033 (void) printf(gettext("--- ------------\n"));
2034 cb->cb_foundone = B_TRUE;
2037 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2044 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2046 upgrade_cbdata_t *cb = data;
2047 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2048 int needed_spa_version;
2051 if (zfs_spa_version(zhp, &spa_version) < 0)
2054 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2056 if (needed_spa_version < 0)
2059 if (spa_version < needed_spa_version) {
2061 (void) printf(gettext("%s: can not be "
2062 "upgraded; the pool version needs to first "
2063 "be upgraded\nto version %d\n\n"),
2064 zfs_get_name(zhp), needed_spa_version);
2070 if (version < cb->cb_version) {
2072 (void) snprintf(verstr, sizeof (verstr),
2073 "%llu", cb->cb_version);
2074 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2076 * If they did "zfs upgrade -a", then we could
2077 * be doing ioctls to different pools. We need
2078 * to log this history once to each pool, and bypass
2079 * the normal history logging that happens in main().
2081 (void) zpool_log_history(g_zfs, history_str);
2082 log_history = B_FALSE;
2084 if (zfs_prop_set(zhp, "version", verstr) == 0)
2085 cb->cb_numupgraded++;
2088 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2089 } else if (version > cb->cb_version) {
2090 /* can't downgrade */
2091 (void) printf(gettext("%s: can not be downgraded; "
2092 "it is already at version %u\n"),
2093 zfs_get_name(zhp), version);
2096 cb->cb_numsamegraded++;
2104 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2107 zfs_do_upgrade(int argc, char **argv)
2109 boolean_t all = B_FALSE;
2110 boolean_t showversions = B_FALSE;
2112 upgrade_cbdata_t cb = { 0 };
2114 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2117 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2120 flags |= ZFS_ITER_RECURSE;
2123 showversions = B_TRUE;
2126 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2127 optarg, &cb.cb_version) != 0) {
2128 (void) fprintf(stderr,
2129 gettext("invalid version %s\n"), optarg);
2138 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2147 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2149 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2150 cb.cb_version || argc))
2152 if ((all || argc) && (showversions))
2158 /* Show info on available versions. */
2159 (void) printf(gettext("The following filesystem versions are "
2161 (void) printf(gettext("VER DESCRIPTION\n"));
2162 (void) printf("--- -----------------------------------------"
2163 "---------------\n");
2164 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2165 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2166 (void) printf(gettext(" 3 Case insensitive and filesystem "
2167 "user identifier (FUID)\n"));
2168 (void) printf(gettext(" 4 userquota, groupquota "
2170 (void) printf(gettext(" 5 System attributes\n"));
2171 (void) printf(gettext("\nFor more information on a particular "
2172 "version, including supported releases,\n"));
2173 (void) printf("see the ZFS Administration Guide.\n\n");
2175 } else if (argc || all) {
2176 /* Upgrade filesystems */
2177 if (cb.cb_version == 0)
2178 cb.cb_version = ZPL_VERSION;
2179 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2180 NULL, NULL, 0, upgrade_set_callback, &cb);
2181 (void) printf(gettext("%llu filesystems upgraded\n"),
2183 if (cb.cb_numsamegraded) {
2184 (void) printf(gettext("%llu filesystems already at "
2186 cb.cb_numsamegraded);
2188 if (cb.cb_numfailed != 0)
2191 /* List old-version filesytems */
2193 (void) printf(gettext("This system is currently running "
2194 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2196 flags |= ZFS_ITER_RECURSE;
2197 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2198 NULL, NULL, 0, upgrade_list_callback, &cb);
2200 found = cb.cb_foundone;
2201 cb.cb_foundone = B_FALSE;
2202 cb.cb_newer = B_TRUE;
2204 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2205 NULL, NULL, 0, upgrade_list_callback, &cb);
2207 if (!cb.cb_foundone && !found) {
2208 (void) printf(gettext("All filesystems are "
2209 "formatted with the current version.\n"));
2217 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2218 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2219 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2220 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2222 * -H Scripted mode; elide headers and separate columns by tabs.
2223 * -i Translate SID to POSIX ID.
2224 * -n Print numeric ID instead of user/group name.
2225 * -o Control which fields to display.
2226 * -p Use exact (parsable) numeric output.
2227 * -s Specify sort columns, descending order.
2228 * -S Specify sort columns, ascending order.
2229 * -t Control which object types to display.
2231 * Displays space consumed by, and quotas on, each user in the specified
2232 * filesystem or snapshot.
2235 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2236 enum us_field_types {
2242 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA" };
2243 static char *us_field_names[] = { "type", "name", "used", "quota" };
2244 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2246 #define USTYPE_PSX_GRP (1 << 0)
2247 #define USTYPE_PSX_USR (1 << 1)
2248 #define USTYPE_SMB_GRP (1 << 2)
2249 #define USTYPE_SMB_USR (1 << 3)
2250 #define USTYPE_ALL \
2251 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2253 static int us_type_bits[] = {
2260 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2263 typedef struct us_node {
2265 uu_avl_node_t usn_avlnode;
2266 uu_list_node_t usn_listnode;
2269 typedef struct us_cbdata {
2271 uu_avl_pool_t *cb_avl_pool;
2273 boolean_t cb_numname;
2274 boolean_t cb_nicenum;
2275 boolean_t cb_sid2posix;
2276 zfs_userquota_prop_t cb_prop;
2277 zfs_sort_column_t *cb_sortcol;
2278 size_t cb_width[USFIELD_LAST];
2281 static boolean_t us_populated = B_FALSE;
2284 zfs_sort_column_t *si_sortcol;
2285 boolean_t si_numname;
2289 us_field_index(char *field)
2293 for (i = 0; i < USFIELD_LAST; i++) {
2294 if (strcmp(field, us_field_names[i]) == 0)
2302 us_compare(const void *larg, const void *rarg, void *unused)
2304 const us_node_t *l = larg;
2305 const us_node_t *r = rarg;
2306 us_sort_info_t *si = (us_sort_info_t *)unused;
2307 zfs_sort_column_t *sortcol = si->si_sortcol;
2308 boolean_t numname = si->si_numname;
2309 nvlist_t *lnvl = l->usn_nvl;
2310 nvlist_t *rnvl = r->usn_nvl;
2314 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2321 zfs_prop_t prop = sortcol->sc_prop;
2322 const char *propname = NULL;
2323 boolean_t reverse = sortcol->sc_reverse;
2328 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2329 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2331 rc = (rv32 < lv32) ? 1 : -1;
2336 (void) nvlist_lookup_uint64(lnvl, propname,
2338 (void) nvlist_lookup_uint64(rnvl, propname,
2341 rc = (rv64 < lv64) ? 1 : -1;
2343 (void) nvlist_lookup_string(lnvl, propname,
2345 (void) nvlist_lookup_string(rnvl, propname,
2347 rc = strcmp(lvstr, rvstr);
2351 case ZFS_PROP_QUOTA:
2354 if (prop == ZFS_PROP_USED)
2358 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2359 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2361 rc = (rv64 < lv64) ? 1 : -1;
2370 return (reverse ? 1 : -1);
2372 return (reverse ? -1 : 1);
2377 * If entries still seem to be the same, check if they are of the same
2378 * type (smbentity is added only if we are doing SID to POSIX ID
2379 * translation where we can have duplicate type/name combinations).
2381 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2382 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2384 return (lvb < rvb ? -1 : 1);
2389 static inline const char *
2390 us_type2str(unsigned field_type)
2392 switch (field_type) {
2393 case USTYPE_PSX_USR:
2394 return ("POSIX User");
2395 case USTYPE_PSX_GRP:
2396 return ("POSIX Group");
2397 case USTYPE_SMB_USR:
2398 return ("SMB User");
2399 case USTYPE_SMB_GRP:
2400 return ("SMB Group");
2402 return ("Undefined");
2407 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2409 us_cbdata_t *cb = (us_cbdata_t *)arg;
2410 zfs_userquota_prop_t prop = cb->cb_prop;
2415 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2416 uu_avl_t *avl = cb->cb_avl;
2420 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2422 const char *typestr;
2426 int typeidx, nameidx, sizeidx;
2427 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2428 boolean_t smbentity = B_FALSE;
2430 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2432 node = safe_malloc(sizeof (us_node_t));
2433 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2434 node->usn_nvl = props;
2436 if (domain != NULL && domain[0] != '\0') {
2438 char sid[ZFS_MAXNAMELEN + 32];
2442 int flag = IDMAP_REQ_FLG_USE_CACHE;
2447 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2449 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2450 type = USTYPE_SMB_GRP;
2452 err = sid_to_id(sid, B_FALSE, &id);
2455 type = USTYPE_SMB_USR;
2457 err = sid_to_id(sid, B_TRUE, &id);
2464 if (!cb->cb_sid2posix) {
2465 if (type == USTYPE_SMB_USR) {
2466 (void) idmap_getwinnamebyuid(rid, flag,
2469 (void) idmap_getwinnamebygid(rid, flag,
2479 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2481 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2482 type = USTYPE_PSX_GRP;
2483 if (!cb->cb_numname) {
2486 if ((g = getgrgid(rid)) != NULL)
2490 type = USTYPE_PSX_USR;
2491 if (!cb->cb_numname) {
2494 if ((p = getpwuid(rid)) != NULL)
2501 * Make sure that the type/name combination is unique when doing
2502 * SID to POSIX ID translation (hence changing the type from SMB to
2505 if (cb->cb_sid2posix &&
2506 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2509 /* Calculate/update width of TYPE field */
2510 typestr = us_type2str(type);
2511 typelen = strlen(gettext(typestr));
2512 typeidx = us_field_index("type");
2513 if (typelen > cb->cb_width[typeidx])
2514 cb->cb_width[typeidx] = typelen;
2515 if (nvlist_add_uint32(props, "type", type) != 0)
2518 /* Calculate/update width of NAME field */
2519 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2520 if (nvlist_add_uint64(props, "name", rid) != 0)
2522 namelen = snprintf(NULL, 0, "%u", rid);
2524 if (nvlist_add_string(props, "name", name) != 0)
2526 namelen = strlen(name);
2528 nameidx = us_field_index("name");
2529 if (namelen > cb->cb_width[nameidx])
2530 cb->cb_width[nameidx] = namelen;
2533 * Check if this type/name combination is in the list and update it;
2534 * otherwise add new node to the list.
2536 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2537 uu_avl_insert(avl, node, idx);
2542 props = node->usn_nvl;
2545 /* Calculate/update width of USED/QUOTA fields */
2547 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2549 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu", space);
2550 sizelen = strlen(sizebuf);
2551 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2553 if (!nvlist_exists(props, "quota"))
2554 (void) nvlist_add_uint64(props, "quota", 0);
2557 if (!nvlist_exists(props, "used"))
2558 (void) nvlist_add_uint64(props, "used", 0);
2560 sizeidx = us_field_index(propname);
2561 if (sizelen > cb->cb_width[sizeidx])
2562 cb->cb_width[sizeidx] = sizelen;
2564 if (nvlist_add_uint64(props, propname, space) != 0)
2571 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2572 size_t *width, us_node_t *node)
2574 nvlist_t *nvl = node->usn_nvl;
2575 char valstr[ZFS_MAXNAMELEN];
2576 boolean_t first = B_TRUE;
2582 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2583 if (!(ustype & types))
2586 while ((field = fields[cfield]) != USFIELD_LAST) {
2587 nvpair_t *nvp = NULL;
2591 char *strval = NULL;
2593 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2594 if (strcmp(nvpair_name(nvp),
2595 us_field_names[field]) == 0)
2599 type = nvpair_type(nvp);
2601 case DATA_TYPE_UINT32:
2602 (void) nvpair_value_uint32(nvp, &val32);
2604 case DATA_TYPE_UINT64:
2605 (void) nvpair_value_uint64(nvp, &val64);
2607 case DATA_TYPE_STRING:
2608 (void) nvpair_value_string(nvp, &strval);
2611 (void) fprintf(stderr, "invalid data type\n");
2616 strval = (char *)us_type2str(val32);
2619 if (type == DATA_TYPE_UINT64) {
2620 (void) sprintf(valstr, "%llu", val64);
2626 if (type == DATA_TYPE_UINT64) {
2628 (void) sprintf(valstr, "%llu", val64);
2630 zfs_nicenum(val64, valstr,
2633 if (field == USFIELD_QUOTA &&
2634 strcmp(valstr, "0") == 0)
2644 (void) printf("\t");
2649 (void) printf("%s", strval);
2650 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2651 (void) printf("%-*s", width[field], strval);
2653 (void) printf("%*s", width[field], strval);
2659 (void) printf("\n");
2663 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2664 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2672 boolean_t first = B_TRUE;
2674 while ((field = fields[cfield]) != USFIELD_LAST) {
2675 col = gettext(us_field_hdr[field]);
2676 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2677 (void) printf(first ? "%-*s" : " %-*s",
2680 (void) printf(first ? "%*s" : " %*s",
2686 (void) printf("\n");
2689 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2690 print_us_node(scripted, parsable, fields, types, width, node);
2692 nvlist_free(node->usn_nvl);
2697 zfs_do_userspace(int argc, char **argv)
2700 zfs_userquota_prop_t p;
2702 uu_avl_pool_t *avl_pool;
2704 uu_avl_walk_t *walk;
2706 char deffields[] = "type,name,used,quota";
2707 char *ofield = NULL;
2708 char *tfield = NULL;
2712 boolean_t scripted = B_FALSE;
2713 boolean_t prtnum = B_FALSE;
2714 boolean_t parsable = B_FALSE;
2715 boolean_t sid2posix = B_FALSE;
2718 zfs_sort_column_t *sortcol = NULL;
2719 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2723 uu_list_pool_t *listpool;
2725 uu_avl_index_t idx = 0;
2726 uu_list_index_t idx2 = 0;
2731 if (strcmp(argv[0], "groupspace") == 0)
2732 /* Toggle default group types */
2733 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2735 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2751 if (zfs_add_sort_column(&sortcol, optarg,
2752 c == 's' ? B_FALSE : B_TRUE) != 0) {
2753 (void) fprintf(stderr,
2754 gettext("invalid field '%s'\n"), optarg);
2765 (void) fprintf(stderr, gettext("missing argument for "
2766 "'%c' option\n"), optopt);
2770 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2780 (void) fprintf(stderr, gettext("missing dataset name\n"));
2784 (void) fprintf(stderr, gettext("too many arguments\n"));
2788 /* Use default output fields if not specified using -o */
2792 if ((delim = strchr(ofield, ',')) != NULL)
2794 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2795 (void) fprintf(stderr, gettext("invalid type '%s' "
2796 "for -o option\n"), ofield);
2801 } while (delim != NULL);
2802 fields[cfield] = USFIELD_LAST;
2804 /* Override output types (-t option) */
2805 if (tfield != NULL) {
2809 boolean_t found = B_FALSE;
2811 if ((delim = strchr(tfield, ',')) != NULL)
2813 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2815 if (strcmp(tfield, us_type_names[i]) == 0) {
2817 types |= us_type_bits[i];
2822 (void) fprintf(stderr, gettext("invalid type "
2823 "'%s' for -t option\n"), tfield);
2828 } while (delim != NULL);
2831 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2834 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2835 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2837 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2840 /* Always add default sorting columns */
2841 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2842 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2844 cb.cb_sortcol = sortcol;
2845 cb.cb_numname = prtnum;
2846 cb.cb_nicenum = !parsable;
2847 cb.cb_avl_pool = avl_pool;
2848 cb.cb_avl = avl_tree;
2849 cb.cb_sid2posix = sid2posix;
2851 for (i = 0; i < USFIELD_LAST; i++)
2852 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2854 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2855 if (((p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA) &&
2856 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2857 ((p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA) &&
2858 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2861 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2866 if ((node = uu_avl_first(avl_tree)) == NULL)
2869 us_populated = B_TRUE;
2871 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2872 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2873 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2874 uu_list_node_init(node, &node->usn_listnode, listpool);
2876 while (node != NULL) {
2878 node = uu_avl_next(avl_tree, node);
2879 uu_avl_remove(avl_tree, rmnode);
2880 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2881 uu_list_insert(list, rmnode, idx2);
2884 for (node = uu_list_first(list); node != NULL;
2885 node = uu_list_next(list, node)) {
2886 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2888 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
2889 uu_avl_insert(avl_tree, node, idx);
2892 uu_list_destroy(list);
2893 uu_list_pool_destroy(listpool);
2895 /* Print and free node nvlist memory */
2896 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
2899 zfs_free_sort_columns(sortcol);
2901 /* Clean up the AVL tree */
2902 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
2905 while ((node = uu_avl_walk_next(walk)) != NULL) {
2906 uu_avl_remove(cb.cb_avl, node);
2910 uu_avl_walk_end(walk);
2911 uu_avl_destroy(avl_tree);
2912 uu_avl_pool_destroy(avl_pool);
2918 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property] ...
2919 * [-t type[,...]] [filesystem|volume|snapshot] ...
2921 * -H Scripted mode; elide headers and separate columns by tabs.
2922 * -p Display values in parsable (literal) format.
2923 * -r Recurse over all children.
2924 * -d Limit recursion by depth.
2925 * -o Control which fields to display.
2926 * -s Specify sort columns, descending order.
2927 * -S Specify sort columns, ascending order.
2928 * -t Control which object types to display.
2930 * When given no arguments, list all filesystems in the system.
2931 * Otherwise, list the specified datasets, optionally recursing down them if
2932 * '-r' is specified.
2934 typedef struct list_cbdata {
2936 boolean_t cb_literal;
2937 boolean_t cb_scripted;
2938 zprop_list_t *cb_proplist;
2942 * Given a list of columns to display, output appropriate headers for each one.
2945 print_header(list_cbdata_t *cb)
2947 zprop_list_t *pl = cb->cb_proplist;
2948 char headerbuf[ZFS_MAXPROPLEN];
2951 boolean_t first = B_TRUE;
2952 boolean_t right_justify;
2954 for (; pl != NULL; pl = pl->pl_next) {
2961 right_justify = B_FALSE;
2962 if (pl->pl_prop != ZPROP_INVAL) {
2963 header = zfs_prop_column_name(pl->pl_prop);
2964 right_justify = zfs_prop_align_right(pl->pl_prop);
2966 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
2967 headerbuf[i] = toupper(pl->pl_user_prop[i]);
2968 headerbuf[i] = '\0';
2972 if (pl->pl_next == NULL && !right_justify)
2973 (void) printf("%s", header);
2974 else if (right_justify)
2975 (void) printf("%*s", pl->pl_width, header);
2977 (void) printf("%-*s", pl->pl_width, header);
2980 (void) printf("\n");
2984 * Given a dataset and a list of fields, print out all the properties according
2985 * to the described layout.
2988 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
2990 zprop_list_t *pl = cb->cb_proplist;
2991 boolean_t first = B_TRUE;
2992 char property[ZFS_MAXPROPLEN];
2993 nvlist_t *userprops = zfs_get_user_props(zhp);
2996 boolean_t right_justify;
2998 for (; pl != NULL; pl = pl->pl_next) {
3000 if (cb->cb_scripted)
3001 (void) printf("\t");
3008 if (pl->pl_prop == ZFS_PROP_NAME) {
3009 (void) strlcpy(property, zfs_get_name(zhp),
3012 right_justify = zfs_prop_align_right(pl->pl_prop);
3013 } else if (pl->pl_prop != ZPROP_INVAL) {
3014 if (zfs_prop_get(zhp, pl->pl_prop, property,
3015 sizeof (property), NULL, NULL, 0,
3016 cb->cb_literal) != 0)
3020 right_justify = zfs_prop_align_right(pl->pl_prop);
3021 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3022 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3023 property, sizeof (property), cb->cb_literal) != 0)
3027 right_justify = B_TRUE;
3028 } else if (zfs_prop_written(pl->pl_user_prop)) {
3029 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3030 property, sizeof (property), cb->cb_literal) != 0)
3034 right_justify = B_TRUE;
3036 if (nvlist_lookup_nvlist(userprops,
3037 pl->pl_user_prop, &propval) != 0)
3040 verify(nvlist_lookup_string(propval,
3041 ZPROP_VALUE, &propstr) == 0);
3042 right_justify = B_FALSE;
3046 * If this is being called in scripted mode, or if this is the
3047 * last column and it is left-justified, don't include a width
3050 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3051 (void) printf("%s", propstr);
3052 else if (right_justify)
3053 (void) printf("%*s", pl->pl_width, propstr);
3055 (void) printf("%-*s", pl->pl_width, propstr);
3058 (void) printf("\n");
3062 * Generic callback function to list a dataset or snapshot.
3065 list_callback(zfs_handle_t *zhp, void *data)
3067 list_cbdata_t *cbp = data;
3069 if (cbp->cb_first) {
3070 if (!cbp->cb_scripted)
3072 cbp->cb_first = B_FALSE;
3075 print_dataset(zhp, cbp);
3081 zfs_do_list(int argc, char **argv)
3084 static char default_fields[] =
3085 "name,used,available,referenced,mountpoint";
3086 int types = ZFS_TYPE_DATASET;
3087 boolean_t types_specified = B_FALSE;
3088 char *fields = NULL;
3089 list_cbdata_t cb = { 0 };
3093 zfs_sort_column_t *sortcol = NULL;
3094 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3097 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3103 cb.cb_literal = B_TRUE;
3104 flags |= ZFS_ITER_LITERAL_PROPS;
3107 limit = parse_depth(optarg, &flags);
3110 flags |= ZFS_ITER_RECURSE;
3113 cb.cb_scripted = B_TRUE;
3116 if (zfs_add_sort_column(&sortcol, optarg,
3118 (void) fprintf(stderr,
3119 gettext("invalid property '%s'\n"), optarg);
3124 if (zfs_add_sort_column(&sortcol, optarg,
3126 (void) fprintf(stderr,
3127 gettext("invalid property '%s'\n"), optarg);
3133 types_specified = B_TRUE;
3134 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3135 while (*optarg != '\0') {
3136 static char *type_subopts[] = { "filesystem",
3137 "volume", "snapshot", "snap", "bookmark",
3140 switch (getsubopt(&optarg, type_subopts,
3143 types |= ZFS_TYPE_FILESYSTEM;
3146 types |= ZFS_TYPE_VOLUME;
3150 types |= ZFS_TYPE_SNAPSHOT;
3153 types |= ZFS_TYPE_BOOKMARK;
3156 types = ZFS_TYPE_DATASET |
3160 (void) fprintf(stderr,
3161 gettext("invalid type '%s'\n"),
3168 (void) fprintf(stderr, gettext("missing argument for "
3169 "'%c' option\n"), optopt);
3173 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3183 fields = default_fields;
3186 * If we are only going to list snapshot names and sort by name,
3187 * then we can use faster version.
3189 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3190 flags |= ZFS_ITER_SIMPLE;
3193 * If "-o space" and no types were specified, don't display snapshots.
3195 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3196 types &= ~ZFS_TYPE_SNAPSHOT;
3199 * If the user specifies '-o all', the zprop_get_list() doesn't
3200 * normally include the name of the dataset. For 'zfs list', we always
3201 * want this property to be first.
3203 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3207 cb.cb_first = B_TRUE;
3209 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3210 limit, list_callback, &cb);
3212 zprop_free_list(cb.cb_proplist);
3213 zfs_free_sort_columns(sortcol);
3215 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3216 (void) printf(gettext("no datasets available\n"));
3222 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3223 * zfs rename [-f] -p <fs | vol> <fs | vol>
3224 * zfs rename -r <snap> <snap>
3225 * zfs rename -u [-p] <fs> <fs>
3227 * Renames the given dataset to another of the same type.
3229 * The '-p' flag creates all the non-existing ancestors of the target first.
3233 zfs_do_rename(int argc, char **argv)
3236 renameflags_t flags = { 0 };
3240 boolean_t parents = B_FALSE;
3241 char *snapshot = NULL;
3244 while ((c = getopt(argc, argv, "fpru")) != -1) {
3250 flags.recurse = B_TRUE;
3253 flags.nounmount = B_TRUE;
3256 flags.forceunmount = B_TRUE;
3260 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3269 /* check number of arguments */
3271 (void) fprintf(stderr, gettext("missing source dataset "
3276 (void) fprintf(stderr, gettext("missing target dataset "
3281 (void) fprintf(stderr, gettext("too many arguments\n"));
3285 if (flags.recurse && parents) {
3286 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3291 if (flags.recurse && strchr(argv[0], '@') == 0) {
3292 (void) fprintf(stderr, gettext("source dataset for recursive "
3293 "rename must be a snapshot\n"));
3297 if (flags.nounmount && parents) {
3298 (void) fprintf(stderr, gettext("-u and -p options are mutually "
3303 if (flags.nounmount)
3304 types = ZFS_TYPE_FILESYSTEM;
3306 types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
3308 types = ZFS_TYPE_DATASET;
3310 if (flags.recurse) {
3312 * When we do recursive rename we are fine when the given
3313 * snapshot for the given dataset doesn't exist - it can
3314 * still exists below.
3317 snapshot = strchr(argv[0], '@');
3318 assert(snapshot != NULL);
3323 if ((zhp = zfs_open(g_zfs, argv[0], types)) == NULL)
3326 /* If we were asked and the name looks good, try to create ancestors. */
3327 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3328 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3333 ret = (zfs_rename(zhp, snapshot, argv[1], flags) != 0);
3342 * Promotes the given clone fs to be the parent
3346 zfs_do_promote(int argc, char **argv)
3352 if (argc > 1 && argv[1][0] == '-') {
3353 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3358 /* check number of arguments */
3360 (void) fprintf(stderr, gettext("missing clone filesystem"
3365 (void) fprintf(stderr, gettext("too many arguments\n"));
3369 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3373 ret = (zfs_promote(zhp) != 0);
3381 * zfs rollback [-rRf] <snapshot>
3383 * -r Delete any intervening snapshots before doing rollback
3384 * -R Delete any snapshots and their clones
3385 * -f ignored for backwards compatability
3387 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3388 * since then and making it the active dataset. If more recent snapshots exist,
3389 * the command will complain unless the '-r' flag is given.
3391 typedef struct rollback_cbdata {
3397 boolean_t cb_recurse;
3398 } rollback_cbdata_t;
3401 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3403 rollback_cbdata_t *cbp = data;
3405 if (cbp->cb_first && cbp->cb_recurse) {
3406 (void) fprintf(stderr, gettext("cannot rollback to "
3407 "'%s': clones of previous snapshots exist\n"),
3409 (void) fprintf(stderr, gettext("use '-R' to "
3410 "force deletion of the following clones and "
3416 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3423 * Report any snapshots more recent than the one specified. Used when '-r' is
3424 * not specified. We reuse this same callback for the snapshot dependents - if
3425 * 'cb_dependent' is set, then this is a dependent and we should report it
3426 * without checking the transaction group.
3429 rollback_check(zfs_handle_t *zhp, void *data)
3431 rollback_cbdata_t *cbp = data;
3433 if (cbp->cb_doclones) {
3438 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3439 if (cbp->cb_first && !cbp->cb_recurse) {
3440 (void) fprintf(stderr, gettext("cannot "
3441 "rollback to '%s': more recent snapshots "
3442 "or bookmarks exist\n"),
3444 (void) fprintf(stderr, gettext("use '-r' to "
3445 "force deletion of the following "
3446 "snapshots and bookmarks:\n"));
3451 if (cbp->cb_recurse) {
3452 if (zfs_iter_dependents(zhp, B_TRUE,
3453 rollback_check_dependent, cbp) != 0) {
3458 (void) fprintf(stderr, "%s\n",
3467 zfs_do_rollback(int argc, char **argv)
3471 boolean_t force = B_FALSE;
3472 rollback_cbdata_t cb = { 0 };
3473 zfs_handle_t *zhp, *snap;
3474 char parentname[ZFS_MAXNAMELEN];
3478 while ((c = getopt(argc, argv, "rRf")) != -1) {
3491 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3500 /* check number of arguments */
3502 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3506 (void) fprintf(stderr, gettext("too many arguments\n"));
3510 /* open the snapshot */
3511 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3514 /* open the parent dataset */
3515 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3516 verify((delim = strrchr(parentname, '@')) != NULL);
3518 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3524 * Check for more recent snapshots and/or clones based on the presence
3527 cb.cb_target = argv[0];
3528 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3529 cb.cb_first = B_TRUE;
3531 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3533 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3536 if ((ret = cb.cb_error) != 0)
3540 * Rollback parent to the given snapshot.
3542 ret = zfs_rollback(zhp, snap, force);
3555 * zfs set property=value ... { fs | snap | vol } ...
3557 * Sets the given properties for all datasets specified on the command line.
3561 set_callback(zfs_handle_t *zhp, void *data)
3563 nvlist_t *props = data;
3565 if (zfs_prop_set_list(zhp, props) != 0) {
3566 switch (libzfs_errno(g_zfs)) {
3567 case EZFS_MOUNTFAILED:
3568 (void) fprintf(stderr, gettext("property may be set "
3569 "but unable to remount filesystem\n"));
3571 case EZFS_SHARENFSFAILED:
3572 (void) fprintf(stderr, gettext("property may be set "
3573 "but unable to reshare filesystem\n"));
3582 zfs_do_set(int argc, char **argv)
3584 nvlist_t *props = NULL;
3585 int ds_start = -1; /* argv idx of first dataset arg */
3588 /* check for options */
3589 if (argc > 1 && argv[1][0] == '-') {
3590 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3595 /* check number of arguments */
3597 (void) fprintf(stderr, gettext("missing arguments\n"));
3601 if (strchr(argv[1], '=') == NULL) {
3602 (void) fprintf(stderr, gettext("missing property=value "
3605 (void) fprintf(stderr, gettext("missing dataset "
3611 /* validate argument order: prop=val args followed by dataset args */
3612 for (i = 1; i < argc; i++) {
3613 if (strchr(argv[i], '=') != NULL) {
3615 /* out-of-order prop=val argument */
3616 (void) fprintf(stderr, gettext("invalid "
3617 "argument order\n"), i);
3620 } else if (ds_start < 0) {
3625 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3629 /* Populate a list of property settings */
3630 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3632 for (i = 1; i < ds_start; i++) {
3633 if ((ret = parseprop(props, argv[i])) != 0)
3637 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3638 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3645 typedef struct snap_cbdata {
3647 boolean_t sd_recursive;
3648 const char *sd_snapname;
3652 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3654 snap_cbdata_t *sd = arg;
3659 if (sd->sd_recursive &&
3660 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3665 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3668 fnvlist_add_boolean(sd->sd_nvl, name);
3671 if (sd->sd_recursive)
3672 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3678 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3680 * Creates a snapshot with the given name. While functionally equivalent to
3681 * 'zfs create', it is a separate command to differentiate intent.
3684 zfs_do_snapshot(int argc, char **argv)
3689 snap_cbdata_t sd = { 0 };
3690 boolean_t multiple_snaps = B_FALSE;
3692 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3694 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3698 while ((c = getopt(argc, argv, "ro:")) != -1) {
3701 if (parseprop(props, optarg) != 0)
3705 sd.sd_recursive = B_TRUE;
3706 multiple_snaps = B_TRUE;
3709 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3718 /* check number of arguments */
3720 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3725 multiple_snaps = B_TRUE;
3726 for (; argc > 0; argc--, argv++) {
3730 atp = strchr(argv[0], '@');
3734 sd.sd_snapname = atp + 1;
3735 zhp = zfs_open(g_zfs, argv[0],
3736 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3739 if (zfs_snapshot_cb(zhp, &sd) != 0)
3743 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3744 nvlist_free(sd.sd_nvl);
3746 if (ret != 0 && multiple_snaps)
3747 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3751 nvlist_free(sd.sd_nvl);
3758 * Send a backup stream to stdout.
3761 zfs_do_send(int argc, char **argv)
3763 char *fromname = NULL;
3764 char *toname = NULL;
3765 char *resume_token = NULL;
3768 sendflags_t flags = { 0 };
3770 nvlist_t *dbgnv = NULL;
3771 boolean_t extraverbose = B_FALSE;
3774 while ((c = getopt(argc, argv, ":i:I:RDpvnPLet:")) != -1) {
3785 flags.doall = B_TRUE;
3788 flags.replicate = B_TRUE;
3791 flags.props = B_TRUE;
3794 flags.parsable = B_TRUE;
3795 flags.verbose = B_TRUE;
3799 extraverbose = B_TRUE;
3800 flags.verbose = B_TRUE;
3801 flags.progress = B_TRUE;
3804 flags.dedup = B_TRUE;
3807 flags.dryrun = B_TRUE;
3810 flags.largeblock = B_TRUE;
3813 flags.embed_data = B_TRUE;
3816 resume_token = optarg;
3819 (void) fprintf(stderr, gettext("missing argument for "
3820 "'%c' option\n"), optopt);
3824 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3833 if (resume_token != NULL) {
3834 if (fromname != NULL || flags.replicate || flags.props ||
3836 (void) fprintf(stderr,
3837 gettext("invalid flags combined with -t\n"));
3841 (void) fprintf(stderr, gettext("no additional "
3842 "arguments are permitted with -t\n"));
3847 (void) fprintf(stderr,
3848 gettext("missing snapshot argument\n"));
3852 (void) fprintf(stderr, gettext("too many arguments\n"));
3857 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
3858 (void) fprintf(stderr,
3859 gettext("Error: Stream can not be written to a terminal.\n"
3860 "You must redirect standard output.\n"));
3864 if (resume_token != NULL) {
3865 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
3870 * Special case sending a filesystem, or from a bookmark.
3872 if (strchr(argv[0], '@') == NULL ||
3873 (fromname && strchr(fromname, '#') != NULL)) {
3874 char frombuf[ZFS_MAXNAMELEN];
3875 enum lzc_send_flags lzc_flags = 0;
3877 if (flags.replicate || flags.doall || flags.props ||
3878 flags.dedup || flags.dryrun || flags.verbose ||
3880 (void) fprintf(stderr,
3882 "Unsupported flag with filesystem or bookmark.\n"));
3886 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
3890 if (flags.largeblock)
3891 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
3892 if (flags.embed_data)
3893 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
3895 if (fromname != NULL &&
3896 (fromname[0] == '#' || fromname[0] == '@')) {
3898 * Incremental source name begins with # or @.
3899 * Default to same fs as target.
3901 (void) strncpy(frombuf, argv[0], sizeof (frombuf));
3902 cp = strchr(frombuf, '@');
3905 (void) strlcat(frombuf, fromname, sizeof (frombuf));
3908 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, lzc_flags);
3913 cp = strchr(argv[0], '@');
3916 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3921 * If they specified the full path to the snapshot, chop off
3922 * everything except the short name of the snapshot, but special
3923 * case if they specify the origin.
3925 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
3926 char origin[ZFS_MAXNAMELEN];
3929 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
3930 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
3932 if (strcmp(origin, fromname) == 0) {
3934 flags.fromorigin = B_TRUE;
3937 if (cp != fromname && strcmp(argv[0], fromname)) {
3938 (void) fprintf(stderr,
3939 gettext("incremental source must be "
3940 "in same filesystem\n"));
3944 if (strchr(fromname, '@') || strchr(fromname, '/')) {
3945 (void) fprintf(stderr,
3946 gettext("invalid incremental source\n"));
3952 if (flags.replicate && fromname == NULL)
3953 flags.doall = B_TRUE;
3955 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
3956 extraverbose ? &dbgnv : NULL);
3958 if (extraverbose && dbgnv != NULL) {
3960 * dump_nvlist prints to stdout, but that's been
3961 * redirected to a file. Make it print to stderr
3964 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
3965 dump_nvlist(dbgnv, 0);
3974 * Restore a backup stream from stdin.
3977 zfs_do_receive(int argc, char **argv)
3980 recvflags_t flags = { 0 };
3981 boolean_t abort_resumable = B_FALSE;
3984 nvpair_t *nvp = NULL;
3986 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3990 while ((c = getopt(argc, argv, ":o:denuvFsA")) != -1) {
3993 if (parseprop(props, optarg) != 0)
3997 flags.isprefix = B_TRUE;
4000 flags.isprefix = B_TRUE;
4001 flags.istail = B_TRUE;
4004 flags.dryrun = B_TRUE;
4007 flags.nomount = B_TRUE;
4010 flags.verbose = B_TRUE;
4013 flags.resumable = B_TRUE;
4016 flags.force = B_TRUE;
4019 abort_resumable = B_TRUE;
4022 (void) fprintf(stderr, gettext("missing argument for "
4023 "'%c' option\n"), optopt);
4027 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4036 /* check number of arguments */
4038 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4042 (void) fprintf(stderr, gettext("too many arguments\n"));
4046 while ((nvp = nvlist_next_nvpair(props, nvp))) {
4047 if (strcmp(nvpair_name(nvp), "origin") != 0) {
4048 (void) fprintf(stderr, gettext("invalid option"));
4053 if (abort_resumable) {
4054 if (flags.isprefix || flags.istail || flags.dryrun ||
4055 flags.resumable || flags.nomount) {
4056 (void) fprintf(stderr, gettext("invalid option"));
4060 char namebuf[ZFS_MAXNAMELEN];
4061 (void) snprintf(namebuf, sizeof (namebuf),
4062 "%s/%%recv", argv[0]);
4064 if (zfs_dataset_exists(g_zfs, namebuf,
4065 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4066 zfs_handle_t *zhp = zfs_open(g_zfs,
4067 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4070 err = zfs_destroy(zhp, B_FALSE);
4072 zfs_handle_t *zhp = zfs_open(g_zfs,
4073 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4076 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4077 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4078 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4079 (void) fprintf(stderr,
4080 gettext("'%s' does not have any "
4081 "resumable receive state to abort\n"),
4085 err = zfs_destroy(zhp, B_FALSE);
4091 if (isatty(STDIN_FILENO)) {
4092 (void) fprintf(stderr,
4093 gettext("Error: Backup stream can not be read "
4094 "from a terminal.\n"
4095 "You must redirect standard input.\n"));
4098 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4104 * allow/unallow stuff
4106 /* copied from zfs/sys/dsl_deleg.h */
4107 #define ZFS_DELEG_PERM_CREATE "create"
4108 #define ZFS_DELEG_PERM_DESTROY "destroy"
4109 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4110 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4111 #define ZFS_DELEG_PERM_CLONE "clone"
4112 #define ZFS_DELEG_PERM_PROMOTE "promote"
4113 #define ZFS_DELEG_PERM_RENAME "rename"
4114 #define ZFS_DELEG_PERM_MOUNT "mount"
4115 #define ZFS_DELEG_PERM_SHARE "share"
4116 #define ZFS_DELEG_PERM_SEND "send"
4117 #define ZFS_DELEG_PERM_RECEIVE "receive"
4118 #define ZFS_DELEG_PERM_ALLOW "allow"
4119 #define ZFS_DELEG_PERM_USERPROP "userprop"
4120 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4121 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4122 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4123 #define ZFS_DELEG_PERM_USERUSED "userused"
4124 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4125 #define ZFS_DELEG_PERM_HOLD "hold"
4126 #define ZFS_DELEG_PERM_RELEASE "release"
4127 #define ZFS_DELEG_PERM_DIFF "diff"
4128 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4130 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4132 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4133 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4134 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4135 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4136 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4137 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4138 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4139 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4140 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4141 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4142 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4143 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4144 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4145 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4146 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4147 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4148 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4150 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4151 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4152 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4153 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4154 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4155 { NULL, ZFS_DELEG_NOTE_NONE }
4158 /* permission structure */
4159 typedef struct deleg_perm {
4160 zfs_deleg_who_type_t dp_who_type;
4161 const char *dp_name;
4163 boolean_t dp_descend;
4167 typedef struct deleg_perm_node {
4168 deleg_perm_t dpn_perm;
4170 uu_avl_node_t dpn_avl_node;
4171 } deleg_perm_node_t;
4173 typedef struct fs_perm fs_perm_t;
4175 /* permissions set */
4176 typedef struct who_perm {
4177 zfs_deleg_who_type_t who_type;
4178 const char *who_name; /* id */
4179 char who_ug_name[256]; /* user/group name */
4180 fs_perm_t *who_fsperm; /* uplink */
4182 uu_avl_t *who_deleg_perm_avl; /* permissions */
4186 typedef struct who_perm_node {
4187 who_perm_t who_perm;
4188 uu_avl_node_t who_avl_node;
4191 typedef struct fs_perm_set fs_perm_set_t;
4192 /* fs permissions */
4194 const char *fsp_name;
4196 uu_avl_t *fsp_sc_avl; /* sets,create */
4197 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4199 fs_perm_set_t *fsp_set; /* uplink */
4203 typedef struct fs_perm_node {
4204 fs_perm_t fspn_fsperm;
4207 uu_list_node_t fspn_list_node;
4210 /* top level structure */
4211 struct fs_perm_set {
4212 uu_list_pool_t *fsps_list_pool;
4213 uu_list_t *fsps_list; /* list of fs_perms */
4215 uu_avl_pool_t *fsps_named_set_avl_pool;
4216 uu_avl_pool_t *fsps_who_perm_avl_pool;
4217 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4220 static inline const char *
4221 deleg_perm_type(zfs_deleg_note_t note)
4227 case ZFS_DELEG_NOTE_GROUPQUOTA:
4228 case ZFS_DELEG_NOTE_GROUPUSED:
4229 case ZFS_DELEG_NOTE_USERPROP:
4230 case ZFS_DELEG_NOTE_USERQUOTA:
4231 case ZFS_DELEG_NOTE_USERUSED:
4233 return (gettext("other"));
4235 return (gettext("subcommand"));
4240 who_type2weight(zfs_deleg_who_type_t who_type)
4244 case ZFS_DELEG_NAMED_SET_SETS:
4245 case ZFS_DELEG_NAMED_SET:
4248 case ZFS_DELEG_CREATE_SETS:
4249 case ZFS_DELEG_CREATE:
4252 case ZFS_DELEG_USER_SETS:
4253 case ZFS_DELEG_USER:
4256 case ZFS_DELEG_GROUP_SETS:
4257 case ZFS_DELEG_GROUP:
4260 case ZFS_DELEG_EVERYONE_SETS:
4261 case ZFS_DELEG_EVERYONE:
4273 who_perm_compare(const void *larg, const void *rarg, void *unused)
4275 const who_perm_node_t *l = larg;
4276 const who_perm_node_t *r = rarg;
4277 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4278 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4279 int lweight = who_type2weight(ltype);
4280 int rweight = who_type2weight(rtype);
4281 int res = lweight - rweight;
4283 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4284 ZFS_MAX_DELEG_NAME-1);
4296 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4298 const deleg_perm_node_t *l = larg;
4299 const deleg_perm_node_t *r = rarg;
4300 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4301 ZFS_MAX_DELEG_NAME-1);
4313 fs_perm_set_init(fs_perm_set_t *fspset)
4315 bzero(fspset, sizeof (fs_perm_set_t));
4317 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4318 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4319 NULL, UU_DEFAULT)) == NULL)
4321 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4322 UU_DEFAULT)) == NULL)
4325 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4326 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4327 who_perm_node_t, who_avl_node), who_perm_compare,
4328 UU_DEFAULT)) == NULL)
4331 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4332 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4333 who_perm_node_t, who_avl_node), who_perm_compare,
4334 UU_DEFAULT)) == NULL)
4337 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4338 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4339 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4344 static inline void fs_perm_fini(fs_perm_t *);
4345 static inline void who_perm_fini(who_perm_t *);
4348 fs_perm_set_fini(fs_perm_set_t *fspset)
4350 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4352 while (node != NULL) {
4353 fs_perm_node_t *next_node =
4354 uu_list_next(fspset->fsps_list, node);
4355 fs_perm_t *fsperm = &node->fspn_fsperm;
4356 fs_perm_fini(fsperm);
4357 uu_list_remove(fspset->fsps_list, node);
4362 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4363 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4364 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4368 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4371 deleg_perm->dp_who_type = type;
4372 deleg_perm->dp_name = name;
4376 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4377 zfs_deleg_who_type_t type, const char *name)
4379 uu_avl_pool_t *pool;
4380 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4382 bzero(who_perm, sizeof (who_perm_t));
4384 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4385 UU_DEFAULT)) == NULL)
4388 who_perm->who_type = type;
4389 who_perm->who_name = name;
4390 who_perm->who_fsperm = fsperm;
4394 who_perm_fini(who_perm_t *who_perm)
4396 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4398 while (node != NULL) {
4399 deleg_perm_node_t *next_node =
4400 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4402 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4407 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4411 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4413 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4414 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4416 bzero(fsperm, sizeof (fs_perm_t));
4418 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4422 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4426 fsperm->fsp_set = fspset;
4427 fsperm->fsp_name = fsname;
4431 fs_perm_fini(fs_perm_t *fsperm)
4433 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4434 while (node != NULL) {
4435 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4437 who_perm_t *who_perm = &node->who_perm;
4438 who_perm_fini(who_perm);
4439 uu_avl_remove(fsperm->fsp_sc_avl, node);
4444 node = uu_avl_first(fsperm->fsp_uge_avl);
4445 while (node != NULL) {
4446 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4448 who_perm_t *who_perm = &node->who_perm;
4449 who_perm_fini(who_perm);
4450 uu_avl_remove(fsperm->fsp_uge_avl, node);
4455 uu_avl_destroy(fsperm->fsp_sc_avl);
4456 uu_avl_destroy(fsperm->fsp_uge_avl);
4460 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4461 zfs_deleg_who_type_t who_type, const char *name, char locality)
4463 uu_avl_index_t idx = 0;
4465 deleg_perm_node_t *found_node = NULL;
4466 deleg_perm_t *deleg_perm = &node->dpn_perm;
4468 deleg_perm_init(deleg_perm, who_type, name);
4470 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4472 uu_avl_insert(avl, node, idx);
4475 deleg_perm = &node->dpn_perm;
4480 case ZFS_DELEG_LOCAL:
4481 deleg_perm->dp_local = B_TRUE;
4483 case ZFS_DELEG_DESCENDENT:
4484 deleg_perm->dp_descend = B_TRUE;
4489 assert(B_FALSE); /* invalid locality */
4494 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4496 nvpair_t *nvp = NULL;
4497 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4498 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4499 zfs_deleg_who_type_t who_type = who_perm->who_type;
4501 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4502 const char *name = nvpair_name(nvp);
4503 data_type_t type = nvpair_type(nvp);
4504 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4505 deleg_perm_node_t *node =
4506 safe_malloc(sizeof (deleg_perm_node_t));
4508 assert(type == DATA_TYPE_BOOLEAN);
4510 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4511 set_deleg_perm_node(avl, node, who_type, name, locality);
4518 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4520 nvpair_t *nvp = NULL;
4521 fs_perm_set_t *fspset = fsperm->fsp_set;
4523 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4524 nvlist_t *nvl2 = NULL;
4525 const char *name = nvpair_name(nvp);
4526 uu_avl_t *avl = NULL;
4527 uu_avl_pool_t *avl_pool = NULL;
4528 zfs_deleg_who_type_t perm_type = name[0];
4529 char perm_locality = name[1];
4530 const char *perm_name = name + 3;
4531 boolean_t is_set = B_TRUE;
4532 who_perm_t *who_perm = NULL;
4534 assert('$' == name[2]);
4536 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4539 switch (perm_type) {
4540 case ZFS_DELEG_CREATE:
4541 case ZFS_DELEG_CREATE_SETS:
4542 case ZFS_DELEG_NAMED_SET:
4543 case ZFS_DELEG_NAMED_SET_SETS:
4544 avl_pool = fspset->fsps_named_set_avl_pool;
4545 avl = fsperm->fsp_sc_avl;
4547 case ZFS_DELEG_USER:
4548 case ZFS_DELEG_USER_SETS:
4549 case ZFS_DELEG_GROUP:
4550 case ZFS_DELEG_GROUP_SETS:
4551 case ZFS_DELEG_EVERYONE:
4552 case ZFS_DELEG_EVERYONE_SETS:
4553 avl_pool = fspset->fsps_who_perm_avl_pool;
4554 avl = fsperm->fsp_uge_avl;
4558 assert(!"unhandled zfs_deleg_who_type_t");
4562 who_perm_node_t *found_node = NULL;
4563 who_perm_node_t *node = safe_malloc(
4564 sizeof (who_perm_node_t));
4565 who_perm = &node->who_perm;
4566 uu_avl_index_t idx = 0;
4568 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4569 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4571 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4573 if (avl == fsperm->fsp_uge_avl) {
4575 struct passwd *p = NULL;
4576 struct group *g = NULL;
4577 const char *nice_name = NULL;
4579 switch (perm_type) {
4580 case ZFS_DELEG_USER_SETS:
4581 case ZFS_DELEG_USER:
4582 rid = atoi(perm_name);
4585 nice_name = p->pw_name;
4587 case ZFS_DELEG_GROUP_SETS:
4588 case ZFS_DELEG_GROUP:
4589 rid = atoi(perm_name);
4592 nice_name = g->gr_name;
4599 if (nice_name != NULL)
4601 node->who_perm.who_ug_name,
4605 uu_avl_insert(avl, node, idx);
4608 who_perm = &node->who_perm;
4612 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4619 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4621 nvpair_t *nvp = NULL;
4622 uu_avl_index_t idx = 0;
4624 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4625 nvlist_t *nvl2 = NULL;
4626 const char *fsname = nvpair_name(nvp);
4627 data_type_t type = nvpair_type(nvp);
4628 fs_perm_t *fsperm = NULL;
4629 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4633 fsperm = &node->fspn_fsperm;
4635 assert(DATA_TYPE_NVLIST == type);
4637 uu_list_node_init(node, &node->fspn_list_node,
4638 fspset->fsps_list_pool);
4640 idx = uu_list_numnodes(fspset->fsps_list);
4641 fs_perm_init(fsperm, fspset, fsname);
4643 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4646 (void) parse_fs_perm(fsperm, nvl2);
4648 uu_list_insert(fspset->fsps_list, node, idx);
4654 static inline const char *
4655 deleg_perm_comment(zfs_deleg_note_t note)
4657 const char *str = "";
4662 case ZFS_DELEG_NOTE_ALLOW:
4663 str = gettext("Must also have the permission that is being"
4664 "\n\t\t\t\tallowed");
4666 case ZFS_DELEG_NOTE_CLONE:
4667 str = gettext("Must also have the 'create' ability and 'mount'"
4668 "\n\t\t\t\tability in the origin file system");
4670 case ZFS_DELEG_NOTE_CREATE:
4671 str = gettext("Must also have the 'mount' ability");
4673 case ZFS_DELEG_NOTE_DESTROY:
4674 str = gettext("Must also have the 'mount' ability");
4676 case ZFS_DELEG_NOTE_DIFF:
4677 str = gettext("Allows lookup of paths within a dataset;"
4678 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4679 "\n\t\t\t\tin order to use zfs diff");
4681 case ZFS_DELEG_NOTE_HOLD:
4682 str = gettext("Allows adding a user hold to a snapshot");
4684 case ZFS_DELEG_NOTE_MOUNT:
4685 str = gettext("Allows mount/umount of ZFS datasets");
4687 case ZFS_DELEG_NOTE_PROMOTE:
4688 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4689 " 'promote' ability in the origin file system");
4691 case ZFS_DELEG_NOTE_RECEIVE:
4692 str = gettext("Must also have the 'mount' and 'create'"
4695 case ZFS_DELEG_NOTE_RELEASE:
4696 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4697 "might destroy the snapshot");
4699 case ZFS_DELEG_NOTE_RENAME:
4700 str = gettext("Must also have the 'mount' and 'create'"
4701 "\n\t\t\t\tability in the new parent");
4703 case ZFS_DELEG_NOTE_ROLLBACK:
4706 case ZFS_DELEG_NOTE_SEND:
4709 case ZFS_DELEG_NOTE_SHARE:
4710 str = gettext("Allows sharing file systems over NFS or SMB"
4711 "\n\t\t\t\tprotocols");
4713 case ZFS_DELEG_NOTE_SNAPSHOT:
4717 * case ZFS_DELEG_NOTE_VSCAN:
4718 * str = gettext("");
4722 case ZFS_DELEG_NOTE_GROUPQUOTA:
4723 str = gettext("Allows accessing any groupquota@... property");
4725 case ZFS_DELEG_NOTE_GROUPUSED:
4726 str = gettext("Allows reading any groupused@... property");
4728 case ZFS_DELEG_NOTE_USERPROP:
4729 str = gettext("Allows changing any user property");
4731 case ZFS_DELEG_NOTE_USERQUOTA:
4732 str = gettext("Allows accessing any userquota@... property");
4734 case ZFS_DELEG_NOTE_USERUSED:
4735 str = gettext("Allows reading any userused@... property");
4753 boolean_t recursive; /* unallow only */
4754 boolean_t prt_usage;
4756 boolean_t prt_perms;
4759 const char *dataset;
4763 prop_cmp(const void *a, const void *b)
4765 const char *str1 = *(const char **)a;
4766 const char *str2 = *(const char **)b;
4767 return (strcmp(str1, str2));
4771 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4773 const char *opt_desc[] = {
4774 "-h", gettext("show this help message and exit"),
4775 "-l", gettext("set permission locally"),
4776 "-d", gettext("set permission for descents"),
4777 "-u", gettext("set permission for user"),
4778 "-g", gettext("set permission for group"),
4779 "-e", gettext("set permission for everyone"),
4780 "-c", gettext("set create time permission"),
4781 "-s", gettext("define permission set"),
4783 "-r", gettext("remove permissions recursively"),
4785 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4786 size_t allow_size = unallow_size - 2;
4787 const char *props[ZFS_NUM_PROPS];
4790 FILE *fp = requested ? stdout : stderr;
4791 zprop_desc_t *pdtbl = zfs_prop_get_table();
4792 const char *fmt = gettext("%-16s %-14s\t%s\n");
4794 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4796 (void) fprintf(fp, gettext("Options:\n"));
4797 for (i = 0; i < (un ? unallow_size : allow_size); i++) {
4798 const char *opt = opt_desc[i++];
4799 const char *optdsc = opt_desc[i];
4800 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4803 (void) fprintf(fp, gettext("\nThe following permissions are "
4805 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4807 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4808 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4809 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4810 const char *perm_type = deleg_perm_type(perm_note);
4811 const char *perm_comment = deleg_perm_comment(perm_note);
4812 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4815 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4816 zprop_desc_t *pd = &pdtbl[i];
4817 if (pd->pd_visible != B_TRUE)
4820 if (pd->pd_attr == PROP_READONLY)
4823 props[count++] = pd->pd_name;
4825 props[count] = NULL;
4827 qsort(props, count, sizeof (char *), prop_cmp);
4829 for (i = 0; i < count; i++)
4830 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
4833 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
4835 exit(requested ? 0 : 2);
4838 static inline const char *
4839 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
4842 if (un && argc == expected_argc - 1)
4844 else if (argc == expected_argc)
4845 *permsp = argv[argc - 2];
4847 allow_usage(un, B_FALSE,
4848 gettext("wrong number of parameters\n"));
4850 return (argv[argc - 1]);
4854 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
4856 int uge_sum = opts->user + opts->group + opts->everyone;
4857 int csuge_sum = opts->create + opts->set + uge_sum;
4858 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
4859 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
4862 allow_usage(un, B_FALSE,
4863 gettext("-u, -g, and -e are mutually exclusive\n"));
4865 if (opts->prt_usage) {
4866 if (argc == 0 && all_sum == 0)
4867 allow_usage(un, B_TRUE, NULL);
4874 allow_usage(un, B_FALSE,
4875 gettext("invalid options combined with -s\n"));
4877 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4878 if (argv[0][0] != '@')
4879 allow_usage(un, B_FALSE,
4880 gettext("invalid set name: missing '@' prefix\n"));
4881 opts->who = argv[0];
4882 } else if (opts->create) {
4883 if (ldcsuge_sum > 1)
4884 allow_usage(un, B_FALSE,
4885 gettext("invalid options combined with -c\n"));
4886 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4887 } else if (opts->everyone) {
4889 allow_usage(un, B_FALSE,
4890 gettext("invalid options combined with -e\n"));
4891 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4892 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
4894 opts->everyone = B_TRUE;
4897 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
4898 } else if (argc == 1 && !un) {
4899 opts->prt_perms = B_TRUE;
4900 opts->dataset = argv[argc-1];
4902 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
4903 opts->who = argv[0];
4906 if (!opts->local && !opts->descend) {
4907 opts->local = B_TRUE;
4908 opts->descend = B_TRUE;
4913 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
4914 const char *who, char *perms, nvlist_t *top_nvl)
4917 char ld[2] = { '\0', '\0' };
4918 char who_buf[ZFS_MAXNAMELEN+32];
4919 char base_type = '\0';
4920 char set_type = '\0';
4921 nvlist_t *base_nvl = NULL;
4922 nvlist_t *set_nvl = NULL;
4925 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
4927 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
4931 case ZFS_DELEG_NAMED_SET_SETS:
4932 case ZFS_DELEG_NAMED_SET:
4933 set_type = ZFS_DELEG_NAMED_SET_SETS;
4934 base_type = ZFS_DELEG_NAMED_SET;
4935 ld[0] = ZFS_DELEG_NA;
4937 case ZFS_DELEG_CREATE_SETS:
4938 case ZFS_DELEG_CREATE:
4939 set_type = ZFS_DELEG_CREATE_SETS;
4940 base_type = ZFS_DELEG_CREATE;
4941 ld[0] = ZFS_DELEG_NA;
4943 case ZFS_DELEG_USER_SETS:
4944 case ZFS_DELEG_USER:
4945 set_type = ZFS_DELEG_USER_SETS;
4946 base_type = ZFS_DELEG_USER;
4948 ld[0] = ZFS_DELEG_LOCAL;
4950 ld[1] = ZFS_DELEG_DESCENDENT;
4952 case ZFS_DELEG_GROUP_SETS:
4953 case ZFS_DELEG_GROUP:
4954 set_type = ZFS_DELEG_GROUP_SETS;
4955 base_type = ZFS_DELEG_GROUP;
4957 ld[0] = ZFS_DELEG_LOCAL;
4959 ld[1] = ZFS_DELEG_DESCENDENT;
4961 case ZFS_DELEG_EVERYONE_SETS:
4962 case ZFS_DELEG_EVERYONE:
4963 set_type = ZFS_DELEG_EVERYONE_SETS;
4964 base_type = ZFS_DELEG_EVERYONE;
4966 ld[0] = ZFS_DELEG_LOCAL;
4968 ld[1] = ZFS_DELEG_DESCENDENT;
4972 assert(set_type != '\0' && base_type != '\0');
4975 if (perms != NULL) {
4977 char *end = curr + strlen(perms);
4979 while (curr < end) {
4980 char *delim = strchr(curr, ',');
4991 (void) nvlist_add_boolean(nvl, curr);
4997 for (i = 0; i < 2; i++) {
4998 char locality = ld[i];
5002 if (!nvlist_empty(base_nvl)) {
5004 (void) snprintf(who_buf,
5005 sizeof (who_buf), "%c%c$%s",
5006 base_type, locality, who);
5008 (void) snprintf(who_buf,
5009 sizeof (who_buf), "%c%c$",
5010 base_type, locality);
5012 (void) nvlist_add_nvlist(top_nvl, who_buf,
5017 if (!nvlist_empty(set_nvl)) {
5019 (void) snprintf(who_buf,
5020 sizeof (who_buf), "%c%c$%s",
5021 set_type, locality, who);
5023 (void) snprintf(who_buf,
5024 sizeof (who_buf), "%c%c$",
5025 set_type, locality);
5027 (void) nvlist_add_nvlist(top_nvl, who_buf,
5032 for (i = 0; i < 2; i++) {
5033 char locality = ld[i];
5038 (void) snprintf(who_buf, sizeof (who_buf),
5039 "%c%c$%s", base_type, locality, who);
5041 (void) snprintf(who_buf, sizeof (who_buf),
5042 "%c%c$", base_type, locality);
5043 (void) nvlist_add_boolean(top_nvl, who_buf);
5046 (void) snprintf(who_buf, sizeof (who_buf),
5047 "%c%c$%s", set_type, locality, who);
5049 (void) snprintf(who_buf, sizeof (who_buf),
5050 "%c%c$", set_type, locality);
5051 (void) nvlist_add_boolean(top_nvl, who_buf);
5057 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5059 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5063 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5064 opts->descend, opts->who, opts->perms, *nvlp);
5065 } else if (opts->create) {
5066 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5067 opts->descend, NULL, opts->perms, *nvlp);
5068 } else if (opts->everyone) {
5069 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5070 opts->descend, NULL, opts->perms, *nvlp);
5072 char *curr = opts->who;
5073 char *end = curr + strlen(curr);
5075 while (curr < end) {
5077 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5079 char *delim = strchr(curr, ',');
5082 struct passwd *p = NULL;
5083 struct group *g = NULL;
5091 rid = (uid_t)strtol(curr, &endch, 0);
5093 who_type = ZFS_DELEG_USER;
5102 (void) snprintf(errbuf, 256, gettext(
5103 "invalid user %s"), curr);
5104 allow_usage(un, B_TRUE, errbuf);
5106 } else if (opts->group) {
5107 who_type = ZFS_DELEG_GROUP;
5116 (void) snprintf(errbuf, 256, gettext(
5117 "invalid group %s"), curr);
5118 allow_usage(un, B_TRUE, errbuf);
5121 if (*endch != '\0') {
5128 if (*endch != '\0') {
5136 who_type = ZFS_DELEG_USER;
5138 } else if (g != NULL) {
5139 who_type = ZFS_DELEG_GROUP;
5142 (void) snprintf(errbuf, 256, gettext(
5143 "invalid user/group %s"), curr);
5144 allow_usage(un, B_TRUE, errbuf);
5148 (void) sprintf(id, "%u", rid);
5151 store_allow_perm(who_type, opts->local,
5152 opts->descend, who, opts->perms, *nvlp);
5161 print_set_creat_perms(uu_avl_t *who_avl)
5163 const char *sc_title[] = {
5164 gettext("Permission sets:\n"),
5165 gettext("Create time permissions:\n"),
5168 const char **title_ptr = sc_title;
5169 who_perm_node_t *who_node = NULL;
5170 int prev_weight = -1;
5172 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5173 who_node = uu_avl_next(who_avl, who_node)) {
5174 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5175 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5176 const char *who_name = who_node->who_perm.who_name;
5177 int weight = who_type2weight(who_type);
5178 boolean_t first = B_TRUE;
5179 deleg_perm_node_t *deleg_node;
5181 if (prev_weight != weight) {
5182 (void) printf(*title_ptr++);
5183 prev_weight = weight;
5186 if (who_name == NULL || strnlen(who_name, 1) == 0)
5187 (void) printf("\t");
5189 (void) printf("\t%s ", who_name);
5191 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5192 deleg_node = uu_avl_next(avl, deleg_node)) {
5195 deleg_node->dpn_perm.dp_name);
5198 (void) printf(",%s",
5199 deleg_node->dpn_perm.dp_name);
5202 (void) printf("\n");
5207 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5210 who_perm_node_t *who_node = NULL;
5211 boolean_t prt_title = B_TRUE;
5212 uu_avl_walk_t *walk;
5214 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5217 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5218 const char *who_name = who_node->who_perm.who_name;
5219 const char *nice_who_name = who_node->who_perm.who_ug_name;
5220 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5221 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5223 deleg_perm_node_t *deleg_node;
5224 boolean_t prt_who = B_TRUE;
5226 for (deleg_node = uu_avl_first(avl);
5228 deleg_node = uu_avl_next(avl, deleg_node)) {
5229 if (local != deleg_node->dpn_perm.dp_local ||
5230 descend != deleg_node->dpn_perm.dp_descend)
5234 const char *who = NULL;
5236 prt_title = B_FALSE;
5237 (void) printf(title);
5241 case ZFS_DELEG_USER_SETS:
5242 case ZFS_DELEG_USER:
5243 who = gettext("user");
5245 who_name = nice_who_name;
5247 case ZFS_DELEG_GROUP_SETS:
5248 case ZFS_DELEG_GROUP:
5249 who = gettext("group");
5251 who_name = nice_who_name;
5253 case ZFS_DELEG_EVERYONE_SETS:
5254 case ZFS_DELEG_EVERYONE:
5255 who = gettext("everyone");
5260 assert(who != NULL);
5264 if (who_name == NULL)
5265 (void) printf("\t%s", who);
5267 (void) printf("\t%s %s", who, who_name);
5270 (void) printf("%c%s", delim,
5271 deleg_node->dpn_perm.dp_name);
5276 (void) printf("\n");
5279 uu_avl_walk_end(walk);
5283 print_fs_perms(fs_perm_set_t *fspset)
5285 fs_perm_node_t *node = NULL;
5286 char buf[ZFS_MAXNAMELEN+32];
5287 const char *dsname = buf;
5289 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5290 node = uu_list_next(fspset->fsps_list, node)) {
5291 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5292 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5295 (void) snprintf(buf, ZFS_MAXNAMELEN+32,
5296 gettext("---- Permissions on %s "),
5297 node->fspn_fsperm.fsp_name);
5298 (void) printf(dsname);
5299 left = 70 - strlen(buf);
5302 (void) printf("\n");
5304 print_set_creat_perms(sc_avl);
5305 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5306 gettext("Local permissions:\n"));
5307 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5308 gettext("Descendent permissions:\n"));
5309 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5310 gettext("Local+Descendent permissions:\n"));
5314 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5316 struct deleg_perms {
5322 set_deleg_perms(zfs_handle_t *zhp, void *data)
5324 struct deleg_perms *perms = (struct deleg_perms *)data;
5325 zfs_type_t zfs_type = zfs_get_type(zhp);
5327 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5330 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5334 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5337 nvlist_t *perm_nvl = NULL;
5338 nvlist_t *update_perm_nvl = NULL;
5341 struct allow_opts opts = { 0 };
5343 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5346 while ((c = getopt(argc, argv, optstr)) != -1) {
5349 opts.local = B_TRUE;
5352 opts.descend = B_TRUE;
5358 opts.group = B_TRUE;
5361 opts.everyone = B_TRUE;
5367 opts.create = B_TRUE;
5370 opts.recursive = B_TRUE;
5373 (void) fprintf(stderr, gettext("missing argument for "
5374 "'%c' option\n"), optopt);
5378 opts.prt_usage = B_TRUE;
5381 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5390 /* check arguments */
5391 parse_allow_args(argc, argv, un, &opts);
5393 /* try to open the dataset */
5394 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5395 ZFS_TYPE_VOLUME)) == NULL) {
5396 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5401 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5404 fs_perm_set_init(&fs_perm_set);
5405 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5406 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5411 print_fs_perms(&fs_perm_set);
5413 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5414 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5417 if (un && opts.recursive) {
5418 struct deleg_perms data = { un, update_perm_nvl };
5419 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5428 nvlist_free(perm_nvl);
5429 nvlist_free(update_perm_nvl);
5431 fs_perm_set_fini(&fs_perm_set);
5439 zfs_do_allow(int argc, char **argv)
5441 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5445 zfs_do_unallow(int argc, char **argv)
5447 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5451 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5456 boolean_t recursive = B_FALSE;
5457 const char *opts = holding ? "rt" : "r";
5461 while ((c = getopt(argc, argv, opts)) != -1) {
5467 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5476 /* check number of arguments */
5484 if (holding && tag[0] == '.') {
5485 /* tags starting with '.' are reserved for libzfs */
5486 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5490 for (i = 0; i < argc; ++i) {
5492 char parent[ZFS_MAXNAMELEN];
5494 char *path = argv[i];
5496 delim = strchr(path, '@');
5497 if (delim == NULL) {
5498 (void) fprintf(stderr,
5499 gettext("'%s' is not a snapshot\n"), path);
5503 (void) strncpy(parent, path, delim - path);
5504 parent[delim - path] = '\0';
5506 zhp = zfs_open(g_zfs, parent,
5507 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5513 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5516 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5522 return (errors != 0);
5526 * zfs hold [-r] [-t] <tag> <snap> ...
5528 * -r Recursively hold
5530 * Apply a user-hold with the given tag to the list of snapshots.
5533 zfs_do_hold(int argc, char **argv)
5535 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5539 * zfs release [-r] <tag> <snap> ...
5541 * -r Recursively release
5543 * Release a user-hold with the given tag from the list of snapshots.
5546 zfs_do_release(int argc, char **argv)
5548 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5551 typedef struct holds_cbdata {
5552 boolean_t cb_recursive;
5553 const char *cb_snapname;
5555 size_t cb_max_namelen;
5556 size_t cb_max_taglen;
5559 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5560 #define DATETIME_BUF_LEN (32)
5565 print_holds(boolean_t scripted, size_t nwidth, size_t tagwidth, nvlist_t *nvl)
5568 nvpair_t *nvp = NULL;
5569 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5573 for (i = 0; i < 3; i++) {
5574 col = gettext(hdr_cols[i]);
5576 (void) printf("%-*s ", i ? tagwidth : nwidth,
5579 (void) printf("%s\n", col);
5583 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5584 char *zname = nvpair_name(nvp);
5586 nvpair_t *nvp2 = NULL;
5587 (void) nvpair_value_nvlist(nvp, &nvl2);
5588 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5589 char tsbuf[DATETIME_BUF_LEN];
5590 char *tagname = nvpair_name(nvp2);
5594 char sep = scripted ? '\t' : ' ';
5595 size_t sepnum = scripted ? 1 : 2;
5597 (void) nvpair_value_uint64(nvp2, &val);
5599 (void) localtime_r(&time, &t);
5600 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5601 gettext(STRFTIME_FMT_STR), &t);
5603 (void) printf("%-*s%*c%-*s%*c%s\n", nwidth, zname,
5604 sepnum, sep, tagwidth, tagname, sepnum, sep, tsbuf);
5610 * Generic callback function to list a dataset or snapshot.
5613 holds_callback(zfs_handle_t *zhp, void *data)
5615 holds_cbdata_t *cbp = data;
5616 nvlist_t *top_nvl = *cbp->cb_nvlp;
5617 nvlist_t *nvl = NULL;
5618 nvpair_t *nvp = NULL;
5619 const char *zname = zfs_get_name(zhp);
5620 size_t znamelen = strnlen(zname, ZFS_MAXNAMELEN);
5622 if (cbp->cb_recursive) {
5623 const char *snapname;
5624 char *delim = strchr(zname, '@');
5628 snapname = delim + 1;
5629 if (strcmp(cbp->cb_snapname, snapname))
5633 if (zfs_get_holds(zhp, &nvl) != 0)
5636 if (znamelen > cbp->cb_max_namelen)
5637 cbp->cb_max_namelen = znamelen;
5639 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5640 const char *tag = nvpair_name(nvp);
5641 size_t taglen = strnlen(tag, MAXNAMELEN);
5642 if (taglen > cbp->cb_max_taglen)
5643 cbp->cb_max_taglen = taglen;
5646 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5650 * zfs holds [-r] <snap> ...
5652 * -r Recursively hold
5655 zfs_do_holds(int argc, char **argv)
5660 boolean_t scripted = B_FALSE;
5661 boolean_t recursive = B_FALSE;
5662 const char *opts = "rH";
5665 int types = ZFS_TYPE_SNAPSHOT;
5666 holds_cbdata_t cb = { 0 };
5673 while ((c = getopt(argc, argv, opts)) != -1) {
5682 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5689 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5690 flags |= ZFS_ITER_RECURSE;
5696 /* check number of arguments */
5700 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5703 for (i = 0; i < argc; ++i) {
5704 char *snapshot = argv[i];
5706 const char *snapname;
5708 delim = strchr(snapshot, '@');
5709 if (delim == NULL) {
5710 (void) fprintf(stderr,
5711 gettext("'%s' is not a snapshot\n"), snapshot);
5715 snapname = delim + 1;
5717 snapshot[delim - snapshot] = '\0';
5719 cb.cb_recursive = recursive;
5720 cb.cb_snapname = snapname;
5724 * 1. collect holds data, set format options
5726 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5727 holds_callback, &cb);
5733 * 2. print holds data
5735 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5737 if (nvlist_empty(nvl))
5738 (void) printf(gettext("no datasets available\n"));
5742 return (0 != errors);
5745 #define CHECK_SPINNER 30
5746 #define SPINNER_TIME 3 /* seconds */
5747 #define MOUNT_TIME 5 /* seconds */
5750 get_one_dataset(zfs_handle_t *zhp, void *data)
5752 static char *spin[] = { "-", "\\", "|", "/" };
5753 static int spinval = 0;
5754 static int spincheck = 0;
5755 static time_t last_spin_time = (time_t)0;
5756 get_all_cb_t *cbp = data;
5757 zfs_type_t type = zfs_get_type(zhp);
5759 if (cbp->cb_verbose) {
5760 if (--spincheck < 0) {
5761 time_t now = time(NULL);
5762 if (last_spin_time + SPINNER_TIME < now) {
5763 update_progress(spin[spinval++ % 4]);
5764 last_spin_time = now;
5766 spincheck = CHECK_SPINNER;
5771 * Interate over any nested datasets.
5773 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5779 * Skip any datasets whose type does not match.
5781 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5785 libzfs_add_handle(cbp, zhp);
5786 assert(cbp->cb_used <= cbp->cb_alloc);
5792 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5794 get_all_cb_t cb = { 0 };
5795 cb.cb_verbose = verbose;
5796 cb.cb_getone = get_one_dataset;
5799 set_progress_header(gettext("Reading ZFS config"));
5800 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5802 *dslist = cb.cb_handles;
5803 *count = cb.cb_used;
5806 finish_progress(gettext("done."));
5810 * Generic callback for sharing or mounting filesystems. Because the code is so
5811 * similar, we have a common function with an extra parameter to determine which
5812 * mode we are using.
5814 #define OP_SHARE 0x1
5815 #define OP_MOUNT 0x2
5818 * Share or mount a dataset.
5821 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
5822 boolean_t explicit, const char *options)
5824 char mountpoint[ZFS_MAXPROPLEN];
5825 char shareopts[ZFS_MAXPROPLEN];
5826 char smbshareopts[ZFS_MAXPROPLEN];
5827 const char *cmdname = op == OP_SHARE ? "share" : "mount";
5829 uint64_t zoned, canmount;
5830 boolean_t shared_nfs, shared_smb;
5832 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
5835 * Check to make sure we can mount/share this dataset. If we
5836 * are in the global zone and the filesystem is exported to a
5837 * local zone, or if we are in a local zone and the
5838 * filesystem is not exported, then it is an error.
5840 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
5842 if (zoned && getzoneid() == GLOBAL_ZONEID) {
5846 (void) fprintf(stderr, gettext("cannot %s '%s': "
5847 "dataset is exported to a local zone\n"), cmdname,
5851 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
5855 (void) fprintf(stderr, gettext("cannot %s '%s': "
5856 "permission denied\n"), cmdname,
5862 * Ignore any filesystems which don't apply to us. This
5863 * includes those with a legacy mountpoint, or those with
5864 * legacy share options.
5866 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
5867 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
5868 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
5869 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
5870 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
5871 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
5873 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
5874 strcmp(smbshareopts, "off") == 0) {
5878 (void) fprintf(stderr, gettext("cannot share '%s': "
5879 "legacy share\n"), zfs_get_name(zhp));
5880 (void) fprintf(stderr, gettext("to "
5881 "share this filesystem set "
5882 "sharenfs property on\n"));
5887 * We cannot share or mount legacy filesystems. If the
5888 * shareopts is non-legacy but the mountpoint is legacy, we
5889 * treat it as a legacy share.
5891 if (strcmp(mountpoint, "legacy") == 0) {
5895 (void) fprintf(stderr, gettext("cannot %s '%s': "
5896 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
5897 (void) fprintf(stderr, gettext("use %s(8) to "
5898 "%s this filesystem\n"), cmdname, cmdname);
5902 if (strcmp(mountpoint, "none") == 0) {
5906 (void) fprintf(stderr, gettext("cannot %s '%s': no "
5907 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
5912 * canmount explicit outcome
5913 * on no pass through
5914 * on yes pass through
5916 * off yes display error, return 1
5917 * noauto no return 0
5918 * noauto yes pass through
5920 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
5921 if (canmount == ZFS_CANMOUNT_OFF) {
5925 (void) fprintf(stderr, gettext("cannot %s '%s': "
5926 "'canmount' property is set to 'off'\n"), cmdname,
5929 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
5934 * If this filesystem is inconsistent and has a receive resume
5935 * token, we can not mount it.
5937 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
5938 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
5939 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
5943 (void) fprintf(stderr, gettext("cannot %s '%s': "
5944 "Contains partially-completed state from "
5945 "\"zfs receive -r\", which can be resumed with "
5946 "\"zfs send -t\"\n"),
5947 cmdname, zfs_get_name(zhp));
5952 * At this point, we have verified that the mountpoint and/or
5953 * shareopts are appropriate for auto management. If the
5954 * filesystem is already mounted or shared, return (failing
5955 * for explicit requests); otherwise mount or share the
5961 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
5962 shared_smb = zfs_is_shared_smb(zhp, NULL);
5964 if ((shared_nfs && shared_smb) ||
5965 (shared_nfs && strcmp(shareopts, "on") == 0 &&
5966 strcmp(smbshareopts, "off") == 0) ||
5967 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
5968 strcmp(shareopts, "off") == 0)) {
5972 (void) fprintf(stderr, gettext("cannot share "
5973 "'%s': filesystem already shared\n"),
5978 if (!zfs_is_mounted(zhp, NULL) &&
5979 zfs_mount(zhp, NULL, 0) != 0)
5982 if (protocol == NULL) {
5983 if (zfs_shareall(zhp) != 0)
5985 } else if (strcmp(protocol, "nfs") == 0) {
5986 if (zfs_share_nfs(zhp))
5988 } else if (strcmp(protocol, "smb") == 0) {
5989 if (zfs_share_smb(zhp))
5992 (void) fprintf(stderr, gettext("cannot share "
5993 "'%s': invalid share type '%s' "
5995 zfs_get_name(zhp), protocol);
6002 if (options == NULL)
6003 mnt.mnt_mntopts = "";
6005 mnt.mnt_mntopts = (char *)options;
6007 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6008 zfs_is_mounted(zhp, NULL)) {
6012 (void) fprintf(stderr, gettext("cannot mount "
6013 "'%s': filesystem already mounted\n"),
6018 if (zfs_mount(zhp, options, flags) != 0)
6027 * Reports progress in the form "(current/total)". Not thread-safe.
6030 report_mount_progress(int current, int total)
6032 static time_t last_progress_time = 0;
6033 time_t now = time(NULL);
6036 /* report 1..n instead of 0..n-1 */
6039 /* display header if we're here for the first time */
6041 set_progress_header(gettext("Mounting ZFS filesystems"));
6042 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6043 /* too soon to report again */
6047 last_progress_time = now;
6049 (void) sprintf(info, "(%d/%d)", current, total);
6051 if (current == total)
6052 finish_progress(info);
6054 update_progress(info);
6058 append_options(char *mntopts, char *newopts)
6060 int len = strlen(mntopts);
6062 /* original length plus new string to append plus 1 for the comma */
6063 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6064 (void) fprintf(stderr, gettext("the opts argument for "
6065 "'%c' option is too long (more than %d chars)\n"),
6066 "-o", MNT_LINE_MAX);
6071 mntopts[len++] = ',';
6073 (void) strcpy(&mntopts[len], newopts);
6077 share_mount(int op, int argc, char **argv)
6080 boolean_t verbose = B_FALSE;
6082 char *options = NULL;
6086 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":avo:O" : "a"))
6096 if (*optarg == '\0') {
6097 (void) fprintf(stderr, gettext("empty mount "
6098 "options (-o) specified\n"));
6102 if (options == NULL)
6103 options = safe_malloc(MNT_LINE_MAX + 1);
6105 /* option validation is done later */
6106 append_options(options, optarg);
6110 warnx("no overlay mounts support on FreeBSD, ignoring");
6113 (void) fprintf(stderr, gettext("missing argument for "
6114 "'%c' option\n"), optopt);
6118 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6127 /* check number of arguments */
6129 zfs_handle_t **dslist = NULL;
6130 size_t i, count = 0;
6131 char *protocol = NULL;
6133 if (op == OP_SHARE && argc > 0) {
6134 if (strcmp(argv[0], "nfs") != 0 &&
6135 strcmp(argv[0], "smb") != 0) {
6136 (void) fprintf(stderr, gettext("share type "
6137 "must be 'nfs' or 'smb'\n"));
6146 (void) fprintf(stderr, gettext("too many arguments\n"));
6150 start_progress_timer();
6151 get_all_datasets(&dslist, &count, verbose);
6156 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6158 for (i = 0; i < count; i++) {
6160 report_mount_progress(i, count);
6162 if (share_mount_one(dslist[i], op, flags, protocol,
6163 B_FALSE, options) != 0)
6165 zfs_close(dslist[i]);
6169 } else if (argc == 0) {
6170 struct mnttab entry;
6172 if ((op == OP_SHARE) || (options != NULL)) {
6173 (void) fprintf(stderr, gettext("missing filesystem "
6174 "argument (specify -a for all)\n"));
6179 * When mount is given no arguments, go through /etc/mnttab and
6180 * display any active ZFS mounts. We hide any snapshots, since
6181 * they are controlled automatically.
6183 rewind(mnttab_file);
6184 while (getmntent(mnttab_file, &entry) == 0) {
6185 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6186 strchr(entry.mnt_special, '@') != NULL)
6189 (void) printf("%-30s %s\n", entry.mnt_special,
6197 (void) fprintf(stderr,
6198 gettext("too many arguments\n"));
6202 if ((zhp = zfs_open(g_zfs, argv[0],
6203 ZFS_TYPE_FILESYSTEM)) == NULL) {
6206 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6216 * zfs mount -a [nfs]
6217 * zfs mount filesystem
6219 * Mount all filesystems, or mount the given filesystem.
6222 zfs_do_mount(int argc, char **argv)
6224 return (share_mount(OP_MOUNT, argc, argv));
6228 * zfs share -a [nfs | smb]
6229 * zfs share filesystem
6231 * Share all filesystems, or share the given filesystem.
6234 zfs_do_share(int argc, char **argv)
6236 return (share_mount(OP_SHARE, argc, argv));
6239 typedef struct unshare_unmount_node {
6240 zfs_handle_t *un_zhp;
6242 uu_avl_node_t un_avlnode;
6243 } unshare_unmount_node_t;
6247 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6249 const unshare_unmount_node_t *l = larg;
6250 const unshare_unmount_node_t *r = rarg;
6252 return (strcmp(l->un_mountp, r->un_mountp));
6256 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6257 * absolute path, find the entry /etc/mnttab, verify that its a ZFS filesystem,
6258 * and unmount it appropriately.
6261 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6265 struct stat64 statbuf;
6266 struct extmnttab entry;
6267 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6271 * Search for the path in /etc/mnttab. Rather than looking for the
6272 * specific path, which can be fooled by non-standard paths (i.e. ".."
6273 * or "//"), we stat() the path and search for the corresponding
6274 * (major,minor) device pair.
6276 if (stat64(path, &statbuf) != 0) {
6277 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6278 cmdname, path, strerror(errno));
6281 path_inode = statbuf.st_ino;
6284 * Search for the given (major,minor) pair in the mount table.
6287 rewind(mnttab_file);
6288 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6289 if (entry.mnt_major == major(statbuf.st_dev) &&
6290 entry.mnt_minor == minor(statbuf.st_dev))
6297 if (statfs(path, &sfs) != 0) {
6298 (void) fprintf(stderr, "%s: %s\n", path,
6302 statfs2mnttab(&sfs, &entry);
6306 if (op == OP_SHARE) {
6307 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6308 "currently mounted\n"), cmdname, path);
6311 (void) fprintf(stderr, gettext("warning: %s not in mnttab\n"),
6313 if ((ret = umount2(path, flags)) != 0)
6314 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6319 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6320 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6321 "filesystem\n"), cmdname, path);
6325 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6326 ZFS_TYPE_FILESYSTEM)) == NULL)
6330 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6331 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6332 cmdname, path, strerror(errno));
6334 } else if (statbuf.st_ino != path_inode) {
6335 (void) fprintf(stderr, gettext("cannot "
6336 "%s '%s': not a mountpoint\n"), cmdname, path);
6340 if (op == OP_SHARE) {
6341 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6342 char smbshare_prop[ZFS_MAXPROPLEN];
6344 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6345 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6346 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6347 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6349 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6350 strcmp(smbshare_prop, "off") == 0) {
6351 (void) fprintf(stderr, gettext("cannot unshare "
6352 "'%s': legacy share\n"), path);
6354 (void) fprintf(stderr, gettext("use "
6355 "unshare(1M) to unshare this filesystem\n"));
6357 } else if (!zfs_is_shared(zhp)) {
6358 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6359 "not currently shared\n"), path);
6361 ret = zfs_unshareall_bypath(zhp, path);
6364 char mtpt_prop[ZFS_MAXPROPLEN];
6366 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6367 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6370 ret = zfs_unmount(zhp, NULL, flags);
6371 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6372 (void) fprintf(stderr, gettext("cannot unmount "
6373 "'%s': legacy mountpoint\n"),
6375 (void) fprintf(stderr, gettext("use umount(8) "
6376 "to unmount this filesystem\n"));
6378 ret = zfs_unmountall(zhp, flags);
6389 * Generic callback for unsharing or unmounting a filesystem.
6392 unshare_unmount(int op, int argc, char **argv)
6399 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6400 char sharesmb[ZFS_MAXPROPLEN];
6403 while ((c = getopt(argc, argv, op == OP_SHARE ? "a" : "af")) != -1) {
6412 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6423 * We could make use of zfs_for_each() to walk all datasets in
6424 * the system, but this would be very inefficient, especially
6425 * since we would have to linearly search /etc/mnttab for each
6426 * one. Instead, do one pass through /etc/mnttab looking for
6427 * zfs entries and call zfs_unmount() for each one.
6429 * Things get a little tricky if the administrator has created
6430 * mountpoints beneath other ZFS filesystems. In this case, we
6431 * have to unmount the deepest filesystems first. To accomplish
6432 * this, we place all the mountpoints in an AVL tree sorted by
6433 * the special type (dataset name), and walk the result in
6434 * reverse to make sure to get any snapshots first.
6436 struct mnttab entry;
6437 uu_avl_pool_t *pool;
6438 uu_avl_t *tree = NULL;
6439 unshare_unmount_node_t *node;
6441 uu_avl_walk_t *walk;
6444 (void) fprintf(stderr, gettext("too many arguments\n"));
6448 if (((pool = uu_avl_pool_create("unmount_pool",
6449 sizeof (unshare_unmount_node_t),
6450 offsetof(unshare_unmount_node_t, un_avlnode),
6451 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6452 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6455 rewind(mnttab_file);
6456 while (getmntent(mnttab_file, &entry) == 0) {
6458 /* ignore non-ZFS entries */
6459 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6462 /* ignore snapshots */
6463 if (strchr(entry.mnt_special, '@') != NULL)
6466 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6467 ZFS_TYPE_FILESYSTEM)) == NULL) {
6474 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6476 sizeof (nfs_mnt_prop),
6477 NULL, NULL, 0, B_FALSE) == 0);
6478 if (strcmp(nfs_mnt_prop, "off") != 0)
6480 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6482 sizeof (nfs_mnt_prop),
6483 NULL, NULL, 0, B_FALSE) == 0);
6484 if (strcmp(nfs_mnt_prop, "off") == 0)
6488 /* Ignore legacy mounts */
6489 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6491 sizeof (nfs_mnt_prop),
6492 NULL, NULL, 0, B_FALSE) == 0);
6493 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6495 /* Ignore canmount=noauto mounts */
6496 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6497 ZFS_CANMOUNT_NOAUTO)
6503 node = safe_malloc(sizeof (unshare_unmount_node_t));
6505 node->un_mountp = safe_strdup(entry.mnt_mountp);
6507 uu_avl_node_init(node, &node->un_avlnode, pool);
6509 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6510 uu_avl_insert(tree, node, idx);
6512 zfs_close(node->un_zhp);
6513 free(node->un_mountp);
6519 * Walk the AVL tree in reverse, unmounting each filesystem and
6520 * removing it from the AVL tree in the process.
6522 if ((walk = uu_avl_walk_start(tree,
6523 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6526 while ((node = uu_avl_walk_next(walk)) != NULL) {
6527 uu_avl_remove(tree, node);
6531 if (zfs_unshareall_bypath(node->un_zhp,
6532 node->un_mountp) != 0)
6537 if (zfs_unmount(node->un_zhp,
6538 node->un_mountp, flags) != 0)
6543 zfs_close(node->un_zhp);
6544 free(node->un_mountp);
6548 uu_avl_walk_end(walk);
6549 uu_avl_destroy(tree);
6550 uu_avl_pool_destroy(pool);
6555 (void) fprintf(stderr,
6556 gettext("missing filesystem argument\n"));
6558 (void) fprintf(stderr,
6559 gettext("too many arguments\n"));
6564 * We have an argument, but it may be a full path or a ZFS
6565 * filesystem. Pass full paths off to unmount_path() (shared by
6566 * manual_unmount), otherwise open the filesystem and pass to
6569 if (argv[0][0] == '/')
6570 return (unshare_unmount_path(op, argv[0],
6573 if ((zhp = zfs_open(g_zfs, argv[0],
6574 ZFS_TYPE_FILESYSTEM)) == NULL)
6577 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6578 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6579 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6580 NULL, 0, B_FALSE) == 0);
6584 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6586 sizeof (nfs_mnt_prop),
6587 NULL, NULL, 0, B_FALSE) == 0);
6588 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6589 sharesmb, sizeof (sharesmb), NULL, NULL,
6592 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6593 strcmp(sharesmb, "off") == 0) {
6594 (void) fprintf(stderr, gettext("cannot "
6595 "unshare '%s': legacy share\n"),
6598 (void) fprintf(stderr, gettext("use "
6599 "unshare(1M) to unshare this "
6603 } else if (!zfs_is_shared(zhp)) {
6604 (void) fprintf(stderr, gettext("cannot "
6605 "unshare '%s': not currently "
6606 "shared\n"), zfs_get_name(zhp));
6608 } else if (zfs_unshareall(zhp) != 0) {
6614 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6615 (void) fprintf(stderr, gettext("cannot "
6616 "unmount '%s': legacy "
6617 "mountpoint\n"), zfs_get_name(zhp));
6618 (void) fprintf(stderr, gettext("use "
6619 "umount(8) to unmount this "
6622 } else if (!zfs_is_mounted(zhp, NULL)) {
6623 (void) fprintf(stderr, gettext("cannot "
6624 "unmount '%s': not currently "
6628 } else if (zfs_unmountall(zhp, flags) != 0) {
6642 * zfs unmount filesystem
6644 * Unmount all filesystems, or a specific ZFS filesystem.
6647 zfs_do_unmount(int argc, char **argv)
6649 return (unshare_unmount(OP_MOUNT, argc, argv));
6654 * zfs unshare filesystem
6656 * Unshare all filesystems, or a specific ZFS filesystem.
6659 zfs_do_unshare(int argc, char **argv)
6661 return (unshare_unmount(OP_SHARE, argc, argv));
6665 * Attach/detach the given dataset to/from the given jail
6669 do_jail(int argc, char **argv, int attach)
6674 /* check number of arguments */
6676 (void) fprintf(stderr, gettext("missing argument(s)\n"));
6680 (void) fprintf(stderr, gettext("too many arguments\n"));
6684 jailid = jail_getid(argv[1]);
6686 (void) fprintf(stderr, gettext("invalid jail id or name\n"));
6690 zhp = zfs_open(g_zfs, argv[2], ZFS_TYPE_FILESYSTEM);
6694 ret = (zfs_jail(zhp, jailid, attach) != 0);
6701 * zfs jail jailid filesystem
6703 * Attach the given dataset to the given jail
6707 zfs_do_jail(int argc, char **argv)
6710 return (do_jail(argc, argv, 1));
6714 * zfs unjail jailid filesystem
6716 * Detach the given dataset from the given jail
6720 zfs_do_unjail(int argc, char **argv)
6723 return (do_jail(argc, argv, 0));
6727 * Called when invoked as /etc/fs/zfs/mount. Do the mount if the mountpoint is
6728 * 'legacy'. Otherwise, complain that use should be using 'zfs mount'.
6731 manual_mount(int argc, char **argv)
6734 char mountpoint[ZFS_MAXPROPLEN];
6735 char mntopts[MNT_LINE_MAX] = { '\0' };
6739 char *dataset, *path;
6742 while ((c = getopt(argc, argv, ":mo:O")) != -1) {
6745 (void) strlcpy(mntopts, optarg, sizeof (mntopts));
6748 flags |= MS_OVERLAY;
6751 flags |= MS_NOMNTTAB;
6754 (void) fprintf(stderr, gettext("missing argument for "
6755 "'%c' option\n"), optopt);
6759 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6761 (void) fprintf(stderr, gettext("usage: mount [-o opts] "
6770 /* check that we only have two arguments */
6773 (void) fprintf(stderr, gettext("missing dataset "
6776 (void) fprintf(stderr,
6777 gettext("missing mountpoint argument\n"));
6779 (void) fprintf(stderr, gettext("too many arguments\n"));
6780 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
6787 /* try to open the dataset */
6788 if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL)
6791 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6792 sizeof (mountpoint), NULL, NULL, 0, B_FALSE);
6794 /* check for legacy mountpoint and complain appropriately */
6796 if (strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
6797 if (zmount(dataset, path, flags, MNTTYPE_ZFS,
6798 NULL, 0, mntopts, sizeof (mntopts)) != 0) {
6799 (void) fprintf(stderr, gettext("mount failed: %s\n"),
6804 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
6805 "mounted using 'mount -t zfs'\n"), dataset);
6806 (void) fprintf(stderr, gettext("Use 'zfs set mountpoint=%s' "
6807 "instead.\n"), path);
6808 (void) fprintf(stderr, gettext("If you must use 'mount -t zfs' "
6809 "or /etc/fstab, use 'zfs set mountpoint=legacy'.\n"));
6810 (void) fprintf(stderr, gettext("See zfs(8) for more "
6819 * Called when invoked as /etc/fs/zfs/umount. Unlike a manual mount, we allow
6820 * unmounts of non-legacy filesystems, as this is the dominant administrative
6824 manual_unmount(int argc, char **argv)
6830 while ((c = getopt(argc, argv, "f")) != -1) {
6836 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6838 (void) fprintf(stderr, gettext("usage: unmount [-f] "
6847 /* check arguments */
6850 (void) fprintf(stderr, gettext("missing path "
6853 (void) fprintf(stderr, gettext("too many arguments\n"));
6854 (void) fprintf(stderr, gettext("usage: unmount [-f] <path>\n"));
6858 return (unshare_unmount_path(OP_MOUNT, argv[0], flags, B_TRUE));
6862 find_command_idx(char *command, int *idx)
6866 for (i = 0; i < NCOMMAND; i++) {
6867 if (command_table[i].name == NULL)
6870 if (strcmp(command, command_table[i].name) == 0) {
6879 zfs_do_diff(int argc, char **argv)
6883 char *tosnap = NULL;
6884 char *fromsnap = NULL;
6889 while ((c = getopt(argc, argv, "FHt")) != -1) {
6892 flags |= ZFS_DIFF_CLASSIFY;
6895 flags |= ZFS_DIFF_PARSEABLE;
6898 flags |= ZFS_DIFF_TIMESTAMP;
6901 (void) fprintf(stderr,
6902 gettext("invalid option '%c'\n"), optopt);
6911 (void) fprintf(stderr,
6912 gettext("must provide at least one snapshot name\n"));
6917 (void) fprintf(stderr, gettext("too many arguments\n"));
6922 tosnap = (argc == 2) ? argv[1] : NULL;
6925 if (*fromsnap != '@')
6926 copy = strdup(fromsnap);
6928 copy = strdup(tosnap);
6932 if ((atp = strchr(copy, '@')) != NULL)
6935 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL)
6941 * Ignore SIGPIPE so that the library can give us
6942 * information on any failure
6944 (void) sigignore(SIGPIPE);
6946 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6954 * zfs bookmark <fs@snap> <fs#bmark>
6956 * Creates a bookmark with the given name from the given snapshot.
6959 zfs_do_bookmark(int argc, char **argv)
6961 char snapname[ZFS_MAXNAMELEN];
6968 while ((c = getopt(argc, argv, "")) != -1) {
6971 (void) fprintf(stderr,
6972 gettext("invalid option '%c'\n"), optopt);
6980 /* check number of arguments */
6982 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
6986 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
6990 if (strchr(argv[1], '#') == NULL) {
6991 (void) fprintf(stderr,
6992 gettext("invalid bookmark name '%s' -- "
6993 "must contain a '#'\n"), argv[1]);
6997 if (argv[0][0] == '@') {
6999 * Snapshot name begins with @.
7000 * Default to same fs as bookmark.
7002 (void) strncpy(snapname, argv[1], sizeof (snapname));
7003 *strchr(snapname, '#') = '\0';
7004 (void) strlcat(snapname, argv[0], sizeof (snapname));
7006 (void) strncpy(snapname, argv[0], sizeof (snapname));
7008 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
7014 nvl = fnvlist_alloc();
7015 fnvlist_add_string(nvl, argv[1], snapname);
7016 ret = lzc_bookmark(nvl, NULL);
7020 const char *err_msg;
7023 (void) snprintf(errbuf, sizeof (errbuf),
7024 dgettext(TEXT_DOMAIN,
7025 "cannot create bookmark '%s'"), argv[1]);
7029 err_msg = "bookmark is in a different pool";
7032 err_msg = "bookmark exists";
7035 err_msg = "invalid argument";
7038 err_msg = "bookmark feature not enabled";
7041 err_msg = "out of space";
7044 err_msg = "unknown error";
7047 (void) fprintf(stderr, "%s: %s\n", errbuf,
7048 dgettext(TEXT_DOMAIN, err_msg));
7059 main(int argc, char **argv)
7066 (void) setlocale(LC_ALL, "");
7067 (void) textdomain(TEXT_DOMAIN);
7071 if ((g_zfs = libzfs_init()) == NULL) {
7072 (void) fprintf(stderr, gettext("internal error: failed to "
7073 "initialize ZFS library\n"));
7077 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7079 libzfs_print_on_error(g_zfs, B_TRUE);
7081 if ((mnttab_file = fopen(MNTTAB, "r")) == NULL) {
7082 (void) fprintf(stderr, gettext("internal error: unable to "
7083 "open %s\n"), MNTTAB);
7088 * This command also doubles as the /etc/fs mount and unmount program.
7089 * Determine if we should take this behavior based on argv[0].
7091 progname = basename(argv[0]);
7092 if (strcmp(progname, "mount") == 0) {
7093 ret = manual_mount(argc, argv);
7094 } else if (strcmp(progname, "umount") == 0) {
7095 ret = manual_unmount(argc, argv);
7098 * Make sure the user has specified some command.
7101 (void) fprintf(stderr, gettext("missing command\n"));
7108 * The 'umount' command is an alias for 'unmount'
7110 if (strcmp(cmdname, "umount") == 0)
7111 cmdname = "unmount";
7114 * The 'recv' command is an alias for 'receive'
7116 if (strcmp(cmdname, "recv") == 0)
7117 cmdname = "receive";
7120 * The 'snap' command is an alias for 'snapshot'
7122 if (strcmp(cmdname, "snap") == 0)
7123 cmdname = "snapshot";
7128 if (strcmp(cmdname, "-?") == 0)
7132 * Run the appropriate command.
7134 libzfs_mnttab_cache(g_zfs, B_TRUE);
7135 if (find_command_idx(cmdname, &i) == 0) {
7136 current_command = &command_table[i];
7137 ret = command_table[i].func(argc - 1, argv + 1);
7138 } else if (strchr(cmdname, '=') != NULL) {
7139 verify(find_command_idx("set", &i) == 0);
7140 current_command = &command_table[i];
7141 ret = command_table[i].func(argc, argv);
7143 (void) fprintf(stderr, gettext("unrecognized "
7144 "command '%s'\n"), cmdname);
7147 libzfs_mnttab_cache(g_zfs, B_FALSE);
7150 (void) fclose(mnttab_file);
7152 if (ret == 0 && log_history)
7153 (void) zpool_log_history(g_zfs, history_str);
7158 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7159 * for the purposes of running ::findleaks.
7161 if (getenv("ZFS_ABORT") != NULL) {
7162 (void) printf("dumping core by request\n");