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, 2017 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) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29 * Copyright 2016 Nexenta Systems, Inc.
39 #include <libnvpair.h>
51 #include <sys/debug.h>
53 #include <sys/mkdev.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/systeminfo.h>
60 #include <sys/types.h>
64 #include <libzfs_core.h>
66 #include <zfs_deleg.h>
70 #include <directory.h>
71 #endif /* HAVE_IDMAP */
75 #include "zfs_comutil.h"
76 #include "libzfs_impl.h"
78 libzfs_handle_t *g_zfs;
80 static FILE *mnttab_file;
81 static char history_str[HIS_MAX_RECORD_LEN];
82 static boolean_t log_history = B_TRUE;
84 static int zfs_do_clone(int argc, char **argv);
85 static int zfs_do_create(int argc, char **argv);
86 static int zfs_do_destroy(int argc, char **argv);
87 static int zfs_do_get(int argc, char **argv);
88 static int zfs_do_inherit(int argc, char **argv);
89 static int zfs_do_list(int argc, char **argv);
90 static int zfs_do_mount(int argc, char **argv);
91 static int zfs_do_rename(int argc, char **argv);
92 static int zfs_do_rollback(int argc, char **argv);
93 static int zfs_do_set(int argc, char **argv);
94 static int zfs_do_upgrade(int argc, char **argv);
95 static int zfs_do_snapshot(int argc, char **argv);
96 static int zfs_do_unmount(int argc, char **argv);
97 static int zfs_do_share(int argc, char **argv);
98 static int zfs_do_unshare(int argc, char **argv);
99 static int zfs_do_send(int argc, char **argv);
100 static int zfs_do_receive(int argc, char **argv);
101 static int zfs_do_promote(int argc, char **argv);
102 static int zfs_do_userspace(int argc, char **argv);
103 static int zfs_do_allow(int argc, char **argv);
104 static int zfs_do_unallow(int argc, char **argv);
105 static int zfs_do_hold(int argc, char **argv);
106 static int zfs_do_holds(int argc, char **argv);
107 static int zfs_do_release(int argc, char **argv);
108 static int zfs_do_diff(int argc, char **argv);
109 static int zfs_do_bookmark(int argc, char **argv);
110 static int zfs_do_channel_program(int argc, char **argv);
111 static int zfs_do_load_key(int argc, char **argv);
112 static int zfs_do_unload_key(int argc, char **argv);
113 static int zfs_do_change_key(int argc, char **argv);
116 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
121 _umem_debug_init(void)
123 return ("default,verbose"); /* $UMEM_DEBUG setting */
127 _umem_logging_init(void)
129 return ("fail,contents"); /* $UMEM_LOGGING setting */
161 HELP_CHANNEL_PROGRAM,
167 typedef struct zfs_command {
169 int (*func)(int argc, char **argv);
174 * Master command table. Each ZFS command has a name, associated function, and
175 * usage message. The usage messages need to be internationalized, so we have
176 * to have a function to return the usage message based on a command index.
178 * These commands are organized according to how they are displayed in the usage
179 * message. An empty command (one with a NULL name) indicates an empty line in
180 * the generic usage message.
182 static zfs_command_t command_table[] = {
183 { "create", zfs_do_create, HELP_CREATE },
184 { "destroy", zfs_do_destroy, HELP_DESTROY },
186 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
187 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
188 { "clone", zfs_do_clone, HELP_CLONE },
189 { "promote", zfs_do_promote, HELP_PROMOTE },
190 { "rename", zfs_do_rename, HELP_RENAME },
191 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
192 { "program", zfs_do_channel_program, HELP_CHANNEL_PROGRAM },
194 { "list", zfs_do_list, HELP_LIST },
196 { "set", zfs_do_set, HELP_SET },
197 { "get", zfs_do_get, HELP_GET },
198 { "inherit", zfs_do_inherit, HELP_INHERIT },
199 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
200 { "userspace", zfs_do_userspace, HELP_USERSPACE },
201 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
203 { "mount", zfs_do_mount, HELP_MOUNT },
204 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
205 { "share", zfs_do_share, HELP_SHARE },
206 { "unshare", zfs_do_unshare, HELP_UNSHARE },
208 { "send", zfs_do_send, HELP_SEND },
209 { "receive", zfs_do_receive, HELP_RECEIVE },
211 { "allow", zfs_do_allow, HELP_ALLOW },
213 { "unallow", zfs_do_unallow, HELP_UNALLOW },
215 { "hold", zfs_do_hold, HELP_HOLD },
216 { "holds", zfs_do_holds, HELP_HOLDS },
217 { "release", zfs_do_release, HELP_RELEASE },
218 { "diff", zfs_do_diff, HELP_DIFF },
219 { "load-key", zfs_do_load_key, HELP_LOAD_KEY },
220 { "unload-key", zfs_do_unload_key, HELP_UNLOAD_KEY },
221 { "change-key", zfs_do_change_key, HELP_CHANGE_KEY },
224 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
226 zfs_command_t *current_command;
229 get_usage(zfs_help_t idx)
233 return (gettext("\tclone [-p] [-o property=value] ... "
234 "<snapshot> <filesystem|volume>\n"));
236 return (gettext("\tcreate [-p] [-o property=value] ... "
238 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
239 "-V <size> <volume>\n"));
241 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
242 "\tdestroy [-dnpRrv] "
243 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
244 "\tdestroy <filesystem|volume>#<bookmark>\n"));
246 return (gettext("\tget [-rHp] [-d max] "
247 "[-o \"all\" | field[,...]]\n"
248 "\t [-t type[,...]] [-s source[,...]]\n"
249 "\t <\"all\" | property[,...]> "
250 "[filesystem|volume|snapshot|bookmark] ...\n"));
252 return (gettext("\tinherit [-rS] <property> "
253 "<filesystem|volume|snapshot> ...\n"));
255 return (gettext("\tupgrade [-v]\n"
256 "\tupgrade [-r] [-V version] <-a | 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 [-lvO] [-o opts] <-a | filesystem>\n"));
265 return (gettext("\tpromote <clone-filesystem>\n"));
267 return (gettext("\treceive [-vnsFu] "
268 "[-o <property>=<value>] ... [-x <property>] ...\n"
269 "\t <filesystem|volume|snapshot>\n"
270 "\treceive [-vnsFu] [-o <property>=<value>] ... "
271 "[-x <property>] ... \n"
272 "\t [-d | -e] <filesystem>\n"
273 "\treceive -A <filesystem|volume>\n"));
275 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
276 "<filesystem|volume|snapshot>\n"
277 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
278 "\trename -r <snapshot> <snapshot>\n"));
280 return (gettext("\trollback [-rRf] <snapshot>\n"));
282 return (gettext("\tsend [-DnPpRvLecr] [-[i|I] snapshot] "
284 "\tsend [-Lecr] [-i snapshot|bookmark] "
285 "<filesystem|volume|snapshot>\n"
286 "\tsend [-nvPe] -t <receive_resume_token>\n"));
288 return (gettext("\tset <property=value> ... "
289 "<filesystem|volume|snapshot> ...\n"));
291 return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
293 return (gettext("\tsnapshot [-r] [-o property=value] ... "
294 "<filesystem|volume>@<snap> ...\n"));
296 return (gettext("\tunmount [-f] "
297 "<-a | filesystem|mountpoint>\n"));
299 return (gettext("\tunshare "
300 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
302 return (gettext("\tallow <filesystem|volume>\n"
304 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
305 "\t <filesystem|volume>\n"
306 "\tallow [-ld] -e <perm|@setname>[,...] "
307 "<filesystem|volume>\n"
308 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
309 "\tallow -s @setname <perm|@setname>[,...] "
310 "<filesystem|volume>\n"));
312 return (gettext("\tunallow [-rldug] "
313 "<\"everyone\"|user|group>[,...]\n"
314 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
315 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
316 "<filesystem|volume>\n"
317 "\tunallow [-r] -c [<perm|@setname>[,...]] "
318 "<filesystem|volume>\n"
319 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
320 "<filesystem|volume>\n"));
322 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
324 "\t [-S field] ... [-t type[,...]] "
325 "<filesystem|snapshot>\n"));
326 case HELP_GROUPSPACE:
327 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
329 "\t [-S field] ... [-t type[,...]] "
330 "<filesystem|snapshot>\n"));
332 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
334 return (gettext("\tholds [-r] <snapshot> ...\n"));
336 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
338 return (gettext("\tdiff [-FHt] <snapshot> "
339 "[snapshot|filesystem]\n"));
341 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
342 case HELP_CHANNEL_PROGRAM:
343 return (gettext("\tprogram [-n] [-t <instruction limit>] "
344 "[-m <memory limit (b)>] <pool> <program file> "
347 return (gettext("\tload-key [-rn] [-L <keylocation>] "
348 "<-a | filesystem|volume>\n"));
349 case HELP_UNLOAD_KEY:
350 return (gettext("\tunload-key [-r] "
351 "<-a | filesystem|volume>\n"));
352 case HELP_CHANGE_KEY:
353 return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
354 "\t [-o keylocation=<value>] [-o pbkfd2iters=<value>]\n"
355 "\t <filesystem|volume>\n"
356 "\tchange-key -i [-l] <filesystem|volume>\n"));
366 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
371 * Utility function to guarantee malloc() success.
375 safe_malloc(size_t size)
379 if ((data = calloc(1, size)) == NULL)
386 safe_realloc(void *data, size_t size)
389 if ((newp = realloc(data, size)) == NULL) {
398 safe_strdup(char *str)
400 char *dupstr = strdup(str);
409 * Callback routine that will print out information for each of
413 usage_prop_cb(int prop, void *cb)
417 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
419 if (zfs_prop_readonly(prop))
420 (void) fprintf(fp, " NO ");
422 (void) fprintf(fp, "YES ");
424 if (zfs_prop_inheritable(prop))
425 (void) fprintf(fp, " YES ");
427 (void) fprintf(fp, " NO ");
429 if (zfs_prop_values(prop) == NULL)
430 (void) fprintf(fp, "-\n");
432 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
438 * Display usage message. If we're inside a command, display only the usage for
439 * that command. Otherwise, iterate over the entire command table and display
440 * a complete usage message.
443 usage(boolean_t requested)
446 boolean_t show_properties = B_FALSE;
447 FILE *fp = requested ? stdout : stderr;
449 if (current_command == NULL) {
451 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
453 gettext("where 'command' is one of the following:\n\n"));
455 for (i = 0; i < NCOMMAND; i++) {
456 if (command_table[i].name == NULL)
457 (void) fprintf(fp, "\n");
459 (void) fprintf(fp, "%s",
460 get_usage(command_table[i].usage));
463 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
464 "pool/[dataset/]*dataset[@name]\n"));
466 (void) fprintf(fp, gettext("usage:\n"));
467 (void) fprintf(fp, "%s", get_usage(current_command->usage));
470 if (current_command != NULL &&
471 (strcmp(current_command->name, "set") == 0 ||
472 strcmp(current_command->name, "get") == 0 ||
473 strcmp(current_command->name, "inherit") == 0 ||
474 strcmp(current_command->name, "list") == 0))
475 show_properties = B_TRUE;
477 if (show_properties) {
479 gettext("\nThe following properties are supported:\n"));
481 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
482 "PROPERTY", "EDIT", "INHERIT", "VALUES");
484 /* Iterate over all properties */
485 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
488 (void) fprintf(fp, "\t%-15s ", "userused@...");
489 (void) fprintf(fp, " NO NO <size>\n");
490 (void) fprintf(fp, "\t%-15s ", "groupused@...");
491 (void) fprintf(fp, " NO NO <size>\n");
492 (void) fprintf(fp, "\t%-15s ", "userquota@...");
493 (void) fprintf(fp, "YES NO <size> | none\n");
494 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
495 (void) fprintf(fp, "YES NO <size> | none\n");
496 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
497 (void) fprintf(fp, " NO NO <size>\n");
499 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
500 "with standard units such as K, M, G, etc.\n"));
501 (void) fprintf(fp, gettext("\nUser-defined properties can "
502 "be specified by using a name containing a colon (:).\n"));
503 (void) fprintf(fp, gettext("\nThe {user|group}{used|quota}@ "
504 "properties must be appended with\n"
505 "a user or group specifier of one of these forms:\n"
506 " POSIX name (eg: \"matt\")\n"
507 " POSIX id (eg: \"126829\")\n"
508 " SMB name@domain (eg: \"matt@sun\")\n"
509 " SMB SID (eg: \"S-1-234-567-89\")\n"));
512 gettext("\nFor the property list, run: %s\n"),
515 gettext("\nFor the delegated permission list, run: %s\n"),
516 "zfs allow|unallow");
520 * See comments at end of main().
522 if (getenv("ZFS_ABORT") != NULL) {
523 (void) printf("dumping core by request\n");
527 exit(requested ? 0 : 2);
531 * Take a property=value argument string and add it to the given nvlist.
532 * Modifies the argument inplace.
535 parseprop(nvlist_t *props, char *propname)
539 if ((propval = strchr(propname, '=')) == NULL) {
540 (void) fprintf(stderr, gettext("missing "
541 "'=' for property=value argument\n"));
546 if (nvlist_exists(props, propname)) {
547 (void) fprintf(stderr, gettext("property '%s' "
548 "specified multiple times\n"), propname);
551 if (nvlist_add_string(props, propname, propval) != 0)
557 * Take a property name argument and add it to the given nvlist.
558 * Modifies the argument inplace.
561 parsepropname(nvlist_t *props, char *propname)
563 if (strchr(propname, '=') != NULL) {
564 (void) fprintf(stderr, gettext("invalid character "
565 "'=' in property argument\n"));
568 if (nvlist_exists(props, propname)) {
569 (void) fprintf(stderr, gettext("property '%s' "
570 "specified multiple times\n"), propname);
573 if (nvlist_add_boolean(props, propname) != 0)
579 parse_depth(char *opt, int *flags)
584 depth = (int)strtol(opt, &tmp, 0);
586 (void) fprintf(stderr,
587 gettext("%s is not an integer\n"), optarg);
591 (void) fprintf(stderr,
592 gettext("Depth can not be negative.\n"));
595 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
599 #define PROGRESS_DELAY 2 /* seconds */
601 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";
602 static time_t pt_begin;
603 static char *pt_header = NULL;
604 static boolean_t pt_shown;
607 start_progress_timer(void)
609 pt_begin = time(NULL) + PROGRESS_DELAY;
614 set_progress_header(char *header)
616 assert(pt_header == NULL);
617 pt_header = safe_strdup(header);
619 (void) printf("%s: ", header);
620 (void) fflush(stdout);
625 update_progress(char *update)
627 if (!pt_shown && time(NULL) > pt_begin) {
628 int len = strlen(update);
630 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
632 (void) fflush(stdout);
634 } else if (pt_shown) {
635 int len = strlen(update);
637 (void) printf("%s%*.*s", update, len, len, pt_reverse);
638 (void) fflush(stdout);
643 finish_progress(char *done)
646 (void) printf("%s\n", done);
647 (void) fflush(stdout);
654 zfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type)
656 zfs_handle_t *zhp = NULL;
659 zhp = zfs_open(hdl, dataset, type);
664 * Volumes may neither be mounted or shared. Potentially in the
665 * future filesystems detected on these volumes could be mounted.
667 if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
673 * Mount and/or share the new filesystem as appropriate. We provide a
674 * verbose error message to let the user know that their filesystem was
675 * in fact created, even if we failed to mount or share it.
677 * If the user doesn't want the dataset automatically mounted, then
678 * skip the mount/share step
680 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type, B_FALSE) &&
681 zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON) {
682 if (geteuid() != 0) {
683 (void) fprintf(stderr, gettext("filesystem "
684 "successfully created, but it may only be "
685 "mounted by root\n"));
687 } else if (zfs_mount(zhp, NULL, 0) != 0) {
688 (void) fprintf(stderr, gettext("filesystem "
689 "successfully created, but not mounted\n"));
691 } else if (zfs_share(zhp) != 0) {
692 (void) fprintf(stderr, gettext("filesystem "
693 "successfully created, but not shared\n"));
704 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
706 * Given an existing dataset, create a writable copy whose initial contents
707 * are the same as the source. The newly created dataset maintains a
708 * dependency on the original; the original cannot be destroyed so long as
711 * The '-p' flag creates all the non-existing ancestors of the target first.
714 zfs_do_clone(int argc, char **argv)
716 zfs_handle_t *zhp = NULL;
717 boolean_t parents = B_FALSE;
722 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
726 while ((c = getopt(argc, argv, "o:p")) != -1) {
729 if (!parseprop(props, optarg)) {
738 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
747 /* check number of arguments */
749 (void) fprintf(stderr, gettext("missing source dataset "
754 (void) fprintf(stderr, gettext("missing target dataset "
759 (void) fprintf(stderr, gettext("too many arguments\n"));
763 /* open the source dataset */
764 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) {
769 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
772 * Now create the ancestors of the target dataset. If the
773 * target already exists and '-p' option was used we should not
776 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
782 if (zfs_create_ancestors(g_zfs, argv[1]) != 0) {
790 ret = zfs_clone(zhp, argv[1], props);
792 /* create the mountpoint if necessary */
795 (void) zpool_log_history(g_zfs, history_str);
796 log_history = B_FALSE;
799 ret = zfs_mount_and_share(g_zfs, argv[1], ZFS_TYPE_DATASET);
808 ASSERT3P(zhp, ==, NULL);
815 * zfs create [-p] [-o prop=value] ... fs
816 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
818 * Create a new dataset. This command can be used to create filesystems
819 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
820 * For volumes, the user must specify a size to be used.
822 * The '-s' flag applies only to volumes, and indicates that we should not try
823 * to set the reservation for this volume. By default we set a reservation
824 * equal to the size for any volume. For pools with SPA_VERSION >=
825 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
827 * The '-p' flag creates all the non-existing ancestors of the target first.
830 zfs_do_create(int argc, char **argv)
832 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
833 uint64_t volsize = 0;
835 boolean_t noreserve = B_FALSE;
836 boolean_t bflag = B_FALSE;
837 boolean_t parents = B_FALSE;
842 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
846 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
849 type = ZFS_TYPE_VOLUME;
850 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
851 (void) fprintf(stderr, gettext("bad volume "
852 "size '%s': %s\n"), optarg,
853 libzfs_error_description(g_zfs));
857 if (nvlist_add_uint64(props,
858 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
867 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
868 (void) fprintf(stderr, gettext("bad volume "
869 "block size '%s': %s\n"), optarg,
870 libzfs_error_description(g_zfs));
874 if (nvlist_add_uint64(props,
875 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
880 if (!parseprop(props, optarg))
887 (void) fprintf(stderr, gettext("missing size "
891 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
897 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
898 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
899 "used when creating a volume\n"));
906 /* check number of arguments */
908 (void) fprintf(stderr, gettext("missing %s argument\n"),
909 zfs_type_to_name(type));
913 (void) fprintf(stderr, gettext("too many arguments\n"));
917 if (type == ZFS_TYPE_VOLUME && !noreserve) {
918 zpool_handle_t *zpool_handle;
919 nvlist_t *real_props = NULL;
920 uint64_t spa_version;
922 zfs_prop_t resv_prop;
926 if ((p = strchr(argv[0], '/')) != NULL)
928 zpool_handle = zpool_open(g_zfs, argv[0]);
931 if (zpool_handle == NULL)
933 spa_version = zpool_get_prop_int(zpool_handle,
934 ZPOOL_PROP_VERSION, NULL);
935 if (spa_version >= SPA_VERSION_REFRESERVATION)
936 resv_prop = ZFS_PROP_REFRESERVATION;
938 resv_prop = ZFS_PROP_RESERVATION;
940 (void) snprintf(msg, sizeof (msg),
941 gettext("cannot create '%s'"), argv[0]);
942 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
943 props, 0, NULL, zpool_handle, B_TRUE, msg)) == NULL) {
944 zpool_close(zpool_handle);
947 zpool_close(zpool_handle);
949 volsize = zvol_volsize_to_reservation(volsize, real_props);
950 nvlist_free(real_props);
952 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
954 if (nvlist_add_uint64(props,
955 zfs_prop_to_name(resv_prop), volsize) != 0) {
962 if (parents && zfs_name_valid(argv[0], type)) {
964 * Now create the ancestors of target dataset. If the target
965 * already exists and '-p' option was used we should not
968 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
972 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
977 if (zfs_create(g_zfs, argv[0], type, props) != 0)
981 (void) zpool_log_history(g_zfs, history_str);
982 log_history = B_FALSE;
985 ret = zfs_mount_and_share(g_zfs, argv[0], ZFS_TYPE_DATASET);
996 * zfs destroy [-rRf] <fs, vol>
997 * zfs destroy [-rRd] <snap>
999 * -r Recursively destroy all children
1000 * -R Recursively destroy all dependents, including clones
1001 * -f Force unmounting of any dependents
1002 * -d If we can't destroy now, mark for deferred destruction
1004 * Destroys the given dataset. By default, it will unmount any filesystems,
1005 * and refuse to destroy a dataset that has any dependents. A dependent can
1006 * either be a child, or a clone of a child.
1008 typedef struct destroy_cbdata {
1011 boolean_t cb_recurse;
1013 boolean_t cb_doclones;
1014 zfs_handle_t *cb_target;
1015 boolean_t cb_defer_destroy;
1016 boolean_t cb_verbose;
1017 boolean_t cb_parsable;
1018 boolean_t cb_dryrun;
1020 nvlist_t *cb_batchedsnaps;
1022 /* first snap in contiguous run */
1024 /* previous snap in contiguous run */
1026 int64_t cb_snapused;
1032 * Check for any dependents based on the '-r' or '-R' flags.
1035 destroy_check_dependent(zfs_handle_t *zhp, void *data)
1037 destroy_cbdata_t *cbp = data;
1038 const char *tname = zfs_get_name(cbp->cb_target);
1039 const char *name = zfs_get_name(zhp);
1041 if (strncmp(tname, name, strlen(tname)) == 0 &&
1042 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
1044 * This is a direct descendant, not a clone somewhere else in
1047 if (cbp->cb_recurse)
1050 if (cbp->cb_first) {
1051 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1052 "%s has children\n"),
1053 zfs_get_name(cbp->cb_target),
1054 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1055 (void) fprintf(stderr, gettext("use '-r' to destroy "
1056 "the following datasets:\n"));
1057 cbp->cb_first = B_FALSE;
1058 cbp->cb_error = B_TRUE;
1061 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1064 * This is a clone. We only want to report this if the '-r'
1065 * wasn't specified, or the target is a snapshot.
1067 if (!cbp->cb_recurse &&
1068 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1071 if (cbp->cb_first) {
1072 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1073 "%s has dependent clones\n"),
1074 zfs_get_name(cbp->cb_target),
1075 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1076 (void) fprintf(stderr, gettext("use '-R' to destroy "
1077 "the following datasets:\n"));
1078 cbp->cb_first = B_FALSE;
1079 cbp->cb_error = B_TRUE;
1080 cbp->cb_dryrun = B_TRUE;
1083 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1092 destroy_callback(zfs_handle_t *zhp, void *data)
1094 destroy_cbdata_t *cb = data;
1095 const char *name = zfs_get_name(zhp);
1097 if (cb->cb_verbose) {
1098 if (cb->cb_parsable) {
1099 (void) printf("destroy\t%s\n", name);
1100 } else if (cb->cb_dryrun) {
1101 (void) printf(gettext("would destroy %s\n"),
1104 (void) printf(gettext("will destroy %s\n"),
1110 * Ignore pools (which we've already flagged as an error before getting
1113 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1114 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1118 if (cb->cb_dryrun) {
1124 * We batch up all contiguous snapshots (even of different
1125 * filesystems) and destroy them with one ioctl. We can't
1126 * simply do all snap deletions and then all fs deletions,
1127 * because we must delete a clone before its origin.
1129 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1130 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1132 int error = zfs_destroy_snaps_nvl(g_zfs,
1133 cb->cb_batchedsnaps, B_FALSE);
1134 fnvlist_free(cb->cb_batchedsnaps);
1135 cb->cb_batchedsnaps = fnvlist_alloc();
1138 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1139 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1150 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1152 destroy_cbdata_t *cb = arg;
1153 const char *name = zfs_get_name(zhp);
1156 if (nvlist_exists(cb->cb_nvl, name)) {
1157 if (cb->cb_firstsnap == NULL)
1158 cb->cb_firstsnap = strdup(name);
1159 if (cb->cb_prevsnap != NULL)
1160 free(cb->cb_prevsnap);
1161 /* this snap continues the current range */
1162 cb->cb_prevsnap = strdup(name);
1163 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1165 if (cb->cb_verbose) {
1166 if (cb->cb_parsable) {
1167 (void) printf("destroy\t%s\n", name);
1168 } else if (cb->cb_dryrun) {
1169 (void) printf(gettext("would destroy %s\n"),
1172 (void) printf(gettext("will destroy %s\n"),
1176 } else if (cb->cb_firstsnap != NULL) {
1177 /* end of this range */
1179 err = lzc_snaprange_space(cb->cb_firstsnap,
1180 cb->cb_prevsnap, &used);
1181 cb->cb_snapused += used;
1182 free(cb->cb_firstsnap);
1183 cb->cb_firstsnap = NULL;
1184 free(cb->cb_prevsnap);
1185 cb->cb_prevsnap = NULL;
1192 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1195 assert(cb->cb_firstsnap == NULL);
1196 assert(cb->cb_prevsnap == NULL);
1197 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1198 if (cb->cb_firstsnap != NULL) {
1201 err = lzc_snaprange_space(cb->cb_firstsnap,
1202 cb->cb_prevsnap, &used);
1204 cb->cb_snapused += used;
1205 free(cb->cb_firstsnap);
1206 cb->cb_firstsnap = NULL;
1207 free(cb->cb_prevsnap);
1208 cb->cb_prevsnap = NULL;
1214 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1216 destroy_cbdata_t *cb = arg;
1219 /* Check for clones. */
1220 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1221 cb->cb_target = zhp;
1222 cb->cb_first = B_TRUE;
1223 err = zfs_iter_dependents(zhp, B_TRUE,
1224 destroy_check_dependent, cb);
1228 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1236 gather_snapshots(zfs_handle_t *zhp, void *arg)
1238 destroy_cbdata_t *cb = arg;
1241 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1247 if (cb->cb_verbose) {
1248 err = destroy_print_snapshots(zhp, cb);
1254 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1262 destroy_clones(destroy_cbdata_t *cb)
1265 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1267 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1268 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1271 boolean_t defer = cb->cb_defer_destroy;
1275 * We can't defer destroy non-snapshots, so set it to
1276 * false while destroying the clones.
1278 cb->cb_defer_destroy = B_FALSE;
1279 err = zfs_iter_dependents(zhp, B_FALSE,
1280 destroy_callback, cb);
1281 cb->cb_defer_destroy = defer;
1291 zfs_do_destroy(int argc, char **argv)
1293 destroy_cbdata_t cb = { 0 };
1297 zfs_handle_t *zhp = NULL;
1299 zfs_type_t type = ZFS_TYPE_DATASET;
1302 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1305 cb.cb_verbose = B_TRUE;
1308 cb.cb_verbose = B_TRUE;
1309 cb.cb_parsable = B_TRUE;
1312 cb.cb_dryrun = B_TRUE;
1315 cb.cb_defer_destroy = B_TRUE;
1316 type = ZFS_TYPE_SNAPSHOT;
1319 cb.cb_force = B_TRUE;
1322 cb.cb_recurse = B_TRUE;
1325 cb.cb_recurse = B_TRUE;
1326 cb.cb_doclones = B_TRUE;
1330 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1339 /* check number of arguments */
1341 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1345 (void) fprintf(stderr, gettext("too many arguments\n"));
1349 at = strchr(argv[0], '@');
1350 pound = strchr(argv[0], '#');
1353 /* Build the list of snaps to destroy in cb_nvl. */
1354 cb.cb_nvl = fnvlist_alloc();
1357 zhp = zfs_open(g_zfs, argv[0],
1358 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1360 nvlist_free(cb.cb_nvl);
1364 cb.cb_snapspec = at + 1;
1365 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1371 if (nvlist_empty(cb.cb_nvl)) {
1372 (void) fprintf(stderr, gettext("could not find any "
1373 "snapshots to destroy; check snapshot names.\n"));
1378 if (cb.cb_verbose) {
1380 zfs_nicebytes(cb.cb_snapused, buf, sizeof (buf));
1381 if (cb.cb_parsable) {
1382 (void) printf("reclaim\t%llu\n",
1383 (u_longlong_t)cb.cb_snapused);
1384 } else if (cb.cb_dryrun) {
1385 (void) printf(gettext("would reclaim %s\n"),
1388 (void) printf(gettext("will reclaim %s\n"),
1393 if (!cb.cb_dryrun) {
1394 if (cb.cb_doclones) {
1395 cb.cb_batchedsnaps = fnvlist_alloc();
1396 err = destroy_clones(&cb);
1398 err = zfs_destroy_snaps_nvl(g_zfs,
1399 cb.cb_batchedsnaps, B_FALSE);
1407 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1408 cb.cb_defer_destroy);
1414 } else if (pound != NULL) {
1419 (void) fprintf(stderr,
1420 "dryrun is not supported with bookmark\n");
1424 if (cb.cb_defer_destroy) {
1425 (void) fprintf(stderr,
1426 "defer destroy is not supported with bookmark\n");
1430 if (cb.cb_recurse) {
1431 (void) fprintf(stderr,
1432 "recursive is not supported with bookmark\n");
1436 if (!zfs_bookmark_exists(argv[0])) {
1437 (void) fprintf(stderr, gettext("bookmark '%s' "
1438 "does not exist.\n"), argv[0]);
1442 nvl = fnvlist_alloc();
1443 fnvlist_add_boolean(nvl, argv[0]);
1445 err = lzc_destroy_bookmarks(nvl, NULL);
1447 (void) zfs_standard_error(g_zfs, err,
1448 "cannot destroy bookmark");
1455 /* Open the given dataset */
1456 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1462 * Perform an explicit check for pools before going any further.
1464 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1465 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1466 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1467 "operation does not apply to pools\n"),
1469 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1470 "%s' to destroy all datasets in the pool\n"),
1472 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1473 "to destroy the pool itself\n"), zfs_get_name(zhp));
1479 * Check for any dependents and/or clones.
1481 cb.cb_first = B_TRUE;
1482 if (!cb.cb_doclones &&
1483 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1494 cb.cb_batchedsnaps = fnvlist_alloc();
1495 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1502 * Do the real thing. The callback will close the
1503 * handle regardless of whether it succeeds or not.
1505 err = destroy_callback(zhp, &cb);
1508 err = zfs_destroy_snaps_nvl(g_zfs,
1509 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1516 fnvlist_free(cb.cb_batchedsnaps);
1517 fnvlist_free(cb.cb_nvl);
1524 is_recvd_column(zprop_get_cbdata_t *cbp)
1527 zfs_get_column_t col;
1529 for (i = 0; i < ZFS_GET_NCOLS &&
1530 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1531 if (col == GET_COL_RECVD)
1537 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1538 * < all | property[,property]... > < fs | snap | vol > ...
1540 * -r recurse over any child datasets
1541 * -H scripted mode. Headers are stripped, and fields are separated
1542 * by tabs instead of spaces.
1543 * -o Set of fields to display. One of "name,property,value,
1544 * received,source". Default is "name,property,value,source".
1545 * "all" is an alias for all five.
1546 * -s Set of sources to allow. One of
1547 * "local,default,inherited,received,temporary,none". Default is
1549 * -p Display values in parsable (literal) format.
1551 * Prints properties for the given datasets. The user can control which
1552 * columns to display as well as which property types to allow.
1556 * Invoked to display the properties for a single dataset.
1559 get_callback(zfs_handle_t *zhp, void *data)
1561 char buf[ZFS_MAXPROPLEN];
1562 char rbuf[ZFS_MAXPROPLEN];
1563 zprop_source_t sourcetype;
1564 char source[ZFS_MAX_DATASET_NAME_LEN];
1565 zprop_get_cbdata_t *cbp = data;
1566 nvlist_t *user_props = zfs_get_user_props(zhp);
1567 zprop_list_t *pl = cbp->cb_proplist;
1571 boolean_t received = is_recvd_column(cbp);
1573 for (; pl != NULL; pl = pl->pl_next) {
1574 char *recvdval = NULL;
1576 * Skip the special fake placeholder. This will also skip over
1577 * the name property when 'all' is specified.
1579 if (pl->pl_prop == ZFS_PROP_NAME &&
1580 pl == cbp->cb_proplist)
1583 if (pl->pl_prop != ZPROP_INVAL) {
1584 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1585 sizeof (buf), &sourcetype, source,
1587 cbp->cb_literal) != 0) {
1590 if (!zfs_prop_valid_for_type(pl->pl_prop,
1591 ZFS_TYPE_DATASET, B_FALSE)) {
1592 (void) fprintf(stderr,
1593 gettext("No such property '%s'\n"),
1594 zfs_prop_to_name(pl->pl_prop));
1597 sourcetype = ZPROP_SRC_NONE;
1598 (void) strlcpy(buf, "-", sizeof (buf));
1601 if (received && (zfs_prop_get_recvd(zhp,
1602 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1603 cbp->cb_literal) == 0))
1606 zprop_print_one_property(zfs_get_name(zhp), cbp,
1607 zfs_prop_to_name(pl->pl_prop),
1608 buf, sourcetype, source, recvdval);
1609 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1610 sourcetype = ZPROP_SRC_LOCAL;
1612 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1613 buf, sizeof (buf), cbp->cb_literal) != 0) {
1614 sourcetype = ZPROP_SRC_NONE;
1615 (void) strlcpy(buf, "-", sizeof (buf));
1618 zprop_print_one_property(zfs_get_name(zhp), cbp,
1619 pl->pl_user_prop, buf, sourcetype, source, NULL);
1620 } else if (zfs_prop_written(pl->pl_user_prop)) {
1621 sourcetype = ZPROP_SRC_LOCAL;
1623 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1624 buf, sizeof (buf), cbp->cb_literal) != 0) {
1625 sourcetype = ZPROP_SRC_NONE;
1626 (void) strlcpy(buf, "-", sizeof (buf));
1629 zprop_print_one_property(zfs_get_name(zhp), cbp,
1630 pl->pl_user_prop, buf, sourcetype, source, NULL);
1632 if (nvlist_lookup_nvlist(user_props,
1633 pl->pl_user_prop, &propval) != 0) {
1636 sourcetype = ZPROP_SRC_NONE;
1639 verify(nvlist_lookup_string(propval,
1640 ZPROP_VALUE, &strval) == 0);
1641 verify(nvlist_lookup_string(propval,
1642 ZPROP_SOURCE, &sourceval) == 0);
1644 if (strcmp(sourceval,
1645 zfs_get_name(zhp)) == 0) {
1646 sourcetype = ZPROP_SRC_LOCAL;
1647 } else if (strcmp(sourceval,
1648 ZPROP_SOURCE_VAL_RECVD) == 0) {
1649 sourcetype = ZPROP_SRC_RECEIVED;
1651 sourcetype = ZPROP_SRC_INHERITED;
1652 (void) strlcpy(source,
1653 sourceval, sizeof (source));
1657 if (received && (zfs_prop_get_recvd(zhp,
1658 pl->pl_user_prop, rbuf, sizeof (rbuf),
1659 cbp->cb_literal) == 0))
1662 zprop_print_one_property(zfs_get_name(zhp), cbp,
1663 pl->pl_user_prop, strval, sourcetype,
1672 zfs_do_get(int argc, char **argv)
1674 zprop_get_cbdata_t cb = { 0 };
1675 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1676 int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
1677 char *value, *fields;
1680 zprop_list_t fake_name = { 0 };
1683 * Set up default columns and sources.
1685 cb.cb_sources = ZPROP_SRC_ALL;
1686 cb.cb_columns[0] = GET_COL_NAME;
1687 cb.cb_columns[1] = GET_COL_PROPERTY;
1688 cb.cb_columns[2] = GET_COL_VALUE;
1689 cb.cb_columns[3] = GET_COL_SOURCE;
1690 cb.cb_type = ZFS_TYPE_DATASET;
1693 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1696 cb.cb_literal = B_TRUE;
1699 limit = parse_depth(optarg, &flags);
1702 flags |= ZFS_ITER_RECURSE;
1705 cb.cb_scripted = B_TRUE;
1708 (void) fprintf(stderr, gettext("missing argument for "
1709 "'%c' option\n"), optopt);
1714 * Process the set of columns to display. We zero out
1715 * the structure to give us a blank slate.
1717 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1719 while (*optarg != '\0') {
1720 static char *col_subopts[] =
1721 { "name", "property", "value", "received",
1722 "source", "all", NULL };
1724 if (i == ZFS_GET_NCOLS) {
1725 (void) fprintf(stderr, gettext("too "
1726 "many fields given to -o "
1731 switch (getsubopt(&optarg, col_subopts,
1734 cb.cb_columns[i++] = GET_COL_NAME;
1737 cb.cb_columns[i++] = GET_COL_PROPERTY;
1740 cb.cb_columns[i++] = GET_COL_VALUE;
1743 cb.cb_columns[i++] = GET_COL_RECVD;
1744 flags |= ZFS_ITER_RECVD_PROPS;
1747 cb.cb_columns[i++] = GET_COL_SOURCE;
1751 (void) fprintf(stderr,
1752 gettext("\"all\" conflicts "
1753 "with specific fields "
1754 "given to -o option\n"));
1757 cb.cb_columns[0] = GET_COL_NAME;
1758 cb.cb_columns[1] = GET_COL_PROPERTY;
1759 cb.cb_columns[2] = GET_COL_VALUE;
1760 cb.cb_columns[3] = GET_COL_RECVD;
1761 cb.cb_columns[4] = GET_COL_SOURCE;
1762 flags |= ZFS_ITER_RECVD_PROPS;
1766 (void) fprintf(stderr,
1767 gettext("invalid column name "
1776 while (*optarg != '\0') {
1777 static char *source_subopts[] = {
1778 "local", "default", "inherited",
1779 "received", "temporary", "none",
1782 switch (getsubopt(&optarg, source_subopts,
1785 cb.cb_sources |= ZPROP_SRC_LOCAL;
1788 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1791 cb.cb_sources |= ZPROP_SRC_INHERITED;
1794 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1797 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1800 cb.cb_sources |= ZPROP_SRC_NONE;
1803 (void) fprintf(stderr,
1804 gettext("invalid source "
1813 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1814 while (*optarg != '\0') {
1815 static char *type_subopts[] = { "filesystem",
1816 "volume", "snapshot", "bookmark",
1819 switch (getsubopt(&optarg, type_subopts,
1822 types |= ZFS_TYPE_FILESYSTEM;
1825 types |= ZFS_TYPE_VOLUME;
1828 types |= ZFS_TYPE_SNAPSHOT;
1831 types |= ZFS_TYPE_BOOKMARK;
1834 types = ZFS_TYPE_DATASET |
1839 (void) fprintf(stderr,
1840 gettext("invalid type '%s'\n"),
1848 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1858 (void) fprintf(stderr, gettext("missing property "
1865 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1873 * As part of zfs_expand_proplist(), we keep track of the maximum column
1874 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1875 * need to know the maximum name length. However, the user likely did
1876 * not specify 'name' as one of the properties to fetch, so we need to
1877 * make sure we always include at least this property for
1878 * print_get_headers() to work properly.
1880 if (cb.cb_proplist != NULL) {
1881 fake_name.pl_prop = ZFS_PROP_NAME;
1882 fake_name.pl_width = strlen(gettext("NAME"));
1883 fake_name.pl_next = cb.cb_proplist;
1884 cb.cb_proplist = &fake_name;
1887 cb.cb_first = B_TRUE;
1889 /* run for each object */
1890 ret = zfs_for_each(argc, argv, flags, types, NULL,
1891 &cb.cb_proplist, limit, get_callback, &cb);
1893 if (cb.cb_proplist == &fake_name)
1894 zprop_free_list(fake_name.pl_next);
1896 zprop_free_list(cb.cb_proplist);
1902 * inherit [-rS] <property> <fs|vol> ...
1904 * -r Recurse over all children
1905 * -S Revert to received value, if any
1907 * For each dataset specified on the command line, inherit the given property
1908 * from its parent. Inheriting a property at the pool level will cause it to
1909 * use the default value. The '-r' flag will recurse over all children, and is
1910 * useful for setting a property on a hierarchy-wide basis, regardless of any
1911 * local modifications for each dataset.
1914 typedef struct inherit_cbdata {
1915 const char *cb_propname;
1916 boolean_t cb_received;
1920 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1922 inherit_cbdata_t *cb = data;
1923 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1926 * If we're doing it recursively, then ignore properties that
1927 * are not valid for this type of dataset.
1929 if (prop != ZPROP_INVAL &&
1930 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp), B_FALSE))
1933 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1937 inherit_cb(zfs_handle_t *zhp, void *data)
1939 inherit_cbdata_t *cb = data;
1941 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1945 zfs_do_inherit(int argc, char **argv)
1949 inherit_cbdata_t cb = { 0 };
1953 boolean_t received = B_FALSE;
1956 while ((c = getopt(argc, argv, "rS")) != -1) {
1959 flags |= ZFS_ITER_RECURSE;
1966 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1975 /* check number of arguments */
1977 (void) fprintf(stderr, gettext("missing property argument\n"));
1981 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1989 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
1990 if (zfs_prop_readonly(prop)) {
1991 (void) fprintf(stderr, gettext(
1992 "%s property is read-only\n"),
1996 if (!zfs_prop_inheritable(prop) && !received) {
1997 (void) fprintf(stderr, gettext("'%s' property cannot "
1998 "be inherited\n"), propname);
1999 if (prop == ZFS_PROP_QUOTA ||
2000 prop == ZFS_PROP_RESERVATION ||
2001 prop == ZFS_PROP_REFQUOTA ||
2002 prop == ZFS_PROP_REFRESERVATION) {
2003 (void) fprintf(stderr, gettext("use 'zfs set "
2004 "%s=none' to clear\n"), propname);
2005 (void) fprintf(stderr, gettext("use 'zfs "
2006 "inherit -S %s' to revert to received "
2007 "value\n"), propname);
2011 if (received && (prop == ZFS_PROP_VOLSIZE ||
2012 prop == ZFS_PROP_VERSION)) {
2013 (void) fprintf(stderr, gettext("'%s' property cannot "
2014 "be reverted to a received value\n"), propname);
2017 } else if (!zfs_prop_user(propname)) {
2018 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
2023 cb.cb_propname = propname;
2024 cb.cb_received = received;
2026 if (flags & ZFS_ITER_RECURSE) {
2027 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2028 NULL, NULL, 0, inherit_recurse_cb, &cb);
2030 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2031 NULL, NULL, 0, inherit_cb, &cb);
2037 typedef struct upgrade_cbdata {
2038 uint64_t cb_numupgraded;
2039 uint64_t cb_numsamegraded;
2040 uint64_t cb_numfailed;
2041 uint64_t cb_version;
2043 boolean_t cb_foundone;
2044 char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
2048 same_pool(zfs_handle_t *zhp, const char *name)
2050 int len1 = strcspn(name, "/@");
2051 const char *zhname = zfs_get_name(zhp);
2052 int len2 = strcspn(zhname, "/@");
2056 return (strncmp(name, zhname, len1) == 0);
2060 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2062 upgrade_cbdata_t *cb = data;
2063 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2065 /* list if it's old/new */
2066 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2067 (cb->cb_newer && version > ZPL_VERSION)) {
2070 str = gettext("The following filesystems are "
2071 "formatted using a newer software version and\n"
2072 "cannot be accessed on the current system.\n\n");
2074 str = gettext("The following filesystems are "
2075 "out of date, and can be upgraded. After being\n"
2076 "upgraded, these filesystems (and any 'zfs send' "
2077 "streams generated from\n"
2078 "subsequent snapshots) will no longer be "
2079 "accessible by older software versions.\n\n");
2082 if (!cb->cb_foundone) {
2084 (void) printf(gettext("VER FILESYSTEM\n"));
2085 (void) printf(gettext("--- ------------\n"));
2086 cb->cb_foundone = B_TRUE;
2089 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2096 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2098 upgrade_cbdata_t *cb = data;
2099 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2100 int needed_spa_version;
2103 if (zfs_spa_version(zhp, &spa_version) < 0)
2106 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2108 if (needed_spa_version < 0)
2111 if (spa_version < needed_spa_version) {
2113 (void) printf(gettext("%s: can not be "
2114 "upgraded; the pool version needs to first "
2115 "be upgraded\nto version %d\n\n"),
2116 zfs_get_name(zhp), needed_spa_version);
2122 if (version < cb->cb_version) {
2124 (void) snprintf(verstr, sizeof (verstr),
2125 "%llu", (u_longlong_t)cb->cb_version);
2126 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2128 * If they did "zfs upgrade -a", then we could
2129 * be doing ioctls to different pools. We need
2130 * to log this history once to each pool, and bypass
2131 * the normal history logging that happens in main().
2133 (void) zpool_log_history(g_zfs, history_str);
2134 log_history = B_FALSE;
2136 if (zfs_prop_set(zhp, "version", verstr) == 0)
2137 cb->cb_numupgraded++;
2140 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2141 } else if (version > cb->cb_version) {
2142 /* can't downgrade */
2143 (void) printf(gettext("%s: can not be downgraded; "
2144 "it is already at version %u\n"),
2145 zfs_get_name(zhp), version);
2148 cb->cb_numsamegraded++;
2156 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2159 zfs_do_upgrade(int argc, char **argv)
2161 boolean_t all = B_FALSE;
2162 boolean_t showversions = B_FALSE;
2164 upgrade_cbdata_t cb = { 0 };
2166 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2169 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2172 flags |= ZFS_ITER_RECURSE;
2175 showversions = B_TRUE;
2178 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2179 optarg, &cb.cb_version) != 0) {
2180 (void) fprintf(stderr,
2181 gettext("invalid version %s\n"), optarg);
2190 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2199 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2201 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2202 cb.cb_version || argc))
2204 if ((all || argc) && (showversions))
2210 /* Show info on available versions. */
2211 (void) printf(gettext("The following filesystem versions are "
2213 (void) printf(gettext("VER DESCRIPTION\n"));
2214 (void) printf("--- -----------------------------------------"
2215 "---------------\n");
2216 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2217 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2218 (void) printf(gettext(" 3 Case insensitive and filesystem "
2219 "user identifier (FUID)\n"));
2220 (void) printf(gettext(" 4 userquota, groupquota "
2222 (void) printf(gettext(" 5 System attributes\n"));
2223 (void) printf(gettext("\nFor more information on a particular "
2224 "version, including supported releases,\n"));
2225 (void) printf("see the ZFS Administration Guide.\n\n");
2227 } else if (argc || all) {
2228 /* Upgrade filesystems */
2229 if (cb.cb_version == 0)
2230 cb.cb_version = ZPL_VERSION;
2231 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2232 NULL, NULL, 0, upgrade_set_callback, &cb);
2233 (void) printf(gettext("%llu filesystems upgraded\n"),
2234 (u_longlong_t)cb.cb_numupgraded);
2235 if (cb.cb_numsamegraded) {
2236 (void) printf(gettext("%llu filesystems already at "
2238 (u_longlong_t)cb.cb_numsamegraded);
2240 if (cb.cb_numfailed != 0)
2243 /* List old-version filesystems */
2245 (void) printf(gettext("This system is currently running "
2246 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2248 flags |= ZFS_ITER_RECURSE;
2249 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2250 NULL, NULL, 0, upgrade_list_callback, &cb);
2252 found = cb.cb_foundone;
2253 cb.cb_foundone = B_FALSE;
2254 cb.cb_newer = B_TRUE;
2256 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2257 NULL, NULL, 0, upgrade_list_callback, &cb);
2259 if (!cb.cb_foundone && !found) {
2260 (void) printf(gettext("All filesystems are "
2261 "formatted with the current version.\n"));
2269 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2270 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2271 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2272 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2274 * -H Scripted mode; elide headers and separate columns by tabs.
2275 * -i Translate SID to POSIX ID.
2276 * -n Print numeric ID instead of user/group name.
2277 * -o Control which fields to display.
2278 * -p Use exact (parsable) numeric output.
2279 * -s Specify sort columns, descending order.
2280 * -S Specify sort columns, ascending order.
2281 * -t Control which object types to display.
2283 * Displays space consumed by, and quotas on, each user in the specified
2284 * filesystem or snapshot.
2287 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2288 enum us_field_types {
2296 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA",
2297 "OBJUSED", "OBJQUOTA" };
2298 static char *us_field_names[] = { "type", "name", "used", "quota",
2299 "objused", "objquota" };
2300 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2302 #define USTYPE_PSX_GRP (1 << 0)
2303 #define USTYPE_PSX_USR (1 << 1)
2304 #define USTYPE_SMB_GRP (1 << 2)
2305 #define USTYPE_SMB_USR (1 << 3)
2306 #define USTYPE_ALL \
2307 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR)
2309 static int us_type_bits[] = {
2316 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2319 typedef struct us_node {
2321 uu_avl_node_t usn_avlnode;
2322 uu_list_node_t usn_listnode;
2325 typedef struct us_cbdata {
2327 uu_avl_pool_t *cb_avl_pool;
2329 boolean_t cb_numname;
2330 boolean_t cb_nicenum;
2331 boolean_t cb_sid2posix;
2332 zfs_userquota_prop_t cb_prop;
2333 zfs_sort_column_t *cb_sortcol;
2334 size_t cb_width[USFIELD_LAST];
2337 static boolean_t us_populated = B_FALSE;
2340 zfs_sort_column_t *si_sortcol;
2341 boolean_t si_numname;
2345 us_field_index(char *field)
2349 for (i = 0; i < USFIELD_LAST; i++) {
2350 if (strcmp(field, us_field_names[i]) == 0)
2358 us_compare(const void *larg, const void *rarg, void *unused)
2360 const us_node_t *l = larg;
2361 const us_node_t *r = rarg;
2362 us_sort_info_t *si = (us_sort_info_t *)unused;
2363 zfs_sort_column_t *sortcol = si->si_sortcol;
2364 boolean_t numname = si->si_numname;
2365 nvlist_t *lnvl = l->usn_nvl;
2366 nvlist_t *rnvl = r->usn_nvl;
2370 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2377 zfs_prop_t prop = sortcol->sc_prop;
2378 const char *propname = NULL;
2379 boolean_t reverse = sortcol->sc_reverse;
2384 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2385 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2387 rc = (rv32 < lv32) ? 1 : -1;
2393 (void) nvlist_lookup_uint64(lnvl, propname,
2395 (void) nvlist_lookup_uint64(rnvl, propname,
2398 rc = (rv64 < lv64) ? 1 : -1;
2400 if ((nvlist_lookup_string(lnvl, propname,
2401 &lvstr) == ENOENT) ||
2402 (nvlist_lookup_string(rnvl, propname,
2403 &rvstr) == ENOENT)) {
2406 rc = strcmp(lvstr, rvstr);
2410 case ZFS_PROP_QUOTA:
2413 if (prop == ZFS_PROP_USED)
2417 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2418 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2420 rc = (rv64 < lv64) ? 1 : -1;
2429 return (reverse ? 1 : -1);
2431 return (reverse ? -1 : 1);
2436 * If entries still seem to be the same, check if they are of the same
2437 * type (smbentity is added only if we are doing SID to POSIX ID
2438 * translation where we can have duplicate type/name combinations).
2440 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2441 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2443 return (lvb < rvb ? -1 : 1);
2449 zfs_prop_is_user(unsigned p)
2451 return (p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA ||
2452 p == ZFS_PROP_USEROBJUSED || p == ZFS_PROP_USEROBJQUOTA);
2456 zfs_prop_is_group(unsigned p)
2458 return (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA ||
2459 p == ZFS_PROP_GROUPOBJUSED || p == ZFS_PROP_GROUPOBJQUOTA);
2462 static inline const char *
2463 us_type2str(unsigned field_type)
2465 switch (field_type) {
2466 case USTYPE_PSX_USR:
2467 return ("POSIX User");
2468 case USTYPE_PSX_GRP:
2469 return ("POSIX Group");
2470 case USTYPE_SMB_USR:
2471 return ("SMB User");
2472 case USTYPE_SMB_GRP:
2473 return ("SMB Group");
2475 return ("Undefined");
2480 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2482 us_cbdata_t *cb = (us_cbdata_t *)arg;
2483 zfs_userquota_prop_t prop = cb->cb_prop;
2488 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2489 uu_avl_t *avl = cb->cb_avl;
2493 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2495 const char *typestr;
2499 int typeidx, nameidx, sizeidx;
2500 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2501 boolean_t smbentity = B_FALSE;
2503 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2505 node = safe_malloc(sizeof (us_node_t));
2506 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2507 node->usn_nvl = props;
2509 if (domain != NULL && domain[0] != '\0') {
2512 char sid[MAXNAMELEN + 32];
2516 directory_error_t e;
2520 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2522 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2523 type = USTYPE_SMB_GRP;
2524 err = sid_to_id(sid, B_FALSE, &id);
2526 type = USTYPE_SMB_USR;
2527 err = sid_to_id(sid, B_TRUE, &id);
2532 if (!cb->cb_sid2posix) {
2533 e = directory_name_from_sid(NULL, sid, &name,
2536 directory_error_free(e);
2546 #endif /* HAVE_IDMAP */
2549 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2551 if (zfs_prop_is_group(prop)) {
2552 type = USTYPE_PSX_GRP;
2553 if (!cb->cb_numname) {
2556 if ((g = getgrgid(rid)) != NULL)
2560 type = USTYPE_PSX_USR;
2561 if (!cb->cb_numname) {
2564 if ((p = getpwuid(rid)) != NULL)
2571 * Make sure that the type/name combination is unique when doing
2572 * SID to POSIX ID translation (hence changing the type from SMB to
2575 if (cb->cb_sid2posix &&
2576 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2579 /* Calculate/update width of TYPE field */
2580 typestr = us_type2str(type);
2581 typelen = strlen(gettext(typestr));
2582 typeidx = us_field_index("type");
2583 if (typelen > cb->cb_width[typeidx])
2584 cb->cb_width[typeidx] = typelen;
2585 if (nvlist_add_uint32(props, "type", type) != 0)
2588 /* Calculate/update width of NAME field */
2589 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2590 if (nvlist_add_uint64(props, "name", rid) != 0)
2592 namelen = snprintf(NULL, 0, "%u", rid);
2594 if (nvlist_add_string(props, "name", name) != 0)
2596 namelen = strlen(name);
2598 nameidx = us_field_index("name");
2599 if (nameidx >= 0 && namelen > cb->cb_width[nameidx])
2600 cb->cb_width[nameidx] = namelen;
2603 * Check if this type/name combination is in the list and update it;
2604 * otherwise add new node to the list.
2606 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2607 uu_avl_insert(avl, node, idx);
2612 props = node->usn_nvl;
2615 /* Calculate/update width of USED/QUOTA fields */
2616 if (cb->cb_nicenum) {
2617 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
2618 prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA) {
2619 zfs_nicebytes(space, sizebuf, sizeof (sizebuf));
2621 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2624 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
2625 (u_longlong_t)space);
2627 sizelen = strlen(sizebuf);
2628 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED) {
2630 if (!nvlist_exists(props, "quota"))
2631 (void) nvlist_add_uint64(props, "quota", 0);
2632 } else if (prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA) {
2634 if (!nvlist_exists(props, "used"))
2635 (void) nvlist_add_uint64(props, "used", 0);
2636 } else if (prop == ZFS_PROP_USEROBJUSED ||
2637 prop == ZFS_PROP_GROUPOBJUSED) {
2638 propname = "objused";
2639 if (!nvlist_exists(props, "objquota"))
2640 (void) nvlist_add_uint64(props, "objquota", 0);
2641 } else if (prop == ZFS_PROP_USEROBJQUOTA ||
2642 prop == ZFS_PROP_GROUPOBJQUOTA) {
2643 propname = "objquota";
2644 if (!nvlist_exists(props, "objused"))
2645 (void) nvlist_add_uint64(props, "objused", 0);
2649 sizeidx = us_field_index(propname);
2650 if (sizeidx >= 0 && sizelen > cb->cb_width[sizeidx])
2651 cb->cb_width[sizeidx] = sizelen;
2653 if (nvlist_add_uint64(props, propname, space) != 0)
2660 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2661 size_t *width, us_node_t *node)
2663 nvlist_t *nvl = node->usn_nvl;
2664 char valstr[MAXNAMELEN];
2665 boolean_t first = B_TRUE;
2671 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2672 if (!(ustype & types))
2675 while ((field = fields[cfield]) != USFIELD_LAST) {
2676 nvpair_t *nvp = NULL;
2682 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2683 if (strcmp(nvpair_name(nvp),
2684 us_field_names[field]) == 0)
2688 type = nvp == NULL ? DATA_TYPE_UNKNOWN : nvpair_type(nvp);
2690 case DATA_TYPE_UINT32:
2691 (void) nvpair_value_uint32(nvp, &val32);
2693 case DATA_TYPE_UINT64:
2694 (void) nvpair_value_uint64(nvp, &val64);
2696 case DATA_TYPE_STRING:
2697 (void) nvpair_value_string(nvp, &strval);
2699 case DATA_TYPE_UNKNOWN:
2702 (void) fprintf(stderr, "invalid data type\n");
2707 if (type == DATA_TYPE_UINT32)
2708 strval = (char *)us_type2str(val32);
2711 if (type == DATA_TYPE_UINT64) {
2712 (void) sprintf(valstr, "%llu",
2713 (u_longlong_t)val64);
2719 if (type == DATA_TYPE_UINT64) {
2721 (void) sprintf(valstr, "%llu",
2722 (u_longlong_t)val64);
2724 } else if (field == USFIELD_QUOTA &&
2728 zfs_nicebytes(val64, valstr,
2734 case USFIELD_OBJUSED:
2735 case USFIELD_OBJQUOTA:
2736 if (type == DATA_TYPE_UINT64) {
2738 (void) sprintf(valstr, "%llu",
2739 (u_longlong_t)val64);
2741 } else if (field == USFIELD_OBJQUOTA &&
2745 zfs_nicenum(val64, valstr,
2755 (void) printf("\t");
2760 (void) printf("%s", strval);
2761 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2762 (void) printf("%-*s", (int)width[field], strval);
2764 (void) printf("%*s", (int)width[field], strval);
2770 (void) printf("\n");
2774 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2775 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2783 boolean_t first = B_TRUE;
2785 while ((field = fields[cfield]) != USFIELD_LAST) {
2786 col = gettext(us_field_hdr[field]);
2787 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2788 (void) printf(first ? "%-*s" : " %-*s",
2789 (int)width[field], col);
2791 (void) printf(first ? "%*s" : " %*s",
2792 (int)width[field], col);
2797 (void) printf("\n");
2800 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2801 print_us_node(scripted, parsable, fields, types, width, node);
2803 nvlist_free(node->usn_nvl);
2808 zfs_do_userspace(int argc, char **argv)
2811 zfs_userquota_prop_t p;
2812 uu_avl_pool_t *avl_pool;
2814 uu_avl_walk_t *walk;
2816 char deffields[] = "type,name,used,quota,objused,objquota";
2817 char *ofield = NULL;
2818 char *tfield = NULL;
2822 boolean_t scripted = B_FALSE;
2823 boolean_t prtnum = B_FALSE;
2824 boolean_t parsable = B_FALSE;
2825 boolean_t sid2posix = B_FALSE;
2828 zfs_sort_column_t *sortcol = NULL;
2829 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2833 uu_list_pool_t *listpool;
2835 uu_avl_index_t idx = 0;
2836 uu_list_index_t idx2 = 0;
2841 if (strcmp(argv[0], "groupspace") == 0)
2842 /* Toggle default group types */
2843 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2845 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2861 if (zfs_add_sort_column(&sortcol, optarg,
2862 c == 's' ? B_FALSE : B_TRUE) != 0) {
2863 (void) fprintf(stderr,
2864 gettext("invalid field '%s'\n"), optarg);
2875 (void) fprintf(stderr, gettext("missing argument for "
2876 "'%c' option\n"), optopt);
2880 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2890 (void) fprintf(stderr, gettext("missing dataset name\n"));
2894 (void) fprintf(stderr, gettext("too many arguments\n"));
2898 /* Use default output fields if not specified using -o */
2902 if ((delim = strchr(ofield, ',')) != NULL)
2904 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2905 (void) fprintf(stderr, gettext("invalid type '%s' "
2906 "for -o option\n"), ofield);
2911 } while (delim != NULL);
2912 fields[cfield] = USFIELD_LAST;
2914 /* Override output types (-t option) */
2915 if (tfield != NULL) {
2919 boolean_t found = B_FALSE;
2921 if ((delim = strchr(tfield, ',')) != NULL)
2923 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2925 if (strcmp(tfield, us_type_names[i]) == 0) {
2927 types |= us_type_bits[i];
2932 (void) fprintf(stderr, gettext("invalid type "
2933 "'%s' for -t option\n"), tfield);
2938 } while (delim != NULL);
2941 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
2944 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
2945 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
2947 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
2950 /* Always add default sorting columns */
2951 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
2952 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
2954 cb.cb_sortcol = sortcol;
2955 cb.cb_numname = prtnum;
2956 cb.cb_nicenum = !parsable;
2957 cb.cb_avl_pool = avl_pool;
2958 cb.cb_avl = avl_tree;
2959 cb.cb_sid2posix = sid2posix;
2961 for (i = 0; i < USFIELD_LAST; i++)
2962 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
2964 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
2965 if ((zfs_prop_is_user(p) &&
2966 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
2967 (zfs_prop_is_group(p) &&
2968 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))))
2972 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
2977 if ((node = uu_avl_first(avl_tree)) == NULL)
2980 us_populated = B_TRUE;
2982 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
2983 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
2984 list = uu_list_create(listpool, NULL, UU_DEFAULT);
2985 uu_list_node_init(node, &node->usn_listnode, listpool);
2987 while (node != NULL) {
2989 node = uu_avl_next(avl_tree, node);
2990 uu_avl_remove(avl_tree, rmnode);
2991 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
2992 uu_list_insert(list, rmnode, idx2);
2995 for (node = uu_list_first(list); node != NULL;
2996 node = uu_list_next(list, node)) {
2997 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
2999 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
3000 uu_avl_insert(avl_tree, node, idx);
3003 uu_list_destroy(list);
3004 uu_list_pool_destroy(listpool);
3006 /* Print and free node nvlist memory */
3007 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
3010 zfs_free_sort_columns(sortcol);
3012 /* Clean up the AVL tree */
3013 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
3016 while ((node = uu_avl_walk_next(walk)) != NULL) {
3017 uu_avl_remove(cb.cb_avl, node);
3021 uu_avl_walk_end(walk);
3022 uu_avl_destroy(avl_tree);
3023 uu_avl_pool_destroy(avl_pool);
3029 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3030 * [-t type[,...]] [filesystem|volume|snapshot] ...
3032 * -H Scripted mode; elide headers and separate columns by tabs
3033 * -p Display values in parsable (literal) format.
3034 * -r Recurse over all children
3035 * -d Limit recursion by depth.
3036 * -o Control which fields to display.
3037 * -s Specify sort columns, descending order.
3038 * -S Specify sort columns, ascending order.
3039 * -t Control which object types to display.
3041 * When given no arguments, list all filesystems in the system.
3042 * Otherwise, list the specified datasets, optionally recursing down them if
3043 * '-r' is specified.
3045 typedef struct list_cbdata {
3047 boolean_t cb_literal;
3048 boolean_t cb_scripted;
3049 zprop_list_t *cb_proplist;
3053 * Given a list of columns to display, output appropriate headers for each one.
3056 print_header(list_cbdata_t *cb)
3058 zprop_list_t *pl = cb->cb_proplist;
3059 char headerbuf[ZFS_MAXPROPLEN];
3062 boolean_t first = B_TRUE;
3063 boolean_t right_justify;
3065 for (; pl != NULL; pl = pl->pl_next) {
3072 right_justify = B_FALSE;
3073 if (pl->pl_prop != ZPROP_INVAL) {
3074 header = zfs_prop_column_name(pl->pl_prop);
3075 right_justify = zfs_prop_align_right(pl->pl_prop);
3077 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3078 headerbuf[i] = toupper(pl->pl_user_prop[i]);
3079 headerbuf[i] = '\0';
3083 if (pl->pl_next == NULL && !right_justify)
3084 (void) printf("%s", header);
3085 else if (right_justify)
3086 (void) printf("%*s", (int)pl->pl_width, header);
3088 (void) printf("%-*s", (int)pl->pl_width, header);
3091 (void) printf("\n");
3095 * Given a dataset and a list of fields, print out all the properties according
3096 * to the described layout.
3099 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3101 zprop_list_t *pl = cb->cb_proplist;
3102 boolean_t first = B_TRUE;
3103 char property[ZFS_MAXPROPLEN];
3104 nvlist_t *userprops = zfs_get_user_props(zhp);
3107 boolean_t right_justify;
3109 for (; pl != NULL; pl = pl->pl_next) {
3111 if (cb->cb_scripted)
3112 (void) printf("\t");
3119 if (pl->pl_prop == ZFS_PROP_NAME) {
3120 (void) strlcpy(property, zfs_get_name(zhp),
3123 right_justify = zfs_prop_align_right(pl->pl_prop);
3124 } else if (pl->pl_prop != ZPROP_INVAL) {
3125 if (zfs_prop_get(zhp, pl->pl_prop, property,
3126 sizeof (property), NULL, NULL, 0,
3127 cb->cb_literal) != 0)
3131 right_justify = zfs_prop_align_right(pl->pl_prop);
3132 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3133 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3134 property, sizeof (property), cb->cb_literal) != 0)
3138 right_justify = B_TRUE;
3139 } else if (zfs_prop_written(pl->pl_user_prop)) {
3140 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3141 property, sizeof (property), cb->cb_literal) != 0)
3145 right_justify = B_TRUE;
3147 if (nvlist_lookup_nvlist(userprops,
3148 pl->pl_user_prop, &propval) != 0)
3151 verify(nvlist_lookup_string(propval,
3152 ZPROP_VALUE, &propstr) == 0);
3153 right_justify = B_FALSE;
3157 * If this is being called in scripted mode, or if this is the
3158 * last column and it is left-justified, don't include a width
3161 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3162 (void) printf("%s", propstr);
3163 else if (right_justify)
3164 (void) printf("%*s", (int)pl->pl_width, propstr);
3166 (void) printf("%-*s", (int)pl->pl_width, propstr);
3169 (void) printf("\n");
3173 * Generic callback function to list a dataset or snapshot.
3176 list_callback(zfs_handle_t *zhp, void *data)
3178 list_cbdata_t *cbp = data;
3180 if (cbp->cb_first) {
3181 if (!cbp->cb_scripted)
3183 cbp->cb_first = B_FALSE;
3186 print_dataset(zhp, cbp);
3192 zfs_do_list(int argc, char **argv)
3195 static char default_fields[] =
3196 "name,used,available,referenced,mountpoint";
3197 int types = ZFS_TYPE_DATASET;
3198 boolean_t types_specified = B_FALSE;
3199 char *fields = NULL;
3200 list_cbdata_t cb = { 0 };
3204 zfs_sort_column_t *sortcol = NULL;
3205 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3208 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3214 cb.cb_literal = B_TRUE;
3215 flags |= ZFS_ITER_LITERAL_PROPS;
3218 limit = parse_depth(optarg, &flags);
3221 flags |= ZFS_ITER_RECURSE;
3224 cb.cb_scripted = B_TRUE;
3227 if (zfs_add_sort_column(&sortcol, optarg,
3229 (void) fprintf(stderr,
3230 gettext("invalid property '%s'\n"), optarg);
3235 if (zfs_add_sort_column(&sortcol, optarg,
3237 (void) fprintf(stderr,
3238 gettext("invalid property '%s'\n"), optarg);
3244 types_specified = B_TRUE;
3245 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3246 while (*optarg != '\0') {
3247 static char *type_subopts[] = { "filesystem",
3248 "volume", "snapshot", "snap", "bookmark",
3251 switch (getsubopt(&optarg, type_subopts,
3254 types |= ZFS_TYPE_FILESYSTEM;
3257 types |= ZFS_TYPE_VOLUME;
3261 types |= ZFS_TYPE_SNAPSHOT;
3264 types |= ZFS_TYPE_BOOKMARK;
3267 types = ZFS_TYPE_DATASET |
3271 (void) fprintf(stderr,
3272 gettext("invalid type '%s'\n"),
3279 (void) fprintf(stderr, gettext("missing argument for "
3280 "'%c' option\n"), optopt);
3284 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3294 fields = default_fields;
3297 * If we are only going to list snapshot names and sort by name,
3298 * then we can use faster version.
3300 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3301 flags |= ZFS_ITER_SIMPLE;
3304 * If "-o space" and no types were specified, don't display snapshots.
3306 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3307 types &= ~ZFS_TYPE_SNAPSHOT;
3310 * If the user specifies '-o all', the zprop_get_list() doesn't
3311 * normally include the name of the dataset. For 'zfs list', we always
3312 * want this property to be first.
3314 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3318 cb.cb_first = B_TRUE;
3320 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3321 limit, list_callback, &cb);
3323 zprop_free_list(cb.cb_proplist);
3324 zfs_free_sort_columns(sortcol);
3326 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3327 (void) fprintf(stderr, gettext("no datasets available\n"));
3333 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3334 * zfs rename [-f] -p <fs | vol> <fs | vol>
3335 * zfs rename -r <snap> <snap>
3337 * Renames the given dataset to another of the same type.
3339 * The '-p' flag creates all the non-existing ancestors of the target first.
3343 zfs_do_rename(int argc, char **argv)
3348 boolean_t recurse = B_FALSE;
3349 boolean_t parents = B_FALSE;
3350 boolean_t force_unmount = B_FALSE;
3353 while ((c = getopt(argc, argv, "prf")) != -1) {
3362 force_unmount = B_TRUE;
3366 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3375 /* check number of arguments */
3377 (void) fprintf(stderr, gettext("missing source dataset "
3382 (void) fprintf(stderr, gettext("missing target dataset "
3387 (void) fprintf(stderr, gettext("too many arguments\n"));
3391 if (recurse && parents) {
3392 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3397 if (recurse && strchr(argv[0], '@') == 0) {
3398 (void) fprintf(stderr, gettext("source dataset for recursive "
3399 "rename must be a snapshot\n"));
3403 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3404 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3407 /* If we were asked and the name looks good, try to create ancestors. */
3408 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3409 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3414 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3423 * Promotes the given clone fs to be the parent
3427 zfs_do_promote(int argc, char **argv)
3433 if (argc > 1 && argv[1][0] == '-') {
3434 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3439 /* check number of arguments */
3441 (void) fprintf(stderr, gettext("missing clone filesystem"
3446 (void) fprintf(stderr, gettext("too many arguments\n"));
3450 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3454 ret = (zfs_promote(zhp) != 0);
3462 * zfs rollback [-rRf] <snapshot>
3464 * -r Delete any intervening snapshots before doing rollback
3465 * -R Delete any snapshots and their clones
3466 * -f ignored for backwards compatibility
3468 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3469 * since then and making it the active dataset. If more recent snapshots exist,
3470 * the command will complain unless the '-r' flag is given.
3472 typedef struct rollback_cbdata {
3478 boolean_t cb_recurse;
3479 } rollback_cbdata_t;
3482 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3484 rollback_cbdata_t *cbp = data;
3486 if (cbp->cb_first && cbp->cb_recurse) {
3487 (void) fprintf(stderr, gettext("cannot rollback to "
3488 "'%s': clones of previous snapshots exist\n"),
3490 (void) fprintf(stderr, gettext("use '-R' to "
3491 "force deletion of the following clones and "
3497 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3505 * Report any snapshots more recent than the one specified. Used when '-r' is
3506 * not specified. We reuse this same callback for the snapshot dependents - if
3507 * 'cb_dependent' is set, then this is a dependent and we should report it
3508 * without checking the transaction group.
3511 rollback_check(zfs_handle_t *zhp, void *data)
3513 rollback_cbdata_t *cbp = data;
3515 if (cbp->cb_doclones) {
3520 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3521 if (cbp->cb_first && !cbp->cb_recurse) {
3522 (void) fprintf(stderr, gettext("cannot "
3523 "rollback to '%s': more recent snapshots "
3524 "or bookmarks exist\n"),
3526 (void) fprintf(stderr, gettext("use '-r' to "
3527 "force deletion of the following "
3528 "snapshots and bookmarks:\n"));
3533 if (cbp->cb_recurse) {
3534 if (zfs_iter_dependents(zhp, B_TRUE,
3535 rollback_check_dependent, cbp) != 0) {
3540 (void) fprintf(stderr, "%s\n",
3549 zfs_do_rollback(int argc, char **argv)
3553 boolean_t force = B_FALSE;
3554 rollback_cbdata_t cb = { 0 };
3555 zfs_handle_t *zhp, *snap;
3556 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3560 while ((c = getopt(argc, argv, "rRf")) != -1) {
3573 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3582 /* check number of arguments */
3584 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3588 (void) fprintf(stderr, gettext("too many arguments\n"));
3592 /* open the snapshot */
3593 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3596 /* open the parent dataset */
3597 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3598 verify((delim = strrchr(parentname, '@')) != NULL);
3600 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3606 * Check for more recent snapshots and/or clones based on the presence
3609 cb.cb_target = argv[0];
3610 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3611 cb.cb_first = B_TRUE;
3613 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3615 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3618 if ((ret = cb.cb_error) != 0)
3622 * Rollback parent to the given snapshot.
3624 ret = zfs_rollback(zhp, snap, force);
3637 * zfs set property=value ... { fs | snap | vol } ...
3639 * Sets the given properties for all datasets specified on the command line.
3643 set_callback(zfs_handle_t *zhp, void *data)
3645 nvlist_t *props = data;
3647 if (zfs_prop_set_list(zhp, props) != 0) {
3648 switch (libzfs_errno(g_zfs)) {
3649 case EZFS_MOUNTFAILED:
3650 (void) fprintf(stderr, gettext("property may be set "
3651 "but unable to remount filesystem\n"));
3653 case EZFS_SHARENFSFAILED:
3654 (void) fprintf(stderr, gettext("property may be set "
3655 "but unable to reshare filesystem\n"));
3664 zfs_do_set(int argc, char **argv)
3666 nvlist_t *props = NULL;
3667 int ds_start = -1; /* argv idx of first dataset arg */
3671 /* check for options */
3672 if (argc > 1 && argv[1][0] == '-') {
3673 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3678 /* check number of arguments */
3680 (void) fprintf(stderr, gettext("missing arguments\n"));
3684 if (strchr(argv[1], '=') == NULL) {
3685 (void) fprintf(stderr, gettext("missing property=value "
3688 (void) fprintf(stderr, gettext("missing dataset "
3694 /* validate argument order: prop=val args followed by dataset args */
3695 for (i = 1; i < argc; i++) {
3696 if (strchr(argv[i], '=') != NULL) {
3698 /* out-of-order prop=val argument */
3699 (void) fprintf(stderr, gettext("invalid "
3700 "argument order\n"));
3703 } else if (ds_start < 0) {
3708 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3712 /* Populate a list of property settings */
3713 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3715 for (i = 1; i < ds_start; i++) {
3716 if (!parseprop(props, argv[i])) {
3722 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3723 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3730 typedef struct snap_cbdata {
3732 boolean_t sd_recursive;
3733 const char *sd_snapname;
3737 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3739 snap_cbdata_t *sd = arg;
3744 if (sd->sd_recursive &&
3745 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3750 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3753 fnvlist_add_boolean(sd->sd_nvl, name);
3756 if (sd->sd_recursive)
3757 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3763 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3765 * Creates a snapshot with the given name. While functionally equivalent to
3766 * 'zfs create', it is a separate command to differentiate intent.
3769 zfs_do_snapshot(int argc, char **argv)
3774 snap_cbdata_t sd = { 0 };
3775 boolean_t multiple_snaps = B_FALSE;
3777 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3779 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3783 while ((c = getopt(argc, argv, "ro:")) != -1) {
3786 if (!parseprop(props, optarg)) {
3787 nvlist_free(sd.sd_nvl);
3793 sd.sd_recursive = B_TRUE;
3794 multiple_snaps = B_TRUE;
3797 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3806 /* check number of arguments */
3808 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3813 multiple_snaps = B_TRUE;
3814 for (; argc > 0; argc--, argv++) {
3818 atp = strchr(argv[0], '@');
3822 sd.sd_snapname = atp + 1;
3823 zhp = zfs_open(g_zfs, argv[0],
3824 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3827 if (zfs_snapshot_cb(zhp, &sd) != 0)
3831 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3832 nvlist_free(sd.sd_nvl);
3834 if (ret != 0 && multiple_snaps)
3835 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3839 nvlist_free(sd.sd_nvl);
3846 * Send a backup stream to stdout.
3849 zfs_do_send(int argc, char **argv)
3851 char *fromname = NULL;
3852 char *toname = NULL;
3853 char *resume_token = NULL;
3856 sendflags_t flags = { 0 };
3858 nvlist_t *dbgnv = NULL;
3859 boolean_t extraverbose = B_FALSE;
3861 struct option long_options[] = {
3862 {"replicate", no_argument, NULL, 'R'},
3863 {"props", no_argument, NULL, 'p'},
3864 {"parsable", no_argument, NULL, 'P'},
3865 {"dedup", no_argument, NULL, 'D'},
3866 {"verbose", no_argument, NULL, 'v'},
3867 {"dryrun", no_argument, NULL, 'n'},
3868 {"large-block", no_argument, NULL, 'L'},
3869 {"embed", no_argument, NULL, 'e'},
3870 {"resume", required_argument, NULL, 't'},
3871 {"compressed", no_argument, NULL, 'c'},
3872 {"raw", no_argument, NULL, 'w'},
3877 while ((c = getopt_long(argc, argv, ":i:I:RDpvnPLet:cw", long_options,
3889 flags.doall = B_TRUE;
3892 flags.replicate = B_TRUE;
3895 flags.props = B_TRUE;
3898 flags.parsable = B_TRUE;
3899 flags.verbose = B_TRUE;
3903 extraverbose = B_TRUE;
3904 flags.verbose = B_TRUE;
3905 flags.progress = B_TRUE;
3908 flags.dedup = B_TRUE;
3911 flags.dryrun = B_TRUE;
3914 flags.largeblock = B_TRUE;
3917 flags.embed_data = B_TRUE;
3920 resume_token = optarg;
3923 flags.compress = B_TRUE;
3927 flags.compress = B_TRUE;
3928 flags.embed_data = B_TRUE;
3929 flags.largeblock = B_TRUE;
3933 * If a parameter was not passed, optopt contains the
3934 * value that would normally lead us into the
3935 * appropriate case statement. If it's > 256, then this
3936 * must be a longopt and we should look at argv to get
3937 * the string. Otherwise it's just the character, so we
3938 * should use it directly.
3940 if (optopt <= UINT8_MAX) {
3941 (void) fprintf(stderr,
3942 gettext("missing argument for '%c' "
3943 "option\n"), optopt);
3945 (void) fprintf(stderr,
3946 gettext("missing argument for '%s' "
3947 "option\n"), argv[optind - 1]);
3955 * If an invalid flag was passed, optopt contains the
3956 * character if it was a short flag, or 0 if it was a
3960 (void) fprintf(stderr,
3961 gettext("invalid option '%c'\n"), optopt);
3963 (void) fprintf(stderr,
3964 gettext("invalid option '%s'\n"),
3975 if (resume_token != NULL) {
3976 if (fromname != NULL || flags.replicate || flags.props ||
3978 (void) fprintf(stderr,
3979 gettext("invalid flags combined with -t\n"));
3983 (void) fprintf(stderr, gettext("no additional "
3984 "arguments are permitted with -t\n"));
3989 (void) fprintf(stderr,
3990 gettext("missing snapshot argument\n"));
3994 (void) fprintf(stderr, gettext("too many arguments\n"));
3999 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
4000 (void) fprintf(stderr,
4001 gettext("Error: Stream can not be written to a terminal.\n"
4002 "You must redirect standard output.\n"));
4006 if (resume_token != NULL) {
4007 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
4012 * Special case sending a filesystem, or from a bookmark.
4014 if (strchr(argv[0], '@') == NULL ||
4015 (fromname && strchr(fromname, '#') != NULL)) {
4016 char frombuf[ZFS_MAX_DATASET_NAME_LEN];
4018 if (flags.replicate || flags.doall || flags.props ||
4019 flags.dedup || (strchr(argv[0], '@') == NULL &&
4020 (flags.dryrun || flags.verbose || flags.progress))) {
4021 (void) fprintf(stderr, gettext("Error: "
4022 "Unsupported flag with filesystem or bookmark.\n"));
4026 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
4030 if (fromname != NULL &&
4031 (fromname[0] == '#' || fromname[0] == '@')) {
4033 * Incremental source name begins with # or @.
4034 * Default to same fs as target.
4036 (void) strlcpy(frombuf, argv[0], sizeof (frombuf));
4037 cp = strchr(frombuf, '@');
4040 (void) strlcat(frombuf, fromname, sizeof (frombuf));
4043 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, flags);
4048 cp = strchr(argv[0], '@');
4051 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4056 * If they specified the full path to the snapshot, chop off
4057 * everything except the short name of the snapshot, but special
4058 * case if they specify the origin.
4060 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
4061 char origin[ZFS_MAX_DATASET_NAME_LEN];
4064 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
4065 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
4067 if (strcmp(origin, fromname) == 0) {
4069 flags.fromorigin = B_TRUE;
4072 if (cp != fromname && strcmp(argv[0], fromname)) {
4073 (void) fprintf(stderr,
4074 gettext("incremental source must be "
4075 "in same filesystem\n"));
4079 if (strchr(fromname, '@') || strchr(fromname, '/')) {
4080 (void) fprintf(stderr,
4081 gettext("invalid incremental source\n"));
4087 if (flags.replicate && fromname == NULL)
4088 flags.doall = B_TRUE;
4090 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
4091 extraverbose ? &dbgnv : NULL);
4093 if (extraverbose && dbgnv != NULL) {
4095 * dump_nvlist prints to stdout, but that's been
4096 * redirected to a file. Make it print to stderr
4099 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
4100 dump_nvlist(dbgnv, 0);
4109 * Restore a backup stream from stdin.
4112 zfs_do_receive(int argc, char **argv)
4115 recvflags_t flags = { 0 };
4116 boolean_t abort_resumable = B_FALSE;
4119 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4123 while ((c = getopt(argc, argv, ":o:x:denuvFsA")) != -1) {
4126 if (!parseprop(props, optarg)) {
4132 if (!parsepropname(props, optarg)) {
4138 flags.isprefix = B_TRUE;
4141 flags.isprefix = B_TRUE;
4142 flags.istail = B_TRUE;
4145 flags.dryrun = B_TRUE;
4148 flags.nomount = B_TRUE;
4151 flags.verbose = B_TRUE;
4154 flags.resumable = B_TRUE;
4157 flags.force = B_TRUE;
4160 abort_resumable = B_TRUE;
4163 (void) fprintf(stderr, gettext("missing argument for "
4164 "'%c' option\n"), optopt);
4168 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4177 /* check number of arguments */
4179 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4183 (void) fprintf(stderr, gettext("too many arguments\n"));
4187 if (abort_resumable) {
4188 if (flags.isprefix || flags.istail || flags.dryrun ||
4189 flags.resumable || flags.nomount) {
4190 (void) fprintf(stderr, gettext("invalid option\n"));
4194 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4195 (void) snprintf(namebuf, sizeof (namebuf),
4196 "%s/%%recv", argv[0]);
4198 if (zfs_dataset_exists(g_zfs, namebuf,
4199 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4200 zfs_handle_t *zhp = zfs_open(g_zfs,
4201 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4206 err = zfs_destroy(zhp, B_FALSE);
4209 zfs_handle_t *zhp = zfs_open(g_zfs,
4210 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4213 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4214 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4215 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4216 (void) fprintf(stderr,
4217 gettext("'%s' does not have any "
4218 "resumable receive state to abort\n"),
4224 err = zfs_destroy(zhp, B_FALSE);
4231 if (isatty(STDIN_FILENO)) {
4232 (void) fprintf(stderr,
4233 gettext("Error: Backup stream can not be read "
4234 "from a terminal.\n"
4235 "You must redirect standard input.\n"));
4239 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4246 * allow/unallow stuff
4248 /* copied from zfs/sys/dsl_deleg.h */
4249 #define ZFS_DELEG_PERM_CREATE "create"
4250 #define ZFS_DELEG_PERM_DESTROY "destroy"
4251 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4252 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4253 #define ZFS_DELEG_PERM_CLONE "clone"
4254 #define ZFS_DELEG_PERM_PROMOTE "promote"
4255 #define ZFS_DELEG_PERM_RENAME "rename"
4256 #define ZFS_DELEG_PERM_MOUNT "mount"
4257 #define ZFS_DELEG_PERM_SHARE "share"
4258 #define ZFS_DELEG_PERM_SEND "send"
4259 #define ZFS_DELEG_PERM_RECEIVE "receive"
4260 #define ZFS_DELEG_PERM_ALLOW "allow"
4261 #define ZFS_DELEG_PERM_USERPROP "userprop"
4262 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4263 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4264 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4265 #define ZFS_DELEG_PERM_USERUSED "userused"
4266 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4267 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4268 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4269 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4270 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4272 #define ZFS_DELEG_PERM_HOLD "hold"
4273 #define ZFS_DELEG_PERM_RELEASE "release"
4274 #define ZFS_DELEG_PERM_DIFF "diff"
4275 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4276 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
4277 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
4279 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4281 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4282 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4283 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4284 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4285 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4286 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4287 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4288 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4289 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4290 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4291 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4292 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4293 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4294 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4295 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4296 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4297 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4298 { ZFS_DELEG_PERM_LOAD_KEY, ZFS_DELEG_NOTE_LOAD_KEY },
4299 { ZFS_DELEG_PERM_CHANGE_KEY, ZFS_DELEG_NOTE_CHANGE_KEY },
4301 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4302 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4303 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4304 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4305 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4306 { ZFS_DELEG_PERM_USEROBJQUOTA, ZFS_DELEG_NOTE_USEROBJQUOTA },
4307 { ZFS_DELEG_PERM_USEROBJUSED, ZFS_DELEG_NOTE_USEROBJUSED },
4308 { ZFS_DELEG_PERM_GROUPOBJQUOTA, ZFS_DELEG_NOTE_GROUPOBJQUOTA },
4309 { ZFS_DELEG_PERM_GROUPOBJUSED, ZFS_DELEG_NOTE_GROUPOBJUSED },
4310 { NULL, ZFS_DELEG_NOTE_NONE }
4313 /* permission structure */
4314 typedef struct deleg_perm {
4315 zfs_deleg_who_type_t dp_who_type;
4316 const char *dp_name;
4318 boolean_t dp_descend;
4322 typedef struct deleg_perm_node {
4323 deleg_perm_t dpn_perm;
4325 uu_avl_node_t dpn_avl_node;
4326 } deleg_perm_node_t;
4328 typedef struct fs_perm fs_perm_t;
4330 /* permissions set */
4331 typedef struct who_perm {
4332 zfs_deleg_who_type_t who_type;
4333 const char *who_name; /* id */
4334 char who_ug_name[256]; /* user/group name */
4335 fs_perm_t *who_fsperm; /* uplink */
4337 uu_avl_t *who_deleg_perm_avl; /* permissions */
4341 typedef struct who_perm_node {
4342 who_perm_t who_perm;
4343 uu_avl_node_t who_avl_node;
4346 typedef struct fs_perm_set fs_perm_set_t;
4347 /* fs permissions */
4349 const char *fsp_name;
4351 uu_avl_t *fsp_sc_avl; /* sets,create */
4352 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4354 fs_perm_set_t *fsp_set; /* uplink */
4358 typedef struct fs_perm_node {
4359 fs_perm_t fspn_fsperm;
4362 uu_list_node_t fspn_list_node;
4365 /* top level structure */
4366 struct fs_perm_set {
4367 uu_list_pool_t *fsps_list_pool;
4368 uu_list_t *fsps_list; /* list of fs_perms */
4370 uu_avl_pool_t *fsps_named_set_avl_pool;
4371 uu_avl_pool_t *fsps_who_perm_avl_pool;
4372 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4375 static inline const char *
4376 deleg_perm_type(zfs_deleg_note_t note)
4382 case ZFS_DELEG_NOTE_GROUPQUOTA:
4383 case ZFS_DELEG_NOTE_GROUPUSED:
4384 case ZFS_DELEG_NOTE_USERPROP:
4385 case ZFS_DELEG_NOTE_USERQUOTA:
4386 case ZFS_DELEG_NOTE_USERUSED:
4387 case ZFS_DELEG_NOTE_USEROBJQUOTA:
4388 case ZFS_DELEG_NOTE_USEROBJUSED:
4389 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
4390 case ZFS_DELEG_NOTE_GROUPOBJUSED:
4392 return (gettext("other"));
4394 return (gettext("subcommand"));
4399 who_type2weight(zfs_deleg_who_type_t who_type)
4403 case ZFS_DELEG_NAMED_SET_SETS:
4404 case ZFS_DELEG_NAMED_SET:
4407 case ZFS_DELEG_CREATE_SETS:
4408 case ZFS_DELEG_CREATE:
4411 case ZFS_DELEG_USER_SETS:
4412 case ZFS_DELEG_USER:
4415 case ZFS_DELEG_GROUP_SETS:
4416 case ZFS_DELEG_GROUP:
4419 case ZFS_DELEG_EVERYONE_SETS:
4420 case ZFS_DELEG_EVERYONE:
4432 who_perm_compare(const void *larg, const void *rarg, void *unused)
4434 const who_perm_node_t *l = larg;
4435 const who_perm_node_t *r = rarg;
4436 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4437 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4438 int lweight = who_type2weight(ltype);
4439 int rweight = who_type2weight(rtype);
4440 int res = lweight - rweight;
4442 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4443 ZFS_MAX_DELEG_NAME-1);
4455 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4457 const deleg_perm_node_t *l = larg;
4458 const deleg_perm_node_t *r = rarg;
4459 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4460 ZFS_MAX_DELEG_NAME-1);
4472 fs_perm_set_init(fs_perm_set_t *fspset)
4474 bzero(fspset, sizeof (fs_perm_set_t));
4476 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4477 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4478 NULL, UU_DEFAULT)) == NULL)
4480 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4481 UU_DEFAULT)) == NULL)
4484 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4485 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4486 who_perm_node_t, who_avl_node), who_perm_compare,
4487 UU_DEFAULT)) == NULL)
4490 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4491 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4492 who_perm_node_t, who_avl_node), who_perm_compare,
4493 UU_DEFAULT)) == NULL)
4496 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4497 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4498 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4503 static inline void fs_perm_fini(fs_perm_t *);
4504 static inline void who_perm_fini(who_perm_t *);
4507 fs_perm_set_fini(fs_perm_set_t *fspset)
4509 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4511 while (node != NULL) {
4512 fs_perm_node_t *next_node =
4513 uu_list_next(fspset->fsps_list, node);
4514 fs_perm_t *fsperm = &node->fspn_fsperm;
4515 fs_perm_fini(fsperm);
4516 uu_list_remove(fspset->fsps_list, node);
4521 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4522 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4523 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4527 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4530 deleg_perm->dp_who_type = type;
4531 deleg_perm->dp_name = name;
4535 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4536 zfs_deleg_who_type_t type, const char *name)
4538 uu_avl_pool_t *pool;
4539 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4541 bzero(who_perm, sizeof (who_perm_t));
4543 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4544 UU_DEFAULT)) == NULL)
4547 who_perm->who_type = type;
4548 who_perm->who_name = name;
4549 who_perm->who_fsperm = fsperm;
4553 who_perm_fini(who_perm_t *who_perm)
4555 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4557 while (node != NULL) {
4558 deleg_perm_node_t *next_node =
4559 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4561 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4566 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4570 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4572 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4573 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4575 bzero(fsperm, sizeof (fs_perm_t));
4577 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4581 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4585 fsperm->fsp_set = fspset;
4586 fsperm->fsp_name = fsname;
4590 fs_perm_fini(fs_perm_t *fsperm)
4592 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4593 while (node != NULL) {
4594 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4596 who_perm_t *who_perm = &node->who_perm;
4597 who_perm_fini(who_perm);
4598 uu_avl_remove(fsperm->fsp_sc_avl, node);
4603 node = uu_avl_first(fsperm->fsp_uge_avl);
4604 while (node != NULL) {
4605 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4607 who_perm_t *who_perm = &node->who_perm;
4608 who_perm_fini(who_perm);
4609 uu_avl_remove(fsperm->fsp_uge_avl, node);
4614 uu_avl_destroy(fsperm->fsp_sc_avl);
4615 uu_avl_destroy(fsperm->fsp_uge_avl);
4619 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4620 zfs_deleg_who_type_t who_type, const char *name, char locality)
4622 uu_avl_index_t idx = 0;
4624 deleg_perm_node_t *found_node = NULL;
4625 deleg_perm_t *deleg_perm = &node->dpn_perm;
4627 deleg_perm_init(deleg_perm, who_type, name);
4629 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4631 uu_avl_insert(avl, node, idx);
4634 deleg_perm = &node->dpn_perm;
4639 case ZFS_DELEG_LOCAL:
4640 deleg_perm->dp_local = B_TRUE;
4642 case ZFS_DELEG_DESCENDENT:
4643 deleg_perm->dp_descend = B_TRUE;
4648 assert(B_FALSE); /* invalid locality */
4653 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4655 nvpair_t *nvp = NULL;
4656 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4657 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4658 zfs_deleg_who_type_t who_type = who_perm->who_type;
4660 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4661 const char *name = nvpair_name(nvp);
4662 data_type_t type = nvpair_type(nvp);
4663 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4664 deleg_perm_node_t *node =
4665 safe_malloc(sizeof (deleg_perm_node_t));
4667 VERIFY(type == DATA_TYPE_BOOLEAN);
4669 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4670 set_deleg_perm_node(avl, node, who_type, name, locality);
4677 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4679 nvpair_t *nvp = NULL;
4680 fs_perm_set_t *fspset = fsperm->fsp_set;
4682 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4683 nvlist_t *nvl2 = NULL;
4684 const char *name = nvpair_name(nvp);
4685 uu_avl_t *avl = NULL;
4686 uu_avl_pool_t *avl_pool = NULL;
4687 zfs_deleg_who_type_t perm_type = name[0];
4688 char perm_locality = name[1];
4689 const char *perm_name = name + 3;
4690 boolean_t is_set = B_TRUE;
4691 who_perm_t *who_perm = NULL;
4693 assert('$' == name[2]);
4695 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4698 switch (perm_type) {
4699 case ZFS_DELEG_CREATE:
4700 case ZFS_DELEG_CREATE_SETS:
4701 case ZFS_DELEG_NAMED_SET:
4702 case ZFS_DELEG_NAMED_SET_SETS:
4703 avl_pool = fspset->fsps_named_set_avl_pool;
4704 avl = fsperm->fsp_sc_avl;
4706 case ZFS_DELEG_USER:
4707 case ZFS_DELEG_USER_SETS:
4708 case ZFS_DELEG_GROUP:
4709 case ZFS_DELEG_GROUP_SETS:
4710 case ZFS_DELEG_EVERYONE:
4711 case ZFS_DELEG_EVERYONE_SETS:
4712 avl_pool = fspset->fsps_who_perm_avl_pool;
4713 avl = fsperm->fsp_uge_avl;
4717 assert(!"unhandled zfs_deleg_who_type_t");
4721 who_perm_node_t *found_node = NULL;
4722 who_perm_node_t *node = safe_malloc(
4723 sizeof (who_perm_node_t));
4724 who_perm = &node->who_perm;
4725 uu_avl_index_t idx = 0;
4727 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4728 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4730 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4732 if (avl == fsperm->fsp_uge_avl) {
4734 struct passwd *p = NULL;
4735 struct group *g = NULL;
4736 const char *nice_name = NULL;
4738 switch (perm_type) {
4739 case ZFS_DELEG_USER_SETS:
4740 case ZFS_DELEG_USER:
4741 rid = atoi(perm_name);
4744 nice_name = p->pw_name;
4746 case ZFS_DELEG_GROUP_SETS:
4747 case ZFS_DELEG_GROUP:
4748 rid = atoi(perm_name);
4751 nice_name = g->gr_name;
4758 if (nice_name != NULL)
4760 node->who_perm.who_ug_name,
4764 uu_avl_insert(avl, node, idx);
4767 who_perm = &node->who_perm;
4770 VERIFY3P(who_perm, !=, NULL);
4771 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4778 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4780 nvpair_t *nvp = NULL;
4781 uu_avl_index_t idx = 0;
4783 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4784 nvlist_t *nvl2 = NULL;
4785 const char *fsname = nvpair_name(nvp);
4786 data_type_t type = nvpair_type(nvp);
4787 fs_perm_t *fsperm = NULL;
4788 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4792 fsperm = &node->fspn_fsperm;
4794 VERIFY(DATA_TYPE_NVLIST == type);
4796 uu_list_node_init(node, &node->fspn_list_node,
4797 fspset->fsps_list_pool);
4799 idx = uu_list_numnodes(fspset->fsps_list);
4800 fs_perm_init(fsperm, fspset, fsname);
4802 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4805 (void) parse_fs_perm(fsperm, nvl2);
4807 uu_list_insert(fspset->fsps_list, node, idx);
4813 static inline const char *
4814 deleg_perm_comment(zfs_deleg_note_t note)
4816 const char *str = "";
4821 case ZFS_DELEG_NOTE_ALLOW:
4822 str = gettext("Must also have the permission that is being"
4823 "\n\t\t\t\tallowed");
4825 case ZFS_DELEG_NOTE_CLONE:
4826 str = gettext("Must also have the 'create' ability and 'mount'"
4827 "\n\t\t\t\tability in the origin file system");
4829 case ZFS_DELEG_NOTE_CREATE:
4830 str = gettext("Must also have the 'mount' ability");
4832 case ZFS_DELEG_NOTE_DESTROY:
4833 str = gettext("Must also have the 'mount' ability");
4835 case ZFS_DELEG_NOTE_DIFF:
4836 str = gettext("Allows lookup of paths within a dataset;"
4837 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4838 "\n\t\t\t\tin order to use zfs diff");
4840 case ZFS_DELEG_NOTE_HOLD:
4841 str = gettext("Allows adding a user hold to a snapshot");
4843 case ZFS_DELEG_NOTE_MOUNT:
4844 str = gettext("Allows mount/umount of ZFS datasets");
4846 case ZFS_DELEG_NOTE_PROMOTE:
4847 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4848 " 'promote' ability in the origin file system");
4850 case ZFS_DELEG_NOTE_RECEIVE:
4851 str = gettext("Must also have the 'mount' and 'create'"
4854 case ZFS_DELEG_NOTE_RELEASE:
4855 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4856 "might destroy the snapshot");
4858 case ZFS_DELEG_NOTE_RENAME:
4859 str = gettext("Must also have the 'mount' and 'create'"
4860 "\n\t\t\t\tability in the new parent");
4862 case ZFS_DELEG_NOTE_ROLLBACK:
4865 case ZFS_DELEG_NOTE_SEND:
4868 case ZFS_DELEG_NOTE_SHARE:
4869 str = gettext("Allows sharing file systems over NFS or SMB"
4870 "\n\t\t\t\tprotocols");
4872 case ZFS_DELEG_NOTE_SNAPSHOT:
4875 case ZFS_DELEG_NOTE_LOAD_KEY:
4876 str = gettext("Allows loading or unloading an encryption key");
4878 case ZFS_DELEG_NOTE_CHANGE_KEY:
4879 str = gettext("Allows changing or adding an encryption key");
4882 * case ZFS_DELEG_NOTE_VSCAN:
4883 * str = gettext("");
4887 case ZFS_DELEG_NOTE_GROUPQUOTA:
4888 str = gettext("Allows accessing any groupquota@... property");
4890 case ZFS_DELEG_NOTE_GROUPUSED:
4891 str = gettext("Allows reading any groupused@... property");
4893 case ZFS_DELEG_NOTE_USERPROP:
4894 str = gettext("Allows changing any user property");
4896 case ZFS_DELEG_NOTE_USERQUOTA:
4897 str = gettext("Allows accessing any userquota@... property");
4899 case ZFS_DELEG_NOTE_USERUSED:
4900 str = gettext("Allows reading any userused@... property");
4902 case ZFS_DELEG_NOTE_USEROBJQUOTA:
4903 str = gettext("Allows accessing any userobjquota@... property");
4905 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
4906 str = gettext("Allows accessing any \n\t\t\t\t"
4907 "groupobjquota@... property");
4909 case ZFS_DELEG_NOTE_GROUPOBJUSED:
4910 str = gettext("Allows reading any groupobjused@... property");
4912 case ZFS_DELEG_NOTE_USEROBJUSED:
4913 str = gettext("Allows reading any userobjused@... property");
4931 boolean_t recursive; /* unallow only */
4932 boolean_t prt_usage;
4934 boolean_t prt_perms;
4937 const char *dataset;
4941 prop_cmp(const void *a, const void *b)
4943 const char *str1 = *(const char **)a;
4944 const char *str2 = *(const char **)b;
4945 return (strcmp(str1, str2));
4949 allow_usage(boolean_t un, boolean_t requested, const char *msg)
4951 const char *opt_desc[] = {
4952 "-h", gettext("show this help message and exit"),
4953 "-l", gettext("set permission locally"),
4954 "-d", gettext("set permission for descents"),
4955 "-u", gettext("set permission for user"),
4956 "-g", gettext("set permission for group"),
4957 "-e", gettext("set permission for everyone"),
4958 "-c", gettext("set create time permission"),
4959 "-s", gettext("define permission set"),
4961 "-r", gettext("remove permissions recursively"),
4963 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
4964 size_t allow_size = unallow_size - 2;
4965 const char *props[ZFS_NUM_PROPS];
4968 FILE *fp = requested ? stdout : stderr;
4969 zprop_desc_t *pdtbl = zfs_prop_get_table();
4970 const char *fmt = gettext("%-16s %-14s\t%s\n");
4972 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
4974 (void) fprintf(fp, gettext("Options:\n"));
4975 for (i = 0; i < (un ? unallow_size : allow_size); i += 2) {
4976 const char *opt = opt_desc[i];
4977 const char *optdsc = opt_desc[i + 1];
4978 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
4981 (void) fprintf(fp, gettext("\nThe following permissions are "
4983 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
4985 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
4986 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
4987 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
4988 const char *perm_type = deleg_perm_type(perm_note);
4989 const char *perm_comment = deleg_perm_comment(perm_note);
4990 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
4993 for (i = 0; i < ZFS_NUM_PROPS; i++) {
4994 zprop_desc_t *pd = &pdtbl[i];
4995 if (pd->pd_visible != B_TRUE)
4998 if (pd->pd_attr == PROP_READONLY)
5001 props[count++] = pd->pd_name;
5003 props[count] = NULL;
5005 qsort(props, count, sizeof (char *), prop_cmp);
5007 for (i = 0; i < count; i++)
5008 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
5011 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
5013 exit(requested ? 0 : 2);
5016 static inline const char *
5017 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
5020 if (un && argc == expected_argc - 1)
5022 else if (argc == expected_argc)
5023 *permsp = argv[argc - 2];
5025 allow_usage(un, B_FALSE,
5026 gettext("wrong number of parameters\n"));
5028 return (argv[argc - 1]);
5032 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
5034 int uge_sum = opts->user + opts->group + opts->everyone;
5035 int csuge_sum = opts->create + opts->set + uge_sum;
5036 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
5037 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
5040 allow_usage(un, B_FALSE,
5041 gettext("-u, -g, and -e are mutually exclusive\n"));
5043 if (opts->prt_usage) {
5044 if (argc == 0 && all_sum == 0)
5045 allow_usage(un, B_TRUE, NULL);
5052 allow_usage(un, B_FALSE,
5053 gettext("invalid options combined with -s\n"));
5055 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5056 if (argv[0][0] != '@')
5057 allow_usage(un, B_FALSE,
5058 gettext("invalid set name: missing '@' prefix\n"));
5059 opts->who = argv[0];
5060 } else if (opts->create) {
5061 if (ldcsuge_sum > 1)
5062 allow_usage(un, B_FALSE,
5063 gettext("invalid options combined with -c\n"));
5064 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5065 } else if (opts->everyone) {
5067 allow_usage(un, B_FALSE,
5068 gettext("invalid options combined with -e\n"));
5069 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5070 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
5072 opts->everyone = B_TRUE;
5075 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5076 } else if (argc == 1 && !un) {
5077 opts->prt_perms = B_TRUE;
5078 opts->dataset = argv[argc-1];
5080 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5081 opts->who = argv[0];
5084 if (!opts->local && !opts->descend) {
5085 opts->local = B_TRUE;
5086 opts->descend = B_TRUE;
5091 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
5092 const char *who, char *perms, nvlist_t *top_nvl)
5095 char ld[2] = { '\0', '\0' };
5096 char who_buf[MAXNAMELEN + 32];
5097 char base_type = '\0';
5098 char set_type = '\0';
5099 nvlist_t *base_nvl = NULL;
5100 nvlist_t *set_nvl = NULL;
5103 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
5105 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
5109 case ZFS_DELEG_NAMED_SET_SETS:
5110 case ZFS_DELEG_NAMED_SET:
5111 set_type = ZFS_DELEG_NAMED_SET_SETS;
5112 base_type = ZFS_DELEG_NAMED_SET;
5113 ld[0] = ZFS_DELEG_NA;
5115 case ZFS_DELEG_CREATE_SETS:
5116 case ZFS_DELEG_CREATE:
5117 set_type = ZFS_DELEG_CREATE_SETS;
5118 base_type = ZFS_DELEG_CREATE;
5119 ld[0] = ZFS_DELEG_NA;
5121 case ZFS_DELEG_USER_SETS:
5122 case ZFS_DELEG_USER:
5123 set_type = ZFS_DELEG_USER_SETS;
5124 base_type = ZFS_DELEG_USER;
5126 ld[0] = ZFS_DELEG_LOCAL;
5128 ld[1] = ZFS_DELEG_DESCENDENT;
5130 case ZFS_DELEG_GROUP_SETS:
5131 case ZFS_DELEG_GROUP:
5132 set_type = ZFS_DELEG_GROUP_SETS;
5133 base_type = ZFS_DELEG_GROUP;
5135 ld[0] = ZFS_DELEG_LOCAL;
5137 ld[1] = ZFS_DELEG_DESCENDENT;
5139 case ZFS_DELEG_EVERYONE_SETS:
5140 case ZFS_DELEG_EVERYONE:
5141 set_type = ZFS_DELEG_EVERYONE_SETS;
5142 base_type = ZFS_DELEG_EVERYONE;
5144 ld[0] = ZFS_DELEG_LOCAL;
5146 ld[1] = ZFS_DELEG_DESCENDENT;
5150 assert(set_type != '\0' && base_type != '\0');
5153 if (perms != NULL) {
5155 char *end = curr + strlen(perms);
5157 while (curr < end) {
5158 char *delim = strchr(curr, ',');
5169 (void) nvlist_add_boolean(nvl, curr);
5175 for (i = 0; i < 2; i++) {
5176 char locality = ld[i];
5180 if (!nvlist_empty(base_nvl)) {
5182 (void) snprintf(who_buf,
5183 sizeof (who_buf), "%c%c$%s",
5184 base_type, locality, who);
5186 (void) snprintf(who_buf,
5187 sizeof (who_buf), "%c%c$",
5188 base_type, locality);
5190 (void) nvlist_add_nvlist(top_nvl, who_buf,
5195 if (!nvlist_empty(set_nvl)) {
5197 (void) snprintf(who_buf,
5198 sizeof (who_buf), "%c%c$%s",
5199 set_type, locality, who);
5201 (void) snprintf(who_buf,
5202 sizeof (who_buf), "%c%c$",
5203 set_type, locality);
5205 (void) nvlist_add_nvlist(top_nvl, who_buf,
5210 for (i = 0; i < 2; i++) {
5211 char locality = ld[i];
5216 (void) snprintf(who_buf, sizeof (who_buf),
5217 "%c%c$%s", base_type, locality, who);
5219 (void) snprintf(who_buf, sizeof (who_buf),
5220 "%c%c$", base_type, locality);
5221 (void) nvlist_add_boolean(top_nvl, who_buf);
5224 (void) snprintf(who_buf, sizeof (who_buf),
5225 "%c%c$%s", set_type, locality, who);
5227 (void) snprintf(who_buf, sizeof (who_buf),
5228 "%c%c$", set_type, locality);
5229 (void) nvlist_add_boolean(top_nvl, who_buf);
5235 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5237 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5241 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5242 opts->descend, opts->who, opts->perms, *nvlp);
5243 } else if (opts->create) {
5244 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5245 opts->descend, NULL, opts->perms, *nvlp);
5246 } else if (opts->everyone) {
5247 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5248 opts->descend, NULL, opts->perms, *nvlp);
5250 char *curr = opts->who;
5251 char *end = curr + strlen(curr);
5253 while (curr < end) {
5255 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5257 char *delim = strchr(curr, ',');
5260 struct passwd *p = NULL;
5261 struct group *g = NULL;
5269 rid = (uid_t)strtol(curr, &endch, 0);
5271 who_type = ZFS_DELEG_USER;
5280 (void) snprintf(errbuf, 256, gettext(
5281 "invalid user %s"), curr);
5282 allow_usage(un, B_TRUE, errbuf);
5284 } else if (opts->group) {
5285 who_type = ZFS_DELEG_GROUP;
5294 (void) snprintf(errbuf, 256, gettext(
5295 "invalid group %s"), curr);
5296 allow_usage(un, B_TRUE, errbuf);
5299 if (*endch != '\0') {
5306 if (*endch != '\0') {
5314 who_type = ZFS_DELEG_USER;
5316 } else if (g != NULL) {
5317 who_type = ZFS_DELEG_GROUP;
5320 (void) snprintf(errbuf, 256, gettext(
5321 "invalid user/group %s"), curr);
5322 allow_usage(un, B_TRUE, errbuf);
5326 (void) sprintf(id, "%u", rid);
5329 store_allow_perm(who_type, opts->local,
5330 opts->descend, who, opts->perms, *nvlp);
5339 print_set_creat_perms(uu_avl_t *who_avl)
5341 const char *sc_title[] = {
5342 gettext("Permission sets:\n"),
5343 gettext("Create time permissions:\n"),
5346 const char **title_ptr = sc_title;
5347 who_perm_node_t *who_node = NULL;
5348 int prev_weight = -1;
5350 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5351 who_node = uu_avl_next(who_avl, who_node)) {
5352 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5353 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5354 const char *who_name = who_node->who_perm.who_name;
5355 int weight = who_type2weight(who_type);
5356 boolean_t first = B_TRUE;
5357 deleg_perm_node_t *deleg_node;
5359 if (prev_weight != weight) {
5360 (void) printf("%s", *title_ptr++);
5361 prev_weight = weight;
5364 if (who_name == NULL || strnlen(who_name, 1) == 0)
5365 (void) printf("\t");
5367 (void) printf("\t%s ", who_name);
5369 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5370 deleg_node = uu_avl_next(avl, deleg_node)) {
5373 deleg_node->dpn_perm.dp_name);
5376 (void) printf(",%s",
5377 deleg_node->dpn_perm.dp_name);
5380 (void) printf("\n");
5385 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5388 who_perm_node_t *who_node = NULL;
5389 boolean_t prt_title = B_TRUE;
5390 uu_avl_walk_t *walk;
5392 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5395 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5396 const char *who_name = who_node->who_perm.who_name;
5397 const char *nice_who_name = who_node->who_perm.who_ug_name;
5398 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5399 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5401 deleg_perm_node_t *deleg_node;
5402 boolean_t prt_who = B_TRUE;
5404 for (deleg_node = uu_avl_first(avl);
5406 deleg_node = uu_avl_next(avl, deleg_node)) {
5407 if (local != deleg_node->dpn_perm.dp_local ||
5408 descend != deleg_node->dpn_perm.dp_descend)
5412 const char *who = NULL;
5414 prt_title = B_FALSE;
5415 (void) printf("%s", title);
5419 case ZFS_DELEG_USER_SETS:
5420 case ZFS_DELEG_USER:
5421 who = gettext("user");
5423 who_name = nice_who_name;
5425 case ZFS_DELEG_GROUP_SETS:
5426 case ZFS_DELEG_GROUP:
5427 who = gettext("group");
5429 who_name = nice_who_name;
5431 case ZFS_DELEG_EVERYONE_SETS:
5432 case ZFS_DELEG_EVERYONE:
5433 who = gettext("everyone");
5438 assert(who != NULL);
5442 if (who_name == NULL)
5443 (void) printf("\t%s", who);
5445 (void) printf("\t%s %s", who, who_name);
5448 (void) printf("%c%s", delim,
5449 deleg_node->dpn_perm.dp_name);
5454 (void) printf("\n");
5457 uu_avl_walk_end(walk);
5461 print_fs_perms(fs_perm_set_t *fspset)
5463 fs_perm_node_t *node = NULL;
5464 char buf[MAXNAMELEN + 32];
5465 const char *dsname = buf;
5467 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5468 node = uu_list_next(fspset->fsps_list, node)) {
5469 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5470 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5473 (void) snprintf(buf, sizeof (buf),
5474 gettext("---- Permissions on %s "),
5475 node->fspn_fsperm.fsp_name);
5476 (void) printf("%s", dsname);
5477 left = 70 - strlen(buf);
5480 (void) printf("\n");
5482 print_set_creat_perms(sc_avl);
5483 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5484 gettext("Local permissions:\n"));
5485 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5486 gettext("Descendent permissions:\n"));
5487 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5488 gettext("Local+Descendent permissions:\n"));
5492 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5494 struct deleg_perms {
5500 set_deleg_perms(zfs_handle_t *zhp, void *data)
5502 struct deleg_perms *perms = (struct deleg_perms *)data;
5503 zfs_type_t zfs_type = zfs_get_type(zhp);
5505 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5508 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5512 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5515 nvlist_t *perm_nvl = NULL;
5516 nvlist_t *update_perm_nvl = NULL;
5519 struct allow_opts opts = { 0 };
5521 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5524 while ((c = getopt(argc, argv, optstr)) != -1) {
5527 opts.local = B_TRUE;
5530 opts.descend = B_TRUE;
5536 opts.group = B_TRUE;
5539 opts.everyone = B_TRUE;
5545 opts.create = B_TRUE;
5548 opts.recursive = B_TRUE;
5551 (void) fprintf(stderr, gettext("missing argument for "
5552 "'%c' option\n"), optopt);
5556 opts.prt_usage = B_TRUE;
5559 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5568 /* check arguments */
5569 parse_allow_args(argc, argv, un, &opts);
5571 /* try to open the dataset */
5572 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5573 ZFS_TYPE_VOLUME)) == NULL) {
5574 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5579 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5582 fs_perm_set_init(&fs_perm_set);
5583 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5584 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5589 print_fs_perms(&fs_perm_set);
5591 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5592 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5595 if (un && opts.recursive) {
5596 struct deleg_perms data = { un, update_perm_nvl };
5597 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5606 nvlist_free(perm_nvl);
5607 nvlist_free(update_perm_nvl);
5609 fs_perm_set_fini(&fs_perm_set);
5617 zfs_do_allow(int argc, char **argv)
5619 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5623 zfs_do_unallow(int argc, char **argv)
5625 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5629 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5634 boolean_t recursive = B_FALSE;
5635 const char *opts = holding ? "rt" : "r";
5639 while ((c = getopt(argc, argv, opts)) != -1) {
5645 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5654 /* check number of arguments */
5662 if (holding && tag[0] == '.') {
5663 /* tags starting with '.' are reserved for libzfs */
5664 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5668 for (i = 0; i < argc; ++i) {
5670 char parent[ZFS_MAX_DATASET_NAME_LEN];
5672 char *path = argv[i];
5674 delim = strchr(path, '@');
5675 if (delim == NULL) {
5676 (void) fprintf(stderr,
5677 gettext("'%s' is not a snapshot\n"), path);
5681 (void) strncpy(parent, path, delim - path);
5682 parent[delim - path] = '\0';
5684 zhp = zfs_open(g_zfs, parent,
5685 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5691 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5694 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5700 return (errors != 0);
5704 * zfs hold [-r] [-t] <tag> <snap> ...
5706 * -r Recursively hold
5708 * Apply a user-hold with the given tag to the list of snapshots.
5711 zfs_do_hold(int argc, char **argv)
5713 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5717 * zfs release [-r] <tag> <snap> ...
5719 * -r Recursively release
5721 * Release a user-hold with the given tag from the list of snapshots.
5724 zfs_do_release(int argc, char **argv)
5726 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5729 typedef struct holds_cbdata {
5730 boolean_t cb_recursive;
5731 const char *cb_snapname;
5733 size_t cb_max_namelen;
5734 size_t cb_max_taglen;
5737 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5738 #define DATETIME_BUF_LEN (32)
5743 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
5746 nvpair_t *nvp = NULL;
5747 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5751 for (i = 0; i < 3; i++) {
5752 col = gettext(hdr_cols[i]);
5754 (void) printf("%-*s ", i ? tagwidth : nwidth,
5757 (void) printf("%s\n", col);
5761 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5762 char *zname = nvpair_name(nvp);
5764 nvpair_t *nvp2 = NULL;
5765 (void) nvpair_value_nvlist(nvp, &nvl2);
5766 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5767 char tsbuf[DATETIME_BUF_LEN];
5768 char *tagname = nvpair_name(nvp2);
5773 (void) nvpair_value_uint64(nvp2, &val);
5775 (void) localtime_r(&time, &t);
5776 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5777 gettext(STRFTIME_FMT_STR), &t);
5780 (void) printf("%s\t%s\t%s\n", zname,
5783 (void) printf("%-*s %-*s %s\n", nwidth,
5784 zname, tagwidth, tagname, tsbuf);
5791 * Generic callback function to list a dataset or snapshot.
5794 holds_callback(zfs_handle_t *zhp, void *data)
5796 holds_cbdata_t *cbp = data;
5797 nvlist_t *top_nvl = *cbp->cb_nvlp;
5798 nvlist_t *nvl = NULL;
5799 nvpair_t *nvp = NULL;
5800 const char *zname = zfs_get_name(zhp);
5801 size_t znamelen = strlen(zname);
5803 if (cbp->cb_recursive) {
5804 const char *snapname;
5805 char *delim = strchr(zname, '@');
5809 snapname = delim + 1;
5810 if (strcmp(cbp->cb_snapname, snapname))
5814 if (zfs_get_holds(zhp, &nvl) != 0)
5817 if (znamelen > cbp->cb_max_namelen)
5818 cbp->cb_max_namelen = znamelen;
5820 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5821 const char *tag = nvpair_name(nvp);
5822 size_t taglen = strlen(tag);
5823 if (taglen > cbp->cb_max_taglen)
5824 cbp->cb_max_taglen = taglen;
5827 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5831 * zfs holds [-r] <snap> ...
5833 * -r Recursively hold
5836 zfs_do_holds(int argc, char **argv)
5841 boolean_t scripted = B_FALSE;
5842 boolean_t recursive = B_FALSE;
5843 const char *opts = "rH";
5846 int types = ZFS_TYPE_SNAPSHOT;
5847 holds_cbdata_t cb = { 0 };
5854 while ((c = getopt(argc, argv, opts)) != -1) {
5863 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5870 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5871 flags |= ZFS_ITER_RECURSE;
5877 /* check number of arguments */
5881 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5884 for (i = 0; i < argc; ++i) {
5885 char *snapshot = argv[i];
5887 const char *snapname;
5889 delim = strchr(snapshot, '@');
5890 if (delim == NULL) {
5891 (void) fprintf(stderr,
5892 gettext("'%s' is not a snapshot\n"), snapshot);
5896 snapname = delim + 1;
5898 snapshot[delim - snapshot] = '\0';
5900 cb.cb_recursive = recursive;
5901 cb.cb_snapname = snapname;
5905 * 1. collect holds data, set format options
5907 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
5908 holds_callback, &cb);
5914 * 2. print holds data
5916 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
5918 if (nvlist_empty(nvl))
5919 (void) fprintf(stderr, gettext("no datasets available\n"));
5923 return (0 != errors);
5926 #define CHECK_SPINNER 30
5927 #define SPINNER_TIME 3 /* seconds */
5928 #define MOUNT_TIME 5 /* seconds */
5931 get_one_dataset(zfs_handle_t *zhp, void *data)
5933 static char *spin[] = { "-", "\\", "|", "/" };
5934 static int spinval = 0;
5935 static int spincheck = 0;
5936 static time_t last_spin_time = (time_t)0;
5937 get_all_cb_t *cbp = data;
5938 zfs_type_t type = zfs_get_type(zhp);
5940 if (cbp->cb_verbose) {
5941 if (--spincheck < 0) {
5942 time_t now = time(NULL);
5943 if (last_spin_time + SPINNER_TIME < now) {
5944 update_progress(spin[spinval++ % 4]);
5945 last_spin_time = now;
5947 spincheck = CHECK_SPINNER;
5952 * Iterate over any nested datasets.
5954 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
5960 * Skip any datasets whose type does not match.
5962 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
5966 libzfs_add_handle(cbp, zhp);
5967 assert(cbp->cb_used <= cbp->cb_alloc);
5973 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
5975 get_all_cb_t cb = { 0 };
5976 cb.cb_verbose = verbose;
5977 cb.cb_getone = get_one_dataset;
5980 set_progress_header(gettext("Reading ZFS config"));
5981 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
5983 *dslist = cb.cb_handles;
5984 *count = cb.cb_used;
5987 finish_progress(gettext("done."));
5991 * Generic callback for sharing or mounting filesystems. Because the code is so
5992 * similar, we have a common function with an extra parameter to determine which
5993 * mode we are using.
5995 #define OP_SHARE 0x1
5996 #define OP_MOUNT 0x2
5999 * Share or mount a dataset.
6002 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
6003 boolean_t explicit, const char *options)
6005 char mountpoint[ZFS_MAXPROPLEN];
6006 char shareopts[ZFS_MAXPROPLEN];
6007 char smbshareopts[ZFS_MAXPROPLEN];
6008 const char *cmdname = op == OP_SHARE ? "share" : "mount";
6010 uint64_t zoned, canmount;
6011 boolean_t shared_nfs, shared_smb;
6013 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
6016 * Check to make sure we can mount/share this dataset. If we
6017 * are in the global zone and the filesystem is exported to a
6018 * local zone, or if we are in a local zone and the
6019 * filesystem is not exported, then it is an error.
6021 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
6023 if (zoned && getzoneid() == GLOBAL_ZONEID) {
6027 (void) fprintf(stderr, gettext("cannot %s '%s': "
6028 "dataset is exported to a local zone\n"), cmdname,
6032 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
6036 (void) fprintf(stderr, gettext("cannot %s '%s': "
6037 "permission denied\n"), cmdname,
6043 * Ignore any filesystems which don't apply to us. This
6044 * includes those with a legacy mountpoint, or those with
6045 * legacy share options.
6047 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6048 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
6049 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
6050 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
6051 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
6052 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
6054 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
6055 strcmp(smbshareopts, "off") == 0) {
6059 (void) fprintf(stderr, gettext("cannot share '%s': "
6060 "legacy share\n"), zfs_get_name(zhp));
6061 (void) fprintf(stderr, gettext("use share(1M) to "
6062 "share this filesystem, or set "
6063 "sharenfs property on\n"));
6068 * We cannot share or mount legacy filesystems. If the
6069 * shareopts is non-legacy but the mountpoint is legacy, we
6070 * treat it as a legacy share.
6072 if (strcmp(mountpoint, "legacy") == 0) {
6076 (void) fprintf(stderr, gettext("cannot %s '%s': "
6077 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
6078 (void) fprintf(stderr, gettext("use %s(1M) to "
6079 "%s this filesystem\n"), cmdname, cmdname);
6083 if (strcmp(mountpoint, "none") == 0) {
6087 (void) fprintf(stderr, gettext("cannot %s '%s': no "
6088 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
6093 * canmount explicit outcome
6094 * on no pass through
6095 * on yes pass through
6097 * off yes display error, return 1
6098 * noauto no return 0
6099 * noauto yes pass through
6101 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
6102 if (canmount == ZFS_CANMOUNT_OFF) {
6106 (void) fprintf(stderr, gettext("cannot %s '%s': "
6107 "'canmount' property is set to 'off'\n"), cmdname,
6110 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
6115 * If this filesystem is inconsistent and has a receive resume
6116 * token, we can not mount it.
6118 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
6119 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
6120 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
6124 (void) fprintf(stderr, gettext("cannot %s '%s': "
6125 "Contains partially-completed state from "
6126 "\"zfs receive -r\", which can be resumed with "
6127 "\"zfs send -t\"\n"),
6128 cmdname, zfs_get_name(zhp));
6133 * At this point, we have verified that the mountpoint and/or
6134 * shareopts are appropriate for auto management. If the
6135 * filesystem is already mounted or shared, return (failing
6136 * for explicit requests); otherwise mount or share the
6142 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
6143 shared_smb = zfs_is_shared_smb(zhp, NULL);
6145 if ((shared_nfs && shared_smb) ||
6146 (shared_nfs && strcmp(shareopts, "on") == 0 &&
6147 strcmp(smbshareopts, "off") == 0) ||
6148 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
6149 strcmp(shareopts, "off") == 0)) {
6153 (void) fprintf(stderr, gettext("cannot share "
6154 "'%s': filesystem already shared\n"),
6159 if (!zfs_is_mounted(zhp, NULL) &&
6160 zfs_mount(zhp, NULL, flags) != 0)
6163 if (protocol == NULL) {
6164 if (zfs_shareall(zhp) != 0)
6166 } else if (strcmp(protocol, "nfs") == 0) {
6167 if (zfs_share_nfs(zhp))
6169 } else if (strcmp(protocol, "smb") == 0) {
6170 if (zfs_share_smb(zhp))
6173 (void) fprintf(stderr, gettext("cannot share "
6174 "'%s': invalid share type '%s' "
6176 zfs_get_name(zhp), protocol);
6183 if (options == NULL)
6184 mnt.mnt_mntopts = "";
6186 mnt.mnt_mntopts = (char *)options;
6188 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6189 zfs_is_mounted(zhp, NULL)) {
6193 (void) fprintf(stderr, gettext("cannot mount "
6194 "'%s': filesystem already mounted\n"),
6199 if (zfs_mount(zhp, options, flags) != 0)
6208 * Reports progress in the form "(current/total)". Not thread-safe.
6211 report_mount_progress(int current, int total)
6213 static time_t last_progress_time = 0;
6214 time_t now = time(NULL);
6217 /* report 1..n instead of 0..n-1 */
6220 /* display header if we're here for the first time */
6222 set_progress_header(gettext("Mounting ZFS filesystems"));
6223 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6224 /* too soon to report again */
6228 last_progress_time = now;
6230 (void) sprintf(info, "(%d/%d)", current, total);
6232 if (current == total)
6233 finish_progress(info);
6235 update_progress(info);
6239 append_options(char *mntopts, char *newopts)
6241 int len = strlen(mntopts);
6243 /* original length plus new string to append plus 1 for the comma */
6244 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6245 (void) fprintf(stderr, gettext("the opts argument for "
6246 "'%s' option is too long (more than %d chars)\n"),
6247 "-o", MNT_LINE_MAX);
6252 mntopts[len++] = ',';
6254 (void) strcpy(&mntopts[len], newopts);
6258 share_mount(int op, int argc, char **argv)
6261 boolean_t verbose = B_FALSE;
6263 char *options = NULL;
6267 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":alvo:O" : "al"))
6280 if (*optarg == '\0') {
6281 (void) fprintf(stderr, gettext("empty mount "
6282 "options (-o) specified\n"));
6286 if (options == NULL)
6287 options = safe_malloc(MNT_LINE_MAX + 1);
6289 /* option validation is done later */
6290 append_options(options, optarg);
6293 flags |= MS_OVERLAY;
6296 (void) fprintf(stderr, gettext("missing argument for "
6297 "'%c' option\n"), optopt);
6301 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6310 /* check number of arguments */
6312 zfs_handle_t **dslist = NULL;
6313 size_t i, count = 0;
6314 char *protocol = NULL;
6316 if (op == OP_SHARE && argc > 0) {
6317 if (strcmp(argv[0], "nfs") != 0 &&
6318 strcmp(argv[0], "smb") != 0) {
6319 (void) fprintf(stderr, gettext("share type "
6320 "must be 'nfs' or 'smb'\n"));
6329 (void) fprintf(stderr, gettext("too many arguments\n"));
6333 start_progress_timer();
6334 get_all_datasets(&dslist, &count, verbose);
6337 if (options != NULL)
6342 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6344 for (i = 0; i < count; i++) {
6346 report_mount_progress(i, count);
6348 if (share_mount_one(dslist[i], op, flags, protocol,
6349 B_FALSE, options) != 0)
6351 zfs_close(dslist[i]);
6355 } else if (argc == 0) {
6356 struct mnttab entry;
6358 if ((op == OP_SHARE) || (options != NULL)) {
6359 (void) fprintf(stderr, gettext("missing filesystem "
6360 "argument (specify -a for all)\n"));
6365 * When mount is given no arguments, go through
6366 * /proc/self/mounts and display any active ZFS mounts.
6367 * We hide any snapshots, since they are controlled
6371 /* Reopen MNTTAB to prevent reading stale data from open file */
6372 if (freopen(MNTTAB, "r", mnttab_file) == NULL) {
6373 if (options != NULL)
6378 while (getmntent(mnttab_file, &entry) == 0) {
6379 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6380 strchr(entry.mnt_special, '@') != NULL)
6383 (void) printf("%-30s %s\n", entry.mnt_special,
6391 (void) fprintf(stderr,
6392 gettext("too many arguments\n"));
6396 if ((zhp = zfs_open(g_zfs, argv[0],
6397 ZFS_TYPE_FILESYSTEM)) == NULL) {
6400 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6406 if (options != NULL)
6413 * zfs mount -a [nfs]
6414 * zfs mount filesystem
6416 * Mount all filesystems, or mount the given filesystem.
6419 zfs_do_mount(int argc, char **argv)
6421 return (share_mount(OP_MOUNT, argc, argv));
6425 * zfs share -a [nfs | smb]
6426 * zfs share filesystem
6428 * Share all filesystems, or share the given filesystem.
6431 zfs_do_share(int argc, char **argv)
6433 return (share_mount(OP_SHARE, argc, argv));
6436 typedef struct unshare_unmount_node {
6437 zfs_handle_t *un_zhp;
6439 uu_avl_node_t un_avlnode;
6440 } unshare_unmount_node_t;
6444 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6446 const unshare_unmount_node_t *l = larg;
6447 const unshare_unmount_node_t *r = rarg;
6449 return (strcmp(l->un_mountp, r->un_mountp));
6453 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6454 * absolute path, find the entry /proc/self/mounts, verify that its a
6455 * ZFS filesystems, and unmount it appropriately.
6458 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6462 struct stat64 statbuf;
6463 struct extmnttab entry;
6464 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6468 * Search for the path in /proc/self/mounts. Rather than looking for the
6469 * specific path, which can be fooled by non-standard paths (i.e. ".."
6470 * or "//"), we stat() the path and search for the corresponding
6471 * (major,minor) device pair.
6473 if (stat64(path, &statbuf) != 0) {
6474 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6475 cmdname, path, strerror(errno));
6478 path_inode = statbuf.st_ino;
6481 * Search for the given (major,minor) pair in the mount table.
6484 /* Reopen MNTTAB to prevent reading stale data from open file */
6485 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6488 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6489 if (entry.mnt_major == major(statbuf.st_dev) &&
6490 entry.mnt_minor == minor(statbuf.st_dev))
6494 if (op == OP_SHARE) {
6495 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6496 "currently mounted\n"), cmdname, path);
6499 (void) fprintf(stderr, gettext("warning: %s not in"
6500 "/proc/self/mounts\n"), path);
6501 if ((ret = umount2(path, flags)) != 0)
6502 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6507 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6508 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6509 "filesystem\n"), cmdname, path);
6513 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6514 ZFS_TYPE_FILESYSTEM)) == NULL)
6518 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6519 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6520 cmdname, path, strerror(errno));
6522 } else if (statbuf.st_ino != path_inode) {
6523 (void) fprintf(stderr, gettext("cannot "
6524 "%s '%s': not a mountpoint\n"), cmdname, path);
6528 if (op == OP_SHARE) {
6529 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6530 char smbshare_prop[ZFS_MAXPROPLEN];
6532 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6533 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6534 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6535 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6537 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6538 strcmp(smbshare_prop, "off") == 0) {
6539 (void) fprintf(stderr, gettext("cannot unshare "
6540 "'%s': legacy share\n"), path);
6541 (void) fprintf(stderr, gettext("use exportfs(8) "
6542 "or smbcontrol(1) to unshare this filesystem\n"));
6543 } else if (!zfs_is_shared(zhp)) {
6544 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6545 "not currently shared\n"), path);
6547 ret = zfs_unshareall_bypath(zhp, path);
6550 char mtpt_prop[ZFS_MAXPROPLEN];
6552 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6553 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6556 ret = zfs_unmount(zhp, NULL, flags);
6557 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6558 (void) fprintf(stderr, gettext("cannot unmount "
6559 "'%s': legacy mountpoint\n"),
6561 (void) fprintf(stderr, gettext("use umount(8) "
6562 "to unmount this filesystem\n"));
6564 ret = zfs_unmountall(zhp, flags);
6575 * Generic callback for unsharing or unmounting a filesystem.
6578 unshare_unmount(int op, int argc, char **argv)
6585 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6586 char sharesmb[ZFS_MAXPROPLEN];
6589 while ((c = getopt(argc, argv, op == OP_SHARE ? ":a" : "af")) != -1) {
6598 (void) fprintf(stderr, gettext("missing argument for "
6599 "'%c' option\n"), optopt);
6603 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6614 * We could make use of zfs_for_each() to walk all datasets in
6615 * the system, but this would be very inefficient, especially
6616 * since we would have to linearly search /proc/self/mounts for
6617 * each one. Instead, do one pass through /proc/self/mounts
6618 * looking for zfs entries and call zfs_unmount() for each one.
6620 * Things get a little tricky if the administrator has created
6621 * mountpoints beneath other ZFS filesystems. In this case, we
6622 * have to unmount the deepest filesystems first. To accomplish
6623 * this, we place all the mountpoints in an AVL tree sorted by
6624 * the special type (dataset name), and walk the result in
6625 * reverse to make sure to get any snapshots first.
6627 struct mnttab entry;
6628 uu_avl_pool_t *pool;
6629 uu_avl_t *tree = NULL;
6630 unshare_unmount_node_t *node;
6632 uu_avl_walk_t *walk;
6633 char *protocol = NULL;
6635 if (op == OP_SHARE && argc > 0) {
6636 if (strcmp(argv[0], "nfs") != 0 &&
6637 strcmp(argv[0], "smb") != 0) {
6638 (void) fprintf(stderr, gettext("share type "
6639 "must be 'nfs' or 'smb'\n"));
6648 (void) fprintf(stderr, gettext("too many arguments\n"));
6652 if (((pool = uu_avl_pool_create("unmount_pool",
6653 sizeof (unshare_unmount_node_t),
6654 offsetof(unshare_unmount_node_t, un_avlnode),
6655 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6656 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6659 /* Reopen MNTTAB to prevent reading stale data from open file */
6660 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6663 while (getmntent(mnttab_file, &entry) == 0) {
6665 /* ignore non-ZFS entries */
6666 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6669 /* ignore snapshots */
6670 if (strchr(entry.mnt_special, '@') != NULL)
6673 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6674 ZFS_TYPE_FILESYSTEM)) == NULL) {
6680 * Ignore datasets that are excluded/restricted by
6683 if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
6690 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6692 sizeof (nfs_mnt_prop),
6693 NULL, NULL, 0, B_FALSE) == 0);
6694 if (strcmp(nfs_mnt_prop, "off") != 0)
6696 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6698 sizeof (nfs_mnt_prop),
6699 NULL, NULL, 0, B_FALSE) == 0);
6700 if (strcmp(nfs_mnt_prop, "off") == 0)
6704 /* Ignore legacy mounts */
6705 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6707 sizeof (nfs_mnt_prop),
6708 NULL, NULL, 0, B_FALSE) == 0);
6709 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6711 /* Ignore canmount=noauto mounts */
6712 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6713 ZFS_CANMOUNT_NOAUTO)
6719 node = safe_malloc(sizeof (unshare_unmount_node_t));
6721 node->un_mountp = safe_strdup(entry.mnt_mountp);
6723 uu_avl_node_init(node, &node->un_avlnode, pool);
6725 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6726 uu_avl_insert(tree, node, idx);
6728 zfs_close(node->un_zhp);
6729 free(node->un_mountp);
6735 * Walk the AVL tree in reverse, unmounting each filesystem and
6736 * removing it from the AVL tree in the process.
6738 if ((walk = uu_avl_walk_start(tree,
6739 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6742 while ((node = uu_avl_walk_next(walk)) != NULL) {
6743 uu_avl_remove(tree, node);
6747 if (zfs_unshareall_bytype(node->un_zhp,
6748 node->un_mountp, protocol) != 0)
6753 if (zfs_unmount(node->un_zhp,
6754 node->un_zhp->zfs_name, flags) != 0)
6759 zfs_close(node->un_zhp);
6760 free(node->un_mountp);
6764 uu_avl_walk_end(walk);
6765 uu_avl_destroy(tree);
6766 uu_avl_pool_destroy(pool);
6771 (void) fprintf(stderr,
6772 gettext("missing filesystem argument\n"));
6774 (void) fprintf(stderr,
6775 gettext("too many arguments\n"));
6780 * We have an argument, but it may be a full path or a ZFS
6781 * filesystem. Pass full paths off to unmount_path() (shared by
6782 * manual_unmount), otherwise open the filesystem and pass to
6785 if (argv[0][0] == '/')
6786 return (unshare_unmount_path(op, argv[0],
6789 if ((zhp = zfs_open(g_zfs, argv[0],
6790 ZFS_TYPE_FILESYSTEM)) == NULL)
6793 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6794 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6795 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6796 NULL, 0, B_FALSE) == 0);
6800 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6802 sizeof (nfs_mnt_prop),
6803 NULL, NULL, 0, B_FALSE) == 0);
6804 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6805 sharesmb, sizeof (sharesmb), NULL, NULL,
6808 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6809 strcmp(sharesmb, "off") == 0) {
6810 (void) fprintf(stderr, gettext("cannot "
6811 "unshare '%s': legacy share\n"),
6813 (void) fprintf(stderr, gettext("use "
6814 "unshare(1M) to unshare this "
6817 } else if (!zfs_is_shared(zhp)) {
6818 (void) fprintf(stderr, gettext("cannot "
6819 "unshare '%s': not currently "
6820 "shared\n"), zfs_get_name(zhp));
6822 } else if (zfs_unshareall(zhp) != 0) {
6828 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6829 (void) fprintf(stderr, gettext("cannot "
6830 "unmount '%s': legacy "
6831 "mountpoint\n"), zfs_get_name(zhp));
6832 (void) fprintf(stderr, gettext("use "
6833 "umount(1M) to unmount this "
6836 } else if (!zfs_is_mounted(zhp, NULL)) {
6837 (void) fprintf(stderr, gettext("cannot "
6838 "unmount '%s': not currently "
6842 } else if (zfs_unmountall(zhp, flags) != 0) {
6856 * zfs unmount filesystem
6858 * Unmount all filesystems, or a specific ZFS filesystem.
6861 zfs_do_unmount(int argc, char **argv)
6863 return (unshare_unmount(OP_MOUNT, argc, argv));
6868 * zfs unshare filesystem
6870 * Unshare all filesystems, or a specific ZFS filesystem.
6873 zfs_do_unshare(int argc, char **argv)
6875 return (unshare_unmount(OP_SHARE, argc, argv));
6879 find_command_idx(char *command, int *idx)
6883 for (i = 0; i < NCOMMAND; i++) {
6884 if (command_table[i].name == NULL)
6887 if (strcmp(command, command_table[i].name) == 0) {
6896 zfs_do_diff(int argc, char **argv)
6900 char *tosnap = NULL;
6901 char *fromsnap = NULL;
6906 while ((c = getopt(argc, argv, "FHt")) != -1) {
6909 flags |= ZFS_DIFF_CLASSIFY;
6912 flags |= ZFS_DIFF_PARSEABLE;
6915 flags |= ZFS_DIFF_TIMESTAMP;
6918 (void) fprintf(stderr,
6919 gettext("invalid option '%c'\n"), optopt);
6928 (void) fprintf(stderr,
6929 gettext("must provide at least one snapshot name\n"));
6934 (void) fprintf(stderr, gettext("too many arguments\n"));
6939 tosnap = (argc == 2) ? argv[1] : NULL;
6942 if (*fromsnap != '@')
6943 copy = strdup(fromsnap);
6945 copy = strdup(tosnap);
6949 if ((atp = strchr(copy, '@')) != NULL)
6952 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) {
6959 * Ignore SIGPIPE so that the library can give us
6960 * information on any failure
6962 (void) sigignore(SIGPIPE);
6964 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
6972 * zfs bookmark <fs@snap> <fs#bmark>
6974 * Creates a bookmark with the given name from the given snapshot.
6977 zfs_do_bookmark(int argc, char **argv)
6979 char snapname[ZFS_MAX_DATASET_NAME_LEN];
6980 char bookname[ZFS_MAX_DATASET_NAME_LEN];
6987 while ((c = getopt(argc, argv, "")) != -1) {
6990 (void) fprintf(stderr,
6991 gettext("invalid option '%c'\n"), optopt);
6999 /* check number of arguments */
7001 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
7005 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
7009 if (strchr(argv[1], '#') == NULL) {
7010 (void) fprintf(stderr,
7011 gettext("invalid bookmark name '%s': "
7012 "must contain a '#'\n"), argv[1]);
7016 if (argv[0][0] == '@') {
7018 * Snapshot name begins with @.
7019 * Default to same fs as bookmark.
7021 (void) strlcpy(snapname, argv[1], sizeof (snapname));
7022 *strchr(snapname, '#') = '\0';
7023 (void) strlcat(snapname, argv[0], sizeof (snapname));
7025 (void) strlcpy(snapname, argv[0], sizeof (snapname));
7027 if (argv[1][0] == '#') {
7029 * Bookmark name begins with #.
7030 * Default to same fs as snapshot.
7032 (void) strlcpy(bookname, argv[0], sizeof (bookname));
7033 *strchr(bookname, '@') = '\0';
7034 (void) strlcat(bookname, argv[1], sizeof (bookname));
7036 (void) strlcpy(bookname, argv[1], sizeof (bookname));
7039 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
7045 nvl = fnvlist_alloc();
7046 fnvlist_add_string(nvl, bookname, snapname);
7047 ret = lzc_bookmark(nvl, NULL);
7051 const char *err_msg;
7054 (void) snprintf(errbuf, sizeof (errbuf),
7055 dgettext(TEXT_DOMAIN,
7056 "cannot create bookmark '%s'"), bookname);
7060 err_msg = "bookmark is in a different pool";
7063 err_msg = "bookmark exists";
7066 err_msg = "invalid argument";
7069 err_msg = "bookmark feature not enabled";
7072 err_msg = "out of space";
7075 err_msg = "dataset does not exist";
7078 err_msg = "unknown error";
7081 (void) fprintf(stderr, "%s: %s\n", errbuf,
7082 dgettext(TEXT_DOMAIN, err_msg));
7093 zfs_do_channel_program(int argc, char **argv)
7096 char *progbuf, *filename, *poolname;
7097 size_t progsize, progread;
7099 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
7100 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
7101 boolean_t sync_flag = B_TRUE;
7102 zpool_handle_t *zhp;
7105 while ((c = getopt(argc, argv, "nt:m:")) != -1) {
7113 arg = strtoull(optarg, &endp, 0);
7114 if (errno != 0 || *endp != '\0') {
7115 (void) fprintf(stderr, gettext(
7117 "'%s': expected integer\n"), optarg);
7122 if (arg > ZCP_MAX_INSTRLIMIT || arg == 0) {
7123 (void) fprintf(stderr, gettext(
7124 "Invalid instruction limit: "
7131 ASSERT3U(c, ==, 'm');
7132 if (arg > ZCP_MAX_MEMLIMIT || arg == 0) {
7133 (void) fprintf(stderr, gettext(
7134 "Invalid memory limit: "
7144 sync_flag = B_FALSE;
7148 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7158 (void) fprintf(stderr,
7159 gettext("invalid number of arguments\n"));
7165 if (strcmp(filename, "-") == 0) {
7167 filename = "standard input";
7168 } else if ((fd = open(filename, O_RDONLY)) < 0) {
7169 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
7170 filename, strerror(errno));
7174 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7175 (void) fprintf(stderr, gettext("cannot open pool '%s'"),
7182 * Read in the channel program, expanding the program buffer as
7187 progbuf = safe_malloc(progsize);
7189 ret = read(fd, progbuf + progread, progsize - progread);
7191 if (progread == progsize && ret > 0) {
7193 progbuf = safe_realloc(progbuf, progsize);
7201 (void) fprintf(stderr,
7202 gettext("cannot read '%s': %s\n"),
7203 filename, strerror(errno));
7206 progbuf[progread] = '\0';
7209 * Any remaining arguments are passed as arguments to the lua script as
7212 * "argv" -> [ "arg 1", ... "arg n" ],
7215 nvlist_t *argnvl = fnvlist_alloc();
7216 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);
7219 ret = lzc_channel_program(poolname, progbuf,
7220 instrlimit, memlimit, argnvl, &outnvl);
7222 ret = lzc_channel_program_nosync(poolname, progbuf,
7223 instrlimit, memlimit, argnvl, &outnvl);
7228 * On error, report the error message handed back by lua if one
7229 * exists. Otherwise, generate an appropriate error message,
7230 * falling back on strerror() for an unexpected return code.
7232 char *errstring = NULL;
7233 uint64_t instructions = 0;
7234 if (nvlist_exists(outnvl, ZCP_RET_ERROR)) {
7235 (void) nvlist_lookup_string(outnvl,
7236 ZCP_RET_ERROR, &errstring);
7237 if (errstring == NULL)
7238 errstring = strerror(ret);
7240 (void) nvlist_lookup_uint64(outnvl,
7241 ZCP_ARG_INSTRLIMIT, &instructions);
7247 "Invalid instruction or memory limit.";
7250 errstring = "Return value too large.";
7253 errstring = "Memory limit exhausted.";
7256 errstring = "Timed out.";
7259 errstring = "Permission denied. Channel "
7260 "programs must be run as root.";
7263 errstring = strerror(ret);
7266 (void) fprintf(stderr,
7267 gettext("Channel program execution failed:\n%s\n"),
7269 if (ret == ETIME && instructions != 0)
7270 (void) fprintf(stderr, "%llu Lua instructions\n",
7271 (u_longlong_t)instructions);
7273 (void) printf("Channel program fully executed ");
7274 if (nvlist_empty(outnvl)) {
7275 (void) printf("with no return value.\n");
7277 (void) printf("with return value:\n");
7278 dump_nvlist(outnvl, 4);
7283 fnvlist_free(outnvl);
7284 fnvlist_free(argnvl);
7293 typedef struct loadkey_cbdata {
7294 boolean_t cb_loadkey;
7295 boolean_t cb_recursive;
7297 char *cb_keylocation;
7298 uint64_t cb_numfailed;
7299 uint64_t cb_numattempted;
7303 load_key_callback(zfs_handle_t *zhp, void *data)
7306 boolean_t is_encroot;
7307 loadkey_cbdata_t *cb = data;
7308 uint64_t keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
7311 * If we are working recursively, we want to skip loading / unloading
7312 * keys for non-encryption roots and datasets whose keys are already
7313 * in the desired end-state.
7315 if (cb->cb_recursive) {
7316 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
7322 if ((cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_AVAILABLE) ||
7323 (!cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_UNAVAILABLE))
7327 cb->cb_numattempted++;
7330 ret = zfs_crypto_load_key(zhp, cb->cb_noop, cb->cb_keylocation);
7332 ret = zfs_crypto_unload_key(zhp);
7343 load_unload_keys(int argc, char **argv, boolean_t loadkey)
7345 int c, ret = 0, flags = 0;
7346 boolean_t do_all = B_FALSE;
7347 loadkey_cbdata_t cb = { 0 };
7349 cb.cb_loadkey = loadkey;
7351 while ((c = getopt(argc, argv, "anrL:")) != -1) {
7352 /* noop and alternate keylocations only apply to zfs load-key */
7356 cb.cb_noop = B_TRUE;
7359 cb.cb_keylocation = optarg;
7369 cb.cb_recursive = B_TRUE;
7372 flags |= ZFS_ITER_RECURSE;
7373 cb.cb_recursive = B_TRUE;
7376 (void) fprintf(stderr,
7377 gettext("invalid option '%c'\n"), optopt);
7385 if (!do_all && argc == 0) {
7386 (void) fprintf(stderr,
7387 gettext("Missing dataset argument or -a option\n"));
7391 if (do_all && argc != 0) {
7392 (void) fprintf(stderr,
7393 gettext("Cannot specify dataset with -a option\n"));
7397 if (cb.cb_recursive && cb.cb_keylocation != NULL &&
7398 strcmp(cb.cb_keylocation, "prompt") != 0) {
7399 (void) fprintf(stderr, gettext("alternate keylocation may only "
7400 "be 'prompt' with -r or -a\n"));
7404 ret = zfs_for_each(argc, argv, flags,
7405 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL, 0,
7406 load_key_callback, &cb);
7408 if (cb.cb_noop || (cb.cb_recursive && cb.cb_numattempted != 0)) {
7409 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
7410 (u_longlong_t)(cb.cb_numattempted - cb.cb_numfailed),
7411 (u_longlong_t)cb.cb_numattempted,
7412 loadkey ? (cb.cb_noop ? "verified" : "loaded") :
7416 if (cb.cb_numfailed != 0)
7423 zfs_do_load_key(int argc, char **argv)
7425 return (load_unload_keys(argc, argv, B_TRUE));
7430 zfs_do_unload_key(int argc, char **argv)
7432 return (load_unload_keys(argc, argv, B_FALSE));
7436 zfs_do_change_key(int argc, char **argv)
7440 boolean_t loadkey = B_FALSE, inheritkey = B_FALSE;
7441 zfs_handle_t *zhp = NULL;
7442 nvlist_t *props = fnvlist_alloc();
7444 while ((c = getopt(argc, argv, "lio:")) != -1) {
7450 inheritkey = B_TRUE;
7453 if (!parseprop(props, optarg)) {
7459 (void) fprintf(stderr,
7460 gettext("invalid option '%c'\n"), optopt);
7465 if (inheritkey && !nvlist_empty(props)) {
7466 (void) fprintf(stderr,
7467 gettext("Properties not allowed for inheriting\n"));
7475 (void) fprintf(stderr, gettext("Missing dataset argument\n"));
7480 (void) fprintf(stderr, gettext("Too many arguments\n"));
7484 zhp = zfs_open(g_zfs, argv[argc - 1],
7485 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
7490 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
7491 if (keystatus != ZFS_KEYSTATUS_AVAILABLE) {
7492 ret = zfs_crypto_load_key(zhp, B_FALSE, NULL);
7500 /* refresh the properties so the new keystatus is visible */
7501 zfs_refresh_properties(zhp);
7504 ret = zfs_crypto_rewrap(zhp, props, inheritkey);
7517 main(int argc, char **argv)
7523 (void) setlocale(LC_ALL, "");
7524 (void) textdomain(TEXT_DOMAIN);
7529 * Make sure the user has specified some command.
7532 (void) fprintf(stderr, gettext("missing command\n"));
7539 * The 'umount' command is an alias for 'unmount'
7541 if (strcmp(cmdname, "umount") == 0)
7542 cmdname = "unmount";
7545 * The 'recv' command is an alias for 'receive'
7547 if (strcmp(cmdname, "recv") == 0)
7548 cmdname = "receive";
7551 * The 'snap' command is an alias for 'snapshot'
7553 if (strcmp(cmdname, "snap") == 0)
7554 cmdname = "snapshot";
7559 if ((strcmp(cmdname, "-?") == 0) ||
7560 (strcmp(cmdname, "--help") == 0))
7563 if ((g_zfs = libzfs_init()) == NULL) {
7564 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
7568 mnttab_file = g_zfs->libzfs_mnttab;
7570 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7572 libzfs_print_on_error(g_zfs, B_TRUE);
7575 * Run the appropriate command.
7577 libzfs_mnttab_cache(g_zfs, B_TRUE);
7578 if (find_command_idx(cmdname, &i) == 0) {
7579 current_command = &command_table[i];
7580 ret = command_table[i].func(argc - 1, argv + 1);
7581 } else if (strchr(cmdname, '=') != NULL) {
7582 verify(find_command_idx("set", &i) == 0);
7583 current_command = &command_table[i];
7584 ret = command_table[i].func(argc, argv);
7586 (void) fprintf(stderr, gettext("unrecognized "
7587 "command '%s'\n"), cmdname);
7592 if (ret == 0 && log_history)
7593 (void) zpool_log_history(g_zfs, history_str);
7598 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7599 * for the purposes of running ::findleaks.
7601 if (getenv("ZFS_ABORT") != NULL) {
7602 (void) printf("dumping core by request\n");