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, 2018 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) {
1193 * When performing a recursive destroy we ignore errors
1194 * so that the recursive destroy could continue
1195 * destroying past problem datasets
1197 if (cb->cb_recurse) {
1198 cb->cb_error = B_TRUE;
1210 destroy_print_cb(zfs_handle_t *zhp, void *arg)
1212 destroy_cbdata_t *cb = arg;
1213 const char *name = zfs_get_name(zhp);
1216 if (nvlist_exists(cb->cb_nvl, name)) {
1217 if (cb->cb_firstsnap == NULL)
1218 cb->cb_firstsnap = strdup(name);
1219 if (cb->cb_prevsnap != NULL)
1220 free(cb->cb_prevsnap);
1221 /* this snap continues the current range */
1222 cb->cb_prevsnap = strdup(name);
1223 if (cb->cb_firstsnap == NULL || cb->cb_prevsnap == NULL)
1225 if (cb->cb_verbose) {
1226 if (cb->cb_parsable) {
1227 (void) printf("destroy\t%s\n", name);
1228 } else if (cb->cb_dryrun) {
1229 (void) printf(gettext("would destroy %s\n"),
1232 (void) printf(gettext("will destroy %s\n"),
1236 } else if (cb->cb_firstsnap != NULL) {
1237 /* end of this range */
1239 err = lzc_snaprange_space(cb->cb_firstsnap,
1240 cb->cb_prevsnap, &used);
1241 cb->cb_snapused += used;
1242 free(cb->cb_firstsnap);
1243 cb->cb_firstsnap = NULL;
1244 free(cb->cb_prevsnap);
1245 cb->cb_prevsnap = NULL;
1252 destroy_print_snapshots(zfs_handle_t *fs_zhp, destroy_cbdata_t *cb)
1255 assert(cb->cb_firstsnap == NULL);
1256 assert(cb->cb_prevsnap == NULL);
1257 err = zfs_iter_snapshots_sorted(fs_zhp, destroy_print_cb, cb);
1258 if (cb->cb_firstsnap != NULL) {
1261 err = lzc_snaprange_space(cb->cb_firstsnap,
1262 cb->cb_prevsnap, &used);
1264 cb->cb_snapused += used;
1265 free(cb->cb_firstsnap);
1266 cb->cb_firstsnap = NULL;
1267 free(cb->cb_prevsnap);
1268 cb->cb_prevsnap = NULL;
1274 snapshot_to_nvl_cb(zfs_handle_t *zhp, void *arg)
1276 destroy_cbdata_t *cb = arg;
1279 /* Check for clones. */
1280 if (!cb->cb_doclones && !cb->cb_defer_destroy) {
1281 cb->cb_target = zhp;
1282 cb->cb_first = B_TRUE;
1283 err = zfs_iter_dependents(zhp, B_TRUE,
1284 destroy_check_dependent, cb);
1288 if (nvlist_add_boolean(cb->cb_nvl, zfs_get_name(zhp)))
1296 gather_snapshots(zfs_handle_t *zhp, void *arg)
1298 destroy_cbdata_t *cb = arg;
1301 err = zfs_iter_snapspec(zhp, cb->cb_snapspec, snapshot_to_nvl_cb, cb);
1307 if (cb->cb_verbose) {
1308 err = destroy_print_snapshots(zhp, cb);
1314 err = zfs_iter_filesystems(zhp, gather_snapshots, cb);
1322 destroy_clones(destroy_cbdata_t *cb)
1325 for (pair = nvlist_next_nvpair(cb->cb_nvl, NULL);
1327 pair = nvlist_next_nvpair(cb->cb_nvl, pair)) {
1328 zfs_handle_t *zhp = zfs_open(g_zfs, nvpair_name(pair),
1331 boolean_t defer = cb->cb_defer_destroy;
1335 * We can't defer destroy non-snapshots, so set it to
1336 * false while destroying the clones.
1338 cb->cb_defer_destroy = B_FALSE;
1339 err = zfs_iter_dependents(zhp, B_FALSE,
1340 destroy_callback, cb);
1341 cb->cb_defer_destroy = defer;
1351 zfs_do_destroy(int argc, char **argv)
1353 destroy_cbdata_t cb = { 0 };
1357 zfs_handle_t *zhp = NULL;
1359 zfs_type_t type = ZFS_TYPE_DATASET;
1362 while ((c = getopt(argc, argv, "vpndfrR")) != -1) {
1365 cb.cb_verbose = B_TRUE;
1368 cb.cb_verbose = B_TRUE;
1369 cb.cb_parsable = B_TRUE;
1372 cb.cb_dryrun = B_TRUE;
1375 cb.cb_defer_destroy = B_TRUE;
1376 type = ZFS_TYPE_SNAPSHOT;
1379 cb.cb_force = B_TRUE;
1382 cb.cb_recurse = B_TRUE;
1385 cb.cb_recurse = B_TRUE;
1386 cb.cb_doclones = B_TRUE;
1390 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1399 /* check number of arguments */
1401 (void) fprintf(stderr, gettext("missing dataset argument\n"));
1405 (void) fprintf(stderr, gettext("too many arguments\n"));
1409 at = strchr(argv[0], '@');
1410 pound = strchr(argv[0], '#');
1413 /* Build the list of snaps to destroy in cb_nvl. */
1414 cb.cb_nvl = fnvlist_alloc();
1417 zhp = zfs_open(g_zfs, argv[0],
1418 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1420 nvlist_free(cb.cb_nvl);
1424 cb.cb_snapspec = at + 1;
1425 if (gather_snapshots(zfs_handle_dup(zhp), &cb) != 0 ||
1431 if (nvlist_empty(cb.cb_nvl)) {
1432 (void) fprintf(stderr, gettext("could not find any "
1433 "snapshots to destroy; check snapshot names.\n"));
1438 if (cb.cb_verbose) {
1440 zfs_nicebytes(cb.cb_snapused, buf, sizeof (buf));
1441 if (cb.cb_parsable) {
1442 (void) printf("reclaim\t%llu\n",
1443 (u_longlong_t)cb.cb_snapused);
1444 } else if (cb.cb_dryrun) {
1445 (void) printf(gettext("would reclaim %s\n"),
1448 (void) printf(gettext("will reclaim %s\n"),
1453 if (!cb.cb_dryrun) {
1454 if (cb.cb_doclones) {
1455 cb.cb_batchedsnaps = fnvlist_alloc();
1456 err = destroy_clones(&cb);
1458 err = zfs_destroy_snaps_nvl(g_zfs,
1459 cb.cb_batchedsnaps, B_FALSE);
1467 err = zfs_destroy_snaps_nvl(g_zfs, cb.cb_nvl,
1468 cb.cb_defer_destroy);
1474 } else if (pound != NULL) {
1479 (void) fprintf(stderr,
1480 "dryrun is not supported with bookmark\n");
1484 if (cb.cb_defer_destroy) {
1485 (void) fprintf(stderr,
1486 "defer destroy is not supported with bookmark\n");
1490 if (cb.cb_recurse) {
1491 (void) fprintf(stderr,
1492 "recursive is not supported with bookmark\n");
1496 if (!zfs_bookmark_exists(argv[0])) {
1497 (void) fprintf(stderr, gettext("bookmark '%s' "
1498 "does not exist.\n"), argv[0]);
1502 nvl = fnvlist_alloc();
1503 fnvlist_add_boolean(nvl, argv[0]);
1505 err = lzc_destroy_bookmarks(nvl, NULL);
1507 (void) zfs_standard_error(g_zfs, err,
1508 "cannot destroy bookmark");
1515 /* Open the given dataset */
1516 if ((zhp = zfs_open(g_zfs, argv[0], type)) == NULL)
1522 * Perform an explicit check for pools before going any further.
1524 if (!cb.cb_recurse && strchr(zfs_get_name(zhp), '/') == NULL &&
1525 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
1526 (void) fprintf(stderr, gettext("cannot destroy '%s': "
1527 "operation does not apply to pools\n"),
1529 (void) fprintf(stderr, gettext("use 'zfs destroy -r "
1530 "%s' to destroy all datasets in the pool\n"),
1532 (void) fprintf(stderr, gettext("use 'zpool destroy %s' "
1533 "to destroy the pool itself\n"), zfs_get_name(zhp));
1539 * Check for any dependents and/or clones.
1541 cb.cb_first = B_TRUE;
1542 if (!cb.cb_doclones &&
1543 zfs_iter_dependents(zhp, B_TRUE, destroy_check_dependent,
1553 cb.cb_batchedsnaps = fnvlist_alloc();
1554 if (zfs_iter_dependents(zhp, B_FALSE, destroy_callback,
1561 * Do the real thing. The callback will close the
1562 * handle regardless of whether it succeeds or not.
1564 err = destroy_callback(zhp, &cb);
1567 err = zfs_destroy_snaps_nvl(g_zfs,
1568 cb.cb_batchedsnaps, cb.cb_defer_destroy);
1570 if (err != 0 || cb.cb_error == B_TRUE)
1575 fnvlist_free(cb.cb_batchedsnaps);
1576 fnvlist_free(cb.cb_nvl);
1583 is_recvd_column(zprop_get_cbdata_t *cbp)
1586 zfs_get_column_t col;
1588 for (i = 0; i < ZFS_GET_NCOLS &&
1589 (col = cbp->cb_columns[i]) != GET_COL_NONE; i++)
1590 if (col == GET_COL_RECVD)
1596 * zfs get [-rHp] [-o all | field[,field]...] [-s source[,source]...]
1597 * < all | property[,property]... > < fs | snap | vol > ...
1599 * -r recurse over any child datasets
1600 * -H scripted mode. Headers are stripped, and fields are separated
1601 * by tabs instead of spaces.
1602 * -o Set of fields to display. One of "name,property,value,
1603 * received,source". Default is "name,property,value,source".
1604 * "all" is an alias for all five.
1605 * -s Set of sources to allow. One of
1606 * "local,default,inherited,received,temporary,none". Default is
1608 * -p Display values in parsable (literal) format.
1610 * Prints properties for the given datasets. The user can control which
1611 * columns to display as well as which property types to allow.
1615 * Invoked to display the properties for a single dataset.
1618 get_callback(zfs_handle_t *zhp, void *data)
1620 char buf[ZFS_MAXPROPLEN];
1621 char rbuf[ZFS_MAXPROPLEN];
1622 zprop_source_t sourcetype;
1623 char source[ZFS_MAX_DATASET_NAME_LEN];
1624 zprop_get_cbdata_t *cbp = data;
1625 nvlist_t *user_props = zfs_get_user_props(zhp);
1626 zprop_list_t *pl = cbp->cb_proplist;
1630 boolean_t received = is_recvd_column(cbp);
1632 for (; pl != NULL; pl = pl->pl_next) {
1633 char *recvdval = NULL;
1635 * Skip the special fake placeholder. This will also skip over
1636 * the name property when 'all' is specified.
1638 if (pl->pl_prop == ZFS_PROP_NAME &&
1639 pl == cbp->cb_proplist)
1642 if (pl->pl_prop != ZPROP_INVAL) {
1643 if (zfs_prop_get(zhp, pl->pl_prop, buf,
1644 sizeof (buf), &sourcetype, source,
1646 cbp->cb_literal) != 0) {
1649 if (!zfs_prop_valid_for_type(pl->pl_prop,
1650 ZFS_TYPE_DATASET, B_FALSE)) {
1651 (void) fprintf(stderr,
1652 gettext("No such property '%s'\n"),
1653 zfs_prop_to_name(pl->pl_prop));
1656 sourcetype = ZPROP_SRC_NONE;
1657 (void) strlcpy(buf, "-", sizeof (buf));
1660 if (received && (zfs_prop_get_recvd(zhp,
1661 zfs_prop_to_name(pl->pl_prop), rbuf, sizeof (rbuf),
1662 cbp->cb_literal) == 0))
1665 zprop_print_one_property(zfs_get_name(zhp), cbp,
1666 zfs_prop_to_name(pl->pl_prop),
1667 buf, sourcetype, source, recvdval);
1668 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
1669 sourcetype = ZPROP_SRC_LOCAL;
1671 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
1672 buf, sizeof (buf), cbp->cb_literal) != 0) {
1673 sourcetype = ZPROP_SRC_NONE;
1674 (void) strlcpy(buf, "-", sizeof (buf));
1677 zprop_print_one_property(zfs_get_name(zhp), cbp,
1678 pl->pl_user_prop, buf, sourcetype, source, NULL);
1679 } else if (zfs_prop_written(pl->pl_user_prop)) {
1680 sourcetype = ZPROP_SRC_LOCAL;
1682 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
1683 buf, sizeof (buf), cbp->cb_literal) != 0) {
1684 sourcetype = ZPROP_SRC_NONE;
1685 (void) strlcpy(buf, "-", sizeof (buf));
1688 zprop_print_one_property(zfs_get_name(zhp), cbp,
1689 pl->pl_user_prop, buf, sourcetype, source, NULL);
1691 if (nvlist_lookup_nvlist(user_props,
1692 pl->pl_user_prop, &propval) != 0) {
1695 sourcetype = ZPROP_SRC_NONE;
1698 verify(nvlist_lookup_string(propval,
1699 ZPROP_VALUE, &strval) == 0);
1700 verify(nvlist_lookup_string(propval,
1701 ZPROP_SOURCE, &sourceval) == 0);
1703 if (strcmp(sourceval,
1704 zfs_get_name(zhp)) == 0) {
1705 sourcetype = ZPROP_SRC_LOCAL;
1706 } else if (strcmp(sourceval,
1707 ZPROP_SOURCE_VAL_RECVD) == 0) {
1708 sourcetype = ZPROP_SRC_RECEIVED;
1710 sourcetype = ZPROP_SRC_INHERITED;
1711 (void) strlcpy(source,
1712 sourceval, sizeof (source));
1716 if (received && (zfs_prop_get_recvd(zhp,
1717 pl->pl_user_prop, rbuf, sizeof (rbuf),
1718 cbp->cb_literal) == 0))
1721 zprop_print_one_property(zfs_get_name(zhp), cbp,
1722 pl->pl_user_prop, strval, sourcetype,
1731 zfs_do_get(int argc, char **argv)
1733 zprop_get_cbdata_t cb = { 0 };
1734 int i, c, flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
1735 int types = ZFS_TYPE_DATASET | ZFS_TYPE_BOOKMARK;
1736 char *value, *fields;
1739 zprop_list_t fake_name = { 0 };
1742 * Set up default columns and sources.
1744 cb.cb_sources = ZPROP_SRC_ALL;
1745 cb.cb_columns[0] = GET_COL_NAME;
1746 cb.cb_columns[1] = GET_COL_PROPERTY;
1747 cb.cb_columns[2] = GET_COL_VALUE;
1748 cb.cb_columns[3] = GET_COL_SOURCE;
1749 cb.cb_type = ZFS_TYPE_DATASET;
1752 while ((c = getopt(argc, argv, ":d:o:s:rt:Hp")) != -1) {
1755 cb.cb_literal = B_TRUE;
1758 limit = parse_depth(optarg, &flags);
1761 flags |= ZFS_ITER_RECURSE;
1764 cb.cb_scripted = B_TRUE;
1767 (void) fprintf(stderr, gettext("missing argument for "
1768 "'%c' option\n"), optopt);
1773 * Process the set of columns to display. We zero out
1774 * the structure to give us a blank slate.
1776 bzero(&cb.cb_columns, sizeof (cb.cb_columns));
1778 while (*optarg != '\0') {
1779 static char *col_subopts[] =
1780 { "name", "property", "value", "received",
1781 "source", "all", NULL };
1783 if (i == ZFS_GET_NCOLS) {
1784 (void) fprintf(stderr, gettext("too "
1785 "many fields given to -o "
1790 switch (getsubopt(&optarg, col_subopts,
1793 cb.cb_columns[i++] = GET_COL_NAME;
1796 cb.cb_columns[i++] = GET_COL_PROPERTY;
1799 cb.cb_columns[i++] = GET_COL_VALUE;
1802 cb.cb_columns[i++] = GET_COL_RECVD;
1803 flags |= ZFS_ITER_RECVD_PROPS;
1806 cb.cb_columns[i++] = GET_COL_SOURCE;
1810 (void) fprintf(stderr,
1811 gettext("\"all\" conflicts "
1812 "with specific fields "
1813 "given to -o option\n"));
1816 cb.cb_columns[0] = GET_COL_NAME;
1817 cb.cb_columns[1] = GET_COL_PROPERTY;
1818 cb.cb_columns[2] = GET_COL_VALUE;
1819 cb.cb_columns[3] = GET_COL_RECVD;
1820 cb.cb_columns[4] = GET_COL_SOURCE;
1821 flags |= ZFS_ITER_RECVD_PROPS;
1825 (void) fprintf(stderr,
1826 gettext("invalid column name "
1835 while (*optarg != '\0') {
1836 static char *source_subopts[] = {
1837 "local", "default", "inherited",
1838 "received", "temporary", "none",
1841 switch (getsubopt(&optarg, source_subopts,
1844 cb.cb_sources |= ZPROP_SRC_LOCAL;
1847 cb.cb_sources |= ZPROP_SRC_DEFAULT;
1850 cb.cb_sources |= ZPROP_SRC_INHERITED;
1853 cb.cb_sources |= ZPROP_SRC_RECEIVED;
1856 cb.cb_sources |= ZPROP_SRC_TEMPORARY;
1859 cb.cb_sources |= ZPROP_SRC_NONE;
1862 (void) fprintf(stderr,
1863 gettext("invalid source "
1872 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
1873 while (*optarg != '\0') {
1874 static char *type_subopts[] = { "filesystem",
1875 "volume", "snapshot", "bookmark",
1878 switch (getsubopt(&optarg, type_subopts,
1881 types |= ZFS_TYPE_FILESYSTEM;
1884 types |= ZFS_TYPE_VOLUME;
1887 types |= ZFS_TYPE_SNAPSHOT;
1890 types |= ZFS_TYPE_BOOKMARK;
1893 types = ZFS_TYPE_DATASET |
1898 (void) fprintf(stderr,
1899 gettext("invalid type '%s'\n"),
1907 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
1917 (void) fprintf(stderr, gettext("missing property "
1924 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
1932 * As part of zfs_expand_proplist(), we keep track of the maximum column
1933 * width for each property. For the 'NAME' (and 'SOURCE') columns, we
1934 * need to know the maximum name length. However, the user likely did
1935 * not specify 'name' as one of the properties to fetch, so we need to
1936 * make sure we always include at least this property for
1937 * print_get_headers() to work properly.
1939 if (cb.cb_proplist != NULL) {
1940 fake_name.pl_prop = ZFS_PROP_NAME;
1941 fake_name.pl_width = strlen(gettext("NAME"));
1942 fake_name.pl_next = cb.cb_proplist;
1943 cb.cb_proplist = &fake_name;
1946 cb.cb_first = B_TRUE;
1948 /* run for each object */
1949 ret = zfs_for_each(argc, argv, flags, types, NULL,
1950 &cb.cb_proplist, limit, get_callback, &cb);
1952 if (cb.cb_proplist == &fake_name)
1953 zprop_free_list(fake_name.pl_next);
1955 zprop_free_list(cb.cb_proplist);
1961 * inherit [-rS] <property> <fs|vol> ...
1963 * -r Recurse over all children
1964 * -S Revert to received value, if any
1966 * For each dataset specified on the command line, inherit the given property
1967 * from its parent. Inheriting a property at the pool level will cause it to
1968 * use the default value. The '-r' flag will recurse over all children, and is
1969 * useful for setting a property on a hierarchy-wide basis, regardless of any
1970 * local modifications for each dataset.
1973 typedef struct inherit_cbdata {
1974 const char *cb_propname;
1975 boolean_t cb_received;
1979 inherit_recurse_cb(zfs_handle_t *zhp, void *data)
1981 inherit_cbdata_t *cb = data;
1982 zfs_prop_t prop = zfs_name_to_prop(cb->cb_propname);
1985 * If we're doing it recursively, then ignore properties that
1986 * are not valid for this type of dataset.
1988 if (prop != ZPROP_INVAL &&
1989 !zfs_prop_valid_for_type(prop, zfs_get_type(zhp), B_FALSE))
1992 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
1996 inherit_cb(zfs_handle_t *zhp, void *data)
1998 inherit_cbdata_t *cb = data;
2000 return (zfs_prop_inherit(zhp, cb->cb_propname, cb->cb_received) != 0);
2004 zfs_do_inherit(int argc, char **argv)
2008 inherit_cbdata_t cb = { 0 };
2012 boolean_t received = B_FALSE;
2015 while ((c = getopt(argc, argv, "rS")) != -1) {
2018 flags |= ZFS_ITER_RECURSE;
2025 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2034 /* check number of arguments */
2036 (void) fprintf(stderr, gettext("missing property argument\n"));
2040 (void) fprintf(stderr, gettext("missing dataset argument\n"));
2048 if ((prop = zfs_name_to_prop(propname)) != ZPROP_INVAL) {
2049 if (zfs_prop_readonly(prop)) {
2050 (void) fprintf(stderr, gettext(
2051 "%s property is read-only\n"),
2055 if (!zfs_prop_inheritable(prop) && !received) {
2056 (void) fprintf(stderr, gettext("'%s' property cannot "
2057 "be inherited\n"), propname);
2058 if (prop == ZFS_PROP_QUOTA ||
2059 prop == ZFS_PROP_RESERVATION ||
2060 prop == ZFS_PROP_REFQUOTA ||
2061 prop == ZFS_PROP_REFRESERVATION) {
2062 (void) fprintf(stderr, gettext("use 'zfs set "
2063 "%s=none' to clear\n"), propname);
2064 (void) fprintf(stderr, gettext("use 'zfs "
2065 "inherit -S %s' to revert to received "
2066 "value\n"), propname);
2070 if (received && (prop == ZFS_PROP_VOLSIZE ||
2071 prop == ZFS_PROP_VERSION)) {
2072 (void) fprintf(stderr, gettext("'%s' property cannot "
2073 "be reverted to a received value\n"), propname);
2076 } else if (!zfs_prop_user(propname)) {
2077 (void) fprintf(stderr, gettext("invalid property '%s'\n"),
2082 cb.cb_propname = propname;
2083 cb.cb_received = received;
2085 if (flags & ZFS_ITER_RECURSE) {
2086 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2087 NULL, NULL, 0, inherit_recurse_cb, &cb);
2089 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
2090 NULL, NULL, 0, inherit_cb, &cb);
2096 typedef struct upgrade_cbdata {
2097 uint64_t cb_numupgraded;
2098 uint64_t cb_numsamegraded;
2099 uint64_t cb_numfailed;
2100 uint64_t cb_version;
2102 boolean_t cb_foundone;
2103 char cb_lastfs[ZFS_MAX_DATASET_NAME_LEN];
2107 same_pool(zfs_handle_t *zhp, const char *name)
2109 int len1 = strcspn(name, "/@");
2110 const char *zhname = zfs_get_name(zhp);
2111 int len2 = strcspn(zhname, "/@");
2115 return (strncmp(name, zhname, len1) == 0);
2119 upgrade_list_callback(zfs_handle_t *zhp, void *data)
2121 upgrade_cbdata_t *cb = data;
2122 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2124 /* list if it's old/new */
2125 if ((!cb->cb_newer && version < ZPL_VERSION) ||
2126 (cb->cb_newer && version > ZPL_VERSION)) {
2129 str = gettext("The following filesystems are "
2130 "formatted using a newer software version and\n"
2131 "cannot be accessed on the current system.\n\n");
2133 str = gettext("The following filesystems are "
2134 "out of date, and can be upgraded. After being\n"
2135 "upgraded, these filesystems (and any 'zfs send' "
2136 "streams generated from\n"
2137 "subsequent snapshots) will no longer be "
2138 "accessible by older software versions.\n\n");
2141 if (!cb->cb_foundone) {
2143 (void) printf(gettext("VER FILESYSTEM\n"));
2144 (void) printf(gettext("--- ------------\n"));
2145 cb->cb_foundone = B_TRUE;
2148 (void) printf("%2u %s\n", version, zfs_get_name(zhp));
2155 upgrade_set_callback(zfs_handle_t *zhp, void *data)
2157 upgrade_cbdata_t *cb = data;
2158 int version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2159 int needed_spa_version;
2162 if (zfs_spa_version(zhp, &spa_version) < 0)
2165 needed_spa_version = zfs_spa_version_map(cb->cb_version);
2167 if (needed_spa_version < 0)
2170 if (spa_version < needed_spa_version) {
2172 (void) printf(gettext("%s: can not be "
2173 "upgraded; the pool version needs to first "
2174 "be upgraded\nto version %d\n\n"),
2175 zfs_get_name(zhp), needed_spa_version);
2181 if (version < cb->cb_version) {
2183 (void) snprintf(verstr, sizeof (verstr),
2184 "%llu", (u_longlong_t)cb->cb_version);
2185 if (cb->cb_lastfs[0] && !same_pool(zhp, cb->cb_lastfs)) {
2187 * If they did "zfs upgrade -a", then we could
2188 * be doing ioctls to different pools. We need
2189 * to log this history once to each pool, and bypass
2190 * the normal history logging that happens in main().
2192 (void) zpool_log_history(g_zfs, history_str);
2193 log_history = B_FALSE;
2195 if (zfs_prop_set(zhp, "version", verstr) == 0)
2196 cb->cb_numupgraded++;
2199 (void) strcpy(cb->cb_lastfs, zfs_get_name(zhp));
2200 } else if (version > cb->cb_version) {
2201 /* can't downgrade */
2202 (void) printf(gettext("%s: can not be downgraded; "
2203 "it is already at version %u\n"),
2204 zfs_get_name(zhp), version);
2207 cb->cb_numsamegraded++;
2215 * zfs upgrade [-r] [-V <version>] <-a | filesystem>
2218 zfs_do_upgrade(int argc, char **argv)
2220 boolean_t all = B_FALSE;
2221 boolean_t showversions = B_FALSE;
2223 upgrade_cbdata_t cb = { 0 };
2225 int flags = ZFS_ITER_ARGS_CAN_BE_PATHS;
2228 while ((c = getopt(argc, argv, "rvV:a")) != -1) {
2231 flags |= ZFS_ITER_RECURSE;
2234 showversions = B_TRUE;
2237 if (zfs_prop_string_to_index(ZFS_PROP_VERSION,
2238 optarg, &cb.cb_version) != 0) {
2239 (void) fprintf(stderr,
2240 gettext("invalid version %s\n"), optarg);
2249 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2258 if ((!all && !argc) && ((flags & ZFS_ITER_RECURSE) | cb.cb_version))
2260 if (showversions && (flags & ZFS_ITER_RECURSE || all ||
2261 cb.cb_version || argc))
2263 if ((all || argc) && (showversions))
2269 /* Show info on available versions. */
2270 (void) printf(gettext("The following filesystem versions are "
2272 (void) printf(gettext("VER DESCRIPTION\n"));
2273 (void) printf("--- -----------------------------------------"
2274 "---------------\n");
2275 (void) printf(gettext(" 1 Initial ZFS filesystem version\n"));
2276 (void) printf(gettext(" 2 Enhanced directory entries\n"));
2277 (void) printf(gettext(" 3 Case insensitive and filesystem "
2278 "user identifier (FUID)\n"));
2279 (void) printf(gettext(" 4 userquota, groupquota "
2281 (void) printf(gettext(" 5 System attributes\n"));
2282 (void) printf(gettext("\nFor more information on a particular "
2283 "version, including supported releases,\n"));
2284 (void) printf("see the ZFS Administration Guide.\n\n");
2286 } else if (argc || all) {
2287 /* Upgrade filesystems */
2288 if (cb.cb_version == 0)
2289 cb.cb_version = ZPL_VERSION;
2290 ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
2291 NULL, NULL, 0, upgrade_set_callback, &cb);
2292 (void) printf(gettext("%llu filesystems upgraded\n"),
2293 (u_longlong_t)cb.cb_numupgraded);
2294 if (cb.cb_numsamegraded) {
2295 (void) printf(gettext("%llu filesystems already at "
2297 (u_longlong_t)cb.cb_numsamegraded);
2299 if (cb.cb_numfailed != 0)
2302 /* List old-version filesystems */
2304 (void) printf(gettext("This system is currently running "
2305 "ZFS filesystem version %llu.\n\n"), ZPL_VERSION);
2307 flags |= ZFS_ITER_RECURSE;
2308 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2309 NULL, NULL, 0, upgrade_list_callback, &cb);
2311 found = cb.cb_foundone;
2312 cb.cb_foundone = B_FALSE;
2313 cb.cb_newer = B_TRUE;
2315 ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
2316 NULL, NULL, 0, upgrade_list_callback, &cb);
2318 if (!cb.cb_foundone && !found) {
2319 (void) printf(gettext("All filesystems are "
2320 "formatted with the current version.\n"));
2328 * zfs userspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2329 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2330 * zfs groupspace [-Hinp] [-o field[,...]] [-s field [-s field]...]
2331 * [-S field [-S field]...] [-t type[,...]] filesystem | snapshot
2332 * zfs projectspace [-Hp] [-o field[,...]] [-s field [-s field]...]
2333 * [-S field [-S field]...] filesystem | snapshot
2335 * -H Scripted mode; elide headers and separate columns by tabs.
2336 * -i Translate SID to POSIX ID.
2337 * -n Print numeric ID instead of user/group name.
2338 * -o Control which fields to display.
2339 * -p Use exact (parsable) numeric output.
2340 * -s Specify sort columns, descending order.
2341 * -S Specify sort columns, ascending order.
2342 * -t Control which object types to display.
2344 * Displays space consumed by, and quotas on, each user in the specified
2345 * filesystem or snapshot.
2348 /* us_field_types, us_field_hdr and us_field_names should be kept in sync */
2349 enum us_field_types {
2357 static char *us_field_hdr[] = { "TYPE", "NAME", "USED", "QUOTA",
2358 "OBJUSED", "OBJQUOTA" };
2359 static char *us_field_names[] = { "type", "name", "used", "quota",
2360 "objused", "objquota" };
2361 #define USFIELD_LAST (sizeof (us_field_names) / sizeof (char *))
2363 #define USTYPE_PSX_GRP (1 << 0)
2364 #define USTYPE_PSX_USR (1 << 1)
2365 #define USTYPE_SMB_GRP (1 << 2)
2366 #define USTYPE_SMB_USR (1 << 3)
2367 #define USTYPE_PROJ (1 << 4)
2368 #define USTYPE_ALL \
2369 (USTYPE_PSX_GRP | USTYPE_PSX_USR | USTYPE_SMB_GRP | USTYPE_SMB_USR | \
2372 static int us_type_bits[] = {
2379 static char *us_type_names[] = { "posixgroup", "posixuser", "smbgroup",
2382 typedef struct us_node {
2384 uu_avl_node_t usn_avlnode;
2385 uu_list_node_t usn_listnode;
2388 typedef struct us_cbdata {
2390 uu_avl_pool_t *cb_avl_pool;
2392 boolean_t cb_numname;
2393 boolean_t cb_nicenum;
2394 boolean_t cb_sid2posix;
2395 zfs_userquota_prop_t cb_prop;
2396 zfs_sort_column_t *cb_sortcol;
2397 size_t cb_width[USFIELD_LAST];
2400 static boolean_t us_populated = B_FALSE;
2403 zfs_sort_column_t *si_sortcol;
2404 boolean_t si_numname;
2408 us_field_index(char *field)
2412 for (i = 0; i < USFIELD_LAST; i++) {
2413 if (strcmp(field, us_field_names[i]) == 0)
2421 us_compare(const void *larg, const void *rarg, void *unused)
2423 const us_node_t *l = larg;
2424 const us_node_t *r = rarg;
2425 us_sort_info_t *si = (us_sort_info_t *)unused;
2426 zfs_sort_column_t *sortcol = si->si_sortcol;
2427 boolean_t numname = si->si_numname;
2428 nvlist_t *lnvl = l->usn_nvl;
2429 nvlist_t *rnvl = r->usn_nvl;
2433 for (; sortcol != NULL; sortcol = sortcol->sc_next) {
2440 zfs_prop_t prop = sortcol->sc_prop;
2441 const char *propname = NULL;
2442 boolean_t reverse = sortcol->sc_reverse;
2447 (void) nvlist_lookup_uint32(lnvl, propname, &lv32);
2448 (void) nvlist_lookup_uint32(rnvl, propname, &rv32);
2450 rc = (rv32 < lv32) ? 1 : -1;
2456 (void) nvlist_lookup_uint64(lnvl, propname,
2458 (void) nvlist_lookup_uint64(rnvl, propname,
2461 rc = (rv64 < lv64) ? 1 : -1;
2463 if ((nvlist_lookup_string(lnvl, propname,
2464 &lvstr) == ENOENT) ||
2465 (nvlist_lookup_string(rnvl, propname,
2466 &rvstr) == ENOENT)) {
2469 rc = strcmp(lvstr, rvstr);
2473 case ZFS_PROP_QUOTA:
2476 if (prop == ZFS_PROP_USED)
2480 (void) nvlist_lookup_uint64(lnvl, propname, &lv64);
2481 (void) nvlist_lookup_uint64(rnvl, propname, &rv64);
2483 rc = (rv64 < lv64) ? 1 : -1;
2492 return (reverse ? 1 : -1);
2494 return (reverse ? -1 : 1);
2499 * If entries still seem to be the same, check if they are of the same
2500 * type (smbentity is added only if we are doing SID to POSIX ID
2501 * translation where we can have duplicate type/name combinations).
2503 if (nvlist_lookup_boolean_value(lnvl, "smbentity", &lvb) == 0 &&
2504 nvlist_lookup_boolean_value(rnvl, "smbentity", &rvb) == 0 &&
2506 return (lvb < rvb ? -1 : 1);
2512 zfs_prop_is_user(unsigned p)
2514 return (p == ZFS_PROP_USERUSED || p == ZFS_PROP_USERQUOTA ||
2515 p == ZFS_PROP_USEROBJUSED || p == ZFS_PROP_USEROBJQUOTA);
2519 zfs_prop_is_group(unsigned p)
2521 return (p == ZFS_PROP_GROUPUSED || p == ZFS_PROP_GROUPQUOTA ||
2522 p == ZFS_PROP_GROUPOBJUSED || p == ZFS_PROP_GROUPOBJQUOTA);
2526 zfs_prop_is_project(unsigned p)
2528 return (p == ZFS_PROP_PROJECTUSED || p == ZFS_PROP_PROJECTQUOTA ||
2529 p == ZFS_PROP_PROJECTOBJUSED || p == ZFS_PROP_PROJECTOBJQUOTA);
2532 static inline const char *
2533 us_type2str(unsigned field_type)
2535 switch (field_type) {
2536 case USTYPE_PSX_USR:
2537 return ("POSIX User");
2538 case USTYPE_PSX_GRP:
2539 return ("POSIX Group");
2540 case USTYPE_SMB_USR:
2541 return ("SMB User");
2542 case USTYPE_SMB_GRP:
2543 return ("SMB Group");
2547 return ("Undefined");
2552 userspace_cb(void *arg, const char *domain, uid_t rid, uint64_t space)
2554 us_cbdata_t *cb = (us_cbdata_t *)arg;
2555 zfs_userquota_prop_t prop = cb->cb_prop;
2560 uu_avl_pool_t *avl_pool = cb->cb_avl_pool;
2561 uu_avl_t *avl = cb->cb_avl;
2565 zfs_sort_column_t *sortcol = cb->cb_sortcol;
2567 const char *typestr;
2571 int typeidx, nameidx, sizeidx;
2572 us_sort_info_t sortinfo = { sortcol, cb->cb_numname };
2573 boolean_t smbentity = B_FALSE;
2575 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
2577 node = safe_malloc(sizeof (us_node_t));
2578 uu_avl_node_init(node, &node->usn_avlnode, avl_pool);
2579 node->usn_nvl = props;
2581 if (domain != NULL && domain[0] != '\0') {
2584 char sid[MAXNAMELEN + 32];
2588 directory_error_t e;
2592 (void) snprintf(sid, sizeof (sid), "%s-%u", domain, rid);
2594 if (prop == ZFS_PROP_GROUPUSED || prop == ZFS_PROP_GROUPQUOTA) {
2595 type = USTYPE_SMB_GRP;
2596 err = sid_to_id(sid, B_FALSE, &id);
2598 type = USTYPE_SMB_USR;
2599 err = sid_to_id(sid, B_TRUE, &id);
2604 if (!cb->cb_sid2posix) {
2605 e = directory_name_from_sid(NULL, sid, &name,
2608 directory_error_free(e);
2618 #endif /* HAVE_IDMAP */
2621 if (cb->cb_sid2posix || domain == NULL || domain[0] == '\0') {
2623 if (zfs_prop_is_group(prop)) {
2624 type = USTYPE_PSX_GRP;
2625 if (!cb->cb_numname) {
2628 if ((g = getgrgid(rid)) != NULL)
2631 } else if (zfs_prop_is_user(prop)) {
2632 type = USTYPE_PSX_USR;
2633 if (!cb->cb_numname) {
2636 if ((p = getpwuid(rid)) != NULL)
2645 * Make sure that the type/name combination is unique when doing
2646 * SID to POSIX ID translation (hence changing the type from SMB to
2649 if (cb->cb_sid2posix &&
2650 nvlist_add_boolean_value(props, "smbentity", smbentity) != 0)
2653 /* Calculate/update width of TYPE field */
2654 typestr = us_type2str(type);
2655 typelen = strlen(gettext(typestr));
2656 typeidx = us_field_index("type");
2657 if (typelen > cb->cb_width[typeidx])
2658 cb->cb_width[typeidx] = typelen;
2659 if (nvlist_add_uint32(props, "type", type) != 0)
2662 /* Calculate/update width of NAME field */
2663 if ((cb->cb_numname && cb->cb_sid2posix) || name == NULL) {
2664 if (nvlist_add_uint64(props, "name", rid) != 0)
2666 namelen = snprintf(NULL, 0, "%u", rid);
2668 if (nvlist_add_string(props, "name", name) != 0)
2670 namelen = strlen(name);
2672 nameidx = us_field_index("name");
2673 if (nameidx >= 0 && namelen > cb->cb_width[nameidx])
2674 cb->cb_width[nameidx] = namelen;
2677 * Check if this type/name combination is in the list and update it;
2678 * otherwise add new node to the list.
2680 if ((n = uu_avl_find(avl, node, &sortinfo, &idx)) == NULL) {
2681 uu_avl_insert(avl, node, idx);
2686 props = node->usn_nvl;
2689 /* Calculate/update width of USED/QUOTA fields */
2690 if (cb->cb_nicenum) {
2691 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
2692 prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
2693 prop == ZFS_PROP_PROJECTUSED ||
2694 prop == ZFS_PROP_PROJECTQUOTA) {
2695 zfs_nicebytes(space, sizebuf, sizeof (sizebuf));
2697 zfs_nicenum(space, sizebuf, sizeof (sizebuf));
2700 (void) snprintf(sizebuf, sizeof (sizebuf), "%llu",
2701 (u_longlong_t)space);
2703 sizelen = strlen(sizebuf);
2704 if (prop == ZFS_PROP_USERUSED || prop == ZFS_PROP_GROUPUSED ||
2705 prop == ZFS_PROP_PROJECTUSED) {
2707 if (!nvlist_exists(props, "quota"))
2708 (void) nvlist_add_uint64(props, "quota", 0);
2709 } else if (prop == ZFS_PROP_USERQUOTA || prop == ZFS_PROP_GROUPQUOTA ||
2710 prop == ZFS_PROP_PROJECTQUOTA) {
2712 if (!nvlist_exists(props, "used"))
2713 (void) nvlist_add_uint64(props, "used", 0);
2714 } else if (prop == ZFS_PROP_USEROBJUSED ||
2715 prop == ZFS_PROP_GROUPOBJUSED || prop == ZFS_PROP_PROJECTOBJUSED) {
2716 propname = "objused";
2717 if (!nvlist_exists(props, "objquota"))
2718 (void) nvlist_add_uint64(props, "objquota", 0);
2719 } else if (prop == ZFS_PROP_USEROBJQUOTA ||
2720 prop == ZFS_PROP_GROUPOBJQUOTA ||
2721 prop == ZFS_PROP_PROJECTOBJQUOTA) {
2722 propname = "objquota";
2723 if (!nvlist_exists(props, "objused"))
2724 (void) nvlist_add_uint64(props, "objused", 0);
2728 sizeidx = us_field_index(propname);
2729 if (sizeidx >= 0 && sizelen > cb->cb_width[sizeidx])
2730 cb->cb_width[sizeidx] = sizelen;
2732 if (nvlist_add_uint64(props, propname, space) != 0)
2739 print_us_node(boolean_t scripted, boolean_t parsable, int *fields, int types,
2740 size_t *width, us_node_t *node)
2742 nvlist_t *nvl = node->usn_nvl;
2743 char valstr[MAXNAMELEN];
2744 boolean_t first = B_TRUE;
2750 (void) nvlist_lookup_uint32(nvl, "type", &ustype);
2751 if (!(ustype & types))
2754 while ((field = fields[cfield]) != USFIELD_LAST) {
2755 nvpair_t *nvp = NULL;
2761 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2762 if (strcmp(nvpair_name(nvp),
2763 us_field_names[field]) == 0)
2767 type = nvp == NULL ? DATA_TYPE_UNKNOWN : nvpair_type(nvp);
2769 case DATA_TYPE_UINT32:
2770 (void) nvpair_value_uint32(nvp, &val32);
2772 case DATA_TYPE_UINT64:
2773 (void) nvpair_value_uint64(nvp, &val64);
2775 case DATA_TYPE_STRING:
2776 (void) nvpair_value_string(nvp, &strval);
2778 case DATA_TYPE_UNKNOWN:
2781 (void) fprintf(stderr, "invalid data type\n");
2786 if (type == DATA_TYPE_UINT32)
2787 strval = (char *)us_type2str(val32);
2790 if (type == DATA_TYPE_UINT64) {
2791 (void) sprintf(valstr, "%llu",
2792 (u_longlong_t)val64);
2798 if (type == DATA_TYPE_UINT64) {
2800 (void) sprintf(valstr, "%llu",
2801 (u_longlong_t)val64);
2803 } else if (field == USFIELD_QUOTA &&
2807 zfs_nicebytes(val64, valstr,
2813 case USFIELD_OBJUSED:
2814 case USFIELD_OBJQUOTA:
2815 if (type == DATA_TYPE_UINT64) {
2817 (void) sprintf(valstr, "%llu",
2818 (u_longlong_t)val64);
2820 } else if (field == USFIELD_OBJQUOTA &&
2824 zfs_nicenum(val64, valstr,
2834 (void) printf("\t");
2839 (void) printf("%s", strval);
2840 else if (field == USFIELD_TYPE || field == USFIELD_NAME)
2841 (void) printf("%-*s", (int)width[field], strval);
2843 (void) printf("%*s", (int)width[field], strval);
2849 (void) printf("\n");
2853 print_us(boolean_t scripted, boolean_t parsable, int *fields, int types,
2854 size_t *width, boolean_t rmnode, uu_avl_t *avl)
2862 boolean_t first = B_TRUE;
2864 while ((field = fields[cfield]) != USFIELD_LAST) {
2865 col = gettext(us_field_hdr[field]);
2866 if (field == USFIELD_TYPE || field == USFIELD_NAME) {
2867 (void) printf(first ? "%-*s" : " %-*s",
2868 (int)width[field], col);
2870 (void) printf(first ? "%*s" : " %*s",
2871 (int)width[field], col);
2876 (void) printf("\n");
2879 for (node = uu_avl_first(avl); node; node = uu_avl_next(avl, node)) {
2880 print_us_node(scripted, parsable, fields, types, width, node);
2882 nvlist_free(node->usn_nvl);
2887 zfs_do_userspace(int argc, char **argv)
2890 zfs_userquota_prop_t p;
2891 uu_avl_pool_t *avl_pool;
2893 uu_avl_walk_t *walk;
2895 char deffields[] = "type,name,used,quota,objused,objquota";
2896 char *ofield = NULL;
2897 char *tfield = NULL;
2901 boolean_t scripted = B_FALSE;
2902 boolean_t prtnum = B_FALSE;
2903 boolean_t parsable = B_FALSE;
2904 boolean_t sid2posix = B_FALSE;
2907 zfs_sort_column_t *sortcol = NULL;
2908 int types = USTYPE_PSX_USR | USTYPE_SMB_USR;
2912 uu_list_pool_t *listpool;
2914 uu_avl_index_t idx = 0;
2915 uu_list_index_t idx2 = 0;
2920 if (strcmp(argv[0], "groupspace") == 0) {
2921 /* Toggle default group types */
2922 types = USTYPE_PSX_GRP | USTYPE_SMB_GRP;
2923 } else if (strcmp(argv[0], "projectspace") == 0) {
2924 types = USTYPE_PROJ;
2928 while ((c = getopt(argc, argv, "nHpo:s:S:t:i")) != -1) {
2931 if (types == USTYPE_PROJ) {
2932 (void) fprintf(stderr,
2933 gettext("invalid option 'n'\n"));
2949 if (zfs_add_sort_column(&sortcol, optarg,
2950 c == 's' ? B_FALSE : B_TRUE) != 0) {
2951 (void) fprintf(stderr,
2952 gettext("invalid field '%s'\n"), optarg);
2957 if (types == USTYPE_PROJ) {
2958 (void) fprintf(stderr,
2959 gettext("invalid option 't'\n"));
2965 if (types == USTYPE_PROJ) {
2966 (void) fprintf(stderr,
2967 gettext("invalid option 'i'\n"));
2973 (void) fprintf(stderr, gettext("missing argument for "
2974 "'%c' option\n"), optopt);
2978 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
2988 (void) fprintf(stderr, gettext("missing dataset name\n"));
2992 (void) fprintf(stderr, gettext("too many arguments\n"));
2996 /* Use default output fields if not specified using -o */
3000 if ((delim = strchr(ofield, ',')) != NULL)
3002 if ((fields[cfield++] = us_field_index(ofield)) == -1) {
3003 (void) fprintf(stderr, gettext("invalid type '%s' "
3004 "for -o option\n"), ofield);
3009 } while (delim != NULL);
3010 fields[cfield] = USFIELD_LAST;
3012 /* Override output types (-t option) */
3013 if (tfield != NULL) {
3017 boolean_t found = B_FALSE;
3019 if ((delim = strchr(tfield, ',')) != NULL)
3021 for (i = 0; i < sizeof (us_type_bits) / sizeof (int);
3023 if (strcmp(tfield, us_type_names[i]) == 0) {
3025 types |= us_type_bits[i];
3030 (void) fprintf(stderr, gettext("invalid type "
3031 "'%s' for -t option\n"), tfield);
3036 } while (delim != NULL);
3039 if ((zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET)) == NULL)
3042 if ((avl_pool = uu_avl_pool_create("us_avl_pool", sizeof (us_node_t),
3043 offsetof(us_node_t, usn_avlnode), us_compare, UU_DEFAULT)) == NULL)
3045 if ((avl_tree = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
3048 /* Always add default sorting columns */
3049 (void) zfs_add_sort_column(&sortcol, "type", B_FALSE);
3050 (void) zfs_add_sort_column(&sortcol, "name", B_FALSE);
3052 cb.cb_sortcol = sortcol;
3053 cb.cb_numname = prtnum;
3054 cb.cb_nicenum = !parsable;
3055 cb.cb_avl_pool = avl_pool;
3056 cb.cb_avl = avl_tree;
3057 cb.cb_sid2posix = sid2posix;
3059 for (i = 0; i < USFIELD_LAST; i++)
3060 cb.cb_width[i] = strlen(gettext(us_field_hdr[i]));
3062 for (p = 0; p < ZFS_NUM_USERQUOTA_PROPS; p++) {
3063 if ((zfs_prop_is_user(p) &&
3064 !(types & (USTYPE_PSX_USR | USTYPE_SMB_USR))) ||
3065 (zfs_prop_is_group(p) &&
3066 !(types & (USTYPE_PSX_GRP | USTYPE_SMB_GRP))) ||
3067 (zfs_prop_is_project(p) && types != USTYPE_PROJ))
3071 if ((ret = zfs_userspace(zhp, p, userspace_cb, &cb)) != 0)
3076 if ((node = uu_avl_first(avl_tree)) == NULL)
3079 us_populated = B_TRUE;
3081 listpool = uu_list_pool_create("tmplist", sizeof (us_node_t),
3082 offsetof(us_node_t, usn_listnode), NULL, UU_DEFAULT);
3083 list = uu_list_create(listpool, NULL, UU_DEFAULT);
3084 uu_list_node_init(node, &node->usn_listnode, listpool);
3086 while (node != NULL) {
3088 node = uu_avl_next(avl_tree, node);
3089 uu_avl_remove(avl_tree, rmnode);
3090 if (uu_list_find(list, rmnode, NULL, &idx2) == NULL)
3091 uu_list_insert(list, rmnode, idx2);
3094 for (node = uu_list_first(list); node != NULL;
3095 node = uu_list_next(list, node)) {
3096 us_sort_info_t sortinfo = { sortcol, cb.cb_numname };
3098 if (uu_avl_find(avl_tree, node, &sortinfo, &idx) == NULL)
3099 uu_avl_insert(avl_tree, node, idx);
3102 uu_list_destroy(list);
3103 uu_list_pool_destroy(listpool);
3105 /* Print and free node nvlist memory */
3106 print_us(scripted, parsable, fields, types, cb.cb_width, B_TRUE,
3109 zfs_free_sort_columns(sortcol);
3111 /* Clean up the AVL tree */
3112 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
3115 while ((node = uu_avl_walk_next(walk)) != NULL) {
3116 uu_avl_remove(cb.cb_avl, node);
3120 uu_avl_walk_end(walk);
3121 uu_avl_destroy(avl_tree);
3122 uu_avl_pool_destroy(avl_pool);
3128 * list [-Hp][-r|-d max] [-o property[,...]] [-s property] ... [-S property]
3129 * [-t type[,...]] [filesystem|volume|snapshot] ...
3131 * -H Scripted mode; elide headers and separate columns by tabs
3132 * -p Display values in parsable (literal) format.
3133 * -r Recurse over all children
3134 * -d Limit recursion by depth.
3135 * -o Control which fields to display.
3136 * -s Specify sort columns, descending order.
3137 * -S Specify sort columns, ascending order.
3138 * -t Control which object types to display.
3140 * When given no arguments, list all filesystems in the system.
3141 * Otherwise, list the specified datasets, optionally recursing down them if
3142 * '-r' is specified.
3144 typedef struct list_cbdata {
3146 boolean_t cb_literal;
3147 boolean_t cb_scripted;
3148 zprop_list_t *cb_proplist;
3152 * Given a list of columns to display, output appropriate headers for each one.
3155 print_header(list_cbdata_t *cb)
3157 zprop_list_t *pl = cb->cb_proplist;
3158 char headerbuf[ZFS_MAXPROPLEN];
3161 boolean_t first = B_TRUE;
3162 boolean_t right_justify;
3164 for (; pl != NULL; pl = pl->pl_next) {
3171 right_justify = B_FALSE;
3172 if (pl->pl_prop != ZPROP_INVAL) {
3173 header = zfs_prop_column_name(pl->pl_prop);
3174 right_justify = zfs_prop_align_right(pl->pl_prop);
3176 for (i = 0; pl->pl_user_prop[i] != '\0'; i++)
3177 headerbuf[i] = toupper(pl->pl_user_prop[i]);
3178 headerbuf[i] = '\0';
3182 if (pl->pl_next == NULL && !right_justify)
3183 (void) printf("%s", header);
3184 else if (right_justify)
3185 (void) printf("%*s", (int)pl->pl_width, header);
3187 (void) printf("%-*s", (int)pl->pl_width, header);
3190 (void) printf("\n");
3194 * Given a dataset and a list of fields, print out all the properties according
3195 * to the described layout.
3198 print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
3200 zprop_list_t *pl = cb->cb_proplist;
3201 boolean_t first = B_TRUE;
3202 char property[ZFS_MAXPROPLEN];
3203 nvlist_t *userprops = zfs_get_user_props(zhp);
3206 boolean_t right_justify;
3208 for (; pl != NULL; pl = pl->pl_next) {
3210 if (cb->cb_scripted)
3211 (void) printf("\t");
3218 if (pl->pl_prop == ZFS_PROP_NAME) {
3219 (void) strlcpy(property, zfs_get_name(zhp),
3222 right_justify = zfs_prop_align_right(pl->pl_prop);
3223 } else if (pl->pl_prop != ZPROP_INVAL) {
3224 if (zfs_prop_get(zhp, pl->pl_prop, property,
3225 sizeof (property), NULL, NULL, 0,
3226 cb->cb_literal) != 0)
3230 right_justify = zfs_prop_align_right(pl->pl_prop);
3231 } else if (zfs_prop_userquota(pl->pl_user_prop)) {
3232 if (zfs_prop_get_userquota(zhp, pl->pl_user_prop,
3233 property, sizeof (property), cb->cb_literal) != 0)
3237 right_justify = B_TRUE;
3238 } else if (zfs_prop_written(pl->pl_user_prop)) {
3239 if (zfs_prop_get_written(zhp, pl->pl_user_prop,
3240 property, sizeof (property), cb->cb_literal) != 0)
3244 right_justify = B_TRUE;
3246 if (nvlist_lookup_nvlist(userprops,
3247 pl->pl_user_prop, &propval) != 0)
3250 verify(nvlist_lookup_string(propval,
3251 ZPROP_VALUE, &propstr) == 0);
3252 right_justify = B_FALSE;
3256 * If this is being called in scripted mode, or if this is the
3257 * last column and it is left-justified, don't include a width
3260 if (cb->cb_scripted || (pl->pl_next == NULL && !right_justify))
3261 (void) printf("%s", propstr);
3262 else if (right_justify)
3263 (void) printf("%*s", (int)pl->pl_width, propstr);
3265 (void) printf("%-*s", (int)pl->pl_width, propstr);
3268 (void) printf("\n");
3272 * Generic callback function to list a dataset or snapshot.
3275 list_callback(zfs_handle_t *zhp, void *data)
3277 list_cbdata_t *cbp = data;
3279 if (cbp->cb_first) {
3280 if (!cbp->cb_scripted)
3282 cbp->cb_first = B_FALSE;
3285 print_dataset(zhp, cbp);
3291 zfs_do_list(int argc, char **argv)
3294 static char default_fields[] =
3295 "name,used,available,referenced,mountpoint";
3296 int types = ZFS_TYPE_DATASET;
3297 boolean_t types_specified = B_FALSE;
3298 char *fields = NULL;
3299 list_cbdata_t cb = { 0 };
3303 zfs_sort_column_t *sortcol = NULL;
3304 int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
3307 while ((c = getopt(argc, argv, "HS:d:o:prs:t:")) != -1) {
3313 cb.cb_literal = B_TRUE;
3314 flags |= ZFS_ITER_LITERAL_PROPS;
3317 limit = parse_depth(optarg, &flags);
3320 flags |= ZFS_ITER_RECURSE;
3323 cb.cb_scripted = B_TRUE;
3326 if (zfs_add_sort_column(&sortcol, optarg,
3328 (void) fprintf(stderr,
3329 gettext("invalid property '%s'\n"), optarg);
3334 if (zfs_add_sort_column(&sortcol, optarg,
3336 (void) fprintf(stderr,
3337 gettext("invalid property '%s'\n"), optarg);
3343 types_specified = B_TRUE;
3344 flags &= ~ZFS_ITER_PROP_LISTSNAPS;
3345 while (*optarg != '\0') {
3346 static char *type_subopts[] = { "filesystem",
3347 "volume", "snapshot", "snap", "bookmark",
3350 switch (getsubopt(&optarg, type_subopts,
3353 types |= ZFS_TYPE_FILESYSTEM;
3356 types |= ZFS_TYPE_VOLUME;
3360 types |= ZFS_TYPE_SNAPSHOT;
3363 types |= ZFS_TYPE_BOOKMARK;
3366 types = ZFS_TYPE_DATASET |
3370 (void) fprintf(stderr,
3371 gettext("invalid type '%s'\n"),
3378 (void) fprintf(stderr, gettext("missing argument for "
3379 "'%c' option\n"), optopt);
3383 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3393 fields = default_fields;
3396 * If we are only going to list snapshot names and sort by name,
3397 * then we can use faster version.
3399 if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
3400 flags |= ZFS_ITER_SIMPLE;
3403 * If "-o space" and no types were specified, don't display snapshots.
3405 if (strcmp(fields, "space") == 0 && types_specified == B_FALSE)
3406 types &= ~ZFS_TYPE_SNAPSHOT;
3409 * If the user specifies '-o all', the zprop_get_list() doesn't
3410 * normally include the name of the dataset. For 'zfs list', we always
3411 * want this property to be first.
3413 if (zprop_get_list(g_zfs, fields, &cb.cb_proplist, ZFS_TYPE_DATASET)
3417 cb.cb_first = B_TRUE;
3419 ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
3420 limit, list_callback, &cb);
3422 zprop_free_list(cb.cb_proplist);
3423 zfs_free_sort_columns(sortcol);
3425 if (ret == 0 && cb.cb_first && !cb.cb_scripted)
3426 (void) fprintf(stderr, gettext("no datasets available\n"));
3432 * zfs rename [-f] <fs | snap | vol> <fs | snap | vol>
3433 * zfs rename [-f] -p <fs | vol> <fs | vol>
3434 * zfs rename -r <snap> <snap>
3436 * Renames the given dataset to another of the same type.
3438 * The '-p' flag creates all the non-existing ancestors of the target first.
3442 zfs_do_rename(int argc, char **argv)
3447 boolean_t recurse = B_FALSE;
3448 boolean_t parents = B_FALSE;
3449 boolean_t force_unmount = B_FALSE;
3452 while ((c = getopt(argc, argv, "prf")) != -1) {
3461 force_unmount = B_TRUE;
3465 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3474 /* check number of arguments */
3476 (void) fprintf(stderr, gettext("missing source dataset "
3481 (void) fprintf(stderr, gettext("missing target dataset "
3486 (void) fprintf(stderr, gettext("too many arguments\n"));
3490 if (recurse && parents) {
3491 (void) fprintf(stderr, gettext("-p and -r options are mutually "
3496 if (recurse && strchr(argv[0], '@') == 0) {
3497 (void) fprintf(stderr, gettext("source dataset for recursive "
3498 "rename must be a snapshot\n"));
3502 if ((zhp = zfs_open(g_zfs, argv[0], parents ? ZFS_TYPE_FILESYSTEM |
3503 ZFS_TYPE_VOLUME : ZFS_TYPE_DATASET)) == NULL)
3506 /* If we were asked and the name looks good, try to create ancestors. */
3507 if (parents && zfs_name_valid(argv[1], zfs_get_type(zhp)) &&
3508 zfs_create_ancestors(g_zfs, argv[1]) != 0) {
3513 ret = (zfs_rename(zhp, argv[1], recurse, force_unmount) != 0);
3522 * Promotes the given clone fs to be the parent
3526 zfs_do_promote(int argc, char **argv)
3532 if (argc > 1 && argv[1][0] == '-') {
3533 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3538 /* check number of arguments */
3540 (void) fprintf(stderr, gettext("missing clone filesystem"
3545 (void) fprintf(stderr, gettext("too many arguments\n"));
3549 zhp = zfs_open(g_zfs, argv[1], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3553 ret = (zfs_promote(zhp) != 0);
3561 * zfs rollback [-rRf] <snapshot>
3563 * -r Delete any intervening snapshots before doing rollback
3564 * -R Delete any snapshots and their clones
3565 * -f ignored for backwards compatibility
3567 * Given a filesystem, rollback to a specific snapshot, discarding any changes
3568 * since then and making it the active dataset. If more recent snapshots exist,
3569 * the command will complain unless the '-r' flag is given.
3571 typedef struct rollback_cbdata {
3577 boolean_t cb_recurse;
3578 } rollback_cbdata_t;
3581 rollback_check_dependent(zfs_handle_t *zhp, void *data)
3583 rollback_cbdata_t *cbp = data;
3585 if (cbp->cb_first && cbp->cb_recurse) {
3586 (void) fprintf(stderr, gettext("cannot rollback to "
3587 "'%s': clones of previous snapshots exist\n"),
3589 (void) fprintf(stderr, gettext("use '-R' to "
3590 "force deletion of the following clones and "
3596 (void) fprintf(stderr, "%s\n", zfs_get_name(zhp));
3604 * Report any snapshots more recent than the one specified. Used when '-r' is
3605 * not specified. We reuse this same callback for the snapshot dependents - if
3606 * 'cb_dependent' is set, then this is a dependent and we should report it
3607 * without checking the transaction group.
3610 rollback_check(zfs_handle_t *zhp, void *data)
3612 rollback_cbdata_t *cbp = data;
3614 if (cbp->cb_doclones) {
3619 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3620 if (cbp->cb_first && !cbp->cb_recurse) {
3621 (void) fprintf(stderr, gettext("cannot "
3622 "rollback to '%s': more recent snapshots "
3623 "or bookmarks exist\n"),
3625 (void) fprintf(stderr, gettext("use '-r' to "
3626 "force deletion of the following "
3627 "snapshots and bookmarks:\n"));
3632 if (cbp->cb_recurse) {
3633 if (zfs_iter_dependents(zhp, B_TRUE,
3634 rollback_check_dependent, cbp) != 0) {
3639 (void) fprintf(stderr, "%s\n",
3648 zfs_do_rollback(int argc, char **argv)
3652 boolean_t force = B_FALSE;
3653 rollback_cbdata_t cb = { 0 };
3654 zfs_handle_t *zhp, *snap;
3655 char parentname[ZFS_MAX_DATASET_NAME_LEN];
3659 while ((c = getopt(argc, argv, "rRf")) != -1) {
3672 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3681 /* check number of arguments */
3683 (void) fprintf(stderr, gettext("missing dataset argument\n"));
3687 (void) fprintf(stderr, gettext("too many arguments\n"));
3691 /* open the snapshot */
3692 if ((snap = zfs_open(g_zfs, argv[0], ZFS_TYPE_SNAPSHOT)) == NULL)
3695 /* open the parent dataset */
3696 (void) strlcpy(parentname, argv[0], sizeof (parentname));
3697 verify((delim = strrchr(parentname, '@')) != NULL);
3699 if ((zhp = zfs_open(g_zfs, parentname, ZFS_TYPE_DATASET)) == NULL) {
3705 * Check for more recent snapshots and/or clones based on the presence
3708 cb.cb_target = argv[0];
3709 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3710 cb.cb_first = B_TRUE;
3712 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, rollback_check, &cb)) != 0)
3714 if ((ret = zfs_iter_bookmarks(zhp, rollback_check, &cb)) != 0)
3717 if ((ret = cb.cb_error) != 0)
3721 * Rollback parent to the given snapshot.
3723 ret = zfs_rollback(zhp, snap, force);
3736 * zfs set property=value ... { fs | snap | vol } ...
3738 * Sets the given properties for all datasets specified on the command line.
3742 set_callback(zfs_handle_t *zhp, void *data)
3744 nvlist_t *props = data;
3746 if (zfs_prop_set_list(zhp, props) != 0) {
3747 switch (libzfs_errno(g_zfs)) {
3748 case EZFS_MOUNTFAILED:
3749 (void) fprintf(stderr, gettext("property may be set "
3750 "but unable to remount filesystem\n"));
3752 case EZFS_SHARENFSFAILED:
3753 (void) fprintf(stderr, gettext("property may be set "
3754 "but unable to reshare filesystem\n"));
3763 zfs_do_set(int argc, char **argv)
3765 nvlist_t *props = NULL;
3766 int ds_start = -1; /* argv idx of first dataset arg */
3770 /* check for options */
3771 if (argc > 1 && argv[1][0] == '-') {
3772 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3777 /* check number of arguments */
3779 (void) fprintf(stderr, gettext("missing arguments\n"));
3783 if (strchr(argv[1], '=') == NULL) {
3784 (void) fprintf(stderr, gettext("missing property=value "
3787 (void) fprintf(stderr, gettext("missing dataset "
3793 /* validate argument order: prop=val args followed by dataset args */
3794 for (i = 1; i < argc; i++) {
3795 if (strchr(argv[i], '=') != NULL) {
3797 /* out-of-order prop=val argument */
3798 (void) fprintf(stderr, gettext("invalid "
3799 "argument order\n"));
3802 } else if (ds_start < 0) {
3807 (void) fprintf(stderr, gettext("missing dataset name(s)\n"));
3811 /* Populate a list of property settings */
3812 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3814 for (i = 1; i < ds_start; i++) {
3815 if (!parseprop(props, argv[i])) {
3821 ret = zfs_for_each(argc - ds_start, argv + ds_start, 0,
3822 ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, props);
3829 typedef struct snap_cbdata {
3831 boolean_t sd_recursive;
3832 const char *sd_snapname;
3836 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3838 snap_cbdata_t *sd = arg;
3843 if (sd->sd_recursive &&
3844 zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) != 0) {
3849 error = asprintf(&name, "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3852 fnvlist_add_boolean(sd->sd_nvl, name);
3855 if (sd->sd_recursive)
3856 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3862 * zfs snapshot [-r] [-o prop=value] ... <fs@snap>
3864 * Creates a snapshot with the given name. While functionally equivalent to
3865 * 'zfs create', it is a separate command to differentiate intent.
3868 zfs_do_snapshot(int argc, char **argv)
3873 snap_cbdata_t sd = { 0 };
3874 boolean_t multiple_snaps = B_FALSE;
3876 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
3878 if (nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) != 0)
3882 while ((c = getopt(argc, argv, "ro:")) != -1) {
3885 if (!parseprop(props, optarg)) {
3886 nvlist_free(sd.sd_nvl);
3892 sd.sd_recursive = B_TRUE;
3893 multiple_snaps = B_TRUE;
3896 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
3905 /* check number of arguments */
3907 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
3912 multiple_snaps = B_TRUE;
3913 for (; argc > 0; argc--, argv++) {
3917 atp = strchr(argv[0], '@');
3921 sd.sd_snapname = atp + 1;
3922 zhp = zfs_open(g_zfs, argv[0],
3923 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3926 if (zfs_snapshot_cb(zhp, &sd) != 0)
3930 ret = zfs_snapshot_nvl(g_zfs, sd.sd_nvl, props);
3931 nvlist_free(sd.sd_nvl);
3933 if (ret != 0 && multiple_snaps)
3934 (void) fprintf(stderr, gettext("no snapshots were created\n"));
3938 nvlist_free(sd.sd_nvl);
3945 * Send a backup stream to stdout.
3948 zfs_do_send(int argc, char **argv)
3950 char *fromname = NULL;
3951 char *toname = NULL;
3952 char *resume_token = NULL;
3955 sendflags_t flags = { 0 };
3957 nvlist_t *dbgnv = NULL;
3958 boolean_t extraverbose = B_FALSE;
3960 struct option long_options[] = {
3961 {"replicate", no_argument, NULL, 'R'},
3962 {"props", no_argument, NULL, 'p'},
3963 {"parsable", no_argument, NULL, 'P'},
3964 {"dedup", no_argument, NULL, 'D'},
3965 {"verbose", no_argument, NULL, 'v'},
3966 {"dryrun", no_argument, NULL, 'n'},
3967 {"large-block", no_argument, NULL, 'L'},
3968 {"embed", no_argument, NULL, 'e'},
3969 {"resume", required_argument, NULL, 't'},
3970 {"compressed", no_argument, NULL, 'c'},
3971 {"raw", no_argument, NULL, 'w'},
3972 {"backup", no_argument, NULL, 'b'},
3977 while ((c = getopt_long(argc, argv, ":i:I:RDpvnPLet:cwb", long_options,
3989 flags.doall = B_TRUE;
3992 flags.replicate = B_TRUE;
3995 flags.props = B_TRUE;
3998 flags.backup = B_TRUE;
4001 flags.parsable = B_TRUE;
4002 flags.verbose = B_TRUE;
4006 extraverbose = B_TRUE;
4007 flags.verbose = B_TRUE;
4008 flags.progress = B_TRUE;
4011 flags.dedup = B_TRUE;
4014 flags.dryrun = B_TRUE;
4017 flags.largeblock = B_TRUE;
4020 flags.embed_data = B_TRUE;
4023 resume_token = optarg;
4026 flags.compress = B_TRUE;
4030 flags.compress = B_TRUE;
4031 flags.embed_data = B_TRUE;
4032 flags.largeblock = B_TRUE;
4036 * If a parameter was not passed, optopt contains the
4037 * value that would normally lead us into the
4038 * appropriate case statement. If it's > 256, then this
4039 * must be a longopt and we should look at argv to get
4040 * the string. Otherwise it's just the character, so we
4041 * should use it directly.
4043 if (optopt <= UINT8_MAX) {
4044 (void) fprintf(stderr,
4045 gettext("missing argument for '%c' "
4046 "option\n"), optopt);
4048 (void) fprintf(stderr,
4049 gettext("missing argument for '%s' "
4050 "option\n"), argv[optind - 1]);
4058 * If an invalid flag was passed, optopt contains the
4059 * character if it was a short flag, or 0 if it was a
4063 (void) fprintf(stderr,
4064 gettext("invalid option '%c'\n"), optopt);
4066 (void) fprintf(stderr,
4067 gettext("invalid option '%s'\n"),
4078 if (resume_token != NULL) {
4079 if (fromname != NULL || flags.replicate || flags.props ||
4080 flags.backup || flags.dedup) {
4081 (void) fprintf(stderr,
4082 gettext("invalid flags combined with -t\n"));
4086 (void) fprintf(stderr, gettext("no additional "
4087 "arguments are permitted with -t\n"));
4092 (void) fprintf(stderr,
4093 gettext("missing snapshot argument\n"));
4097 (void) fprintf(stderr, gettext("too many arguments\n"));
4102 if (!flags.dryrun && isatty(STDOUT_FILENO)) {
4103 (void) fprintf(stderr,
4104 gettext("Error: Stream can not be written to a terminal.\n"
4105 "You must redirect standard output.\n"));
4109 if (resume_token != NULL) {
4110 return (zfs_send_resume(g_zfs, &flags, STDOUT_FILENO,
4115 * Special case sending a filesystem, or from a bookmark.
4117 if (strchr(argv[0], '@') == NULL ||
4118 (fromname && strchr(fromname, '#') != NULL)) {
4119 char frombuf[ZFS_MAX_DATASET_NAME_LEN];
4121 if (flags.replicate || flags.doall || flags.props ||
4122 flags.backup || flags.dedup ||
4123 (strchr(argv[0], '@') == NULL &&
4124 (flags.dryrun || flags.verbose || flags.progress))) {
4125 (void) fprintf(stderr, gettext("Error: "
4126 "Unsupported flag with filesystem or bookmark.\n"));
4130 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_DATASET);
4134 if (fromname != NULL &&
4135 (fromname[0] == '#' || fromname[0] == '@')) {
4137 * Incremental source name begins with # or @.
4138 * Default to same fs as target.
4140 (void) strlcpy(frombuf, argv[0], sizeof (frombuf));
4141 cp = strchr(frombuf, '@');
4144 (void) strlcat(frombuf, fromname, sizeof (frombuf));
4147 err = zfs_send_one(zhp, fromname, STDOUT_FILENO, flags);
4152 cp = strchr(argv[0], '@');
4155 zhp = zfs_open(g_zfs, argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4160 * If they specified the full path to the snapshot, chop off
4161 * everything except the short name of the snapshot, but special
4162 * case if they specify the origin.
4164 if (fromname && (cp = strchr(fromname, '@')) != NULL) {
4165 char origin[ZFS_MAX_DATASET_NAME_LEN];
4168 (void) zfs_prop_get(zhp, ZFS_PROP_ORIGIN,
4169 origin, sizeof (origin), &src, NULL, 0, B_FALSE);
4171 if (strcmp(origin, fromname) == 0) {
4173 flags.fromorigin = B_TRUE;
4176 if (cp != fromname && strcmp(argv[0], fromname)) {
4177 (void) fprintf(stderr,
4178 gettext("incremental source must be "
4179 "in same filesystem\n"));
4183 if (strchr(fromname, '@') || strchr(fromname, '/')) {
4184 (void) fprintf(stderr,
4185 gettext("invalid incremental source\n"));
4191 if (flags.replicate && fromname == NULL)
4192 flags.doall = B_TRUE;
4194 err = zfs_send(zhp, fromname, toname, &flags, STDOUT_FILENO, NULL, 0,
4195 extraverbose ? &dbgnv : NULL);
4197 if (extraverbose && dbgnv != NULL) {
4199 * dump_nvlist prints to stdout, but that's been
4200 * redirected to a file. Make it print to stderr
4203 (void) dup2(STDERR_FILENO, STDOUT_FILENO);
4204 dump_nvlist(dbgnv, 0);
4213 * Restore a backup stream from stdin.
4216 zfs_do_receive(int argc, char **argv)
4219 recvflags_t flags = { 0 };
4220 boolean_t abort_resumable = B_FALSE;
4223 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
4227 while ((c = getopt(argc, argv, ":o:x:denuvFsA")) != -1) {
4230 if (!parseprop(props, optarg)) {
4236 if (!parsepropname(props, optarg)) {
4242 flags.isprefix = B_TRUE;
4245 flags.isprefix = B_TRUE;
4246 flags.istail = B_TRUE;
4249 flags.dryrun = B_TRUE;
4252 flags.nomount = B_TRUE;
4255 flags.verbose = B_TRUE;
4258 flags.resumable = B_TRUE;
4261 flags.force = B_TRUE;
4264 abort_resumable = B_TRUE;
4267 (void) fprintf(stderr, gettext("missing argument for "
4268 "'%c' option\n"), optopt);
4272 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
4281 /* check number of arguments */
4283 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
4287 (void) fprintf(stderr, gettext("too many arguments\n"));
4291 if (abort_resumable) {
4292 if (flags.isprefix || flags.istail || flags.dryrun ||
4293 flags.resumable || flags.nomount) {
4294 (void) fprintf(stderr, gettext("invalid option\n"));
4298 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4299 (void) snprintf(namebuf, sizeof (namebuf),
4300 "%s/%%recv", argv[0]);
4302 if (zfs_dataset_exists(g_zfs, namebuf,
4303 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) {
4304 zfs_handle_t *zhp = zfs_open(g_zfs,
4305 namebuf, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4310 err = zfs_destroy(zhp, B_FALSE);
4313 zfs_handle_t *zhp = zfs_open(g_zfs,
4314 argv[0], ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4317 if (!zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) ||
4318 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4319 NULL, 0, NULL, NULL, 0, B_TRUE) == -1) {
4320 (void) fprintf(stderr,
4321 gettext("'%s' does not have any "
4322 "resumable receive state to abort\n"),
4328 err = zfs_destroy(zhp, B_FALSE);
4335 if (isatty(STDIN_FILENO)) {
4336 (void) fprintf(stderr,
4337 gettext("Error: Backup stream can not be read "
4338 "from a terminal.\n"
4339 "You must redirect standard input.\n"));
4343 err = zfs_receive(g_zfs, argv[0], props, &flags, STDIN_FILENO, NULL);
4350 * allow/unallow stuff
4352 /* copied from zfs/sys/dsl_deleg.h */
4353 #define ZFS_DELEG_PERM_CREATE "create"
4354 #define ZFS_DELEG_PERM_DESTROY "destroy"
4355 #define ZFS_DELEG_PERM_SNAPSHOT "snapshot"
4356 #define ZFS_DELEG_PERM_ROLLBACK "rollback"
4357 #define ZFS_DELEG_PERM_CLONE "clone"
4358 #define ZFS_DELEG_PERM_PROMOTE "promote"
4359 #define ZFS_DELEG_PERM_RENAME "rename"
4360 #define ZFS_DELEG_PERM_MOUNT "mount"
4361 #define ZFS_DELEG_PERM_SHARE "share"
4362 #define ZFS_DELEG_PERM_SEND "send"
4363 #define ZFS_DELEG_PERM_RECEIVE "receive"
4364 #define ZFS_DELEG_PERM_ALLOW "allow"
4365 #define ZFS_DELEG_PERM_USERPROP "userprop"
4366 #define ZFS_DELEG_PERM_VSCAN "vscan" /* ??? */
4367 #define ZFS_DELEG_PERM_USERQUOTA "userquota"
4368 #define ZFS_DELEG_PERM_GROUPQUOTA "groupquota"
4369 #define ZFS_DELEG_PERM_USERUSED "userused"
4370 #define ZFS_DELEG_PERM_GROUPUSED "groupused"
4371 #define ZFS_DELEG_PERM_USEROBJQUOTA "userobjquota"
4372 #define ZFS_DELEG_PERM_GROUPOBJQUOTA "groupobjquota"
4373 #define ZFS_DELEG_PERM_USEROBJUSED "userobjused"
4374 #define ZFS_DELEG_PERM_GROUPOBJUSED "groupobjused"
4376 #define ZFS_DELEG_PERM_HOLD "hold"
4377 #define ZFS_DELEG_PERM_RELEASE "release"
4378 #define ZFS_DELEG_PERM_DIFF "diff"
4379 #define ZFS_DELEG_PERM_BOOKMARK "bookmark"
4380 #define ZFS_DELEG_PERM_REMAP "remap"
4381 #define ZFS_DELEG_PERM_LOAD_KEY "load-key"
4382 #define ZFS_DELEG_PERM_CHANGE_KEY "change-key"
4384 #define ZFS_DELEG_PERM_PROJECTUSED "projectused"
4385 #define ZFS_DELEG_PERM_PROJECTQUOTA "projectquota"
4386 #define ZFS_DELEG_PERM_PROJECTOBJUSED "projectobjused"
4387 #define ZFS_DELEG_PERM_PROJECTOBJQUOTA "projectobjquota"
4389 #define ZFS_NUM_DELEG_NOTES ZFS_DELEG_NOTE_NONE
4391 static zfs_deleg_perm_tab_t zfs_deleg_perm_tbl[] = {
4392 { ZFS_DELEG_PERM_ALLOW, ZFS_DELEG_NOTE_ALLOW },
4393 { ZFS_DELEG_PERM_CLONE, ZFS_DELEG_NOTE_CLONE },
4394 { ZFS_DELEG_PERM_CREATE, ZFS_DELEG_NOTE_CREATE },
4395 { ZFS_DELEG_PERM_DESTROY, ZFS_DELEG_NOTE_DESTROY },
4396 { ZFS_DELEG_PERM_DIFF, ZFS_DELEG_NOTE_DIFF},
4397 { ZFS_DELEG_PERM_HOLD, ZFS_DELEG_NOTE_HOLD },
4398 { ZFS_DELEG_PERM_MOUNT, ZFS_DELEG_NOTE_MOUNT },
4399 { ZFS_DELEG_PERM_PROMOTE, ZFS_DELEG_NOTE_PROMOTE },
4400 { ZFS_DELEG_PERM_RECEIVE, ZFS_DELEG_NOTE_RECEIVE },
4401 { ZFS_DELEG_PERM_RELEASE, ZFS_DELEG_NOTE_RELEASE },
4402 { ZFS_DELEG_PERM_RENAME, ZFS_DELEG_NOTE_RENAME },
4403 { ZFS_DELEG_PERM_ROLLBACK, ZFS_DELEG_NOTE_ROLLBACK },
4404 { ZFS_DELEG_PERM_SEND, ZFS_DELEG_NOTE_SEND },
4405 { ZFS_DELEG_PERM_SHARE, ZFS_DELEG_NOTE_SHARE },
4406 { ZFS_DELEG_PERM_SNAPSHOT, ZFS_DELEG_NOTE_SNAPSHOT },
4407 { ZFS_DELEG_PERM_BOOKMARK, ZFS_DELEG_NOTE_BOOKMARK },
4408 { ZFS_DELEG_PERM_REMAP, ZFS_DELEG_NOTE_REMAP },
4409 { ZFS_DELEG_PERM_LOAD_KEY, ZFS_DELEG_NOTE_LOAD_KEY },
4410 { ZFS_DELEG_PERM_CHANGE_KEY, ZFS_DELEG_NOTE_CHANGE_KEY },
4412 { ZFS_DELEG_PERM_GROUPQUOTA, ZFS_DELEG_NOTE_GROUPQUOTA },
4413 { ZFS_DELEG_PERM_GROUPUSED, ZFS_DELEG_NOTE_GROUPUSED },
4414 { ZFS_DELEG_PERM_USERPROP, ZFS_DELEG_NOTE_USERPROP },
4415 { ZFS_DELEG_PERM_USERQUOTA, ZFS_DELEG_NOTE_USERQUOTA },
4416 { ZFS_DELEG_PERM_USERUSED, ZFS_DELEG_NOTE_USERUSED },
4417 { ZFS_DELEG_PERM_USEROBJQUOTA, ZFS_DELEG_NOTE_USEROBJQUOTA },
4418 { ZFS_DELEG_PERM_USEROBJUSED, ZFS_DELEG_NOTE_USEROBJUSED },
4419 { ZFS_DELEG_PERM_GROUPOBJQUOTA, ZFS_DELEG_NOTE_GROUPOBJQUOTA },
4420 { ZFS_DELEG_PERM_GROUPOBJUSED, ZFS_DELEG_NOTE_GROUPOBJUSED },
4421 { ZFS_DELEG_PERM_PROJECTUSED, ZFS_DELEG_NOTE_PROJECTUSED },
4422 { ZFS_DELEG_PERM_PROJECTQUOTA, ZFS_DELEG_NOTE_PROJECTQUOTA },
4423 { ZFS_DELEG_PERM_PROJECTOBJUSED, ZFS_DELEG_NOTE_PROJECTOBJUSED },
4424 { ZFS_DELEG_PERM_PROJECTOBJQUOTA, ZFS_DELEG_NOTE_PROJECTOBJQUOTA },
4425 { NULL, ZFS_DELEG_NOTE_NONE }
4428 /* permission structure */
4429 typedef struct deleg_perm {
4430 zfs_deleg_who_type_t dp_who_type;
4431 const char *dp_name;
4433 boolean_t dp_descend;
4437 typedef struct deleg_perm_node {
4438 deleg_perm_t dpn_perm;
4440 uu_avl_node_t dpn_avl_node;
4441 } deleg_perm_node_t;
4443 typedef struct fs_perm fs_perm_t;
4445 /* permissions set */
4446 typedef struct who_perm {
4447 zfs_deleg_who_type_t who_type;
4448 const char *who_name; /* id */
4449 char who_ug_name[256]; /* user/group name */
4450 fs_perm_t *who_fsperm; /* uplink */
4452 uu_avl_t *who_deleg_perm_avl; /* permissions */
4456 typedef struct who_perm_node {
4457 who_perm_t who_perm;
4458 uu_avl_node_t who_avl_node;
4461 typedef struct fs_perm_set fs_perm_set_t;
4462 /* fs permissions */
4464 const char *fsp_name;
4466 uu_avl_t *fsp_sc_avl; /* sets,create */
4467 uu_avl_t *fsp_uge_avl; /* user,group,everyone */
4469 fs_perm_set_t *fsp_set; /* uplink */
4473 typedef struct fs_perm_node {
4474 fs_perm_t fspn_fsperm;
4477 uu_list_node_t fspn_list_node;
4480 /* top level structure */
4481 struct fs_perm_set {
4482 uu_list_pool_t *fsps_list_pool;
4483 uu_list_t *fsps_list; /* list of fs_perms */
4485 uu_avl_pool_t *fsps_named_set_avl_pool;
4486 uu_avl_pool_t *fsps_who_perm_avl_pool;
4487 uu_avl_pool_t *fsps_deleg_perm_avl_pool;
4490 static inline const char *
4491 deleg_perm_type(zfs_deleg_note_t note)
4497 case ZFS_DELEG_NOTE_GROUPQUOTA:
4498 case ZFS_DELEG_NOTE_GROUPUSED:
4499 case ZFS_DELEG_NOTE_USERPROP:
4500 case ZFS_DELEG_NOTE_USERQUOTA:
4501 case ZFS_DELEG_NOTE_USERUSED:
4502 case ZFS_DELEG_NOTE_USEROBJQUOTA:
4503 case ZFS_DELEG_NOTE_USEROBJUSED:
4504 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
4505 case ZFS_DELEG_NOTE_GROUPOBJUSED:
4506 case ZFS_DELEG_NOTE_PROJECTUSED:
4507 case ZFS_DELEG_NOTE_PROJECTQUOTA:
4508 case ZFS_DELEG_NOTE_PROJECTOBJUSED:
4509 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
4511 return (gettext("other"));
4513 return (gettext("subcommand"));
4518 who_type2weight(zfs_deleg_who_type_t who_type)
4522 case ZFS_DELEG_NAMED_SET_SETS:
4523 case ZFS_DELEG_NAMED_SET:
4526 case ZFS_DELEG_CREATE_SETS:
4527 case ZFS_DELEG_CREATE:
4530 case ZFS_DELEG_USER_SETS:
4531 case ZFS_DELEG_USER:
4534 case ZFS_DELEG_GROUP_SETS:
4535 case ZFS_DELEG_GROUP:
4538 case ZFS_DELEG_EVERYONE_SETS:
4539 case ZFS_DELEG_EVERYONE:
4551 who_perm_compare(const void *larg, const void *rarg, void *unused)
4553 const who_perm_node_t *l = larg;
4554 const who_perm_node_t *r = rarg;
4555 zfs_deleg_who_type_t ltype = l->who_perm.who_type;
4556 zfs_deleg_who_type_t rtype = r->who_perm.who_type;
4557 int lweight = who_type2weight(ltype);
4558 int rweight = who_type2weight(rtype);
4559 int res = lweight - rweight;
4561 res = strncmp(l->who_perm.who_name, r->who_perm.who_name,
4562 ZFS_MAX_DELEG_NAME-1);
4574 deleg_perm_compare(const void *larg, const void *rarg, void *unused)
4576 const deleg_perm_node_t *l = larg;
4577 const deleg_perm_node_t *r = rarg;
4578 int res = strncmp(l->dpn_perm.dp_name, r->dpn_perm.dp_name,
4579 ZFS_MAX_DELEG_NAME-1);
4591 fs_perm_set_init(fs_perm_set_t *fspset)
4593 bzero(fspset, sizeof (fs_perm_set_t));
4595 if ((fspset->fsps_list_pool = uu_list_pool_create("fsps_list_pool",
4596 sizeof (fs_perm_node_t), offsetof(fs_perm_node_t, fspn_list_node),
4597 NULL, UU_DEFAULT)) == NULL)
4599 if ((fspset->fsps_list = uu_list_create(fspset->fsps_list_pool, NULL,
4600 UU_DEFAULT)) == NULL)
4603 if ((fspset->fsps_named_set_avl_pool = uu_avl_pool_create(
4604 "named_set_avl_pool", sizeof (who_perm_node_t), offsetof(
4605 who_perm_node_t, who_avl_node), who_perm_compare,
4606 UU_DEFAULT)) == NULL)
4609 if ((fspset->fsps_who_perm_avl_pool = uu_avl_pool_create(
4610 "who_perm_avl_pool", sizeof (who_perm_node_t), offsetof(
4611 who_perm_node_t, who_avl_node), who_perm_compare,
4612 UU_DEFAULT)) == NULL)
4615 if ((fspset->fsps_deleg_perm_avl_pool = uu_avl_pool_create(
4616 "deleg_perm_avl_pool", sizeof (deleg_perm_node_t), offsetof(
4617 deleg_perm_node_t, dpn_avl_node), deleg_perm_compare, UU_DEFAULT))
4622 static inline void fs_perm_fini(fs_perm_t *);
4623 static inline void who_perm_fini(who_perm_t *);
4626 fs_perm_set_fini(fs_perm_set_t *fspset)
4628 fs_perm_node_t *node = uu_list_first(fspset->fsps_list);
4630 while (node != NULL) {
4631 fs_perm_node_t *next_node =
4632 uu_list_next(fspset->fsps_list, node);
4633 fs_perm_t *fsperm = &node->fspn_fsperm;
4634 fs_perm_fini(fsperm);
4635 uu_list_remove(fspset->fsps_list, node);
4640 uu_avl_pool_destroy(fspset->fsps_named_set_avl_pool);
4641 uu_avl_pool_destroy(fspset->fsps_who_perm_avl_pool);
4642 uu_avl_pool_destroy(fspset->fsps_deleg_perm_avl_pool);
4646 deleg_perm_init(deleg_perm_t *deleg_perm, zfs_deleg_who_type_t type,
4649 deleg_perm->dp_who_type = type;
4650 deleg_perm->dp_name = name;
4654 who_perm_init(who_perm_t *who_perm, fs_perm_t *fsperm,
4655 zfs_deleg_who_type_t type, const char *name)
4657 uu_avl_pool_t *pool;
4658 pool = fsperm->fsp_set->fsps_deleg_perm_avl_pool;
4660 bzero(who_perm, sizeof (who_perm_t));
4662 if ((who_perm->who_deleg_perm_avl = uu_avl_create(pool, NULL,
4663 UU_DEFAULT)) == NULL)
4666 who_perm->who_type = type;
4667 who_perm->who_name = name;
4668 who_perm->who_fsperm = fsperm;
4672 who_perm_fini(who_perm_t *who_perm)
4674 deleg_perm_node_t *node = uu_avl_first(who_perm->who_deleg_perm_avl);
4676 while (node != NULL) {
4677 deleg_perm_node_t *next_node =
4678 uu_avl_next(who_perm->who_deleg_perm_avl, node);
4680 uu_avl_remove(who_perm->who_deleg_perm_avl, node);
4685 uu_avl_destroy(who_perm->who_deleg_perm_avl);
4689 fs_perm_init(fs_perm_t *fsperm, fs_perm_set_t *fspset, const char *fsname)
4691 uu_avl_pool_t *nset_pool = fspset->fsps_named_set_avl_pool;
4692 uu_avl_pool_t *who_pool = fspset->fsps_who_perm_avl_pool;
4694 bzero(fsperm, sizeof (fs_perm_t));
4696 if ((fsperm->fsp_sc_avl = uu_avl_create(nset_pool, NULL, UU_DEFAULT))
4700 if ((fsperm->fsp_uge_avl = uu_avl_create(who_pool, NULL, UU_DEFAULT))
4704 fsperm->fsp_set = fspset;
4705 fsperm->fsp_name = fsname;
4709 fs_perm_fini(fs_perm_t *fsperm)
4711 who_perm_node_t *node = uu_avl_first(fsperm->fsp_sc_avl);
4712 while (node != NULL) {
4713 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_sc_avl,
4715 who_perm_t *who_perm = &node->who_perm;
4716 who_perm_fini(who_perm);
4717 uu_avl_remove(fsperm->fsp_sc_avl, node);
4722 node = uu_avl_first(fsperm->fsp_uge_avl);
4723 while (node != NULL) {
4724 who_perm_node_t *next_node = uu_avl_next(fsperm->fsp_uge_avl,
4726 who_perm_t *who_perm = &node->who_perm;
4727 who_perm_fini(who_perm);
4728 uu_avl_remove(fsperm->fsp_uge_avl, node);
4733 uu_avl_destroy(fsperm->fsp_sc_avl);
4734 uu_avl_destroy(fsperm->fsp_uge_avl);
4738 set_deleg_perm_node(uu_avl_t *avl, deleg_perm_node_t *node,
4739 zfs_deleg_who_type_t who_type, const char *name, char locality)
4741 uu_avl_index_t idx = 0;
4743 deleg_perm_node_t *found_node = NULL;
4744 deleg_perm_t *deleg_perm = &node->dpn_perm;
4746 deleg_perm_init(deleg_perm, who_type, name);
4748 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4750 uu_avl_insert(avl, node, idx);
4753 deleg_perm = &node->dpn_perm;
4758 case ZFS_DELEG_LOCAL:
4759 deleg_perm->dp_local = B_TRUE;
4761 case ZFS_DELEG_DESCENDENT:
4762 deleg_perm->dp_descend = B_TRUE;
4767 assert(B_FALSE); /* invalid locality */
4772 parse_who_perm(who_perm_t *who_perm, nvlist_t *nvl, char locality)
4774 nvpair_t *nvp = NULL;
4775 fs_perm_set_t *fspset = who_perm->who_fsperm->fsp_set;
4776 uu_avl_t *avl = who_perm->who_deleg_perm_avl;
4777 zfs_deleg_who_type_t who_type = who_perm->who_type;
4779 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4780 const char *name = nvpair_name(nvp);
4781 data_type_t type = nvpair_type(nvp);
4782 uu_avl_pool_t *avl_pool = fspset->fsps_deleg_perm_avl_pool;
4783 deleg_perm_node_t *node =
4784 safe_malloc(sizeof (deleg_perm_node_t));
4786 VERIFY(type == DATA_TYPE_BOOLEAN);
4788 uu_avl_node_init(node, &node->dpn_avl_node, avl_pool);
4789 set_deleg_perm_node(avl, node, who_type, name, locality);
4796 parse_fs_perm(fs_perm_t *fsperm, nvlist_t *nvl)
4798 nvpair_t *nvp = NULL;
4799 fs_perm_set_t *fspset = fsperm->fsp_set;
4801 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4802 nvlist_t *nvl2 = NULL;
4803 const char *name = nvpair_name(nvp);
4804 uu_avl_t *avl = NULL;
4805 uu_avl_pool_t *avl_pool = NULL;
4806 zfs_deleg_who_type_t perm_type = name[0];
4807 char perm_locality = name[1];
4808 const char *perm_name = name + 3;
4809 boolean_t is_set = B_TRUE;
4810 who_perm_t *who_perm = NULL;
4812 assert('$' == name[2]);
4814 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4817 switch (perm_type) {
4818 case ZFS_DELEG_CREATE:
4819 case ZFS_DELEG_CREATE_SETS:
4820 case ZFS_DELEG_NAMED_SET:
4821 case ZFS_DELEG_NAMED_SET_SETS:
4822 avl_pool = fspset->fsps_named_set_avl_pool;
4823 avl = fsperm->fsp_sc_avl;
4825 case ZFS_DELEG_USER:
4826 case ZFS_DELEG_USER_SETS:
4827 case ZFS_DELEG_GROUP:
4828 case ZFS_DELEG_GROUP_SETS:
4829 case ZFS_DELEG_EVERYONE:
4830 case ZFS_DELEG_EVERYONE_SETS:
4831 avl_pool = fspset->fsps_who_perm_avl_pool;
4832 avl = fsperm->fsp_uge_avl;
4836 assert(!"unhandled zfs_deleg_who_type_t");
4840 who_perm_node_t *found_node = NULL;
4841 who_perm_node_t *node = safe_malloc(
4842 sizeof (who_perm_node_t));
4843 who_perm = &node->who_perm;
4844 uu_avl_index_t idx = 0;
4846 uu_avl_node_init(node, &node->who_avl_node, avl_pool);
4847 who_perm_init(who_perm, fsperm, perm_type, perm_name);
4849 if ((found_node = uu_avl_find(avl, node, NULL, &idx))
4851 if (avl == fsperm->fsp_uge_avl) {
4853 struct passwd *p = NULL;
4854 struct group *g = NULL;
4855 const char *nice_name = NULL;
4857 switch (perm_type) {
4858 case ZFS_DELEG_USER_SETS:
4859 case ZFS_DELEG_USER:
4860 rid = atoi(perm_name);
4863 nice_name = p->pw_name;
4865 case ZFS_DELEG_GROUP_SETS:
4866 case ZFS_DELEG_GROUP:
4867 rid = atoi(perm_name);
4870 nice_name = g->gr_name;
4877 if (nice_name != NULL)
4879 node->who_perm.who_ug_name,
4883 uu_avl_insert(avl, node, idx);
4886 who_perm = &node->who_perm;
4889 VERIFY3P(who_perm, !=, NULL);
4890 (void) parse_who_perm(who_perm, nvl2, perm_locality);
4897 parse_fs_perm_set(fs_perm_set_t *fspset, nvlist_t *nvl)
4899 nvpair_t *nvp = NULL;
4900 uu_avl_index_t idx = 0;
4902 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
4903 nvlist_t *nvl2 = NULL;
4904 const char *fsname = nvpair_name(nvp);
4905 data_type_t type = nvpair_type(nvp);
4906 fs_perm_t *fsperm = NULL;
4907 fs_perm_node_t *node = safe_malloc(sizeof (fs_perm_node_t));
4911 fsperm = &node->fspn_fsperm;
4913 VERIFY(DATA_TYPE_NVLIST == type);
4915 uu_list_node_init(node, &node->fspn_list_node,
4916 fspset->fsps_list_pool);
4918 idx = uu_list_numnodes(fspset->fsps_list);
4919 fs_perm_init(fsperm, fspset, fsname);
4921 if (nvpair_value_nvlist(nvp, &nvl2) != 0)
4924 (void) parse_fs_perm(fsperm, nvl2);
4926 uu_list_insert(fspset->fsps_list, node, idx);
4932 static inline const char *
4933 deleg_perm_comment(zfs_deleg_note_t note)
4935 const char *str = "";
4940 case ZFS_DELEG_NOTE_ALLOW:
4941 str = gettext("Must also have the permission that is being"
4942 "\n\t\t\t\tallowed");
4944 case ZFS_DELEG_NOTE_CLONE:
4945 str = gettext("Must also have the 'create' ability and 'mount'"
4946 "\n\t\t\t\tability in the origin file system");
4948 case ZFS_DELEG_NOTE_CREATE:
4949 str = gettext("Must also have the 'mount' ability");
4951 case ZFS_DELEG_NOTE_DESTROY:
4952 str = gettext("Must also have the 'mount' ability");
4954 case ZFS_DELEG_NOTE_DIFF:
4955 str = gettext("Allows lookup of paths within a dataset;"
4956 "\n\t\t\t\tgiven an object number. Ordinary users need this"
4957 "\n\t\t\t\tin order to use zfs diff");
4959 case ZFS_DELEG_NOTE_HOLD:
4960 str = gettext("Allows adding a user hold to a snapshot");
4962 case ZFS_DELEG_NOTE_MOUNT:
4963 str = gettext("Allows mount/umount of ZFS datasets");
4965 case ZFS_DELEG_NOTE_PROMOTE:
4966 str = gettext("Must also have the 'mount'\n\t\t\t\tand"
4967 " 'promote' ability in the origin file system");
4969 case ZFS_DELEG_NOTE_RECEIVE:
4970 str = gettext("Must also have the 'mount' and 'create'"
4973 case ZFS_DELEG_NOTE_RELEASE:
4974 str = gettext("Allows releasing a user hold which\n\t\t\t\t"
4975 "might destroy the snapshot");
4977 case ZFS_DELEG_NOTE_RENAME:
4978 str = gettext("Must also have the 'mount' and 'create'"
4979 "\n\t\t\t\tability in the new parent");
4981 case ZFS_DELEG_NOTE_ROLLBACK:
4984 case ZFS_DELEG_NOTE_SEND:
4987 case ZFS_DELEG_NOTE_SHARE:
4988 str = gettext("Allows sharing file systems over NFS or SMB"
4989 "\n\t\t\t\tprotocols");
4991 case ZFS_DELEG_NOTE_SNAPSHOT:
4994 case ZFS_DELEG_NOTE_LOAD_KEY:
4995 str = gettext("Allows loading or unloading an encryption key");
4997 case ZFS_DELEG_NOTE_CHANGE_KEY:
4998 str = gettext("Allows changing or adding an encryption key");
5001 * case ZFS_DELEG_NOTE_VSCAN:
5002 * str = gettext("");
5006 case ZFS_DELEG_NOTE_GROUPQUOTA:
5007 str = gettext("Allows accessing any groupquota@... property");
5009 case ZFS_DELEG_NOTE_GROUPUSED:
5010 str = gettext("Allows reading any groupused@... property");
5012 case ZFS_DELEG_NOTE_USERPROP:
5013 str = gettext("Allows changing any user property");
5015 case ZFS_DELEG_NOTE_USERQUOTA:
5016 str = gettext("Allows accessing any userquota@... property");
5018 case ZFS_DELEG_NOTE_USERUSED:
5019 str = gettext("Allows reading any userused@... property");
5021 case ZFS_DELEG_NOTE_USEROBJQUOTA:
5022 str = gettext("Allows accessing any userobjquota@... property");
5024 case ZFS_DELEG_NOTE_GROUPOBJQUOTA:
5025 str = gettext("Allows accessing any \n\t\t\t\t"
5026 "groupobjquota@... property");
5028 case ZFS_DELEG_NOTE_GROUPOBJUSED:
5029 str = gettext("Allows reading any groupobjused@... property");
5031 case ZFS_DELEG_NOTE_USEROBJUSED:
5032 str = gettext("Allows reading any userobjused@... property");
5034 case ZFS_DELEG_NOTE_PROJECTQUOTA:
5035 str = gettext("Allows accessing any projectquota@... property");
5037 case ZFS_DELEG_NOTE_PROJECTOBJQUOTA:
5038 str = gettext("Allows accessing any \n\t\t\t\t"
5039 "projectobjquota@... property");
5041 case ZFS_DELEG_NOTE_PROJECTUSED:
5042 str = gettext("Allows reading any projectused@... property");
5044 case ZFS_DELEG_NOTE_PROJECTOBJUSED:
5045 str = gettext("Allows accessing any \n\t\t\t\t"
5046 "projectobjused@... property");
5064 boolean_t recursive; /* unallow only */
5065 boolean_t prt_usage;
5067 boolean_t prt_perms;
5070 const char *dataset;
5074 prop_cmp(const void *a, const void *b)
5076 const char *str1 = *(const char **)a;
5077 const char *str2 = *(const char **)b;
5078 return (strcmp(str1, str2));
5082 allow_usage(boolean_t un, boolean_t requested, const char *msg)
5084 const char *opt_desc[] = {
5085 "-h", gettext("show this help message and exit"),
5086 "-l", gettext("set permission locally"),
5087 "-d", gettext("set permission for descents"),
5088 "-u", gettext("set permission for user"),
5089 "-g", gettext("set permission for group"),
5090 "-e", gettext("set permission for everyone"),
5091 "-c", gettext("set create time permission"),
5092 "-s", gettext("define permission set"),
5094 "-r", gettext("remove permissions recursively"),
5096 size_t unallow_size = sizeof (opt_desc) / sizeof (char *);
5097 size_t allow_size = unallow_size - 2;
5098 const char *props[ZFS_NUM_PROPS];
5101 FILE *fp = requested ? stdout : stderr;
5102 zprop_desc_t *pdtbl = zfs_prop_get_table();
5103 const char *fmt = gettext("%-16s %-14s\t%s\n");
5105 (void) fprintf(fp, gettext("Usage: %s\n"), get_usage(un ? HELP_UNALLOW :
5107 (void) fprintf(fp, gettext("Options:\n"));
5108 for (i = 0; i < (un ? unallow_size : allow_size); i += 2) {
5109 const char *opt = opt_desc[i];
5110 const char *optdsc = opt_desc[i + 1];
5111 (void) fprintf(fp, gettext(" %-10s %s\n"), opt, optdsc);
5114 (void) fprintf(fp, gettext("\nThe following permissions are "
5116 (void) fprintf(fp, fmt, gettext("NAME"), gettext("TYPE"),
5118 for (i = 0; i < ZFS_NUM_DELEG_NOTES; i++) {
5119 const char *perm_name = zfs_deleg_perm_tbl[i].z_perm;
5120 zfs_deleg_note_t perm_note = zfs_deleg_perm_tbl[i].z_note;
5121 const char *perm_type = deleg_perm_type(perm_note);
5122 const char *perm_comment = deleg_perm_comment(perm_note);
5123 (void) fprintf(fp, fmt, perm_name, perm_type, perm_comment);
5126 for (i = 0; i < ZFS_NUM_PROPS; i++) {
5127 zprop_desc_t *pd = &pdtbl[i];
5128 if (pd->pd_visible != B_TRUE)
5131 if (pd->pd_attr == PROP_READONLY)
5134 props[count++] = pd->pd_name;
5136 props[count] = NULL;
5138 qsort(props, count, sizeof (char *), prop_cmp);
5140 for (i = 0; i < count; i++)
5141 (void) fprintf(fp, fmt, props[i], gettext("property"), "");
5144 (void) fprintf(fp, gettext("\nzfs: error: %s"), msg);
5146 exit(requested ? 0 : 2);
5149 static inline const char *
5150 munge_args(int argc, char **argv, boolean_t un, size_t expected_argc,
5153 if (un && argc == expected_argc - 1)
5155 else if (argc == expected_argc)
5156 *permsp = argv[argc - 2];
5158 allow_usage(un, B_FALSE,
5159 gettext("wrong number of parameters\n"));
5161 return (argv[argc - 1]);
5165 parse_allow_args(int argc, char **argv, boolean_t un, struct allow_opts *opts)
5167 int uge_sum = opts->user + opts->group + opts->everyone;
5168 int csuge_sum = opts->create + opts->set + uge_sum;
5169 int ldcsuge_sum = csuge_sum + opts->local + opts->descend;
5170 int all_sum = un ? ldcsuge_sum + opts->recursive : ldcsuge_sum;
5173 allow_usage(un, B_FALSE,
5174 gettext("-u, -g, and -e are mutually exclusive\n"));
5176 if (opts->prt_usage) {
5177 if (argc == 0 && all_sum == 0)
5178 allow_usage(un, B_TRUE, NULL);
5185 allow_usage(un, B_FALSE,
5186 gettext("invalid options combined with -s\n"));
5188 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5189 if (argv[0][0] != '@')
5190 allow_usage(un, B_FALSE,
5191 gettext("invalid set name: missing '@' prefix\n"));
5192 opts->who = argv[0];
5193 } else if (opts->create) {
5194 if (ldcsuge_sum > 1)
5195 allow_usage(un, B_FALSE,
5196 gettext("invalid options combined with -c\n"));
5197 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5198 } else if (opts->everyone) {
5200 allow_usage(un, B_FALSE,
5201 gettext("invalid options combined with -e\n"));
5202 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5203 } else if (uge_sum == 0 && argc > 0 && strcmp(argv[0], "everyone")
5205 opts->everyone = B_TRUE;
5208 opts->dataset = munge_args(argc, argv, un, 2, &opts->perms);
5209 } else if (argc == 1 && !un) {
5210 opts->prt_perms = B_TRUE;
5211 opts->dataset = argv[argc-1];
5213 opts->dataset = munge_args(argc, argv, un, 3, &opts->perms);
5214 opts->who = argv[0];
5217 if (!opts->local && !opts->descend) {
5218 opts->local = B_TRUE;
5219 opts->descend = B_TRUE;
5224 store_allow_perm(zfs_deleg_who_type_t type, boolean_t local, boolean_t descend,
5225 const char *who, char *perms, nvlist_t *top_nvl)
5228 char ld[2] = { '\0', '\0' };
5229 char who_buf[MAXNAMELEN + 32];
5230 char base_type = '\0';
5231 char set_type = '\0';
5232 nvlist_t *base_nvl = NULL;
5233 nvlist_t *set_nvl = NULL;
5236 if (nvlist_alloc(&base_nvl, NV_UNIQUE_NAME, 0) != 0)
5238 if (nvlist_alloc(&set_nvl, NV_UNIQUE_NAME, 0) != 0)
5242 case ZFS_DELEG_NAMED_SET_SETS:
5243 case ZFS_DELEG_NAMED_SET:
5244 set_type = ZFS_DELEG_NAMED_SET_SETS;
5245 base_type = ZFS_DELEG_NAMED_SET;
5246 ld[0] = ZFS_DELEG_NA;
5248 case ZFS_DELEG_CREATE_SETS:
5249 case ZFS_DELEG_CREATE:
5250 set_type = ZFS_DELEG_CREATE_SETS;
5251 base_type = ZFS_DELEG_CREATE;
5252 ld[0] = ZFS_DELEG_NA;
5254 case ZFS_DELEG_USER_SETS:
5255 case ZFS_DELEG_USER:
5256 set_type = ZFS_DELEG_USER_SETS;
5257 base_type = ZFS_DELEG_USER;
5259 ld[0] = ZFS_DELEG_LOCAL;
5261 ld[1] = ZFS_DELEG_DESCENDENT;
5263 case ZFS_DELEG_GROUP_SETS:
5264 case ZFS_DELEG_GROUP:
5265 set_type = ZFS_DELEG_GROUP_SETS;
5266 base_type = ZFS_DELEG_GROUP;
5268 ld[0] = ZFS_DELEG_LOCAL;
5270 ld[1] = ZFS_DELEG_DESCENDENT;
5272 case ZFS_DELEG_EVERYONE_SETS:
5273 case ZFS_DELEG_EVERYONE:
5274 set_type = ZFS_DELEG_EVERYONE_SETS;
5275 base_type = ZFS_DELEG_EVERYONE;
5277 ld[0] = ZFS_DELEG_LOCAL;
5279 ld[1] = ZFS_DELEG_DESCENDENT;
5283 assert(set_type != '\0' && base_type != '\0');
5286 if (perms != NULL) {
5288 char *end = curr + strlen(perms);
5290 while (curr < end) {
5291 char *delim = strchr(curr, ',');
5302 (void) nvlist_add_boolean(nvl, curr);
5308 for (i = 0; i < 2; i++) {
5309 char locality = ld[i];
5313 if (!nvlist_empty(base_nvl)) {
5315 (void) snprintf(who_buf,
5316 sizeof (who_buf), "%c%c$%s",
5317 base_type, locality, who);
5319 (void) snprintf(who_buf,
5320 sizeof (who_buf), "%c%c$",
5321 base_type, locality);
5323 (void) nvlist_add_nvlist(top_nvl, who_buf,
5328 if (!nvlist_empty(set_nvl)) {
5330 (void) snprintf(who_buf,
5331 sizeof (who_buf), "%c%c$%s",
5332 set_type, locality, who);
5334 (void) snprintf(who_buf,
5335 sizeof (who_buf), "%c%c$",
5336 set_type, locality);
5338 (void) nvlist_add_nvlist(top_nvl, who_buf,
5343 for (i = 0; i < 2; i++) {
5344 char locality = ld[i];
5349 (void) snprintf(who_buf, sizeof (who_buf),
5350 "%c%c$%s", base_type, locality, who);
5352 (void) snprintf(who_buf, sizeof (who_buf),
5353 "%c%c$", base_type, locality);
5354 (void) nvlist_add_boolean(top_nvl, who_buf);
5357 (void) snprintf(who_buf, sizeof (who_buf),
5358 "%c%c$%s", set_type, locality, who);
5360 (void) snprintf(who_buf, sizeof (who_buf),
5361 "%c%c$", set_type, locality);
5362 (void) nvlist_add_boolean(top_nvl, who_buf);
5368 construct_fsacl_list(boolean_t un, struct allow_opts *opts, nvlist_t **nvlp)
5370 if (nvlist_alloc(nvlp, NV_UNIQUE_NAME, 0) != 0)
5374 store_allow_perm(ZFS_DELEG_NAMED_SET, opts->local,
5375 opts->descend, opts->who, opts->perms, *nvlp);
5376 } else if (opts->create) {
5377 store_allow_perm(ZFS_DELEG_CREATE, opts->local,
5378 opts->descend, NULL, opts->perms, *nvlp);
5379 } else if (opts->everyone) {
5380 store_allow_perm(ZFS_DELEG_EVERYONE, opts->local,
5381 opts->descend, NULL, opts->perms, *nvlp);
5383 char *curr = opts->who;
5384 char *end = curr + strlen(curr);
5386 while (curr < end) {
5388 zfs_deleg_who_type_t who_type = ZFS_DELEG_WHO_UNKNOWN;
5390 char *delim = strchr(curr, ',');
5393 struct passwd *p = NULL;
5394 struct group *g = NULL;
5402 rid = (uid_t)strtol(curr, &endch, 0);
5404 who_type = ZFS_DELEG_USER;
5413 (void) snprintf(errbuf, 256, gettext(
5414 "invalid user %s"), curr);
5415 allow_usage(un, B_TRUE, errbuf);
5417 } else if (opts->group) {
5418 who_type = ZFS_DELEG_GROUP;
5427 (void) snprintf(errbuf, 256, gettext(
5428 "invalid group %s"), curr);
5429 allow_usage(un, B_TRUE, errbuf);
5432 if (*endch != '\0') {
5439 if (*endch != '\0') {
5447 who_type = ZFS_DELEG_USER;
5449 } else if (g != NULL) {
5450 who_type = ZFS_DELEG_GROUP;
5453 (void) snprintf(errbuf, 256, gettext(
5454 "invalid user/group %s"), curr);
5455 allow_usage(un, B_TRUE, errbuf);
5459 (void) sprintf(id, "%u", rid);
5462 store_allow_perm(who_type, opts->local,
5463 opts->descend, who, opts->perms, *nvlp);
5472 print_set_creat_perms(uu_avl_t *who_avl)
5474 const char *sc_title[] = {
5475 gettext("Permission sets:\n"),
5476 gettext("Create time permissions:\n"),
5479 const char **title_ptr = sc_title;
5480 who_perm_node_t *who_node = NULL;
5481 int prev_weight = -1;
5483 for (who_node = uu_avl_first(who_avl); who_node != NULL;
5484 who_node = uu_avl_next(who_avl, who_node)) {
5485 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5486 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5487 const char *who_name = who_node->who_perm.who_name;
5488 int weight = who_type2weight(who_type);
5489 boolean_t first = B_TRUE;
5490 deleg_perm_node_t *deleg_node;
5492 if (prev_weight != weight) {
5493 (void) printf("%s", *title_ptr++);
5494 prev_weight = weight;
5497 if (who_name == NULL || strnlen(who_name, 1) == 0)
5498 (void) printf("\t");
5500 (void) printf("\t%s ", who_name);
5502 for (deleg_node = uu_avl_first(avl); deleg_node != NULL;
5503 deleg_node = uu_avl_next(avl, deleg_node)) {
5506 deleg_node->dpn_perm.dp_name);
5509 (void) printf(",%s",
5510 deleg_node->dpn_perm.dp_name);
5513 (void) printf("\n");
5518 print_uge_deleg_perms(uu_avl_t *who_avl, boolean_t local, boolean_t descend,
5521 who_perm_node_t *who_node = NULL;
5522 boolean_t prt_title = B_TRUE;
5523 uu_avl_walk_t *walk;
5525 if ((walk = uu_avl_walk_start(who_avl, UU_WALK_ROBUST)) == NULL)
5528 while ((who_node = uu_avl_walk_next(walk)) != NULL) {
5529 const char *who_name = who_node->who_perm.who_name;
5530 const char *nice_who_name = who_node->who_perm.who_ug_name;
5531 uu_avl_t *avl = who_node->who_perm.who_deleg_perm_avl;
5532 zfs_deleg_who_type_t who_type = who_node->who_perm.who_type;
5534 deleg_perm_node_t *deleg_node;
5535 boolean_t prt_who = B_TRUE;
5537 for (deleg_node = uu_avl_first(avl);
5539 deleg_node = uu_avl_next(avl, deleg_node)) {
5540 if (local != deleg_node->dpn_perm.dp_local ||
5541 descend != deleg_node->dpn_perm.dp_descend)
5545 const char *who = NULL;
5547 prt_title = B_FALSE;
5548 (void) printf("%s", title);
5552 case ZFS_DELEG_USER_SETS:
5553 case ZFS_DELEG_USER:
5554 who = gettext("user");
5556 who_name = nice_who_name;
5558 case ZFS_DELEG_GROUP_SETS:
5559 case ZFS_DELEG_GROUP:
5560 who = gettext("group");
5562 who_name = nice_who_name;
5564 case ZFS_DELEG_EVERYONE_SETS:
5565 case ZFS_DELEG_EVERYONE:
5566 who = gettext("everyone");
5571 assert(who != NULL);
5575 if (who_name == NULL)
5576 (void) printf("\t%s", who);
5578 (void) printf("\t%s %s", who, who_name);
5581 (void) printf("%c%s", delim,
5582 deleg_node->dpn_perm.dp_name);
5587 (void) printf("\n");
5590 uu_avl_walk_end(walk);
5594 print_fs_perms(fs_perm_set_t *fspset)
5596 fs_perm_node_t *node = NULL;
5597 char buf[MAXNAMELEN + 32];
5598 const char *dsname = buf;
5600 for (node = uu_list_first(fspset->fsps_list); node != NULL;
5601 node = uu_list_next(fspset->fsps_list, node)) {
5602 uu_avl_t *sc_avl = node->fspn_fsperm.fsp_sc_avl;
5603 uu_avl_t *uge_avl = node->fspn_fsperm.fsp_uge_avl;
5606 (void) snprintf(buf, sizeof (buf),
5607 gettext("---- Permissions on %s "),
5608 node->fspn_fsperm.fsp_name);
5609 (void) printf("%s", dsname);
5610 left = 70 - strlen(buf);
5613 (void) printf("\n");
5615 print_set_creat_perms(sc_avl);
5616 print_uge_deleg_perms(uge_avl, B_TRUE, B_FALSE,
5617 gettext("Local permissions:\n"));
5618 print_uge_deleg_perms(uge_avl, B_FALSE, B_TRUE,
5619 gettext("Descendent permissions:\n"));
5620 print_uge_deleg_perms(uge_avl, B_TRUE, B_TRUE,
5621 gettext("Local+Descendent permissions:\n"));
5625 static fs_perm_set_t fs_perm_set = { NULL, NULL, NULL, NULL };
5627 struct deleg_perms {
5633 set_deleg_perms(zfs_handle_t *zhp, void *data)
5635 struct deleg_perms *perms = (struct deleg_perms *)data;
5636 zfs_type_t zfs_type = zfs_get_type(zhp);
5638 if (zfs_type != ZFS_TYPE_FILESYSTEM && zfs_type != ZFS_TYPE_VOLUME)
5641 return (zfs_set_fsacl(zhp, perms->un, perms->nvl));
5645 zfs_do_allow_unallow_impl(int argc, char **argv, boolean_t un)
5648 nvlist_t *perm_nvl = NULL;
5649 nvlist_t *update_perm_nvl = NULL;
5652 struct allow_opts opts = { 0 };
5654 const char *optstr = un ? "ldugecsrh" : "ldugecsh";
5657 while ((c = getopt(argc, argv, optstr)) != -1) {
5660 opts.local = B_TRUE;
5663 opts.descend = B_TRUE;
5669 opts.group = B_TRUE;
5672 opts.everyone = B_TRUE;
5678 opts.create = B_TRUE;
5681 opts.recursive = B_TRUE;
5684 (void) fprintf(stderr, gettext("missing argument for "
5685 "'%c' option\n"), optopt);
5689 opts.prt_usage = B_TRUE;
5692 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5701 /* check arguments */
5702 parse_allow_args(argc, argv, un, &opts);
5704 /* try to open the dataset */
5705 if ((zhp = zfs_open(g_zfs, opts.dataset, ZFS_TYPE_FILESYSTEM |
5706 ZFS_TYPE_VOLUME)) == NULL) {
5707 (void) fprintf(stderr, "Failed to open dataset: %s\n",
5712 if (zfs_get_fsacl(zhp, &perm_nvl) != 0)
5715 fs_perm_set_init(&fs_perm_set);
5716 if (parse_fs_perm_set(&fs_perm_set, perm_nvl) != 0) {
5717 (void) fprintf(stderr, "Failed to parse fsacl permissions\n");
5722 print_fs_perms(&fs_perm_set);
5724 (void) construct_fsacl_list(un, &opts, &update_perm_nvl);
5725 if (zfs_set_fsacl(zhp, un, update_perm_nvl) != 0)
5728 if (un && opts.recursive) {
5729 struct deleg_perms data = { un, update_perm_nvl };
5730 if (zfs_iter_filesystems(zhp, set_deleg_perms,
5739 nvlist_free(perm_nvl);
5740 nvlist_free(update_perm_nvl);
5742 fs_perm_set_fini(&fs_perm_set);
5750 zfs_do_allow(int argc, char **argv)
5752 return (zfs_do_allow_unallow_impl(argc, argv, B_FALSE));
5756 zfs_do_unallow(int argc, char **argv)
5758 return (zfs_do_allow_unallow_impl(argc, argv, B_TRUE));
5762 zfs_do_hold_rele_impl(int argc, char **argv, boolean_t holding)
5767 boolean_t recursive = B_FALSE;
5768 const char *opts = holding ? "rt" : "r";
5772 while ((c = getopt(argc, argv, opts)) != -1) {
5778 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
5787 /* check number of arguments */
5795 if (holding && tag[0] == '.') {
5796 /* tags starting with '.' are reserved for libzfs */
5797 (void) fprintf(stderr, gettext("tag may not start with '.'\n"));
5801 for (i = 0; i < argc; ++i) {
5803 char parent[ZFS_MAX_DATASET_NAME_LEN];
5805 char *path = argv[i];
5807 delim = strchr(path, '@');
5808 if (delim == NULL) {
5809 (void) fprintf(stderr,
5810 gettext("'%s' is not a snapshot\n"), path);
5814 (void) strncpy(parent, path, delim - path);
5815 parent[delim - path] = '\0';
5817 zhp = zfs_open(g_zfs, parent,
5818 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5824 if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
5827 if (zfs_release(zhp, delim+1, tag, recursive) != 0)
5833 return (errors != 0);
5837 * zfs hold [-r] [-t] <tag> <snap> ...
5839 * -r Recursively hold
5841 * Apply a user-hold with the given tag to the list of snapshots.
5844 zfs_do_hold(int argc, char **argv)
5846 return (zfs_do_hold_rele_impl(argc, argv, B_TRUE));
5850 * zfs release [-r] <tag> <snap> ...
5852 * -r Recursively release
5854 * Release a user-hold with the given tag from the list of snapshots.
5857 zfs_do_release(int argc, char **argv)
5859 return (zfs_do_hold_rele_impl(argc, argv, B_FALSE));
5862 typedef struct holds_cbdata {
5863 boolean_t cb_recursive;
5864 const char *cb_snapname;
5866 size_t cb_max_namelen;
5867 size_t cb_max_taglen;
5870 #define STRFTIME_FMT_STR "%a %b %e %k:%M %Y"
5871 #define DATETIME_BUF_LEN (32)
5876 print_holds(boolean_t scripted, int nwidth, int tagwidth, nvlist_t *nvl)
5879 nvpair_t *nvp = NULL;
5880 char *hdr_cols[] = { "NAME", "TAG", "TIMESTAMP" };
5884 for (i = 0; i < 3; i++) {
5885 col = gettext(hdr_cols[i]);
5887 (void) printf("%-*s ", i ? tagwidth : nwidth,
5890 (void) printf("%s\n", col);
5894 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5895 char *zname = nvpair_name(nvp);
5897 nvpair_t *nvp2 = NULL;
5898 (void) nvpair_value_nvlist(nvp, &nvl2);
5899 while ((nvp2 = nvlist_next_nvpair(nvl2, nvp2)) != NULL) {
5900 char tsbuf[DATETIME_BUF_LEN];
5901 char *tagname = nvpair_name(nvp2);
5906 (void) nvpair_value_uint64(nvp2, &val);
5908 (void) localtime_r(&time, &t);
5909 (void) strftime(tsbuf, DATETIME_BUF_LEN,
5910 gettext(STRFTIME_FMT_STR), &t);
5913 (void) printf("%s\t%s\t%s\n", zname,
5916 (void) printf("%-*s %-*s %s\n", nwidth,
5917 zname, tagwidth, tagname, tsbuf);
5924 * Generic callback function to list a dataset or snapshot.
5927 holds_callback(zfs_handle_t *zhp, void *data)
5929 holds_cbdata_t *cbp = data;
5930 nvlist_t *top_nvl = *cbp->cb_nvlp;
5931 nvlist_t *nvl = NULL;
5932 nvpair_t *nvp = NULL;
5933 const char *zname = zfs_get_name(zhp);
5934 size_t znamelen = strlen(zname);
5936 if (cbp->cb_recursive) {
5937 const char *snapname;
5938 char *delim = strchr(zname, '@');
5942 snapname = delim + 1;
5943 if (strcmp(cbp->cb_snapname, snapname))
5947 if (zfs_get_holds(zhp, &nvl) != 0)
5950 if (znamelen > cbp->cb_max_namelen)
5951 cbp->cb_max_namelen = znamelen;
5953 while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
5954 const char *tag = nvpair_name(nvp);
5955 size_t taglen = strlen(tag);
5956 if (taglen > cbp->cb_max_taglen)
5957 cbp->cb_max_taglen = taglen;
5960 return (nvlist_add_nvlist(top_nvl, zname, nvl));
5964 * zfs holds [-r] <snap> ...
5966 * -r Recursively hold
5969 zfs_do_holds(int argc, char **argv)
5974 boolean_t scripted = B_FALSE;
5975 boolean_t recursive = B_FALSE;
5976 const char *opts = "rH";
5979 int types = ZFS_TYPE_SNAPSHOT;
5980 holds_cbdata_t cb = { 0 };
5987 while ((c = getopt(argc, argv, opts)) != -1) {
5996 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6003 types |= ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME;
6004 flags |= ZFS_ITER_RECURSE;
6010 /* check number of arguments */
6014 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
6017 for (i = 0; i < argc; ++i) {
6018 char *snapshot = argv[i];
6020 const char *snapname;
6022 delim = strchr(snapshot, '@');
6023 if (delim == NULL) {
6024 (void) fprintf(stderr,
6025 gettext("'%s' is not a snapshot\n"), snapshot);
6029 snapname = delim + 1;
6031 snapshot[delim - snapshot] = '\0';
6033 cb.cb_recursive = recursive;
6034 cb.cb_snapname = snapname;
6038 * 1. collect holds data, set format options
6040 ret = zfs_for_each(argc, argv, flags, types, NULL, NULL, limit,
6041 holds_callback, &cb);
6047 * 2. print holds data
6049 print_holds(scripted, cb.cb_max_namelen, cb.cb_max_taglen, nvl);
6051 if (nvlist_empty(nvl))
6052 (void) fprintf(stderr, gettext("no datasets available\n"));
6056 return (0 != errors);
6059 #define CHECK_SPINNER 30
6060 #define SPINNER_TIME 3 /* seconds */
6061 #define MOUNT_TIME 5 /* seconds */
6064 get_one_dataset(zfs_handle_t *zhp, void *data)
6066 static char *spin[] = { "-", "\\", "|", "/" };
6067 static int spinval = 0;
6068 static int spincheck = 0;
6069 static time_t last_spin_time = (time_t)0;
6070 get_all_cb_t *cbp = data;
6071 zfs_type_t type = zfs_get_type(zhp);
6073 if (cbp->cb_verbose) {
6074 if (--spincheck < 0) {
6075 time_t now = time(NULL);
6076 if (last_spin_time + SPINNER_TIME < now) {
6077 update_progress(spin[spinval++ % 4]);
6078 last_spin_time = now;
6080 spincheck = CHECK_SPINNER;
6085 * Iterate over any nested datasets.
6087 if (zfs_iter_filesystems(zhp, get_one_dataset, data) != 0) {
6093 * Skip any datasets whose type does not match.
6095 if ((type & ZFS_TYPE_FILESYSTEM) == 0) {
6099 libzfs_add_handle(cbp, zhp);
6100 assert(cbp->cb_used <= cbp->cb_alloc);
6106 get_all_datasets(zfs_handle_t ***dslist, size_t *count, boolean_t verbose)
6108 get_all_cb_t cb = { 0 };
6109 cb.cb_verbose = verbose;
6110 cb.cb_getone = get_one_dataset;
6113 set_progress_header(gettext("Reading ZFS config"));
6114 (void) zfs_iter_root(g_zfs, get_one_dataset, &cb);
6116 *dslist = cb.cb_handles;
6117 *count = cb.cb_used;
6120 finish_progress(gettext("done."));
6124 * Generic callback for sharing or mounting filesystems. Because the code is so
6125 * similar, we have a common function with an extra parameter to determine which
6126 * mode we are using.
6128 #define OP_SHARE 0x1
6129 #define OP_MOUNT 0x2
6132 * Share or mount a dataset.
6135 share_mount_one(zfs_handle_t *zhp, int op, int flags, char *protocol,
6136 boolean_t explicit, const char *options)
6138 char mountpoint[ZFS_MAXPROPLEN];
6139 char shareopts[ZFS_MAXPROPLEN];
6140 char smbshareopts[ZFS_MAXPROPLEN];
6141 const char *cmdname = op == OP_SHARE ? "share" : "mount";
6143 uint64_t zoned, canmount;
6144 boolean_t shared_nfs, shared_smb;
6146 assert(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM);
6149 * Check to make sure we can mount/share this dataset. If we
6150 * are in the global zone and the filesystem is exported to a
6151 * local zone, or if we are in a local zone and the
6152 * filesystem is not exported, then it is an error.
6154 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
6156 if (zoned && getzoneid() == GLOBAL_ZONEID) {
6160 (void) fprintf(stderr, gettext("cannot %s '%s': "
6161 "dataset is exported to a local zone\n"), cmdname,
6165 } else if (!zoned && getzoneid() != GLOBAL_ZONEID) {
6169 (void) fprintf(stderr, gettext("cannot %s '%s': "
6170 "permission denied\n"), cmdname,
6176 * Ignore any filesystems which don't apply to us. This
6177 * includes those with a legacy mountpoint, or those with
6178 * legacy share options.
6180 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mountpoint,
6181 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
6182 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts,
6183 sizeof (shareopts), NULL, NULL, 0, B_FALSE) == 0);
6184 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshareopts,
6185 sizeof (smbshareopts), NULL, NULL, 0, B_FALSE) == 0);
6187 if (op == OP_SHARE && strcmp(shareopts, "off") == 0 &&
6188 strcmp(smbshareopts, "off") == 0) {
6192 (void) fprintf(stderr, gettext("cannot share '%s': "
6193 "legacy share\n"), zfs_get_name(zhp));
6194 (void) fprintf(stderr, gettext("use share(1M) to "
6195 "share this filesystem, or set "
6196 "sharenfs property on\n"));
6201 * We cannot share or mount legacy filesystems. If the
6202 * shareopts is non-legacy but the mountpoint is legacy, we
6203 * treat it as a legacy share.
6205 if (strcmp(mountpoint, "legacy") == 0) {
6209 (void) fprintf(stderr, gettext("cannot %s '%s': "
6210 "legacy mountpoint\n"), cmdname, zfs_get_name(zhp));
6211 (void) fprintf(stderr, gettext("use %s(1M) to "
6212 "%s this filesystem\n"), cmdname, cmdname);
6216 if (strcmp(mountpoint, "none") == 0) {
6220 (void) fprintf(stderr, gettext("cannot %s '%s': no "
6221 "mountpoint set\n"), cmdname, zfs_get_name(zhp));
6226 * canmount explicit outcome
6227 * on no pass through
6228 * on yes pass through
6230 * off yes display error, return 1
6231 * noauto no return 0
6232 * noauto yes pass through
6234 canmount = zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT);
6235 if (canmount == ZFS_CANMOUNT_OFF) {
6239 (void) fprintf(stderr, gettext("cannot %s '%s': "
6240 "'canmount' property is set to 'off'\n"), cmdname,
6243 } else if (canmount == ZFS_CANMOUNT_NOAUTO && !explicit) {
6248 * If this filesystem is encrypted and does not have
6249 * a loaded key, we can not mount it.
6251 if ((flags & MS_CRYPT) == 0 &&
6252 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF &&
6253 zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS) ==
6254 ZFS_KEYSTATUS_UNAVAILABLE) {
6258 (void) fprintf(stderr, gettext("cannot %s '%s': "
6259 "encryption key not loaded\n"), cmdname, zfs_get_name(zhp));
6264 * If this filesystem is inconsistent and has a receive resume
6265 * token, we can not mount it.
6267 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
6268 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
6269 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
6273 (void) fprintf(stderr, gettext("cannot %s '%s': "
6274 "Contains partially-completed state from "
6275 "\"zfs receive -s\", which can be resumed with "
6276 "\"zfs send -t\"\n"),
6277 cmdname, zfs_get_name(zhp));
6282 * At this point, we have verified that the mountpoint and/or
6283 * shareopts are appropriate for auto management. If the
6284 * filesystem is already mounted or shared, return (failing
6285 * for explicit requests); otherwise mount or share the
6291 shared_nfs = zfs_is_shared_nfs(zhp, NULL);
6292 shared_smb = zfs_is_shared_smb(zhp, NULL);
6294 if ((shared_nfs && shared_smb) ||
6295 (shared_nfs && strcmp(shareopts, "on") == 0 &&
6296 strcmp(smbshareopts, "off") == 0) ||
6297 (shared_smb && strcmp(smbshareopts, "on") == 0 &&
6298 strcmp(shareopts, "off") == 0)) {
6302 (void) fprintf(stderr, gettext("cannot share "
6303 "'%s': filesystem already shared\n"),
6308 if (!zfs_is_mounted(zhp, NULL) &&
6309 zfs_mount(zhp, NULL, flags) != 0)
6312 if (protocol == NULL) {
6313 if (zfs_shareall(zhp) != 0)
6315 } else if (strcmp(protocol, "nfs") == 0) {
6316 if (zfs_share_nfs(zhp))
6318 } else if (strcmp(protocol, "smb") == 0) {
6319 if (zfs_share_smb(zhp))
6322 (void) fprintf(stderr, gettext("cannot share "
6323 "'%s': invalid share type '%s' "
6325 zfs_get_name(zhp), protocol);
6332 if (options == NULL)
6333 mnt.mnt_mntopts = "";
6335 mnt.mnt_mntopts = (char *)options;
6337 if (!hasmntopt(&mnt, MNTOPT_REMOUNT) &&
6338 zfs_is_mounted(zhp, NULL)) {
6342 (void) fprintf(stderr, gettext("cannot mount "
6343 "'%s': filesystem already mounted\n"),
6348 if (zfs_mount(zhp, options, flags) != 0)
6357 * Reports progress in the form "(current/total)". Not thread-safe.
6360 report_mount_progress(int current, int total)
6362 static time_t last_progress_time = 0;
6363 time_t now = time(NULL);
6366 /* report 1..n instead of 0..n-1 */
6369 /* display header if we're here for the first time */
6371 set_progress_header(gettext("Mounting ZFS filesystems"));
6372 } else if (current != total && last_progress_time + MOUNT_TIME >= now) {
6373 /* too soon to report again */
6377 last_progress_time = now;
6379 (void) sprintf(info, "(%d/%d)", current, total);
6381 if (current == total)
6382 finish_progress(info);
6384 update_progress(info);
6388 append_options(char *mntopts, char *newopts)
6390 int len = strlen(mntopts);
6392 /* original length plus new string to append plus 1 for the comma */
6393 if (len + 1 + strlen(newopts) >= MNT_LINE_MAX) {
6394 (void) fprintf(stderr, gettext("the opts argument for "
6395 "'%s' option is too long (more than %d chars)\n"),
6396 "-o", MNT_LINE_MAX);
6401 mntopts[len++] = ',';
6403 (void) strcpy(&mntopts[len], newopts);
6407 share_mount(int op, int argc, char **argv)
6410 boolean_t verbose = B_FALSE;
6412 char *options = NULL;
6416 while ((c = getopt(argc, argv, op == OP_MOUNT ? ":alvo:O" : "al"))
6429 if (*optarg == '\0') {
6430 (void) fprintf(stderr, gettext("empty mount "
6431 "options (-o) specified\n"));
6435 if (options == NULL)
6436 options = safe_malloc(MNT_LINE_MAX + 1);
6438 /* option validation is done later */
6439 append_options(options, optarg);
6442 flags |= MS_OVERLAY;
6445 (void) fprintf(stderr, gettext("missing argument for "
6446 "'%c' option\n"), optopt);
6450 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6459 /* check number of arguments */
6461 zfs_handle_t **dslist = NULL;
6462 size_t i, count = 0;
6463 char *protocol = NULL;
6465 if (op == OP_SHARE && argc > 0) {
6466 if (strcmp(argv[0], "nfs") != 0 &&
6467 strcmp(argv[0], "smb") != 0) {
6468 (void) fprintf(stderr, gettext("share type "
6469 "must be 'nfs' or 'smb'\n"));
6478 (void) fprintf(stderr, gettext("too many arguments\n"));
6482 start_progress_timer();
6483 get_all_datasets(&dslist, &count, verbose);
6486 if (options != NULL)
6491 qsort(dslist, count, sizeof (void *), libzfs_dataset_cmp);
6493 for (i = 0; i < count; i++) {
6495 report_mount_progress(i, count);
6497 if (share_mount_one(dslist[i], op, flags, protocol,
6498 B_FALSE, options) != 0)
6500 zfs_close(dslist[i]);
6504 } else if (argc == 0) {
6505 struct mnttab entry;
6507 if ((op == OP_SHARE) || (options != NULL)) {
6508 (void) fprintf(stderr, gettext("missing filesystem "
6509 "argument (specify -a for all)\n"));
6514 * When mount is given no arguments, go through
6515 * /proc/self/mounts and display any active ZFS mounts.
6516 * We hide any snapshots, since they are controlled
6520 /* Reopen MNTTAB to prevent reading stale data from open file */
6521 if (freopen(MNTTAB, "r", mnttab_file) == NULL) {
6522 if (options != NULL)
6527 while (getmntent(mnttab_file, &entry) == 0) {
6528 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0 ||
6529 strchr(entry.mnt_special, '@') != NULL)
6532 (void) printf("%-30s %s\n", entry.mnt_special,
6540 (void) fprintf(stderr,
6541 gettext("too many arguments\n"));
6545 if ((zhp = zfs_open(g_zfs, argv[0],
6546 ZFS_TYPE_FILESYSTEM)) == NULL) {
6549 ret = share_mount_one(zhp, op, flags, NULL, B_TRUE,
6555 if (options != NULL)
6562 * zfs mount -a [nfs]
6563 * zfs mount filesystem
6565 * Mount all filesystems, or mount the given filesystem.
6568 zfs_do_mount(int argc, char **argv)
6570 return (share_mount(OP_MOUNT, argc, argv));
6574 * zfs share -a [nfs | smb]
6575 * zfs share filesystem
6577 * Share all filesystems, or share the given filesystem.
6580 zfs_do_share(int argc, char **argv)
6582 return (share_mount(OP_SHARE, argc, argv));
6585 typedef struct unshare_unmount_node {
6586 zfs_handle_t *un_zhp;
6588 uu_avl_node_t un_avlnode;
6589 } unshare_unmount_node_t;
6593 unshare_unmount_compare(const void *larg, const void *rarg, void *unused)
6595 const unshare_unmount_node_t *l = larg;
6596 const unshare_unmount_node_t *r = rarg;
6598 return (strcmp(l->un_mountp, r->un_mountp));
6602 * Convenience routine used by zfs_do_umount() and manual_unmount(). Given an
6603 * absolute path, find the entry /proc/self/mounts, verify that its a
6604 * ZFS filesystems, and unmount it appropriately.
6607 unshare_unmount_path(int op, char *path, int flags, boolean_t is_manual)
6611 struct stat64 statbuf;
6612 struct extmnttab entry;
6613 const char *cmdname = (op == OP_SHARE) ? "unshare" : "unmount";
6617 * Search for the path in /proc/self/mounts. Rather than looking for the
6618 * specific path, which can be fooled by non-standard paths (i.e. ".."
6619 * or "//"), we stat() the path and search for the corresponding
6620 * (major,minor) device pair.
6622 if (stat64(path, &statbuf) != 0) {
6623 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6624 cmdname, path, strerror(errno));
6627 path_inode = statbuf.st_ino;
6630 * Search for the given (major,minor) pair in the mount table.
6633 /* Reopen MNTTAB to prevent reading stale data from open file */
6634 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6637 while ((ret = getextmntent(mnttab_file, &entry, 0)) == 0) {
6638 if (entry.mnt_major == major(statbuf.st_dev) &&
6639 entry.mnt_minor == minor(statbuf.st_dev))
6643 if (op == OP_SHARE) {
6644 (void) fprintf(stderr, gettext("cannot %s '%s': not "
6645 "currently mounted\n"), cmdname, path);
6648 (void) fprintf(stderr, gettext("warning: %s not in"
6649 "/proc/self/mounts\n"), path);
6650 if ((ret = umount2(path, flags)) != 0)
6651 (void) fprintf(stderr, gettext("%s: %s\n"), path,
6656 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) {
6657 (void) fprintf(stderr, gettext("cannot %s '%s': not a ZFS "
6658 "filesystem\n"), cmdname, path);
6662 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6663 ZFS_TYPE_FILESYSTEM)) == NULL)
6667 if (stat64(entry.mnt_mountp, &statbuf) != 0) {
6668 (void) fprintf(stderr, gettext("cannot %s '%s': %s\n"),
6669 cmdname, path, strerror(errno));
6671 } else if (statbuf.st_ino != path_inode) {
6672 (void) fprintf(stderr, gettext("cannot "
6673 "%s '%s': not a mountpoint\n"), cmdname, path);
6677 if (op == OP_SHARE) {
6678 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6679 char smbshare_prop[ZFS_MAXPROPLEN];
6681 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS, nfs_mnt_prop,
6682 sizeof (nfs_mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
6683 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB, smbshare_prop,
6684 sizeof (smbshare_prop), NULL, NULL, 0, B_FALSE) == 0);
6686 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6687 strcmp(smbshare_prop, "off") == 0) {
6688 (void) fprintf(stderr, gettext("cannot unshare "
6689 "'%s': legacy share\n"), path);
6690 (void) fprintf(stderr, gettext("use exportfs(8) "
6691 "or smbcontrol(1) to unshare this filesystem\n"));
6692 } else if (!zfs_is_shared(zhp)) {
6693 (void) fprintf(stderr, gettext("cannot unshare '%s': "
6694 "not currently shared\n"), path);
6696 ret = zfs_unshareall_bypath(zhp, path);
6699 char mtpt_prop[ZFS_MAXPROPLEN];
6701 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, mtpt_prop,
6702 sizeof (mtpt_prop), NULL, NULL, 0, B_FALSE) == 0);
6705 ret = zfs_unmount(zhp, NULL, flags);
6706 } else if (strcmp(mtpt_prop, "legacy") == 0) {
6707 (void) fprintf(stderr, gettext("cannot unmount "
6708 "'%s': legacy mountpoint\n"),
6710 (void) fprintf(stderr, gettext("use umount(8) "
6711 "to unmount this filesystem\n"));
6713 ret = zfs_unmountall(zhp, flags);
6724 * Generic callback for unsharing or unmounting a filesystem.
6727 unshare_unmount(int op, int argc, char **argv)
6734 char nfs_mnt_prop[ZFS_MAXPROPLEN];
6735 char sharesmb[ZFS_MAXPROPLEN];
6738 while ((c = getopt(argc, argv, op == OP_SHARE ? ":a" : "af")) != -1) {
6747 (void) fprintf(stderr, gettext("missing argument for "
6748 "'%c' option\n"), optopt);
6752 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
6763 * We could make use of zfs_for_each() to walk all datasets in
6764 * the system, but this would be very inefficient, especially
6765 * since we would have to linearly search /proc/self/mounts for
6766 * each one. Instead, do one pass through /proc/self/mounts
6767 * looking for zfs entries and call zfs_unmount() for each one.
6769 * Things get a little tricky if the administrator has created
6770 * mountpoints beneath other ZFS filesystems. In this case, we
6771 * have to unmount the deepest filesystems first. To accomplish
6772 * this, we place all the mountpoints in an AVL tree sorted by
6773 * the special type (dataset name), and walk the result in
6774 * reverse to make sure to get any snapshots first.
6776 struct mnttab entry;
6777 uu_avl_pool_t *pool;
6778 uu_avl_t *tree = NULL;
6779 unshare_unmount_node_t *node;
6781 uu_avl_walk_t *walk;
6782 char *protocol = NULL;
6784 if (op == OP_SHARE && argc > 0) {
6785 if (strcmp(argv[0], "nfs") != 0 &&
6786 strcmp(argv[0], "smb") != 0) {
6787 (void) fprintf(stderr, gettext("share type "
6788 "must be 'nfs' or 'smb'\n"));
6797 (void) fprintf(stderr, gettext("too many arguments\n"));
6801 if (((pool = uu_avl_pool_create("unmount_pool",
6802 sizeof (unshare_unmount_node_t),
6803 offsetof(unshare_unmount_node_t, un_avlnode),
6804 unshare_unmount_compare, UU_DEFAULT)) == NULL) ||
6805 ((tree = uu_avl_create(pool, NULL, UU_DEFAULT)) == NULL))
6808 /* Reopen MNTTAB to prevent reading stale data from open file */
6809 if (freopen(MNTTAB, "r", mnttab_file) == NULL)
6812 while (getmntent(mnttab_file, &entry) == 0) {
6814 /* ignore non-ZFS entries */
6815 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
6818 /* ignore snapshots */
6819 if (strchr(entry.mnt_special, '@') != NULL)
6822 if ((zhp = zfs_open(g_zfs, entry.mnt_special,
6823 ZFS_TYPE_FILESYSTEM)) == NULL) {
6829 * Ignore datasets that are excluded/restricted by
6832 if (zpool_skip_pool(zfs_get_pool_name(zhp))) {
6839 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6841 sizeof (nfs_mnt_prop),
6842 NULL, NULL, 0, B_FALSE) == 0);
6843 if (strcmp(nfs_mnt_prop, "off") != 0)
6845 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6847 sizeof (nfs_mnt_prop),
6848 NULL, NULL, 0, B_FALSE) == 0);
6849 if (strcmp(nfs_mnt_prop, "off") == 0)
6853 /* Ignore legacy mounts */
6854 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT,
6856 sizeof (nfs_mnt_prop),
6857 NULL, NULL, 0, B_FALSE) == 0);
6858 if (strcmp(nfs_mnt_prop, "legacy") == 0)
6860 /* Ignore canmount=noauto mounts */
6861 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) ==
6862 ZFS_CANMOUNT_NOAUTO)
6868 node = safe_malloc(sizeof (unshare_unmount_node_t));
6870 node->un_mountp = safe_strdup(entry.mnt_mountp);
6872 uu_avl_node_init(node, &node->un_avlnode, pool);
6874 if (uu_avl_find(tree, node, NULL, &idx) == NULL) {
6875 uu_avl_insert(tree, node, idx);
6877 zfs_close(node->un_zhp);
6878 free(node->un_mountp);
6884 * Walk the AVL tree in reverse, unmounting each filesystem and
6885 * removing it from the AVL tree in the process.
6887 if ((walk = uu_avl_walk_start(tree,
6888 UU_WALK_REVERSE | UU_WALK_ROBUST)) == NULL)
6891 while ((node = uu_avl_walk_next(walk)) != NULL) {
6892 uu_avl_remove(tree, node);
6896 if (zfs_unshareall_bytype(node->un_zhp,
6897 node->un_mountp, protocol) != 0)
6902 if (zfs_unmount(node->un_zhp,
6903 node->un_zhp->zfs_name, flags) != 0)
6908 zfs_close(node->un_zhp);
6909 free(node->un_mountp);
6913 uu_avl_walk_end(walk);
6914 uu_avl_destroy(tree);
6915 uu_avl_pool_destroy(pool);
6920 (void) fprintf(stderr,
6921 gettext("missing filesystem argument\n"));
6923 (void) fprintf(stderr,
6924 gettext("too many arguments\n"));
6929 * We have an argument, but it may be a full path or a ZFS
6930 * filesystem. Pass full paths off to unmount_path() (shared by
6931 * manual_unmount), otherwise open the filesystem and pass to
6934 if (argv[0][0] == '/')
6935 return (unshare_unmount_path(op, argv[0],
6938 if ((zhp = zfs_open(g_zfs, argv[0],
6939 ZFS_TYPE_FILESYSTEM)) == NULL)
6942 verify(zfs_prop_get(zhp, op == OP_SHARE ?
6943 ZFS_PROP_SHARENFS : ZFS_PROP_MOUNTPOINT,
6944 nfs_mnt_prop, sizeof (nfs_mnt_prop), NULL,
6945 NULL, 0, B_FALSE) == 0);
6949 verify(zfs_prop_get(zhp, ZFS_PROP_SHARENFS,
6951 sizeof (nfs_mnt_prop),
6952 NULL, NULL, 0, B_FALSE) == 0);
6953 verify(zfs_prop_get(zhp, ZFS_PROP_SHARESMB,
6954 sharesmb, sizeof (sharesmb), NULL, NULL,
6957 if (strcmp(nfs_mnt_prop, "off") == 0 &&
6958 strcmp(sharesmb, "off") == 0) {
6959 (void) fprintf(stderr, gettext("cannot "
6960 "unshare '%s': legacy share\n"),
6962 (void) fprintf(stderr, gettext("use "
6963 "unshare(1M) to unshare this "
6966 } else if (!zfs_is_shared(zhp)) {
6967 (void) fprintf(stderr, gettext("cannot "
6968 "unshare '%s': not currently "
6969 "shared\n"), zfs_get_name(zhp));
6971 } else if (zfs_unshareall(zhp) != 0) {
6977 if (strcmp(nfs_mnt_prop, "legacy") == 0) {
6978 (void) fprintf(stderr, gettext("cannot "
6979 "unmount '%s': legacy "
6980 "mountpoint\n"), zfs_get_name(zhp));
6981 (void) fprintf(stderr, gettext("use "
6982 "umount(1M) to unmount this "
6985 } else if (!zfs_is_mounted(zhp, NULL)) {
6986 (void) fprintf(stderr, gettext("cannot "
6987 "unmount '%s': not currently "
6991 } else if (zfs_unmountall(zhp, flags) != 0) {
7005 * zfs unmount filesystem
7007 * Unmount all filesystems, or a specific ZFS filesystem.
7010 zfs_do_unmount(int argc, char **argv)
7012 return (unshare_unmount(OP_MOUNT, argc, argv));
7017 * zfs unshare filesystem
7019 * Unshare all filesystems, or a specific ZFS filesystem.
7022 zfs_do_unshare(int argc, char **argv)
7024 return (unshare_unmount(OP_SHARE, argc, argv));
7028 find_command_idx(char *command, int *idx)
7032 for (i = 0; i < NCOMMAND; i++) {
7033 if (command_table[i].name == NULL)
7036 if (strcmp(command, command_table[i].name) == 0) {
7045 zfs_do_diff(int argc, char **argv)
7049 char *tosnap = NULL;
7050 char *fromsnap = NULL;
7055 while ((c = getopt(argc, argv, "FHt")) != -1) {
7058 flags |= ZFS_DIFF_CLASSIFY;
7061 flags |= ZFS_DIFF_PARSEABLE;
7064 flags |= ZFS_DIFF_TIMESTAMP;
7067 (void) fprintf(stderr,
7068 gettext("invalid option '%c'\n"), optopt);
7077 (void) fprintf(stderr,
7078 gettext("must provide at least one snapshot name\n"));
7083 (void) fprintf(stderr, gettext("too many arguments\n"));
7088 tosnap = (argc == 2) ? argv[1] : NULL;
7091 if (*fromsnap != '@')
7092 copy = strdup(fromsnap);
7094 copy = strdup(tosnap);
7098 if ((atp = strchr(copy, '@')) != NULL)
7101 if ((zhp = zfs_open(g_zfs, copy, ZFS_TYPE_FILESYSTEM)) == NULL) {
7108 * Ignore SIGPIPE so that the library can give us
7109 * information on any failure
7111 (void) sigignore(SIGPIPE);
7113 err = zfs_show_diffs(zhp, STDOUT_FILENO, fromsnap, tosnap, flags);
7122 * zfs remap <filesystem | volume>
7124 * Remap the indirect blocks in the given fileystem or volume.
7127 zfs_do_remap(int argc, char **argv)
7134 while ((c = getopt(argc, argv, "")) != -1) {
7137 (void) fprintf(stderr,
7138 gettext("invalid option '%c'\n"), optopt);
7144 (void) fprintf(stderr, gettext("wrong number of arguments\n"));
7149 err = zfs_remap_indirects(g_zfs, fsname);
7155 * zfs bookmark <fs@snap> <fs#bmark>
7157 * Creates a bookmark with the given name from the given snapshot.
7160 zfs_do_bookmark(int argc, char **argv)
7162 char snapname[ZFS_MAX_DATASET_NAME_LEN];
7163 char bookname[ZFS_MAX_DATASET_NAME_LEN];
7170 while ((c = getopt(argc, argv, "")) != -1) {
7173 (void) fprintf(stderr,
7174 gettext("invalid option '%c'\n"), optopt);
7182 /* check number of arguments */
7184 (void) fprintf(stderr, gettext("missing snapshot argument\n"));
7188 (void) fprintf(stderr, gettext("missing bookmark argument\n"));
7192 if (strchr(argv[0], '@') == NULL) {
7193 (void) fprintf(stderr,
7194 gettext("invalid snapshot name '%s': "
7195 "must contain a '@'\n"), argv[0]);
7198 if (strchr(argv[1], '#') == NULL) {
7199 (void) fprintf(stderr,
7200 gettext("invalid bookmark name '%s': "
7201 "must contain a '#'\n"), argv[1]);
7205 if (argv[0][0] == '@') {
7207 * Snapshot name begins with @.
7208 * Default to same fs as bookmark.
7210 (void) strlcpy(snapname, argv[1], sizeof (snapname));
7211 *strchr(snapname, '#') = '\0';
7212 (void) strlcat(snapname, argv[0], sizeof (snapname));
7214 (void) strlcpy(snapname, argv[0], sizeof (snapname));
7216 if (argv[1][0] == '#') {
7218 * Bookmark name begins with #.
7219 * Default to same fs as snapshot.
7221 (void) strlcpy(bookname, argv[0], sizeof (bookname));
7222 *strchr(bookname, '@') = '\0';
7223 (void) strlcat(bookname, argv[1], sizeof (bookname));
7225 (void) strlcpy(bookname, argv[1], sizeof (bookname));
7228 zhp = zfs_open(g_zfs, snapname, ZFS_TYPE_SNAPSHOT);
7234 nvl = fnvlist_alloc();
7235 fnvlist_add_string(nvl, bookname, snapname);
7236 ret = lzc_bookmark(nvl, NULL);
7240 const char *err_msg;
7243 (void) snprintf(errbuf, sizeof (errbuf),
7244 dgettext(TEXT_DOMAIN,
7245 "cannot create bookmark '%s'"), bookname);
7249 err_msg = "bookmark is in a different pool";
7252 err_msg = "bookmark exists";
7255 err_msg = "invalid argument";
7258 err_msg = "bookmark feature not enabled";
7261 err_msg = "out of space";
7264 err_msg = "dataset does not exist";
7267 err_msg = "unknown error";
7270 (void) fprintf(stderr, "%s: %s\n", errbuf,
7271 dgettext(TEXT_DOMAIN, err_msg));
7282 zfs_do_channel_program(int argc, char **argv)
7285 char *progbuf, *filename, *poolname;
7286 size_t progsize, progread;
7288 uint64_t instrlimit = ZCP_DEFAULT_INSTRLIMIT;
7289 uint64_t memlimit = ZCP_DEFAULT_MEMLIMIT;
7290 boolean_t sync_flag = B_TRUE, json_output = B_FALSE;
7291 zpool_handle_t *zhp;
7294 while ((c = getopt(argc, argv, "nt:m:j")) != -1) {
7302 arg = strtoull(optarg, &endp, 0);
7303 if (errno != 0 || *endp != '\0') {
7304 (void) fprintf(stderr, gettext(
7306 "'%s': expected integer\n"), optarg);
7313 ASSERT3U(c, ==, 'm');
7319 sync_flag = B_FALSE;
7323 json_output = B_TRUE;
7327 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7337 (void) fprintf(stderr,
7338 gettext("invalid number of arguments\n"));
7344 if (strcmp(filename, "-") == 0) {
7346 filename = "standard input";
7347 } else if ((fd = open(filename, O_RDONLY)) < 0) {
7348 (void) fprintf(stderr, gettext("cannot open '%s': %s\n"),
7349 filename, strerror(errno));
7353 if ((zhp = zpool_open(g_zfs, poolname)) == NULL) {
7354 (void) fprintf(stderr, gettext("cannot open pool '%s'"),
7363 * Read in the channel program, expanding the program buffer as
7368 progbuf = safe_malloc(progsize);
7370 ret = read(fd, progbuf + progread, progsize - progread);
7372 if (progread == progsize && ret > 0) {
7374 progbuf = safe_realloc(progbuf, progsize);
7382 (void) fprintf(stderr,
7383 gettext("cannot read '%s': %s\n"),
7384 filename, strerror(errno));
7387 progbuf[progread] = '\0';
7390 * Any remaining arguments are passed as arguments to the lua script as
7393 * "argv" -> [ "arg 1", ... "arg n" ],
7396 nvlist_t *argnvl = fnvlist_alloc();
7397 fnvlist_add_string_array(argnvl, ZCP_ARG_CLIARGV, argv + 2, argc - 2);
7400 ret = lzc_channel_program(poolname, progbuf,
7401 instrlimit, memlimit, argnvl, &outnvl);
7403 ret = lzc_channel_program_nosync(poolname, progbuf,
7404 instrlimit, memlimit, argnvl, &outnvl);
7409 * On error, report the error message handed back by lua if one
7410 * exists. Otherwise, generate an appropriate error message,
7411 * falling back on strerror() for an unexpected return code.
7413 char *errstring = NULL;
7414 uint64_t instructions = 0;
7415 if (nvlist_exists(outnvl, ZCP_RET_ERROR)) {
7416 (void) nvlist_lookup_string(outnvl,
7417 ZCP_RET_ERROR, &errstring);
7418 if (errstring == NULL)
7419 errstring = strerror(ret);
7421 (void) nvlist_lookup_uint64(outnvl,
7422 ZCP_ARG_INSTRLIMIT, &instructions);
7428 "Invalid instruction or memory limit.";
7431 errstring = "Return value too large.";
7434 errstring = "Memory limit exhausted.";
7437 errstring = "Timed out.";
7440 errstring = "Permission denied. Channel "
7441 "programs must be run as root.";
7444 errstring = strerror(ret);
7447 (void) fprintf(stderr,
7448 gettext("Channel program execution failed:\n%s\n"),
7450 if (ret == ETIME && instructions != 0)
7451 (void) fprintf(stderr,
7452 gettext("%llu Lua instructions\n"),
7453 (u_longlong_t)instructions);
7456 (void) nvlist_print_json(stdout, outnvl);
7457 } else if (nvlist_empty(outnvl)) {
7458 (void) fprintf(stdout, gettext("Channel program fully "
7459 "executed and did not produce output.\n"));
7461 (void) fprintf(stdout, gettext("Channel program fully "
7462 "executed and produced output:\n"));
7463 dump_nvlist(outnvl, 4);
7468 fnvlist_free(outnvl);
7469 fnvlist_free(argnvl);
7478 typedef struct loadkey_cbdata {
7479 boolean_t cb_loadkey;
7480 boolean_t cb_recursive;
7482 char *cb_keylocation;
7483 uint64_t cb_numfailed;
7484 uint64_t cb_numattempted;
7488 load_key_callback(zfs_handle_t *zhp, void *data)
7491 boolean_t is_encroot;
7492 loadkey_cbdata_t *cb = data;
7493 uint64_t keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
7496 * If we are working recursively, we want to skip loading / unloading
7497 * keys for non-encryption roots and datasets whose keys are already
7498 * in the desired end-state.
7500 if (cb->cb_recursive) {
7501 ret = zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
7507 if ((cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_AVAILABLE) ||
7508 (!cb->cb_loadkey && keystatus == ZFS_KEYSTATUS_UNAVAILABLE))
7512 cb->cb_numattempted++;
7515 ret = zfs_crypto_load_key(zhp, cb->cb_noop, cb->cb_keylocation);
7517 ret = zfs_crypto_unload_key(zhp);
7528 load_unload_keys(int argc, char **argv, boolean_t loadkey)
7530 int c, ret = 0, flags = 0;
7531 boolean_t do_all = B_FALSE;
7532 loadkey_cbdata_t cb = { 0 };
7534 cb.cb_loadkey = loadkey;
7536 while ((c = getopt(argc, argv, "anrL:")) != -1) {
7537 /* noop and alternate keylocations only apply to zfs load-key */
7541 cb.cb_noop = B_TRUE;
7544 cb.cb_keylocation = optarg;
7554 cb.cb_recursive = B_TRUE;
7557 flags |= ZFS_ITER_RECURSE;
7558 cb.cb_recursive = B_TRUE;
7561 (void) fprintf(stderr,
7562 gettext("invalid option '%c'\n"), optopt);
7570 if (!do_all && argc == 0) {
7571 (void) fprintf(stderr,
7572 gettext("Missing dataset argument or -a option\n"));
7576 if (do_all && argc != 0) {
7577 (void) fprintf(stderr,
7578 gettext("Cannot specify dataset with -a option\n"));
7582 if (cb.cb_recursive && cb.cb_keylocation != NULL &&
7583 strcmp(cb.cb_keylocation, "prompt") != 0) {
7584 (void) fprintf(stderr, gettext("alternate keylocation may only "
7585 "be 'prompt' with -r or -a\n"));
7589 ret = zfs_for_each(argc, argv, flags,
7590 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME, NULL, NULL, 0,
7591 load_key_callback, &cb);
7593 if (cb.cb_noop || (cb.cb_recursive && cb.cb_numattempted != 0)) {
7594 (void) printf(gettext("%llu / %llu key(s) successfully %s\n"),
7595 (u_longlong_t)(cb.cb_numattempted - cb.cb_numfailed),
7596 (u_longlong_t)cb.cb_numattempted,
7597 loadkey ? (cb.cb_noop ? "verified" : "loaded") :
7601 if (cb.cb_numfailed != 0)
7608 zfs_do_load_key(int argc, char **argv)
7610 return (load_unload_keys(argc, argv, B_TRUE));
7615 zfs_do_unload_key(int argc, char **argv)
7617 return (load_unload_keys(argc, argv, B_FALSE));
7621 zfs_do_change_key(int argc, char **argv)
7625 boolean_t loadkey = B_FALSE, inheritkey = B_FALSE;
7626 zfs_handle_t *zhp = NULL;
7627 nvlist_t *props = fnvlist_alloc();
7629 while ((c = getopt(argc, argv, "lio:")) != -1) {
7635 inheritkey = B_TRUE;
7638 if (!parseprop(props, optarg)) {
7644 (void) fprintf(stderr,
7645 gettext("invalid option '%c'\n"), optopt);
7650 if (inheritkey && !nvlist_empty(props)) {
7651 (void) fprintf(stderr,
7652 gettext("Properties not allowed for inheriting\n"));
7660 (void) fprintf(stderr, gettext("Missing dataset argument\n"));
7665 (void) fprintf(stderr, gettext("Too many arguments\n"));
7669 zhp = zfs_open(g_zfs, argv[argc - 1],
7670 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
7675 keystatus = zfs_prop_get_int(zhp, ZFS_PROP_KEYSTATUS);
7676 if (keystatus != ZFS_KEYSTATUS_AVAILABLE) {
7677 ret = zfs_crypto_load_key(zhp, B_FALSE, NULL);
7685 /* refresh the properties so the new keystatus is visible */
7686 zfs_refresh_properties(zhp);
7689 ret = zfs_crypto_rewrap(zhp, props, inheritkey);
7702 * 1) zfs project [-d|-r] <file|directory ...>
7703 * List project ID and inherit flag of file(s) or directories.
7704 * -d: List the directory itself, not its children.
7705 * -r: List subdirectories recursively.
7707 * 2) zfs project -C [-k] [-r] <file|directory ...>
7708 * Clear project inherit flag and/or ID on the file(s) or directories.
7709 * -k: Keep the project ID unchanged. If not specified, the project ID
7710 * will be reset as zero.
7711 * -r: Clear on subdirectories recursively.
7713 * 3) zfs project -c [-0] [-d|-r] [-p id] <file|directory ...>
7714 * Check project ID and inherit flag on the file(s) or directories,
7715 * report the outliers.
7716 * -0: Print file name followed by a NUL instead of newline.
7717 * -d: Check the directory itself, not its children.
7718 * -p: Specify the referenced ID for comparing with the target file(s)
7719 * or directories' project IDs. If not specified, the target (top)
7720 * directory's project ID will be used as the referenced one.
7721 * -r: Check subdirectories recursively.
7723 * 4) zfs project [-p id] [-r] [-s] <file|directory ...>
7724 * Set project ID and/or inherit flag on the file(s) or directories.
7725 * -p: Set the project ID as the given id.
7726 * -r: Set on subdirectorie recursively. If not specify "-p" option,
7727 * it will use top-level directory's project ID as the given id,
7728 * then set both project ID and inherit flag on all descendants
7729 * of the top-level directory.
7730 * -s: Set project inherit flag.
7733 zfs_do_project(int argc, char **argv)
7735 zfs_project_control_t zpc = {
7736 .zpc_expected_projid = ZFS_INVALID_PROJID,
7737 .zpc_op = ZFS_PROJECT_OP_DEFAULT,
7738 .zpc_dironly = B_FALSE,
7739 .zpc_keep_projid = B_FALSE,
7740 .zpc_newline = B_TRUE,
7741 .zpc_recursive = B_FALSE,
7742 .zpc_set_flag = B_FALSE,
7749 while ((c = getopt(argc, argv, "0Ccdkp:rs")) != -1) {
7752 zpc.zpc_newline = B_FALSE;
7755 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
7756 (void) fprintf(stderr, gettext("cannot "
7757 "specify '-C' '-c' '-s' together\n"));
7761 zpc.zpc_op = ZFS_PROJECT_OP_CLEAR;
7764 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
7765 (void) fprintf(stderr, gettext("cannot "
7766 "specify '-C' '-c' '-s' together\n"));
7770 zpc.zpc_op = ZFS_PROJECT_OP_CHECK;
7773 zpc.zpc_dironly = B_TRUE;
7774 /* overwrite "-r" option */
7775 zpc.zpc_recursive = B_FALSE;
7778 zpc.zpc_keep_projid = B_TRUE;
7784 zpc.zpc_expected_projid = strtoull(optarg, &endptr, 0);
7785 if (errno != 0 || *endptr != '\0') {
7786 (void) fprintf(stderr,
7787 gettext("project ID must be less than "
7788 "%u\n"), UINT32_MAX);
7791 if (zpc.zpc_expected_projid >= UINT32_MAX) {
7792 (void) fprintf(stderr,
7793 gettext("invalid project ID\n"));
7799 zpc.zpc_recursive = B_TRUE;
7800 /* overwrite "-d" option */
7801 zpc.zpc_dironly = B_FALSE;
7804 if (zpc.zpc_op != ZFS_PROJECT_OP_DEFAULT) {
7805 (void) fprintf(stderr, gettext("cannot "
7806 "specify '-C' '-c' '-s' together\n"));
7810 zpc.zpc_set_flag = B_TRUE;
7811 zpc.zpc_op = ZFS_PROJECT_OP_SET;
7814 (void) fprintf(stderr, gettext("invalid option '%c'\n"),
7820 if (zpc.zpc_op == ZFS_PROJECT_OP_DEFAULT) {
7821 if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID)
7822 zpc.zpc_op = ZFS_PROJECT_OP_SET;
7824 zpc.zpc_op = ZFS_PROJECT_OP_LIST;
7827 switch (zpc.zpc_op) {
7828 case ZFS_PROJECT_OP_LIST:
7829 if (zpc.zpc_keep_projid) {
7830 (void) fprintf(stderr,
7831 gettext("'-k' is only valid together with '-C'\n"));
7834 if (!zpc.zpc_newline) {
7835 (void) fprintf(stderr,
7836 gettext("'-0' is only valid together with '-c'\n"));
7840 case ZFS_PROJECT_OP_CHECK:
7841 if (zpc.zpc_keep_projid) {
7842 (void) fprintf(stderr,
7843 gettext("'-k' is only valid together with '-C'\n"));
7847 case ZFS_PROJECT_OP_CLEAR:
7848 if (zpc.zpc_dironly) {
7849 (void) fprintf(stderr,
7850 gettext("'-d' is useless together with '-C'\n"));
7853 if (!zpc.zpc_newline) {
7854 (void) fprintf(stderr,
7855 gettext("'-0' is only valid together with '-c'\n"));
7858 if (zpc.zpc_expected_projid != ZFS_INVALID_PROJID) {
7859 (void) fprintf(stderr,
7860 gettext("'-p' is useless together with '-C'\n"));
7864 case ZFS_PROJECT_OP_SET:
7865 if (zpc.zpc_dironly) {
7866 (void) fprintf(stderr,
7867 gettext("'-d' is useless for set project ID and/or "
7871 if (zpc.zpc_keep_projid) {
7872 (void) fprintf(stderr,
7873 gettext("'-k' is only valid together with '-C'\n"));
7876 if (!zpc.zpc_newline) {
7877 (void) fprintf(stderr,
7878 gettext("'-0' is only valid together with '-c'\n"));
7890 (void) fprintf(stderr,
7891 gettext("missing file or directory target(s)\n"));
7895 for (int i = 0; i < argc; i++) {
7898 err = zfs_project_handle(argv[i], &zpc);
7907 main(int argc, char **argv)
7913 (void) setlocale(LC_ALL, "");
7914 (void) textdomain(TEXT_DOMAIN);
7919 * Make sure the user has specified some command.
7922 (void) fprintf(stderr, gettext("missing command\n"));
7929 * The 'umount' command is an alias for 'unmount'
7931 if (strcmp(cmdname, "umount") == 0)
7932 cmdname = "unmount";
7935 * The 'recv' command is an alias for 'receive'
7937 if (strcmp(cmdname, "recv") == 0)
7938 cmdname = "receive";
7941 * The 'snap' command is an alias for 'snapshot'
7943 if (strcmp(cmdname, "snap") == 0)
7944 cmdname = "snapshot";
7949 if ((strcmp(cmdname, "-?") == 0) ||
7950 (strcmp(cmdname, "--help") == 0))
7953 if ((g_zfs = libzfs_init()) == NULL) {
7954 (void) fprintf(stderr, "%s", libzfs_error_init(errno));
7958 mnttab_file = g_zfs->libzfs_mnttab;
7960 zfs_save_arguments(argc, argv, history_str, sizeof (history_str));
7962 libzfs_print_on_error(g_zfs, B_TRUE);
7965 * Run the appropriate command.
7967 libzfs_mnttab_cache(g_zfs, B_TRUE);
7968 if (find_command_idx(cmdname, &i) == 0) {
7969 current_command = &command_table[i];
7970 ret = command_table[i].func(argc - 1, argv + 1);
7971 } else if (strchr(cmdname, '=') != NULL) {
7972 verify(find_command_idx("set", &i) == 0);
7973 current_command = &command_table[i];
7974 ret = command_table[i].func(argc, argv);
7976 (void) fprintf(stderr, gettext("unrecognized "
7977 "command '%s'\n"), cmdname);
7982 if (ret == 0 && log_history)
7983 (void) zpool_log_history(g_zfs, history_str);
7988 * The 'ZFS_ABORT' environment variable causes us to dump core on exit
7989 * for the purposes of running ::findleaks.
7991 if (getenv("ZFS_ABORT") != NULL) {
7992 (void) printf("dumping core by request\n");