4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
25 * Copyright 2012 Milan Jurik. All rights reserved.
26 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>.
29 * Copyright 2016 Nexenta Systems, Inc.
39 #include <libnvpair.h>
51 #include <sys/debug.h>
53 #include <sys/mkdev.h>
54 #include <sys/mntent.h>
55 #include <sys/mnttab.h>
56 #include <sys/mount.h>
58 #include <sys/fs/zfs.h>
59 #include <sys/systeminfo.h>
60 #include <sys/types.h>
62 #include <sys/zfs_project.h>
65 #include <libzfs_core.h>
67 #include <zfs_deleg.h>
71 #include <directory.h>
72 #endif /* HAVE_IDMAP */
76 #include "zfs_comutil.h"
77 #include "libzfs_impl.h"
78 #include "zfs_projectutil.h"
80 libzfs_handle_t *g_zfs;
82 static FILE *mnttab_file;
83 static char history_str[HIS_MAX_RECORD_LEN];
84 static boolean_t log_history = B_TRUE;
86 static int zfs_do_clone(int argc, char **argv);
87 static int zfs_do_create(int argc, char **argv);
88 static int zfs_do_destroy(int argc, char **argv);
89 static int zfs_do_get(int argc, char **argv);
90 static int zfs_do_inherit(int argc, char **argv);
91 static int zfs_do_list(int argc, char **argv);
92 static int zfs_do_mount(int argc, char **argv);
93 static int zfs_do_rename(int argc, char **argv);
94 static int zfs_do_rollback(int argc, char **argv);
95 static int zfs_do_set(int argc, char **argv);
96 static int zfs_do_upgrade(int argc, char **argv);
97 static int zfs_do_snapshot(int argc, char **argv);
98 static int zfs_do_unmount(int argc, char **argv);
99 static int zfs_do_share(int argc, char **argv);
100 static int zfs_do_unshare(int argc, char **argv);
101 static int zfs_do_send(int argc, char **argv);
102 static int zfs_do_receive(int argc, char **argv);
103 static int zfs_do_promote(int argc, char **argv);
104 static int zfs_do_userspace(int argc, char **argv);
105 static int zfs_do_allow(int argc, char **argv);
106 static int zfs_do_unallow(int argc, char **argv);
107 static int zfs_do_hold(int argc, char **argv);
108 static int zfs_do_holds(int argc, char **argv);
109 static int zfs_do_release(int argc, char **argv);
110 static int zfs_do_diff(int argc, char **argv);
111 static int zfs_do_bookmark(int argc, char **argv);
112 static int zfs_do_channel_program(int argc, char **argv);
113 static int zfs_do_load_key(int argc, char **argv);
114 static int zfs_do_unload_key(int argc, char **argv);
115 static int zfs_do_change_key(int argc, char **argv);
116 static int zfs_do_project(int argc, char **argv);
119 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
124 _umem_debug_init(void)
126 return ("default,verbose"); /* $UMEM_DEBUG setting */
130 _umem_logging_init(void)
132 return ("fail,contents"); /* $UMEM_LOGGING setting */
166 HELP_CHANNEL_PROGRAM,
172 typedef struct zfs_command {
174 int (*func)(int argc, char **argv);
179 * Master command table. Each ZFS command has a name, associated function, and
180 * usage message. The usage messages need to be internationalized, so we have
181 * to have a function to return the usage message based on a command index.
183 * These commands are organized according to how they are displayed in the usage
184 * message. An empty command (one with a NULL name) indicates an empty line in
185 * the generic usage message.
187 static zfs_command_t command_table[] = {
188 { "create", zfs_do_create, HELP_CREATE },
189 { "destroy", zfs_do_destroy, HELP_DESTROY },
191 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
192 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
193 { "clone", zfs_do_clone, HELP_CLONE },
194 { "promote", zfs_do_promote, HELP_PROMOTE },
195 { "rename", zfs_do_rename, HELP_RENAME },
196 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
197 { "program", zfs_do_channel_program, HELP_CHANNEL_PROGRAM },
199 { "list", zfs_do_list, HELP_LIST },
201 { "set", zfs_do_set, HELP_SET },
202 { "get", zfs_do_get, HELP_GET },
203 { "inherit", zfs_do_inherit, HELP_INHERIT },
204 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
206 { "userspace", zfs_do_userspace, HELP_USERSPACE },
207 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
208 { "projectspace", zfs_do_userspace, HELP_PROJECTSPACE },
210 { "project", zfs_do_project, HELP_PROJECT },
212 { "mount", zfs_do_mount, HELP_MOUNT },
213 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
214 { "share", zfs_do_share, HELP_SHARE },
215 { "unshare", zfs_do_unshare, HELP_UNSHARE },
217 { "send", zfs_do_send, HELP_SEND },
218 { "receive", zfs_do_receive, HELP_RECEIVE },
220 { "allow", zfs_do_allow, HELP_ALLOW },
222 { "unallow", zfs_do_unallow, HELP_UNALLOW },
224 { "hold", zfs_do_hold, HELP_HOLD },
225 { "holds", zfs_do_holds, HELP_HOLDS },
226 { "release", zfs_do_release, HELP_RELEASE },
227 { "diff", zfs_do_diff, HELP_DIFF },
228 { "load-key", zfs_do_load_key, HELP_LOAD_KEY },
229 { "unload-key", zfs_do_unload_key, HELP_UNLOAD_KEY },
230 { "change-key", zfs_do_change_key, HELP_CHANGE_KEY },
233 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
235 zfs_command_t *current_command;
238 get_usage(zfs_help_t idx)
242 return (gettext("\tclone [-p] [-o property=value] ... "
243 "<snapshot> <filesystem|volume>\n"));
245 return (gettext("\tcreate [-p] [-o property=value] ... "
247 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
248 "-V <size> <volume>\n"));
250 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
251 "\tdestroy [-dnpRrv] "
252 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
253 "\tdestroy <filesystem|volume>#<bookmark>\n"));
255 return (gettext("\tget [-rHp] [-d max] "
256 "[-o \"all\" | field[,...]]\n"
257 "\t [-t type[,...]] [-s source[,...]]\n"
258 "\t <\"all\" | property[,...]> "
259 "[filesystem|volume|snapshot|bookmark] ...\n"));
261 return (gettext("\tinherit [-rS] <property> "
262 "<filesystem|volume|snapshot> ...\n"));
264 return (gettext("\tupgrade [-v]\n"
265 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
267 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
268 "[-s property]...\n\t [-S property]... [-t type[,...]] "
269 "[filesystem|volume|snapshot] ...\n"));
271 return (gettext("\tmount\n"
272 "\tmount [-lvO] [-o opts] <-a | filesystem>\n"));
274 return (gettext("\tpromote <clone-filesystem>\n"));
276 return (gettext("\treceive [-vnsFu] "
277 "[-o <property>=<value>] ... [-x <property>] ...\n"
278 "\t <filesystem|volume|snapshot>\n"
279 "\treceive [-vnsFu] [-o <property>=<value>] ... "
280 "[-x <property>] ... \n"
281 "\t [-d | -e] <filesystem>\n"
282 "\treceive -A <filesystem|volume>\n"));
284 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
285 "<filesystem|volume|snapshot>\n"
286 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
287 "\trename -r <snapshot> <snapshot>\n"));
289 return (gettext("\trollback [-rRf] <snapshot>\n"));
291 return (gettext("\tsend [-DnPpRvLecr] [-[i|I] snapshot] "
293 "\tsend [-Lecr] [-i snapshot|bookmark] "
294 "<filesystem|volume|snapshot>\n"
295 "\tsend [-nvPe] -t <receive_resume_token>\n"));
297 return (gettext("\tset <property=value> ... "
298 "<filesystem|volume|snapshot> ...\n"));
300 return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
302 return (gettext("\tsnapshot [-r] [-o property=value] ... "
303 "<filesystem|volume>@<snap> ...\n"));
305 return (gettext("\tunmount [-f] "
306 "<-a | filesystem|mountpoint>\n"));
308 return (gettext("\tunshare "
309 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
311 return (gettext("\tallow <filesystem|volume>\n"
313 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
314 "\t <filesystem|volume>\n"
315 "\tallow [-ld] -e <perm|@setname>[,...] "
316 "<filesystem|volume>\n"
317 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
318 "\tallow -s @setname <perm|@setname>[,...] "
319 "<filesystem|volume>\n"));
321 return (gettext("\tunallow [-rldug] "
322 "<\"everyone\"|user|group>[,...]\n"
323 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
324 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
325 "<filesystem|volume>\n"
326 "\tunallow [-r] -c [<perm|@setname>[,...]] "
327 "<filesystem|volume>\n"
328 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
329 "<filesystem|volume>\n"));
331 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
333 "\t [-S field] ... [-t type[,...]] "
334 "<filesystem|snapshot>\n"));
335 case HELP_GROUPSPACE:
336 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
338 "\t [-S field] ... [-t type[,...]] "
339 "<filesystem|snapshot>\n"));
340 case HELP_PROJECTSPACE:
341 return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
343 "\t [-S field] ... <filesystem|snapshot>\n"));
345 return (gettext("\tproject [-d|-r] <directory|file ...>\n"
346 "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
347 "\tproject -C [-k] [-r] <directory ...>\n"
348 "\tproject [-p id] [-r] [-s] <directory ...>\n"));
350 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
352 return (gettext("\tholds [-r] <snapshot> ...\n"));
354 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
356 return (gettext("\tdiff [-FHt] <snapshot> "
357 "[snapshot|filesystem]\n"));
359 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
360 case HELP_CHANNEL_PROGRAM:
361 return (gettext("\tprogram [-n] [-t <instruction limit>] "
362 "[-m <memory limit (b)>] <pool> <program file> "
365 return (gettext("\tload-key [-rn] [-L <keylocation>] "
366 "<-a | filesystem|volume>\n"));
367 case HELP_UNLOAD_KEY:
368 return (gettext("\tunload-key [-r] "
369 "<-a | filesystem|volume>\n"));
370 case HELP_CHANGE_KEY:
371 return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
372 "\t [-o keylocation=<value>] [-o pbkfd2iters=<value>]\n"
373 "\t <filesystem|volume>\n"
374 "\tchange-key -i [-l] <filesystem|volume>\n"));
384 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
389 * Utility function to guarantee malloc() success.
393 safe_malloc(size_t size)
397 if ((data = calloc(1, size)) == NULL)
404 safe_realloc(void *data, size_t size)
407 if ((newp = realloc(data, size)) == NULL) {
416 safe_strdup(char *str)
418 char *dupstr = strdup(str);
427 * Callback routine that will print out information for each of
431 usage_prop_cb(int prop, void *cb)
435 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
437 if (zfs_prop_readonly(prop))
438 (void) fprintf(fp, " NO ");
440 (void) fprintf(fp, "YES ");
442 if (zfs_prop_inheritable(prop))
443 (void) fprintf(fp, " YES ");
445 (void) fprintf(fp, " NO ");
447 if (zfs_prop_values(prop) == NULL)
448 (void) fprintf(fp, "-\n");
450 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
456 * Display usage message. If we're inside a command, display only the usage for
457 * that command. Otherwise, iterate over the entire command table and display
458 * a complete usage message.
461 usage(boolean_t requested)
464 boolean_t show_properties = B_FALSE;
465 FILE *fp = requested ? stdout : stderr;
467 if (current_command == NULL) {
469 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
471 gettext("where 'command' is one of the following:\n\n"));
473 for (i = 0; i < NCOMMAND; i++) {
474 if (command_table[i].name == NULL)
475 (void) fprintf(fp, "\n");
477 (void) fprintf(fp, "%s",
478 get_usage(command_table[i].usage));
481 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
482 "pool/[dataset/]*dataset[@name]\n"));
484 (void) fprintf(fp, gettext("usage:\n"));
485 (void) fprintf(fp, "%s", get_usage(current_command->usage));
488 if (current_command != NULL &&
489 (strcmp(current_command->name, "set") == 0 ||
490 strcmp(current_command->name, "get") == 0 ||
491 strcmp(current_command->name, "inherit") == 0 ||
492 strcmp(current_command->name, "list") == 0))
493 show_properties = B_TRUE;
495 if (show_properties) {
497 gettext("\nThe following properties are supported:\n"));
499 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
500 "PROPERTY", "EDIT", "INHERIT", "VALUES");
502 /* Iterate over all properties */
503 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
506 (void) fprintf(fp, "\t%-15s ", "userused@...");
507 (void) fprintf(fp, " NO NO <size>\n");
508 (void) fprintf(fp, "\t%-15s ", "groupused@...");
509 (void) fprintf(fp, " NO NO <size>\n");
510 (void) fprintf(fp, "\t%-15s ", "projectused@...");
511 (void) fprintf(fp, " NO NO <size>\n");
512 (void) fprintf(fp, "\t%-15s ", "userobjused@...");
513 (void) fprintf(fp, " NO NO <size>\n");
514 (void) fprintf(fp, "\t%-15s ", "groupobjused@...");
515 (void) fprintf(fp, " NO NO <size>\n");
516 (void) fprintf(fp, "\t%-15s ", "projectobjused@...");
517 (void) fprintf(fp, " NO NO <size>\n");
518 (void) fprintf(fp, "\t%-15s ", "userquota@...");
519 (void) fprintf(fp, "YES NO <size> | none\n");
520 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
521 (void) fprintf(fp, "YES NO <size> | none\n");
522 (void) fprintf(fp, "\t%-15s ", "projectquota@...");
523 (void) fprintf(fp, "YES NO <size> | none\n");
524 (void) fprintf(fp, "\t%-15s ", "userobjquota@...");
525 (void) fprintf(fp, "YES NO <size> | none\n");
526 (void) fprintf(fp, "\t%-15s ", "groupobjquota@...");
527 (void) fprintf(fp, "YES NO <size> | none\n");
528 (void) fprintf(fp, "\t%-15s ", "projectobjquota@...");
529 (void) fprintf(fp, "YES NO <size> | none\n");
530 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
531 (void) fprintf(fp, " NO NO <size>\n");
533 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
534 "with standard units such as K, M, G, etc.\n"));
535 (void) fprintf(fp, gettext("\nUser-defined properties can "
536 "be specified by using a name containing a colon (:).\n"));
537 (void) fprintf(fp, gettext("\nThe {user|group|project}"
538 "[obj]{used|quota}@ properties must be appended with\n"
539 "a user|group|project specifier of one of these forms:\n"
540 " POSIX name (eg: \"matt\")\n"
541 " POSIX id (eg: \"126829\")\n"
542 " SMB name@domain (eg: \"matt@sun\")\n"
543 " SMB SID (eg: \"S-1-234-567-89\")\n"));
546 gettext("\nFor the property list, run: %s\n"),
549 gettext("\nFor the delegated permission list, run: %s\n"),
550 "zfs allow|unallow");
554 * See comments at end of main().
556 if (getenv("ZFS_ABORT") != NULL) {
557 (void) printf("dumping core by request\n");
561 exit(requested ? 0 : 2);
565 * Take a property=value argument string and add it to the given nvlist.
566 * Modifies the argument inplace.
569 parseprop(nvlist_t *props, char *propname)
573 if ((propval = strchr(propname, '=')) == NULL) {
574 (void) fprintf(stderr, gettext("missing "
575 "'=' for property=value argument\n"));
580 if (nvlist_exists(props, propname)) {
581 (void) fprintf(stderr, gettext("property '%s' "
582 "specified multiple times\n"), propname);
585 if (nvlist_add_string(props, propname, propval) != 0)
591 * Take a property name argument and add it to the given nvlist.
592 * Modifies the argument inplace.
595 parsepropname(nvlist_t *props, char *propname)
597 if (strchr(propname, '=') != NULL) {
598 (void) fprintf(stderr, gettext("invalid character "
599 "'=' in property argument\n"));
602 if (nvlist_exists(props, propname)) {
603 (void) fprintf(stderr, gettext("property '%s' "
604 "specified multiple times\n"), propname);
607 if (nvlist_add_boolean(props, propname) != 0)
613 parse_depth(char *opt, int *flags)
618 depth = (int)strtol(opt, &tmp, 0);
620 (void) fprintf(stderr,
621 gettext("%s is not an integer\n"), optarg);
625 (void) fprintf(stderr,
626 gettext("Depth can not be negative.\n"));
629 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
633 #define PROGRESS_DELAY 2 /* seconds */
635 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";
636 static time_t pt_begin;
637 static char *pt_header = NULL;
638 static boolean_t pt_shown;
641 start_progress_timer(void)
643 pt_begin = time(NULL) + PROGRESS_DELAY;
648 set_progress_header(char *header)
650 assert(pt_header == NULL);
651 pt_header = safe_strdup(header);
653 (void) printf("%s: ", header);
654 (void) fflush(stdout);
659 update_progress(char *update)
661 if (!pt_shown && time(NULL) > pt_begin) {
662 int len = strlen(update);
664 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
666 (void) fflush(stdout);
668 } else if (pt_shown) {
669 int len = strlen(update);
671 (void) printf("%s%*.*s", update, len, len, pt_reverse);
672 (void) fflush(stdout);
677 finish_progress(char *done)
680 (void) printf("%s\n", done);
681 (void) fflush(stdout);
688 zfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type)
690 zfs_handle_t *zhp = NULL;
693 zhp = zfs_open(hdl, dataset, type);
698 * Volumes may neither be mounted or shared. Potentially in the
699 * future filesystems detected on these volumes could be mounted.
701 if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
707 * Mount and/or share the new filesystem as appropriate. We provide a
708 * verbose error message to let the user know that their filesystem was
709 * in fact created, even if we failed to mount or share it.
711 * If the user doesn't want the dataset automatically mounted, then
712 * skip the mount/share step
714 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type, B_FALSE) &&
715 zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON) {
716 if (geteuid() != 0) {
717 (void) fprintf(stderr, gettext("filesystem "
718 "successfully created, but it may only be "
719 "mounted by root\n"));
721 } else if (zfs_mount(zhp, NULL, 0) != 0) {
722 (void) fprintf(stderr, gettext("filesystem "
723 "successfully created, but not mounted\n"));
725 } else if (zfs_share(zhp) != 0) {
726 (void) fprintf(stderr, gettext("filesystem "
727 "successfully created, but not shared\n"));
738 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
740 * Given an existing dataset, create a writable copy whose initial contents
741 * are the same as the source. The newly created dataset maintains a
742 * dependency on the original; the original cannot be destroyed so long as
745 * The '-p' flag creates all the non-existing ancestors of the target first.
748 zfs_do_clone(int argc, char **argv)
750 zfs_handle_t *zhp = NULL;
751 boolean_t parents = B_FALSE;
756 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
760 while ((c = getopt(argc, argv, "o:p")) != -1) {
763 if (!parseprop(props, optarg)) {
772 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
781 /* check number of arguments */
783 (void) fprintf(stderr, gettext("missing source dataset "
788 (void) fprintf(stderr, gettext("missing target dataset "
793 (void) fprintf(stderr, gettext("too many arguments\n"));
797 /* open the source dataset */
798 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) {
803 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
806 * Now create the ancestors of the target dataset. If the
807 * target already exists and '-p' option was used we should not
810 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
816 if (zfs_create_ancestors(g_zfs, argv[1]) != 0) {
824 ret = zfs_clone(zhp, argv[1], props);
826 /* create the mountpoint if necessary */
829 (void) zpool_log_history(g_zfs, history_str);
830 log_history = B_FALSE;
833 ret = zfs_mount_and_share(g_zfs, argv[1], ZFS_TYPE_DATASET);
842 ASSERT3P(zhp, ==, NULL);
849 * zfs create [-p] [-o prop=value] ... fs
850 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
852 * Create a new dataset. This command can be used to create filesystems
853 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
854 * For volumes, the user must specify a size to be used.
856 * The '-s' flag applies only to volumes, and indicates that we should not try
857 * to set the reservation for this volume. By default we set a reservation
858 * equal to the size for any volume. For pools with SPA_VERSION >=
859 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
861 * The '-p' flag creates all the non-existing ancestors of the target first.
864 zfs_do_create(int argc, char **argv)
866 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
867 uint64_t volsize = 0;
869 boolean_t noreserve = B_FALSE;
870 boolean_t bflag = B_FALSE;
871 boolean_t parents = B_FALSE;
876 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
880 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
883 type = ZFS_TYPE_VOLUME;
884 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
885 (void) fprintf(stderr, gettext("bad volume "
886 "size '%s': %s\n"), optarg,
887 libzfs_error_description(g_zfs));
891 if (nvlist_add_uint64(props,
892 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
901 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
902 (void) fprintf(stderr, gettext("bad volume "
903 "block size '%s': %s\n"), optarg,
904 libzfs_error_description(g_zfs));
908 if (nvlist_add_uint64(props,
909 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
914 if (!parseprop(props, optarg))
921 (void) fprintf(stderr, gettext("missing size "
925 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
931 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
932 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
933 "used when creating a volume\n"));
940 /* check number of arguments */
942 (void) fprintf(stderr, gettext("missing %s argument\n"),
943 zfs_type_to_name(type));
947 (void) fprintf(stderr, gettext("too many arguments\n"));
951 if (type == ZFS_TYPE_VOLUME && !noreserve) {
952 zpool_handle_t *zpool_handle;
953 nvlist_t *real_props = NULL;
954 uint64_t spa_version;
956 zfs_prop_t resv_prop;
960 if ((p = strchr(argv[0], '/')) != NULL)
962 zpool_handle = zpool_open(g_zfs, argv[0]);
965 if (zpool_handle == NULL)
967 spa_version = zpool_get_prop_int(zpool_handle,
968 ZPOOL_PROP_VERSION, NULL);
969 if (spa_version >= SPA_VERSION_REFRESERVATION)
970 resv_prop = ZFS_PROP_REFRESERVATION;
972 resv_prop = ZFS_PROP_RESERVATION;
974 (void) snprintf(msg, sizeof (msg),
975 gettext("cannot create '%s'"), argv[0]);
976 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
977 props, 0, NULL, zpool_handle, B_TRUE, msg)) == NULL) {
978 zpool_close(zpool_handle);
981 zpool_close(zpool_handle);
983 volsize = zvol_volsize_to_reservation(volsize, real_props);
984 nvlist_free(real_props);
986 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
988 if (nvlist_add_uint64(props,
989 zfs_prop_to_name(resv_prop), volsize) != 0) {
996 if (parents && zfs_name_valid(argv[0], type)) {
998 * Now create the ancestors of target dataset. If the target
999 * already exists and '-p' option was used we should not
1002 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
1006 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
1010 /* pass to libzfs */
1011 if (zfs_create(g_zfs, argv[0], type, props) != 0)
1015 (void) zpool_log_history(g_zfs, history_str);
1016 log_history = B_FALSE;
1019 ret = zfs_mount_and_share(g_zfs, argv[0], ZFS_TYPE_DATASET);
1030 * zfs destroy [-rRf] <fs, vol>
1031 * zfs destroy [-rRd] <snap>
1033 * -r Recursively destroy all children
1034 * -R Recursively destroy all dependents, including clones
1035 * -f Force unmounting of any dependents
1036 * -d If we can't destroy now, mark for deferred destruction
1038 * Destroys the given dataset. By default, it will unmount any filesystems,
1039 * and refuse to destroy a dataset that has any dependents. A dependent can
1040 * either be a child, or a clone of a child.
1042 typedef struct destroy_cbdata {
1045 boolean_t cb_recurse;
1047 boolean_t cb_doclones;
1048 zfs_handle_t *cb_target;
1049 boolean_t cb_defer_destroy;
1050 boolean_t cb_verbose;
1051 boolean_t cb_parsable;
1052 boolean_t cb_dryrun;
1054 nvlist_t *cb_batchedsnaps;
1056 /* first snap in contiguous run */
1058 /* previous snap in contiguous run */
1060 int64_t cb_snapused;
1066 * Check for any dependents based on the '-r' or '-R' flags.
1069 destroy_check_dependent(zfs_handle_t *zhp, void *data)
1071 destroy_cbdata_t *cbp = data;
1072 const char *tname = zfs_get_name(cbp->cb_target);
1073 const char *name = zfs_get_name(zhp);
1075 if (strncmp(tname, name, strlen(tname)) == 0 &&
1076 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
1078 * This is a direct descendant, not a clone somewhere else in
1081 if (cbp->cb_recurse)
1084 if (cbp->cb_first) {
1085 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1086 "%s has children\n"),
1087 zfs_get_name(cbp->cb_target),
1088 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1089 (void) fprintf(stderr, gettext("use '-r' to destroy "
1090 "the following datasets:\n"));
1091 cbp->cb_first = B_FALSE;
1092 cbp->cb_error = B_TRUE;
1095 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1098 * This is a clone. We only want to report this if the '-r'
1099 * wasn't specified, or the target is a snapshot.
1101 if (!cbp->cb_recurse &&
1102 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1105 if (cbp->cb_first) {
1106 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1107 "%s has dependent clones\n"),
1108 zfs_get_name(cbp->cb_target),
1109 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1110 (void) fprintf(stderr, gettext("use '-R' to destroy "
1111 "the following datasets:\n"));
1112 cbp->cb_first = B_FALSE;
1113 cbp->cb_error = B_TRUE;
1114 cbp->cb_dryrun = B_TRUE;
1117 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1126 destroy_callback(zfs_handle_t *zhp, void *data)
1128 destroy_cbdata_t *cb = data;
1129 const char *name = zfs_get_name(zhp);
1131 if (cb->cb_verbose) {
1132 if (cb->cb_parsable) {
1133 (void) printf("destroy\t%s\n", name);
1134 } else if (cb->cb_dryrun) {
1135 (void) printf(gettext("would destroy %s\n"),
1138 (void) printf(gettext("will destroy %s\n"),
1144 * Ignore pools (which we've already flagged as an error before getting
1147 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1148 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1152 if (cb->cb_dryrun) {
1158 * We batch up all contiguous snapshots (even of different
1159 * filesystems) and destroy them with one ioctl. We can't
1160 * simply do all snap deletions and then all fs deletions,
1161 * because we must delete a clone before its origin.
1163 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1164 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1166 int error = zfs_destroy_snaps_nvl(g_zfs,
1167 cb->cb_batchedsnaps, B_FALSE);
1168 fnvlist_free(cb->cb_batchedsnaps);
1169 cb->cb_batchedsnaps = fnvlist_alloc();
1172 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1173 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1184 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1186 destroy_cbdata_t *cb = arg;
1187 const char *name = zfs_get_name(zhp);
1190 if (nvlist_exists(cb->cb_nvl, name)) {
1191 if (cb->cb_firstsnap == NULL)
1192 cb->cb_firstsnap = strdup(name);
1193 if (cb->cb_prevsnap != NULL)
1194 free(cb->cb_prevsnap);
1195 /* this snap continues the current range */
1196 cb->cb_prevsnap = strdup(name);
1197 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1199 if (cb->cb_verbose) {
1200 if (cb->cb_parsable) {
1201 (void) printf("destroy\t%s\n", name);
1202 } else if (cb->cb_dryrun) {
1203 (void) printf(gettext("would destroy %s\n"),
1206 (void) printf(gettext("will destroy %s\n"),
1210 } else if (cb->cb_firstsnap != NULL) {
1211 /* end of this range */
1213 err = lzc_snaprange_space(cb->cb_firstsnap,
1214 cb->cb_prevsnap, &used);
1215 cb->cb_snapused += used;
1216 free(cb->cb_firstsnap);
1217 cb->cb_firstsnap = NULL;
1218 free(cb->cb_prevsnap);
1219 cb->cb_prevsnap = NULL;
1226 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1229 assert(cb->cb_firstsnap == NULL);
1230 assert(cb->cb_prevsnap == NULL);
1231 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1232 if (cb->cb_firstsnap != NULL) {
1235 err = lzc_snaprange_space(cb->cb_firstsnap,
1236 cb->cb_prevsnap, &used);
1238 cb->cb_snapused += used;
1239 free(cb->cb_firstsnap);
1240 cb->cb_firstsnap = NULL;
1241 free(cb->cb_prevsnap);
1242 cb->cb_prevsnap = NULL;
1248 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1250 destroy_cbdata_t *cb = arg;
1253 /* Check for clones. */
1254 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1255 cb->cb_target = zhp;
1256 cb->cb_first = B_TRUE;
1257 err = zfs_iter_dependents(zhp, B_TRUE,
1258 destroy_check_dependent, cb);
1262 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1270 gather_snapshots(zfs_handle_t *zhp, void *arg)
1272 destroy_cbdata_t *cb = arg;
1275 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1281 if (cb->cb_verbose) {
1282 err = destroy_print_snapshots(zhp, cb);
1288 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1296 destroy_clones(destroy_cbdata_t *cb)
1299 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1301 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1302 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1305 boolean_t defer = cb->cb_defer_destroy;
1309 * We can't defer destroy non-snapshots, so set it to
1310 * false while destroying the clones.
1312 cb->cb_defer_destroy = B_FALSE;
1313 err = zfs_iter_dependents(zhp, B_FALSE,
1314 destroy_callback, cb);
1315 cb->cb_defer_destroy = defer;
1325 zfs_do_destroy(int argc, char **argv)
1327 destroy_cbdata_t cb = { 0 };
1331 zfs_handle_t *zhp = NULL;
1333 zfs_type_t type = ZFS_TYPE_DATASET;
1336 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1339 cb.cb_verbose = B_TRUE;
1342 cb.cb_verbose = B_TRUE;
1343 cb.cb_parsable = B_TRUE;
1346 cb.cb_dryrun = B_TRUE;
1349 cb.cb_defer_destroy = B_TRUE;
1350 type = ZFS_TYPE_SNAPSHOT;
1353 cb.cb_force = B_TRUE;
1356 cb.cb_recurse = B_TRUE;
1359 cb.cb_recurse = B_TRUE;
1360 cb.cb_doclones = B_TRUE;
1364 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1373 /* check number of arguments */
1375 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1379 (void) fprintf(stderr, gettext("too many arguments\n"));
1383 at = strchr(argv[0], '@');
1384 pound = strchr(argv[0], '#');
1387 /* Build the list of snaps to destroy in cb_nvl. */
1388 cb.cb_nvl = fnvlist_alloc();
1391 zhp = zfs_open(g_zfs, argv[0],
1392 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1394 nvlist_free(cb.cb_nvl);
1398 cb.cb_snapspec = at + 1;
1399 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1405 if (nvlist_empty(cb.cb_nvl)) {
1406 (void) fprintf(stderr, gettext("could not find any "
1407 "snapshots to destroy; check snapshot names.\n"));
1412 if (cb.cb_verbose) {
1414 zfs_nicebytes(cb.cb_snapused, buf, sizeof (buf));
1415 if (cb.cb_parsable) {
1416 (void) printf("reclaim\t%llu\n",
1417 (u_longlong_t)cb.cb_snapused);
1418 } else if (cb.cb_dryrun) {
1419 (void) printf(gettext("would reclaim %s\n"),
1422 (void) printf(gettext("will reclaim %s\n"),
1427 if (!cb.cb_dryrun) {
1428 if (cb.cb_doclones) {
1429 cb.cb_batchedsnaps = fnvlist_alloc();
1430 err = destroy_clones(&cb);
1432 err = zfs_destroy_snaps_nvl(g_zfs,
1433 cb.cb_batchedsnaps, B_FALSE);
1441 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1442 cb.cb_defer_destroy);
1448 } else if (pound != NULL) {
1453 (void) fprintf(stderr,
1454 "dryrun is not supported with bookmark\n");
1458 if (cb.cb_defer_destroy) {
1459 (void) fprintf(stderr,
1460 "defer destroy is not supported with bookmark\n");
1464 if (cb.cb_recurse) {
1465 (void) fprintf(stderr,
1466 "recursive is not supported with bookmark\n");
1470 if (!zfs_bookmark_exists(argv[0])) {
1471 (void) fprintf(stderr, gettext("bookmark '%s' "
1472 "does not exist.\n"), argv[0]);
1476 nvl = fnvlist_alloc();
1477 fnvlist_add_boolean(nvl, argv[0]);
1479 err = lzc_destroy_bookmarks(nvl, NULL);
1481 (void) zfs_standard_error(g_zfs, err,
1482 "cannot destroy bookmark");
1489 /* Open the given dataset */
1490 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1496 * Perform an explicit check for pools before going any further.
1498 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1499 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1500 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1501 "operation does not apply to pools\n"),
1503 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1504 "%s' to destroy all datasets in the pool\n"),
1506 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1507 "to destroy the pool itself\n"), zfs_get_name(zhp));
1513 * Check for any dependents and/or clones.
1515 cb.cb_first = B_TRUE;
1516 if (!cb.cb_doclones &&
1517 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1528 cb.cb_batchedsnaps = fnvlist_alloc();
1529 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1536 * Do the real thing. The callback will close the
1537 * handle regardless of whether it succeeds or not.
1539 err = destroy_callback(zhp, &cb);
1542 err = zfs_destroy_snaps_nvl(g_zfs,
1543 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1550 fnvlist_free(cb.cb_batchedsnaps);
1551 fnvlist_free(cb.cb_nvl);
1558 is_recvd_column(zprop_get_cbdata_t *cbp)
1561 zfs_get_column_t col;
1563 for (i = 0; i < ZFS_GET_NCOLS &&
1564 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1565 if (col == GET_COL_RECVD)
1571 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1572 * < all | property[,property]... > < fs | snap | vol > ...
1574 * -r recurse over any child datasets
1575 * -H scripted mode. Headers are stripped, and fields are separated
1576 * by tabs instead of spaces.
1577 * -o Set of fields to display. One of "name,property,value,
1578 * received,source". Default is "name,property,value,source".
1579 * "all" is an alias for all five.
1580 * -s Set of sources to allow. One of
1581 * "local,default,inherited,received,temporary,none". Default is
1583 * -p Display values in parsable (literal) format.
1585 * Prints properties for the given datasets. The user can control which
1586 * columns to display as well as which property types to allow.
1590 * Invoked to display the properties for a single dataset.
1593 get_callback(zfs_handle_t *zhp, void *data)
1595 char buf[ZFS_MAXPROPLEN];
1596 char rbuf[ZFS_MAXPROPLEN];
1597 zprop_source_t sourcetype;
1598 char source[ZFS_MAX_DATASET_NAME_LEN];
1599 zprop_get_cbdata_t *cbp = data;
1600 nvlist_t *user_props = zfs_get_user_props(zhp);
1601 zprop_list_t *pl = cbp->cb_proplist;
1605 boolean_t received = is_recvd_column(cbp);
1607 for (; pl != NULL; pl = pl->pl_next) {
1608 char *recvdval = NULL;
1610 * Skip the special fake placeholder. This will also skip over
1611 * the name property when 'all' is specified.
1613 if (pl->pl_prop == ZFS_PROP_NAME &&
1614 pl == cbp->cb_proplist)
1617 if (pl->pl_prop != ZPROP_INVAL) {
1618 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1619 sizeof (buf), &sourcetype, source,
1621 cbp->cb_literal) != 0) {
1624 if (!zfs_prop_valid_for_type(pl->pl_prop,
1625 ZFS_TYPE_DATASET, B_FALSE)) {
1626 (void) fprintf(stderr,
1627 gettext("No such property '%s'\n"),
1628 zfs_prop_to_name(pl->pl_prop));
1631 sourcetype = ZPROP_SRC_NONE;
1632 (void) strlcpy(buf, "-", sizeof (buf));
1635 if (received && (zfs_prop_get_recvd(zhp,
1636 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1637 cbp->cb_literal) == 0))
1640 zprop_print_one_property(zfs_get_name(zhp), cbp,
1641 zfs_prop_to_name(pl->pl_prop),
1642 buf, sourcetype, source, recvdval);
1643 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1644 sourcetype = ZPROP_SRC_LOCAL;
1646 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1647 buf, sizeof (buf), cbp->cb_literal) != 0) {
1648 sourcetype = ZPROP_SRC_NONE;
1649 (void) strlcpy(buf, "-", sizeof (buf));
1652 zprop_print_one_property(zfs_get_name(zhp), cbp,
1653 pl->pl_user_prop, buf, sourcetype, source, NULL);
1654 } else if (zfs_prop_written(pl->pl_user_prop)) {
1655 sourcetype = ZPROP_SRC_LOCAL;
1657 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1658 buf, sizeof (buf), cbp->cb_literal) != 0) {
1659 sourcetype = ZPROP_SRC_NONE;
1660 (void) strlcpy(buf, "-", sizeof (buf));
1663 zprop_print_one_property(zfs_get_name(zhp), cbp,
1664 pl->pl_user_prop, buf, sourcetype, source, NULL);
1666 if (nvlist_lookup_nvlist(user_props,
1667 pl->pl_user_prop, &propval) != 0) {
1670 sourcetype = ZPROP_SRC_NONE;
1673 verify(nvlist_lookup_string(propval,
1674 ZPROP_VALUE, &strval) == 0);
1675 verify(nvlist_lookup_string(propval,
1676 ZPROP_SOURCE, &sourceval) == 0);
1678 if (strcmp(sourceval,
1679 zfs_get_name(zhp)) == 0) {
1680 sourcetype = ZPROP_SRC_LOCAL;
1681 } else if (strcmp(sourceval,
1682 ZPROP_SOURCE_VAL_RECVD) == 0) {
1683 sourcetype = ZPROP_SRC_RECEIVED;
1685 sourcetype = ZPROP_SRC_INHERITED;
1686 (void) strlcpy(source,
1687 sourceval, sizeof (source));
1691 if (received && (zfs_prop_get_recvd(zhp,
1692 pl->pl_user_prop, rbuf, sizeof (rbuf),
1693 cbp->cb_literal) == 0))
1696 zprop_print_one_property(zfs_get_name(zhp), cbp,
1697 pl->pl_user_prop, strval, sourcetype,
1706 zfs_do_get(int argc, char **argv)
1708 zprop_get_cbdata_t cb = { 0 };
1709 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1710 int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
1711 char *value, *fields;
1714 zprop_list_t fake_name = { 0 };
1717 * Set up default columns and sources.
1719 cb.cb_sources = ZPROP_SRC_ALL;
1720 cb.cb_columns[0] = GET_COL_NAME;
1721 cb.cb_columns[1] = GET_COL_PROPERTY;
1722 cb.cb_columns[2] = GET_COL_VALUE;
1723 cb.cb_columns[3] = GET_COL_SOURCE;
1724 cb.cb_type = ZFS_TYPE_DATASET;
1727 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1730 cb.cb_literal = B_TRUE;
1733 limit = parse_depth(optarg, &flags);
1736 flags |= ZFS_ITER_RECURSE;
1739 cb.cb_scripted = B_TRUE;
1742 (void) fprintf(stderr, gettext("missing argument for "
1743 "'%c' option\n"), optopt);
1748 * Process the set of columns to display. We zero out
1749 * the structure to give us a blank slate.
1751 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1753 while (*optarg != '\0') {
1754 static char *col_subopts[] =
1755 { "name", "property", "value", "received",
1756 "source", "all", NULL };
1758 if (i == ZFS_GET_NCOLS) {
1759 (void) fprintf(stderr, gettext("too "
1760 "many fields given to -o "
1765 switch (getsubopt(&optarg, col_subopts,
1768 cb.cb_columns[i++] = GET_COL_NAME;
1771 cb.cb_columns[i++] = GET_COL_PROPERTY;
1774 cb.cb_columns[i++] = GET_COL_VALUE;
1777 cb.cb_columns[i++] = GET_COL_RECVD;
1778 flags |= ZFS_ITER_RECVD_PROPS;
1781 cb.cb_columns[i++] = GET_COL_SOURCE;
1785 (void) fprintf(stderr,
1786 gettext("\"all\" conflicts "
1787 "with specific fields "
1788 "given to -o option\n"));
1791 cb.cb_columns[0] = GET_COL_NAME;
1792 cb.cb_columns[1] = GET_COL_PROPERTY;
1793 cb.cb_columns[2] = GET_COL_VALUE;
1794 cb.cb_columns[3] = GET_COL_RECVD;
1795 cb.cb_columns[4] = GET_COL_SOURCE;
1796 flags |= ZFS_ITER_RECVD_PROPS;
1800 (void) fprintf(stderr,
1801 gettext("invalid column name "
1810 while (*optarg != '\0') {
1811 static char *source_subopts[] = {
1812 "local", "default", "inherited",
1813 "received", "temporary", "none",
1816 switch (getsubopt(&optarg, source_subopts,
1819 cb.cb_sources |= ZPROP_SRC_LOCAL;
1822 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1825 cb.cb_sources |= ZPROP_SRC_INHERITED;
1828 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1831 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1834 cb.cb_sources |= ZPROP_SRC_NONE;
1837 (void) fprintf(stderr,
1838 gettext("invalid source "
1847 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1848 while (*optarg != '\0') {
1849 static char *type_subopts[] = { "filesystem",
1850 "volume", "snapshot", "bookmark",
1853 switch (getsubopt(&optarg, type_subopts,
1856 types |= ZFS_TYPE_FILESYSTEM;
1859 types |= ZFS_TYPE_VOLUME;
1862 types |= ZFS_TYPE_SNAPSHOT;
1865 types |= ZFS_TYPE_BOOKMARK;
1868 types = ZFS_TYPE_DATASET |
1873 (void) fprintf(stderr,
1874 gettext("invalid type '%s'\n"),
1882 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1892 (void) fprintf(stderr, gettext("missing property "
1899 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1907 * As part of zfs_expand_proplist(), we keep track of the maximum column
1908 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1909 * need to know the maximum name length. However, the user likely did
1910 * not specify 'name' as one of the properties to fetch, so we need to
1911 * make sure we always include at least this property for
1912 * print_get_headers() to work properly.
1914 if (cb.cb_proplist != NULL) {
1915 fake_name.pl_prop = ZFS_PROP_NAME;
1916 fake_name.pl_width = strlen(gettext("NAME"));
1917 fake_name.pl_next = cb.cb_proplist;
1918 cb.cb_proplist = &fake_name;
1921 cb.cb_first = B_TRUE;
1923 /* run for each object */
1924 ret = zfs_for_each(argc, argv, flags, types, NULL,
1925 &cb.cb_proplist, limit, get_callback, &cb);
1927 if (cb.cb_proplist == &fake_name)
1928 zprop_free_list(fake_name.pl_next);
1930 zprop_free_list(cb.cb_proplist);
1936 * inherit [-rS] <property> <fs|vol> ...
1938 * -r Recurse over all children
1939 * -S Revert to received value, if any
1941 * For each dataset specified on the command line, inherit the given property
1942 * from its parent. Inheriting a property at the pool level will cause it to
1943 * use the default value. The '-r' flag will recurse over all children, and is
1944 * useful for setting a property on a hierarchy-wide basis, regardless of any
1945 * local modifications for each dataset.
1948 typedef struct inherit_cbdata {
1949 const char *cb_propname;
1950 boolean_t cb_received;
1954 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1956 inherit_cbdata_t *cb = data;
1957 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1960 * If we're doing it recursively, then ignore properties that
1961 * are not valid for this type of dataset.
1963 if (prop != ZPROP_INVAL &&
1964 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp), B_FALSE))
1967 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1971 inherit_cb(zfs_handle_t *zhp, void *data)
1973 inherit_cbdata_t *cb = data;
1975 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1979 zfs_do_inherit(int argc, char **argv)
1983 inherit_cbdata_t cb = { 0 };
1987 boolean_t received = B_FALSE;
1990 while ((c = getopt(argc, argv, "rS")) != -1) {
1993 flags |= ZFS_ITER_RECURSE;
2000 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2009 /* check number of arguments */
2011 (void) fprintf(stderr, gettext("missing property argument\n"));
2015 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2023 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
2024 if (zfs_prop_readonly(prop)) {
2025 (void) fprintf(stderr, gettext(
2026 "%s property is read-only\n"),
2030 if (!zfs_prop_inheritable(prop) && !received) {
2031 (void) fprintf(stderr, gettext("'%s' property cannot "
2032 "be inherited\n"), propname);
2033 if (prop == ZFS_PROP_QUOTA ||
2034 prop == ZFS_PROP_RESERVATION ||
2035 prop == ZFS_PROP_REFQUOTA ||
2036 prop == ZFS_PROP_REFRESERVATION) {
2037 (void) fprintf(stderr, gettext("use 'zfs set "
2038 "%s=none' to clear\n"), propname);
2039 (void) fprintf(stderr, gettext("use 'zfs "
2040 "inherit -S %s' to revert to received "
2041 "value\n"), propname);
2045 if (received && (prop == ZFS_PROP_VOLSIZE ||
2046 prop == ZFS_PROP_VERSION)) {
2047 (void) fprintf(stderr, gettext("'%s' property cannot "
2048 "be reverted to a received value\n"), propname);
2051 } else if (!zfs_prop_user(propname)) {
2052 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
2057 cb.cb_propname = propname;
2058 cb.cb_received = received;
2060 if (flags & ZFS_ITER_RECURSE) {
2061 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2062 NULL, NULL, 0, inherit_recurse_cb, &cb);
2064 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2065 NULL, NULL, 0, inherit_cb, &cb);
2071 typedef struct upgrade_cbdata {
2072 uint64_t cb_numupgraded;
2073 uint64_t cb_numsamegraded;
2074 uint64_t cb_numfailed;
2075 uint64_t cb_version;
2077 boolean_t cb_foundone;
2078 char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
2082 same_pool(zfs_handle_t *zhp, const char *name)
2084 int len1 = strcspn(name, "/@");
2085 const char *zhname = zfs_get_name(zhp);
2086 int len2 = strcspn(zhname, "/@");
2090 return (strncmp(name, zhname, len1) == 0);
2094 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2096 upgrade_cbdata_t *cb = data;
2097 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2099 /* list if it's old/new */
2100 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2101 (cb->cb_newer && version > ZPL_VERSION)) {
2104 str = gettext("The following filesystems are "
2105 "formatted using a newer software version and\n"
2106 "cannot be accessed on the current system.\n\n");
2108 str = gettext("The following filesystems are "
2109 "out of date, and can be upgraded. After being\n"
2110 "upgraded, these filesystems (and any 'zfs send' "
2111 "streams generated from\n"
2112 "subsequent snapshots) will no longer be "
2113 "accessible by older software versions.\n\n");
2116 if (!cb->cb_foundone) {
2118 (void) printf(gettext("VER FILESYSTEM\n"));
2119 (void) printf(gettext("--- ------------\n"));
2120 cb->cb_foundone = B_TRUE;
2123 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2130 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2132 upgrade_cbdata_t *cb = data;
2133 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2134 int needed_spa_version;
2137 if (zfs_spa_version(zhp, &spa_version) < 0)
2140 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2142 if (needed_spa_version < 0)
2145 if (spa_version < needed_spa_version) {
2147 (void) printf(gettext("%s: can not be "
2148 "upgraded; the pool version needs to first "
2149 "be upgraded\nto version %d\n\n"),
2150 zfs_get_name(zhp), needed_spa_version);
2156 if (version < cb->cb_version) {
2158 (void) snprintf(verstr, sizeof (verstr),
2159 "%llu", (u_longlong_t)cb->cb_version);
2160 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2162 * If they did "zfs upgrade -a", then we could
2163 * be doing ioctls to different pools. We need
2164 * to log this history once to each pool, and bypass
2165 * the normal history logging that happens in main().
2167 (void) zpool_log_history(g_zfs, history_str);
2168 log_history = B_FALSE;
2170 if (zfs_prop_set(zhp, "version", verstr) == 0)
2171 cb->cb_numupgraded++;
2174 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2175 } else if (version > cb->cb_version) {
2176 /* can't downgrade */
2177 (void) printf(gettext("%s: can not be downgraded; "
2178 "it is already at version %u\n"),
2179 zfs_get_name(zhp), version);
2182 cb->cb_numsamegraded++;
2190 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2193 zfs_do_upgrade(int argc, char **argv)
2195 boolean_t all = B_FALSE;
2196 boolean_t showversions = B_FALSE;
2198 upgrade_cbdata_t cb = { 0 };
2200 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2203 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2206 flags |= ZFS_ITER_RECURSE;
2209 showversions = B_TRUE;
2212 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2213 optarg, &cb.cb_version) != 0) {
2214 (void) fprintf(stderr,
2215 gettext("invalid version %s\n"), optarg);
2224 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2233 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2235 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2236 cb.cb_version || argc))
2238 if ((all || argc) && (showversions))
2244 /* Show info on available versions. */
2245 (void) printf(gettext("The following filesystem versions are "
2247 (void) printf(gettext("VER DESCRIPTION\n"));
2248 (void) printf("--- -----------------------------------------"
2249 "---------------\n");
2250 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2251 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2252 (void) printf(gettext(" 3 Case insensitive and filesystem "
2253 "user identifier (FUID)\n"));
2254 (void) printf(gettext(" 4 userquota, groupquota "
2256 (void) printf(gettext(" 5 System attributes\n"));
2257 (void) printf(gettext("\nFor more information on a particular "
2258 "version, including supported releases,\n"));
2259 (void) printf("see the ZFS Administration Guide.\n\n");
2261 } else if (argc || all) {
2262 /* Upgrade filesystems */
2263 if (cb.cb_version == 0)
2264 cb.cb_version = ZPL_VERSION;
2265 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2266 NULL, NULL, 0, upgrade_set_callback, &cb);
2267 (void) printf(gettext("%llu filesystems upgraded\n"),
2268 (u_longlong_t)cb.cb_numupgraded);
2269 if (cb.cb_numsamegraded) {
2270 (void) printf(gettext("%llu filesystems already at "
2272 (u_longlong_t)cb.cb_numsamegraded);
2274 if (cb.cb_numfailed != 0)
2277 /* List old-version filesystems */
2279 (void) printf(gettext("This system is currently running "
2280 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2282 flags |= ZFS_ITER_RECURSE;
2283 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2284 NULL, NULL, 0, upgrade_list_callback, &cb);
2286 found = cb.cb_foundone;
2287 cb.cb_foundone = B_FALSE;
2288 cb.cb_newer = B_TRUE;
2290 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2291 NULL, NULL, 0, upgrade_list_callback, &cb);
2293 if (!cb.cb_foundone && !found) {
2294 (void) printf(gettext("All filesystems are "
2295 "formatted with the current version.\n"));
2303 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2304 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2305 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2306 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2307 * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2308 * [-S field [-S field]...] filesystem | snapshot
2310 * -H Scripted mode; elide headers and separate columns by tabs.
2311 * -i Translate SID to POSIX ID.
2312 * -n Print numeric ID instead of user/group name.
2313 * -o Control which fields to display.
2314 * -p Use exact (parsable) numeric output.
2315 * -s Specify sort columns, descending order.
2316 * -S Specify sort columns, ascending order.
2317 * -t Control which object types to display.
2319 * Displays space consumed by, and quotas on, each user in the specified
2320 * filesystem or snapshot.
2323 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2324 enum us_field_types {
2332 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA",
2333 "OBJUSED", "OBJQUOTA" };
2334 static char *us_field_names[] = { "type", "name", "used", "quota",
2335 "objused", "objquota" };
2336 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2338 #define USTYPE_PSX_GRP (1 << 0)
2339 #define USTYPE_PSX_USR (1 << 1)
2340 #define USTYPE_SMB_GRP (1 << 2)
2341 #define USTYPE_SMB_USR (1 << 3)
2342 #define USTYPE_PROJ (1 << 4)
2343 #define USTYPE_ALL \
2344 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2347 static int us_type_bits[] = {
2354 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2357 typedef struct us_node {
2359 uu_avl_node_t usn_avlnode;
2360 uu_list_node_t usn_listnode;
2363 typedef struct us_cbdata {
2365 uu_avl_pool_t *cb_avl_pool;
2367 boolean_t cb_numname;
2368 boolean_t cb_nicenum;
2369 boolean_t cb_sid2posix;
2370 zfs_userquota_prop_t cb_prop;
2371 zfs_sort_column_t *cb_sortcol;
2372 size_t cb_width[USFIELD_LAST];
2375 static boolean_t us_populated = B_FALSE;
2378 zfs_sort_column_t *si_sortcol;
2379 boolean_t si_numname;
2383 us_field_index(char *field)
2387 for (i = 0; i < USFIELD_LAST; i++) {
2388 if (strcmp(field, us_field_names[i]) == 0)
2396 us_compare(const void *larg, const void *rarg, void *unused)
2398 const us_node_t *l = larg;
2399 const us_node_t *r = rarg;
2400 us_sort_info_t *si = (us_sort_info_t *)unused;
2401 zfs_sort_column_t *sortcol = si->si_sortcol;
2402 boolean_t numname = si->si_numname;
2403 nvlist_t *lnvl = l->usn_nvl;
2404 nvlist_t *rnvl = r->usn_nvl;
2408 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2415 zfs_prop_t prop = sortcol->sc_prop;
2416 const char *propname = NULL;
2417 boolean_t reverse = sortcol->sc_reverse;
2422 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2423 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2425 rc = (rv32 < lv32) ? 1 : -1;
2431 (void) nvlist_lookup_uint64(lnvl, propname,
2433 (void) nvlist_lookup_uint64(rnvl, propname,
2436 rc = (rv64 < lv64) ? 1 : -1;
2438 if ((nvlist_lookup_string(lnvl, propname,
2439 &lvstr) == ENOENT) ||
2440 (nvlist_lookup_string(rnvl, propname,
2441 &rvstr) == ENOENT)) {
2444 rc = strcmp(lvstr, rvstr);
2448 case ZFS_PROP_QUOTA:
2451 if (prop == ZFS_PROP_USED)
2455 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2456 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2458 rc = (rv64 < lv64) ? 1 : -1;
2467 return (reverse ? 1 : -1);
2469 return (reverse ? -1 : 1);
2474 * If entries still seem to be the same, check if they are of the same
2475 * type (smbentity is added only if we are doing SID to POSIX ID
2476 * translation where we can have duplicate type/name combinations).
2478 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2479 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2481 return (lvb < rvb ? -1 : 1);
2487 zfs_prop_is_user(unsigned p)
2489 return (p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA ||
2490 p == ZFS_PROP_USEROBJUSED || p == ZFS_PROP_USEROBJQUOTA);
2494 zfs_prop_is_group(unsigned p)
2496 return (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA ||
2497 p == ZFS_PROP_GROUPOBJUSED || p == ZFS_PROP_GROUPOBJQUOTA);
2501 zfs_prop_is_project(unsigned p)
2503 return (p == ZFS_PROP_PROJECTUSED || p == ZFS_PROP_PROJECTQUOTA ||
2504 p == ZFS_PROP_PROJECTOBJUSED || p == ZFS_PROP_PROJECTOBJQUOTA);
2507 static inline const char *
2508 us_type2str(unsigned field_type)
2510 switch (field_type) {
2511 case USTYPE_PSX_USR:
2512 return ("POSIX User");
2513 case USTYPE_PSX_GRP:
2514 return ("POSIX Group");
2515 case USTYPE_SMB_USR:
2516 return ("SMB User");
2517 case USTYPE_SMB_GRP:
2518 return ("SMB Group");
2522 return ("Undefined");
2527 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2529 us_cbdata_t *cb = (us_cbdata_t *)arg;
2530 zfs_userquota_prop_t prop = cb->cb_prop;
2535 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2536 uu_avl_t *avl = cb->cb_avl;
2540 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2542 const char *typestr;
2546 int typeidx, nameidx, sizeidx;
2547 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2548 boolean_t smbentity = B_FALSE;
2550 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2552 node = safe_malloc(sizeof (us_node_t));
2553 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2554 node->usn_nvl = props;
2556 if (domain != NULL && domain[0] != '\0') {
2559 char sid[MAXNAMELEN + 32];
2563 directory_error_t e;
2567 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2569 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2570 type = USTYPE_SMB_GRP;
2571 err = sid_to_id(sid, B_FALSE, &id);
2573 type = USTYPE_SMB_USR;
2574 err = sid_to_id(sid, B_TRUE, &id);
2579 if (!cb->cb_sid2posix) {
2580 e = directory_name_from_sid(NULL, sid, &name,
2583 directory_error_free(e);
2593 #endif /* HAVE_IDMAP */
2596 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2598 if (zfs_prop_is_group(prop)) {
2599 type = USTYPE_PSX_GRP;
2600 if (!cb->cb_numname) {
2603 if ((g = getgrgid(rid)) != NULL)
2606 } else if (zfs_prop_is_user(prop)) {
2607 type = USTYPE_PSX_USR;
2608 if (!cb->cb_numname) {
2611 if ((p = getpwuid(rid)) != NULL)
2620 * Make sure that the type/name combination is unique when doing
2621 * SID to POSIX ID translation (hence changing the type from SMB to
2624 if (cb->cb_sid2posix &&
2625 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2628 /* Calculate/update width of TYPE field */
2629 typestr = us_type2str(type);
2630 typelen = strlen(gettext(typestr));
2631 typeidx = us_field_index("type");
2632 if (typelen > cb->cb_width[typeidx])
2633 cb->cb_width[typeidx] = typelen;
2634 if (nvlist_add_uint32(props, "type", type) != 0)
2637 /* Calculate/update width of NAME field */
2638 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2639 if (nvlist_add_uint64(props, "name", rid) != 0)
2641 namelen = snprintf(NULL, 0, "%u", rid);
2643 if (nvlist_add_string(props, "name", name) != 0)
2645 namelen = strlen(name);
2647 nameidx = us_field_index("name");
2648 if (nameidx >= 0 && namelen > cb->cb_width[nameidx])
2649 cb->cb_width[nameidx] = namelen;
2652 * Check if this type/name combination is in the list and update it;
2653 * otherwise add new node to the list.
2655 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2656 uu_avl_insert(avl, node, idx);
2661 props = node->usn_nvl;
2664 /* Calculate/update width of USED/QUOTA fields */
2665 if (cb->cb_nicenum) {
2666 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
2667 prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
2668 prop == ZFS_PROP_PROJECTUSED ||
2669 prop == ZFS_PROP_PROJECTQUOTA) {
2670 zfs_nicebytes(space, sizebuf, sizeof (sizebuf));
2672 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2675 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
2676 (u_longlong_t)space);
2678 sizelen = strlen(sizebuf);
2679 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
2680 prop == ZFS_PROP_PROJECTUSED) {
2682 if (!nvlist_exists(props, "quota"))
2683 (void) nvlist_add_uint64(props, "quota", 0);
2684 } else if (prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
2685 prop == ZFS_PROP_PROJECTQUOTA) {
2687 if (!nvlist_exists(props, "used"))
2688 (void) nvlist_add_uint64(props, "used", 0);
2689 } else if (prop == ZFS_PROP_USEROBJUSED ||
2690 prop == ZFS_PROP_GROUPOBJUSED || prop == ZFS_PROP_PROJECTOBJUSED) {
2691 propname = "objused";
2692 if (!nvlist_exists(props, "objquota"))
2693 (void) nvlist_add_uint64(props, "objquota", 0);
2694 } else if (prop == ZFS_PROP_USEROBJQUOTA ||
2695 prop == ZFS_PROP_GROUPOBJQUOTA ||
2696 prop == ZFS_PROP_PROJECTOBJQUOTA) {
2697 propname = "objquota";
2698 if (!nvlist_exists(props, "objused"))
2699 (void) nvlist_add_uint64(props, "objused", 0);
2703 sizeidx = us_field_index(propname);
2704 if (sizeidx >= 0 && sizelen > cb->cb_width[sizeidx])
2705 cb->cb_width[sizeidx] = sizelen;
2707 if (nvlist_add_uint64(props, propname, space) != 0)
2714 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2715 size_t *width, us_node_t *node)
2717 nvlist_t *nvl = node->usn_nvl;
2718 char valstr[MAXNAMELEN];
2719 boolean_t first = B_TRUE;
2725 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2726 if (!(ustype & types))
2729 while ((field = fields[cfield]) != USFIELD_LAST) {
2730 nvpair_t *nvp = NULL;
2736 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2737 if (strcmp(nvpair_name(nvp),
2738 us_field_names[field]) == 0)
2742 type = nvp == NULL ? DATA_TYPE_UNKNOWN : nvpair_type(nvp);
2744 case DATA_TYPE_UINT32:
2745 (void) nvpair_value_uint32(nvp, &val32);
2747 case DATA_TYPE_UINT64:
2748 (void) nvpair_value_uint64(nvp, &val64);
2750 case DATA_TYPE_STRING:
2751 (void) nvpair_value_string(nvp, &strval);
2753 case DATA_TYPE_UNKNOWN:
2756 (void) fprintf(stderr, "invalid data type\n");
2761 if (type == DATA_TYPE_UINT32)
2762 strval = (char *)us_type2str(val32);
2765 if (type == DATA_TYPE_UINT64) {
2766 (void) sprintf(valstr, "%llu",
2767 (u_longlong_t)val64);
2773 if (type == DATA_TYPE_UINT64) {
2775 (void) sprintf(valstr, "%llu",
2776 (u_longlong_t)val64);
2778 } else if (field == USFIELD_QUOTA &&
2782 zfs_nicebytes(val64, valstr,
2788 case USFIELD_OBJUSED:
2789 case USFIELD_OBJQUOTA:
2790 if (type == DATA_TYPE_UINT64) {
2792 (void) sprintf(valstr, "%llu",
2793 (u_longlong_t)val64);
2795 } else if (field == USFIELD_OBJQUOTA &&
2799 zfs_nicenum(val64, valstr,
2809 (void) printf("\t");
2814 (void) printf("%s", strval);
2815 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2816 (void) printf("%-*s", (int)width[field], strval);
2818 (void) printf("%*s", (int)width[field], strval);
2824 (void) printf("\n");
2828 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2829 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2837 boolean_t first = B_TRUE;
2839 while ((field = fields[cfield]) != USFIELD_LAST) {
2840 col = gettext(us_field_hdr[field]);
2841 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2842 (void) printf(first ? "%-*s" : " %-*s",
2843 (int)width[field], col);
2845 (void) printf(first ? "%*s" : " %*s",
2846 (int)width[field], col);
2851 (void) printf("\n");
2854 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2855 print_us_node(scripted, parsable, fields, types, width, node);
2857 nvlist_free(node->usn_nvl);
2862 zfs_do_userspace(int argc, char **argv)
2865 zfs_userquota_prop_t p;
2866 uu_avl_pool_t *avl_pool;
2868 uu_avl_walk_t *walk;
2870 char deffields[] = "type,name,used,quota,objused,objquota";
2871 char *ofield = NULL;
2872 char *tfield = NULL;
2876 boolean_t scripted = B_FALSE;
2877 boolean_t prtnum = B_FALSE;
2878 boolean_t parsable = B_FALSE;
2879 boolean_t sid2posix = B_FALSE;
2882 zfs_sort_column_t *sortcol = NULL;
2883 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2887 uu_list_pool_t *listpool;
2889 uu_avl_index_t idx = 0;
2890 uu_list_index_t idx2 = 0;
2895 if (strcmp(argv[0], "groupspace") == 0) {
2896 /* Toggle default group types */
2897 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2898 } else if (strcmp(argv[0], "projectspace") == 0) {
2899 types = USTYPE_PROJ;
2903 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2906 if (types == USTYPE_PROJ) {
2907 (void) fprintf(stderr,
2908 gettext("invalid option 'n'\n"));
2924 if (zfs_add_sort_column(&sortcol, optarg,
2925 c == 's' ? B_FALSE : B_TRUE) != 0) {
2926 (void) fprintf(stderr,
2927 gettext("invalid field '%s'\n"), optarg);
2932 if (types == USTYPE_PROJ) {
2933 (void) fprintf(stderr,
2934 gettext("invalid option 't'\n"));
2940 if (types == USTYPE_PROJ) {
2941 (void) fprintf(stderr,
2942 gettext("invalid option 'i'\n"));
2948 (void) fprintf(stderr, gettext("missing argument for "
2949 "'%c' option\n"), optopt);
2953 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2963 (void) fprintf(stderr, gettext("missing dataset name\n"));
2967 (void) fprintf(stderr, gettext("too many arguments\n"));
2971 /* Use default output fields if not specified using -o */
2975 if ((delim = strchr(ofield, ',')) != NULL)
2977 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2978 (void) fprintf(stderr, gettext("invalid type '%s' "
2979 "for -o option\n"), ofield);
2984 } while (delim != NULL);
2985 fields[cfield] = USFIELD_LAST;
2987 /* Override output types (-t option) */
2988 if (tfield != NULL) {
2992 boolean_t found = B_FALSE;
2994 if ((delim = strchr(tfield, ',')) != NULL)
2996 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
2998 if (strcmp(tfield, us_type_names[i]) == 0) {
3000 types |= us_type_bits[i];
3005 (void) fprintf(stderr, gettext("invalid type "
3006 "'%s' for -t option\n"), tfield);
3011 } while (delim != NULL);
3014 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
3017 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
3018 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
3020 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
3023 /* Always add default sorting columns */
3024 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
3025 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
3027 cb.cb_sortcol = sortcol;
3028 cb.cb_numname = prtnum;
3029 cb.cb_nicenum = !parsable;
3030 cb.cb_avl_pool = avl_pool;
3031 cb.cb_avl = avl_tree;
3032 cb.cb_sid2posix = sid2posix;
3034 for (i = 0; i < USFIELD_LAST; i++)
3035 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
3037 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
3038 if ((zfs_prop_is_user(p) &&
3039 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
3040 (zfs_prop_is_group(p) &&
3041 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))) ||
3042 (zfs_prop_is_project(p) && types != USTYPE_PROJ))
3046 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
3051 if ((node = uu_avl_first(avl_tree)) == NULL)
3054 us_populated = B_TRUE;
3056 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
3057 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
3058 list = uu_list_create(listpool, NULL, UU_DEFAULT);
3059 uu_list_node_init(node, &node->usn_listnode, listpool);
3061 while (node != NULL) {
3063 node = uu_avl_next(avl_tree, node);
3064 uu_avl_remove(avl_tree, rmnode);
3065 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
3066 uu_list_insert(list, rmnode, idx2);
3069 for (node = uu_list_first(list); node != NULL;
3070 node = uu_list_next(list, node)) {
3071 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
3073 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
3074 uu_avl_insert(avl_tree, node, idx);
3077 uu_list_destroy(list);
3078 uu_list_pool_destroy(listpool);
3080 /* Print and free node nvlist memory */
3081 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
3084 zfs_free_sort_columns(sortcol);
3086 /* Clean up the AVL tree */
3087 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
3090 while ((node = uu_avl_walk_next(walk)) != NULL) {
3091 uu_avl_remove(cb.cb_avl, node);
3095 uu_avl_walk_end(walk);
3096 uu_avl_destroy(avl_tree);
3097 uu_avl_pool_destroy(avl_pool);
3103 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3104 * [-t type[,...]] [filesystem|volume|snapshot] ...
3106 * -H Scripted mode; elide headers and separate columns by tabs
3107 * -p Display values in parsable (literal) format.
3108 * -r Recurse over all children
3109 * -d Limit recursion by depth.
3110 * -o Control which fields to display.
3111 * -s Specify sort columns, descending order.
3112 * -S Specify sort columns, ascending order.
3113 * -t Control which object types to display.
3115 * When given no arguments, list all filesystems in the system.
3116 * Otherwise, list the specified datasets, optionally recursing down them if
3117 * '-r' is specified.
3119 typedef struct list_cbdata {
3121 boolean_t cb_literal;
3122 boolean_t cb_scripted;
3123 zprop_list_t *cb_proplist;
3127 * Given a list of columns to display, output appropriate headers for each one.
3130 print_header(list_cbdata_t *cb)
3132 zprop_list_t *pl = cb->cb_proplist;
3133 char headerbuf[ZFS_MAXPROPLEN];
3136 boolean_t first = B_TRUE;
3137 boolean_t right_justify;
3139 for (; pl != NULL; pl = pl->pl_next) {
3146 right_justify = B_FALSE;
3147 if (pl->pl_prop != ZPROP_INVAL) {
3148 header = zfs_prop_column_name(pl->pl_prop);
3149 right_justify = zfs_prop_align_right(pl->pl_prop);
3151 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3152 headerbuf[i] = toupper(pl->pl_user_prop[i]);
3153 headerbuf[i] = '\0';
3157 if (pl->pl_next == NULL && !right_justify)
3158 (void) printf("%s", header);
3159 else if (right_justify)
3160 (void) printf("%*s", (int)pl->pl_width, header);
3162 (void) printf("%-*s", (int)pl->pl_width, header);
3165 (void) printf("\n");
3169 * Given a dataset and a list of fields, print out all the properties according
3170 * to the described layout.
3173 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3175 zprop_list_t *pl = cb->cb_proplist;
3176 boolean_t first = B_TRUE;
3177 char property[ZFS_MAXPROPLEN];
3178 nvlist_t *userprops = zfs_get_user_props(zhp);
3181 boolean_t right_justify;
3183 for (; pl != NULL; pl = pl->pl_next) {
3185 if (cb->cb_scripted)
3186 (void) printf("\t");
3193 if (pl->pl_prop == ZFS_PROP_NAME) {
3194 (void) strlcpy(property, zfs_get_name(zhp),
3197 right_justify = zfs_prop_align_right(pl->pl_prop);
3198 } else if (pl->pl_prop != ZPROP_INVAL) {
3199 if (zfs_prop_get(zhp, pl->pl_prop, property,
3200 sizeof (property), NULL, NULL, 0,
3201 cb->cb_literal) != 0)
3205 right_justify = zfs_prop_align_right(pl->pl_prop);
3206 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3207 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3208 property, sizeof (property), cb->cb_literal) != 0)
3212 right_justify = B_TRUE;
3213 } else if (zfs_prop_written(pl->pl_user_prop)) {
3214 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3215 property, sizeof (property), cb->cb_literal) != 0)
3219 right_justify = B_TRUE;
3221 if (nvlist_lookup_nvlist(userprops,
3222 pl->pl_user_prop, &propval) != 0)
3225 verify(nvlist_lookup_string(propval,
3226 ZPROP_VALUE, &propstr) == 0);
3227 right_justify = B_FALSE;
3231 * If this is being called in scripted mode, or if this is the
3232 * last column and it is left-justified, don't include a width
3235 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3236 (void) printf("%s", propstr);
3237 else if (right_justify)
3238 (void) printf("%*s", (int)pl->pl_width, propstr);
3240 (void) printf("%-*s", (int)pl->pl_width, propstr);
3243 (void) printf("\n");
3247 * Generic callback function to list a dataset or snapshot.
3250 list_callback(zfs_handle_t *zhp, void *data)
3252 list_cbdata_t *cbp = data;
3254 if (cbp->cb_first) {
3255 if (!cbp->cb_scripted)
3257 cbp->cb_first = B_FALSE;
3260 print_dataset(zhp, cbp);
3266 zfs_do_list(int argc, char **argv)
3269 static char default_fields[] =
3270 "name,used,available,referenced,mountpoint";
3271 int types = ZFS_TYPE_DATASET;
3272 boolean_t types_specified = B_FALSE;
3273 char *fields = NULL;
3274 list_cbdata_t cb = { 0 };
3278 zfs_sort_column_t *sortcol = NULL;
3279 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3282 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3288 cb.cb_literal = B_TRUE;
3289 flags |= ZFS_ITER_LITERAL_PROPS;
3292 limit = parse_depth(optarg, &flags);
3295 flags |= ZFS_ITER_RECURSE;
3298 cb.cb_scripted = B_TRUE;
3301 if (zfs_add_sort_column(&sortcol, optarg,
3303 (void) fprintf(stderr,
3304 gettext("invalid property '%s'\n"), optarg);
3309 if (zfs_add_sort_column(&sortcol, optarg,
3311 (void) fprintf(stderr,
3312 gettext("invalid property '%s'\n"), optarg);
3318 types_specified = B_TRUE;
3319 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3320 while (*optarg != '\0') {
3321 static char *type_subopts[] = { "filesystem",
3322 "volume", "snapshot", "snap", "bookmark",
3325 switch (getsubopt(&optarg, type_subopts,
3328 types |= ZFS_TYPE_FILESYSTEM;
3331 types |= ZFS_TYPE_VOLUME;
3335 types |= ZFS_TYPE_SNAPSHOT;
3338 types |= ZFS_TYPE_BOOKMARK;
3341 types = ZFS_TYPE_DATASET |
3345 (void) fprintf(stderr,
3346 gettext("invalid type '%s'\n"),
3353 (void) fprintf(stderr, gettext("missing argument for "
3354 "'%c' option\n"), optopt);
3358 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3368 fields = default_fields;
3371 * If we are only going to list snapshot names and sort by name,
3372 * then we can use faster version.
3374 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3375 flags |= ZFS_ITER_SIMPLE;
3378 * If "-o space" and no types were specified, don't display snapshots.
3380 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3381 types &= ~ZFS_TYPE_SNAPSHOT;
3384 * If the user specifies '-o all', the zprop_get_list() doesn't
3385 * normally include the name of the dataset. For 'zfs list', we always
3386 * want this property to be first.
3388 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3392 cb.cb_first = B_TRUE;
3394 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3395 limit, list_callback, &cb);
3397 zprop_free_list(cb.cb_proplist);
3398 zfs_free_sort_columns(sortcol);
3400 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3401 (void) fprintf(stderr, gettext("no datasets available\n"));
3407 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3408 * zfs rename [-f] -p <fs | vol> <fs | vol>
3409 * zfs rename -r <snap> <snap>
3411 * Renames the given dataset to another of the same type.
3413 * The '-p' flag creates all the non-existing ancestors of the target first.
3417 zfs_do_rename(int argc, char **argv)
3422 boolean_t recurse = B_FALSE;
3423 boolean_t parents = B_FALSE;
3424 boolean_t force_unmount = B_FALSE;
3427 while ((c = getopt(argc, argv, "prf")) != -1) {
3436 force_unmount = B_TRUE;
3440 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3449 /* check number of arguments */
3451 (void) fprintf(stderr, gettext("missing source dataset "
3456 (void) fprintf(stderr, gettext("missing target dataset "
3461 (void) fprintf(stderr, gettext("too many arguments\n"));
3465 if (recurse && parents) {
3466 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3471 if (recurse && strchr(argv[0], '@') == 0) {
3472 (void) fprintf(stderr, gettext("source dataset for recursive "
3473 "rename must be a snapshot\n"));
3477 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3478 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3481 /* If we were asked and the name looks good, try to create ancestors. */
3482 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3483 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3488 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3497 * Promotes the given clone fs to be the parent
3501 zfs_do_promote(int argc, char **argv)
3507 if (argc > 1 && argv[1][0] == '-') {
3508 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3513 /* check number of arguments */
3515 (void) fprintf(stderr, gettext("missing clone filesystem"
3520 (void) fprintf(stderr, gettext("too many arguments\n"));
3524 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3528 ret = (zfs_promote(zhp) != 0);
3536 * zfs rollback [-rRf] <snapshot>
3538 * -r Delete any intervening snapshots before doing rollback
3539 * -R Delete any snapshots and their clones
3540 * -f ignored for backwards compatibility
3542 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3543 * since then and making it the active dataset. If more recent snapshots exist,
3544 * the command will complain unless the '-r' flag is given.
3546 typedef struct rollback_cbdata {
3552 boolean_t cb_recurse;
3553 } rollback_cbdata_t;
3556 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3558 rollback_cbdata_t *cbp = data;
3560 if (cbp->cb_first && cbp->cb_recurse) {
3561 (void) fprintf(stderr, gettext("cannot rollback to "
3562 "'%s': clones of previous snapshots exist\n"),
3564 (void) fprintf(stderr, gettext("use '-R' to "
3565 "force deletion of the following clones and "
3571 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3579 * Report any snapshots more recent than the one specified. Used when '-r' is
3580 * not specified. We reuse this same callback for the snapshot dependents - if
3581 * 'cb_dependent' is set, then this is a dependent and we should report it
3582 * without checking the transaction group.
3585 rollback_check(zfs_handle_t *zhp, void *data)
3587 rollback_cbdata_t *cbp = data;
3589 if (cbp->cb_doclones) {
3594 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3595 if (cbp->cb_first && !cbp->cb_recurse) {
3596 (void) fprintf(stderr, gettext("cannot "
3597 "rollback to '%s': more recent snapshots "
3598 "or bookmarks exist\n"),
3600 (void) fprintf(stderr, gettext("use '-r' to "
3601 "force deletion of the following "
3602 "snapshots and bookmarks:\n"));
3607 if (cbp->cb_recurse) {
3608 if (zfs_iter_dependents(zhp, B_TRUE,
3609 rollback_check_dependent, cbp) != 0) {
3614 (void) fprintf(stderr, "%s\n",
3623 zfs_do_rollback(int argc, char **argv)
3627 boolean_t force = B_FALSE;
3628 rollback_cbdata_t cb = { 0 };
3629 zfs_handle_t *zhp, *snap;
3630 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3634 while ((c = getopt(argc, argv, "rRf")) != -1) {
3647 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3656 /* check number of arguments */
3658 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3662 (void) fprintf(stderr, gettext("too many arguments\n"));
3666 /* open the snapshot */
3667 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3670 /* open the parent dataset */
3671 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3672 verify((delim = strrchr(parentname, '@')) != NULL);
3674 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3680 * Check for more recent snapshots and/or clones based on the presence
3683 cb.cb_target = argv[0];
3684 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3685 cb.cb_first = B_TRUE;
3687 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3689 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3692 if ((ret = cb.cb_error) != 0)
3696 * Rollback parent to the given snapshot.
3698 ret = zfs_rollback(zhp, snap, force);
3711 * zfs set property=value ... { fs | snap | vol } ...
3713 * Sets the given properties for all datasets specified on the command line.
3717 set_callback(zfs_handle_t *zhp, void *data)
3719 nvlist_t *props = data;
3721 if (zfs_prop_set_list(zhp, props) != 0) {
3722 switch (libzfs_errno(g_zfs)) {
3723 case EZFS_MOUNTFAILED:
3724 (void) fprintf(stderr, gettext("property may be set "
3725 "but unable to remount filesystem\n"));
3727 case EZFS_SHARENFSFAILED:
3728 (void) fprintf(stderr, gettext("property may be set "
3729 "but unable to reshare filesystem\n"));
3738 zfs_do_set(int argc, char **argv)
3740 nvlist_t *props = NULL;
3741 int ds_start = -1; /* argv idx of first dataset arg */
3745 /* check for options */
3746 if (argc > 1 && argv[1][0] == '-') {
3747 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3752 /* check number of arguments */
3754 (void) fprintf(stderr, gettext("missing arguments\n"));
3758 if (strchr(argv[1], '=') == NULL) {
3759 (void) fprintf(stderr, gettext("missing property=value "
3762 (void) fprintf(stderr, gettext("missing dataset "
3768 /* validate argument order: prop=val args followed by dataset args */
3769 for (i = 1; i < argc; i++) {
3770 if (strchr(argv[i], '=') != NULL) {
3772 /* out-of-order prop=val argument */
3773 (void) fprintf(stderr, gettext("invalid "
3774 "argument order\n"));
3777 } else if (ds_start < 0) {
3782 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3786 /* Populate a list of property settings */
3787 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3789 for (i = 1; i < ds_start; i++) {
3790 if (!parseprop(props, argv[i])) {
3796 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3797 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3804 typedef struct snap_cbdata {
3806 boolean_t sd_recursive;
3807 const char *sd_snapname;
3811 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3813 snap_cbdata_t *sd = arg;
3818 if (sd->sd_recursive &&
3819 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3824 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3827 fnvlist_add_boolean(sd->sd_nvl, name);
3830 if (sd->sd_recursive)
3831 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3837 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3839 * Creates a snapshot with the given name. While functionally equivalent to
3840 * 'zfs create', it is a separate command to differentiate intent.
3843 zfs_do_snapshot(int argc, char **argv)
3848 snap_cbdata_t sd = { 0 };
3849 boolean_t multiple_snaps = B_FALSE;
3851 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3853 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3857 while ((c = getopt(argc, argv, "ro:")) != -1) {
3860 if (!parseprop(props, optarg)) {
3861 nvlist_free(sd.sd_nvl);
3867 sd.sd_recursive = B_TRUE;
3868 multiple_snaps = B_TRUE;
3871 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3880 /* check number of arguments */
3882 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3887 multiple_snaps = B_TRUE;
3888 for (; argc > 0; argc--, argv++) {
3892 atp = strchr(argv[0], '@');
3896 sd.sd_snapname = atp + 1;
3897 zhp = zfs_open(g_zfs, argv[0],
3898 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3901 if (zfs_snapshot_cb(zhp, &sd) != 0)
3905 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3906 nvlist_free(sd.sd_nvl);
3908 if (ret != 0 && multiple_snaps)
3909 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3913 nvlist_free(sd.sd_nvl);
3920 * Send a backup stream to stdout.
3923 zfs_do_send(int argc, char **argv)
3925 char *fromname = NULL;
3926 char *toname = NULL;
3927 char *resume_token = NULL;
3930 sendflags_t flags = { 0 };
3932 nvlist_t *dbgnv = NULL;
3933 boolean_t extraverbose = B_FALSE;
3935 struct option long_options[] = {
3936 {"replicate", no_argument, NULL, 'R'},
3937 {"props", no_argument, NULL, 'p'},
3938 {"parsable", no_argument, NULL, 'P'},
3939 {"dedup", no_argument, NULL, 'D'},
3940 {"verbose", no_argument, NULL, 'v'},
3941 {"dryrun", no_argument, NULL, 'n'},
3942 {"large-block", no_argument, NULL, 'L'},
3943 {"embed", no_argument, NULL, 'e'},
3944 {"resume", required_argument, NULL, 't'},
3945 {"compressed", no_argument, NULL, 'c'},
3946 {"raw", no_argument, NULL, 'w'},
3951 while ((c = getopt_long(argc, argv, ":i:I:RDpvnPLet:cw", long_options,
3963 flags.doall = B_TRUE;
3966 flags.replicate = B_TRUE;
3969 flags.props = B_TRUE;
3972 flags.parsable = B_TRUE;
3973 flags.verbose = B_TRUE;
3977 extraverbose = B_TRUE;
3978 flags.verbose = B_TRUE;
3979 flags.progress = B_TRUE;
3982 flags.dedup = B_TRUE;
3985 flags.dryrun = B_TRUE;
3988 flags.largeblock = B_TRUE;
3991 flags.embed_data = B_TRUE;
3994 resume_token = optarg;
3997 flags.compress = B_TRUE;
4001 flags.compress = B_TRUE;
4002 flags.embed_data = B_TRUE;
4003 flags.largeblock = B_TRUE;
4007 * If a parameter was not passed, optopt contains the
4008 * value that would normally lead us into the
4009 * appropriate case statement. If it's > 256, then this
4010 * must be a longopt and we should look at argv to get
4011 * the string. Otherwise it's just the character, so we
4012 * should use it directly.
4014 if (optopt <= UINT8_MAX) {
4015 (void) fprintf(stderr,
4016 gettext("missing argument for '%c' "
4017 "option\n"), optopt);
4019 (void) fprintf(stderr,
4020 gettext("missing argument for '%s' "
4021 "option\n"), argv[optind - 1]);
4029 * If an invalid flag was passed, optopt contains the
4030 * character if it was a short flag, or 0 if it was a
4034 (void) fprintf(stderr,
4035 gettext("invalid option '%c'\n"), optopt);
4037 (void) fprintf(stderr,
4038 gettext("invalid option '%s'\n"),
4049 if (resume_token != NULL) {
4050 if (fromname != NULL || flags.replicate || flags.props ||
4052 (void) fprintf(stderr,
4053 gettext("invalid flags combined with -t\n"));
4057 (void) fprintf(stderr, gettext("no additional "
4058 "arguments are permitted with -t\n"));
4063 (void) fprintf(stderr,
4064 gettext("missing snapshot argument\n"));
4068 (void) fprintf(stderr, gettext("too many arguments\n"));
4073 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
4074 (void) fprintf(stderr,
4075 gettext("Error: Stream can not be written to a terminal.\n"
4076 "You must redirect standard output.\n"));
4080 if (resume_token != NULL) {
4081 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
4086 * Special case sending a filesystem, or from a bookmark.
4088 if (strchr(argv[0], '@') == NULL ||
4089 (fromname && strchr(fromname, '#') != NULL)) {
4090 char frombuf[ZFS_MAX_DATASET_NAME_LEN];
4092 if (flags.replicate || flags.doall || flags.props ||
4093 flags.dedup || (strchr(argv[0], '@') == NULL &&
4094 (flags.dryrun || flags.verbose || flags.progress))) {
4095 (void) fprintf(stderr, gettext("Error: "
4096 "Unsupported flag with filesystem or bookmark.\n"));
4100 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
4104 if (fromname != NULL &&
4105 (fromname[0] == '#' || fromname[0] == '@')) {
4107 * Incremental source name begins with # or @.
4108 * Default to same fs as target.
4110 (void) strlcpy(frombuf, argv[0], sizeof (frombuf));
4111 cp = strchr(frombuf, '@');
4114 (void) strlcat(frombuf, fromname, sizeof (frombuf));
4117 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, flags);
4122 cp = strchr(argv[0], '@');
4125 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4130 * If they specified the full path to the snapshot, chop off
4131 * everything except the short name of the snapshot, but special
4132 * case if they specify the origin.
4134 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
4135 char origin[ZFS_MAX_DATASET_NAME_LEN];
4138 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
4139 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
4141 if (strcmp(origin, fromname) == 0) {
4143 flags.fromorigin = B_TRUE;
4146 if (cp != fromname && strcmp(argv[0], fromname)) {
4147 (void) fprintf(stderr,
4148 gettext("incremental source must be "
4149 "in same filesystem\n"));
4153 if (strchr(fromname, '@') || strchr(fromname, '/')) {
4154 (void) fprintf(stderr,
4155 gettext("invalid incremental source\n"));
4161 if (flags.replicate && fromname == NULL)
4162 flags.doall = B_TRUE;
4164 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
4165 extraverbose ? &dbgnv : NULL);
4167 if (extraverbose && dbgnv != NULL) {
4169 * dump_nvlist prints to stdout, but that's been
4170 * redirected to a file. Make it print to stderr
4173 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
4174 dump_nvlist(dbgnv, 0);
4183 * Restore a backup stream from stdin.
4186 zfs_do_receive(int argc, char **argv)
4189 recvflags_t flags = { 0 };
4190 boolean_t abort_resumable = B_FALSE;
4193 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4197 while ((c = getopt(argc, argv, ":o:x:denuvFsA")) != -1) {
4200 if (!parseprop(props, optarg)) {
4206 if (!parsepropname(props, optarg)) {
4212 flags.isprefix = B_TRUE;
4215 flags.isprefix = B_TRUE;
4216 flags.istail = B_TRUE;
4219 flags.dryrun = B_TRUE;
4222 flags.nomount = B_TRUE;
4225 flags.verbose = B_TRUE;
4228 flags.resumable = B_TRUE;
4231 flags.force = B_TRUE;
4234 abort_resumable = B_TRUE;
4237 (void) fprintf(stderr, gettext("missing argument for "
4238 "'%c' option\n"), optopt);
4242 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4251 /* check number of arguments */
4253 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4257 (void) fprintf(stderr, gettext("too many arguments\n"));
4261 if (abort_resumable) {
4262 if (flags.isprefix || flags.istail || flags.dryrun ||
4263 flags.resumable || flags.nomount) {
4264 (void) fprintf(stderr, gettext("invalid option\n"));
4268 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4269 (void) snprintf(namebuf, sizeof (namebuf),
4270 "%s/%%recv", argv[0]);
4272 if (zfs_dataset_exists(g_zfs, namebuf,
4273 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4274 zfs_handle_t *zhp = zfs_open(g_zfs,
4275 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4280 err = zfs_destroy(zhp, B_FALSE);
4283 zfs_handle_t *zhp = zfs_open(g_zfs,
4284 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4287 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4288 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4289 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4290 (void) fprintf(stderr,
4291 gettext("'%s' does not have any "
4292 "resumable receive state to abort\n"),
4298 err = zfs_destroy(zhp, B_FALSE);
4305 if (isatty(STDIN_FILENO)) {
4306 (void) fprintf(stderr,
4307 gettext("Error: Backup stream can not be read "
4308 "from a terminal.\n"
4309 "You must redirect standard input.\n"));
4313 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4320 * allow/unallow stuff
4322 /* copied from zfs/sys/dsl_deleg.h */
4323 #define ZFS_DELEG_PERM_CREATE "create"
4324 #define ZFS_DELEG_PERM_DESTROY "destroy"
4325 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4326 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4327 #define ZFS_DELEG_PERM_CLONE "clone"
4328 #define ZFS_DELEG_PERM_PROMOTE "promote"
4329 #define ZFS_DELEG_PERM_RENAME "rename"
4330 #define ZFS_DELEG_PERM_MOUNT "mount"
4331 #define ZFS_DELEG_PERM_SHARE "share"
4332 #define ZFS_DELEG_PERM_SEND "send"
4333 #define ZFS_DELEG_PERM_RECEIVE "receive"
4334 #define ZFS_DELEG_PERM_ALLOW "allow"
4335 #define ZFS_DELEG_PERM_USERPROP "userprop"
4336 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4337 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4338 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4339 #define ZFS_DELEG_PERM_USERUSED "userused"
4340 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4341 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4342 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4343 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4344 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4346 #define ZFS_DELEG_PERM_HOLD "hold"
4347 #define ZFS_DELEG_PERM_RELEASE "release"
4348 #define ZFS_DELEG_PERM_DIFF "diff"
4349 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4350 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
4351 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
4353 #define ZFS_DELEG_PERM_PROJECTUSED "projectused"
4354 #define ZFS_DELEG_PERM_PROJECTQUOTA "projectquota"
4355 #define ZFS_DELEG_PERM_PROJECTOBJUSED "projectobjused"
4356 #define ZFS_DELEG_PERM_PROJECTOBJQUOTA "projectobjquota"
4358 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4360 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4361 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4362 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4363 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4364 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4365 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4366 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4367 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4368 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4369 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4370 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4371 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4372 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4373 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4374 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4375 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4376 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4377 { ZFS_DELEG_PERM_LOAD_KEY, ZFS_DELEG_NOTE_LOAD_KEY },
4378 { ZFS_DELEG_PERM_CHANGE_KEY, ZFS_DELEG_NOTE_CHANGE_KEY },
4380 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4381 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4382 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4383 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4384 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4385 { ZFS_DELEG_PERM_USEROBJQUOTA, ZFS_DELEG_NOTE_USEROBJQUOTA },
4386 { ZFS_DELEG_PERM_USEROBJUSED, ZFS_DELEG_NOTE_USEROBJUSED },
4387 { ZFS_DELEG_PERM_GROUPOBJQUOTA, ZFS_DELEG_NOTE_GROUPOBJQUOTA },
4388 { ZFS_DELEG_PERM_GROUPOBJUSED, ZFS_DELEG_NOTE_GROUPOBJUSED },
4389 { ZFS_DELEG_PERM_PROJECTUSED, ZFS_DELEG_NOTE_PROJECTUSED },
4390 { ZFS_DELEG_PERM_PROJECTQUOTA, ZFS_DELEG_NOTE_PROJECTQUOTA },
4391 { ZFS_DELEG_PERM_PROJECTOBJUSED, ZFS_DELEG_NOTE_PROJECTOBJUSED },
4392 { ZFS_DELEG_PERM_PROJECTOBJQUOTA, ZFS_DELEG_NOTE_PROJECTOBJQUOTA },
4393 { NULL, ZFS_DELEG_NOTE_NONE }
4396 /* permission structure */
4397 typedef struct deleg_perm {
4398 zfs_deleg_who_type_t dp_who_type;
4399 const char *dp_name;
4401 boolean_t dp_descend;
4405 typedef struct deleg_perm_node {
4406 deleg_perm_t dpn_perm;
4408 uu_avl_node_t dpn_avl_node;
4409 } deleg_perm_node_t;
4411 typedef struct fs_perm fs_perm_t;
4413 /* permissions set */
4414 typedef struct who_perm {
4415 zfs_deleg_who_type_t who_type;
4416 const char *who_name; /* id */
4417 char who_ug_name[256]; /* user/group name */
4418 fs_perm_t *who_fsperm; /* uplink */
4420 uu_avl_t *who_deleg_perm_avl; /* permissions */
4424 typedef struct who_perm_node {
4425 who_perm_t who_perm;
4426 uu_avl_node_t who_avl_node;
4429 typedef struct fs_perm_set fs_perm_set_t;
4430 /* fs permissions */
4432 const char *fsp_name;
4434 uu_avl_t *fsp_sc_avl; /* sets,create */
4435 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4437 fs_perm_set_t *fsp_set; /* uplink */
4441 typedef struct fs_perm_node {
4442 fs_perm_t fspn_fsperm;
4445 uu_list_node_t fspn_list_node;
4448 /* top level structure */
4449 struct fs_perm_set {
4450 uu_list_pool_t *fsps_list_pool;
4451 uu_list_t *fsps_list; /* list of fs_perms */
4453 uu_avl_pool_t *fsps_named_set_avl_pool;
4454 uu_avl_pool_t *fsps_who_perm_avl_pool;
4455 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4458 static inline const char *
4459 deleg_perm_type(zfs_deleg_note_t note)
4465 case ZFS_DELEG_NOTE_GROUPQUOTA:
4466 case ZFS_DELEG_NOTE_GROUPUSED:
4467 case ZFS_DELEG_NOTE_USERPROP:
4468 case ZFS_DELEG_NOTE_USERQUOTA:
4469 case ZFS_DELEG_NOTE_USERUSED:
4470 case ZFS_DELEG_NOTE_USEROBJQUOTA:
4471 case ZFS_DELEG_NOTE_USEROBJUSED:
4472 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
4473 case ZFS_DELEG_NOTE_GROUPOBJUSED:
4474 case ZFS_DELEG_NOTE_PROJECTUSED:
4475 case ZFS_DELEG_NOTE_PROJECTQUOTA:
4476 case ZFS_DELEG_NOTE_PROJECTOBJUSED:
4477 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
4479 return (gettext("other"));
4481 return (gettext("subcommand"));
4486 who_type2weight(zfs_deleg_who_type_t who_type)
4490 case ZFS_DELEG_NAMED_SET_SETS:
4491 case ZFS_DELEG_NAMED_SET:
4494 case ZFS_DELEG_CREATE_SETS:
4495 case ZFS_DELEG_CREATE:
4498 case ZFS_DELEG_USER_SETS:
4499 case ZFS_DELEG_USER:
4502 case ZFS_DELEG_GROUP_SETS:
4503 case ZFS_DELEG_GROUP:
4506 case ZFS_DELEG_EVERYONE_SETS:
4507 case ZFS_DELEG_EVERYONE:
4519 who_perm_compare(const void *larg, const void *rarg, void *unused)
4521 const who_perm_node_t *l = larg;
4522 const who_perm_node_t *r = rarg;
4523 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4524 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4525 int lweight = who_type2weight(ltype);
4526 int rweight = who_type2weight(rtype);
4527 int res = lweight - rweight;
4529 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4530 ZFS_MAX_DELEG_NAME-1);
4542 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4544 const deleg_perm_node_t *l = larg;
4545 const deleg_perm_node_t *r = rarg;
4546 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4547 ZFS_MAX_DELEG_NAME-1);
4559 fs_perm_set_init(fs_perm_set_t *fspset)
4561 bzero(fspset, sizeof (fs_perm_set_t));
4563 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4564 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4565 NULL, UU_DEFAULT)) == NULL)
4567 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4568 UU_DEFAULT)) == NULL)
4571 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4572 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4573 who_perm_node_t, who_avl_node), who_perm_compare,
4574 UU_DEFAULT)) == NULL)
4577 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4578 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4579 who_perm_node_t, who_avl_node), who_perm_compare,
4580 UU_DEFAULT)) == NULL)
4583 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4584 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4585 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4590 static inline void fs_perm_fini(fs_perm_t *);
4591 static inline void who_perm_fini(who_perm_t *);
4594 fs_perm_set_fini(fs_perm_set_t *fspset)
4596 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4598 while (node != NULL) {
4599 fs_perm_node_t *next_node =
4600 uu_list_next(fspset->fsps_list, node);
4601 fs_perm_t *fsperm = &node->fspn_fsperm;
4602 fs_perm_fini(fsperm);
4603 uu_list_remove(fspset->fsps_list, node);
4608 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4609 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4610 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4614 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4617 deleg_perm->dp_who_type = type;
4618 deleg_perm->dp_name = name;
4622 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4623 zfs_deleg_who_type_t type, const char *name)
4625 uu_avl_pool_t *pool;
4626 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4628 bzero(who_perm, sizeof (who_perm_t));
4630 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4631 UU_DEFAULT)) == NULL)
4634 who_perm->who_type = type;
4635 who_perm->who_name = name;
4636 who_perm->who_fsperm = fsperm;
4640 who_perm_fini(who_perm_t *who_perm)
4642 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4644 while (node != NULL) {
4645 deleg_perm_node_t *next_node =
4646 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4648 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4653 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4657 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4659 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4660 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4662 bzero(fsperm, sizeof (fs_perm_t));
4664 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4668 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4672 fsperm->fsp_set = fspset;
4673 fsperm->fsp_name = fsname;
4677 fs_perm_fini(fs_perm_t *fsperm)
4679 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4680 while (node != NULL) {
4681 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4683 who_perm_t *who_perm = &node->who_perm;
4684 who_perm_fini(who_perm);
4685 uu_avl_remove(fsperm->fsp_sc_avl, node);
4690 node = uu_avl_first(fsperm->fsp_uge_avl);
4691 while (node != NULL) {
4692 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4694 who_perm_t *who_perm = &node->who_perm;
4695 who_perm_fini(who_perm);
4696 uu_avl_remove(fsperm->fsp_uge_avl, node);
4701 uu_avl_destroy(fsperm->fsp_sc_avl);
4702 uu_avl_destroy(fsperm->fsp_uge_avl);
4706 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4707 zfs_deleg_who_type_t who_type, const char *name, char locality)
4709 uu_avl_index_t idx = 0;
4711 deleg_perm_node_t *found_node = NULL;
4712 deleg_perm_t *deleg_perm = &node->dpn_perm;
4714 deleg_perm_init(deleg_perm, who_type, name);
4716 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4718 uu_avl_insert(avl, node, idx);
4721 deleg_perm = &node->dpn_perm;
4726 case ZFS_DELEG_LOCAL:
4727 deleg_perm->dp_local = B_TRUE;
4729 case ZFS_DELEG_DESCENDENT:
4730 deleg_perm->dp_descend = B_TRUE;
4735 assert(B_FALSE); /* invalid locality */
4740 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4742 nvpair_t *nvp = NULL;
4743 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4744 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4745 zfs_deleg_who_type_t who_type = who_perm->who_type;
4747 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4748 const char *name = nvpair_name(nvp);
4749 data_type_t type = nvpair_type(nvp);
4750 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4751 deleg_perm_node_t *node =
4752 safe_malloc(sizeof (deleg_perm_node_t));
4754 VERIFY(type == DATA_TYPE_BOOLEAN);
4756 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4757 set_deleg_perm_node(avl, node, who_type, name, locality);
4764 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4766 nvpair_t *nvp = NULL;
4767 fs_perm_set_t *fspset = fsperm->fsp_set;
4769 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4770 nvlist_t *nvl2 = NULL;
4771 const char *name = nvpair_name(nvp);
4772 uu_avl_t *avl = NULL;
4773 uu_avl_pool_t *avl_pool = NULL;
4774 zfs_deleg_who_type_t perm_type = name[0];
4775 char perm_locality = name[1];
4776 const char *perm_name = name + 3;
4777 boolean_t is_set = B_TRUE;
4778 who_perm_t *who_perm = NULL;
4780 assert('$' == name[2]);
4782 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4785 switch (perm_type) {
4786 case ZFS_DELEG_CREATE:
4787 case ZFS_DELEG_CREATE_SETS:
4788 case ZFS_DELEG_NAMED_SET:
4789 case ZFS_DELEG_NAMED_SET_SETS:
4790 avl_pool = fspset->fsps_named_set_avl_pool;
4791 avl = fsperm->fsp_sc_avl;
4793 case ZFS_DELEG_USER:
4794 case ZFS_DELEG_USER_SETS:
4795 case ZFS_DELEG_GROUP:
4796 case ZFS_DELEG_GROUP_SETS:
4797 case ZFS_DELEG_EVERYONE:
4798 case ZFS_DELEG_EVERYONE_SETS:
4799 avl_pool = fspset->fsps_who_perm_avl_pool;
4800 avl = fsperm->fsp_uge_avl;
4804 assert(!"unhandled zfs_deleg_who_type_t");
4808 who_perm_node_t *found_node = NULL;
4809 who_perm_node_t *node = safe_malloc(
4810 sizeof (who_perm_node_t));
4811 who_perm = &node->who_perm;
4812 uu_avl_index_t idx = 0;
4814 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4815 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4817 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4819 if (avl == fsperm->fsp_uge_avl) {
4821 struct passwd *p = NULL;
4822 struct group *g = NULL;
4823 const char *nice_name = NULL;
4825 switch (perm_type) {
4826 case ZFS_DELEG_USER_SETS:
4827 case ZFS_DELEG_USER:
4828 rid = atoi(perm_name);
4831 nice_name = p->pw_name;
4833 case ZFS_DELEG_GROUP_SETS:
4834 case ZFS_DELEG_GROUP:
4835 rid = atoi(perm_name);
4838 nice_name = g->gr_name;
4845 if (nice_name != NULL)
4847 node->who_perm.who_ug_name,
4851 uu_avl_insert(avl, node, idx);
4854 who_perm = &node->who_perm;
4857 VERIFY3P(who_perm, !=, NULL);
4858 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4865 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4867 nvpair_t *nvp = NULL;
4868 uu_avl_index_t idx = 0;
4870 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4871 nvlist_t *nvl2 = NULL;
4872 const char *fsname = nvpair_name(nvp);
4873 data_type_t type = nvpair_type(nvp);
4874 fs_perm_t *fsperm = NULL;
4875 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4879 fsperm = &node->fspn_fsperm;
4881 VERIFY(DATA_TYPE_NVLIST == type);
4883 uu_list_node_init(node, &node->fspn_list_node,
4884 fspset->fsps_list_pool);
4886 idx = uu_list_numnodes(fspset->fsps_list);
4887 fs_perm_init(fsperm, fspset, fsname);
4889 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4892 (void) parse_fs_perm(fsperm, nvl2);
4894 uu_list_insert(fspset->fsps_list, node, idx);
4900 static inline const char *
4901 deleg_perm_comment(zfs_deleg_note_t note)
4903 const char *str = "";
4908 case ZFS_DELEG_NOTE_ALLOW:
4909 str = gettext("Must also have the permission that is being"
4910 "\n\t\t\t\tallowed");
4912 case ZFS_DELEG_NOTE_CLONE:
4913 str = gettext("Must also have the 'create' ability and 'mount'"
4914 "\n\t\t\t\tability in the origin file system");
4916 case ZFS_DELEG_NOTE_CREATE:
4917 str = gettext("Must also have the 'mount' ability");
4919 case ZFS_DELEG_NOTE_DESTROY:
4920 str = gettext("Must also have the 'mount' ability");
4922 case ZFS_DELEG_NOTE_DIFF:
4923 str = gettext("Allows lookup of paths within a dataset;"
4924 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4925 "\n\t\t\t\tin order to use zfs diff");
4927 case ZFS_DELEG_NOTE_HOLD:
4928 str = gettext("Allows adding a user hold to a snapshot");
4930 case ZFS_DELEG_NOTE_MOUNT:
4931 str = gettext("Allows mount/umount of ZFS datasets");
4933 case ZFS_DELEG_NOTE_PROMOTE:
4934 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4935 " 'promote' ability in the origin file system");
4937 case ZFS_DELEG_NOTE_RECEIVE:
4938 str = gettext("Must also have the 'mount' and 'create'"
4941 case ZFS_DELEG_NOTE_RELEASE:
4942 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4943 "might destroy the snapshot");
4945 case ZFS_DELEG_NOTE_RENAME:
4946 str = gettext("Must also have the 'mount' and 'create'"
4947 "\n\t\t\t\tability in the new parent");
4949 case ZFS_DELEG_NOTE_ROLLBACK:
4952 case ZFS_DELEG_NOTE_SEND:
4955 case ZFS_DELEG_NOTE_SHARE:
4956 str = gettext("Allows sharing file systems over NFS or SMB"
4957 "\n\t\t\t\tprotocols");
4959 case ZFS_DELEG_NOTE_SNAPSHOT:
4962 case ZFS_DELEG_NOTE_LOAD_KEY:
4963 str = gettext("Allows loading or unloading an encryption key");
4965 case ZFS_DELEG_NOTE_CHANGE_KEY:
4966 str = gettext("Allows changing or adding an encryption key");
4969 * case ZFS_DELEG_NOTE_VSCAN:
4970 * str = gettext("");
4974 case ZFS_DELEG_NOTE_GROUPQUOTA:
4975 str = gettext("Allows accessing any groupquota@... property");
4977 case ZFS_DELEG_NOTE_GROUPUSED:
4978 str = gettext("Allows reading any groupused@... property");
4980 case ZFS_DELEG_NOTE_USERPROP:
4981 str = gettext("Allows changing any user property");
4983 case ZFS_DELEG_NOTE_USERQUOTA:
4984 str = gettext("Allows accessing any userquota@... property");
4986 case ZFS_DELEG_NOTE_USERUSED:
4987 str = gettext("Allows reading any userused@... property");
4989 case ZFS_DELEG_NOTE_USEROBJQUOTA:
4990 str = gettext("Allows accessing any userobjquota@... property");
4992 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
4993 str = gettext("Allows accessing any \n\t\t\t\t"
4994 "groupobjquota@... property");
4996 case ZFS_DELEG_NOTE_GROUPOBJUSED:
4997 str = gettext("Allows reading any groupobjused@... property");
4999 case ZFS_DELEG_NOTE_USEROBJUSED:
5000 str = gettext("Allows reading any userobjused@... property");
5002 case ZFS_DELEG_NOTE_PROJECTQUOTA:
5003 str = gettext("Allows accessing any projectquota@... property");
5005 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
5006 str = gettext("Allows accessing any \n\t\t\t\t"
5007 "projectobjquota@... property");
5009 case ZFS_DELEG_NOTE_PROJECTUSED:
5010 str = gettext("Allows reading any projectused@... property");
5012 case ZFS_DELEG_NOTE_PROJECTOBJUSED:
5013 str = gettext("Allows accessing any \n\t\t\t\t"
5014 "projectobjused@... property");
5032 boolean_t recursive; /* unallow only */
5033 boolean_t prt_usage;
5035 boolean_t prt_perms;
5038 const char *dataset;
5042 prop_cmp(const void *a, const void *b)
5044 const char *str1 = *(const char **)a;
5045 const char *str2 = *(const char **)b;
5046 return (strcmp(str1, str2));
5050 allow_usage(boolean_t un, boolean_t requested, const char *msg)
5052 const char *opt_desc[] = {
5053 "-h", gettext("show this help message and exit"),
5054 "-l", gettext("set permission locally"),
5055 "-d", gettext("set permission for descents"),
5056 "-u", gettext("set permission for user"),
5057 "-g", gettext("set permission for group"),
5058 "-e", gettext("set permission for everyone"),
5059 "-c", gettext("set create time permission"),
5060 "-s", gettext("define permission set"),
5062 "-r", gettext("remove permissions recursively"),
5064 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
5065 size_t allow_size = unallow_size - 2;
5066 const char *props[ZFS_NUM_PROPS];
5069 FILE *fp = requested ? stdout : stderr;
5070 zprop_desc_t *pdtbl = zfs_prop_get_table();
5071 const char *fmt = gettext("%-16s %-14s\t%s\n");
5073 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
5075 (void) fprintf(fp, gettext("Options:\n"));
5076 for (i = 0; i < (un ? unallow_size : allow_size); i += 2) {
5077 const char *opt = opt_desc[i];
5078 const char *optdsc = opt_desc[i + 1];
5079 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
5082 (void) fprintf(fp, gettext("\nThe following permissions are "
5084 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
5086 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
5087 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
5088 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
5089 const char *perm_type = deleg_perm_type(perm_note);
5090 const char *perm_comment = deleg_perm_comment(perm_note);
5091 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
5094 for (i = 0; i < ZFS_NUM_PROPS; i++) {
5095 zprop_desc_t *pd = &pdtbl[i];
5096 if (pd->pd_visible != B_TRUE)
5099 if (pd->pd_attr == PROP_READONLY)
5102 props[count++] = pd->pd_name;
5104 props[count] = NULL;
5106 qsort(props, count, sizeof (char *), prop_cmp);
5108 for (i = 0; i < count; i++)
5109 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
5112 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
5114 exit(requested ? 0 : 2);
5117 static inline const char *
5118 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
5121 if (un && argc == expected_argc - 1)
5123 else if (argc == expected_argc)
5124 *permsp = argv[argc - 2];
5126 allow_usage(un, B_FALSE,
5127 gettext("wrong number of parameters\n"));
5129 return (argv[argc - 1]);
5133 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
5135 int uge_sum = opts->user + opts->group + opts->everyone;
5136 int csuge_sum = opts->create + opts->set + uge_sum;
5137 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
5138 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
5141 allow_usage(un, B_FALSE,
5142 gettext("-u, -g, and -e are mutually exclusive\n"));
5144 if (opts->prt_usage) {
5145 if (argc == 0 && all_sum == 0)
5146 allow_usage(un, B_TRUE, NULL);
5153 allow_usage(un, B_FALSE,
5154 gettext("invalid options combined with -s\n"));
5156 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5157 if (argv[0][0] != '@')
5158 allow_usage(un, B_FALSE,
5159 gettext("invalid set name: missing '@' prefix\n"));
5160 opts->who = argv[0];
5161 } else if (opts->create) {
5162 if (ldcsuge_sum > 1)
5163 allow_usage(un, B_FALSE,
5164 gettext("invalid options combined with -c\n"));
5165 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5166 } else if (opts->everyone) {
5168 allow_usage(un, B_FALSE,
5169 gettext("invalid options combined with -e\n"));
5170 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5171 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
5173 opts->everyone = B_TRUE;
5176 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5177 } else if (argc == 1 && !un) {
5178 opts->prt_perms = B_TRUE;
5179 opts->dataset = argv[argc-1];
5181 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5182 opts->who = argv[0];
5185 if (!opts->local && !opts->descend) {
5186 opts->local = B_TRUE;
5187 opts->descend = B_TRUE;
5192 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
5193 const char *who, char *perms, nvlist_t *top_nvl)
5196 char ld[2] = { '\0', '\0' };
5197 char who_buf[MAXNAMELEN + 32];
5198 char base_type = '\0';
5199 char set_type = '\0';
5200 nvlist_t *base_nvl = NULL;
5201 nvlist_t *set_nvl = NULL;
5204 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
5206 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
5210 case ZFS_DELEG_NAMED_SET_SETS:
5211 case ZFS_DELEG_NAMED_SET:
5212 set_type = ZFS_DELEG_NAMED_SET_SETS;
5213 base_type = ZFS_DELEG_NAMED_SET;
5214 ld[0] = ZFS_DELEG_NA;
5216 case ZFS_DELEG_CREATE_SETS:
5217 case ZFS_DELEG_CREATE:
5218 set_type = ZFS_DELEG_CREATE_SETS;
5219 base_type = ZFS_DELEG_CREATE;
5220 ld[0] = ZFS_DELEG_NA;
5222 case ZFS_DELEG_USER_SETS:
5223 case ZFS_DELEG_USER:
5224 set_type = ZFS_DELEG_USER_SETS;
5225 base_type = ZFS_DELEG_USER;
5227 ld[0] = ZFS_DELEG_LOCAL;
5229 ld[1] = ZFS_DELEG_DESCENDENT;
5231 case ZFS_DELEG_GROUP_SETS:
5232 case ZFS_DELEG_GROUP:
5233 set_type = ZFS_DELEG_GROUP_SETS;
5234 base_type = ZFS_DELEG_GROUP;
5236 ld[0] = ZFS_DELEG_LOCAL;
5238 ld[1] = ZFS_DELEG_DESCENDENT;
5240 case ZFS_DELEG_EVERYONE_SETS:
5241 case ZFS_DELEG_EVERYONE:
5242 set_type = ZFS_DELEG_EVERYONE_SETS;
5243 base_type = ZFS_DELEG_EVERYONE;
5245 ld[0] = ZFS_DELEG_LOCAL;
5247 ld[1] = ZFS_DELEG_DESCENDENT;
5251 assert(set_type != '\0' && base_type != '\0');
5254 if (perms != NULL) {
5256 char *end = curr + strlen(perms);
5258 while (curr < end) {
5259 char *delim = strchr(curr, ',');
5270 (void) nvlist_add_boolean(nvl, curr);
5276 for (i = 0; i < 2; i++) {
5277 char locality = ld[i];
5281 if (!nvlist_empty(base_nvl)) {
5283 (void) snprintf(who_buf,
5284 sizeof (who_buf), "%c%c$%s",
5285 base_type, locality, who);
5287 (void) snprintf(who_buf,
5288 sizeof (who_buf), "%c%c$",
5289 base_type, locality);
5291 (void) nvlist_add_nvlist(top_nvl, who_buf,
5296 if (!nvlist_empty(set_nvl)) {
5298 (void) snprintf(who_buf,
5299 sizeof (who_buf), "%c%c$%s",
5300 set_type, locality, who);
5302 (void) snprintf(who_buf,
5303 sizeof (who_buf), "%c%c$",
5304 set_type, locality);
5306 (void) nvlist_add_nvlist(top_nvl, who_buf,
5311 for (i = 0; i < 2; i++) {
5312 char locality = ld[i];
5317 (void) snprintf(who_buf, sizeof (who_buf),
5318 "%c%c$%s", base_type, locality, who);
5320 (void) snprintf(who_buf, sizeof (who_buf),
5321 "%c%c$", base_type, locality);
5322 (void) nvlist_add_boolean(top_nvl, who_buf);
5325 (void) snprintf(who_buf, sizeof (who_buf),
5326 "%c%c$%s", set_type, locality, who);
5328 (void) snprintf(who_buf, sizeof (who_buf),
5329 "%c%c$", set_type, locality);
5330 (void) nvlist_add_boolean(top_nvl, who_buf);
5336 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5338 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5342 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5343 opts->descend, opts->who, opts->perms, *nvlp);
5344 } else if (opts->create) {
5345 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5346 opts->descend, NULL, opts->perms, *nvlp);
5347 } else if (opts->everyone) {
5348 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5349 opts->descend, NULL, opts->perms, *nvlp);
5351 char *curr = opts->who;
5352 char *end = curr + strlen(curr);
5354 while (curr < end) {
5356 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5358 char *delim = strchr(curr, ',');
5361 struct passwd *p = NULL;
5362 struct group *g = NULL;
5370 rid = (uid_t)strtol(curr, &endch, 0);
5372 who_type = ZFS_DELEG_USER;
5381 (void) snprintf(errbuf, 256, gettext(
5382 "invalid user %s"), curr);
5383 allow_usage(un, B_TRUE, errbuf);
5385 } else if (opts->group) {
5386 who_type = ZFS_DELEG_GROUP;
5395 (void) snprintf(errbuf, 256, gettext(
5396 "invalid group %s"), curr);
5397 allow_usage(un, B_TRUE, errbuf);
5400 if (*endch != '\0') {
5407 if (*endch != '\0') {
5415 who_type = ZFS_DELEG_USER;
5417 } else if (g != NULL) {
5418 who_type = ZFS_DELEG_GROUP;
5421 (void) snprintf(errbuf, 256, gettext(
5422 "invalid user/group %s"), curr);
5423 allow_usage(un, B_TRUE, errbuf);
5427 (void) sprintf(id, "%u", rid);
5430 store_allow_perm(who_type, opts->local,
5431 opts->descend, who, opts->perms, *nvlp);
5440 print_set_creat_perms(uu_avl_t *who_avl)
5442 const char *sc_title[] = {
5443 gettext("Permission sets:\n"),
5444 gettext("Create time permissions:\n"),
5447 const char **title_ptr = sc_title;
5448 who_perm_node_t *who_node = NULL;
5449 int prev_weight = -1;
5451 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5452 who_node = uu_avl_next(who_avl, who_node)) {
5453 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5454 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5455 const char *who_name = who_node->who_perm.who_name;
5456 int weight = who_type2weight(who_type);
5457 boolean_t first = B_TRUE;
5458 deleg_perm_node_t *deleg_node;
5460 if (prev_weight != weight) {
5461 (void) printf("%s", *title_ptr++);
5462 prev_weight = weight;
5465 if (who_name == NULL || strnlen(who_name, 1) == 0)
5466 (void) printf("\t");
5468 (void) printf("\t%s ", who_name);
5470 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5471 deleg_node = uu_avl_next(avl, deleg_node)) {
5474 deleg_node->dpn_perm.dp_name);
5477 (void) printf(",%s",
5478 deleg_node->dpn_perm.dp_name);
5481 (void) printf("\n");
5486 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5489 who_perm_node_t *who_node = NULL;
5490 boolean_t prt_title = B_TRUE;
5491 uu_avl_walk_t *walk;
5493 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5496 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5497 const char *who_name = who_node->who_perm.who_name;
5498 const char *nice_who_name = who_node->who_perm.who_ug_name;
5499 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5500 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5502 deleg_perm_node_t *deleg_node;
5503 boolean_t prt_who = B_TRUE;
5505 for (deleg_node = uu_avl_first(avl);
5507 deleg_node = uu_avl_next(avl, deleg_node)) {
5508 if (local != deleg_node->dpn_perm.dp_local ||
5509 descend != deleg_node->dpn_perm.dp_descend)
5513 const char *who = NULL;
5515 prt_title = B_FALSE;
5516 (void) printf("%s", title);
5520 case ZFS_DELEG_USER_SETS:
5521 case ZFS_DELEG_USER:
5522 who = gettext("user");
5524 who_name = nice_who_name;
5526 case ZFS_DELEG_GROUP_SETS:
5527 case ZFS_DELEG_GROUP:
5528 who = gettext("group");
5530 who_name = nice_who_name;
5532 case ZFS_DELEG_EVERYONE_SETS:
5533 case ZFS_DELEG_EVERYONE:
5534 who = gettext("everyone");
5539 assert(who != NULL);
5543 if (who_name == NULL)
5544 (void) printf("\t%s", who);
5546 (void) printf("\t%s %s", who, who_name);
5549 (void) printf("%c%s", delim,
5550 deleg_node->dpn_perm.dp_name);
5555 (void) printf("\n");
5558 uu_avl_walk_end(walk);
5562 print_fs_perms(fs_perm_set_t *fspset)
5564 fs_perm_node_t *node = NULL;
5565 char buf[MAXNAMELEN + 32];
5566 const char *dsname = buf;
5568 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5569 node = uu_list_next(fspset->fsps_list, node)) {
5570 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5571 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5574 (void) snprintf(buf, sizeof (buf),
5575 gettext("---- Permissions on %s "),
5576 node->fspn_fsperm.fsp_name);
5577 (void) printf("%s", dsname);
5578 left = 70 - strlen(buf);
5581 (void) printf("\n");
5583 print_set_creat_perms(sc_avl);
5584 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5585 gettext("Local permissions:\n"));
5586 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5587 gettext("Descendent permissions:\n"));
5588 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5589 gettext("Local+Descendent permissions:\n"));
5593 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5595 struct deleg_perms {
5601 set_deleg_perms(zfs_handle_t *zhp, void *data)
5603 struct deleg_perms *perms = (struct deleg_perms *)data;
5604 zfs_type_t zfs_type = zfs_get_type(zhp);
5606 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5609 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5613 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5616 nvlist_t *perm_nvl = NULL;
5617 nvlist_t *update_perm_nvl = NULL;
5620 struct allow_opts opts = { 0 };
5622 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5625 while ((c = getopt(argc, argv, optstr)) != -1) {
5628 opts.local = B_TRUE;
5631 opts.descend = B_TRUE;
5637 opts.group = B_TRUE;
5640 opts.everyone = B_TRUE;
5646 opts.create = B_TRUE;
5649 opts.recursive = B_TRUE;
5652 (void) fprintf(stderr, gettext("missing argument for "
5653 "'%c' option\n"), optopt);
5657 opts.prt_usage = B_TRUE;
5660 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5669 /* check arguments */
5670 parse_allow_args(argc, argv, un, &opts);
5672 /* try to open the dataset */
5673 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5674 ZFS_TYPE_VOLUME)) == NULL) {
5675 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5680 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5683 fs_perm_set_init(&fs_perm_set);
5684 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5685 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5690 print_fs_perms(&fs_perm_set);
5692 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5693 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5696 if (un && opts.recursive) {
5697 struct deleg_perms data = { un, update_perm_nvl };
5698 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5707 nvlist_free(perm_nvl);
5708 nvlist_free(update_perm_nvl);
5710 fs_perm_set_fini(&fs_perm_set);
5718 zfs_do_allow(int argc, char **argv)
5720 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5724 zfs_do_unallow(int argc, char **argv)
5726 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5730 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5735 boolean_t recursive = B_FALSE;
5736 const char *opts = holding ? "rt" : "r";
5740 while ((c = getopt(argc, argv, opts)) != -1) {
5746 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5755 /* check number of arguments */
5763 if (holding && tag[0] == '.') {
5764 /* tags starting with '.' are reserved for libzfs */
5765 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5769 for (i = 0; i < argc; ++i) {
5771 char parent[ZFS_MAX_DATASET_NAME_LEN];
5773 char *path = argv[i];
5775 delim = strchr(path, '@');
5776 if (delim == NULL) {
5777 (void) fprintf(stderr,
5778 gettext("'%s' is not a snapshot\n"), path);
5782 (void) strncpy(parent, path, delim - path);
5783 parent[delim - path] = '\0';
5785 zhp = zfs_open(g_zfs, parent,
5786 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5792 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5795 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5801 return (errors != 0);
5805 * zfs hold [-r] [-t] <tag> <snap> ...
5807 * -r Recursively hold
5809 * Apply a user-hold with the given tag to the list of snapshots.
5812 zfs_do_hold(int argc, char **argv)
5814 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5818 * zfs release [-r] <tag> <snap> ...
5820 * -r Recursively release
5822 * Release a user-hold with the given tag from the list of snapshots.
5825 zfs_do_release(int argc, char **argv)
5827 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5830 typedef struct holds_cbdata {
5831 boolean_t cb_recursive;
5832 const char *cb_snapname;
5834 size_t cb_max_namelen;
5835 size_t cb_max_taglen;
5838 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5839 #define DATETIME_BUF_LEN (32)
5844 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
5847 nvpair_t *nvp = NULL;
5848 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5852 for (i = 0; i < 3; i++) {
5853 col = gettext(hdr_cols[i]);
5855 (void) printf("%-*s ", i ? tagwidth : nwidth,
5858 (void) printf("%s\n", col);
5862 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5863 char *zname = nvpair_name(nvp);
5865 nvpair_t *nvp2 = NULL;
5866 (void) nvpair_value_nvlist(nvp, &nvl2);
5867 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5868 char tsbuf[DATETIME_BUF_LEN];
5869 char *tagname = nvpair_name(nvp2);
5874 (void) nvpair_value_uint64(nvp2, &val);
5876 (void) localtime_r(&time, &t);
5877 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5878 gettext(STRFTIME_FMT_STR), &t);
5881 (void) printf("%s\t%s\t%s\n", zname,
5884 (void) printf("%-*s %-*s %s\n", nwidth,
5885 zname, tagwidth, tagname, tsbuf);
5892 * Generic callback function to list a dataset or snapshot.
5895 holds_callback(zfs_handle_t *zhp, void *data)
5897 holds_cbdata_t *cbp = data;
5898 nvlist_t *top_nvl = *cbp->cb_nvlp;
5899 nvlist_t *nvl = NULL;
5900 nvpair_t *nvp = NULL;
5901 const char *zname = zfs_get_name(zhp);
5902 size_t znamelen = strlen(zname);
5904 if (cbp->cb_recursive) {
5905 const char *snapname;
5906 char *delim = strchr(zname, '@');
5910 snapname = delim + 1;
5911 if (strcmp(cbp->cb_snapname, snapname))
5915 if (zfs_get_holds(zhp, &nvl) != 0)
5918 if (znamelen > cbp->cb_max_namelen)
5919 cbp->cb_max_namelen = znamelen;
5921 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5922 const char *tag = nvpair_name(nvp);
5923 size_t taglen = strlen(tag);
5924 if (taglen > cbp->cb_max_taglen)
5925 cbp->cb_max_taglen = taglen;
5928 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5932 * zfs holds [-r] <snap> ...
5934 * -r Recursively hold
5937 zfs_do_holds(int argc, char **argv)
5942 boolean_t scripted = B_FALSE;
5943 boolean_t recursive = B_FALSE;
5944 const char *opts = "rH";
5947 int types = ZFS_TYPE_SNAPSHOT;
5948 holds_cbdata_t cb = { 0 };
5955 while ((c = getopt(argc, argv, opts)) != -1) {
5964 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5971 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5972 flags |= ZFS_ITER_RECURSE;
5978 /* check number of arguments */
5982 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5985 for (i = 0; i < argc; ++i) {
5986 char *snapshot = argv[i];
5988 const char *snapname;
5990 delim = strchr(snapshot, '@');
5991 if (delim == NULL) {
5992 (void) fprintf(stderr,
5993 gettext("'%s' is not a snapshot\n"), snapshot);
5997 snapname = delim + 1;
5999 snapshot[delim - snapshot] = '\0';
6001 cb.cb_recursive = recursive;
6002 cb.cb_snapname = snapname;
6006 * 1. collect holds data, set format options
6008 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
6009 holds_callback, &cb);
6015 * 2. print holds data
6017 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
6019 if (nvlist_empty(nvl))
6020 (void) fprintf(stderr, gettext("no datasets available\n"));
6024 return (0 != errors);
6027 #define CHECK_SPINNER 30
6028 #define SPINNER_TIME 3 /* seconds */
6029 #define MOUNT_TIME 5 /* seconds */
6032 get_one_dataset(zfs_handle_t *zhp, void *data)
6034 static char *spin[] = { "-", "\\", "|", "/" };
6035 static int spinval = 0;
6036 static int spincheck = 0;
6037 static time_t last_spin_time = (time_t)0;
6038 get_all_cb_t *cbp = data;
6039 zfs_type_t type = zfs_get_type(zhp);
6041 if (cbp->cb_verbose) {
6042 if (--spincheck < 0) {
6043 time_t now = time(NULL);
6044 if (last_spin_time + SPINNER_TIME < now) {
6045 update_progress(spin[spinval++ % 4]);
6046 last_spin_time = now;
6048 spincheck = CHECK_SPINNER;
6053 * Iterate over any nested datasets.
6055 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
6061 * Skip any datasets whose type does not match.
6063 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
6067 libzfs_add_handle(cbp, zhp);
6068 assert(cbp->cb_used <= cbp->cb_alloc);
6074 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
6076 get_all_cb_t cb = { 0 };
6077 cb.cb_verbose = verbose;
6078 cb.cb_getone = get_one_dataset;
6081 set_progress_header(gettext("Reading ZFS config"));
6082 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
6084 *dslist = cb.cb_handles;
6085 *count = cb.cb_used;
6088 finish_progress(gettext("done."));
6092 * Generic callback for sharing or mounting filesystems. Because the code is so
6093 * similar, we have a common function with an extra parameter to determine which
6094 * mode we are using.
6096 #define OP_SHARE 0x1
6097 #define OP_MOUNT 0x2
6100 * Share or mount a dataset.
6103 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
6104 boolean_t explicit, const char *options)
6106 char mountpoint[ZFS_MAXPROPLEN];
6107 char shareopts[ZFS_MAXPROPLEN];
6108 char smbshareopts[ZFS_MAXPROPLEN];
6109 const char *cmdname = op == OP_SHARE ? "share" : "mount";
6111 uint64_t zoned, canmount;
6112 boolean_t shared_nfs, shared_smb;
6114 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
6117 * Check to make sure we can mount/share this dataset. If we
6118 * are in the global zone and the filesystem is exported to a
6119 * local zone, or if we are in a local zone and the
6120 * filesystem is not exported, then it is an error.
6122 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
6124 if (zoned && getzoneid() == GLOBAL_ZONEID) {
6128 (void) fprintf(stderr, gettext("cannot %s '%s': "
6129 "dataset is exported to a local zone\n"), cmdname,
6133 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
6137 (void) fprintf(stderr, gettext("cannot %s '%s': "
6138 "permission denied\n"), cmdname,
6144 * Ignore any filesystems which don't apply to us. This
6145 * includes those with a legacy mountpoint, or those with
6146 * legacy share options.
6148 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6149 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
6150 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
6151 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
6152 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
6153 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
6155 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
6156 strcmp(smbshareopts, "off") == 0) {
6160 (void) fprintf(stderr, gettext("cannot share '%s': "
6161 "legacy share\n"), zfs_get_name(zhp));
6162 (void) fprintf(stderr, gettext("use share(1M) to "
6163 "share this filesystem, or set "
6164 "sharenfs property on\n"));
6169 * We cannot share or mount legacy filesystems. If the
6170 * shareopts is non-legacy but the mountpoint is legacy, we
6171 * treat it as a legacy share.
6173 if (strcmp(mountpoint, "legacy") == 0) {
6177 (void) fprintf(stderr, gettext("cannot %s '%s': "
6178 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
6179 (void) fprintf(stderr, gettext("use %s(1M) to "
6180 "%s this filesystem\n"), cmdname, cmdname);
6184 if (strcmp(mountpoint, "none") == 0) {
6188 (void) fprintf(stderr, gettext("cannot %s '%s': no "
6189 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
6194 * canmount explicit outcome
6195 * on no pass through
6196 * on yes pass through
6198 * off yes display error, return 1
6199 * noauto no return 0
6200 * noauto yes pass through
6202 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
6203 if (canmount == ZFS_CANMOUNT_OFF) {
6207 (void) fprintf(stderr, gettext("cannot %s '%s': "
6208 "'canmount' property is set to 'off'\n"), cmdname,
6211 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
6216 * If this filesystem is inconsistent and has a receive resume
6217 * token, we can not mount it.
6219 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
6220 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
6221 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
6225 (void) fprintf(stderr, gettext("cannot %s '%s': "
6226 "Contains partially-completed state from "
6227 "\"zfs receive -s\", which can be resumed with "
6228 "\"zfs send -t\"\n"),
6229 cmdname, zfs_get_name(zhp));
6234 * At this point, we have verified that the mountpoint and/or
6235 * shareopts are appropriate for auto management. If the
6236 * filesystem is already mounted or shared, return (failing
6237 * for explicit requests); otherwise mount or share the
6243 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
6244 shared_smb = zfs_is_shared_smb(zhp, NULL);
6246 if ((shared_nfs && shared_smb) ||
6247 (shared_nfs && strcmp(shareopts, "on") == 0 &&
6248 strcmp(smbshareopts, "off") == 0) ||
6249 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
6250 strcmp(shareopts, "off") == 0)) {
6254 (void) fprintf(stderr, gettext("cannot share "
6255 "'%s': filesystem already shared\n"),
6260 if (!zfs_is_mounted(zhp, NULL) &&
6261 zfs_mount(zhp, NULL, flags) != 0)
6264 if (protocol == NULL) {
6265 if (zfs_shareall(zhp) != 0)
6267 } else if (strcmp(protocol, "nfs") == 0) {
6268 if (zfs_share_nfs(zhp))
6270 } else if (strcmp(protocol, "smb") == 0) {
6271 if (zfs_share_smb(zhp))
6274 (void) fprintf(stderr, gettext("cannot share "
6275 "'%s': invalid share type '%s' "
6277 zfs_get_name(zhp), protocol);
6284 if (options == NULL)
6285 mnt.mnt_mntopts = "";
6287 mnt.mnt_mntopts = (char *)options;
6289 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6290 zfs_is_mounted(zhp, NULL)) {
6294 (void) fprintf(stderr, gettext("cannot mount "
6295 "'%s': filesystem already mounted\n"),
6300 if (zfs_mount(zhp, options, flags) != 0)
6309 * Reports progress in the form "(current/total)". Not thread-safe.
6312 report_mount_progress(int current, int total)
6314 static time_t last_progress_time = 0;
6315 time_t now = time(NULL);
6318 /* report 1..n instead of 0..n-1 */
6321 /* display header if we're here for the first time */
6323 set_progress_header(gettext("Mounting ZFS filesystems"));
6324 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6325 /* too soon to report again */
6329 last_progress_time = now;
6331 (void) sprintf(info, "(%d/%d)", current, total);
6333 if (current == total)
6334 finish_progress(info);
6336 update_progress(info);
6340 append_options(char *mntopts, char *newopts)
6342 int len = strlen(mntopts);
6344 /* original length plus new string to append plus 1 for the comma */
6345 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6346 (void) fprintf(stderr, gettext("the opts argument for "
6347 "'%s' option is too long (more than %d chars)\n"),
6348 "-o", MNT_LINE_MAX);
6353 mntopts[len++] = ',';
6355 (void) strcpy(&mntopts[len], newopts);
6359 share_mount(int op, int argc, char **argv)
6362 boolean_t verbose = B_FALSE;
6364 char *options = NULL;
6368 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":alvo:O" : "al"))
6381 if (*optarg == '\0') {
6382 (void) fprintf(stderr, gettext("empty mount "
6383 "options (-o) specified\n"));
6387 if (options == NULL)
6388 options = safe_malloc(MNT_LINE_MAX + 1);
6390 /* option validation is done later */
6391 append_options(options, optarg);
6394 flags |= MS_OVERLAY;
6397 (void) fprintf(stderr, gettext("missing argument for "
6398 "'%c' option\n"), optopt);
6402 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6411 /* check number of arguments */
6413 zfs_handle_t **dslist = NULL;
6414 size_t i, count = 0;
6415 char *protocol = NULL;
6417 if (op == OP_SHARE && argc > 0) {
6418 if (strcmp(argv[0], "nfs") != 0 &&
6419 strcmp(argv[0], "smb") != 0) {
6420 (void) fprintf(stderr, gettext("share type "
6421 "must be 'nfs' or 'smb'\n"));
6430 (void) fprintf(stderr, gettext("too many arguments\n"));
6434 start_progress_timer();
6435 get_all_datasets(&dslist, &count, verbose);
6438 if (options != NULL)
6443 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6445 for (i = 0; i < count; i++) {
6447 report_mount_progress(i, count);
6449 if (share_mount_one(dslist[i], op, flags, protocol,
6450 B_FALSE, options) != 0)
6452 zfs_close(dslist[i]);
6456 } else if (argc == 0) {
6457 struct mnttab entry;
6459 if ((op == OP_SHARE) || (options != NULL)) {
6460 (void) fprintf(stderr, gettext("missing filesystem "
6461 "argument (specify -a for all)\n"));
6466 * When mount is given no arguments, go through
6467 * /proc/self/mounts and display any active ZFS mounts.
6468 * We hide any snapshots, since they are controlled
6472 /* Reopen MNTTAB to prevent reading stale data from open file */
6473 if (freopen(MNTTAB, "r", mnttab_file) == NULL) {
6474 if (options != NULL)
6479 while (getmntent(mnttab_file, &entry) == 0) {
6480 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6481 strchr(entry.mnt_special, '@') != NULL)
6484 (void) printf("%-30s %s\n", entry.mnt_special,
6492 (void) fprintf(stderr,
6493 gettext("too many arguments\n"));
6497 if ((zhp = zfs_open(g_zfs, argv[0],
6498 ZFS_TYPE_FILESYSTEM)) == NULL) {
6501 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6507 if (options != NULL)
6514 * zfs mount -a [nfs]
6515 * zfs mount filesystem
6517 * Mount all filesystems, or mount the given filesystem.
6520 zfs_do_mount(int argc, char **argv)
6522 return (share_mount(OP_MOUNT, argc, argv));
6526 * zfs share -a [nfs | smb]
6527 * zfs share filesystem
6529 * Share all filesystems, or share the given filesystem.
6532 zfs_do_share(int argc, char **argv)
6534 return (share_mount(OP_SHARE, argc, argv));
6537 typedef struct unshare_unmount_node {
6538 zfs_handle_t *un_zhp;
6540 uu_avl_node_t un_avlnode;
6541 } unshare_unmount_node_t;
6545 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6547 const unshare_unmount_node_t *l = larg;
6548 const unshare_unmount_node_t *r = rarg;
6550 return (strcmp(l->un_mountp, r->un_mountp));
6554 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6555 * absolute path, find the entry /proc/self/mounts, verify that its a
6556 * ZFS filesystems, and unmount it appropriately.
6559 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6563 struct stat64 statbuf;
6564 struct extmnttab entry;
6565 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6569 * Search for the path in /proc/self/mounts. Rather than looking for the
6570 * specific path, which can be fooled by non-standard paths (i.e. ".."
6571 * or "//"), we stat() the path and search for the corresponding
6572 * (major,minor) device pair.
6574 if (stat64(path, &statbuf) != 0) {
6575 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6576 cmdname, path, strerror(errno));
6579 path_inode = statbuf.st_ino;
6582 * Search for the given (major,minor) pair in the mount table.
6585 /* Reopen MNTTAB to prevent reading stale data from open file */
6586 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6589 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6590 if (entry.mnt_major == major(statbuf.st_dev) &&
6591 entry.mnt_minor == minor(statbuf.st_dev))
6595 if (op == OP_SHARE) {
6596 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6597 "currently mounted\n"), cmdname, path);
6600 (void) fprintf(stderr, gettext("warning: %s not in"
6601 "/proc/self/mounts\n"), path);
6602 if ((ret = umount2(path, flags)) != 0)
6603 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6608 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6609 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6610 "filesystem\n"), cmdname, path);
6614 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6615 ZFS_TYPE_FILESYSTEM)) == NULL)
6619 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6620 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6621 cmdname, path, strerror(errno));
6623 } else if (statbuf.st_ino != path_inode) {
6624 (void) fprintf(stderr, gettext("cannot "
6625 "%s '%s': not a mountpoint\n"), cmdname, path);
6629 if (op == OP_SHARE) {
6630 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6631 char smbshare_prop[ZFS_MAXPROPLEN];
6633 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6634 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6635 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6636 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6638 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6639 strcmp(smbshare_prop, "off") == 0) {
6640 (void) fprintf(stderr, gettext("cannot unshare "
6641 "'%s': legacy share\n"), path);
6642 (void) fprintf(stderr, gettext("use exportfs(8) "
6643 "or smbcontrol(1) to unshare this filesystem\n"));
6644 } else if (!zfs_is_shared(zhp)) {
6645 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6646 "not currently shared\n"), path);
6648 ret = zfs_unshareall_bypath(zhp, path);
6651 char mtpt_prop[ZFS_MAXPROPLEN];
6653 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6654 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6657 ret = zfs_unmount(zhp, NULL, flags);
6658 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6659 (void) fprintf(stderr, gettext("cannot unmount "
6660 "'%s': legacy mountpoint\n"),
6662 (void) fprintf(stderr, gettext("use umount(8) "
6663 "to unmount this filesystem\n"));
6665 ret = zfs_unmountall(zhp, flags);
6676 * Generic callback for unsharing or unmounting a filesystem.
6679 unshare_unmount(int op, int argc, char **argv)
6686 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6687 char sharesmb[ZFS_MAXPROPLEN];
6690 while ((c = getopt(argc, argv, op == OP_SHARE ? ":a" : "af")) != -1) {
6699 (void) fprintf(stderr, gettext("missing argument for "
6700 "'%c' option\n"), optopt);
6704 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6715 * We could make use of zfs_for_each() to walk all datasets in
6716 * the system, but this would be very inefficient, especially
6717 * since we would have to linearly search /proc/self/mounts for
6718 * each one. Instead, do one pass through /proc/self/mounts
6719 * looking for zfs entries and call zfs_unmount() for each one.
6721 * Things get a little tricky if the administrator has created
6722 * mountpoints beneath other ZFS filesystems. In this case, we
6723 * have to unmount the deepest filesystems first. To accomplish
6724 * this, we place all the mountpoints in an AVL tree sorted by
6725 * the special type (dataset name), and walk the result in
6726 * reverse to make sure to get any snapshots first.
6728 struct mnttab entry;
6729 uu_avl_pool_t *pool;
6730 uu_avl_t *tree = NULL;
6731 unshare_unmount_node_t *node;
6733 uu_avl_walk_t *walk;
6734 char *protocol = NULL;
6736 if (op == OP_SHARE && argc > 0) {
6737 if (strcmp(argv[0], "nfs") != 0 &&
6738 strcmp(argv[0], "smb") != 0) {
6739 (void) fprintf(stderr, gettext("share type "
6740 "must be 'nfs' or 'smb'\n"));
6749 (void) fprintf(stderr, gettext("too many arguments\n"));
6753 if (((pool = uu_avl_pool_create("unmount_pool",
6754 sizeof (unshare_unmount_node_t),
6755 offsetof(unshare_unmount_node_t, un_avlnode),
6756 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6757 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6760 /* Reopen MNTTAB to prevent reading stale data from open file */
6761 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6764 while (getmntent(mnttab_file, &entry) == 0) {
6766 /* ignore non-ZFS entries */
6767 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6770 /* ignore snapshots */
6771 if (strchr(entry.mnt_special, '@') != NULL)
6774 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6775 ZFS_TYPE_FILESYSTEM)) == NULL) {
6781 * Ignore datasets that are excluded/restricted by
6784 if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
6791 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6793 sizeof (nfs_mnt_prop),
6794 NULL, NULL, 0, B_FALSE) == 0);
6795 if (strcmp(nfs_mnt_prop, "off") != 0)
6797 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6799 sizeof (nfs_mnt_prop),
6800 NULL, NULL, 0, B_FALSE) == 0);
6801 if (strcmp(nfs_mnt_prop, "off") == 0)
6805 /* Ignore legacy mounts */
6806 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6808 sizeof (nfs_mnt_prop),
6809 NULL, NULL, 0, B_FALSE) == 0);
6810 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6812 /* Ignore canmount=noauto mounts */
6813 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6814 ZFS_CANMOUNT_NOAUTO)
6820 node = safe_malloc(sizeof (unshare_unmount_node_t));
6822 node->un_mountp = safe_strdup(entry.mnt_mountp);
6824 uu_avl_node_init(node, &node->un_avlnode, pool);
6826 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6827 uu_avl_insert(tree, node, idx);
6829 zfs_close(node->un_zhp);
6830 free(node->un_mountp);
6836 * Walk the AVL tree in reverse, unmounting each filesystem and
6837 * removing it from the AVL tree in the process.
6839 if ((walk = uu_avl_walk_start(tree,
6840 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6843 while ((node = uu_avl_walk_next(walk)) != NULL) {
6844 uu_avl_remove(tree, node);
6848 if (zfs_unshareall_bytype(node->un_zhp,
6849 node->un_mountp, protocol) != 0)
6854 if (zfs_unmount(node->un_zhp,
6855 node->un_zhp->zfs_name, flags) != 0)
6860 zfs_close(node->un_zhp);
6861 free(node->un_mountp);
6865 uu_avl_walk_end(walk);
6866 uu_avl_destroy(tree);
6867 uu_avl_pool_destroy(pool);
6872 (void) fprintf(stderr,
6873 gettext("missing filesystem argument\n"));
6875 (void) fprintf(stderr,
6876 gettext("too many arguments\n"));
6881 * We have an argument, but it may be a full path or a ZFS
6882 * filesystem. Pass full paths off to unmount_path() (shared by
6883 * manual_unmount), otherwise open the filesystem and pass to
6886 if (argv[0][0] == '/')
6887 return (unshare_unmount_path(op, argv[0],
6890 if ((zhp = zfs_open(g_zfs, argv[0],
6891 ZFS_TYPE_FILESYSTEM)) == NULL)
6894 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6895 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6896 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6897 NULL, 0, B_FALSE) == 0);
6901 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6903 sizeof (nfs_mnt_prop),
6904 NULL, NULL, 0, B_FALSE) == 0);
6905 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6906 sharesmb, sizeof (sharesmb), NULL, NULL,
6909 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6910 strcmp(sharesmb, "off") == 0) {
6911 (void) fprintf(stderr, gettext("cannot "
6912 "unshare '%s': legacy share\n"),
6914 (void) fprintf(stderr, gettext("use "
6915 "unshare(1M) to unshare this "
6918 } else if (!zfs_is_shared(zhp)) {
6919 (void) fprintf(stderr, gettext("cannot "
6920 "unshare '%s': not currently "
6921 "shared\n"), zfs_get_name(zhp));
6923 } else if (zfs_unshareall(zhp) != 0) {
6929 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6930 (void) fprintf(stderr, gettext("cannot "
6931 "unmount '%s': legacy "
6932 "mountpoint\n"), zfs_get_name(zhp));
6933 (void) fprintf(stderr, gettext("use "
6934 "umount(1M) to unmount this "
6937 } else if (!zfs_is_mounted(zhp, NULL)) {
6938 (void) fprintf(stderr, gettext("cannot "
6939 "unmount '%s': not currently "
6943 } else if (zfs_unmountall(zhp, flags) != 0) {
6957 * zfs unmount filesystem
6959 * Unmount all filesystems, or a specific ZFS filesystem.
6962 zfs_do_unmount(int argc, char **argv)
6964 return (unshare_unmount(OP_MOUNT, argc, argv));
6969 * zfs unshare filesystem
6971 * Unshare all filesystems, or a specific ZFS filesystem.
6974 zfs_do_unshare(int argc, char **argv)
6976 return (unshare_unmount(OP_SHARE, argc, argv));
6980 find_command_idx(char *command, int *idx)
6984 for (i = 0; i < NCOMMAND; i++) {
6985 if (command_table[i].name == NULL)
6988 if (strcmp(command, command_table[i].name) == 0) {
6997 zfs_do_diff(int argc, char **argv)
7001 char *tosnap = NULL;
7002 char *fromsnap = NULL;
7007 while ((c = getopt(argc, argv, "FHt")) != -1) {
7010 flags |= ZFS_DIFF_CLASSIFY;
7013 flags |= ZFS_DIFF_PARSEABLE;
7016 flags |= ZFS_DIFF_TIMESTAMP;
7019 (void) fprintf(stderr,
7020 gettext("invalid option '%c'\n"), optopt);
7029 (void) fprintf(stderr,
7030 gettext("must provide at least one snapshot name\n"));
7035 (void) fprintf(stderr, gettext("too many arguments\n"));
7040 tosnap = (argc == 2) ? argv[1] : NULL;
7043 if (*fromsnap != '@')
7044 copy = strdup(fromsnap);
7046 copy = strdup(tosnap);
7050 if ((atp = strchr(copy, '@')) != NULL)
7053 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) {
7060 * Ignore SIGPIPE so that the library can give us
7061 * information on any failure
7063 (void) sigignore(SIGPIPE);
7065 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
7073 * zfs bookmark <fs@snap> <fs#bmark>
7075 * Creates a bookmark with the given name from the given snapshot.
7078 zfs_do_bookmark(int argc, char **argv)
7080 char snapname[ZFS_MAX_DATASET_NAME_LEN];
7081 char bookname[ZFS_MAX_DATASET_NAME_LEN];
7088 while ((c = getopt(argc, argv, "")) != -1) {
7091 (void) fprintf(stderr,
7092 gettext("invalid option '%c'\n"), optopt);
7100 /* check number of arguments */
7102 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
7106 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
7110 if (strchr(argv[1], '#') == NULL) {
7111 (void) fprintf(stderr,
7112 gettext("invalid bookmark name '%s': "
7113 "must contain a '#'\n"), argv[1]);
7117 if (argv[0][0] == '@') {
7119 * Snapshot name begins with @.
7120 * Default to same fs as bookmark.
7122 (void) strlcpy(snapname, argv[1], sizeof (snapname));
7123 *strchr(snapname, '#') = '\0';
7124 (void) strlcat(snapname, argv[0], sizeof (snapname));
7126 (void) strlcpy(snapname, argv[0], sizeof (snapname));
7128 if (argv[1][0] == '#') {
7130 * Bookmark name begins with #.
7131 * Default to same fs as snapshot.
7133 (void) strlcpy(bookname, argv[0], sizeof (bookname));
7134 *strchr(bookname, '@') = '\0';
7135 (void) strlcat(bookname, argv[1], sizeof (bookname));
7137 (void) strlcpy(bookname, argv[1], sizeof (bookname));
7140 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
7146 nvl = fnvlist_alloc();
7147 fnvlist_add_string(nvl, bookname, snapname);
7148 ret = lzc_bookmark(nvl, NULL);
7152 const char *err_msg;
7155 (void) snprintf(errbuf, sizeof (errbuf),
7156 dgettext(TEXT_DOMAIN,
7157 "cannot create bookmark '%s'"), bookname);
7161 err_msg = "bookmark is in a different pool";
7164 err_msg = "bookmark exists";
7167 err_msg = "invalid argument";
7170 err_msg = "bookmark feature not enabled";
7173 err_msg = "out of space";
7176 err_msg = "dataset does not exist";
7179 err_msg = "unknown error";
7182 (void) fprintf(stderr, "%s: %s\n", errbuf,
7183 dgettext(TEXT_DOMAIN, err_msg));
7194 zfs_do_channel_program(int argc, char **argv)
7197 char *progbuf, *filename, *poolname;
7198 size_t progsize, progread;
7200 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
7201 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
7202 boolean_t sync_flag = B_TRUE;
7203 zpool_handle_t *zhp;
7206 while ((c = getopt(argc, argv, "nt:m:")) != -1) {
7214 arg = strtoull(optarg, &endp, 0);
7215 if (errno != 0 || *endp != '\0') {
7216 (void) fprintf(stderr, gettext(
7218 "'%s': expected integer\n"), optarg);
7223 if (arg > ZCP_MAX_INSTRLIMIT || arg == 0) {
7224 (void) fprintf(stderr, gettext(
7225 "Invalid instruction limit: "
7232 ASSERT3U(c, ==, 'm');
7233 if (arg > ZCP_MAX_MEMLIMIT || arg == 0) {
7234 (void) fprintf(stderr, gettext(
7235 "Invalid memory limit: "
7245 sync_flag = B_FALSE;
7249 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7259 (void) fprintf(stderr,
7260 gettext("invalid number of arguments\n"));
7266 if (strcmp(filename, "-") == 0) {
7268 filename = "standard input";
7269 } else if ((fd = open(filename, O_RDONLY)) < 0) {
7270 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
7271 filename, strerror(errno));
7275 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7276 (void) fprintf(stderr, gettext("cannot open pool '%s'"),
7283 * Read in the channel program, expanding the program buffer as
7288 progbuf = safe_malloc(progsize);
7290 ret = read(fd, progbuf + progread, progsize - progread);
7292 if (progread == progsize && ret > 0) {
7294 progbuf = safe_realloc(progbuf, progsize);
7302 (void) fprintf(stderr,
7303 gettext("cannot read '%s': %s\n"),
7304 filename, strerror(errno));
7307 progbuf[progread] = '\0';
7310 * Any remaining arguments are passed as arguments to the lua script as
7313 * "argv" -> [ "arg 1", ... "arg n" ],
7316 nvlist_t *argnvl = fnvlist_alloc();
7317 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);
7320 ret = lzc_channel_program(poolname, progbuf,
7321 instrlimit, memlimit, argnvl, &outnvl);
7323 ret = lzc_channel_program_nosync(poolname, progbuf,
7324 instrlimit, memlimit, argnvl, &outnvl);
7329 * On error, report the error message handed back by lua if one
7330 * exists. Otherwise, generate an appropriate error message,
7331 * falling back on strerror() for an unexpected return code.
7333 char *errstring = NULL;
7334 uint64_t instructions = 0;
7335 if (nvlist_exists(outnvl, ZCP_RET_ERROR)) {
7336 (void) nvlist_lookup_string(outnvl,
7337 ZCP_RET_ERROR, &errstring);
7338 if (errstring == NULL)
7339 errstring = strerror(ret);
7341 (void) nvlist_lookup_uint64(outnvl,
7342 ZCP_ARG_INSTRLIMIT, &instructions);
7348 "Invalid instruction or memory limit.";
7351 errstring = "Return value too large.";
7354 errstring = "Memory limit exhausted.";
7357 errstring = "Timed out.";
7360 errstring = "Permission denied. Channel "
7361 "programs must be run as root.";
7364 errstring = strerror(ret);
7367 (void) fprintf(stderr,
7368 gettext("Channel program execution failed:\n%s\n"),
7370 if (ret == ETIME && instructions != 0)
7371 (void) fprintf(stderr, "%llu Lua instructions\n",
7372 (u_longlong_t)instructions);
7374 (void) printf("Channel program fully executed ");
7375 if (nvlist_empty(outnvl)) {
7376 (void) printf("with no return value.\n");
7378 (void) printf("with return value:\n");
7379 dump_nvlist(outnvl, 4);
7384 fnvlist_free(outnvl);
7385 fnvlist_free(argnvl);
7394 typedef struct loadkey_cbdata {
7395 boolean_t cb_loadkey;
7396 boolean_t cb_recursive;
7398 char *cb_keylocation;
7399 uint64_t cb_numfailed;
7400 uint64_t cb_numattempted;
7404 load_key_callback(zfs_handle_t *zhp, void *data)
7407 boolean_t is_encroot;
7408 loadkey_cbdata_t *cb = data;
7409 uint64_t keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
7412 * If we are working recursively, we want to skip loading / unloading
7413 * keys for non-encryption roots and datasets whose keys are already
7414 * in the desired end-state.
7416 if (cb->cb_recursive) {
7417 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
7423 if ((cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_AVAILABLE) ||
7424 (!cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_UNAVAILABLE))
7428 cb->cb_numattempted++;
7431 ret = zfs_crypto_load_key(zhp, cb->cb_noop, cb->cb_keylocation);
7433 ret = zfs_crypto_unload_key(zhp);
7444 load_unload_keys(int argc, char **argv, boolean_t loadkey)
7446 int c, ret = 0, flags = 0;
7447 boolean_t do_all = B_FALSE;
7448 loadkey_cbdata_t cb = { 0 };
7450 cb.cb_loadkey = loadkey;
7452 while ((c = getopt(argc, argv, "anrL:")) != -1) {
7453 /* noop and alternate keylocations only apply to zfs load-key */
7457 cb.cb_noop = B_TRUE;
7460 cb.cb_keylocation = optarg;
7470 cb.cb_recursive = B_TRUE;
7473 flags |= ZFS_ITER_RECURSE;
7474 cb.cb_recursive = B_TRUE;
7477 (void) fprintf(stderr,
7478 gettext("invalid option '%c'\n"), optopt);
7486 if (!do_all && argc == 0) {
7487 (void) fprintf(stderr,
7488 gettext("Missing dataset argument or -a option\n"));
7492 if (do_all && argc != 0) {
7493 (void) fprintf(stderr,
7494 gettext("Cannot specify dataset with -a option\n"));
7498 if (cb.cb_recursive && cb.cb_keylocation != NULL &&
7499 strcmp(cb.cb_keylocation, "prompt") != 0) {
7500 (void) fprintf(stderr, gettext("alternate keylocation may only "
7501 "be 'prompt' with -r or -a\n"));
7505 ret = zfs_for_each(argc, argv, flags,
7506 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL, 0,
7507 load_key_callback, &cb);
7509 if (cb.cb_noop || (cb.cb_recursive && cb.cb_numattempted != 0)) {
7510 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
7511 (u_longlong_t)(cb.cb_numattempted - cb.cb_numfailed),
7512 (u_longlong_t)cb.cb_numattempted,
7513 loadkey ? (cb.cb_noop ? "verified" : "loaded") :
7517 if (cb.cb_numfailed != 0)
7524 zfs_do_load_key(int argc, char **argv)
7526 return (load_unload_keys(argc, argv, B_TRUE));
7531 zfs_do_unload_key(int argc, char **argv)
7533 return (load_unload_keys(argc, argv, B_FALSE));
7537 zfs_do_change_key(int argc, char **argv)
7541 boolean_t loadkey = B_FALSE, inheritkey = B_FALSE;
7542 zfs_handle_t *zhp = NULL;
7543 nvlist_t *props = fnvlist_alloc();
7545 while ((c = getopt(argc, argv, "lio:")) != -1) {
7551 inheritkey = B_TRUE;
7554 if (!parseprop(props, optarg)) {
7560 (void) fprintf(stderr,
7561 gettext("invalid option '%c'\n"), optopt);
7566 if (inheritkey && !nvlist_empty(props)) {
7567 (void) fprintf(stderr,
7568 gettext("Properties not allowed for inheriting\n"));
7576 (void) fprintf(stderr, gettext("Missing dataset argument\n"));
7581 (void) fprintf(stderr, gettext("Too many arguments\n"));
7585 zhp = zfs_open(g_zfs, argv[argc - 1],
7586 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
7591 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
7592 if (keystatus != ZFS_KEYSTATUS_AVAILABLE) {
7593 ret = zfs_crypto_load_key(zhp, B_FALSE, NULL);
7601 /* refresh the properties so the new keystatus is visible */
7602 zfs_refresh_properties(zhp);
7605 ret = zfs_crypto_rewrap(zhp, props, inheritkey);
7618 * 1) zfs project [-d|-r] <file|directory ...>
7619 * List project ID and inherit flag of file(s) or directories.
7620 * -d: List the directory itself, not its children.
7621 * -r: List subdirectories recursively.
7623 * 2) zfs project -C [-k] [-r] <file|directory ...>
7624 * Clear project inherit flag and/or ID on the file(s) or directories.
7625 * -k: Keep the project ID unchanged. If not specified, the project ID
7626 * will be reset as zero.
7627 * -r: Clear on subdirectories recursively.
7629 * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
7630 * Check project ID and inherit flag on the file(s) or directories,
7631 * report the outliers.
7632 * -0: Print file name followed by a NUL instead of newline.
7633 * -d: Check the directory itself, not its children.
7634 * -p: Specify the referenced ID for comparing with the target file(s)
7635 * or directories' project IDs. If not specified, the target (top)
7636 * directory's project ID will be used as the referenced one.
7637 * -r: Check subdirectories recursively.
7639 * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
7640 * Set project ID and/or inherit flag on the file(s) or directories.
7641 * -p: Set the project ID as the given id.
7642 * -r: Set on subdirectorie recursively. If not specify "-p" option,
7643 * it will use top-level directory's project ID as the given id,
7644 * then set both project ID and inherit flag on all descendants
7645 * of the top-level directory.
7646 * -s: Set project inherit flag.
7649 zfs_do_project(int argc, char **argv)
7651 zfs_project_control_t zpc = {
7652 .zpc_expected_projid = ZFS_INVALID_PROJID,
7653 .zpc_op = ZFS_PROJECT_OP_DEFAULT,
7654 .zpc_dironly = B_FALSE,
7655 .zpc_keep_projid = B_FALSE,
7656 .zpc_newline = B_TRUE,
7657 .zpc_recursive = B_FALSE,
7658 .zpc_set_flag = B_FALSE,
7665 while ((c = getopt(argc, argv, "0Ccdkp:rs")) != -1) {
7668 zpc.zpc_newline = B_FALSE;
7671 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
7672 (void) fprintf(stderr, gettext("cannot "
7673 "specify '-C' '-c' '-s' together\n"));
7677 zpc.zpc_op = ZFS_PROJECT_OP_CLEAR;
7680 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
7681 (void) fprintf(stderr, gettext("cannot "
7682 "specify '-C' '-c' '-s' together\n"));
7686 zpc.zpc_op = ZFS_PROJECT_OP_CHECK;
7689 zpc.zpc_dironly = B_TRUE;
7690 /* overwrite "-r" option */
7691 zpc.zpc_recursive = B_FALSE;
7694 zpc.zpc_keep_projid = B_TRUE;
7700 zpc.zpc_expected_projid = strtoull(optarg, &endptr, 0);
7701 if (errno != 0 || *endptr != '\0') {
7702 (void) fprintf(stderr,
7703 gettext("project ID must be less than "
7704 "%u\n"), UINT32_MAX);
7707 if (zpc.zpc_expected_projid >= UINT32_MAX) {
7708 (void) fprintf(stderr,
7709 gettext("invalid project ID\n"));
7715 zpc.zpc_recursive = B_TRUE;
7716 /* overwrite "-d" option */
7717 zpc.zpc_dironly = B_FALSE;
7720 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
7721 (void) fprintf(stderr, gettext("cannot "
7722 "specify '-C' '-c' '-s' together\n"));
7726 zpc.zpc_set_flag = B_TRUE;
7727 zpc.zpc_op = ZFS_PROJECT_OP_SET;
7730 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7736 if (zpc.zpc_op == ZFS_PROJECT_OP_DEFAULT) {
7737 if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID)
7738 zpc.zpc_op = ZFS_PROJECT_OP_SET;
7740 zpc.zpc_op = ZFS_PROJECT_OP_LIST;
7743 switch (zpc.zpc_op) {
7744 case ZFS_PROJECT_OP_LIST:
7745 if (zpc.zpc_keep_projid) {
7746 (void) fprintf(stderr,
7747 gettext("'-k' is only valid together with '-C'\n"));
7750 if (!zpc.zpc_newline) {
7751 (void) fprintf(stderr,
7752 gettext("'-0' is only valid together with '-c'\n"));
7756 case ZFS_PROJECT_OP_CHECK:
7757 if (zpc.zpc_keep_projid) {
7758 (void) fprintf(stderr,
7759 gettext("'-k' is only valid together with '-C'\n"));
7763 case ZFS_PROJECT_OP_CLEAR:
7764 if (zpc.zpc_dironly) {
7765 (void) fprintf(stderr,
7766 gettext("'-d' is useless together with '-C'\n"));
7769 if (!zpc.zpc_newline) {
7770 (void) fprintf(stderr,
7771 gettext("'-0' is only valid together with '-c'\n"));
7774 if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID) {
7775 (void) fprintf(stderr,
7776 gettext("'-p' is useless together with '-C'\n"));
7780 case ZFS_PROJECT_OP_SET:
7781 if (zpc.zpc_dironly) {
7782 (void) fprintf(stderr,
7783 gettext("'-d' is useless for set project ID and/or "
7787 if (zpc.zpc_keep_projid) {
7788 (void) fprintf(stderr,
7789 gettext("'-k' is only valid together with '-C'\n"));
7792 if (!zpc.zpc_newline) {
7793 (void) fprintf(stderr,
7794 gettext("'-0' is only valid together with '-c'\n"));
7806 (void) fprintf(stderr,
7807 gettext("missing file or directory target(s)\n"));
7811 for (int i = 0; i < argc; i++) {
7814 err = zfs_project_handle(argv[i], &zpc);
7823 main(int argc, char **argv)
7829 (void) setlocale(LC_ALL, "");
7830 (void) textdomain(TEXT_DOMAIN);
7835 * Make sure the user has specified some command.
7838 (void) fprintf(stderr, gettext("missing command\n"));
7845 * The 'umount' command is an alias for 'unmount'
7847 if (strcmp(cmdname, "umount") == 0)
7848 cmdname = "unmount";
7851 * The 'recv' command is an alias for 'receive'
7853 if (strcmp(cmdname, "recv") == 0)
7854 cmdname = "receive";
7857 * The 'snap' command is an alias for 'snapshot'
7859 if (strcmp(cmdname, "snap") == 0)
7860 cmdname = "snapshot";
7865 if ((strcmp(cmdname, "-?") == 0) ||
7866 (strcmp(cmdname, "--help") == 0))
7869 if ((g_zfs = libzfs_init()) == NULL) {
7870 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
7874 mnttab_file = g_zfs->libzfs_mnttab;
7876 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7878 libzfs_print_on_error(g_zfs, B_TRUE);
7881 * Run the appropriate command.
7883 libzfs_mnttab_cache(g_zfs, B_TRUE);
7884 if (find_command_idx(cmdname, &i) == 0) {
7885 current_command = &command_table[i];
7886 ret = command_table[i].func(argc - 1, argv + 1);
7887 } else if (strchr(cmdname, '=') != NULL) {
7888 verify(find_command_idx("set", &i) == 0);
7889 current_command = &command_table[i];
7890 ret = command_table[i].func(argc, argv);
7892 (void) fprintf(stderr, gettext("unrecognized "
7893 "command '%s'\n"), cmdname);
7898 if (ret == 0 && log_history)
7899 (void) zpool_log_history(g_zfs, history_str);
7904 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7905 * for the purposes of running ::findleaks.
7907 if (getenv("ZFS_ABORT") != NULL) {
7908 (void) printf("dumping core by request\n");