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, 2019 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.
30 * Copyright (c) 2019 Datto Inc.
40 #include <libnvpair.h>
52 #include <sys/debug.h>
54 #include <sys/mkdev.h>
55 #include <sys/mntent.h>
56 #include <sys/mnttab.h>
57 #include <sys/mount.h>
59 #include <sys/fs/zfs.h>
60 #include <sys/systeminfo.h>
61 #include <sys/types.h>
63 #include <sys/zfs_project.h>
66 #include <libzfs_core.h>
68 #include <zfs_deleg.h>
73 #include <directory.h>
74 #endif /* HAVE_IDMAP */
78 #include "zfs_comutil.h"
79 #include "libzfs_impl.h"
80 #include "zfs_projectutil.h"
82 libzfs_handle_t *g_zfs;
84 static FILE *mnttab_file;
85 static char history_str[HIS_MAX_RECORD_LEN];
86 static boolean_t log_history = B_TRUE;
88 static int zfs_do_clone(int argc, char **argv);
89 static int zfs_do_create(int argc, char **argv);
90 static int zfs_do_destroy(int argc, char **argv);
91 static int zfs_do_get(int argc, char **argv);
92 static int zfs_do_inherit(int argc, char **argv);
93 static int zfs_do_list(int argc, char **argv);
94 static int zfs_do_mount(int argc, char **argv);
95 static int zfs_do_rename(int argc, char **argv);
96 static int zfs_do_rollback(int argc, char **argv);
97 static int zfs_do_set(int argc, char **argv);
98 static int zfs_do_upgrade(int argc, char **argv);
99 static int zfs_do_snapshot(int argc, char **argv);
100 static int zfs_do_unmount(int argc, char **argv);
101 static int zfs_do_share(int argc, char **argv);
102 static int zfs_do_unshare(int argc, char **argv);
103 static int zfs_do_send(int argc, char **argv);
104 static int zfs_do_receive(int argc, char **argv);
105 static int zfs_do_promote(int argc, char **argv);
106 static int zfs_do_userspace(int argc, char **argv);
107 static int zfs_do_allow(int argc, char **argv);
108 static int zfs_do_unallow(int argc, char **argv);
109 static int zfs_do_hold(int argc, char **argv);
110 static int zfs_do_holds(int argc, char **argv);
111 static int zfs_do_release(int argc, char **argv);
112 static int zfs_do_diff(int argc, char **argv);
113 static int zfs_do_bookmark(int argc, char **argv);
114 static int zfs_do_channel_program(int argc, char **argv);
115 static int zfs_do_remap(int argc, char **argv);
116 static int zfs_do_load_key(int argc, char **argv);
117 static int zfs_do_unload_key(int argc, char **argv);
118 static int zfs_do_change_key(int argc, char **argv);
119 static int zfs_do_project(int argc, char **argv);
120 static int zfs_do_version(int argc, char **argv);
123 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
128 _umem_debug_init(void)
130 return ("default,verbose"); /* $UMEM_DEBUG setting */
134 _umem_logging_init(void)
136 return ("fail,contents"); /* $UMEM_LOGGING setting */
171 HELP_CHANNEL_PROGRAM,
178 typedef struct zfs_command {
180 int (*func)(int argc, char **argv);
185 * Master command table. Each ZFS command has a name, associated function, and
186 * usage message. The usage messages need to be internationalized, so we have
187 * to have a function to return the usage message based on a command index.
189 * These commands are organized according to how they are displayed in the usage
190 * message. An empty command (one with a NULL name) indicates an empty line in
191 * the generic usage message.
193 static zfs_command_t command_table[] = {
194 { "version", zfs_do_version, HELP_VERSION },
196 { "create", zfs_do_create, HELP_CREATE },
197 { "destroy", zfs_do_destroy, HELP_DESTROY },
199 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
200 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
201 { "clone", zfs_do_clone, HELP_CLONE },
202 { "promote", zfs_do_promote, HELP_PROMOTE },
203 { "rename", zfs_do_rename, HELP_RENAME },
204 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
205 { "program", zfs_do_channel_program, HELP_CHANNEL_PROGRAM },
207 { "list", zfs_do_list, HELP_LIST },
209 { "set", zfs_do_set, HELP_SET },
210 { "get", zfs_do_get, HELP_GET },
211 { "inherit", zfs_do_inherit, HELP_INHERIT },
212 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
214 { "userspace", zfs_do_userspace, HELP_USERSPACE },
215 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
216 { "projectspace", zfs_do_userspace, HELP_PROJECTSPACE },
218 { "project", zfs_do_project, HELP_PROJECT },
220 { "mount", zfs_do_mount, HELP_MOUNT },
221 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
222 { "share", zfs_do_share, HELP_SHARE },
223 { "unshare", zfs_do_unshare, HELP_UNSHARE },
225 { "send", zfs_do_send, HELP_SEND },
226 { "receive", zfs_do_receive, HELP_RECEIVE },
228 { "allow", zfs_do_allow, HELP_ALLOW },
230 { "unallow", zfs_do_unallow, HELP_UNALLOW },
232 { "hold", zfs_do_hold, HELP_HOLD },
233 { "holds", zfs_do_holds, HELP_HOLDS },
234 { "release", zfs_do_release, HELP_RELEASE },
235 { "diff", zfs_do_diff, HELP_DIFF },
236 { "remap", zfs_do_remap, HELP_REMAP },
237 { "load-key", zfs_do_load_key, HELP_LOAD_KEY },
238 { "unload-key", zfs_do_unload_key, HELP_UNLOAD_KEY },
239 { "change-key", zfs_do_change_key, HELP_CHANGE_KEY },
242 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
244 zfs_command_t *current_command;
247 get_usage(zfs_help_t idx)
251 return (gettext("\tclone [-p] [-o property=value] ... "
252 "<snapshot> <filesystem|volume>\n"));
254 return (gettext("\tcreate [-p] [-o property=value] ... "
256 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
257 "-V <size> <volume>\n"));
259 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
260 "\tdestroy [-dnpRrv] "
261 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
262 "\tdestroy <filesystem|volume>#<bookmark>\n"));
264 return (gettext("\tget [-rHp] [-d max] "
265 "[-o \"all\" | field[,...]]\n"
266 "\t [-t type[,...]] [-s source[,...]]\n"
267 "\t <\"all\" | property[,...]> "
268 "[filesystem|volume|snapshot|bookmark] ...\n"));
270 return (gettext("\tinherit [-rS] <property> "
271 "<filesystem|volume|snapshot> ...\n"));
273 return (gettext("\tupgrade [-v]\n"
274 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
276 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
277 "[-s property]...\n\t [-S property]... [-t type[,...]] "
278 "[filesystem|volume|snapshot] ...\n"));
280 return (gettext("\tmount\n"
281 "\tmount [-lvO] [-o opts] <-a | filesystem>\n"));
283 return (gettext("\tpromote <clone-filesystem>\n"));
285 return (gettext("\treceive [-vnsFhu] "
286 "[-o <property>=<value>] ... [-x <property>] ...\n"
287 "\t <filesystem|volume|snapshot>\n"
288 "\treceive [-vnsFhu] [-o <property>=<value>] ... "
289 "[-x <property>] ... \n"
290 "\t [-d | -e] <filesystem>\n"
291 "\treceive -A <filesystem|volume>\n"));
293 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
294 "<filesystem|volume|snapshot>\n"
295 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
296 "\trename -r <snapshot> <snapshot>\n"));
298 return (gettext("\trollback [-rRf] <snapshot>\n"));
300 return (gettext("\tsend [-DnPpRvLecwhb] [-[i|I] snapshot] "
302 "\tsend [-nvPLecw] [-i snapshot|bookmark] "
303 "<filesystem|volume|snapshot>\n"
304 "\tsend [-nvPe] -t <receive_resume_token>\n"));
306 return (gettext("\tset <property=value> ... "
307 "<filesystem|volume|snapshot> ...\n"));
309 return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
311 return (gettext("\tsnapshot [-r] [-o property=value] ... "
312 "<filesystem|volume>@<snap> ...\n"));
314 return (gettext("\tunmount [-f] "
315 "<-a | filesystem|mountpoint>\n"));
317 return (gettext("\tunshare "
318 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
320 return (gettext("\tallow <filesystem|volume>\n"
322 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
323 "\t <filesystem|volume>\n"
324 "\tallow [-ld] -e <perm|@setname>[,...] "
325 "<filesystem|volume>\n"
326 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
327 "\tallow -s @setname <perm|@setname>[,...] "
328 "<filesystem|volume>\n"));
330 return (gettext("\tunallow [-rldug] "
331 "<\"everyone\"|user|group>[,...]\n"
332 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
333 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
334 "<filesystem|volume>\n"
335 "\tunallow [-r] -c [<perm|@setname>[,...]] "
336 "<filesystem|volume>\n"
337 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
338 "<filesystem|volume>\n"));
340 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
342 "\t [-S field] ... [-t type[,...]] "
343 "<filesystem|snapshot>\n"));
344 case HELP_GROUPSPACE:
345 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
347 "\t [-S field] ... [-t type[,...]] "
348 "<filesystem|snapshot>\n"));
349 case HELP_PROJECTSPACE:
350 return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
352 "\t [-S field] ... <filesystem|snapshot>\n"));
354 return (gettext("\tproject [-d|-r] <directory|file ...>\n"
355 "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
356 "\tproject -C [-k] [-r] <directory ...>\n"
357 "\tproject [-p id] [-r] [-s] <directory ...>\n"));
359 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
361 return (gettext("\tholds [-rH] <snapshot> ...\n"));
363 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
365 return (gettext("\tdiff [-FHt] <snapshot> "
366 "[snapshot|filesystem]\n"));
368 return (gettext("\tremap <filesystem | volume>\n"));
370 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
371 case HELP_CHANNEL_PROGRAM:
372 return (gettext("\tprogram [-jn] [-t <instruction limit>] "
373 "[-m <memory limit (b)>]\n"
374 "\t <pool> <program file> [lua args...]\n"));
376 return (gettext("\tload-key [-rn] [-L <keylocation>] "
377 "<-a | filesystem|volume>\n"));
378 case HELP_UNLOAD_KEY:
379 return (gettext("\tunload-key [-r] "
380 "<-a | filesystem|volume>\n"));
381 case HELP_CHANGE_KEY:
382 return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
383 "\t [-o keylocation=<value>] [-o pbkfd2iters=<value>]\n"
384 "\t <filesystem|volume>\n"
385 "\tchange-key -i [-l] <filesystem|volume>\n"));
387 return (gettext("\tversion\n"));
397 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
402 * Utility function to guarantee malloc() success.
406 safe_malloc(size_t size)
410 if ((data = calloc(1, size)) == NULL)
417 safe_realloc(void *data, size_t size)
420 if ((newp = realloc(data, size)) == NULL) {
429 safe_strdup(char *str)
431 char *dupstr = strdup(str);
440 * Callback routine that will print out information for each of
444 usage_prop_cb(int prop, void *cb)
448 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
450 if (zfs_prop_readonly(prop))
451 (void) fprintf(fp, " NO ");
453 (void) fprintf(fp, "YES ");
455 if (zfs_prop_inheritable(prop))
456 (void) fprintf(fp, " YES ");
458 (void) fprintf(fp, " NO ");
460 if (zfs_prop_values(prop) == NULL)
461 (void) fprintf(fp, "-\n");
463 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
469 * Display usage message. If we're inside a command, display only the usage for
470 * that command. Otherwise, iterate over the entire command table and display
471 * a complete usage message.
474 usage(boolean_t requested)
477 boolean_t show_properties = B_FALSE;
478 FILE *fp = requested ? stdout : stderr;
480 if (current_command == NULL) {
482 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
484 gettext("where 'command' is one of the following:\n\n"));
486 for (i = 0; i < NCOMMAND; i++) {
487 if (command_table[i].name == NULL)
488 (void) fprintf(fp, "\n");
490 (void) fprintf(fp, "%s",
491 get_usage(command_table[i].usage));
494 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
495 "pool/[dataset/]*dataset[@name]\n"));
497 (void) fprintf(fp, gettext("usage:\n"));
498 (void) fprintf(fp, "%s", get_usage(current_command->usage));
501 if (current_command != NULL &&
502 (strcmp(current_command->name, "set") == 0 ||
503 strcmp(current_command->name, "get") == 0 ||
504 strcmp(current_command->name, "inherit") == 0 ||
505 strcmp(current_command->name, "list") == 0))
506 show_properties = B_TRUE;
508 if (show_properties) {
510 gettext("\nThe following properties are supported:\n"));
512 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
513 "PROPERTY", "EDIT", "INHERIT", "VALUES");
515 /* Iterate over all properties */
516 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
519 (void) fprintf(fp, "\t%-15s ", "userused@...");
520 (void) fprintf(fp, " NO NO <size>\n");
521 (void) fprintf(fp, "\t%-15s ", "groupused@...");
522 (void) fprintf(fp, " NO NO <size>\n");
523 (void) fprintf(fp, "\t%-15s ", "projectused@...");
524 (void) fprintf(fp, " NO NO <size>\n");
525 (void) fprintf(fp, "\t%-15s ", "userobjused@...");
526 (void) fprintf(fp, " NO NO <size>\n");
527 (void) fprintf(fp, "\t%-15s ", "groupobjused@...");
528 (void) fprintf(fp, " NO NO <size>\n");
529 (void) fprintf(fp, "\t%-15s ", "projectobjused@...");
530 (void) fprintf(fp, " NO NO <size>\n");
531 (void) fprintf(fp, "\t%-15s ", "userquota@...");
532 (void) fprintf(fp, "YES NO <size> | none\n");
533 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
534 (void) fprintf(fp, "YES NO <size> | none\n");
535 (void) fprintf(fp, "\t%-15s ", "projectquota@...");
536 (void) fprintf(fp, "YES NO <size> | none\n");
537 (void) fprintf(fp, "\t%-15s ", "userobjquota@...");
538 (void) fprintf(fp, "YES NO <size> | none\n");
539 (void) fprintf(fp, "\t%-15s ", "groupobjquota@...");
540 (void) fprintf(fp, "YES NO <size> | none\n");
541 (void) fprintf(fp, "\t%-15s ", "projectobjquota@...");
542 (void) fprintf(fp, "YES NO <size> | none\n");
543 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
544 (void) fprintf(fp, " NO NO <size>\n");
546 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
547 "with standard units such as K, M, G, etc.\n"));
548 (void) fprintf(fp, gettext("\nUser-defined properties can "
549 "be specified by using a name containing a colon (:).\n"));
550 (void) fprintf(fp, gettext("\nThe {user|group|project}"
551 "[obj]{used|quota}@ properties must be appended with\n"
552 "a user|group|project specifier of one of these forms:\n"
553 " POSIX name (eg: \"matt\")\n"
554 " POSIX id (eg: \"126829\")\n"
555 " SMB name@domain (eg: \"matt@sun\")\n"
556 " SMB SID (eg: \"S-1-234-567-89\")\n"));
559 gettext("\nFor the property list, run: %s\n"),
562 gettext("\nFor the delegated permission list, run: %s\n"),
563 "zfs allow|unallow");
567 * See comments at end of main().
569 if (getenv("ZFS_ABORT") != NULL) {
570 (void) printf("dumping core by request\n");
574 exit(requested ? 0 : 2);
578 * Take a property=value argument string and add it to the given nvlist.
579 * Modifies the argument inplace.
582 parseprop(nvlist_t *props, char *propname)
586 if ((propval = strchr(propname, '=')) == NULL) {
587 (void) fprintf(stderr, gettext("missing "
588 "'=' for property=value argument\n"));
593 if (nvlist_exists(props, propname)) {
594 (void) fprintf(stderr, gettext("property '%s' "
595 "specified multiple times\n"), propname);
598 if (nvlist_add_string(props, propname, propval) != 0)
604 * Take a property name argument and add it to the given nvlist.
605 * Modifies the argument inplace.
608 parsepropname(nvlist_t *props, char *propname)
610 if (strchr(propname, '=') != NULL) {
611 (void) fprintf(stderr, gettext("invalid character "
612 "'=' in property argument\n"));
615 if (nvlist_exists(props, propname)) {
616 (void) fprintf(stderr, gettext("property '%s' "
617 "specified multiple times\n"), propname);
620 if (nvlist_add_boolean(props, propname) != 0)
626 parse_depth(char *opt, int *flags)
631 depth = (int)strtol(opt, &tmp, 0);
633 (void) fprintf(stderr,
634 gettext("%s is not an integer\n"), optarg);
638 (void) fprintf(stderr,
639 gettext("Depth can not be negative.\n"));
642 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
646 #define PROGRESS_DELAY 2 /* seconds */
648 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";
649 static time_t pt_begin;
650 static char *pt_header = NULL;
651 static boolean_t pt_shown;
654 start_progress_timer(void)
656 pt_begin = time(NULL) + PROGRESS_DELAY;
661 set_progress_header(char *header)
663 assert(pt_header == NULL);
664 pt_header = safe_strdup(header);
666 (void) printf("%s: ", header);
667 (void) fflush(stdout);
672 update_progress(char *update)
674 if (!pt_shown && time(NULL) > pt_begin) {
675 int len = strlen(update);
677 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
679 (void) fflush(stdout);
681 } else if (pt_shown) {
682 int len = strlen(update);
684 (void) printf("%s%*.*s", update, len, len, pt_reverse);
685 (void) fflush(stdout);
690 finish_progress(char *done)
693 (void) printf("%s\n", done);
694 (void) fflush(stdout);
701 zfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type)
703 zfs_handle_t *zhp = NULL;
706 zhp = zfs_open(hdl, dataset, type);
711 * Volumes may neither be mounted or shared. Potentially in the
712 * future filesystems detected on these volumes could be mounted.
714 if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
720 * Mount and/or share the new filesystem as appropriate. We provide a
721 * verbose error message to let the user know that their filesystem was
722 * in fact created, even if we failed to mount or share it.
724 * If the user doesn't want the dataset automatically mounted, then
725 * skip the mount/share step
727 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type, B_FALSE) &&
728 zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON) {
729 if (geteuid() != 0) {
730 (void) fprintf(stderr, gettext("filesystem "
731 "successfully created, but it may only be "
732 "mounted by root\n"));
734 } else if (zfs_mount(zhp, NULL, 0) != 0) {
735 (void) fprintf(stderr, gettext("filesystem "
736 "successfully created, but not mounted\n"));
738 } else if (zfs_share(zhp) != 0) {
739 (void) fprintf(stderr, gettext("filesystem "
740 "successfully created, but not shared\n"));
751 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
753 * Given an existing dataset, create a writable copy whose initial contents
754 * are the same as the source. The newly created dataset maintains a
755 * dependency on the original; the original cannot be destroyed so long as
758 * The '-p' flag creates all the non-existing ancestors of the target first.
761 zfs_do_clone(int argc, char **argv)
763 zfs_handle_t *zhp = NULL;
764 boolean_t parents = B_FALSE;
769 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
773 while ((c = getopt(argc, argv, "o:p")) != -1) {
776 if (!parseprop(props, optarg)) {
785 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
794 /* check number of arguments */
796 (void) fprintf(stderr, gettext("missing source dataset "
801 (void) fprintf(stderr, gettext("missing target dataset "
806 (void) fprintf(stderr, gettext("too many arguments\n"));
810 /* open the source dataset */
811 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) {
816 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
819 * Now create the ancestors of the target dataset. If the
820 * target already exists and '-p' option was used we should not
823 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
829 if (zfs_create_ancestors(g_zfs, argv[1]) != 0) {
837 ret = zfs_clone(zhp, argv[1], props);
839 /* create the mountpoint if necessary */
842 (void) zpool_log_history(g_zfs, history_str);
843 log_history = B_FALSE;
846 ret = zfs_mount_and_share(g_zfs, argv[1], ZFS_TYPE_DATASET);
855 ASSERT3P(zhp, ==, NULL);
862 * zfs create [-p] [-o prop=value] ... fs
863 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
865 * Create a new dataset. This command can be used to create filesystems
866 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
867 * For volumes, the user must specify a size to be used.
869 * The '-s' flag applies only to volumes, and indicates that we should not try
870 * to set the reservation for this volume. By default we set a reservation
871 * equal to the size for any volume. For pools with SPA_VERSION >=
872 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
874 * The '-p' flag creates all the non-existing ancestors of the target first.
877 zfs_do_create(int argc, char **argv)
879 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
880 uint64_t volsize = 0;
882 boolean_t noreserve = B_FALSE;
883 boolean_t bflag = B_FALSE;
884 boolean_t parents = B_FALSE;
889 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
893 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
896 type = ZFS_TYPE_VOLUME;
897 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
898 (void) fprintf(stderr, gettext("bad volume "
899 "size '%s': %s\n"), optarg,
900 libzfs_error_description(g_zfs));
904 if (nvlist_add_uint64(props,
905 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
914 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
915 (void) fprintf(stderr, gettext("bad volume "
916 "block size '%s': %s\n"), optarg,
917 libzfs_error_description(g_zfs));
921 if (nvlist_add_uint64(props,
922 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
927 if (!parseprop(props, optarg))
934 (void) fprintf(stderr, gettext("missing size "
938 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
944 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
945 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
946 "used when creating a volume\n"));
953 /* check number of arguments */
955 (void) fprintf(stderr, gettext("missing %s argument\n"),
956 zfs_type_to_name(type));
960 (void) fprintf(stderr, gettext("too many arguments\n"));
964 if (type == ZFS_TYPE_VOLUME && !noreserve) {
965 zpool_handle_t *zpool_handle;
966 nvlist_t *real_props = NULL;
967 uint64_t spa_version;
969 zfs_prop_t resv_prop;
973 if ((p = strchr(argv[0], '/')) != NULL)
975 zpool_handle = zpool_open(g_zfs, argv[0]);
978 if (zpool_handle == NULL)
980 spa_version = zpool_get_prop_int(zpool_handle,
981 ZPOOL_PROP_VERSION, NULL);
982 if (spa_version >= SPA_VERSION_REFRESERVATION)
983 resv_prop = ZFS_PROP_REFRESERVATION;
985 resv_prop = ZFS_PROP_RESERVATION;
987 (void) snprintf(msg, sizeof (msg),
988 gettext("cannot create '%s'"), argv[0]);
989 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
990 props, 0, NULL, zpool_handle, B_TRUE, msg)) == NULL) {
991 zpool_close(zpool_handle);
994 zpool_close(zpool_handle);
996 volsize = zvol_volsize_to_reservation(volsize, real_props);
997 nvlist_free(real_props);
999 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
1001 if (nvlist_add_uint64(props,
1002 zfs_prop_to_name(resv_prop), volsize) != 0) {
1009 if (parents && zfs_name_valid(argv[0], type)) {
1011 * Now create the ancestors of target dataset. If the target
1012 * already exists and '-p' option was used we should not
1015 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
1019 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
1023 /* pass to libzfs */
1024 if (zfs_create(g_zfs, argv[0], type, props) != 0)
1028 (void) zpool_log_history(g_zfs, history_str);
1029 log_history = B_FALSE;
1032 ret = zfs_mount_and_share(g_zfs, argv[0], ZFS_TYPE_DATASET);
1043 * zfs destroy [-rRf] <fs, vol>
1044 * zfs destroy [-rRd] <snap>
1046 * -r Recursively destroy all children
1047 * -R Recursively destroy all dependents, including clones
1048 * -f Force unmounting of any dependents
1049 * -d If we can't destroy now, mark for deferred destruction
1051 * Destroys the given dataset. By default, it will unmount any filesystems,
1052 * and refuse to destroy a dataset that has any dependents. A dependent can
1053 * either be a child, or a clone of a child.
1055 typedef struct destroy_cbdata {
1058 boolean_t cb_recurse;
1060 boolean_t cb_doclones;
1061 zfs_handle_t *cb_target;
1062 boolean_t cb_defer_destroy;
1063 boolean_t cb_verbose;
1064 boolean_t cb_parsable;
1065 boolean_t cb_dryrun;
1067 nvlist_t *cb_batchedsnaps;
1069 /* first snap in contiguous run */
1071 /* previous snap in contiguous run */
1073 int64_t cb_snapused;
1076 uint64_t cb_snap_count;
1080 * Check for any dependents based on the '-r' or '-R' flags.
1083 destroy_check_dependent(zfs_handle_t *zhp, void *data)
1085 destroy_cbdata_t *cbp = data;
1086 const char *tname = zfs_get_name(cbp->cb_target);
1087 const char *name = zfs_get_name(zhp);
1089 if (strncmp(tname, name, strlen(tname)) == 0 &&
1090 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
1092 * This is a direct descendant, not a clone somewhere else in
1095 if (cbp->cb_recurse)
1098 if (cbp->cb_first) {
1099 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1100 "%s has children\n"),
1101 zfs_get_name(cbp->cb_target),
1102 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1103 (void) fprintf(stderr, gettext("use '-r' to destroy "
1104 "the following datasets:\n"));
1105 cbp->cb_first = B_FALSE;
1106 cbp->cb_error = B_TRUE;
1109 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1112 * This is a clone. We only want to report this if the '-r'
1113 * wasn't specified, or the target is a snapshot.
1115 if (!cbp->cb_recurse &&
1116 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1119 if (cbp->cb_first) {
1120 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1121 "%s has dependent clones\n"),
1122 zfs_get_name(cbp->cb_target),
1123 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1124 (void) fprintf(stderr, gettext("use '-R' to destroy "
1125 "the following datasets:\n"));
1126 cbp->cb_first = B_FALSE;
1127 cbp->cb_error = B_TRUE;
1128 cbp->cb_dryrun = B_TRUE;
1131 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1140 destroy_batched(destroy_cbdata_t *cb)
1142 int error = zfs_destroy_snaps_nvl(g_zfs,
1143 cb->cb_batchedsnaps, B_FALSE);
1144 fnvlist_free(cb->cb_batchedsnaps);
1145 cb->cb_batchedsnaps = fnvlist_alloc();
1150 destroy_callback(zfs_handle_t *zhp, void *data)
1152 destroy_cbdata_t *cb = data;
1153 const char *name = zfs_get_name(zhp);
1156 if (cb->cb_verbose) {
1157 if (cb->cb_parsable) {
1158 (void) printf("destroy\t%s\n", name);
1159 } else if (cb->cb_dryrun) {
1160 (void) printf(gettext("would destroy %s\n"),
1163 (void) printf(gettext("will destroy %s\n"),
1169 * Ignore pools (which we've already flagged as an error before getting
1172 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1173 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1177 if (cb->cb_dryrun) {
1183 * We batch up all contiguous snapshots (even of different
1184 * filesystems) and destroy them with one ioctl. We can't
1185 * simply do all snap deletions and then all fs deletions,
1186 * because we must delete a clone before its origin.
1188 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1189 cb->cb_snap_count++;
1190 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1191 if (cb->cb_snap_count % 10 == 0 && cb->cb_defer_destroy)
1192 error = destroy_batched(cb);
1194 error = destroy_batched(cb);
1196 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1197 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1200 * When performing a recursive destroy we ignore errors
1201 * so that the recursive destroy could continue
1202 * destroying past problem datasets
1204 if (cb->cb_recurse) {
1205 cb->cb_error = B_TRUE;
1217 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1219 destroy_cbdata_t *cb = arg;
1220 const char *name = zfs_get_name(zhp);
1223 if (nvlist_exists(cb->cb_nvl, name)) {
1224 if (cb->cb_firstsnap == NULL)
1225 cb->cb_firstsnap = strdup(name);
1226 if (cb->cb_prevsnap != NULL)
1227 free(cb->cb_prevsnap);
1228 /* this snap continues the current range */
1229 cb->cb_prevsnap = strdup(name);
1230 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1232 if (cb->cb_verbose) {
1233 if (cb->cb_parsable) {
1234 (void) printf("destroy\t%s\n", name);
1235 } else if (cb->cb_dryrun) {
1236 (void) printf(gettext("would destroy %s\n"),
1239 (void) printf(gettext("will destroy %s\n"),
1243 } else if (cb->cb_firstsnap != NULL) {
1244 /* end of this range */
1246 err = lzc_snaprange_space(cb->cb_firstsnap,
1247 cb->cb_prevsnap, &used);
1248 cb->cb_snapused += used;
1249 free(cb->cb_firstsnap);
1250 cb->cb_firstsnap = NULL;
1251 free(cb->cb_prevsnap);
1252 cb->cb_prevsnap = NULL;
1259 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1262 assert(cb->cb_firstsnap == NULL);
1263 assert(cb->cb_prevsnap == NULL);
1264 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb, 0, 0);
1265 if (cb->cb_firstsnap != NULL) {
1268 err = lzc_snaprange_space(cb->cb_firstsnap,
1269 cb->cb_prevsnap, &used);
1271 cb->cb_snapused += used;
1272 free(cb->cb_firstsnap);
1273 cb->cb_firstsnap = NULL;
1274 free(cb->cb_prevsnap);
1275 cb->cb_prevsnap = NULL;
1281 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1283 destroy_cbdata_t *cb = arg;
1286 /* Check for clones. */
1287 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1288 cb->cb_target = zhp;
1289 cb->cb_first = B_TRUE;
1290 err = zfs_iter_dependents(zhp, B_TRUE,
1291 destroy_check_dependent, cb);
1295 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1303 gather_snapshots(zfs_handle_t *zhp, void *arg)
1305 destroy_cbdata_t *cb = arg;
1308 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1314 if (cb->cb_verbose) {
1315 err = destroy_print_snapshots(zhp, cb);
1321 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1329 destroy_clones(destroy_cbdata_t *cb)
1332 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1334 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1335 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1338 boolean_t defer = cb->cb_defer_destroy;
1342 * We can't defer destroy non-snapshots, so set it to
1343 * false while destroying the clones.
1345 cb->cb_defer_destroy = B_FALSE;
1346 err = zfs_iter_dependents(zhp, B_FALSE,
1347 destroy_callback, cb);
1348 cb->cb_defer_destroy = defer;
1358 zfs_do_destroy(int argc, char **argv)
1360 destroy_cbdata_t cb = { 0 };
1364 zfs_handle_t *zhp = NULL;
1366 zfs_type_t type = ZFS_TYPE_DATASET;
1369 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1372 cb.cb_verbose = B_TRUE;
1375 cb.cb_verbose = B_TRUE;
1376 cb.cb_parsable = B_TRUE;
1379 cb.cb_dryrun = B_TRUE;
1382 cb.cb_defer_destroy = B_TRUE;
1383 type = ZFS_TYPE_SNAPSHOT;
1386 cb.cb_force = B_TRUE;
1389 cb.cb_recurse = B_TRUE;
1392 cb.cb_recurse = B_TRUE;
1393 cb.cb_doclones = B_TRUE;
1397 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1406 /* check number of arguments */
1408 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1412 (void) fprintf(stderr, gettext("too many arguments\n"));
1416 at = strchr(argv[0], '@');
1417 pound = strchr(argv[0], '#');
1420 /* Build the list of snaps to destroy in cb_nvl. */
1421 cb.cb_nvl = fnvlist_alloc();
1424 zhp = zfs_open(g_zfs, argv[0],
1425 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1427 nvlist_free(cb.cb_nvl);
1431 cb.cb_snapspec = at + 1;
1432 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1438 if (nvlist_empty(cb.cb_nvl)) {
1439 (void) fprintf(stderr, gettext("could not find any "
1440 "snapshots to destroy; check snapshot names.\n"));
1445 if (cb.cb_verbose) {
1447 zfs_nicebytes(cb.cb_snapused, buf, sizeof (buf));
1448 if (cb.cb_parsable) {
1449 (void) printf("reclaim\t%llu\n",
1450 (u_longlong_t)cb.cb_snapused);
1451 } else if (cb.cb_dryrun) {
1452 (void) printf(gettext("would reclaim %s\n"),
1455 (void) printf(gettext("will reclaim %s\n"),
1460 if (!cb.cb_dryrun) {
1461 if (cb.cb_doclones) {
1462 cb.cb_batchedsnaps = fnvlist_alloc();
1463 err = destroy_clones(&cb);
1465 err = zfs_destroy_snaps_nvl(g_zfs,
1466 cb.cb_batchedsnaps, B_FALSE);
1474 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1475 cb.cb_defer_destroy);
1481 } else if (pound != NULL) {
1486 (void) fprintf(stderr,
1487 "dryrun is not supported with bookmark\n");
1491 if (cb.cb_defer_destroy) {
1492 (void) fprintf(stderr,
1493 "defer destroy is not supported with bookmark\n");
1497 if (cb.cb_recurse) {
1498 (void) fprintf(stderr,
1499 "recursive is not supported with bookmark\n");
1503 if (!zfs_bookmark_exists(argv[0])) {
1504 (void) fprintf(stderr, gettext("bookmark '%s' "
1505 "does not exist.\n"), argv[0]);
1509 nvl = fnvlist_alloc();
1510 fnvlist_add_boolean(nvl, argv[0]);
1512 err = lzc_destroy_bookmarks(nvl, NULL);
1514 (void) zfs_standard_error(g_zfs, err,
1515 "cannot destroy bookmark");
1522 /* Open the given dataset */
1523 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1529 * Perform an explicit check for pools before going any further.
1531 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1532 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1533 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1534 "operation does not apply to pools\n"),
1536 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1537 "%s' to destroy all datasets in the pool\n"),
1539 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1540 "to destroy the pool itself\n"), zfs_get_name(zhp));
1546 * Check for any dependents and/or clones.
1548 cb.cb_first = B_TRUE;
1549 if (!cb.cb_doclones &&
1550 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1560 cb.cb_batchedsnaps = fnvlist_alloc();
1561 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1568 * Do the real thing. The callback will close the
1569 * handle regardless of whether it succeeds or not.
1571 err = destroy_callback(zhp, &cb);
1574 err = zfs_destroy_snaps_nvl(g_zfs,
1575 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1577 if (err != 0 || cb.cb_error == B_TRUE)
1582 fnvlist_free(cb.cb_batchedsnaps);
1583 fnvlist_free(cb.cb_nvl);
1590 is_recvd_column(zprop_get_cbdata_t *cbp)
1593 zfs_get_column_t col;
1595 for (i = 0; i < ZFS_GET_NCOLS &&
1596 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1597 if (col == GET_COL_RECVD)
1603 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1604 * < all | property[,property]... > < fs | snap | vol > ...
1606 * -r recurse over any child datasets
1607 * -H scripted mode. Headers are stripped, and fields are separated
1608 * by tabs instead of spaces.
1609 * -o Set of fields to display. One of "name,property,value,
1610 * received,source". Default is "name,property,value,source".
1611 * "all" is an alias for all five.
1612 * -s Set of sources to allow. One of
1613 * "local,default,inherited,received,temporary,none". Default is
1615 * -p Display values in parsable (literal) format.
1617 * Prints properties for the given datasets. The user can control which
1618 * columns to display as well as which property types to allow.
1622 * Invoked to display the properties for a single dataset.
1625 get_callback(zfs_handle_t *zhp, void *data)
1627 char buf[ZFS_MAXPROPLEN];
1628 char rbuf[ZFS_MAXPROPLEN];
1629 zprop_source_t sourcetype;
1630 char source[ZFS_MAX_DATASET_NAME_LEN];
1631 zprop_get_cbdata_t *cbp = data;
1632 nvlist_t *user_props = zfs_get_user_props(zhp);
1633 zprop_list_t *pl = cbp->cb_proplist;
1637 boolean_t received = is_recvd_column(cbp);
1639 for (; pl != NULL; pl = pl->pl_next) {
1640 char *recvdval = NULL;
1642 * Skip the special fake placeholder. This will also skip over
1643 * the name property when 'all' is specified.
1645 if (pl->pl_prop == ZFS_PROP_NAME &&
1646 pl == cbp->cb_proplist)
1649 if (pl->pl_prop != ZPROP_INVAL) {
1650 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1651 sizeof (buf), &sourcetype, source,
1653 cbp->cb_literal) != 0) {
1656 if (!zfs_prop_valid_for_type(pl->pl_prop,
1657 ZFS_TYPE_DATASET, B_FALSE)) {
1658 (void) fprintf(stderr,
1659 gettext("No such property '%s'\n"),
1660 zfs_prop_to_name(pl->pl_prop));
1663 sourcetype = ZPROP_SRC_NONE;
1664 (void) strlcpy(buf, "-", sizeof (buf));
1667 if (received && (zfs_prop_get_recvd(zhp,
1668 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1669 cbp->cb_literal) == 0))
1672 zprop_print_one_property(zfs_get_name(zhp), cbp,
1673 zfs_prop_to_name(pl->pl_prop),
1674 buf, sourcetype, source, recvdval);
1675 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1676 sourcetype = ZPROP_SRC_LOCAL;
1678 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1679 buf, sizeof (buf), cbp->cb_literal) != 0) {
1680 sourcetype = ZPROP_SRC_NONE;
1681 (void) strlcpy(buf, "-", sizeof (buf));
1684 zprop_print_one_property(zfs_get_name(zhp), cbp,
1685 pl->pl_user_prop, buf, sourcetype, source, NULL);
1686 } else if (zfs_prop_written(pl->pl_user_prop)) {
1687 sourcetype = ZPROP_SRC_LOCAL;
1689 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1690 buf, sizeof (buf), cbp->cb_literal) != 0) {
1691 sourcetype = ZPROP_SRC_NONE;
1692 (void) strlcpy(buf, "-", sizeof (buf));
1695 zprop_print_one_property(zfs_get_name(zhp), cbp,
1696 pl->pl_user_prop, buf, sourcetype, source, NULL);
1698 if (nvlist_lookup_nvlist(user_props,
1699 pl->pl_user_prop, &propval) != 0) {
1702 sourcetype = ZPROP_SRC_NONE;
1705 verify(nvlist_lookup_string(propval,
1706 ZPROP_VALUE, &strval) == 0);
1707 verify(nvlist_lookup_string(propval,
1708 ZPROP_SOURCE, &sourceval) == 0);
1710 if (strcmp(sourceval,
1711 zfs_get_name(zhp)) == 0) {
1712 sourcetype = ZPROP_SRC_LOCAL;
1713 } else if (strcmp(sourceval,
1714 ZPROP_SOURCE_VAL_RECVD) == 0) {
1715 sourcetype = ZPROP_SRC_RECEIVED;
1717 sourcetype = ZPROP_SRC_INHERITED;
1718 (void) strlcpy(source,
1719 sourceval, sizeof (source));
1723 if (received && (zfs_prop_get_recvd(zhp,
1724 pl->pl_user_prop, rbuf, sizeof (rbuf),
1725 cbp->cb_literal) == 0))
1728 zprop_print_one_property(zfs_get_name(zhp), cbp,
1729 pl->pl_user_prop, strval, sourcetype,
1738 zfs_do_get(int argc, char **argv)
1740 zprop_get_cbdata_t cb = { 0 };
1741 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1742 int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
1743 char *value, *fields;
1746 zprop_list_t fake_name = { 0 };
1749 * Set up default columns and sources.
1751 cb.cb_sources = ZPROP_SRC_ALL;
1752 cb.cb_columns[0] = GET_COL_NAME;
1753 cb.cb_columns[1] = GET_COL_PROPERTY;
1754 cb.cb_columns[2] = GET_COL_VALUE;
1755 cb.cb_columns[3] = GET_COL_SOURCE;
1756 cb.cb_type = ZFS_TYPE_DATASET;
1759 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1762 cb.cb_literal = B_TRUE;
1765 limit = parse_depth(optarg, &flags);
1768 flags |= ZFS_ITER_RECURSE;
1771 cb.cb_scripted = B_TRUE;
1774 (void) fprintf(stderr, gettext("missing argument for "
1775 "'%c' option\n"), optopt);
1780 * Process the set of columns to display. We zero out
1781 * the structure to give us a blank slate.
1783 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1785 while (*optarg != '\0') {
1786 static char *col_subopts[] =
1787 { "name", "property", "value", "received",
1788 "source", "all", NULL };
1790 if (i == ZFS_GET_NCOLS) {
1791 (void) fprintf(stderr, gettext("too "
1792 "many fields given to -o "
1797 switch (getsubopt(&optarg, col_subopts,
1800 cb.cb_columns[i++] = GET_COL_NAME;
1803 cb.cb_columns[i++] = GET_COL_PROPERTY;
1806 cb.cb_columns[i++] = GET_COL_VALUE;
1809 cb.cb_columns[i++] = GET_COL_RECVD;
1810 flags |= ZFS_ITER_RECVD_PROPS;
1813 cb.cb_columns[i++] = GET_COL_SOURCE;
1817 (void) fprintf(stderr,
1818 gettext("\"all\" conflicts "
1819 "with specific fields "
1820 "given to -o option\n"));
1823 cb.cb_columns[0] = GET_COL_NAME;
1824 cb.cb_columns[1] = GET_COL_PROPERTY;
1825 cb.cb_columns[2] = GET_COL_VALUE;
1826 cb.cb_columns[3] = GET_COL_RECVD;
1827 cb.cb_columns[4] = GET_COL_SOURCE;
1828 flags |= ZFS_ITER_RECVD_PROPS;
1832 (void) fprintf(stderr,
1833 gettext("invalid column name "
1842 while (*optarg != '\0') {
1843 static char *source_subopts[] = {
1844 "local", "default", "inherited",
1845 "received", "temporary", "none",
1848 switch (getsubopt(&optarg, source_subopts,
1851 cb.cb_sources |= ZPROP_SRC_LOCAL;
1854 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1857 cb.cb_sources |= ZPROP_SRC_INHERITED;
1860 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1863 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1866 cb.cb_sources |= ZPROP_SRC_NONE;
1869 (void) fprintf(stderr,
1870 gettext("invalid source "
1879 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1880 while (*optarg != '\0') {
1881 static char *type_subopts[] = { "filesystem",
1882 "volume", "snapshot", "bookmark",
1885 switch (getsubopt(&optarg, type_subopts,
1888 types |= ZFS_TYPE_FILESYSTEM;
1891 types |= ZFS_TYPE_VOLUME;
1894 types |= ZFS_TYPE_SNAPSHOT;
1897 types |= ZFS_TYPE_BOOKMARK;
1900 types = ZFS_TYPE_DATASET |
1905 (void) fprintf(stderr,
1906 gettext("invalid type '%s'\n"),
1914 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1924 (void) fprintf(stderr, gettext("missing property "
1932 * Handle users who want to get all snapshots of the current
1933 * dataset (ex. 'zfs get -t snapshot refer <dataset>').
1935 if (types == ZFS_TYPE_SNAPSHOT && argc > 1 &&
1936 (flags & ZFS_ITER_RECURSE) == 0 && limit == 0) {
1937 flags |= (ZFS_ITER_DEPTH_LIMIT | ZFS_ITER_RECURSE);
1941 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1949 * As part of zfs_expand_proplist(), we keep track of the maximum column
1950 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1951 * need to know the maximum name length. However, the user likely did
1952 * not specify 'name' as one of the properties to fetch, so we need to
1953 * make sure we always include at least this property for
1954 * print_get_headers() to work properly.
1956 if (cb.cb_proplist != NULL) {
1957 fake_name.pl_prop = ZFS_PROP_NAME;
1958 fake_name.pl_width = strlen(gettext("NAME"));
1959 fake_name.pl_next = cb.cb_proplist;
1960 cb.cb_proplist = &fake_name;
1963 cb.cb_first = B_TRUE;
1965 /* run for each object */
1966 ret = zfs_for_each(argc, argv, flags, types, NULL,
1967 &cb.cb_proplist, limit, get_callback, &cb);
1969 if (cb.cb_proplist == &fake_name)
1970 zprop_free_list(fake_name.pl_next);
1972 zprop_free_list(cb.cb_proplist);
1978 * inherit [-rS] <property> <fs|vol> ...
1980 * -r Recurse over all children
1981 * -S Revert to received value, if any
1983 * For each dataset specified on the command line, inherit the given property
1984 * from its parent. Inheriting a property at the pool level will cause it to
1985 * use the default value. The '-r' flag will recurse over all children, and is
1986 * useful for setting a property on a hierarchy-wide basis, regardless of any
1987 * local modifications for each dataset.
1990 typedef struct inherit_cbdata {
1991 const char *cb_propname;
1992 boolean_t cb_received;
1996 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1998 inherit_cbdata_t *cb = data;
1999 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
2002 * If we're doing it recursively, then ignore properties that
2003 * are not valid for this type of dataset.
2005 if (prop != ZPROP_INVAL &&
2006 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp), B_FALSE))
2009 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
2013 inherit_cb(zfs_handle_t *zhp, void *data)
2015 inherit_cbdata_t *cb = data;
2017 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
2021 zfs_do_inherit(int argc, char **argv)
2025 inherit_cbdata_t cb = { 0 };
2029 boolean_t received = B_FALSE;
2032 while ((c = getopt(argc, argv, "rS")) != -1) {
2035 flags |= ZFS_ITER_RECURSE;
2042 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2051 /* check number of arguments */
2053 (void) fprintf(stderr, gettext("missing property argument\n"));
2057 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2065 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
2066 if (zfs_prop_readonly(prop)) {
2067 (void) fprintf(stderr, gettext(
2068 "%s property is read-only\n"),
2072 if (!zfs_prop_inheritable(prop) && !received) {
2073 (void) fprintf(stderr, gettext("'%s' property cannot "
2074 "be inherited\n"), propname);
2075 if (prop == ZFS_PROP_QUOTA ||
2076 prop == ZFS_PROP_RESERVATION ||
2077 prop == ZFS_PROP_REFQUOTA ||
2078 prop == ZFS_PROP_REFRESERVATION) {
2079 (void) fprintf(stderr, gettext("use 'zfs set "
2080 "%s=none' to clear\n"), propname);
2081 (void) fprintf(stderr, gettext("use 'zfs "
2082 "inherit -S %s' to revert to received "
2083 "value\n"), propname);
2087 if (received && (prop == ZFS_PROP_VOLSIZE ||
2088 prop == ZFS_PROP_VERSION)) {
2089 (void) fprintf(stderr, gettext("'%s' property cannot "
2090 "be reverted to a received value\n"), propname);
2093 } else if (!zfs_prop_user(propname)) {
2094 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
2099 cb.cb_propname = propname;
2100 cb.cb_received = received;
2102 if (flags & ZFS_ITER_RECURSE) {
2103 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2104 NULL, NULL, 0, inherit_recurse_cb, &cb);
2106 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2107 NULL, NULL, 0, inherit_cb, &cb);
2113 typedef struct upgrade_cbdata {
2114 uint64_t cb_numupgraded;
2115 uint64_t cb_numsamegraded;
2116 uint64_t cb_numfailed;
2117 uint64_t cb_version;
2119 boolean_t cb_foundone;
2120 char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
2124 same_pool(zfs_handle_t *zhp, const char *name)
2126 int len1 = strcspn(name, "/@");
2127 const char *zhname = zfs_get_name(zhp);
2128 int len2 = strcspn(zhname, "/@");
2132 return (strncmp(name, zhname, len1) == 0);
2136 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2138 upgrade_cbdata_t *cb = data;
2139 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2141 /* list if it's old/new */
2142 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2143 (cb->cb_newer && version > ZPL_VERSION)) {
2146 str = gettext("The following filesystems are "
2147 "formatted using a newer software version and\n"
2148 "cannot be accessed on the current system.\n\n");
2150 str = gettext("The following filesystems are "
2151 "out of date, and can be upgraded. After being\n"
2152 "upgraded, these filesystems (and any 'zfs send' "
2153 "streams generated from\n"
2154 "subsequent snapshots) will no longer be "
2155 "accessible by older software versions.\n\n");
2158 if (!cb->cb_foundone) {
2160 (void) printf(gettext("VER FILESYSTEM\n"));
2161 (void) printf(gettext("--- ------------\n"));
2162 cb->cb_foundone = B_TRUE;
2165 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2172 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2174 upgrade_cbdata_t *cb = data;
2175 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2176 int needed_spa_version;
2179 if (zfs_spa_version(zhp, &spa_version) < 0)
2182 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2184 if (needed_spa_version < 0)
2187 if (spa_version < needed_spa_version) {
2189 (void) printf(gettext("%s: can not be "
2190 "upgraded; the pool version needs to first "
2191 "be upgraded\nto version %d\n\n"),
2192 zfs_get_name(zhp), needed_spa_version);
2198 if (version < cb->cb_version) {
2200 (void) snprintf(verstr, sizeof (verstr),
2201 "%llu", (u_longlong_t)cb->cb_version);
2202 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2204 * If they did "zfs upgrade -a", then we could
2205 * be doing ioctls to different pools. We need
2206 * to log this history once to each pool, and bypass
2207 * the normal history logging that happens in main().
2209 (void) zpool_log_history(g_zfs, history_str);
2210 log_history = B_FALSE;
2212 if (zfs_prop_set(zhp, "version", verstr) == 0)
2213 cb->cb_numupgraded++;
2216 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2217 } else if (version > cb->cb_version) {
2218 /* can't downgrade */
2219 (void) printf(gettext("%s: can not be downgraded; "
2220 "it is already at version %u\n"),
2221 zfs_get_name(zhp), version);
2224 cb->cb_numsamegraded++;
2232 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2235 zfs_do_upgrade(int argc, char **argv)
2237 boolean_t all = B_FALSE;
2238 boolean_t showversions = B_FALSE;
2240 upgrade_cbdata_t cb = { 0 };
2242 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2245 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2248 flags |= ZFS_ITER_RECURSE;
2251 showversions = B_TRUE;
2254 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2255 optarg, &cb.cb_version) != 0) {
2256 (void) fprintf(stderr,
2257 gettext("invalid version %s\n"), optarg);
2266 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2275 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2277 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2278 cb.cb_version || argc))
2280 if ((all || argc) && (showversions))
2286 /* Show info on available versions. */
2287 (void) printf(gettext("The following filesystem versions are "
2289 (void) printf(gettext("VER DESCRIPTION\n"));
2290 (void) printf("--- -----------------------------------------"
2291 "---------------\n");
2292 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2293 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2294 (void) printf(gettext(" 3 Case insensitive and filesystem "
2295 "user identifier (FUID)\n"));
2296 (void) printf(gettext(" 4 userquota, groupquota "
2298 (void) printf(gettext(" 5 System attributes\n"));
2299 (void) printf(gettext("\nFor more information on a particular "
2300 "version, including supported releases,\n"));
2301 (void) printf("see the ZFS Administration Guide.\n\n");
2303 } else if (argc || all) {
2304 /* Upgrade filesystems */
2305 if (cb.cb_version == 0)
2306 cb.cb_version = ZPL_VERSION;
2307 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2308 NULL, NULL, 0, upgrade_set_callback, &cb);
2309 (void) printf(gettext("%llu filesystems upgraded\n"),
2310 (u_longlong_t)cb.cb_numupgraded);
2311 if (cb.cb_numsamegraded) {
2312 (void) printf(gettext("%llu filesystems already at "
2314 (u_longlong_t)cb.cb_numsamegraded);
2316 if (cb.cb_numfailed != 0)
2319 /* List old-version filesystems */
2321 (void) printf(gettext("This system is currently running "
2322 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2324 flags |= ZFS_ITER_RECURSE;
2325 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2326 NULL, NULL, 0, upgrade_list_callback, &cb);
2328 found = cb.cb_foundone;
2329 cb.cb_foundone = B_FALSE;
2330 cb.cb_newer = B_TRUE;
2332 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2333 NULL, NULL, 0, upgrade_list_callback, &cb);
2335 if (!cb.cb_foundone && !found) {
2336 (void) printf(gettext("All filesystems are "
2337 "formatted with the current version.\n"));
2345 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2346 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2347 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2348 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2349 * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2350 * [-S field [-S field]...] filesystem | snapshot
2352 * -H Scripted mode; elide headers and separate columns by tabs.
2353 * -i Translate SID to POSIX ID.
2354 * -n Print numeric ID instead of user/group name.
2355 * -o Control which fields to display.
2356 * -p Use exact (parsable) numeric output.
2357 * -s Specify sort columns, descending order.
2358 * -S Specify sort columns, ascending order.
2359 * -t Control which object types to display.
2361 * Displays space consumed by, and quotas on, each user in the specified
2362 * filesystem or snapshot.
2365 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2366 enum us_field_types {
2374 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA",
2375 "OBJUSED", "OBJQUOTA" };
2376 static char *us_field_names[] = { "type", "name", "used", "quota",
2377 "objused", "objquota" };
2378 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2380 #define USTYPE_PSX_GRP (1 << 0)
2381 #define USTYPE_PSX_USR (1 << 1)
2382 #define USTYPE_SMB_GRP (1 << 2)
2383 #define USTYPE_SMB_USR (1 << 3)
2384 #define USTYPE_PROJ (1 << 4)
2385 #define USTYPE_ALL \
2386 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2389 static int us_type_bits[] = {
2396 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2399 typedef struct us_node {
2401 uu_avl_node_t usn_avlnode;
2402 uu_list_node_t usn_listnode;
2405 typedef struct us_cbdata {
2407 uu_avl_pool_t *cb_avl_pool;
2409 boolean_t cb_numname;
2410 boolean_t cb_nicenum;
2411 boolean_t cb_sid2posix;
2412 zfs_userquota_prop_t cb_prop;
2413 zfs_sort_column_t *cb_sortcol;
2414 size_t cb_width[USFIELD_LAST];
2417 static boolean_t us_populated = B_FALSE;
2420 zfs_sort_column_t *si_sortcol;
2421 boolean_t si_numname;
2425 us_field_index(char *field)
2429 for (i = 0; i < USFIELD_LAST; i++) {
2430 if (strcmp(field, us_field_names[i]) == 0)
2438 us_compare(const void *larg, const void *rarg, void *unused)
2440 const us_node_t *l = larg;
2441 const us_node_t *r = rarg;
2442 us_sort_info_t *si = (us_sort_info_t *)unused;
2443 zfs_sort_column_t *sortcol = si->si_sortcol;
2444 boolean_t numname = si->si_numname;
2445 nvlist_t *lnvl = l->usn_nvl;
2446 nvlist_t *rnvl = r->usn_nvl;
2450 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2457 zfs_prop_t prop = sortcol->sc_prop;
2458 const char *propname = NULL;
2459 boolean_t reverse = sortcol->sc_reverse;
2464 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2465 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2467 rc = (rv32 < lv32) ? 1 : -1;
2473 (void) nvlist_lookup_uint64(lnvl, propname,
2475 (void) nvlist_lookup_uint64(rnvl, propname,
2478 rc = (rv64 < lv64) ? 1 : -1;
2480 if ((nvlist_lookup_string(lnvl, propname,
2481 &lvstr) == ENOENT) ||
2482 (nvlist_lookup_string(rnvl, propname,
2483 &rvstr) == ENOENT)) {
2486 rc = strcmp(lvstr, rvstr);
2490 case ZFS_PROP_QUOTA:
2493 if (prop == ZFS_PROP_USED)
2497 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2498 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2500 rc = (rv64 < lv64) ? 1 : -1;
2509 return (reverse ? 1 : -1);
2511 return (reverse ? -1 : 1);
2516 * If entries still seem to be the same, check if they are of the same
2517 * type (smbentity is added only if we are doing SID to POSIX ID
2518 * translation where we can have duplicate type/name combinations).
2520 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2521 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2523 return (lvb < rvb ? -1 : 1);
2529 zfs_prop_is_user(unsigned p)
2531 return (p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA ||
2532 p == ZFS_PROP_USEROBJUSED || p == ZFS_PROP_USEROBJQUOTA);
2536 zfs_prop_is_group(unsigned p)
2538 return (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA ||
2539 p == ZFS_PROP_GROUPOBJUSED || p == ZFS_PROP_GROUPOBJQUOTA);
2543 zfs_prop_is_project(unsigned p)
2545 return (p == ZFS_PROP_PROJECTUSED || p == ZFS_PROP_PROJECTQUOTA ||
2546 p == ZFS_PROP_PROJECTOBJUSED || p == ZFS_PROP_PROJECTOBJQUOTA);
2549 static inline const char *
2550 us_type2str(unsigned field_type)
2552 switch (field_type) {
2553 case USTYPE_PSX_USR:
2554 return ("POSIX User");
2555 case USTYPE_PSX_GRP:
2556 return ("POSIX Group");
2557 case USTYPE_SMB_USR:
2558 return ("SMB User");
2559 case USTYPE_SMB_GRP:
2560 return ("SMB Group");
2564 return ("Undefined");
2569 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2571 us_cbdata_t *cb = (us_cbdata_t *)arg;
2572 zfs_userquota_prop_t prop = cb->cb_prop;
2577 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2578 uu_avl_t *avl = cb->cb_avl;
2582 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2584 const char *typestr;
2588 int typeidx, nameidx, sizeidx;
2589 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2590 boolean_t smbentity = B_FALSE;
2592 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2594 node = safe_malloc(sizeof (us_node_t));
2595 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2596 node->usn_nvl = props;
2598 if (domain != NULL && domain[0] != '\0') {
2601 char sid[MAXNAMELEN + 32];
2605 directory_error_t e;
2609 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2611 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2612 type = USTYPE_SMB_GRP;
2613 err = sid_to_id(sid, B_FALSE, &id);
2615 type = USTYPE_SMB_USR;
2616 err = sid_to_id(sid, B_TRUE, &id);
2621 if (!cb->cb_sid2posix) {
2622 e = directory_name_from_sid(NULL, sid, &name,
2625 directory_error_free(e);
2635 #endif /* HAVE_IDMAP */
2638 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2640 if (zfs_prop_is_group(prop)) {
2641 type = USTYPE_PSX_GRP;
2642 if (!cb->cb_numname) {
2645 if ((g = getgrgid(rid)) != NULL)
2648 } else if (zfs_prop_is_user(prop)) {
2649 type = USTYPE_PSX_USR;
2650 if (!cb->cb_numname) {
2653 if ((p = getpwuid(rid)) != NULL)
2662 * Make sure that the type/name combination is unique when doing
2663 * SID to POSIX ID translation (hence changing the type from SMB to
2666 if (cb->cb_sid2posix &&
2667 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2670 /* Calculate/update width of TYPE field */
2671 typestr = us_type2str(type);
2672 typelen = strlen(gettext(typestr));
2673 typeidx = us_field_index("type");
2674 if (typelen > cb->cb_width[typeidx])
2675 cb->cb_width[typeidx] = typelen;
2676 if (nvlist_add_uint32(props, "type", type) != 0)
2679 /* Calculate/update width of NAME field */
2680 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2681 if (nvlist_add_uint64(props, "name", rid) != 0)
2683 namelen = snprintf(NULL, 0, "%u", rid);
2685 if (nvlist_add_string(props, "name", name) != 0)
2687 namelen = strlen(name);
2689 nameidx = us_field_index("name");
2690 if (nameidx >= 0 && namelen > cb->cb_width[nameidx])
2691 cb->cb_width[nameidx] = namelen;
2694 * Check if this type/name combination is in the list and update it;
2695 * otherwise add new node to the list.
2697 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2698 uu_avl_insert(avl, node, idx);
2703 props = node->usn_nvl;
2706 /* Calculate/update width of USED/QUOTA fields */
2707 if (cb->cb_nicenum) {
2708 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
2709 prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
2710 prop == ZFS_PROP_PROJECTUSED ||
2711 prop == ZFS_PROP_PROJECTQUOTA) {
2712 zfs_nicebytes(space, sizebuf, sizeof (sizebuf));
2714 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2717 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
2718 (u_longlong_t)space);
2720 sizelen = strlen(sizebuf);
2721 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
2722 prop == ZFS_PROP_PROJECTUSED) {
2724 if (!nvlist_exists(props, "quota"))
2725 (void) nvlist_add_uint64(props, "quota", 0);
2726 } else if (prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
2727 prop == ZFS_PROP_PROJECTQUOTA) {
2729 if (!nvlist_exists(props, "used"))
2730 (void) nvlist_add_uint64(props, "used", 0);
2731 } else if (prop == ZFS_PROP_USEROBJUSED ||
2732 prop == ZFS_PROP_GROUPOBJUSED || prop == ZFS_PROP_PROJECTOBJUSED) {
2733 propname = "objused";
2734 if (!nvlist_exists(props, "objquota"))
2735 (void) nvlist_add_uint64(props, "objquota", 0);
2736 } else if (prop == ZFS_PROP_USEROBJQUOTA ||
2737 prop == ZFS_PROP_GROUPOBJQUOTA ||
2738 prop == ZFS_PROP_PROJECTOBJQUOTA) {
2739 propname = "objquota";
2740 if (!nvlist_exists(props, "objused"))
2741 (void) nvlist_add_uint64(props, "objused", 0);
2745 sizeidx = us_field_index(propname);
2746 if (sizeidx >= 0 && sizelen > cb->cb_width[sizeidx])
2747 cb->cb_width[sizeidx] = sizelen;
2749 if (nvlist_add_uint64(props, propname, space) != 0)
2756 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2757 size_t *width, us_node_t *node)
2759 nvlist_t *nvl = node->usn_nvl;
2760 char valstr[MAXNAMELEN];
2761 boolean_t first = B_TRUE;
2767 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2768 if (!(ustype & types))
2771 while ((field = fields[cfield]) != USFIELD_LAST) {
2772 nvpair_t *nvp = NULL;
2778 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2779 if (strcmp(nvpair_name(nvp),
2780 us_field_names[field]) == 0)
2784 type = nvp == NULL ? DATA_TYPE_UNKNOWN : nvpair_type(nvp);
2786 case DATA_TYPE_UINT32:
2787 (void) nvpair_value_uint32(nvp, &val32);
2789 case DATA_TYPE_UINT64:
2790 (void) nvpair_value_uint64(nvp, &val64);
2792 case DATA_TYPE_STRING:
2793 (void) nvpair_value_string(nvp, &strval);
2795 case DATA_TYPE_UNKNOWN:
2798 (void) fprintf(stderr, "invalid data type\n");
2803 if (type == DATA_TYPE_UINT32)
2804 strval = (char *)us_type2str(val32);
2807 if (type == DATA_TYPE_UINT64) {
2808 (void) sprintf(valstr, "%llu",
2809 (u_longlong_t)val64);
2815 if (type == DATA_TYPE_UINT64) {
2817 (void) sprintf(valstr, "%llu",
2818 (u_longlong_t)val64);
2820 } else if (field == USFIELD_QUOTA &&
2824 zfs_nicebytes(val64, valstr,
2830 case USFIELD_OBJUSED:
2831 case USFIELD_OBJQUOTA:
2832 if (type == DATA_TYPE_UINT64) {
2834 (void) sprintf(valstr, "%llu",
2835 (u_longlong_t)val64);
2837 } else if (field == USFIELD_OBJQUOTA &&
2841 zfs_nicenum(val64, valstr,
2851 (void) printf("\t");
2856 (void) printf("%s", strval);
2857 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2858 (void) printf("%-*s", (int)width[field], strval);
2860 (void) printf("%*s", (int)width[field], strval);
2866 (void) printf("\n");
2870 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2871 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2879 boolean_t first = B_TRUE;
2881 while ((field = fields[cfield]) != USFIELD_LAST) {
2882 col = gettext(us_field_hdr[field]);
2883 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2884 (void) printf(first ? "%-*s" : " %-*s",
2885 (int)width[field], col);
2887 (void) printf(first ? "%*s" : " %*s",
2888 (int)width[field], col);
2893 (void) printf("\n");
2896 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2897 print_us_node(scripted, parsable, fields, types, width, node);
2899 nvlist_free(node->usn_nvl);
2904 zfs_do_userspace(int argc, char **argv)
2907 zfs_userquota_prop_t p;
2908 uu_avl_pool_t *avl_pool;
2910 uu_avl_walk_t *walk;
2912 char deffields[] = "type,name,used,quota,objused,objquota";
2913 char *ofield = NULL;
2914 char *tfield = NULL;
2918 boolean_t scripted = B_FALSE;
2919 boolean_t prtnum = B_FALSE;
2920 boolean_t parsable = B_FALSE;
2921 boolean_t sid2posix = B_FALSE;
2924 zfs_sort_column_t *sortcol = NULL;
2925 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2929 uu_list_pool_t *listpool;
2931 uu_avl_index_t idx = 0;
2932 uu_list_index_t idx2 = 0;
2937 if (strcmp(argv[0], "groupspace") == 0) {
2938 /* Toggle default group types */
2939 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2940 } else if (strcmp(argv[0], "projectspace") == 0) {
2941 types = USTYPE_PROJ;
2945 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2948 if (types == USTYPE_PROJ) {
2949 (void) fprintf(stderr,
2950 gettext("invalid option 'n'\n"));
2966 if (zfs_add_sort_column(&sortcol, optarg,
2967 c == 's' ? B_FALSE : B_TRUE) != 0) {
2968 (void) fprintf(stderr,
2969 gettext("invalid field '%s'\n"), optarg);
2974 if (types == USTYPE_PROJ) {
2975 (void) fprintf(stderr,
2976 gettext("invalid option 't'\n"));
2982 if (types == USTYPE_PROJ) {
2983 (void) fprintf(stderr,
2984 gettext("invalid option 'i'\n"));
2990 (void) fprintf(stderr, gettext("missing argument for "
2991 "'%c' option\n"), optopt);
2995 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3005 (void) fprintf(stderr, gettext("missing dataset name\n"));
3009 (void) fprintf(stderr, gettext("too many arguments\n"));
3013 /* Use default output fields if not specified using -o */
3017 if ((delim = strchr(ofield, ',')) != NULL)
3019 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
3020 (void) fprintf(stderr, gettext("invalid type '%s' "
3021 "for -o option\n"), ofield);
3026 } while (delim != NULL);
3027 fields[cfield] = USFIELD_LAST;
3029 /* Override output types (-t option) */
3030 if (tfield != NULL) {
3034 boolean_t found = B_FALSE;
3036 if ((delim = strchr(tfield, ',')) != NULL)
3038 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
3040 if (strcmp(tfield, us_type_names[i]) == 0) {
3042 types |= us_type_bits[i];
3047 (void) fprintf(stderr, gettext("invalid type "
3048 "'%s' for -t option\n"), tfield);
3053 } while (delim != NULL);
3056 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM |
3057 ZFS_TYPE_SNAPSHOT)) == NULL)
3059 if (zhp->zfs_head_type != ZFS_TYPE_FILESYSTEM) {
3060 (void) fprintf(stderr, gettext("operation is only applicable "
3061 "to filesystems and their snapshots\n"));
3066 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
3067 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
3069 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
3072 /* Always add default sorting columns */
3073 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
3074 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
3076 cb.cb_sortcol = sortcol;
3077 cb.cb_numname = prtnum;
3078 cb.cb_nicenum = !parsable;
3079 cb.cb_avl_pool = avl_pool;
3080 cb.cb_avl = avl_tree;
3081 cb.cb_sid2posix = sid2posix;
3083 for (i = 0; i < USFIELD_LAST; i++)
3084 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
3086 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
3087 if ((zfs_prop_is_user(p) &&
3088 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
3089 (zfs_prop_is_group(p) &&
3090 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))) ||
3091 (zfs_prop_is_project(p) && types != USTYPE_PROJ))
3095 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0) {
3103 if ((node = uu_avl_first(avl_tree)) == NULL)
3106 us_populated = B_TRUE;
3108 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
3109 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
3110 list = uu_list_create(listpool, NULL, UU_DEFAULT);
3111 uu_list_node_init(node, &node->usn_listnode, listpool);
3113 while (node != NULL) {
3115 node = uu_avl_next(avl_tree, node);
3116 uu_avl_remove(avl_tree, rmnode);
3117 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
3118 uu_list_insert(list, rmnode, idx2);
3121 for (node = uu_list_first(list); node != NULL;
3122 node = uu_list_next(list, node)) {
3123 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
3125 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
3126 uu_avl_insert(avl_tree, node, idx);
3129 uu_list_destroy(list);
3130 uu_list_pool_destroy(listpool);
3132 /* Print and free node nvlist memory */
3133 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
3136 zfs_free_sort_columns(sortcol);
3138 /* Clean up the AVL tree */
3139 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
3142 while ((node = uu_avl_walk_next(walk)) != NULL) {
3143 uu_avl_remove(cb.cb_avl, node);
3147 uu_avl_walk_end(walk);
3148 uu_avl_destroy(avl_tree);
3149 uu_avl_pool_destroy(avl_pool);
3155 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3156 * [-t type[,...]] [filesystem|volume|snapshot] ...
3158 * -H Scripted mode; elide headers and separate columns by tabs
3159 * -p Display values in parsable (literal) format.
3160 * -r Recurse over all children
3161 * -d Limit recursion by depth.
3162 * -o Control which fields to display.
3163 * -s Specify sort columns, descending order.
3164 * -S Specify sort columns, ascending order.
3165 * -t Control which object types to display.
3167 * When given no arguments, list all filesystems in the system.
3168 * Otherwise, list the specified datasets, optionally recursing down them if
3169 * '-r' is specified.
3171 typedef struct list_cbdata {
3173 boolean_t cb_literal;
3174 boolean_t cb_scripted;
3175 zprop_list_t *cb_proplist;
3179 * Given a list of columns to display, output appropriate headers for each one.
3182 print_header(list_cbdata_t *cb)
3184 zprop_list_t *pl = cb->cb_proplist;
3185 char headerbuf[ZFS_MAXPROPLEN];
3188 boolean_t first = B_TRUE;
3189 boolean_t right_justify;
3191 for (; pl != NULL; pl = pl->pl_next) {
3198 right_justify = B_FALSE;
3199 if (pl->pl_prop != ZPROP_INVAL) {
3200 header = zfs_prop_column_name(pl->pl_prop);
3201 right_justify = zfs_prop_align_right(pl->pl_prop);
3203 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3204 headerbuf[i] = toupper(pl->pl_user_prop[i]);
3205 headerbuf[i] = '\0';
3209 if (pl->pl_next == NULL && !right_justify)
3210 (void) printf("%s", header);
3211 else if (right_justify)
3212 (void) printf("%*s", (int)pl->pl_width, header);
3214 (void) printf("%-*s", (int)pl->pl_width, header);
3217 (void) printf("\n");
3221 * Given a dataset and a list of fields, print out all the properties according
3222 * to the described layout.
3225 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3227 zprop_list_t *pl = cb->cb_proplist;
3228 boolean_t first = B_TRUE;
3229 char property[ZFS_MAXPROPLEN];
3230 nvlist_t *userprops = zfs_get_user_props(zhp);
3233 boolean_t right_justify;
3235 for (; pl != NULL; pl = pl->pl_next) {
3237 if (cb->cb_scripted)
3238 (void) printf("\t");
3245 if (pl->pl_prop == ZFS_PROP_NAME) {
3246 (void) strlcpy(property, zfs_get_name(zhp),
3249 right_justify = zfs_prop_align_right(pl->pl_prop);
3250 } else if (pl->pl_prop != ZPROP_INVAL) {
3251 if (zfs_prop_get(zhp, pl->pl_prop, property,
3252 sizeof (property), NULL, NULL, 0,
3253 cb->cb_literal) != 0)
3257 right_justify = zfs_prop_align_right(pl->pl_prop);
3258 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3259 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3260 property, sizeof (property), cb->cb_literal) != 0)
3264 right_justify = B_TRUE;
3265 } else if (zfs_prop_written(pl->pl_user_prop)) {
3266 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3267 property, sizeof (property), cb->cb_literal) != 0)
3271 right_justify = B_TRUE;
3273 if (nvlist_lookup_nvlist(userprops,
3274 pl->pl_user_prop, &propval) != 0)
3277 verify(nvlist_lookup_string(propval,
3278 ZPROP_VALUE, &propstr) == 0);
3279 right_justify = B_FALSE;
3283 * If this is being called in scripted mode, or if this is the
3284 * last column and it is left-justified, don't include a width
3287 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3288 (void) printf("%s", propstr);
3289 else if (right_justify)
3290 (void) printf("%*s", (int)pl->pl_width, propstr);
3292 (void) printf("%-*s", (int)pl->pl_width, propstr);
3295 (void) printf("\n");
3299 * Generic callback function to list a dataset or snapshot.
3302 list_callback(zfs_handle_t *zhp, void *data)
3304 list_cbdata_t *cbp = data;
3306 if (cbp->cb_first) {
3307 if (!cbp->cb_scripted)
3309 cbp->cb_first = B_FALSE;
3312 print_dataset(zhp, cbp);
3318 zfs_do_list(int argc, char **argv)
3321 static char default_fields[] =
3322 "name,used,available,referenced,mountpoint";
3323 int types = ZFS_TYPE_DATASET;
3324 boolean_t types_specified = B_FALSE;
3325 char *fields = NULL;
3326 list_cbdata_t cb = { 0 };
3330 zfs_sort_column_t *sortcol = NULL;
3331 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3334 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3340 cb.cb_literal = B_TRUE;
3341 flags |= ZFS_ITER_LITERAL_PROPS;
3344 limit = parse_depth(optarg, &flags);
3347 flags |= ZFS_ITER_RECURSE;
3350 cb.cb_scripted = B_TRUE;
3353 if (zfs_add_sort_column(&sortcol, optarg,
3355 (void) fprintf(stderr,
3356 gettext("invalid property '%s'\n"), optarg);
3361 if (zfs_add_sort_column(&sortcol, optarg,
3363 (void) fprintf(stderr,
3364 gettext("invalid property '%s'\n"), optarg);
3370 types_specified = B_TRUE;
3371 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3372 while (*optarg != '\0') {
3373 static char *type_subopts[] = { "filesystem",
3374 "volume", "snapshot", "snap", "bookmark",
3377 switch (getsubopt(&optarg, type_subopts,
3380 types |= ZFS_TYPE_FILESYSTEM;
3383 types |= ZFS_TYPE_VOLUME;
3387 types |= ZFS_TYPE_SNAPSHOT;
3390 types |= ZFS_TYPE_BOOKMARK;
3393 types = ZFS_TYPE_DATASET |
3397 (void) fprintf(stderr,
3398 gettext("invalid type '%s'\n"),
3405 (void) fprintf(stderr, gettext("missing argument for "
3406 "'%c' option\n"), optopt);
3410 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3420 fields = default_fields;
3423 * If we are only going to list snapshot names and sort by name,
3424 * then we can use faster version.
3426 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3427 flags |= ZFS_ITER_SIMPLE;
3430 * If "-o space" and no types were specified, don't display snapshots.
3432 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3433 types &= ~ZFS_TYPE_SNAPSHOT;
3436 * Handle users who want to list all snapshots of the current
3437 * dataset (ex. 'zfs list -t snapshot <dataset>').
3439 if (types == ZFS_TYPE_SNAPSHOT && argc > 0 &&
3440 (flags & ZFS_ITER_RECURSE) == 0 && limit == 0) {
3441 flags |= (ZFS_ITER_DEPTH_LIMIT | ZFS_ITER_RECURSE);
3446 * If the user specifies '-o all', the zprop_get_list() doesn't
3447 * normally include the name of the dataset. For 'zfs list', we always
3448 * want this property to be first.
3450 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3454 cb.cb_first = B_TRUE;
3456 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3457 limit, list_callback, &cb);
3459 zprop_free_list(cb.cb_proplist);
3460 zfs_free_sort_columns(sortcol);
3462 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3463 (void) fprintf(stderr, gettext("no datasets available\n"));
3469 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3470 * zfs rename [-f] -p <fs | vol> <fs | vol>
3471 * zfs rename -r <snap> <snap>
3473 * Renames the given dataset to another of the same type.
3475 * The '-p' flag creates all the non-existing ancestors of the target first.
3479 zfs_do_rename(int argc, char **argv)
3484 boolean_t recurse = B_FALSE;
3485 boolean_t parents = B_FALSE;
3486 boolean_t force_unmount = B_FALSE;
3489 while ((c = getopt(argc, argv, "prf")) != -1) {
3498 force_unmount = B_TRUE;
3502 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3511 /* check number of arguments */
3513 (void) fprintf(stderr, gettext("missing source dataset "
3518 (void) fprintf(stderr, gettext("missing target dataset "
3523 (void) fprintf(stderr, gettext("too many arguments\n"));
3527 if (recurse && parents) {
3528 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3533 if (recurse && strchr(argv[0], '@') == 0) {
3534 (void) fprintf(stderr, gettext("source dataset for recursive "
3535 "rename must be a snapshot\n"));
3539 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3540 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3543 /* If we were asked and the name looks good, try to create ancestors. */
3544 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3545 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3550 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3559 * Promotes the given clone fs to be the parent
3563 zfs_do_promote(int argc, char **argv)
3569 if (argc > 1 && argv[1][0] == '-') {
3570 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3575 /* check number of arguments */
3577 (void) fprintf(stderr, gettext("missing clone filesystem"
3582 (void) fprintf(stderr, gettext("too many arguments\n"));
3586 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3590 ret = (zfs_promote(zhp) != 0);
3598 * zfs rollback [-rRf] <snapshot>
3600 * -r Delete any intervening snapshots before doing rollback
3601 * -R Delete any snapshots and their clones
3602 * -f ignored for backwards compatibility
3604 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3605 * since then and making it the active dataset. If more recent snapshots exist,
3606 * the command will complain unless the '-r' flag is given.
3608 typedef struct rollback_cbdata {
3610 uint8_t cb_younger_ds_printed;
3615 boolean_t cb_recurse;
3616 } rollback_cbdata_t;
3619 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3621 rollback_cbdata_t *cbp = data;
3623 if (cbp->cb_first && cbp->cb_recurse) {
3624 (void) fprintf(stderr, gettext("cannot rollback to "
3625 "'%s': clones of previous snapshots exist\n"),
3627 (void) fprintf(stderr, gettext("use '-R' to "
3628 "force deletion of the following clones and "
3634 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3642 * Report some snapshots/bookmarks more recent than the one specified.
3643 * Used when '-r' is not specified. We reuse this same callback for the
3644 * snapshot dependents - if 'cb_dependent' is set, then this is a
3645 * dependent and we should report it without checking the transaction group.
3648 rollback_check(zfs_handle_t *zhp, void *data)
3650 rollback_cbdata_t *cbp = data;
3652 * Max number of younger snapshots and/or bookmarks to display before
3653 * we stop the iteration.
3655 const uint8_t max_younger = 32;
3657 if (cbp->cb_doclones) {
3662 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3663 if (cbp->cb_first && !cbp->cb_recurse) {
3664 (void) fprintf(stderr, gettext("cannot "
3665 "rollback to '%s': more recent snapshots "
3666 "or bookmarks exist\n"),
3668 (void) fprintf(stderr, gettext("use '-r' to "
3669 "force deletion of the following "
3670 "snapshots and bookmarks:\n"));
3675 if (cbp->cb_recurse) {
3676 if (zfs_iter_dependents(zhp, B_TRUE,
3677 rollback_check_dependent, cbp) != 0) {
3682 (void) fprintf(stderr, "%s\n",
3684 cbp->cb_younger_ds_printed++;
3689 if (cbp->cb_younger_ds_printed == max_younger) {
3691 * This non-recursive rollback is going to fail due to the
3692 * presence of snapshots and/or bookmarks that are younger than
3693 * the rollback target.
3694 * We printed some of the offending objects, now we stop
3695 * zfs_iter_snapshot/bookmark iteration so we can fail fast and
3696 * avoid iterating over the rest of the younger objects
3698 (void) fprintf(stderr, gettext("Output limited to %d "
3699 "snapshots/bookmarks\n"), max_younger);
3706 zfs_do_rollback(int argc, char **argv)
3710 boolean_t force = B_FALSE;
3711 rollback_cbdata_t cb = { 0 };
3712 zfs_handle_t *zhp, *snap;
3713 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3715 uint64_t min_txg = 0;
3718 while ((c = getopt(argc, argv, "rRf")) != -1) {
3731 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3740 /* check number of arguments */
3742 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3746 (void) fprintf(stderr, gettext("too many arguments\n"));
3750 /* open the snapshot */
3751 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3754 /* open the parent dataset */
3755 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3756 verify((delim = strrchr(parentname, '@')) != NULL);
3758 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3764 * Check for more recent snapshots and/or clones based on the presence
3767 cb.cb_target = argv[0];
3768 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3769 cb.cb_first = B_TRUE;
3772 if (cb.cb_create > 0)
3773 min_txg = cb.cb_create;
3775 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb,
3778 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3781 if ((ret = cb.cb_error) != 0)
3785 * Rollback parent to the given snapshot.
3787 ret = zfs_rollback(zhp, snap, force);
3800 * zfs set property=value ... { fs | snap | vol } ...
3802 * Sets the given properties for all datasets specified on the command line.
3806 set_callback(zfs_handle_t *zhp, void *data)
3808 nvlist_t *props = data;
3810 if (zfs_prop_set_list(zhp, props) != 0) {
3811 switch (libzfs_errno(g_zfs)) {
3812 case EZFS_MOUNTFAILED:
3813 (void) fprintf(stderr, gettext("property may be set "
3814 "but unable to remount filesystem\n"));
3816 case EZFS_SHARENFSFAILED:
3817 (void) fprintf(stderr, gettext("property may be set "
3818 "but unable to reshare filesystem\n"));
3827 zfs_do_set(int argc, char **argv)
3829 nvlist_t *props = NULL;
3830 int ds_start = -1; /* argv idx of first dataset arg */
3834 /* check for options */
3835 if (argc > 1 && argv[1][0] == '-') {
3836 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3841 /* check number of arguments */
3843 (void) fprintf(stderr, gettext("missing arguments\n"));
3847 if (strchr(argv[1], '=') == NULL) {
3848 (void) fprintf(stderr, gettext("missing property=value "
3851 (void) fprintf(stderr, gettext("missing dataset "
3857 /* validate argument order: prop=val args followed by dataset args */
3858 for (i = 1; i < argc; i++) {
3859 if (strchr(argv[i], '=') != NULL) {
3861 /* out-of-order prop=val argument */
3862 (void) fprintf(stderr, gettext("invalid "
3863 "argument order\n"));
3866 } else if (ds_start < 0) {
3871 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3875 /* Populate a list of property settings */
3876 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3878 for (i = 1; i < ds_start; i++) {
3879 if (!parseprop(props, argv[i])) {
3885 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3886 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3893 typedef struct snap_cbdata {
3895 boolean_t sd_recursive;
3896 const char *sd_snapname;
3900 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3902 snap_cbdata_t *sd = arg;
3907 if (sd->sd_recursive &&
3908 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3913 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3916 fnvlist_add_boolean(sd->sd_nvl, name);
3919 if (sd->sd_recursive)
3920 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3926 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3928 * Creates a snapshot with the given name. While functionally equivalent to
3929 * 'zfs create', it is a separate command to differentiate intent.
3932 zfs_do_snapshot(int argc, char **argv)
3937 snap_cbdata_t sd = { 0 };
3938 boolean_t multiple_snaps = B_FALSE;
3940 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3942 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3946 while ((c = getopt(argc, argv, "ro:")) != -1) {
3949 if (!parseprop(props, optarg)) {
3950 nvlist_free(sd.sd_nvl);
3956 sd.sd_recursive = B_TRUE;
3957 multiple_snaps = B_TRUE;
3960 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3969 /* check number of arguments */
3971 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3976 multiple_snaps = B_TRUE;
3977 for (; argc > 0; argc--, argv++) {
3981 atp = strchr(argv[0], '@');
3985 sd.sd_snapname = atp + 1;
3986 zhp = zfs_open(g_zfs, argv[0],
3987 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3990 if (zfs_snapshot_cb(zhp, &sd) != 0)
3994 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3995 nvlist_free(sd.sd_nvl);
3997 if (ret != 0 && multiple_snaps)
3998 (void) fprintf(stderr, gettext("no snapshots were created\n"));
4002 nvlist_free(sd.sd_nvl);
4009 * Send a backup stream to stdout.
4012 zfs_do_send(int argc, char **argv)
4014 char *fromname = NULL;
4015 char *toname = NULL;
4016 char *resume_token = NULL;
4019 sendflags_t flags = { 0 };
4021 nvlist_t *dbgnv = NULL;
4022 boolean_t extraverbose = B_FALSE;
4024 struct option long_options[] = {
4025 {"replicate", no_argument, NULL, 'R'},
4026 {"props", no_argument, NULL, 'p'},
4027 {"parsable", no_argument, NULL, 'P'},
4028 {"dedup", no_argument, NULL, 'D'},
4029 {"verbose", no_argument, NULL, 'v'},
4030 {"dryrun", no_argument, NULL, 'n'},
4031 {"large-block", no_argument, NULL, 'L'},
4032 {"embed", no_argument, NULL, 'e'},
4033 {"resume", required_argument, NULL, 't'},
4034 {"compressed", no_argument, NULL, 'c'},
4035 {"raw", no_argument, NULL, 'w'},
4036 {"backup", no_argument, NULL, 'b'},
4037 {"holds", no_argument, NULL, 'h'},
4042 while ((c = getopt_long(argc, argv, ":i:I:RDpvnPLeht:cwb", long_options,
4054 flags.doall = B_TRUE;
4057 flags.replicate = B_TRUE;
4060 flags.props = B_TRUE;
4063 flags.backup = B_TRUE;
4066 flags.holds = B_TRUE;
4069 flags.parsable = B_TRUE;
4070 flags.verbose = B_TRUE;
4074 extraverbose = B_TRUE;
4075 flags.verbose = B_TRUE;
4076 flags.progress = B_TRUE;
4079 flags.dedup = B_TRUE;
4082 flags.dryrun = B_TRUE;
4085 flags.largeblock = B_TRUE;
4088 flags.embed_data = B_TRUE;
4091 resume_token = optarg;
4094 flags.compress = B_TRUE;
4098 flags.compress = B_TRUE;
4099 flags.embed_data = B_TRUE;
4100 flags.largeblock = B_TRUE;
4104 * If a parameter was not passed, optopt contains the
4105 * value that would normally lead us into the
4106 * appropriate case statement. If it's > 256, then this
4107 * must be a longopt and we should look at argv to get
4108 * the string. Otherwise it's just the character, so we
4109 * should use it directly.
4111 if (optopt <= UINT8_MAX) {
4112 (void) fprintf(stderr,
4113 gettext("missing argument for '%c' "
4114 "option\n"), optopt);
4116 (void) fprintf(stderr,
4117 gettext("missing argument for '%s' "
4118 "option\n"), argv[optind - 1]);
4126 * If an invalid flag was passed, optopt contains the
4127 * character if it was a short flag, or 0 if it was a
4131 (void) fprintf(stderr,
4132 gettext("invalid option '%c'\n"), optopt);
4134 (void) fprintf(stderr,
4135 gettext("invalid option '%s'\n"),
4146 if (resume_token != NULL) {
4147 if (fromname != NULL || flags.replicate || flags.props ||
4148 flags.backup || flags.dedup) {
4149 (void) fprintf(stderr,
4150 gettext("invalid flags combined with -t\n"));
4154 (void) fprintf(stderr, gettext("no additional "
4155 "arguments are permitted with -t\n"));
4160 (void) fprintf(stderr,
4161 gettext("missing snapshot argument\n"));
4165 (void) fprintf(stderr, gettext("too many arguments\n"));
4170 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
4171 (void) fprintf(stderr,
4172 gettext("Error: Stream can not be written to a terminal.\n"
4173 "You must redirect standard output.\n"));
4177 if (resume_token != NULL) {
4178 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
4183 * Special case sending a filesystem, or from a bookmark.
4185 if (strchr(argv[0], '@') == NULL ||
4186 (fromname && strchr(fromname, '#') != NULL)) {
4187 char frombuf[ZFS_MAX_DATASET_NAME_LEN];
4189 if (flags.replicate || flags.doall || flags.props ||
4190 flags.backup || flags.dedup || flags.holds ||
4191 (strchr(argv[0], '@') == NULL &&
4192 (flags.dryrun || flags.verbose || flags.progress))) {
4193 (void) fprintf(stderr, gettext("Error: "
4194 "Unsupported flag with filesystem or bookmark.\n"));
4198 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
4202 if (fromname != NULL &&
4203 (fromname[0] == '#' || fromname[0] == '@')) {
4205 * Incremental source name begins with # or @.
4206 * Default to same fs as target.
4208 (void) strlcpy(frombuf, argv[0], sizeof (frombuf));
4209 cp = strchr(frombuf, '@');
4212 (void) strlcat(frombuf, fromname, sizeof (frombuf));
4215 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, flags);
4220 cp = strchr(argv[0], '@');
4223 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4228 * If they specified the full path to the snapshot, chop off
4229 * everything except the short name of the snapshot, but special
4230 * case if they specify the origin.
4232 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
4233 char origin[ZFS_MAX_DATASET_NAME_LEN];
4236 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
4237 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
4239 if (strcmp(origin, fromname) == 0) {
4241 flags.fromorigin = B_TRUE;
4244 if (cp != fromname && strcmp(argv[0], fromname)) {
4245 (void) fprintf(stderr,
4246 gettext("incremental source must be "
4247 "in same filesystem\n"));
4251 if (strchr(fromname, '@') || strchr(fromname, '/')) {
4252 (void) fprintf(stderr,
4253 gettext("invalid incremental source\n"));
4259 if (flags.replicate && fromname == NULL)
4260 flags.doall = B_TRUE;
4262 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
4263 extraverbose ? &dbgnv : NULL);
4265 if (extraverbose && dbgnv != NULL) {
4267 * dump_nvlist prints to stdout, but that's been
4268 * redirected to a file. Make it print to stderr
4271 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
4272 dump_nvlist(dbgnv, 0);
4281 * Restore a backup stream from stdin.
4284 zfs_do_receive(int argc, char **argv)
4287 recvflags_t flags = { 0 };
4288 boolean_t abort_resumable = B_FALSE;
4291 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4295 while ((c = getopt(argc, argv, ":o:x:dehnuvFsA")) != -1) {
4298 if (!parseprop(props, optarg)) {
4304 if (!parsepropname(props, optarg)) {
4311 (void) fprintf(stderr, gettext("invalid option "
4312 "combination: -d and -e are mutually "
4316 flags.isprefix = B_TRUE;
4319 if (flags.isprefix) {
4320 (void) fprintf(stderr, gettext("invalid option "
4321 "combination: -d and -e are mutually "
4325 flags.istail = B_TRUE;
4328 flags.skipholds = B_TRUE;
4331 flags.dryrun = B_TRUE;
4334 flags.nomount = B_TRUE;
4337 flags.verbose = B_TRUE;
4340 flags.resumable = B_TRUE;
4343 flags.force = B_TRUE;
4346 abort_resumable = B_TRUE;
4349 (void) fprintf(stderr, gettext("missing argument for "
4350 "'%c' option\n"), optopt);
4354 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4363 /* zfs recv -e (use "tail" name) implies -d (remove dataset "head") */
4365 flags.isprefix = B_TRUE;
4367 /* check number of arguments */
4369 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4373 (void) fprintf(stderr, gettext("too many arguments\n"));
4377 if (abort_resumable) {
4378 if (flags.isprefix || flags.istail || flags.dryrun ||
4379 flags.resumable || flags.nomount) {
4380 (void) fprintf(stderr, gettext("invalid option\n"));
4384 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4385 (void) snprintf(namebuf, sizeof (namebuf),
4386 "%s/%%recv", argv[0]);
4388 if (zfs_dataset_exists(g_zfs, namebuf,
4389 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4390 zfs_handle_t *zhp = zfs_open(g_zfs,
4391 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4396 err = zfs_destroy(zhp, B_FALSE);
4399 zfs_handle_t *zhp = zfs_open(g_zfs,
4400 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4403 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4404 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4405 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4406 (void) fprintf(stderr,
4407 gettext("'%s' does not have any "
4408 "resumable receive state to abort\n"),
4414 err = zfs_destroy(zhp, B_FALSE);
4421 if (isatty(STDIN_FILENO)) {
4422 (void) fprintf(stderr,
4423 gettext("Error: Backup stream can not be read "
4424 "from a terminal.\n"
4425 "You must redirect standard input.\n"));
4429 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4436 * allow/unallow stuff
4438 /* copied from zfs/sys/dsl_deleg.h */
4439 #define ZFS_DELEG_PERM_CREATE "create"
4440 #define ZFS_DELEG_PERM_DESTROY "destroy"
4441 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4442 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4443 #define ZFS_DELEG_PERM_CLONE "clone"
4444 #define ZFS_DELEG_PERM_PROMOTE "promote"
4445 #define ZFS_DELEG_PERM_RENAME "rename"
4446 #define ZFS_DELEG_PERM_MOUNT "mount"
4447 #define ZFS_DELEG_PERM_SHARE "share"
4448 #define ZFS_DELEG_PERM_SEND "send"
4449 #define ZFS_DELEG_PERM_RECEIVE "receive"
4450 #define ZFS_DELEG_PERM_ALLOW "allow"
4451 #define ZFS_DELEG_PERM_USERPROP "userprop"
4452 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4453 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4454 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4455 #define ZFS_DELEG_PERM_USERUSED "userused"
4456 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4457 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4458 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4459 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4460 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4462 #define ZFS_DELEG_PERM_HOLD "hold"
4463 #define ZFS_DELEG_PERM_RELEASE "release"
4464 #define ZFS_DELEG_PERM_DIFF "diff"
4465 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4466 #define ZFS_DELEG_PERM_REMAP "remap"
4467 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
4468 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
4470 #define ZFS_DELEG_PERM_PROJECTUSED "projectused"
4471 #define ZFS_DELEG_PERM_PROJECTQUOTA "projectquota"
4472 #define ZFS_DELEG_PERM_PROJECTOBJUSED "projectobjused"
4473 #define ZFS_DELEG_PERM_PROJECTOBJQUOTA "projectobjquota"
4475 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4477 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4478 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4479 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4480 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4481 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4482 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4483 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4484 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4485 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4486 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4487 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4488 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4489 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4490 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4491 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4492 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4493 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4494 { ZFS_DELEG_PERM_REMAP, ZFS_DELEG_NOTE_REMAP },
4495 { ZFS_DELEG_PERM_LOAD_KEY, ZFS_DELEG_NOTE_LOAD_KEY },
4496 { ZFS_DELEG_PERM_CHANGE_KEY, ZFS_DELEG_NOTE_CHANGE_KEY },
4498 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4499 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4500 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4501 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4502 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4503 { ZFS_DELEG_PERM_USEROBJQUOTA, ZFS_DELEG_NOTE_USEROBJQUOTA },
4504 { ZFS_DELEG_PERM_USEROBJUSED, ZFS_DELEG_NOTE_USEROBJUSED },
4505 { ZFS_DELEG_PERM_GROUPOBJQUOTA, ZFS_DELEG_NOTE_GROUPOBJQUOTA },
4506 { ZFS_DELEG_PERM_GROUPOBJUSED, ZFS_DELEG_NOTE_GROUPOBJUSED },
4507 { ZFS_DELEG_PERM_PROJECTUSED, ZFS_DELEG_NOTE_PROJECTUSED },
4508 { ZFS_DELEG_PERM_PROJECTQUOTA, ZFS_DELEG_NOTE_PROJECTQUOTA },
4509 { ZFS_DELEG_PERM_PROJECTOBJUSED, ZFS_DELEG_NOTE_PROJECTOBJUSED },
4510 { ZFS_DELEG_PERM_PROJECTOBJQUOTA, ZFS_DELEG_NOTE_PROJECTOBJQUOTA },
4511 { NULL, ZFS_DELEG_NOTE_NONE }
4514 /* permission structure */
4515 typedef struct deleg_perm {
4516 zfs_deleg_who_type_t dp_who_type;
4517 const char *dp_name;
4519 boolean_t dp_descend;
4523 typedef struct deleg_perm_node {
4524 deleg_perm_t dpn_perm;
4526 uu_avl_node_t dpn_avl_node;
4527 } deleg_perm_node_t;
4529 typedef struct fs_perm fs_perm_t;
4531 /* permissions set */
4532 typedef struct who_perm {
4533 zfs_deleg_who_type_t who_type;
4534 const char *who_name; /* id */
4535 char who_ug_name[256]; /* user/group name */
4536 fs_perm_t *who_fsperm; /* uplink */
4538 uu_avl_t *who_deleg_perm_avl; /* permissions */
4542 typedef struct who_perm_node {
4543 who_perm_t who_perm;
4544 uu_avl_node_t who_avl_node;
4547 typedef struct fs_perm_set fs_perm_set_t;
4548 /* fs permissions */
4550 const char *fsp_name;
4552 uu_avl_t *fsp_sc_avl; /* sets,create */
4553 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4555 fs_perm_set_t *fsp_set; /* uplink */
4559 typedef struct fs_perm_node {
4560 fs_perm_t fspn_fsperm;
4563 uu_list_node_t fspn_list_node;
4566 /* top level structure */
4567 struct fs_perm_set {
4568 uu_list_pool_t *fsps_list_pool;
4569 uu_list_t *fsps_list; /* list of fs_perms */
4571 uu_avl_pool_t *fsps_named_set_avl_pool;
4572 uu_avl_pool_t *fsps_who_perm_avl_pool;
4573 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4576 static inline const char *
4577 deleg_perm_type(zfs_deleg_note_t note)
4583 case ZFS_DELEG_NOTE_GROUPQUOTA:
4584 case ZFS_DELEG_NOTE_GROUPUSED:
4585 case ZFS_DELEG_NOTE_USERPROP:
4586 case ZFS_DELEG_NOTE_USERQUOTA:
4587 case ZFS_DELEG_NOTE_USERUSED:
4588 case ZFS_DELEG_NOTE_USEROBJQUOTA:
4589 case ZFS_DELEG_NOTE_USEROBJUSED:
4590 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
4591 case ZFS_DELEG_NOTE_GROUPOBJUSED:
4592 case ZFS_DELEG_NOTE_PROJECTUSED:
4593 case ZFS_DELEG_NOTE_PROJECTQUOTA:
4594 case ZFS_DELEG_NOTE_PROJECTOBJUSED:
4595 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
4597 return (gettext("other"));
4599 return (gettext("subcommand"));
4604 who_type2weight(zfs_deleg_who_type_t who_type)
4608 case ZFS_DELEG_NAMED_SET_SETS:
4609 case ZFS_DELEG_NAMED_SET:
4612 case ZFS_DELEG_CREATE_SETS:
4613 case ZFS_DELEG_CREATE:
4616 case ZFS_DELEG_USER_SETS:
4617 case ZFS_DELEG_USER:
4620 case ZFS_DELEG_GROUP_SETS:
4621 case ZFS_DELEG_GROUP:
4624 case ZFS_DELEG_EVERYONE_SETS:
4625 case ZFS_DELEG_EVERYONE:
4637 who_perm_compare(const void *larg, const void *rarg, void *unused)
4639 const who_perm_node_t *l = larg;
4640 const who_perm_node_t *r = rarg;
4641 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4642 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4643 int lweight = who_type2weight(ltype);
4644 int rweight = who_type2weight(rtype);
4645 int res = lweight - rweight;
4647 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4648 ZFS_MAX_DELEG_NAME-1);
4660 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4662 const deleg_perm_node_t *l = larg;
4663 const deleg_perm_node_t *r = rarg;
4664 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4665 ZFS_MAX_DELEG_NAME-1);
4677 fs_perm_set_init(fs_perm_set_t *fspset)
4679 bzero(fspset, sizeof (fs_perm_set_t));
4681 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4682 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4683 NULL, UU_DEFAULT)) == NULL)
4685 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4686 UU_DEFAULT)) == NULL)
4689 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4690 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4691 who_perm_node_t, who_avl_node), who_perm_compare,
4692 UU_DEFAULT)) == NULL)
4695 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4696 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4697 who_perm_node_t, who_avl_node), who_perm_compare,
4698 UU_DEFAULT)) == NULL)
4701 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4702 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4703 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4708 static inline void fs_perm_fini(fs_perm_t *);
4709 static inline void who_perm_fini(who_perm_t *);
4712 fs_perm_set_fini(fs_perm_set_t *fspset)
4714 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4716 while (node != NULL) {
4717 fs_perm_node_t *next_node =
4718 uu_list_next(fspset->fsps_list, node);
4719 fs_perm_t *fsperm = &node->fspn_fsperm;
4720 fs_perm_fini(fsperm);
4721 uu_list_remove(fspset->fsps_list, node);
4726 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4727 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4728 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4732 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4735 deleg_perm->dp_who_type = type;
4736 deleg_perm->dp_name = name;
4740 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4741 zfs_deleg_who_type_t type, const char *name)
4743 uu_avl_pool_t *pool;
4744 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4746 bzero(who_perm, sizeof (who_perm_t));
4748 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4749 UU_DEFAULT)) == NULL)
4752 who_perm->who_type = type;
4753 who_perm->who_name = name;
4754 who_perm->who_fsperm = fsperm;
4758 who_perm_fini(who_perm_t *who_perm)
4760 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4762 while (node != NULL) {
4763 deleg_perm_node_t *next_node =
4764 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4766 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4771 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4775 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4777 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4778 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4780 bzero(fsperm, sizeof (fs_perm_t));
4782 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4786 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4790 fsperm->fsp_set = fspset;
4791 fsperm->fsp_name = fsname;
4795 fs_perm_fini(fs_perm_t *fsperm)
4797 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4798 while (node != NULL) {
4799 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4801 who_perm_t *who_perm = &node->who_perm;
4802 who_perm_fini(who_perm);
4803 uu_avl_remove(fsperm->fsp_sc_avl, node);
4808 node = uu_avl_first(fsperm->fsp_uge_avl);
4809 while (node != NULL) {
4810 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4812 who_perm_t *who_perm = &node->who_perm;
4813 who_perm_fini(who_perm);
4814 uu_avl_remove(fsperm->fsp_uge_avl, node);
4819 uu_avl_destroy(fsperm->fsp_sc_avl);
4820 uu_avl_destroy(fsperm->fsp_uge_avl);
4824 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4825 zfs_deleg_who_type_t who_type, const char *name, char locality)
4827 uu_avl_index_t idx = 0;
4829 deleg_perm_node_t *found_node = NULL;
4830 deleg_perm_t *deleg_perm = &node->dpn_perm;
4832 deleg_perm_init(deleg_perm, who_type, name);
4834 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4836 uu_avl_insert(avl, node, idx);
4839 deleg_perm = &node->dpn_perm;
4844 case ZFS_DELEG_LOCAL:
4845 deleg_perm->dp_local = B_TRUE;
4847 case ZFS_DELEG_DESCENDENT:
4848 deleg_perm->dp_descend = B_TRUE;
4853 assert(B_FALSE); /* invalid locality */
4858 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4860 nvpair_t *nvp = NULL;
4861 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4862 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4863 zfs_deleg_who_type_t who_type = who_perm->who_type;
4865 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4866 const char *name = nvpair_name(nvp);
4867 data_type_t type = nvpair_type(nvp);
4868 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4869 deleg_perm_node_t *node =
4870 safe_malloc(sizeof (deleg_perm_node_t));
4872 VERIFY(type == DATA_TYPE_BOOLEAN);
4874 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4875 set_deleg_perm_node(avl, node, who_type, name, locality);
4882 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4884 nvpair_t *nvp = NULL;
4885 fs_perm_set_t *fspset = fsperm->fsp_set;
4887 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4888 nvlist_t *nvl2 = NULL;
4889 const char *name = nvpair_name(nvp);
4890 uu_avl_t *avl = NULL;
4891 uu_avl_pool_t *avl_pool = NULL;
4892 zfs_deleg_who_type_t perm_type = name[0];
4893 char perm_locality = name[1];
4894 const char *perm_name = name + 3;
4895 boolean_t is_set = B_TRUE;
4896 who_perm_t *who_perm = NULL;
4898 assert('$' == name[2]);
4900 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4903 switch (perm_type) {
4904 case ZFS_DELEG_CREATE:
4905 case ZFS_DELEG_CREATE_SETS:
4906 case ZFS_DELEG_NAMED_SET:
4907 case ZFS_DELEG_NAMED_SET_SETS:
4908 avl_pool = fspset->fsps_named_set_avl_pool;
4909 avl = fsperm->fsp_sc_avl;
4911 case ZFS_DELEG_USER:
4912 case ZFS_DELEG_USER_SETS:
4913 case ZFS_DELEG_GROUP:
4914 case ZFS_DELEG_GROUP_SETS:
4915 case ZFS_DELEG_EVERYONE:
4916 case ZFS_DELEG_EVERYONE_SETS:
4917 avl_pool = fspset->fsps_who_perm_avl_pool;
4918 avl = fsperm->fsp_uge_avl;
4922 assert(!"unhandled zfs_deleg_who_type_t");
4926 who_perm_node_t *found_node = NULL;
4927 who_perm_node_t *node = safe_malloc(
4928 sizeof (who_perm_node_t));
4929 who_perm = &node->who_perm;
4930 uu_avl_index_t idx = 0;
4932 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4933 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4935 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4937 if (avl == fsperm->fsp_uge_avl) {
4939 struct passwd *p = NULL;
4940 struct group *g = NULL;
4941 const char *nice_name = NULL;
4943 switch (perm_type) {
4944 case ZFS_DELEG_USER_SETS:
4945 case ZFS_DELEG_USER:
4946 rid = atoi(perm_name);
4949 nice_name = p->pw_name;
4951 case ZFS_DELEG_GROUP_SETS:
4952 case ZFS_DELEG_GROUP:
4953 rid = atoi(perm_name);
4956 nice_name = g->gr_name;
4963 if (nice_name != NULL)
4965 node->who_perm.who_ug_name,
4969 uu_avl_insert(avl, node, idx);
4972 who_perm = &node->who_perm;
4975 VERIFY3P(who_perm, !=, NULL);
4976 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4983 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4985 nvpair_t *nvp = NULL;
4986 uu_avl_index_t idx = 0;
4988 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4989 nvlist_t *nvl2 = NULL;
4990 const char *fsname = nvpair_name(nvp);
4991 data_type_t type = nvpair_type(nvp);
4992 fs_perm_t *fsperm = NULL;
4993 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4997 fsperm = &node->fspn_fsperm;
4999 VERIFY(DATA_TYPE_NVLIST == type);
5001 uu_list_node_init(node, &node->fspn_list_node,
5002 fspset->fsps_list_pool);
5004 idx = uu_list_numnodes(fspset->fsps_list);
5005 fs_perm_init(fsperm, fspset, fsname);
5007 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
5010 (void) parse_fs_perm(fsperm, nvl2);
5012 uu_list_insert(fspset->fsps_list, node, idx);
5018 static inline const char *
5019 deleg_perm_comment(zfs_deleg_note_t note)
5021 const char *str = "";
5026 case ZFS_DELEG_NOTE_ALLOW:
5027 str = gettext("Must also have the permission that is being"
5028 "\n\t\t\t\tallowed");
5030 case ZFS_DELEG_NOTE_CLONE:
5031 str = gettext("Must also have the 'create' ability and 'mount'"
5032 "\n\t\t\t\tability in the origin file system");
5034 case ZFS_DELEG_NOTE_CREATE:
5035 str = gettext("Must also have the 'mount' ability");
5037 case ZFS_DELEG_NOTE_DESTROY:
5038 str = gettext("Must also have the 'mount' ability");
5040 case ZFS_DELEG_NOTE_DIFF:
5041 str = gettext("Allows lookup of paths within a dataset;"
5042 "\n\t\t\t\tgiven an object number. Ordinary users need this"
5043 "\n\t\t\t\tin order to use zfs diff");
5045 case ZFS_DELEG_NOTE_HOLD:
5046 str = gettext("Allows adding a user hold to a snapshot");
5048 case ZFS_DELEG_NOTE_MOUNT:
5049 str = gettext("Allows mount/umount of ZFS datasets");
5051 case ZFS_DELEG_NOTE_PROMOTE:
5052 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
5053 " 'promote' ability in the origin file system");
5055 case ZFS_DELEG_NOTE_RECEIVE:
5056 str = gettext("Must also have the 'mount' and 'create'"
5059 case ZFS_DELEG_NOTE_RELEASE:
5060 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
5061 "might destroy the snapshot");
5063 case ZFS_DELEG_NOTE_RENAME:
5064 str = gettext("Must also have the 'mount' and 'create'"
5065 "\n\t\t\t\tability in the new parent");
5067 case ZFS_DELEG_NOTE_ROLLBACK:
5070 case ZFS_DELEG_NOTE_SEND:
5073 case ZFS_DELEG_NOTE_SHARE:
5074 str = gettext("Allows sharing file systems over NFS or SMB"
5075 "\n\t\t\t\tprotocols");
5077 case ZFS_DELEG_NOTE_SNAPSHOT:
5080 case ZFS_DELEG_NOTE_LOAD_KEY:
5081 str = gettext("Allows loading or unloading an encryption key");
5083 case ZFS_DELEG_NOTE_CHANGE_KEY:
5084 str = gettext("Allows changing or adding an encryption key");
5087 * case ZFS_DELEG_NOTE_VSCAN:
5088 * str = gettext("");
5092 case ZFS_DELEG_NOTE_GROUPQUOTA:
5093 str = gettext("Allows accessing any groupquota@... property");
5095 case ZFS_DELEG_NOTE_GROUPUSED:
5096 str = gettext("Allows reading any groupused@... property");
5098 case ZFS_DELEG_NOTE_USERPROP:
5099 str = gettext("Allows changing any user property");
5101 case ZFS_DELEG_NOTE_USERQUOTA:
5102 str = gettext("Allows accessing any userquota@... property");
5104 case ZFS_DELEG_NOTE_USERUSED:
5105 str = gettext("Allows reading any userused@... property");
5107 case ZFS_DELEG_NOTE_USEROBJQUOTA:
5108 str = gettext("Allows accessing any userobjquota@... property");
5110 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
5111 str = gettext("Allows accessing any \n\t\t\t\t"
5112 "groupobjquota@... property");
5114 case ZFS_DELEG_NOTE_GROUPOBJUSED:
5115 str = gettext("Allows reading any groupobjused@... property");
5117 case ZFS_DELEG_NOTE_USEROBJUSED:
5118 str = gettext("Allows reading any userobjused@... property");
5120 case ZFS_DELEG_NOTE_PROJECTQUOTA:
5121 str = gettext("Allows accessing any projectquota@... property");
5123 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
5124 str = gettext("Allows accessing any \n\t\t\t\t"
5125 "projectobjquota@... property");
5127 case ZFS_DELEG_NOTE_PROJECTUSED:
5128 str = gettext("Allows reading any projectused@... property");
5130 case ZFS_DELEG_NOTE_PROJECTOBJUSED:
5131 str = gettext("Allows accessing any \n\t\t\t\t"
5132 "projectobjused@... property");
5150 boolean_t recursive; /* unallow only */
5151 boolean_t prt_usage;
5153 boolean_t prt_perms;
5156 const char *dataset;
5160 prop_cmp(const void *a, const void *b)
5162 const char *str1 = *(const char **)a;
5163 const char *str2 = *(const char **)b;
5164 return (strcmp(str1, str2));
5168 allow_usage(boolean_t un, boolean_t requested, const char *msg)
5170 const char *opt_desc[] = {
5171 "-h", gettext("show this help message and exit"),
5172 "-l", gettext("set permission locally"),
5173 "-d", gettext("set permission for descents"),
5174 "-u", gettext("set permission for user"),
5175 "-g", gettext("set permission for group"),
5176 "-e", gettext("set permission for everyone"),
5177 "-c", gettext("set create time permission"),
5178 "-s", gettext("define permission set"),
5180 "-r", gettext("remove permissions recursively"),
5182 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
5183 size_t allow_size = unallow_size - 2;
5184 const char *props[ZFS_NUM_PROPS];
5187 FILE *fp = requested ? stdout : stderr;
5188 zprop_desc_t *pdtbl = zfs_prop_get_table();
5189 const char *fmt = gettext("%-16s %-14s\t%s\n");
5191 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
5193 (void) fprintf(fp, gettext("Options:\n"));
5194 for (i = 0; i < (un ? unallow_size : allow_size); i += 2) {
5195 const char *opt = opt_desc[i];
5196 const char *optdsc = opt_desc[i + 1];
5197 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
5200 (void) fprintf(fp, gettext("\nThe following permissions are "
5202 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
5204 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
5205 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
5206 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
5207 const char *perm_type = deleg_perm_type(perm_note);
5208 const char *perm_comment = deleg_perm_comment(perm_note);
5209 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
5212 for (i = 0; i < ZFS_NUM_PROPS; i++) {
5213 zprop_desc_t *pd = &pdtbl[i];
5214 if (pd->pd_visible != B_TRUE)
5217 if (pd->pd_attr == PROP_READONLY)
5220 props[count++] = pd->pd_name;
5222 props[count] = NULL;
5224 qsort(props, count, sizeof (char *), prop_cmp);
5226 for (i = 0; i < count; i++)
5227 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
5230 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
5232 exit(requested ? 0 : 2);
5235 static inline const char *
5236 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
5239 if (un && argc == expected_argc - 1)
5241 else if (argc == expected_argc)
5242 *permsp = argv[argc - 2];
5244 allow_usage(un, B_FALSE,
5245 gettext("wrong number of parameters\n"));
5247 return (argv[argc - 1]);
5251 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
5253 int uge_sum = opts->user + opts->group + opts->everyone;
5254 int csuge_sum = opts->create + opts->set + uge_sum;
5255 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
5256 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
5259 allow_usage(un, B_FALSE,
5260 gettext("-u, -g, and -e are mutually exclusive\n"));
5262 if (opts->prt_usage) {
5263 if (argc == 0 && all_sum == 0)
5264 allow_usage(un, B_TRUE, NULL);
5271 allow_usage(un, B_FALSE,
5272 gettext("invalid options combined with -s\n"));
5274 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5275 if (argv[0][0] != '@')
5276 allow_usage(un, B_FALSE,
5277 gettext("invalid set name: missing '@' prefix\n"));
5278 opts->who = argv[0];
5279 } else if (opts->create) {
5280 if (ldcsuge_sum > 1)
5281 allow_usage(un, B_FALSE,
5282 gettext("invalid options combined with -c\n"));
5283 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5284 } else if (opts->everyone) {
5286 allow_usage(un, B_FALSE,
5287 gettext("invalid options combined with -e\n"));
5288 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5289 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
5291 opts->everyone = B_TRUE;
5294 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5295 } else if (argc == 1 && !un) {
5296 opts->prt_perms = B_TRUE;
5297 opts->dataset = argv[argc-1];
5299 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5300 opts->who = argv[0];
5303 if (!opts->local && !opts->descend) {
5304 opts->local = B_TRUE;
5305 opts->descend = B_TRUE;
5310 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
5311 const char *who, char *perms, nvlist_t *top_nvl)
5314 char ld[2] = { '\0', '\0' };
5315 char who_buf[MAXNAMELEN + 32];
5316 char base_type = '\0';
5317 char set_type = '\0';
5318 nvlist_t *base_nvl = NULL;
5319 nvlist_t *set_nvl = NULL;
5322 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
5324 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
5328 case ZFS_DELEG_NAMED_SET_SETS:
5329 case ZFS_DELEG_NAMED_SET:
5330 set_type = ZFS_DELEG_NAMED_SET_SETS;
5331 base_type = ZFS_DELEG_NAMED_SET;
5332 ld[0] = ZFS_DELEG_NA;
5334 case ZFS_DELEG_CREATE_SETS:
5335 case ZFS_DELEG_CREATE:
5336 set_type = ZFS_DELEG_CREATE_SETS;
5337 base_type = ZFS_DELEG_CREATE;
5338 ld[0] = ZFS_DELEG_NA;
5340 case ZFS_DELEG_USER_SETS:
5341 case ZFS_DELEG_USER:
5342 set_type = ZFS_DELEG_USER_SETS;
5343 base_type = ZFS_DELEG_USER;
5345 ld[0] = ZFS_DELEG_LOCAL;
5347 ld[1] = ZFS_DELEG_DESCENDENT;
5349 case ZFS_DELEG_GROUP_SETS:
5350 case ZFS_DELEG_GROUP:
5351 set_type = ZFS_DELEG_GROUP_SETS;
5352 base_type = ZFS_DELEG_GROUP;
5354 ld[0] = ZFS_DELEG_LOCAL;
5356 ld[1] = ZFS_DELEG_DESCENDENT;
5358 case ZFS_DELEG_EVERYONE_SETS:
5359 case ZFS_DELEG_EVERYONE:
5360 set_type = ZFS_DELEG_EVERYONE_SETS;
5361 base_type = ZFS_DELEG_EVERYONE;
5363 ld[0] = ZFS_DELEG_LOCAL;
5365 ld[1] = ZFS_DELEG_DESCENDENT;
5369 assert(set_type != '\0' && base_type != '\0');
5372 if (perms != NULL) {
5374 char *end = curr + strlen(perms);
5376 while (curr < end) {
5377 char *delim = strchr(curr, ',');
5388 (void) nvlist_add_boolean(nvl, curr);
5394 for (i = 0; i < 2; i++) {
5395 char locality = ld[i];
5399 if (!nvlist_empty(base_nvl)) {
5401 (void) snprintf(who_buf,
5402 sizeof (who_buf), "%c%c$%s",
5403 base_type, locality, who);
5405 (void) snprintf(who_buf,
5406 sizeof (who_buf), "%c%c$",
5407 base_type, locality);
5409 (void) nvlist_add_nvlist(top_nvl, who_buf,
5414 if (!nvlist_empty(set_nvl)) {
5416 (void) snprintf(who_buf,
5417 sizeof (who_buf), "%c%c$%s",
5418 set_type, locality, who);
5420 (void) snprintf(who_buf,
5421 sizeof (who_buf), "%c%c$",
5422 set_type, locality);
5424 (void) nvlist_add_nvlist(top_nvl, who_buf,
5429 for (i = 0; i < 2; i++) {
5430 char locality = ld[i];
5435 (void) snprintf(who_buf, sizeof (who_buf),
5436 "%c%c$%s", base_type, locality, who);
5438 (void) snprintf(who_buf, sizeof (who_buf),
5439 "%c%c$", base_type, locality);
5440 (void) nvlist_add_boolean(top_nvl, who_buf);
5443 (void) snprintf(who_buf, sizeof (who_buf),
5444 "%c%c$%s", set_type, locality, who);
5446 (void) snprintf(who_buf, sizeof (who_buf),
5447 "%c%c$", set_type, locality);
5448 (void) nvlist_add_boolean(top_nvl, who_buf);
5454 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5456 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5460 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5461 opts->descend, opts->who, opts->perms, *nvlp);
5462 } else if (opts->create) {
5463 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5464 opts->descend, NULL, opts->perms, *nvlp);
5465 } else if (opts->everyone) {
5466 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5467 opts->descend, NULL, opts->perms, *nvlp);
5469 char *curr = opts->who;
5470 char *end = curr + strlen(curr);
5472 while (curr < end) {
5474 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5476 char *delim = strchr(curr, ',');
5479 struct passwd *p = NULL;
5480 struct group *g = NULL;
5488 rid = (uid_t)strtol(curr, &endch, 0);
5490 who_type = ZFS_DELEG_USER;
5499 (void) snprintf(errbuf, 256, gettext(
5500 "invalid user %s"), curr);
5501 allow_usage(un, B_TRUE, errbuf);
5503 } else if (opts->group) {
5504 who_type = ZFS_DELEG_GROUP;
5513 (void) snprintf(errbuf, 256, gettext(
5514 "invalid group %s"), curr);
5515 allow_usage(un, B_TRUE, errbuf);
5518 if (*endch != '\0') {
5525 if (*endch != '\0') {
5533 who_type = ZFS_DELEG_USER;
5535 } else if (g != NULL) {
5536 who_type = ZFS_DELEG_GROUP;
5539 (void) snprintf(errbuf, 256, gettext(
5540 "invalid user/group %s"), curr);
5541 allow_usage(un, B_TRUE, errbuf);
5545 (void) sprintf(id, "%u", rid);
5548 store_allow_perm(who_type, opts->local,
5549 opts->descend, who, opts->perms, *nvlp);
5558 print_set_creat_perms(uu_avl_t *who_avl)
5560 const char *sc_title[] = {
5561 gettext("Permission sets:\n"),
5562 gettext("Create time permissions:\n"),
5565 who_perm_node_t *who_node = NULL;
5566 int prev_weight = -1;
5568 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5569 who_node = uu_avl_next(who_avl, who_node)) {
5570 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5571 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5572 const char *who_name = who_node->who_perm.who_name;
5573 int weight = who_type2weight(who_type);
5574 boolean_t first = B_TRUE;
5575 deleg_perm_node_t *deleg_node;
5577 if (prev_weight != weight) {
5578 (void) printf("%s", sc_title[weight]);
5579 prev_weight = weight;
5582 if (who_name == NULL || strnlen(who_name, 1) == 0)
5583 (void) printf("\t");
5585 (void) printf("\t%s ", who_name);
5587 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5588 deleg_node = uu_avl_next(avl, deleg_node)) {
5591 deleg_node->dpn_perm.dp_name);
5594 (void) printf(",%s",
5595 deleg_node->dpn_perm.dp_name);
5598 (void) printf("\n");
5603 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5606 who_perm_node_t *who_node = NULL;
5607 boolean_t prt_title = B_TRUE;
5608 uu_avl_walk_t *walk;
5610 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5613 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5614 const char *who_name = who_node->who_perm.who_name;
5615 const char *nice_who_name = who_node->who_perm.who_ug_name;
5616 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5617 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5619 deleg_perm_node_t *deleg_node;
5620 boolean_t prt_who = B_TRUE;
5622 for (deleg_node = uu_avl_first(avl);
5624 deleg_node = uu_avl_next(avl, deleg_node)) {
5625 if (local != deleg_node->dpn_perm.dp_local ||
5626 descend != deleg_node->dpn_perm.dp_descend)
5630 const char *who = NULL;
5632 prt_title = B_FALSE;
5633 (void) printf("%s", title);
5637 case ZFS_DELEG_USER_SETS:
5638 case ZFS_DELEG_USER:
5639 who = gettext("user");
5641 who_name = nice_who_name;
5643 case ZFS_DELEG_GROUP_SETS:
5644 case ZFS_DELEG_GROUP:
5645 who = gettext("group");
5647 who_name = nice_who_name;
5649 case ZFS_DELEG_EVERYONE_SETS:
5650 case ZFS_DELEG_EVERYONE:
5651 who = gettext("everyone");
5656 assert(who != NULL);
5660 if (who_name == NULL)
5661 (void) printf("\t%s", who);
5663 (void) printf("\t%s %s", who, who_name);
5666 (void) printf("%c%s", delim,
5667 deleg_node->dpn_perm.dp_name);
5672 (void) printf("\n");
5675 uu_avl_walk_end(walk);
5679 print_fs_perms(fs_perm_set_t *fspset)
5681 fs_perm_node_t *node = NULL;
5682 char buf[MAXNAMELEN + 32];
5683 const char *dsname = buf;
5685 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5686 node = uu_list_next(fspset->fsps_list, node)) {
5687 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5688 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5691 (void) snprintf(buf, sizeof (buf),
5692 gettext("---- Permissions on %s "),
5693 node->fspn_fsperm.fsp_name);
5694 (void) printf("%s", dsname);
5695 left = 70 - strlen(buf);
5698 (void) printf("\n");
5700 print_set_creat_perms(sc_avl);
5701 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5702 gettext("Local permissions:\n"));
5703 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5704 gettext("Descendent permissions:\n"));
5705 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5706 gettext("Local+Descendent permissions:\n"));
5710 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5712 struct deleg_perms {
5718 set_deleg_perms(zfs_handle_t *zhp, void *data)
5720 struct deleg_perms *perms = (struct deleg_perms *)data;
5721 zfs_type_t zfs_type = zfs_get_type(zhp);
5723 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5726 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5730 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5733 nvlist_t *perm_nvl = NULL;
5734 nvlist_t *update_perm_nvl = NULL;
5737 struct allow_opts opts = { 0 };
5739 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5742 while ((c = getopt(argc, argv, optstr)) != -1) {
5745 opts.local = B_TRUE;
5748 opts.descend = B_TRUE;
5754 opts.group = B_TRUE;
5757 opts.everyone = B_TRUE;
5763 opts.create = B_TRUE;
5766 opts.recursive = B_TRUE;
5769 (void) fprintf(stderr, gettext("missing argument for "
5770 "'%c' option\n"), optopt);
5774 opts.prt_usage = B_TRUE;
5777 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5786 /* check arguments */
5787 parse_allow_args(argc, argv, un, &opts);
5789 /* try to open the dataset */
5790 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5791 ZFS_TYPE_VOLUME)) == NULL) {
5792 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5797 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5800 fs_perm_set_init(&fs_perm_set);
5801 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5802 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5807 print_fs_perms(&fs_perm_set);
5809 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5810 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5813 if (un && opts.recursive) {
5814 struct deleg_perms data = { un, update_perm_nvl };
5815 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5824 nvlist_free(perm_nvl);
5825 nvlist_free(update_perm_nvl);
5827 fs_perm_set_fini(&fs_perm_set);
5835 zfs_do_allow(int argc, char **argv)
5837 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5841 zfs_do_unallow(int argc, char **argv)
5843 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5847 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5852 boolean_t recursive = B_FALSE;
5853 const char *opts = holding ? "rt" : "r";
5857 while ((c = getopt(argc, argv, opts)) != -1) {
5863 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5872 /* check number of arguments */
5880 if (holding && tag[0] == '.') {
5881 /* tags starting with '.' are reserved for libzfs */
5882 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5886 for (i = 0; i < argc; ++i) {
5888 char parent[ZFS_MAX_DATASET_NAME_LEN];
5890 char *path = argv[i];
5892 delim = strchr(path, '@');
5893 if (delim == NULL) {
5894 (void) fprintf(stderr,
5895 gettext("'%s' is not a snapshot\n"), path);
5899 (void) strncpy(parent, path, delim - path);
5900 parent[delim - path] = '\0';
5902 zhp = zfs_open(g_zfs, parent,
5903 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5909 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5912 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5918 return (errors != 0);
5922 * zfs hold [-r] [-t] <tag> <snap> ...
5924 * -r Recursively hold
5926 * Apply a user-hold with the given tag to the list of snapshots.
5929 zfs_do_hold(int argc, char **argv)
5931 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5935 * zfs release [-r] <tag> <snap> ...
5937 * -r Recursively release
5939 * Release a user-hold with the given tag from the list of snapshots.
5942 zfs_do_release(int argc, char **argv)
5944 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5947 typedef struct holds_cbdata {
5948 boolean_t cb_recursive;
5949 const char *cb_snapname;
5951 size_t cb_max_namelen;
5952 size_t cb_max_taglen;
5955 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5956 #define DATETIME_BUF_LEN (32)
5961 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
5964 nvpair_t *nvp = NULL;
5965 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5969 for (i = 0; i < 3; i++) {
5970 col = gettext(hdr_cols[i]);
5972 (void) printf("%-*s ", i ? tagwidth : nwidth,
5975 (void) printf("%s\n", col);
5979 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5980 char *zname = nvpair_name(nvp);
5982 nvpair_t *nvp2 = NULL;
5983 (void) nvpair_value_nvlist(nvp, &nvl2);
5984 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5985 char tsbuf[DATETIME_BUF_LEN];
5986 char *tagname = nvpair_name(nvp2);
5991 (void) nvpair_value_uint64(nvp2, &val);
5993 (void) localtime_r(&time, &t);
5994 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5995 gettext(STRFTIME_FMT_STR), &t);
5998 (void) printf("%s\t%s\t%s\n", zname,
6001 (void) printf("%-*s %-*s %s\n", nwidth,
6002 zname, tagwidth, tagname, tsbuf);
6009 * Generic callback function to list a dataset or snapshot.
6012 holds_callback(zfs_handle_t *zhp, void *data)
6014 holds_cbdata_t *cbp = data;
6015 nvlist_t *top_nvl = *cbp->cb_nvlp;
6016 nvlist_t *nvl = NULL;
6017 nvpair_t *nvp = NULL;
6018 const char *zname = zfs_get_name(zhp);
6019 size_t znamelen = strlen(zname);
6021 if (cbp->cb_recursive) {
6022 const char *snapname;
6023 char *delim = strchr(zname, '@');
6027 snapname = delim + 1;
6028 if (strcmp(cbp->cb_snapname, snapname))
6032 if (zfs_get_holds(zhp, &nvl) != 0)
6035 if (znamelen > cbp->cb_max_namelen)
6036 cbp->cb_max_namelen = znamelen;
6038 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
6039 const char *tag = nvpair_name(nvp);
6040 size_t taglen = strlen(tag);
6041 if (taglen > cbp->cb_max_taglen)
6042 cbp->cb_max_taglen = taglen;
6045 return (nvlist_add_nvlist(top_nvl, zname, nvl));
6049 * zfs holds [-rH] <snap> ...
6051 * -r Lists holds that are set on the named snapshots recursively.
6052 * -H Scripted mode; elide headers and separate columns by tabs.
6055 zfs_do_holds(int argc, char **argv)
6060 boolean_t scripted = B_FALSE;
6061 boolean_t recursive = B_FALSE;
6062 const char *opts = "rH";
6065 int types = ZFS_TYPE_SNAPSHOT;
6066 holds_cbdata_t cb = { 0 };
6073 while ((c = getopt(argc, argv, opts)) != -1) {
6082 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6089 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
6090 flags |= ZFS_ITER_RECURSE;
6096 /* check number of arguments */
6100 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
6103 for (i = 0; i < argc; ++i) {
6104 char *snapshot = argv[i];
6106 const char *snapname;
6108 delim = strchr(snapshot, '@');
6109 if (delim == NULL) {
6110 (void) fprintf(stderr,
6111 gettext("'%s' is not a snapshot\n"), snapshot);
6115 snapname = delim + 1;
6117 snapshot[delim - snapshot] = '\0';
6119 cb.cb_recursive = recursive;
6120 cb.cb_snapname = snapname;
6124 * 1. collect holds data, set format options
6126 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
6127 holds_callback, &cb);
6133 * 2. print holds data
6135 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
6137 if (nvlist_empty(nvl))
6138 (void) fprintf(stderr, gettext("no datasets available\n"));
6142 return (0 != errors);
6145 #define CHECK_SPINNER 30
6146 #define SPINNER_TIME 3 /* seconds */
6147 #define MOUNT_TIME 1 /* seconds */
6149 typedef struct get_all_state {
6150 boolean_t ga_verbose;
6151 get_all_cb_t *ga_cbp;
6155 get_one_dataset(zfs_handle_t *zhp, void *data)
6157 static char *spin[] = { "-", "\\", "|", "/" };
6158 static int spinval = 0;
6159 static int spincheck = 0;
6160 static time_t last_spin_time = (time_t)0;
6161 get_all_state_t *state = data;
6162 zfs_type_t type = zfs_get_type(zhp);
6164 if (state->ga_verbose) {
6165 if (--spincheck < 0) {
6166 time_t now = time(NULL);
6167 if (last_spin_time + SPINNER_TIME < now) {
6168 update_progress(spin[spinval++ % 4]);
6169 last_spin_time = now;
6171 spincheck = CHECK_SPINNER;
6176 * Iterate over any nested datasets.
6178 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
6184 * Skip any datasets whose type does not match.
6186 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
6190 libzfs_add_handle(state->ga_cbp, zhp);
6191 assert(state->ga_cbp->cb_used <= state->ga_cbp->cb_alloc);
6197 get_all_datasets(get_all_cb_t *cbp, boolean_t verbose)
6199 get_all_state_t state = {
6200 .ga_verbose = verbose,
6205 set_progress_header(gettext("Reading ZFS config"));
6206 (void) zfs_iter_root(g_zfs, get_one_dataset, &state);
6209 finish_progress(gettext("done."));
6213 * Generic callback for sharing or mounting filesystems. Because the code is so
6214 * similar, we have a common function with an extra parameter to determine which
6215 * mode we are using.
6217 typedef enum { OP_SHARE, OP_MOUNT } share_mount_op_t;
6219 typedef struct share_mount_state {
6220 share_mount_op_t sm_op;
6221 boolean_t sm_verbose;
6224 char *sm_proto; /* only valid for OP_SHARE */
6225 pthread_mutex_t sm_lock; /* protects the remaining fields */
6226 uint_t sm_total; /* number of filesystems to process */
6227 uint_t sm_done; /* number of filesystems processed */
6228 int sm_status; /* -1 if any of the share/mount operations failed */
6229 } share_mount_state_t;
6232 * Share or mount a dataset.
6235 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
6236 boolean_t explicit, const char *options)
6238 char mountpoint[ZFS_MAXPROPLEN];
6239 char shareopts[ZFS_MAXPROPLEN];
6240 char smbshareopts[ZFS_MAXPROPLEN];
6241 const char *cmdname = op == OP_SHARE ? "share" : "mount";
6243 uint64_t zoned, canmount;
6244 boolean_t shared_nfs, shared_smb;
6246 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
6249 * Check to make sure we can mount/share this dataset. If we
6250 * are in the global zone and the filesystem is exported to a
6251 * local zone, or if we are in a local zone and the
6252 * filesystem is not exported, then it is an error.
6254 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
6256 if (zoned && getzoneid() == GLOBAL_ZONEID) {
6260 (void) fprintf(stderr, gettext("cannot %s '%s': "
6261 "dataset is exported to a local zone\n"), cmdname,
6265 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
6269 (void) fprintf(stderr, gettext("cannot %s '%s': "
6270 "permission denied\n"), cmdname,
6276 * Ignore any filesystems which don't apply to us. This
6277 * includes those with a legacy mountpoint, or those with
6278 * legacy share options.
6280 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6281 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
6282 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
6283 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
6284 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
6285 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
6287 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
6288 strcmp(smbshareopts, "off") == 0) {
6292 (void) fprintf(stderr, gettext("cannot share '%s': "
6293 "legacy share\n"), zfs_get_name(zhp));
6294 (void) fprintf(stderr, gettext("use share(1M) to "
6295 "share this filesystem, or set "
6296 "sharenfs property on\n"));
6301 * We cannot share or mount legacy filesystems. If the
6302 * shareopts is non-legacy but the mountpoint is legacy, we
6303 * treat it as a legacy share.
6305 if (strcmp(mountpoint, "legacy") == 0) {
6309 (void) fprintf(stderr, gettext("cannot %s '%s': "
6310 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
6311 (void) fprintf(stderr, gettext("use %s(1M) to "
6312 "%s this filesystem\n"), cmdname, cmdname);
6316 if (strcmp(mountpoint, "none") == 0) {
6320 (void) fprintf(stderr, gettext("cannot %s '%s': no "
6321 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
6326 * canmount explicit outcome
6327 * on no pass through
6328 * on yes pass through
6330 * off yes display error, return 1
6331 * noauto no return 0
6332 * noauto yes pass through
6334 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
6335 if (canmount == ZFS_CANMOUNT_OFF) {
6339 (void) fprintf(stderr, gettext("cannot %s '%s': "
6340 "'canmount' property is set to 'off'\n"), cmdname,
6343 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
6348 * If this filesystem is encrypted and does not have
6349 * a loaded key, we can not mount it.
6351 if ((flags & MS_CRYPT) == 0 &&
6352 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF &&
6353 zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
6354 ZFS_KEYSTATUS_UNAVAILABLE) {
6358 (void) fprintf(stderr, gettext("cannot %s '%s': "
6359 "encryption key not loaded\n"), cmdname, zfs_get_name(zhp));
6364 * If this filesystem is inconsistent and has a receive resume
6365 * token, we can not mount it.
6367 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
6368 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
6369 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
6373 (void) fprintf(stderr, gettext("cannot %s '%s': "
6374 "Contains partially-completed state from "
6375 "\"zfs receive -s\", which can be resumed with "
6376 "\"zfs send -t\"\n"),
6377 cmdname, zfs_get_name(zhp));
6382 * At this point, we have verified that the mountpoint and/or
6383 * shareopts are appropriate for auto management. If the
6384 * filesystem is already mounted or shared, return (failing
6385 * for explicit requests); otherwise mount or share the
6391 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
6392 shared_smb = zfs_is_shared_smb(zhp, NULL);
6394 if ((shared_nfs && shared_smb) ||
6395 (shared_nfs && strcmp(shareopts, "on") == 0 &&
6396 strcmp(smbshareopts, "off") == 0) ||
6397 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
6398 strcmp(shareopts, "off") == 0)) {
6402 (void) fprintf(stderr, gettext("cannot share "
6403 "'%s': filesystem already shared\n"),
6408 if (!zfs_is_mounted(zhp, NULL) &&
6409 zfs_mount(zhp, NULL, flags) != 0)
6412 if (protocol == NULL) {
6413 if (zfs_shareall(zhp) != 0)
6415 } else if (strcmp(protocol, "nfs") == 0) {
6416 if (zfs_share_nfs(zhp))
6418 } else if (strcmp(protocol, "smb") == 0) {
6419 if (zfs_share_smb(zhp))
6422 (void) fprintf(stderr, gettext("cannot share "
6423 "'%s': invalid share type '%s' "
6425 zfs_get_name(zhp), protocol);
6432 if (options == NULL)
6433 mnt.mnt_mntopts = "";
6435 mnt.mnt_mntopts = (char *)options;
6437 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6438 zfs_is_mounted(zhp, NULL)) {
6442 (void) fprintf(stderr, gettext("cannot mount "
6443 "'%s': filesystem already mounted\n"),
6448 if (zfs_mount(zhp, options, flags) != 0)
6457 * Reports progress in the form "(current/total)". Not thread-safe.
6460 report_mount_progress(int current, int total)
6462 static time_t last_progress_time = 0;
6463 time_t now = time(NULL);
6466 /* report 1..n instead of 0..n-1 */
6469 /* display header if we're here for the first time */
6471 set_progress_header(gettext("Mounting ZFS filesystems"));
6472 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6473 /* too soon to report again */
6477 last_progress_time = now;
6479 (void) sprintf(info, "(%d/%d)", current, total);
6481 if (current == total)
6482 finish_progress(info);
6484 update_progress(info);
6488 * zfs_foreach_mountpoint() callback that mounts or shares one filesystem and
6489 * updates the progress meter.
6492 share_mount_one_cb(zfs_handle_t *zhp, void *arg)
6494 share_mount_state_t *sms = arg;
6497 ret = share_mount_one(zhp, sms->sm_op, sms->sm_flags, sms->sm_proto,
6498 B_FALSE, sms->sm_options);
6500 pthread_mutex_lock(&sms->sm_lock);
6502 sms->sm_status = ret;
6504 if (sms->sm_verbose)
6505 report_mount_progress(sms->sm_done, sms->sm_total);
6506 pthread_mutex_unlock(&sms->sm_lock);
6511 append_options(char *mntopts, char *newopts)
6513 int len = strlen(mntopts);
6515 /* original length plus new string to append plus 1 for the comma */
6516 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6517 (void) fprintf(stderr, gettext("the opts argument for "
6518 "'%s' option is too long (more than %d chars)\n"),
6519 "-o", MNT_LINE_MAX);
6524 mntopts[len++] = ',';
6526 (void) strcpy(&mntopts[len], newopts);
6530 share_mount(int op, int argc, char **argv)
6533 boolean_t verbose = B_FALSE;
6535 char *options = NULL;
6539 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":alvo:O" : "al"))
6552 if (*optarg == '\0') {
6553 (void) fprintf(stderr, gettext("empty mount "
6554 "options (-o) specified\n"));
6558 if (options == NULL)
6559 options = safe_malloc(MNT_LINE_MAX + 1);
6561 /* option validation is done later */
6562 append_options(options, optarg);
6565 flags |= MS_OVERLAY;
6568 (void) fprintf(stderr, gettext("missing argument for "
6569 "'%c' option\n"), optopt);
6573 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6582 /* check number of arguments */
6584 char *protocol = NULL;
6586 if (op == OP_SHARE && argc > 0) {
6587 if (strcmp(argv[0], "nfs") != 0 &&
6588 strcmp(argv[0], "smb") != 0) {
6589 (void) fprintf(stderr, gettext("share type "
6590 "must be 'nfs' or 'smb'\n"));
6599 (void) fprintf(stderr, gettext("too many arguments\n"));
6603 start_progress_timer();
6604 get_all_cb_t cb = { 0 };
6605 get_all_datasets(&cb, verbose);
6607 if (cb.cb_used == 0) {
6608 if (options != NULL)
6613 share_mount_state_t share_mount_state = { 0 };
6614 share_mount_state.sm_op = op;
6615 share_mount_state.sm_verbose = verbose;
6616 share_mount_state.sm_flags = flags;
6617 share_mount_state.sm_options = options;
6618 share_mount_state.sm_proto = protocol;
6619 share_mount_state.sm_total = cb.cb_used;
6620 pthread_mutex_init(&share_mount_state.sm_lock, NULL);
6623 * libshare isn't mt-safe, so only do the operation in parallel
6624 * if we're mounting.
6626 zfs_foreach_mountpoint(g_zfs, cb.cb_handles, cb.cb_used,
6627 share_mount_one_cb, &share_mount_state, op == OP_MOUNT);
6628 ret = share_mount_state.sm_status;
6630 for (int i = 0; i < cb.cb_used; i++)
6631 zfs_close(cb.cb_handles[i]);
6632 free(cb.cb_handles);
6633 } else if (argc == 0) {
6634 struct mnttab entry;
6636 if ((op == OP_SHARE) || (options != NULL)) {
6637 (void) fprintf(stderr, gettext("missing filesystem "
6638 "argument (specify -a for all)\n"));
6643 * When mount is given no arguments, go through
6644 * /proc/self/mounts and display any active ZFS mounts.
6645 * We hide any snapshots, since they are controlled
6649 /* Reopen MNTTAB to prevent reading stale data from open file */
6650 if (freopen(MNTTAB, "r", mnttab_file) == NULL) {
6651 if (options != NULL)
6656 while (getmntent(mnttab_file, &entry) == 0) {
6657 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6658 strchr(entry.mnt_special, '@') != NULL)
6661 (void) printf("%-30s %s\n", entry.mnt_special,
6669 (void) fprintf(stderr,
6670 gettext("too many arguments\n"));
6674 if ((zhp = zfs_open(g_zfs, argv[0],
6675 ZFS_TYPE_FILESYSTEM)) == NULL) {
6678 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6684 if (options != NULL)
6691 * zfs mount -a [nfs]
6692 * zfs mount filesystem
6694 * Mount all filesystems, or mount the given filesystem.
6697 zfs_do_mount(int argc, char **argv)
6699 return (share_mount(OP_MOUNT, argc, argv));
6703 * zfs share -a [nfs | smb]
6704 * zfs share filesystem
6706 * Share all filesystems, or share the given filesystem.
6709 zfs_do_share(int argc, char **argv)
6711 return (share_mount(OP_SHARE, argc, argv));
6714 typedef struct unshare_unmount_node {
6715 zfs_handle_t *un_zhp;
6717 uu_avl_node_t un_avlnode;
6718 } unshare_unmount_node_t;
6722 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6724 const unshare_unmount_node_t *l = larg;
6725 const unshare_unmount_node_t *r = rarg;
6727 return (strcmp(l->un_mountp, r->un_mountp));
6731 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6732 * absolute path, find the entry /proc/self/mounts, verify that its a
6733 * ZFS filesystems, and unmount it appropriately.
6736 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6740 struct stat64 statbuf;
6741 struct extmnttab entry;
6742 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6746 * Search for the path in /proc/self/mounts. Rather than looking for the
6747 * specific path, which can be fooled by non-standard paths (i.e. ".."
6748 * or "//"), we stat() the path and search for the corresponding
6749 * (major,minor) device pair.
6751 if (stat64(path, &statbuf) != 0) {
6752 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6753 cmdname, path, strerror(errno));
6756 path_inode = statbuf.st_ino;
6759 * Search for the given (major,minor) pair in the mount table.
6762 /* Reopen MNTTAB to prevent reading stale data from open file */
6763 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6766 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6767 if (entry.mnt_major == major(statbuf.st_dev) &&
6768 entry.mnt_minor == minor(statbuf.st_dev))
6772 if (op == OP_SHARE) {
6773 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6774 "currently mounted\n"), cmdname, path);
6777 (void) fprintf(stderr, gettext("warning: %s not in"
6778 "/proc/self/mounts\n"), path);
6779 if ((ret = umount2(path, flags)) != 0)
6780 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6785 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6786 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6787 "filesystem\n"), cmdname, path);
6791 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6792 ZFS_TYPE_FILESYSTEM)) == NULL)
6796 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6797 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6798 cmdname, path, strerror(errno));
6800 } else if (statbuf.st_ino != path_inode) {
6801 (void) fprintf(stderr, gettext("cannot "
6802 "%s '%s': not a mountpoint\n"), cmdname, path);
6806 if (op == OP_SHARE) {
6807 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6808 char smbshare_prop[ZFS_MAXPROPLEN];
6810 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6811 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6812 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6813 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6815 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6816 strcmp(smbshare_prop, "off") == 0) {
6817 (void) fprintf(stderr, gettext("cannot unshare "
6818 "'%s': legacy share\n"), path);
6819 (void) fprintf(stderr, gettext("use exportfs(8) "
6820 "or smbcontrol(1) to unshare this filesystem\n"));
6821 } else if (!zfs_is_shared(zhp)) {
6822 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6823 "not currently shared\n"), path);
6825 ret = zfs_unshareall_bypath(zhp, path);
6828 char mtpt_prop[ZFS_MAXPROPLEN];
6830 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6831 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6834 ret = zfs_unmount(zhp, NULL, flags);
6835 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6836 (void) fprintf(stderr, gettext("cannot unmount "
6837 "'%s': legacy mountpoint\n"),
6839 (void) fprintf(stderr, gettext("use umount(8) "
6840 "to unmount this filesystem\n"));
6842 ret = zfs_unmountall(zhp, flags);
6853 * Generic callback for unsharing or unmounting a filesystem.
6856 unshare_unmount(int op, int argc, char **argv)
6863 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6864 char sharesmb[ZFS_MAXPROPLEN];
6867 while ((c = getopt(argc, argv, op == OP_SHARE ? ":a" : "af")) != -1) {
6876 (void) fprintf(stderr, gettext("missing argument for "
6877 "'%c' option\n"), optopt);
6881 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6892 * We could make use of zfs_for_each() to walk all datasets in
6893 * the system, but this would be very inefficient, especially
6894 * since we would have to linearly search /proc/self/mounts for
6895 * each one. Instead, do one pass through /proc/self/mounts
6896 * looking for zfs entries and call zfs_unmount() for each one.
6898 * Things get a little tricky if the administrator has created
6899 * mountpoints beneath other ZFS filesystems. In this case, we
6900 * have to unmount the deepest filesystems first. To accomplish
6901 * this, we place all the mountpoints in an AVL tree sorted by
6902 * the special type (dataset name), and walk the result in
6903 * reverse to make sure to get any snapshots first.
6905 struct mnttab entry;
6906 uu_avl_pool_t *pool;
6907 uu_avl_t *tree = NULL;
6908 unshare_unmount_node_t *node;
6910 uu_avl_walk_t *walk;
6911 char *protocol = NULL;
6913 if (op == OP_SHARE && argc > 0) {
6914 if (strcmp(argv[0], "nfs") != 0 &&
6915 strcmp(argv[0], "smb") != 0) {
6916 (void) fprintf(stderr, gettext("share type "
6917 "must be 'nfs' or 'smb'\n"));
6926 (void) fprintf(stderr, gettext("too many arguments\n"));
6930 if (((pool = uu_avl_pool_create("unmount_pool",
6931 sizeof (unshare_unmount_node_t),
6932 offsetof(unshare_unmount_node_t, un_avlnode),
6933 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6934 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6937 /* Reopen MNTTAB to prevent reading stale data from open file */
6938 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6941 while (getmntent(mnttab_file, &entry) == 0) {
6943 /* ignore non-ZFS entries */
6944 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6947 /* ignore snapshots */
6948 if (strchr(entry.mnt_special, '@') != NULL)
6951 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6952 ZFS_TYPE_FILESYSTEM)) == NULL) {
6958 * Ignore datasets that are excluded/restricted by
6961 if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
6968 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6970 sizeof (nfs_mnt_prop),
6971 NULL, NULL, 0, B_FALSE) == 0);
6972 if (strcmp(nfs_mnt_prop, "off") != 0)
6974 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6976 sizeof (nfs_mnt_prop),
6977 NULL, NULL, 0, B_FALSE) == 0);
6978 if (strcmp(nfs_mnt_prop, "off") == 0)
6982 /* Ignore legacy mounts */
6983 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6985 sizeof (nfs_mnt_prop),
6986 NULL, NULL, 0, B_FALSE) == 0);
6987 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6989 /* Ignore canmount=noauto mounts */
6990 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6991 ZFS_CANMOUNT_NOAUTO)
6997 node = safe_malloc(sizeof (unshare_unmount_node_t));
6999 node->un_mountp = safe_strdup(entry.mnt_mountp);
7001 uu_avl_node_init(node, &node->un_avlnode, pool);
7003 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
7004 uu_avl_insert(tree, node, idx);
7006 zfs_close(node->un_zhp);
7007 free(node->un_mountp);
7013 * Walk the AVL tree in reverse, unmounting each filesystem and
7014 * removing it from the AVL tree in the process.
7016 if ((walk = uu_avl_walk_start(tree,
7017 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
7020 while ((node = uu_avl_walk_next(walk)) != NULL) {
7021 uu_avl_remove(tree, node);
7025 if (zfs_unshareall_bytype(node->un_zhp,
7026 node->un_mountp, protocol) != 0)
7031 if (zfs_unmount(node->un_zhp,
7032 node->un_zhp->zfs_name, flags) != 0)
7037 zfs_close(node->un_zhp);
7038 free(node->un_mountp);
7042 uu_avl_walk_end(walk);
7043 uu_avl_destroy(tree);
7044 uu_avl_pool_destroy(pool);
7049 (void) fprintf(stderr,
7050 gettext("missing filesystem argument\n"));
7052 (void) fprintf(stderr,
7053 gettext("too many arguments\n"));
7058 * We have an argument, but it may be a full path or a ZFS
7059 * filesystem. Pass full paths off to unmount_path() (shared by
7060 * manual_unmount), otherwise open the filesystem and pass to
7063 if (argv[0][0] == '/')
7064 return (unshare_unmount_path(op, argv[0],
7067 if ((zhp = zfs_open(g_zfs, argv[0],
7068 ZFS_TYPE_FILESYSTEM)) == NULL)
7071 verify(zfs_prop_get(zhp, op == OP_SHARE ?
7072 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
7073 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
7074 NULL, 0, B_FALSE) == 0);
7078 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
7080 sizeof (nfs_mnt_prop),
7081 NULL, NULL, 0, B_FALSE) == 0);
7082 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
7083 sharesmb, sizeof (sharesmb), NULL, NULL,
7086 if (strcmp(nfs_mnt_prop, "off") == 0 &&
7087 strcmp(sharesmb, "off") == 0) {
7088 (void) fprintf(stderr, gettext("cannot "
7089 "unshare '%s': legacy share\n"),
7091 (void) fprintf(stderr, gettext("use "
7092 "unshare(1M) to unshare this "
7095 } else if (!zfs_is_shared(zhp)) {
7096 (void) fprintf(stderr, gettext("cannot "
7097 "unshare '%s': not currently "
7098 "shared\n"), zfs_get_name(zhp));
7100 } else if (zfs_unshareall(zhp) != 0) {
7106 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
7107 (void) fprintf(stderr, gettext("cannot "
7108 "unmount '%s': legacy "
7109 "mountpoint\n"), zfs_get_name(zhp));
7110 (void) fprintf(stderr, gettext("use "
7111 "umount(1M) to unmount this "
7114 } else if (!zfs_is_mounted(zhp, NULL)) {
7115 (void) fprintf(stderr, gettext("cannot "
7116 "unmount '%s': not currently "
7120 } else if (zfs_unmountall(zhp, flags) != 0) {
7134 * zfs unmount filesystem
7136 * Unmount all filesystems, or a specific ZFS filesystem.
7139 zfs_do_unmount(int argc, char **argv)
7141 return (unshare_unmount(OP_MOUNT, argc, argv));
7146 * zfs unshare filesystem
7148 * Unshare all filesystems, or a specific ZFS filesystem.
7151 zfs_do_unshare(int argc, char **argv)
7153 return (unshare_unmount(OP_SHARE, argc, argv));
7157 disable_command_idx(char *command)
7159 for (int i = 0; i < NCOMMAND; i++) {
7160 if (command_table[i].name == NULL)
7163 if (strcmp(command, command_table[i].name) == 0) {
7164 command_table[i].name = NULL;
7172 find_command_idx(char *command, int *idx)
7176 for (i = 0; i < NCOMMAND; i++) {
7177 if (command_table[i].name == NULL)
7180 if (strcmp(command, command_table[i].name) == 0) {
7189 zfs_do_diff(int argc, char **argv)
7193 char *tosnap = NULL;
7194 char *fromsnap = NULL;
7199 while ((c = getopt(argc, argv, "FHt")) != -1) {
7202 flags |= ZFS_DIFF_CLASSIFY;
7205 flags |= ZFS_DIFF_PARSEABLE;
7208 flags |= ZFS_DIFF_TIMESTAMP;
7211 (void) fprintf(stderr,
7212 gettext("invalid option '%c'\n"), optopt);
7221 (void) fprintf(stderr,
7222 gettext("must provide at least one snapshot name\n"));
7227 (void) fprintf(stderr, gettext("too many arguments\n"));
7232 tosnap = (argc == 2) ? argv[1] : NULL;
7235 if (*fromsnap != '@')
7236 copy = strdup(fromsnap);
7238 copy = strdup(tosnap);
7242 if ((atp = strchr(copy, '@')) != NULL)
7245 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) {
7252 * Ignore SIGPIPE so that the library can give us
7253 * information on any failure
7255 (void) sigignore(SIGPIPE);
7257 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
7266 * zfs remap <filesystem | volume>
7268 * N.B. The remap command has been disabled and may be removed in the future.
7270 * Remap the indirect blocks in the given filesystem or volume so that they no
7271 * longer reference blocks on previously removed vdevs and we can eventually
7272 * shrink the size of the indirect mapping objects for the previously removed
7273 * vdevs. Note that remapping all blocks might not be possible and that
7274 * references from snapshots will still exist and cannot be remapped.
7276 * This functionality is no longer particularly useful now that the removal
7277 * code can map large chunks. Furthermore, explaining what this command
7278 * does and why it may be useful requires a detailed understanding of the
7279 * internals of device removal. These are details users should not be
7280 * bothered with. If required, the remap command can be re-enabled by
7281 * setting the ZFS_REMAP_ENABLED environment variable.
7283 * > ZFS_REMAP_ENABLED=yes zfs remap <filesystem | volume>
7286 zfs_do_remap(int argc, char **argv)
7293 while ((c = getopt(argc, argv, "")) != -1) {
7296 (void) fprintf(stderr,
7297 gettext("invalid option '%c'\n"), optopt);
7303 (void) fprintf(stderr, gettext("wrong number of arguments\n"));
7308 err = zfs_remap_indirects(g_zfs, fsname);
7314 * zfs bookmark <fs@snap> <fs#bmark>
7316 * Creates a bookmark with the given name from the given snapshot.
7319 zfs_do_bookmark(int argc, char **argv)
7321 char snapname[ZFS_MAX_DATASET_NAME_LEN];
7322 char bookname[ZFS_MAX_DATASET_NAME_LEN];
7329 while ((c = getopt(argc, argv, "")) != -1) {
7332 (void) fprintf(stderr,
7333 gettext("invalid option '%c'\n"), optopt);
7341 /* check number of arguments */
7343 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
7347 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
7351 if (strchr(argv[0], '@') == NULL) {
7352 (void) fprintf(stderr,
7353 gettext("invalid snapshot name '%s': "
7354 "must contain a '@'\n"), argv[0]);
7357 if (strchr(argv[1], '#') == NULL) {
7358 (void) fprintf(stderr,
7359 gettext("invalid bookmark name '%s': "
7360 "must contain a '#'\n"), argv[1]);
7364 if (argv[0][0] == '@') {
7366 * Snapshot name begins with @.
7367 * Default to same fs as bookmark.
7369 (void) strlcpy(snapname, argv[1], sizeof (snapname));
7370 *strchr(snapname, '#') = '\0';
7371 (void) strlcat(snapname, argv[0], sizeof (snapname));
7373 (void) strlcpy(snapname, argv[0], sizeof (snapname));
7375 if (argv[1][0] == '#') {
7377 * Bookmark name begins with #.
7378 * Default to same fs as snapshot.
7380 (void) strlcpy(bookname, argv[0], sizeof (bookname));
7381 *strchr(bookname, '@') = '\0';
7382 (void) strlcat(bookname, argv[1], sizeof (bookname));
7384 (void) strlcpy(bookname, argv[1], sizeof (bookname));
7387 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
7393 nvl = fnvlist_alloc();
7394 fnvlist_add_string(nvl, bookname, snapname);
7395 ret = lzc_bookmark(nvl, NULL);
7399 const char *err_msg = NULL;
7402 (void) snprintf(errbuf, sizeof (errbuf),
7403 dgettext(TEXT_DOMAIN,
7404 "cannot create bookmark '%s'"), bookname);
7408 err_msg = "bookmark is in a different pool";
7411 err_msg = "bookmark exists";
7414 err_msg = "invalid argument";
7417 err_msg = "bookmark feature not enabled";
7420 err_msg = "out of space";
7423 err_msg = "dataset does not exist";
7426 (void) zfs_standard_error(g_zfs, ret, errbuf);
7429 if (err_msg != NULL) {
7430 (void) fprintf(stderr, "%s: %s\n", errbuf,
7431 dgettext(TEXT_DOMAIN, err_msg));
7443 zfs_do_channel_program(int argc, char **argv)
7446 char *progbuf, *filename, *poolname;
7447 size_t progsize, progread;
7448 nvlist_t *outnvl = NULL;
7449 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
7450 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
7451 boolean_t sync_flag = B_TRUE, json_output = B_FALSE;
7452 zpool_handle_t *zhp;
7455 while ((c = getopt(argc, argv, "nt:m:j")) != -1) {
7463 arg = strtoull(optarg, &endp, 0);
7464 if (errno != 0 || *endp != '\0') {
7465 (void) fprintf(stderr, gettext(
7467 "'%s': expected integer\n"), optarg);
7474 ASSERT3U(c, ==, 'm');
7480 sync_flag = B_FALSE;
7484 json_output = B_TRUE;
7488 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7498 (void) fprintf(stderr,
7499 gettext("invalid number of arguments\n"));
7505 if (strcmp(filename, "-") == 0) {
7507 filename = "standard input";
7508 } else if ((fd = open(filename, O_RDONLY)) < 0) {
7509 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
7510 filename, strerror(errno));
7514 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7515 (void) fprintf(stderr, gettext("cannot open pool '%s'"),
7524 * Read in the channel program, expanding the program buffer as
7529 progbuf = safe_malloc(progsize);
7531 ret = read(fd, progbuf + progread, progsize - progread);
7533 if (progread == progsize && ret > 0) {
7535 progbuf = safe_realloc(progbuf, progsize);
7543 (void) fprintf(stderr,
7544 gettext("cannot read '%s': %s\n"),
7545 filename, strerror(errno));
7548 progbuf[progread] = '\0';
7551 * Any remaining arguments are passed as arguments to the lua script as
7554 * "argv" -> [ "arg 1", ... "arg n" ],
7557 nvlist_t *argnvl = fnvlist_alloc();
7558 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);
7561 ret = lzc_channel_program(poolname, progbuf,
7562 instrlimit, memlimit, argnvl, &outnvl);
7564 ret = lzc_channel_program_nosync(poolname, progbuf,
7565 instrlimit, memlimit, argnvl, &outnvl);
7570 * On error, report the error message handed back by lua if one
7571 * exists. Otherwise, generate an appropriate error message,
7572 * falling back on strerror() for an unexpected return code.
7574 char *errstring = NULL;
7575 const char *msg = gettext("Channel program execution failed");
7576 uint64_t instructions = 0;
7577 if (outnvl != NULL && nvlist_exists(outnvl, ZCP_RET_ERROR)) {
7578 (void) nvlist_lookup_string(outnvl,
7579 ZCP_RET_ERROR, &errstring);
7580 if (errstring == NULL)
7581 errstring = strerror(ret);
7583 (void) nvlist_lookup_uint64(outnvl,
7584 ZCP_ARG_INSTRLIMIT, &instructions);
7590 "Invalid instruction or memory limit.";
7593 errstring = "Return value too large.";
7596 errstring = "Memory limit exhausted.";
7599 errstring = "Timed out.";
7602 errstring = "Permission denied. Channel "
7603 "programs must be run as root.";
7606 (void) zfs_standard_error(g_zfs, ret, msg);
7609 if (errstring != NULL)
7610 (void) fprintf(stderr, "%s:\n%s\n", msg, errstring);
7612 if (ret == ETIME && instructions != 0)
7613 (void) fprintf(stderr,
7614 gettext("%llu Lua instructions\n"),
7615 (u_longlong_t)instructions);
7618 (void) nvlist_print_json(stdout, outnvl);
7619 } else if (nvlist_empty(outnvl)) {
7620 (void) fprintf(stdout, gettext("Channel program fully "
7621 "executed and did not produce output.\n"));
7623 (void) fprintf(stdout, gettext("Channel program fully "
7624 "executed and produced output:\n"));
7625 dump_nvlist(outnvl, 4);
7630 fnvlist_free(outnvl);
7631 fnvlist_free(argnvl);
7640 typedef struct loadkey_cbdata {
7641 boolean_t cb_loadkey;
7642 boolean_t cb_recursive;
7644 char *cb_keylocation;
7645 uint64_t cb_numfailed;
7646 uint64_t cb_numattempted;
7650 load_key_callback(zfs_handle_t *zhp, void *data)
7653 boolean_t is_encroot;
7654 loadkey_cbdata_t *cb = data;
7655 uint64_t keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
7658 * If we are working recursively, we want to skip loading / unloading
7659 * keys for non-encryption roots and datasets whose keys are already
7660 * in the desired end-state.
7662 if (cb->cb_recursive) {
7663 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
7669 if ((cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_AVAILABLE) ||
7670 (!cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_UNAVAILABLE))
7674 cb->cb_numattempted++;
7677 ret = zfs_crypto_load_key(zhp, cb->cb_noop, cb->cb_keylocation);
7679 ret = zfs_crypto_unload_key(zhp);
7690 load_unload_keys(int argc, char **argv, boolean_t loadkey)
7692 int c, ret = 0, flags = 0;
7693 boolean_t do_all = B_FALSE;
7694 loadkey_cbdata_t cb = { 0 };
7696 cb.cb_loadkey = loadkey;
7698 while ((c = getopt(argc, argv, "anrL:")) != -1) {
7699 /* noop and alternate keylocations only apply to zfs load-key */
7703 cb.cb_noop = B_TRUE;
7706 cb.cb_keylocation = optarg;
7716 cb.cb_recursive = B_TRUE;
7719 flags |= ZFS_ITER_RECURSE;
7720 cb.cb_recursive = B_TRUE;
7723 (void) fprintf(stderr,
7724 gettext("invalid option '%c'\n"), optopt);
7732 if (!do_all && argc == 0) {
7733 (void) fprintf(stderr,
7734 gettext("Missing dataset argument or -a option\n"));
7738 if (do_all && argc != 0) {
7739 (void) fprintf(stderr,
7740 gettext("Cannot specify dataset with -a option\n"));
7744 if (cb.cb_recursive && cb.cb_keylocation != NULL &&
7745 strcmp(cb.cb_keylocation, "prompt") != 0) {
7746 (void) fprintf(stderr, gettext("alternate keylocation may only "
7747 "be 'prompt' with -r or -a\n"));
7751 ret = zfs_for_each(argc, argv, flags,
7752 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL, 0,
7753 load_key_callback, &cb);
7755 if (cb.cb_noop || (cb.cb_recursive && cb.cb_numattempted != 0)) {
7756 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
7757 (u_longlong_t)(cb.cb_numattempted - cb.cb_numfailed),
7758 (u_longlong_t)cb.cb_numattempted,
7759 loadkey ? (cb.cb_noop ? "verified" : "loaded") :
7763 if (cb.cb_numfailed != 0)
7770 zfs_do_load_key(int argc, char **argv)
7772 return (load_unload_keys(argc, argv, B_TRUE));
7777 zfs_do_unload_key(int argc, char **argv)
7779 return (load_unload_keys(argc, argv, B_FALSE));
7783 zfs_do_change_key(int argc, char **argv)
7787 boolean_t loadkey = B_FALSE, inheritkey = B_FALSE;
7788 zfs_handle_t *zhp = NULL;
7789 nvlist_t *props = fnvlist_alloc();
7791 while ((c = getopt(argc, argv, "lio:")) != -1) {
7797 inheritkey = B_TRUE;
7800 if (!parseprop(props, optarg)) {
7806 (void) fprintf(stderr,
7807 gettext("invalid option '%c'\n"), optopt);
7812 if (inheritkey && !nvlist_empty(props)) {
7813 (void) fprintf(stderr,
7814 gettext("Properties not allowed for inheriting\n"));
7822 (void) fprintf(stderr, gettext("Missing dataset argument\n"));
7827 (void) fprintf(stderr, gettext("Too many arguments\n"));
7831 zhp = zfs_open(g_zfs, argv[argc - 1],
7832 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
7837 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
7838 if (keystatus != ZFS_KEYSTATUS_AVAILABLE) {
7839 ret = zfs_crypto_load_key(zhp, B_FALSE, NULL);
7847 /* refresh the properties so the new keystatus is visible */
7848 zfs_refresh_properties(zhp);
7851 ret = zfs_crypto_rewrap(zhp, props, inheritkey);
7864 * 1) zfs project [-d|-r] <file|directory ...>
7865 * List project ID and inherit flag of file(s) or directories.
7866 * -d: List the directory itself, not its children.
7867 * -r: List subdirectories recursively.
7869 * 2) zfs project -C [-k] [-r] <file|directory ...>
7870 * Clear project inherit flag and/or ID on the file(s) or directories.
7871 * -k: Keep the project ID unchanged. If not specified, the project ID
7872 * will be reset as zero.
7873 * -r: Clear on subdirectories recursively.
7875 * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
7876 * Check project ID and inherit flag on the file(s) or directories,
7877 * report the outliers.
7878 * -0: Print file name followed by a NUL instead of newline.
7879 * -d: Check the directory itself, not its children.
7880 * -p: Specify the referenced ID for comparing with the target file(s)
7881 * or directories' project IDs. If not specified, the target (top)
7882 * directory's project ID will be used as the referenced one.
7883 * -r: Check subdirectories recursively.
7885 * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
7886 * Set project ID and/or inherit flag on the file(s) or directories.
7887 * -p: Set the project ID as the given id.
7888 * -r: Set on subdirectorie recursively. If not specify "-p" option,
7889 * it will use top-level directory's project ID as the given id,
7890 * then set both project ID and inherit flag on all descendants
7891 * of the top-level directory.
7892 * -s: Set project inherit flag.
7895 zfs_do_project(int argc, char **argv)
7897 zfs_project_control_t zpc = {
7898 .zpc_expected_projid = ZFS_INVALID_PROJID,
7899 .zpc_op = ZFS_PROJECT_OP_DEFAULT,
7900 .zpc_dironly = B_FALSE,
7901 .zpc_keep_projid = B_FALSE,
7902 .zpc_newline = B_TRUE,
7903 .zpc_recursive = B_FALSE,
7904 .zpc_set_flag = B_FALSE,
7911 while ((c = getopt(argc, argv, "0Ccdkp:rs")) != -1) {
7914 zpc.zpc_newline = B_FALSE;
7917 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
7918 (void) fprintf(stderr, gettext("cannot "
7919 "specify '-C' '-c' '-s' together\n"));
7923 zpc.zpc_op = ZFS_PROJECT_OP_CLEAR;
7926 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
7927 (void) fprintf(stderr, gettext("cannot "
7928 "specify '-C' '-c' '-s' together\n"));
7932 zpc.zpc_op = ZFS_PROJECT_OP_CHECK;
7935 zpc.zpc_dironly = B_TRUE;
7936 /* overwrite "-r" option */
7937 zpc.zpc_recursive = B_FALSE;
7940 zpc.zpc_keep_projid = B_TRUE;
7946 zpc.zpc_expected_projid = strtoull(optarg, &endptr, 0);
7947 if (errno != 0 || *endptr != '\0') {
7948 (void) fprintf(stderr,
7949 gettext("project ID must be less than "
7950 "%u\n"), UINT32_MAX);
7953 if (zpc.zpc_expected_projid >= UINT32_MAX) {
7954 (void) fprintf(stderr,
7955 gettext("invalid project ID\n"));
7961 zpc.zpc_recursive = B_TRUE;
7962 /* overwrite "-d" option */
7963 zpc.zpc_dironly = B_FALSE;
7966 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
7967 (void) fprintf(stderr, gettext("cannot "
7968 "specify '-C' '-c' '-s' together\n"));
7972 zpc.zpc_set_flag = B_TRUE;
7973 zpc.zpc_op = ZFS_PROJECT_OP_SET;
7976 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7982 if (zpc.zpc_op == ZFS_PROJECT_OP_DEFAULT) {
7983 if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID)
7984 zpc.zpc_op = ZFS_PROJECT_OP_SET;
7986 zpc.zpc_op = ZFS_PROJECT_OP_LIST;
7989 switch (zpc.zpc_op) {
7990 case ZFS_PROJECT_OP_LIST:
7991 if (zpc.zpc_keep_projid) {
7992 (void) fprintf(stderr,
7993 gettext("'-k' is only valid together with '-C'\n"));
7996 if (!zpc.zpc_newline) {
7997 (void) fprintf(stderr,
7998 gettext("'-0' is only valid together with '-c'\n"));
8002 case ZFS_PROJECT_OP_CHECK:
8003 if (zpc.zpc_keep_projid) {
8004 (void) fprintf(stderr,
8005 gettext("'-k' is only valid together with '-C'\n"));
8009 case ZFS_PROJECT_OP_CLEAR:
8010 if (zpc.zpc_dironly) {
8011 (void) fprintf(stderr,
8012 gettext("'-d' is useless together with '-C'\n"));
8015 if (!zpc.zpc_newline) {
8016 (void) fprintf(stderr,
8017 gettext("'-0' is only valid together with '-c'\n"));
8020 if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID) {
8021 (void) fprintf(stderr,
8022 gettext("'-p' is useless together with '-C'\n"));
8026 case ZFS_PROJECT_OP_SET:
8027 if (zpc.zpc_dironly) {
8028 (void) fprintf(stderr,
8029 gettext("'-d' is useless for set project ID and/or "
8033 if (zpc.zpc_keep_projid) {
8034 (void) fprintf(stderr,
8035 gettext("'-k' is only valid together with '-C'\n"));
8038 if (!zpc.zpc_newline) {
8039 (void) fprintf(stderr,
8040 gettext("'-0' is only valid together with '-c'\n"));
8052 (void) fprintf(stderr,
8053 gettext("missing file or directory target(s)\n"));
8057 for (int i = 0; i < argc; i++) {
8060 err = zfs_project_handle(argv[i], &zpc);
8069 * Display version message
8072 zfs_do_version(int argc, char **argv)
8074 if (zfs_version_print() == -1)
8081 main(int argc, char **argv)
8088 (void) setlocale(LC_ALL, "");
8089 (void) textdomain(TEXT_DOMAIN);
8094 * Make sure the user has specified some command.
8097 (void) fprintf(stderr, gettext("missing command\n"));
8104 * The 'umount' command is an alias for 'unmount'
8106 if (strcmp(cmdname, "umount") == 0)
8107 cmdname = "unmount";
8110 * The 'recv' command is an alias for 'receive'
8112 if (strcmp(cmdname, "recv") == 0)
8113 cmdname = "receive";
8116 * The 'snap' command is an alias for 'snapshot'
8118 if (strcmp(cmdname, "snap") == 0)
8119 cmdname = "snapshot";
8122 * The 'remap' command has been disabled and may be removed in the
8123 * future. See the comment above zfs_do_remap() for details.
8125 if (!libzfs_envvar_is_set("ZFS_REMAP_ENABLED"))
8126 disable_command_idx("remap");
8131 if ((strcmp(cmdname, "-?") == 0) ||
8132 (strcmp(cmdname, "--help") == 0))
8136 * Special case '-V|--version'
8138 if ((strcmp(cmdname, "-V") == 0) || (strcmp(cmdname, "--version") == 0))
8139 return (zfs_do_version(argc, argv));
8141 if ((g_zfs = libzfs_init()) == NULL) {
8142 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
8146 mnttab_file = g_zfs->libzfs_mnttab;
8148 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
8150 libzfs_print_on_error(g_zfs, B_TRUE);
8153 * Many commands modify input strings for string parsing reasons.
8154 * We create a copy to protect the original argv.
8156 newargv = malloc((argc + 1) * sizeof (newargv[0]));
8157 for (i = 0; i < argc; i++)
8158 newargv[i] = strdup(argv[i]);
8159 newargv[argc] = NULL;
8162 * Run the appropriate command.
8164 libzfs_mnttab_cache(g_zfs, B_TRUE);
8165 if (find_command_idx(cmdname, &i) == 0) {
8166 current_command = &command_table[i];
8167 ret = command_table[i].func(argc - 1, newargv + 1);
8168 } else if (strchr(cmdname, '=') != NULL) {
8169 verify(find_command_idx("set", &i) == 0);
8170 current_command = &command_table[i];
8171 ret = command_table[i].func(argc, newargv);
8173 (void) fprintf(stderr, gettext("unrecognized "
8174 "command '%s'\n"), cmdname);
8179 for (i = 0; i < argc; i++)
8183 if (ret == 0 && log_history)
8184 (void) zpool_log_history(g_zfs, history_str);
8189 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
8190 * for the purposes of running ::findleaks.
8192 if (getenv("ZFS_ABORT") != NULL) {
8193 (void) printf("dumping core by request\n");