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.
30 * Copyright (c) 2018 Datto Inc.
40 #include <libnvpair.h>
52 #include <sys/debug.h>
54 #include <sys/mkdev.h>
55 #include <sys/mntent.h>
56 #include <sys/mnttab.h>
57 #include <sys/mount.h>
59 #include <sys/fs/zfs.h>
60 #include <sys/systeminfo.h>
61 #include <sys/types.h>
63 #include <sys/zfs_project.h>
66 #include <libzfs_core.h>
68 #include <zfs_deleg.h>
72 #include <directory.h>
73 #endif /* HAVE_IDMAP */
77 #include "zfs_comutil.h"
78 #include "libzfs_impl.h"
79 #include "zfs_projectutil.h"
81 libzfs_handle_t *g_zfs;
83 static FILE *mnttab_file;
84 static char history_str[HIS_MAX_RECORD_LEN];
85 static boolean_t log_history = B_TRUE;
87 static int zfs_do_clone(int argc, char **argv);
88 static int zfs_do_create(int argc, char **argv);
89 static int zfs_do_destroy(int argc, char **argv);
90 static int zfs_do_get(int argc, char **argv);
91 static int zfs_do_inherit(int argc, char **argv);
92 static int zfs_do_list(int argc, char **argv);
93 static int zfs_do_mount(int argc, char **argv);
94 static int zfs_do_rename(int argc, char **argv);
95 static int zfs_do_rollback(int argc, char **argv);
96 static int zfs_do_set(int argc, char **argv);
97 static int zfs_do_upgrade(int argc, char **argv);
98 static int zfs_do_snapshot(int argc, char **argv);
99 static int zfs_do_unmount(int argc, char **argv);
100 static int zfs_do_share(int argc, char **argv);
101 static int zfs_do_unshare(int argc, char **argv);
102 static int zfs_do_send(int argc, char **argv);
103 static int zfs_do_receive(int argc, char **argv);
104 static int zfs_do_promote(int argc, char **argv);
105 static int zfs_do_userspace(int argc, char **argv);
106 static int zfs_do_allow(int argc, char **argv);
107 static int zfs_do_unallow(int argc, char **argv);
108 static int zfs_do_hold(int argc, char **argv);
109 static int zfs_do_holds(int argc, char **argv);
110 static int zfs_do_release(int argc, char **argv);
111 static int zfs_do_diff(int argc, char **argv);
112 static int zfs_do_bookmark(int argc, char **argv);
113 static int zfs_do_channel_program(int argc, char **argv);
114 static int zfs_do_remap(int argc, char **argv);
115 static int zfs_do_load_key(int argc, char **argv);
116 static int zfs_do_unload_key(int argc, char **argv);
117 static int zfs_do_change_key(int argc, char **argv);
118 static int zfs_do_project(int argc, char **argv);
121 * Enable a reasonable set of defaults for libumem debugging on DEBUG builds.
126 _umem_debug_init(void)
128 return ("default,verbose"); /* $UMEM_DEBUG setting */
132 _umem_logging_init(void)
134 return ("fail,contents"); /* $UMEM_LOGGING setting */
169 HELP_CHANNEL_PROGRAM,
175 typedef struct zfs_command {
177 int (*func)(int argc, char **argv);
182 * Master command table. Each ZFS command has a name, associated function, and
183 * usage message. The usage messages need to be internationalized, so we have
184 * to have a function to return the usage message based on a command index.
186 * These commands are organized according to how they are displayed in the usage
187 * message. An empty command (one with a NULL name) indicates an empty line in
188 * the generic usage message.
190 static zfs_command_t command_table[] = {
191 { "create", zfs_do_create, HELP_CREATE },
192 { "destroy", zfs_do_destroy, HELP_DESTROY },
194 { "snapshot", zfs_do_snapshot, HELP_SNAPSHOT },
195 { "rollback", zfs_do_rollback, HELP_ROLLBACK },
196 { "clone", zfs_do_clone, HELP_CLONE },
197 { "promote", zfs_do_promote, HELP_PROMOTE },
198 { "rename", zfs_do_rename, HELP_RENAME },
199 { "bookmark", zfs_do_bookmark, HELP_BOOKMARK },
200 { "program", zfs_do_channel_program, HELP_CHANNEL_PROGRAM },
202 { "list", zfs_do_list, HELP_LIST },
204 { "set", zfs_do_set, HELP_SET },
205 { "get", zfs_do_get, HELP_GET },
206 { "inherit", zfs_do_inherit, HELP_INHERIT },
207 { "upgrade", zfs_do_upgrade, HELP_UPGRADE },
209 { "userspace", zfs_do_userspace, HELP_USERSPACE },
210 { "groupspace", zfs_do_userspace, HELP_GROUPSPACE },
211 { "projectspace", zfs_do_userspace, HELP_PROJECTSPACE },
213 { "project", zfs_do_project, HELP_PROJECT },
215 { "mount", zfs_do_mount, HELP_MOUNT },
216 { "unmount", zfs_do_unmount, HELP_UNMOUNT },
217 { "share", zfs_do_share, HELP_SHARE },
218 { "unshare", zfs_do_unshare, HELP_UNSHARE },
220 { "send", zfs_do_send, HELP_SEND },
221 { "receive", zfs_do_receive, HELP_RECEIVE },
223 { "allow", zfs_do_allow, HELP_ALLOW },
225 { "unallow", zfs_do_unallow, HELP_UNALLOW },
227 { "hold", zfs_do_hold, HELP_HOLD },
228 { "holds", zfs_do_holds, HELP_HOLDS },
229 { "release", zfs_do_release, HELP_RELEASE },
230 { "diff", zfs_do_diff, HELP_DIFF },
231 { "remap", zfs_do_remap, HELP_REMAP },
232 { "load-key", zfs_do_load_key, HELP_LOAD_KEY },
233 { "unload-key", zfs_do_unload_key, HELP_UNLOAD_KEY },
234 { "change-key", zfs_do_change_key, HELP_CHANGE_KEY },
237 #define NCOMMAND (sizeof (command_table) / sizeof (command_table[0]))
239 zfs_command_t *current_command;
242 get_usage(zfs_help_t idx)
246 return (gettext("\tclone [-p] [-o property=value] ... "
247 "<snapshot> <filesystem|volume>\n"));
249 return (gettext("\tcreate [-p] [-o property=value] ... "
251 "\tcreate [-ps] [-b blocksize] [-o property=value] ... "
252 "-V <size> <volume>\n"));
254 return (gettext("\tdestroy [-fnpRrv] <filesystem|volume>\n"
255 "\tdestroy [-dnpRrv] "
256 "<filesystem|volume>@<snap>[%<snap>][,...]\n"
257 "\tdestroy <filesystem|volume>#<bookmark>\n"));
259 return (gettext("\tget [-rHp] [-d max] "
260 "[-o \"all\" | field[,...]]\n"
261 "\t [-t type[,...]] [-s source[,...]]\n"
262 "\t <\"all\" | property[,...]> "
263 "[filesystem|volume|snapshot|bookmark] ...\n"));
265 return (gettext("\tinherit [-rS] <property> "
266 "<filesystem|volume|snapshot> ...\n"));
268 return (gettext("\tupgrade [-v]\n"
269 "\tupgrade [-r] [-V version] <-a | filesystem ...>\n"));
271 return (gettext("\tlist [-Hp] [-r|-d max] [-o property[,...]] "
272 "[-s property]...\n\t [-S property]... [-t type[,...]] "
273 "[filesystem|volume|snapshot] ...\n"));
275 return (gettext("\tmount\n"
276 "\tmount [-lvO] [-o opts] <-a | filesystem>\n"));
278 return (gettext("\tpromote <clone-filesystem>\n"));
280 return (gettext("\treceive [-vnsFu] "
281 "[-o <property>=<value>] ... [-x <property>] ...\n"
282 "\t <filesystem|volume|snapshot>\n"
283 "\treceive [-vnsFu] [-o <property>=<value>] ... "
284 "[-x <property>] ... \n"
285 "\t [-d | -e] <filesystem>\n"
286 "\treceive -A <filesystem|volume>\n"));
288 return (gettext("\trename [-f] <filesystem|volume|snapshot> "
289 "<filesystem|volume|snapshot>\n"
290 "\trename [-f] -p <filesystem|volume> <filesystem|volume>\n"
291 "\trename -r <snapshot> <snapshot>\n"));
293 return (gettext("\trollback [-rRf] <snapshot>\n"));
295 return (gettext("\tsend [-DnPpRvLecwb] [-[i|I] snapshot] "
297 "\tsend [-nvPLecw] [-i snapshot|bookmark] "
298 "<filesystem|volume|snapshot>\n"
299 "\tsend [-nvPe] -t <receive_resume_token>\n"));
301 return (gettext("\tset <property=value> ... "
302 "<filesystem|volume|snapshot> ...\n"));
304 return (gettext("\tshare [-l] <-a [nfs|smb] | filesystem>\n"));
306 return (gettext("\tsnapshot [-r] [-o property=value] ... "
307 "<filesystem|volume>@<snap> ...\n"));
309 return (gettext("\tunmount [-f] "
310 "<-a | filesystem|mountpoint>\n"));
312 return (gettext("\tunshare "
313 "<-a [nfs|smb] | filesystem|mountpoint>\n"));
315 return (gettext("\tallow <filesystem|volume>\n"
317 "<\"everyone\"|user|group>[,...] <perm|@setname>[,...]\n"
318 "\t <filesystem|volume>\n"
319 "\tallow [-ld] -e <perm|@setname>[,...] "
320 "<filesystem|volume>\n"
321 "\tallow -c <perm|@setname>[,...] <filesystem|volume>\n"
322 "\tallow -s @setname <perm|@setname>[,...] "
323 "<filesystem|volume>\n"));
325 return (gettext("\tunallow [-rldug] "
326 "<\"everyone\"|user|group>[,...]\n"
327 "\t [<perm|@setname>[,...]] <filesystem|volume>\n"
328 "\tunallow [-rld] -e [<perm|@setname>[,...]] "
329 "<filesystem|volume>\n"
330 "\tunallow [-r] -c [<perm|@setname>[,...]] "
331 "<filesystem|volume>\n"
332 "\tunallow [-r] -s @setname [<perm|@setname>[,...]] "
333 "<filesystem|volume>\n"));
335 return (gettext("\tuserspace [-Hinp] [-o field[,...]] "
337 "\t [-S field] ... [-t type[,...]] "
338 "<filesystem|snapshot>\n"));
339 case HELP_GROUPSPACE:
340 return (gettext("\tgroupspace [-Hinp] [-o field[,...]] "
342 "\t [-S field] ... [-t type[,...]] "
343 "<filesystem|snapshot>\n"));
344 case HELP_PROJECTSPACE:
345 return (gettext("\tprojectspace [-Hp] [-o field[,...]] "
347 "\t [-S field] ... <filesystem|snapshot>\n"));
349 return (gettext("\tproject [-d|-r] <directory|file ...>\n"
350 "\tproject -c [-0] [-d|-r] [-p id] <directory|file ...>\n"
351 "\tproject -C [-k] [-r] <directory ...>\n"
352 "\tproject [-p id] [-r] [-s] <directory ...>\n"));
354 return (gettext("\thold [-r] <tag> <snapshot> ...\n"));
356 return (gettext("\tholds [-r] <snapshot> ...\n"));
358 return (gettext("\trelease [-r] <tag> <snapshot> ...\n"));
360 return (gettext("\tdiff [-FHt] <snapshot> "
361 "[snapshot|filesystem]\n"));
363 return (gettext("\tremap <filesystem | volume>\n"));
365 return (gettext("\tbookmark <snapshot> <bookmark>\n"));
366 case HELP_CHANNEL_PROGRAM:
367 return (gettext("\tprogram [-jn] [-t <instruction limit>] "
368 "[-m <memory limit (b)>] <pool> <program file> "
371 return (gettext("\tload-key [-rn] [-L <keylocation>] "
372 "<-a | filesystem|volume>\n"));
373 case HELP_UNLOAD_KEY:
374 return (gettext("\tunload-key [-r] "
375 "<-a | filesystem|volume>\n"));
376 case HELP_CHANGE_KEY:
377 return (gettext("\tchange-key [-l] [-o keyformat=<value>]\n"
378 "\t [-o keylocation=<value>] [-o pbkfd2iters=<value>]\n"
379 "\t <filesystem|volume>\n"
380 "\tchange-key -i [-l] <filesystem|volume>\n"));
390 (void) fprintf(stderr, gettext("internal error: out of memory\n"));
395 * Utility function to guarantee malloc() success.
399 safe_malloc(size_t size)
403 if ((data = calloc(1, size)) == NULL)
410 safe_realloc(void *data, size_t size)
413 if ((newp = realloc(data, size)) == NULL) {
422 safe_strdup(char *str)
424 char *dupstr = strdup(str);
433 * Callback routine that will print out information for each of
437 usage_prop_cb(int prop, void *cb)
441 (void) fprintf(fp, "\t%-15s ", zfs_prop_to_name(prop));
443 if (zfs_prop_readonly(prop))
444 (void) fprintf(fp, " NO ");
446 (void) fprintf(fp, "YES ");
448 if (zfs_prop_inheritable(prop))
449 (void) fprintf(fp, " YES ");
451 (void) fprintf(fp, " NO ");
453 if (zfs_prop_values(prop) == NULL)
454 (void) fprintf(fp, "-\n");
456 (void) fprintf(fp, "%s\n", zfs_prop_values(prop));
462 * Display usage message. If we're inside a command, display only the usage for
463 * that command. Otherwise, iterate over the entire command table and display
464 * a complete usage message.
467 usage(boolean_t requested)
470 boolean_t show_properties = B_FALSE;
471 FILE *fp = requested ? stdout : stderr;
473 if (current_command == NULL) {
475 (void) fprintf(fp, gettext("usage: zfs command args ...\n"));
477 gettext("where 'command' is one of the following:\n\n"));
479 for (i = 0; i < NCOMMAND; i++) {
480 if (command_table[i].name == NULL)
481 (void) fprintf(fp, "\n");
483 (void) fprintf(fp, "%s",
484 get_usage(command_table[i].usage));
487 (void) fprintf(fp, gettext("\nEach dataset is of the form: "
488 "pool/[dataset/]*dataset[@name]\n"));
490 (void) fprintf(fp, gettext("usage:\n"));
491 (void) fprintf(fp, "%s", get_usage(current_command->usage));
494 if (current_command != NULL &&
495 (strcmp(current_command->name, "set") == 0 ||
496 strcmp(current_command->name, "get") == 0 ||
497 strcmp(current_command->name, "inherit") == 0 ||
498 strcmp(current_command->name, "list") == 0))
499 show_properties = B_TRUE;
501 if (show_properties) {
503 gettext("\nThe following properties are supported:\n"));
505 (void) fprintf(fp, "\n\t%-14s %s %s %s\n\n",
506 "PROPERTY", "EDIT", "INHERIT", "VALUES");
508 /* Iterate over all properties */
509 (void) zprop_iter(usage_prop_cb, fp, B_FALSE, B_TRUE,
512 (void) fprintf(fp, "\t%-15s ", "userused@...");
513 (void) fprintf(fp, " NO NO <size>\n");
514 (void) fprintf(fp, "\t%-15s ", "groupused@...");
515 (void) fprintf(fp, " NO NO <size>\n");
516 (void) fprintf(fp, "\t%-15s ", "projectused@...");
517 (void) fprintf(fp, " NO NO <size>\n");
518 (void) fprintf(fp, "\t%-15s ", "userobjused@...");
519 (void) fprintf(fp, " NO NO <size>\n");
520 (void) fprintf(fp, "\t%-15s ", "groupobjused@...");
521 (void) fprintf(fp, " NO NO <size>\n");
522 (void) fprintf(fp, "\t%-15s ", "projectobjused@...");
523 (void) fprintf(fp, " NO NO <size>\n");
524 (void) fprintf(fp, "\t%-15s ", "userquota@...");
525 (void) fprintf(fp, "YES NO <size> | none\n");
526 (void) fprintf(fp, "\t%-15s ", "groupquota@...");
527 (void) fprintf(fp, "YES NO <size> | none\n");
528 (void) fprintf(fp, "\t%-15s ", "projectquota@...");
529 (void) fprintf(fp, "YES NO <size> | none\n");
530 (void) fprintf(fp, "\t%-15s ", "userobjquota@...");
531 (void) fprintf(fp, "YES NO <size> | none\n");
532 (void) fprintf(fp, "\t%-15s ", "groupobjquota@...");
533 (void) fprintf(fp, "YES NO <size> | none\n");
534 (void) fprintf(fp, "\t%-15s ", "projectobjquota@...");
535 (void) fprintf(fp, "YES NO <size> | none\n");
536 (void) fprintf(fp, "\t%-15s ", "written@<snap>");
537 (void) fprintf(fp, " NO NO <size>\n");
539 (void) fprintf(fp, gettext("\nSizes are specified in bytes "
540 "with standard units such as K, M, G, etc.\n"));
541 (void) fprintf(fp, gettext("\nUser-defined properties can "
542 "be specified by using a name containing a colon (:).\n"));
543 (void) fprintf(fp, gettext("\nThe {user|group|project}"
544 "[obj]{used|quota}@ properties must be appended with\n"
545 "a user|group|project specifier of one of these forms:\n"
546 " POSIX name (eg: \"matt\")\n"
547 " POSIX id (eg: \"126829\")\n"
548 " SMB name@domain (eg: \"matt@sun\")\n"
549 " SMB SID (eg: \"S-1-234-567-89\")\n"));
552 gettext("\nFor the property list, run: %s\n"),
555 gettext("\nFor the delegated permission list, run: %s\n"),
556 "zfs allow|unallow");
560 * See comments at end of main().
562 if (getenv("ZFS_ABORT") != NULL) {
563 (void) printf("dumping core by request\n");
567 exit(requested ? 0 : 2);
571 * Take a property=value argument string and add it to the given nvlist.
572 * Modifies the argument inplace.
575 parseprop(nvlist_t *props, char *propname)
579 if ((propval = strchr(propname, '=')) == NULL) {
580 (void) fprintf(stderr, gettext("missing "
581 "'=' for property=value argument\n"));
586 if (nvlist_exists(props, propname)) {
587 (void) fprintf(stderr, gettext("property '%s' "
588 "specified multiple times\n"), propname);
591 if (nvlist_add_string(props, propname, propval) != 0)
597 * Take a property name argument and add it to the given nvlist.
598 * Modifies the argument inplace.
601 parsepropname(nvlist_t *props, char *propname)
603 if (strchr(propname, '=') != NULL) {
604 (void) fprintf(stderr, gettext("invalid character "
605 "'=' in property argument\n"));
608 if (nvlist_exists(props, propname)) {
609 (void) fprintf(stderr, gettext("property '%s' "
610 "specified multiple times\n"), propname);
613 if (nvlist_add_boolean(props, propname) != 0)
619 parse_depth(char *opt, int *flags)
624 depth = (int)strtol(opt, &tmp, 0);
626 (void) fprintf(stderr,
627 gettext("%s is not an integer\n"), optarg);
631 (void) fprintf(stderr,
632 gettext("Depth can not be negative.\n"));
635 *flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
639 #define PROGRESS_DELAY 2 /* seconds */
641 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";
642 static time_t pt_begin;
643 static char *pt_header = NULL;
644 static boolean_t pt_shown;
647 start_progress_timer(void)
649 pt_begin = time(NULL) + PROGRESS_DELAY;
654 set_progress_header(char *header)
656 assert(pt_header == NULL);
657 pt_header = safe_strdup(header);
659 (void) printf("%s: ", header);
660 (void) fflush(stdout);
665 update_progress(char *update)
667 if (!pt_shown && time(NULL) > pt_begin) {
668 int len = strlen(update);
670 (void) printf("%s: %s%*.*s", pt_header, update, len, len,
672 (void) fflush(stdout);
674 } else if (pt_shown) {
675 int len = strlen(update);
677 (void) printf("%s%*.*s", update, len, len, pt_reverse);
678 (void) fflush(stdout);
683 finish_progress(char *done)
686 (void) printf("%s\n", done);
687 (void) fflush(stdout);
694 zfs_mount_and_share(libzfs_handle_t *hdl, const char *dataset, zfs_type_t type)
696 zfs_handle_t *zhp = NULL;
699 zhp = zfs_open(hdl, dataset, type);
704 * Volumes may neither be mounted or shared. Potentially in the
705 * future filesystems detected on these volumes could be mounted.
707 if (zfs_get_type(zhp) == ZFS_TYPE_VOLUME) {
713 * Mount and/or share the new filesystem as appropriate. We provide a
714 * verbose error message to let the user know that their filesystem was
715 * in fact created, even if we failed to mount or share it.
717 * If the user doesn't want the dataset automatically mounted, then
718 * skip the mount/share step
720 if (zfs_prop_valid_for_type(ZFS_PROP_CANMOUNT, type, B_FALSE) &&
721 zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_ON) {
722 if (geteuid() != 0) {
723 (void) fprintf(stderr, gettext("filesystem "
724 "successfully created, but it may only be "
725 "mounted by root\n"));
727 } else if (zfs_mount(zhp, NULL, 0) != 0) {
728 (void) fprintf(stderr, gettext("filesystem "
729 "successfully created, but not mounted\n"));
731 } else if (zfs_share(zhp) != 0) {
732 (void) fprintf(stderr, gettext("filesystem "
733 "successfully created, but not shared\n"));
744 * zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
746 * Given an existing dataset, create a writable copy whose initial contents
747 * are the same as the source. The newly created dataset maintains a
748 * dependency on the original; the original cannot be destroyed so long as
751 * The '-p' flag creates all the non-existing ancestors of the target first.
754 zfs_do_clone(int argc, char **argv)
756 zfs_handle_t *zhp = NULL;
757 boolean_t parents = B_FALSE;
762 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
766 while ((c = getopt(argc, argv, "o:p")) != -1) {
769 if (!parseprop(props, optarg)) {
778 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
787 /* check number of arguments */
789 (void) fprintf(stderr, gettext("missing source dataset "
794 (void) fprintf(stderr, gettext("missing target dataset "
799 (void) fprintf(stderr, gettext("too many arguments\n"));
803 /* open the source dataset */
804 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL) {
809 if (parents && zfs_name_valid(argv[1], ZFS_TYPE_FILESYSTEM |
812 * Now create the ancestors of the target dataset. If the
813 * target already exists and '-p' option was used we should not
816 if (zfs_dataset_exists(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM |
822 if (zfs_create_ancestors(g_zfs, argv[1]) != 0) {
830 ret = zfs_clone(zhp, argv[1], props);
832 /* create the mountpoint if necessary */
835 (void) zpool_log_history(g_zfs, history_str);
836 log_history = B_FALSE;
839 ret = zfs_mount_and_share(g_zfs, argv[1], ZFS_TYPE_DATASET);
848 ASSERT3P(zhp, ==, NULL);
855 * zfs create [-p] [-o prop=value] ... fs
856 * zfs create [-ps] [-b blocksize] [-o prop=value] ... -V vol size
858 * Create a new dataset. This command can be used to create filesystems
859 * and volumes. Snapshot creation is handled by 'zfs snapshot'.
860 * For volumes, the user must specify a size to be used.
862 * The '-s' flag applies only to volumes, and indicates that we should not try
863 * to set the reservation for this volume. By default we set a reservation
864 * equal to the size for any volume. For pools with SPA_VERSION >=
865 * SPA_VERSION_REFRESERVATION, we set a refreservation instead.
867 * The '-p' flag creates all the non-existing ancestors of the target first.
870 zfs_do_create(int argc, char **argv)
872 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
873 uint64_t volsize = 0;
875 boolean_t noreserve = B_FALSE;
876 boolean_t bflag = B_FALSE;
877 boolean_t parents = B_FALSE;
882 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
886 while ((c = getopt(argc, argv, ":V:b:so:p")) != -1) {
889 type = ZFS_TYPE_VOLUME;
890 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
891 (void) fprintf(stderr, gettext("bad volume "
892 "size '%s': %s\n"), optarg,
893 libzfs_error_description(g_zfs));
897 if (nvlist_add_uint64(props,
898 zfs_prop_to_name(ZFS_PROP_VOLSIZE), intval) != 0)
907 if (zfs_nicestrtonum(g_zfs, optarg, &intval) != 0) {
908 (void) fprintf(stderr, gettext("bad volume "
909 "block size '%s': %s\n"), optarg,
910 libzfs_error_description(g_zfs));
914 if (nvlist_add_uint64(props,
915 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
920 if (!parseprop(props, optarg))
927 (void) fprintf(stderr, gettext("missing size "
931 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
937 if ((bflag || noreserve) && type != ZFS_TYPE_VOLUME) {
938 (void) fprintf(stderr, gettext("'-s' and '-b' can only be "
939 "used when creating a volume\n"));
946 /* check number of arguments */
948 (void) fprintf(stderr, gettext("missing %s argument\n"),
949 zfs_type_to_name(type));
953 (void) fprintf(stderr, gettext("too many arguments\n"));
957 if (type == ZFS_TYPE_VOLUME && !noreserve) {
958 zpool_handle_t *zpool_handle;
959 nvlist_t *real_props = NULL;
960 uint64_t spa_version;
962 zfs_prop_t resv_prop;
966 if ((p = strchr(argv[0], '/')) != NULL)
968 zpool_handle = zpool_open(g_zfs, argv[0]);
971 if (zpool_handle == NULL)
973 spa_version = zpool_get_prop_int(zpool_handle,
974 ZPOOL_PROP_VERSION, NULL);
975 if (spa_version >= SPA_VERSION_REFRESERVATION)
976 resv_prop = ZFS_PROP_REFRESERVATION;
978 resv_prop = ZFS_PROP_RESERVATION;
980 (void) snprintf(msg, sizeof (msg),
981 gettext("cannot create '%s'"), argv[0]);
982 if (props && (real_props = zfs_valid_proplist(g_zfs, type,
983 props, 0, NULL, zpool_handle, B_TRUE, msg)) == NULL) {
984 zpool_close(zpool_handle);
987 zpool_close(zpool_handle);
989 volsize = zvol_volsize_to_reservation(volsize, real_props);
990 nvlist_free(real_props);
992 if (nvlist_lookup_string(props, zfs_prop_to_name(resv_prop),
994 if (nvlist_add_uint64(props,
995 zfs_prop_to_name(resv_prop), volsize) != 0) {
1002 if (parents && zfs_name_valid(argv[0], type)) {
1004 * Now create the ancestors of target dataset. If the target
1005 * already exists and '-p' option was used we should not
1008 if (zfs_dataset_exists(g_zfs, argv[0], type)) {
1012 if (zfs_create_ancestors(g_zfs, argv[0]) != 0)
1016 /* pass to libzfs */
1017 if (zfs_create(g_zfs, argv[0], type, props) != 0)
1021 (void) zpool_log_history(g_zfs, history_str);
1022 log_history = B_FALSE;
1025 ret = zfs_mount_and_share(g_zfs, argv[0], ZFS_TYPE_DATASET);
1036 * zfs destroy [-rRf] <fs, vol>
1037 * zfs destroy [-rRd] <snap>
1039 * -r Recursively destroy all children
1040 * -R Recursively destroy all dependents, including clones
1041 * -f Force unmounting of any dependents
1042 * -d If we can't destroy now, mark for deferred destruction
1044 * Destroys the given dataset. By default, it will unmount any filesystems,
1045 * and refuse to destroy a dataset that has any dependents. A dependent can
1046 * either be a child, or a clone of a child.
1048 typedef struct destroy_cbdata {
1051 boolean_t cb_recurse;
1053 boolean_t cb_doclones;
1054 zfs_handle_t *cb_target;
1055 boolean_t cb_defer_destroy;
1056 boolean_t cb_verbose;
1057 boolean_t cb_parsable;
1058 boolean_t cb_dryrun;
1060 nvlist_t *cb_batchedsnaps;
1062 /* first snap in contiguous run */
1064 /* previous snap in contiguous run */
1066 int64_t cb_snapused;
1069 uint64_t cb_snap_count;
1073 * Check for any dependents based on the '-r' or '-R' flags.
1076 destroy_check_dependent(zfs_handle_t *zhp, void *data)
1078 destroy_cbdata_t *cbp = data;
1079 const char *tname = zfs_get_name(cbp->cb_target);
1080 const char *name = zfs_get_name(zhp);
1082 if (strncmp(tname, name, strlen(tname)) == 0 &&
1083 (name[strlen(tname)] == '/' || name[strlen(tname)] == '@')) {
1085 * This is a direct descendant, not a clone somewhere else in
1088 if (cbp->cb_recurse)
1091 if (cbp->cb_first) {
1092 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1093 "%s has children\n"),
1094 zfs_get_name(cbp->cb_target),
1095 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1096 (void) fprintf(stderr, gettext("use '-r' to destroy "
1097 "the following datasets:\n"));
1098 cbp->cb_first = B_FALSE;
1099 cbp->cb_error = B_TRUE;
1102 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1105 * This is a clone. We only want to report this if the '-r'
1106 * wasn't specified, or the target is a snapshot.
1108 if (!cbp->cb_recurse &&
1109 zfs_get_type(cbp->cb_target) != ZFS_TYPE_SNAPSHOT)
1112 if (cbp->cb_first) {
1113 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1114 "%s has dependent clones\n"),
1115 zfs_get_name(cbp->cb_target),
1116 zfs_type_to_name(zfs_get_type(cbp->cb_target)));
1117 (void) fprintf(stderr, gettext("use '-R' to destroy "
1118 "the following datasets:\n"));
1119 cbp->cb_first = B_FALSE;
1120 cbp->cb_error = B_TRUE;
1121 cbp->cb_dryrun = B_TRUE;
1124 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
1133 destroy_batched(destroy_cbdata_t *cb)
1135 int error = zfs_destroy_snaps_nvl(g_zfs,
1136 cb->cb_batchedsnaps, B_FALSE);
1137 fnvlist_free(cb->cb_batchedsnaps);
1138 cb->cb_batchedsnaps = fnvlist_alloc();
1143 destroy_callback(zfs_handle_t *zhp, void *data)
1145 destroy_cbdata_t *cb = data;
1146 const char *name = zfs_get_name(zhp);
1149 if (cb->cb_verbose) {
1150 if (cb->cb_parsable) {
1151 (void) printf("destroy\t%s\n", name);
1152 } else if (cb->cb_dryrun) {
1153 (void) printf(gettext("would destroy %s\n"),
1156 (void) printf(gettext("will destroy %s\n"),
1162 * Ignore pools (which we've already flagged as an error before getting
1165 if (strchr(zfs_get_name(zhp), '/') == NULL &&
1166 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1170 if (cb->cb_dryrun) {
1176 * We batch up all contiguous snapshots (even of different
1177 * filesystems) and destroy them with one ioctl. We can't
1178 * simply do all snap deletions and then all fs deletions,
1179 * because we must delete a clone before its origin.
1181 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) {
1182 cb->cb_snap_count++;
1183 fnvlist_add_boolean(cb->cb_batchedsnaps, name);
1184 if (cb->cb_snap_count % 10 == 0 && cb->cb_defer_destroy)
1185 error = destroy_batched(cb);
1187 error = destroy_batched(cb);
1189 zfs_unmount(zhp, NULL, cb->cb_force ? MS_FORCE : 0) != 0 ||
1190 zfs_destroy(zhp, cb->cb_defer_destroy) != 0) {
1201 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1203 destroy_cbdata_t *cb = arg;
1204 const char *name = zfs_get_name(zhp);
1207 if (nvlist_exists(cb->cb_nvl, name)) {
1208 if (cb->cb_firstsnap == NULL)
1209 cb->cb_firstsnap = strdup(name);
1210 if (cb->cb_prevsnap != NULL)
1211 free(cb->cb_prevsnap);
1212 /* this snap continues the current range */
1213 cb->cb_prevsnap = strdup(name);
1214 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1216 if (cb->cb_verbose) {
1217 if (cb->cb_parsable) {
1218 (void) printf("destroy\t%s\n", name);
1219 } else if (cb->cb_dryrun) {
1220 (void) printf(gettext("would destroy %s\n"),
1223 (void) printf(gettext("will destroy %s\n"),
1227 } else if (cb->cb_firstsnap != NULL) {
1228 /* end of this range */
1230 err = lzc_snaprange_space(cb->cb_firstsnap,
1231 cb->cb_prevsnap, &used);
1232 cb->cb_snapused += used;
1233 free(cb->cb_firstsnap);
1234 cb->cb_firstsnap = NULL;
1235 free(cb->cb_prevsnap);
1236 cb->cb_prevsnap = NULL;
1243 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1246 assert(cb->cb_firstsnap == NULL);
1247 assert(cb->cb_prevsnap == NULL);
1248 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1249 if (cb->cb_firstsnap != NULL) {
1252 err = lzc_snaprange_space(cb->cb_firstsnap,
1253 cb->cb_prevsnap, &used);
1255 cb->cb_snapused += used;
1256 free(cb->cb_firstsnap);
1257 cb->cb_firstsnap = NULL;
1258 free(cb->cb_prevsnap);
1259 cb->cb_prevsnap = NULL;
1265 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1267 destroy_cbdata_t *cb = arg;
1270 /* Check for clones. */
1271 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1272 cb->cb_target = zhp;
1273 cb->cb_first = B_TRUE;
1274 err = zfs_iter_dependents(zhp, B_TRUE,
1275 destroy_check_dependent, cb);
1279 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1287 gather_snapshots(zfs_handle_t *zhp, void *arg)
1289 destroy_cbdata_t *cb = arg;
1292 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1298 if (cb->cb_verbose) {
1299 err = destroy_print_snapshots(zhp, cb);
1305 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1313 destroy_clones(destroy_cbdata_t *cb)
1316 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1318 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1319 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1322 boolean_t defer = cb->cb_defer_destroy;
1326 * We can't defer destroy non-snapshots, so set it to
1327 * false while destroying the clones.
1329 cb->cb_defer_destroy = B_FALSE;
1330 err = zfs_iter_dependents(zhp, B_FALSE,
1331 destroy_callback, cb);
1332 cb->cb_defer_destroy = defer;
1342 zfs_do_destroy(int argc, char **argv)
1344 destroy_cbdata_t cb = { 0 };
1348 zfs_handle_t *zhp = NULL;
1350 zfs_type_t type = ZFS_TYPE_DATASET;
1353 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1356 cb.cb_verbose = B_TRUE;
1359 cb.cb_verbose = B_TRUE;
1360 cb.cb_parsable = B_TRUE;
1363 cb.cb_dryrun = B_TRUE;
1366 cb.cb_defer_destroy = B_TRUE;
1367 type = ZFS_TYPE_SNAPSHOT;
1370 cb.cb_force = B_TRUE;
1373 cb.cb_recurse = B_TRUE;
1376 cb.cb_recurse = B_TRUE;
1377 cb.cb_doclones = B_TRUE;
1381 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1390 /* check number of arguments */
1392 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1396 (void) fprintf(stderr, gettext("too many arguments\n"));
1400 at = strchr(argv[0], '@');
1401 pound = strchr(argv[0], '#');
1404 /* Build the list of snaps to destroy in cb_nvl. */
1405 cb.cb_nvl = fnvlist_alloc();
1408 zhp = zfs_open(g_zfs, argv[0],
1409 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1411 nvlist_free(cb.cb_nvl);
1415 cb.cb_snapspec = at + 1;
1416 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1422 if (nvlist_empty(cb.cb_nvl)) {
1423 (void) fprintf(stderr, gettext("could not find any "
1424 "snapshots to destroy; check snapshot names.\n"));
1429 if (cb.cb_verbose) {
1431 zfs_nicebytes(cb.cb_snapused, buf, sizeof (buf));
1432 if (cb.cb_parsable) {
1433 (void) printf("reclaim\t%llu\n",
1434 (u_longlong_t)cb.cb_snapused);
1435 } else if (cb.cb_dryrun) {
1436 (void) printf(gettext("would reclaim %s\n"),
1439 (void) printf(gettext("will reclaim %s\n"),
1444 if (!cb.cb_dryrun) {
1445 if (cb.cb_doclones) {
1446 cb.cb_batchedsnaps = fnvlist_alloc();
1447 err = destroy_clones(&cb);
1449 err = zfs_destroy_snaps_nvl(g_zfs,
1450 cb.cb_batchedsnaps, B_FALSE);
1458 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1459 cb.cb_defer_destroy);
1465 } else if (pound != NULL) {
1470 (void) fprintf(stderr,
1471 "dryrun is not supported with bookmark\n");
1475 if (cb.cb_defer_destroy) {
1476 (void) fprintf(stderr,
1477 "defer destroy is not supported with bookmark\n");
1481 if (cb.cb_recurse) {
1482 (void) fprintf(stderr,
1483 "recursive is not supported with bookmark\n");
1487 if (!zfs_bookmark_exists(argv[0])) {
1488 (void) fprintf(stderr, gettext("bookmark '%s' "
1489 "does not exist.\n"), argv[0]);
1493 nvl = fnvlist_alloc();
1494 fnvlist_add_boolean(nvl, argv[0]);
1496 err = lzc_destroy_bookmarks(nvl, NULL);
1498 (void) zfs_standard_error(g_zfs, err,
1499 "cannot destroy bookmark");
1506 /* Open the given dataset */
1507 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1513 * Perform an explicit check for pools before going any further.
1515 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1516 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1517 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1518 "operation does not apply to pools\n"),
1520 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1521 "%s' to destroy all datasets in the pool\n"),
1523 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1524 "to destroy the pool itself\n"), zfs_get_name(zhp));
1530 * Check for any dependents and/or clones.
1532 cb.cb_first = B_TRUE;
1533 if (!cb.cb_doclones &&
1534 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1544 cb.cb_batchedsnaps = fnvlist_alloc();
1545 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1552 * Do the real thing. The callback will close the
1553 * handle regardless of whether it succeeds or not.
1555 err = destroy_callback(zhp, &cb);
1558 err = zfs_destroy_snaps_nvl(g_zfs,
1559 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1566 fnvlist_free(cb.cb_batchedsnaps);
1567 fnvlist_free(cb.cb_nvl);
1574 is_recvd_column(zprop_get_cbdata_t *cbp)
1577 zfs_get_column_t col;
1579 for (i = 0; i < ZFS_GET_NCOLS &&
1580 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1581 if (col == GET_COL_RECVD)
1587 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1588 * < all | property[,property]... > < fs | snap | vol > ...
1590 * -r recurse over any child datasets
1591 * -H scripted mode. Headers are stripped, and fields are separated
1592 * by tabs instead of spaces.
1593 * -o Set of fields to display. One of "name,property,value,
1594 * received,source". Default is "name,property,value,source".
1595 * "all" is an alias for all five.
1596 * -s Set of sources to allow. One of
1597 * "local,default,inherited,received,temporary,none". Default is
1599 * -p Display values in parsable (literal) format.
1601 * Prints properties for the given datasets. The user can control which
1602 * columns to display as well as which property types to allow.
1606 * Invoked to display the properties for a single dataset.
1609 get_callback(zfs_handle_t *zhp, void *data)
1611 char buf[ZFS_MAXPROPLEN];
1612 char rbuf[ZFS_MAXPROPLEN];
1613 zprop_source_t sourcetype;
1614 char source[ZFS_MAX_DATASET_NAME_LEN];
1615 zprop_get_cbdata_t *cbp = data;
1616 nvlist_t *user_props = zfs_get_user_props(zhp);
1617 zprop_list_t *pl = cbp->cb_proplist;
1621 boolean_t received = is_recvd_column(cbp);
1623 for (; pl != NULL; pl = pl->pl_next) {
1624 char *recvdval = NULL;
1626 * Skip the special fake placeholder. This will also skip over
1627 * the name property when 'all' is specified.
1629 if (pl->pl_prop == ZFS_PROP_NAME &&
1630 pl == cbp->cb_proplist)
1633 if (pl->pl_prop != ZPROP_INVAL) {
1634 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1635 sizeof (buf), &sourcetype, source,
1637 cbp->cb_literal) != 0) {
1640 if (!zfs_prop_valid_for_type(pl->pl_prop,
1641 ZFS_TYPE_DATASET, B_FALSE)) {
1642 (void) fprintf(stderr,
1643 gettext("No such property '%s'\n"),
1644 zfs_prop_to_name(pl->pl_prop));
1647 sourcetype = ZPROP_SRC_NONE;
1648 (void) strlcpy(buf, "-", sizeof (buf));
1651 if (received && (zfs_prop_get_recvd(zhp,
1652 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1653 cbp->cb_literal) == 0))
1656 zprop_print_one_property(zfs_get_name(zhp), cbp,
1657 zfs_prop_to_name(pl->pl_prop),
1658 buf, sourcetype, source, recvdval);
1659 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1660 sourcetype = ZPROP_SRC_LOCAL;
1662 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1663 buf, sizeof (buf), cbp->cb_literal) != 0) {
1664 sourcetype = ZPROP_SRC_NONE;
1665 (void) strlcpy(buf, "-", sizeof (buf));
1668 zprop_print_one_property(zfs_get_name(zhp), cbp,
1669 pl->pl_user_prop, buf, sourcetype, source, NULL);
1670 } else if (zfs_prop_written(pl->pl_user_prop)) {
1671 sourcetype = ZPROP_SRC_LOCAL;
1673 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1674 buf, sizeof (buf), cbp->cb_literal) != 0) {
1675 sourcetype = ZPROP_SRC_NONE;
1676 (void) strlcpy(buf, "-", sizeof (buf));
1679 zprop_print_one_property(zfs_get_name(zhp), cbp,
1680 pl->pl_user_prop, buf, sourcetype, source, NULL);
1682 if (nvlist_lookup_nvlist(user_props,
1683 pl->pl_user_prop, &propval) != 0) {
1686 sourcetype = ZPROP_SRC_NONE;
1689 verify(nvlist_lookup_string(propval,
1690 ZPROP_VALUE, &strval) == 0);
1691 verify(nvlist_lookup_string(propval,
1692 ZPROP_SOURCE, &sourceval) == 0);
1694 if (strcmp(sourceval,
1695 zfs_get_name(zhp)) == 0) {
1696 sourcetype = ZPROP_SRC_LOCAL;
1697 } else if (strcmp(sourceval,
1698 ZPROP_SOURCE_VAL_RECVD) == 0) {
1699 sourcetype = ZPROP_SRC_RECEIVED;
1701 sourcetype = ZPROP_SRC_INHERITED;
1702 (void) strlcpy(source,
1703 sourceval, sizeof (source));
1707 if (received && (zfs_prop_get_recvd(zhp,
1708 pl->pl_user_prop, rbuf, sizeof (rbuf),
1709 cbp->cb_literal) == 0))
1712 zprop_print_one_property(zfs_get_name(zhp), cbp,
1713 pl->pl_user_prop, strval, sourcetype,
1722 zfs_do_get(int argc, char **argv)
1724 zprop_get_cbdata_t cb = { 0 };
1725 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1726 int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
1727 char *value, *fields;
1730 zprop_list_t fake_name = { 0 };
1733 * Set up default columns and sources.
1735 cb.cb_sources = ZPROP_SRC_ALL;
1736 cb.cb_columns[0] = GET_COL_NAME;
1737 cb.cb_columns[1] = GET_COL_PROPERTY;
1738 cb.cb_columns[2] = GET_COL_VALUE;
1739 cb.cb_columns[3] = GET_COL_SOURCE;
1740 cb.cb_type = ZFS_TYPE_DATASET;
1743 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1746 cb.cb_literal = B_TRUE;
1749 limit = parse_depth(optarg, &flags);
1752 flags |= ZFS_ITER_RECURSE;
1755 cb.cb_scripted = B_TRUE;
1758 (void) fprintf(stderr, gettext("missing argument for "
1759 "'%c' option\n"), optopt);
1764 * Process the set of columns to display. We zero out
1765 * the structure to give us a blank slate.
1767 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1769 while (*optarg != '\0') {
1770 static char *col_subopts[] =
1771 { "name", "property", "value", "received",
1772 "source", "all", NULL };
1774 if (i == ZFS_GET_NCOLS) {
1775 (void) fprintf(stderr, gettext("too "
1776 "many fields given to -o "
1781 switch (getsubopt(&optarg, col_subopts,
1784 cb.cb_columns[i++] = GET_COL_NAME;
1787 cb.cb_columns[i++] = GET_COL_PROPERTY;
1790 cb.cb_columns[i++] = GET_COL_VALUE;
1793 cb.cb_columns[i++] = GET_COL_RECVD;
1794 flags |= ZFS_ITER_RECVD_PROPS;
1797 cb.cb_columns[i++] = GET_COL_SOURCE;
1801 (void) fprintf(stderr,
1802 gettext("\"all\" conflicts "
1803 "with specific fields "
1804 "given to -o option\n"));
1807 cb.cb_columns[0] = GET_COL_NAME;
1808 cb.cb_columns[1] = GET_COL_PROPERTY;
1809 cb.cb_columns[2] = GET_COL_VALUE;
1810 cb.cb_columns[3] = GET_COL_RECVD;
1811 cb.cb_columns[4] = GET_COL_SOURCE;
1812 flags |= ZFS_ITER_RECVD_PROPS;
1816 (void) fprintf(stderr,
1817 gettext("invalid column name "
1826 while (*optarg != '\0') {
1827 static char *source_subopts[] = {
1828 "local", "default", "inherited",
1829 "received", "temporary", "none",
1832 switch (getsubopt(&optarg, source_subopts,
1835 cb.cb_sources |= ZPROP_SRC_LOCAL;
1838 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1841 cb.cb_sources |= ZPROP_SRC_INHERITED;
1844 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1847 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1850 cb.cb_sources |= ZPROP_SRC_NONE;
1853 (void) fprintf(stderr,
1854 gettext("invalid source "
1863 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1864 while (*optarg != '\0') {
1865 static char *type_subopts[] = { "filesystem",
1866 "volume", "snapshot", "bookmark",
1869 switch (getsubopt(&optarg, type_subopts,
1872 types |= ZFS_TYPE_FILESYSTEM;
1875 types |= ZFS_TYPE_VOLUME;
1878 types |= ZFS_TYPE_SNAPSHOT;
1881 types |= ZFS_TYPE_BOOKMARK;
1884 types = ZFS_TYPE_DATASET |
1889 (void) fprintf(stderr,
1890 gettext("invalid type '%s'\n"),
1898 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1908 (void) fprintf(stderr, gettext("missing property "
1915 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1923 * As part of zfs_expand_proplist(), we keep track of the maximum column
1924 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1925 * need to know the maximum name length. However, the user likely did
1926 * not specify 'name' as one of the properties to fetch, so we need to
1927 * make sure we always include at least this property for
1928 * print_get_headers() to work properly.
1930 if (cb.cb_proplist != NULL) {
1931 fake_name.pl_prop = ZFS_PROP_NAME;
1932 fake_name.pl_width = strlen(gettext("NAME"));
1933 fake_name.pl_next = cb.cb_proplist;
1934 cb.cb_proplist = &fake_name;
1937 cb.cb_first = B_TRUE;
1939 /* run for each object */
1940 ret = zfs_for_each(argc, argv, flags, types, NULL,
1941 &cb.cb_proplist, limit, get_callback, &cb);
1943 if (cb.cb_proplist == &fake_name)
1944 zprop_free_list(fake_name.pl_next);
1946 zprop_free_list(cb.cb_proplist);
1952 * inherit [-rS] <property> <fs|vol> ...
1954 * -r Recurse over all children
1955 * -S Revert to received value, if any
1957 * For each dataset specified on the command line, inherit the given property
1958 * from its parent. Inheriting a property at the pool level will cause it to
1959 * use the default value. The '-r' flag will recurse over all children, and is
1960 * useful for setting a property on a hierarchy-wide basis, regardless of any
1961 * local modifications for each dataset.
1964 typedef struct inherit_cbdata {
1965 const char *cb_propname;
1966 boolean_t cb_received;
1970 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1972 inherit_cbdata_t *cb = data;
1973 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1976 * If we're doing it recursively, then ignore properties that
1977 * are not valid for this type of dataset.
1979 if (prop != ZPROP_INVAL &&
1980 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp), B_FALSE))
1983 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1987 inherit_cb(zfs_handle_t *zhp, void *data)
1989 inherit_cbdata_t *cb = data;
1991 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1995 zfs_do_inherit(int argc, char **argv)
1999 inherit_cbdata_t cb = { 0 };
2003 boolean_t received = B_FALSE;
2006 while ((c = getopt(argc, argv, "rS")) != -1) {
2009 flags |= ZFS_ITER_RECURSE;
2016 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2025 /* check number of arguments */
2027 (void) fprintf(stderr, gettext("missing property argument\n"));
2031 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2039 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
2040 if (zfs_prop_readonly(prop)) {
2041 (void) fprintf(stderr, gettext(
2042 "%s property is read-only\n"),
2046 if (!zfs_prop_inheritable(prop) && !received) {
2047 (void) fprintf(stderr, gettext("'%s' property cannot "
2048 "be inherited\n"), propname);
2049 if (prop == ZFS_PROP_QUOTA ||
2050 prop == ZFS_PROP_RESERVATION ||
2051 prop == ZFS_PROP_REFQUOTA ||
2052 prop == ZFS_PROP_REFRESERVATION) {
2053 (void) fprintf(stderr, gettext("use 'zfs set "
2054 "%s=none' to clear\n"), propname);
2055 (void) fprintf(stderr, gettext("use 'zfs "
2056 "inherit -S %s' to revert to received "
2057 "value\n"), propname);
2061 if (received && (prop == ZFS_PROP_VOLSIZE ||
2062 prop == ZFS_PROP_VERSION)) {
2063 (void) fprintf(stderr, gettext("'%s' property cannot "
2064 "be reverted to a received value\n"), propname);
2067 } else if (!zfs_prop_user(propname)) {
2068 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
2073 cb.cb_propname = propname;
2074 cb.cb_received = received;
2076 if (flags & ZFS_ITER_RECURSE) {
2077 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2078 NULL, NULL, 0, inherit_recurse_cb, &cb);
2080 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2081 NULL, NULL, 0, inherit_cb, &cb);
2087 typedef struct upgrade_cbdata {
2088 uint64_t cb_numupgraded;
2089 uint64_t cb_numsamegraded;
2090 uint64_t cb_numfailed;
2091 uint64_t cb_version;
2093 boolean_t cb_foundone;
2094 char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
2098 same_pool(zfs_handle_t *zhp, const char *name)
2100 int len1 = strcspn(name, "/@");
2101 const char *zhname = zfs_get_name(zhp);
2102 int len2 = strcspn(zhname, "/@");
2106 return (strncmp(name, zhname, len1) == 0);
2110 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2112 upgrade_cbdata_t *cb = data;
2113 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2115 /* list if it's old/new */
2116 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2117 (cb->cb_newer && version > ZPL_VERSION)) {
2120 str = gettext("The following filesystems are "
2121 "formatted using a newer software version and\n"
2122 "cannot be accessed on the current system.\n\n");
2124 str = gettext("The following filesystems are "
2125 "out of date, and can be upgraded. After being\n"
2126 "upgraded, these filesystems (and any 'zfs send' "
2127 "streams generated from\n"
2128 "subsequent snapshots) will no longer be "
2129 "accessible by older software versions.\n\n");
2132 if (!cb->cb_foundone) {
2134 (void) printf(gettext("VER FILESYSTEM\n"));
2135 (void) printf(gettext("--- ------------\n"));
2136 cb->cb_foundone = B_TRUE;
2139 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2146 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2148 upgrade_cbdata_t *cb = data;
2149 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2150 int needed_spa_version;
2153 if (zfs_spa_version(zhp, &spa_version) < 0)
2156 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2158 if (needed_spa_version < 0)
2161 if (spa_version < needed_spa_version) {
2163 (void) printf(gettext("%s: can not be "
2164 "upgraded; the pool version needs to first "
2165 "be upgraded\nto version %d\n\n"),
2166 zfs_get_name(zhp), needed_spa_version);
2172 if (version < cb->cb_version) {
2174 (void) snprintf(verstr, sizeof (verstr),
2175 "%llu", (u_longlong_t)cb->cb_version);
2176 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2178 * If they did "zfs upgrade -a", then we could
2179 * be doing ioctls to different pools. We need
2180 * to log this history once to each pool, and bypass
2181 * the normal history logging that happens in main().
2183 (void) zpool_log_history(g_zfs, history_str);
2184 log_history = B_FALSE;
2186 if (zfs_prop_set(zhp, "version", verstr) == 0)
2187 cb->cb_numupgraded++;
2190 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2191 } else if (version > cb->cb_version) {
2192 /* can't downgrade */
2193 (void) printf(gettext("%s: can not be downgraded; "
2194 "it is already at version %u\n"),
2195 zfs_get_name(zhp), version);
2198 cb->cb_numsamegraded++;
2206 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2209 zfs_do_upgrade(int argc, char **argv)
2211 boolean_t all = B_FALSE;
2212 boolean_t showversions = B_FALSE;
2214 upgrade_cbdata_t cb = { 0 };
2216 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2219 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2222 flags |= ZFS_ITER_RECURSE;
2225 showversions = B_TRUE;
2228 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2229 optarg, &cb.cb_version) != 0) {
2230 (void) fprintf(stderr,
2231 gettext("invalid version %s\n"), optarg);
2240 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2249 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2251 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2252 cb.cb_version || argc))
2254 if ((all || argc) && (showversions))
2260 /* Show info on available versions. */
2261 (void) printf(gettext("The following filesystem versions are "
2263 (void) printf(gettext("VER DESCRIPTION\n"));
2264 (void) printf("--- -----------------------------------------"
2265 "---------------\n");
2266 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2267 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2268 (void) printf(gettext(" 3 Case insensitive and filesystem "
2269 "user identifier (FUID)\n"));
2270 (void) printf(gettext(" 4 userquota, groupquota "
2272 (void) printf(gettext(" 5 System attributes\n"));
2273 (void) printf(gettext("\nFor more information on a particular "
2274 "version, including supported releases,\n"));
2275 (void) printf("see the ZFS Administration Guide.\n\n");
2277 } else if (argc || all) {
2278 /* Upgrade filesystems */
2279 if (cb.cb_version == 0)
2280 cb.cb_version = ZPL_VERSION;
2281 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2282 NULL, NULL, 0, upgrade_set_callback, &cb);
2283 (void) printf(gettext("%llu filesystems upgraded\n"),
2284 (u_longlong_t)cb.cb_numupgraded);
2285 if (cb.cb_numsamegraded) {
2286 (void) printf(gettext("%llu filesystems already at "
2288 (u_longlong_t)cb.cb_numsamegraded);
2290 if (cb.cb_numfailed != 0)
2293 /* List old-version filesystems */
2295 (void) printf(gettext("This system is currently running "
2296 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2298 flags |= ZFS_ITER_RECURSE;
2299 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2300 NULL, NULL, 0, upgrade_list_callback, &cb);
2302 found = cb.cb_foundone;
2303 cb.cb_foundone = B_FALSE;
2304 cb.cb_newer = B_TRUE;
2306 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2307 NULL, NULL, 0, upgrade_list_callback, &cb);
2309 if (!cb.cb_foundone && !found) {
2310 (void) printf(gettext("All filesystems are "
2311 "formatted with the current version.\n"));
2319 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2320 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2321 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2322 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2323 * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2324 * [-S field [-S field]...] filesystem | snapshot
2326 * -H Scripted mode; elide headers and separate columns by tabs.
2327 * -i Translate SID to POSIX ID.
2328 * -n Print numeric ID instead of user/group name.
2329 * -o Control which fields to display.
2330 * -p Use exact (parsable) numeric output.
2331 * -s Specify sort columns, descending order.
2332 * -S Specify sort columns, ascending order.
2333 * -t Control which object types to display.
2335 * Displays space consumed by, and quotas on, each user in the specified
2336 * filesystem or snapshot.
2339 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2340 enum us_field_types {
2348 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA",
2349 "OBJUSED", "OBJQUOTA" };
2350 static char *us_field_names[] = { "type", "name", "used", "quota",
2351 "objused", "objquota" };
2352 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2354 #define USTYPE_PSX_GRP (1 << 0)
2355 #define USTYPE_PSX_USR (1 << 1)
2356 #define USTYPE_SMB_GRP (1 << 2)
2357 #define USTYPE_SMB_USR (1 << 3)
2358 #define USTYPE_PROJ (1 << 4)
2359 #define USTYPE_ALL \
2360 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2363 static int us_type_bits[] = {
2370 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2373 typedef struct us_node {
2375 uu_avl_node_t usn_avlnode;
2376 uu_list_node_t usn_listnode;
2379 typedef struct us_cbdata {
2381 uu_avl_pool_t *cb_avl_pool;
2383 boolean_t cb_numname;
2384 boolean_t cb_nicenum;
2385 boolean_t cb_sid2posix;
2386 zfs_userquota_prop_t cb_prop;
2387 zfs_sort_column_t *cb_sortcol;
2388 size_t cb_width[USFIELD_LAST];
2391 static boolean_t us_populated = B_FALSE;
2394 zfs_sort_column_t *si_sortcol;
2395 boolean_t si_numname;
2399 us_field_index(char *field)
2403 for (i = 0; i < USFIELD_LAST; i++) {
2404 if (strcmp(field, us_field_names[i]) == 0)
2412 us_compare(const void *larg, const void *rarg, void *unused)
2414 const us_node_t *l = larg;
2415 const us_node_t *r = rarg;
2416 us_sort_info_t *si = (us_sort_info_t *)unused;
2417 zfs_sort_column_t *sortcol = si->si_sortcol;
2418 boolean_t numname = si->si_numname;
2419 nvlist_t *lnvl = l->usn_nvl;
2420 nvlist_t *rnvl = r->usn_nvl;
2424 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2431 zfs_prop_t prop = sortcol->sc_prop;
2432 const char *propname = NULL;
2433 boolean_t reverse = sortcol->sc_reverse;
2438 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2439 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2441 rc = (rv32 < lv32) ? 1 : -1;
2447 (void) nvlist_lookup_uint64(lnvl, propname,
2449 (void) nvlist_lookup_uint64(rnvl, propname,
2452 rc = (rv64 < lv64) ? 1 : -1;
2454 if ((nvlist_lookup_string(lnvl, propname,
2455 &lvstr) == ENOENT) ||
2456 (nvlist_lookup_string(rnvl, propname,
2457 &rvstr) == ENOENT)) {
2460 rc = strcmp(lvstr, rvstr);
2464 case ZFS_PROP_QUOTA:
2467 if (prop == ZFS_PROP_USED)
2471 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2472 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2474 rc = (rv64 < lv64) ? 1 : -1;
2483 return (reverse ? 1 : -1);
2485 return (reverse ? -1 : 1);
2490 * If entries still seem to be the same, check if they are of the same
2491 * type (smbentity is added only if we are doing SID to POSIX ID
2492 * translation where we can have duplicate type/name combinations).
2494 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2495 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2497 return (lvb < rvb ? -1 : 1);
2503 zfs_prop_is_user(unsigned p)
2505 return (p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA ||
2506 p == ZFS_PROP_USEROBJUSED || p == ZFS_PROP_USEROBJQUOTA);
2510 zfs_prop_is_group(unsigned p)
2512 return (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA ||
2513 p == ZFS_PROP_GROUPOBJUSED || p == ZFS_PROP_GROUPOBJQUOTA);
2517 zfs_prop_is_project(unsigned p)
2519 return (p == ZFS_PROP_PROJECTUSED || p == ZFS_PROP_PROJECTQUOTA ||
2520 p == ZFS_PROP_PROJECTOBJUSED || p == ZFS_PROP_PROJECTOBJQUOTA);
2523 static inline const char *
2524 us_type2str(unsigned field_type)
2526 switch (field_type) {
2527 case USTYPE_PSX_USR:
2528 return ("POSIX User");
2529 case USTYPE_PSX_GRP:
2530 return ("POSIX Group");
2531 case USTYPE_SMB_USR:
2532 return ("SMB User");
2533 case USTYPE_SMB_GRP:
2534 return ("SMB Group");
2538 return ("Undefined");
2543 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2545 us_cbdata_t *cb = (us_cbdata_t *)arg;
2546 zfs_userquota_prop_t prop = cb->cb_prop;
2551 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2552 uu_avl_t *avl = cb->cb_avl;
2556 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2558 const char *typestr;
2562 int typeidx, nameidx, sizeidx;
2563 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2564 boolean_t smbentity = B_FALSE;
2566 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2568 node = safe_malloc(sizeof (us_node_t));
2569 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2570 node->usn_nvl = props;
2572 if (domain != NULL && domain[0] != '\0') {
2575 char sid[MAXNAMELEN + 32];
2579 directory_error_t e;
2583 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2585 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2586 type = USTYPE_SMB_GRP;
2587 err = sid_to_id(sid, B_FALSE, &id);
2589 type = USTYPE_SMB_USR;
2590 err = sid_to_id(sid, B_TRUE, &id);
2595 if (!cb->cb_sid2posix) {
2596 e = directory_name_from_sid(NULL, sid, &name,
2599 directory_error_free(e);
2609 #endif /* HAVE_IDMAP */
2612 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2614 if (zfs_prop_is_group(prop)) {
2615 type = USTYPE_PSX_GRP;
2616 if (!cb->cb_numname) {
2619 if ((g = getgrgid(rid)) != NULL)
2622 } else if (zfs_prop_is_user(prop)) {
2623 type = USTYPE_PSX_USR;
2624 if (!cb->cb_numname) {
2627 if ((p = getpwuid(rid)) != NULL)
2636 * Make sure that the type/name combination is unique when doing
2637 * SID to POSIX ID translation (hence changing the type from SMB to
2640 if (cb->cb_sid2posix &&
2641 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2644 /* Calculate/update width of TYPE field */
2645 typestr = us_type2str(type);
2646 typelen = strlen(gettext(typestr));
2647 typeidx = us_field_index("type");
2648 if (typelen > cb->cb_width[typeidx])
2649 cb->cb_width[typeidx] = typelen;
2650 if (nvlist_add_uint32(props, "type", type) != 0)
2653 /* Calculate/update width of NAME field */
2654 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2655 if (nvlist_add_uint64(props, "name", rid) != 0)
2657 namelen = snprintf(NULL, 0, "%u", rid);
2659 if (nvlist_add_string(props, "name", name) != 0)
2661 namelen = strlen(name);
2663 nameidx = us_field_index("name");
2664 if (nameidx >= 0 && namelen > cb->cb_width[nameidx])
2665 cb->cb_width[nameidx] = namelen;
2668 * Check if this type/name combination is in the list and update it;
2669 * otherwise add new node to the list.
2671 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2672 uu_avl_insert(avl, node, idx);
2677 props = node->usn_nvl;
2680 /* Calculate/update width of USED/QUOTA fields */
2681 if (cb->cb_nicenum) {
2682 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
2683 prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
2684 prop == ZFS_PROP_PROJECTUSED ||
2685 prop == ZFS_PROP_PROJECTQUOTA) {
2686 zfs_nicebytes(space, sizebuf, sizeof (sizebuf));
2688 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2691 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
2692 (u_longlong_t)space);
2694 sizelen = strlen(sizebuf);
2695 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
2696 prop == ZFS_PROP_PROJECTUSED) {
2698 if (!nvlist_exists(props, "quota"))
2699 (void) nvlist_add_uint64(props, "quota", 0);
2700 } else if (prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
2701 prop == ZFS_PROP_PROJECTQUOTA) {
2703 if (!nvlist_exists(props, "used"))
2704 (void) nvlist_add_uint64(props, "used", 0);
2705 } else if (prop == ZFS_PROP_USEROBJUSED ||
2706 prop == ZFS_PROP_GROUPOBJUSED || prop == ZFS_PROP_PROJECTOBJUSED) {
2707 propname = "objused";
2708 if (!nvlist_exists(props, "objquota"))
2709 (void) nvlist_add_uint64(props, "objquota", 0);
2710 } else if (prop == ZFS_PROP_USEROBJQUOTA ||
2711 prop == ZFS_PROP_GROUPOBJQUOTA ||
2712 prop == ZFS_PROP_PROJECTOBJQUOTA) {
2713 propname = "objquota";
2714 if (!nvlist_exists(props, "objused"))
2715 (void) nvlist_add_uint64(props, "objused", 0);
2719 sizeidx = us_field_index(propname);
2720 if (sizeidx >= 0 && sizelen > cb->cb_width[sizeidx])
2721 cb->cb_width[sizeidx] = sizelen;
2723 if (nvlist_add_uint64(props, propname, space) != 0)
2730 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2731 size_t *width, us_node_t *node)
2733 nvlist_t *nvl = node->usn_nvl;
2734 char valstr[MAXNAMELEN];
2735 boolean_t first = B_TRUE;
2741 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2742 if (!(ustype & types))
2745 while ((field = fields[cfield]) != USFIELD_LAST) {
2746 nvpair_t *nvp = NULL;
2752 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2753 if (strcmp(nvpair_name(nvp),
2754 us_field_names[field]) == 0)
2758 type = nvp == NULL ? DATA_TYPE_UNKNOWN : nvpair_type(nvp);
2760 case DATA_TYPE_UINT32:
2761 (void) nvpair_value_uint32(nvp, &val32);
2763 case DATA_TYPE_UINT64:
2764 (void) nvpair_value_uint64(nvp, &val64);
2766 case DATA_TYPE_STRING:
2767 (void) nvpair_value_string(nvp, &strval);
2769 case DATA_TYPE_UNKNOWN:
2772 (void) fprintf(stderr, "invalid data type\n");
2777 if (type == DATA_TYPE_UINT32)
2778 strval = (char *)us_type2str(val32);
2781 if (type == DATA_TYPE_UINT64) {
2782 (void) sprintf(valstr, "%llu",
2783 (u_longlong_t)val64);
2789 if (type == DATA_TYPE_UINT64) {
2791 (void) sprintf(valstr, "%llu",
2792 (u_longlong_t)val64);
2794 } else if (field == USFIELD_QUOTA &&
2798 zfs_nicebytes(val64, valstr,
2804 case USFIELD_OBJUSED:
2805 case USFIELD_OBJQUOTA:
2806 if (type == DATA_TYPE_UINT64) {
2808 (void) sprintf(valstr, "%llu",
2809 (u_longlong_t)val64);
2811 } else if (field == USFIELD_OBJQUOTA &&
2815 zfs_nicenum(val64, valstr,
2825 (void) printf("\t");
2830 (void) printf("%s", strval);
2831 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2832 (void) printf("%-*s", (int)width[field], strval);
2834 (void) printf("%*s", (int)width[field], strval);
2840 (void) printf("\n");
2844 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2845 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2853 boolean_t first = B_TRUE;
2855 while ((field = fields[cfield]) != USFIELD_LAST) {
2856 col = gettext(us_field_hdr[field]);
2857 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2858 (void) printf(first ? "%-*s" : " %-*s",
2859 (int)width[field], col);
2861 (void) printf(first ? "%*s" : " %*s",
2862 (int)width[field], col);
2867 (void) printf("\n");
2870 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2871 print_us_node(scripted, parsable, fields, types, width, node);
2873 nvlist_free(node->usn_nvl);
2878 zfs_do_userspace(int argc, char **argv)
2881 zfs_userquota_prop_t p;
2882 uu_avl_pool_t *avl_pool;
2884 uu_avl_walk_t *walk;
2886 char deffields[] = "type,name,used,quota,objused,objquota";
2887 char *ofield = NULL;
2888 char *tfield = NULL;
2892 boolean_t scripted = B_FALSE;
2893 boolean_t prtnum = B_FALSE;
2894 boolean_t parsable = B_FALSE;
2895 boolean_t sid2posix = B_FALSE;
2898 zfs_sort_column_t *sortcol = NULL;
2899 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2903 uu_list_pool_t *listpool;
2905 uu_avl_index_t idx = 0;
2906 uu_list_index_t idx2 = 0;
2911 if (strcmp(argv[0], "groupspace") == 0) {
2912 /* Toggle default group types */
2913 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2914 } else if (strcmp(argv[0], "projectspace") == 0) {
2915 types = USTYPE_PROJ;
2919 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2922 if (types == USTYPE_PROJ) {
2923 (void) fprintf(stderr,
2924 gettext("invalid option 'n'\n"));
2940 if (zfs_add_sort_column(&sortcol, optarg,
2941 c == 's' ? B_FALSE : B_TRUE) != 0) {
2942 (void) fprintf(stderr,
2943 gettext("invalid field '%s'\n"), optarg);
2948 if (types == USTYPE_PROJ) {
2949 (void) fprintf(stderr,
2950 gettext("invalid option 't'\n"));
2956 if (types == USTYPE_PROJ) {
2957 (void) fprintf(stderr,
2958 gettext("invalid option 'i'\n"));
2964 (void) fprintf(stderr, gettext("missing argument for "
2965 "'%c' option\n"), optopt);
2969 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2979 (void) fprintf(stderr, gettext("missing dataset name\n"));
2983 (void) fprintf(stderr, gettext("too many arguments\n"));
2987 /* Use default output fields if not specified using -o */
2991 if ((delim = strchr(ofield, ',')) != NULL)
2993 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
2994 (void) fprintf(stderr, gettext("invalid type '%s' "
2995 "for -o option\n"), ofield);
3000 } while (delim != NULL);
3001 fields[cfield] = USFIELD_LAST;
3003 /* Override output types (-t option) */
3004 if (tfield != NULL) {
3008 boolean_t found = B_FALSE;
3010 if ((delim = strchr(tfield, ',')) != NULL)
3012 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
3014 if (strcmp(tfield, us_type_names[i]) == 0) {
3016 types |= us_type_bits[i];
3021 (void) fprintf(stderr, gettext("invalid type "
3022 "'%s' for -t option\n"), tfield);
3027 } while (delim != NULL);
3030 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
3033 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
3034 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
3036 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
3039 /* Always add default sorting columns */
3040 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
3041 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
3043 cb.cb_sortcol = sortcol;
3044 cb.cb_numname = prtnum;
3045 cb.cb_nicenum = !parsable;
3046 cb.cb_avl_pool = avl_pool;
3047 cb.cb_avl = avl_tree;
3048 cb.cb_sid2posix = sid2posix;
3050 for (i = 0; i < USFIELD_LAST; i++)
3051 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
3053 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
3054 if ((zfs_prop_is_user(p) &&
3055 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
3056 (zfs_prop_is_group(p) &&
3057 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))) ||
3058 (zfs_prop_is_project(p) && types != USTYPE_PROJ))
3062 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
3067 if ((node = uu_avl_first(avl_tree)) == NULL)
3070 us_populated = B_TRUE;
3072 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
3073 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
3074 list = uu_list_create(listpool, NULL, UU_DEFAULT);
3075 uu_list_node_init(node, &node->usn_listnode, listpool);
3077 while (node != NULL) {
3079 node = uu_avl_next(avl_tree, node);
3080 uu_avl_remove(avl_tree, rmnode);
3081 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
3082 uu_list_insert(list, rmnode, idx2);
3085 for (node = uu_list_first(list); node != NULL;
3086 node = uu_list_next(list, node)) {
3087 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
3089 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
3090 uu_avl_insert(avl_tree, node, idx);
3093 uu_list_destroy(list);
3094 uu_list_pool_destroy(listpool);
3096 /* Print and free node nvlist memory */
3097 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
3100 zfs_free_sort_columns(sortcol);
3102 /* Clean up the AVL tree */
3103 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
3106 while ((node = uu_avl_walk_next(walk)) != NULL) {
3107 uu_avl_remove(cb.cb_avl, node);
3111 uu_avl_walk_end(walk);
3112 uu_avl_destroy(avl_tree);
3113 uu_avl_pool_destroy(avl_pool);
3119 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3120 * [-t type[,...]] [filesystem|volume|snapshot] ...
3122 * -H Scripted mode; elide headers and separate columns by tabs
3123 * -p Display values in parsable (literal) format.
3124 * -r Recurse over all children
3125 * -d Limit recursion by depth.
3126 * -o Control which fields to display.
3127 * -s Specify sort columns, descending order.
3128 * -S Specify sort columns, ascending order.
3129 * -t Control which object types to display.
3131 * When given no arguments, list all filesystems in the system.
3132 * Otherwise, list the specified datasets, optionally recursing down them if
3133 * '-r' is specified.
3135 typedef struct list_cbdata {
3137 boolean_t cb_literal;
3138 boolean_t cb_scripted;
3139 zprop_list_t *cb_proplist;
3143 * Given a list of columns to display, output appropriate headers for each one.
3146 print_header(list_cbdata_t *cb)
3148 zprop_list_t *pl = cb->cb_proplist;
3149 char headerbuf[ZFS_MAXPROPLEN];
3152 boolean_t first = B_TRUE;
3153 boolean_t right_justify;
3155 for (; pl != NULL; pl = pl->pl_next) {
3162 right_justify = B_FALSE;
3163 if (pl->pl_prop != ZPROP_INVAL) {
3164 header = zfs_prop_column_name(pl->pl_prop);
3165 right_justify = zfs_prop_align_right(pl->pl_prop);
3167 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3168 headerbuf[i] = toupper(pl->pl_user_prop[i]);
3169 headerbuf[i] = '\0';
3173 if (pl->pl_next == NULL && !right_justify)
3174 (void) printf("%s", header);
3175 else if (right_justify)
3176 (void) printf("%*s", (int)pl->pl_width, header);
3178 (void) printf("%-*s", (int)pl->pl_width, header);
3181 (void) printf("\n");
3185 * Given a dataset and a list of fields, print out all the properties according
3186 * to the described layout.
3189 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3191 zprop_list_t *pl = cb->cb_proplist;
3192 boolean_t first = B_TRUE;
3193 char property[ZFS_MAXPROPLEN];
3194 nvlist_t *userprops = zfs_get_user_props(zhp);
3197 boolean_t right_justify;
3199 for (; pl != NULL; pl = pl->pl_next) {
3201 if (cb->cb_scripted)
3202 (void) printf("\t");
3209 if (pl->pl_prop == ZFS_PROP_NAME) {
3210 (void) strlcpy(property, zfs_get_name(zhp),
3213 right_justify = zfs_prop_align_right(pl->pl_prop);
3214 } else if (pl->pl_prop != ZPROP_INVAL) {
3215 if (zfs_prop_get(zhp, pl->pl_prop, property,
3216 sizeof (property), NULL, NULL, 0,
3217 cb->cb_literal) != 0)
3221 right_justify = zfs_prop_align_right(pl->pl_prop);
3222 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3223 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3224 property, sizeof (property), cb->cb_literal) != 0)
3228 right_justify = B_TRUE;
3229 } else if (zfs_prop_written(pl->pl_user_prop)) {
3230 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3231 property, sizeof (property), cb->cb_literal) != 0)
3235 right_justify = B_TRUE;
3237 if (nvlist_lookup_nvlist(userprops,
3238 pl->pl_user_prop, &propval) != 0)
3241 verify(nvlist_lookup_string(propval,
3242 ZPROP_VALUE, &propstr) == 0);
3243 right_justify = B_FALSE;
3247 * If this is being called in scripted mode, or if this is the
3248 * last column and it is left-justified, don't include a width
3251 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3252 (void) printf("%s", propstr);
3253 else if (right_justify)
3254 (void) printf("%*s", (int)pl->pl_width, propstr);
3256 (void) printf("%-*s", (int)pl->pl_width, propstr);
3259 (void) printf("\n");
3263 * Generic callback function to list a dataset or snapshot.
3266 list_callback(zfs_handle_t *zhp, void *data)
3268 list_cbdata_t *cbp = data;
3270 if (cbp->cb_first) {
3271 if (!cbp->cb_scripted)
3273 cbp->cb_first = B_FALSE;
3276 print_dataset(zhp, cbp);
3282 zfs_do_list(int argc, char **argv)
3285 static char default_fields[] =
3286 "name,used,available,referenced,mountpoint";
3287 int types = ZFS_TYPE_DATASET;
3288 boolean_t types_specified = B_FALSE;
3289 char *fields = NULL;
3290 list_cbdata_t cb = { 0 };
3294 zfs_sort_column_t *sortcol = NULL;
3295 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3298 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3304 cb.cb_literal = B_TRUE;
3305 flags |= ZFS_ITER_LITERAL_PROPS;
3308 limit = parse_depth(optarg, &flags);
3311 flags |= ZFS_ITER_RECURSE;
3314 cb.cb_scripted = B_TRUE;
3317 if (zfs_add_sort_column(&sortcol, optarg,
3319 (void) fprintf(stderr,
3320 gettext("invalid property '%s'\n"), optarg);
3325 if (zfs_add_sort_column(&sortcol, optarg,
3327 (void) fprintf(stderr,
3328 gettext("invalid property '%s'\n"), optarg);
3334 types_specified = B_TRUE;
3335 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3336 while (*optarg != '\0') {
3337 static char *type_subopts[] = { "filesystem",
3338 "volume", "snapshot", "snap", "bookmark",
3341 switch (getsubopt(&optarg, type_subopts,
3344 types |= ZFS_TYPE_FILESYSTEM;
3347 types |= ZFS_TYPE_VOLUME;
3351 types |= ZFS_TYPE_SNAPSHOT;
3354 types |= ZFS_TYPE_BOOKMARK;
3357 types = ZFS_TYPE_DATASET |
3361 (void) fprintf(stderr,
3362 gettext("invalid type '%s'\n"),
3369 (void) fprintf(stderr, gettext("missing argument for "
3370 "'%c' option\n"), optopt);
3374 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3384 fields = default_fields;
3387 * If we are only going to list snapshot names and sort by name,
3388 * then we can use faster version.
3390 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3391 flags |= ZFS_ITER_SIMPLE;
3394 * If "-o space" and no types were specified, don't display snapshots.
3396 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3397 types &= ~ZFS_TYPE_SNAPSHOT;
3400 * If the user specifies '-o all', the zprop_get_list() doesn't
3401 * normally include the name of the dataset. For 'zfs list', we always
3402 * want this property to be first.
3404 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3408 cb.cb_first = B_TRUE;
3410 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3411 limit, list_callback, &cb);
3413 zprop_free_list(cb.cb_proplist);
3414 zfs_free_sort_columns(sortcol);
3416 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3417 (void) fprintf(stderr, gettext("no datasets available\n"));
3423 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3424 * zfs rename [-f] -p <fs | vol> <fs | vol>
3425 * zfs rename -r <snap> <snap>
3427 * Renames the given dataset to another of the same type.
3429 * The '-p' flag creates all the non-existing ancestors of the target first.
3433 zfs_do_rename(int argc, char **argv)
3438 boolean_t recurse = B_FALSE;
3439 boolean_t parents = B_FALSE;
3440 boolean_t force_unmount = B_FALSE;
3443 while ((c = getopt(argc, argv, "prf")) != -1) {
3452 force_unmount = B_TRUE;
3456 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3465 /* check number of arguments */
3467 (void) fprintf(stderr, gettext("missing source dataset "
3472 (void) fprintf(stderr, gettext("missing target dataset "
3477 (void) fprintf(stderr, gettext("too many arguments\n"));
3481 if (recurse && parents) {
3482 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3487 if (recurse && strchr(argv[0], '@') == 0) {
3488 (void) fprintf(stderr, gettext("source dataset for recursive "
3489 "rename must be a snapshot\n"));
3493 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3494 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3497 /* If we were asked and the name looks good, try to create ancestors. */
3498 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3499 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3504 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3513 * Promotes the given clone fs to be the parent
3517 zfs_do_promote(int argc, char **argv)
3523 if (argc > 1 && argv[1][0] == '-') {
3524 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3529 /* check number of arguments */
3531 (void) fprintf(stderr, gettext("missing clone filesystem"
3536 (void) fprintf(stderr, gettext("too many arguments\n"));
3540 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3544 ret = (zfs_promote(zhp) != 0);
3552 * zfs rollback [-rRf] <snapshot>
3554 * -r Delete any intervening snapshots before doing rollback
3555 * -R Delete any snapshots and their clones
3556 * -f ignored for backwards compatibility
3558 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3559 * since then and making it the active dataset. If more recent snapshots exist,
3560 * the command will complain unless the '-r' flag is given.
3562 typedef struct rollback_cbdata {
3568 boolean_t cb_recurse;
3569 } rollback_cbdata_t;
3572 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3574 rollback_cbdata_t *cbp = data;
3576 if (cbp->cb_first && cbp->cb_recurse) {
3577 (void) fprintf(stderr, gettext("cannot rollback to "
3578 "'%s': clones of previous snapshots exist\n"),
3580 (void) fprintf(stderr, gettext("use '-R' to "
3581 "force deletion of the following clones and "
3587 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3595 * Report any snapshots more recent than the one specified. Used when '-r' is
3596 * not specified. We reuse this same callback for the snapshot dependents - if
3597 * 'cb_dependent' is set, then this is a dependent and we should report it
3598 * without checking the transaction group.
3601 rollback_check(zfs_handle_t *zhp, void *data)
3603 rollback_cbdata_t *cbp = data;
3605 if (cbp->cb_doclones) {
3610 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3611 if (cbp->cb_first && !cbp->cb_recurse) {
3612 (void) fprintf(stderr, gettext("cannot "
3613 "rollback to '%s': more recent snapshots "
3614 "or bookmarks exist\n"),
3616 (void) fprintf(stderr, gettext("use '-r' to "
3617 "force deletion of the following "
3618 "snapshots and bookmarks:\n"));
3623 if (cbp->cb_recurse) {
3624 if (zfs_iter_dependents(zhp, B_TRUE,
3625 rollback_check_dependent, cbp) != 0) {
3630 (void) fprintf(stderr, "%s\n",
3639 zfs_do_rollback(int argc, char **argv)
3643 boolean_t force = B_FALSE;
3644 rollback_cbdata_t cb = { 0 };
3645 zfs_handle_t *zhp, *snap;
3646 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3650 while ((c = getopt(argc, argv, "rRf")) != -1) {
3663 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3672 /* check number of arguments */
3674 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3678 (void) fprintf(stderr, gettext("too many arguments\n"));
3682 /* open the snapshot */
3683 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3686 /* open the parent dataset */
3687 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3688 verify((delim = strrchr(parentname, '@')) != NULL);
3690 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3696 * Check for more recent snapshots and/or clones based on the presence
3699 cb.cb_target = argv[0];
3700 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3701 cb.cb_first = B_TRUE;
3703 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3705 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3708 if ((ret = cb.cb_error) != 0)
3712 * Rollback parent to the given snapshot.
3714 ret = zfs_rollback(zhp, snap, force);
3727 * zfs set property=value ... { fs | snap | vol } ...
3729 * Sets the given properties for all datasets specified on the command line.
3733 set_callback(zfs_handle_t *zhp, void *data)
3735 nvlist_t *props = data;
3737 if (zfs_prop_set_list(zhp, props) != 0) {
3738 switch (libzfs_errno(g_zfs)) {
3739 case EZFS_MOUNTFAILED:
3740 (void) fprintf(stderr, gettext("property may be set "
3741 "but unable to remount filesystem\n"));
3743 case EZFS_SHARENFSFAILED:
3744 (void) fprintf(stderr, gettext("property may be set "
3745 "but unable to reshare filesystem\n"));
3754 zfs_do_set(int argc, char **argv)
3756 nvlist_t *props = NULL;
3757 int ds_start = -1; /* argv idx of first dataset arg */
3761 /* check for options */
3762 if (argc > 1 && argv[1][0] == '-') {
3763 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3768 /* check number of arguments */
3770 (void) fprintf(stderr, gettext("missing arguments\n"));
3774 if (strchr(argv[1], '=') == NULL) {
3775 (void) fprintf(stderr, gettext("missing property=value "
3778 (void) fprintf(stderr, gettext("missing dataset "
3784 /* validate argument order: prop=val args followed by dataset args */
3785 for (i = 1; i < argc; i++) {
3786 if (strchr(argv[i], '=') != NULL) {
3788 /* out-of-order prop=val argument */
3789 (void) fprintf(stderr, gettext("invalid "
3790 "argument order\n"));
3793 } else if (ds_start < 0) {
3798 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3802 /* Populate a list of property settings */
3803 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3805 for (i = 1; i < ds_start; i++) {
3806 if (!parseprop(props, argv[i])) {
3812 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3813 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3820 typedef struct snap_cbdata {
3822 boolean_t sd_recursive;
3823 const char *sd_snapname;
3827 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3829 snap_cbdata_t *sd = arg;
3834 if (sd->sd_recursive &&
3835 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3840 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3843 fnvlist_add_boolean(sd->sd_nvl, name);
3846 if (sd->sd_recursive)
3847 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3853 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3855 * Creates a snapshot with the given name. While functionally equivalent to
3856 * 'zfs create', it is a separate command to differentiate intent.
3859 zfs_do_snapshot(int argc, char **argv)
3864 snap_cbdata_t sd = { 0 };
3865 boolean_t multiple_snaps = B_FALSE;
3867 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3869 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3873 while ((c = getopt(argc, argv, "ro:")) != -1) {
3876 if (!parseprop(props, optarg)) {
3877 nvlist_free(sd.sd_nvl);
3883 sd.sd_recursive = B_TRUE;
3884 multiple_snaps = B_TRUE;
3887 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3896 /* check number of arguments */
3898 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3903 multiple_snaps = B_TRUE;
3904 for (; argc > 0; argc--, argv++) {
3908 atp = strchr(argv[0], '@');
3912 sd.sd_snapname = atp + 1;
3913 zhp = zfs_open(g_zfs, argv[0],
3914 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3917 if (zfs_snapshot_cb(zhp, &sd) != 0)
3921 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3922 nvlist_free(sd.sd_nvl);
3924 if (ret != 0 && multiple_snaps)
3925 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3929 nvlist_free(sd.sd_nvl);
3936 * Send a backup stream to stdout.
3939 zfs_do_send(int argc, char **argv)
3941 char *fromname = NULL;
3942 char *toname = NULL;
3943 char *resume_token = NULL;
3946 sendflags_t flags = { 0 };
3948 nvlist_t *dbgnv = NULL;
3949 boolean_t extraverbose = B_FALSE;
3951 struct option long_options[] = {
3952 {"replicate", no_argument, NULL, 'R'},
3953 {"props", no_argument, NULL, 'p'},
3954 {"parsable", no_argument, NULL, 'P'},
3955 {"dedup", no_argument, NULL, 'D'},
3956 {"verbose", no_argument, NULL, 'v'},
3957 {"dryrun", no_argument, NULL, 'n'},
3958 {"large-block", no_argument, NULL, 'L'},
3959 {"embed", no_argument, NULL, 'e'},
3960 {"resume", required_argument, NULL, 't'},
3961 {"compressed", no_argument, NULL, 'c'},
3962 {"raw", no_argument, NULL, 'w'},
3963 {"backup", no_argument, NULL, 'b'},
3968 while ((c = getopt_long(argc, argv, ":i:I:RDpvnPLet:cwb", long_options,
3980 flags.doall = B_TRUE;
3983 flags.replicate = B_TRUE;
3986 flags.props = B_TRUE;
3989 flags.backup = B_TRUE;
3992 flags.parsable = B_TRUE;
3993 flags.verbose = B_TRUE;
3997 extraverbose = B_TRUE;
3998 flags.verbose = B_TRUE;
3999 flags.progress = B_TRUE;
4002 flags.dedup = B_TRUE;
4005 flags.dryrun = B_TRUE;
4008 flags.largeblock = B_TRUE;
4011 flags.embed_data = B_TRUE;
4014 resume_token = optarg;
4017 flags.compress = B_TRUE;
4021 flags.compress = B_TRUE;
4022 flags.embed_data = B_TRUE;
4023 flags.largeblock = B_TRUE;
4027 * If a parameter was not passed, optopt contains the
4028 * value that would normally lead us into the
4029 * appropriate case statement. If it's > 256, then this
4030 * must be a longopt and we should look at argv to get
4031 * the string. Otherwise it's just the character, so we
4032 * should use it directly.
4034 if (optopt <= UINT8_MAX) {
4035 (void) fprintf(stderr,
4036 gettext("missing argument for '%c' "
4037 "option\n"), optopt);
4039 (void) fprintf(stderr,
4040 gettext("missing argument for '%s' "
4041 "option\n"), argv[optind - 1]);
4049 * If an invalid flag was passed, optopt contains the
4050 * character if it was a short flag, or 0 if it was a
4054 (void) fprintf(stderr,
4055 gettext("invalid option '%c'\n"), optopt);
4057 (void) fprintf(stderr,
4058 gettext("invalid option '%s'\n"),
4069 if (resume_token != NULL) {
4070 if (fromname != NULL || flags.replicate || flags.props ||
4071 flags.backup || flags.dedup) {
4072 (void) fprintf(stderr,
4073 gettext("invalid flags combined with -t\n"));
4077 (void) fprintf(stderr, gettext("no additional "
4078 "arguments are permitted with -t\n"));
4083 (void) fprintf(stderr,
4084 gettext("missing snapshot argument\n"));
4088 (void) fprintf(stderr, gettext("too many arguments\n"));
4093 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
4094 (void) fprintf(stderr,
4095 gettext("Error: Stream can not be written to a terminal.\n"
4096 "You must redirect standard output.\n"));
4100 if (resume_token != NULL) {
4101 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
4106 * Special case sending a filesystem, or from a bookmark.
4108 if (strchr(argv[0], '@') == NULL ||
4109 (fromname && strchr(fromname, '#') != NULL)) {
4110 char frombuf[ZFS_MAX_DATASET_NAME_LEN];
4112 if (flags.replicate || flags.doall || flags.props ||
4113 flags.backup || flags.dedup ||
4114 (strchr(argv[0], '@') == NULL &&
4115 (flags.dryrun || flags.verbose || flags.progress))) {
4116 (void) fprintf(stderr, gettext("Error: "
4117 "Unsupported flag with filesystem or bookmark.\n"));
4121 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
4125 if (fromname != NULL &&
4126 (fromname[0] == '#' || fromname[0] == '@')) {
4128 * Incremental source name begins with # or @.
4129 * Default to same fs as target.
4131 (void) strlcpy(frombuf, argv[0], sizeof (frombuf));
4132 cp = strchr(frombuf, '@');
4135 (void) strlcat(frombuf, fromname, sizeof (frombuf));
4138 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, flags);
4143 cp = strchr(argv[0], '@');
4146 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4151 * If they specified the full path to the snapshot, chop off
4152 * everything except the short name of the snapshot, but special
4153 * case if they specify the origin.
4155 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
4156 char origin[ZFS_MAX_DATASET_NAME_LEN];
4159 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
4160 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
4162 if (strcmp(origin, fromname) == 0) {
4164 flags.fromorigin = B_TRUE;
4167 if (cp != fromname && strcmp(argv[0], fromname)) {
4168 (void) fprintf(stderr,
4169 gettext("incremental source must be "
4170 "in same filesystem\n"));
4174 if (strchr(fromname, '@') || strchr(fromname, '/')) {
4175 (void) fprintf(stderr,
4176 gettext("invalid incremental source\n"));
4182 if (flags.replicate && fromname == NULL)
4183 flags.doall = B_TRUE;
4185 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
4186 extraverbose ? &dbgnv : NULL);
4188 if (extraverbose && dbgnv != NULL) {
4190 * dump_nvlist prints to stdout, but that's been
4191 * redirected to a file. Make it print to stderr
4194 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
4195 dump_nvlist(dbgnv, 0);
4204 * Restore a backup stream from stdin.
4207 zfs_do_receive(int argc, char **argv)
4210 recvflags_t flags = { 0 };
4211 boolean_t abort_resumable = B_FALSE;
4214 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4218 while ((c = getopt(argc, argv, ":o:x:denuvFsA")) != -1) {
4221 if (!parseprop(props, optarg)) {
4227 if (!parsepropname(props, optarg)) {
4233 flags.isprefix = B_TRUE;
4236 flags.isprefix = B_TRUE;
4237 flags.istail = B_TRUE;
4240 flags.dryrun = B_TRUE;
4243 flags.nomount = B_TRUE;
4246 flags.verbose = B_TRUE;
4249 flags.resumable = B_TRUE;
4252 flags.force = B_TRUE;
4255 abort_resumable = B_TRUE;
4258 (void) fprintf(stderr, gettext("missing argument for "
4259 "'%c' option\n"), optopt);
4263 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4272 /* check number of arguments */
4274 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4278 (void) fprintf(stderr, gettext("too many arguments\n"));
4282 if (abort_resumable) {
4283 if (flags.isprefix || flags.istail || flags.dryrun ||
4284 flags.resumable || flags.nomount) {
4285 (void) fprintf(stderr, gettext("invalid option\n"));
4289 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4290 (void) snprintf(namebuf, sizeof (namebuf),
4291 "%s/%%recv", argv[0]);
4293 if (zfs_dataset_exists(g_zfs, namebuf,
4294 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4295 zfs_handle_t *zhp = zfs_open(g_zfs,
4296 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4301 err = zfs_destroy(zhp, B_FALSE);
4304 zfs_handle_t *zhp = zfs_open(g_zfs,
4305 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4308 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4309 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4310 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4311 (void) fprintf(stderr,
4312 gettext("'%s' does not have any "
4313 "resumable receive state to abort\n"),
4319 err = zfs_destroy(zhp, B_FALSE);
4326 if (isatty(STDIN_FILENO)) {
4327 (void) fprintf(stderr,
4328 gettext("Error: Backup stream can not be read "
4329 "from a terminal.\n"
4330 "You must redirect standard input.\n"));
4334 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4341 * allow/unallow stuff
4343 /* copied from zfs/sys/dsl_deleg.h */
4344 #define ZFS_DELEG_PERM_CREATE "create"
4345 #define ZFS_DELEG_PERM_DESTROY "destroy"
4346 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4347 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4348 #define ZFS_DELEG_PERM_CLONE "clone"
4349 #define ZFS_DELEG_PERM_PROMOTE "promote"
4350 #define ZFS_DELEG_PERM_RENAME "rename"
4351 #define ZFS_DELEG_PERM_MOUNT "mount"
4352 #define ZFS_DELEG_PERM_SHARE "share"
4353 #define ZFS_DELEG_PERM_SEND "send"
4354 #define ZFS_DELEG_PERM_RECEIVE "receive"
4355 #define ZFS_DELEG_PERM_ALLOW "allow"
4356 #define ZFS_DELEG_PERM_USERPROP "userprop"
4357 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4358 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4359 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4360 #define ZFS_DELEG_PERM_USERUSED "userused"
4361 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4362 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4363 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4364 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4365 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4367 #define ZFS_DELEG_PERM_HOLD "hold"
4368 #define ZFS_DELEG_PERM_RELEASE "release"
4369 #define ZFS_DELEG_PERM_DIFF "diff"
4370 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4371 #define ZFS_DELEG_PERM_REMAP "remap"
4372 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
4373 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
4375 #define ZFS_DELEG_PERM_PROJECTUSED "projectused"
4376 #define ZFS_DELEG_PERM_PROJECTQUOTA "projectquota"
4377 #define ZFS_DELEG_PERM_PROJECTOBJUSED "projectobjused"
4378 #define ZFS_DELEG_PERM_PROJECTOBJQUOTA "projectobjquota"
4380 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4382 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4383 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4384 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4385 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4386 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4387 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4388 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4389 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4390 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4391 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4392 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4393 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4394 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4395 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4396 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4397 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4398 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4399 { ZFS_DELEG_PERM_REMAP, ZFS_DELEG_NOTE_REMAP },
4400 { ZFS_DELEG_PERM_LOAD_KEY, ZFS_DELEG_NOTE_LOAD_KEY },
4401 { ZFS_DELEG_PERM_CHANGE_KEY, ZFS_DELEG_NOTE_CHANGE_KEY },
4403 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4404 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4405 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4406 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4407 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4408 { ZFS_DELEG_PERM_USEROBJQUOTA, ZFS_DELEG_NOTE_USEROBJQUOTA },
4409 { ZFS_DELEG_PERM_USEROBJUSED, ZFS_DELEG_NOTE_USEROBJUSED },
4410 { ZFS_DELEG_PERM_GROUPOBJQUOTA, ZFS_DELEG_NOTE_GROUPOBJQUOTA },
4411 { ZFS_DELEG_PERM_GROUPOBJUSED, ZFS_DELEG_NOTE_GROUPOBJUSED },
4412 { ZFS_DELEG_PERM_PROJECTUSED, ZFS_DELEG_NOTE_PROJECTUSED },
4413 { ZFS_DELEG_PERM_PROJECTQUOTA, ZFS_DELEG_NOTE_PROJECTQUOTA },
4414 { ZFS_DELEG_PERM_PROJECTOBJUSED, ZFS_DELEG_NOTE_PROJECTOBJUSED },
4415 { ZFS_DELEG_PERM_PROJECTOBJQUOTA, ZFS_DELEG_NOTE_PROJECTOBJQUOTA },
4416 { NULL, ZFS_DELEG_NOTE_NONE }
4419 /* permission structure */
4420 typedef struct deleg_perm {
4421 zfs_deleg_who_type_t dp_who_type;
4422 const char *dp_name;
4424 boolean_t dp_descend;
4428 typedef struct deleg_perm_node {
4429 deleg_perm_t dpn_perm;
4431 uu_avl_node_t dpn_avl_node;
4432 } deleg_perm_node_t;
4434 typedef struct fs_perm fs_perm_t;
4436 /* permissions set */
4437 typedef struct who_perm {
4438 zfs_deleg_who_type_t who_type;
4439 const char *who_name; /* id */
4440 char who_ug_name[256]; /* user/group name */
4441 fs_perm_t *who_fsperm; /* uplink */
4443 uu_avl_t *who_deleg_perm_avl; /* permissions */
4447 typedef struct who_perm_node {
4448 who_perm_t who_perm;
4449 uu_avl_node_t who_avl_node;
4452 typedef struct fs_perm_set fs_perm_set_t;
4453 /* fs permissions */
4455 const char *fsp_name;
4457 uu_avl_t *fsp_sc_avl; /* sets,create */
4458 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4460 fs_perm_set_t *fsp_set; /* uplink */
4464 typedef struct fs_perm_node {
4465 fs_perm_t fspn_fsperm;
4468 uu_list_node_t fspn_list_node;
4471 /* top level structure */
4472 struct fs_perm_set {
4473 uu_list_pool_t *fsps_list_pool;
4474 uu_list_t *fsps_list; /* list of fs_perms */
4476 uu_avl_pool_t *fsps_named_set_avl_pool;
4477 uu_avl_pool_t *fsps_who_perm_avl_pool;
4478 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4481 static inline const char *
4482 deleg_perm_type(zfs_deleg_note_t note)
4488 case ZFS_DELEG_NOTE_GROUPQUOTA:
4489 case ZFS_DELEG_NOTE_GROUPUSED:
4490 case ZFS_DELEG_NOTE_USERPROP:
4491 case ZFS_DELEG_NOTE_USERQUOTA:
4492 case ZFS_DELEG_NOTE_USERUSED:
4493 case ZFS_DELEG_NOTE_USEROBJQUOTA:
4494 case ZFS_DELEG_NOTE_USEROBJUSED:
4495 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
4496 case ZFS_DELEG_NOTE_GROUPOBJUSED:
4497 case ZFS_DELEG_NOTE_PROJECTUSED:
4498 case ZFS_DELEG_NOTE_PROJECTQUOTA:
4499 case ZFS_DELEG_NOTE_PROJECTOBJUSED:
4500 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
4502 return (gettext("other"));
4504 return (gettext("subcommand"));
4509 who_type2weight(zfs_deleg_who_type_t who_type)
4513 case ZFS_DELEG_NAMED_SET_SETS:
4514 case ZFS_DELEG_NAMED_SET:
4517 case ZFS_DELEG_CREATE_SETS:
4518 case ZFS_DELEG_CREATE:
4521 case ZFS_DELEG_USER_SETS:
4522 case ZFS_DELEG_USER:
4525 case ZFS_DELEG_GROUP_SETS:
4526 case ZFS_DELEG_GROUP:
4529 case ZFS_DELEG_EVERYONE_SETS:
4530 case ZFS_DELEG_EVERYONE:
4542 who_perm_compare(const void *larg, const void *rarg, void *unused)
4544 const who_perm_node_t *l = larg;
4545 const who_perm_node_t *r = rarg;
4546 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4547 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4548 int lweight = who_type2weight(ltype);
4549 int rweight = who_type2weight(rtype);
4550 int res = lweight - rweight;
4552 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4553 ZFS_MAX_DELEG_NAME-1);
4565 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4567 const deleg_perm_node_t *l = larg;
4568 const deleg_perm_node_t *r = rarg;
4569 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4570 ZFS_MAX_DELEG_NAME-1);
4582 fs_perm_set_init(fs_perm_set_t *fspset)
4584 bzero(fspset, sizeof (fs_perm_set_t));
4586 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4587 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4588 NULL, UU_DEFAULT)) == NULL)
4590 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4591 UU_DEFAULT)) == NULL)
4594 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4595 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4596 who_perm_node_t, who_avl_node), who_perm_compare,
4597 UU_DEFAULT)) == NULL)
4600 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4601 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4602 who_perm_node_t, who_avl_node), who_perm_compare,
4603 UU_DEFAULT)) == NULL)
4606 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4607 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4608 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4613 static inline void fs_perm_fini(fs_perm_t *);
4614 static inline void who_perm_fini(who_perm_t *);
4617 fs_perm_set_fini(fs_perm_set_t *fspset)
4619 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4621 while (node != NULL) {
4622 fs_perm_node_t *next_node =
4623 uu_list_next(fspset->fsps_list, node);
4624 fs_perm_t *fsperm = &node->fspn_fsperm;
4625 fs_perm_fini(fsperm);
4626 uu_list_remove(fspset->fsps_list, node);
4631 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4632 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4633 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4637 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4640 deleg_perm->dp_who_type = type;
4641 deleg_perm->dp_name = name;
4645 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4646 zfs_deleg_who_type_t type, const char *name)
4648 uu_avl_pool_t *pool;
4649 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4651 bzero(who_perm, sizeof (who_perm_t));
4653 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4654 UU_DEFAULT)) == NULL)
4657 who_perm->who_type = type;
4658 who_perm->who_name = name;
4659 who_perm->who_fsperm = fsperm;
4663 who_perm_fini(who_perm_t *who_perm)
4665 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4667 while (node != NULL) {
4668 deleg_perm_node_t *next_node =
4669 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4671 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4676 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4680 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4682 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4683 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4685 bzero(fsperm, sizeof (fs_perm_t));
4687 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4691 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4695 fsperm->fsp_set = fspset;
4696 fsperm->fsp_name = fsname;
4700 fs_perm_fini(fs_perm_t *fsperm)
4702 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4703 while (node != NULL) {
4704 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4706 who_perm_t *who_perm = &node->who_perm;
4707 who_perm_fini(who_perm);
4708 uu_avl_remove(fsperm->fsp_sc_avl, node);
4713 node = uu_avl_first(fsperm->fsp_uge_avl);
4714 while (node != NULL) {
4715 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4717 who_perm_t *who_perm = &node->who_perm;
4718 who_perm_fini(who_perm);
4719 uu_avl_remove(fsperm->fsp_uge_avl, node);
4724 uu_avl_destroy(fsperm->fsp_sc_avl);
4725 uu_avl_destroy(fsperm->fsp_uge_avl);
4729 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4730 zfs_deleg_who_type_t who_type, const char *name, char locality)
4732 uu_avl_index_t idx = 0;
4734 deleg_perm_node_t *found_node = NULL;
4735 deleg_perm_t *deleg_perm = &node->dpn_perm;
4737 deleg_perm_init(deleg_perm, who_type, name);
4739 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4741 uu_avl_insert(avl, node, idx);
4744 deleg_perm = &node->dpn_perm;
4749 case ZFS_DELEG_LOCAL:
4750 deleg_perm->dp_local = B_TRUE;
4752 case ZFS_DELEG_DESCENDENT:
4753 deleg_perm->dp_descend = B_TRUE;
4758 assert(B_FALSE); /* invalid locality */
4763 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4765 nvpair_t *nvp = NULL;
4766 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4767 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4768 zfs_deleg_who_type_t who_type = who_perm->who_type;
4770 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4771 const char *name = nvpair_name(nvp);
4772 data_type_t type = nvpair_type(nvp);
4773 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4774 deleg_perm_node_t *node =
4775 safe_malloc(sizeof (deleg_perm_node_t));
4777 VERIFY(type == DATA_TYPE_BOOLEAN);
4779 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4780 set_deleg_perm_node(avl, node, who_type, name, locality);
4787 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4789 nvpair_t *nvp = NULL;
4790 fs_perm_set_t *fspset = fsperm->fsp_set;
4792 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4793 nvlist_t *nvl2 = NULL;
4794 const char *name = nvpair_name(nvp);
4795 uu_avl_t *avl = NULL;
4796 uu_avl_pool_t *avl_pool = NULL;
4797 zfs_deleg_who_type_t perm_type = name[0];
4798 char perm_locality = name[1];
4799 const char *perm_name = name + 3;
4800 boolean_t is_set = B_TRUE;
4801 who_perm_t *who_perm = NULL;
4803 assert('$' == name[2]);
4805 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4808 switch (perm_type) {
4809 case ZFS_DELEG_CREATE:
4810 case ZFS_DELEG_CREATE_SETS:
4811 case ZFS_DELEG_NAMED_SET:
4812 case ZFS_DELEG_NAMED_SET_SETS:
4813 avl_pool = fspset->fsps_named_set_avl_pool;
4814 avl = fsperm->fsp_sc_avl;
4816 case ZFS_DELEG_USER:
4817 case ZFS_DELEG_USER_SETS:
4818 case ZFS_DELEG_GROUP:
4819 case ZFS_DELEG_GROUP_SETS:
4820 case ZFS_DELEG_EVERYONE:
4821 case ZFS_DELEG_EVERYONE_SETS:
4822 avl_pool = fspset->fsps_who_perm_avl_pool;
4823 avl = fsperm->fsp_uge_avl;
4827 assert(!"unhandled zfs_deleg_who_type_t");
4831 who_perm_node_t *found_node = NULL;
4832 who_perm_node_t *node = safe_malloc(
4833 sizeof (who_perm_node_t));
4834 who_perm = &node->who_perm;
4835 uu_avl_index_t idx = 0;
4837 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4838 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4840 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4842 if (avl == fsperm->fsp_uge_avl) {
4844 struct passwd *p = NULL;
4845 struct group *g = NULL;
4846 const char *nice_name = NULL;
4848 switch (perm_type) {
4849 case ZFS_DELEG_USER_SETS:
4850 case ZFS_DELEG_USER:
4851 rid = atoi(perm_name);
4854 nice_name = p->pw_name;
4856 case ZFS_DELEG_GROUP_SETS:
4857 case ZFS_DELEG_GROUP:
4858 rid = atoi(perm_name);
4861 nice_name = g->gr_name;
4868 if (nice_name != NULL)
4870 node->who_perm.who_ug_name,
4874 uu_avl_insert(avl, node, idx);
4877 who_perm = &node->who_perm;
4880 VERIFY3P(who_perm, !=, NULL);
4881 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4888 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4890 nvpair_t *nvp = NULL;
4891 uu_avl_index_t idx = 0;
4893 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4894 nvlist_t *nvl2 = NULL;
4895 const char *fsname = nvpair_name(nvp);
4896 data_type_t type = nvpair_type(nvp);
4897 fs_perm_t *fsperm = NULL;
4898 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4902 fsperm = &node->fspn_fsperm;
4904 VERIFY(DATA_TYPE_NVLIST == type);
4906 uu_list_node_init(node, &node->fspn_list_node,
4907 fspset->fsps_list_pool);
4909 idx = uu_list_numnodes(fspset->fsps_list);
4910 fs_perm_init(fsperm, fspset, fsname);
4912 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4915 (void) parse_fs_perm(fsperm, nvl2);
4917 uu_list_insert(fspset->fsps_list, node, idx);
4923 static inline const char *
4924 deleg_perm_comment(zfs_deleg_note_t note)
4926 const char *str = "";
4931 case ZFS_DELEG_NOTE_ALLOW:
4932 str = gettext("Must also have the permission that is being"
4933 "\n\t\t\t\tallowed");
4935 case ZFS_DELEG_NOTE_CLONE:
4936 str = gettext("Must also have the 'create' ability and 'mount'"
4937 "\n\t\t\t\tability in the origin file system");
4939 case ZFS_DELEG_NOTE_CREATE:
4940 str = gettext("Must also have the 'mount' ability");
4942 case ZFS_DELEG_NOTE_DESTROY:
4943 str = gettext("Must also have the 'mount' ability");
4945 case ZFS_DELEG_NOTE_DIFF:
4946 str = gettext("Allows lookup of paths within a dataset;"
4947 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4948 "\n\t\t\t\tin order to use zfs diff");
4950 case ZFS_DELEG_NOTE_HOLD:
4951 str = gettext("Allows adding a user hold to a snapshot");
4953 case ZFS_DELEG_NOTE_MOUNT:
4954 str = gettext("Allows mount/umount of ZFS datasets");
4956 case ZFS_DELEG_NOTE_PROMOTE:
4957 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4958 " 'promote' ability in the origin file system");
4960 case ZFS_DELEG_NOTE_RECEIVE:
4961 str = gettext("Must also have the 'mount' and 'create'"
4964 case ZFS_DELEG_NOTE_RELEASE:
4965 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4966 "might destroy the snapshot");
4968 case ZFS_DELEG_NOTE_RENAME:
4969 str = gettext("Must also have the 'mount' and 'create'"
4970 "\n\t\t\t\tability in the new parent");
4972 case ZFS_DELEG_NOTE_ROLLBACK:
4975 case ZFS_DELEG_NOTE_SEND:
4978 case ZFS_DELEG_NOTE_SHARE:
4979 str = gettext("Allows sharing file systems over NFS or SMB"
4980 "\n\t\t\t\tprotocols");
4982 case ZFS_DELEG_NOTE_SNAPSHOT:
4985 case ZFS_DELEG_NOTE_LOAD_KEY:
4986 str = gettext("Allows loading or unloading an encryption key");
4988 case ZFS_DELEG_NOTE_CHANGE_KEY:
4989 str = gettext("Allows changing or adding an encryption key");
4992 * case ZFS_DELEG_NOTE_VSCAN:
4993 * str = gettext("");
4997 case ZFS_DELEG_NOTE_GROUPQUOTA:
4998 str = gettext("Allows accessing any groupquota@... property");
5000 case ZFS_DELEG_NOTE_GROUPUSED:
5001 str = gettext("Allows reading any groupused@... property");
5003 case ZFS_DELEG_NOTE_USERPROP:
5004 str = gettext("Allows changing any user property");
5006 case ZFS_DELEG_NOTE_USERQUOTA:
5007 str = gettext("Allows accessing any userquota@... property");
5009 case ZFS_DELEG_NOTE_USERUSED:
5010 str = gettext("Allows reading any userused@... property");
5012 case ZFS_DELEG_NOTE_USEROBJQUOTA:
5013 str = gettext("Allows accessing any userobjquota@... property");
5015 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
5016 str = gettext("Allows accessing any \n\t\t\t\t"
5017 "groupobjquota@... property");
5019 case ZFS_DELEG_NOTE_GROUPOBJUSED:
5020 str = gettext("Allows reading any groupobjused@... property");
5022 case ZFS_DELEG_NOTE_USEROBJUSED:
5023 str = gettext("Allows reading any userobjused@... property");
5025 case ZFS_DELEG_NOTE_PROJECTQUOTA:
5026 str = gettext("Allows accessing any projectquota@... property");
5028 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
5029 str = gettext("Allows accessing any \n\t\t\t\t"
5030 "projectobjquota@... property");
5032 case ZFS_DELEG_NOTE_PROJECTUSED:
5033 str = gettext("Allows reading any projectused@... property");
5035 case ZFS_DELEG_NOTE_PROJECTOBJUSED:
5036 str = gettext("Allows accessing any \n\t\t\t\t"
5037 "projectobjused@... property");
5055 boolean_t recursive; /* unallow only */
5056 boolean_t prt_usage;
5058 boolean_t prt_perms;
5061 const char *dataset;
5065 prop_cmp(const void *a, const void *b)
5067 const char *str1 = *(const char **)a;
5068 const char *str2 = *(const char **)b;
5069 return (strcmp(str1, str2));
5073 allow_usage(boolean_t un, boolean_t requested, const char *msg)
5075 const char *opt_desc[] = {
5076 "-h", gettext("show this help message and exit"),
5077 "-l", gettext("set permission locally"),
5078 "-d", gettext("set permission for descents"),
5079 "-u", gettext("set permission for user"),
5080 "-g", gettext("set permission for group"),
5081 "-e", gettext("set permission for everyone"),
5082 "-c", gettext("set create time permission"),
5083 "-s", gettext("define permission set"),
5085 "-r", gettext("remove permissions recursively"),
5087 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
5088 size_t allow_size = unallow_size - 2;
5089 const char *props[ZFS_NUM_PROPS];
5092 FILE *fp = requested ? stdout : stderr;
5093 zprop_desc_t *pdtbl = zfs_prop_get_table();
5094 const char *fmt = gettext("%-16s %-14s\t%s\n");
5096 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
5098 (void) fprintf(fp, gettext("Options:\n"));
5099 for (i = 0; i < (un ? unallow_size : allow_size); i += 2) {
5100 const char *opt = opt_desc[i];
5101 const char *optdsc = opt_desc[i + 1];
5102 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
5105 (void) fprintf(fp, gettext("\nThe following permissions are "
5107 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
5109 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
5110 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
5111 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
5112 const char *perm_type = deleg_perm_type(perm_note);
5113 const char *perm_comment = deleg_perm_comment(perm_note);
5114 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
5117 for (i = 0; i < ZFS_NUM_PROPS; i++) {
5118 zprop_desc_t *pd = &pdtbl[i];
5119 if (pd->pd_visible != B_TRUE)
5122 if (pd->pd_attr == PROP_READONLY)
5125 props[count++] = pd->pd_name;
5127 props[count] = NULL;
5129 qsort(props, count, sizeof (char *), prop_cmp);
5131 for (i = 0; i < count; i++)
5132 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
5135 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
5137 exit(requested ? 0 : 2);
5140 static inline const char *
5141 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
5144 if (un && argc == expected_argc - 1)
5146 else if (argc == expected_argc)
5147 *permsp = argv[argc - 2];
5149 allow_usage(un, B_FALSE,
5150 gettext("wrong number of parameters\n"));
5152 return (argv[argc - 1]);
5156 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
5158 int uge_sum = opts->user + opts->group + opts->everyone;
5159 int csuge_sum = opts->create + opts->set + uge_sum;
5160 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
5161 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
5164 allow_usage(un, B_FALSE,
5165 gettext("-u, -g, and -e are mutually exclusive\n"));
5167 if (opts->prt_usage) {
5168 if (argc == 0 && all_sum == 0)
5169 allow_usage(un, B_TRUE, NULL);
5176 allow_usage(un, B_FALSE,
5177 gettext("invalid options combined with -s\n"));
5179 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5180 if (argv[0][0] != '@')
5181 allow_usage(un, B_FALSE,
5182 gettext("invalid set name: missing '@' prefix\n"));
5183 opts->who = argv[0];
5184 } else if (opts->create) {
5185 if (ldcsuge_sum > 1)
5186 allow_usage(un, B_FALSE,
5187 gettext("invalid options combined with -c\n"));
5188 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5189 } else if (opts->everyone) {
5191 allow_usage(un, B_FALSE,
5192 gettext("invalid options combined with -e\n"));
5193 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5194 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
5196 opts->everyone = B_TRUE;
5199 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5200 } else if (argc == 1 && !un) {
5201 opts->prt_perms = B_TRUE;
5202 opts->dataset = argv[argc-1];
5204 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5205 opts->who = argv[0];
5208 if (!opts->local && !opts->descend) {
5209 opts->local = B_TRUE;
5210 opts->descend = B_TRUE;
5215 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
5216 const char *who, char *perms, nvlist_t *top_nvl)
5219 char ld[2] = { '\0', '\0' };
5220 char who_buf[MAXNAMELEN + 32];
5221 char base_type = '\0';
5222 char set_type = '\0';
5223 nvlist_t *base_nvl = NULL;
5224 nvlist_t *set_nvl = NULL;
5227 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
5229 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
5233 case ZFS_DELEG_NAMED_SET_SETS:
5234 case ZFS_DELEG_NAMED_SET:
5235 set_type = ZFS_DELEG_NAMED_SET_SETS;
5236 base_type = ZFS_DELEG_NAMED_SET;
5237 ld[0] = ZFS_DELEG_NA;
5239 case ZFS_DELEG_CREATE_SETS:
5240 case ZFS_DELEG_CREATE:
5241 set_type = ZFS_DELEG_CREATE_SETS;
5242 base_type = ZFS_DELEG_CREATE;
5243 ld[0] = ZFS_DELEG_NA;
5245 case ZFS_DELEG_USER_SETS:
5246 case ZFS_DELEG_USER:
5247 set_type = ZFS_DELEG_USER_SETS;
5248 base_type = ZFS_DELEG_USER;
5250 ld[0] = ZFS_DELEG_LOCAL;
5252 ld[1] = ZFS_DELEG_DESCENDENT;
5254 case ZFS_DELEG_GROUP_SETS:
5255 case ZFS_DELEG_GROUP:
5256 set_type = ZFS_DELEG_GROUP_SETS;
5257 base_type = ZFS_DELEG_GROUP;
5259 ld[0] = ZFS_DELEG_LOCAL;
5261 ld[1] = ZFS_DELEG_DESCENDENT;
5263 case ZFS_DELEG_EVERYONE_SETS:
5264 case ZFS_DELEG_EVERYONE:
5265 set_type = ZFS_DELEG_EVERYONE_SETS;
5266 base_type = ZFS_DELEG_EVERYONE;
5268 ld[0] = ZFS_DELEG_LOCAL;
5270 ld[1] = ZFS_DELEG_DESCENDENT;
5274 assert(set_type != '\0' && base_type != '\0');
5277 if (perms != NULL) {
5279 char *end = curr + strlen(perms);
5281 while (curr < end) {
5282 char *delim = strchr(curr, ',');
5293 (void) nvlist_add_boolean(nvl, curr);
5299 for (i = 0; i < 2; i++) {
5300 char locality = ld[i];
5304 if (!nvlist_empty(base_nvl)) {
5306 (void) snprintf(who_buf,
5307 sizeof (who_buf), "%c%c$%s",
5308 base_type, locality, who);
5310 (void) snprintf(who_buf,
5311 sizeof (who_buf), "%c%c$",
5312 base_type, locality);
5314 (void) nvlist_add_nvlist(top_nvl, who_buf,
5319 if (!nvlist_empty(set_nvl)) {
5321 (void) snprintf(who_buf,
5322 sizeof (who_buf), "%c%c$%s",
5323 set_type, locality, who);
5325 (void) snprintf(who_buf,
5326 sizeof (who_buf), "%c%c$",
5327 set_type, locality);
5329 (void) nvlist_add_nvlist(top_nvl, who_buf,
5334 for (i = 0; i < 2; i++) {
5335 char locality = ld[i];
5340 (void) snprintf(who_buf, sizeof (who_buf),
5341 "%c%c$%s", base_type, locality, who);
5343 (void) snprintf(who_buf, sizeof (who_buf),
5344 "%c%c$", base_type, locality);
5345 (void) nvlist_add_boolean(top_nvl, who_buf);
5348 (void) snprintf(who_buf, sizeof (who_buf),
5349 "%c%c$%s", set_type, locality, who);
5351 (void) snprintf(who_buf, sizeof (who_buf),
5352 "%c%c$", set_type, locality);
5353 (void) nvlist_add_boolean(top_nvl, who_buf);
5359 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5361 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5365 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5366 opts->descend, opts->who, opts->perms, *nvlp);
5367 } else if (opts->create) {
5368 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5369 opts->descend, NULL, opts->perms, *nvlp);
5370 } else if (opts->everyone) {
5371 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5372 opts->descend, NULL, opts->perms, *nvlp);
5374 char *curr = opts->who;
5375 char *end = curr + strlen(curr);
5377 while (curr < end) {
5379 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5381 char *delim = strchr(curr, ',');
5384 struct passwd *p = NULL;
5385 struct group *g = NULL;
5393 rid = (uid_t)strtol(curr, &endch, 0);
5395 who_type = ZFS_DELEG_USER;
5404 (void) snprintf(errbuf, 256, gettext(
5405 "invalid user %s"), curr);
5406 allow_usage(un, B_TRUE, errbuf);
5408 } else if (opts->group) {
5409 who_type = ZFS_DELEG_GROUP;
5418 (void) snprintf(errbuf, 256, gettext(
5419 "invalid group %s"), curr);
5420 allow_usage(un, B_TRUE, errbuf);
5423 if (*endch != '\0') {
5430 if (*endch != '\0') {
5438 who_type = ZFS_DELEG_USER;
5440 } else if (g != NULL) {
5441 who_type = ZFS_DELEG_GROUP;
5444 (void) snprintf(errbuf, 256, gettext(
5445 "invalid user/group %s"), curr);
5446 allow_usage(un, B_TRUE, errbuf);
5450 (void) sprintf(id, "%u", rid);
5453 store_allow_perm(who_type, opts->local,
5454 opts->descend, who, opts->perms, *nvlp);
5463 print_set_creat_perms(uu_avl_t *who_avl)
5465 const char *sc_title[] = {
5466 gettext("Permission sets:\n"),
5467 gettext("Create time permissions:\n"),
5470 const char **title_ptr = sc_title;
5471 who_perm_node_t *who_node = NULL;
5472 int prev_weight = -1;
5474 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5475 who_node = uu_avl_next(who_avl, who_node)) {
5476 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5477 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5478 const char *who_name = who_node->who_perm.who_name;
5479 int weight = who_type2weight(who_type);
5480 boolean_t first = B_TRUE;
5481 deleg_perm_node_t *deleg_node;
5483 if (prev_weight != weight) {
5484 (void) printf("%s", *title_ptr++);
5485 prev_weight = weight;
5488 if (who_name == NULL || strnlen(who_name, 1) == 0)
5489 (void) printf("\t");
5491 (void) printf("\t%s ", who_name);
5493 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5494 deleg_node = uu_avl_next(avl, deleg_node)) {
5497 deleg_node->dpn_perm.dp_name);
5500 (void) printf(",%s",
5501 deleg_node->dpn_perm.dp_name);
5504 (void) printf("\n");
5509 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5512 who_perm_node_t *who_node = NULL;
5513 boolean_t prt_title = B_TRUE;
5514 uu_avl_walk_t *walk;
5516 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5519 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5520 const char *who_name = who_node->who_perm.who_name;
5521 const char *nice_who_name = who_node->who_perm.who_ug_name;
5522 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5523 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5525 deleg_perm_node_t *deleg_node;
5526 boolean_t prt_who = B_TRUE;
5528 for (deleg_node = uu_avl_first(avl);
5530 deleg_node = uu_avl_next(avl, deleg_node)) {
5531 if (local != deleg_node->dpn_perm.dp_local ||
5532 descend != deleg_node->dpn_perm.dp_descend)
5536 const char *who = NULL;
5538 prt_title = B_FALSE;
5539 (void) printf("%s", title);
5543 case ZFS_DELEG_USER_SETS:
5544 case ZFS_DELEG_USER:
5545 who = gettext("user");
5547 who_name = nice_who_name;
5549 case ZFS_DELEG_GROUP_SETS:
5550 case ZFS_DELEG_GROUP:
5551 who = gettext("group");
5553 who_name = nice_who_name;
5555 case ZFS_DELEG_EVERYONE_SETS:
5556 case ZFS_DELEG_EVERYONE:
5557 who = gettext("everyone");
5562 assert(who != NULL);
5566 if (who_name == NULL)
5567 (void) printf("\t%s", who);
5569 (void) printf("\t%s %s", who, who_name);
5572 (void) printf("%c%s", delim,
5573 deleg_node->dpn_perm.dp_name);
5578 (void) printf("\n");
5581 uu_avl_walk_end(walk);
5585 print_fs_perms(fs_perm_set_t *fspset)
5587 fs_perm_node_t *node = NULL;
5588 char buf[MAXNAMELEN + 32];
5589 const char *dsname = buf;
5591 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5592 node = uu_list_next(fspset->fsps_list, node)) {
5593 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5594 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5597 (void) snprintf(buf, sizeof (buf),
5598 gettext("---- Permissions on %s "),
5599 node->fspn_fsperm.fsp_name);
5600 (void) printf("%s", dsname);
5601 left = 70 - strlen(buf);
5604 (void) printf("\n");
5606 print_set_creat_perms(sc_avl);
5607 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5608 gettext("Local permissions:\n"));
5609 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5610 gettext("Descendent permissions:\n"));
5611 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5612 gettext("Local+Descendent permissions:\n"));
5616 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5618 struct deleg_perms {
5624 set_deleg_perms(zfs_handle_t *zhp, void *data)
5626 struct deleg_perms *perms = (struct deleg_perms *)data;
5627 zfs_type_t zfs_type = zfs_get_type(zhp);
5629 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5632 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5636 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5639 nvlist_t *perm_nvl = NULL;
5640 nvlist_t *update_perm_nvl = NULL;
5643 struct allow_opts opts = { 0 };
5645 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5648 while ((c = getopt(argc, argv, optstr)) != -1) {
5651 opts.local = B_TRUE;
5654 opts.descend = B_TRUE;
5660 opts.group = B_TRUE;
5663 opts.everyone = B_TRUE;
5669 opts.create = B_TRUE;
5672 opts.recursive = B_TRUE;
5675 (void) fprintf(stderr, gettext("missing argument for "
5676 "'%c' option\n"), optopt);
5680 opts.prt_usage = B_TRUE;
5683 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5692 /* check arguments */
5693 parse_allow_args(argc, argv, un, &opts);
5695 /* try to open the dataset */
5696 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5697 ZFS_TYPE_VOLUME)) == NULL) {
5698 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5703 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5706 fs_perm_set_init(&fs_perm_set);
5707 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5708 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5713 print_fs_perms(&fs_perm_set);
5715 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5716 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5719 if (un && opts.recursive) {
5720 struct deleg_perms data = { un, update_perm_nvl };
5721 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5730 nvlist_free(perm_nvl);
5731 nvlist_free(update_perm_nvl);
5733 fs_perm_set_fini(&fs_perm_set);
5741 zfs_do_allow(int argc, char **argv)
5743 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5747 zfs_do_unallow(int argc, char **argv)
5749 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5753 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5758 boolean_t recursive = B_FALSE;
5759 const char *opts = holding ? "rt" : "r";
5763 while ((c = getopt(argc, argv, opts)) != -1) {
5769 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5778 /* check number of arguments */
5786 if (holding && tag[0] == '.') {
5787 /* tags starting with '.' are reserved for libzfs */
5788 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5792 for (i = 0; i < argc; ++i) {
5794 char parent[ZFS_MAX_DATASET_NAME_LEN];
5796 char *path = argv[i];
5798 delim = strchr(path, '@');
5799 if (delim == NULL) {
5800 (void) fprintf(stderr,
5801 gettext("'%s' is not a snapshot\n"), path);
5805 (void) strncpy(parent, path, delim - path);
5806 parent[delim - path] = '\0';
5808 zhp = zfs_open(g_zfs, parent,
5809 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5815 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5818 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5824 return (errors != 0);
5828 * zfs hold [-r] [-t] <tag> <snap> ...
5830 * -r Recursively hold
5832 * Apply a user-hold with the given tag to the list of snapshots.
5835 zfs_do_hold(int argc, char **argv)
5837 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5841 * zfs release [-r] <tag> <snap> ...
5843 * -r Recursively release
5845 * Release a user-hold with the given tag from the list of snapshots.
5848 zfs_do_release(int argc, char **argv)
5850 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5853 typedef struct holds_cbdata {
5854 boolean_t cb_recursive;
5855 const char *cb_snapname;
5857 size_t cb_max_namelen;
5858 size_t cb_max_taglen;
5861 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5862 #define DATETIME_BUF_LEN (32)
5867 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
5870 nvpair_t *nvp = NULL;
5871 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5875 for (i = 0; i < 3; i++) {
5876 col = gettext(hdr_cols[i]);
5878 (void) printf("%-*s ", i ? tagwidth : nwidth,
5881 (void) printf("%s\n", col);
5885 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5886 char *zname = nvpair_name(nvp);
5888 nvpair_t *nvp2 = NULL;
5889 (void) nvpair_value_nvlist(nvp, &nvl2);
5890 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5891 char tsbuf[DATETIME_BUF_LEN];
5892 char *tagname = nvpair_name(nvp2);
5897 (void) nvpair_value_uint64(nvp2, &val);
5899 (void) localtime_r(&time, &t);
5900 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5901 gettext(STRFTIME_FMT_STR), &t);
5904 (void) printf("%s\t%s\t%s\n", zname,
5907 (void) printf("%-*s %-*s %s\n", nwidth,
5908 zname, tagwidth, tagname, tsbuf);
5915 * Generic callback function to list a dataset or snapshot.
5918 holds_callback(zfs_handle_t *zhp, void *data)
5920 holds_cbdata_t *cbp = data;
5921 nvlist_t *top_nvl = *cbp->cb_nvlp;
5922 nvlist_t *nvl = NULL;
5923 nvpair_t *nvp = NULL;
5924 const char *zname = zfs_get_name(zhp);
5925 size_t znamelen = strlen(zname);
5927 if (cbp->cb_recursive) {
5928 const char *snapname;
5929 char *delim = strchr(zname, '@');
5933 snapname = delim + 1;
5934 if (strcmp(cbp->cb_snapname, snapname))
5938 if (zfs_get_holds(zhp, &nvl) != 0)
5941 if (znamelen > cbp->cb_max_namelen)
5942 cbp->cb_max_namelen = znamelen;
5944 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5945 const char *tag = nvpair_name(nvp);
5946 size_t taglen = strlen(tag);
5947 if (taglen > cbp->cb_max_taglen)
5948 cbp->cb_max_taglen = taglen;
5951 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5955 * zfs holds [-r] <snap> ...
5957 * -r Recursively hold
5960 zfs_do_holds(int argc, char **argv)
5965 boolean_t scripted = B_FALSE;
5966 boolean_t recursive = B_FALSE;
5967 const char *opts = "rH";
5970 int types = ZFS_TYPE_SNAPSHOT;
5971 holds_cbdata_t cb = { 0 };
5978 while ((c = getopt(argc, argv, opts)) != -1) {
5987 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5994 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
5995 flags |= ZFS_ITER_RECURSE;
6001 /* check number of arguments */
6005 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
6008 for (i = 0; i < argc; ++i) {
6009 char *snapshot = argv[i];
6011 const char *snapname;
6013 delim = strchr(snapshot, '@');
6014 if (delim == NULL) {
6015 (void) fprintf(stderr,
6016 gettext("'%s' is not a snapshot\n"), snapshot);
6020 snapname = delim + 1;
6022 snapshot[delim - snapshot] = '\0';
6024 cb.cb_recursive = recursive;
6025 cb.cb_snapname = snapname;
6029 * 1. collect holds data, set format options
6031 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
6032 holds_callback, &cb);
6038 * 2. print holds data
6040 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
6042 if (nvlist_empty(nvl))
6043 (void) fprintf(stderr, gettext("no datasets available\n"));
6047 return (0 != errors);
6050 #define CHECK_SPINNER 30
6051 #define SPINNER_TIME 3 /* seconds */
6052 #define MOUNT_TIME 5 /* seconds */
6055 get_one_dataset(zfs_handle_t *zhp, void *data)
6057 static char *spin[] = { "-", "\\", "|", "/" };
6058 static int spinval = 0;
6059 static int spincheck = 0;
6060 static time_t last_spin_time = (time_t)0;
6061 get_all_cb_t *cbp = data;
6062 zfs_type_t type = zfs_get_type(zhp);
6064 if (cbp->cb_verbose) {
6065 if (--spincheck < 0) {
6066 time_t now = time(NULL);
6067 if (last_spin_time + SPINNER_TIME < now) {
6068 update_progress(spin[spinval++ % 4]);
6069 last_spin_time = now;
6071 spincheck = CHECK_SPINNER;
6076 * Iterate over any nested datasets.
6078 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
6084 * Skip any datasets whose type does not match.
6086 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
6090 libzfs_add_handle(cbp, zhp);
6091 assert(cbp->cb_used <= cbp->cb_alloc);
6097 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
6099 get_all_cb_t cb = { 0 };
6100 cb.cb_verbose = verbose;
6101 cb.cb_getone = get_one_dataset;
6104 set_progress_header(gettext("Reading ZFS config"));
6105 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
6107 *dslist = cb.cb_handles;
6108 *count = cb.cb_used;
6111 finish_progress(gettext("done."));
6115 * Generic callback for sharing or mounting filesystems. Because the code is so
6116 * similar, we have a common function with an extra parameter to determine which
6117 * mode we are using.
6119 #define OP_SHARE 0x1
6120 #define OP_MOUNT 0x2
6123 * Share or mount a dataset.
6126 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
6127 boolean_t explicit, const char *options)
6129 char mountpoint[ZFS_MAXPROPLEN];
6130 char shareopts[ZFS_MAXPROPLEN];
6131 char smbshareopts[ZFS_MAXPROPLEN];
6132 const char *cmdname = op == OP_SHARE ? "share" : "mount";
6134 uint64_t zoned, canmount;
6135 boolean_t shared_nfs, shared_smb;
6137 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
6140 * Check to make sure we can mount/share this dataset. If we
6141 * are in the global zone and the filesystem is exported to a
6142 * local zone, or if we are in a local zone and the
6143 * filesystem is not exported, then it is an error.
6145 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
6147 if (zoned && getzoneid() == GLOBAL_ZONEID) {
6151 (void) fprintf(stderr, gettext("cannot %s '%s': "
6152 "dataset is exported to a local zone\n"), cmdname,
6156 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
6160 (void) fprintf(stderr, gettext("cannot %s '%s': "
6161 "permission denied\n"), cmdname,
6167 * Ignore any filesystems which don't apply to us. This
6168 * includes those with a legacy mountpoint, or those with
6169 * legacy share options.
6171 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6172 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
6173 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
6174 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
6175 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
6176 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
6178 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
6179 strcmp(smbshareopts, "off") == 0) {
6183 (void) fprintf(stderr, gettext("cannot share '%s': "
6184 "legacy share\n"), zfs_get_name(zhp));
6185 (void) fprintf(stderr, gettext("use share(1M) to "
6186 "share this filesystem, or set "
6187 "sharenfs property on\n"));
6192 * We cannot share or mount legacy filesystems. If the
6193 * shareopts is non-legacy but the mountpoint is legacy, we
6194 * treat it as a legacy share.
6196 if (strcmp(mountpoint, "legacy") == 0) {
6200 (void) fprintf(stderr, gettext("cannot %s '%s': "
6201 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
6202 (void) fprintf(stderr, gettext("use %s(1M) to "
6203 "%s this filesystem\n"), cmdname, cmdname);
6207 if (strcmp(mountpoint, "none") == 0) {
6211 (void) fprintf(stderr, gettext("cannot %s '%s': no "
6212 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
6217 * canmount explicit outcome
6218 * on no pass through
6219 * on yes pass through
6221 * off yes display error, return 1
6222 * noauto no return 0
6223 * noauto yes pass through
6225 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
6226 if (canmount == ZFS_CANMOUNT_OFF) {
6230 (void) fprintf(stderr, gettext("cannot %s '%s': "
6231 "'canmount' property is set to 'off'\n"), cmdname,
6234 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
6239 * If this filesystem is encrypted and does not have
6240 * a loaded key, we can not mount it.
6242 if ((flags & MS_CRYPT) == 0 &&
6243 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF &&
6244 zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
6245 ZFS_KEYSTATUS_UNAVAILABLE) {
6249 (void) fprintf(stderr, gettext("cannot %s '%s': "
6250 "encryption key not loaded\n"), cmdname, zfs_get_name(zhp));
6255 * If this filesystem is inconsistent and has a receive resume
6256 * token, we can not mount it.
6258 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
6259 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
6260 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
6264 (void) fprintf(stderr, gettext("cannot %s '%s': "
6265 "Contains partially-completed state from "
6266 "\"zfs receive -s\", which can be resumed with "
6267 "\"zfs send -t\"\n"),
6268 cmdname, zfs_get_name(zhp));
6273 * At this point, we have verified that the mountpoint and/or
6274 * shareopts are appropriate for auto management. If the
6275 * filesystem is already mounted or shared, return (failing
6276 * for explicit requests); otherwise mount or share the
6282 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
6283 shared_smb = zfs_is_shared_smb(zhp, NULL);
6285 if ((shared_nfs && shared_smb) ||
6286 (shared_nfs && strcmp(shareopts, "on") == 0 &&
6287 strcmp(smbshareopts, "off") == 0) ||
6288 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
6289 strcmp(shareopts, "off") == 0)) {
6293 (void) fprintf(stderr, gettext("cannot share "
6294 "'%s': filesystem already shared\n"),
6299 if (!zfs_is_mounted(zhp, NULL) &&
6300 zfs_mount(zhp, NULL, flags) != 0)
6303 if (protocol == NULL) {
6304 if (zfs_shareall(zhp) != 0)
6306 } else if (strcmp(protocol, "nfs") == 0) {
6307 if (zfs_share_nfs(zhp))
6309 } else if (strcmp(protocol, "smb") == 0) {
6310 if (zfs_share_smb(zhp))
6313 (void) fprintf(stderr, gettext("cannot share "
6314 "'%s': invalid share type '%s' "
6316 zfs_get_name(zhp), protocol);
6323 if (options == NULL)
6324 mnt.mnt_mntopts = "";
6326 mnt.mnt_mntopts = (char *)options;
6328 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6329 zfs_is_mounted(zhp, NULL)) {
6333 (void) fprintf(stderr, gettext("cannot mount "
6334 "'%s': filesystem already mounted\n"),
6339 if (zfs_mount(zhp, options, flags) != 0)
6348 * Reports progress in the form "(current/total)". Not thread-safe.
6351 report_mount_progress(int current, int total)
6353 static time_t last_progress_time = 0;
6354 time_t now = time(NULL);
6357 /* report 1..n instead of 0..n-1 */
6360 /* display header if we're here for the first time */
6362 set_progress_header(gettext("Mounting ZFS filesystems"));
6363 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6364 /* too soon to report again */
6368 last_progress_time = now;
6370 (void) sprintf(info, "(%d/%d)", current, total);
6372 if (current == total)
6373 finish_progress(info);
6375 update_progress(info);
6379 append_options(char *mntopts, char *newopts)
6381 int len = strlen(mntopts);
6383 /* original length plus new string to append plus 1 for the comma */
6384 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6385 (void) fprintf(stderr, gettext("the opts argument for "
6386 "'%s' option is too long (more than %d chars)\n"),
6387 "-o", MNT_LINE_MAX);
6392 mntopts[len++] = ',';
6394 (void) strcpy(&mntopts[len], newopts);
6398 share_mount(int op, int argc, char **argv)
6401 boolean_t verbose = B_FALSE;
6403 char *options = NULL;
6407 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":alvo:O" : "al"))
6420 if (*optarg == '\0') {
6421 (void) fprintf(stderr, gettext("empty mount "
6422 "options (-o) specified\n"));
6426 if (options == NULL)
6427 options = safe_malloc(MNT_LINE_MAX + 1);
6429 /* option validation is done later */
6430 append_options(options, optarg);
6433 flags |= MS_OVERLAY;
6436 (void) fprintf(stderr, gettext("missing argument for "
6437 "'%c' option\n"), optopt);
6441 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6450 /* check number of arguments */
6452 zfs_handle_t **dslist = NULL;
6453 size_t i, count = 0;
6454 char *protocol = NULL;
6456 if (op == OP_SHARE && argc > 0) {
6457 if (strcmp(argv[0], "nfs") != 0 &&
6458 strcmp(argv[0], "smb") != 0) {
6459 (void) fprintf(stderr, gettext("share type "
6460 "must be 'nfs' or 'smb'\n"));
6469 (void) fprintf(stderr, gettext("too many arguments\n"));
6473 start_progress_timer();
6474 get_all_datasets(&dslist, &count, verbose);
6477 if (options != NULL)
6482 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6484 for (i = 0; i < count; i++) {
6486 report_mount_progress(i, count);
6488 if (share_mount_one(dslist[i], op, flags, protocol,
6489 B_FALSE, options) != 0)
6491 zfs_close(dslist[i]);
6495 } else if (argc == 0) {
6496 struct mnttab entry;
6498 if ((op == OP_SHARE) || (options != NULL)) {
6499 (void) fprintf(stderr, gettext("missing filesystem "
6500 "argument (specify -a for all)\n"));
6505 * When mount is given no arguments, go through
6506 * /proc/self/mounts and display any active ZFS mounts.
6507 * We hide any snapshots, since they are controlled
6511 /* Reopen MNTTAB to prevent reading stale data from open file */
6512 if (freopen(MNTTAB, "r", mnttab_file) == NULL) {
6513 if (options != NULL)
6518 while (getmntent(mnttab_file, &entry) == 0) {
6519 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6520 strchr(entry.mnt_special, '@') != NULL)
6523 (void) printf("%-30s %s\n", entry.mnt_special,
6531 (void) fprintf(stderr,
6532 gettext("too many arguments\n"));
6536 if ((zhp = zfs_open(g_zfs, argv[0],
6537 ZFS_TYPE_FILESYSTEM)) == NULL) {
6540 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6546 if (options != NULL)
6553 * zfs mount -a [nfs]
6554 * zfs mount filesystem
6556 * Mount all filesystems, or mount the given filesystem.
6559 zfs_do_mount(int argc, char **argv)
6561 return (share_mount(OP_MOUNT, argc, argv));
6565 * zfs share -a [nfs | smb]
6566 * zfs share filesystem
6568 * Share all filesystems, or share the given filesystem.
6571 zfs_do_share(int argc, char **argv)
6573 return (share_mount(OP_SHARE, argc, argv));
6576 typedef struct unshare_unmount_node {
6577 zfs_handle_t *un_zhp;
6579 uu_avl_node_t un_avlnode;
6580 } unshare_unmount_node_t;
6584 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6586 const unshare_unmount_node_t *l = larg;
6587 const unshare_unmount_node_t *r = rarg;
6589 return (strcmp(l->un_mountp, r->un_mountp));
6593 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6594 * absolute path, find the entry /proc/self/mounts, verify that its a
6595 * ZFS filesystems, and unmount it appropriately.
6598 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6602 struct stat64 statbuf;
6603 struct extmnttab entry;
6604 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6608 * Search for the path in /proc/self/mounts. Rather than looking for the
6609 * specific path, which can be fooled by non-standard paths (i.e. ".."
6610 * or "//"), we stat() the path and search for the corresponding
6611 * (major,minor) device pair.
6613 if (stat64(path, &statbuf) != 0) {
6614 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6615 cmdname, path, strerror(errno));
6618 path_inode = statbuf.st_ino;
6621 * Search for the given (major,minor) pair in the mount table.
6624 /* Reopen MNTTAB to prevent reading stale data from open file */
6625 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6628 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6629 if (entry.mnt_major == major(statbuf.st_dev) &&
6630 entry.mnt_minor == minor(statbuf.st_dev))
6634 if (op == OP_SHARE) {
6635 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6636 "currently mounted\n"), cmdname, path);
6639 (void) fprintf(stderr, gettext("warning: %s not in"
6640 "/proc/self/mounts\n"), path);
6641 if ((ret = umount2(path, flags)) != 0)
6642 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6647 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6648 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6649 "filesystem\n"), cmdname, path);
6653 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6654 ZFS_TYPE_FILESYSTEM)) == NULL)
6658 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6659 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6660 cmdname, path, strerror(errno));
6662 } else if (statbuf.st_ino != path_inode) {
6663 (void) fprintf(stderr, gettext("cannot "
6664 "%s '%s': not a mountpoint\n"), cmdname, path);
6668 if (op == OP_SHARE) {
6669 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6670 char smbshare_prop[ZFS_MAXPROPLEN];
6672 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6673 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6674 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6675 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6677 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6678 strcmp(smbshare_prop, "off") == 0) {
6679 (void) fprintf(stderr, gettext("cannot unshare "
6680 "'%s': legacy share\n"), path);
6681 (void) fprintf(stderr, gettext("use exportfs(8) "
6682 "or smbcontrol(1) to unshare this filesystem\n"));
6683 } else if (!zfs_is_shared(zhp)) {
6684 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6685 "not currently shared\n"), path);
6687 ret = zfs_unshareall_bypath(zhp, path);
6690 char mtpt_prop[ZFS_MAXPROPLEN];
6692 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6693 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6696 ret = zfs_unmount(zhp, NULL, flags);
6697 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6698 (void) fprintf(stderr, gettext("cannot unmount "
6699 "'%s': legacy mountpoint\n"),
6701 (void) fprintf(stderr, gettext("use umount(8) "
6702 "to unmount this filesystem\n"));
6704 ret = zfs_unmountall(zhp, flags);
6715 * Generic callback for unsharing or unmounting a filesystem.
6718 unshare_unmount(int op, int argc, char **argv)
6725 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6726 char sharesmb[ZFS_MAXPROPLEN];
6729 while ((c = getopt(argc, argv, op == OP_SHARE ? ":a" : "af")) != -1) {
6738 (void) fprintf(stderr, gettext("missing argument for "
6739 "'%c' option\n"), optopt);
6743 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6754 * We could make use of zfs_for_each() to walk all datasets in
6755 * the system, but this would be very inefficient, especially
6756 * since we would have to linearly search /proc/self/mounts for
6757 * each one. Instead, do one pass through /proc/self/mounts
6758 * looking for zfs entries and call zfs_unmount() for each one.
6760 * Things get a little tricky if the administrator has created
6761 * mountpoints beneath other ZFS filesystems. In this case, we
6762 * have to unmount the deepest filesystems first. To accomplish
6763 * this, we place all the mountpoints in an AVL tree sorted by
6764 * the special type (dataset name), and walk the result in
6765 * reverse to make sure to get any snapshots first.
6767 struct mnttab entry;
6768 uu_avl_pool_t *pool;
6769 uu_avl_t *tree = NULL;
6770 unshare_unmount_node_t *node;
6772 uu_avl_walk_t *walk;
6773 char *protocol = NULL;
6775 if (op == OP_SHARE && argc > 0) {
6776 if (strcmp(argv[0], "nfs") != 0 &&
6777 strcmp(argv[0], "smb") != 0) {
6778 (void) fprintf(stderr, gettext("share type "
6779 "must be 'nfs' or 'smb'\n"));
6788 (void) fprintf(stderr, gettext("too many arguments\n"));
6792 if (((pool = uu_avl_pool_create("unmount_pool",
6793 sizeof (unshare_unmount_node_t),
6794 offsetof(unshare_unmount_node_t, un_avlnode),
6795 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6796 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6799 /* Reopen MNTTAB to prevent reading stale data from open file */
6800 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6803 while (getmntent(mnttab_file, &entry) == 0) {
6805 /* ignore non-ZFS entries */
6806 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6809 /* ignore snapshots */
6810 if (strchr(entry.mnt_special, '@') != NULL)
6813 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6814 ZFS_TYPE_FILESYSTEM)) == NULL) {
6820 * Ignore datasets that are excluded/restricted by
6823 if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
6830 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6832 sizeof (nfs_mnt_prop),
6833 NULL, NULL, 0, B_FALSE) == 0);
6834 if (strcmp(nfs_mnt_prop, "off") != 0)
6836 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6838 sizeof (nfs_mnt_prop),
6839 NULL, NULL, 0, B_FALSE) == 0);
6840 if (strcmp(nfs_mnt_prop, "off") == 0)
6844 /* Ignore legacy mounts */
6845 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6847 sizeof (nfs_mnt_prop),
6848 NULL, NULL, 0, B_FALSE) == 0);
6849 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6851 /* Ignore canmount=noauto mounts */
6852 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6853 ZFS_CANMOUNT_NOAUTO)
6859 node = safe_malloc(sizeof (unshare_unmount_node_t));
6861 node->un_mountp = safe_strdup(entry.mnt_mountp);
6863 uu_avl_node_init(node, &node->un_avlnode, pool);
6865 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6866 uu_avl_insert(tree, node, idx);
6868 zfs_close(node->un_zhp);
6869 free(node->un_mountp);
6875 * Walk the AVL tree in reverse, unmounting each filesystem and
6876 * removing it from the AVL tree in the process.
6878 if ((walk = uu_avl_walk_start(tree,
6879 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6882 while ((node = uu_avl_walk_next(walk)) != NULL) {
6883 uu_avl_remove(tree, node);
6887 if (zfs_unshareall_bytype(node->un_zhp,
6888 node->un_mountp, protocol) != 0)
6893 if (zfs_unmount(node->un_zhp,
6894 node->un_zhp->zfs_name, flags) != 0)
6899 zfs_close(node->un_zhp);
6900 free(node->un_mountp);
6904 uu_avl_walk_end(walk);
6905 uu_avl_destroy(tree);
6906 uu_avl_pool_destroy(pool);
6911 (void) fprintf(stderr,
6912 gettext("missing filesystem argument\n"));
6914 (void) fprintf(stderr,
6915 gettext("too many arguments\n"));
6920 * We have an argument, but it may be a full path or a ZFS
6921 * filesystem. Pass full paths off to unmount_path() (shared by
6922 * manual_unmount), otherwise open the filesystem and pass to
6925 if (argv[0][0] == '/')
6926 return (unshare_unmount_path(op, argv[0],
6929 if ((zhp = zfs_open(g_zfs, argv[0],
6930 ZFS_TYPE_FILESYSTEM)) == NULL)
6933 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6934 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6935 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6936 NULL, 0, B_FALSE) == 0);
6940 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6942 sizeof (nfs_mnt_prop),
6943 NULL, NULL, 0, B_FALSE) == 0);
6944 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6945 sharesmb, sizeof (sharesmb), NULL, NULL,
6948 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6949 strcmp(sharesmb, "off") == 0) {
6950 (void) fprintf(stderr, gettext("cannot "
6951 "unshare '%s': legacy share\n"),
6953 (void) fprintf(stderr, gettext("use "
6954 "unshare(1M) to unshare this "
6957 } else if (!zfs_is_shared(zhp)) {
6958 (void) fprintf(stderr, gettext("cannot "
6959 "unshare '%s': not currently "
6960 "shared\n"), zfs_get_name(zhp));
6962 } else if (zfs_unshareall(zhp) != 0) {
6968 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6969 (void) fprintf(stderr, gettext("cannot "
6970 "unmount '%s': legacy "
6971 "mountpoint\n"), zfs_get_name(zhp));
6972 (void) fprintf(stderr, gettext("use "
6973 "umount(1M) to unmount this "
6976 } else if (!zfs_is_mounted(zhp, NULL)) {
6977 (void) fprintf(stderr, gettext("cannot "
6978 "unmount '%s': not currently "
6982 } else if (zfs_unmountall(zhp, flags) != 0) {
6996 * zfs unmount filesystem
6998 * Unmount all filesystems, or a specific ZFS filesystem.
7001 zfs_do_unmount(int argc, char **argv)
7003 return (unshare_unmount(OP_MOUNT, argc, argv));
7008 * zfs unshare filesystem
7010 * Unshare all filesystems, or a specific ZFS filesystem.
7013 zfs_do_unshare(int argc, char **argv)
7015 return (unshare_unmount(OP_SHARE, argc, argv));
7019 find_command_idx(char *command, int *idx)
7023 for (i = 0; i < NCOMMAND; i++) {
7024 if (command_table[i].name == NULL)
7027 if (strcmp(command, command_table[i].name) == 0) {
7036 zfs_do_diff(int argc, char **argv)
7040 char *tosnap = NULL;
7041 char *fromsnap = NULL;
7046 while ((c = getopt(argc, argv, "FHt")) != -1) {
7049 flags |= ZFS_DIFF_CLASSIFY;
7052 flags |= ZFS_DIFF_PARSEABLE;
7055 flags |= ZFS_DIFF_TIMESTAMP;
7058 (void) fprintf(stderr,
7059 gettext("invalid option '%c'\n"), optopt);
7068 (void) fprintf(stderr,
7069 gettext("must provide at least one snapshot name\n"));
7074 (void) fprintf(stderr, gettext("too many arguments\n"));
7079 tosnap = (argc == 2) ? argv[1] : NULL;
7082 if (*fromsnap != '@')
7083 copy = strdup(fromsnap);
7085 copy = strdup(tosnap);
7089 if ((atp = strchr(copy, '@')) != NULL)
7092 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) {
7099 * Ignore SIGPIPE so that the library can give us
7100 * information on any failure
7102 (void) sigignore(SIGPIPE);
7104 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
7113 * zfs remap <filesystem | volume>
7115 * Remap the indirect blocks in the given fileystem or volume.
7118 zfs_do_remap(int argc, char **argv)
7125 while ((c = getopt(argc, argv, "")) != -1) {
7128 (void) fprintf(stderr,
7129 gettext("invalid option '%c'\n"), optopt);
7135 (void) fprintf(stderr, gettext("wrong number of arguments\n"));
7140 err = zfs_remap_indirects(g_zfs, fsname);
7146 * zfs bookmark <fs@snap> <fs#bmark>
7148 * Creates a bookmark with the given name from the given snapshot.
7151 zfs_do_bookmark(int argc, char **argv)
7153 char snapname[ZFS_MAX_DATASET_NAME_LEN];
7154 char bookname[ZFS_MAX_DATASET_NAME_LEN];
7161 while ((c = getopt(argc, argv, "")) != -1) {
7164 (void) fprintf(stderr,
7165 gettext("invalid option '%c'\n"), optopt);
7173 /* check number of arguments */
7175 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
7179 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
7183 if (strchr(argv[0], '@') == NULL) {
7184 (void) fprintf(stderr,
7185 gettext("invalid snapshot name '%s': "
7186 "must contain a '@'\n"), argv[0]);
7189 if (strchr(argv[1], '#') == NULL) {
7190 (void) fprintf(stderr,
7191 gettext("invalid bookmark name '%s': "
7192 "must contain a '#'\n"), argv[1]);
7196 if (argv[0][0] == '@') {
7198 * Snapshot name begins with @.
7199 * Default to same fs as bookmark.
7201 (void) strlcpy(snapname, argv[1], sizeof (snapname));
7202 *strchr(snapname, '#') = '\0';
7203 (void) strlcat(snapname, argv[0], sizeof (snapname));
7205 (void) strlcpy(snapname, argv[0], sizeof (snapname));
7207 if (argv[1][0] == '#') {
7209 * Bookmark name begins with #.
7210 * Default to same fs as snapshot.
7212 (void) strlcpy(bookname, argv[0], sizeof (bookname));
7213 *strchr(bookname, '@') = '\0';
7214 (void) strlcat(bookname, argv[1], sizeof (bookname));
7216 (void) strlcpy(bookname, argv[1], sizeof (bookname));
7219 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
7225 nvl = fnvlist_alloc();
7226 fnvlist_add_string(nvl, bookname, snapname);
7227 ret = lzc_bookmark(nvl, NULL);
7231 const char *err_msg;
7234 (void) snprintf(errbuf, sizeof (errbuf),
7235 dgettext(TEXT_DOMAIN,
7236 "cannot create bookmark '%s'"), bookname);
7240 err_msg = "bookmark is in a different pool";
7243 err_msg = "bookmark exists";
7246 err_msg = "invalid argument";
7249 err_msg = "bookmark feature not enabled";
7252 err_msg = "out of space";
7255 err_msg = "dataset does not exist";
7258 err_msg = "unknown error";
7261 (void) fprintf(stderr, "%s: %s\n", errbuf,
7262 dgettext(TEXT_DOMAIN, err_msg));
7273 zfs_do_channel_program(int argc, char **argv)
7276 char *progbuf, *filename, *poolname;
7277 size_t progsize, progread;
7279 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
7280 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
7281 boolean_t sync_flag = B_TRUE, json_output = B_FALSE;
7282 zpool_handle_t *zhp;
7285 while ((c = getopt(argc, argv, "nt:m:j")) != -1) {
7293 arg = strtoull(optarg, &endp, 0);
7294 if (errno != 0 || *endp != '\0') {
7295 (void) fprintf(stderr, gettext(
7297 "'%s': expected integer\n"), optarg);
7302 if (arg > ZCP_MAX_INSTRLIMIT || arg == 0) {
7303 (void) fprintf(stderr, gettext(
7304 "Invalid instruction limit: "
7311 ASSERT3U(c, ==, 'm');
7312 if (arg > ZCP_MAX_MEMLIMIT || arg == 0) {
7313 (void) fprintf(stderr, gettext(
7314 "Invalid memory limit: "
7324 sync_flag = B_FALSE;
7328 json_output = B_TRUE;
7332 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7342 (void) fprintf(stderr,
7343 gettext("invalid number of arguments\n"));
7349 if (strcmp(filename, "-") == 0) {
7351 filename = "standard input";
7352 } else if ((fd = open(filename, O_RDONLY)) < 0) {
7353 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
7354 filename, strerror(errno));
7358 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7359 (void) fprintf(stderr, gettext("cannot open pool '%s'"),
7368 * Read in the channel program, expanding the program buffer as
7373 progbuf = safe_malloc(progsize);
7375 ret = read(fd, progbuf + progread, progsize - progread);
7377 if (progread == progsize && ret > 0) {
7379 progbuf = safe_realloc(progbuf, progsize);
7387 (void) fprintf(stderr,
7388 gettext("cannot read '%s': %s\n"),
7389 filename, strerror(errno));
7392 progbuf[progread] = '\0';
7395 * Any remaining arguments are passed as arguments to the lua script as
7398 * "argv" -> [ "arg 1", ... "arg n" ],
7401 nvlist_t *argnvl = fnvlist_alloc();
7402 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);
7405 ret = lzc_channel_program(poolname, progbuf,
7406 instrlimit, memlimit, argnvl, &outnvl);
7408 ret = lzc_channel_program_nosync(poolname, progbuf,
7409 instrlimit, memlimit, argnvl, &outnvl);
7414 * On error, report the error message handed back by lua if one
7415 * exists. Otherwise, generate an appropriate error message,
7416 * falling back on strerror() for an unexpected return code.
7418 char *errstring = NULL;
7419 uint64_t instructions = 0;
7420 if (nvlist_exists(outnvl, ZCP_RET_ERROR)) {
7421 (void) nvlist_lookup_string(outnvl,
7422 ZCP_RET_ERROR, &errstring);
7423 if (errstring == NULL)
7424 errstring = strerror(ret);
7426 (void) nvlist_lookup_uint64(outnvl,
7427 ZCP_ARG_INSTRLIMIT, &instructions);
7433 "Invalid instruction or memory limit.";
7436 errstring = "Return value too large.";
7439 errstring = "Memory limit exhausted.";
7442 errstring = "Timed out.";
7445 errstring = "Permission denied. Channel "
7446 "programs must be run as root.";
7449 errstring = strerror(ret);
7452 (void) fprintf(stderr,
7453 gettext("Channel program execution failed:\n%s\n"),
7455 if (ret == ETIME && instructions != 0)
7456 (void) fprintf(stderr,
7457 gettext("%llu Lua instructions\n"),
7458 (u_longlong_t)instructions);
7461 (void) nvlist_print_json(stdout, outnvl);
7462 } else if (nvlist_empty(outnvl)) {
7463 (void) fprintf(stdout, gettext("Channel program fully "
7464 "executed and did not produce output.\n"));
7466 (void) fprintf(stdout, gettext("Channel program fully "
7467 "executed and produced output:\n"));
7468 dump_nvlist(outnvl, 4);
7473 fnvlist_free(outnvl);
7474 fnvlist_free(argnvl);
7483 typedef struct loadkey_cbdata {
7484 boolean_t cb_loadkey;
7485 boolean_t cb_recursive;
7487 char *cb_keylocation;
7488 uint64_t cb_numfailed;
7489 uint64_t cb_numattempted;
7493 load_key_callback(zfs_handle_t *zhp, void *data)
7496 boolean_t is_encroot;
7497 loadkey_cbdata_t *cb = data;
7498 uint64_t keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
7501 * If we are working recursively, we want to skip loading / unloading
7502 * keys for non-encryption roots and datasets whose keys are already
7503 * in the desired end-state.
7505 if (cb->cb_recursive) {
7506 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
7512 if ((cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_AVAILABLE) ||
7513 (!cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_UNAVAILABLE))
7517 cb->cb_numattempted++;
7520 ret = zfs_crypto_load_key(zhp, cb->cb_noop, cb->cb_keylocation);
7522 ret = zfs_crypto_unload_key(zhp);
7533 load_unload_keys(int argc, char **argv, boolean_t loadkey)
7535 int c, ret = 0, flags = 0;
7536 boolean_t do_all = B_FALSE;
7537 loadkey_cbdata_t cb = { 0 };
7539 cb.cb_loadkey = loadkey;
7541 while ((c = getopt(argc, argv, "anrL:")) != -1) {
7542 /* noop and alternate keylocations only apply to zfs load-key */
7546 cb.cb_noop = B_TRUE;
7549 cb.cb_keylocation = optarg;
7559 cb.cb_recursive = B_TRUE;
7562 flags |= ZFS_ITER_RECURSE;
7563 cb.cb_recursive = B_TRUE;
7566 (void) fprintf(stderr,
7567 gettext("invalid option '%c'\n"), optopt);
7575 if (!do_all && argc == 0) {
7576 (void) fprintf(stderr,
7577 gettext("Missing dataset argument or -a option\n"));
7581 if (do_all && argc != 0) {
7582 (void) fprintf(stderr,
7583 gettext("Cannot specify dataset with -a option\n"));
7587 if (cb.cb_recursive && cb.cb_keylocation != NULL &&
7588 strcmp(cb.cb_keylocation, "prompt") != 0) {
7589 (void) fprintf(stderr, gettext("alternate keylocation may only "
7590 "be 'prompt' with -r or -a\n"));
7594 ret = zfs_for_each(argc, argv, flags,
7595 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL, 0,
7596 load_key_callback, &cb);
7598 if (cb.cb_noop || (cb.cb_recursive && cb.cb_numattempted != 0)) {
7599 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
7600 (u_longlong_t)(cb.cb_numattempted - cb.cb_numfailed),
7601 (u_longlong_t)cb.cb_numattempted,
7602 loadkey ? (cb.cb_noop ? "verified" : "loaded") :
7606 if (cb.cb_numfailed != 0)
7613 zfs_do_load_key(int argc, char **argv)
7615 return (load_unload_keys(argc, argv, B_TRUE));
7620 zfs_do_unload_key(int argc, char **argv)
7622 return (load_unload_keys(argc, argv, B_FALSE));
7626 zfs_do_change_key(int argc, char **argv)
7630 boolean_t loadkey = B_FALSE, inheritkey = B_FALSE;
7631 zfs_handle_t *zhp = NULL;
7632 nvlist_t *props = fnvlist_alloc();
7634 while ((c = getopt(argc, argv, "lio:")) != -1) {
7640 inheritkey = B_TRUE;
7643 if (!parseprop(props, optarg)) {
7649 (void) fprintf(stderr,
7650 gettext("invalid option '%c'\n"), optopt);
7655 if (inheritkey && !nvlist_empty(props)) {
7656 (void) fprintf(stderr,
7657 gettext("Properties not allowed for inheriting\n"));
7665 (void) fprintf(stderr, gettext("Missing dataset argument\n"));
7670 (void) fprintf(stderr, gettext("Too many arguments\n"));
7674 zhp = zfs_open(g_zfs, argv[argc - 1],
7675 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
7680 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
7681 if (keystatus != ZFS_KEYSTATUS_AVAILABLE) {
7682 ret = zfs_crypto_load_key(zhp, B_FALSE, NULL);
7690 /* refresh the properties so the new keystatus is visible */
7691 zfs_refresh_properties(zhp);
7694 ret = zfs_crypto_rewrap(zhp, props, inheritkey);
7707 * 1) zfs project [-d|-r] <file|directory ...>
7708 * List project ID and inherit flag of file(s) or directories.
7709 * -d: List the directory itself, not its children.
7710 * -r: List subdirectories recursively.
7712 * 2) zfs project -C [-k] [-r] <file|directory ...>
7713 * Clear project inherit flag and/or ID on the file(s) or directories.
7714 * -k: Keep the project ID unchanged. If not specified, the project ID
7715 * will be reset as zero.
7716 * -r: Clear on subdirectories recursively.
7718 * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
7719 * Check project ID and inherit flag on the file(s) or directories,
7720 * report the outliers.
7721 * -0: Print file name followed by a NUL instead of newline.
7722 * -d: Check the directory itself, not its children.
7723 * -p: Specify the referenced ID for comparing with the target file(s)
7724 * or directories' project IDs. If not specified, the target (top)
7725 * directory's project ID will be used as the referenced one.
7726 * -r: Check subdirectories recursively.
7728 * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
7729 * Set project ID and/or inherit flag on the file(s) or directories.
7730 * -p: Set the project ID as the given id.
7731 * -r: Set on subdirectorie recursively. If not specify "-p" option,
7732 * it will use top-level directory's project ID as the given id,
7733 * then set both project ID and inherit flag on all descendants
7734 * of the top-level directory.
7735 * -s: Set project inherit flag.
7738 zfs_do_project(int argc, char **argv)
7740 zfs_project_control_t zpc = {
7741 .zpc_expected_projid = ZFS_INVALID_PROJID,
7742 .zpc_op = ZFS_PROJECT_OP_DEFAULT,
7743 .zpc_dironly = B_FALSE,
7744 .zpc_keep_projid = B_FALSE,
7745 .zpc_newline = B_TRUE,
7746 .zpc_recursive = B_FALSE,
7747 .zpc_set_flag = B_FALSE,
7754 while ((c = getopt(argc, argv, "0Ccdkp:rs")) != -1) {
7757 zpc.zpc_newline = B_FALSE;
7760 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
7761 (void) fprintf(stderr, gettext("cannot "
7762 "specify '-C' '-c' '-s' together\n"));
7766 zpc.zpc_op = ZFS_PROJECT_OP_CLEAR;
7769 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
7770 (void) fprintf(stderr, gettext("cannot "
7771 "specify '-C' '-c' '-s' together\n"));
7775 zpc.zpc_op = ZFS_PROJECT_OP_CHECK;
7778 zpc.zpc_dironly = B_TRUE;
7779 /* overwrite "-r" option */
7780 zpc.zpc_recursive = B_FALSE;
7783 zpc.zpc_keep_projid = B_TRUE;
7789 zpc.zpc_expected_projid = strtoull(optarg, &endptr, 0);
7790 if (errno != 0 || *endptr != '\0') {
7791 (void) fprintf(stderr,
7792 gettext("project ID must be less than "
7793 "%u\n"), UINT32_MAX);
7796 if (zpc.zpc_expected_projid >= UINT32_MAX) {
7797 (void) fprintf(stderr,
7798 gettext("invalid project ID\n"));
7804 zpc.zpc_recursive = B_TRUE;
7805 /* overwrite "-d" option */
7806 zpc.zpc_dironly = B_FALSE;
7809 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
7810 (void) fprintf(stderr, gettext("cannot "
7811 "specify '-C' '-c' '-s' together\n"));
7815 zpc.zpc_set_flag = B_TRUE;
7816 zpc.zpc_op = ZFS_PROJECT_OP_SET;
7819 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7825 if (zpc.zpc_op == ZFS_PROJECT_OP_DEFAULT) {
7826 if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID)
7827 zpc.zpc_op = ZFS_PROJECT_OP_SET;
7829 zpc.zpc_op = ZFS_PROJECT_OP_LIST;
7832 switch (zpc.zpc_op) {
7833 case ZFS_PROJECT_OP_LIST:
7834 if (zpc.zpc_keep_projid) {
7835 (void) fprintf(stderr,
7836 gettext("'-k' is only valid together with '-C'\n"));
7839 if (!zpc.zpc_newline) {
7840 (void) fprintf(stderr,
7841 gettext("'-0' is only valid together with '-c'\n"));
7845 case ZFS_PROJECT_OP_CHECK:
7846 if (zpc.zpc_keep_projid) {
7847 (void) fprintf(stderr,
7848 gettext("'-k' is only valid together with '-C'\n"));
7852 case ZFS_PROJECT_OP_CLEAR:
7853 if (zpc.zpc_dironly) {
7854 (void) fprintf(stderr,
7855 gettext("'-d' is useless together with '-C'\n"));
7858 if (!zpc.zpc_newline) {
7859 (void) fprintf(stderr,
7860 gettext("'-0' is only valid together with '-c'\n"));
7863 if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID) {
7864 (void) fprintf(stderr,
7865 gettext("'-p' is useless together with '-C'\n"));
7869 case ZFS_PROJECT_OP_SET:
7870 if (zpc.zpc_dironly) {
7871 (void) fprintf(stderr,
7872 gettext("'-d' is useless for set project ID and/or "
7876 if (zpc.zpc_keep_projid) {
7877 (void) fprintf(stderr,
7878 gettext("'-k' is only valid together with '-C'\n"));
7881 if (!zpc.zpc_newline) {
7882 (void) fprintf(stderr,
7883 gettext("'-0' is only valid together with '-c'\n"));
7895 (void) fprintf(stderr,
7896 gettext("missing file or directory target(s)\n"));
7900 for (int i = 0; i < argc; i++) {
7903 err = zfs_project_handle(argv[i], &zpc);
7912 main(int argc, char **argv)
7918 (void) setlocale(LC_ALL, "");
7919 (void) textdomain(TEXT_DOMAIN);
7924 * Make sure the user has specified some command.
7927 (void) fprintf(stderr, gettext("missing command\n"));
7934 * The 'umount' command is an alias for 'unmount'
7936 if (strcmp(cmdname, "umount") == 0)
7937 cmdname = "unmount";
7940 * The 'recv' command is an alias for 'receive'
7942 if (strcmp(cmdname, "recv") == 0)
7943 cmdname = "receive";
7946 * The 'snap' command is an alias for 'snapshot'
7948 if (strcmp(cmdname, "snap") == 0)
7949 cmdname = "snapshot";
7954 if ((strcmp(cmdname, "-?") == 0) ||
7955 (strcmp(cmdname, "--help") == 0))
7958 if ((g_zfs = libzfs_init()) == NULL) {
7959 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
7963 mnttab_file = g_zfs->libzfs_mnttab;
7965 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7967 libzfs_print_on_error(g_zfs, B_TRUE);
7970 * Run the appropriate command.
7972 libzfs_mnttab_cache(g_zfs, B_TRUE);
7973 if (find_command_idx(cmdname, &i) == 0) {
7974 current_command = &command_table[i];
7975 ret = command_table[i].func(argc - 1, argv + 1);
7976 } else if (strchr(cmdname, '=') != NULL) {
7977 verify(find_command_idx("set", &i) == 0);
7978 current_command = &command_table[i];
7979 ret = command_table[i].func(argc, argv);
7981 (void) fprintf(stderr, gettext("unrecognized "
7982 "command '%s'\n"), cmdname);
7987 if (ret == 0 && log_history)
7988 (void) zpool_log_history(g_zfs, history_str);
7993 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7994 * for the purposes of running ::findleaks.
7996 if (getenv("ZFS_ABORT") != NULL) {
7997 (void) printf("dumping core by request\n");